fix(subflow-node-origin): use node origin in node position calculation

This commit is contained in:
Christopher Möller
2022-11-24 16:55:51 +01:00
parent 6bbc004315
commit 3685420ebf
7 changed files with 97 additions and 36 deletions
+42 -9
View File
@@ -141,23 +141,55 @@ export const pointToRendererPoint = (
return position;
};
export const getNodePosition = (
node: Node | undefined,
nodeOrigin: NodeOrigin = [0, 0]
): XYPosition & { positionAbsolute: XYPosition } => {
if (!node) {
return {
x: 0,
y: 0,
positionAbsolute: {
x: 0,
y: 0,
},
};
}
const position: XYPosition = {
x: node.position.x - (node.width ?? 0) * nodeOrigin[0],
y: node.position.y - (node.height ?? 0) * nodeOrigin[1],
};
const positionAbsolute: XYPosition = {
x: (node.positionAbsolute?.x ?? 0) - (node.width ?? 0) * nodeOrigin[0],
y: (node.positionAbsolute?.y ?? 0) - (node.height ?? 0) * nodeOrigin[1],
};
return {
...position,
positionAbsolute,
};
};
export const getRectOfNodes = (nodes: Node[], nodeOrigin: NodeOrigin = [0, 0]): Rect => {
if (nodes.length === 0) {
return { x: 0, y: 0, width: 0, height: 0 };
}
const box = nodes.reduce(
(currBox, { positionAbsolute, position, width, height }) => {
(currBox, node) => {
const { positionAbsolute, ...position } = getNodePosition(node, nodeOrigin);
const nodeX = positionAbsolute ? positionAbsolute.x : position.x;
const nodeY = positionAbsolute ? positionAbsolute.y : position.y;
return getBoundsOfBoxes(
currBox,
rectToBox({
x: nodeX - nodeOrigin[0] * (width || 0),
y: nodeY - nodeOrigin[1] * (height || 0),
width: width || 0,
height: height || 0,
x: nodeX,
y: nodeY,
width: node.width || 0,
height: node.height || 0,
})
);
},
@@ -186,15 +218,17 @@ export const getNodesInside = (
const visibleNodes: Node[] = [];
nodeInternals.forEach((node) => {
const { width, height, selectable = true, positionAbsolute = { x: 0, y: 0 } } = node;
const { width, height, selectable = true } = node;
if (excludeNonSelectableNodes && !selectable) {
return false;
}
const { positionAbsolute } = getNodePosition(node, nodeOrigin);
const nodeRect = {
x: positionAbsolute.x - nodeOrigin[0] * (width || 0),
y: positionAbsolute.y - nodeOrigin[1] * (height || 0),
x: positionAbsolute.x,
y: positionAbsolute.y,
width: width || 0,
height: height || 0,
};
@@ -243,4 +277,3 @@ export const getTransformForBounds = (
export const getD3Transition = (selection: D3Selection<Element, unknown, null, undefined>, duration = 0) => {
return selection.transition().duration(duration);
};