@@ -4,6 +4,7 @@ import type {
4
4
Actions ,
5
5
Connection ,
6
6
ConnectionStatus ,
7
+ Dimensions ,
7
8
GraphEdge ,
8
9
GraphNode ,
9
10
HandleType ,
@@ -13,12 +14,10 @@ import type {
13
14
XYPosition ,
14
15
} from '~/types'
15
16
16
- export interface ConnectionHandle {
17
+ export interface ConnectionHandle extends XYPosition , Dimensions {
17
18
id : string | null
18
19
type : HandleType
19
20
nodeId : string
20
- x : number
21
- y : number
22
21
}
23
22
24
23
function defaultValidHandleResult ( ) : ValidHandleResult {
@@ -50,6 +49,8 @@ export function getHandles(
50
49
nodeId : node . id ,
51
50
x : ( node . computedPosition ?. x ?? 0 ) + h . x + h . width / 2 ,
52
51
y : ( node . computedPosition ?. y ?? 0 ) + h . y + h . height / 2 ,
52
+ width : h . width ,
53
+ height : h . height ,
53
54
} )
54
55
}
55
56
return res
@@ -66,7 +67,8 @@ export function getClosestHandle(
66
67
let minDistance = Infinity
67
68
68
69
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 )
70
72
71
73
if ( distance <= connectionRadius ) {
72
74
const validHandleResult = validator ( handle )
@@ -147,13 +149,13 @@ export function isValidHandle(
147
149
? ( isTarget && handleType === 'source' ) || ( ! isTarget && handleType === 'target' )
148
150
: handleNodeId !== fromNodeId || handleId !== fromHandleId )
149
151
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
+ }
156
157
158
+ if ( isValid ) {
157
159
result . isValid = isValidConnection ( connection , {
158
160
edges,
159
161
sourceNode : findNode ( connection . source ) ! ,
0 commit comments