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
import * as monaco from 'monaco-editor/esm/vs/editor/editor.api';
import 'monaco-editor/esm/vs/basic-languages/javascript/javascript.contribution';
import beautifier from './json-beautifier'
function noop() { }
export { monaco };
export default {
name: 'MonacoEditor',
props: {
diffEditor: { type: Boolean, default: false }, //是否使用diff模式
width: { type: [String, Number], default: '100%' },
height: { type: [String, Number], default: '100%' },
original: String, //只有在diff模式下有效
value: [String, Object],
language: { type: String, default: 'javascript' },
theme: { type: String, default: 'vs' },
readOnly: { type: Boolean, default: false },
options: { type: Object, default() { return {}; } },
editorMounted: { type: Function, default: noop },
editorBeforeMount: { type: Function, default: noop },
keyIndex: { type: String }
},
watch: {
options: {
deep: true,
handler(options) {
this.editor && this.editor.updateOptions(options);
}
},
keyIndex() {
let data = this.value
if (typeof data != 'object') return
data = beautifier(data)
if (this.editor && data !== this._getValue()) {
this._setValue(data)
}
},
language() {
if (!this.editor) return;
if (this.diffEditor) { //diff模式下更新language
const { original, modified } = this.editor.getModel();
monaco.editor.setModelLanguage(original, this.language);
monaco.editor.setModelLanguage(modified, this.language);
} else
monaco.editor.setModelLanguage(this.editor.getModel(), this.language);
},
theme() {
this.editor && monaco.editor.setTheme(this.theme);
},
style() {
this.editor && this.$nextTick(() => {
this.editor.layout();
});
}
},
computed: {
style() {
return {
width: !/^\d+$/.test(this.width) ? this.width : `${this.width}px`,
height: !/^\d+$/.test(this.height) ? this.height : `${this.height}px`
}
}
},
mounted() {
this.initMonaco();
},
beforeDestroy() {
this.editor && this.editor.dispose();
},
render() {
return (
<div class="monaco_editor_container" style={this.style}></div>
);
},
methods: {
initMonaco() {
const { value, language, theme, readOnly, options } = this;
Object.assign(options, this._editorBeforeMount()); //编辑器初始化前
this.editor = monaco.editor[this.diffEditor ? 'createDiffEditor' : 'create'](this.$el, {
value: (typeof value == 'string') ? value : beautifier(value),
language: language,
theme: theme,
readOnly: readOnly,
...options
});
this.diffEditor && this._setModel(this.value, this.original);
this._editorMounted(this.editor); //编辑器初始化后
},
_getEditor() {
if (!this.editor) return null;
return this.diffEditor ? this.editor.modifiedEditor : this.editor;
},
_setModel(value, original) { //diff模式下设置model
const { language } = this;
const originalModel = monaco.editor.createModel(original, language);
const modifiedModel = monaco.editor.createModel(value, language);
this.editor.setModel({
original: originalModel,
modified: modifiedModel
});
},
_setValue(value) {
let editor = this._getEditor();
if (editor) return editor.setValue(value);
},
_getValue() {
let editor = this._getEditor();
if (!editor) return '';
return editor.getValue();
},
_editorBeforeMount() {
const options = this.editorBeforeMount(monaco);
return options || {};
},
_editorMounted(editor) {
this.editorMounted(editor, monaco);
if (this.diffEditor) {
editor.onDidUpdateDiff((event) => {
const value = this._getValue();
this._emitChange(value, event);
});
} else {
editor.onDidChangeModelContent(event => {
const value = this._getValue();
this._emitChange(value, event);
});
}
},
_emitChange(value, event) {
this.$emit('change', value, event);
this.$emit('input', value);
}
}
}