add vitepress doc

This commit is contained in:
Timi
2026-09-21 15:32:12 +08:00
parent 2fc96083c4
commit efd683e808
81 changed files with 7319 additions and 90 deletions
@@ -0,0 +1,162 @@
<template>
<div v-if="api" class="tui-api">
<section v-if="api.props.length > 0">
<h3>Props</h3>
<table>
<thead>
<tr>
<th>名称</th>
<th>类型</th>
<th>默认值</th>
<th>说明</th>
</tr>
</thead>
<tbody>
<tr v-for="row in api.props" :key="row.name">
<td class="name">
<code v-text="row.name" />
<span v-if="row.required" class="required" title="必填">*</span>
</td>
<td><code v-text="row.type" /></td>
<td><code v-text="row.defaultValue" /></td>
<td v-text="row.description" />
</tr>
</tbody>
</table>
</section>
<section v-if="api.events.length > 0">
<h3>Events</h3>
<table>
<thead>
<tr>
<th>名称</th>
<th>参数</th>
<th>说明</th>
</tr>
</thead>
<tbody>
<tr v-for="row in api.events" :key="row.name">
<td><code v-text="row.name" /></td>
<td><code v-text="row.type" /></td>
<td v-text="row.description" />
</tr>
</tbody>
</table>
</section>
<section v-if="api.slots.length > 0">
<h3>Slots</h3>
<table>
<thead>
<tr>
<th>名称</th>
<th>说明</th>
</tr>
</thead>
<tbody>
<tr v-for="row in api.slots" :key="row.name">
<td><code v-text="row.name" /></td>
<td v-text="row.description" />
</tr>
</tbody>
</table>
</section>
<section v-if="api.exposes.length > 0">
<h3>Expose</h3>
<table>
<thead>
<tr>
<th>名称</th>
<th>类型</th>
<th>说明</th>
</tr>
</thead>
<tbody>
<tr v-for="row in api.exposes" :key="row.name">
<td><code v-text="row.name" /></td>
<td><code v-text="row.type" /></td>
<td v-text="row.description" />
</tr>
</tbody>
</table>
</section>
<p v-if="isEmpty && !api.parsed" class="notice warn">
未能从源码中解析出 API 信息,请检查组件是否使用了 defineProps / defineEmits / defineExpose。
</p>
<p v-else-if="isEmpty" class="notice">
该组件不需要任何 props、events、slots 或实例方法。
</p>
</div>
<p v-else class="notice warn" v-text="`未找到组件 ${name} 的 API 数据,请检查 components.ts 中的 name 与 entry 是否正确`" />
</template>
<script lang="ts" setup>
import { computed } from "vue";
import apiMap from "virtual:timi-api";
/**
* 组件 API 表格。
*
* 数据来自构建期 `vue-docgen-api` 对组件源码的解析结果,
* 无需在 markdown 中手写 props / events / slots 表格。
* 某个分区没有内容时整段不渲染,避免出现空表。
*/
const props = defineProps<{
/** 组件名,需与 components.ts 中的 name 一致。 */
name: string;
}>();
const api = computed(() => apiMap[props.name]);
const isEmpty = computed(() => {
if (!api.value) {
return false;
}
const { props: propRows, events, slots, exposes } = api.value;
return propRows.length === 0 && events.length === 0 && slots.length === 0 && exposes.length === 0;
});
</script>
<style lang="less" scoped>
.tui-api {
section {
margin-bottom: 1.5rem;
&:last-child {
margin-bottom: 0;
}
}
h3 {
margin: 1.5rem 0 .75rem;
font-size: 1rem;
}
.name {
white-space: nowrap;
}
.required {
margin-left: .125rem;
color: var(--vp-c-danger-1);
}
.notice {
margin: 1rem 0;
padding: .75rem 1rem;
font-size: .875rem;
border-radius: .5rem;
color: var(--vp-c-text-2);
background: var(--vp-c-bg-soft);
&.warn {
color: var(--vp-c-warning-1);
background: var(--vp-c-warning-soft);
}
}
}
</style>
@@ -0,0 +1,149 @@
<template>
<div v-if="api" class="tui-utils-api">
<section v-if="api.members.length > 0">
<h3>成员</h3>
<table>
<thead>
<tr>
<th>名称</th>
<th>签名</th>
<th>说明</th>
</tr>
</thead>
<tbody>
<tr v-for="row in api.members" :key="row.name">
<td class="name"><code v-text="row.name" /></td>
<td class="signature"><code v-text="row.signature" /></td>
<td class="desc" v-text="row.description" />
</tr>
</tbody>
</table>
</section>
<section v-if="api.types.length > 0">
<h3>类型</h3>
<div v-for="item in api.types" :key="item.name" class="type">
<p class="head">
<code v-text="item.name" />
<span class="kind" v-text="item.kind" />
</p>
<p v-if="item.description" class="desc" v-text="item.description" />
<pre class="body"><code v-text="item.text" /></pre>
</div>
</section>
<p v-if="isEmpty && !api.parsed" class="notice warn">
未能从源码中解析出该模块的成员,请检查文件是否为合法的 TypeScript 模块。
</p>
<p v-else-if="isEmpty" class="notice">
该模块没有可列出的导出成员。
</p>
</div>
<p v-else class="notice warn" v-text="`未找到模块 ${name} 的数据,请检查 utils.ts 中的 name 与 entry 是否正确`" />
</template>
<script lang="ts" setup>
import { computed } from "vue";
import utilsMap from "virtual:timi-utils";
/**
* 工具模块成员表。
*
* 数据由构建期 `plugins/tsdocgen.ts` 解析 TS 源码得到,通过虚拟模块
* `virtual:timi-utils` 交给页面渲染。`vue-docgen-api` 只支持 SFC,
* 因此工具模块走的是 TypeScript 自带的 AST 解析。
*/
const props = defineProps<{
/** 模块名,需与 utils.ts 中的 name 一致。 */
name: string;
}>();
const api = computed(() => utilsMap[props.name]);
const isEmpty = computed(() => {
if (!api.value) {
return false;
}
return api.value.members.length === 0 && api.value.types.length === 0;
});
</script>
<style lang="less" scoped>
.tui-utils-api {
section {
margin-bottom: 1.5rem;
&:last-child {
margin-bottom: 0;
}
}
h3 {
margin: 1.5rem 0 .75rem;
font-size: 1rem;
}
.name,
.signature {
white-space: nowrap;
}
// 说明里保留了 JSDoc 的段落结构
.desc {
white-space: pre-line;
}
.type {
margin-bottom: 1rem;
&:last-child {
margin-bottom: 0;
}
.head {
display: flex;
gap: .5rem;
align-items: center;
margin: 0 0 .25rem;
}
.desc {
margin: 0 0 .5rem;
font-size: .875rem;
color: var(--vp-c-text-2);
}
.kind {
padding: .0625rem .375rem;
font-size: .75rem;
border-radius: .25rem;
color: var(--vp-c-text-2);
background: var(--vp-c-bg-soft);
}
.body {
margin: 0;
padding: .75rem 1rem;
overflow-x: auto;
font-size: .8125rem;
border-radius: .5rem;
background: var(--vp-c-bg-soft);
}
}
.notice {
margin: 1rem 0;
padding: .75rem 1rem;
font-size: .875rem;
border-radius: .5rem;
color: var(--vp-c-text-2);
background: var(--vp-c-bg-soft);
&.warn {
color: var(--vp-c-warning-1);
background: var(--vp-c-warning-soft);
}
}
}
</style>
@@ -0,0 +1,184 @@
<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">
<button
class="action"
type="button"
@click="handleCopy"
v-text="copied ? '已复制' : '复制代码'"
/>
<button
class="action"
type="button"
@click="toggleExpanded"
v-text="expanded ? '收起代码' : '展开代码'"
/>
</div>
</section>
<section v-show="expanded" class="source">
<div class="language-vue" v-html="sourceHtml" />
</section>
</div>
</template>
<script lang="ts" setup>
import { computed, onUnmounted, ref } from "vue";
/**
* 预览容器。
*
* props 契约由 `@vitepress-demo-preview/plugin` 决定,它把 markdown 里的
* `:::preview` 容器编译成 `<demo-preview>` 标签并传入下列属性:
* - `code`:`encodeURI` 处理过的组件源码
* - `showCode`:`encodeURIComponent` 处理过的高亮 HTML
*
* 这里刻意不依赖官方容器,也不引入任何 UI 库(timi-web 本身不依赖 TDesign 等框架),
* 只用原生元素配合 VitePress 的 CSS 变量,深色模式随之自动适配。
*/
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 copied = ref(false);
let copiedTimer: ReturnType<typeof setTimeout> | null = null;
const sourceHtml = computed(() => decodeURIComponent(props.showCode));
const sourceText = computed(() => decodeURIComponent(props.code));
/** 复制源码到剪贴板,clipboard 不可用时保持原按钮文案。 */
async function handleCopy() {
try {
await navigator.clipboard.writeText(sourceText.value);
} catch {
return;
}
copied.value = true;
if (copiedTimer !== null) {
clearTimeout(copiedTimer);
}
copiedTimer = setTimeout(() => {
copied.value = false;
copiedTimer = null;
}, 1500);
}
function toggleExpanded() {
expanded.value = !expanded.value;
}
onUnmounted(() => {
if (copiedTimer !== null) {
clearTimeout(copiedTimer);
}
});
</script>
<style lang="less" scoped>
.tui-demo {
overflow: hidden;
border: 1px solid var(--vp-c-divider);
border-radius: .75rem;
background: var(--vp-c-bg);
.stage {
padding: 2rem;
}
.meta {
display: flex;
gap: 1rem;
align-items: center;
justify-content: space-between;
padding: .75rem 1.5rem;
border-top: 1px solid var(--vp-c-divider);
background: var(--vp-c-bg-soft);
}
.info {
min-width: 0;
}
.title {
margin: 0;
font-size: .875rem;
font-weight: 600;
color: var(--vp-c-text-1);
}
.desc {
margin: .25rem 0 0;
font-size: .75rem;
line-height: 1.5;
color: var(--vp-c-text-2);
}
.actions {
display: flex;
flex: none;
gap: .25rem;
}
.action {
padding: .25rem .5rem;
font-size: .75rem;
cursor: pointer;
border: none;
border-radius: .25rem;
color: var(--vp-c-text-2);
background: transparent;
&:hover {
color: var(--vp-c-brand-1);
background: var(--vp-c-default-soft);
}
}
.source {
border-top: 1px solid var(--vp-c-divider);
: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>
+24
View File
@@ -0,0 +1,24 @@
/// <reference types="vitepress/client" />
/** 构建期由 vue-docgen-api 解析生成的组件 API 数据。 */
declare module "virtual:timi-api" {
import type { ComponentApiMap } from "../api-types";
const apiMap: ComponentApiMap;
export default apiMap;
}
/** 构建期由 TypeScript AST 解析生成的工具模块数据。 */
declare module "virtual:timi-utils" {
import type { UtilApiMap } from "../utils-types";
const utilsMap: UtilApiMap;
export default utilsMap;
}
declare module "*.vue" {
import type { DefineComponent } from "vue";
const component: DefineComponent<Record<string, unknown>, Record<string, unknown>, unknown>;
export default component;
}
+28
View File
@@ -0,0 +1,28 @@
import DefaultTheme from "vitepress/theme";
import type { App } from "vue";
import "./styles/index.less";
import DemoPreview from "./containers/DemoPreview.vue";
import ApiTable from "./components/ApiTable.vue";
import UtilsApiTable from "./components/UtilsApiTable.vue";
/**
* 文档站主题。
*
* 刻意不在这里 import timi-web:组件的依赖(Prism、ResizeObserver、document 等)
* 只适合在浏览器里运行,而演示统一以 `clientOnly` 延迟渲染,
* 让 timi-web 只出现在演示的动态导入里,SSR 阶段完全不加载它。
*
* 同理也不注册 `v-popup` 全局指令——需要它的演示在 `<script setup>` 里
* 导入 `VPopup` 即可,编译器会把它解析为局部绑定。
*/
export default {
extends: DefaultTheme,
enhanceApp({ app }: { app: App }) {
app.component("demo-preview", DemoPreview);
app.component("api-table", ApiTable);
app.component("utils-api", UtilsApiTable);
}
};
+18
View File
@@ -0,0 +1,18 @@
/**
* 文档站全局样式。
*
* 只处理 VitePress 正文与演示容器、API 表格的交界处;
* 组件自身的样式一律各自 scoped,避免相互污染。
*/
.vp-doc {
// 演示容器与正文的呼吸感
.tui-demo {
margin: 1rem 0;
}
// API 表格里的类型与默认值不换行,避免长签名把列挤变形
table code {
white-space: nowrap;
}
}