From 1bfc45b240cb1ab4fe9b6b86077f98c143c8ec32 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=AD=9F?= <3111696955@qq.com> Date: Thu, 31 Jul 2025 14:32:00 +0800 Subject: [PATCH] =?UTF-8?q?feat(components):=20=E4=BC=98=E5=8C=96=E7=9B=AE?= =?UTF-8?q?=E5=BD=95=E7=BB=93=E6=9E=84=E5=B9=B6=E6=B7=BB=E5=8A=A0=E7=BC=96?= =?UTF-8?q?=E8=BE=91=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 重构 buildTree 函数,提高目录结构生成效率 - 在目录项中添加编辑图标和相关事件处理 - 优化目录项的样式和布局 --- biji-qianduan/src/components/HomePage.vue | 60 +++++++++++++++++------ 1 file changed, 44 insertions(+), 16 deletions(-) diff --git a/biji-qianduan/src/components/HomePage.vue b/biji-qianduan/src/components/HomePage.vue index 6958910..b3f9ef6 100644 --- a/biji-qianduan/src/components/HomePage.vue +++ b/biji-qianduan/src/components/HomePage.vue @@ -267,18 +267,45 @@ const initVditor = () => { }); }; -const buildTree = (items, parentId = 0) => { - return items - .filter(item => +item.parentId === +parentId) - .map(item => { - const children = buildTree(items, item.id); - return { - ...item, - value: item.id, - label: item.grouping, - children: children.length > 0 ? children : undefined, - }; - }); +const buildTree = (items) => { + const tree = []; + const itemMap = new Map(); + + // First, map all items by their id + items.forEach(item => { + itemMap.set(String(item.id), { + ...item, + value: item.id, + label: item.grouping, + children: [], // Initialize children array + }); + }); + + // Then, build the tree structure + itemMap.forEach(item => { + const parentId = String(item.parentId); + if (parentId !== '0' && itemMap.has(parentId)) { + const parent = itemMap.get(parentId); + parent.children.push(item); + } else { + // If it's a root node or an orphan, add it to the top level + tree.push(item); + } + }); + + // Helper to remove empty children arrays + const cleanTree = (nodes) => { + nodes.forEach(node => { + if (node.children.length === 0) { + delete node.children; + } else { + cleanTree(node.children); + } + }); + }; + + cleanTree(tree); + return tree; }; const fetchGroupings = async () => { @@ -388,10 +415,11 @@ const renderMenu = (item) => { }); } return h(ElMenuItem, { index: `group-${item.id}`, onClick: () => selectFile(item) }, { - default: () => [ - h(ElIcon, () => h(Document)), - h('span', null, item.grouping) - ] + default: () => h('div', { class: 'menu-item-title' }, [ + h(ElIcon, () => h(Folder)), + h('span', null, item.grouping), + h(ElIcon, { class: 'edit-icon', onClick: (e) => { e.stopPropagation(); openRenameDialog(item, 'group'); } }, () => h(Edit)) + ]) }); };