Skip to content

Commit 02449bf

Browse files
committed
feat: 新增查看json的功能
1 parent 411eba0 commit 02449bf

File tree

4 files changed

+139
-31
lines changed

4 files changed

+139
-31
lines changed

src/styles/mixin.scss

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
@mixin action-bar {
2+
.action-bar {
3+
height: 33px;
4+
background: #f2fafb;
5+
padding: 0 15px;
6+
box-sizing: border-box;
7+
8+
.bar-btn {
9+
display: inline-block;
10+
padding: 0 6px;
11+
line-height: 32px;
12+
color: #8285f5;
13+
cursor: pointer;
14+
font-size: 14px;
15+
user-select: none;
16+
& i {
17+
font-size: 20px;
18+
}
19+
&:hover {
20+
color: #4348d4;
21+
}
22+
}
23+
.bar-btn + .bar-btn {
24+
margin-left: 8px;
25+
}
26+
.delete-btn {
27+
color: #f56c6c;
28+
&:hover {
29+
color: #ea0b30;
30+
}
31+
}
32+
}
33+
}

src/views/index/FormDrawer.vue

Lines changed: 6 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ export default {
125125
created() {},
126126
mounted() {
127127
window.addEventListener('keydown', this.preventDefaultSave)
128-
const a = new ClipboardJS('.copy-btn', {
128+
const clipboard = new ClipboardJS('.copy-btn', {
129129
text: trigger => {
130130
const codeStr = this.generateCode()
131131
this.$notify({
@@ -136,6 +136,9 @@ export default {
136136
return codeStr
137137
}
138138
})
139+
clipboard.on('error', e => {
140+
this.$message.error('代码复制失败')
141+
})
139142
},
140143
beforeDestroy() {
141144
window.removeEventListener('keydown', this.preventDefaultSave)
@@ -268,6 +271,7 @@ export default {
268271
</script>
269272

270273
<style lang="scss" scoped>
274+
@import '@/styles/mixin.scss';
271275
.tab-editor {
272276
position: absolute;
273277
top: 33px;
@@ -293,44 +297,15 @@ export default {
293297
}
294298
.right-preview {
295299
height: 100%;
296-
.action-bar {
297-
height: 33px;
298-
background: #f2fafb;
299-
padding: 0 15px;
300-
box-sizing: border-box;
301-
}
302300
.result-wrapper {
303301
height: calc(100vh - 33px);
304302
width: 100%;
305303
overflow: auto;
306304
padding: 12px;
307305
box-sizing: border-box;
308306
}
309-
.bar-btn {
310-
display: inline-block;
311-
padding: 0 6px;
312-
line-height: 32px;
313-
color: #8285f5;
314-
cursor: pointer;
315-
font-size: 14px;
316-
user-select: none;
317-
& i {
318-
font-size: 20px;
319-
}
320-
&:hover {
321-
color: #4348d4;
322-
}
323-
}
324-
.bar-btn + .bar-btn {
325-
margin-left: 8px;
326-
}
327-
.delete-btn {
328-
color: #f56c6c;
329-
&:hover {
330-
color: #ea0b30;
331-
}
332-
}
333307
}
308+
@include action-bar;
334309
::v-deep .el-drawer__header {
335310
display: none;
336311
}

src/views/index/Home.vue

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@
4747
<el-button icon="el-icon-video-play" type="text" @click="run">
4848
运行
4949
</el-button>
50+
<el-button icon="el-icon-view" type="text" @click="showJson">
51+
查看json
52+
</el-button>
5053
<el-button icon="el-icon-download" type="text" @click="download">
5154
导出vue文件
5255
</el-button>
@@ -100,6 +103,11 @@
100103
size="100%"
101104
:generate-conf="generateConf"
102105
/>
106+
<json-drawer
107+
size="60%"
108+
:visible.sync="jsonDrawerVisible"
109+
:json-str="JSON.stringify(formData)"
110+
/>
103111
<code-type-dialog
104112
:visible.sync="dialogVisible"
105113
title="选择生成类型"
@@ -118,6 +126,7 @@ import beautifier from 'beautifier'
118126
import ClipboardJS from 'clipboard'
119127
import render from '@/components/render'
120128
import FormDrawer from './FormDrawer'
129+
import JsonDrawer from './JsonDrawer'
121130
import RightPanel from './RightPanel'
122131
import {
123132
inputComponents, selectComponents, layoutComponents, formConf
@@ -150,6 +159,7 @@ export default {
150159
draggable,
151160
render,
152161
FormDrawer,
162+
JsonDrawer,
153163
RightPanel,
154164
CodeTypeDialog,
155165
DraggableItem
@@ -169,6 +179,7 @@ export default {
169179
drawerVisible: false,
170180
formData: {},
171181
dialogVisible: false,
182+
jsonDrawerVisible: false,
172183
generateConf: null,
173184
showFileName: false,
174185
activeData: drawingDefalut[0],
@@ -351,6 +362,10 @@ export default {
351362
const css = cssStyle(makeUpCss(this.formData))
352363
return beautifier.html(html + script + css, beautifierConf.html)
353364
},
365+
showJson() {
366+
this.AssembleFormData()
367+
this.jsonDrawerVisible = true
368+
},
354369
download() {
355370
this.dialogVisible = true
356371
this.showFileName = true

src/views/index/JsonDrawer.vue

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<template>
2+
<div>
3+
<el-drawer v-bind="$attrs" v-on="$listeners" @opened="onOpen" @close="onClose">
4+
<div class="action-bar" :style="{'text-align': 'left'}">
5+
<span ref="copyBtn" class="bar-btn copy-json-btn">
6+
<i class="el-icon-document-copy" />
7+
复制JSON
8+
</span>
9+
<span class="bar-btn delete-btn" @click="$emit('update:visible', false)">
10+
<i class="el-icon-circle-close" />
11+
关闭
12+
</span>
13+
</div>
14+
<div id="editorJson" :style="{height: '100%'}" />
15+
</el-drawer>
16+
</div>
17+
</template>
18+
19+
<script>
20+
import monaco from 'monaco'
21+
import beautifier from 'beautifier'
22+
import { beautifierConf } from '@/utils/index'
23+
import ClipboardJS from 'clipboard'
24+
25+
export default {
26+
components: {},
27+
props: {
28+
jsonStr: {
29+
type: String,
30+
required: true,
31+
beautifier: null,
32+
jsonEditor: null
33+
}
34+
},
35+
data() {
36+
return {}
37+
},
38+
computed: {},
39+
watch: {},
40+
created() {},
41+
mounted() {
42+
const clipboard = new ClipboardJS('.copy-json-btn', {
43+
text: trigger => {
44+
this.$notify({
45+
title: '成功',
46+
message: '代码已复制到剪切板,可粘贴。',
47+
type: 'success'
48+
})
49+
return this.beautifierJson
50+
}
51+
})
52+
clipboard.on('error', e => {
53+
this.$message.error('代码复制失败')
54+
})
55+
},
56+
methods: {
57+
onOpen() {
58+
this.beautifierJson = beautifier.js(this.jsonStr, beautifierConf.js)
59+
this.setEditorValue('editorJson', this.beautifierJson)
60+
},
61+
onClose() {},
62+
setEditorValue(id, codeStr) {
63+
if (this.jsonEditor) {
64+
this.jsonEditor.setValue(codeStr)
65+
} else {
66+
this.jsonEditor = monaco.editor.create(document.getElementById(id), {
67+
value: codeStr,
68+
theme: 'vs-dark',
69+
language: 'json',
70+
automaticLayout: true
71+
})
72+
}
73+
}
74+
}
75+
}
76+
</script>
77+
78+
<style lang="scss" scoped>
79+
@import '@/styles/mixin.scss';
80+
81+
::v-deep .el-drawer__header {
82+
display: none;
83+
}
84+
@include action-bar;
85+
</style>

0 commit comments

Comments
 (0)