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

100% Statements 5/5
100% Branches 8/8
100% Functions 1/1
100% Lines 5/5

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 221x                   1x 7x   6x         6x      
import { setMarkedIndex } from './utils/setMarkedIndex';
import type { IEditor } from 'roosterjs-content-model-types';
import type { FindReplaceContext } from './types/FindReplaceContext';
 
/**
 * Move the highlight to next or previous match
 * @param editor The editor instance
 * @param context The FindReplaceContext to use
 * @param forward Whether to move forward or backward
 */
export function moveHighlight(editor: IEditor, context: FindReplaceContext, forward: boolean) {
    if (context.ranges.length > 0) {
        const newIndex =
            !forward && context.markedIndex == -1
                ? context.ranges.length - 1
                : (context.markedIndex + (forward ? 1 : -1) + context.ranges.length) %
                  context.ranges.length;
 
        setMarkedIndex(editor, context, newIndex);
    }
}