fix(@vben/web-antdv-next): 修复代码生成部分,预览时鼠标移入文件树,预览内容不显示的问题

pull/362/head
XuZhiqiang 2026-06-17 16:25:45 +08:00
parent 407d0bf25d
commit b0c151e6fd
1 changed files with 22 additions and 15 deletions

View File

@ -26,6 +26,7 @@ const loading = ref(false);
const fileTree = ref<FileNode[]>([]); const fileTree = ref<FileNode[]>([]);
const previewFiles = ref<InfraCodegenApi.CodegenPreview[]>([]); const previewFiles = ref<InfraCodegenApi.CodegenPreview[]>([]);
const activeKey = ref<string>(''); const activeKey = ref<string>('');
const selectedKeys = ref<string[]>([]);
/** 代码地图 */ /** 代码地图 */
const codeMap = ref<Map<string, string>>(new Map<string, string>()); const codeMap = ref<Map<string, string>>(new Map<string, string>());
@ -53,31 +54,35 @@ function removeCodeMapKey(targetKey: any) {
/** 复制代码 */ /** 复制代码 */
async function copyCode() { async function copyCode() {
const { copy } = useClipboard(); const { copy } = useClipboard();
const file = previewFiles.value.find( const file = findPreviewFile(activeKey.value);
(item) => item.filePath === activeKey.value,
);
if (file) { if (file) {
await copy(file.code); await copy(file.code);
message.success('复制成功'); message.success('复制成功');
} }
} }
/** 文件节点点击事件 */ function findPreviewFile(fileKey: string) {
function handleNodeClick(_: any[], e: any) { return previewFiles.value.find((item) => {
if (!e.node.isLeaf) { const list = fileKey.split('.');
return;
}
activeKey.value = e.node.key;
const file = previewFiles.value.find((item) => {
const list = activeKey.value.split('.');
// - // -
if (list.length > 2) { if (list.length > 2) {
const lang = list.pop(); const lang = list.pop();
return item.filePath === `${list.join('/')}.${lang}`; return item.filePath === `${list.join('/')}.${lang}`;
} }
return item.filePath === activeKey.value; return item.filePath === fileKey;
}); });
}
/** 文件节点点击事件 */
function handleNodeClick(_: any[], e: any) {
if (!e.node.isLeaf) {
selectedKeys.value = activeKey.value ? [activeKey.value] : [];
return;
}
activeKey.value = String(e.node.key);
selectedKeys.value = [activeKey.value];
const file = findPreviewFile(activeKey.value);
if (!file) { if (!file) {
return; return;
} }
@ -179,6 +184,7 @@ const [Modal, modalApi] = useVbenModal({
if (!isOpen) { if (!isOpen) {
// //
codeMap.value.clear(); codeMap.value.clear();
selectedKeys.value = [];
return; return;
} }
@ -197,6 +203,7 @@ const [Modal, modalApi] = useVbenModal({
fileTree.value = handleFiles(data); fileTree.value = handleFiles(data);
if (data.length > 0) { if (data.length > 0) {
activeKey.value = data[0]?.filePath || ''; activeKey.value = data[0]?.filePath || '';
selectedKeys.value = activeKey.value ? [activeKey.value] : [];
const code = data[0]?.code || ''; const code = data[0]?.code || '';
setCodeMap(activeKey.value, code); setCodeMap(activeKey.value, code);
} }
@ -217,7 +224,7 @@ const [Modal, modalApi] = useVbenModal({
<DirectoryTree <DirectoryTree
v-if="fileTree.length > 0" v-if="fileTree.length > 0"
default-expand-all default-expand-all
v-model:active-key="activeKey" v-model:selected-keys="selectedKeys"
@select="handleNodeClick" @select="handleNodeClick"
:tree-data="fileTree" :tree-data="fileTree"
/> />
@ -240,7 +247,7 @@ const [Modal, modalApi] = useVbenModal({
> >
<CodeEditor <CodeEditor
class="max-h-200" class="max-h-200"
:value="codeMap.get(activeKey)" :value="codeMap.get(key)"
mode="application/json" mode="application/json"
:readonly="true" :readonly="true"
:bordered="true" :bordered="true"