153 lines
3.3 KiB
Vue
153 lines
3.3 KiB
Vue
<template>
|
|
<div class="demo">
|
|
<div class="bar">
|
|
<button
|
|
v-for="item in contentTypeList"
|
|
:key="item.value"
|
|
type="button"
|
|
class="btn"
|
|
:class="{ active: value.contentType === item.value }"
|
|
@click="switchContentType(item.value)"
|
|
v-text="item.label"
|
|
/>
|
|
</div>
|
|
|
|
<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"
|
|
/>
|
|
|
|
<div class="footer">
|
|
<button type="button" class="btn primary" @click="submit" v-text="'提交'" />
|
|
<span class="status" v-text="status" />
|
|
</div>
|
|
</div>
|
|
</template>
|
|
|
|
<script lang="ts" setup>
|
|
import { ref } from "vue";
|
|
import { ArticleEditor } from "timi-tdesign-mobile";
|
|
import type {
|
|
Article,
|
|
ArticleContentType,
|
|
ArticleEditorExpose,
|
|
UploadResult
|
|
} from "timi-tdesign-mobile";
|
|
|
|
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: .375rem;
|
|
margin-bottom: .75rem;
|
|
}
|
|
|
|
.btn {
|
|
padding: .25rem .625rem;
|
|
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);
|
|
|
|
&.active {
|
|
color: #fff;
|
|
border-color: var(--vp-c-brand-1);
|
|
background: var(--vp-c-brand-1);
|
|
}
|
|
|
|
&.primary {
|
|
color: #fff;
|
|
border-color: var(--vp-c-brand-1);
|
|
background: var(--vp-c-brand-1);
|
|
}
|
|
}
|
|
|
|
.field {
|
|
width: 100%;
|
|
padding: .375rem .5rem;
|
|
margin-bottom: .75rem;
|
|
box-sizing: border-box;
|
|
font-size: .8125rem;
|
|
border: 1px solid var(--vp-c-divider);
|
|
border-radius: .375rem;
|
|
color: var(--vp-c-text-1);
|
|
background: var(--vp-c-bg);
|
|
}
|
|
|
|
.footer {
|
|
display: flex;
|
|
gap: .75rem;
|
|
align-items: center;
|
|
margin-top: .75rem;
|
|
}
|
|
|
|
.status {
|
|
font-size: .75rem;
|
|
color: var(--vp-c-text-2);
|
|
}
|
|
}
|
|
</style>
|