HtMonacoEditor.vue
6.74 KB
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
<template>
<div class="code-editor-container" :style="{height: height}">
<div v-if="showAll">
<span
:title="'全屏'"
class="code-editor-fullscreen-icon"
@click="fullVisible = true"
>
<i class="top-link-icon icon-quanpingbianji"></i>
</span>
</div>
<div class="my-editor" ref="container"></div>
<input
v-show="false"
:name="fieldName"
v-model="value"
v-validate="{required: required}"
/>
<span v-if="hasErrors" class="field-tail__wrap"
><small class="el-form-item__error"> 必填 </small></span
>
<ht-sidebar-dialog
width="100%"
height="100%"
:visible.sync="fullVisible"
class="code-editor"
append-to-body
>
<div class="code-editor-container code-editor-container-full">
<div>
<span
:title="'退出全屏'"
class="code-editor-fullscreen-icon"
style="right: 20px"
@click="fullVisible = false"
>
<i class="top-link-icon icon-restore"></i>
</span>
</div>
<div class="my-editor1" ref="containerFull"></div>
</div>
</ht-sidebar-dialog>
</div>
</template>
<script>
import * as monaco from 'monaco-editor'
export default {
name: 'codeEditor',
props: {
value: {
type: String,
default: ''
},
language: {
type: String,
default: 'javascript' // shell、sql、python, javascript , java , json
},
readOnly: {
type: Boolean,
default: false
},
required: {
type: Boolean,
default: false
},
height: {
type: String,
default: '100%'
},
fieldName: {
type: String,
default: '脚本编辑器'
},
showAll: {
type: Boolean,
default: true
},
contextmenu: {
//显示右键菜单
type: Boolean,
default: true
}
},
data() {
return {
fullVisible: false,
instanceMap: {}
}
},
computed: {
hasErrors: function() {
if (
this.required &&
!this.value &&
this.value !== 0 &&
this.errors.items &&
this.errors.items.filter(l => l.field == this.fieldName).length > 0
) {
return true
} else {
return false
}
}
},
watch: {
value: function(nVal, oVal) {
this.setValue(this.value)
},
fullVisible: function(nVal, oVal) {
this.$nextTick(() => {
this.init()
})
}
},
mounted() {
this.init()
},
created() {
this.$validator = this.$root.$validator
},
methods: {
init() {
if (!this.getInstance()) {
this.createInstance()
} else {
this.setValue(this.value)
}
},
resized() {
this.$nextTick(() => {
this.getInstance() && this.getInstance().layout()
})
},
getInstanceName() {
return this.fullVisible ? 'containerFull' : 'container'
},
getInstance() {
const containerName = this.getInstanceName()
if (!this.instanceMap[containerName]) {
return null
}
return this.instanceMap[containerName]
},
createInstance() {
let this_ = this
const containerName = this.getInstanceName()
monaco.editor.defineTheme('my-vs-theme',{
base:'vs',
inherit:true,
rules:[{background:'#f3f3f3'}],
colors:{
// 相关颜色属性配置
'editor.background':'#f3f3f3', //编辑器的背景色
// 'editor.foreground':'#ffffff',// 编辑器的前景色
// "editor.lineHighlightBackground': "#0e0FF2" // 当前行的背景色
'editor.selectionBackground': "#a6a6a6" // 选中文本的背景色
}
})
// 初始化编辑器实例
let monacoInstance = monaco.editor.create(this.$refs[containerName], {
value: this.value,
theme: 'my-vs-theme', // vs, hc-black, or vs-dark
autoIndex: true,
language: this.language,
readOnly: this.readOnly,
contextmenu: this.contextmenu,
})
if (this.language == 'json') {
setTimeout(() => {
monacoInstance.trigger('anyString', 'editor.action.formatDocument')
monacoInstance.setValue(monacoInstance.getValue())
}, 200);
}
//监测窗口变化
window.addEventListener('resize', function() {
this_.resized()
})
// 监听编辑器content变化事件
monacoInstance.onDidChangeModelContent(() => {
this.$emit('input', monacoInstance.getValue())
})
this.instanceMap[containerName] = monacoInstance
},
setValue(val) {
if (!this.getInstance()) {
return
}
const oldVal = this.getInstance().getValue()
// 要更新的值和当前编辑器一致,则不再执行更新动作。只有当其他地方修改了编辑器的model值时,才更新
if (val == oldVal) {
return
}
let position = this.getInstance().getPosition()
this.getInstance().setValue(val)
// 原来的编辑器没有值,则不计算新内容的坐标和鼠标聚焦。因为不知道新内容的行数
if (!oldVal || oldVal == ' ') {
return
}
position.column = position.column + val.length - oldVal.length
this.getInstance().setPosition(position)
},
insertValue(val) {
// manacoEditor 为 实例对象
// 1 获取光标位置
let position = this.getInstance().getPosition()
// 2 插入
this.getInstance().executeEdits('', [
{
range: new monaco.Range(
position.lineNumber,
position.column,
position.lineNumber,
position.column
),
text: val
}
])
// 3 设置新的光标位置
position.column = position.column + val.length
this.getInstance().setPosition(position)
// 4 聚焦
setTimeout(() => {
this.getInstance().focus()
}, 500)
}
}
}
</script>
<style lang="scss" scoped>
.code-editor-container-full {
height: 100%;
}
.code-editor-container {
width: 100%;
position: relative;
border: 1px solid #dcdcdb;
.my-editor,
.my-editor1 {
width: 100%;
height: 100%;
overflow: hidden;
}
.monaco-editor .scroll-decoration {
box-shadow: none;
// background: rgba(243,243,243,0.39);
}
}
.code-editor /deep/ .el-dialog__header {
display: none;
}
.code-editor /deep/ .el-dialog__body {
padding: 5px;
height: calc(100% - 10px);
}
.code-editor-fullscreen-icon {
border: 1px solid #dcdee0;
display: inline-block;
width: 30px;
height: 30px;
line-height: 30px;
text-align: center;
font-size: 20px;
color: #666666;
cursor: pointer;
position: absolute;
top: 10px;
right: 20px;
z-index: 9999;
}
</style>