148 lines
3.2 KiB
Vue
148 lines
3.2 KiB
Vue
<template>
|
|
<div class="demo">
|
|
<div class="bar">
|
|
<t-button
|
|
v-for="item in contentTypeList"
|
|
:key="item.value"
|
|
size="small"
|
|
variant="outline"
|
|
:theme="article.contentType === item.value ? 'primary' : 'default'"
|
|
@click="switchContentType(item.value)"
|
|
>
|
|
{{ item.label }}
|
|
</t-button>
|
|
<t-button size="small" theme="primary" @click="submit">提交文章</t-button>
|
|
</div>
|
|
|
|
<div class="grid">
|
|
<section class="pane">
|
|
<h4 v-text="'编辑'" />
|
|
<t-input v-model="article.title" class="title" placeholder="文章标题" />
|
|
<article-editor
|
|
ref="editorRef"
|
|
:model-value="article"
|
|
:upload-image="uploadMedia"
|
|
:upload-video="uploadMedia"
|
|
@change="onChange"
|
|
@update:model-value="onChange"
|
|
/>
|
|
</section>
|
|
|
|
<section class="pane">
|
|
<h4 v-text="'预览'" />
|
|
<article-view :article="article" />
|
|
</section>
|
|
</div>
|
|
|
|
<p class="status" v-text="status" />
|
|
</div>
|
|
</template>
|
|
|
|
<script lang="ts" setup>
|
|
import { ref } from "vue";
|
|
import { ArticleEditor, ArticleView } from "timi-tdesign-pc";
|
|
import type {
|
|
Article,
|
|
ArticleContentType,
|
|
ArticleEditorExpose,
|
|
UploadResult
|
|
} from "timi-tdesign-pc";
|
|
|
|
/** 本场景验证「同一份 Article 数据,编辑与预览实时联动」。 */
|
|
const RICH_CONTENT = `<h2>文章编辑与预览</h2>
|
|
<p>左侧改动会立刻反映到右侧,两侧共用同一个 <code>Article</code> 对象。</p>
|
|
<blockquote><p>切换正文类型时,编辑区与预览区会同时切换渲染分支。</p></blockquote>`;
|
|
|
|
const contentTypeList: Array<{ label: string; value: ArticleContentType }> = [
|
|
{ label: "纯文本", value: "TEXT" },
|
|
{ label: "Markdown", value: "MARKDOWN" },
|
|
{ label: "富文本", value: "TIP_TAP" }
|
|
];
|
|
|
|
const article = ref<Article>({
|
|
title: "编辑器组件演示文章",
|
|
contentType: "TIP_TAP",
|
|
content: RICH_CONTENT,
|
|
attachmentList: []
|
|
});
|
|
|
|
const status = ref("等待提交");
|
|
const editorRef = ref<ArticleEditorExpose>();
|
|
|
|
function switchContentType(contentType: ArticleContentType) {
|
|
const content = contentType === "TEXT"
|
|
? "这是一段纯文本示例。"
|
|
: contentType === "MARKDOWN"
|
|
? "## Markdown 示例\n\n这是一段 Markdown 正文。"
|
|
: RICH_CONTENT;
|
|
article.value = {
|
|
...article.value,
|
|
contentType,
|
|
content
|
|
};
|
|
status.value = `已切换到 ${contentType}`;
|
|
}
|
|
|
|
function onChange(value: Article) {
|
|
article.value = value;
|
|
}
|
|
|
|
async function submit() {
|
|
const value = await editorRef.value?.submit();
|
|
if (value) {
|
|
article.value = value;
|
|
status.value = `已提交,${value.attachmentList?.length || 0} 个附件`;
|
|
}
|
|
}
|
|
|
|
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;
|
|
margin-bottom: 1rem;
|
|
}
|
|
|
|
.grid {
|
|
display: grid;
|
|
gap: 1rem;
|
|
grid-template-columns: repeat(2, minmax(0, 1fr));
|
|
}
|
|
|
|
.pane {
|
|
min-width: 0;
|
|
|
|
h4 {
|
|
margin: 0 0 .75rem;
|
|
font-size: .875rem;
|
|
}
|
|
}
|
|
|
|
.title {
|
|
margin-bottom: .75rem;
|
|
}
|
|
|
|
.status {
|
|
margin: 1rem 0 0;
|
|
font-size: .75rem;
|
|
color: var(--td-text-color-secondary);
|
|
}
|
|
|
|
@media (max-width: 60rem) {
|
|
.grid {
|
|
grid-template-columns: 1fr;
|
|
}
|
|
}
|
|
}
|
|
</style>
|