157 lines
4.9 KiB
Vue
157 lines
4.9 KiB
Vue
<script setup lang="ts">
|
||
import { IconSearch } from "@tabler/icons-vue";
|
||
import {
|
||
computed,
|
||
ref,
|
||
watch,
|
||
onMounted,
|
||
onBeforeUnmount,
|
||
nextTick,
|
||
} from "vue";
|
||
import { withBase, useRouter } from "vitepress";
|
||
import {
|
||
DialogRoot,
|
||
DialogTrigger,
|
||
DialogPortal,
|
||
DialogOverlay,
|
||
DialogContent,
|
||
DialogTitle,
|
||
DialogDescription,
|
||
DialogClose,
|
||
} from "reka-ui";
|
||
import { searchIndex } from "virtual:docs-data";
|
||
import { searchDocs, searchExcerpt } from "./search";
|
||
const router = useRouter();
|
||
const navigating = ref(false);
|
||
const open = ref(false),
|
||
query = ref(""),
|
||
active = ref(0),
|
||
field = ref<HTMLInputElement>();
|
||
const results = computed(() => searchDocs(searchIndex, query.value));
|
||
watch(query, () => {
|
||
active.value = 0;
|
||
});
|
||
watch(open, (value) => {
|
||
if (!value) query.value = "";
|
||
else navigating.value = false;
|
||
});
|
||
function shortcut(event: KeyboardEvent) {
|
||
if ((event.metaKey || event.ctrlKey) && event.key.toLowerCase() === "k") {
|
||
event.preventDefault();
|
||
open.value = !open.value;
|
||
}
|
||
}
|
||
function move(delta: number) {
|
||
if (!results.value.length) return;
|
||
active.value =
|
||
(active.value + delta + results.value.length) % results.value.length;
|
||
void nextTick(() =>
|
||
document
|
||
.getElementById("search-result-" + active.value)
|
||
?.scrollIntoView({ block: "nearest" }),
|
||
);
|
||
}
|
||
async function navigateTo(entry: (typeof searchIndex)[number]) {
|
||
navigating.value = true;
|
||
open.value = false;
|
||
await router.go(withBase(entry.url));
|
||
await nextTick();
|
||
const id = entry.url.split("#")[1];
|
||
const target = id
|
||
? document.getElementById(decodeURIComponent(id))
|
||
: document.querySelector<HTMLElement>(".docs-main h1");
|
||
target?.setAttribute("tabindex", "-1");
|
||
target?.focus({ preventScroll: true });
|
||
}
|
||
function navigate() {
|
||
const entry = results.value[active.value];
|
||
if (entry) void navigateTo(entry);
|
||
}
|
||
function closeAutoFocus(event: Event) {
|
||
if (navigating.value) event.preventDefault();
|
||
}
|
||
onMounted(() => window.addEventListener("keydown", shortcut));
|
||
onBeforeUnmount(() => window.removeEventListener("keydown", shortcut));
|
||
</script>
|
||
<template>
|
||
<DialogRoot v-model:open="open">
|
||
<DialogTrigger class="search-trigger" aria-label="搜索文档">
|
||
<IconSearch aria-hidden="true" :size="17" />
|
||
<span>搜索文档…</span>
|
||
<kbd>⌘ / Ctrl K</kbd>
|
||
</DialogTrigger>
|
||
<DialogPortal>
|
||
<DialogOverlay class="search-overlay" />
|
||
<DialogContent
|
||
class="search-dialog"
|
||
@open-auto-focus.prevent="field?.focus()"
|
||
@close-auto-focus="closeAutoFocus"
|
||
>
|
||
<DialogTitle class="sr-only">搜索文档</DialogTitle>
|
||
<DialogDescription class="sr-only">
|
||
搜索组件、参数和用法;使用方向键选择,回车打开,Esc 关闭。
|
||
</DialogDescription>
|
||
<div class="search-field">
|
||
<input
|
||
ref="field"
|
||
v-model="query"
|
||
placeholder="搜索组件、参数或用法…"
|
||
aria-label="搜索组件、参数或用法"
|
||
role="combobox"
|
||
aria-autocomplete="list"
|
||
aria-controls="doc-search-results"
|
||
:aria-expanded="Boolean(query.trim())"
|
||
:aria-activedescendant="
|
||
results.length ? 'search-result-' + active : undefined
|
||
"
|
||
@keydown.down.prevent="move(1)"
|
||
@keydown.up.prevent="move(-1)"
|
||
@keydown.enter.prevent="navigate"
|
||
/>
|
||
<DialogClose class="search-close" aria-label="关闭搜索">
|
||
Esc
|
||
</DialogClose>
|
||
</div>
|
||
<div
|
||
class="search-results"
|
||
id="doc-search-results"
|
||
role="listbox"
|
||
aria-label="搜索结果"
|
||
>
|
||
<p v-if="!query.trim()" class="search-empty">
|
||
试试搜索「isPending」「主题」或「受控」
|
||
</p>
|
||
<p v-else-if="!results.length" class="search-empty" role="status">
|
||
没有找到“{{ query }}”,试试组件名或参数名。
|
||
</p>
|
||
<a
|
||
v-for="(entry, index) in results"
|
||
:id="'search-result-' + index"
|
||
:key="entry.url"
|
||
:href="withBase(entry.url)"
|
||
role="option"
|
||
:aria-selected="index === active"
|
||
:tabindex="-1"
|
||
@mousemove="active = index"
|
||
@click.prevent="navigateTo(entry)"
|
||
>
|
||
<span class="result-title">
|
||
{{ entry.title }}
|
||
<span>{{ entry.section }}</span>
|
||
</span>
|
||
<span class="result-excerpt">
|
||
{{ searchExcerpt(entry, query) || "查看文档" }}
|
||
</span>
|
||
</a>
|
||
</div>
|
||
<div class="search-footer" aria-live="polite">
|
||
<span>
|
||
{{ query.trim() ? results.length + " 条结果" : "本地文档搜索" }}
|
||
</span>
|
||
<span>↑ ↓ 选择 ↵ 打开</span>
|
||
</div>
|
||
</DialogContent>
|
||
</DialogPortal>
|
||
</DialogRoot>
|
||
</template>
|