Skip to content

Commit

Permalink
✨ feat: add dispatch
Browse files Browse the repository at this point in the history
  • Loading branch information
jiangchu committed Jan 3, 2025
1 parent cf366cd commit 7e3fb02
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
11 changes: 9 additions & 2 deletions src/FlowEditor/store/slices/edgesSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, Edge>, options?: ActionOptions) => void;
addEdges: (edges: Record<string, Edge> | Edge[], options?: ActionOptions) => void;
deleteEdge: (id: string) => void;
deleteEdges: (ids: string[]) => void;
updateEdge: (id: string, edge: Edge, options?: ActionOptions) => void;
Expand Down Expand Up @@ -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<string, Edge>, edge) => {
acc[edge.id] = edge;
return acc;
}, {})
: edges;

get().dispatchEdges({ type: 'addEdges', edges: _edges }, options);
},

updateEdgesOnConnection: (connection) => {
Expand Down
11 changes: 9 additions & 2 deletions src/FlowEditor/store/slices/nodesSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export interface PublicNodesAction {
* @param index 要添加到的位置,默认为末尾
*/
addNode: (node: Node, index?: number) => void;
addNodes: (nodes: Record<string, Node>, options?: ActionOptions) => void;
addNodes: (nodes: Record<string, Node> | Node[], options?: ActionOptions) => void;
/**
* 移除指定 id 的节点
* @param id 要移除的节点 id
Expand Down Expand Up @@ -170,10 +170,17 @@ export const nodesSlice: StateCreator<
},

addNodes: (nodes, options) => {
const _nodes = Array.isArray(nodes)
? nodes.reduce((acc: Record<string, Node>, node) => {
acc[node.id] = node;
return acc;
}, {})
: nodes;

get().dispatchNodes(
{
type: 'addNodes',
nodes,
nodes: _nodes,
},
options,
);
Expand Down
10 changes: 9 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

0 comments on commit 7e3fb02

Please sign in to comment.