forked from bazingaedward/monaco-editor-vue3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMonacoEditor.vue
124 lines (120 loc) · 3.39 KB
/
MonacoEditor.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
<template>
<div class="monaco-editor-vue3" :style="style"></div>
</template>
<script>
import { defineComponent, computed, toRefs } from 'vue'
import * as monaco from 'monaco-editor'
export default defineComponent({
name: "MonacoEditor",
props: {
diffEditor: { type: Boolean, default: false },
width: {type: [String, Number], default: '100%'},
height: {type: [String, Number], default: '100%'},
original: String,
value: String,
language: {type: String, default: 'javascript'},
theme: {type: String, default: 'vs'},
options: {type: Object, default() {return {};}},
},
emits: [
'editorWillMount',
'editorDidMount',
'change'
],
setup(props){
const { width, height } = toRefs(props)
const style = computed(()=>{
const fixedWidth = width.value.toString().includes('%') ? width.value : `${width.value}px`
const fixedHeight = height.value.toString().includes('%')? height.value : `${height.value}px`
return {
width: fixedWidth,
height: fixedHeight,
'text-align': 'left'
}
})
return {
style
}
},
mounted() {
this.initMonaco()
},
beforeDestroy() {
this.editor && this.editor.dispose();
},
methods: {
initMonaco(){
this.$emit('editorWillMount', this.monaco)
const { value, language, theme, options } = this;
this.editor = monaco.editor[this.diffEditor ? 'createDiffEditor' : 'create'](this.$el, {
value: value,
language: language,
theme: theme,
...options
});
this.diffEditor && this._setModel(this.value, this.original);
// @event `change`
const editor = this._getEditor()
editor.onDidChangeModelContent(event => {
const value = editor.getValue()
if (this.value !== value) {
this.$emit('change', value, event)
}
})
this.$emit('editorDidMount', this.editor)
},
_setModel(value, original) {
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();
},
_getEditor() {
if(!this.editor) return null;
return this.diffEditor ? this.editor.modifiedEditor : this.editor;
},
_setOriginal(){
const { original } = this.editor.getModel()
original.setValue(this.original)
}
},
watch: {
options: {
deep: true,
handler(options) {
this.editor.updateOptions(options);
}
},
value() {
this.value !== this._getValue() && this._setValue(this.value);
},
original() {
this._setOriginal()
},
language() {
if(!this.editor) return;
if(this.diffEditor){
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() {
monaco.editor.setTheme(this.theme);
},
}
});
</script>