All files / roosterjs-content-model-api/lib/modelApi/selection adjustWordSelection.ts

95.38% Statements 62/65
84.44% Branches 38/45
100% Functions 4/4
95.08% Lines 58/61

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 1561x                                 1x           35x   35x 35x   35x     35x   35x 35x 35x 35x 33x 33x 33x 33x 8x 3x     5x   5x   5x   25x             35x 35x     35x 8x     27x 12x 12x 12x 12x 3x 1x   2x 2x 2x   9x               27x 17x     10x 10x                                     45x 45x 45x 12x 27x   27x 3x 3x       33x 106x   106x 8x 8x       45x                 7x 7x           7x           7x    
import {
    createText,
    isPunctuation,
    isSpace,
    iterateSelections,
    mutateBlock,
} from 'roosterjs-content-model-dom';
import type {
    ContentModelText,
    ReadonlyContentModelDocument,
    ShallowMutableContentModelParagraph,
    ShallowMutableContentModelSegment,
} from 'roosterjs-content-model-types';
 
/**
 * @internal
 */
export function adjustWordSelection(
    model: ReadonlyContentModelDocument,
    marker: ShallowMutableContentModelSegment
): ShallowMutableContentModelSegment[] {
    let markerBlock: ShallowMutableContentModelParagraph | undefined;
 
    iterateSelections(model, (_, __, block, segments) => {
        //Find the block with the selection marker
        Eif (block?.blockType == 'Paragraph' && segments?.length == 1 && segments[0] == marker) {
            markerBlock = mutateBlock(block);
        }
        return true;
    });
 
    const tempSegments = markerBlock ? [...markerBlock.segments] : undefined;
 
    Eif (tempSegments && markerBlock) {
        const segments: ShallowMutableContentModelSegment[] = [];
        let markerSelectionIndex = tempSegments.indexOf(marker);
        for (let i = markerSelectionIndex - 1; i >= 0; i--) {
            const currentSegment = tempSegments[i];
            Eif (currentSegment.segmentType == 'Text') {
                const found = findDelimiter(currentSegment, false /*moveRightward*/);
                if (found > -1) {
                    if (found == currentSegment.text.length) {
                        break;
                    }
 
                    splitTextSegment(tempSegments, currentSegment, i, found);
 
                    segments.push(tempSegments[i + 1]);
 
                    break;
                } else {
                    segments.push(tempSegments[i]);
                }
            } else {
                break;
            }
        }
 
        markerSelectionIndex = tempSegments.indexOf(marker);
        segments.push(marker);
 
        // Marker is at start of word
        if (segments.length <= 1) {
            return segments;
        }
 
        for (let i = markerSelectionIndex + 1; i < tempSegments.length; i++) {
            const currentSegment = tempSegments[i];
            Eif (currentSegment.segmentType == 'Text') {
                const found = findDelimiter(currentSegment, true /*moveRightward*/);
                if (found > -1) {
                    if (found == 0) {
                        break;
                    }
                    splitTextSegment(tempSegments, currentSegment, i, found);
                    segments.push(tempSegments[i]);
                    break;
                } else {
                    segments.push(tempSegments[i]);
                }
            } else {
                break;
            }
        }
 
        // Marker is at end of word
        if (segments[segments.length - 1] == marker) {
            return [marker];
        }
 
        markerBlock.segments = tempSegments;
        return segments;
    } else {
        return [marker];
    }
}
 
/*
// These are unicode characters mostly from the Category Space Separator (Zs)
https://unicode.org/Public/UNIDATA/Scripts.txt
 
\u2000 = EN QUAD
\u2009 = THIN SPACE
\u200a = HAIR SPACE
​\u200b = ZERO WIDTH SPACE
​\u202f = NARROW NO-BREAK SPACE
\u205f​ = MEDIUM MATHEMATICAL SPACE
\u3000 = IDEOGRAPHIC SPACE
*/
function findDelimiter(segment: ContentModelText, moveRightward: boolean): number {
    const word = segment.text;
    let offset = -1;
    if (moveRightward) {
        for (let i = 0; i < word.length; i++) {
            const char = word[i];
 
            if (isPunctuation(char) || isSpace(char)) {
                offset = i;
                break;
            }
        }
    } else {
        for (let i = word.length - 1; i >= 0; i--) {
            const char = word[i];
 
            if (isPunctuation(char) || isSpace(char)) {
                offset = i + 1;
                break;
            }
        }
    }
    return offset;
}
 
function splitTextSegment(
    segments: ShallowMutableContentModelSegment[],
    textSegment: Readonly<ContentModelText>,
    index: number,
    found: number
) {
    const text = textSegment.text;
    const newSegmentLeft = createText(
        text.substring(0, found),
        textSegment.format,
        textSegment.link,
        textSegment.code
    );
    const newSegmentRight = createText(
        text.substring(found, text.length),
        textSegment.format,
        textSegment.link,
        textSegment.code
    );
    segments.splice(index, 1, newSegmentLeft, newSegmentRight);
}