Files
iot-device-management-frontend/apps/web-antd/src/views/infra/codegen/modules/preview-code.vue

372 lines
9.2 KiB
Vue
Raw Normal View History

2025-04-10 11:34:20 +08:00
<script lang="ts" setup>
2025-04-19 10:18:54 +08:00
// TODO @芋艿待定vben2.0 有 CodeEditor不确定官方后续会不会迁移
2025-04-10 11:34:20 +08:00
import type { InfraCodegenApi } from '#/api/infra/codegen';
2025-04-22 22:10:33 +08:00
import { h, ref } from 'vue';
2025-04-10 11:34:20 +08:00
import { useVbenModal } from '@vben/common-ui';
import { Copy } from '@vben/icons';
import { useClipboard } from '@vueuse/core';
2025-04-22 22:10:33 +08:00
import { Button, DirectoryTree, message, Tabs } from 'ant-design-vue';
2025-04-10 11:34:20 +08:00
import hljs from 'highlight.js/lib/core';
import java from 'highlight.js/lib/languages/java';
import javascript from 'highlight.js/lib/languages/javascript';
import sql from 'highlight.js/lib/languages/sql';
import typescript from 'highlight.js/lib/languages/typescript';
import xml from 'highlight.js/lib/languages/xml';
2025-04-22 22:10:33 +08:00
import { previewCodegen } from '#/api/infra/codegen';
2025-04-10 11:34:20 +08:00
/** 注册代码高亮语言 */
hljs.registerLanguage('java', java);
hljs.registerLanguage('xml', xml);
hljs.registerLanguage('html', xml);
hljs.registerLanguage('vue', xml);
hljs.registerLanguage('javascript', javascript);
hljs.registerLanguage('sql', sql);
hljs.registerLanguage('typescript', typescript);
/** 文件树类型 */
interface FileNode {
key: string;
title: string;
parentKey: string;
isLeaf?: boolean;
children?: FileNode[];
}
/** 组件状态 */
const loading = ref(false);
const fileTree = ref<FileNode[]>([]);
const previewFiles = ref<InfraCodegenApi.CodegenPreview[]>([]);
const activeKey = ref<string>('');
2025-04-16 11:23:19 +08:00
/** 代码地图 */
2025-04-22 16:10:42 +08:00
const codeMap = ref<Map<string, string>>(new Map<string, string>());
const setCodeMap = (key: string, lang: string, code: string) => {
2025-04-16 11:23:19 +08:00
// 处理可能的缩进问题特别是对Java文件
const trimmedCode = code.trimStart();
2025-04-22 16:10:42 +08:00
// 如果已有缓存则不重新构建
if (codeMap.value.has(key)) {
return;
}
2025-04-16 11:23:19 +08:00
try {
const highlightedCode = hljs.highlight(trimmedCode, {
language: lang,
}).value;
2025-04-22 16:10:42 +08:00
codeMap.value.set(key, highlightedCode);
2025-04-16 11:23:19 +08:00
} catch {
2025-04-22 16:10:42 +08:00
codeMap.value.set(key, trimmedCode);
2025-04-16 11:23:19 +08:00
}
};
const removeCodeMapKey = (targetKey: any) => {
// 只有一个代码视图时不允许删除
2025-04-22 16:10:42 +08:00
if (codeMap.value.size === 1) {
2025-04-16 11:23:19 +08:00
return;
}
2025-04-22 16:10:42 +08:00
if (codeMap.value.has(targetKey)) {
codeMap.value.delete(targetKey);
2025-04-16 11:23:19 +08:00
}
};
2025-04-10 11:34:20 +08:00
/** 复制代码 */
const copyCode = async () => {
const { copy } = useClipboard();
2025-04-22 22:10:33 +08:00
const file = previewFiles.value.find(
(item) => item.filePath === activeKey.value,
);
2025-04-10 11:34:20 +08:00
if (file) {
await copy(file.code);
message.success('复制成功');
}
};
/** 文件节点点击事件 */
const handleNodeClick = (_: any[], e: any) => {
2025-04-16 11:23:19 +08:00
if (!e.node.isLeaf) return;
activeKey.value = e.node.key;
2025-04-16 11:32:51 +08:00
const file = previewFiles.value.find((item) => {
const list = activeKey.value.split('.');
// 特殊处理-包合并
if (list.length > 2) {
const lang = list.pop();
return item.filePath === `${list.join('/')}.${lang}`;
2025-04-16 11:32:51 +08:00
}
return item.filePath === activeKey.value;
});
2025-04-16 11:23:19 +08:00
if (!file) return;
const lang = file.filePath.split('.').pop() || '';
2025-04-22 16:10:42 +08:00
setCodeMap(activeKey.value, lang, file.code);
2025-04-10 11:34:20 +08:00
};
/** 处理文件树 */
const handleFiles = (data: InfraCodegenApi.CodegenPreview[]): FileNode[] => {
const exists: Record<string, boolean> = {};
const files: FileNode[] = [];
// 处理文件路径
for (const item of data) {
const paths = item.filePath.split('/');
2025-04-16 11:23:19 +08:00
let cursor = 0;
2025-04-10 11:34:20 +08:00
let fullPath = '';
2025-04-16 11:23:19 +08:00
while (cursor < paths.length) {
const path = paths[cursor] || '';
const oldFullPath = fullPath;
2025-04-10 11:34:20 +08:00
2025-04-16 11:23:19 +08:00
// 处理Java包路径特殊情况
if (path === 'java' && cursor + 1 < paths.length) {
fullPath = fullPath ? `${fullPath}/${path}` : path;
cursor++;
2025-04-10 11:34:20 +08:00
// 合并包路径
let packagePath = '';
2025-04-16 11:23:19 +08:00
while (cursor < paths.length) {
const nextPath = paths[cursor] || '';
2025-04-22 22:10:33 +08:00
if (
[
'controller',
'convert',
'dal',
'dataobject',
'enums',
'mysql',
'service',
'vo',
].includes(nextPath)
) {
2025-04-10 11:34:20 +08:00
break;
}
packagePath = packagePath ? `${packagePath}.${nextPath}` : nextPath;
2025-04-16 11:23:19 +08:00
cursor++;
2025-04-10 11:34:20 +08:00
}
if (packagePath) {
2025-04-16 11:23:19 +08:00
const newFullPath = `${fullPath}/${packagePath}`;
if (!exists[newFullPath]) {
exists[newFullPath] = true;
files.push({
key: newFullPath,
title: packagePath,
parentKey: oldFullPath || '/',
isLeaf: cursor === paths.length,
});
}
fullPath = newFullPath;
2025-04-10 11:34:20 +08:00
}
continue;
}
2025-04-16 11:23:19 +08:00
// 处理普通路径
fullPath = fullPath ? `${fullPath}/${path}` : path;
if (!exists[fullPath]) {
exists[fullPath] = true;
files.push({
key: fullPath,
title: path,
parentKey: oldFullPath || '/',
isLeaf: cursor === paths.length - 1,
});
2025-04-10 11:34:20 +08:00
}
2025-04-16 11:23:19 +08:00
cursor++;
2025-04-10 11:34:20 +08:00
}
}
/** 构建树形结构 */
const buildTree = (parentKey: string): FileNode[] => {
return files
.filter((file) => file.parentKey === parentKey)
.map((file) => ({
...file,
children: buildTree(file.key),
}));
};
return buildTree('/');
};
/** 模态框实例 */
const [Modal, modalApi] = useVbenModal({
footer: false,
2025-04-22 16:10:42 +08:00
fullscreen: true,
2025-04-10 11:34:20 +08:00
async onOpenChange(isOpen: boolean) {
if (!isOpen) {
// 关闭时清除代码视图缓存
2025-04-22 16:10:42 +08:00
codeMap.value.clear();
2025-04-10 11:34:20 +08:00
return;
}
const row = modalApi.getData<InfraCodegenApi.CodegenTable>();
if (!row) return;
// 加载预览数据
loading.value = true;
try {
const data = await previewCodegen(row.id);
previewFiles.value = data;
2025-04-11 18:43:25 +08:00
// 构建代码树,并默认选中第一个文件
fileTree.value = handleFiles(data);
2025-04-10 11:34:20 +08:00
if (data.length > 0) {
activeKey.value = data[0]?.filePath || '';
const lang = activeKey.value.split('.').pop() || '';
const code = data[0]?.code || '';
2025-04-22 16:10:42 +08:00
setCodeMap(activeKey.value, lang, code);
2025-04-10 11:34:20 +08:00
}
} finally {
loading.value = false;
}
},
});
</script>
<template>
<Modal title="代码预览">
2025-04-15 18:19:29 +08:00
<div class="flex h-full" v-loading="loading">
2025-04-10 11:34:20 +08:00
<!-- 文件树 -->
2025-04-22 22:10:33 +08:00
<div
class="h-full w-1/3 overflow-auto border-r border-gray-200 pr-4 dark:border-gray-700"
>
2025-04-22 16:10:42 +08:00
<DirectoryTree
v-if="fileTree.length > 0"
default-expand-all
v-model:active-key="activeKey"
@select="handleNodeClick"
:tree-data="fileTree"
/>
2025-04-10 11:34:20 +08:00
</div>
<!-- 代码预览 -->
2025-04-22 16:10:42 +08:00
<div class="h-full w-2/3 overflow-auto pl-4">
2025-04-22 22:10:33 +08:00
<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()"
>
<div
class="h-full rounded-md bg-gray-50 !p-0 text-gray-800 dark:bg-gray-800 dark:text-gray-200"
>
2025-04-22 16:10:42 +08:00
<!-- eslint-disable-next-line vue/no-v-html -->
2025-04-22 22:10:33 +08:00
<code
v-html="codeMap.get(activeKey)"
class="code-highlight"
></code>
2025-04-16 11:23:19 +08:00
</div>
</Tabs.TabPane>
<template #rightExtra>
2025-04-22 22:10:33 +08:00
<Button type="primary" ghost @click="copyCode" :icon="h(Copy)">
复制代码
</Button>
2025-04-16 11:23:19 +08:00
</template>
</Tabs>
2025-04-10 11:34:20 +08:00
</div>
</div>
</Modal>
</template>
<style scoped>
/* stylelint-disable selector-class-pattern */
/* 代码高亮样式 - 支持暗黑模式 */
:deep(.code-highlight) {
2025-04-16 11:23:19 +08:00
display: block;
white-space: pre;
2025-04-10 11:34:20 +08:00
background: transparent;
}
/* 关键字 */
:deep(.hljs-keyword) {
@apply text-purple-600 dark:text-purple-400;
}
/* 字符串 */
:deep(.hljs-string) {
@apply text-green-600 dark:text-green-400;
}
/* 注释 */
:deep(.hljs-comment) {
@apply text-gray-500 dark:text-gray-400;
}
/* 函数 */
:deep(.hljs-function) {
@apply text-blue-600 dark:text-blue-400;
}
/* 数字 */
:deep(.hljs-number) {
@apply text-orange-600 dark:text-orange-400;
}
/* 类 */
:deep(.hljs-class) {
@apply text-yellow-600 dark:text-yellow-400;
}
/* 标题/函数名 */
:deep(.hljs-title) {
@apply font-bold text-blue-600 dark:text-blue-400;
}
/* 参数 */
:deep(.hljs-params) {
@apply text-gray-700 dark:text-gray-300;
}
/* 内置对象 */
:deep(.hljs-built_in) {
@apply text-teal-600 dark:text-teal-400;
}
/* HTML标签 */
:deep(.hljs-tag) {
@apply text-blue-600 dark:text-blue-400;
}
/* 属性 */
:deep(.hljs-attribute),
:deep(.hljs-attr) {
@apply text-green-600 dark:text-green-400;
}
/* 字面量 */
:deep(.hljs-literal) {
@apply text-purple-600 dark:text-purple-400;
}
/* 元信息 */
:deep(.hljs-meta) {
@apply text-gray-500 dark:text-gray-400;
}
/* 选择器标签 */
:deep(.hljs-selector-tag) {
@apply text-blue-600 dark:text-blue-400;
}
/* XML/HTML名称 */
:deep(.hljs-name) {
@apply text-blue-600 dark:text-blue-400;
}
/* 变量 */
:deep(.hljs-variable) {
@apply text-orange-600 dark:text-orange-400;
}
/* 属性 */
:deep(.hljs-property) {
@apply text-red-600 dark:text-red-400;
}
/* stylelint-enable selector-class-pattern */
</style>