|
2 | 2 | 'use strict'
|
3 | 3 |
|
4 | 4 | var CTRL_KEY_CODE = 17
|
| 5 | + var LT_KEY_CODE = 188 |
5 | 6 | var S_KEY_CODE = 83
|
6 | 7 | var SOLIDUS_KEY_CODE = 191
|
7 | 8 | var SEARCH_FILTER_ACTIVE_KEY = 'docs:search-filter-active'
|
| 9 | + var SAVED_SEARCH_STATE_KEY = 'docs:saved-search-state' |
8 | 10 |
|
9 | 11 | activateSearch(require('docsearch.js/dist/cdn/docsearch.js'), document.getElementById('search-script').dataset)
|
10 | 12 |
|
|
38 | 40 | var menu = dropdown.$menu
|
39 | 41 | typeahead.setVal() // clear value on page reload
|
40 | 42 | input.on('autocomplete:closed', clearSearch.bind(typeahead))
|
| 43 | + input.on('autocomplete:cursorchanged autocomplete:cursorremoved', saveSearchState.bind(typeahead)) |
41 | 44 | input.on('autocomplete:selected', disableClose)
|
42 | 45 | input.on('autocomplete:updated', onResultsUpdated.bind(typeahead))
|
43 | 46 | dropdown._ensureVisible = ensureVisible
|
|
63 | 66 | }
|
64 | 67 |
|
65 | 68 | function onResultsUpdated () {
|
| 69 | + var dropdown = this.dropdown |
| 70 | + var restoring = dropdown.restoring |
| 71 | + delete dropdown.restoring |
66 | 72 | if (isClosed(this)) return
|
67 |
| - this.dropdown.datasets[0].$el.scrollTop(0) |
| 73 | + var dataset = dropdown.datasets[0] |
| 74 | + dataset.$el.scrollTop(0) |
| 75 | + if (restoring && restoring.query === this.getVal() && restoring.filter === this.$facetFilterInput.prop('checked')) { |
| 76 | + var cursor = restoring.cursor |
| 77 | + if (cursor) dropdown._moveCursor(cursor) |
| 78 | + } else { |
| 79 | + saveSearchState.call(this) |
| 80 | + } |
68 | 81 | }
|
69 | 82 |
|
70 | 83 | function toggleFilter (e) {
|
|
101 | 114 |
|
102 | 115 | function handleShortcuts (e) {
|
103 | 116 | var target = e.target || {}
|
| 117 | + if (e.ctrlKey && e.keyCode === LT_KEY_CODE && target === this.$input[0]) { |
| 118 | + restoreSearch.call(this) |
| 119 | + return |
| 120 | + } |
104 | 121 | if (e.altKey || e.shiftKey || target.isContentEditable || 'disabled' in target) return
|
105 | 122 | if (e.ctrlKey ? e.keyCode === SOLIDUS_KEY_CODE : e.keyCode === S_KEY_CODE) {
|
106 | 123 | this.$input.focus()
|
|
168 | 185 | return hit
|
169 | 186 | })
|
170 | 187 | }
|
| 188 | + |
| 189 | + function readSavedSearchState () { |
| 190 | + try { |
| 191 | + var state = window.localStorage.getItem(SAVED_SEARCH_STATE_KEY) |
| 192 | + if (state) return JSON.parse(state) |
| 193 | + } catch (e) { |
| 194 | + window.localStorage.removeItem(SAVED_SEARCH_STATE_KEY) |
| 195 | + } |
| 196 | + } |
| 197 | + |
| 198 | + function restoreSearch () { |
| 199 | + var searchState = readSavedSearchState() |
| 200 | + if (!searchState) return |
| 201 | + this.setVal() |
| 202 | + this.$facetFilterInput.prop('checked', searchState.filter) |
| 203 | + var dropdown = this.dropdown |
| 204 | + dropdown.datasets[0].clearCachedSuggestions() |
| 205 | + dropdown.restoring = searchState |
| 206 | + this.setVal(searchState.query) // cursor is restored by onResultsUpdated => |
| 207 | + } |
| 208 | + |
| 209 | + function saveSearchState () { |
| 210 | + if (isClosed(this)) return |
| 211 | + window.localStorage.setItem( |
| 212 | + SAVED_SEARCH_STATE_KEY, |
| 213 | + JSON.stringify({ |
| 214 | + query: this.getVal(), |
| 215 | + filter: this.$facetFilterInput.prop('checked'), |
| 216 | + cursor: this.dropdown.getCurrentCursor().index() + 1, |
| 217 | + }) |
| 218 | + ) |
| 219 | + } |
171 | 220 | })()
|
0 commit comments