feat: initialize Vue component library and aligned documentation
This commit is contained in:
@@ -0,0 +1,103 @@
|
||||
<script setup lang="ts">
|
||||
import { IconCode, IconCopy } from "@tabler/icons-vue";
|
||||
import { computed, ref, watch, onBeforeUnmount, useId } from "vue";
|
||||
import { highlights } from "virtual:docs-data";
|
||||
const props = defineProps<{ name: string }>();
|
||||
const demos = import.meta.glob("../../demos/*.vue", {
|
||||
eager: true,
|
||||
import: "default",
|
||||
});
|
||||
const sources = import.meta.glob("../../demos/*.vue", {
|
||||
eager: true,
|
||||
query: "?raw",
|
||||
import: "default",
|
||||
});
|
||||
const component = computed(() => demos[`../../demos/${props.name}.vue`]);
|
||||
const code = computed(() =>
|
||||
String(sources[`../../demos/${props.name}.vue`] ?? ""),
|
||||
);
|
||||
const expanded = ref(false),
|
||||
copyStatus = ref("复制代码");
|
||||
const id = useId();
|
||||
const highlightedCode = ref("");
|
||||
watch([expanded, () => props.name], async ([open, name], _, onCleanup) => {
|
||||
let current = true;
|
||||
onCleanup(() => {
|
||||
current = false;
|
||||
});
|
||||
highlightedCode.value = "";
|
||||
if (open) {
|
||||
try {
|
||||
const result = await highlights[name]?.();
|
||||
if (current) highlightedCode.value = result?.default ?? "";
|
||||
} catch {
|
||||
// The plain source remains available if a highlight chunk cannot load.
|
||||
}
|
||||
}
|
||||
});
|
||||
let timer: ReturnType<typeof setTimeout> | undefined;
|
||||
async function copy() {
|
||||
clearTimeout(timer);
|
||||
try {
|
||||
await navigator.clipboard.writeText(code.value);
|
||||
copyStatus.value = "已复制";
|
||||
timer = setTimeout(() => {
|
||||
copyStatus.value = "复制代码";
|
||||
}, 2000);
|
||||
} catch {
|
||||
expanded.value = true;
|
||||
copyStatus.value = "复制失败,请手动选择代码";
|
||||
}
|
||||
}
|
||||
onBeforeUnmount(() => clearTimeout(timer));
|
||||
</script>
|
||||
<template>
|
||||
<div class="component-preview-container" :data-name="name">
|
||||
<div class="preview not-prose">
|
||||
<div class="demo-row">
|
||||
<component :is="component" v-if="component" />
|
||||
<p v-else role="alert">示例不存在:{{ name }}</p>
|
||||
</div>
|
||||
</div>
|
||||
<div class="code-section">
|
||||
<div class="code-toolbar">
|
||||
<span class="code-language">
|
||||
<span class="code-dot" aria-hidden="true" />
|
||||
Vue
|
||||
</span>
|
||||
<div>
|
||||
<button
|
||||
:aria-label="(expanded ? '收起' : '展开') + name + ' 示例代码'"
|
||||
:aria-controls="id"
|
||||
:aria-expanded="expanded"
|
||||
@click="expanded = !expanded"
|
||||
>
|
||||
<IconCode aria-hidden="true" :size="14" />
|
||||
{{ expanded ? "收起代码" : "展开代码" }}
|
||||
</button>
|
||||
<button :aria-label="'复制' + name + ' 示例代码'" @click="copy">
|
||||
<IconCopy aria-hidden="true" :size="14" />
|
||||
复制代码
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
<p
|
||||
class="copy-feedback"
|
||||
:class="{ 'sr-only': copyStatus === '复制代码' }"
|
||||
role="status"
|
||||
>
|
||||
{{ copyStatus === "复制代码" ? "" : copyStatus }}
|
||||
</p>
|
||||
<div
|
||||
v-if="expanded"
|
||||
:id="id"
|
||||
class="highlighted-source"
|
||||
tabindex="0"
|
||||
:aria-label="name + ' 示例源码'"
|
||||
>
|
||||
<div v-if="highlightedCode" v-html="highlightedCode" />
|
||||
<pre v-else><code>{{ code }}</code></pre>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
Reference in New Issue
Block a user