1
1
<template >
2
2
<Dialog
3
- : title =" modelTitle "
3
+ title =" 代码预览 "
4
4
v-model =" modelVisible"
5
5
align-center
6
- width =" 60 %"
6
+ width =" 80 %"
7
7
class =" app-infra-codegen-preview-container"
8
8
>
9
9
<div class =" flex" >
10
- <el-card class =" w-1/4" :gutter =" 12" shadow =" hover" >
10
+ <!-- 代码目录树 -->
11
+ <el-card
12
+ class =" w-1/3"
13
+ :gutter =" 12"
14
+ shadow =" hover"
15
+ v-loading =" loading"
16
+ element-loading-text =" 生成文件目录中..."
17
+ >
11
18
<el-scrollbar height =" calc(100vh - 88px - 40px - 50px)" >
12
19
<el-tree
13
20
ref =" treeRef"
20
27
/>
21
28
</el-scrollbar >
22
29
</el-card >
23
- <el-card class =" w-3/4 ml-3" :gutter =" 12" shadow =" hover" >
30
+ <!-- 代码 -->
31
+ <el-card
32
+ class =" w-2/3 ml-3"
33
+ :gutter =" 12"
34
+ shadow =" hover"
35
+ v-loading =" loading"
36
+ element-loading-text =" 加载代码中..."
37
+ >
24
38
<el-tabs v-model =" preview.activeName" >
25
39
<el-tab-pane
26
40
v-for =" item in previewCodegen"
31
45
<el-button text type =" primary" class =" float-right" @click =" copy(item.code)" >
32
46
{{ t('common.copy') }}
33
47
</el-button >
34
- <pre >{{ item.code }}</pre >
48
+ <div v-highlight >
49
+ <code >{{ item.code }}</code >
50
+ </div >
35
51
</el-tab-pane >
36
52
</el-tabs >
37
53
</el-card >
42
58
import { useClipboard } from ' @vueuse/core'
43
59
import { handleTree2 } from ' @/utils/tree'
44
60
import * as CodegenApi from ' @/api/infra/codegen'
45
- import { CodegenPreviewVO } from ' @/api/infra/codegen/types'
46
-
47
61
const { t } = useI18n () // 国际化
48
62
const message = useMessage () // 消息弹窗
49
63
50
64
const modelVisible = ref (false ) // 弹窗的是否展示
51
- const modelTitle = ref (' 代码预览' ) // 弹窗的标题
52
- // ======== 显示页面 ========
65
+ const loading = ref (false ) // 加载中的状态
53
66
const preview = reactive ({
54
- fileTree: [],
55
- activeName: ' '
67
+ fileTree: [], // 文件树
68
+ activeName: ' ' // 激活的文件名
56
69
})
57
- const previewCodegen = ref <CodegenPreviewVO []>()
70
+ const previewCodegen = ref <CodegenApi . CodegenPreviewVO []>()
58
71
72
+ /** 点击文件 */
59
73
const handleNodeClick = async (data , node ) => {
60
74
if (node && ! node .isLeaf ) {
61
75
return false
62
76
}
63
77
preview .activeName = data .id
64
78
}
79
+
65
80
/** 生成 files 目录 **/
66
81
interface filesType {
67
82
id: string
68
83
label: string
69
84
parentId: string
70
85
}
71
- const handleFiles = (datas : CodegenPreviewVO []) => {
86
+
87
+ /** 打开弹窗 */
88
+ const open = async (id : number ) => {
89
+ modelVisible .value = true
90
+ try {
91
+ loading .value = true
92
+ // 生成代码
93
+ const data = await CodegenApi .previewCodegen (id )
94
+ previewCodegen .value = data
95
+ // 处理文件
96
+ let file = handleFiles (data )
97
+ preview .fileTree = handleTree2 (file , ' id' , ' parentId' , ' children' , ' /' )
98
+ // 点击首个文件
99
+ preview .activeName = data [0 ].filePath
100
+ } finally {
101
+ loading .value = false
102
+ }
103
+ }
104
+ defineExpose ({ open }) // 提供 open 方法,用于打开弹窗
105
+
106
+ /** 处理文件 */
107
+ const handleFiles = (datas : CodegenApi .CodegenPreviewVO []) => {
72
108
let exists = {} // key:file 的 id;value:true
73
109
let files: filesType [] = []
74
110
// 遍历每个元素
@@ -130,6 +166,7 @@ const handleFiles = (datas: CodegenPreviewVO[]) => {
130
166
}
131
167
return files
132
168
}
169
+
133
170
/** 复制 **/
134
171
const copy = async (text : string ) => {
135
172
const { copy, copied, isSupported } = useClipboard ({ source: text })
@@ -142,17 +179,6 @@ const copy = async (text: string) => {
142
179
message .success (t (' common.copySuccess' ))
143
180
}
144
181
}
145
-
146
- /** 打开弹窗 */
147
- const openModal = async (id : number ) => {
148
- modelVisible .value = true
149
- const res = await CodegenApi .previewCodegen (id )
150
- let file = handleFiles (res )
151
- previewCodegen .value = res
152
- preview .fileTree = handleTree2 (file , ' id' , ' parentId' , ' children' , ' /' )
153
- preview .activeName = res [0 ].filePath
154
- }
155
- defineExpose ({ openModal }) // 提供 openModal 方法,用于打开弹窗
156
182
</script >
157
183
<style lang="scss">
158
184
.app-infra-codegen-preview-container {
0 commit comments