From 6c306861277ded485634debca08867242d1624da Mon Sep 17 00:00:00 2001 From: moklick Date: Thu, 18 Apr 2024 09:17:51 +0200 Subject: [PATCH] refactor(utils): cleanup getPositionWithOrigin --- .../MiniMap/MiniMapNodes.tsx | 2 +- packages/system/src/utils/general.ts | 14 +++--- packages/system/src/utils/graph.ts | 44 ++++++------------- packages/system/src/utils/store.ts | 14 +++--- 4 files changed, 30 insertions(+), 44 deletions(-) diff --git a/packages/react/src/additional-components/MiniMap/MiniMapNodes.tsx b/packages/react/src/additional-components/MiniMap/MiniMapNodes.tsx index f2e7944c..da6d3745 100644 --- a/packages/react/src/additional-components/MiniMap/MiniMapNodes.tsx +++ b/packages/react/src/additional-components/MiniMap/MiniMapNodes.tsx @@ -86,7 +86,7 @@ function NodeComponentWrapperInner({ }) { const { node, x, y } = useStore((s) => { const node = s.nodeLookup.get(id) as InternalNode; - const { x, y } = getNodePositionWithOrigin(node, node?.origin || nodeOrigin).positionAbsolute; + const { x, y } = getNodePositionWithOrigin(node, nodeOrigin).positionAbsolute; return { node, diff --git a/packages/system/src/utils/general.ts b/packages/system/src/utils/general.ts index 0c259bbc..1023b44e 100644 --- a/packages/system/src/utils/general.ts +++ b/packages/system/src/utils/general.ts @@ -68,22 +68,24 @@ export const boxToRect = ({ x, y, x2, y2 }: Box): Rect => ({ }); export const nodeToRect = (node: InternalNodeBase | NodeBase, nodeOrigin: NodeOrigin = [0, 0]): Rect => { - const { positionAbsolute } = getNodePositionWithOrigin(node, node.origin || nodeOrigin); + const { x, y } = getNodePositionWithOrigin(node, nodeOrigin).positionAbsolute; return { - ...positionAbsolute, + x, + y, width: node.measured?.width ?? node.width ?? 0, height: node.measured?.height ?? node.height ?? 0, }; }; export const nodeToBox = (node: InternalNodeBase | NodeBase, nodeOrigin: NodeOrigin = [0, 0]): Box => { - const { positionAbsolute } = getNodePositionWithOrigin(node, node.origin || nodeOrigin); + const { x, y } = getNodePositionWithOrigin(node, nodeOrigin).positionAbsolute; return { - ...positionAbsolute, - x2: positionAbsolute.x + (node.measured?.width ?? node.width ?? 0), - y2: positionAbsolute.y + (node.measured?.height ?? node.height ?? 0), + x, + y, + x2: x + (node.measured?.width ?? node.width ?? 0), + y2: y + (node.measured?.height ?? node.height ?? 0), }; }; diff --git a/packages/system/src/utils/graph.ts b/packages/system/src/utils/graph.ts index e8bb5357..c306f3e3 100644 --- a/packages/system/src/utils/graph.ts +++ b/packages/system/src/utils/graph.ts @@ -108,40 +108,24 @@ export const getIncomers = { - if (!node) { - return { - position: { - x: 0, - y: 0, - }, - positionAbsolute: { - x: 0, - y: 0, - }, - }; - } - const { width, height } = getNodeDimensions(node); - const offsetX = width * nodeOrigin[0]; - const offsetY = height * nodeOrigin[1]; - - const position: XYPosition = { - x: node.position.x - offsetX, - y: node.position.y - offsetY, - }; + const positionAbsolute = 'internals' in node ? node.internals.positionAbsolute : node.position; + const origin = node.origin || nodeOrigin; + const offsetX = width * origin[0]; + const offsetY = height * origin[1]; return { - position, - positionAbsolute: - 'internals' in node - ? { - x: node.internals.positionAbsolute.x - offsetX, - y: node.internals.positionAbsolute.y - offsetY, - } - : position, + position: { + x: node.position.x - offsetX, + y: node.position.y - offsetY, + }, + positionAbsolute: { + x: positionAbsolute.x - offsetX, + y: positionAbsolute.y - offsetY, + }, }; }; @@ -170,7 +154,7 @@ export const getNodesBounds = ( const box = nodes.reduce( (currBox, node) => { - const nodePos = getNodePositionWithOrigin(node, node.origin || params.nodeOrigin); + const nodePos = getNodePositionWithOrigin(node, params.nodeOrigin); return getBoundsOfBoxes( currBox, rectToBox({ diff --git a/packages/system/src/utils/store.ts b/packages/system/src/utils/store.ts index 56eba62b..385b44ef 100644 --- a/packages/system/src/utils/store.ts +++ b/packages/system/src/utils/store.ts @@ -139,18 +139,18 @@ function calculateXYZPosition( return result; } - const parentNode = nodeLookup.get(node.parentId)!; - const { position: parentNodePosition } = getNodePositionWithOrigin(parentNode, parentNode?.origin || nodeOrigin); + const parent = nodeLookup.get(node.parentId)!; + const parentPosition = getNodePositionWithOrigin(parent, nodeOrigin).position; return calculateXYZPosition( - parentNode, + parent, nodeLookup, { - x: (result.x ?? 0) + parentNodePosition.x, - y: (result.y ?? 0) + parentNodePosition.y, - z: (parentNode.internals.z ?? 0) > (result.z ?? 0) ? parentNode.internals.z ?? 0 : result.z ?? 0, + x: (result.x ?? 0) + parentPosition.x, + y: (result.y ?? 0) + parentPosition.y, + z: (parent.internals.z ?? 0) > (result.z ?? 0) ? parent.internals.z ?? 0 : result.z ?? 0, }, - parentNode.origin || nodeOrigin + parent.origin || nodeOrigin ); }