refactor(types): cleanup edge types

This commit is contained in:
moklick
2023-12-06 15:56:17 +01:00
parent b264c11c54
commit ba8ee0272a
5 changed files with 117 additions and 82 deletions
+2 -1
View File
@@ -20,6 +20,7 @@ import type {
OnError, OnError,
IsValidConnection, IsValidConnection,
ColorMode, ColorMode,
SnapGrid,
} from '@xyflow/system'; } from '@xyflow/system';
import type { import type {
@@ -110,7 +111,7 @@ export type ReactFlowProps = Omit<HTMLAttributes<HTMLDivElement>, 'onError'> & {
multiSelectionKeyCode?: KeyCode | null; multiSelectionKeyCode?: KeyCode | null;
zoomActivationKeyCode?: KeyCode | null; zoomActivationKeyCode?: KeyCode | null;
snapToGrid?: boolean; snapToGrid?: boolean;
snapGrid?: [number, number]; snapGrid?: SnapGrid;
onlyRenderVisibleElements?: boolean; onlyRenderVisibleElements?: boolean;
nodesDraggable?: boolean; nodesDraggable?: boolean;
nodesConnectable?: boolean; nodesConnectable?: boolean;
+39 -24
View File
@@ -13,7 +13,6 @@ import type {
HandleElement, HandleElement,
ConnectionStatus, ConnectionStatus,
EdgePosition, EdgePosition,
Optional,
StepPathOptions, StepPathOptions,
} from '@xyflow/system'; } from '@xyflow/system';
@@ -30,13 +29,13 @@ export type EdgeLabelOptions = {
export type EdgeUpdatable = boolean | HandleType; export type EdgeUpdatable = boolean | HandleType;
export type DefaultEdge<EdgeData = any> = EdgeBase<EdgeData> & { export type DefaultEdge<EdgeData = any> = EdgeBase<EdgeData> &
EdgeLabelOptions & {
style?: CSSProperties; style?: CSSProperties;
className?: string; className?: string;
sourceNode?: Node;
targetNode?: Node;
updatable?: EdgeUpdatable; updatable?: EdgeUpdatable;
} & EdgeLabelOptions; focusable?: boolean;
};
type SmoothStepEdgeType<T> = DefaultEdge<T> & { type SmoothStepEdgeType<T> = DefaultEdge<T> & {
type: 'smoothstep'; type: 'smoothstep';
@@ -53,6 +52,9 @@ type StepEdgeType<T> = DefaultEdge<T> & {
pathOptions?: StepPathOptions; pathOptions?: StepPathOptions;
}; };
/**
* The Edge type is mainly used for the `edges` that get passed to the ReactFlow component.
*/
export type Edge<T = any> = DefaultEdge<T> | SmoothStepEdgeType<T> | BezierEdgeType<T> | StepEdgeType<T>; export type Edge<T = any> = DefaultEdge<T> | SmoothStepEdgeType<T> | BezierEdgeType<T> | StepEdgeType<T>;
export type EdgeMouseHandler = (event: ReactMouseEvent, edge: Edge) => void; export type EdgeMouseHandler = (event: ReactMouseEvent, edge: Edge) => void;
@@ -85,45 +87,58 @@ export type EdgeTextProps = HTMLAttributes<SVGElement> &
y: number; y: number;
}; };
// props that get passed to a custom edge /**
* Custom edge component props.
*/
export type EdgeProps<T = any> = Pick< export type EdgeProps<T = any> = Pick<
Edge<T>, Edge<T>,
'id' | 'animated' | 'data' | 'style' | 'selected' | 'source' | 'target' 'id' | 'animated' | 'data' | 'style' | 'selected' | 'source' | 'target'
> & > &
Pick<WrapEdgeProps, 'sourceHandleId' | 'targetHandleId' | 'interactionWidth'> &
EdgePosition & EdgePosition &
EdgeLabelOptions & { EdgeLabelOptions & {
interactionWidth?: number;
sourceHandleId?: string | null;
targetHandleId?: string | null;
markerStart?: string; markerStart?: string;
markerEnd?: string; markerEnd?: string;
// @TODO: how can we get better types for pathOptions? // @TODO: how can we get better types for pathOptions?
pathOptions?: any; pathOptions?: any;
}; };
export type BaseEdgeProps = Pick<EdgeProps, 'style' | 'markerStart' | 'markerEnd' | 'interactionWidth'> & /**
EdgeLabelOptions & { * BaseEdge component props.
*/
export type BaseEdgeProps = EdgeLabelOptions & {
id?: string; id?: string;
interactionWidth?: number;
labelX?: number; labelX?: number;
labelY?: number; labelY?: number;
markerStart?: string;
markerEnd?: string;
path: string; path: string;
style?: CSSProperties;
};
export type EdgeComponentProps = EdgePosition &
EdgeLabelOptions & {
id?: EdgeProps['id'];
markerStart?: EdgeProps['markerStart'];
markerEnd?: EdgeProps['markerEnd'];
interactionWidth?: EdgeProps['interactionWidth'];
style?: EdgeProps['style'];
sourceHandleId?: EdgeProps['sourceHandleId'];
targetHandleId?: EdgeProps['targetHandleId'];
}; };
export type EdgeComponentProps<T = any> = Optional<Omit<EdgeProps<T>, 'source' | 'target'>, 'id'>; export type EdgeComponentWithPathOptions<PathOptions> = EdgeComponentProps & {
pathOptions?: PathOptions;
export type StraightEdgeProps<T = any> = Omit<EdgeComponentProps<T>, 'sourcePosition' | 'targetPosition'>;
export type SmoothStepEdgeProps<T = any> = EdgeComponentProps<T> & {
pathOptions?: SmoothStepPathOptions;
}; };
export type BezierEdgeProps<T = any> = EdgeComponentProps<T> & { export type BezierEdgeProps = EdgeComponentWithPathOptions<BezierPathOptions>;
pathOptions?: BezierPathOptions; export type SmoothStepEdgeProps = EdgeComponentWithPathOptions<SmoothStepPathOptions>;
}; export type StepEdgeProps = EdgeComponentWithPathOptions<StepPathOptions>;
export type StraightEdgeProps = Omit<EdgeComponentProps, 'sourcePosition' | 'targetPosition'>;
export type StepEdgeProps<T = any> = EdgeComponentProps<T> & { export type SimpleBezierEdgeProps = EdgeComponentProps;
pathOptions?: StepPathOptions;
};
export type SimpleBezierEdgeProps<T = any> = EdgeComponentProps<T>;
export type OnEdgeUpdateFunc<T = any> = (oldEdge: Edge<T>, newConnection: Connection) => void; export type OnEdgeUpdateFunc<T = any> = (oldEdge: Edge<T>, newConnection: Connection) => void;
+18 -3
View File
@@ -1,12 +1,25 @@
import type { CSSProperties, MouseEvent as ReactMouseEvent } from 'react'; import type { CSSProperties, MouseEvent as ReactMouseEvent } from 'react';
import type { NodeBase, XYPosition } from '@xyflow/system'; import type { NodeBase, XYPosition } from '@xyflow/system';
/**
* The node data structure that gets used for the nodes prop.
* @public
*/
export type Node<NodeData = any, NodeType extends string | undefined = string | undefined> = NodeBase< export type Node<NodeData = any, NodeType extends string | undefined = string | undefined> = NodeBase<
NodeData, NodeData,
NodeType NodeType
> & { > & {
/**
* Inline style object
*/
style?: CSSProperties; style?: CSSProperties;
/**
* Inline style object
*/
className?: string; className?: string;
/**
* Inline style object
*/
resizing?: boolean; resizing?: boolean;
}; };
@@ -17,8 +30,10 @@ export type SelectionDragHandler = (event: ReactMouseEvent, nodes: Node[]) => vo
export type WrapNodeProps<NodeData = any> = Pick< export type WrapNodeProps<NodeData = any> = Pick<
Node<NodeData>, Node<NodeData>,
'id' | 'data' | 'style' | 'className' | 'dragHandle' | 'sourcePosition' | 'targetPosition' | 'hidden' | 'ariaLabel' 'id' | 'data' | 'style' | 'className' | 'dragHandle' | 'sourcePosition' | 'targetPosition' | 'hidden' | 'ariaLabel'
> & > & {
Required<Pick<Node<NodeData>, 'selected' | 'type' | 'zIndex'>> & { type: string;
selected: boolean;
zIndex: number;
isConnectable: boolean; isConnectable: boolean;
xPos: number; xPos: number;
yPos: number; yPos: number;
@@ -43,4 +58,4 @@ export type WrapNodeProps<NodeData = any> = Pick<
disableKeyboardA11y: boolean; disableKeyboardA11y: boolean;
width?: number; width?: number;
height?: number; height?: number;
}; };
+27 -22
View File
@@ -6,13 +6,12 @@ import type {
DefaultEdgeOptionsBase, DefaultEdgeOptionsBase,
EdgePosition, EdgePosition,
SmoothStepPathOptions, SmoothStepPathOptions,
Optional,
StepPathOptions StepPathOptions
} from '@xyflow/system'; } from '@xyflow/system';
import type { Node } from '$lib/types'; import type { Node } from '$lib/types';
export type DefaultEdge<EdgeData = any> = Omit<EdgeBase<EdgeData>, 'focusable'> & { export type DefaultEdge<EdgeData = any> = EdgeBase<EdgeData> & {
label?: string; label?: string;
labelStyle?: string; labelStyle?: string;
style?: string; style?: string;
@@ -34,12 +33,18 @@ type StepEdgeType<T> = DefaultEdge<T> & {
pathOptions?: StepPathOptions; pathOptions?: StepPathOptions;
}; };
/**
* The Edge type is mainly used for the `edges` that get passed to the SvelteFlow component.
*/
export type Edge<T = any> = export type Edge<T = any> =
| DefaultEdge<T> | DefaultEdge<T>
| SmoothStepEdgeType<T> | SmoothStepEdgeType<T>
| BezierEdgeType<T> | BezierEdgeType<T>
| StepEdgeType<T>; | StepEdgeType<T>;
/**
* Custom edge component props.
*/
export type EdgeProps<T = any> = Omit<Edge<T>, 'sourceHandle' | 'targetHandle' | 'type'> & export type EdgeProps<T = any> = Omit<Edge<T>, 'sourceHandle' | 'targetHandle' | 'type'> &
EdgePosition & { EdgePosition & {
markerStart?: string; markerStart?: string;
@@ -48,30 +53,30 @@ export type EdgeProps<T = any> = Omit<Edge<T>, 'sourceHandle' | 'targetHandle' |
targetHandleId?: string | null; targetHandleId?: string | null;
}; };
export type EdgeComponentProps<T = any> = Optional< export type EdgeComponentProps = EdgePosition & {
Omit< id?: EdgeProps['id'];
EdgeProps<T>, hidden?: EdgeProps['hidden'];
'source' | 'target' | 'sourceHandleId' | 'targetHandleId' | 'animated' | 'selected' | 'data' deletable?: EdgeProps['deletable'];
>, selectable?: EdgeProps['selectable'];
'id' markerStart?: EdgeProps['markerStart'];
>; markerEnd?: EdgeProps['markerEnd'];
zIndex?: EdgeProps['zIndex'];
export type BezierEdgeProps<T = any> = EdgeComponentProps<T> & { ariaLabel?: EdgeProps['ariaLabel'];
pathOptions?: BezierPathOptions; interactionWidth?: EdgeProps['interactionWidth'];
label?: EdgeProps['label'];
labelStyle?: EdgeProps['labelStyle'];
style?: EdgeProps['style'];
class?: EdgeProps['class'];
}; };
export type SmoothStepEdgeProps<T = any> = EdgeComponentProps<T> & { export type EdgeComponentWithPathOptions<PathOptions> = EdgeComponentProps & {
pathOptions?: SmoothStepPathOptions; pathOptions?: PathOptions;
}; };
export type StepEdgeProps<T = any> = EdgeComponentProps<T> & { export type BezierEdgeProps = EdgeComponentWithPathOptions<BezierPathOptions>;
pathOptions?: StepPathOptions; export type SmoothStepEdgeProps = EdgeComponentWithPathOptions<SmoothStepPathOptions>;
}; export type StepEdgeProps = EdgeComponentWithPathOptions<StepPathOptions>;
export type StraightEdgeProps = Omit<EdgeComponentProps, 'sourcePosition' | 'targetPosition'>;
export type StraightEdgeProps<T = any> = Omit<
EdgeComponentProps<T>,
'sourcePosition' | 'targetPosition'
>;
export type EdgeTypes = Record<string, ComponentType<SvelteComponent<EdgeProps>>>; export type EdgeTypes = Record<string, ComponentType<SvelteComponent<EdgeProps>>>;
+1 -2
View File
@@ -19,7 +19,6 @@ export type EdgeBase<EdgeData = any> = {
zIndex?: number; zIndex?: number;
ariaLabel?: string; ariaLabel?: string;
interactionWidth?: number; interactionWidth?: number;
focusable?: boolean;
}; };
export type SmoothStepPathOptions = { export type SmoothStepPathOptions = {
@@ -37,7 +36,7 @@ export type BezierPathOptions = {
export type DefaultEdgeOptionsBase<EdgeType extends EdgeBase> = Omit< export type DefaultEdgeOptionsBase<EdgeType extends EdgeBase> = Omit<
EdgeType, EdgeType,
'id' | 'source' | 'target' | 'sourceHandle' | 'targetHandle' | 'sourceNode' | 'targetNode' 'id' | 'source' | 'target' | 'sourceHandle' | 'targetHandle'
>; >;
export enum ConnectionLineType { export enum ConnectionLineType {