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
+88
View File
@@ -0,0 +1,88 @@
<template>
<div class="demo">
<div class="row">
<button type="button" class="btn" @click="randomize" v-text="'换一个数值'" />
</div>
<div class="list">
<div class="item">
<span class="label" v-text="'默认(自动小数位)'" />
<animated-number class="value" :value="value" :duration="800" thousands />
</div>
<div class="item">
<span class="label" v-text="'固定两位小数'" />
<animated-number class="value" :value="value" :decimals="2" :duration="800" thousands />
</div>
<div class="item">
<span class="label" v-text="'自定义分隔符 + 线性缓动'" />
<animated-number
class="value"
:value="value"
:duration="1200"
:decimals="0"
thousands
separator=" "
easing="linear"
/>
</div>
</div>
</div>
</template>
<script lang="ts" setup>
import { ref } from "vue";
import { AnimatedNumber } from "timi-web";
/** 改动 value 会触发滚动过渡,注意所有实例共用同一个值以便对比缓动差异。 */
const value = ref(12345);
function randomize() {
value.value = Math.round(Math.random() * 900000 + 10000);
}
</script>
<style lang="less" scoped>
.demo {
.row {
margin-bottom: 1.25rem;
}
.btn {
padding: .25rem .75rem;
font-size: .75rem;
cursor: pointer;
border: 1px solid var(--vp-c-divider);
border-radius: .375rem;
color: var(--vp-c-text-1);
background: var(--vp-c-bg-soft);
&:hover {
color: var(--vp-c-brand-1);
border-color: var(--vp-c-brand-1);
}
}
.list {
display: grid;
gap: .75rem;
}
.item {
display: flex;
gap: 1rem;
align-items: baseline;
}
.label {
width: 14rem;
font-size: .75rem;
color: var(--vp-c-text-2);
}
.value {
font-size: 1.25rem;
font-weight: 600;
color: var(--vp-c-brand-1);
}
}
</style>
+68
View File
@@ -0,0 +1,68 @@
<template>
<div class="demo">
<div class="row">
<captcha :request="requestCaptcha" @update="onUpdate" @error="onError" />
<captcha
:width="120"
:height="48"
title="点我换一张"
placeholder="加载中"
:request="requestCaptcha"
@update="onUpdate"
/>
</div>
<p class="status" v-text="status" />
</div>
</template>
<script lang="ts" setup>
import { ref } from "vue";
import { Captcha } from "timi-web";
import type { CaptchaRequest, CaptchaResult } from "timi-web";
const status = ref("等待加载验证码");
/**
* 演示用验证码:直接生成一张 SVG 图片,无需后端接口。
* 真实项目里在这里调用后端换取 captchaId 与图片。
*/
async function requestCaptcha(width: number, height: number): Promise<CaptchaResult> {
const text = String(Math.floor(1000 + Math.random() * 9000));
const svg = `<svg xmlns="http://www.w3.org/2000/svg" width="${width}" height="${height}">` +
`<rect width="100%" height="100%" fill="#f3f3f3"/>` +
`<text x="50%" y="50%" font-family="monospace" font-size="24" fill="#006eff" ` +
`text-anchor="middle" dominant-baseline="central">${text}</text></svg>`;
return {
id: `demo-${Date.now()}`,
data: `data:image/svg+xml;utf8,${encodeURIComponent(svg)}`
};
}
function onUpdate(result: CaptchaResult) {
status.value = `已刷新,captchaId = ${result.id}`;
}
function onError(error: unknown) {
status.value = `获取失败:${String(error)}`;
}
</script>
<style lang="less" scoped>
.demo {
.row {
display: flex;
flex-wrap: wrap;
gap: 1.5rem;
align-items: center;
margin-bottom: 1rem;
}
.status {
margin: 0;
font-size: .75rem;
color: var(--vp-c-text-2);
}
}
</style>
+47
View File
@@ -0,0 +1,47 @@
<template>
<div class="demo">
<div v-for="item in numberList" :key="item.value" class="row">
<contact-number :value="item.value" underline />
<span class="note" v-text="item.note" />
</div>
<div class="row">
<contact-number value="13800138000" separator="-">
<template #icon>
<icon name="mail" :scale="1.2" />
</template>
</contact-number>
<span class="note" v-text="'自定义前置图标 + 短横线分隔'" />
</div>
</div>
</template>
<script lang="ts" setup>
import { ContactNumber, Icon } from "timi-web";
const numberList = [
{ value: "13800138000", note: "手机号" },
{ value: "4008001234", note: "客服热线" },
{ value: "+8613800138000", note: "带国际区号" }
];
</script>
<style lang="less" scoped>
.demo {
.row {
display: flex;
gap: 1rem;
align-items: center;
margin-bottom: .75rem;
&:last-child {
margin-bottom: 0;
}
}
.note {
font-size: .75rem;
color: var(--vp-c-text-2);
}
}
</style>
+23
View File
@@ -0,0 +1,23 @@
<template>
<div class="demo">
<copyright
icp="京ICP备 00000000 号"
domain="example.com"
author="Timi"
text="朝朝频顾惜,夜夜不相忘"
/>
<copyright background text="带背景图的页脚" />
</div>
</template>
<script lang="ts" setup>
import { Copyright } from "timi-web";
</script>
<style lang="less" scoped>
.demo {
border: 1px dashed var(--vp-c-divider);
border-radius: .5rem;
}
</style>
+35
View File
@@ -0,0 +1,35 @@
<template>
<div class="demo">
<description-grid :items="items" :columns="2" />
<description-grid :items="items" :columns="3" :bordered="false" />
<description-grid :items="items" :columns="2" value-align="right" label-align="right" empty-text="暂无" />
</div>
</template>
<script lang="ts" setup>
import { DescriptionGrid } from "timi-web";
/** value 为空的项会显示 emptyText。 */
const items = [
{ key: "name", label: "项目名称", value: "timi-web" },
{ key: "version", label: "版本", value: "0.0.24" },
{ key: "license", label: "开源协议", value: "MIT" },
{ key: "author", label: "维护者", value: "Timi" },
{ key: "homepage", label: "主页", value: null },
{ key: "engine", label: "Node 版本", value: ">=16" }
];
</script>
<style lang="less" scoped>
.demo {
> div {
margin-bottom: 2rem;
&:last-child {
margin-bottom: 0;
}
}
}
</style>
+28
View File
@@ -0,0 +1,28 @@
<template>
<div class="demo">
<div class="box">
<empty-tips />
</div>
<div class="box">
<empty-tips content="暂无权限查看该内容" />
</div>
</div>
</template>
<script lang="ts" setup>
import { EmptyTips } from "timi-web";
</script>
<style lang="less" scoped>
.demo {
.box {
margin-bottom: 1rem;
border: 1px dashed var(--vp-c-divider);
border-radius: .5rem;
&:last-child {
margin-bottom: 0;
}
}
}
</style>
+51
View File
@@ -0,0 +1,51 @@
<template>
<div class="demo">
<button
type="button"
class="btn"
@click="active = !active"
v-text="active ? '关闭落花背景' : '开启落花背景'"
/>
<p class="note" v-text="'组件本身是 fixed 全屏背景(z-index: -1),开启后花瓣会铺满整个视口并持续飘落。'" />
<b-e-flower-fall v-if="active" />
</div>
</template>
<script lang="ts" setup>
import { ref } from "vue";
import { BEFlowerFall } from "timi-web";
/**
* 落花按 document 尺寸计算飘落边界,因此适合作为整页背景。
* 这里用开关让它在真实形态下展示,而不是塞进小盒子里。
*/
const active = ref(false);
</script>
<style lang="less" scoped>
.demo {
.btn {
padding: .375rem .875rem;
font-size: .8125rem;
cursor: pointer;
border: 1px solid var(--vp-c-divider);
border-radius: .375rem;
color: var(--vp-c-text-1);
background: var(--vp-c-bg-soft);
&:hover {
color: var(--vp-c-brand-1);
border-color: var(--vp-c-brand-1);
}
}
.note {
margin: 1rem 0 0;
font-size: .75rem;
line-height: 1.6;
color: var(--vp-c-text-2);
}
}
</style>
+79
View File
@@ -0,0 +1,79 @@
<template>
<div class="demo">
<div class="row">
<hover-popup value="组件形式的文本提示">
<span class="anchor" v-text="'悬停:纯文本'" />
</hover-popup>
<hover-popup :type="PopupType.IMG" :value="image">
<span class="anchor" v-text="'悬停:图片'" />
</hover-popup>
<hover-popup>
<span class="anchor" v-text="'悬停:自定义节点'" />
<template #popup>
<div class="card">
<strong v-text="'自定义浮层'" />
<p v-text="'浮层内容完全由插槽决定,可以是任意结构。'" />
</div>
</template>
</hover-popup>
</div>
<p class="note" v-text="'组件形式不需要注册全局指令,props 与 v-popup 指令的取值一致。'" />
<popup />
</div>
</template>
<script lang="ts" setup>
import { HoverPopup, Popup, PopupType } from "timi-web";
/**
* 演示用图片:内联 SVG,避免依赖外部图床。
*/
const image = `data:image/svg+xml;utf8,${encodeURIComponent(
`<svg xmlns="http://www.w3.org/2000/svg" width="180" height="80">` +
`<rect width="100%" height="100%" fill="#00a870"/>` +
`<text x="50%" y="50%" font-size="15" fill="#fff" text-anchor="middle" ` +
`dominant-baseline="central">HoverPopup</text></svg>`
)}`;
</script>
<style lang="less" scoped>
.demo {
.row {
display: flex;
flex-wrap: wrap;
gap: 1.5rem;
}
.anchor {
cursor: help;
border-bottom: 1px dashed var(--vp-c-brand-1);
color: var(--vp-c-brand-1);
}
.note {
margin: 1rem 0 0;
font-size: .75rem;
color: var(--vp-c-text-2);
}
.card {
padding: .5rem .75rem;
max-width: 16rem;
strong {
font-size: .8125rem;
}
p {
margin: .25rem 0 0;
font-size: .75rem;
line-height: 1.5;
color: #666;
}
}
}
</style>
+79
View File
@@ -0,0 +1,79 @@
<template>
<div class="demo">
<div class="grid">
<div v-for="item in iconList" :key="item" class="cell">
<icon :name="item" :scale="1.5" />
<span class="name" v-text="item" />
</div>
</div>
<div class="row">
<div class="item">
<icon name="star" :scale="2.5" fill="#f5a623" />
<span v-text="'放大 + 着色'" />
</div>
<div class="item">
<icon name="success" :scale="2.5" fill="#00a870" />
<span v-text="'success'" />
</div>
<div class="item">
<icon name="fail" :scale="2.5" disable />
<span v-text="'禁用态'" />
</div>
</div>
</div>
</template>
<script lang="ts" setup>
import { Icon } from "timi-web";
/** 图标名不区分大小写,取自内置的 icon.json(共 149 个)。 */
const iconList = [
"home", "user", "mail", "clock", "star", "book",
"folder", "file", "code", "copy", "delete", "save",
"search", "filter", "upload", "download", "link", "image",
"play", "volume", "message", "info", "success", "fail"
];
</script>
<style lang="less" scoped>
.demo {
.grid {
display: grid;
gap: .5rem;
margin-bottom: 1.5rem;
grid-template-columns: repeat(auto-fill, minmax(5.5rem, 1fr));
}
.cell {
display: flex;
gap: .5rem;
align-items: center;
padding: .5rem;
border-radius: .375rem;
background: var(--vp-c-bg-soft);
}
.name {
overflow: hidden;
font-size: .75rem;
white-space: nowrap;
text-overflow: ellipsis;
color: var(--vp-c-text-2);
}
.row {
display: flex;
flex-wrap: wrap;
gap: 1.5rem;
}
.item {
display: flex;
gap: .5rem;
align-items: center;
font-size: .75rem;
color: var(--vp-c-text-2);
}
}
</style>
+52
View File
@@ -0,0 +1,52 @@
<template>
<div class="demo">
<div class="row">
<div class="item">
<loading />
<span v-text="'默认'" />
</div>
<div class="item">
<loading :size="40" tips="拼命加载中" />
<span v-text="'放大 + 自定义文案'" />
</div>
<div class="item">
<loading :size="16" :tips="false" />
<span v-text="'小尺寸 + 无文案'" />
</div>
</div>
<div class="box">
<loading filled tips="铺满容器展示" />
</div>
</div>
</template>
<script lang="ts" setup>
import { Loading } from "timi-web";
</script>
<style lang="less" scoped>
.demo {
.row {
display: flex;
flex-wrap: wrap;
gap: 2rem;
margin-bottom: 1.5rem;
}
.item {
display: flex;
gap: .75rem;
align-items: center;
font-size: .75rem;
color: var(--vp-c-text-2);
}
.box {
height: 10rem;
overflow: hidden;
border: 1px dashed var(--vp-c-divider);
border-radius: .5rem;
}
}
</style>
+33
View File
@@ -0,0 +1,33 @@
<template>
<div class="demo">
<markdown-editor v-model:data="data" :min-rows="8" :max-rows="20" />
<p class="status" v-text="`当前正文 ${data.length} 个字符`" />
</div>
</template>
<script lang="ts" setup>
import { ref } from "vue";
import { MarkdownEditor } from "timi-web";
const data = ref(`## 左侧编辑,右侧预览
在左侧输入 Markdown,右侧会实时渲染。
\`\`\`typescript
const message: string = "hello timi-web";
\`\`\`
容器宽度小于 650px 时预览区会自动折叠,可用工具栏切换。
`);
</script>
<style lang="less" scoped>
.demo {
.status {
margin: .75rem 0 0;
font-size: .75rem;
color: var(--vp-c-text-2);
}
}
</style>
+48
View File
@@ -0,0 +1,48 @@
<template>
<div class="demo">
<markdown-view :content="content" max-height="24rem" />
<markdown-view :code="code" max-height="16rem" />
</div>
</template>
<script lang="ts" setup>
import { MarkdownView } from "timi-web";
const content = `## Markdown 渲染
支持标题、列表、表格与代码块的完整渲染:
- 无序列表
- **加粗**、*斜体*、\`行内代码\`
| 参数 | 说明 |
| --- | --- |
| content | Markdown 原文 |
| code | 单段代码,形如 \`lang:code\` |
\`\`\`typescript
const answer: number = 42;
\`\`\`
`;
/** 传 code 时按「语言:正文」解析,用于只展示一段代码的场景。 */
const code = `typescript:interface User {
id: string;
name: string;
}
const user: User = { id: "u1", name: "Timi" };`;
</script>
<style lang="less" scoped>
.demo {
> div {
margin-bottom: 1.5rem;
&:last-child {
margin-bottom: 0;
}
}
}
</style>
+52
View File
@@ -0,0 +1,52 @@
<template>
<div class="demo">
<div v-for="item in timeList" :key="item.offset" class="row">
<span class="label" v-text="item.label" />
<pass-time-label :timestamp="item.timestamp" underline />
</div>
<div class="row">
<span class="label" v-text="'仅时间 / 仅日期'" />
<pass-time-label :timestamp="now" only-time underline />
<pass-time-label :timestamp="now" only-date underline />
</div>
</div>
</template>
<script lang="ts" setup>
import { PassTimeLabel } from "timi-web";
const now = Date.now();
/** 点击标签可在「多久以前」与完整时间之间切换。 */
const timeList = [
{ offset: 30_000, label: "30 秒前" },
{ offset: 30 * 60_000, label: "30 分钟前" },
{ offset: 5 * 3600_000, label: "5 小时前" },
{ offset: 3 * 86400_000, label: "3 天前" }
].map(item => ({
...item,
timestamp: now - item.offset
}));
</script>
<style lang="less" scoped>
.demo {
.row {
display: flex;
gap: 1rem;
align-items: center;
margin-bottom: .75rem;
&:last-child {
margin-bottom: 0;
}
}
.label {
width: 12rem;
font-size: .75rem;
color: var(--vp-c-text-2);
}
}
</style>
+65
View File
@@ -0,0 +1,65 @@
<template>
<div class="demo">
<p class="tip">
<span class="anchor" v-popup="'这是一段纯文本提示'">悬停看文本提示</span>
</p>
<p class="tip">
<span class="anchor" v-popup="imageConfig">悬停看图片</span>
</p>
<p class="tip">
<span class="anchor" v-popup="htmlConfig">悬停看 HTML</span>
</p>
<p class="note" v-text="'Popup 是全局唯一的浮层挂载点,页面上只需放置一次。'" />
<popup />
</div>
</template>
<script lang="ts" setup>
import { Popup, PopupType, VPopup } from "timi-web";
/** 演示用图片:内联 SVG,避免依赖外部图床。 */
const image = `data:image/svg+xml;utf8,${encodeURIComponent(
`<svg xmlns="http://www.w3.org/2000/svg" width="200" height="90">` +
`<rect width="100%" height="100%" fill="#3451b2"/>` +
`<text x="50%" y="50%" font-size="16" fill="#fff" text-anchor="middle" ` +
`dominant-baseline="central">Popup 图片</text></svg>`
)}`;
/**
* `v-popup` 的值可以是字符串(按纯文本展示),
* 也可以是包含 type / value 的配置对象。
*/
const imageConfig = {
type: PopupType.IMG,
value: image
};
const htmlConfig = {
type: PopupType.HTML,
value: "<b>加粗</b>与<i>斜体</i>都能渲染"
};
</script>
<style lang="less" scoped>
.demo {
.tip {
margin: 0 0 .75rem;
}
.anchor {
cursor: help;
border-bottom: 1px dashed var(--vp-c-brand-1);
color: var(--vp-c-brand-1);
}
.note {
margin: 1rem 0 0;
font-size: .75rem;
color: var(--vp-c-text-2);
}
}
</style>
+106
View File
@@ -0,0 +1,106 @@
<template>
<div class="demo">
<div class="bar">
<input v-model="name" class="field" placeholder="键" />
<input v-model="value" class="field" placeholder="值" />
<input v-model.number="ttl" class="field small" type="number" placeholder="毫秒" />
<button type="button" class="btn" @click="set" v-text="'写入'" />
<button type="button" class="btn" @click="get" v-text="'读取'" />
<button type="button" class="btn" @click="remove" v-text="'删除'" />
</div>
<p class="note" v-text="'有效期留空表示会话级 Cookie,关闭浏览器即失效。'" />
<ol class="log">
<li v-for="(item, index) in log" :key="index" v-text="item" />
</ol>
</div>
</template>
<script lang="ts" setup>
import { ref } from "vue";
import { Cooker } from "timi-web";
const name = ref("timi_demo");
const value = ref("hello cookie");
const ttl = ref(0);
const log = ref<string[]>([]);
function push(message: string) {
log.value = [`${new Date().toLocaleTimeString()} ${message}`, ...log.value].slice(0, 6);
}
function set() {
Cooker.set(name.value, value.value, ttl.value);
const expires = ttl.value ? `,有效期 ${ttl.value}ms` : ",会话级";
push(`set("${name.value}", "${value.value}")${expires}`);
}
function get() {
const result = Cooker.get(name.value);
push(`get("${name.value}") → ${result === undefined ? "undefined" : result}`);
}
function remove() {
Cooker.remove(name.value);
push(`remove("${name.value}")`);
}
</script>
<style lang="less" scoped>
.demo {
.bar {
display: flex;
flex-wrap: wrap;
gap: .5rem;
margin-bottom: .5rem;
}
.field {
width: 9rem;
padding: .25rem .5rem;
font-size: .8125rem;
border: 1px solid var(--vp-c-divider);
border-radius: .375rem;
color: var(--vp-c-text-1);
background: var(--vp-c-bg);
&.small {
width: 6rem;
}
}
.btn {
padding: .25rem .75rem;
font-size: .75rem;
cursor: pointer;
border: 1px solid var(--vp-c-divider);
border-radius: .375rem;
color: var(--vp-c-text-1);
background: var(--vp-c-bg-soft);
&:hover {
color: var(--vp-c-brand-1);
border-color: var(--vp-c-brand-1);
}
}
.note {
margin: 0 0 .75rem;
font-size: .75rem;
color: var(--vp-c-text-2);
}
.log {
margin: 0;
padding-left: 1.25rem;
font-size: .75rem;
font-family: monospace;
color: var(--vp-c-text-2);
li {
margin-bottom: .25rem;
}
}
}
</style>
+103
View File
@@ -0,0 +1,103 @@
<template>
<div class="demo">
<div ref="stageRef" class="stage">
<div
class="box"
v-draggable="config"
:style="{ left: `${x}px`, top: `${y}px` }"
v-text="'拖我'"
/>
</div>
<div class="bar">
<label class="check">
<input v-model="locked" type="checkbox" />
<span v-text="'锁定(interruptWhen 返回 true)'" />
</label>
</div>
<p class="state" v-text="status" />
</div>
</template>
<script lang="ts" setup>
import { ref } from "vue";
import { VDraggable } from "timi-web";
import type { DraggableConfig } from "timi-web";
const x = ref(16);
const y = ref(16);
const locked = ref(false);
const status = ref("等待操作");
/**
* 指令只负责把移动过程报出来,位置由这里写回 style,
* 因此接不接管、怎么接管完全由业务决定。
*/
const config: DraggableConfig = {
onMouseDown: () => {
status.value = "已按下";
},
interruptWhen: () => locked.value,
onDragging: (_event, relX, relY) => {
x.value = Math.max(0, relX);
y.value = Math.max(0, relY);
status.value = `拖动中 relX=${relX} relY=${relY}`;
},
onDragged: (_event, offsetX, offsetY) => {
status.value = `已释放,本次位移 ${offsetX}, ${offsetY}`;
}
};
</script>
<style lang="less" scoped>
.demo {
.stage {
height: 14rem;
position: relative;
overflow: hidden;
margin-bottom: 1rem;
border: 1px dashed var(--vp-c-divider);
border-radius: .5rem;
background: var(--vp-c-bg-soft);
}
.box {
width: 5rem;
height: 5rem;
position: absolute;
display: flex;
cursor: grab;
font-size: .8125rem;
user-select: none;
align-items: center;
justify-content: center;
border-radius: .5rem;
color: #fff;
background: var(--vp-c-brand-1);
&:active {
cursor: grabbing;
}
}
.bar {
margin-bottom: .5rem;
}
.check {
display: flex;
gap: .375rem;
align-items: center;
font-size: .75rem;
color: var(--vp-c-text-2);
}
.state {
margin: 0;
font-size: .75rem;
font-family: monospace;
color: var(--vp-c-text-2);
}
}
</style>
+64
View File
@@ -0,0 +1,64 @@
<template>
<div class="demo">
<textarea v-model="source" class="field" spellcheck="false" />
<h4 v-text="'toHTML 的输出'" />
<div class="preview" v-html="html" />
</div>
</template>
<script lang="ts" setup>
import { ref, watch } from "vue";
import { Markdown } from "timi-web";
const source = ref(`## 渲染结果
支持标题、列表与代码高亮:
- 无序列表项
- 行内 \`代码\` 会渲染为红色
\`\`\`typescript
const answer: number = 42;
\`\`\`
`);
const html = ref("");
/** toHTML 返回字符串或 Promise,两种都要接住。 */
async function render() {
html.value = await Markdown.getInstance().toHTML(source.value);
}
watch(source, render, { immediate: true });
</script>
<style lang="less" scoped>
.demo {
.field {
width: 100%;
height: 11rem;
padding: .75rem;
margin-bottom: 1.25rem;
box-sizing: border-box;
font-size: .8125rem;
font-family: monospace;
line-height: 1.6;
border: 1px solid var(--vp-c-divider);
border-radius: .5rem;
color: var(--vp-c-text-1);
background: var(--vp-c-bg-soft);
}
h4 {
margin: 0 0 .5rem;
font-size: .8125rem;
}
.preview {
padding: 0 1rem;
border: 1px solid var(--vp-c-divider);
border-radius: .5rem;
}
}
</style>
+87
View File
@@ -0,0 +1,87 @@
<template>
<div class="demo">
<div class="bar">
<input v-model="input" class="field" placeholder="输入语言名或别名,如 ts" />
</div>
<p class="result">
<span v-text="'resolvePrismLanguage → '" />
<code v-text="resolved || '(空字符串,表示不支持)'" />
</p>
<h4 v-text="'已支持的语言'" />
<div class="grid">
<div v-for="item in supported" :key="item.value" class="cell">
<code v-text="item.value" />
<span v-text="item.label" />
</div>
</div>
</div>
</template>
<script lang="ts" setup>
import { computed, ref } from "vue";
import { resolvePrismLanguage, supportedCodeLanguages } from "timi-web";
const input = ref("ts");
const supported = supportedCodeLanguages;
const resolved = computed(() => resolvePrismLanguage(input.value));
</script>
<style lang="less" scoped>
.demo {
.bar {
margin-bottom: .75rem;
}
.field {
width: 16rem;
padding: .25rem .5rem;
font-size: .8125rem;
border: 1px solid var(--vp-c-divider);
border-radius: .375rem;
color: var(--vp-c-text-1);
background: var(--vp-c-bg);
}
.result {
margin: 0 0 1.25rem;
font-size: .8125rem;
color: var(--vp-c-text-2);
code {
color: var(--vp-c-brand-1);
}
}
h4 {
margin: 0 0 .5rem;
font-size: .8125rem;
}
.grid {
display: grid;
gap: .375rem;
grid-template-columns: repeat(auto-fill, minmax(9rem, 1fr));
}
.cell {
display: flex;
gap: .5rem;
align-items: baseline;
padding: .375rem .5rem;
border-radius: .375rem;
background: var(--vp-c-bg-soft);
code {
font-size: .75rem;
}
span {
font-size: .6875rem;
color: var(--vp-c-text-2);
}
}
}
</style>
+104
View File
@@ -0,0 +1,104 @@
<template>
<div class="demo">
<p class="size" v-text="`${width} × ${height}`" />
<p class="note" v-text="`回调已触发 ${count} 次(含注册时的立即触发)`" />
<div class="bar">
<button
type="button"
class="btn"
@click="toggle"
v-text="registered ? '移除监听' : '注册监听'"
/>
<button type="button" class="btn" @click="read" v-text="'手动读取当前尺寸'" />
</div>
<p class="note" v-text="'缩放浏览器窗口即可看到数字变化。'" />
</div>
</template>
<script lang="ts" setup>
import { onMounted, onUnmounted, ref } from "vue";
import { Resizer } from "timi-web";
/** 监听名在内部用于去重,同名会覆盖。 */
const LISTENER_NAME = "TimiDocsResizerDemo";
const width = ref(0);
const height = ref(0);
const count = ref(0);
const registered = ref(false);
function onChange(nextWidth: number, nextHeight: number) {
width.value = nextWidth;
height.value = nextHeight;
count.value += 1;
}
function register() {
// 注册后会立即触发一次,因此不需要再手动取一次尺寸
Resizer.addListener(LISTENER_NAME, onChange);
registered.value = true;
}
function unregister() {
Resizer.removeListener(LISTENER_NAME);
registered.value = false;
}
function toggle() {
if (registered.value) {
unregister();
} else {
register();
}
}
/** getWidth / getHeight 直接读文档可视区,与监听无关。 */
function read() {
width.value = Resizer.getWidth();
height.value = Resizer.getHeight();
}
onMounted(register);
// 演示组件卸载时务必移除,避免监听残留
onUnmounted(unregister);
</script>
<style lang="less" scoped>
.demo {
.size {
margin: 0 0 .25rem;
font-size: 1.5rem;
font-weight: 600;
font-variant-numeric: tabular-nums;
color: var(--vp-c-brand-1);
}
.note {
margin: 0 0 .75rem;
font-size: .75rem;
color: var(--vp-c-text-2);
}
.bar {
display: flex;
gap: .5rem;
margin-bottom: .75rem;
}
.btn {
padding: .25rem .75rem;
font-size: .75rem;
cursor: pointer;
border: 1px solid var(--vp-c-divider);
border-radius: .375rem;
color: var(--vp-c-text-1);
background: var(--vp-c-bg-soft);
&:hover {
color: var(--vp-c-brand-1);
border-color: var(--vp-c-brand-1);
}
}
}
</style>
+117
View File
@@ -0,0 +1,117 @@
<template>
<div class="demo">
<div ref="boxRef" class="box">
<p class="tip" v-text="'滚动这个容器试试'" />
<div class="filler" />
<p ref="markRef" class="mark" v-text="'标记位置'" />
<div class="filler" />
</div>
<p class="state" v-text="`top=${state.top} bottom=${state.bottom} scrollHeight=${state.scrollHeight}`" />
<div class="bar">
<button type="button" class="btn" @click="scrollToMark" v-text="'toElement(标记)'" />
</div>
</div>
</template>
<script lang="ts" setup>
import { onMounted, onUnmounted, reactive, ref } from "vue";
import { Scroller } from "timi-web";
import type { ScrollListener } from "timi-web";
const LISTENER_NAME = "TimiDocsScrollerDemo";
const boxRef = ref<HTMLElement>();
const markRef = ref<HTMLElement>();
const state = reactive({
top: 0,
bottom: 0,
scrollHeight: 0
});
/** 传 getElement 时监听的是该容器的滚动,不传则监听 window。 */
function getElement() {
return boxRef.value;
}
function onScroll(event: ScrollListener) {
state.top = event.top;
state.bottom = event.bottom;
state.scrollHeight = event.scrollHeight;
}
/** toElement 会让目标滚动到可视区,这里目标在容器内,因此滚动的是容器。 */
function scrollToMark() {
if (markRef.value) {
Scroller.toElement(markRef.value);
}
}
onMounted(() => {
Scroller.addListener(LISTENER_NAME, onScroll, getElement);
// 首次同步一次状态
onScroll({ top: 0, bottom: 0, scrollHeight: boxRef.value?.scrollHeight ?? 0 } as ScrollListener);
});
onUnmounted(() => {
Scroller.removeListener(LISTENER_NAME);
});
</script>
<style lang="less" scoped>
.demo {
.box {
height: 12rem;
padding: 0 1rem;
margin-bottom: .75rem;
overflow-y: auto;
border: 1px solid var(--vp-c-divider);
border-radius: .5rem;
background: var(--vp-c-bg-soft);
}
.tip {
margin: 1rem 0;
font-size: .8125rem;
color: var(--vp-c-text-2);
}
.mark {
margin: 0;
padding: .5rem;
font-size: .8125rem;
text-align: center;
border-radius: .375rem;
color: var(--vp-c-brand-1);
background: var(--vp-c-default-soft);
}
.filler {
height: 14rem;
}
.state {
margin: 0 0 .75rem;
font-size: .75rem;
font-family: monospace;
color: var(--vp-c-text-2);
}
.btn {
padding: .25rem .75rem;
font-size: .75rem;
cursor: pointer;
border: 1px solid var(--vp-c-divider);
border-radius: .375rem;
color: var(--vp-c-text-1);
background: var(--vp-c-bg-soft);
&:hover {
color: var(--vp-c-brand-1);
border-color: var(--vp-c-brand-1);
}
}
}
</style>
+107
View File
@@ -0,0 +1,107 @@
<template>
<div class="demo">
<div class="bar">
<input v-model="key" class="field" placeholder="键" />
<input v-model="value" class="field" placeholder="值" />
<button type="button" class="btn" @click="save" v-text="'写入'" />
<button type="button" class="btn" @click="load" v-text="'读取'" />
<button type="button" class="btn" @click="remove" v-text="'删除'" />
</div>
<p class="state" v-text="`当前键存在:${exists}`" />
<ol class="log">
<li v-for="(item, index) in log" :key="index" v-text="item" />
</ol>
</div>
</template>
<script lang="ts" setup>
import { computed, ref } from "vue";
import { Storage } from "timi-web";
const key = ref("timi-demo");
const value = ref("hello storage");
const log = ref<string[]>([]);
/** 手动维护一个计数,用来在增删后重新读取 has */
const version = ref(0);
/** 直接读真实 localStorage,演示里的增删会反映到浏览器里。 */
const exists = computed(() => {
void version.value;
return Storage.has(key.value);
});
function push(message: string) {
log.value = [`${new Date().toLocaleTimeString()} ${message}`, ...log.value].slice(0, 6);
version.value += 1;
}
function save() {
Storage.setObject(key.value, value.value);
push(`setObject("${key.value}", "${value.value}")`);
}
function load() {
const raw = Storage.getString(key.value);
push(`getString → ${raw === null ? "null" : raw}`);
}
function remove() {
Storage.remove(key.value);
push(`remove("${key.value}")`);
}
</script>
<style lang="less" scoped>
.demo {
.bar {
display: flex;
flex-wrap: wrap;
gap: .5rem;
margin-bottom: 1rem;
}
.field {
padding: .25rem .5rem;
font-size: .8125rem;
border: 1px solid var(--vp-c-divider);
border-radius: .375rem;
color: var(--vp-c-text-1);
background: var(--vp-c-bg);
}
.btn {
padding: .25rem .75rem;
font-size: .75rem;
cursor: pointer;
border: 1px solid var(--vp-c-divider);
border-radius: .375rem;
color: var(--vp-c-text-1);
background: var(--vp-c-bg-soft);
&:hover {
color: var(--vp-c-brand-1);
border-color: var(--vp-c-brand-1);
}
}
.state {
margin: 0 0 .75rem;
font-size: .8125rem;
color: var(--vp-c-text-2);
}
.log {
margin: 0;
padding-left: 1.25rem;
font-size: .75rem;
font-family: monospace;
color: var(--vp-c-text-2);
li {
margin-bottom: .25rem;
}
}
}
</style>
+90
View File
@@ -0,0 +1,90 @@
<template>
<div class="demo">
<section class="block">
<h4 v-text="'toFormData:嵌套对象扁平化'" />
<table>
<thead>
<tr>
<th>键</th>
<th>值</th>
</tr>
</thead>
<tbody>
<tr v-for="row in formRows" :key="row.key">
<td><code v-text="row.key" /></td>
<td v-text="row.value" />
</tr>
</tbody>
</table>
</section>
<section class="block">
<h4 v-text="'base64ToFile:还原为 File'" />
<p class="line" v-text="fileInfo" />
</section>
<section class="block">
<h4 v-text="'isFile / getAttribute'" />
<p class="line" v-text="fileCheck" />
<p class="line" v-text="attributeCheck" />
</section>
</div>
</template>
<script lang="ts" setup>
import { onMounted, ref } from "vue";
import { WebToolkit } from "timi-web";
/** toFormData 会把嵌套对象展开为点号连接的键。 */
const source = {
name: "timi",
version: 1,
author: { first: "Ti", last: "mi" },
tags: ["a", "b"]
};
const formRows = [...WebToolkit.toFormData(source).entries()].map(([key, item]) => ({
key,
value: typeof item === "string" ? item : `${item.constructor.name}(${(item as Blob).size} 字节)`
}));
const fileInfo = ref("");
const fileCheck = ref("");
const attributeCheck = ref("");
onMounted(() => {
// 1×1 的透明 PNG
const dataUrl = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg==";
const file = WebToolkit.base64ToFile(dataUrl, "pixel.png");
fileInfo.value = `name="${file.name}" size=${file.size} type="${file.type}"`;
fileCheck.value = `isFile(file) = ${WebToolkit.isFile(file)}|isFile({}) = ${WebToolkit.isFile({})}`;
const element = document.createElement("div");
element.setAttribute("data-role", "demo");
attributeCheck.value = `getAttribute(el, "data-role") = ${WebToolkit.getAttribute(element, "data-role")}|getAttribute(el, "missing") = ${WebToolkit.getAttribute(element, "missing")}`;
});
</script>
<style lang="less" scoped>
.demo {
.block {
margin-bottom: 1.5rem;
&:last-child {
margin-bottom: 0;
}
}
h4 {
margin: 0 0 .5rem;
font-size: .8125rem;
}
.line {
margin: 0 0 .25rem;
font-size: .75rem;
font-family: monospace;
color: var(--vp-c-text-2);
}
}
</style>