From c20bd283319158135e2d850485970dfc5fe82812 Mon Sep 17 00:00:00 2001 From: Vinicius Teixeira Dias <69281620+viniciusteixeiradias@users.noreply.github.com> Date: Tue, 9 May 2023 02:55:32 +1000 Subject: [PATCH] feat(theme): open search box on pressing slash too (#2328) Co-authored-by: Divyansh Singh <40380293+brc-dd@users.noreply.github.com> --- .../components/VPNavBarSearch.vue | 28 +++++++++++++++++-- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/src/client/theme-default/components/VPNavBarSearch.vue b/src/client/theme-default/components/VPNavBarSearch.vue index dfa5e588..12c3929e 100644 --- a/src/client/theme-default/components/VPNavBarSearch.vue +++ b/src/client/theme-default/components/VPNavBarSearch.vue @@ -61,9 +61,12 @@ onMounted(() => { preconnect() - const handleSearchHotKey = (e: KeyboardEvent) => { - if (e.key === 'k' && (e.ctrlKey || e.metaKey)) { - e.preventDefault() + const handleSearchHotKey = (event: KeyboardEvent) => { + if ( + (event.key.toLowerCase() === 'k' && (event.metaKey || event.ctrlKey)) || + (!isEditingContent(event) && event.key === '/') + ) { + event.preventDefault() load() remove() } @@ -101,6 +104,18 @@ function poll() { }, 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 const showSearch = ref(false) @@ -112,6 +127,13 @@ if (__VP_LOCAL_SEARCH__) { showSearch.value = true } }) + + onKeyStroke('/', (event) => { + if (!isEditingContent(event)) { + event.preventDefault() + showSearch.value = true + } + }) } const metaKey = ref(`'Meta'`)