20261007220117
This commit is contained in:
1 parent
9217b3fa42
commit
8b1ec67609
275 files changed
+20026
-49299
No files matched your search
@@ -0,0 +1,137 @@
|
||||
<script setup lang="ts">
|
||||
import { computed } from 'vue'
|
||||
|
||||
interface ApiEntry {
|
||||
name: string
|
||||
description: string
|
||||
type: string
|
||||
defaultValue?: string
|
||||
}
|
||||
|
||||
interface ApiDoc {
|
||||
displayName: string
|
||||
description: string
|
||||
props: ApiEntry[]
|
||||
events: ApiEntry[]
|
||||
slots: ApiEntry[]
|
||||
}
|
||||
|
||||
const props = defineProps<{
|
||||
/** 组件名(与 packages/ui/src/<name>/ 目录一致),例如 "button" */
|
||||
name: string
|
||||
}>()
|
||||
|
||||
/**
|
||||
* 数据由 scripts/gen-api.mjs 从组件源码生成,不要手写。
|
||||
* 源码改动后执行 `pnpm gen:api` 即可刷新。
|
||||
*/
|
||||
const data = import.meta.glob('../../../data/api/*.json', { eager: true }) as Record<
|
||||
string,
|
||||
{ default: ApiDoc }
|
||||
>
|
||||
|
||||
const doc = computed<ApiDoc | undefined>(() => {
|
||||
const key = Object.keys(data).find((path) => path.endsWith(`/${props.name}.json`))
|
||||
return key ? data[key].default : undefined
|
||||
})
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div v-if="doc" class="g3-api">
|
||||
<p v-if="doc.description" class="g3-api__desc">{{ doc.description }}</p>
|
||||
|
||||
<template v-if="doc.props.length">
|
||||
<h3>属性</h3>
|
||||
<table>
|
||||
<thead>
|
||||
<tr><th>属性</th><th>说明</th><th>类型</th><th>默认值</th></tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr v-for="item in doc.props" :key="item.name">
|
||||
<td><code>{{ item.name }}</code></td>
|
||||
<td>{{ item.description }}</td>
|
||||
<td><code>{{ item.type }}</code></td>
|
||||
<td><code>{{ item.defaultValue }}</code></td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
</template>
|
||||
|
||||
<template v-if="doc.events.length">
|
||||
<h3>事件</h3>
|
||||
<table>
|
||||
<thead>
|
||||
<tr><th>事件名</th><th>说明</th><th>类型</th></tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr v-for="item in doc.events" :key="item.name">
|
||||
<td><code>{{ item.name }}</code></td>
|
||||
<td>{{ item.description }}</td>
|
||||
<td><code>{{ item.type }}</code></td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
</template>
|
||||
|
||||
<template v-if="doc.slots.length">
|
||||
<h3>插槽</h3>
|
||||
<table>
|
||||
<thead>
|
||||
<tr><th>插槽名</th><th>说明</th></tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr v-for="item in doc.slots" :key="item.name">
|
||||
<td><code>{{ item.name }}</code></td>
|
||||
<td>{{ item.description }}</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
</template>
|
||||
</div>
|
||||
|
||||
<div v-else class="g3-api__missing">
|
||||
未找到 <code>{{ name }}</code> 的 API 数据,请先执行 <code>pnpm gen:api</code>。
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<style>
|
||||
.g3-api__desc {
|
||||
color: var(--ui-text-muted, #6b7280);
|
||||
}
|
||||
|
||||
.g3-api h3 {
|
||||
margin-top: 28px;
|
||||
font-size: 16px;
|
||||
}
|
||||
|
||||
.g3-api__missing {
|
||||
padding: 12px 16px;
|
||||
border: 1px dashed var(--ui-border, #e5e7eb);
|
||||
border-radius: 8px;
|
||||
font-size: 13px;
|
||||
color: var(--ui-text-muted, #6b7280);
|
||||
}
|
||||
|
||||
.g3-api table {
|
||||
width: 100%;
|
||||
border-collapse: collapse;
|
||||
font-size: 13px;
|
||||
}
|
||||
|
||||
.g3-api th,
|
||||
.g3-api td {
|
||||
padding: 10px 14px;
|
||||
border: 1px solid var(--ui-border, #e5e7eb);
|
||||
text-align: left;
|
||||
vertical-align: top;
|
||||
}
|
||||
|
||||
.g3-api th {
|
||||
background: var(--ui-bg-muted, #f9fafb);
|
||||
font-weight: 600;
|
||||
}
|
||||
|
||||
.g3-api td code {
|
||||
white-space: nowrap;
|
||||
}
|
||||
</style>
|
||||
@@ -0,0 +1,152 @@
|
||||
<script setup lang="ts">
|
||||
import { computed, ref, type Component } from 'vue'
|
||||
|
||||
const props = defineProps<{
|
||||
/** 示例路径,相对 docs/examples 且不含 .vue,例如 "button/basic" */
|
||||
name: string
|
||||
}>()
|
||||
|
||||
/**
|
||||
* 收集所有示例。glob 只认相对路径与项目根绝对路径,这里用相对路径,
|
||||
* key 归一化成 "button/basic" 形式。
|
||||
*/
|
||||
const modules = import.meta.glob('../../../examples/**/*.vue', { eager: true }) as Record<
|
||||
string,
|
||||
{ default: Component }
|
||||
>
|
||||
const sources = import.meta.glob('../../../examples/**/*.vue', {
|
||||
query: '?raw',
|
||||
import: 'default',
|
||||
eager: true,
|
||||
}) as Record<string, string>
|
||||
|
||||
function normalize(path: string): string {
|
||||
return path.replace(/^.*\/examples\//, '').replace(/\.vue$/, '')
|
||||
}
|
||||
|
||||
const registry: Record<string, Component> = {}
|
||||
for (const [path, mod] of Object.entries(modules)) {
|
||||
registry[normalize(path)] = mod.default
|
||||
}
|
||||
|
||||
const sourceMap: Record<string, string> = {}
|
||||
for (const [path, raw] of Object.entries(sources)) {
|
||||
sourceMap[normalize(path)] = raw
|
||||
}
|
||||
|
||||
const demo = computed(() => registry[props.name])
|
||||
const source = computed(() => sourceMap[props.name] ?? '')
|
||||
|
||||
const showCode = ref(false)
|
||||
const copied = ref(false)
|
||||
|
||||
async function copyCode() {
|
||||
if (!source.value) return
|
||||
try {
|
||||
await navigator.clipboard.writeText(source.value)
|
||||
copied.value = true
|
||||
window.setTimeout(() => {
|
||||
copied.value = false
|
||||
}, 1500)
|
||||
} catch {
|
||||
// 非安全上下文(http)下剪贴板不可用,静默忽略
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div class="g3-demo">
|
||||
<div class="g3-demo__preview">
|
||||
<component :is="demo" v-if="demo" />
|
||||
<p v-else class="g3-demo__missing">未找到示例 docs/examples/{{ name }}.vue</p>
|
||||
</div>
|
||||
|
||||
<div class="g3-demo__toolbar">
|
||||
<button type="button" class="g3-demo__action" @click="showCode = !showCode">
|
||||
{{ showCode ? '收起代码' : '查看代码' }}
|
||||
</button>
|
||||
<button type="button" class="g3-demo__action" @click="copyCode">
|
||||
{{ copied ? '已复制' : '复制代码' }}
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<div v-show="showCode" class="g3-demo__code">
|
||||
<pre><code>{{ source }}</code></pre>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<style>
|
||||
/**
|
||||
* 示例卡片样式随组件自身提供,不依赖文档站的全局样式。
|
||||
* 颜色优先取 Nuxt UI 的设计令牌(--ui-*),未定义时退回中性值。
|
||||
*/
|
||||
.g3-demo {
|
||||
margin: 16px 0;
|
||||
border: 1px solid var(--ui-border, #e5e7eb);
|
||||
border-radius: 10px;
|
||||
background: var(--ui-bg, #fff);
|
||||
overflow: hidden;
|
||||
}
|
||||
|
||||
.g3-demo__preview {
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
gap: 12px;
|
||||
align-items: center;
|
||||
padding: 28px 20px;
|
||||
}
|
||||
|
||||
.g3-demo__missing {
|
||||
margin: 0;
|
||||
font-size: 13px;
|
||||
color: #ef4444;
|
||||
}
|
||||
|
||||
.g3-demo__toolbar {
|
||||
display: flex;
|
||||
gap: 4px;
|
||||
justify-content: flex-end;
|
||||
padding: 6px 12px;
|
||||
border-top: 1px solid var(--ui-border, #e5e7eb);
|
||||
background: var(--ui-bg-muted, #f9fafb);
|
||||
}
|
||||
|
||||
.g3-demo__action {
|
||||
padding: 4px 10px;
|
||||
border: 0;
|
||||
border-radius: 6px;
|
||||
background: transparent;
|
||||
color: var(--ui-text-muted, #6b7280);
|
||||
font-family: inherit;
|
||||
font-size: 12px;
|
||||
cursor: pointer;
|
||||
transition:
|
||||
background 0.2s,
|
||||
color 0.2s;
|
||||
}
|
||||
|
||||
.g3-demo__action:hover {
|
||||
background: var(--ui-bg-elevated, rgba(0, 0, 0, 0.04));
|
||||
color: var(--ui-primary, #1677ff);
|
||||
}
|
||||
|
||||
.g3-demo__code {
|
||||
border-top: 1px solid var(--ui-border, #e5e7eb);
|
||||
background: var(--ui-bg-muted, #f9fafb);
|
||||
max-height: 420px;
|
||||
overflow: auto;
|
||||
}
|
||||
|
||||
.g3-demo__code pre {
|
||||
margin: 0;
|
||||
padding: 16px 20px;
|
||||
}
|
||||
|
||||
.g3-demo__code code {
|
||||
font-family: var(--g3-font-family-mono, ui-monospace, monospace);
|
||||
font-size: 13px;
|
||||
line-height: 1.7;
|
||||
white-space: pre;
|
||||
}
|
||||
</style>
|
||||
Reference in new issue
Block a user