refactor(nodelookup): add treelevel
This commit is contained in:
@@ -251,8 +251,8 @@ const EdgeRenderer = (props: EdgeRendererProps) => {
|
|||||||
<g transform={`translate(${transform[0]},${transform[1]}) scale(${transform[2]})`}>
|
<g transform={`translate(${transform[0]},${transform[1]}) scale(${transform[2]})`}>
|
||||||
{edges.map((edge: Edge) => {
|
{edges.map((edge: Edge) => {
|
||||||
// @todo: getSourceTargetNodes is called many times during dragging/creating edges
|
// @todo: getSourceTargetNodes is called many times during dragging/creating edges
|
||||||
const sourceNode = nodeLookup.current.get(edge.source);
|
const sourceNode = nodeLookup.get(edge.source);
|
||||||
const targetNode = nodeLookup.current.get(edge.target);
|
const targetNode = nodeLookup.get(edge.target);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Edge
|
<Edge
|
||||||
|
|||||||
@@ -236,11 +236,13 @@ const NodeRenderer = (props: NodeRendererProps) => {
|
|||||||
});
|
});
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
|
console.log(nodeLookup);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="react-flow__nodes" style={transformStyle}>
|
<div className="react-flow__nodes" style={transformStyle}>
|
||||||
<MemoizedNodes
|
<MemoizedNodes
|
||||||
nodes={nodes}
|
nodes={nodes}
|
||||||
nodeLookup={nodeLookup.current}
|
nodeLookup={nodeLookup}
|
||||||
snapToGrid={snapToGrid}
|
snapToGrid={snapToGrid}
|
||||||
snapGrid={snapGrid}
|
snapGrid={snapGrid}
|
||||||
nodesDraggable={nodesDraggable}
|
nodesDraggable={nodesDraggable}
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ function useNodeLookup() {
|
|||||||
const store = useStoreApi();
|
const store = useStoreApi();
|
||||||
const nodeLookup = useRef(store.getState().nodeLookup);
|
const nodeLookup = useRef(store.getState().nodeLookup);
|
||||||
|
|
||||||
return nodeLookup;
|
return nodeLookup.current;
|
||||||
}
|
}
|
||||||
|
|
||||||
export default useNodeLookup;
|
export default useNodeLookup;
|
||||||
|
|||||||
+27
-8
@@ -73,21 +73,32 @@ const createNodeOrEdgeSelectionChange = (isSelected: boolean) => (item: Node | E
|
|||||||
// return result;
|
// return result;
|
||||||
// }
|
// }
|
||||||
|
|
||||||
function addPositions(posA: XYPosition, posB: XYPosition): XYPosition {
|
type XYPosAndTreeLevel = XYPosition & { treeLevel: number };
|
||||||
|
|
||||||
|
function addPositions(a: XYPosAndTreeLevel, b: XYPosition): XYPosAndTreeLevel {
|
||||||
return {
|
return {
|
||||||
x: (posA.x ?? 0) + (posB.x ?? 0),
|
x: (a.x ?? 0) + (b.x ?? 0),
|
||||||
y: (posA.y ?? 0) + (posB.y ?? 0),
|
y: (a.y ?? 0) + (b.y ?? 0),
|
||||||
|
treeLevel: a.treeLevel + 1,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
function getAbsolutePosition(node: NodeLookupItem, nodeLookup: NodeLookup, result: XYPosition): XYPosition {
|
function getAbsolutePositionAndTreeLevel(
|
||||||
|
node: NodeLookupItem,
|
||||||
|
nodeLookup: NodeLookup,
|
||||||
|
result: XYPosAndTreeLevel
|
||||||
|
): XYPosAndTreeLevel {
|
||||||
const parentNode = node.parentNode ? nodeLookup.get(node.parentNode) : false;
|
const parentNode = node.parentNode ? nodeLookup.get(node.parentNode) : false;
|
||||||
|
|
||||||
if (!parentNode) {
|
if (!parentNode) {
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
return getAbsolutePosition(parentNode, nodeLookup, addPositions(result, parentNode.position || { x: 0, y: 0 }));
|
return getAbsolutePositionAndTreeLevel(
|
||||||
|
parentNode,
|
||||||
|
nodeLookup,
|
||||||
|
addPositions(result, parentNode.position || { x: 0, y: 0 })
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
const createStore = () =>
|
const createStore = () =>
|
||||||
@@ -158,6 +169,7 @@ const createStore = () =>
|
|||||||
height: node.height || null,
|
height: node.height || null,
|
||||||
position: node.position,
|
position: node.position,
|
||||||
positionAbsolute: node.position,
|
positionAbsolute: node.position,
|
||||||
|
treeLevel: 0,
|
||||||
};
|
};
|
||||||
if (node.parentNode) {
|
if (node.parentNode) {
|
||||||
lookupNode.parentNode = node.parentNode;
|
lookupNode.parentNode = node.parentNode;
|
||||||
@@ -168,12 +180,19 @@ const createStore = () =>
|
|||||||
nodes
|
nodes
|
||||||
.filter((node) => node.parentNode)
|
.filter((node) => node.parentNode)
|
||||||
.forEach((node) => {
|
.forEach((node) => {
|
||||||
const positionAbsolute = getAbsolutePosition(node, nodeLookup, node.position);
|
const positionAbsoluteAndTreeLevel = getAbsolutePositionAndTreeLevel(node, nodeLookup, {
|
||||||
|
...node.position,
|
||||||
|
treeLevel: 0,
|
||||||
|
});
|
||||||
|
|
||||||
if (positionAbsolute) {
|
if (positionAbsoluteAndTreeLevel) {
|
||||||
nodeLookup.set(node.id, {
|
nodeLookup.set(node.id, {
|
||||||
...nodeLookup.get(node.id),
|
...nodeLookup.get(node.id),
|
||||||
positionAbsolute,
|
positionAbsolute: {
|
||||||
|
x: positionAbsoluteAndTreeLevel.x,
|
||||||
|
y: positionAbsoluteAndTreeLevel.y,
|
||||||
|
},
|
||||||
|
treeLevel: positionAbsoluteAndTreeLevel.treeLevel,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -465,6 +465,7 @@ export type NodeLookupItem = {
|
|||||||
position?: XYPosition;
|
position?: XYPosition;
|
||||||
positionAbsolute?: XYPosition;
|
positionAbsolute?: XYPosition;
|
||||||
handleBounds?: NodeHandleBounds;
|
handleBounds?: NodeHandleBounds;
|
||||||
|
treeLevel?: number;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type NodeLookup = Map<ElementId, NodeLookupItem>;
|
export type NodeLookup = Map<ElementId, NodeLookupItem>;
|
||||||
|
|||||||
Reference in New Issue
Block a user