refactor(core): calculate correct handle pos in handle lookup (#1494)

* refactor(core): use `getHandlePosition` to calculate handle pos

* chore(changeset): add
This commit is contained in:
Braks
2024-07-01 09:44:55 +02:00
parent e435485b77
commit 09e222124c
5 changed files with 32 additions and 34 deletions
+5
View File
@@ -0,0 +1,5 @@
---
"@vue-flow/core": patch
---
Calculate correct handle position in handle lookup
@@ -74,11 +74,7 @@ const ConnectionLine = defineComponent({
const fromHandle = (startHandleId ? handleBounds.find((d) => d.id === startHandleId) : handleBounds[0]) ?? null const fromHandle = (startHandleId ? handleBounds.find((d) => d.id === startHandleId) : handleBounds[0]) ?? null
const fromPosition = fromHandle?.position || Position.Top const fromPosition = fromHandle?.position || Position.Top
const { x: fromX, y: fromY } = getHandlePosition( const { x: fromX, y: fromY } = getHandlePosition(fromPosition, fromNode.value, fromHandle)
fromPosition,
{ ...fromNode.value.dimensions, ...fromNode.value.computedPosition },
fromHandle,
)
let toHandle: HandleElement | null = null let toHandle: HandleElement | null = null
if (toNode.value && connectionEndHandle.value?.handleId) { if (toNode.value && connectionEndHandle.value?.handleId) {
+12 -26
View File
@@ -1,12 +1,12 @@
import type { Actions, EdgePositions, GraphEdge, GraphNode, HandleElement, Rect, ViewportTransform, XYPosition } from '../types' import type { Actions, EdgePositions, GraphEdge, GraphNode, HandleElement, ViewportTransform, XYPosition } from '../types'
import { Position } from '../types' import { Position } from '../types'
import { rectToBox } from '.' import { rectToBox } from '.'
export function getHandlePosition(position: Position, rect: Rect, handle: HandleElement | null): XYPosition { export function getHandlePosition(position: Position, node: GraphNode, handle: HandleElement | null): XYPosition {
const x = (handle?.x ?? 0) + rect.x const x = (handle?.x ?? 0) + node.computedPosition.x
const y = (handle?.y ?? 0) + rect.y const y = (handle?.y ?? 0) + node.computedPosition.y
const width = handle?.width ?? rect.width const width = handle?.width ?? node.dimensions.width
const height = handle?.height ?? rect.height const height = handle?.height ?? node.dimensions.height
switch (position) { switch (position) {
case Position.Top: case Position.Top:
@@ -48,28 +48,14 @@ export function getEdgePositions(
targetHandle: HandleElement | null, targetHandle: HandleElement | null,
targetPosition: Position, targetPosition: Position,
): EdgePositions { ): EdgePositions {
const sourceHandlePos = getHandlePosition( const { x: sourceX, y: sourceY } = getHandlePosition(sourcePosition, sourceNode, sourceHandle)
sourcePosition, const { x: targetX, y: targetY } = getHandlePosition(targetPosition, targetNode, targetHandle)
{
...sourceNode.dimensions,
...sourceNode.computedPosition,
},
sourceHandle,
)
const targetHandlePos = getHandlePosition(
targetPosition,
{
...targetNode.dimensions,
...targetNode.computedPosition,
},
targetHandle,
)
return { return {
sourceX: sourceHandlePos.x, sourceX,
sourceY: sourceHandlePos.y, sourceY,
targetX: targetHandlePos.x, targetX,
targetY: targetHandlePos.y, targetY,
} }
} }
+9
View File
@@ -1,3 +1,5 @@
import type { GraphNode } from '../types'
export function isMouseEvent(event: MouseEvent | TouchEvent): event is MouseEvent { export function isMouseEvent(event: MouseEvent | TouchEvent): event is MouseEvent {
return 'clientX' in event return 'clientX' in event
} }
@@ -14,3 +16,10 @@ export function getEventPosition(event: MouseEvent | TouchEvent, bounds?: DOMRec
} }
export const isMacOs = () => typeof navigator !== 'undefined' && navigator?.userAgent?.indexOf('Mac') >= 0 export const isMacOs = () => typeof navigator !== 'undefined' && navigator?.userAgent?.indexOf('Mac') >= 0
export function getNodeDimensions(node: GraphNode): { width: number; height: number } {
return {
width: node.dimensions?.width ?? node.width ?? 0,
height: node.dimensions?.height ?? node.height ?? 0,
}
}
+5 -3
View File
@@ -12,7 +12,7 @@ import type {
ValidHandleResult, ValidHandleResult,
XYPosition, XYPosition,
} from '../types' } from '../types'
import { getEventPosition } from '.' import { getEventPosition, getHandlePosition } from '.'
export interface ConnectionHandle extends XYPosition, Dimensions { export interface ConnectionHandle extends XYPosition, Dimensions {
id: string | null id: string | null
@@ -43,12 +43,14 @@ export function getHandles(
): ConnectionHandle[] { ): ConnectionHandle[] {
return (handleBounds[type] || []).reduce<ConnectionHandle[]>((res, h) => { return (handleBounds[type] || []).reduce<ConnectionHandle[]>((res, h) => {
if (`${node.id}-${h.id}-${type}` !== currentHandle) { if (`${node.id}-${h.id}-${type}` !== currentHandle) {
const handlePosition = getHandlePosition(h.position, node, h)
res.push({ res.push({
id: h.id || null, id: h.id || null,
type, type,
nodeId: node.id, nodeId: node.id,
x: (node.computedPosition?.x ?? 0) + h.x + h.width / 2, x: handlePosition.x,
y: (node.computedPosition?.y ?? 0) + h.y + h.height / 2, y: handlePosition.y,
width: h.width, width: h.width,
height: h.height, height: h.height,
}) })