Skip to content

Commit 90a913f

Browse files
committed
fix(core): clamp child pos to parent
Signed-off-by: braks <[email protected]>
1 parent 4c429ec commit 90a913f

File tree

4 files changed

+14
-5
lines changed

4 files changed

+14
-5
lines changed

Diff for: packages/core/src/components/Nodes/NodeWrapper.ts

+2
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,8 @@ const NodeWrapper = defineComponent({
226226
} else {
227227
node.computedPosition = xyzPos
228228
}
229+
230+
clampPosition()
229231
},
230232
{ flush: 'post', immediate: true },
231233
)

Diff for: packages/core/src/utils/drag.ts

+10-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import type {
99
State,
1010
XYPosition,
1111
} from '../types'
12-
import { ErrorCode, VueFlowError, clampPosition, isParentSelected } from '.'
12+
import { ErrorCode, VueFlowError, clampPosition, clampPositionToParent, isParentSelected } from '.'
1313

1414
export function hasSelector(target: Element, selector: string, node: Element): boolean {
1515
let current = target
@@ -192,15 +192,23 @@ export function calcNextPosition(
192192
nodeExtent?: State['nodeExtent'],
193193
parentNode?: GraphNode,
194194
) {
195+
const currentExtent = node.extent || nodeExtent
195196
const extent = clampNodeExtent(node.dimensions, getExtent(node, triggerError, nodeExtent, parentNode))
196197

197198
const clampedPos = clampPosition(nextPosition, extent)
198199

200+
let computedPos = clampedPos
201+
202+
if (parentNode && (currentExtent === 'parent' || (!Array.isArray(currentExtent) && currentExtent?.range === 'parent'))) {
203+
console.log('clamp to parent')
204+
computedPos = clampPositionToParent(clampedPos, node.dimensions, parentNode)
205+
}
206+
199207
return {
200208
position: {
201209
x: clampedPos.x - (parentNode?.computedPosition.x || 0),
202210
y: clampedPos.y - (parentNode?.computedPosition.y || 0),
203211
},
204-
computedPosition: clampedPos,
212+
computedPosition: computedPos,
205213
}
206214
}

Diff for: packages/core/src/utils/general.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import type { GraphNode, SnapGrid, XYPosition } from '../types'
21
import type { UseDragEvent } from '../composables'
3-
import type { Dimensions, GraphNode, XYPosition } from '../types'
2+
import type { Dimensions, GraphNode, SnapGrid, XYPosition } from '../types'
43
import { clampPosition } from './graph'
54

65
export function isMouseEvent(event: MouseEvent | TouchEvent): event is MouseEvent {

Diff for: packages/core/src/utils/graph.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ export function clamp(val: number, min = 0, max = 1) {
4949
return Math.min(Math.max(val, min), max)
5050
}
5151

52-
export function clampPosition(position: XYPosition = { x: 0, y: 0 }, extent: CoordinateExtent, dimensions: Partial<Dimensions>) {
52+
export function clampPosition(position: XYPosition = { x: 0, y: 0 }, extent: CoordinateExtent, dimensions?: Partial<Dimensions>) {
5353
return {
5454
x: clamp(position.x, extent[0][0], extent[1][0] - (dimensions?.width ?? 0)),
5555
y: clamp(position.y, extent[0][1], extent[1][1] - (dimensions?.height ?? 0)),

0 commit comments

Comments
 (0)