Make internal nodes immutable

This commit is contained in:
peterkogo
2025-01-21 15:45:08 +01:00
parent 7b3d9eae5b
commit 7cb9a8686d
+25 -10
View File
@@ -87,6 +87,7 @@ export function adoptUserNodes<NodeType extends NodeBase>(
options?: UpdateNodesOptions<NodeType> options?: UpdateNodesOptions<NodeType>
) { ) {
const _options = mergeObjects(adoptUserNodesDefaultOptions, options); const _options = mergeObjects(adoptUserNodesDefaultOptions, options);
const tmpLookup = new Map(nodeLookup); const tmpLookup = new Map(nodeLookup);
const selectedNodeZ: number = _options?.elevateNodesOnSelect ? 1000 : 0; const selectedNodeZ: number = _options?.elevateNodesOnSelect ? 1000 : 0;
@@ -173,11 +174,15 @@ function updateChildNode<NodeType extends NodeBase>(
const positionChanged = x !== positionAbsolute.x || y !== positionAbsolute.y; const positionChanged = x !== positionAbsolute.x || y !== positionAbsolute.y;
if (positionChanged || z !== node.internals.z) { if (positionChanged || z !== node.internals.z) {
node.internals = { // we create a new object to mark the node as updated
nodeLookup.set(node.id, {
...node,
internals: {
...node.internals, ...node.internals,
positionAbsolute: positionChanged ? { x, y } : positionAbsolute, positionAbsolute: positionChanged ? { x, y } : positionAbsolute,
z, z,
}; },
});
} }
} }
@@ -333,12 +338,17 @@ export function updateNodeInternals<NodeType extends InternalNodeBase>(
} }
if (node.hidden) { if (node.hidden) {
node.internals = { nodeLookup.set(node.id, {
...node,
internals: {
...node.internals, ...node.internals,
handleBounds: undefined, handleBounds: undefined,
}; },
});
updatedInternals = true; updatedInternals = true;
} else { continue;
}
const dimensions = getDimensions(update.nodeElement); const dimensions = getDimensions(update.nodeElement);
const dimensionChanged = node.measured.width !== dimensions.width || node.measured.height !== dimensions.height; const dimensionChanged = node.measured.width !== dimensions.width || node.measured.height !== dimensions.height;
const doUpdate = !!( const doUpdate = !!(
@@ -358,17 +368,23 @@ export function updateNodeInternals<NodeType extends InternalNodeBase>(
positionAbsolute = clampPosition(positionAbsolute, extent, dimensions); positionAbsolute = clampPosition(positionAbsolute, extent, dimensions);
} }
node.measured = dimensions; const newNode = {
node.internals = { ...node,
measured: dimensions,
internals: {
...node.internals, ...node.internals,
positionAbsolute, positionAbsolute,
handleBounds: { handleBounds: {
source: getHandleBounds('source', update.nodeElement, nodeBounds, zoom, node.id), source: getHandleBounds('source', update.nodeElement, nodeBounds, zoom, node.id),
target: getHandleBounds('target', update.nodeElement, nodeBounds, zoom, node.id), target: getHandleBounds('target', update.nodeElement, nodeBounds, zoom, node.id),
}, },
},
}; };
nodeLookup.set(node.id, newNode);
if (node.parentId) { if (node.parentId) {
updateChildNode(node, nodeLookup, parentLookup, { nodeOrigin }); updateChildNode(newNode, nodeLookup, parentLookup, { nodeOrigin });
} }
updatedInternals = true; updatedInternals = true;
@@ -384,13 +400,12 @@ export function updateNodeInternals<NodeType extends InternalNodeBase>(
parentExpandChildren.push({ parentExpandChildren.push({
id: node.id, id: node.id,
parentId: node.parentId, parentId: node.parentId,
rect: nodeToRect(node, nodeOrigin), rect: nodeToRect(newNode, nodeOrigin),
}); });
} }
} }
} }
} }
}
if (parentExpandChildren.length > 0) { if (parentExpandChildren.length > 0) {
const parentExpandChanges = handleExpandParent(parentExpandChildren, nodeLookup, parentLookup, nodeOrigin); const parentExpandChanges = handleExpandParent(parentExpandChildren, nodeLookup, parentLookup, nodeOrigin);