-
Notifications
You must be signed in to change notification settings - Fork 109
/
codemirror-editor.vue
364 lines (322 loc) · 8.8 KB
/
codemirror-editor.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
<template>
<v-md-container
:left-toolbar="leftToolbar"
:right-toolbar="rightToolbar"
:toolbars="toolbars"
:disabled-menus="disabledMenus"
:height="height"
:fullscreen="fullscreen"
:left-area-visible="tocVisible"
:left-area-title="langConfig.toc.title"
:left-area-reverse="tocNavPositionRight"
:mode="currentMode"
@toolbar-item-click="handleToolbarItemClick"
@toolbar-menu-click="handleToolbarMenuClick"
@resize="handleContainerResize"
ref="contaner"
>
<scrollbar slot="left-area">
<toc-nav
:titles="titles"
@nav-click="handleNavClick"
/>
</scrollbar>
<div
class="codemirror-wrapper"
:class="{
'codemirror-reset': codemirrorStyleReset
}"
slot="editor"
ref="codemirrorEditor"
/>
<scrollbar
slot="preview"
ref="previewScroller"
>
<v-md-preview
:text="text"
:tab-size="tabSize"
:scroll-container="getPreviewScrollContainer"
@change="handleChange"
@image-click="handlePreviewImageClick"
ref="preview"
/>
</scrollbar>
<v-md-upload-file
v-if="hasUploadImage"
:upload-config="uploadConfig"
ref="uploadFile"
/>
</v-md-container>
</template>
<script>
import createEditor from './create-editor';
import { smooth } from '@/utils/smooth-scroll';
import HotKeys from '@/utils/hotkeys';
const component = {
props: {
codemirrorConfig: Object,
codemirrorStyleReset: {
type: Boolean,
default: true,
},
},
watch: {
value() {
if (this.value !== this.text) {
this.text = this.value;
this.codemirrorInstance.setValue(this.text);
}
},
},
created() {
this.hotkeysManager = new HotKeys();
},
computed: {
Codemirror() {
return this.$options.Codemirror;
},
},
mounted() {
if (!this.Codemirror) {
return console.error(
'1.5.0与2.1.0版本之后Codemirror将由用户自己配置,请配置Codemirror,如何配置请参考相关文档'
);
}
this.codemirrorInstance = new this.Codemirror(this.$refs.codemirrorEditor, {
lineNumbers: true,
styleActiveLine: true,
autoCloseTags: true,
matchBrackets: true,
indentWithTabs: true,
autoCloseBrackets: true,
tabSize: this.tabSize,
indentUnit: this.tabSize,
value: this.text,
placeholder: this.placeholder,
mode: 'markdown',
lineWrapping: true,
scrollbarStyle: 'overlay',
...this.codemirrorConfig
});
this.codemirrorInstance.on('change', () => {
const newValue = this.getValue();
this.handleInput(newValue);
});
this.codemirrorInstance.on('scroll', () => {
this.handleEditorScroll();
});
this.codemirrorInstance.on('keydown', (_, e) => {
this.hotkeysManager.dispatch(e);
});
this.codemirrorInstance.on('drop', (_, e) => {
this.handleDrop(e);
});
this.codemirrorInstance.on('paste', (_, e) => {
this.handlePaste(e);
});
},
beforeDestory() {
const element = this.codemirrorInstance.doc.cm.getWrapperElement();
element?.remove?.();
},
methods: {
handleContainerResize() {
if (!this.Codemirror) return;
// 容器大小变化的时候刷新 codemirror 解决滚动条的显示问题
this.codemirrorInstance.refresh();
},
getValue() {
return this.codemirrorInstance.getValue();
},
getIndexInInterval(number, start, end) {
if (start <= number && number <= end) {
return number - start;
}
},
// Must implement
delLineLeft() {
this.codemirrorInstance.execCommand('delLineLeft');
},
// Must implement
getCursorLineLeftText() {
const { codemirrorInstance } = this;
const { line: startLine, ch: startCh } = codemirrorInstance.getCursor('from');
const { line: endLine, ch: endCh } = codemirrorInstance.getCursor('to');
if (startLine !== endLine || startCh !== endCh) return;
return codemirrorInstance.getLine(startLine).slice(0, startCh);
},
// Must implement
editorRegisterHotkeys(...arg) {
this.hotkeysManager.registerHotkeys(...arg);
},
// Must implement
editorScrollToTop(scrollTop) {
const currentScrollTop = this.getScrollInfo().top;
smooth({
currentScrollTop,
scrollToTop: scrollTop,
scrollFn: (scrollTop) => this.codemirrorInstance.scrollTo(0, scrollTop),
});
},
// Must implement
getScrollInfo() {
return this.codemirrorInstance.getScrollInfo();
},
// Must implement
heightAtLine(...arg) {
return this.codemirrorInstance.heightAtLine(...arg);
},
// Must implement
focus() {
this.codemirrorInstance.focus();
},
// Must implement
undo() {
this.codemirrorInstance.undo();
},
// Must implement
redo() {
this.codemirrorInstance.redo();
},
// Must implement
clear() {
this.codemirrorInstance.setValue('');
},
// Must implement
editorFocusEnd() {
this.focus();
const lastLineIndex = this.codemirrorInstance.lastLine();
const lastLineContent = this.codemirrorInstance.getLine(lastLineIndex);
this.codemirrorInstance.setCursor({ line: lastLineIndex, ch: lastLineContent.length });
},
// Must implement
replaceSelectionText(text, type = 'around') {
this.codemirrorInstance.replaceSelection(text, type);
},
// Must implement
changeSelctionTo(insertText, selectedText) {
const curStartLine = this.codemirrorInstance.getCursor('from');
const curEndLine = this.codemirrorInstance.getCursor('to');
if (!selectedText) {
this.codemirrorInstance.setSelection(curEndLine);
return;
}
const lines = this.text.split('\n').slice(curStartLine.line, curEndLine.line + 1);
const startIndex = lines.join('\n').indexOf(selectedText, curStartLine.ch);
const endIndex = startIndex + selectedText.length;
const start = {
line: 0,
ch: null,
indexOfSelected: startIndex,
};
const end = {
line: 0,
ch: null,
indexOfSelected: endIndex,
};
let strLen = 0;
lines.find((rowText, lineIndex) => {
const rowLength = rowText.length;
[start, end].forEach((position) => {
const newCursor = this.getIndexInInterval(
position.indexOfSelected,
strLen,
strLen + rowLength
);
if (typeof newCursor !== 'undefined') {
position.ch = newCursor;
position.line = curStartLine.line + lineIndex;
}
});
// + 1 是算上换行符占的长度
strLen += rowLength + 1;
return start.ch !== null && end.ch !== null;
});
this.codemirrorInstance.setSelection(end, start);
},
// Must implement
getCurrentSelectedStr() {
return this.codemirrorInstance.getSelection();
},
},
};
createEditor(component);
export default component;
</script>
<style lang="scss">
@import '@/styles/var';
.v-md-editor {
.codemirror-wrapper {
height: 100%;
.CodeMirror {
height: 100%;
}
&.codemirror-reset {
.CodeMirror {
color: $text-color;
font-size: $editor-font-size;
font-family: $editor-font-family;
line-height: $editor-line-height;
&:hover {
.CodeMirror-overlayscroll-vertical,
.CodeMirror-overlayscroll-horizontal {
& > div {
opacity: 1;
}
}
}
pre {
padding: 0 12px;
word-break: break-all;
}
}
.CodeMirror-empty {
color: $text-color-placeholder;
}
.cm-header,
.cm-url,
.cm-link {
color: #1890ff;
}
.cm-link {
text-decoration: none;
}
.cm-url {
text-decoration: underline;
}
.cm-quote,
.cm-comment,
.cm-variable-2:not(.cm-url),
.cm-link {
color: $text-color;
}
// 选中代码的高亮背景色
.CodeMirror-selected {
background: mix(#e8f2ff, #000, 90%);
}
.CodeMirror-linenumbers {
padding: 0 5px;
}
// 滚动条样式
.CodeMirror-overlayscroll-vertical,
.CodeMirror-overlayscroll-horizontal {
& > div {
background-color: $scrollbar-background-color;
border-radius: $scrollbar-border-radius;
cursor: pointer;
opacity: 0;
transition: opacity $scrollbar-opacity-transition,
background-color $scrollbar-background-transition;
&:hover {
background-color: $scrollbar-active-background-color;
}
}
}
.CodeMirror-overlayscroll-vertical {
right: 2px;
}
}
}
}
</style>