refactor: 代码生成预览优化

pull/78/head
puhui999 2025-04-22 16:10:42 +08:00
parent 86fe4ff96a
commit ffe235433d
3 changed files with 33 additions and 41 deletions

View File

@ -3,7 +3,6 @@ import type { OnActionClickFn, VxeTableGridOptions } from '#/adapter/vxe-table';
import type { InfraCodegenApi } from '#/api/infra/codegen'; import type { InfraCodegenApi } from '#/api/infra/codegen';
import type { SystemMenuApi } from '#/api/system/menu'; import type { SystemMenuApi } from '#/api/system/menu';
import type { Recordable } from '@vben/types'; import type { Recordable } from '@vben/types';
import type { ComputedRef } from 'vue';
import { IconifyIcon } from '@vben/icons'; import { IconifyIcon } from '@vben/icons';
@ -388,18 +387,17 @@ export function useGridFormSchema(): VbenFormSchema[] {
]; ];
} }
const dataSourceConfigList = await getDataSourceConfigList();
/** 列表的字段 */ /** 列表的字段 */
// TODO @puhui999getDataSourceConfigName要不改成 data.ts 加载 list然后使用。
export function useGridColumns<T = InfraCodegenApi.CodegenTable>( export function useGridColumns<T = InfraCodegenApi.CodegenTable>(
onActionClick: OnActionClickFn<T>, onActionClick: OnActionClickFn<T>,
getDataSourceConfigName: ComputedRef<(cellValue: number) => string>,
): VxeTableGridOptions['columns'] { ): VxeTableGridOptions['columns'] {
return [ return [
{ {
field: 'dataSourceConfigId', field: 'dataSourceConfigId',
title: '数据源', title: '数据源',
minWidth: 120, minWidth: 120,
formatter: ({ cellValue }) => getDataSourceConfigName.value(cellValue), formatter: ({ cellValue }) => dataSourceConfigList.find((item) => item.id === cellValue)?.name || '',
}, },
{ {
field: 'tableName', field: 'tableName',

View File

@ -11,6 +11,7 @@ import { getCodegenTable, updateCodegenTable } from '#/api/infra/codegen';
import { $t } from '#/locales'; import { $t } from '#/locales';
import { ref, unref } from 'vue'; import { ref, unref } from 'vue';
import { useTabs } from '@vben/hooks';
import { useRoute, useRouter } from 'vue-router'; import { useRoute, useRouter } from 'vue-router';
const route = useRoute(); const route = useRoute();
@ -72,7 +73,6 @@ const submitForm = async () => {
content: $t('ui.actionMessage.operationSuccess'), content: $t('ui.actionMessage.operationSuccess'),
key: 'action_process_msg', key: 'action_process_msg',
}); });
// TODO @puhui999 tab
close(); close();
} catch (error) { } catch (error) {
console.error('保存失败', error); console.error('保存失败', error);
@ -80,9 +80,10 @@ const submitForm = async () => {
hideLoading(); hideLoading();
} }
}; };
const tabs = useTabs();
/** 返回列表 */ /** 返回列表 */
const close = () => { const close = () => {
tabs.closeCurrentTab();
router.push('/infra/codegen'); router.push('/infra/codegen');
}; };
@ -137,9 +138,7 @@ getDetail();
<div class="mt-4 flex justify-end space-x-2"> <div class="mt-4 flex justify-end space-x-2">
<Button v-show="currentStep > 0" @click="prevStep"></Button> <Button v-show="currentStep > 0" @click="prevStep"></Button>
<Button v-show="currentStep < steps.length - 1" @click="nextStep"></Button> <Button v-show="currentStep < steps.length - 1" @click="nextStep"></Button>
<Button type="primary" :loading="loading" @click="submitForm"> <Button type="primary" :loading="loading" @click="submitForm"> </Button>
保存
</Button>
</div> </div>
</div> </div>
</Page> </Page>

View File

@ -1,9 +1,4 @@
<script lang="ts" setup> <script lang="ts" setup>
// TODO @puhui999bug
// TODO @puhui999
// TODO @puhui999
// TODO @puhui999
// TODO @vben2.0 CodeEditor // TODO @vben2.0 CodeEditor
import type { InfraCodegenApi } from '#/api/infra/codegen'; import type { InfraCodegenApi } from '#/api/infra/codegen';
@ -47,27 +42,30 @@ const previewFiles = ref<InfraCodegenApi.CodegenPreview[]>([]);
const activeKey = ref<string>(''); const activeKey = ref<string>('');
/** 代码地图 */ /** 代码地图 */
const codeMap = new Map<string, string>(); const codeMap = ref<Map<string, string>>(new Map<string, string>());
const setCodeMode = (key: string, lang: string, code: string) => { const setCodeMap = (key: string, lang: string, code: string) => {
// Java // Java
const trimmedCode = code.trimStart(); const trimmedCode = code.trimStart();
//
if (codeMap.value.has(key)) {
return;
}
try { try {
const highlightedCode = hljs.highlight(trimmedCode, { const highlightedCode = hljs.highlight(trimmedCode, {
language: lang, language: lang,
}).value; }).value;
codeMap.set(key, highlightedCode); codeMap.value.set(key, highlightedCode);
} catch { } catch {
codeMap.set(key, trimmedCode); codeMap.value.set(key, trimmedCode);
} }
}; };
const removeCodeMapKey = (targetKey: any) => { const removeCodeMapKey = (targetKey: any) => {
// //
if (codeMap.size === 1) { if (codeMap.value.size === 1) {
return; return;
} }
if (codeMap.has(targetKey)) { if (codeMap.value.has(targetKey)) {
codeMap.delete(targetKey); codeMap.value.delete(targetKey);
} }
}; };
@ -98,7 +96,7 @@ const handleNodeClick = (_: any[], e: any) => {
if (!file) return; if (!file) return;
const lang = file.filePath.split('.').pop() || ''; const lang = file.filePath.split('.').pop() || '';
setCodeMode(activeKey.value, lang, file.code); setCodeMap(activeKey.value, lang, file.code);
}; };
/** 处理文件树 */ /** 处理文件树 */
@ -179,11 +177,11 @@ const handleFiles = (data: InfraCodegenApi.CodegenPreview[]): FileNode[] => {
/** 模态框实例 */ /** 模态框实例 */
const [Modal, modalApi] = useVbenModal({ const [Modal, modalApi] = useVbenModal({
footer: false, footer: false,
class: 'w-3/5', fullscreen: true,
async onOpenChange(isOpen: boolean) { async onOpenChange(isOpen: boolean) {
if (!isOpen) { if (!isOpen) {
// //
codeMap.clear(); codeMap.value.clear();
return; return;
} }
@ -202,7 +200,7 @@ const [Modal, modalApi] = useVbenModal({
activeKey.value = data[0]?.filePath || ''; activeKey.value = data[0]?.filePath || '';
const lang = activeKey.value.split('.').pop() || ''; const lang = activeKey.value.split('.').pop() || '';
const code = data[0]?.code || ''; const code = data[0]?.code || '';
setCodeMode(activeKey.value, lang, code); setCodeMap(activeKey.value, lang, code);
} }
} finally { } finally {
loading.value = false; loading.value = false;
@ -215,18 +213,22 @@ const [Modal, modalApi] = useVbenModal({
<Modal title="代码预览"> <Modal title="代码预览">
<div class="flex h-full" v-loading="loading"> <div class="flex h-full" v-loading="loading">
<!-- 文件树 --> <!-- 文件树 -->
<div class="w-1/3 border-r border-gray-200 pr-4 dark:border-gray-700"> <div class="h-full w-1/3 overflow-auto border-r border-gray-200 pr-4 dark:border-gray-700">
<DirectoryTree v-model:active-key="activeKey" @select="handleNodeClick" :tree-data="fileTree" /> <DirectoryTree
v-if="fileTree.length > 0"
default-expand-all
v-model:active-key="activeKey"
@select="handleNodeClick"
:tree-data="fileTree"
/>
</div> </div>
<!-- 代码预览 --> <!-- 代码预览 -->
<div class="w-2/3 pl-4"> <div class="h-full w-2/3 overflow-auto pl-4">
<Tabs v-model:active-key="activeKey" hide-add type="editable-card" @edit="removeCodeMapKey"> <Tabs v-model:active-key="activeKey" hide-add type="editable-card" @edit="removeCodeMapKey">
<Tabs.TabPane v-for="key in codeMap.keys()" :key="key" :tab="key.split('/').pop()"> <Tabs.TabPane v-for="key in codeMap.keys()" :key="key" :tab="key.split('/').pop()">
<div class="h-[calc(100%-40px)] overflow-auto"> <div class="h-full rounded-md bg-gray-50 !p-0 text-gray-800 dark:bg-gray-800 dark:text-gray-200">
<pre class="overflow-auto rounded-md bg-gray-50 p-4 text-gray-800 dark:bg-gray-800 dark:text-gray-200"> <!-- eslint-disable-next-line vue/no-v-html -->
<!-- eslint-disable-next-line vue/no-v-html --> <code v-html="codeMap.get(activeKey)" class="code-highlight"></code>
<code v-html="codeMap.get(activeKey)" class="code-highlight"></code>
</pre>
</div> </div>
</Tabs.TabPane> </Tabs.TabPane>
<template #rightExtra> <template #rightExtra>
@ -248,13 +250,6 @@ const [Modal, modalApi] = useVbenModal({
background: transparent; background: transparent;
} }
/* 代码块内容无缩进 */
:deep(pre) {
padding: 1rem;
margin: 0;
white-space: pre;
}
/* 关键字 */ /* 关键字 */
:deep(.hljs-keyword) { :deep(.hljs-keyword) {
@apply text-purple-600 dark:text-purple-400; @apply text-purple-600 dark:text-purple-400;