vue中整合editor.md

在做毕设过程中需要在前端用到Markdown编辑器,一想就想到了Editor.md,虽然有和Vue整合好的mavonEditor可供选择,但是还是喜欢Editor.md,就想办法搞一下。

Editor.md作为一个很成熟开元Markdown编辑器,虽然已经好久不更新了,但是无疑还是非常实用的,由于Editor.md并没有提供与Vue整合的直接方案,但是我们可以自己把它封装成一个Vue组件来使用。

参考了两篇文章:

https://blog.csdn.net/jdbdh/article/details/90314447

http://www.lwl.tech/post/3

首先了解到Editor.md是通过附着在window对象上来暴露给用户使用的,并且在初始化的时候依赖了jQuery,那么我们要解决:

  1. 把jQuery挂载到window对象

  2. 异步加载editormd.mi.js后创建editor示例

MarkdownEditor.vue

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
<template>
<div class="markdown-editor-box">
<link rel="stylesheet" href="/static/editormd/css/editormd.min.css" />
<link rel="stylesheet" :href="'/static/editormd/css/themes/' + themeCode" />
<div :id="id"></div>
</div>
</template>
<script>
import $ from "jquery";
import { v4 as uuid } from "uuid";
import { defaultConfig } from "../../config/editor.md";

export default {
name: "MarkdownEditor",
props: {
id: {
type: String,
default: uuid(),
},
config: {
type: Object,
},
themeCode: {
type: String,
default: "atelier-forest-light.min.css",
// preview at https://jmblog.github.io/color-themes-for-google-code-prettify/
// location /static/editormd/css/themes/
},
initData: {
type: String,
},
delay: {
type: Number,
default: 0,
},
},
data: function () {
return {
editor: null,
};
},
methods: {
getConfig: function () {
// return Object.assign(defaultConfig, this.config);
// or
return { ...defaultConfig, ...this.config };
// maybe add custom config from back-end
// return { ...defaultConfig, ...this.config, ...customConfig };
},
initEditor: function () {
let vm = this;
window.$ = window.jQuery = $;
(async () => {
await $.getScript("/static/editormd/editormd.min.js");
let editor = window.editormd(this.id, this.getConfig());
vm.editor = editor;
// this.$nextTick(() => {
// let editor = window.editormd(this.editorId, this.getConfig());
// editor.on("load", () => {
// setTimeout(() => {
// // hack bug: 一个页面多个编辑器只能初始化其中一个数据问题
// this.editorLoaded = true;
// this.initData && editor.setMarkdown(this.initData);
// }, this.initDataDelay);
// });
// this.onchange &&
// editor.on("change", () => {
// let html = editor.getPreviewedHTML();
// this.onchange({
// markdown: editor.getMarkdown(),
// html: html,
// text: window.$(html).text(),
// });
// });
// this.editor = editor;
// });
})();
},
},
mounted: function () {
let vm = this;
vm.initEditor();
},
watch: {
initData: function (newVal) {
if (newVal) {
this.editorLoaded && this.editor.setMarkdown(newVal);
}
},
},
};
</script>
1
"undo","redo","|","bold","del","italic","quote","ucwords","uppercase","lowercase","h1","h2","h3","h4","h5","|","list-ul","list-ol","hr","|","link","reference-link","image","code","preformatted-text","code-block","table","datetime","emoji","html-entities","pagebreak","goto-line","watch","preview","fullscreen","clear","search","help","info"
1
"undo","redo","|","bold","del","italic","quote","list-ul","list-ol","hr","|","h1","h2","h3","|","link","image","code-block","table","||","watch","preview","fullscreen","clear","search"
本文由adiynil原创编写,转载请尽量保留版权网址,感谢您的理解与分享!