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

100% Statements 15/15
100% Branches 8/8
100% Functions 1/1
100% Lines 14/14

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 37 38 39 40 41 42                1x   20x   18x 4x       18x   12x 12x     12x     12x 12x 12x     12x       12x     2x      
import type { LogicalRootChangedEvent, SetLogicalRoot } from 'roosterjs-content-model-types';
 
/**
 * @internal
 * Change which node is the current logical root
 * @param core The StandaloneEditorCore object
 * @param logicalRoot The new logical root (has to be child of physicalRoot), pass null to use physicalRoot as logical root
 */
export const setLogicalRoot: SetLogicalRoot = (core, logicalRoot) => {
    // make sure we either want to reset to physical root or the logical root is a child of physical root
    if (!logicalRoot || core.physicalRoot.contains(logicalRoot)) {
        // if null, reset to physical root
        if (!logicalRoot) {
            logicalRoot = core.physicalRoot;
        }
 
        // if the logical root changed
        if (logicalRoot !== core.logicalRoot) {
            // make sure the old logical root is not content editable and the new one is
            core.logicalRoot.contentEditable = 'false';
            logicalRoot.contentEditable = 'true';
 
            // update the logical root
            core.logicalRoot = logicalRoot;
 
            // clear internal caches
            core.selection.selection = null;
            core.cache.cachedModel = undefined;
            core.cache.cachedSelection = undefined;
 
            // tell plugins in case they need to clear their caches
            const event: LogicalRootChangedEvent = {
                eventType: 'logicalRootChanged',
                logicalRoot,
            };
            core.api.triggerEvent(core, event, false /*broadcast*/);
        }
    } else {
        return null;
    }
};