Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 09c32c5

Browse files
authoredJun 19, 2024··
refactor(core): calculate correct handle pos in handle lookup (#1494)
* refactor(core): use `getHandlePosition` to calculate handle pos * chore(changeset): add
1 parent 2dff0dd commit 09c32c5

File tree

5 files changed

+32
-34
lines changed

5 files changed

+32
-34
lines changed
 

‎.changeset/giant-mayflies-study.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@vue-flow/core": patch
3+
---
4+
5+
Calculate correct handle position in handle lookup

‎packages/core/src/components/ConnectionLine/index.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,7 @@ const ConnectionLine = defineComponent({
7474

7575
const fromHandle = (startHandleId ? handleBounds.find((d) => d.id === startHandleId) : handleBounds[0]) ?? null
7676
const fromPosition = fromHandle?.position || Position.Top
77-
const { x: fromX, y: fromY } = getHandlePosition(
78-
fromPosition,
79-
{ ...fromNode.value.dimensions, ...fromNode.value.computedPosition },
80-
fromHandle,
81-
)
77+
const { x: fromX, y: fromY } = getHandlePosition(fromPosition, fromNode.value, fromHandle)
8278

8379
let toHandle: HandleElement | null = null
8480
if (toNode.value && connectionEndHandle.value?.handleId) {

‎packages/core/src/utils/edge.ts

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import type { Actions, EdgePositions, GraphEdge, GraphNode, HandleElement, Rect, ViewportTransform, XYPosition } from '../types'
1+
import type { Actions, EdgePositions, GraphEdge, GraphNode, HandleElement, ViewportTransform, XYPosition } from '../types'
22
import { Position } from '../types'
33
import { rectToBox } from '.'
44

5-
export function getHandlePosition(position: Position, rect: Rect, handle: HandleElement | null): XYPosition {
6-
const x = (handle?.x ?? 0) + rect.x
7-
const y = (handle?.y ?? 0) + rect.y
8-
const width = handle?.width ?? rect.width
9-
const height = handle?.height ?? rect.height
5+
export function getHandlePosition(position: Position, node: GraphNode, handle: HandleElement | null): XYPosition {
6+
const x = (handle?.x ?? 0) + node.computedPosition.x
7+
const y = (handle?.y ?? 0) + node.computedPosition.y
8+
const width = handle?.width ?? node.dimensions.width
9+
const height = handle?.height ?? node.dimensions.height
1010

1111
switch (position) {
1212
case Position.Top:
@@ -48,28 +48,14 @@ export function getEdgePositions(
4848
targetHandle: HandleElement | null,
4949
targetPosition: Position,
5050
): EdgePositions {
51-
const sourceHandlePos = getHandlePosition(
52-
sourcePosition,
53-
{
54-
...sourceNode.dimensions,
55-
...sourceNode.computedPosition,
56-
},
57-
sourceHandle,
58-
)
59-
const targetHandlePos = getHandlePosition(
60-
targetPosition,
61-
{
62-
...targetNode.dimensions,
63-
...targetNode.computedPosition,
64-
},
65-
targetHandle,
66-
)
51+
const { x: sourceX, y: sourceY } = getHandlePosition(sourcePosition, sourceNode, sourceHandle)
52+
const { x: targetX, y: targetY } = getHandlePosition(targetPosition, targetNode, targetHandle)
6753

6854
return {
69-
sourceX: sourceHandlePos.x,
70-
sourceY: sourceHandlePos.y,
71-
targetX: targetHandlePos.x,
72-
targetY: targetHandlePos.y,
55+
sourceX,
56+
sourceY,
57+
targetX,
58+
targetY,
7359
}
7460
}
7561

‎packages/core/src/utils/general.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import type { GraphNode } from '../types'
2+
13
export function isMouseEvent(event: MouseEvent | TouchEvent): event is MouseEvent {
24
return 'clientX' in event
35
}
@@ -14,3 +16,10 @@ export function getEventPosition(event: MouseEvent | TouchEvent, bounds?: DOMRec
1416
}
1517

1618
export const isMacOs = () => typeof navigator !== 'undefined' && navigator?.userAgent?.indexOf('Mac') >= 0
19+
20+
export function getNodeDimensions(node: GraphNode): { width: number; height: number } {
21+
return {
22+
width: node.dimensions?.width ?? node.width ?? 0,
23+
height: node.dimensions?.height ?? node.height ?? 0,
24+
}
25+
}

‎packages/core/src/utils/handle.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import type {
1212
ValidHandleResult,
1313
XYPosition,
1414
} from '../types'
15-
import { getEventPosition } from '.'
15+
import { getEventPosition, getHandlePosition } from '.'
1616

1717
export interface ConnectionHandle extends XYPosition, Dimensions {
1818
id: string | null
@@ -43,12 +43,14 @@ export function getHandles(
4343
): ConnectionHandle[] {
4444
return (handleBounds[type] || []).reduce<ConnectionHandle[]>((res, h) => {
4545
if (`${node.id}-${h.id}-${type}` !== currentHandle) {
46+
const handlePosition = getHandlePosition(h.position, node, h)
47+
4648
res.push({
4749
id: h.id || null,
4850
type,
4951
nodeId: node.id,
50-
x: (node.computedPosition?.x ?? 0) + h.x + h.width / 2,
51-
y: (node.computedPosition?.y ?? 0) + h.y + h.height / 2,
52+
x: handlePosition.x,
53+
y: handlePosition.y,
5254
width: h.width,
5355
height: h.height,
5456
})

0 commit comments

Comments
 (0)
Please sign in to comment.