|
1 | 1 | (function() { |
2 | 2 | const DATA_TEXTCONTENT = 'data-dd-original-textcontent'; |
3 | 3 |
|
| 4 | + const flatten = function(xs) { |
| 5 | + return xs.reduce((acc, x) => { |
| 6 | + if (Array.isArray(x)) { |
| 7 | + return acc.concat(flatten(x)); |
| 8 | + } |
| 9 | + return acc.concat([x]); |
| 10 | + }, []); |
| 11 | + }; |
| 12 | + |
4 | 13 | const rootSearchNode = function() { |
5 | 14 | return document.querySelector('main[role="main"]'); |
6 | 15 | }; |
|
14 | 23 | const mutateDOM = function(mutations) { |
15 | 24 | return new Promise(function(resolve, _reject) { |
16 | 25 | requestAnimationFrame(function() { |
17 | | - mutations.forEach(function(mut) { |
18 | | - mut(); |
19 | | - }); |
20 | | - resolve(true); |
| 26 | + resolve(mutations.map((mut) => mut())); |
21 | 27 | }); |
22 | 28 | }); |
23 | 29 | }; |
|
40 | 46 | (match) => `<mark>${match}</mark>` |
41 | 47 | ); |
42 | 48 | node.innerHTML = newContent; |
| 49 | + return Array.from(node.querySelectorAll('mark')); |
43 | 50 | }; |
44 | 51 | }; |
45 | 52 |
|
|
55 | 62 | const reset = function() { |
56 | 63 | const mainNode = rootSearchNode(); |
57 | 64 | const treeWalker = document.createTreeWalker(mainNode, NodeFilter.SHOW_ELEMENT, { |
58 | | - acceptNode: function(node) { |
| 65 | + acceptNode(node) { |
59 | 66 | if (node.hasAttribute(DATA_TEXTCONTENT)) { |
60 | 67 | return NodeFilter.FILTER_ACCEPT; |
61 | 68 | } |
|
72 | 79 | const search = function(term) { |
73 | 80 | const mainNode = rootSearchNode(); |
74 | 81 | const treeWalker = document.createTreeWalker(mainNode, NodeFilter.SHOW_TEXT, { |
75 | | - acceptNode: function(node) { |
| 82 | + acceptNode(node) { |
76 | 83 | var parent = node.parentNode; |
77 | 84 | if (parent.tagName === 'MARK') { |
78 | 85 | return NodeFilter.FILTER_REJECT; |
|
94 | 101 | return mutateDOM(domMutations); |
95 | 102 | }; |
96 | 103 |
|
97 | | - // hacky, but seems to be the only reliable way to clear search results on |
98 | | - // page change. |
| 104 | + // No reliable API to detect route transitions so that we can restore the |
| 105 | + // original page state. |
99 | 106 | (function() { |
100 | 107 | const observer = new MutationObserver(() => { |
101 | | - reset(); |
| 108 | + window.resetSearch(); |
102 | 109 | }); |
103 | 110 | const titleEl = document.querySelector('title'); |
104 | 111 | observer.observe(titleEl, { |
|
108 | 115 | }); |
109 | 116 | })(); |
110 | 117 |
|
111 | | - window.search = function(term) { |
112 | | - if (term === '' || typeof term !== 'string') { |
113 | | - return; |
| 118 | + // Install custom CSS |
| 119 | + (function() { |
| 120 | + const styleEl = document.createElement('style'); |
| 121 | + styleEl.setAttribute('type', 'text/css'); |
| 122 | + styleEl.textContent = ` |
| 123 | + mark.dd-macos-current { |
| 124 | + font-size: 2em; |
| 125 | + } |
| 126 | + `; |
| 127 | + document.querySelector('head').appendChild(styleEl); |
| 128 | + })(); |
| 129 | + |
| 130 | + class SearchState { |
| 131 | + constructor({ term, marks }) { |
| 132 | + this.term = term; |
| 133 | + this.marks = marks; |
| 134 | + } |
| 135 | + |
| 136 | + isCurrentTerm(term) { |
| 137 | + return this.term === term; |
| 138 | + } |
| 139 | + |
| 140 | + async spotlightMark() { |
| 141 | + if (this.marks.length === 0) { |
| 142 | + return this; |
| 143 | + } |
| 144 | + const [next, ...rest] = this.marks; |
| 145 | + const prev = this.marks[this.marks.length - 1]; |
| 146 | + |
| 147 | + await mutateDOM([() => { |
| 148 | + prev.removeAttribute('class'); |
| 149 | + next.setAttribute('class', 'dd-macos-current'); |
| 150 | + // Use DevDocs own utilities. |
| 151 | + $.scrollTo(next); |
| 152 | + }]); |
| 153 | + |
| 154 | + this.marks = rest.concat([next]); |
| 155 | + return this; |
114 | 156 | } |
115 | | - return reset().then(() => search(term)); |
| 157 | + } |
| 158 | + |
| 159 | + let searchState; |
| 160 | + |
| 161 | + // Public API |
| 162 | + |
| 163 | + window.search = async function(term) { |
| 164 | + if (typeof term !== 'string') { |
| 165 | + return false; |
| 166 | + } |
| 167 | + const searchTerm = term.trim(); |
| 168 | + if (searchTerm === '') { |
| 169 | + return false; |
| 170 | + } |
| 171 | + if (searchState && searchState.isCurrentTerm(searchTerm)) { |
| 172 | + await searchState.spotlightMark(); |
| 173 | + return true; |
| 174 | + } |
| 175 | + |
| 176 | + await reset(); |
| 177 | + const insertedMarks = await search(searchTerm); |
| 178 | + const ss = new SearchState({ |
| 179 | + term, |
| 180 | + marks: flatten(insertedMarks), |
| 181 | + }); |
| 182 | + await ss.spotlightMark(); |
| 183 | + searchState = ss; |
| 184 | + return true; |
116 | 185 | }; |
117 | 186 |
|
118 | | - window.resetSearch = function() { |
119 | | - return reset(); |
| 187 | + window.resetSearch = async function() { |
| 188 | + await reset(); |
| 189 | + searchState = null |
| 190 | + return true; |
120 | 191 | }; |
121 | 192 | })(); |
122 | 193 |
|
0 commit comments