Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat($plugin-search): Add support for search hotkeys #1848

Merged
merged 7 commits into from
Sep 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 = 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