-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
/
Search.vue
232 lines (196 loc) Β· 6.14 KB
/
Search.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
<!-- eslint-disable vue/attribute-hyphenation -->
<template>
<!-- possible bug with dialog overflow with scrollable -->
<v-dialog
v-model="model"
content-class="overflow-visible align-self-start mt-16"
max-height="900"
width="600"
scrollable
@after-leave="searchString = ''"
>
<template #activator="{ props: activatorProps }">
<AppBtn
:active="model"
:icon="xs ? 'mdi-magnify' : undefined"
:prepend-icon="smAndUp ? 'mdi-magnify' : undefined"
v-bind="activatorProps"
>
<span :class="mdAndUp && 'me-n1'">
<span v-if="smAndUp">
{{ t('search.label') }}
</span>
<span
:class="[
smAndDown ? 'border-opacity-0' : 'py-1 px-2 ms-2',
'border rounded text-disabled text-caption'
]"
>
<span v-if="mdAndUp">
{{ t(`search.key-hint${user.slashSearch ? '-slash' : platform.mac ? '-mac' : ''}`) }}
</span>
</span>
</span>
</AppBtn>
</template>
<v-card>
<AppTextField
v-model="searchString"
:placeholder="`${t('search.looking') }...`"
class="flex-grow-0 mb-4"
variant="filled"
autofocus
@focus="$event.target.select()"
>
<template #append-inner>
<AppBtn size="small" border>
<span class="text-caption text-disabled">{{ t('esc') }}</span>
</AppBtn>
</template>
</AppTextField>
<v-card-text :class="['px-4 py-0 d-flex flex-wrap justify-center', searchString ? 'align-start' : 'align-center']">
<AppSearchSearchRecent
v-if="searches.length && !searchString"
:searches="searches"
@click:delete="onClickDelete"
/>
<template v-else-if="!searchString">
<div class="text-center">
<v-icon
class="mb-6 mx-auto text-disabled"
icon="mdi-text-box-search-outline"
size="150"
/>
<br>
<v-list-subheader class="d-inline-flex">
{{ t('search.results') }}
</v-list-subheader>
</div>
</template>
<ais-instant-search
v-else
:search-client="searchClient"
class="flex-grow-1"
index-name="vuetifyjs-v3"
@state-change="searchFunction"
>
<ais-configure
:facetFilters="[`lang:${locale}`]"
:hitsPerPage="50"
:query="searchString"
/>
<ais-hits v-slot="{ items }">
<AppSearchSearchResults
ref="list"
:groups="transformItems(items)"
@click:result="onClickResult"
/>
</ais-hits>
</ais-instant-search>
</v-card-text>
<v-divider class="my-4" />
<AisPoweredBy class="ms-auto me-4 mb-2" />
</v-card>
</v-dialog>
</template>
<script setup lang="ts">
// Types
import SearchResults from '@/components/app/drawer/SearchResults.vue'
// Utilities
import { AisConfigure, AisHits, AisInstantSearch, AisPoweredBy } from 'vue-instantsearch/vue3/es/src/instantsearch.js'
import algoliasearch from 'algoliasearch'
// Types
import type { AlgoliaSearchHelper } from 'algoliasearch-helper'
const { t } = useI18n()
const { smAndUp, smAndDown, mdAndUp, xs, platform } = useDisplay()
const { query } = useRoute()
const user = useUserStore()
const list = ref<InstanceType<typeof SearchResults>>()
const model = ref(false)
const searchString = ref('')
const searchClient = algoliasearch(
'NHT6C0IV19', // docsearch app ID
'ffa344297924c76b0f4155384aff7ef2' // vuetify API key
)
const searches = ref(JSON.parse(localStorage.getItem('searches') || '[]'))
const locale = 'en'
watch(searches, val => {
localStorage.setItem('searches', JSON.stringify(val))
})
onMounted(() => {
document.addEventListener('keydown', onDocumentKeydown)
if (query?.search) {
searchString.value = query.search as string
model.value = true
}
})
onBeforeUnmount(() => {
document.removeEventListener('keydown', onDocumentKeydown)
})
onBeforeRouteLeave(() => {
model.value = false
})
function searchFunction (helper: AlgoliaSearchHelper) {
if (helper.state.query) helper.search()
}
function transformItems (items: any[]) {
// const sorted = sortItems([...items], ['hierarchy.lvl0', 'hierarchy.lvl1'], [false, false], locale)
items = items.map(item => {
const url = new URL(item.url)
return {
...item,
url: url.href.split(url.origin).pop(),
}
})
const groups = groupItems(items, 'lvl0')
groups.forEach(group => {
group.items = groupItems(group.items, 'lvl1')
})
// const uiIndex = groups.findIndex(val => val.name === 'UI Components')
// if (uiIndex > 0) {
// groups.unshift(groups.splice(uiIndex, 1)[0])
// }
return groups
}
function groupItems (items: any[], attribute: string) {
const groups: any[] = []
items.forEach(item => {
const group = groups.find(val => val.name === item.hierarchy[attribute])
if (group) {
group.items.push(item)
} else {
groups.push({
name: item.hierarchy[attribute],
items: [item],
})
}
})
return groups
}
function onDocumentKeydown (e: KeyboardEvent) {
const modifierKey = platform.value.mac ? e.metaKey : e.ctrlKey
const isSearchKey = user.slashSearch ? e.key === '/' : modifierKey && e.key === 'k'
if (!model.value && isSearchKey) {
e.preventDefault()
model.value = true
} else if (model.value && ['ArrowDown', 'ArrowUp'].includes(e.key)) {
e.preventDefault()
list.value?.rootEl?.focus()
}
}
function onClickDelete (index: number) {
const array = searches.value.slice(0, 6)
array.splice(index, 1)
searches.value = array
}
function onClickResult (result: any) {
const array = searches.value.slice(0, 6)
array.unshift(result)
searches.value = array
}
</script>
<style scoped>
:deep(.v-field--variant-solo) {
box-shadow: none;
}
</style>