@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,68 @@
|
|
|
1
|
+
import { useEffect } from 'octane';
|
|
2
|
+
import { resolveHookSlot, subSlot } from './slot';
|
|
3
|
+
import type { OnViewportChange } from '@xyflow/system';
|
|
4
|
+
|
|
5
|
+
import { useStoreApi } from './useStore';
|
|
6
|
+
|
|
7
|
+
export type UseOnViewportChangeOptions = {
|
|
8
|
+
/** Gets called when the viewport starts changing. */
|
|
9
|
+
onStart?: OnViewportChange;
|
|
10
|
+
/** Gets called when the viewport changes. */
|
|
11
|
+
onChange?: OnViewportChange;
|
|
12
|
+
/** Gets called when the viewport stops changing. */
|
|
13
|
+
onEnd?: OnViewportChange;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* The `useOnViewportChange` hook lets you listen for changes to the viewport such
|
|
18
|
+
* as panning and zooming. You can provide a callback for each phase of a viewport
|
|
19
|
+
* change: `onStart`, `onChange`, and `onEnd`.
|
|
20
|
+
*
|
|
21
|
+
* @public
|
|
22
|
+
* @example
|
|
23
|
+
* ```jsx
|
|
24
|
+
*import { useCallback } from 'octane';
|
|
25
|
+
*import { useOnViewportChange } from '@xyflow/react';
|
|
26
|
+
*
|
|
27
|
+
*function ViewportChangeLogger() {
|
|
28
|
+
* useOnViewportChange({
|
|
29
|
+
* onStart: (viewport: Viewport) => console.log('start', viewport),
|
|
30
|
+
* onChange: (viewport: Viewport) => console.log('change', viewport),
|
|
31
|
+
* onEnd: (viewport: Viewport) => console.log('end', viewport),
|
|
32
|
+
* });
|
|
33
|
+
*
|
|
34
|
+
* return null;
|
|
35
|
+
*}
|
|
36
|
+
*```
|
|
37
|
+
*/
|
|
38
|
+
export function useOnViewportChange(
|
|
39
|
+
{ onStart, onChange, onEnd }: UseOnViewportChangeOptions,
|
|
40
|
+
...rest: [slot?: symbol]
|
|
41
|
+
) {
|
|
42
|
+
const slot = resolveHookSlot(rest);
|
|
43
|
+
const store = useStoreApi(subSlot(slot, 'store'));
|
|
44
|
+
|
|
45
|
+
useEffect(
|
|
46
|
+
function setOnStart() {
|
|
47
|
+
store.setState({ onViewportChangeStart: onStart });
|
|
48
|
+
},
|
|
49
|
+
[onStart],
|
|
50
|
+
subSlot(slot, 'start'),
|
|
51
|
+
);
|
|
52
|
+
|
|
53
|
+
useEffect(
|
|
54
|
+
function setOnChange() {
|
|
55
|
+
store.setState({ onViewportChange: onChange });
|
|
56
|
+
},
|
|
57
|
+
[onChange],
|
|
58
|
+
subSlot(slot, 'change'),
|
|
59
|
+
);
|
|
60
|
+
|
|
61
|
+
useEffect(
|
|
62
|
+
function setOnEnd() {
|
|
63
|
+
store.setState({ onViewportChangeEnd: onEnd });
|
|
64
|
+
},
|
|
65
|
+
[onEnd],
|
|
66
|
+
subSlot(slot, 'end'),
|
|
67
|
+
);
|
|
68
|
+
}
|
|
@@ -0,0 +1,347 @@
|
|
|
1
|
+
import { useMemo } from 'octane';
|
|
2
|
+
import {
|
|
3
|
+
EdgeRemoveChange,
|
|
4
|
+
evaluateAbsolutePosition,
|
|
5
|
+
getElementsToRemove,
|
|
6
|
+
getNodesBounds,
|
|
7
|
+
getOverlappingArea,
|
|
8
|
+
isRectObject,
|
|
9
|
+
NodeRemoveChange,
|
|
10
|
+
nodeToRect,
|
|
11
|
+
withResolvers,
|
|
12
|
+
type Rect,
|
|
13
|
+
} from '@xyflow/system';
|
|
14
|
+
|
|
15
|
+
import useViewportHelper from './useViewportHelper';
|
|
16
|
+
import { useStore, useStoreApi } from './useStore';
|
|
17
|
+
import { useBatchContext } from '../components/BatchProvider/index.tsrx';
|
|
18
|
+
import { resolveHookSlot, subSlot } from './slot';
|
|
19
|
+
import { elementToRemoveChange, isEdge, isNode } from '../utils';
|
|
20
|
+
import type {
|
|
21
|
+
ReactFlowInstance,
|
|
22
|
+
Node,
|
|
23
|
+
Edge,
|
|
24
|
+
InternalNode,
|
|
25
|
+
ReactFlowState,
|
|
26
|
+
GeneralHelpers,
|
|
27
|
+
FitViewOptions,
|
|
28
|
+
} from '../types';
|
|
29
|
+
|
|
30
|
+
const selector = (s: ReactFlowState) => !!s.panZoom;
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* This hook returns a ReactFlowInstance that can be used to update nodes and edges, manipulate the viewport, or query the current state of the flow.
|
|
34
|
+
*
|
|
35
|
+
* @public
|
|
36
|
+
* @example
|
|
37
|
+
* ```jsx
|
|
38
|
+
*import { useCallback, useState } from 'octane';
|
|
39
|
+
*import { useReactFlow } from '@xyflow/react';
|
|
40
|
+
*
|
|
41
|
+
*export function NodeCounter() {
|
|
42
|
+
* const reactFlow = useReactFlow();
|
|
43
|
+
* const [count, setCount] = useState(0);
|
|
44
|
+
* const countNodes = useCallback(() => {
|
|
45
|
+
* setCount(reactFlow.getNodes().length);
|
|
46
|
+
* // you need to pass it as a dependency if you are using it with useEffect or useCallback
|
|
47
|
+
* // because at the first render, it's not initialized yet and some functions might not work.
|
|
48
|
+
* }, [reactFlow]);
|
|
49
|
+
*
|
|
50
|
+
* return (
|
|
51
|
+
* <div>
|
|
52
|
+
* <button onClick={countNodes}>Update count</button>
|
|
53
|
+
* <p>There are {count} nodes in the flow.</p>
|
|
54
|
+
* </div>
|
|
55
|
+
* );
|
|
56
|
+
*}
|
|
57
|
+
*```
|
|
58
|
+
*/
|
|
59
|
+
export function useReactFlow<NodeType extends Node = Node, EdgeType extends Edge = Edge>(
|
|
60
|
+
...rest: [slot?: symbol]
|
|
61
|
+
): ReactFlowInstance<NodeType, EdgeType> {
|
|
62
|
+
const slot = resolveHookSlot(rest);
|
|
63
|
+
const viewportHelper = useViewportHelper(subSlot(slot, 'viewport-helper'));
|
|
64
|
+
const store = useStoreApi<NodeType, EdgeType>(subSlot(slot, 'store'));
|
|
65
|
+
const batchContext = useBatchContext(subSlot(slot, 'batch'));
|
|
66
|
+
const viewportInitialized = useStore(selector, undefined, subSlot(slot, 'viewport-initialized'));
|
|
67
|
+
|
|
68
|
+
const generalHelper = useMemo<GeneralHelpers<NodeType, EdgeType>>(
|
|
69
|
+
function buildGeneralHelper() {
|
|
70
|
+
const getInternalNode: GeneralHelpers<NodeType, EdgeType>['getInternalNode'] = (id) =>
|
|
71
|
+
store.getState().nodeLookup.get(id) as InternalNode<NodeType>;
|
|
72
|
+
|
|
73
|
+
const setNodes: GeneralHelpers<NodeType, EdgeType>['setNodes'] = (payload) => {
|
|
74
|
+
batchContext.nodeQueue.push(payload as NodeType[]);
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
const setEdges: GeneralHelpers<NodeType, EdgeType>['setEdges'] = (payload) => {
|
|
78
|
+
batchContext.edgeQueue.push(payload as EdgeType[]);
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
const getNodeRect = (node: NodeType | { id: string }): Rect | null => {
|
|
82
|
+
const { nodeLookup, nodeOrigin } = store.getState();
|
|
83
|
+
|
|
84
|
+
const nodeToUse = isNode<NodeType>(node) ? node : nodeLookup.get(node.id)!;
|
|
85
|
+
const position = nodeToUse.parentId
|
|
86
|
+
? evaluateAbsolutePosition(
|
|
87
|
+
nodeToUse.position,
|
|
88
|
+
nodeToUse.measured,
|
|
89
|
+
nodeToUse.parentId,
|
|
90
|
+
nodeLookup,
|
|
91
|
+
nodeOrigin,
|
|
92
|
+
)
|
|
93
|
+
: nodeToUse.position;
|
|
94
|
+
|
|
95
|
+
const nodeWithPosition = {
|
|
96
|
+
...nodeToUse,
|
|
97
|
+
position,
|
|
98
|
+
width: nodeToUse.measured?.width ?? nodeToUse.width,
|
|
99
|
+
height: nodeToUse.measured?.height ?? nodeToUse.height,
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
return nodeToRect(nodeWithPosition);
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
const updateNode: GeneralHelpers<NodeType, EdgeType>['updateNode'] = (
|
|
106
|
+
id,
|
|
107
|
+
nodeUpdate,
|
|
108
|
+
options = { replace: false },
|
|
109
|
+
) => {
|
|
110
|
+
setNodes((prevNodes) =>
|
|
111
|
+
prevNodes.map((node) => {
|
|
112
|
+
if (node.id === id) {
|
|
113
|
+
const nextNode = typeof nodeUpdate === 'function' ? nodeUpdate(node) : nodeUpdate;
|
|
114
|
+
return options.replace && isNode(nextNode)
|
|
115
|
+
? (nextNode as NodeType)
|
|
116
|
+
: { ...node, ...nextNode };
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
return node;
|
|
120
|
+
}),
|
|
121
|
+
);
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
const updateEdge: GeneralHelpers<NodeType, EdgeType>['updateEdge'] = (
|
|
125
|
+
id,
|
|
126
|
+
edgeUpdate,
|
|
127
|
+
options = { replace: false },
|
|
128
|
+
) => {
|
|
129
|
+
setEdges((prevEdges) =>
|
|
130
|
+
prevEdges.map((edge) => {
|
|
131
|
+
if (edge.id === id) {
|
|
132
|
+
const nextEdge = typeof edgeUpdate === 'function' ? edgeUpdate(edge) : edgeUpdate;
|
|
133
|
+
return options.replace && isEdge(nextEdge)
|
|
134
|
+
? (nextEdge as EdgeType)
|
|
135
|
+
: { ...edge, ...nextEdge };
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
return edge;
|
|
139
|
+
}),
|
|
140
|
+
);
|
|
141
|
+
};
|
|
142
|
+
|
|
143
|
+
return {
|
|
144
|
+
getNodes: () => store.getState().nodes.map((n) => ({ ...n })) as NodeType[],
|
|
145
|
+
getNode: (id) => getInternalNode(id)?.internals.userNode as NodeType,
|
|
146
|
+
getInternalNode,
|
|
147
|
+
getEdges: () => {
|
|
148
|
+
const { edges = [] } = store.getState();
|
|
149
|
+
return edges.map((e) => ({ ...e })) as EdgeType[];
|
|
150
|
+
},
|
|
151
|
+
getEdge: (id) => store.getState().edgeLookup.get(id) as EdgeType,
|
|
152
|
+
setNodes,
|
|
153
|
+
setEdges,
|
|
154
|
+
addNodes: (payload) => {
|
|
155
|
+
const newNodes = Array.isArray(payload) ? payload : [payload];
|
|
156
|
+
batchContext.nodeQueue.push((nodes) => [...nodes, ...newNodes]);
|
|
157
|
+
},
|
|
158
|
+
addEdges: (payload) => {
|
|
159
|
+
const newEdges = Array.isArray(payload) ? payload : [payload];
|
|
160
|
+
batchContext.edgeQueue.push((edges) => [...edges, ...newEdges]);
|
|
161
|
+
},
|
|
162
|
+
toObject: () => {
|
|
163
|
+
const { nodes = [], edges = [], transform } = store.getState();
|
|
164
|
+
const [x, y, zoom] = transform;
|
|
165
|
+
return {
|
|
166
|
+
nodes: nodes.map((n) => ({ ...n })) as NodeType[],
|
|
167
|
+
edges: edges.map((e) => ({ ...e })) as EdgeType[],
|
|
168
|
+
viewport: {
|
|
169
|
+
x,
|
|
170
|
+
y,
|
|
171
|
+
zoom,
|
|
172
|
+
},
|
|
173
|
+
};
|
|
174
|
+
},
|
|
175
|
+
deleteElements: async ({ nodes: nodesToRemove = [], edges: edgesToRemove = [] }) => {
|
|
176
|
+
const {
|
|
177
|
+
nodes,
|
|
178
|
+
edges,
|
|
179
|
+
onNodesDelete,
|
|
180
|
+
onEdgesDelete,
|
|
181
|
+
triggerNodeChanges,
|
|
182
|
+
triggerEdgeChanges,
|
|
183
|
+
onDelete,
|
|
184
|
+
onBeforeDelete,
|
|
185
|
+
} = store.getState();
|
|
186
|
+
const { nodes: matchingNodes, edges: matchingEdges } = await getElementsToRemove<
|
|
187
|
+
NodeType,
|
|
188
|
+
EdgeType
|
|
189
|
+
>({
|
|
190
|
+
nodesToRemove: nodesToRemove as Partial<NodeType>[],
|
|
191
|
+
edgesToRemove: edgesToRemove as Partial<EdgeType>[],
|
|
192
|
+
nodes,
|
|
193
|
+
edges,
|
|
194
|
+
onBeforeDelete,
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
const hasMatchingEdges = matchingEdges.length > 0;
|
|
198
|
+
const hasMatchingNodes = matchingNodes.length > 0;
|
|
199
|
+
|
|
200
|
+
if (hasMatchingEdges) {
|
|
201
|
+
const edgeChanges: EdgeRemoveChange[] = matchingEdges.map(elementToRemoveChange);
|
|
202
|
+
|
|
203
|
+
onEdgesDelete?.(matchingEdges);
|
|
204
|
+
triggerEdgeChanges(edgeChanges);
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
if (hasMatchingNodes) {
|
|
208
|
+
const nodeChanges: NodeRemoveChange[] = matchingNodes.map(elementToRemoveChange);
|
|
209
|
+
|
|
210
|
+
onNodesDelete?.(matchingNodes);
|
|
211
|
+
triggerNodeChanges(nodeChanges);
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
if (hasMatchingNodes || hasMatchingEdges) {
|
|
215
|
+
onDelete?.({ nodes: matchingNodes, edges: matchingEdges });
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
return { deletedNodes: matchingNodes, deletedEdges: matchingEdges };
|
|
219
|
+
},
|
|
220
|
+
/**
|
|
221
|
+
* Partial is defined as "the 2 nodes/areas are intersecting partially".
|
|
222
|
+
* If a is contained in b or b is contained in a, they are both
|
|
223
|
+
* considered fully intersecting.
|
|
224
|
+
*/
|
|
225
|
+
getIntersectingNodes: (nodeOrRect, partially = true, nodes) => {
|
|
226
|
+
const isRect = isRectObject(nodeOrRect);
|
|
227
|
+
const nodeRect = isRect ? nodeOrRect : getNodeRect(nodeOrRect);
|
|
228
|
+
const hasNodesOption = nodes !== undefined;
|
|
229
|
+
|
|
230
|
+
if (!nodeRect) {
|
|
231
|
+
return [];
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
return (nodes || store.getState().nodes).filter((n) => {
|
|
235
|
+
const internalNode = store.getState().nodeLookup.get(n.id);
|
|
236
|
+
|
|
237
|
+
if (
|
|
238
|
+
internalNode &&
|
|
239
|
+
!isRect &&
|
|
240
|
+
(n.id === nodeOrRect.id || !internalNode.internals.positionAbsolute)
|
|
241
|
+
) {
|
|
242
|
+
return false;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
const currNodeRect = nodeToRect(hasNodesOption ? n : internalNode!);
|
|
246
|
+
const overlappingArea = getOverlappingArea(currNodeRect, nodeRect);
|
|
247
|
+
const partiallyVisible = partially && overlappingArea > 0;
|
|
248
|
+
|
|
249
|
+
return (
|
|
250
|
+
partiallyVisible ||
|
|
251
|
+
overlappingArea >= currNodeRect.width * currNodeRect.height ||
|
|
252
|
+
overlappingArea >= nodeRect.width * nodeRect.height
|
|
253
|
+
);
|
|
254
|
+
}) as NodeType[];
|
|
255
|
+
},
|
|
256
|
+
isNodeIntersecting: (nodeOrRect, area, partially = true) => {
|
|
257
|
+
const isRect = isRectObject(nodeOrRect);
|
|
258
|
+
const nodeRect = isRect ? nodeOrRect : getNodeRect(nodeOrRect);
|
|
259
|
+
|
|
260
|
+
if (!nodeRect) {
|
|
261
|
+
return false;
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
const overlappingArea = getOverlappingArea(nodeRect, area);
|
|
265
|
+
const partiallyVisible = partially && overlappingArea > 0;
|
|
266
|
+
|
|
267
|
+
return (
|
|
268
|
+
partiallyVisible ||
|
|
269
|
+
overlappingArea >= area.width * area.height ||
|
|
270
|
+
overlappingArea >= nodeRect.width * nodeRect.height
|
|
271
|
+
);
|
|
272
|
+
},
|
|
273
|
+
updateNode,
|
|
274
|
+
updateNodeData: (id, dataUpdate, options = { replace: false }) => {
|
|
275
|
+
updateNode(
|
|
276
|
+
id,
|
|
277
|
+
(node) => {
|
|
278
|
+
const nextData = typeof dataUpdate === 'function' ? dataUpdate(node) : dataUpdate;
|
|
279
|
+
return options.replace
|
|
280
|
+
? { ...node, data: nextData }
|
|
281
|
+
: { ...node, data: { ...node.data, ...nextData } };
|
|
282
|
+
},
|
|
283
|
+
options,
|
|
284
|
+
);
|
|
285
|
+
},
|
|
286
|
+
updateEdge,
|
|
287
|
+
updateEdgeData: (id, dataUpdate, options = { replace: false }) => {
|
|
288
|
+
updateEdge(
|
|
289
|
+
id,
|
|
290
|
+
(edge) => {
|
|
291
|
+
const nextData = typeof dataUpdate === 'function' ? dataUpdate(edge) : dataUpdate;
|
|
292
|
+
return options.replace
|
|
293
|
+
? { ...edge, data: nextData }
|
|
294
|
+
: { ...edge, data: { ...edge.data, ...nextData } };
|
|
295
|
+
},
|
|
296
|
+
options,
|
|
297
|
+
);
|
|
298
|
+
},
|
|
299
|
+
getNodesBounds: (nodes: (NodeType | InternalNode | string)[]): Rect => {
|
|
300
|
+
const { nodeLookup, nodeOrigin } = store.getState();
|
|
301
|
+
return getNodesBounds(nodes, { nodeLookup, nodeOrigin });
|
|
302
|
+
},
|
|
303
|
+
getHandleConnections: ({ type, id, nodeId }) =>
|
|
304
|
+
Array.from(
|
|
305
|
+
store
|
|
306
|
+
.getState()
|
|
307
|
+
.connectionLookup.get(`${nodeId}-${type}${id ? `-${id}` : ''}`)
|
|
308
|
+
?.values() ?? [],
|
|
309
|
+
),
|
|
310
|
+
getNodeConnections: ({ type, handleId, nodeId }) =>
|
|
311
|
+
Array.from(
|
|
312
|
+
store
|
|
313
|
+
.getState()
|
|
314
|
+
.connectionLookup.get(
|
|
315
|
+
`${nodeId}${type ? (handleId ? `-${type}-${handleId}` : `-${type}`) : ''}`,
|
|
316
|
+
)
|
|
317
|
+
?.values() ?? [],
|
|
318
|
+
),
|
|
319
|
+
fitView: async (options: FitViewOptions<NodeType> | undefined) => {
|
|
320
|
+
// We either create a new Promise or reuse the existing one
|
|
321
|
+
// Even if fitView is called multiple times in a row, we only end up with a single Promise
|
|
322
|
+
const fitViewResolver = store.getState().fitViewResolver ?? withResolvers<boolean>();
|
|
323
|
+
|
|
324
|
+
// We schedule a fitView by setting fitViewQueued and triggering a setNodes
|
|
325
|
+
store.setState({ fitViewQueued: true, fitViewOptions: options, fitViewResolver });
|
|
326
|
+
batchContext.nodeQueue.push((nodes) => [...nodes]);
|
|
327
|
+
|
|
328
|
+
return fitViewResolver.promise;
|
|
329
|
+
},
|
|
330
|
+
};
|
|
331
|
+
},
|
|
332
|
+
[],
|
|
333
|
+
subSlot(slot, 'general-helper'),
|
|
334
|
+
);
|
|
335
|
+
|
|
336
|
+
return useMemo(
|
|
337
|
+
function combineHelpers() {
|
|
338
|
+
return {
|
|
339
|
+
...generalHelper,
|
|
340
|
+
...viewportHelper,
|
|
341
|
+
viewportInitialized,
|
|
342
|
+
};
|
|
343
|
+
},
|
|
344
|
+
[viewportInitialized, generalHelper, viewportHelper],
|
|
345
|
+
subSlot(slot, 'combined-helper'),
|
|
346
|
+
);
|
|
347
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { useEffect } from 'octane';
|
|
2
|
+
import type { RefObject } from '../react-shim.js';
|
|
3
|
+
import { resolveHookSlot } from './slot';
|
|
4
|
+
import { errorMessages, getDimensions } from '@xyflow/system';
|
|
5
|
+
|
|
6
|
+
import { useStoreApi } from '../hooks/useStore';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Hook for handling resize events.
|
|
10
|
+
*
|
|
11
|
+
* @internal
|
|
12
|
+
*/
|
|
13
|
+
export function useResizeHandler(
|
|
14
|
+
domNode: RefObject<HTMLDivElement | null>,
|
|
15
|
+
...rest: [slot?: symbol]
|
|
16
|
+
): void {
|
|
17
|
+
const slot = resolveHookSlot(rest);
|
|
18
|
+
const store = useStoreApi(slot);
|
|
19
|
+
|
|
20
|
+
useEffect(
|
|
21
|
+
function observeResize() {
|
|
22
|
+
const updateDimensions = () => {
|
|
23
|
+
if (!domNode.current || !(domNode.current.checkVisibility?.() ?? true)) {
|
|
24
|
+
return false;
|
|
25
|
+
}
|
|
26
|
+
const size = getDimensions(domNode.current);
|
|
27
|
+
|
|
28
|
+
if (size.height === 0 || size.width === 0) {
|
|
29
|
+
store.getState().onError?.('004', errorMessages['error004']());
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
store.setState({ width: size.width || 500, height: size.height || 500 });
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
if (domNode.current) {
|
|
36
|
+
updateDimensions();
|
|
37
|
+
window.addEventListener('resize', updateDimensions);
|
|
38
|
+
|
|
39
|
+
const resizeObserver = new ResizeObserver(() => updateDimensions());
|
|
40
|
+
resizeObserver.observe(domNode.current);
|
|
41
|
+
|
|
42
|
+
return () => {
|
|
43
|
+
window.removeEventListener('resize', updateDimensions);
|
|
44
|
+
|
|
45
|
+
if (resizeObserver && domNode.current) {
|
|
46
|
+
resizeObserver.unobserve(domNode.current);
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
},
|
|
51
|
+
[],
|
|
52
|
+
slot,
|
|
53
|
+
);
|
|
54
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { useContext, useMemo } from 'octane';
|
|
2
|
+
import { useStoreWithEqualityFn as useZustandStore } from '@octanejs/zustand/traditional';
|
|
3
|
+
import type { StoreApi } from '@octanejs/zustand';
|
|
4
|
+
import { errorMessages } from '@xyflow/system';
|
|
5
|
+
|
|
6
|
+
import StoreContext from '../contexts/StoreContext';
|
|
7
|
+
import type { Edge, Node, ReactFlowState } from '../types';
|
|
8
|
+
|
|
9
|
+
const zustandErrorMessage = errorMessages['error001']('react');
|
|
10
|
+
|
|
11
|
+
type EqualityStoreHook = <TState, StateSlice>(
|
|
12
|
+
api: StoreApi<TState>,
|
|
13
|
+
selector: (state: TState) => StateSlice,
|
|
14
|
+
equalityFn: ((a: StateSlice, b: StateSlice) => boolean) | undefined,
|
|
15
|
+
slot: symbol | undefined,
|
|
16
|
+
) => StateSlice;
|
|
17
|
+
|
|
18
|
+
function useStore<StateSlice = unknown>(
|
|
19
|
+
selector: (state: ReactFlowState) => StateSlice,
|
|
20
|
+
equalityFn?: (a: StateSlice, b: StateSlice) => boolean,
|
|
21
|
+
...rest: [slot?: symbol]
|
|
22
|
+
): StateSlice {
|
|
23
|
+
const tail = rest[rest.length - 1];
|
|
24
|
+
const slot = typeof tail === 'symbol' ? (tail as symbol) : undefined;
|
|
25
|
+
const store = useContext(StoreContext);
|
|
26
|
+
|
|
27
|
+
if (store === null) {
|
|
28
|
+
throw new Error(zustandErrorMessage);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
return (useZustandStore as EqualityStoreHook)(store, selector, equalityFn, slot);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function useStoreApi<NodeType extends Node = Node, EdgeType extends Edge = Edge>(
|
|
35
|
+
...rest: [slot?: symbol]
|
|
36
|
+
) {
|
|
37
|
+
const tail = rest[rest.length - 1];
|
|
38
|
+
const slot = typeof tail === 'symbol' ? (tail as symbol) : undefined;
|
|
39
|
+
const store = useContext(StoreContext) as StoreApi<ReactFlowState<NodeType, EdgeType>> | null;
|
|
40
|
+
|
|
41
|
+
if (store === null) {
|
|
42
|
+
throw new Error(zustandErrorMessage);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
return useMemo(
|
|
46
|
+
function memoStoreApi() {
|
|
47
|
+
return {
|
|
48
|
+
getState: store.getState,
|
|
49
|
+
setState: store.setState,
|
|
50
|
+
subscribe: store.subscribe,
|
|
51
|
+
};
|
|
52
|
+
},
|
|
53
|
+
[store],
|
|
54
|
+
slot,
|
|
55
|
+
);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export { useStore, useStoreApi };
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { useCallback } from 'octane';
|
|
2
|
+
import { resolveHookSlot } from './slot';
|
|
3
|
+
import type { UpdateNodeInternals, InternalNodeUpdate } from '@xyflow/system';
|
|
4
|
+
|
|
5
|
+
import { useStoreApi } from '../hooks/useStore';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* When you programmatically add or remove handles to a node or update a node's
|
|
9
|
+
* handle position, you need to let React Flow know about it using this hook. This
|
|
10
|
+
* will update the internal dimensions of the node and properly reposition handles
|
|
11
|
+
* on the canvas if necessary.
|
|
12
|
+
*
|
|
13
|
+
* @public
|
|
14
|
+
* @returns Use this function to tell React Flow to update the internal state of one or more nodes
|
|
15
|
+
* that you have changed programmatically.
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```jsx
|
|
19
|
+
*import { useCallback, useState } from 'octane';
|
|
20
|
+
*import { Handle, useUpdateNodeInternals } from '@xyflow/react';
|
|
21
|
+
*
|
|
22
|
+
*export default function RandomHandleNode({ id }) {
|
|
23
|
+
* const updateNodeInternals = useUpdateNodeInternals();
|
|
24
|
+
* const [handleCount, setHandleCount] = useState(0);
|
|
25
|
+
* const randomizeHandleCount = useCallback(() => {
|
|
26
|
+
* setHandleCount(Math.floor(Math.random() * 10));
|
|
27
|
+
* updateNodeInternals(id);
|
|
28
|
+
* }, [id, updateNodeInternals]);
|
|
29
|
+
*
|
|
30
|
+
* return (
|
|
31
|
+
* <>
|
|
32
|
+
* {Array.from({ length: handleCount }).map((_, index) => (
|
|
33
|
+
* <Handle
|
|
34
|
+
* key={index}
|
|
35
|
+
* type="target"
|
|
36
|
+
* position="left"
|
|
37
|
+
* id={`handle-${index}`}
|
|
38
|
+
* />
|
|
39
|
+
* ))}
|
|
40
|
+
*
|
|
41
|
+
* <div>
|
|
42
|
+
* <button onClick={randomizeHandleCount}>Randomize handle count</button>
|
|
43
|
+
* <p>There are {handleCount} handles on this node.</p>
|
|
44
|
+
* </div>
|
|
45
|
+
* </>
|
|
46
|
+
* );
|
|
47
|
+
*}
|
|
48
|
+
*```
|
|
49
|
+
* @remarks This hook can only be used in a component that is a child of a
|
|
50
|
+
*{@link ReactFlowProvider} or a {@link ReactFlow} component.
|
|
51
|
+
*/
|
|
52
|
+
export function useUpdateNodeInternals(...rest: [slot?: symbol]): UpdateNodeInternals {
|
|
53
|
+
const slot = resolveHookSlot(rest);
|
|
54
|
+
const store = useStoreApi(slot);
|
|
55
|
+
|
|
56
|
+
return useCallback<UpdateNodeInternals>((id: string | string[]) => {
|
|
57
|
+
const { domNode, updateNodeInternals } = store.getState();
|
|
58
|
+
const updateIds = Array.isArray(id) ? id : [id];
|
|
59
|
+
const updates = new Map<string, InternalNodeUpdate>();
|
|
60
|
+
|
|
61
|
+
updateIds.forEach((updateId) => {
|
|
62
|
+
const nodeElement = domNode?.querySelector(
|
|
63
|
+
`.react-flow__node[data-id="${updateId}"]`,
|
|
64
|
+
) as HTMLDivElement;
|
|
65
|
+
|
|
66
|
+
if (nodeElement) {
|
|
67
|
+
updates.set(updateId, { id: updateId, nodeElement, force: true });
|
|
68
|
+
}
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
requestAnimationFrame(() => updateNodeInternals(updates, { triggerFitView: false }));
|
|
72
|
+
}, []);
|
|
73
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { shallow } from '@octanejs/zustand/shallow';
|
|
2
|
+
import { resolveHookSlot } from './slot';
|
|
3
|
+
import type { Viewport } from '@xyflow/system';
|
|
4
|
+
|
|
5
|
+
import { useStore } from '../hooks/useStore';
|
|
6
|
+
import type { ReactFlowState } from '../types';
|
|
7
|
+
|
|
8
|
+
const viewportSelector = (state: ReactFlowState) => ({
|
|
9
|
+
x: state.transform[0],
|
|
10
|
+
y: state.transform[1],
|
|
11
|
+
zoom: state.transform[2],
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* The `useViewport` hook is a convenient way to read the current state of the
|
|
16
|
+
* {@link Viewport} in a component. Components that use this hook
|
|
17
|
+
* will re-render **whenever the viewport changes**.
|
|
18
|
+
*
|
|
19
|
+
* @public
|
|
20
|
+
* @returns The current viewport.
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
*
|
|
24
|
+
*```jsx
|
|
25
|
+
*import { useViewport } from '@xyflow/react';
|
|
26
|
+
*
|
|
27
|
+
*export default function ViewportDisplay() {
|
|
28
|
+
* const { x, y, zoom } = useViewport();
|
|
29
|
+
*
|
|
30
|
+
* return (
|
|
31
|
+
* <div>
|
|
32
|
+
* <p>
|
|
33
|
+
* The viewport is currently at ({x}, {y}) and zoomed to {zoom}.
|
|
34
|
+
* </p>
|
|
35
|
+
* </div>
|
|
36
|
+
* );
|
|
37
|
+
*}
|
|
38
|
+
*```
|
|
39
|
+
*
|
|
40
|
+
* @remarks This hook can only be used in a component that is a child of a
|
|
41
|
+
*{@link ReactFlowProvider} or a {@link ReactFlow} component.
|
|
42
|
+
*/
|
|
43
|
+
export function useViewport(...rest: [slot?: symbol]): Viewport {
|
|
44
|
+
const slot = resolveHookSlot(rest);
|
|
45
|
+
const viewport = useStore(viewportSelector, shallow, slot);
|
|
46
|
+
|
|
47
|
+
return viewport;
|
|
48
|
+
}
|