feat(core): support origin on node level closes #2874

This commit is contained in:
moklick
2023-03-14 12:42:06 +01:00
parent cca25a792b
commit 13e7cc340c
8 changed files with 26 additions and 19 deletions
@@ -29,12 +29,14 @@ const initialNodes: Node[] = [
data: { label: 'Node 1' }, data: { label: 'Node 1' },
position: { x: 250, y: 5 }, position: { x: 250, y: 5 },
className: 'light', className: 'light',
origin: [0.5, 0.5],
}, },
{ {
id: '4', id: '4',
data: { label: 'Node 4' }, data: { label: 'Node 4' },
position: { x: 100, y: 200 }, position: { x: 100, y: 200 },
className: 'light', className: 'light',
origin: [0.5, 0.5],
style: { style: {
backgroundColor: 'rgba(255,50, 50, 0.5)', backgroundColor: 'rgba(255,50, 50, 0.5)',
width: 500, width: 500,
@@ -47,6 +49,8 @@ const initialNodes: Node[] = [
position: { x: 15, y: 15 }, position: { x: 15, y: 15 },
className: 'light', className: 'light',
parentNode: '4', parentNode: '4',
origin: [0.5, 0.5],
extent: [ extent: [
[0, 0], [0, 0],
[100, 100], [100, 100],
@@ -97,7 +97,7 @@ const NodeRenderer = (props: NodeRendererProps) => {
y: posY, y: posY,
width: node.width ?? 0, width: node.width ?? 0,
height: node.height ?? 0, height: node.height ?? 0,
origin: props.nodeOrigin, origin: node.origin || props.nodeOrigin,
}); });
return ( return (
+8 -6
View File
@@ -71,6 +71,7 @@ export function getDragItems(
parentNode: n.parentNode, parentNode: n.parentNode,
width: n.width, width: n.width,
height: n.height, height: n.height,
origin: n.origin,
})); }));
} }
@@ -87,14 +88,15 @@ export function calcNextPosition(
if (node.extent === 'parent') { if (node.extent === 'parent') {
if (node.parentNode && node.width && node.height) { if (node.parentNode && node.width && node.height) {
const parent = nodeInternals.get(node.parentNode); const parent = nodeInternals.get(node.parentNode);
const { x: parentX, y: parentY } = getNodePositionWithOrigin(parent, nodeOrigin).positionAbsolute; const parentOrigin = parent?.origin || nodeOrigin;
const { x: parentX, y: parentY } = getNodePositionWithOrigin(parent, parentOrigin).positionAbsolute;
currentExtent = currentExtent =
parent && isNumeric(parentX) && isNumeric(parentY) && isNumeric(parent.width) && isNumeric(parent.height) parent && isNumeric(parentX) && isNumeric(parentY) && isNumeric(parent.width) && isNumeric(parent.height)
? [ ? [
[parentX + node.width * nodeOrigin[0], parentY + node.height * nodeOrigin[1]], [parentX + node.width * parentOrigin[0], parentY + node.height * parentOrigin[1]],
[ [
parentX + parent.width - node.width + node.width * nodeOrigin[0], parentX + parent.width - node.width + node.width * parentOrigin[0],
parentY + parent.height - node.height + node.height * nodeOrigin[1], parentY + parent.height - node.height + node.height * parentOrigin[1],
], ],
] ]
: currentExtent; : currentExtent;
@@ -105,7 +107,7 @@ export function calcNextPosition(
} }
} else if (node.extent && node.parentNode) { } else if (node.extent && node.parentNode) {
const parent = nodeInternals.get(node.parentNode); const parent = nodeInternals.get(node.parentNode);
const { x: parentX, y: parentY } = getNodePositionWithOrigin(parent, nodeOrigin).positionAbsolute; const { x: parentX, y: parentY } = getNodePositionWithOrigin(parent, parent?.origin || nodeOrigin).positionAbsolute;
currentExtent = [ currentExtent = [
[node.extent[0][0] + parentX, node.extent[0][1] + parentY], [node.extent[0][0] + parentX, node.extent[0][1] + parentY],
[node.extent[1][0] + parentX, node.extent[1][1] + parentY], [node.extent[1][0] + parentX, node.extent[1][1] + parentY],
@@ -116,7 +118,7 @@ export function calcNextPosition(
if (node.parentNode) { if (node.parentNode) {
const parentNode = nodeInternals.get(node.parentNode); const parentNode = nodeInternals.get(node.parentNode);
parentPosition = getNodePositionWithOrigin(parentNode, nodeOrigin).positionAbsolute; parentPosition = getNodePositionWithOrigin(parentNode, parentNode?.origin || nodeOrigin).positionAbsolute;
} }
const positionAbsolute = currentExtent const positionAbsolute = currentExtent
+2 -2
View File
@@ -92,8 +92,8 @@ const createRFStore = () =>
[internalsSymbol]: { [internalsSymbol]: {
...node[internalsSymbol], ...node[internalsSymbol],
handleBounds: { handleBounds: {
source: getHandleBounds('.source', update.nodeElement, zoom, nodeOrigin), source: getHandleBounds('.source', update.nodeElement, zoom, node.origin || nodeOrigin),
target: getHandleBounds('.target', update.nodeElement, zoom, nodeOrigin), target: getHandleBounds('.target', update.nodeElement, zoom, node.origin || nodeOrigin),
}, },
}, },
...dimensions, ...dimensions,
+4 -3
View File
@@ -16,7 +16,7 @@ function calculateXYZPosition(
return result; return result;
} }
const parentNode = nodeInternals.get(node.parentNode)!; const parentNode = nodeInternals.get(node.parentNode)!;
const parentNodePosition = getNodePositionWithOrigin(parentNode, nodeOrigin); const parentNodePosition = getNodePositionWithOrigin(parentNode, parentNode?.origin || nodeOrigin);
return calculateXYZPosition( return calculateXYZPosition(
parentNode, parentNode,
@@ -26,7 +26,7 @@ function calculateXYZPosition(
y: (result.y ?? 0) + parentNodePosition.y, y: (result.y ?? 0) + parentNodePosition.y,
z: (parentNode[internalsSymbol]?.z ?? 0) > (result.z ?? 0) ? parentNode[internalsSymbol]?.z ?? 0 : result.z ?? 0, z: (parentNode[internalsSymbol]?.z ?? 0) > (result.z ?? 0) ? parentNode[internalsSymbol]?.z ?? 0 : result.z ?? 0,
}, },
nodeOrigin parentNode.origin || nodeOrigin
); );
} }
@@ -41,6 +41,7 @@ export function updateAbsoluteNodePositions(
} }
if (node.parentNode || parentNodes?.[node.id]) { if (node.parentNode || parentNodes?.[node.id]) {
const parentNode = node.parentNode ? nodeInternals.get(node.parentNode) : null;
const { x, y, z } = calculateXYZPosition( const { x, y, z } = calculateXYZPosition(
node, node,
nodeInternals, nodeInternals,
@@ -48,7 +49,7 @@ export function updateAbsoluteNodePositions(
...node.position, ...node.position,
z: node[internalsSymbol]?.z ?? 0, z: node[internalsSymbol]?.z ?? 0,
}, },
nodeOrigin parentNode?.origin || nodeOrigin
); );
node.positionAbsolute = { node.positionAbsolute = {
+1 -1
View File
@@ -181,7 +181,7 @@ function MiniMap({
> >
{ariaLabel && <title id={labelledBy}>{ariaLabel}</title>} {ariaLabel && <title id={labelledBy}>{ariaLabel}</title>}
{nodes.map((node) => { {nodes.map((node) => {
const { x, y } = getNodePositionWithOrigin(node, nodeOrigin).positionAbsolute; const { x, y } = getNodePositionWithOrigin(node, node.origin || nodeOrigin).positionAbsolute;
return ( return (
<NodeComponent <NodeComponent
+3 -3
View File
@@ -216,7 +216,7 @@ export const getRectOfNodes = (nodes: BaseNode[], nodeOrigin: NodeOrigin = [0, 0
const box = nodes.reduce( const box = nodes.reduce(
(currBox, node) => { (currBox, node) => {
const { x, y } = getNodePositionWithOrigin(node, nodeOrigin).positionAbsolute; const { x, y } = getNodePositionWithOrigin(node, node.origin || nodeOrigin).positionAbsolute;
return getBoundsOfBoxes( return getBoundsOfBoxes(
currBox, currBox,
rectToBox({ rectToBox({
@@ -256,7 +256,7 @@ export const getNodesInside = <NodeType extends BaseNode>(
return res; return res;
} }
const { positionAbsolute } = getNodePositionWithOrigin(node, nodeOrigin); const { positionAbsolute } = getNodePositionWithOrigin(node, node.origin || nodeOrigin);
const nodeRect = { const nodeRect = {
x: positionAbsolute.x, x: positionAbsolute.x,
@@ -316,7 +316,7 @@ export const getD3Transition = (selection: D3SelectionInstance, duration = 0) =>
}; };
export function fitView<Params extends FitViewParamsBase<BaseNode>, Options extends FitViewOptionsBase<BaseNode>>( export function fitView<Params extends FitViewParamsBase<BaseNode>, Options extends FitViewOptionsBase<BaseNode>>(
{ nodes, width, height, d3Zoom, d3Selection, nodeOrigin, minZoom, maxZoom }: Params, { nodes, width, height, d3Zoom, d3Selection, minZoom, maxZoom, nodeOrigin = [0, 0] }: Params,
options?: Options options?: Options
) { ) {
const filteredNodes = nodes.filter((n) => { const filteredNodes = nodes.filter((n) => {
+2 -2
View File
@@ -172,13 +172,13 @@ export const getPositionWithOrigin = ({
y, y,
width, width,
height, height,
origin, origin = [0, 0],
}: { }: {
x: number; x: number;
y: number; y: number;
width: number; width: number;
height: number; height: number;
origin: NodeOrigin; origin?: NodeOrigin;
}): XYPosition => { }): XYPosition => {
if (!width || !height) { if (!width || !height) {
return { x, y }; return { x, y };