forked from developer-plus/vue-hbs-admin
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(demo): markdown-editor (developer-plus#28)
- Loading branch information
1 parent
ee968a9
commit 5e210e4
Showing
5 changed files
with
115 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
<template> | ||
<a-card title="Markdown 编辑器" :bordered="false" w-full> | ||
<div ref="domRef"> | ||
<a-spin tip="正在加载编辑器中" /> | ||
</div> | ||
<div w-full mt="20px" align="center"> | ||
<p w-full> | ||
使用 Viditor 作为 Markdown 编辑器 | ||
</p> | ||
<p w-full> | ||
Github Repo: <a href="https://github.com/Vanessa219/vditor">https://github.com/Vanessa219/vditor</a> | ||
</p> | ||
</div> | ||
</a-card> | ||
<a-card title="Markdown 显示器"> | ||
<div v-html="valueHTML" /> | ||
</a-card> | ||
<a-card title="Markdown value HTML"> | ||
<textarea v-model="valueHTML" readonly w-full h="200px" outline="none" /> | ||
</a-card> | ||
</template> | ||
<script setup lang="ts"> | ||
import { promiseTimeout } from '@vueuse/core' | ||
import Vditor from 'vditor' | ||
import 'vditor/dist/index.css' | ||
const domRef = ref<HTMLElement>() | ||
const editor = ref<Vditor>() | ||
// 编辑器的最小高度 | ||
const editorInitHeight = ref(300) | ||
const valueHTML = ref() | ||
function renderEditor() { | ||
if (!domRef.value) return | ||
editor.value = new Vditor(domRef.value, { | ||
// https://ld246.com/article/1549638745630#options | ||
// 在官方文档查看查看更多配置项 | ||
minHeight: editorInitHeight.value, | ||
// 编辑器主题:'dark'/'classic' | ||
// TODO 等待后期支持黑暗模式,这里需要进行同步切换 | ||
theme: 'classic', | ||
icon: 'ant', | ||
// 是否使用 localStorage 缓存,如果 true | ||
// 必须给定 id,id 是 localStorage 的 key,例如:cache: { enable: true, id: "hbs-md-editor-value" } | ||
cache: { enable: false }, | ||
value: '# Hello World!\n你好世界', | ||
placeholder: '请输入内容...', | ||
after() { | ||
mockAsyncData() | ||
getHTML() | ||
}, | ||
input() { | ||
// input 事件 | ||
getHTML() | ||
}, | ||
focus() { | ||
// focus 事件 | ||
}, | ||
blur() { | ||
// blur 事件 | ||
}, | ||
// 'sv': 双屏 | 'ir': 即时预览 | 'wysiwyg': 所见即所得 | ||
mode: 'ir', | ||
counter: { | ||
enable: true, // 启用计数器 | ||
max: 500, // 最大值 | ||
type: 'markdown' // 统计的类型: markdown | text | ||
}, | ||
outline: { | ||
enable: true, // 是否展示大纲 | ||
position: 'left' // 大纲位置,'left' | 'right' | ||
} | ||
}) | ||
} | ||
function getHTML() { | ||
valueHTML.value = editor.value?.getHTML() | ||
} | ||
async function mockAsyncData() { | ||
await promiseTimeout(2000) | ||
editor.value?.setValue('模拟异步数据') | ||
getHTML() | ||
} | ||
onMounted(() => { | ||
renderEditor() | ||
}) | ||
onBeforeUnmount(() => { | ||
// 组件销毁,同时销毁编辑器 | ||
editor.value?.destroy() | ||
}) | ||
</script> |