@octanejs/xyflow 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +26 -0
- package/package.json +69 -0
- package/src/additional-components/Background/Background.tsrx +144 -0
- package/src/additional-components/Background/Patterns.tsrx +32 -0
- package/src/additional-components/Background/index.tsrx +2 -0
- package/src/additional-components/Background/types.ts +60 -0
- package/src/additional-components/Controls/ControlButton.tsrx +34 -0
- package/src/additional-components/Controls/Controls.tsrx +148 -0
- package/src/additional-components/Controls/Icons/FitView.tsrx +7 -0
- package/src/additional-components/Controls/Icons/Lock.tsrx +7 -0
- package/src/additional-components/Controls/Icons/Minus.tsrx +5 -0
- package/src/additional-components/Controls/Icons/Plus.tsrx +5 -0
- package/src/additional-components/Controls/Icons/Unlock.tsrx +7 -0
- package/src/additional-components/Controls/index.tsrx +3 -0
- package/src/additional-components/Controls/types.ts +68 -0
- package/src/additional-components/EdgeToolbar/EdgeToolbar.tsrx +79 -0
- package/src/additional-components/EdgeToolbar/index.tsrx +2 -0
- package/src/additional-components/EdgeToolbar/types.ts +14 -0
- package/src/additional-components/MiniMap/MiniMap.tsrx +249 -0
- package/src/additional-components/MiniMap/MiniMapNode.tsrx +43 -0
- package/src/additional-components/MiniMap/MiniMapNodes.tsrx +136 -0
- package/src/additional-components/MiniMap/index.ts +3 -0
- package/src/additional-components/MiniMap/types.ts +139 -0
- package/src/additional-components/NodeResizer/NodeResizeControl.tsrx +250 -0
- package/src/additional-components/NodeResizer/NodeResizer.tsrx +102 -0
- package/src/additional-components/NodeResizer/index.ts +4 -0
- package/src/additional-components/NodeResizer/types.ts +122 -0
- package/src/additional-components/NodeToolbar/NodeToolbar.tsrx +141 -0
- package/src/additional-components/NodeToolbar/NodeToolbarPortal.tsrx +17 -0
- package/src/additional-components/NodeToolbar/index.tsrx +2 -0
- package/src/additional-components/NodeToolbar/types.ts +32 -0
- package/src/additional-components/index.ts +6 -0
- package/src/components/A11yDescriptions/index.tsrx +58 -0
- package/src/components/Attribution/index.tsrx +31 -0
- package/src/components/BatchProvider/index.tsrx +122 -0
- package/src/components/BatchProvider/types.ts +7 -0
- package/src/components/BatchProvider/useQueue.ts +62 -0
- package/src/components/ConnectionLine/index.tsrx +140 -0
- package/src/components/EdgeLabelRenderer/index.tsrx +62 -0
- package/src/components/EdgeWrapper/EdgeUpdateAnchors.tsrx +151 -0
- package/src/components/EdgeWrapper/index.tsrx +274 -0
- package/src/components/EdgeWrapper/utils.ts +26 -0
- package/src/components/Edges/BaseEdge.tsrx +76 -0
- package/src/components/Edges/BezierEdge.tsrx +94 -0
- package/src/components/Edges/EdgeAnchor.tsrx +54 -0
- package/src/components/Edges/EdgeText.tsrx +97 -0
- package/src/components/Edges/SimpleBezierEdge.tsrx +150 -0
- package/src/components/Edges/SmoothStepEdge.tsrx +96 -0
- package/src/components/Edges/StepEdge.tsrx +55 -0
- package/src/components/Edges/StraightEdge.tsrx +81 -0
- package/src/components/Edges/index.ts +11 -0
- package/src/components/Handle/index.tsrx +314 -0
- package/src/components/NodeWrapper/index.tsrx +259 -0
- package/src/components/NodeWrapper/useNodeObserver.ts +79 -0
- package/src/components/NodeWrapper/utils.tsrx +40 -0
- package/src/components/Nodes/DefaultNode.tsrx +17 -0
- package/src/components/Nodes/GroupNode.tsrx +3 -0
- package/src/components/Nodes/InputNode.tsrx +13 -0
- package/src/components/Nodes/OutputNode.tsrx +13 -0
- package/src/components/Nodes/utils.ts +45 -0
- package/src/components/NodesSelection/index.tsrx +103 -0
- package/src/components/Panel/index.tsrx +34 -0
- package/src/components/ReactFlowProvider/index.tsrx +128 -0
- package/src/components/SelectionListener/index.tsrx +73 -0
- package/src/components/StoreUpdater/index.tsrx +173 -0
- package/src/components/UserSelection/index.tsrx +27 -0
- package/src/components/ViewportPortal/index.tsrx +42 -0
- package/src/container/EdgeRenderer/MarkerDefinitions.tsrx +87 -0
- package/src/container/EdgeRenderer/MarkerSymbols.tsrx +60 -0
- package/src/container/EdgeRenderer/index.tsrx +98 -0
- package/src/container/FlowRenderer/index.tsrx +146 -0
- package/src/container/GraphView/index.tsrx +212 -0
- package/src/container/GraphView/useNodeOrEdgeTypesWarning.ts +39 -0
- package/src/container/GraphView/useStylesLoadedWarning.ts +26 -0
- package/src/container/NodeRenderer/index.tsrx +103 -0
- package/src/container/NodeRenderer/useResizeObserver.ts +38 -0
- package/src/container/Pane/index.tsrx +397 -0
- package/src/container/ReactFlow/Wrapper.tsrx +66 -0
- package/src/container/ReactFlow/index.tsrx +352 -0
- package/src/container/ReactFlow/init-values.ts +4 -0
- package/src/container/Viewport/index.tsrx +52 -0
- package/src/container/ZoomPane/index.tsrx +159 -0
- package/src/contexts/HandleConfigContext.tsrx +38 -0
- package/src/contexts/NodeIdContext.ts +39 -0
- package/src/contexts/StoreContext.ts +8 -0
- package/src/custom.d.ts +13 -0
- package/src/hooks/slot.ts +22 -0
- package/src/hooks/useColorModeClass.ts +50 -0
- package/src/hooks/useConnection.ts +69 -0
- package/src/hooks/useDrag.ts +95 -0
- package/src/hooks/useEdges.ts +32 -0
- package/src/hooks/useGlobalKeyHandler.ts +55 -0
- package/src/hooks/useHandleConnections.ts +77 -0
- package/src/hooks/useInternalNode.ts +47 -0
- package/src/hooks/useIsomorphicLayoutEffect.ts +5 -0
- package/src/hooks/useKeyPress.ts +217 -0
- package/src/hooks/useMoveSelectedNodes.ts +82 -0
- package/src/hooks/useNodeConnections.ts +86 -0
- package/src/hooks/useNodes.ts +33 -0
- package/src/hooks/useNodesData.ts +66 -0
- package/src/hooks/useNodesEdgesState.ts +138 -0
- package/src/hooks/useNodesInitialized.ts +74 -0
- package/src/hooks/useOnEdgesChangeMiddleware.ts +46 -0
- package/src/hooks/useOnInitHandler.ts +27 -0
- package/src/hooks/useOnNodesChangeMiddleware.ts +46 -0
- package/src/hooks/useOnSelectionChange.ts +78 -0
- package/src/hooks/useOnViewportChange.ts +68 -0
- package/src/hooks/useReactFlow.ts +347 -0
- package/src/hooks/useResizeHandler.ts +54 -0
- package/src/hooks/useStore.ts +58 -0
- package/src/hooks/useUpdateNodeInternals.ts +73 -0
- package/src/hooks/useViewport.ts +48 -0
- package/src/hooks/useViewportHelper.ts +103 -0
- package/src/hooks/useViewportSync.ts +33 -0
- package/src/hooks/useVisibleEdgeIds.ts +52 -0
- package/src/hooks/useVisibleNodeIds.ts +36 -0
- package/src/index.ts +151 -0
- package/src/react-shim.ts +93 -0
- package/src/store/index.ts +516 -0
- package/src/store/initialState.ts +162 -0
- package/src/styles/base.css +516 -0
- package/src/styles/style.css +648 -0
- package/src/styles/utils.ts +9 -0
- package/src/types/component-props.ts +716 -0
- package/src/types/edges.ts +312 -0
- package/src/types/general.ts +239 -0
- package/src/types/index.ts +6 -0
- package/src/types/instance.ts +265 -0
- package/src/types/nodes.ts +144 -0
- package/src/types/store.ts +198 -0
- package/src/utils/changes.ts +329 -0
- package/src/utils/edges.ts +34 -0
- package/src/utils/general.ts +72 -0
- package/src/utils/index.ts +3 -0
|
@@ -0,0 +1,516 @@
|
|
|
1
|
+
import { createWithEqualityFn } from '@octanejs/zustand/traditional';
|
|
2
|
+
import {
|
|
3
|
+
adoptUserNodes,
|
|
4
|
+
updateAbsolutePositions,
|
|
5
|
+
panBy as panBySystem,
|
|
6
|
+
updateNodeInternals as updateNodeInternalsSystem,
|
|
7
|
+
updateConnectionLookup,
|
|
8
|
+
handleExpandParent,
|
|
9
|
+
NodeChange,
|
|
10
|
+
EdgeSelectionChange,
|
|
11
|
+
NodeSelectionChange,
|
|
12
|
+
ParentExpandChild,
|
|
13
|
+
initialConnection,
|
|
14
|
+
NodeOrigin,
|
|
15
|
+
CoordinateExtent,
|
|
16
|
+
fitViewport,
|
|
17
|
+
getHandlePosition,
|
|
18
|
+
Position,
|
|
19
|
+
ZIndexMode,
|
|
20
|
+
} from '@xyflow/system';
|
|
21
|
+
|
|
22
|
+
import {
|
|
23
|
+
applyEdgeChanges,
|
|
24
|
+
applyNodeChanges,
|
|
25
|
+
createSelectionChange,
|
|
26
|
+
getSelectionChanges,
|
|
27
|
+
} from '../utils/changes';
|
|
28
|
+
import getInitialState from './initialState';
|
|
29
|
+
import type {
|
|
30
|
+
ReactFlowState,
|
|
31
|
+
Node,
|
|
32
|
+
Edge,
|
|
33
|
+
UnselectNodesAndEdgesParams,
|
|
34
|
+
FitViewOptions,
|
|
35
|
+
} from '../types';
|
|
36
|
+
|
|
37
|
+
const createStore = ({
|
|
38
|
+
nodes,
|
|
39
|
+
edges,
|
|
40
|
+
defaultNodes,
|
|
41
|
+
defaultEdges,
|
|
42
|
+
width,
|
|
43
|
+
height,
|
|
44
|
+
fitView,
|
|
45
|
+
fitViewOptions,
|
|
46
|
+
minZoom,
|
|
47
|
+
maxZoom,
|
|
48
|
+
nodeOrigin,
|
|
49
|
+
nodeExtent,
|
|
50
|
+
zIndexMode,
|
|
51
|
+
}: {
|
|
52
|
+
nodes?: Node[];
|
|
53
|
+
edges?: Edge[];
|
|
54
|
+
defaultNodes?: Node[];
|
|
55
|
+
defaultEdges?: Edge[];
|
|
56
|
+
width?: number;
|
|
57
|
+
height?: number;
|
|
58
|
+
fitView?: boolean;
|
|
59
|
+
fitViewOptions?: FitViewOptions;
|
|
60
|
+
minZoom?: number;
|
|
61
|
+
maxZoom?: number;
|
|
62
|
+
nodeOrigin?: NodeOrigin;
|
|
63
|
+
nodeExtent?: CoordinateExtent;
|
|
64
|
+
zIndexMode?: ZIndexMode;
|
|
65
|
+
}) =>
|
|
66
|
+
createWithEqualityFn<ReactFlowState>((set, get) => {
|
|
67
|
+
async function resolveFitView() {
|
|
68
|
+
const {
|
|
69
|
+
nodeLookup,
|
|
70
|
+
panZoom,
|
|
71
|
+
fitViewOptions,
|
|
72
|
+
fitViewResolver,
|
|
73
|
+
width,
|
|
74
|
+
height,
|
|
75
|
+
minZoom,
|
|
76
|
+
maxZoom,
|
|
77
|
+
} = get();
|
|
78
|
+
|
|
79
|
+
if (!panZoom) {
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
await fitViewport(
|
|
84
|
+
{
|
|
85
|
+
nodes: nodeLookup,
|
|
86
|
+
width,
|
|
87
|
+
height,
|
|
88
|
+
panZoom,
|
|
89
|
+
minZoom,
|
|
90
|
+
maxZoom,
|
|
91
|
+
},
|
|
92
|
+
fitViewOptions,
|
|
93
|
+
);
|
|
94
|
+
|
|
95
|
+
fitViewResolver?.resolve(true);
|
|
96
|
+
/**
|
|
97
|
+
* wait for the fitViewport to resolve before deleting the resolver,
|
|
98
|
+
* we want to reuse the old resolver if the user calls fitView again in the mean time
|
|
99
|
+
*/
|
|
100
|
+
set({ fitViewResolver: null });
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
return {
|
|
104
|
+
...getInitialState({
|
|
105
|
+
nodes,
|
|
106
|
+
edges,
|
|
107
|
+
width,
|
|
108
|
+
height,
|
|
109
|
+
fitView,
|
|
110
|
+
fitViewOptions,
|
|
111
|
+
minZoom,
|
|
112
|
+
maxZoom,
|
|
113
|
+
nodeOrigin,
|
|
114
|
+
nodeExtent,
|
|
115
|
+
defaultNodes,
|
|
116
|
+
defaultEdges,
|
|
117
|
+
zIndexMode,
|
|
118
|
+
}),
|
|
119
|
+
setNodes: (nodes: Node[]) => {
|
|
120
|
+
const {
|
|
121
|
+
nodeLookup,
|
|
122
|
+
parentLookup,
|
|
123
|
+
nodeOrigin,
|
|
124
|
+
elevateNodesOnSelect,
|
|
125
|
+
fitViewQueued,
|
|
126
|
+
zIndexMode,
|
|
127
|
+
nodesSelectionActive,
|
|
128
|
+
} = get();
|
|
129
|
+
|
|
130
|
+
/*
|
|
131
|
+
* setNodes() is called exclusively in response to user actions:
|
|
132
|
+
* - either when the `<ReactFlow nodes>` prop is updated in the controlled ReactFlow setup,
|
|
133
|
+
* - or when the user calls something like `reactFlowInstance.setNodes()` in an uncontrolled ReactFlow setup.
|
|
134
|
+
*
|
|
135
|
+
* When this happens, we take the note objects passed by the user and extend them with fields
|
|
136
|
+
* relevant for internal React Flow operations.
|
|
137
|
+
*/
|
|
138
|
+
|
|
139
|
+
const { nodesInitialized, hasSelectedNodes } = adoptUserNodes(
|
|
140
|
+
nodes,
|
|
141
|
+
nodeLookup,
|
|
142
|
+
parentLookup,
|
|
143
|
+
{
|
|
144
|
+
nodeOrigin,
|
|
145
|
+
nodeExtent,
|
|
146
|
+
elevateNodesOnSelect,
|
|
147
|
+
checkEquality: true,
|
|
148
|
+
zIndexMode,
|
|
149
|
+
},
|
|
150
|
+
);
|
|
151
|
+
|
|
152
|
+
const nextNodesSelectionActive = nodesSelectionActive && hasSelectedNodes;
|
|
153
|
+
|
|
154
|
+
if (fitViewQueued && nodesInitialized) {
|
|
155
|
+
resolveFitView();
|
|
156
|
+
set({
|
|
157
|
+
nodes,
|
|
158
|
+
nodesInitialized,
|
|
159
|
+
fitViewQueued: false,
|
|
160
|
+
fitViewOptions: undefined,
|
|
161
|
+
nodesSelectionActive: nextNodesSelectionActive,
|
|
162
|
+
});
|
|
163
|
+
} else {
|
|
164
|
+
set({ nodes, nodesInitialized, nodesSelectionActive: nextNodesSelectionActive });
|
|
165
|
+
}
|
|
166
|
+
},
|
|
167
|
+
setEdges: (edges: Edge[]) => {
|
|
168
|
+
const { connectionLookup, edgeLookup } = get();
|
|
169
|
+
|
|
170
|
+
updateConnectionLookup(connectionLookup, edgeLookup, edges);
|
|
171
|
+
|
|
172
|
+
set({ edges });
|
|
173
|
+
},
|
|
174
|
+
setDefaultNodesAndEdges: (nodes?: Node[], edges?: Edge[]) => {
|
|
175
|
+
if (nodes) {
|
|
176
|
+
const { setNodes } = get();
|
|
177
|
+
setNodes(nodes);
|
|
178
|
+
set({ hasDefaultNodes: true });
|
|
179
|
+
}
|
|
180
|
+
if (edges) {
|
|
181
|
+
const { setEdges } = get();
|
|
182
|
+
setEdges(edges);
|
|
183
|
+
set({ hasDefaultEdges: true });
|
|
184
|
+
}
|
|
185
|
+
},
|
|
186
|
+
/*
|
|
187
|
+
* Every node gets registered at a ResizeObserver. Whenever a node
|
|
188
|
+
* changes its dimensions, this function is called to measure the
|
|
189
|
+
* new dimensions and update the nodes.
|
|
190
|
+
*/
|
|
191
|
+
updateNodeInternals: (updates) => {
|
|
192
|
+
const {
|
|
193
|
+
triggerNodeChanges,
|
|
194
|
+
nodeLookup,
|
|
195
|
+
parentLookup,
|
|
196
|
+
domNode,
|
|
197
|
+
nodeOrigin,
|
|
198
|
+
nodeExtent,
|
|
199
|
+
debug,
|
|
200
|
+
fitViewQueued,
|
|
201
|
+
zIndexMode,
|
|
202
|
+
} = get();
|
|
203
|
+
|
|
204
|
+
const { changes, updatedInternals } = updateNodeInternalsSystem(
|
|
205
|
+
updates,
|
|
206
|
+
nodeLookup,
|
|
207
|
+
parentLookup,
|
|
208
|
+
domNode,
|
|
209
|
+
nodeOrigin,
|
|
210
|
+
nodeExtent,
|
|
211
|
+
zIndexMode,
|
|
212
|
+
);
|
|
213
|
+
|
|
214
|
+
if (!updatedInternals) {
|
|
215
|
+
return;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
updateAbsolutePositions(nodeLookup, parentLookup, { nodeOrigin, nodeExtent, zIndexMode });
|
|
219
|
+
|
|
220
|
+
if (fitViewQueued) {
|
|
221
|
+
resolveFitView();
|
|
222
|
+
set({ fitViewQueued: false, fitViewOptions: undefined });
|
|
223
|
+
} else {
|
|
224
|
+
// we always want to trigger useStore calls whenever updateNodeInternals is called
|
|
225
|
+
set({});
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
if (changes?.length > 0) {
|
|
229
|
+
if (debug) {
|
|
230
|
+
console.log('React Flow: trigger node changes', changes);
|
|
231
|
+
}
|
|
232
|
+
triggerNodeChanges?.(changes);
|
|
233
|
+
}
|
|
234
|
+
},
|
|
235
|
+
updateNodePositions: (nodeDragItems, dragging = false) => {
|
|
236
|
+
const parentExpandChildren: ParentExpandChild[] = [];
|
|
237
|
+
let changes = [];
|
|
238
|
+
const {
|
|
239
|
+
nodeLookup,
|
|
240
|
+
triggerNodeChanges,
|
|
241
|
+
connection,
|
|
242
|
+
updateConnection,
|
|
243
|
+
onNodesChangeMiddlewareMap,
|
|
244
|
+
} = get();
|
|
245
|
+
|
|
246
|
+
for (const [id, dragItem] of nodeDragItems) {
|
|
247
|
+
// we are using the nodelookup to be sure to use the current expandParent and parentId value
|
|
248
|
+
const node = nodeLookup.get(id);
|
|
249
|
+
const expandParent = !!(node?.expandParent && node?.parentId && dragItem?.position);
|
|
250
|
+
|
|
251
|
+
const change: NodeChange = {
|
|
252
|
+
id,
|
|
253
|
+
type: 'position',
|
|
254
|
+
position: expandParent
|
|
255
|
+
? {
|
|
256
|
+
x: Math.max(0, dragItem.position.x),
|
|
257
|
+
y: Math.max(0, dragItem.position.y),
|
|
258
|
+
}
|
|
259
|
+
: dragItem.position,
|
|
260
|
+
dragging,
|
|
261
|
+
};
|
|
262
|
+
|
|
263
|
+
if (node && connection.inProgress && connection.fromNode.id === node.id) {
|
|
264
|
+
const updatedFrom = getHandlePosition(node, connection.fromHandle, Position.Left, true);
|
|
265
|
+
updateConnection({ ...connection, from: updatedFrom });
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
if (expandParent && node.parentId) {
|
|
269
|
+
parentExpandChildren.push({
|
|
270
|
+
id,
|
|
271
|
+
parentId: node.parentId,
|
|
272
|
+
rect: {
|
|
273
|
+
...dragItem.internals.positionAbsolute,
|
|
274
|
+
width: dragItem.measured.width ?? 0,
|
|
275
|
+
height: dragItem.measured.height ?? 0,
|
|
276
|
+
},
|
|
277
|
+
});
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
changes.push(change);
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
if (parentExpandChildren.length > 0) {
|
|
284
|
+
const { parentLookup, nodeOrigin } = get();
|
|
285
|
+
const parentExpandChanges = handleExpandParent(
|
|
286
|
+
parentExpandChildren,
|
|
287
|
+
nodeLookup,
|
|
288
|
+
parentLookup,
|
|
289
|
+
nodeOrigin,
|
|
290
|
+
);
|
|
291
|
+
changes.push(...parentExpandChanges);
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
for (const middleware of onNodesChangeMiddlewareMap.values()) {
|
|
295
|
+
changes = middleware(changes);
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
triggerNodeChanges(changes);
|
|
299
|
+
},
|
|
300
|
+
triggerNodeChanges: (changes) => {
|
|
301
|
+
const { onNodesChange, setNodes, nodes, hasDefaultNodes, debug } = get();
|
|
302
|
+
|
|
303
|
+
if (changes?.length) {
|
|
304
|
+
if (hasDefaultNodes) {
|
|
305
|
+
const updatedNodes = applyNodeChanges(changes, nodes);
|
|
306
|
+
setNodes(updatedNodes);
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
if (debug) {
|
|
310
|
+
console.log('React Flow: trigger node changes', changes);
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
onNodesChange?.(changes);
|
|
314
|
+
}
|
|
315
|
+
},
|
|
316
|
+
triggerEdgeChanges: (changes) => {
|
|
317
|
+
const { onEdgesChange, setEdges, edges, hasDefaultEdges, debug } = get();
|
|
318
|
+
|
|
319
|
+
if (changes?.length) {
|
|
320
|
+
if (hasDefaultEdges) {
|
|
321
|
+
const updatedEdges = applyEdgeChanges(changes, edges);
|
|
322
|
+
setEdges(updatedEdges);
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
if (debug) {
|
|
326
|
+
console.log('React Flow: trigger edge changes', changes);
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
onEdgesChange?.(changes);
|
|
330
|
+
}
|
|
331
|
+
},
|
|
332
|
+
addSelectedNodes: (selectedNodeIds) => {
|
|
333
|
+
const {
|
|
334
|
+
multiSelectionActive,
|
|
335
|
+
edgeLookup,
|
|
336
|
+
nodeLookup,
|
|
337
|
+
triggerNodeChanges,
|
|
338
|
+
triggerEdgeChanges,
|
|
339
|
+
} = get();
|
|
340
|
+
|
|
341
|
+
if (multiSelectionActive) {
|
|
342
|
+
const nodeChanges = selectedNodeIds.map((nodeId) => createSelectionChange(nodeId, true));
|
|
343
|
+
triggerNodeChanges(nodeChanges);
|
|
344
|
+
return;
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
triggerNodeChanges(getSelectionChanges(nodeLookup, new Set([...selectedNodeIds]), true));
|
|
348
|
+
triggerEdgeChanges(getSelectionChanges(edgeLookup));
|
|
349
|
+
},
|
|
350
|
+
addSelectedEdges: (selectedEdgeIds) => {
|
|
351
|
+
const {
|
|
352
|
+
multiSelectionActive,
|
|
353
|
+
edgeLookup,
|
|
354
|
+
nodeLookup,
|
|
355
|
+
triggerNodeChanges,
|
|
356
|
+
triggerEdgeChanges,
|
|
357
|
+
} = get();
|
|
358
|
+
|
|
359
|
+
if (multiSelectionActive) {
|
|
360
|
+
const changedEdges = selectedEdgeIds.map((edgeId) => createSelectionChange(edgeId, true));
|
|
361
|
+
triggerEdgeChanges(changedEdges);
|
|
362
|
+
return;
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
triggerEdgeChanges(getSelectionChanges(edgeLookup, new Set([...selectedEdgeIds])));
|
|
366
|
+
triggerNodeChanges(getSelectionChanges(nodeLookup, new Set(), true));
|
|
367
|
+
},
|
|
368
|
+
unselectNodesAndEdges: ({ nodes, edges }: UnselectNodesAndEdgesParams = {}) => {
|
|
369
|
+
const {
|
|
370
|
+
edges: storeEdges,
|
|
371
|
+
nodes: storeNodes,
|
|
372
|
+
nodeLookup,
|
|
373
|
+
triggerNodeChanges,
|
|
374
|
+
triggerEdgeChanges,
|
|
375
|
+
} = get();
|
|
376
|
+
const nodesToUnselect = nodes ? nodes : storeNodes;
|
|
377
|
+
const edgesToUnselect = edges ? edges : storeEdges;
|
|
378
|
+
|
|
379
|
+
const nodeChanges: NodeSelectionChange[] = [];
|
|
380
|
+
|
|
381
|
+
for (const node of nodesToUnselect) {
|
|
382
|
+
if (!node.selected) {
|
|
383
|
+
continue; // skip changing nodes that are not selected
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
const internalNode = nodeLookup.get(node.id);
|
|
387
|
+
|
|
388
|
+
if (internalNode) {
|
|
389
|
+
/*
|
|
390
|
+
* we need to unselect the internal node that was selected previously before we
|
|
391
|
+
* send the change to the user to prevent it to be selected while dragging the new node
|
|
392
|
+
*/
|
|
393
|
+
internalNode.selected = false;
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
nodeChanges.push(createSelectionChange(node.id, false));
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
const edgeChanges: EdgeSelectionChange[] = [];
|
|
400
|
+
|
|
401
|
+
for (const edge of edgesToUnselect) {
|
|
402
|
+
if (!edge.selected) {
|
|
403
|
+
continue; // skip changing edges that are not selected
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
edgeChanges.push(createSelectionChange(edge.id, false));
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
triggerNodeChanges(nodeChanges);
|
|
410
|
+
triggerEdgeChanges(edgeChanges);
|
|
411
|
+
},
|
|
412
|
+
setMinZoom: (minZoom) => {
|
|
413
|
+
const { panZoom, maxZoom } = get();
|
|
414
|
+
panZoom?.setScaleExtent([minZoom, maxZoom]);
|
|
415
|
+
|
|
416
|
+
set({ minZoom });
|
|
417
|
+
},
|
|
418
|
+
setMaxZoom: (maxZoom) => {
|
|
419
|
+
const { panZoom, minZoom } = get();
|
|
420
|
+
panZoom?.setScaleExtent([minZoom, maxZoom]);
|
|
421
|
+
|
|
422
|
+
set({ maxZoom });
|
|
423
|
+
},
|
|
424
|
+
setTranslateExtent: (translateExtent) => {
|
|
425
|
+
get().panZoom?.setTranslateExtent(translateExtent);
|
|
426
|
+
|
|
427
|
+
set({ translateExtent });
|
|
428
|
+
},
|
|
429
|
+
resetSelectedElements: () => {
|
|
430
|
+
const { edges, nodes, triggerNodeChanges, triggerEdgeChanges, elementsSelectable } = get();
|
|
431
|
+
|
|
432
|
+
if (!elementsSelectable) {
|
|
433
|
+
return;
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
const nodeChanges = nodes.reduce<NodeSelectionChange[]>(
|
|
437
|
+
(res, node) => (node.selected ? [...res, createSelectionChange(node.id, false)] : res),
|
|
438
|
+
[],
|
|
439
|
+
);
|
|
440
|
+
const edgeChanges = edges.reduce<EdgeSelectionChange[]>(
|
|
441
|
+
(res, edge) => (edge.selected ? [...res, createSelectionChange(edge.id, false)] : res),
|
|
442
|
+
[],
|
|
443
|
+
);
|
|
444
|
+
|
|
445
|
+
triggerNodeChanges(nodeChanges);
|
|
446
|
+
triggerEdgeChanges(edgeChanges);
|
|
447
|
+
},
|
|
448
|
+
setNodeExtent: (nextNodeExtent) => {
|
|
449
|
+
const {
|
|
450
|
+
nodes,
|
|
451
|
+
nodeLookup,
|
|
452
|
+
parentLookup,
|
|
453
|
+
nodeOrigin,
|
|
454
|
+
elevateNodesOnSelect,
|
|
455
|
+
nodeExtent,
|
|
456
|
+
zIndexMode,
|
|
457
|
+
} = get();
|
|
458
|
+
|
|
459
|
+
if (
|
|
460
|
+
nextNodeExtent[0][0] === nodeExtent[0][0] &&
|
|
461
|
+
nextNodeExtent[0][1] === nodeExtent[0][1] &&
|
|
462
|
+
nextNodeExtent[1][0] === nodeExtent[1][0] &&
|
|
463
|
+
nextNodeExtent[1][1] === nodeExtent[1][1]
|
|
464
|
+
) {
|
|
465
|
+
return;
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
adoptUserNodes(nodes, nodeLookup, parentLookup, {
|
|
469
|
+
nodeOrigin,
|
|
470
|
+
nodeExtent: nextNodeExtent,
|
|
471
|
+
elevateNodesOnSelect,
|
|
472
|
+
checkEquality: false,
|
|
473
|
+
zIndexMode,
|
|
474
|
+
});
|
|
475
|
+
|
|
476
|
+
set({ nodeExtent: nextNodeExtent });
|
|
477
|
+
},
|
|
478
|
+
panBy: (delta): Promise<boolean> => {
|
|
479
|
+
const { transform, width, height, panZoom, translateExtent } = get();
|
|
480
|
+
|
|
481
|
+
return panBySystem({ delta, panZoom, transform, translateExtent, width, height });
|
|
482
|
+
},
|
|
483
|
+
setCenter: async (x, y, options) => {
|
|
484
|
+
const { width, height, maxZoom, panZoom } = get();
|
|
485
|
+
|
|
486
|
+
if (!panZoom) {
|
|
487
|
+
return false;
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
const nextZoom = typeof options?.zoom !== 'undefined' ? options.zoom : maxZoom;
|
|
491
|
+
|
|
492
|
+
await panZoom.setViewport(
|
|
493
|
+
{
|
|
494
|
+
x: width / 2 - x * nextZoom,
|
|
495
|
+
y: height / 2 - y * nextZoom,
|
|
496
|
+
zoom: nextZoom,
|
|
497
|
+
},
|
|
498
|
+
{ duration: options?.duration, ease: options?.ease, interpolate: options?.interpolate },
|
|
499
|
+
);
|
|
500
|
+
|
|
501
|
+
return true;
|
|
502
|
+
},
|
|
503
|
+
cancelConnection: () => {
|
|
504
|
+
set({
|
|
505
|
+
connection: { ...initialConnection },
|
|
506
|
+
});
|
|
507
|
+
},
|
|
508
|
+
updateConnection: (connection) => {
|
|
509
|
+
set({ connection });
|
|
510
|
+
},
|
|
511
|
+
|
|
512
|
+
reset: () => set({ ...getInitialState() }),
|
|
513
|
+
};
|
|
514
|
+
}, Object.is);
|
|
515
|
+
|
|
516
|
+
export { createStore };
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
import {
|
|
2
|
+
infiniteExtent,
|
|
3
|
+
ConnectionMode,
|
|
4
|
+
adoptUserNodes,
|
|
5
|
+
getViewportForBounds,
|
|
6
|
+
Transform,
|
|
7
|
+
updateConnectionLookup,
|
|
8
|
+
createDevWarn,
|
|
9
|
+
getInternalNodesBounds,
|
|
10
|
+
NodeOrigin,
|
|
11
|
+
initialConnection,
|
|
12
|
+
CoordinateExtent,
|
|
13
|
+
defaultAriaLabelConfig,
|
|
14
|
+
ZIndexMode,
|
|
15
|
+
} from '@xyflow/system';
|
|
16
|
+
|
|
17
|
+
const devWarn = createDevWarn('React Flow', 'https://reactflow.dev/');
|
|
18
|
+
|
|
19
|
+
import type { Edge, FitViewOptions, InternalNode, Node, ReactFlowStore } from '../types';
|
|
20
|
+
|
|
21
|
+
const getInitialState = ({
|
|
22
|
+
nodes,
|
|
23
|
+
edges,
|
|
24
|
+
defaultNodes,
|
|
25
|
+
defaultEdges,
|
|
26
|
+
width,
|
|
27
|
+
height,
|
|
28
|
+
fitView,
|
|
29
|
+
fitViewOptions,
|
|
30
|
+
minZoom = 0.5,
|
|
31
|
+
maxZoom = 2,
|
|
32
|
+
nodeOrigin,
|
|
33
|
+
nodeExtent,
|
|
34
|
+
zIndexMode = 'basic',
|
|
35
|
+
}: {
|
|
36
|
+
nodes?: Node[];
|
|
37
|
+
edges?: Edge[];
|
|
38
|
+
defaultNodes?: Node[];
|
|
39
|
+
defaultEdges?: Edge[];
|
|
40
|
+
width?: number;
|
|
41
|
+
height?: number;
|
|
42
|
+
fitView?: boolean;
|
|
43
|
+
fitViewOptions?: FitViewOptions;
|
|
44
|
+
minZoom?: number;
|
|
45
|
+
maxZoom?: number;
|
|
46
|
+
nodeOrigin?: NodeOrigin;
|
|
47
|
+
nodeExtent?: CoordinateExtent;
|
|
48
|
+
zIndexMode?: ZIndexMode;
|
|
49
|
+
} = {}): ReactFlowStore => {
|
|
50
|
+
const nodeLookup = new Map<string, InternalNode>();
|
|
51
|
+
const parentLookup = new Map();
|
|
52
|
+
const connectionLookup = new Map();
|
|
53
|
+
const edgeLookup = new Map();
|
|
54
|
+
|
|
55
|
+
const storeEdges = defaultEdges ?? edges ?? [];
|
|
56
|
+
const storeNodes = defaultNodes ?? nodes ?? [];
|
|
57
|
+
const storeNodeOrigin = nodeOrigin ?? [0, 0];
|
|
58
|
+
const storeNodeExtent = nodeExtent ?? infiniteExtent;
|
|
59
|
+
|
|
60
|
+
updateConnectionLookup(connectionLookup, edgeLookup, storeEdges);
|
|
61
|
+
const { nodesInitialized } = adoptUserNodes(storeNodes, nodeLookup, parentLookup, {
|
|
62
|
+
nodeOrigin: storeNodeOrigin,
|
|
63
|
+
nodeExtent: storeNodeExtent,
|
|
64
|
+
zIndexMode,
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
let transform: Transform = [0, 0, 1];
|
|
68
|
+
|
|
69
|
+
if (fitView && width && height) {
|
|
70
|
+
const bounds = getInternalNodesBounds(nodeLookup, {
|
|
71
|
+
filter: (node) =>
|
|
72
|
+
!!((node.width || node.initialWidth) && (node.height || node.initialHeight)),
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
const { x, y, zoom } = getViewportForBounds(
|
|
76
|
+
bounds,
|
|
77
|
+
width,
|
|
78
|
+
height,
|
|
79
|
+
minZoom,
|
|
80
|
+
maxZoom,
|
|
81
|
+
fitViewOptions?.padding ?? 0.1,
|
|
82
|
+
);
|
|
83
|
+
transform = [x, y, zoom];
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
return {
|
|
87
|
+
rfId: '1',
|
|
88
|
+
width: width ?? 0,
|
|
89
|
+
height: height ?? 0,
|
|
90
|
+
transform,
|
|
91
|
+
nodes: storeNodes,
|
|
92
|
+
nodesInitialized,
|
|
93
|
+
nodeLookup,
|
|
94
|
+
parentLookup,
|
|
95
|
+
edges: storeEdges,
|
|
96
|
+
edgeLookup,
|
|
97
|
+
connectionLookup,
|
|
98
|
+
onNodesChange: null,
|
|
99
|
+
onEdgesChange: null,
|
|
100
|
+
hasDefaultNodes: defaultNodes !== undefined,
|
|
101
|
+
hasDefaultEdges: defaultEdges !== undefined,
|
|
102
|
+
panZoom: null,
|
|
103
|
+
minZoom,
|
|
104
|
+
maxZoom,
|
|
105
|
+
translateExtent: infiniteExtent,
|
|
106
|
+
nodeExtent: storeNodeExtent,
|
|
107
|
+
nodesSelectionActive: false,
|
|
108
|
+
userSelectionActive: false,
|
|
109
|
+
userSelectionRect: null,
|
|
110
|
+
connectionMode: ConnectionMode.Strict,
|
|
111
|
+
domNode: null,
|
|
112
|
+
paneDragging: false,
|
|
113
|
+
noPanClassName: 'nopan',
|
|
114
|
+
nodeOrigin: storeNodeOrigin,
|
|
115
|
+
nodeDragThreshold: 1,
|
|
116
|
+
connectionDragThreshold: 1,
|
|
117
|
+
|
|
118
|
+
snapGrid: [15, 15],
|
|
119
|
+
snapToGrid: false,
|
|
120
|
+
|
|
121
|
+
nodesDraggable: true,
|
|
122
|
+
nodesConnectable: true,
|
|
123
|
+
nodesFocusable: true,
|
|
124
|
+
edgesFocusable: true,
|
|
125
|
+
edgesReconnectable: true,
|
|
126
|
+
elementsSelectable: true,
|
|
127
|
+
elevateNodesOnSelect: true,
|
|
128
|
+
elevateEdgesOnSelect: true,
|
|
129
|
+
selectNodesOnDrag: true,
|
|
130
|
+
|
|
131
|
+
multiSelectionActive: false,
|
|
132
|
+
|
|
133
|
+
fitViewQueued: fitView ?? false,
|
|
134
|
+
fitViewOptions,
|
|
135
|
+
fitViewResolver: null,
|
|
136
|
+
|
|
137
|
+
connection: { ...initialConnection },
|
|
138
|
+
connectionClickStartHandle: null,
|
|
139
|
+
connectOnClick: true,
|
|
140
|
+
|
|
141
|
+
ariaLiveMessage: '',
|
|
142
|
+
autoPanOnConnect: true,
|
|
143
|
+
autoPanOnNodeDrag: true,
|
|
144
|
+
autoPanOnNodeFocus: true,
|
|
145
|
+
autoPanSpeed: 15,
|
|
146
|
+
|
|
147
|
+
connectionRadius: 20,
|
|
148
|
+
onError: devWarn,
|
|
149
|
+
isValidConnection: undefined,
|
|
150
|
+
onSelectionChangeHandlers: [],
|
|
151
|
+
|
|
152
|
+
lib: 'react',
|
|
153
|
+
debug: false,
|
|
154
|
+
ariaLabelConfig: defaultAriaLabelConfig,
|
|
155
|
+
zIndexMode,
|
|
156
|
+
|
|
157
|
+
onNodesChangeMiddlewareMap: new Map(),
|
|
158
|
+
onEdgesChangeMiddlewareMap: new Map(),
|
|
159
|
+
};
|
|
160
|
+
};
|
|
161
|
+
|
|
162
|
+
export default getInitialState;
|