-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathselectionContextMenu.tsx
More file actions
349 lines (291 loc) · 10.4 KB
/
selectionContextMenu.tsx
File metadata and controls
349 lines (291 loc) · 10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
import { Edge, Graph, Node as GraphNode } from '@tokens-studio/graph-engine';
import { GROUP } from '@/ids.js';
import { GROUP_NODE_PADDING } from '@/constants.js';
import { Item, Menu, Separator } from 'react-contexify';
import { Node, getNodesBounds, useReactFlow, useStoreApi } from 'reactflow';
import { height, width, xpos, ypos } from '@/annotations/index.js';
import { useAction } from '@/editor/actions/provider.js';
import { useLocalGraph } from '@/hooks/index.js';
import { v4 as uuid } from 'uuid';
import React, { useCallback, useMemo } from 'react';
export type INodeContextMenuProps = {
id: string;
nodes: Node[];
};
export const SelectionContextMenu = ({ id, nodes }: INodeContextMenuProps) => {
const reactFlowInstance = useReactFlow();
const graph = useLocalGraph();
const store = useStoreApi();
const createNode = useAction('createNode');
const duplicateNodes = useAction('duplicateNodes');
const reactFlowNodes = reactFlowInstance.getNodes();
// Note that we use a filter here to prevent getting nodes that have a parent node, ie are part of a group
const selectedNodes = nodes.filter(
(node) => node.selected && !node.parentId,
);
const selectedNodeIds = selectedNodes.map((node) => node.id);
const onGroup = useCallback(() => {
const bounds = getNodesBounds(nodes);
const parentPosition = {
x: bounds.x,
y: bounds.y,
};
store.getState().resetSelectedElements();
store.setState({ nodesSelectionActive: false });
const newNodes = createNode({
type: GROUP,
position: parentPosition,
});
if (!newNodes) {
return;
}
const { flowNode } = newNodes;
reactFlowInstance.setNodes((nodes) => {
// Note that group nodes should always occur before their children
return [{
...flowNode,
dragHandle: undefined,
style: {
width: bounds.width + GROUP_NODE_PADDING * 2,
height: bounds.height + GROUP_NODE_PADDING * 2,
},
data: {
expandable: true,
expanded: true,
}
} as Node]
.concat(nodes)
.map((node) => {
if (selectedNodeIds.includes(node.id)) {
return {
...node,
position: {
x: node.position.x - parentPosition.x + GROUP_NODE_PADDING,
y: node.position.y - parentPosition.y + GROUP_NODE_PADDING,
},
extent: 'parent' as const,
parentId: flowNode.id,
};
}
return node;
});
});
const reactFlowNodesMap = new Map<string, Node>(
reactFlowNodes.map((node) => [node.id, node]),
);
// Set annotations for all items in the group
nodes.forEach((node) => {
const graphNode = graph.getNode(node.id);
if (graphNode) {
graphNode.annotations[xpos] = node.position.x - parentPosition.x + GROUP_NODE_PADDING;
graphNode.annotations[ypos] = node.position.y - parentPosition.y + GROUP_NODE_PADDING;
graphNode.annotations[width] = reactFlowNodesMap.get(node.id)?.width || 200;
graphNode.annotations[height] = reactFlowNodesMap.get(node.id)?.height || 100;
graphNode.annotations['parentId'] = flowNode.id;
}
});
}, [createNode, graph, nodes, reactFlowInstance, reactFlowNodes, selectedNodeIds, store]);
const onCreateSubgraph = useCallback(() => {
// Get all selected node ids, including children of groups
const selectedNodeIds = selectedNodes
.reduce((acc, node) => {
if (node.type !== GROUP) {
return [...acc, node.id];
}
const children = reactFlowNodes
.filter((n) => n.parentId === node.id)
.map((x) => x.id);
if (children.length > 0) {
return [...acc, node.id, ...children];
}
return acc;
}, [] as string[]);
const lookup = new Set(selectedNodeIds);
//Lets create a new subgraph node
//Get the position by finding the average of the selected nodes
const position = selectedNodes.reduce(
(acc, node) => {
acc.x += node.position.x;
acc.y += node.position.y;
return acc;
},
{ x: 0, y: 0 },
);
const finalPosition = {
x: position.x / selectedNodes.length,
y: position.y / selectedNodes.length,
};
const newNodes = createNode({
type: 'studio.tokens.generic.subgraph',
position: finalPosition,
});
//Request failed in some way
if (!newNodes) {
return;
}
const { graphNode, flowNode } = newNodes;
//@ts-expect-error
const internalGraph = graphNode._innerGraph as unknown as Graph;
//Get the graph nodes from the existing graph
const internalNodes = selectedNodeIds
.map((id) => graph.getNode(id))
.filter((x) => x);
//Get the input node of the subgraph
const existingInternal = Object.values(internalGraph.nodes);
const inputNode = existingInternal.find(
(x) => x.factory.type == 'studio.tokens.generic.input',
);
const outputNode = existingInternal.find(
(x) => x.factory.type == 'studio.tokens.generic.output',
);
//Now we need to determine the inputs and outputs of the subgraph
//We need to find the entry nodes and the exit nodes
//The entry nodes are the nodes that have incoming edges that are not in the selection
//The exit nodes are the nodes that have outgoing edges that are not in the selection
const { exitEdges, internalEdges } = selectedNodeIds.reduce(
(acc, x) => {
const edges = graph.outEdges(x);
//If there is an edge that is not in the selection then it is an entry node
edges.forEach((edge) => {
const isOutgoing = !lookup.has(edge.target);
if (isOutgoing) {
acc.exitEdges.push(edge);
} else {
acc.internalEdges.push(edge);
}
});
return acc;
},
{
exitEdges: [] as Edge[],
internalEdges: [] as Edge[],
},
);
const entryEdges = selectedNodeIds.reduce((acc, x) => {
const edges = graph.inEdges(x);
//If there is an edge that is not in the selection then it is an entry node
const incomingEdges = edges.filter((edge) => {
return !lookup.has(edge.source);
});
if (incomingEdges.length != 0) {
acc = acc.concat(incomingEdges);
}
return acc;
}, [] as Edge[]);
//Add the nodes to the subgraph
internalNodes.forEach((node) => {
internalGraph.addNode(node!);
});
//We need to create a input for each of the entry edges
entryEdges.forEach((edge) => {
//Get the output node of the edge
const outputNode = graph.getNode(edge.target);
const input = outputNode?.inputs[edge.targetHandle];
//Its possible that we have a collision with the name so we need to rename it
const initialName = edge.targetHandle;
let name = initialName;
let count = 0;
while (inputNode?.inputs[name]) {
name = initialName + '_' + count++;
}
inputNode?.addInput(name, {
type: input!.type,
});
//The output won't be dynamically generated until the node is executed, so we add it manually
inputNode?.addOutput(name, {
type: input!.type,
});
//Then we need to restore the old edge and connect it to the new input
const newEdge = graph.createEdge({
id: uuid(),
source: edge.source,
sourceHandle: edge.sourceHandle,
target: graphNode.id,
targetHandle: name,
});
//Create an edge in the internal graph from the input to the target
internalGraph.createEdge({
id: uuid(),
source: inputNode!.id,
sourceHandle: name,
target: edge.target,
targetHandle: edge.targetHandle,
});
//We also need to add a flow edge
reactFlowInstance.addEdges([newEdge]);
});
exitEdges.forEach((edge) => {
//Get the output node of the edge
const sourceNode = graph.getNode(edge.source);
const sourceOutput = sourceNode?.outputs[edge.sourceHandle];
//Its possible that we have a collision with the name so we need to rename it
const initialName = edge.targetHandle;
let name = initialName;
let count = 0;
while (outputNode?.inputs[name]) {
name = initialName + '_' + count++;
}
outputNode?.addInput(name, {
type: sourceOutput!.type,
});
outputNode?.addOutput(name, {
type: sourceOutput!.type,
});
//Ensure the output exists on the outer graph node as it will only be dynamically populated once the subgraph has run
graphNode.addOutput(name, {
type: sourceOutput!.type,
});
//Create an edge in the outer graph from the subgraph to the target
const newEdge = graph.createEdge({
id: uuid(),
source: graphNode.id,
sourceHandle: name,
target: edge.target,
targetHandle: edge.targetHandle,
});
//Create an edge in the internal graph
internalGraph.createEdge({
id: uuid(),
source: edge.source,
sourceHandle: edge.sourceHandle,
target: outputNode!.id,
targetHandle: name,
});
//We also need to add a flow edge
reactFlowInstance.addEdges([newEdge]);
});
//Reconnect the edges that are internal to the subgraph
internalEdges.forEach((edge) => {
//Create an edge in the internal graph
//Do not trigger any updates
internalGraph.createEdge({
...edge,
noPropagate: true,
});
});
// //Remove the selected nodes from the graph
selectedNodeIds.forEach((id) => {
graph.removeNode(id);
});
internalNodes.forEach((node) => {
internalGraph.addNode(node!);
});
//Now filter out nodes that were in the original selection
reactFlowInstance.setNodes((nodes) =>
[...nodes.filter((x) => lookup.has(x.id) == false)].concat([flowNode]),
);
//We then need to find all the downstream nodes from those nodes for the output
}, [createNode, graph, reactFlowInstance, selectedNodeIds, selectedNodes]);
const onDuplicate = () => {
duplicateNodes(selectedNodeIds);
};
const hasGroup = selectedNodes.some((node) => node.type === GROUP);
return (
<Menu id={id}>
{!hasGroup && <Item onClick={onGroup}>Create group</Item>}
<Item onClick={onCreateSubgraph}>Create Subgraph</Item>
<Separator />
<Item onClick={onDuplicate}>Duplicate</Item>
</Menu>
);
};