refactor(nodes): use relative positions for children

This commit is contained in:
moklick
2021-11-02 18:58:47 +01:00
parent 8cd969dd84
commit 248b563231
8 changed files with 168 additions and 80 deletions
+12
View File
@@ -0,0 +1,12 @@
import { useRef } from 'react';
import { useStoreApi } from '../store';
function useNodeLookup() {
const store = useStoreApi();
const nodeLookup = useRef(store.getState().nodeLookup);
return nodeLookup;
}
export default useNodeLookup;
+5 -5
View File
@@ -2,23 +2,23 @@ import { useCallback } from 'react';
import { useStore } from '../store';
import { getNodesInside } from '../utils/graph';
import { ReactFlowState, Node } from '../types';
import { ReactFlowState, Node, NodeRendererNode } from '../types';
function getChildNodes(nodes: Node[], parent?: Node): Node[] {
const children: Node[] = [];
function getChildNodes(nodes: Node[], parent?: Node): NodeRendererNode[] {
const children: NodeRendererNode[] = [];
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);
children.push({ node: n });
} else {
remaining.push(n);
}
}
return children.map((child) => {
child.childNodes = getChildNodes(remaining, child);
child.childNodes = getChildNodes(remaining, child.node);
return child;
});
}