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

100% Statements 12/12
89.47% Branches 17/19
100% Functions 2/2
100% Lines 11/11

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 33 34 35 36          1x 820x 1x   819x   819x             481x   481x 62x     419x 419x       419x            
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 range = core.domHelper.getSelectionRange();
 
    if (!range || !core.logicalRoot.contains(range.commonAncestorContainer)) {
        return null;
    }
 
    const selection = core.logicalRoot.ownerDocument.defaultView?.getSelection();
    const isReverted = selection
        ? selection.focusNode != range.endContainer || selection.focusOffset != range.endOffset
        : false;
 
    return {
        type: 'range',
        range,
        isReverted,
    };
}