From 3d446a6adb68935a7b68404c37e950d85b21087b Mon Sep 17 00:00:00 2001 From: moklick Date: Mon, 21 Mar 2022 22:03:31 +0100 Subject: [PATCH] fix(nodeExtent): nodes node extent over global node extent --- src/store/initialState.ts | 2 +- src/store/utils.ts | 28 +++++++++++++++++----------- 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/src/store/initialState.ts b/src/store/initialState.ts index e5280f43..020c0eab 100644 --- a/src/store/initialState.ts +++ b/src/store/initialState.ts @@ -1,6 +1,6 @@ import { CoordinateExtent, ReactFlowStore, ConnectionMode } from '../types'; -const infiniteExtent: CoordinateExtent = [ +export const infiniteExtent: CoordinateExtent = [ [Number.NEGATIVE_INFINITY, Number.NEGATIVE_INFINITY], [Number.POSITIVE_INFINITY, Number.POSITIVE_INFINITY], ]; diff --git a/src/store/utils.ts b/src/store/utils.ts index a4926169..85b50996 100644 --- a/src/store/utils.ts +++ b/src/store/utils.ts @@ -16,6 +16,7 @@ import { XYZPosition, FitViewOptions, } from '../types'; +import { infiniteExtent } from './initialState'; type ParentNodes = Record; @@ -128,20 +129,25 @@ export function createPositionChange({ if (diff) { const nextPosition = { x: node.position.x + diff.x, y: node.position.y + diff.y }; - let currentExtent = nodeExtent || node.extent; + let currentExtent = node.extent || nodeExtent; - if (node.extent === 'parent' && node.parentNode && node.width && node.height) { - const parent = nodeInternals.get(node.parentNode); - currentExtent = - parent?.width && parent?.height - ? [ - [0, 0], - [parent.width - node.width, parent.height - node.height], - ] - : currentExtent; + if (node.extent === 'parent') { + if (node.parentNode && node.width && node.height) { + const parent = nodeInternals.get(node.parentNode); + currentExtent = + parent?.width && parent?.height + ? [ + [0, 0], + [parent.width - node.width, parent.height - node.height], + ] + : currentExtent; + } else { + console.warn('Only child nodes can use parent extent'); + currentExtent = infiniteExtent; + } } - change.position = currentExtent ? clampPosition(nextPosition, currentExtent) : nextPosition; + change.position = currentExtent ? clampPosition(nextPosition, currentExtent as CoordinateExtent) : nextPosition; } return change;