173 lines
4.7 KiB
Vue
173 lines
4.7 KiB
Vue
<script setup lang="ts">
|
||
import { IconCode, IconCopy, IconCheck } from "@tabler/icons-vue";
|
||
import {
|
||
computed,
|
||
ref,
|
||
watch,
|
||
onBeforeUnmount,
|
||
onMounted,
|
||
useId,
|
||
defineAsyncComponent,
|
||
} from "vue";
|
||
import { Button } from "@heroui-vue/components";
|
||
import { highlights } from "virtual:docs-data";
|
||
const props = defineProps<{
|
||
name: string;
|
||
align?: "center" | "start" | "end";
|
||
isBgSolid?: boolean;
|
||
hideCode?: boolean;
|
||
minHeight?: string;
|
||
description?: string;
|
||
}>();
|
||
const loaders = import.meta.glob("../../demos/*.vue", { import: "default" });
|
||
const demos = Object.fromEntries(
|
||
Object.entries(loaders).map(([name, loader]) => [
|
||
name,
|
||
defineAsyncComponent(loader as () => Promise<import("vue").Component>),
|
||
]),
|
||
);
|
||
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("");
|
||
const preview = ref<HTMLElement>();
|
||
let generation = 0;
|
||
let observer: IntersectionObserver | undefined;
|
||
async function highlight() {
|
||
const current = ++generation;
|
||
try {
|
||
const result = await highlights[props.name]?.();
|
||
if (current === generation) highlightedCode.value = result?.default ?? "";
|
||
} catch {
|
||
/* Plain same-SFC source remains available. */
|
||
}
|
||
}
|
||
watch(
|
||
() => props.name,
|
||
() => {
|
||
highlightedCode.value = "";
|
||
void highlight();
|
||
},
|
||
);
|
||
watch(expanded, (value) => {
|
||
if (value && !highlightedCode.value) void highlight();
|
||
});
|
||
onMounted(() => {
|
||
if (!preview.value || props.hideCode) return;
|
||
if (typeof IntersectionObserver === "undefined") {
|
||
void highlight();
|
||
return;
|
||
}
|
||
observer = new IntersectionObserver(
|
||
(entries) => {
|
||
if (entries.some((entry) => entry.isIntersecting)) {
|
||
void highlight();
|
||
observer?.disconnect();
|
||
}
|
||
},
|
||
{ rootMargin: "200px" },
|
||
);
|
||
observer.observe(preview.value);
|
||
});
|
||
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);
|
||
generation++;
|
||
observer?.disconnect();
|
||
});
|
||
</script>
|
||
<template>
|
||
<div class="component-preview-container" :data-name="name">
|
||
<p v-if="description" class="preview-description">{{ description }}</p>
|
||
<div
|
||
class="preview not-prose"
|
||
:class="{ 'preview-only': hideCode }"
|
||
:style="{
|
||
alignItems:
|
||
align === 'start'
|
||
? 'flex-start'
|
||
: align === 'end'
|
||
? 'flex-end'
|
||
: undefined,
|
||
background: isBgSolid ? 'var(--background)' : undefined,
|
||
}"
|
||
>
|
||
<div class="demo-row" :style="{ minHeight }">
|
||
<component :is="component" v-if="component" />
|
||
<p v-else role="alert">示例不存在:{{ name }}</p>
|
||
</div>
|
||
</div>
|
||
<div
|
||
v-if="!hideCode"
|
||
ref="preview"
|
||
class="code-section not-prose"
|
||
:data-expanded="expanded"
|
||
>
|
||
<Button
|
||
class="preview-copy"
|
||
variant="ghost"
|
||
size="sm"
|
||
is-icon-only
|
||
:aria-label="'复制' + name + ' 示例代码'"
|
||
@click="copy"
|
||
>
|
||
<IconCheck
|
||
v-if="copyStatus === '已复制'"
|
||
aria-hidden="true"
|
||
:size="16"
|
||
/>
|
||
<IconCopy v-else aria-hidden="true" :size="16" />
|
||
</Button>
|
||
<div
|
||
:id="id"
|
||
class="highlighted-source"
|
||
:aria-hidden="!expanded || undefined"
|
||
:tabindex="expanded ? 0 : undefined"
|
||
:aria-label="name + ' 示例源码'"
|
||
>
|
||
<div v-if="highlightedCode" v-html="highlightedCode" />
|
||
<pre
|
||
v-else
|
||
><code>{{expanded ? code : code.split('\n').slice(0,5).join('\n')}}</code></pre>
|
||
</div>
|
||
<Button
|
||
class="preview-expand"
|
||
size="sm"
|
||
variant="tertiary"
|
||
:aria-label="(expanded ? '收起' : '展开') + name + ' 示例代码'"
|
||
:aria-controls="id"
|
||
:aria-expanded="expanded"
|
||
@click="expanded = !expanded"
|
||
>
|
||
<IconCode :size="14" aria-hidden="true" />
|
||
{{ expanded ? "收起代码" : "展开代码" }}
|
||
</Button>
|
||
<p class="sr-only" role="status">
|
||
{{ copyStatus === "复制代码" ? "" : copyStatus }}
|
||
</p>
|
||
</div>
|
||
</div>
|
||
</template>
|