119 lines
3.4 KiB
Vue
119 lines
3.4 KiB
Vue
<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;
|
||
align?: "center" | "start" | "end";
|
||
isBgSolid?: boolean;
|
||
}>();
|
||
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"
|
||
:style="{
|
||
alignItems:
|
||
align === 'start'
|
||
? 'flex-start'
|
||
: align === 'end'
|
||
? 'flex-end'
|
||
: undefined,
|
||
background: isBgSolid ? 'var(--background)' : undefined,
|
||
}"
|
||
>
|
||
<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>
|