feat(theme): open search box on pressing slash too (#2328)

Co-authored-by: Divyansh Singh <40380293+brc-dd@users.noreply.github.com>
pull/2353/head
Vinicius Teixeira Dias 2 years ago committed by GitHub
parent 58795d2f93
commit c20bd28331
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -61,9 +61,12 @@ onMounted(() => {
preconnect() preconnect()
const handleSearchHotKey = (e: KeyboardEvent) => { const handleSearchHotKey = (event: KeyboardEvent) => {
if (e.key === 'k' && (e.ctrlKey || e.metaKey)) { if (
e.preventDefault() (event.key.toLowerCase() === 'k' && (event.metaKey || event.ctrlKey)) ||
(!isEditingContent(event) && event.key === '/')
) {
event.preventDefault()
load() load()
remove() remove()
} }
@ -101,6 +104,18 @@ function poll() {
}, 16) }, 16)
} }
function isEditingContent(event: KeyboardEvent): boolean {
const element = event.target as HTMLElement
const tagName = element.tagName
return (
element.isContentEditable ||
tagName === 'INPUT' ||
tagName === 'SELECT' ||
tagName === 'TEXTAREA'
)
}
// Local search // Local search
const showSearch = ref(false) const showSearch = ref(false)
@ -112,6 +127,13 @@ if (__VP_LOCAL_SEARCH__) {
showSearch.value = true showSearch.value = true
} }
}) })
onKeyStroke('/', (event) => {
if (!isEditingContent(event)) {
event.preventDefault()
showSearch.value = true
}
})
} }
const metaKey = ref(`'Meta'`) const metaKey = ref(`'Meta'`)

Loading…
Cancel
Save