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>
|