feat(noderenderer): convert from flat to nested format internally

This commit is contained in:
Christopher Möller
2021-10-27 12:53:28 +02:00
parent dc36ebf31f
commit 5f9bc2e0f2
3 changed files with 40 additions and 27 deletions
+21 -2
View File
@@ -2,7 +2,26 @@ import { useCallback } from 'react';
import { useStore } from '../store';
import { getNodesInside } from '../utils/graph';
import { ReactFlowState } from '../types';
import { ReactFlowState, Node } from '../types';
function getChildNodes(nodes: Node[], parent?: Node): Node[] {
const children: Node[] = [];
const remaining: Node[] = [];
for (let i = 0; i < nodes.length; i++) {
const n = nodes[i];
if ((!parent && !n.parentNode) || n.parentNode === parent?.id) {
children.push(n);
} else {
remaining.push(n);
}
}
return children.map((child) => {
child.childNodes = getChildNodes(remaining, child);
return child;
});
}
function useVisibleNodes(onlyRenderVisible: boolean) {
const nodes = useStore(
@@ -16,7 +35,7 @@ function useVisibleNodes(onlyRenderVisible: boolean) {
)
);
return nodes;
return getChildNodes(nodes);
}
export default useVisibleNodes;