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
+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>