REVIEW 代码预览(PreviewCode)

This commit is contained in:
YunaiV 2023-03-29 22:25:53 +08:00
parent b1e74a1d05
commit e6dac1ebda
3 changed files with 57 additions and 31 deletions

View File

@ -276,7 +276,7 @@ export const handleTree = (data: any[], id?: string, parentId?: string, children
export const handleTree2 = (data, id, parentId, children, rootId) => { export const handleTree2 = (data, id, parentId, children, rootId) => {
id = id || 'id' id = id || 'id'
parentId = parentId || 'parentId' parentId = parentId || 'parentId'
children = children || 'children' // children = children || 'children'
rootId = rootId =
rootId || rootId ||
Math.min( Math.min(

View File

@ -1,13 +1,20 @@
<template> <template>
<Dialog <Dialog
:title="modelTitle" title="代码预览"
v-model="modelVisible" v-model="modelVisible"
align-center align-center
width="60%" width="80%"
class="app-infra-codegen-preview-container" class="app-infra-codegen-preview-container"
> >
<div class="flex"> <div class="flex">
<el-card class="w-1/4" :gutter="12" shadow="hover"> <!-- 代码目录树 -->
<el-card
class="w-1/3"
:gutter="12"
shadow="hover"
v-loading="loading"
element-loading-text="生成文件目录中..."
>
<el-scrollbar height="calc(100vh - 88px - 40px - 50px)"> <el-scrollbar height="calc(100vh - 88px - 40px - 50px)">
<el-tree <el-tree
ref="treeRef" ref="treeRef"
@ -20,7 +27,14 @@
/> />
</el-scrollbar> </el-scrollbar>
</el-card> </el-card>
<el-card class="w-3/4 ml-3" :gutter="12" shadow="hover"> <!-- 代码 -->
<el-card
class="w-2/3 ml-3"
:gutter="12"
shadow="hover"
v-loading="loading"
element-loading-text="加载代码中..."
>
<el-tabs v-model="preview.activeName"> <el-tabs v-model="preview.activeName">
<el-tab-pane <el-tab-pane
v-for="item in previewCodegen" v-for="item in previewCodegen"
@ -31,7 +45,9 @@
<el-button text type="primary" class="float-right" @click="copy(item.code)"> <el-button text type="primary" class="float-right" @click="copy(item.code)">
{{ t('common.copy') }} {{ t('common.copy') }}
</el-button> </el-button>
<pre>{{ item.code }}</pre> <div v-highlight>
<code>{{ item.code }}</code>
</div>
</el-tab-pane> </el-tab-pane>
</el-tabs> </el-tabs>
</el-card> </el-card>
@ -42,33 +58,53 @@
import { useClipboard } from '@vueuse/core' import { useClipboard } from '@vueuse/core'
import { handleTree2 } from '@/utils/tree' import { handleTree2 } from '@/utils/tree'
import * as CodegenApi from '@/api/infra/codegen' import * as CodegenApi from '@/api/infra/codegen'
import { CodegenPreviewVO } from '@/api/infra/codegen/types'
const { t } = useI18n() // const { t } = useI18n() //
const message = useMessage() // const message = useMessage() //
const modelVisible = ref(false) // const modelVisible = ref(false) //
const modelTitle = ref('代码预览') // const loading = ref(false) //
// ======== ========
const preview = reactive({ const preview = reactive({
fileTree: [], fileTree: [], //
activeName: '' activeName: '' //
}) })
const previewCodegen = ref<CodegenPreviewVO[]>() const previewCodegen = ref<CodegenApi.CodegenPreviewVO[]>()
/** 点击文件 */
const handleNodeClick = async (data, node) => { const handleNodeClick = async (data, node) => {
if (node && !node.isLeaf) { if (node && !node.isLeaf) {
return false return false
} }
preview.activeName = data.id preview.activeName = data.id
} }
/** 生成 files 目录 **/ /** 生成 files 目录 **/
interface filesType { interface filesType {
id: string id: string
label: string label: string
parentId: string parentId: string
} }
const handleFiles = (datas: CodegenPreviewVO[]) => {
/** 打开弹窗 */
const open = async (id: number) => {
modelVisible.value = true
try {
loading.value = true
//
const data = await CodegenApi.previewCodegen(id)
previewCodegen.value = data
//
let file = handleFiles(data)
preview.fileTree = handleTree2(file, 'id', 'parentId', 'children', '/')
//
preview.activeName = data[0].filePath
} finally {
loading.value = false
}
}
defineExpose({ open }) // open
/** 处理文件 */
const handleFiles = (datas: CodegenApi.CodegenPreviewVO[]) => {
let exists = {} // keyfile idvaluetrue let exists = {} // keyfile idvaluetrue
let files: filesType[] = [] let files: filesType[] = []
// //
@ -130,6 +166,7 @@ const handleFiles = (datas: CodegenPreviewVO[]) => {
} }
return files return files
} }
/** 复制 **/ /** 复制 **/
const copy = async (text: string) => { const copy = async (text: string) => {
const { copy, copied, isSupported } = useClipboard({ source: text }) const { copy, copied, isSupported } = useClipboard({ source: text })
@ -142,17 +179,6 @@ const copy = async (text: string) => {
message.success(t('common.copySuccess')) message.success(t('common.copySuccess'))
} }
} }
/** 打开弹窗 */
const openModal = async (id: number) => {
modelVisible.value = true
const res = await CodegenApi.previewCodegen(id)
let file = handleFiles(res)
previewCodegen.value = res
preview.fileTree = handleTree2(file, 'id', 'parentId', 'children', '/')
preview.activeName = res[0].filePath
}
defineExpose({ openModal }) // openModal
</script> </script>
<style lang="scss"> <style lang="scss">
.app-infra-codegen-preview-container { .app-infra-codegen-preview-container {

View File

@ -187,7 +187,7 @@ const resetQuery = () => {
handleQuery() handleQuery()
} }
// /** 导入操作 */
const importRef = ref() const importRef = ref()
const openImportTable = () => { const openImportTable = () => {
importRef.value.open() importRef.value.open()
@ -201,7 +201,7 @@ const handleUpdate = (id: number) => {
/** 预览操作 */ /** 预览操作 */
const previewRef = ref() const previewRef = ref()
const handlePreview = (row: CodegenApi.CodegenTableVO) => { const handlePreview = (row: CodegenApi.CodegenTableVO) => {
previewRef.value.openModal(row.id) previewRef.value.open(row.id)
} }
/** 删除按钮操作 */ /** 删除按钮操作 */