2026-04-23 09:05:55 +08:00
|
|
|
type TreeNodeId = string | number;
|
|
|
|
|
|
2026-03-26 20:18:20 +08:00
|
|
|
type TreeNode = {
|
2026-04-23 09:05:55 +08:00
|
|
|
id: TreeNodeId;
|
|
|
|
|
parentId: TreeNodeId;
|
2026-03-26 20:18:20 +08:00
|
|
|
sort?: number | null;
|
|
|
|
|
children?: TreeNode[] | null;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export function buildMenuTree<T extends TreeNode>(list: T[]) {
|
2026-04-23 09:05:55 +08:00
|
|
|
const nodeMap = new Map<TreeNodeId, T>();
|
2026-03-26 20:18:20 +08:00
|
|
|
const roots: T[] = [];
|
|
|
|
|
|
|
|
|
|
list.forEach(item => {
|
|
|
|
|
nodeMap.set(item.id, {
|
|
|
|
|
...item,
|
|
|
|
|
children: []
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
nodeMap.forEach(node => {
|
2026-04-23 09:05:55 +08:00
|
|
|
if (isRootParentId(node.parentId)) {
|
2026-03-26 20:18:20 +08:00
|
|
|
roots.push(node);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const parent = nodeMap.get(node.parentId);
|
|
|
|
|
|
|
|
|
|
if (!parent) {
|
|
|
|
|
roots.push(node);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
parent.children = [...(parent.children ?? []), node];
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return sortMenuTree(roots);
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-23 09:05:55 +08:00
|
|
|
export function collectDescendantIds<T extends Pick<TreeNode, 'id' | 'children'>>(nodes: T[], targetId: T['id']) {
|
2026-03-26 20:18:20 +08:00
|
|
|
const target = findTreeNode(nodes, targetId);
|
|
|
|
|
|
|
|
|
|
if (!target?.children?.length) {
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-23 09:05:55 +08:00
|
|
|
const ids: T['id'][] = [];
|
2026-03-26 20:18:20 +08:00
|
|
|
|
|
|
|
|
walkTree(target.children, item => {
|
2026-04-23 09:05:55 +08:00
|
|
|
ids.push(item.id as T['id']);
|
2026-03-26 20:18:20 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return ids;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function sortMenuTree<T extends TreeNode>(nodes: T[]) {
|
|
|
|
|
const sortedNodes = [...nodes].sort((prev, next) => Number(prev.sort ?? 0) - Number(next.sort ?? 0));
|
|
|
|
|
|
|
|
|
|
sortedNodes.forEach(node => {
|
|
|
|
|
if (node.children?.length) {
|
|
|
|
|
node.children = sortMenuTree(node.children as T[]);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return sortedNodes;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-23 09:05:55 +08:00
|
|
|
function findTreeNode<T extends Pick<TreeNode, 'id' | 'children'>>(nodes: T[], targetId: T['id']): T | null {
|
2026-03-26 20:18:20 +08:00
|
|
|
for (const node of nodes) {
|
|
|
|
|
if (node.id === targetId) {
|
|
|
|
|
return node;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (node.children?.length) {
|
|
|
|
|
const target = findTreeNode(node.children as unknown as T[], targetId);
|
|
|
|
|
|
|
|
|
|
if (target) {
|
|
|
|
|
return target;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-23 09:05:55 +08:00
|
|
|
function isRootParentId(parentId: TreeNodeId) {
|
|
|
|
|
return parentId === 0 || parentId === '0';
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-26 20:18:20 +08:00
|
|
|
function walkTree<T extends Pick<TreeNode, 'id' | 'children'>>(nodes: T[], callback: (node: T) => void) {
|
|
|
|
|
for (const node of nodes) {
|
|
|
|
|
callback(node);
|
|
|
|
|
|
|
|
|
|
if (node.children?.length) {
|
|
|
|
|
walkTree(node.children as unknown as T[], callback);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|