/
SearchPane.vue
303 lines (261 loc) · 6.76 KB
/
SearchPane.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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
<script lang="ts" setup>
import { useFocus, useDebounce } from '@vueuse/core'
import { computed, ref, watch } from 'vue'
import { Icon } from '@iconify/vue'
import * as flexsearch from 'flexsearch'
import charset from 'flexsearch/dist/module/lang/latin/advanced.js'
import language from 'flexsearch/dist/module/lang/en.js'
import { useStoryStore } from '../../stores/story'
import BaseEmpty from '../base/BaseEmpty.vue'
import type { SearchResult, SearchResultType, Story, Variant } from '../../types'
import SearchItem from './SearchItem.vue'
import { searchData, onUpdate } from './search-title-data'
import type { SearchData } from './types'
const DocSearchData = () => import('./search-docs-data')
const props = defineProps({
shown: {
type: Boolean,
default: false,
},
})
const emit = defineEmits({
close: () => true,
})
function close () {
emit('close')
}
// Autofocus
const input = ref<HTMLInputElement>()
const { focused } = useFocus(input, {
initialValue: true,
})
watch(() => props.shown, value => {
if (value) {
requestAnimationFrame(() => {
focused.value = true
input.value.select()
})
}
})
// Index
const searchInputText = ref('')
const rateLimitedSearch = useDebounce(searchInputText, 50)
const storyStore = useStoryStore()
let titleSearchIndex: flexsearch.Document<any, any>
let titleIdMap: SearchData['idMap']
function createIndex () {
return new flexsearch.Document({
preset: 'match',
document: {
id: 'id',
index: [
'text',
],
},
worker: true,
charset,
language,
tokenize: 'forward',
})
}
async function loadSearchIndex (data: SearchData) {
titleSearchIndex = createIndex()
for (const key of Object.keys(data.index)) {
await titleSearchIndex.import(key, data.index[key])
}
titleIdMap = data.idMap
}
loadSearchIndex(searchData)
// Handle HMR
onUpdate(searchData => {
loadSearchIndex(searchData)
})
let docSearchIndex: flexsearch.Document<any, any>
let docIdMap: SearchData['idMap']
async function loadDocSearchIndex () {
async function load (data: SearchData) {
docSearchIndex = createIndex()
for (const key of Object.keys(data.index)) {
await docSearchIndex.import(key, data.index[key])
}
docIdMap = data.idMap
if (rateLimitedSearch.value) {
searchOnDocField(rateLimitedSearch.value)
}
}
const searchDataModule = await DocSearchData()
load(searchDataModule.searchData)
// Handle HMR
searchDataModule.onUpdate(searchData => {
load(searchData)
})
}
loadDocSearchIndex()
// Search
const titleResults = ref<SearchResult[]>([])
watch(rateLimitedSearch, async value => {
const list: SearchResult[] = []
const raw = await titleSearchIndex.search(value)
let rank = 0
for (const field of raw) {
for (const id of field.result) {
const idMapData = titleIdMap[id]
if (!idMapData) continue
switch (idMapData.kind) {
case 'story': {
list.push(storyResultFactory(storyStore.getStoryById(idMapData.id), rank))
rank++
break
}
case 'variant': {
const [storyId] = idMapData.id.split(':')
const story = storyStore.getStoryById(storyId)
const variant = storyStore.getVariantById(idMapData.id)
list.push(variantResultFactory(story, variant, rank))
rank++
break
}
}
}
}
titleResults.value = list
})
const docsResults = ref<SearchResult[]>([])
async function searchOnDocField (query: string) {
if (docSearchIndex) {
const list: SearchResult[] = []
const raw = await docSearchIndex.search(query)
let rank = 0
for (const field of raw) {
for (const id of field.result) {
const idMapData = docIdMap[id]
if (!idMapData) continue
switch (idMapData.kind) {
case 'story': {
list.push(storyResultFactory(storyStore.getStoryById(idMapData.id), rank, 'docs'))
rank++
break
}
}
}
}
docsResults.value = list
}
}
watch(rateLimitedSearch, searchOnDocField)
function storyResultFactory (story: Story, rank: number, type: SearchResultType = 'title'): SearchResult {
return {
kind: 'story',
rank,
id: `story:${story.id}`,
title: story.title,
route: {
name: 'story',
params: {
storyId: story.id,
},
query: {
...type === 'docs'
? { tab: 'docs' }
: {},
},
},
path: story.file.path.slice(0, -1),
icon: story.icon,
iconColor: story.iconColor,
type,
}
}
function variantResultFactory (story: Story, variant: Variant, rank: number, type: SearchResultType = 'title'): SearchResult {
return {
kind: 'variant',
rank,
id: `variant:${story.id}:${variant.id}`,
title: variant.title,
route: {
name: 'story',
params: {
storyId: story.id,
},
query: {
variantId: variant.id,
...type === 'docs'
? { tab: 'docs' }
: {},
},
},
path: [...story.file.path ?? [], story.title],
icon: variant.icon,
iconColor: variant.iconColor,
type,
}
}
const results = computed(() => {
const list = [...titleResults.value]
const seen = {}
for (const r of titleResults.value) {
seen[r.id] = true
}
for (const r of docsResults.value) {
if (!seen[r.id]) {
list.push(r)
}
}
return list
})
// Selection
const selectedIndex = ref(0)
watch(results, () => {
selectedIndex.value = 0
})
function selectNext () {
selectedIndex.value++
if (selectedIndex.value > results.value.length - 1) {
selectedIndex.value = 0
}
}
function selectPrevious () {
selectedIndex.value--
if (selectedIndex.value < 0) {
selectedIndex.value = results.value.length - 1
}
}
</script>
<template>
<div
class="histoire-search-pane htw-flex htw-items-center htw-gap-4 htw-pl-6 htw-border htw-border-transparent focus-visible:htw-border-primary-500"
@click="focused = true"
>
<Icon
icon="carbon:search"
class="flex-none htw-w-4 htw-h-4"
/>
<input
ref="input"
v-model="searchInputText"
placeholder="Search for stories, variants..."
class="htw-bg-transparent htw-w-full htw-flex-1 htw-pl-0 htw-pr-6 htw-py-4 htw-outline-none"
@keydown.down.prevent="selectNext()"
@keydown.up.prevent="selectPrevious()"
@keydown.escape="close()"
>
</div>
<BaseEmpty
v-if="rateLimitedSearch && !results.length"
class="no-animation"
>
No results
</BaseEmpty>
<div
v-else-if="results.length"
class="htw-max-h-[400px] htw-overflow-y-auto htw-rounded-b-lg"
>
<SearchItem
v-for="(result, index) of results"
:key="result.id"
:result="result"
:selected="index === selectedIndex"
@close="close()"
/>
</div>
</template>