diff --git a/src/FlowEditor/store/slices/edgesSlice.ts b/src/FlowEditor/store/slices/edgesSlice.ts index 21a0c78..956f552 100644 --- a/src/FlowEditor/store/slices/edgesSlice.ts +++ b/src/FlowEditor/store/slices/edgesSlice.ts @@ -10,7 +10,7 @@ import { EdgeDispatch, edgesReducer } from '../reducers/edge'; export interface PublicEdgesAction { dispatchEdges: (payload: EdgeDispatch, options?: ActionOptions) => void; addEdge: (edge: Edge) => void; - addEdges: (edges: Record, options?: ActionOptions) => void; + addEdges: (edges: Record | Edge[], options?: ActionOptions) => void; deleteEdge: (id: string) => void; deleteEdges: (ids: string[]) => void; updateEdge: (id: string, edge: Edge, options?: ActionOptions) => void; @@ -79,7 +79,14 @@ export const edgesSlice: StateCreator< }, addEdges: (edges, options) => { - get().dispatchEdges({ type: 'addEdges', edges: edges }, options); + const _edges = Array.isArray(edges) + ? edges.reduce((acc: Record, edge) => { + acc[edge.id] = edge; + return acc; + }, {}) + : edges; + + get().dispatchEdges({ type: 'addEdges', edges: _edges }, options); }, updateEdgesOnConnection: (connection) => { diff --git a/src/FlowEditor/store/slices/nodesSlice.ts b/src/FlowEditor/store/slices/nodesSlice.ts index 469daa1..6fc4769 100644 --- a/src/FlowEditor/store/slices/nodesSlice.ts +++ b/src/FlowEditor/store/slices/nodesSlice.ts @@ -23,7 +23,7 @@ export interface PublicNodesAction { * @param index 要添加到的位置,默认为末尾 */ addNode: (node: Node, index?: number) => void; - addNodes: (nodes: Record, options?: ActionOptions) => void; + addNodes: (nodes: Record | Node[], options?: ActionOptions) => void; /** * 移除指定 id 的节点 * @param id 要移除的节点 id @@ -170,10 +170,17 @@ export const nodesSlice: StateCreator< }, addNodes: (nodes, options) => { + const _nodes = Array.isArray(nodes) + ? nodes.reduce((acc: Record, node) => { + acc[node.id] = node; + return acc; + }, {}) + : nodes; + get().dispatchNodes( { type: 'addNodes', - nodes, + nodes: _nodes, }, options, ); diff --git a/src/index.ts b/src/index.ts index 01a48f5..e969a0f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -35,6 +35,14 @@ export * from './Input'; export * from './MiniMap'; export * from './constants'; -export type { Connection, EdgeChange, EdgeProps, NodeChange, NodeProps } from 'reactflow'; +export type { + Connection, + Edge, + EdgeChange, + EdgeProps, + Node, + NodeChange, + NodeProps, +} from 'reactflow'; export type { FlowEditorStoreProviderProps } from './FlowStoreProvider'; export type { ExtraAction } from './NodeField';