Skip to content

Commit 4a41664

Browse files
committed
fix(theme-press): cmd + k to search when first mounted, close #567
1 parent 742f744 commit 4a41664

File tree

2 files changed

+42
-30
lines changed

2 files changed

+42
-30
lines changed

packages/valaxy-addon-algolia/components/AlgoliaSearchBox.vue

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -44,35 +44,6 @@ async function update() {
4444
})
4545
}
4646
47-
onMounted(() => {
48-
const options = {
49-
...algolia.value.options,
50-
...algolia.value.options?.locales?.[locale.value],
51-
}
52-
53-
// now only lang:en
54-
// const rawFacetFilters = options.searchParameters?.facetFilters ?? []
55-
// const facetFilters = [
56-
// ...(Array.isArray(rawFacetFilters)
57-
// ? rawFacetFilters
58-
// : [rawFacetFilters]
59-
// ).filter(f => !f.startsWith('lang:')),
60-
// `lang:${locale.value}`,
61-
// ]
62-
63-
if (options && options.apiKey && options.appId && options.indexName) {
64-
if (!document.querySelector('.DocSearch-Container')) {
65-
initialize({
66-
...options as AlgoliaSearchOptions,
67-
searchParameters: {
68-
...options.searchParameters,
69-
// facetFilters,
70-
},
71-
})
72-
}
73-
}
74-
})
75-
7647
function initialize(userOptions: AlgoliaSearchOptions) {
7748
// note: multi-lang search support is removed since the theme
7849
// doesn't support multiple locales as of now.

packages/valaxy-theme-press/components/PressAlgoliaSearch.vue

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,51 @@
11
<script lang="ts" setup>
22
import { useAddonAlgolia } from 'valaxy-addon-algolia'
3+
import { onMounted, onUnmounted } from 'vue'
34
import { useI18n } from 'vue-i18n'
45
56
const { t } = useI18n()
67
7-
const { loaded, load } = useAddonAlgolia()
8+
const { loaded, load, dispatchEvent } = useAddonAlgolia()
9+
10+
defineExpose({
11+
loaded,
12+
load,
13+
dispatchEvent,
14+
})
15+
16+
function isEditingContent(event: KeyboardEvent): boolean {
17+
const element = event.target as HTMLElement
18+
const tagName = element.tagName
19+
20+
return (
21+
element.isContentEditable
22+
|| tagName === 'INPUT'
23+
|| tagName === 'SELECT'
24+
|| tagName === 'TEXTAREA'
25+
)
26+
}
27+
28+
onMounted(() => {
29+
const handleSearchHotKey = (event: KeyboardEvent) => {
30+
if (
31+
(event.key?.toLowerCase() === 'k' && (event.metaKey || event.ctrlKey))
32+
|| (!isEditingContent(event) && event.key === '/')
33+
) {
34+
event.preventDefault()
35+
load()
36+
// eslint-disable-next-line ts/no-use-before-define
37+
remove()
38+
}
39+
}
40+
41+
const remove = () => {
42+
window.removeEventListener('keydown', handleSearchHotKey)
43+
}
44+
45+
window.addEventListener('keydown', handleSearchHotKey)
46+
47+
onUnmounted(remove)
48+
})
849
</script>
950

1051
<template>

0 commit comments

Comments
 (0)