feat(nested-nodes): change data structure to flat array of nodes

This commit is contained in:
Christopher Möller
2021-10-21 15:39:27 +02:00
parent 64239fbb51
commit 2252ea11ae
8 changed files with 64 additions and 205 deletions
+12 -15
View File
@@ -3,7 +3,6 @@ import { ComponentType } from 'react';
import { BezierEdge, StepEdge, SmoothStepEdge, StraightEdge } from '../../components/Edges';
import wrapEdge from '../../components/Edges/wrapEdge';
import { rectToBox } from '../../utils/graph';
import { nodeHelper } from '../../utils/nodes';
import {
EdgeTypesType,
@@ -172,18 +171,16 @@ type SourceTargetNode = {
};
export const getSourceTargetNodes = (edge: Edge, nodes: Node[]): SourceTargetNode => {
return nodeHelper(nodes)
.flatten()
.reduce(
(res, node) => {
if (node.id === edge.source) {
res.sourceNode = node;
}
if (node.id === edge.target) {
res.targetNode = node;
}
return res;
},
{ sourceNode: null, targetNode: null } as SourceTargetNode
);
return nodes.reduce(
(res, node) => {
if (node.id === edge.source) {
res.sourceNode = node;
}
if (node.id === edge.target) {
res.targetNode = node;
}
return res;
},
{ sourceNode: null, targetNode: null } as SourceTargetNode
);
};
+10 -16
View File
@@ -77,8 +77,16 @@ function Nodes({
typeof node.height !== 'undefined';
let childRect;
if (node.childNodes) {
childRect = getRectOfNodes(node.childNodes);
const childNodes = nodes.filter((n) => n.parentNode === node.id);
// if (childNodes.length) {
// console.log(node.id, childNodes, getRectOfNodes(childNodes));
// }
// console.log(childNodes);
if (childNodes.length) {
childRect = getRectOfNodes(childNodes);
node.position = node.isDragging
? node.position
: { x: Math.round(childRect.x) - 10, y: Math.round(childRect.y) - 10 };
@@ -126,20 +134,6 @@ function Nodes({
dragHandle={node.dragHandle}
zIndex={3 + recursionDepth}
/>
{node.childNodes && (
<MemoizedNodes
nodes={node.childNodes}
snapToGrid={snapToGrid}
snapGrid={snapGrid}
nodesDraggable={nodesDraggable}
nodesConnectable={nodesConnectable}
resizeObserver={resizeObserver}
elementsSelectable={elementsSelectable}
scale={scale}
recursionDepth={recursionDepth + 1}
{...props}
/>
)}
</Fragment>
);
});