All files / roosterjs-content-model-plugins/lib/findReplace/utils updateHighlight.ts

100% Statements 25/25
100% Branches 14/14
100% Functions 4/4
100% Lines 24/24

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 521x 1x 1x             1x     12x 12x   8x   8x 21x 7x   7x 4x   8x       3x     7x 3x 1x   3x   4x     7x   1x     8x 6x     8x    
import { getRangesByText } from 'roosterjs-content-model-dom';
import { setMarkedIndex } from './setMarkedIndex';
import { sortRanges } from './sortRanges';
import type { IEditor } from 'roosterjs-content-model-types';
import type { FindReplaceContext } from '../types/FindReplaceContext';
 
/**
 * @internal
 */
export function updateHighlight(
    editor: IEditor,
    context: FindReplaceContext,
    addedBlockElements: HTMLElement[] | null = null,
    removedBlockElements: HTMLElement[] | null = null
) {
    context.findHighlight.clear();
 
    if (context.text) {
        const { text, matchCase, wholeWord } = context;
        const domHelper = editor.getDOMHelper();
 
        if (removedBlockElements) {
            context.ranges = context.ranges.filter(
                r =>
                    !removedBlockElements.some(x => x.contains(r.startContainer)) &&
                    domHelper.isNodeInEditor(r.startContainer, true /*excludeRoot*/)
            );
        } else {
            context.ranges = [];
        }
 
        if (addedBlockElements) {
            const newRanges = addedBlockElements.map(b =>
                getRangesByText(b, text, matchCase, wholeWord, true /*editableOnly*/)
            );
            context.ranges = context.ranges.concat(...newRanges);
        } else {
            context.ranges = domHelper.getRangesByText(text, matchCase, wholeWord);
        }
 
        sortRanges(context.ranges);
    } else {
        context.ranges = [];
    }
 
    if (context.ranges.length > 0) {
        context.findHighlight.addRanges(context.ranges);
    }
 
    setMarkedIndex(editor, context, -1);
}