forked from F-loat/vue-simplemde
/
index.vue
160 lines (148 loc) · 3.79 KB
/
index.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
<template>
<div class="vue-easymde">
<textarea
class="vue-easymde-textarea"
:name="name"
:value="modelValue"
@input="handleInput($event.target.value)"
/>
</div>
</template>
<script>
// eslint-disable-next-line import/extensions
import EasyMDE from 'easymde';
// eslint-disable-next-line import/extensions
import marked from 'marked';
export default {
name: 'vue-easymde',
model: {
prop: 'modelValue',
event: 'update:modelValue',
},
props: {
value: String,
modelValue: String,
name: String,
previewClass: String,
autoinit: {
type: Boolean,
default() {
return true;
},
},
highlight: {
type: Boolean,
default() {
return false;
},
},
sanitize: {
type: Boolean,
default() {
return false;
},
},
configs: {
type: Object,
default() {
return {};
},
},
previewRender: {
type: Function,
},
},
data() {
return {
isValueUpdateFromInner: false,
};
},
mounted() {
if (this.autoinit) this.initialize();
},
deactivated() {
const editor = this.easymde;
if (!editor) return;
const isFullScreen = editor.codemirror.getOption('fullScreen');
if (isFullScreen) editor.toggleFullScreen();
},
methods: {
initialize() {
const configs = Object.assign({
element: this.$el.firstElementChild,
initialValue: this.modelValue || this.value,
previewRender: this.previewRender,
renderingConfig: {},
}, this.configs);
// 同步 modelValue 和 initialValue 的值 \ Synchronize the value of modelValue and initialValue
if (configs.initialValue) {
this.$emit('update:modelValue', configs.initialValue);
}
// 判断是否开启代码高亮 \ Determine whether to enable code highlighting
if (this.highlight) {
configs.renderingConfig.codeSyntaxHighlighting = true;
}
// 设置是否渲染输入的html \ Set whether to render the input html
marked.setOptions({ sanitize: this.sanitize });
// 实例化编辑器 \ Instantiated editor
this.easymde = new EasyMDE(configs);
// 添加自定义 previewClass \ Add a custom previewClass
const className = this.previewClass || '';
this.addPreviewClass(className);
// 绑定事件 \ Binding event
this.bindingEvents();
this.$nextTick(() => {
this.$emit('initialized', this.easymde);
});
},
bindingEvents() {
this.easymde.codemirror.on('change', (instance, changeObj) => {
if (changeObj.origin === 'setValue') {
return;
}
const val = this.easymde.value();
this.handleInput(val);
});
this.easymde.codemirror.on('blur', () => {
const val = this.easymde.value();
this.handleBlur(val);
});
},
addPreviewClass(className) {
const wrapper = this.easymde.codemirror.getWrapperElement();
const preview = document.createElement('div');
wrapper.nextSibling.className += ` ${className}`;
preview.className = `editor-preview ${className}`;
wrapper.appendChild(preview);
},
handleInput(val) {
this.isValueUpdateFromInner = true;
this.$emit('update:modelValue', val);
},
handleBlur(val) {
this.isValueUpdateFromInner = true;
this.$emit('blur', val);
},
},
destroyed() {
this.easymde = null;
},
watch: {
modelValue(val) {
if (this.isValueUpdateFromInner) {
this.isValueUpdateFromInner = false;
} else {
this.easymde.value(val);
}
},
},
};
</script>
<style>
.vue-easymde .markdown-body {
padding: 0.5em
}
.vue-easymde .editor-preview-active, .vue-easymde .editor-preview-active-side {
display: block;
}
</style>