11<template >
2- <div :style =" {height:height+'px',zIndex:zIndex}" class =" simplemde-container" >
3- <textarea :id =" id" />
4- </div >
2+ <div :id =" id" />
53</template >
64
75<script >
8- import ' font-awesome/css/font-awesome.min.css'
9- import ' simplemde/dist/simplemde.min.css'
10- import SimpleMDE from ' simplemde'
6+ // deps for editor
7+ import ' codemirror/lib/codemirror.css' // codemirror
8+ import ' tui-editor/dist/tui-editor.css' // editor ui
9+ import ' tui-editor/dist/tui-editor-contents.css' // editor content
10+
11+ import Editor from ' tui-editor'
12+ import defaultOptions from ' ./defaultOptions'
1113
1214export default {
13- name: ' SimplemdeMd ' ,
15+ name: ' MarddownEditor ' ,
1416 props: {
1517 value: {
1618 type: String ,
@@ -19,105 +21,98 @@ export default {
1921 id: {
2022 type: String ,
2123 required: false ,
22- default: ' markdown-editor-' + + new Date ()
24+ default () {
25+ return ' markdown-editor-' + + new Date () + ((Math .random () * 1000 ).toFixed (0 ) + ' ' )
26+ }
2327 },
24- autofocus: {
25- type: Boolean ,
26- default: false
28+ options: {
29+ type: Object ,
30+ default () {
31+ return defaultOptions
32+ }
2733 },
28- placeholder : {
34+ mode : {
2935 type: String ,
30- default: ' '
36+ default: ' markdown '
3137 },
3238 height: {
33- type: Number ,
34- default: 150
35- },
36- zIndex: {
37- type: Number ,
38- default: 10
39+ type: String ,
40+ required: false ,
41+ default: ' 300px'
3942 },
40- toolbar: {
41- type: Array ,
42- default : function () {
43- return []
44- }
43+ language: {
44+ type: String ,
45+ required: false ,
46+ default: ' en_US' // https://github.com/nhnent/tui.editor/tree/master/src/js/langs
4547 }
4648 },
4749 data () {
4850 return {
49- simplemde: null ,
50- hasChange: false
51+ editor: null
52+ }
53+ },
54+ computed: {
55+ editorOptions () {
56+ const options = Object .assign ({}, defaultOptions, this .options )
57+ options .initialEditType = this .mode
58+ options .height = this .height
59+ options .language = this .language
60+ return options
5161 }
5262 },
5363 watch: {
54- value (val ) {
55- if (val === this .simplemde .value () && ! this .hasChange ) return
56- this .simplemde .value (val)
64+ value (newValue , preValue ) {
65+ if (newValue !== preValue && newValue !== this .editor .getValue ()) {
66+ this .editor .setValue (newValue)
67+ }
68+ },
69+ language (val ) {
70+ this .destroyEditor ()
71+ this .initEditor ()
72+ },
73+ height (newValue ) {
74+ this .editor .height (newValue)
75+ },
76+ mode (newValue ) {
77+ this .editor .changeMode (newValue)
5778 }
5879 },
5980 mounted () {
60- this .simplemde = new SimpleMDE ({
61- element: document .getElementById (this .id ),
62- autoDownloadFontAwesome: false ,
63- autofocus: this .autofocus ,
64- toolbar: this .toolbar .length > 0 ? this .toolbar : undefined ,
65- spellChecker: false ,
66- insertTexts: {
67- link: [' [' , ' ]( )' ]
68- },
69- // hideIcons: ['guide', 'heading', 'quote', 'image', 'preview', 'side-by-side', 'fullscreen'],
70- placeholder: this .placeholder
71- })
72- if (this .value ) {
73- this .simplemde .value (this .value )
74- }
75- this .simplemde .codemirror .on (' change' , () => {
76- if (this .hasChange ) {
77- this .hasChange = true
78- }
79- this .$emit (' input' , this .simplemde .value ())
80- })
81+ this .initEditor ()
8182 },
8283 destroyed () {
83- this .simplemde .toTextArea ()
84- this .simplemde = null
84+ this .destroyEditor ()
85+ },
86+ methods: {
87+ initEditor () {
88+ this .editor = new Editor ({
89+ el: document .getElementById (this .id ),
90+ ... this .editorOptions
91+ })
92+ if (this .value ) {
93+ this .editor .setValue (this .value )
94+ }
95+ this .editor .on (' change' , () => {
96+ this .$emit (' input' , this .editor .getValue ())
97+ })
98+ },
99+ destroyEditor () {
100+ if (! this .editor ) return
101+ this .editor .off (' change' )
102+ this .editor .remove ()
103+ },
104+ setValue (value ) {
105+ this .editor .setValue (value)
106+ },
107+ getValue () {
108+ return this .editor .getValue ()
109+ },
110+ setHtml (value ) {
111+ this .editor .setHtml (value)
112+ },
113+ getHtml () {
114+ return this .editor .getHtml ()
115+ }
85116 }
86117}
87118 </script >
88-
89- <style scoped>
90- .simplemde-container >>> .CodeMirror {
91- min-height : 150px ;
92- line-height : 20px ;
93- }
94-
95- .simplemde-container >>> .CodeMirror-scroll {
96- min-height : 150px ;
97- }
98-
99- .simplemde-container >>> .CodeMirror-code {
100- padding-bottom : 40px ;
101- }
102-
103- .simplemde-container >>> .editor-statusbar {
104- display : none ;
105- }
106-
107- .simplemde-container >>> .CodeMirror .CodeMirror-code .cm-link {
108- color : #1890ff ;
109- }
110-
111- .simplemde-container >>> .CodeMirror .CodeMirror-code .cm-string.cm-url {
112- color : #2d3b4d ;
113- }
114-
115- .simplemde-container >>> .CodeMirror .CodeMirror-code .cm-formatting-link-string.cm-url {
116- padding : 0 2px ;
117- color : #E61E1E ;
118- }
119- .simplemde-container >>> .editor-toolbar.fullscreen ,
120- .simplemde-container >>> .CodeMirror-fullscreen {
121- z-index : 1003 ;
122- }
123- </style >
0 commit comments