/
Search.vue
175 lines (160 loc) · 3.92 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
<script setup lang="ts">
import { onBeforeRouteUpdate } from 'vue-router'
// eslint-disable-next-line @typescript-eslint/consistent-type-imports
import type { ResultItem } from '~/types'
import { input, isSearching, searchResult, selectIndex, userConfigLoading } from '~/composables/state'
const route = useRoute()
const router = useRouter()
const inputEl = $ref<HTMLInputElement>()
const vFocus = {
mounted: (el: HTMLElement) => el.focus(),
}
watch(
() => route.query.s,
async () => {
input.value = String(route.query.s || '')
await excuteSearch()
},
)
async function excuteSearch() {
if (input.value)
isSearching.value = true
try {
searchResult.value = await searcher.search(input.value)
}
catch (e) {
console.error(e)
}
isSearching.value = false
selectIndex.value = 0
isModalOpen.value = false
await router.replace({
path: '/',
query: input.value
? {
s: input.value,
}
: undefined,
})
}
throttledWatch(
input,
excuteSearch,
{ throttle: 100, immediate: true },
)
useEventListener('keydown', (e) => {
if (e.key === 'ArrowDown') {
moveIndex(1)
e.preventDefault()
}
else if (e.key === 'ArrowUp') {
moveIndex(-1)
e.preventDefault()
}
else if (e.key === 'Enter') {
const item = searchResult.value[selectIndex.value]
if (item)
openItem(item)
}
else if (e.key === 'Escape') {
clear()
}
// allow typing from everywhere to search
if (e.key.match(/^[\w:-]$/) && !e.metaKey && !e.ctrlKey && !e.altKey && !e.shiftKey)
inputEl?.focus()
})
onBeforeRouteUpdate(() => {
nextTick().then(() => {
inputEl?.focus()
})
})
function moveIndex(delta: number) {
selectIndex.value = (selectIndex.value + delta + searchResult.value.length) % searchResult.value.length
}
function clear() {
router.push('/')
nextTick().then(() => inputEl?.focus())
}
function openItem(item: ResultItem) {
if (isMobile.value && !isModalOpen.value)
isModalOpen.value = true
else
input.value = searcher.getItemId(item)
}
function selectItem(item: ResultItem) {
const index = searchResult.value.indexOf(item)
if (index < 0)
return
if (selectIndex.value !== index) {
selectIndex.value = index
if (isMobile.value && !isModalOpen.value)
isModalOpen.value = true
}
else {
openItem(item)
}
}
</script>
<template>
<div relative border="~ rounded base" shadow font-200 text-2xl>
<div v-if="userConfigLoading" p="x6 y4" gap2 row items-center animate-pulse>
<div i-carbon-circle-dash w-7 h-7 animate-spin />
<div op50>
loading config...
</div>
</div>
<input
v-else
ref="inputEl"
v-model="input"
v-focus
aria-label="Type to explore"
placeholder="Type to explore..."
type="text"
autocomplete="off" w="full"
p="x6 y4"
bg-transparent border-none
class="!outline-none"
>
<button
v-if="input"
absolute flex right-2 w-10 top-2 bottom-2 text-xl op30 hover:op90
aria-label="Clear search"
@click="clear()"
>
<span i-carbon-close ma block aria-hidden="true" />
</button>
</div>
<div v-if="searchResult.length || isSearching" border="l b r base" mx2 of-auto>
<template v-if="isSearching">
<ItemBase>
<template #badge>
<div i-carbon-circle-dash w-5 h-5 animate-spin ma />
</template>
<template #title>
Searching...
</template>
</ItemBase>
<div divider />
</template>
<template v-for="(i, idx) of searchResult" :key="idx">
<ResultItem
:item="i"
:active="selectIndex === idx"
@click="selectItem(i)"
/>
<div divider />
</template>
</div>
<Intro v-else-if="!input" />
<div v-else p10>
<div op40 italic mb5>
No result found
</div>
<div row justify-center>
<button btn @click="clear()">
Clear search
</button>
</div>
</div>
</template>