Files
timi-tdesign-pc/docs/demos/scenarios/editor.vue
T
2026-10-30 14:28:27 +08:00

180 lines
3.6 KiB
Vue

<template>
<div class="demo">
<div class="bar">
<t-button size="small" theme="primary" @click="openSearch">
同时打开两处查找
</t-button>
<t-button size="small" variant="outline" @click="submitRich">
提交富文本
</t-button>
<span class="status" v-text="status" />
</div>
<section class="block">
<div class="head">
<h4 v-text="'富文本编辑器'" />
<span v-text="'支持拖入图片与视频'" />
</div>
<div class="box rich">
<tiptap-editor
ref="tiptapRef"
v-model="rich"
:upload-image="uploadMedia"
:upload-video="uploadMedia"
@change="onRichChange"
/>
</div>
</section>
<section class="block">
<div class="head">
<h4 v-text="'只读视图'" />
<t-input
v-model="maxHeight"
class="height-input"
placeholder="最大高度,如 20rem"
/>
</div>
<tiptap-view :content="rich" :max-height="maxHeight" />
</section>
<section class="block">
<div class="head">
<h4 v-text="'代码编辑器'" />
<t-button size="small" variant="text" @click="resetCode">
恢复示例
</t-button>
</div>
<div class="box code">
<code-editor
ref="codeRef"
v-model="code"
language="typescript"
placeholder="请输入代码"
/>
</div>
</section>
</div>
</template>
<script lang="ts" setup>
import { ref } from "vue";
import { CodeEditor, TiptapEditor, TiptapView } from "timi-tdesign-pc";
import type {
CodeEditorExpose,
TiptapEditorExpose,
UploadResult
} from "timi-tdesign-pc";
/** 本场景把三类编辑器放在一起,验证它们共用同一套内容与检索能力。 */
const DEMO_CODE = `type DemoUser = {
id: string;
name: string;
};
const user: DemoUser = {
id: "demo-001",
name: "Timi"
};
console.log(user.name);`;
const rich = ref(`<h2>跨平台编辑器</h2>
<p>这段内容同时验证标题、段落、链接、表格和代码块的渲染。</p>
<blockquote><p>移动端和 PC 端只替换交互层,共享文档模型。</p></blockquote>`);
const code = ref(DEMO_CODE);
const maxHeight = ref("20rem");
const status = ref("等待操作");
const tiptapRef = ref<TiptapEditorExpose>();
const codeRef = ref<CodeEditorExpose>();
function onRichChange(value: string) {
status.value = `富文本已更新,${value.length} 个字符`;
}
function resetCode() {
code.value = DEMO_CODE;
status.value = "代码已恢复为示例内容";
}
/** 两个编辑器各有独立的查找栏,这里一次性把它们都唤起来。 */
function openSearch() {
tiptapRef.value?.openSearch(true);
codeRef.value?.openSearch(true);
status.value = "已打开查找栏";
}
async function submitRich() {
const value = await tiptapRef.value?.submit();
if (value !== undefined) {
rich.value = value;
status.value = `富文本已提交,${value.length} 个字符`;
}
}
async function uploadMedia(file: File): Promise<UploadResult> {
return {
id: `demo-${Date.now()}-${file.name}`,
name: file.name,
url: URL.createObjectURL(file)
};
}
</script>
<style lang="less" scoped>
.demo {
.bar {
display: flex;
flex-wrap: wrap;
gap: .5rem;
align-items: center;
margin-bottom: 1.25rem;
}
.status {
font-size: .75rem;
color: var(--td-text-color-secondary);
}
.block {
margin-bottom: 1.5rem;
&:last-child {
margin-bottom: 0;
}
}
.head {
display: flex;
gap: .75rem;
align-items: center;
justify-content: space-between;
margin-bottom: .75rem;
h4 {
margin: 0;
font-size: .875rem;
}
span {
font-size: .75rem;
color: var(--td-text-color-secondary);
}
}
.height-input {
width: 12rem;
}
.box {
height: 20rem;
}
.rich {
height: 24rem;
}
}
</style>