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

100% Statements 26/26
80% Branches 16/20
100% Functions 9/9
100% Lines 23/23

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                                                  5x                 4x     1x       5x   1x 5x 4x 4x   4x       1x 1x 1x 1x 1x     1x 1x 2x 2x         1x 1x   1x                 1x 5x    
import type { HighlightHelper } from '../types/HighlightHelper';
 
interface HighlightRegistry {}
 
interface CSSShim {
    highlights: HighlightRegistry;
}
 
declare class Highlight {
    constructor(...textRange: Range[]);
    clear: () => void;
    add: (range: Range) => void;
}
 
interface WindowWithHighlight extends Window {
    Highlight: typeof Highlight;
    CSS: CSSShim;
}
 
interface HighlightRegistryWithMap extends HighlightRegistry {
    set(name: string, highlight: Highlight): void;
    delete(name: string): void;
}
 
function isWindowWithHighlight(win: Window): win is WindowWithHighlight {
    return (
        typeof (win as WindowWithHighlight).Highlight === 'function' &&
        typeof (win as WindowWithHighlight).CSS === 'object'
    );
}
 
function isHighlightRegistryWithMap(
    highlight: HighlightRegistry
): highlight is HighlightRegistryWithMap {
    return typeof (highlight as HighlightRegistryWithMap).set === 'function';
}
 
class HighlightHelperImpl implements HighlightHelper {
    private highlight: Highlight | undefined;
    private highlights: HighlightRegistryWithMap | undefined;
 
    constructor(private styleKey: string) {}
 
    initialize(win: Window) {
        if (isWindowWithHighlight(win) && isHighlightRegistryWithMap(win.CSS.highlights)) {
            this.highlights = win.CSS.highlights;
            this.highlight = new win.Highlight();
 
            this.highlights.set(this.styleKey, this.highlight);
        }
    }
 
    dispose() {
        this.highlights?.delete(this.styleKey);
        this.highlight?.clear();
        this.highlight = undefined;
        this.highlights = undefined;
    }
 
    addRanges(ranges: Range[]) {
        Eif (this.highlight) {
            for (const range of ranges) {
                this.highlight.add(range);
            }
        }
    }
 
    clear() {
        this.highlight?.clear();
    }
}
 
/**
 * @internal
 * Create a HighlightHelper instance. A highlight helper manages the highlights in the editor
 * @param win The window object
 * @param styleKey The style key for the highlight
 * @returns The created HighlightHelper instance or undefined if the window does not support highlights
 */
export function createHighlightHelper(styleKey: string): HighlightHelper {
    return new HighlightHelperImpl(styleKey);
}