Skip to content

Commit f3931a2

Browse files
committed
fix(core): consider handle dimensions when checking for closest handle
1 parent 82befe7 commit f3931a2

File tree

2 files changed

+14
-12
lines changed

2 files changed

+14
-12
lines changed

packages/core/src/composables/useHandle.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ export function useHandle({
155155
),
156156
)
157157

158-
closestHandle = handle
158+
closestHandle = handle || handleLookup.find((handle) => handle.id === validHandleResult.endHandle?.handleId) || null
159159

160160
if (!autoPanStarted) {
161161
autoPan()
@@ -198,7 +198,7 @@ export function useHandle({
198198
}
199199

200200
function onPointerUp(event: MouseTouchEvent) {
201-
if ((closestHandle || handleDomNode || connectionEndHandle.value) && connection && isValid) {
201+
if ((closestHandle || handleDomNode) && connection && isValid) {
202202
if (!onEdgeUpdate) {
203203
emits.connect(connection)
204204
} else {

packages/core/src/utils/handle.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import type {
44
Actions,
55
Connection,
66
ConnectionStatus,
7+
Dimensions,
78
GraphEdge,
89
GraphNode,
910
HandleType,
@@ -13,12 +14,10 @@ import type {
1314
XYPosition,
1415
} from '~/types'
1516

16-
export interface ConnectionHandle {
17+
export interface ConnectionHandle extends XYPosition, Dimensions {
1718
id: string | null
1819
type: HandleType
1920
nodeId: string
20-
x: number
21-
y: number
2221
}
2322

2423
function defaultValidHandleResult(): ValidHandleResult {
@@ -50,6 +49,8 @@ export function getHandles(
5049
nodeId: node.id,
5150
x: (node.computedPosition?.x ?? 0) + h.x + h.width / 2,
5251
y: (node.computedPosition?.y ?? 0) + h.y + h.height / 2,
52+
width: h.width,
53+
height: h.height,
5354
})
5455
}
5556
return res
@@ -66,7 +67,8 @@ export function getClosestHandle(
6667
let minDistance = Infinity
6768

6869
handles.forEach((handle) => {
69-
const distance = Math.sqrt((handle.x - pos.x) ** 2 + (handle.y - pos.y) ** 2)
70+
// calculate distance from mouse position to center of handle while considering handle width and height as well as x and y position
71+
const distance = Math.sqrt((handle.x - pos.x - handle.width / 2) ** 2 + (handle.y - pos.y - handle.height / 2) ** 2)
7072

7173
if (distance <= connectionRadius) {
7274
const validHandleResult = validator(handle)
@@ -147,13 +149,13 @@ export function isValidHandle(
147149
? (isTarget && handleType === 'source') || (!isTarget && handleType === 'target')
148150
: handleNodeId !== fromNodeId || handleId !== fromHandleId)
149151

150-
if (isValid) {
151-
result.endHandle = {
152-
nodeId: handleNodeId,
153-
handleId,
154-
type: handleType as HandleType,
155-
}
152+
result.endHandle = {
153+
nodeId: handleNodeId,
154+
handleId,
155+
type: handleType as HandleType,
156+
}
156157

158+
if (isValid) {
157159
result.isValid = isValidConnection(connection, {
158160
edges,
159161
sourceNode: findNode(connection.source)!,

0 commit comments

Comments
 (0)