Skip to content

Commit

Permalink
fix(tree-view): do not traverse original nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
metonym committed Nov 22, 2024
1 parent 07d226c commit 38cf331
Showing 1 changed file with 12 additions and 14 deletions.
26 changes: 12 additions & 14 deletions src/TreeView/TreeView.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@
* @type {(filterId?: (node: TreeNode) => boolean) => void}
*/
export function expandNodes(filterNode = (node) => false) {
expandedIds = nodes
expandedIds = flattenedNodes
.filter(
(node) =>
filterNode(node) ||
Expand All @@ -110,7 +110,7 @@
* @type {(filterId?: (node: TreeNode) => boolean) => void}
*/
export function collapseNodes(filterNode = (node) => true) {
expandedIds = nodes
expandedIds = flattenedNodes
.filter((node) => expandedIds.includes(node.id) && !filterNode(node))
.map((node) => node.id);
}
Expand Down Expand Up @@ -207,24 +207,22 @@
});
/**
* Recursively flattens a tree of nodes into a single array
* @param {Array<TreeNode & { nodes?: TreeNode[] }>} nodes
* @returns {Array<TreeNode>}
*/
function traverse(nodes) {
let _nodes = [];
nodes.forEach((node) => {
_nodes.push(node);
if (Array.isArray(node.nodes)) {
_nodes = [...nodes, ...traverse(node.nodes)];
return nodes.reduce((acc, node) => {
acc.push(node);
if (Array.isArray(node.nodes) && node.nodes.length > 0) {
acc.push(...traverse(node.nodes));
}
});
return _nodes;
return acc;
}, []);
}
$: nodes = traverse(nodes);
$: nodeIds = nodes.map((node) => node.id);
$: flattenedNodes = traverse(nodes);
$: nodeIds = flattenedNodes.map((node) => node.id);
$: activeNodeId.set(activeId);
$: selectedNodeIds.set(selectedIds);
$: expandedNodeIds.set(expandedIds);
Expand Down

0 comments on commit 38cf331

Please sign in to comment.