forked from wanglin2/code-run
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConsole.vue
438 lines (391 loc) · 9.83 KB
/
Console.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
<template>
<div class="consoleBox">
<div class="header">
<div class="left">
<span class="btn el-icon-delete" @click="clear"></span>
<span class="errorCount" v-if="errorCount > 0"
><span class="icon el-icon-error"></span>{{ errorCount }}</span
>
</div>
<div class="right"></div>
</div>
<div class="logBox" ref="logBoxRef">
<div
class="logRow"
v-for="(log, index) in logList"
:key="index"
:class="[log.type]"
>
<template v-for="(logItem, itemIndex) in log.data" :key="itemIndex">
<!-- json对象 -->
<div
class="logItem json"
v-if="['object', 'array'].includes(logItem.contentType)"
v-html="logItem.content"
@click="jsonClick"
></div>
<!-- 字符串、数字 -->
<div
class="logItem message"
v-else
:class="[logItem.contentType]"
v-html="logItem.content"
></div>
</template>
</div>
</div>
<div class="commandBox">
<span class="icon el-icon-arrow-right"></span>
<textarea
class="textarea"
v-model="jsInput"
@keydown.enter="implementJs"
></textarea>
</div>
</div>
</template>
<script setup>
import {
ref,
onBeforeUnmount,
getCurrentInstance,
nextTick,
computed,
} from "vue";
// hooks定义部分
// 初始化
const useInit = () => {
const { proxy } = getCurrentInstance();
return {
proxy,
};
};
// 日志处理
const logBoxRef = ref(null); // 日志容器dom
const useLog = ({ proxy }) => {
const logList = ref([]); // 日志信息列表
// 错误信息数量
const errorCount = computed(() => {
return logList.value.filter((item) => {
return item.type === "error";
}).length;
});
// 清空控制台日志
const clear = () => {
logList.value = [];
};
proxy.$eventEmitter.on("clear_logs", clear);
// 接收打印信息
const onMessage = ({ data = {} }) => {
if (data.type === "console") {
if (data.method === "clear") {
clear();
} else {
logList.value.push({
type: data.method,
data: data.data,
});
nextTick(() => {
logBoxRef.value.scrollTop = logBoxRef.value.scrollHeight;
});
}
}
};
// 监听iframe信息
window.addEventListener("message", onMessage);
onBeforeUnmount(() => {
window.removeEventListener("message", onMessage);
proxy.$eventEmitter.off("clear_logs", clear);
});
return {
logList,
errorCount,
clear,
};
};
// 动态执行js
const useImplementJs = ({ proxy }) => {
const jsInput = ref("");
// 动态执行js
const implementJs = (e) => {
if (e.shiftKey) {
return;
}
e.preventDefault();
if (jsInput.value.trim()) {
proxy.$eventEmitter.emit("dynamic_js_command", jsInput.value.trim());
jsInput.value = "";
}
};
return {
jsInput,
implementJs,
};
};
// json数据格式化
const useJSONFormat = () => {
// 获取指定类名的第一个子节点
const getChildByClassName = (el, className) => {
let children = el.children;
for (let i = 0; i < children.length; i++) {
if (children[i].classList.contains(className)) {
return children[i];
}
}
return null;
};
// json数据展开收缩
let expandIndex = 0;
const jsonClick = (e) => {
// 点击是展开收缩按钮
if (e.target && e.target.classList.contains("expandBtn")) {
let target = e.target;
let parent = target.parentNode;
// id,每个展开收缩按钮唯一的标志
let index = target.getAttribute("data-index");
if (index === null) {
index = expandIndex++;
target.setAttribute("data-index", index);
}
// 获取当前状态,0表示收缩、1表示展开
let status = target.getAttribute("expand-status") || "1";
// 在子节点里找到wrap元素
let wrapEl = getChildByClassName(parent, "wrap");
// 找到下层所有的按钮节点
let btnEls = wrapEl.querySelectorAll(".expandBtn");
// 收缩状态 -> 展开状态
if (status === "0") {
// 设置状态为展开
target.setAttribute("expand-status", "1");
// 展开
wrapEl.style.height = "auto";
// 按钮箭头旋转
target.classList.remove("shrink");
// 移除省略号元素
let ellipsisEl = getChildByClassName(parent, "ellipsis");
parent.removeChild(ellipsisEl);
// 显示下级展开收缩按钮
for (let i = 0; i < btnEls.length; i++) {
let _index = btnEls[i].getAttribute("data-for-index");
// 只有被当前按钮收缩的按钮才显示
if (_index === index) {
btnEls[i].removeAttribute("data-for-index");
btnEls[i].style.display = "inline-block";
}
}
} else if (status === "1") {
// 展开状态 -> 收缩状态
target.setAttribute("expand-status", "0");
wrapEl.style.height = 0;
target.classList.add("shrink");
let ellipsisEl = document.createElement("div");
ellipsisEl.textContent = "...";
ellipsisEl.className = "ellipsis";
parent.insertBefore(ellipsisEl, wrapEl);
for (let i = 0; i < btnEls.length; i++) {
let _index = btnEls[i].getAttribute("data-for-index");
// 只隐藏当前可以被隐藏的按钮
if (_index === null) {
btnEls[i].setAttribute("data-for-index", index);
btnEls[i].style.display = "none";
}
}
}
}
};
return {
jsonClick,
};
};
// created部分
const { proxy } = useInit();
const { logList, errorCount, clear } = useLog({ proxy });
const { jsInput, implementJs } = useImplementJs({ proxy });
const { jsonClick } = useJSONFormat();
</script>
<style scoped lang="less">
.consoleBox {
position: relative;
width: 100%;
height: 100%;
background-color: var(--console-background);
overflow: hidden;
.header {
position: absolute;
left: 0;
top: 0;
width: 100%;
height: 30px;
background-color: var(--touch-bar-background);
color: var(--editor-header-title-color);
font-size: 12px;
padding: 0 5px;
display: flex;
justify-content: space-between;
align-items: center;
z-index: 99;
.left {
display: flex;
align-items: center;
}
.btn {
border: 3px solid transparent;
color: var(--command-color);
cursor: pointer;
font-size: 12px;
padding: 0 5px;
opacity: 0.7;
&:active {
transform: translateY(1px);
}
&:hover {
opacity: 1;
}
}
.errorCount {
margin-left: 5px;
color: #f56c6c;
.icon {
color: #f56c6c;
margin-right: 3px;
}
}
}
.logBox {
position: absolute;
left: 0;
top: 30px;
bottom: 30px;
width: 100%;
overflow: auto;
&::-webkit-scrollbar {
width: 10px;
background: #202123;
border-radius: 4px;
}
&::-webkit-scrollbar-track {
background: rgba(255, 255, 255, 0.1);
border-radius: 2px;
}
&::-webkit-scrollbar-thumb {
border: 1px solid rgba(255, 255, 255, 0.16);
background-color: rgba(0, 0, 0, 0.64);
border-radius: 4px;
}
&::-webkit-scrollbar-thumb:hover {
}
&::-webkit-scrollbar-corner {
}
.logRow {
border-bottom: 1px solid #5a5f73;
padding: 10px 10px 5px;
margin-bottom: 0;
display: flex;
&.error {
background: rgba(255, 0, 0, 0.2);
}
&.warn {
background: rgba(235, 255, 0, 0.15);
}
.logItem {
display: inline-block;
margin-right: 10px;
&.message {
color: #fff;
white-space: pre-wrap;
&.string {
color: #ce9178;
}
&.number {
color: #b5cea8;
}
&.boolean,
&.null,
&.undefined {
color: #569cd6;
}
&.symbol {
color: #3dc9b0;
}
}
&.json {
line-height: 22px;
color: #999999;
padding-left: 20px;
position: relative;
/deep/ .expandBtn {
position: absolute;
left: 0;
cursor: pointer;
transform: rotateZ(90deg);
transition: all 0.3s;
line-height: 22px;
&.shrink {
transform: rotateZ(0deg);
}
}
/deep/ .wrap {
overflow: hidden;
}
/deep/ .object,
/deep/ .array {
margin-left: 20px;
}
/deep/ .bracket {
color: #999999;
}
/deep/ .key {
color: #e06c75;
}
/deep/ .colon {
color: #999999;
margin: 0 5px;
}
/deep/ .string,
/deep/ .symbol {
color: #98c379;
}
/deep/ .number {
color: #d19a66;
}
/deep/ .boolean,
/deep/ .null,
/deep/ .undefined {
color: #56b6c2;
}
}
}
}
}
.commandBox {
position: absolute;
left: 0;
bottom: 0;
width: 100%;
height: 30px;
background: var(--command-background);
padding: 0 8px 0 0;
display: flex;
.icon {
width: 30px;
height: 30px;
display: flex;
justify-content: center;
align-items: center;
color: var(--command-color);
flex-grow: 0;
flex-shrink: 0;
}
.textarea {
width: 100%;
height: 100%;
border: none;
background-color: transparent;
outline: none;
color: var(--command-color);
padding: 7px 0 7px 7px;
resize: none;
}
}
}
</style>