feat(system): add handles and dimensions for ssr

This commit is contained in:
moklick
2023-10-05 17:26:59 +02:00
parent 86c041c2a0
commit 1c791811a9
3 changed files with 47 additions and 15 deletions

View File

@@ -1,12 +1,16 @@
import type { XYPosition, Position, Dimensions, OnConnect, IsValidConnection } from '.';
import type { Position, OnConnect, IsValidConnection } from '.';
export type HandleType = 'source' | 'target';
export type HandleElement = XYPosition &
Dimensions & {
id?: string | null;
position: Position;
};
export type HandleElement = {
id?: string | null;
x: number;
y: number;
width: number;
height: number;
position: Position;
type?: HandleType;
};
export type ConnectingHandle = {
nodeId: string;

View File

@@ -1,6 +1,6 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { internalsSymbol } from '../constants';
import type { XYPosition, Position, CoordinateExtent, HandleElement } from '.';
import type { XYPosition, Position, CoordinateExtent, HandleElement, Dimensions } from '.';
// this is stuff that all nodes share independent of the framework
export type NodeBase<T = any, U extends string | undefined = string | undefined> = {
@@ -28,6 +28,8 @@ export type NodeBase<T = any, U extends string | undefined = string | undefined>
ariaLabel?: string;
focusable?: boolean;
origin?: NodeOrigin;
handles?: HandleElement[];
dimensions?: Dimensions;
// only used internally
[internalsSymbol]?: {

View File

@@ -16,10 +16,10 @@ export type GetEdgePositionParams = {
};
export function getEdgePosition(params: GetEdgePositionParams): EdgePosition | null {
const [sourceNodeRect, sourceHandleBounds, sourceIsValid] = getHandleDataByNode(params.sourceNode);
const [targetNodeRect, targetHandleBounds, targetIsValid] = getHandleDataByNode(params.targetNode);
const [sourceNodeRect, sourceHandleBounds, isSourceValid] = getHandleDataByNode(params.sourceNode);
const [targetNodeRect, targetHandleBounds, isTargetValid] = getHandleDataByNode(params.targetNode);
if (!sourceIsValid || !targetIsValid) {
if (!isSourceValid || !isTargetValid) {
return null;
}
@@ -59,13 +59,39 @@ export function getEdgePosition(params: GetEdgePositionParams): EdgePosition | n
};
}
function toHandleBounds(handles?: HandleElement[]) {
if (!handles) {
return null;
}
return handles.reduce<NodeHandleBounds>(
(res, item) => {
if (item.type === 'source') {
res.source?.push(item);
}
if (item.type === 'target') {
res.target?.push(item);
}
return res;
},
{
source: [],
target: [],
}
);
}
function getHandleDataByNode(node?: NodeBase): [Rect, NodeHandleBounds | null, boolean] {
const handleBounds = node?.[internalsSymbol]?.handleBounds || null;
const handleBounds = node?.[internalsSymbol]?.handleBounds || toHandleBounds(node?.handles) || null;
const nodeWidth = node?.width || node?.dimensions?.width;
const nodeHeight = node?.height || node?.dimensions?.height;
const isValid =
handleBounds &&
node?.width &&
node?.height &&
nodeWidth &&
nodeHeight &&
typeof node?.positionAbsolute?.x !== 'undefined' &&
typeof node?.positionAbsolute?.y !== 'undefined';
@@ -73,8 +99,8 @@ function getHandleDataByNode(node?: NodeBase): [Rect, NodeHandleBounds | null, b
{
x: node?.positionAbsolute?.x || 0,
y: node?.positionAbsolute?.y || 0,
width: node?.width || 0,
height: node?.height || 0,
width: nodeWidth || 0,
height: nodeHeight || 0,
},
handleBounds,
!!isValid,