116 lines
2.6 KiB
Vue
116 lines
2.6 KiB
Vue
<template>
|
|
<div class="demo">
|
|
<div class="bar">
|
|
<t-button
|
|
v-for="item in contentTypeList"
|
|
:key="item.value"
|
|
size="small"
|
|
variant="outline"
|
|
:theme="value.contentType === item.value ? 'primary' : 'default'"
|
|
@click="switchContentType(item.value)"
|
|
>
|
|
{{ item.label }}
|
|
</t-button>
|
|
<t-button size="small" theme="primary" @click="submit">提交</t-button>
|
|
</div>
|
|
|
|
<t-input v-model="value.title" class="field" placeholder="文章标题" />
|
|
|
|
<article-editor
|
|
ref="editorRef"
|
|
:model-value="value"
|
|
:upload-image="uploadMedia"
|
|
:upload-video="uploadMedia"
|
|
@change="onChange"
|
|
@update:model-value="onChange"
|
|
/>
|
|
|
|
<p class="status" v-text="status" />
|
|
</div>
|
|
</template>
|
|
|
|
<script lang="ts" setup>
|
|
import { ref } from "vue";
|
|
import { ArticleEditor } from "timi-tdesign-pc";
|
|
import type {
|
|
Article,
|
|
ArticleContentType,
|
|
ArticleEditorExpose,
|
|
UploadResult
|
|
} from "timi-tdesign-pc";
|
|
|
|
const RICH_CONTENT = `<h2>正文类型可切换</h2>
|
|
<p>同一份 <code>Article</code> 数据,按 <code>contentType</code> 切换三种编辑形态。</p>`;
|
|
|
|
const contentTypeList: Array<{ label: string; value: ArticleContentType }> = [
|
|
{ label: "纯文本", value: "TEXT" },
|
|
{ label: "Markdown", value: "MARKDOWN" },
|
|
{ label: "富文本", value: "TIP_TAP" }
|
|
];
|
|
|
|
const value = 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;
|
|
value.value = {
|
|
...value.value,
|
|
contentType,
|
|
content
|
|
};
|
|
}
|
|
|
|
function onChange(article: Article) {
|
|
value.value = article;
|
|
}
|
|
|
|
async function submit() {
|
|
const article = await editorRef.value?.submit();
|
|
if (article) {
|
|
value.value = article;
|
|
status.value = `已提交,标题为「${article.title}」`;
|
|
}
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
.field {
|
|
margin-bottom: 1rem;
|
|
}
|
|
|
|
.status {
|
|
margin: 1rem 0 0;
|
|
font-size: .75rem;
|
|
color: var(--td-text-color-secondary);
|
|
}
|
|
}
|
|
</style>
|