Skip to content

Commit 1690891

Browse files
committed
fix(core): use all handles in radius and find closest valid one
1 parent 4336ff7 commit 1690891

File tree

2 files changed

+38
-27
lines changed

2 files changed

+38
-27
lines changed

packages/core/src/composables/useHandle.ts

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { MaybeRefOrGetter } from '@vueuse/core'
22
import { toValue } from '@vueuse/core'
33
import { useVueFlow } from './useVueFlow'
4-
import type { Connection, ConnectionHandle, HandleType, MouseTouchEvent, ValidConnectionFunc } from '~/types'
4+
import type { Connection, ConnectionHandle, HandleType, MouseTouchEvent, ValidConnectionFunc, ValidHandleResult } from '~/types'
55
import {
66
calcAutoPan,
77
getClosestHandle,
@@ -135,29 +135,47 @@ export function useHandle({
135135
function onPointerMove(event: MouseTouchEvent) {
136136
connectionPosition = getEventPosition(event, containerBounds)
137137

138-
closestHandle = getClosestHandle(
139-
pointToRendererPoint(connectionPosition, viewport.value, false, [1, 1]),
140-
connectionRadius.value,
141-
handleLookup,
142-
)
138+
let result: ValidHandleResult | null = {
139+
handleDomNode: null,
140+
isValid: false,
141+
connection: { source: '', target: '', sourceHandle: null, targetHandle: null },
142+
endHandle: null,
143+
}
144+
145+
closestHandle =
146+
getClosestHandle(
147+
pointToRendererPoint(connectionPosition, viewport.value, false, [1, 1]),
148+
connectionRadius.value,
149+
handleLookup,
150+
)?.find((handle) => {
151+
const validHandleResult = isValidHandle(
152+
event,
153+
handle,
154+
connectionMode.value,
155+
toValue(nodeId),
156+
toValue(handleId),
157+
isTarget ? 'target' : 'source',
158+
isValidConnectionHandler,
159+
doc,
160+
edges.value,
161+
findNode,
162+
)
163+
164+
if (validHandleResult.isValid) {
165+
result = validHandleResult
166+
}
167+
168+
return validHandleResult.isValid
169+
}) || null
143170

144171
if (!autoPanStarted) {
145172
autoPan()
146173
autoPanStarted = true
147174
}
148175

149-
const result = isValidHandle(
150-
event,
151-
closestHandle,
152-
connectionMode.value,
153-
toValue(nodeId),
154-
toValue(handleId),
155-
isTarget ? 'target' : 'source',
156-
isValidConnectionHandler,
157-
doc,
158-
edges.value,
159-
findNode,
160-
)
176+
if (!result) {
177+
return
178+
}
161179

162180
connection = result.connection
163181
isValid = result.isValid

packages/core/src/utils/handle.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,7 @@ export function getHandles(
5353
}, [])
5454
}
5555

56-
export function getClosestHandle(
57-
pos: XYPosition,
58-
connectionRadius: number,
59-
handles: ConnectionHandle[],
60-
): ConnectionHandle | null {
56+
export function getClosestHandle(pos: XYPosition, connectionRadius: number, handles: ConnectionHandle[]) {
6157
let closestHandles: ConnectionHandle[] = []
6258
let minDistance = Infinity
6359

@@ -79,10 +75,7 @@ export function getClosestHandle(
7975
return null
8076
}
8177

82-
return closestHandles.length === 1
83-
? closestHandles[0]
84-
: // if multiple handles are layout on top of each other we take the one with type = target because it's more likely that the user wants to connect to this one
85-
closestHandles.find((handle) => handle.type === 'target') || closestHandles[0]
78+
return closestHandles
8679
}
8780

8881
// checks if and returns connection in fom of an object { source: 123, target: 312 }

0 commit comments

Comments
 (0)