|
1 | 1 | type TSearchCommands = 'performSearch' | 'nextSearchResult' | 'cancelSearch';
|
2 |
| -interface ISearchOptions { |
3 |
| - el: HTMLElement; |
| 2 | + |
| 3 | +export interface ISearchOptions { |
4 | 4 | cmd: TSearchCommands;
|
5 | 5 | query: string | null;
|
6 | 6 | }
|
7 |
| -const searchState = { |
8 |
| - lastCmd: '', |
9 |
| - currentEl: 0, |
| 7 | + |
| 8 | +interface ISearchState { |
| 9 | + foundEls: HTMLElement[]; |
| 10 | + query: string; |
| 11 | + currentIndex: number; |
| 12 | + clear: () => void; |
| 13 | +} |
| 14 | + |
| 15 | +const searchState: ISearchState = { |
| 16 | + foundEls: [], |
| 17 | + query: '', |
| 18 | + currentIndex: -1, |
| 19 | + clear() { |
| 20 | + this.foundEls.splice(0); |
| 21 | + this.query = ''; |
| 22 | + this.currentIndex = -1; |
| 23 | + }, |
10 | 24 | };
|
11 | 25 |
|
12 |
| -function highlightElements(container: HTMLElement, query: string): void { |
13 |
| - const childNodes = container.childNodes; |
| 26 | +const CLASS_FOUND = 'jsdiff-found'; |
| 27 | +const CLASS_FOUND_THIS = 'jsdiff-found-this'; |
| 28 | +const uppercasePattern = /\p{Lu}/u; // 'u' flag enables Unicode matching |
| 29 | + |
| 30 | +function hasUppercaseCharacter(str: string): boolean { |
| 31 | + for (let n = 0, N = str.length; n < N; n++) { |
| 32 | + if (uppercasePattern.test(str.charAt(n))) { |
| 33 | + return true; |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + return false; |
| 38 | +} |
| 39 | + |
| 40 | +function highlightElements( |
| 41 | + container: HTMLElement, |
| 42 | + query: string, |
| 43 | + foundEls: HTMLElement[] |
| 44 | +): void { |
| 45 | + const containerNodes = container.childNodes as NodeListOf<HTMLElement>; |
| 46 | + |
| 47 | + if (containerNodes.length) { |
| 48 | + const firstChild = containerNodes[0]; |
| 49 | + |
| 50 | + if (containerNodes.length === 1 && firstChild.nodeType === Node.TEXT_NODE) { |
| 51 | + const text = firstChild.textContent || firstChild.innerText; |
| 52 | + const found = hasUppercaseCharacter(query) |
| 53 | + ? text.includes(query) // case-sensitive |
| 54 | + : text.toLocaleLowerCase().includes(query); // case-insensitive |
14 | 55 |
|
15 |
| - for (let n = 0, N = childNodes.length; n < N; n++) { |
16 |
| - const child = childNodes[n]; |
17 |
| - if (child.nodeType === Node.ELEMENT_NODE) { |
18 |
| - const element = child as HTMLElement; |
19 |
| - const text = element.textContent || element.innerText; |
20 |
| - if (text.includes(query)) { |
21 |
| - element.classList.add('found'); |
| 56 | + if (found) { |
| 57 | + container.classList.add('jsdiff-found'); |
| 58 | + foundEls.push(container); |
| 59 | + } |
| 60 | + } else { |
| 61 | + for (let n = 0, N = containerNodes.length; n < N; n++) { |
| 62 | + const child = containerNodes[n]; |
| 63 | + |
| 64 | + if (child.nodeType === Node.ELEMENT_NODE) { |
| 65 | + highlightElements(child, query, foundEls); // recursion |
| 66 | + } |
22 | 67 | }
|
23 |
| - highlightElements(element, query); // recursion |
24 | 68 | }
|
25 | 69 | }
|
26 | 70 | }
|
27 | 71 |
|
28 |
| -function clearHighlight(container: HTMLElement) { |
29 |
| - const allFound = container.querySelectorAll('.found'); |
| 72 | +function clearHighlight(container: HTMLElement): void { |
| 73 | + const allFound = container.querySelectorAll(`.${CLASS_FOUND}`); |
30 | 74 |
|
31 |
| - for (let n = 0, N = allFound.length; n < N; n++) { |
32 |
| - allFound[n].classList.remove('found', 'foundThis'); |
| 75 | + for (let n = allFound.length - 1; n >= 0; n--) { |
| 76 | + allFound[n].classList.remove(CLASS_FOUND, CLASS_FOUND_THIS); |
33 | 77 | }
|
34 | 78 | }
|
35 | 79 |
|
36 |
| -export function searchQueryInDom({ el, cmd, query }: ISearchOptions) { |
| 80 | +function highlightCurrentResult(searchState: ISearchState): void { |
| 81 | + searchState.currentIndex++; |
| 82 | + searchState.currentIndex %= searchState.foundEls.length; |
| 83 | + |
| 84 | + const el = searchState.foundEls[searchState.currentIndex]; |
| 85 | + el.classList.add(CLASS_FOUND_THIS); |
| 86 | + el.scrollIntoView({ |
| 87 | + behavior: 'smooth', |
| 88 | + block: 'nearest', |
| 89 | + inline: 'nearest', |
| 90 | + }); |
| 91 | +} |
| 92 | + |
| 93 | +export function searchQueryInDom( |
| 94 | + el: HTMLElement, |
| 95 | + { cmd, query }: ISearchOptions |
| 96 | +): void { |
37 | 97 | query = (typeof query === 'string' && query.trim()) || '';
|
38 |
| - console.log('🔦', cmd, query); |
39 | 98 |
|
40 |
| - if ('performSearch' === cmd && query) { |
41 |
| - highlightElements(el, query); |
42 |
| - } else if ('nextSearchResult' === cmd) { |
43 |
| - // TODO: ... |
| 99 | + // console.log('🔦', cmd, query); |
| 100 | + |
| 101 | + if ('performSearch' === cmd) { |
| 102 | + searchState.query = query; |
| 103 | + |
| 104 | + if (!query) { |
| 105 | + searchState.clear(); |
| 106 | + clearHighlight(el); |
| 107 | + } |
| 108 | + } else if ('nextSearchResult' === cmd && searchState.query) { |
| 109 | + clearHighlight(el); |
| 110 | + searchState.foundEls.splice(0); |
| 111 | + highlightElements(el, searchState.query, searchState.foundEls); |
| 112 | + |
| 113 | + if (searchState.foundEls.length) { |
| 114 | + highlightCurrentResult(searchState); |
| 115 | + } else { |
| 116 | + searchState.clear(); |
| 117 | + clearHighlight(el); |
| 118 | + } |
44 | 119 | } else if ('cancelSearch' === cmd) {
|
| 120 | + searchState.clear(); |
45 | 121 | clearHighlight(el);
|
46 | 122 | }
|
47 | 123 | }
|
0 commit comments