|
193 | 193 | }
|
194 | 194 |
|
195 | 195 | function init (lunr, data) {
|
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(() => { |
| 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 () { |
200 | 199 | try {
|
201 |
| - searchIndex(index.index, index.store, searchInput.value); |
| 200 | + searchIndex(index.index, index.store, searchInput.value) |
202 | 201 | } catch (err) {
|
203 |
| - if (isDebugEnabled) console.debug(`Invalid search query: ${searchInput.value} (${err.message})`); |
| 202 | + if (debug) console.debug('Invalid search query: ' + searchInput.value + ' (' + err.message + ')') |
204 | 203 | }
|
205 |
| - }, 100); |
| 204 | + }, 100) |
| 205 | + searchInput.addEventListener('keydown', search) |
| 206 | + |
| 207 | + searchInput.addEventListener('keydown', function (event) { |
| 208 | + if (event.key === 'Escape') searchInput.value = '' |
| 209 | + }) |
206 | 210 |
|
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); }); |
| 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 | + }) |
210 | 217 | }
|
211 | 218 |
|
212 | 219 | return { init: init }
|
|
0 commit comments