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

100% Statements 8/8
100% Branches 12/12
100% Functions 1/1
100% Lines 7/7

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              1x 63x 186x   62x 40x       62x 60x        
import type { Focus } from 'roosterjs-content-model-types';
 
/**
 * @internal
 * Focus to editor. If there is a cached selection range, use it as current selection
 * @param core The EditorCore object
 */
export const focus: Focus = core => {
    if (!core.lifecycle.shadowEditFragment) {
        const { api, domHelper, selection } = core;
 
        if (!domHelper.hasFocus() && selection.selection?.type == 'range') {
            api.setDOMSelection(core, selection.selection, true /*skipSelectionChangedEvent*/);
        }
 
        // fallback, in case editor still have no focus
        if (!domHelper.hasFocus()) {
            core.logicalRoot.focus();
        }
    }
};