Skip to content

Commit

Permalink
feat($plugin-search): Add support for search hotkeys (#1848)
Browse files Browse the repository at this point in the history
  • Loading branch information
evaera authored and kefranabg committed Sep 12, 2019
1 parent 0434f15 commit 1ba06ae
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
19 changes: 17 additions & 2 deletions packages/@vuepress/plugin-search/SearchBox.vue
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
@keyup.enter="go(focusIndex)"
@keyup.up="onUp"
@keyup.down="onDown"
ref="input"
>
<ul
class="suggestions"
Expand All @@ -37,7 +38,7 @@
</template>

<script>
/* global SEARCH_MAX_SUGGESTIONS, SEARCH_PATHS */
/* global SEARCH_MAX_SUGGESTIONS, SEARCH_PATHS, SEARCH_HOTKEYS */
export default {
data () {
return {
Expand All @@ -50,7 +51,13 @@ export default {
mounted () {
this.placeholder = this.$site.themeConfig.searchPlaceholder || ''
document.addEventListener('keydown', this.onHotkey)
},
beforeDestroy () {
document.removeEventListener('keydown', this.onHotkey)
},
computed: {
showSuggestions () {
return (
Expand All @@ -70,7 +77,8 @@ export default {
const max = this.$site.themeConfig.searchMaxSuggestions || SEARCH_MAX_SUGGESTIONS
const localePath = this.$localePath
const matches = item => (
item.title
item
&& item.title
&& item.title.toLowerCase().indexOf(query) > -1
)
const res = []
Expand Down Expand Up @@ -136,6 +144,13 @@ export default {
}).length > 0
},
onHotkey (event) {
if (event.srcElement === document.body && SEARCH_HOTKEYS.includes(event.key)) {
this.$refs.input.focus()
event.preventDefault()
}
},
onUp () {
if (this.showSuggestions) {
if (this.focusIndex > 0) {
Expand Down
3 changes: 2 additions & 1 deletion packages/@vuepress/plugin-search/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ module.exports = (options) => ({

define: {
SEARCH_MAX_SUGGESTIONS: options.searchMaxSuggestions || 5,
SEARCH_PATHS: options.test || null
SEARCH_PATHS: options.test || null,
SEARCH_HOTKEYS: options.searchHotkeys || ['s', '/']
}
})
7 changes: 7 additions & 0 deletions packages/docs/docs/plugin/official/plugin-search.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,13 @@ You can set up searchable paths with `test` as:

Otherwise, the default search will return duplicates, once you can have similar content between folders `/master/`, `/1.0/` and `/2.0/`.

### searchHotkeys

- Type: `Array<string>`
- Default: `['s', '/']`

Configure the hotkeys which when pressed will focus the search box. Set to an empty array to disable this feature.

## Tips

### Tweak the default colors.
Expand Down

0 comments on commit 1ba06ae

Please sign in to comment.