216 lines
6.7 KiB
Vue
216 lines
6.7 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 {
|
||
Button,
|
||
Link,
|
||
Kbd,
|
||
Modal,
|
||
ModalTrigger,
|
||
ModalBackdrop,
|
||
ModalContainer,
|
||
ModalDialog,
|
||
ModalHeading,
|
||
SearchField,
|
||
SearchFieldGroup,
|
||
SearchFieldInput,
|
||
SearchFieldSearchIcon,
|
||
EmptyState,
|
||
} from "@heroui-vue/components";
|
||
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<{ focus: () => void }>();
|
||
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.defaultPrevented &&
|
||
!event.isComposing &&
|
||
!event.repeat &&
|
||
(event.metaKey || event.ctrlKey) &&
|
||
event.key.toLowerCase() === "k"
|
||
) {
|
||
event.preventDefault();
|
||
open.value = !open.value;
|
||
}
|
||
}
|
||
function keydown(event: KeyboardEvent) {
|
||
if (event.defaultPrevented || event.isComposing || event.keyCode === 229)
|
||
return;
|
||
if (event.key === "ArrowDown" || event.key === "ArrowUp") {
|
||
event.preventDefault();
|
||
move(event.key === "ArrowDown" ? 1 : -1);
|
||
} else if (event.key === "Enter") {
|
||
event.preventDefault();
|
||
navigate();
|
||
} else if (event.key === "Escape") {
|
||
event.preventDefault();
|
||
open.value = false;
|
||
}
|
||
}
|
||
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 guardEscape(event: KeyboardEvent) {
|
||
if (event.isComposing) event.preventDefault();
|
||
}
|
||
function closeAutoFocus(event: Event) {
|
||
if (navigating.value) event.preventDefault();
|
||
}
|
||
onMounted(() => window.addEventListener("keydown", shortcut));
|
||
onBeforeUnmount(() => window.removeEventListener("keydown", shortcut));
|
||
</script>
|
||
<template>
|
||
<Modal v-model:open="open">
|
||
<ModalTrigger as-child>
|
||
<Button class="search-trigger" variant="secondary" aria-label="搜索文档">
|
||
<IconSearch aria-hidden="true" :size="17" />
|
||
<span>搜索文档…</span>
|
||
<Kbd>⌘ / Ctrl K</Kbd>
|
||
</Button>
|
||
</ModalTrigger>
|
||
<ModalBackdrop class="docs-search-backdrop" variant="blur">
|
||
<ModalContainer placement="top" size="lg" class="docs-search-container">
|
||
<ModalDialog
|
||
class="search-dialog"
|
||
@open-auto-focus.prevent="field?.focus()"
|
||
@close-auto-focus="closeAutoFocus"
|
||
@escape-key-down="guardEscape"
|
||
>
|
||
<ModalHeading class="sr-only">搜索文档</ModalHeading>
|
||
<p class="sr-only" id="docs-search-instructions">
|
||
搜索组件、参数和用法;使用方向键选择,回车打开,Esc 关闭。
|
||
</p>
|
||
<div class="search-field">
|
||
<SearchField
|
||
v-model="query"
|
||
class="docs-search-input"
|
||
aria-label="搜索组件、参数或用法"
|
||
>
|
||
<SearchFieldGroup>
|
||
<SearchFieldSearchIcon />
|
||
<SearchFieldInput
|
||
ref="field"
|
||
placeholder="搜索组件、参数或用法…"
|
||
aria-label="搜索组件、参数或用法"
|
||
role="combobox"
|
||
aria-autocomplete="list"
|
||
aria-describedby="docs-search-instructions"
|
||
aria-controls="doc-search-results"
|
||
:aria-expanded="Boolean(query.trim())"
|
||
:aria-activedescendant="
|
||
query.trim() && results.length
|
||
? 'search-result-' + active
|
||
: undefined
|
||
"
|
||
@keydown="keydown"
|
||
/>
|
||
</SearchFieldGroup>
|
||
</SearchField>
|
||
<Button
|
||
variant="ghost"
|
||
size="sm"
|
||
class="search-close"
|
||
aria-label="关闭搜索"
|
||
@click="open = false"
|
||
>
|
||
<Kbd>Esc</Kbd>
|
||
</Button>
|
||
</div>
|
||
<div
|
||
class="search-results"
|
||
id="doc-search-results"
|
||
role="listbox"
|
||
aria-label="搜索结果"
|
||
>
|
||
<EmptyState v-if="!query.trim()" class="search-empty">
|
||
<IconSearch :size="24" aria-hidden="true" />
|
||
<p>试试搜索「isPending」「主题」或「受控」</p>
|
||
</EmptyState>
|
||
<EmptyState
|
||
v-else-if="!results.length"
|
||
class="search-empty"
|
||
role="status"
|
||
>
|
||
<p>没有找到“{{ query }}”,试试组件名或参数名。</p>
|
||
</EmptyState>
|
||
<Link
|
||
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>
|
||
</Link>
|
||
</div>
|
||
<div class="search-footer" aria-live="polite">
|
||
<span>
|
||
{{ query.trim() ? results.length + " 条结果" : "本地文档搜索" }}
|
||
</span>
|
||
<span>
|
||
<Kbd>↑</Kbd>
|
||
<Kbd>↓</Kbd>
|
||
选择
|
||
<Kbd>↵</Kbd>
|
||
打开
|
||
</span>
|
||
</div>
|
||
</ModalDialog>
|
||
</ModalContainer>
|
||
</ModalBackdrop>
|
||
</Modal>
|
||
</template>
|