Skip to content

Commit

Permalink
chore: 删除节点
Browse files Browse the repository at this point in the history
  • Loading branch information
youngjuning committed Dec 27, 2024
1 parent cf3f2fd commit f40aa28
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
12 changes: 11 additions & 1 deletion packages/x-flow/src/components/CustomNode/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export default memo((props: any) => {
widgets[`${capitalize(type)}Node`] || widgets['CommonNode'];
const [isHovered, setIsHovered] = useState(false);
const reactflow = useReactFlow();
const { addNodes, addEdges, copyNode, pasteNode, mousePosition } = useStore(
const { addNodes, addEdges, copyNode, pasteNode, deleteNode, mousePosition } = useStore(
(state: any) => ({
nodes: state.nodes,
edges: state.edges,
Expand All @@ -27,6 +27,7 @@ export default memo((props: any) => {
addEdges: state.addEdges,
copyNode: state.copyNode,
pasteNode: state.pasteNode,
deleteNode: state.deleteNode,
onEdgesChange: state.onEdgesChange,
}),
shallow
Expand Down Expand Up @@ -77,6 +78,10 @@ export default memo((props: any) => {
pasteNode(id)
}, [pasteNode]);

const handleDeleteNode = useCallback(() => {
deleteNode(id)
}, [pasteNode]);

return (
<div
className={classNames('xflow-node-container', {
Expand Down Expand Up @@ -107,6 +112,11 @@ export default memo((props: any) => {
key: 'paste',
onClick: handlePasteNode,
},
{
label: '删除',
key: 'delete',
onClick: handleDeleteNode,
},
],
}}
trigger={['click', 'contextMenu']}
Expand Down
6 changes: 6 additions & 0 deletions packages/x-flow/src/models/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export type FlowState = {
setEdges: (edges: Edge[]) => void;
addNodes: (nodes: FlowNode[]| FlowNode) => void;
addEdges: (edges: Edge[] | Edge) => void;
deleteNode: (nodeId: string) => void;
copyNode: (nodeId: string) => void;
pasteNode: (nodeId: string) => void;
setLayout: (layout: 'LR' | 'TB') => void;
Expand Down Expand Up @@ -142,6 +143,11 @@ const createStore = (initProps?: Partial<FlowProps>) => {
});
}
},
deleteNode: (nodeId) => {
set({
nodes: get().nodes.filter((node) => node.id !== nodeId),
});
},
}),
{
// nodes 和 edges 是引用类型,所以使用深比较
Expand Down

0 comments on commit f40aa28

Please sign in to comment.