chore(core): cleanup handle utils (#1497)

This commit is contained in:
Braks
2024-07-01 09:44:55 +02:00
parent 09e222124c
commit 8454dca6d4
5 changed files with 30 additions and 57 deletions
@@ -74,7 +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(fromPosition, fromNode.value, fromHandle) const { x: fromX, y: fromY } = getHandlePosition(fromNode.value, fromHandle, fromPosition)
let toHandle: HandleElement | null = null let toHandle: HandleElement | null = null
if (toNode.value && connectionEndHandle.value?.handleId) { if (toNode.value && connectionEndHandle.value?.handleId) {
@@ -8,8 +8,8 @@ import {
ErrorCode, ErrorCode,
VueFlowError, VueFlowError,
elementSelectionKeys, elementSelectionKeys,
getEdgePositions,
getHandle, getHandle,
getHandlePosition,
getMarkerId, getMarkerId,
} from '../../utils' } from '../../utils'
import EdgeAnchor from './EdgeAnchor' import EdgeAnchor from './EdgeAnchor'
@@ -160,19 +160,14 @@ const EdgeWrapper = defineComponent({
const targetHandle = getHandle(targetNodeHandles, edge.value.targetHandle) const targetHandle = getHandle(targetNodeHandles, edge.value.targetHandle)
const sourcePosition = sourceHandle ? sourceHandle.position : Position.Bottom const sourcePosition = sourceHandle?.position || Position.Bottom
const targetPosition = targetHandle ? targetHandle.position : Position.Top const targetPosition = targetHandle?.position || Position.Top
const { sourceX, sourceY, targetY, targetX } = getEdgePositions( const { x: sourceX, y: sourceY } = getHandlePosition(sourceNode, sourceHandle, sourcePosition)
sourceNode, const { x: targetX, y: targetY } = getHandlePosition(targetNode, targetHandle, targetPosition)
sourceHandle,
sourcePosition,
targetNode,
targetHandle,
targetPosition,
)
// todo: let's avoid writing these here (in v2 we want to remove all of these self-managed refs)
edge.value.sourceX = sourceX edge.value.sourceX = sourceX
edge.value.sourceY = sourceY edge.value.sourceY = sourceY
edge.value.targetX = targetX edge.value.targetX = targetX
+1 -3
View File
@@ -10,12 +10,10 @@ export interface HandleElement extends XYPosition, Dimensions {
position: Position position: Position
} }
export interface ConnectionHandle { export interface ConnectionHandle extends XYPosition {
id: string | null id: string | null
type: HandleType | null type: HandleType | null
nodeId: string nodeId: string
x: number
y: number
} }
export interface ValidHandleResult { export interface ValidHandleResult {
+9 -24
View File
@@ -1,12 +1,16 @@
import type { Actions, EdgePositions, GraphEdge, GraphNode, HandleElement, ViewportTransform, XYPosition } from '../types' import type { Actions, GraphEdge, GraphNode, HandleElement, ViewportTransform, XYPosition } from '../types'
import { Position } from '../types' import { Position } from '../types'
import { rectToBox } from '.' import { getNodeDimensions, rectToBox } from '.'
export function getHandlePosition(position: Position, node: GraphNode, handle: HandleElement | null): XYPosition { export function getHandlePosition(
node: GraphNode,
handle: HandleElement | null,
fallbackPosition: Position = Position.Left,
): XYPosition {
const x = (handle?.x ?? 0) + node.computedPosition.x const x = (handle?.x ?? 0) + node.computedPosition.x
const y = (handle?.y ?? 0) + node.computedPosition.y const y = (handle?.y ?? 0) + node.computedPosition.y
const width = handle?.width ?? node.dimensions.width const { width, height } = handle ?? getNodeDimensions(node)
const height = handle?.height ?? node.dimensions.height const position = handle?.position ?? fallbackPosition
switch (position) { switch (position) {
case Position.Top: case Position.Top:
@@ -40,25 +44,6 @@ export function getHandle(bounds: HandleElement[] = [], handleId?: string | null
return (!handleId ? bounds[0] : bounds.find((d) => d.id === handleId)) || null return (!handleId ? bounds[0] : bounds.find((d) => d.id === handleId)) || null
} }
export function getEdgePositions(
sourceNode: GraphNode,
sourceHandle: HandleElement | null,
sourcePosition: Position,
targetNode: GraphNode,
targetHandle: HandleElement | null,
targetPosition: Position,
): EdgePositions {
const { x: sourceX, y: sourceY } = getHandlePosition(sourcePosition, sourceNode, sourceHandle)
const { x: targetX, y: targetY } = getHandlePosition(targetPosition, targetNode, targetHandle)
return {
sourceX,
sourceY,
targetX,
targetY,
}
}
interface IsEdgeVisibleParams { interface IsEdgeVisibleParams {
sourcePos: XYPosition sourcePos: XYPosition
targetPos: XYPosition targetPos: XYPosition
+13 -18
View File
@@ -2,8 +2,8 @@ import { ConnectionMode } from '../types'
import type { import type {
Actions, Actions,
Connection, Connection,
ConnectionHandle,
ConnectionStatus, ConnectionStatus,
Dimensions,
GraphEdge, GraphEdge,
GraphNode, GraphNode,
HandleType, HandleType,
@@ -14,12 +14,6 @@ import type {
} from '../types' } from '../types'
import { getEventPosition, getHandlePosition } from '.' import { getEventPosition, getHandlePosition } from '.'
export interface ConnectionHandle extends XYPosition, Dimensions {
id: string | null
type: HandleType | null
nodeId: string
}
function defaultValidHandleResult(): ValidHandleResult { function defaultValidHandleResult(): ValidHandleResult {
return { return {
handleDomNode: null, handleDomNode: null,
@@ -41,22 +35,23 @@ export function getHandles(
type: HandleType, type: HandleType,
currentHandle: string, currentHandle: string,
): ConnectionHandle[] { ): ConnectionHandle[] {
return (handleBounds[type] || []).reduce<ConnectionHandle[]>((res, h) => { const connectionHandles: ConnectionHandle[] = []
if (`${node.id}-${h.id}-${type}` !== currentHandle) {
const handlePosition = getHandlePosition(h.position, node, h)
res.push({ for (const handle of handleBounds[type] || []) {
id: h.id || null, if (`${node.id}-${handle.id}-${type}` !== currentHandle) {
const { x, y } = getHandlePosition(node, handle)
connectionHandles.push({
id: handle.id || null,
type, type,
nodeId: node.id, nodeId: node.id,
x: handlePosition.x, x,
y: handlePosition.y, y,
width: h.width,
height: h.height,
}) })
} }
return res }
}, [])
return connectionHandles
} }
export function getClosestHandle( export function getClosestHandle(