From ac450b0e04ce1609f4b945211ab108a21a719737 Mon Sep 17 00:00:00 2001 From: AceDataCloud Date: Mon, 6 Sep 2021 00:54:49 +0800 Subject: [PATCH] fix editor --- src/components/common/SuccessInfo.vue | 42 +++++++++++++++++++++ src/components/common/TopHeader.vue | 1 - src/components/common/index.ts | 3 +- src/i18n/zh/content/index.ts | 6 ++- src/i18n/zh/content/message.ts | 3 ++ src/i18n/zh/content/text.ts | 3 ++ src/layouts/Content.vue | 1 - src/layouts/Editor.vue | 39 -------------------- src/layouts/index.ts | 4 +- src/libs/vditor/index.ts | 26 ++++++++++++- src/pages/content/article/Detail.vue | 53 ++++++++++++++++++++++----- src/services/article/types.ts | 12 +++--- 12 files changed, 132 insertions(+), 61 deletions(-) create mode 100644 src/components/common/SuccessInfo.vue create mode 100644 src/i18n/zh/content/message.ts create mode 100644 src/i18n/zh/content/text.ts delete mode 100644 src/layouts/Editor.vue diff --git a/src/components/common/SuccessInfo.vue b/src/components/common/SuccessInfo.vue new file mode 100644 index 00000000..3daf8b50 --- /dev/null +++ b/src/components/common/SuccessInfo.vue @@ -0,0 +1,42 @@ + + + + + diff --git a/src/components/common/TopHeader.vue b/src/components/common/TopHeader.vue index 7ca5fbba..c9b22e2a 100644 --- a/src/components/common/TopHeader.vue +++ b/src/components/common/TopHeader.vue @@ -25,7 +25,6 @@ export default defineComponent({ }, computed: { active() { - console.log('this.$route', this.$route); return this.$route.matched[0].path; } }, diff --git a/src/components/common/index.ts b/src/components/common/index.ts index 50b9d8c5..7c9b586e 100644 --- a/src/components/common/index.ts +++ b/src/components/common/index.ts @@ -1,4 +1,5 @@ import TopHeader from './TopHeader.vue'; import Breadcrumb from './Breadcrumb.vue'; import Loading from './Loading.vue'; -export { TopHeader, Breadcrumb, Loading }; +import SuccessInfo from './SuccessInfo.vue'; +export { TopHeader, Breadcrumb, Loading, SuccessInfo }; diff --git a/src/i18n/zh/content/index.ts b/src/i18n/zh/content/index.ts index 297e077a..00019cb5 100644 --- a/src/i18n/zh/content/index.ts +++ b/src/i18n/zh/content/index.ts @@ -1,7 +1,11 @@ import nav from './nav'; import editor from './editor'; +import message from './message'; +import text from './text'; export default { nav, - editor + editor, + message, + text }; diff --git a/src/i18n/zh/content/message.ts b/src/i18n/zh/content/message.ts new file mode 100644 index 00000000..1ea49500 --- /dev/null +++ b/src/i18n/zh/content/message.ts @@ -0,0 +1,3 @@ +export default { + saved: '保存成功', +}; diff --git a/src/i18n/zh/content/text.ts b/src/i18n/zh/content/text.ts new file mode 100644 index 00000000..9752cd41 --- /dev/null +++ b/src/i18n/zh/content/text.ts @@ -0,0 +1,3 @@ +export default { + notitle: '无标题' +}; diff --git a/src/layouts/Content.vue b/src/layouts/Content.vue index 558f9e15..7233dc6f 100644 --- a/src/layouts/Content.vue +++ b/src/layouts/Content.vue @@ -40,7 +40,6 @@ export default defineComponent({ name: 'LayoutContent', computed: { showSidebar(): boolean { - console.log('router', this.$route.name); return !this.isDetailRoute(); }, showFullHeight(): boolean { diff --git a/src/layouts/Editor.vue b/src/layouts/Editor.vue deleted file mode 100644 index db837016..00000000 --- a/src/layouts/Editor.vue +++ /dev/null @@ -1,39 +0,0 @@ - - - - - - diff --git a/src/layouts/index.ts b/src/layouts/index.ts index e4d0b4f9..b5492ae6 100644 --- a/src/layouts/index.ts +++ b/src/layouts/index.ts @@ -1,4 +1,4 @@ import LayoutHome from '@/layouts/Home.vue'; -import LayoutEditor from '@/layouts/Content.vue'; +import LayoutContent from '@/layouts/Content.vue'; -export { LayoutHome, LayoutEditor }; +export { LayoutHome, LayoutContent }; diff --git a/src/libs/vditor/index.ts b/src/libs/vditor/index.ts index bdc0698b..4884b9c5 100644 --- a/src/libs/vditor/index.ts +++ b/src/libs/vditor/index.ts @@ -38,6 +38,7 @@ import { renderDomByMd } from './ts/wysiwyg/renderDomByMd'; class Vditor extends VditorMethod { public readonly version: string; + public initialized = false; public vditor: IVditor; /** @@ -95,6 +96,24 @@ class Vditor extends VditorMethod { } } + public getTitle(defaultValue: string = '') { + const markdown = getMarkdown(this.vditor); + const result = /^\s?#\s{0,1}([^\n]*)\n/g.exec(markdown); + if (result && result.length > 1) { + return result[1] || defaultValue; + } + return defaultValue; + } + + public getContent(defaultValue: string = '') { + const markdown = getMarkdown(this.vditor); + const result = /^(?:\s?#\s{0,1}[^\n]*\n?)?([\s\S]*)$/g.exec(markdown); + if (result && result.length > 1) { + return result[1] || defaultValue; + } + return defaultValue; + } + /** 获取 Markdown 内容 */ public getValue() { return getMarkdown(this.vditor); @@ -255,7 +274,9 @@ class Vditor extends VditorMethod { } /** 设置编辑器内容 */ - public setValue(markdown: string, clearStack = false) { + public setValue({ title, content }: { title?: string; content?: string }, clearStack = false) { + const markdown = `# ${title || ''}\n${content || ''}`; + if (this.vditor.currentMode === 'sv') { this.vditor.sv.element.innerHTML = this.vditor.lute.SpinVditorSVDOM(markdown); processSVAfterRender(this.vditor, { @@ -456,9 +477,12 @@ class Vditor extends VditorMethod { initUI(this.vditor); + this.initialized = true; + if (mergedOptions.after) { mergedOptions.after(); } + if (mergedOptions.icon) { // 防止初始化 2 个编辑器时加载 2 次 addScriptSync(`${mergedOptions.cdn}/dist/js/icons/${mergedOptions.icon}.js`, 'vditorIconScript'); diff --git a/src/pages/content/article/Detail.vue b/src/pages/content/article/Detail.vue index 2685f8d9..89375472 100644 --- a/src/pages/content/article/Detail.vue +++ b/src/pages/content/article/Detail.vue @@ -3,6 +3,7 @@
+
@@ -11,7 +12,7 @@