@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,329 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
2
|
+
import {
|
|
3
|
+
EdgeLookup,
|
|
4
|
+
NodeLookup,
|
|
5
|
+
EdgeChange,
|
|
6
|
+
NodeChange,
|
|
7
|
+
NodeSelectionChange,
|
|
8
|
+
EdgeSelectionChange,
|
|
9
|
+
NodeRemoveChange,
|
|
10
|
+
EdgeRemoveChange,
|
|
11
|
+
} from '@xyflow/system';
|
|
12
|
+
import type { Node, Edge, InternalNode } from '../types';
|
|
13
|
+
|
|
14
|
+
/*
|
|
15
|
+
* This function applies changes to nodes or edges that are triggered by React Flow internally.
|
|
16
|
+
* When you drag a node for example, React Flow will send a position change update.
|
|
17
|
+
* This function then applies the changes and returns the updated elements.
|
|
18
|
+
*/
|
|
19
|
+
function applyChanges(changes: any[], elements: any[]): any[] {
|
|
20
|
+
const updatedElements: any[] = [];
|
|
21
|
+
/*
|
|
22
|
+
* By storing a map of changes for each element, we can a quick lookup as we
|
|
23
|
+
* iterate over the elements array!
|
|
24
|
+
*/
|
|
25
|
+
const changesMap = new Map<any, any[]>();
|
|
26
|
+
const addItemChanges: any[] = [];
|
|
27
|
+
|
|
28
|
+
for (const change of changes) {
|
|
29
|
+
if (change.type === 'add') {
|
|
30
|
+
addItemChanges.push(change);
|
|
31
|
+
continue;
|
|
32
|
+
} else if (change.type === 'remove' || change.type === 'replace') {
|
|
33
|
+
/*
|
|
34
|
+
* For a 'remove' change we can safely ignore any other changes queued for
|
|
35
|
+
* the same element, it's going to be removed anyway!
|
|
36
|
+
*/
|
|
37
|
+
changesMap.set(change.id, [change]);
|
|
38
|
+
} else {
|
|
39
|
+
const elementChanges = changesMap.get(change.id);
|
|
40
|
+
|
|
41
|
+
if (elementChanges) {
|
|
42
|
+
/*
|
|
43
|
+
* If we have some changes queued already, we can do a mutable update of
|
|
44
|
+
* that array and save ourselves some copying.
|
|
45
|
+
*/
|
|
46
|
+
elementChanges.push(change);
|
|
47
|
+
} else {
|
|
48
|
+
changesMap.set(change.id, [change]);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
for (const element of elements) {
|
|
54
|
+
const changes = changesMap.get(element.id);
|
|
55
|
+
|
|
56
|
+
/*
|
|
57
|
+
* When there are no changes for an element we can just push it unmodified,
|
|
58
|
+
* no need to copy it.
|
|
59
|
+
*/
|
|
60
|
+
if (!changes) {
|
|
61
|
+
updatedElements.push(element);
|
|
62
|
+
continue;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// If we have a 'remove' change queued, it'll be the only change in the array
|
|
66
|
+
if (changes[0].type === 'remove') {
|
|
67
|
+
continue;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
if (changes[0].type === 'replace') {
|
|
71
|
+
updatedElements.push({ ...changes[0].item });
|
|
72
|
+
continue;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* For other types of changes, we want to start with a shallow copy of the
|
|
77
|
+
* object so React knows this element has changed. Sequential changes will
|
|
78
|
+
* each _mutate_ this object, so there's only ever one copy.
|
|
79
|
+
*/
|
|
80
|
+
const updatedElement = { ...element };
|
|
81
|
+
|
|
82
|
+
for (const change of changes) {
|
|
83
|
+
applyChange(change, updatedElement);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
updatedElements.push(updatedElement);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/*
|
|
90
|
+
* we need to wait for all changes to be applied before adding new items
|
|
91
|
+
* to be able to add them at the correct index
|
|
92
|
+
*/
|
|
93
|
+
if (addItemChanges.length) {
|
|
94
|
+
addItemChanges.forEach((change) => {
|
|
95
|
+
if (change.index !== undefined) {
|
|
96
|
+
updatedElements.splice(change.index, 0, { ...change.item });
|
|
97
|
+
} else {
|
|
98
|
+
updatedElements.push({ ...change.item });
|
|
99
|
+
}
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
return updatedElements;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
// Applies a single change to an element. This is a *mutable* update.
|
|
107
|
+
function applyChange(change: any, element: any): any {
|
|
108
|
+
switch (change.type) {
|
|
109
|
+
case 'select': {
|
|
110
|
+
element.selected = change.selected;
|
|
111
|
+
break;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
case 'position': {
|
|
115
|
+
if (typeof change.position !== 'undefined') {
|
|
116
|
+
element.position = change.position;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
if (typeof change.dragging !== 'undefined') {
|
|
120
|
+
element.dragging = change.dragging;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
break;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
case 'dimensions': {
|
|
127
|
+
if (typeof change.dimensions !== 'undefined') {
|
|
128
|
+
element.measured = {
|
|
129
|
+
...change.dimensions,
|
|
130
|
+
};
|
|
131
|
+
|
|
132
|
+
if (change.setAttributes) {
|
|
133
|
+
if (change.setAttributes === true || change.setAttributes === 'width') {
|
|
134
|
+
element.width = change.dimensions.width;
|
|
135
|
+
}
|
|
136
|
+
if (change.setAttributes === true || change.setAttributes === 'height') {
|
|
137
|
+
element.height = change.dimensions.height;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
if (typeof change.resizing === 'boolean') {
|
|
143
|
+
element.resizing = change.resizing;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
break;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Drop in function that applies node changes to an array of nodes.
|
|
153
|
+
* @public
|
|
154
|
+
* @param changes - Array of changes to apply.
|
|
155
|
+
* @param nodes - Array of nodes to apply the changes to.
|
|
156
|
+
* @returns Array of updated nodes.
|
|
157
|
+
* @example
|
|
158
|
+
*```tsx
|
|
159
|
+
*import { useState, useCallback } from 'octane';
|
|
160
|
+
*import { ReactFlow, applyNodeChanges, type Node, type Edge, type OnNodesChange } from '@xyflow/react';
|
|
161
|
+
*
|
|
162
|
+
*export default function Flow() {
|
|
163
|
+
* const [nodes, setNodes] = useState<Node[]>([]);
|
|
164
|
+
* const [edges, setEdges] = useState<Edge[]>([]);
|
|
165
|
+
* const onNodesChange: OnNodesChange = useCallback(
|
|
166
|
+
* (changes) => {
|
|
167
|
+
* setNodes((oldNodes) => applyNodeChanges(changes, oldNodes));
|
|
168
|
+
* },
|
|
169
|
+
* [setNodes],
|
|
170
|
+
* );
|
|
171
|
+
*
|
|
172
|
+
* return (
|
|
173
|
+
* <ReactFlow nodes={nodes} edges={edges} onNodesChange={onNodesChange} />
|
|
174
|
+
* );
|
|
175
|
+
*}
|
|
176
|
+
*```
|
|
177
|
+
* @remarks Various events on the <ReactFlow /> component can produce an {@link NodeChange}
|
|
178
|
+
* that describes how to update the edges of your flow in some way.
|
|
179
|
+
* If you don't need any custom behaviour, this util can be used to take an array
|
|
180
|
+
* of these changes and apply them to your edges.
|
|
181
|
+
*/
|
|
182
|
+
export function applyNodeChanges<NodeType extends Node = Node>(
|
|
183
|
+
changes: NodeChange<NodeType>[],
|
|
184
|
+
nodes: NodeType[],
|
|
185
|
+
): NodeType[] {
|
|
186
|
+
return applyChanges(changes, nodes) as NodeType[];
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* Drop in function that applies edge changes to an array of edges.
|
|
191
|
+
* @public
|
|
192
|
+
* @param changes - Array of changes to apply.
|
|
193
|
+
* @param edges - Array of edge to apply the changes to.
|
|
194
|
+
* @returns Array of updated edges.
|
|
195
|
+
* @example
|
|
196
|
+
* ```tsx
|
|
197
|
+
*import { useState, useCallback } from 'octane';
|
|
198
|
+
*import { ReactFlow, applyEdgeChanges } from '@xyflow/react';
|
|
199
|
+
*
|
|
200
|
+
*export default function Flow() {
|
|
201
|
+
* const [nodes, setNodes] = useState([]);
|
|
202
|
+
* const [edges, setEdges] = useState([]);
|
|
203
|
+
* const onEdgesChange = useCallback(
|
|
204
|
+
* (changes) => {
|
|
205
|
+
* setEdges((oldEdges) => applyEdgeChanges(changes, oldEdges));
|
|
206
|
+
* },
|
|
207
|
+
* [setEdges],
|
|
208
|
+
* );
|
|
209
|
+
*
|
|
210
|
+
* return (
|
|
211
|
+
* <ReactFlow nodes={nodes} edges={edges} onEdgesChange={onEdgesChange} />
|
|
212
|
+
* );
|
|
213
|
+
*}
|
|
214
|
+
*```
|
|
215
|
+
* @remarks Various events on the <ReactFlow /> component can produce an {@link EdgeChange}
|
|
216
|
+
* that describes how to update the edges of your flow in some way.
|
|
217
|
+
* If you don't need any custom behaviour, this util can be used to take an array
|
|
218
|
+
* of these changes and apply them to your edges.
|
|
219
|
+
*/
|
|
220
|
+
export function applyEdgeChanges<EdgeType extends Edge = Edge>(
|
|
221
|
+
changes: EdgeChange<EdgeType>[],
|
|
222
|
+
edges: EdgeType[],
|
|
223
|
+
): EdgeType[] {
|
|
224
|
+
return applyChanges(changes, edges) as EdgeType[];
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
export function createSelectionChange(
|
|
228
|
+
id: string,
|
|
229
|
+
selected: boolean,
|
|
230
|
+
): NodeSelectionChange | EdgeSelectionChange {
|
|
231
|
+
return {
|
|
232
|
+
id,
|
|
233
|
+
type: 'select',
|
|
234
|
+
selected,
|
|
235
|
+
};
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
export function getSelectionChanges(
|
|
239
|
+
items: Map<string, any>,
|
|
240
|
+
selectedIds: Set<string> = new Set(),
|
|
241
|
+
mutateItem = false,
|
|
242
|
+
): NodeSelectionChange[] | EdgeSelectionChange[] {
|
|
243
|
+
const changes: NodeSelectionChange[] | EdgeSelectionChange[] = [];
|
|
244
|
+
|
|
245
|
+
for (const [id, item] of items) {
|
|
246
|
+
const willBeSelected = selectedIds.has(id);
|
|
247
|
+
|
|
248
|
+
// we don't want to set all items to selected=false on the first selection
|
|
249
|
+
if (!(item.selected === undefined && !willBeSelected) && item.selected !== willBeSelected) {
|
|
250
|
+
if (mutateItem) {
|
|
251
|
+
/*
|
|
252
|
+
* this hack is needed for nodes. When the user dragged a node, it's selected.
|
|
253
|
+
* When another node gets dragged, we need to deselect the previous one,
|
|
254
|
+
* in order to have only one selected node at a time - the onNodesChange callback comes too late here :/
|
|
255
|
+
*/
|
|
256
|
+
item.selected = willBeSelected;
|
|
257
|
+
}
|
|
258
|
+
changes.push(createSelectionChange(item.id, willBeSelected));
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
return changes;
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
/**
|
|
266
|
+
* This function is used to find the changes between two sets of elements.
|
|
267
|
+
* It is used to determine which nodes or edges have been added, removed or replaced.
|
|
268
|
+
*
|
|
269
|
+
* @internal
|
|
270
|
+
* @param params.items = the next set of elements (nodes or edges)
|
|
271
|
+
* @param params.lookup = a lookup map of the current store elements
|
|
272
|
+
* @returns an array of changes
|
|
273
|
+
*/
|
|
274
|
+
export function getElementsDiffChanges({
|
|
275
|
+
items,
|
|
276
|
+
lookup,
|
|
277
|
+
}: {
|
|
278
|
+
items: Node[] | undefined;
|
|
279
|
+
lookup: NodeLookup<InternalNode<Node>>;
|
|
280
|
+
}): NodeChange[];
|
|
281
|
+
export function getElementsDiffChanges({
|
|
282
|
+
items,
|
|
283
|
+
lookup,
|
|
284
|
+
}: {
|
|
285
|
+
items: Edge[] | undefined;
|
|
286
|
+
lookup: EdgeLookup;
|
|
287
|
+
}): EdgeChange[];
|
|
288
|
+
export function getElementsDiffChanges({
|
|
289
|
+
items = [],
|
|
290
|
+
lookup,
|
|
291
|
+
}: {
|
|
292
|
+
items: any[] | undefined;
|
|
293
|
+
lookup: Map<string, any>;
|
|
294
|
+
}): any[] {
|
|
295
|
+
const changes: any[] = [];
|
|
296
|
+
const itemsLookup = new Map<string, any>(items.map((item) => [item.id, item]));
|
|
297
|
+
|
|
298
|
+
for (const [index, item] of items.entries()) {
|
|
299
|
+
const lookupItem = lookup.get(item.id);
|
|
300
|
+
const storeItem = lookupItem?.internals?.userNode ?? lookupItem;
|
|
301
|
+
|
|
302
|
+
if (storeItem !== undefined && storeItem !== item) {
|
|
303
|
+
changes.push({ id: item.id, item: item, type: 'replace' });
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
if (storeItem === undefined) {
|
|
307
|
+
changes.push({ item: item, type: 'add', index });
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
for (const [id] of lookup) {
|
|
312
|
+
const nextNode = itemsLookup.get(id);
|
|
313
|
+
|
|
314
|
+
if (nextNode === undefined) {
|
|
315
|
+
changes.push({ id, type: 'remove' });
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
return changes;
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
export function elementToRemoveChange<T extends Node | Edge>(
|
|
323
|
+
item: T,
|
|
324
|
+
): NodeRemoveChange | EdgeRemoveChange {
|
|
325
|
+
return {
|
|
326
|
+
id: item.id,
|
|
327
|
+
type: 'remove',
|
|
328
|
+
};
|
|
329
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import {
|
|
2
|
+
addEdge as addEdgeSystem,
|
|
3
|
+
reconnectEdge as reconnectEdgeSystem,
|
|
4
|
+
createDevWarn,
|
|
5
|
+
type AddEdgeOptions,
|
|
6
|
+
type Connection,
|
|
7
|
+
type EdgeBase,
|
|
8
|
+
type ReconnectEdgeOptions,
|
|
9
|
+
} from '@xyflow/system';
|
|
10
|
+
|
|
11
|
+
const defaultOnError = createDevWarn('React Flow', 'https://reactflow.dev/');
|
|
12
|
+
|
|
13
|
+
export function addEdge<EdgeType extends EdgeBase>(
|
|
14
|
+
edgeParams: EdgeType | Connection,
|
|
15
|
+
edges: EdgeType[],
|
|
16
|
+
options: AddEdgeOptions = {},
|
|
17
|
+
): EdgeType[] {
|
|
18
|
+
return addEdgeSystem(edgeParams, edges, {
|
|
19
|
+
...options,
|
|
20
|
+
onError: options.onError ?? defaultOnError,
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function reconnectEdge<EdgeType extends EdgeBase>(
|
|
25
|
+
oldEdge: EdgeType,
|
|
26
|
+
newConnection: Connection,
|
|
27
|
+
edges: EdgeType[],
|
|
28
|
+
options: ReconnectEdgeOptions = { shouldReplaceId: true },
|
|
29
|
+
): EdgeType[] {
|
|
30
|
+
return reconnectEdgeSystem(oldEdge, newConnection, edges, {
|
|
31
|
+
...options,
|
|
32
|
+
onError: options.onError ?? defaultOnError,
|
|
33
|
+
});
|
|
34
|
+
}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import type { Octane } from 'octane/jsx-runtime';
|
|
2
|
+
import { isNodeBase, isEdgeBase } from '@xyflow/system';
|
|
3
|
+
|
|
4
|
+
import type { Edge, Node } from '../types';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Test whether an object is usable as an [`Node`](/api-reference/types/node).
|
|
8
|
+
* In TypeScript this is a type guard that will narrow the type of whatever you pass in to
|
|
9
|
+
* [`Node`](/api-reference/types/node) if it returns `true`.
|
|
10
|
+
*
|
|
11
|
+
* @public
|
|
12
|
+
* @remarks In TypeScript this is a type guard that will narrow the type of whatever you pass in to Node if it returns true
|
|
13
|
+
* @param element - The element to test.
|
|
14
|
+
* @returns Tests whether the provided value can be used as a `Node`. If you're using TypeScript,
|
|
15
|
+
* this function acts as a type guard and will narrow the type of the value to `Node` if it returns
|
|
16
|
+
* `true`.
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* ```js
|
|
20
|
+
*import { isNode } from '@xyflow/react';
|
|
21
|
+
*
|
|
22
|
+
*if (isNode(node)) {
|
|
23
|
+
* // ...
|
|
24
|
+
*}
|
|
25
|
+
*```
|
|
26
|
+
*/
|
|
27
|
+
export const isNode = <NodeType extends Node = Node>(element: unknown): element is NodeType =>
|
|
28
|
+
isNodeBase<NodeType>(element);
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Test whether an object is usable as an [`Edge`](/api-reference/types/edge).
|
|
32
|
+
* In TypeScript this is a type guard that will narrow the type of whatever you pass in to
|
|
33
|
+
* [`Edge`](/api-reference/types/edge) if it returns `true`.
|
|
34
|
+
*
|
|
35
|
+
* @public
|
|
36
|
+
* @remarks In TypeScript this is a type guard that will narrow the type of whatever you pass in to Edge if it returns true
|
|
37
|
+
* @param element - The element to test
|
|
38
|
+
* @returns Tests whether the provided value can be used as an `Edge`. If you're using TypeScript,
|
|
39
|
+
* this function acts as a type guard and will narrow the type of the value to `Edge` if it returns
|
|
40
|
+
* `true`.
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
* ```js
|
|
44
|
+
*import { isEdge } from '@xyflow/react';
|
|
45
|
+
*
|
|
46
|
+
*if (isEdge(edge)) {
|
|
47
|
+
* // ...
|
|
48
|
+
*}
|
|
49
|
+
*```
|
|
50
|
+
*/
|
|
51
|
+
export const isEdge = <EdgeType extends Edge = Edge>(element: unknown): element is EdgeType =>
|
|
52
|
+
isEdgeBase<EdgeType>(element);
|
|
53
|
+
|
|
54
|
+
// eslint-disable-next-line @typescript-eslint/no-empty-object-type
|
|
55
|
+
export function fixedForwardRef<T, P extends object = Record<string, unknown>>(
|
|
56
|
+
render: (
|
|
57
|
+
props: P,
|
|
58
|
+
ref: ((instance: T | null) => void) | { current: T | null } | null,
|
|
59
|
+
) => Octane.JSX.Element,
|
|
60
|
+
): ((
|
|
61
|
+
props: P & { ref?: ((instance: T | null) => void) | { current: T | null } | null },
|
|
62
|
+
) => Octane.JSX.Element) & {
|
|
63
|
+
displayName?: string;
|
|
64
|
+
} {
|
|
65
|
+
function Forwarded(
|
|
66
|
+
props: P & { ref?: ((instance: T | null) => void) | { current: T | null } | null },
|
|
67
|
+
) {
|
|
68
|
+
const { ref, ...rest } = props;
|
|
69
|
+
return render(rest as P, ref ?? null);
|
|
70
|
+
}
|
|
71
|
+
return Forwarded;
|
|
72
|
+
}
|