fix(core): consider handle dimensions when checking for closest handle

This commit is contained in:
braks
2023-06-15 22:35:25 +02:00
committed by Braks
parent 82befe722a
commit f3931a2c05
2 changed files with 14 additions and 12 deletions
+2 -2
View File
@@ -155,7 +155,7 @@ export function useHandle({
), ),
) )
closestHandle = handle closestHandle = handle || handleLookup.find((handle) => handle.id === validHandleResult.endHandle?.handleId) || null
if (!autoPanStarted) { if (!autoPanStarted) {
autoPan() autoPan()
@@ -198,7 +198,7 @@ export function useHandle({
} }
function onPointerUp(event: MouseTouchEvent) { function onPointerUp(event: MouseTouchEvent) {
if ((closestHandle || handleDomNode || connectionEndHandle.value) && connection && isValid) { if ((closestHandle || handleDomNode) && connection && isValid) {
if (!onEdgeUpdate) { if (!onEdgeUpdate) {
emits.connect(connection) emits.connect(connection)
} else { } else {
+12 -10
View File
@@ -4,6 +4,7 @@ import type {
Actions, Actions,
Connection, Connection,
ConnectionStatus, ConnectionStatus,
Dimensions,
GraphEdge, GraphEdge,
GraphNode, GraphNode,
HandleType, HandleType,
@@ -13,12 +14,10 @@ import type {
XYPosition, XYPosition,
} from '~/types' } from '~/types'
export interface ConnectionHandle { export interface ConnectionHandle extends XYPosition, Dimensions {
id: string | null id: string | null
type: HandleType type: HandleType
nodeId: string nodeId: string
x: number
y: number
} }
function defaultValidHandleResult(): ValidHandleResult { function defaultValidHandleResult(): ValidHandleResult {
@@ -50,6 +49,8 @@ export function getHandles(
nodeId: node.id, nodeId: node.id,
x: (node.computedPosition?.x ?? 0) + h.x + h.width / 2, x: (node.computedPosition?.x ?? 0) + h.x + h.width / 2,
y: (node.computedPosition?.y ?? 0) + h.y + h.height / 2, y: (node.computedPosition?.y ?? 0) + h.y + h.height / 2,
width: h.width,
height: h.height,
}) })
} }
return res return res
@@ -66,7 +67,8 @@ export function getClosestHandle(
let minDistance = Infinity let minDistance = Infinity
handles.forEach((handle) => { handles.forEach((handle) => {
const distance = Math.sqrt((handle.x - pos.x) ** 2 + (handle.y - pos.y) ** 2) // calculate distance from mouse position to center of handle while considering handle width and height as well as x and y position
const distance = Math.sqrt((handle.x - pos.x - handle.width / 2) ** 2 + (handle.y - pos.y - handle.height / 2) ** 2)
if (distance <= connectionRadius) { if (distance <= connectionRadius) {
const validHandleResult = validator(handle) const validHandleResult = validator(handle)
@@ -147,13 +149,13 @@ export function isValidHandle(
? (isTarget && handleType === 'source') || (!isTarget && handleType === 'target') ? (isTarget && handleType === 'source') || (!isTarget && handleType === 'target')
: handleNodeId !== fromNodeId || handleId !== fromHandleId) : handleNodeId !== fromNodeId || handleId !== fromHandleId)
if (isValid) { result.endHandle = {
result.endHandle = { nodeId: handleNodeId,
nodeId: handleNodeId, handleId,
handleId, type: handleType as HandleType,
type: handleType as HandleType, }
}
if (isValid) {
result.isValid = isValidConnection(connection, { result.isValid = isValidConnection(connection, {
edges, edges,
sourceNode: findNode(connection.source)!, sourceNode: findNode(connection.source)!,