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
import { Component, Model, Prop, Vue, Watch } from 'vue-property-decorator';
import * as monaco from 'monaco-editor';
import 'monaco-editor/esm/vs/basic-languages/javascript/javascript.contribution';
import 'monaco-editor/esm/vs/editor/contrib/find/findController.js';
import { CreateElement } from 'vue';
interface IToolbarItem {
key: string; //标识
text: string; //文本值
component: any; //组件
visible: boolean; //显示状态
}
@Component
export class AppCodeEditor extends Vue {
/**
* 绑定值
*
* @type {string}
* @memberof AppCodeEditor
*/
@Model('change')
value!: string;
@Watch('value')
onValueWatch() {
this.codeEditor.setValue(this.value);
}
/**
* 主题
*
* @type {string}
* @memberof AppCodeEditor
*/
@Prop({ type: String, default: 'vs-light' })
theme!: 'vs-light' | 'vs-dark';
/**
* 语言
*
* @type {string}
* @memberof AppCodeEditor
*/
@Prop({ type: String, default: 'typescript' })
language!: 'json' | 'javascript' | 'typescript' | 'css' | 'less' | 'sass' | 'java' | 'yaml';
/**
* 只读模式
*
* @type {boolean}
* @memberof AppCodeEditor
*/
@Prop({ type: Boolean, default: false })
readOnly!: boolean;
/**
* 是否显示小地图
*
* @type {boolean}
* @memberof AppCodeEditor
*/
@Prop({type: Boolean, default: true})
showMinimap!: boolean;
/**
* 是否显示行号
*
* @type {boolean}
* @memberof AppCodeEditor
*/
@Prop({type: Boolean, default: true})
showLineNum!: boolean;
/**
* 是否显示语言区
*
* @type {boolean}
* @memberof AppCodeEditor
*/
@Prop({type: Boolean, default: true})
showLanguage!: boolean;
/**
* 是否能够更换语言
*
* @type {boolean}
* @memberof AppCodeEditor
*/
@Prop({ type: Boolean, default: true })
changeLanguage!: boolean;
/**
* 当前使用语言
*
* @type {string}
* @memberof CodeEditor
*/
presentLanguage: string = '';
/**
* 语言列表
*
* @type {string[]}
* @memberof AppCodeEditor
*/
languages: string[] = ['json', 'javascript', 'typescript', 'css', 'less', 'sass', 'java', 'sql', 'yaml'];
/**
* 代码编辑器对象
*
* @type {*}
* @memberof AppCodeEditor
*/
codeEditor: any;
/**
* 左侧工具栏项
*
* @type {IToolbarItem[]}
* @memberof AppCodeEditor
*/
leftToolbarItems: IToolbarItem[] = [];
/**
* 右侧工具栏项
*
* @type {any[]}
* @memberof AppCodeEditor
*/
rightToolbarItems: IToolbarItem[] = [
{
key: 'fullScreen',
text: '全屏',
component: () => this.renderIcon('md-resize'),
visible: true,
},
{
key: 'quitFullScreen',
text: '退出全屏',
component: () => this.renderIcon('md-contract'),
visible: false,
},
];
/**
* 绘制图标
*
* @param {string} type
* @return {*}
* @memberof AppCodeEditor
*/
renderIcon(type: string) {
return <icon type={type}></icon>;
}
/**
* Vue生命周期,实例创建完成
*
* @memberof AppCodeEditor
*/
created() {
this.presentLanguage = this.language;
this.registerEvent = this.registerEvent.bind(this);
}
/**
* Vue生命周期,实例挂载完毕
*
* @memberof AppCodeEditor
*/
mounted() {
this.initCodeEditor();
}
/**
* 初始化编辑器
*
* @memberof AppCodeEditor
*/
initCodeEditor() {
const codeEditorRef = this.$refs.codeEditor;
if (codeEditorRef) {
this.codeEditor = monaco.editor.create(this.$refs.codeEditor as any, {
value: this.value,
theme: this.theme,
language: this.presentLanguage,
readOnly: this.readOnly,
lineNumbers: this.showLineNum ? 'on' : 'off',
minimap: { enabled: this.showMinimap },
});
this.registerEvent();
window.addEventListener('resize', this.resize);
window.addEventListener('fullscreenchange', this.fullscreenchange);
}
}
/**
*注册事件
*
* @memberof AppCodeEditor
*/
registerEvent() {
if (!this.readOnly) {
this.codeEditor.onDidBlurEditorText(
//数据发生改变
(event: any) => {
this.$emit('change', this.codeEditor.getValue());
this.$emit('blur', this.codeEditor.getValue());
},
);
this.codeEditor.onDidFocusEditorText(
//数据发生改变
(event: any) => {
this.$emit('focus', this.codeEditor.getValue());
},
);
}
}
/**
* 重置编辑器大小
*
* @memberof AppCodeEditor
*/
resize() {
this.codeEditor.layout();
}
/**
* 全屏状态切换触发
*
* @memberof AppCodeEditor
*/
fullscreenchange() {
if (document.fullscreenElement) {
const toolbarItem: any = this.rightToolbarItems.find(toolbarItem => toolbarItem.key === 'fullScreen');
toolbarItem.visible = false;
const item: any = this.rightToolbarItems.find(toolbarItem => toolbarItem.key === 'quitFullScreen');
item.visible = true;
} else {
const toolbarItem: any = this.rightToolbarItems.find(toolbarItem => toolbarItem.key === 'quitFullScreen');
toolbarItem.visible = false;
const item: any = this.rightToolbarItems.find(toolbarItem => toolbarItem.key === 'fullScreen');
item.visible = true;
}
}
/**
* 工具栏点击项
*
* @param {IToolbarItem} toolbarItem
* @memberof AppCodeEditor
*/
toolBarClick(toolbarItem: IToolbarItem) {
if (toolbarItem.key === 'fullScreen') {
const editorContainer = document.getElementsByClassName('app-code-editor')[0];
editorContainer.requestFullscreen();
}
if (toolbarItem.key === 'quitFullScreen') {
document.exitFullscreen();
}
this.$forceUpdate();
}
/**
* 切换语言
*
* @param {string} item
* @memberof AppCodeEditor
*/
onLanguageChange(item: string) {
this.codeEditor.dispose();
this.initCodeEditor();
this.codeEditor.trigger('anyString', 'editor.action.formatDocument');
this.$forceUpdate();
}
/**
* Vue实例销毁前
*
* @memberof AppCodeEditor
*/
beforeDestroy() {
window.removeEventListener('resize', this.resize);
}
/**
* 绘制函数
*
* @return {*}
* @memberof AppCodeEditor
*/
render(h: CreateElement) {
return (
<div class='app-code-editor'>
<div class={{ 'app-code-editor__header': true, [this.theme]: true, 'app-code-editor__header--hidden': !this.showLanguage }}>
<div class='app-code-editor__header__left__toolbar'>
<i-select
v-model={this.presentLanguage}
disabled={!this.changeLanguage}
on-on-change={this.onLanguageChange}
>
{this.languages.map((language: string) => {
return (
<i-option value={language} key={language}>
{language}
</i-option>
);
})}
</i-select>
</div>
<div class='app-code-editor__header__right__toolbar'>
{this.rightToolbarItems.map((toolbarItem: IToolbarItem) => {
return toolbarItem.visible ? (
<div
class='toolbar-item'
title={toolbarItem.text}
on-click={() => this.toolBarClick(toolbarItem)}
>
{toolbarItem.component()}
</div>
) : null;
})}
</div>
</div>
<div class='app-code-editor__content' ref='codeEditor'></div>
</div>
);
}
}
// 默认导出
export default AppCodeEditor;