|
193 | 193 | }
|
194 | 194 |
|
195 | 195 | function init (lunr, data) {
|
196 |
| - var index = Object.assign({ index: lunr.Index.load(data.index), store: data.store }) |
197 |
| - var debug = 'URLSearchParams' in window && new URLSearchParams(window.location.search).has('lunr-debug') |
198 |
| - var search = debounce(function () { |
| 196 | + const index = { index: lunr.Index.load(data.index), store: data.store }; |
| 197 | + const isDebugEnabled = 'URLSearchParams' in window && new URLSearchParams(window.location.search).has('lunr-debug'); |
| 198 | + |
| 199 | + const debouncedSearch = debounce(() => { |
199 | 200 | try {
|
200 |
| - searchIndex(index.index, index.store, searchInput.value) |
| 201 | + searchIndex(index.index, index.store, searchInput.value); |
201 | 202 | } catch (err) {
|
202 |
| - if (debug) console.debug('Invalid search query: ' + searchInput.value + ' (' + err.message + ')') |
| 203 | + if (isDebugEnabled) console.debug(`Invalid search query: ${searchInput.value} (${err.message})`); |
203 | 204 | }
|
204 |
| - }, 100) |
205 |
| - searchInput.addEventListener('keydown', search) |
206 |
| - |
207 |
| - searchInput.addEventListener('keydown', function (event) { |
208 |
| - if (event.key === 'Escape') searchInput.value = '' |
209 |
| - }) |
| 205 | + }, 100); |
210 | 206 |
|
211 |
| - // this is prevented in case of mousedown attached to SearchResultItem |
212 |
| - searchInput.addEventListener('blur', function (e) { |
213 |
| - while (searchResult.firstChild) { |
214 |
| - searchResult.removeChild(searchResult.firstChild) |
215 |
| - } |
216 |
| - }) |
| 207 | + searchInput.addEventListener('keydown', debouncedSearch); |
| 208 | + searchInput.addEventListener('keydown', (event) => event.key === 'Escape' && (searchInput.value = '')); |
| 209 | + searchInput.addEventListener('blur', () => { while (searchResult.firstChild) searchResult.removeChild(searchResult.firstChild); }); |
217 | 210 | }
|
218 | 211 |
|
219 | 212 | return { init: init }
|
|
0 commit comments