feat(components): 优化目录结构并添加编辑功能
- 重构 buildTree 函数,提高目录结构生成效率 - 在目录项中添加编辑图标和相关事件处理 - 优化目录项的样式和布局
This commit is contained in:
@@ -267,18 +267,45 @@ const initVditor = () => {
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
const buildTree = (items, parentId = 0) => {
|
const buildTree = (items) => {
|
||||||
return items
|
const tree = [];
|
||||||
.filter(item => +item.parentId === +parentId)
|
const itemMap = new Map();
|
||||||
.map(item => {
|
|
||||||
const children = buildTree(items, item.id);
|
// First, map all items by their id
|
||||||
return {
|
items.forEach(item => {
|
||||||
|
itemMap.set(String(item.id), {
|
||||||
...item,
|
...item,
|
||||||
value: item.id,
|
value: item.id,
|
||||||
label: item.grouping,
|
label: item.grouping,
|
||||||
children: children.length > 0 ? children : undefined,
|
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 () => {
|
const fetchGroupings = async () => {
|
||||||
@@ -388,10 +415,11 @@ const renderMenu = (item) => {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
return h(ElMenuItem, { index: `group-${item.id}`, onClick: () => selectFile(item) }, {
|
return h(ElMenuItem, { index: `group-${item.id}`, onClick: () => selectFile(item) }, {
|
||||||
default: () => [
|
default: () => h('div', { class: 'menu-item-title' }, [
|
||||||
h(ElIcon, () => h(Document)),
|
h(ElIcon, () => h(Folder)),
|
||||||
h('span', null, item.grouping)
|
h('span', null, item.grouping),
|
||||||
]
|
h(ElIcon, { class: 'edit-icon', onClick: (e) => { e.stopPropagation(); openRenameDialog(item, 'group'); } }, () => h(Edit))
|
||||||
|
])
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user