fix(core): use all handles in radius and find closest valid one

This commit is contained in:
braks
2023-05-25 20:48:26 +02:00
committed by Braks
parent 4336ff7578
commit 1690891804
2 changed files with 38 additions and 27 deletions
+36 -18
View File
@@ -1,7 +1,7 @@
import type { MaybeRefOrGetter } from '@vueuse/core' import type { MaybeRefOrGetter } from '@vueuse/core'
import { toValue } from '@vueuse/core' import { toValue } from '@vueuse/core'
import { useVueFlow } from './useVueFlow' import { useVueFlow } from './useVueFlow'
import type { Connection, ConnectionHandle, HandleType, MouseTouchEvent, ValidConnectionFunc } from '~/types' import type { Connection, ConnectionHandle, HandleType, MouseTouchEvent, ValidConnectionFunc, ValidHandleResult } from '~/types'
import { import {
calcAutoPan, calcAutoPan,
getClosestHandle, getClosestHandle,
@@ -135,29 +135,47 @@ export function useHandle({
function onPointerMove(event: MouseTouchEvent) { function onPointerMove(event: MouseTouchEvent) {
connectionPosition = getEventPosition(event, containerBounds) connectionPosition = getEventPosition(event, containerBounds)
closestHandle = getClosestHandle( let result: ValidHandleResult | null = {
pointToRendererPoint(connectionPosition, viewport.value, false, [1, 1]), handleDomNode: null,
connectionRadius.value, isValid: false,
handleLookup, connection: { source: '', target: '', sourceHandle: null, targetHandle: null },
) endHandle: null,
}
closestHandle =
getClosestHandle(
pointToRendererPoint(connectionPosition, viewport.value, false, [1, 1]),
connectionRadius.value,
handleLookup,
)?.find((handle) => {
const validHandleResult = isValidHandle(
event,
handle,
connectionMode.value,
toValue(nodeId),
toValue(handleId),
isTarget ? 'target' : 'source',
isValidConnectionHandler,
doc,
edges.value,
findNode,
)
if (validHandleResult.isValid) {
result = validHandleResult
}
return validHandleResult.isValid
}) || null
if (!autoPanStarted) { if (!autoPanStarted) {
autoPan() autoPan()
autoPanStarted = true autoPanStarted = true
} }
const result = isValidHandle( if (!result) {
event, return
closestHandle, }
connectionMode.value,
toValue(nodeId),
toValue(handleId),
isTarget ? 'target' : 'source',
isValidConnectionHandler,
doc,
edges.value,
findNode,
)
connection = result.connection connection = result.connection
isValid = result.isValid isValid = result.isValid
+2 -9
View File
@@ -53,11 +53,7 @@ export function getHandles(
}, []) }, [])
} }
export function getClosestHandle( export function getClosestHandle(pos: XYPosition, connectionRadius: number, handles: ConnectionHandle[]) {
pos: XYPosition,
connectionRadius: number,
handles: ConnectionHandle[],
): ConnectionHandle | null {
let closestHandles: ConnectionHandle[] = [] let closestHandles: ConnectionHandle[] = []
let minDistance = Infinity let minDistance = Infinity
@@ -79,10 +75,7 @@ export function getClosestHandle(
return null return null
} }
return closestHandles.length === 1 return closestHandles
? closestHandles[0]
: // 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
closestHandles.find((handle) => handle.type === 'target') || closestHandles[0]
} }
// checks if and returns connection in fom of an object { source: 123, target: 312 } // checks if and returns connection in fom of an object { source: 123, target: 312 }