All files / roosterjs-content-model-core/lib/corePlugin/entity adjustSelectionAroundEntity.ts

100% Statements 51/51
91.21% Branches 83/91
100% Functions 4/4
100% Lines 48/48

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 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 1581x                                 1x         25x   25x 3x     44x 22x 22x 22x       22x   22x   22x           13x 13x         13x 13x   13x 13x 13x 13x       13x             13x 10x               10x               13x                       10x   10x 4x 2x   2x     6x 3x   3x   6x 4x 2x   2x         10x                 13x   13x   8x     5x 5x   5x   5x 2x       13x           13x          
import {
    getSelectedSegmentsAndParagraphs,
    isBlockEntityContainer,
    isEntityDelimiter,
    isNodeOfType,
} from 'roosterjs-content-model-dom';
import type {
    ContentModelEntity,
    IEditor,
    ReadonlyContentModelBlockGroup,
    ReadonlyContentModelParagraph,
    ReadonlyContentModelSegment,
} from 'roosterjs-content-model-types';
 
/**
 * @internal
 */
export function adjustSelectionAroundEntity(
    editor: IEditor,
    key: 'ArrowLeft' | 'ArrowRight',
    shiftKey: boolean
) {
    const selection = editor.isDisposed() ? null : editor.getDOMSelection();
 
    if (!selection || selection.type != 'range') {
        return;
    }
 
    const { range, isReverted } = selection;
    const anchorNode = isReverted ? range.startContainer : range.endContainer;
    const offset = isReverted ? range.startOffset : range.endOffset;
    const delimiter = isNodeOfType(anchorNode, 'ELEMENT_NODE')
        ? anchorNode
        : anchorNode.parentElement;
    const isRtl =
        delimiter &&
        editor.getDocument().defaultView?.getComputedStyle(delimiter).direction == 'rtl';
    const movingBefore = (key == 'ArrowLeft') != !!isRtl;
 
    if (
        delimiter &&
        ((isEntityDelimiter(delimiter, !movingBefore) &&
            ((movingBefore && offset == 0) || (!movingBefore && offset == 1))) ||
            isBlockEntityContainer(delimiter))
    ) {
        editor.formatContentModel(model => {
            const allSel = getSelectedSegmentsAndParagraphs(
                model,
                false /*includingFormatHolder*/,
                true /*includingEntity*/
            );
            const sel = allSel[isReverted ? 0 : allSel.length - 1];
            const index = sel?.[1]?.segments.indexOf(sel[0]) ?? -1;
 
            Eif (sel && sel[1] && index >= 0) {
                const [segment, paragraph, path] = sel;
                const isShrinking = shiftKey && !range.collapsed && movingBefore != !!isReverted;
                const entitySegment = isShrinking
                    ? segment
                    : paragraph.segments[movingBefore ? index - 1 : index + 1];
 
                const pairedDelimiter = findPairedDelimiter(
                    entitySegment,
                    path,
                    paragraph,
                    movingBefore
                );
 
                if (pairedDelimiter) {
                    const newRange = getNewRange(
                        range,
                        isShrinking,
                        movingBefore,
                        pairedDelimiter,
                        shiftKey
                    );
 
                    editor.setDOMSelection({
                        type: 'range',
                        range: newRange,
                        isReverted: newRange.collapsed ? false : isReverted,
                    });
                }
            }
 
            return false;
        });
    }
}
 
function getNewRange(
    originalRange: Range,
    isShrinking: boolean,
    movingBefore: boolean,
    pairedDelimiter: HTMLElement,
    shiftKey: boolean
) {
    const newRange = originalRange.cloneRange();
 
    if (isShrinking) {
        if (movingBefore) {
            newRange.setEndBefore(pairedDelimiter);
        } else {
            newRange.setStartAfter(pairedDelimiter);
        }
    } else {
        if (movingBefore) {
            newRange.setStartBefore(pairedDelimiter);
        } else {
            newRange.setEndAfter(pairedDelimiter);
        }
        if (!shiftKey) {
            if (movingBefore) {
                newRange.setEndBefore(pairedDelimiter);
            } else {
                newRange.setStartAfter(pairedDelimiter);
            }
        }
    }
 
    return newRange;
}
 
function findPairedDelimiter(
    entitySegment: ReadonlyContentModelSegment,
    path: ReadonlyContentModelBlockGroup[],
    paragraph: ReadonlyContentModelParagraph,
    movingBefore: boolean
) {
    let entity: ContentModelEntity | null = null;
 
    if (entitySegment?.segmentType == 'Entity') {
        // Inline entity
        entity = entitySegment;
    } else {
        // Block entity
        const blocks = path[0].blocks;
        const paraIndex = blocks.indexOf(paragraph);
        const entityBlock =
            paraIndex >= 0 ? blocks[movingBefore ? paraIndex - 1 : paraIndex + 1] : null;
 
        if (entityBlock?.blockType == 'Entity') {
            entity = entityBlock;
        }
    }
 
    const pairedDelimiter = entity
        ? movingBefore
            ? entity.wrapper.previousElementSibling
            : entity.wrapper.nextElementSibling
        : null;
 
    return isNodeOfType(pairedDelimiter, 'ELEMENT_NODE') &&
        isEntityDelimiter(pairedDelimiter, movingBefore)
        ? pairedDelimiter
        : null;
}