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, x, y } = useStore((s) => {
|
||||||
const node = s.nodeLookup.get(id) as InternalNode<NodeType>;
|
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 {
|
return {
|
||||||
node,
|
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 => {
|
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 {
|
return {
|
||||||
...positionAbsolute,
|
x,
|
||||||
|
y,
|
||||||
width: node.measured?.width ?? node.width ?? 0,
|
width: node.measured?.width ?? node.width ?? 0,
|
||||||
height: node.measured?.height ?? node.height ?? 0,
|
height: node.measured?.height ?? node.height ?? 0,
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
export const nodeToBox = (node: InternalNodeBase | NodeBase, nodeOrigin: NodeOrigin = [0, 0]): Box => {
|
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 {
|
return {
|
||||||
...positionAbsolute,
|
x,
|
||||||
x2: positionAbsolute.x + (node.measured?.width ?? node.width ?? 0),
|
y,
|
||||||
y2: positionAbsolute.y + (node.measured?.height ?? node.height ?? 0),
|
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 = (
|
export const getNodePositionWithOrigin = (
|
||||||
node: InternalNodeBase | NodeBase | undefined,
|
node: InternalNodeBase | NodeBase,
|
||||||
nodeOrigin: NodeOrigin = [0, 0]
|
nodeOrigin: NodeOrigin = [0, 0]
|
||||||
): { position: XYPosition; positionAbsolute: XYPosition } => {
|
): { position: XYPosition; positionAbsolute: XYPosition } => {
|
||||||
if (!node) {
|
|
||||||
return {
|
|
||||||
position: {
|
|
||||||
x: 0,
|
|
||||||
y: 0,
|
|
||||||
},
|
|
||||||
positionAbsolute: {
|
|
||||||
x: 0,
|
|
||||||
y: 0,
|
|
||||||
},
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
const { width, height } = getNodeDimensions(node);
|
const { width, height } = getNodeDimensions(node);
|
||||||
const offsetX = width * nodeOrigin[0];
|
const positionAbsolute = 'internals' in node ? node.internals.positionAbsolute : node.position;
|
||||||
const offsetY = height * nodeOrigin[1];
|
const origin = node.origin || nodeOrigin;
|
||||||
|
const offsetX = width * origin[0];
|
||||||
const position: XYPosition = {
|
const offsetY = height * origin[1];
|
||||||
x: node.position.x - offsetX,
|
|
||||||
y: node.position.y - offsetY,
|
|
||||||
};
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
position,
|
position: {
|
||||||
positionAbsolute:
|
x: node.position.x - offsetX,
|
||||||
'internals' in node
|
y: node.position.y - offsetY,
|
||||||
? {
|
},
|
||||||
x: node.internals.positionAbsolute.x - offsetX,
|
positionAbsolute: {
|
||||||
y: node.internals.positionAbsolute.y - offsetY,
|
x: positionAbsolute.x - offsetX,
|
||||||
}
|
y: positionAbsolute.y - offsetY,
|
||||||
: position,
|
},
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -170,7 +154,7 @@ export const getNodesBounds = (
|
|||||||
|
|
||||||
const box = nodes.reduce(
|
const box = nodes.reduce(
|
||||||
(currBox, node) => {
|
(currBox, node) => {
|
||||||
const nodePos = getNodePositionWithOrigin(node, node.origin || params.nodeOrigin);
|
const nodePos = getNodePositionWithOrigin(node, params.nodeOrigin);
|
||||||
return getBoundsOfBoxes(
|
return getBoundsOfBoxes(
|
||||||
currBox,
|
currBox,
|
||||||
rectToBox({
|
rectToBox({
|
||||||
|
|||||||
@@ -139,18 +139,18 @@ function calculateXYZPosition<NodeType extends NodeBase>(
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
const parentNode = nodeLookup.get(node.parentId)!;
|
const parent = nodeLookup.get(node.parentId)!;
|
||||||
const { position: parentNodePosition } = getNodePositionWithOrigin(parentNode, parentNode?.origin || nodeOrigin);
|
const parentPosition = getNodePositionWithOrigin(parent, nodeOrigin).position;
|
||||||
|
|
||||||
return calculateXYZPosition(
|
return calculateXYZPosition(
|
||||||
parentNode,
|
parent,
|
||||||
nodeLookup,
|
nodeLookup,
|
||||||
{
|
{
|
||||||
x: (result.x ?? 0) + parentNodePosition.x,
|
x: (result.x ?? 0) + parentPosition.x,
|
||||||
y: (result.y ?? 0) + parentNodePosition.y,
|
y: (result.y ?? 0) + parentPosition.y,
|
||||||
z: (parentNode.internals.z ?? 0) > (result.z ?? 0) ? parentNode.internals.z ?? 0 : result.z ?? 0,
|
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