返回 slidev
Goto.vue
根目录 / packages / client / internals / Goto.vue
1 <script setup lang="ts">
2 import Fuse from 'fuse.js'
3 import { computed, ref, watch } from 'vue'
4 import TitleRenderer from '#slidev/title-renderer'
5 import { useNav } from '../composables/useNav'
6 import { activeElement, showGotoDialog } from '../state'
7
8 const container = ref<HTMLDivElement>()
9 const input = ref<HTMLInputElement>()
10 const list = ref<HTMLUListElement>()
11 const items = ref<HTMLLIElement[]>()
12 const text = ref('')
13 const selectedIndex = ref(0)
14
15 const { go, slides } = useNav()
16
17 function notNull<T>(value: T | null | undefined): value is T {
18 return value !== null && value !== undefined
19 }
20
21 const fuse = computed(() => new Fuse(slides.value.map(i => i.meta?.slide).filter(notNull), {
22 keys: ['no', 'title'],
23 threshold: 0.3,
24 shouldSort: true,
25 minMatchCharLength: 1,
26 }))
27
28 const path = computed(() => text.value.startsWith('/')
29 ? text.value.substring(1)
30 : text.value)
31 const result = computed(() => path.value
32 ? fuse.value.search(path.value).map(result => result.item)
33 : [])
34 const valid = computed(() => !!result.value.length)
35
36 function goTo() {
37 if (valid.value) {
38 const item = result.value.at(selectedIndex.value || 0)
39 if (item)
40 go(item.no)
41 }
42 close()
43 }
44
45 function close() {
46 text.value = ''
47 showGotoDialog.value = false
48 }
49
50 function focusDown(event: Event) {
51 event.preventDefault()
52 selectedIndex.value++
53 if (selectedIndex.value >= result.value.length)
54 selectedIndex.value = 0
55 scroll()
56 }
57
58 function focusUp(event: Event) {
59 event.preventDefault()
60 selectedIndex.value--
61 if (selectedIndex.value <= -2)
62 selectedIndex.value = result.value.length - 1
63 scroll()
64 }
65
66 function scroll() {
67 const item = items.value?.[selectedIndex.value]
68 if (item && list.value) {
69 if (item.offsetTop + item.offsetHeight > list.value.offsetHeight + list.value.scrollTop) {
70 list.value.scrollTo({
71 behavior: 'smooth',
72 top: item.offsetTop + item.offsetHeight - list.value.offsetHeight + 1,
73 })
74 }
75 else if (item.offsetTop < list.value.scrollTop) {
76 list.value.scrollTo({
77 behavior: 'smooth',
78 top: item.offsetTop,
79 })
80 }
81 }
82 }
83
84 function updateText(event: Event) {
85 selectedIndex.value = 0
86 text.value = (event.target as HTMLInputElement).value
87 }
88
89 function select(no: number) {
90 go(no)
91 close()
92 }
93
94 watch(showGotoDialog, async (show) => {
95 if (show) {
96 text.value = ''
97 selectedIndex.value = 0
98 // delay the focus to avoid the g character coming from the key that triggered showGotoDialog
99 setTimeout(() => input.value?.focus(), 0)
100 }
101 else {
102 input.value?.blur()
103 }
104 })
105
106 watch(activeElement, () => {
107 if (!container.value?.contains(activeElement.value as Node))
108 close()
109 })
110 </script>
111
112 <template>
113 <div
114 id="slidev-goto-dialog"
115 ref="container"
116 class="fixed right-5 transition-all"
117 w-90 max-w-90 min-w-90
118 :class="showGotoDialog ? 'top-5' : '-top-20 op0 pointer-events-none'"
119 >
120 <div
121 class="bg-main transform"
122 shadow="~"
123 p="x-4 y-2"
124 border="~ transparent rounded dark:main"
125 >
126 <input
127 id="slidev-goto-input"
128 ref="input"
129 :value="text"
130 type="text"
131 :disabled="!showGotoDialog"
132 class="outline-none bg-transparent"
133 placeholder="Goto..."
134 :class="{ 'text-red-400': !valid && text }"
135 @keydown.enter="goTo"
136 @keydown.escape="close"
137 @keydown.down="focusDown"
138 @keydown.up="focusUp"
139 @input="updateText"
140 >
141 </div>
142 <div
143 v-if="showGotoDialog && result.length > 0"
144 ref="list"
145 class="autocomplete-list"
146 shadow="~"
147 border="~ transparent rounded dark:main"
148 >
149 <ul table w-full border-collapse>
150 <li
151 v-for="(item, index) of result"
152 ref="items"
153 :key="item.id"
154 role="button"
155 tabindex="0"
156 cursor-pointer
157 hover="op100"
158 table-row
159 items-center
160 :border="index === 0 ? undefined : 't main'"
161 :class="selectedIndex === index ? 'bg-active op100' : 'op80'"
162 @click.stop.prevent="select(item.no)"
163 >
164 <div text-right op50 text-sm table-cell py-2 pl-4 pr-3 vertical-middle>
165 {{ item.no }}
166 </div>
167 <TitleRenderer table-cell py-2 pr-4 w-full :no="item.no" />
168 </li>
169 </ul>
170 </div>
171 </div>
172 </template>
173
174 <style scoped>
175 .autocomplete-list {
176 --uno: bg-main mt-1;
177 overflow: auto;
178 max-height: calc(100vh - 100px);
179 }
180
181 .autocomplete {
182 cursor: pointer;
183 }
184 </style>
185
185 lines Plain Text