149 lines
3.4 KiB
Vue
149 lines
3.4 KiB
Vue
<template>
|
||
<div class="tui-demo">
|
||
<section class="stage">
|
||
<slot />
|
||
</section>
|
||
|
||
<section class="meta">
|
||
<div class="info">
|
||
<p v-if="title" class="title" v-text="title" />
|
||
<p v-if="description" class="desc" v-text="description" />
|
||
</div>
|
||
<div class="actions">
|
||
<t-button size="small" variant="text" @click="handleCopy">
|
||
复制代码
|
||
</t-button>
|
||
<t-button size="small" variant="text" @click="toggleExpanded">
|
||
{{ expanded ? "收起代码" : "展开代码" }}
|
||
</t-button>
|
||
</div>
|
||
</section>
|
||
|
||
<section v-show="expanded" class="source">
|
||
<div class="language-vue" v-html="sourceHtml" />
|
||
</section>
|
||
</div>
|
||
</template>
|
||
|
||
<script lang="ts" setup>
|
||
import { computed, ref } from "vue";
|
||
import { MessagePlugin } from "tdesign-vue-next";
|
||
|
||
/**
|
||
* 预览容器。
|
||
*
|
||
* props 契约由 `@vitepress-demo-preview/plugin` 决定,它把 markdown 里的
|
||
* `:::preview` 容器编译成 `<demo-preview>` 标签并传入下列属性:
|
||
* - `code`:`encodeURI` 处理过的组件源码
|
||
* - `showCode`:`encodeURIComponent` 处理过的高亮 HTML
|
||
*
|
||
* 这里刻意只依赖 TDesign,不引入官方的 AntDesign / ElementPlus / NaiveUI 容器,
|
||
* 那些容器会额外带来 vue-sonner 与 floating-vue 两套 UI 库。
|
||
*/
|
||
const props = withDefaults(defineProps<{
|
||
/** 源码原文,经 encodeURI 编码。 */
|
||
code?: string;
|
||
/** 高亮后的源码 HTML,经 encodeURIComponent 编码。 */
|
||
showCode?: string;
|
||
/** 演示标题,来自 `:::preview` 后方第一段文字。 */
|
||
title?: string;
|
||
/** 演示描述,来自 `:::preview` 中 `||` 之后的内容。 */
|
||
description?: string;
|
||
}>(), {
|
||
code: "",
|
||
showCode: "",
|
||
title: "",
|
||
description: ""
|
||
});
|
||
|
||
/** 源码区是否展开,默认收起以保持文档阅读节奏。 */
|
||
const expanded = ref(false);
|
||
|
||
const sourceHtml = computed(() => decodeURIComponent(props.showCode));
|
||
const sourceText = computed(() => decodeURIComponent(props.code));
|
||
|
||
/** 复制源码到剪贴板,clipboard 不可用时提示手动复制。 */
|
||
async function handleCopy() {
|
||
try {
|
||
await navigator.clipboard.writeText(sourceText.value);
|
||
MessagePlugin.success("代码已复制到剪贴板");
|
||
} catch {
|
||
MessagePlugin.warning("当前环境不支持自动复制,请手动选择代码");
|
||
}
|
||
}
|
||
|
||
function toggleExpanded() {
|
||
expanded.value = !expanded.value;
|
||
}
|
||
</script>
|
||
|
||
<style lang="less" scoped>
|
||
.tui-demo {
|
||
overflow: hidden;
|
||
border: 1px solid var(--td-component-stroke);
|
||
border-radius: .75rem;
|
||
background: var(--td-bg-color-container);
|
||
|
||
.stage {
|
||
padding: 2rem;
|
||
}
|
||
|
||
.meta {
|
||
display: flex;
|
||
gap: 1rem;
|
||
align-items: center;
|
||
justify-content: space-between;
|
||
padding: .75rem 1.5rem;
|
||
border-top: 1px solid var(--td-component-stroke);
|
||
background: var(--td-bg-color-container-hover);
|
||
}
|
||
|
||
.info {
|
||
min-width: 0;
|
||
}
|
||
|
||
.title {
|
||
margin: 0;
|
||
font-size: .875rem;
|
||
font-weight: 600;
|
||
color: var(--td-text-color-primary);
|
||
}
|
||
|
||
.desc {
|
||
margin: .25rem 0 0;
|
||
font-size: .75rem;
|
||
line-height: 1.5;
|
||
color: var(--td-text-color-secondary);
|
||
}
|
||
|
||
.actions {
|
||
display: flex;
|
||
flex: none;
|
||
gap: .25rem;
|
||
}
|
||
|
||
.source {
|
||
border-top: 1px solid var(--td-component-stroke);
|
||
|
||
:deep(div[class*="language-"]) {
|
||
margin: 0;
|
||
border-radius: 0;
|
||
}
|
||
}
|
||
}
|
||
|
||
@media (max-width: 48rem) {
|
||
.tui-demo {
|
||
.stage {
|
||
padding: 1rem;
|
||
}
|
||
|
||
.meta {
|
||
align-items: flex-start;
|
||
flex-direction: column;
|
||
padding: .75rem 1rem;
|
||
}
|
||
}
|
||
}
|
||
</style>
|