fix(core): clamp node extent by node dimensions to avoid overflow (#1014)

This commit is contained in:
Braks
2023-07-10 19:06:35 +02:00
parent 6967067276
commit 82e79a6113
2 changed files with 21 additions and 2 deletions

View File

@@ -0,0 +1,5 @@
---
"@vue-flow/core": patch
---
Clamp node extent by node dimensions to avoid node overflowing the extent by its size

View File

@@ -1,6 +1,15 @@
import { markRaw } from 'vue'
import { ErrorCode, VueFlowError, clampPosition, isNumber, isParentSelected } from '.'
import type { Actions, CoordinateExtent, CoordinateExtentRange, GraphNode, NodeDragItem, State, XYPosition } from '~/types'
import type {
Actions,
CoordinateExtent,
CoordinateExtentRange,
Dimensions,
GraphNode,
NodeDragItem,
State,
XYPosition,
} from '~/types'
export function hasSelector(target: Element, selector: string, node: Element): boolean {
let current = target
@@ -157,6 +166,11 @@ export function getExtent<T extends NodeDragItem | GraphNode>(
return currentExtent as CoordinateExtent
}
function clampNodeExtent({ width, height }: Dimensions, extent: CoordinateExtent): CoordinateExtent {
return [extent[0], [extent[1][0] - (width || 0), extent[1][1] - (height || 0)]]
}
export function calcNextPosition(
node: GraphNode | NodeDragItem,
nextPosition: XYPosition,
@@ -164,7 +178,7 @@ export function calcNextPosition(
nodeExtent?: State['nodeExtent'],
parentNode?: GraphNode,
) {
const extent = getExtent(node, onError, nodeExtent, parentNode)
const extent = clampNodeExtent(node.dimensions, getExtent(node, onError, nodeExtent, parentNode))
const clampedPos = clampPosition(nextPosition, extent)