From a89e497e82829e9f7f26c7df1e42ed0d74e5dd71 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wit=E3=80=86=E8=8B=97=E5=A4=A7?= <38201220+WitMiao@users.noreply.github.com> Date: Tue, 5 Jul 2022 11:55:26 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20markdown=E6=B7=B1=E8=89=B2=E6=A8=A1?= =?UTF-8?q?=E5=BC=8F=E5=86=85=E5=AE=B9=E5=8C=BA=E5=92=8C=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E5=9D=97=E6=9C=AA=E9=80=82=E9=85=8Dbug;=20markdownViewer?= =?UTF-8?q?=E6=94=B9=E4=B8=BAvidtor=E8=87=AA=E5=B8=A6=E9=A2=84=E8=A7=88?= =?UTF-8?q?=E6=A8=A1=E5=BC=8F=20(#2023)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix(Markdown): 修复深色模式 内容区和代码块 未改变主题bug * perf(Markdown): MarkDown组件示例增加不同功能示例; 切换深色主题按钮 同时改变 内容区和代码块主题 * perf(MarkdownViewer): MarkdownViewer改为vditor自带的预览模式; 同时适配深色模式 Co-authored-by: 苗大 --- src/components/Markdown/src/Markdown.vue | 17 ++++- .../Markdown/src/MarkdownViewer.vue | 65 +++++++++++++++---- src/components/Markdown/src/getTheme.ts | 19 ++++++ src/views/demo/editor/markdown/index.vue | 43 +++++++++++- 4 files changed, 125 insertions(+), 19 deletions(-) create mode 100644 src/components/Markdown/src/getTheme.ts diff --git a/src/components/Markdown/src/Markdown.vue b/src/components/Markdown/src/Markdown.vue index d8117631..c80c779b 100644 --- a/src/components/Markdown/src/Markdown.vue +++ b/src/components/Markdown/src/Markdown.vue @@ -19,6 +19,7 @@ import { useModalContext } from '../../Modal'; import { useRootSetting } from '/@/hooks/setting/useRootSetting'; import { onMountedOrActivated } from '/@/hooks/core/onMountedOrActivated'; + import { getTheme } from './getTheme'; type Lang = 'zh_CN' | 'en_US' | 'ja_JP' | 'ko_KR' | undefined; @@ -46,8 +47,9 @@ if (!inited) { return; } - const theme = val === 'dark' ? 'dark' : 'classic'; - instance.getVditor()?.setTheme(theme); + instance + .getVditor() + ?.setTheme(getTheme(val) as any, getTheme(val, 'content'), getTheme(val, 'code')); }, { immediate: true, @@ -87,13 +89,22 @@ if (!wrapEl) return; const bindValue = { ...attrs, ...props }; const insEditor = new Vditor(wrapEl, { - theme: getDarkMode.value === 'dark' ? 'dark' : 'classic', + // 设置外观主题 + theme: getTheme(getDarkMode.value) as any, lang: unref(getCurrentLang), mode: 'sv', fullscreen: { index: 520, }, preview: { + theme: { + // 设置内容主题 + current: getTheme(getDarkMode.value, 'content'), + }, + hljs: { + // 设置代码块主题 + style: getTheme(getDarkMode.value, 'code'), + }, actions: [], }, input: (v) => { diff --git a/src/components/Markdown/src/MarkdownViewer.vue b/src/components/Markdown/src/MarkdownViewer.vue index 708aa86e..538cedaf 100644 --- a/src/components/Markdown/src/MarkdownViewer.vue +++ b/src/components/Markdown/src/MarkdownViewer.vue @@ -1,23 +1,62 @@ + const viewerRef = ref(null); + const vditorPreviewRef = ref(null) as Ref>; + const { getDarkMode } = useRootSetting(); - + watch( + () => getDarkMode.value, + (val) => { + VditorPreview.setContentTheme(getTheme(val, 'content')); + VditorPreview.setCodeTheme(getTheme(val, 'code')); + init(); + }, + ); + + watch( + () => props.value, + (v, oldValue) => { + v !== oldValue && init(); + }, + ); + + function destroy() { + const vditorInstance = unref(vditorPreviewRef); + if (!vditorInstance) return; + try { + vditorInstance?.destroy?.(); + } catch (error) {} + vditorPreviewRef.value = null; + } + + onMountedOrActivated(init); + + onBeforeUnmount(destroy); + onDeactivated(destroy); + diff --git a/src/components/Markdown/src/getTheme.ts b/src/components/Markdown/src/getTheme.ts new file mode 100644 index 00000000..fcfe9d36 --- /dev/null +++ b/src/components/Markdown/src/getTheme.ts @@ -0,0 +1,19 @@ +/** + * 获取主题类型 深色浅色模式 对应的值 + * @param darkModeVal 深色模式值 + * @param themeMode 主题类型——外观(默认), 内容, 代码块 + */ +export const getTheme = ( + darkModeVal: 'light' | 'dark' | string, + themeMode: 'default' | 'content' | 'code' = 'default', +) => { + const isDark = darkModeVal === 'dark'; + switch (themeMode) { + case 'default': + return isDark ? 'dark' : 'classic'; + case 'content': + return isDark ? 'dark' : 'light'; + case 'code': + return isDark ? 'dracula' : 'github'; + } +}; diff --git a/src/views/demo/editor/markdown/index.vue b/src/views/demo/editor/markdown/index.vue index 968bd910..d88689c9 100644 --- a/src/views/demo/editor/markdown/index.vue +++ b/src/views/demo/editor/markdown/index.vue @@ -28,16 +28,53 @@ setup() { const markDownRef = ref>(null); const valueRef = ref(` -# title +# 标题h1 -# content +##### 标题h5 + +**加粗** +*斜体* +~~删除线~~ +[链接](https://github.com/vbenjs/vue-vben-admin) +↓分割线↓ + +--- + + +* 无序列表1 + * 无序列表1.1 + +1. 有序列表1 +2. 有序列表2 + +* [ ] 任务列表1 +* [x] 任务列表2 + +> 引用示例 + +\`\`\`js +// 代码块: +(() => { + var htmlRoot = document.getElementById('htmlRoot'); + var theme = window.localStorage.getItem('__APP__DARK__MODE__'); + if (htmlRoot && theme) { + htmlRoot.setAttribute('data-theme', theme); + theme = htmlRoot = null; + } +})(); +\`\`\` + +| 表格 | 示例 | 🎉️ | +| --- | --- | --- | +| 1 | 2 | 3 | +| 4 | 5 | 6 | `); function toggleTheme() { const markDown = unref(markDownRef); if (!markDown) return; const vditor = markDown.getVditor(); - vditor.setTheme('dark'); + vditor.setTheme('dark', 'dark', 'dracula'); } function handleChange(v: string) {