refactor(utils): cleanup getPositionWithOrigin
This commit is contained in:
@@ -86,7 +86,7 @@ function NodeComponentWrapperInner<NodeType extends Node>({
|
||||
}) {
|
||||
const { node, x, y } = useStore((s) => {
|
||||
const node = s.nodeLookup.get(id) as InternalNode<NodeType>;
|
||||
const { x, y } = getNodePositionWithOrigin(node, node?.origin || nodeOrigin).positionAbsolute;
|
||||
const { x, y } = getNodePositionWithOrigin(node, nodeOrigin).positionAbsolute;
|
||||
|
||||
return {
|
||||
node,
|
||||
|
||||
@@ -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),
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
@@ -108,40 +108,24 @@ export const getIncomers = <NodeType extends NodeBase = NodeBase, EdgeType exten
|
||||
};
|
||||
|
||||
export const getNodePositionWithOrigin = (
|
||||
node: InternalNodeBase | NodeBase | undefined,
|
||||
node: InternalNodeBase | NodeBase,
|
||||
nodeOrigin: NodeOrigin = [0, 0]
|
||||
): { position: XYPosition; positionAbsolute: XYPosition } => {
|
||||
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({
|
||||
|
||||
@@ -139,18 +139,18 @@ function calculateXYZPosition<NodeType extends NodeBase>(
|
||||
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
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user