1
+ import { Dictionary } from "lodash" ;
2
+ import { flushSync } from "react-dom" ;
3
+ import NodeUtils from "../../components/graph/NodeUtils" ;
4
+ import { batchGroupBy } from "../../reducers/graph/batchGroupBy" ;
5
+ import { prepareNewNodesWithLayout } from "../../reducers/graph/utils" ;
6
+ import { getScenarioGraph } from "../../reducers/selectors/graph" ;
7
+ import { getProcessDefinitionData } from "../../reducers/selectors/settings" ;
1
8
import { Edge , EdgeType , NodeId , NodeType , ProcessDefinitionData , ValidationResult } from "../../types" ;
2
9
import { ThunkAction } from "../reduxTypes" ;
3
- import { layoutChanged , Position } from "./ui/layout" ;
4
10
import { EditNodeAction , EditScenarioLabels , RenameProcessAction } from "./editNode" ;
5
- import { getProcessDefinitionData } from "../../reducers/selectors/settings" ;
6
- import { batchGroupBy } from "../../reducers/graph/batchGroupBy" ;
7
- import NodeUtils from "../../components/graph/NodeUtils" ;
8
- import { getScenarioGraph } from "../../reducers/selectors/graph" ;
9
- import { flushSync } from "react-dom" ;
11
+ import { layoutChanged , NodePosition , Position } from "./ui/layout" ;
10
12
11
13
export type NodesWithPositions = { node : NodeType ; position : Position } [ ] ;
12
14
@@ -31,7 +33,9 @@ type NodesDisonnectedAction = {
31
33
32
34
type NodesWithEdgesAddedAction = {
33
35
type : "NODES_WITH_EDGES_ADDED" ;
34
- nodesWithPositions : NodesWithPositions ;
36
+ nodes : NodeType [ ] ;
37
+ layout : NodePosition [ ] ;
38
+ idMapping : Dictionary < string > ;
35
39
edges : Edge [ ] ;
36
40
processDefinitionData : ProcessDefinitionData ;
37
41
} ;
@@ -43,8 +47,8 @@ type ValidationResultAction = {
43
47
44
48
type NodeAddedAction = {
45
49
type : "NODE_ADDED" ;
46
- node : NodeType ;
47
- position : Position ;
50
+ nodes : NodeType [ ] ;
51
+ layout : NodePosition [ ] ;
48
52
} ;
49
53
50
54
export function deleteNodes ( ids : NodeId [ ] ) : ThunkAction {
@@ -118,13 +122,20 @@ export function injectNode(from: NodeType, middle: NodeType, to: NodeType, { edg
118
122
}
119
123
120
124
export function nodeAdded ( node : NodeType , position : Position ) : ThunkAction {
121
- return ( dispatch ) => {
125
+ return ( dispatch , getState ) => {
122
126
batchGroupBy . startOrContinue ( ) ;
123
127
124
128
// We need to disable automatic React batching https://react.dev/blog/2022/03/29/react-v18#new-feature-automatic-batching
125
129
// since it breaks redux undo in this case
126
130
flushSync ( ( ) => {
127
- dispatch ( { type : "NODE_ADDED" , node, position } ) ;
131
+ const scenarioGraph = getScenarioGraph ( getState ( ) ) ;
132
+ const { nodes, layout } = prepareNewNodesWithLayout ( scenarioGraph . nodes , [ { node, position } ] , false ) ;
133
+
134
+ dispatch ( {
135
+ type : "NODE_ADDED" ,
136
+ nodes,
137
+ layout,
138
+ } ) ;
128
139
dispatch ( layoutChanged ( ) ) ;
129
140
} ) ;
130
141
batchGroupBy . end ( ) ;
@@ -133,11 +144,17 @@ export function nodeAdded(node: NodeType, position: Position): ThunkAction {
133
144
134
145
export function nodesWithEdgesAdded ( nodesWithPositions : NodesWithPositions , edges : Edge [ ] ) : ThunkAction {
135
146
return ( dispatch , getState ) => {
136
- const processDefinitionData = getProcessDefinitionData ( getState ( ) ) ;
147
+ const state = getState ( ) ;
148
+ const processDefinitionData = getProcessDefinitionData ( state ) ;
149
+ const scenarioGraph = getScenarioGraph ( state ) ;
150
+ const { nodes, layout, idMapping } = prepareNewNodesWithLayout ( scenarioGraph . nodes , nodesWithPositions , true ) ;
151
+
137
152
batchGroupBy . startOrContinue ( ) ;
138
153
dispatch ( {
139
154
type : "NODES_WITH_EDGES_ADDED" ,
140
- nodesWithPositions,
155
+ nodes,
156
+ layout,
157
+ idMapping,
141
158
edges,
142
159
processDefinitionData,
143
160
} ) ;
0 commit comments