forked from wanglin2/code-run
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEditorItem.vue
549 lines (509 loc) · 12.6 KB
/
EditorItem.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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
<template>
<div class="editorItem" ref="editorItem">
<div class="editorContent">
<div class="editorContentHeader">
<div class="title" :class="[{ rotate: noSpace }, dir]">{{ title }}</div>
<div class="right">
<!-- 支持es6模块提示 -->
<el-popover placement="bottom" effect="dark" trigger="hover" v-if="supportESModuleMap[language]">
<template #reference>
<div class="addBtn">
<span class="el-icon-sunny"></span>
</div>
</template>
支持使用ES6模块语法,了解更多<a href='https://www.skypack.dev/' target='_blank'>skypack</a>
</el-popover>
<!-- 格式化按钮 -->
<el-tooltip
class="item"
effect="dark"
content="格式化"
placement="bottom"
v-if="formatterParserMap[language]"
>
<div class="addBtn" @click="codeFormatter">
<span class="el-icon-s-open"></span>
</div>
</el-tooltip>
<!-- 添加资源按钮 -->
<template v-if="showAllAddResourcesBtn">
<el-tooltip
class="item"
effect="dark"
content="添加css资源"
placement="bottom"
v-if="showAddBtn"
>
<div class="addBtn" @click="addResource('CSS')">
<span class="el-icon-plus"></span>
</div>
</el-tooltip>
<el-tooltip
class="item"
effect="dark"
content="添加js资源"
placement="bottom"
v-if="showAddBtn"
>
<div class="addBtn" @click="addResource('JS')">
<span class="el-icon-plus"></span>
</div>
</el-tooltip>
</template>
<template v-else>
<el-tooltip
class="item"
effect="dark"
content="添加资源"
placement="bottom"
v-if="showAddBtn"
>
<div class="addBtn" @click="addResource()">
<span class="el-icon-plus"></span>
</div>
</el-tooltip>
</template>
<!-- 选择语言 -->
<el-select
size="mini"
v-model="preprocessor"
@change="preprocessorChange"
>
<el-option
v-for="item in preprocessorList"
:key="item.value"
:label="item.label"
:value="item.value"
>
</el-option>
</el-select>
<!-- 更多 -->
<Dropdown style="margin-left: 10px;" :list="dropdownList" @click="onDropdownClick"></Dropdown>
</div>
</div>
<div class="editorContentBody" ref="editorEl"></div>
</div>
</div>
</template>
<script setup>
import {
ref,
defineProps,
onBeforeUnmount,
onMounted,
watch,
nextTick,
defineEmits,
} from 'vue'
import ResizeObserver from 'resize-observer-polyfill'
import { supportLanguage, formatterParserMap, langTypeMap, supportESModuleMap } from '@/config/constants'
import { ElTooltip, ElSelect, ElOption, ElPopover } from 'element-plus'
import * as monaco from 'monaco-editor/esm/vs/editor/editor.api'
import { wire } from '@/utils/monacoEditor'
import Dropdown from './Dropdown'
// 触发事件
const emit = defineEmits([
'preprocessor-change',
'code-change',
'blur',
'add-resource',
'space-change',
'create-code-img'
])
// props
const props = defineProps({
preprocessorList: {
type: Array,
default() {
return []
},
},
title: {
type: String,
default: '',
},
language: {
type: String,
default: '',
},
codeTheme: {
type: String,
default: '',
},
codeFontSize: {
type: Number,
default: 16,
},
content: {
type: String,
default: '',
},
showAddBtn: {
type: Boolean,
default: false,
},
dir: {
type: String,
default: '',
},
showAllAddResourcesBtn: {
type: Boolean,
default: false,
},
})
// hooks定义部分
// 创建编辑器
let editor = null // 编辑器实例
// 编辑器容器
const editorEl = ref(null)
const useCreateEditor = ({ props, emit, updateDoc }) => {
// 创建编辑器
const createEditor = async () => {
if (!editor) {
// 创建编辑器
editor = monaco.editor.create(editorEl.value, {
model: null,
minimap: {
enabled: false, // 关闭小地图
},
wordWrap: 'on', // 代码超出换行
theme: props.codeTheme || 'vs-dark', // 主题
fontSize: props.codeFontSize || 16,
fontFamily: 'MonoLisa, monospace',
contextmenu: false, // 不显示右键菜单
fixedOverflowWidgets: true, // 让语法提示层能溢出容器
})
// 设置文档内容
updateDoc(props.content, props.language)
// 支持textMate语法解析
wire(props.language, editor)
// 监听编辑事件
editor.onDidChangeModelContent(() => {
emit('code-change', editor.getValue())
})
// 监听失焦事件
editor.onDidBlurEditorText(() => {
emit('blur', editor.getValue())
})
}
// 更新字号
watch(
() => {
return props.codeFontSize
},
(val) => {
editor.updateOptions({
fontSize: val,
})
}
)
}
return {
createEditor,
}
}
// 选择预处理器
const usePreprocessor = ({ props, emit, updateDoc }) => {
// 预处理器
const preprocessor = ref(props.language)
// 修改预处理器
const preprocessorChange = (e) => {
emit('preprocessor-change', e)
}
// 更新语言
watch(
() => {
return props.language
},
() => {
preprocessor.value = props.language
updateDoc(props.content, props.language)
wire(props.language, editor)
}
)
return {
preprocessor,
preprocessorChange,
}
}
// 处理尺寸调整
const editorItem = ref(null)
const useSizeChange = ({ props }) => {
const noSpace = ref(false)
// 更新尺寸
let timer = null
const resize = () => {
// 100ms内只执行一次,优化卡顿问题
if (timer) {
return
}
timer = setTimeout(() => {
nextTick(() => {
if (!editorItem.value) {
return ;
}
let { width, height } = editorItem.value.getBoundingClientRect()
// 宽度小于100像素则旋转标题
noSpace.value = (props.dir === 'h' ? width : height) <= 100
emit('space-change', noSpace.value)
editor && editor.layout()
timer = null
})
}, 100)
}
// 监听dom大小变化
const ro = new ResizeObserver((entries) => {
for (const entry of entries) {
if (entry.target.classList.contains('dragItem')) {
resize()
}
}
})
// 挂载完成
onMounted(() => {
ro.observe(editorItem.value.parentNode)
})
// 即将解除挂载
onBeforeUnmount(() => {
ro.unobserve(editorItem.value.parentNode)
})
return {
noSpace,
}
}
// 处理文档内容
const useDoc = ({ props }) => {
// 更新编辑器文档模型
const updateDoc = (code, language) => {
if (!editor) {
return
}
let oldModel = editor.getModel()
let newModel = monaco.editor.createModel(code, supportLanguage[language])
editor.setModel(newModel)
if (oldModel) {
oldModel.dispose()
}
}
// 获取文档内容
const getValue = () => {
return editor.getValue()
}
// 更新文档内容
watch(
() => {
return props.content
},
() => {
updateDoc(props.content, props.language)
}
)
return {
updateDoc,
getValue,
}
}
// 处理资源
const useResource = ({ emit }) => {
// 点击添加资源
const addResource = (languageType) => {
emit('add-resource', languageType)
}
return {
addResource,
}
}
// 代码格式化
const useCodeFormat = ({ getValue, updateDoc, emit }) => {
// 代码格式化
const codeFormatter = () => {
let str = window.prettier.format(getValue(), {
parser: formatterParserMap[props.language],
plugins: window.prettierPlugins,
})
// 设置文档内容
updateDoc(str, props.language)
// 监听编辑事件
emit('code-change', str)
// editor.onDidChangeModel(() => {
// console.log('编辑')
// emit('code-change', editor.getValue())
// })
}
return {
codeFormatter,
}
}
// 初始化
const useInit = ({ createEditor }) => {
onMounted(() => {
createEditor()
})
}
// 生成代码图片
const useCreateCodeImg = () => {
const createCodeImg = () => {
emit('create-code-img', editor)
}
return {
createCodeImg
};
}
// 打开本地文件
const useOpenLocalFile = ({ updateDoc, emit }) => {
// 打开文件
const openLocalFile = () => {
let el = document.createElement('input')
el.type = 'file'
el.accept = langTypeMap[props.language] ? langTypeMap[props.language].join(',') : ''
el.addEventListener('input', (e) => {
let file = e.target.files[0]
let reader = new FileReader()
reader.readAsText(file)
reader.addEventListener('loadend', () => {
let str = reader.result
// 设置文档内容
updateDoc(str, props.language)
// 监听编辑事件
emit('code-change', str)
el.value = null
el = null
})
reader.addEventListener('error', () => {
el.value = null
el = null
})
})
el.click()
}
return {
openLocalFile,
}
}
// 下拉菜单
const useDropdown = ({ createCodeImg, openLocalFile }) => {
const dropdownList = [
{
name: '生成代码图片',
value: 'createCodeImg'
},
{
name: '打开本地文件',
value: 'openLocalFile'
}
]
const onDropdownClick = (item) => {
switch (item.value) {
case 'createCodeImg':
createCodeImg()
break;
case 'openLocalFile':
openLocalFile()
break;
default:
break;
}
}
return {
dropdownList,
onDropdownClick
}
}
// created部分
const { updateDoc, getValue } = useDoc({ props })
const { createEditor } = useCreateEditor({
props,
emit,
updateDoc,
})
const { preprocessor, preprocessorChange } = usePreprocessor({
props,
emit,
updateDoc,
})
const { noSpace } = useSizeChange({ props })
const { addResource } = useResource({ emit })
const { codeFormatter } = useCodeFormat({ getValue, updateDoc, emit })
useInit({ createEditor })
const { createCodeImg } = useCreateCodeImg({ props })
const { openLocalFile } = useOpenLocalFile({ props, updateDoc, emit })
const { dropdownList, onDropdownClick } = useDropdown({ createCodeImg, openLocalFile })
</script>
<style scoped lang="less">
.editorItem {
width: 100%;
height: 100%;
display: flex;
overflow: hidden;
background-color: var(--editor-background);
.editorContent {
width: 100%;
height: 100%;
display: flex;
flex-direction: column;
.editorContentHeader {
width: 100%;
height: 35px;
background: var(--editor-header-background);
border-bottom: 1px solid var(--editor-header-border-bottom-color);
display: flex;
justify-content: space-between;
align-items: center;
padding: 0 5px;
flex-grow: 0;
flex-shrink: 0;
padding-right: 20px;
.title {
font-weight: bold;
color: var(--editor-header-title-color);
font-size: 18px;
transition: all 0.2s;
&.rotate {
transform: rotate(90deg) translate(-2px, 6px);
transform-origin: left top;
font-size: 14px;
&.v {
transform: rotate(0deg) translateY(-28px);
}
}
}
/deep/ .el-input__inner {
width: 120px;
background-color: transparent;
border-color: var(--editor-header-color);
color: var(--editor-header-color);
}
/deep/ .el-select .el-input .el-select__caret {
color: var(--editor-header-color);
}
.right {
display: flex;
align-items: center;
.addBtn {
width: 30px;
height: 30px;
display: flex;
justify-content: center;
align-items: center;
margin-right: 10px;
color: var(--editor-header-color);
font-weight: bold;
cursor: pointer;
border-radius: 3px;
overflow: hidden;
transition: all 0.3s;
opacity: 0.7;
&:hover {
background-color: transparent;
opacity: 1;
}
}
}
}
.editorContentBody {
width: 100%;
height: 100%;
}
}
}
</style>