chore(onbeforedelete): use correct node/edge types, pass generic

This commit is contained in:
moklick
2024-01-23 13:04:02 +01:00
parent 6c73ab9a45
commit 2e1bfc35c0
9 changed files with 23 additions and 9 deletions

View File

@@ -15,6 +15,7 @@ import {
applyNodeChanges,
OnNodesChange,
OnConnect,
OnBeforeDelete,
} from '@xyflow/react';
import ColorSelectorNode from './ColorSelectorNode';
@@ -142,6 +143,8 @@ const CustomNodeFlow = () => {
[setEdges]
);
const onBeforeDelete: OnBeforeDelete<MyNode> = useCallback(async (params) => true, []);
return (
<ReactFlow
nodes={nodes}
@@ -159,6 +162,7 @@ const CustomNodeFlow = () => {
fitView
minZoom={0.3}
maxZoom={2}
onBeforeDelete={onBeforeDelete}
>
<MiniMap
nodeStrokeColor={(n: MyNode): string => {

View File

@@ -79,7 +79,6 @@ export {
type ColorMode,
type ColorModeClass,
type HandleType,
type OnBeforeDelete,
type ShouldResize,
type OnResizeStart,
type OnResize,

View File

@@ -21,7 +21,6 @@ import type {
IsValidConnection,
ColorMode,
SnapGrid,
OnBeforeDelete,
} from '@xyflow/system';
import type {
@@ -44,6 +43,7 @@ import type {
SelectionDragHandler,
EdgeMouseHandler,
OnNodeDrag,
OnBeforeDelete,
} from '.';
/**

View File

@@ -11,6 +11,7 @@ import {
FitBounds,
XYPosition,
NodeProps,
OnBeforeDeleteBase,
} from '@xyflow/system';
import type { NodeChange, EdgeChange, Node, Edge, ReactFlowInstance, EdgeProps } from '.';
@@ -59,3 +60,8 @@ export type ViewportHelperFunctions = {
flowToScreenPosition: (position: XYPosition) => XYPosition;
viewportInitialized: boolean;
};
export type OnBeforeDelete<NodeType extends Node = Node, EdgeType extends Edge = Edge> = OnBeforeDeleteBase<
NodeType,
EdgeType
>;

View File

@@ -26,7 +26,6 @@ import {
type EdgeLookup,
type ConnectionLookup,
type NodeLookup,
OnBeforeDelete,
} from '@xyflow/system';
import type {
@@ -43,6 +42,7 @@ import type {
UnselectNodesAndEdgesParams,
OnDelete,
OnNodeDrag,
OnBeforeDelete,
} from '.';
export type ReactFlowStore = {

View File

@@ -23,7 +23,6 @@ import {
type OnConnectStart,
type OnConnectEnd,
type NodeLookup,
type OnBeforeDelete,
type EdgeLookup
} from '@xyflow/system';
@@ -47,7 +46,8 @@ import type {
Edge,
FitViewOptions,
OnDelete,
OnEdgeCreate
OnEdgeCreate,
OnBeforeDelete
} from '$lib/types';
import { createNodesStore, createEdgesStore } from './utils';
import { initConnectionProps, type ConnectionProps } from './derived-connection-props';

View File

@@ -5,7 +5,8 @@ import type {
Position,
XYPosition,
ConnectingHandle,
Connection
Connection,
OnBeforeDeleteBase
} from '@xyflow/system';
import type { Node } from './nodes';
@@ -52,3 +53,7 @@ export type FitViewOptions = FitViewOptionsBase<Node>;
export type OnDelete = (params: { nodes: Node[]; edges: Edge[] }) => void;
export type OnEdgeCreate = (connection: Connection) => Edge | Connection | void;
export type OnBeforeDelete<
NodeType extends Node = Node,
EdgeType extends Edge = Edge
> = OnBeforeDeleteBase<NodeType, EdgeType>;

View File

@@ -142,7 +142,7 @@ export type ColorMode = ColorModeClass | 'system';
export type ConnectionLookup = Map<string, Map<string, Connection>>;
export type OnBeforeDelete = <NodeType extends NodeBase = NodeBase, EdgeType extends EdgeBase = EdgeBase>({
export type OnBeforeDeleteBase<NodeType extends NodeBase = NodeBase, EdgeType extends EdgeBase = EdgeBase> = ({
nodes,
edges,
}: {

View File

@@ -22,7 +22,7 @@ import {
NodeDragItem,
CoordinateExtent,
OnError,
OnBeforeDelete,
OnBeforeDeleteBase,
} from '../types';
import { errorMessages } from '../constants';
@@ -347,7 +347,7 @@ export async function getElementsToRemove<NodeType extends NodeBase = NodeBase,
edgesToRemove: Partial<EdgeType>[];
nodes: NodeType[];
edges: EdgeType[];
onBeforeDelete?: OnBeforeDelete;
onBeforeDelete?: OnBeforeDeleteBase<NodeType, EdgeType>;
}): Promise<{
nodes: NodeType[];
edges: EdgeType[];