refactor(node/internals): cleanup

This commit is contained in:
moklick
2024-04-02 20:15:38 +02:00
parent 67d979985d
commit e409939234
2 changed files with 30 additions and 30 deletions
+1 -2
View File
@@ -19,7 +19,6 @@ import type {
Edge, Edge,
EdgeSelectionChange, EdgeSelectionChange,
NodeSelectionChange, NodeSelectionChange,
NodePositionChange,
UnselectNodesAndEdgesParams, UnselectNodesAndEdgesParams,
FitViewOptions, FitViewOptions,
InternalNode, InternalNode,
@@ -99,7 +98,7 @@ const createRFStore = ({
return; return;
} }
updateAbsolutePositions(nodeLookup, nodeOrigin); updateAbsolutePositions(nodeLookup, { nodeOrigin });
// we call fitView once initially after all dimensions are set // we call fitView once initially after all dimensions are set
let nextFitViewDone = fitViewDone; let nextFitViewDone = fitViewDone;
+29 -28
View File
@@ -19,46 +19,45 @@ import { getDimensions, getHandleBounds } from './dom';
import { getBoundsOfRects, getNodeDimensions, isNumeric, nodeToRect } from './general'; import { getBoundsOfRects, getNodeDimensions, isNumeric, nodeToRect } from './general';
import { getNodePositionWithOrigin } from './graph'; import { getNodePositionWithOrigin } from './graph';
type ParentNodes = Set<string>;
export function updateAbsolutePositions<NodeType extends NodeBase>( export function updateAbsolutePositions<NodeType extends NodeBase>(
nodeLookup: Map<string, InternalNodeBase<NodeType>>, nodeLookup: Map<string, InternalNodeBase<NodeType>>,
nodeOrigin: NodeOrigin = [0, 0], options: UpdateNodesOptions<NodeType> = {
parentNodes?: ParentNodes nodeOrigin: [0, 0] as NodeOrigin,
elevateNodesOnSelect: true,
defaults: {},
},
parentNodeIds?: Set<string>
) { ) {
for (const [, node] of nodeLookup) { const selectedNodeZ: number = options?.elevateNodesOnSelect ? 1000 : 0;
if (node.parentNode && !nodeLookup.has(node.parentNode)) {
throw new Error(`Parent node ${node.parentNode} not found`); for (const [id, node] of nodeLookup) {
const parentId = node.parentNode;
if (parentId && !nodeLookup.has(parentId)) {
throw new Error(`Parent node ${parentId} not found`);
} }
if (node.parentNode || parentNodes?.has(node.id)) { if (node.parentNode || node.internals.isParent || parentNodeIds?.has(id)) {
const parentNode = node.parentNode ? nodeLookup.get(node.parentNode) : null; const parentNode = parentId ? nodeLookup.get(parentId) : null;
const { x, y, z } = calculateXYZPosition( const { x, y, z } = calculateXYZPosition(
node, node,
nodeLookup, nodeLookup,
{ {
...node.position, ...node.position,
z: node.internals.z ?? 0, z: (isNumeric(node.zIndex) ? node.zIndex : 0) + (node.selected ? selectedNodeZ : 0),
}, },
parentNode?.origin || nodeOrigin parentNode?.origin || options.nodeOrigin
); );
const positionChanged = x !== node.internals.positionAbsolute?.x || y !== node.internals.positionAbsolute?.y; const currPosition = node.internals.positionAbsolute;
node.internals.positionAbsolute = positionChanged const positionChanged = x !== currPosition.x || y !== currPosition.y;
? {
x,
y,
}
: node.internals.positionAbsolute;
node.internals.positionAbsolute = positionChanged ? { x, y } : currPosition;
node.internals.z = z; node.internals.z = z;
node.internals.isParent = !!parentNodeIds?.has(id);
if (parentNodes?.has(node.id)) { nodeLookup.set(id, node);
node.internals.isParent = true;
}
} }
nodeLookup.set(node.id, node);
} }
} }
@@ -79,14 +78,16 @@ export function adoptUserProvidedNodes<NodeType extends NodeBase>(
) { ) {
const tmpLookup = new Map(nodeLookup); const tmpLookup = new Map(nodeLookup);
nodeLookup.clear(); nodeLookup.clear();
const parentNodes: ParentNodes = new Set();
const selectedNodeZ: number = options?.elevateNodesOnSelect ? 1000 : 0; const selectedNodeZ: number = options?.elevateNodesOnSelect ? 1000 : 0;
const parentNodeIds = new Set<string>();
nodes.forEach((n) => { nodes.forEach((n) => {
const currentStoreNode = tmpLookup.get(n.id); const currentStoreNode = tmpLookup.get(n.id);
if (n.parentNode) { if (n.parentNode) {
parentNodes.add(n.parentNode); parentNodeIds.add(n.parentNode);
} }
if (n === currentStoreNode?.internals?.userProvidedNode) { if (n === currentStoreNode?.internals?.userProvidedNode) {
nodeLookup.set(n.id, currentStoreNode); nodeLookup.set(n.id, currentStoreNode);
return currentStoreNode; return currentStoreNode;
@@ -111,8 +112,8 @@ export function adoptUserProvidedNodes<NodeType extends NodeBase>(
nodeLookup.set(node.id, node); nodeLookup.set(node.id, node);
}); });
if (parentNodes.size > 0) { if (parentNodeIds.size > 0) {
updateAbsolutePositions(nodeLookup, options.nodeOrigin, parentNodes); updateAbsolutePositions(nodeLookup, options, parentNodeIds);
} }
} }
@@ -120,7 +121,7 @@ function calculateXYZPosition<NodeType extends NodeBase>(
node: NodeType, node: NodeType,
nodeLookup: Map<string, InternalNodeBase<NodeType>>, nodeLookup: Map<string, InternalNodeBase<NodeType>>,
result: XYZPosition, result: XYZPosition,
nodeOrigin: NodeOrigin nodeOrigin: NodeOrigin = [0, 0]
): XYZPosition { ): XYZPosition {
if (!node.parentNode) { if (!node.parentNode) {
return result; return result;