File size: 5,337 Bytes
07bbbbf |
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 |
/**
* Highlight Module for Dictionary Content
*
* Provides text search and highlighting functionality for dictionary entries.
* Uses mark.js library for efficient text highlighting and navigation.
*/
import Mark from 'mark.js';
// Type definitions
interface HighlightContext {
keyword: string;
currentMatchIndex: number;
matchedNode: HTMLElement[];
markInstance: Mark | null;
}
const mdx_css_highlight = "__mdx_css_highlight";
function getContext(iframe: HTMLIFrameElement): HighlightContext {
return (iframe.contentWindow as any).highlightContext as HighlightContext;
}
const matchedTextColor: string = "black";
const matchedTextBackgroundColor: string = "Yellow";
const matchedTextHighLightColor: string = "black";
const matchedTextHighLightBackgroundColor: string = "Coral";
// Main entry point to start the search using mark.js
// Returns a Promise that resolves with the total number of matches found
function HighlightAllOccurrencesOfString(
iframe: HTMLIFrameElement,
keyword: string,
_maxMatchCount: number,
highLightFirstMatch?: boolean
): Promise<number> {
let context = getContext(iframe);
console.debug("Highlighting words:" + keyword);
RemoveAllHighlights(iframe);
if (!iframe.contentDocument?.body) {
return Promise.resolve(0);
}
// Create mark.js instance for the iframe content
context.markInstance = new Mark(iframe.contentDocument.body);
context.keyword = keyword;
context.matchedNode = [];
context.currentMatchIndex = -1;
return new Promise<number>((resolve) => {
// Use mark.js to highlight the keyword
context.markInstance!.mark(keyword, {
element: "span",
className: mdx_css_highlight,
exclude: ["script", "style"],
iframes: true,
iframesTimeout: 5000,
acrossElements: false,
caseSensitive: false,
separateWordSearch: false,
accuracy: "partially",
each: function(element: HTMLElement) {
// Apply default highlighting styles
element.style.backgroundColor = matchedTextBackgroundColor;
element.style.color = matchedTextColor;
context.matchedNode.push(element);
},
done: function(totalMarks: number) {
console.debug("Total match found:" + totalMarks);
if (totalMarks > 0 && (highLightFirstMatch === undefined || highLightFirstMatch)) {
clearMatchCursor(iframe);
setMatchCursor(iframe, 0, false);
}
resolve(totalMarks);
}
});
});
}
// Main entry point to remove the highlights using mark.js
function RemoveAllHighlights(iframe: HTMLIFrameElement): void {
let context = getContext(iframe);
if (context && context.markInstance) {
context.markInstance.unmark();
context.matchedNode = [];
context.currentMatchIndex = -1;
context.markInstance = null;
}
}
function clearMatchCursor(iframe: HTMLIFrameElement): boolean {
let context = getContext(iframe);
if (context.currentMatchIndex >= 0 && context.currentMatchIndex < context.matchedNode.length && context.matchedNode[context.currentMatchIndex]) {
context.matchedNode[context.currentMatchIndex].style.color = matchedTextColor;
context.matchedNode[context.currentMatchIndex].style.backgroundColor = matchedTextBackgroundColor;
context.currentMatchIndex = -1;
return true;
}else{
context.currentMatchIndex = -1;
return false;
}
}
function setMatchCursor(iframe: HTMLIFrameElement, pos: number, scrollTo: boolean): boolean {
console.debug("setMatchCursor", pos, scrollTo);
clearMatchCursor(iframe);
let context = getContext(iframe);
context.currentMatchIndex = pos;
if (pos >= 0 && pos < context.matchedNode.length && context.matchedNode[context.currentMatchIndex]) {
console.debug("setMatchCursor", context.matchedNode[context.currentMatchIndex]);
context.matchedNode[context.currentMatchIndex].style.color = matchedTextHighLightColor;
context.matchedNode[context.currentMatchIndex].style.backgroundColor = matchedTextHighLightBackgroundColor;
if (scrollTo) {
context.matchedNode[context.currentMatchIndex].scrollIntoView({ behavior: 'smooth', block: 'center' });
}
return true;
}else{
context.currentMatchIndex = -1;
return false;
}
}
function getMatchCursorIndex(iframe: HTMLIFrameElement): number {
let context = getContext(iframe);
return context.currentMatchIndex;
}
function getMatchCount(iframe: HTMLIFrameElement): number {
let context = getContext(iframe);
return context.matchedNode.length;
}
function highlightSetup(iframe: HTMLIFrameElement) {
(iframe.contentWindow as any).highlightContext = {
keyword: "",
currentMatchIndex: -1,
matchedNode: new Array<HTMLElement>(),
markInstance: null,
};
console.debug("Highlight setup completed");
}
export {
highlightSetup,
HighlightAllOccurrencesOfString,
RemoveAllHighlights,
clearMatchCursor,
getMatchCursorIndex,
setMatchCursor,
getMatchCount
} |