-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
AlgoliaSearchBox.vue
188 lines (160 loc) · 4.63 KB
/
AlgoliaSearchBox.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
<script setup lang="ts">
import '@docsearch/css'
import docsearch from '@docsearch/js'
import { useRoute, useRouter, useData } from 'vitepress'
import { getCurrentInstance, onMounted, watch } from 'vue'
import type { DefaultTheme } from '../config'
import type { DocSearchHit } from '@docsearch/react/dist/esm/types'
const props = defineProps<{
options: DefaultTheme.AlgoliaSearchOptions
multilang?: boolean
}>()
const vm = getCurrentInstance()
const route = useRoute()
const router = useRouter()
watch(
() => props.options,
(value) => {
update(value)
}
)
onMounted(() => {
initialize(props.options)
})
function isSpecialClick(event: MouseEvent) {
return (
event.button === 1 ||
event.altKey ||
event.ctrlKey ||
event.metaKey ||
event.shiftKey
)
}
function getRelativePath(absoluteUrl: string) {
const { pathname, hash } = new URL(absoluteUrl)
return pathname + hash
}
function update(options: any) {
if (vm && vm.vnode.el) {
vm.vnode.el.innerHTML =
'<div class="algolia-search-box" id="docsearch"></div>'
initialize(options)
}
}
const { lang } = useData()
// if the user has multiple locales, the search results should be filtered
// based on the language
const facetFilters: string[] = props.multilang ? ['lang:' + lang.value] : []
if (props.options.searchParameters?.facetFilters) {
facetFilters.push(...props.options.searchParameters.facetFilters)
}
watch(lang, (newLang, oldLang) => {
const index = facetFilters.findIndex((filter) => filter === 'lang:' + oldLang)
if (index > -1) {
facetFilters.splice(index, 1, 'lang:' + newLang)
}
})
function initialize(userOptions: any) {
docsearch(
Object.assign({}, userOptions, {
container: '#docsearch',
searchParameters: Object.assign({}, userOptions.searchParameters, {
// pass a custom lang facetFilter to allow multiple language search
// https://github.com/algolia/docsearch-configs/pull/3942
facetFilters
}),
navigator: {
navigate: ({ itemUrl }: { itemUrl: string }) => {
const { pathname: hitPathname } = new URL(
window.location.origin + itemUrl
)
// Router doesn't handle same-page navigation so we use the native
// browser location API for anchor navigation
if (route.path === hitPathname) {
window.location.assign(window.location.origin + itemUrl)
} else {
router.go(itemUrl)
}
}
},
transformItems: (items: DocSearchHit[]) => {
return items.map((item) => {
return Object.assign({}, item, {
url: getRelativePath(item.url)
})
})
},
hitComponent: ({
hit,
children
}: {
hit: DocSearchHit
children: any
}) => {
const relativeHit = hit.url.startsWith('http')
? getRelativePath(hit.url as string)
: hit.url
return {
type: 'a',
ref: undefined,
constructor: undefined,
key: undefined,
props: {
href: hit.url,
onClick: (event: MouseEvent) => {
if (isSpecialClick(event)) {
return
}
// we rely on the native link scrolling when user is already on
// the right anchor because Router doesn't support duplicated
// history entries
if (route.path === relativeHit) {
return
}
// if the hits goes to another page, we prevent the native link
// behavior to leverage the Router loading feature
if (route.path !== relativeHit) {
event.preventDefault()
}
router.go(relativeHit)
},
children
},
__v: null
}
}
})
)
}
</script>
<template>
<div class="algolia-search-box" id="docsearch" />
</template>
<style>
.algolia-search-box {
padding-top: 1px;
}
@media (min-width: 720px) {
.algolia-search-box {
padding-left: 8px;
}
}
@media (min-width: 751px) {
.algolia-search-box {
min-width: 176.3px; /* avoid layout shift */
}
.algolia-search-box .DocSearch-Button-Placeholder {
padding-left: 8px;
font-size: 0.9rem;
font-weight: 500;
}
}
.DocSearch {
--docsearch-primary-color: var(--c-brand);
--docsearch-highlight-color: var(--docsearch-primary-color);
--docsearch-searchbox-shadow: inset 0 0 0 2px var(--docsearch-primary-color);
--docsearch-text-color: var(--c-text-light);
--docsearch-muted-color: var(--c-text-lighter);
--docsearch-searchbox-background: #f2f2f2;
}
</style>