refactor(types): use same structure for nodes and edges

This commit is contained in:
moklick
2024-02-17 15:45:10 +01:00
parent 776f6ce0b7
commit ad7403cc75
24 changed files with 176 additions and 147 deletions
@@ -10,7 +10,6 @@ import type {
OnMoveEnd,
CoordinateExtent,
PanOnScrollMode,
IsValidConnection,
OnError,
ConnectionMode,
PanelPosition,
@@ -18,8 +17,7 @@ import type {
ColorMode,
OnConnect,
OnConnectStart,
OnConnectEnd,
OnBeforeDelete
OnConnectEnd
} from '@xyflow/system';
import type {
@@ -31,7 +29,9 @@ import type {
DefaultEdgeOptions,
FitViewOptions,
OnDelete,
OnEdgeCreate
OnEdgeCreate,
OnBeforeDelete,
IsValidConnection
} from '$lib/types';
import type { Writable } from 'svelte/store';
@@ -3,7 +3,7 @@ import { derived, type Readable } from 'svelte/store';
import type { Node } from '$lib/types';
import { useStore } from '$lib/store';
function areNodesDataEqual(a: Node['data'][] | null, b: Node['data'][] | null) {
function areNodesDataEqual(a: (Node['data'] | null)[] | null, b: (Node['data'] | null)[] | null) {
if ((!a && !b) || (!a?.length && !b?.length)) {
true;
}
-1
View File
@@ -66,7 +66,6 @@ export {
type OnConnectStart,
type OnConnect,
type OnConnectEnd,
type IsValidConnection,
type Viewport,
type SnapGrid,
PanOnScrollMode,
@@ -14,7 +14,6 @@ import {
type MarkerProps,
type PanZoomInstance,
type CoordinateExtent,
type IsValidConnection,
type NodeOrigin,
type OnError,
type Viewport,
@@ -47,7 +46,8 @@ import type {
FitViewOptions,
OnDelete,
OnEdgeCreate,
OnBeforeDelete
OnBeforeDelete,
IsValidConnection
} from '$lib/types';
import { createNodesStore, createEdgesStore } from './utils';
import { initConnectionProps, type ConnectionProps } from './derived-connection-props';
+24 -17
View File
@@ -1,4 +1,3 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import type { SvelteComponent, ComponentType } from 'svelte';
import type {
EdgeBase,
@@ -11,41 +10,49 @@ import type {
import type { Node } from '$lib/types';
export type DefaultEdge<EdgeData = any> = EdgeBase<EdgeData> & {
/**
* The Edge type is mainly used for the `edges` that get passed to the SvelteFlow component.
*/
export type Edge<
EdgeData extends Record<string, unknown> = Record<string, unknown>,
EdgeType extends string | undefined = string | undefined
> = EdgeBase<EdgeData, EdgeType> & {
label?: string;
labelStyle?: string;
style?: string;
class?: string;
};
type SmoothStepEdgeType<T> = DefaultEdge<T> & {
type: 'smoothstep';
type SmoothStepEdge<EdgeData extends Record<string, unknown> = Record<string, unknown>> = Edge<
EdgeData,
'smoothstep'
> & {
pathOptions?: SmoothStepPathOptions;
};
type BezierEdgeType<T> = DefaultEdge<T> & {
type: 'default';
type BezierEdge<EdgeData extends Record<string, unknown> = Record<string, unknown>> = Edge<
EdgeData,
'default'
> & {
pathOptions?: BezierPathOptions;
};
type StepEdgeType<T> = DefaultEdge<T> & {
type: 'step';
type StepEdge<EdgeData extends Record<string, unknown> = Record<string, unknown>> = Edge<
EdgeData,
'step'
> & {
pathOptions?: StepPathOptions;
};
/**
* The Edge type is mainly used for the `edges` that get passed to the SvelteFlow component.
*/
export type Edge<T = any> =
| DefaultEdge<T>
| SmoothStepEdgeType<T>
| BezierEdgeType<T>
| StepEdgeType<T>;
export type BuiltInEdge = SmoothStepEdge | BezierEdge | StepEdge;
/**
* Custom edge component props.
*/
export type EdgeProps<T = any> = Omit<Edge<T>, 'sourceHandle' | 'targetHandle' | 'type'> &
export type EdgeProps<
EdgeData extends Record<string, unknown> = Record<string, unknown>,
EdgeType extends string | undefined = string | undefined
> = Omit<Edge<EdgeData, EdgeType>, 'sourceHandle' | 'targetHandle' | 'type'> &
EdgePosition & {
markerStart?: string;
markerEnd?: string;
+4
View File
@@ -57,3 +57,7 @@ export type OnBeforeDelete<
NodeType extends Node = Node,
EdgeType extends Edge = Edge
> = OnBeforeDeleteBase<NodeType, EdgeType>;
export type IsValidConnection<EdgeType extends Edge = Edge> = (
edge: EdgeType | Connection
) => boolean;
+3 -1
View File
@@ -7,7 +7,7 @@ import type { NodeBase, NodeProps } from '@xyflow/system';
* @public
*/
export type Node<
NodeData = any,
NodeData extends Record<string, unknown> = Record<string, unknown>,
NodeType extends string | undefined = string | undefined
> = NodeBase<NodeData, NodeType> & {
class?: string;
@@ -17,3 +17,5 @@ export type Node<
export type NodeTypes = Record<string, ComponentType<SvelteComponent<NodeProps>>>;
export type DefaultNodeOptions = Partial<Omit<Node, 'id'>>;
export type BuiltInNode = Node<{ label: string }, 'input' | 'output' | 'default'>;