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
+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 { 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 y = (handle?.y ?? 0) + node.computedPosition.y
const width = handle?.width ?? node.dimensions.width
const height = handle?.height ?? node.dimensions.height
const { width, height } = handle ?? getNodeDimensions(node)
const position = handle?.position ?? fallbackPosition
switch (position) {
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
}
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 {
sourcePos: XYPosition
targetPos: XYPosition
+13 -18
View File
@@ -2,8 +2,8 @@ import { ConnectionMode } from '../types'
import type {
Actions,
Connection,
ConnectionHandle,
ConnectionStatus,
Dimensions,
GraphEdge,
GraphNode,
HandleType,
@@ -14,12 +14,6 @@ import type {
} from '../types'
import { getEventPosition, getHandlePosition } from '.'
export interface ConnectionHandle extends XYPosition, Dimensions {
id: string | null
type: HandleType | null
nodeId: string
}
function defaultValidHandleResult(): ValidHandleResult {
return {
handleDomNode: null,
@@ -41,22 +35,23 @@ export function getHandles(
type: HandleType,
currentHandle: string,
): ConnectionHandle[] {
return (handleBounds[type] || []).reduce<ConnectionHandle[]>((res, h) => {
if (`${node.id}-${h.id}-${type}` !== currentHandle) {
const handlePosition = getHandlePosition(h.position, node, h)
const connectionHandles: ConnectionHandle[] = []
res.push({
id: h.id || null,
for (const handle of handleBounds[type] || []) {
if (`${node.id}-${handle.id}-${type}` !== currentHandle) {
const { x, y } = getHandlePosition(node, handle)
connectionHandles.push({
id: handle.id || null,
type,
nodeId: node.id,
x: handlePosition.x,
y: handlePosition.y,
width: h.width,
height: h.height,
x,
y,
})
}
return res
}, [])
}
return connectionHandles
}
export function getClosestHandle(