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

100% Statements 26/26
90.91% Branches 10/11
100% Functions 1/1
100% Lines 26/26

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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 671x 1x           1x 9x 9x 9x   9x   9x 9x   5x 5x 5x   5x 5x   5x         5x   2x       2x 1x                 2x   2x       2x 1x         2x       9x 7x        
import { getPositionFromPath } from './getPositionFromPath';
import { getSafeIdSelector } from 'roosterjs-content-model-dom';
import type { DOMSelection, EditorCore, Snapshot } from 'roosterjs-content-model-types';
 
/**
 * @internal
 */
export function restoreSnapshotSelection(core: EditorCore, snapshot: Snapshot) {
    const snapshotSelection = snapshot.selection;
    const { physicalRoot } = core;
    let domSelection: DOMSelection | null = null;
 
    try {
        // might fail if the selection is not present, but we do not want to crash
        Eif (snapshotSelection) {
            switch (snapshotSelection.type) {
                case 'range':
                    const startPos = getPositionFromPath(physicalRoot, snapshotSelection.start);
                    const endPos = getPositionFromPath(physicalRoot, snapshotSelection.end);
                    const range = physicalRoot.ownerDocument.createRange();
 
                    range.setStart(startPos.node, startPos.offset);
                    range.setEnd(endPos.node, endPos.offset);
 
                    domSelection = {
                        type: 'range',
                        range,
                        isReverted: snapshotSelection.isReverted,
                    };
                    break;
                case 'table':
                    const table = physicalRoot.querySelector(
                        getSafeIdSelector(snapshotSelection.tableId)
                    ) as HTMLTableElement;
 
                    if (table) {
                        domSelection = {
                            type: 'table',
                            table: table,
                            firstColumn: snapshotSelection.firstColumn,
                            firstRow: snapshotSelection.firstRow,
                            lastColumn: snapshotSelection.lastColumn,
                            lastRow: snapshotSelection.lastRow,
                        };
                    }
                    break;
                case 'image':
                    const image = physicalRoot.querySelector(
                        getSafeIdSelector(snapshotSelection.imageId)
                    ) as HTMLImageElement;
 
                    if (image) {
                        domSelection = {
                            type: 'image',
                            image: image,
                        };
                    }
                    break;
            }
        }
 
        if (domSelection) {
            core.api.setDOMSelection(core, domSelection);
        }
    } catch {}
}