All files / roosterjs-content-model-core/lib/coreApi/getDOMSelection getDOMSelection.ts

100% Statements 9/9
95.45% Branches 21/22
100% Functions 2/2
100% Lines 8/8

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32          1x 552x 1x   551x   551x             274x 274x   274x                    
import type { DOMSelection, GetDOMSelection, EditorCore } from 'roosterjs-content-model-types';
 
/**
 * @internal
 */
export const getDOMSelection: GetDOMSelection = core => {
    if (core.lifecycle.shadowEditFragment) {
        return null;
    } else {
        const selection = core.selection.selection;
 
        return selection && (selection.type != 'range' || !core.domHelper.hasFocus())
            ? selection
            : getNewSelection(core);
    }
};
 
function getNewSelection(core: EditorCore): DOMSelection | null {
    const selection = core.logicalRoot.ownerDocument.defaultView?.getSelection();
    const range = selection && selection.rangeCount > 0 ? selection.getRangeAt(0) : null;
 
    return selection && range && core.logicalRoot.contains(range.commonAncestorContainer)
        ? {
              type: 'range',
              range,
              isReverted:
                  selection.focusNode != range.endContainer ||
                  selection.focusOffset != range.endOffset,
          }
        : null;
}