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
| <template>
| <div style="position: relative">
| <div :id="id" :style="{ overflow: 'hidden', height: height + 'px' }"></div>
| <i-switch
| v-model="themeSwitch"
| @on-change="changeTheme"
| class="monaco-theme"
| v-if="showThemeChange"
| >
| <Icon type="md-moon" slot="open"></Icon>
| <Icon type="md-sunny" slot="close"></Icon>
| </i-switch>
| </div>
| </template>
| <script>
| export default {
| name: "monaco",
| props: {
| id: {
| type: String,
| default: "monaco",
| },
| height: {
| type: [Number, String],
| default: 500,
| },
| value: {
| type: String,
| default: "",
| },
| language: String,
| lineNumbers: {
| type: String,
| default: "on",
| },
| readOnly: {
| type: Boolean,
| default: false,
| },
| cursorStyle: {
| type: String,
| default: "line",
| },
| fontSize: {
| type: Number,
| default: 12,
| },
| showThemeChange: {
| type: Boolean,
| default: true,
| },
| },
| data() {
| return {
| data: this.value,
| monacoEditor: null,
| themeSwitch: false,
| theme: "vs",
| };
| },
| methods: {
| init() {
| this.monacoEditor = monaco.editor.create(
| document.getElementById(this.id),
| {
| value: this.value,
| language: this.language,
|
| theme: this.theme, // 主题'vs' (default), 'vs-dark', 'hc-black'
| lineNumbers: this.lineNumbers, // 显示行号
| readOnly: this.readOnly, // 是否只读
| cursorStyle: this.cursorStyle, // 光标
| fontSize: this.fontSize, //字体大小
| }
| );
| // 格式化代码
| setTimeout(() => {
| if (!this.monacoEditor) {
| return;
| }
| this.monacoEditor.getAction(["editor.action.formatDocument"])._run();
| }, 500);
| // 监听事件
| this.monacoEditor.onDidChangeModelContent((e) => {
| let v = this.monacoEditor.getValue();
| this.data = v;
| this.$emit("input", this.data);
| this.$emit("on-change", this.data);
| });
| },
| changeTheme(v) {
| let theme = "vs";
| if (v) {
| theme = "vs-dark";
| }
| this.monacoEditor.updateOptions({
| theme: theme,
| });
| },
| setData(value) {
| if (!this.monacoEditor) {
| this.init();
| }
| if (value != this.data) {
| this.data = value;
| this.monacoEditor.setValue(this.data);
| // 格式化代码
| this.monacoEditor.getAction(["editor.action.formatDocument"])._run();
| this.$emit("input", this.data);
| this.$emit("on-change", this.data);
| }
| },
| layout(){
| this.monacoEditor.layout();
| }
| },
| beforeDestroy() {
| // 调用销毁 API 对当前编辑器实例进行销毁
| if (this.monacoEditor != null) {
| this.monacoEditor.dispose();
| }
| },
| watch: {
| value(val) {
| this.setData(val);
| },
| },
| mounted() {
| this.init();
| },
| };
| </script>
| <style lang="less" scoped>
| .monaco-theme {
| position: absolute;
| right: 25px;
| bottom: 15px;
| }
| </style>
|
|