@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,259 @@
|
|
|
1
|
+
import { memo } from 'octane';
|
|
2
|
+
import type { KeyboardEvent, MouseEvent } from '../../react-shim.js';
|
|
3
|
+
import cc from 'classcat';
|
|
4
|
+
import { shallow } from '@octanejs/zustand/shallow';
|
|
5
|
+
import {
|
|
6
|
+
elementSelectionKeys,
|
|
7
|
+
errorMessages,
|
|
8
|
+
getNodeDimensions,
|
|
9
|
+
isInputDOMNode,
|
|
10
|
+
nodeHasDimensions,
|
|
11
|
+
getNodesInside,
|
|
12
|
+
} from '@xyflow/system';
|
|
13
|
+
|
|
14
|
+
import { useStore, useStoreApi } from '../../hooks/useStore';
|
|
15
|
+
import { Provider } from '../../contexts/NodeIdContext';
|
|
16
|
+
import { ARIA_NODE_DESC_KEY } from '../A11yDescriptions/index.tsrx';
|
|
17
|
+
import { useDrag } from '../../hooks/useDrag';
|
|
18
|
+
import { useMoveSelectedNodes } from '../../hooks/useMoveSelectedNodes';
|
|
19
|
+
import { handleNodeClick } from '../Nodes/utils';
|
|
20
|
+
import { arrowKeyDiffs, builtinNodeTypes, getNodeInlineStyleDimensions } from './utils.tsrx';
|
|
21
|
+
import { useNodeObserver } from './useNodeObserver';
|
|
22
|
+
import type { InternalNode, Node, NodeWrapperProps } from '../../types';
|
|
23
|
+
|
|
24
|
+
function NodeWrapper<NodeType extends Node>({
|
|
25
|
+
id,
|
|
26
|
+
onClick,
|
|
27
|
+
onMouseEnter,
|
|
28
|
+
onMouseMove,
|
|
29
|
+
onMouseLeave,
|
|
30
|
+
onContextMenu,
|
|
31
|
+
onDoubleClick,
|
|
32
|
+
nodesDraggable,
|
|
33
|
+
elementsSelectable,
|
|
34
|
+
nodesConnectable,
|
|
35
|
+
nodesFocusable,
|
|
36
|
+
resizeObserver,
|
|
37
|
+
noDragClassName,
|
|
38
|
+
noPanClassName,
|
|
39
|
+
disableKeyboardA11y,
|
|
40
|
+
rfId,
|
|
41
|
+
nodeTypes,
|
|
42
|
+
nodeClickDistance,
|
|
43
|
+
onError,
|
|
44
|
+
}: NodeWrapperProps<NodeType>) {
|
|
45
|
+
const { node, internals, isParent } = useStore((s) => {
|
|
46
|
+
const node = s.nodeLookup.get(id)! as InternalNode<NodeType>;
|
|
47
|
+
const isParent = s.parentLookup.has(id);
|
|
48
|
+
|
|
49
|
+
return {
|
|
50
|
+
node,
|
|
51
|
+
internals: node.internals,
|
|
52
|
+
isParent,
|
|
53
|
+
};
|
|
54
|
+
}, shallow);
|
|
55
|
+
|
|
56
|
+
let nodeType = node.type || 'default';
|
|
57
|
+
let NodeComponent = nodeTypes?.[nodeType] || builtinNodeTypes[nodeType];
|
|
58
|
+
|
|
59
|
+
if (NodeComponent === undefined) {
|
|
60
|
+
onError?.('003', errorMessages['error003'](nodeType));
|
|
61
|
+
nodeType = 'default';
|
|
62
|
+
NodeComponent = nodeTypes?.['default'] || builtinNodeTypes.default;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const isDraggable = !!(node.draggable || nodesDraggable && typeof node.draggable === 'undefined');
|
|
66
|
+
const isSelectable = !!(node.selectable ||
|
|
67
|
+
elementsSelectable && typeof node.selectable === 'undefined');
|
|
68
|
+
const isConnectable = !!(node.connectable ||
|
|
69
|
+
nodesConnectable && typeof node.connectable === 'undefined');
|
|
70
|
+
const isFocusable = !!(node.focusable || nodesFocusable && typeof node.focusable === 'undefined');
|
|
71
|
+
|
|
72
|
+
const store = useStoreApi();
|
|
73
|
+
const hasDimensions = nodeHasDimensions(node);
|
|
74
|
+
const nodeRef = useNodeObserver({ node, nodeType, hasDimensions, resizeObserver });
|
|
75
|
+
const dragging = useDrag({
|
|
76
|
+
nodeRef,
|
|
77
|
+
disabled: node.hidden || !isDraggable,
|
|
78
|
+
noDragClassName,
|
|
79
|
+
handleSelector: node.dragHandle,
|
|
80
|
+
nodeId: id,
|
|
81
|
+
isSelectable,
|
|
82
|
+
nodeClickDistance,
|
|
83
|
+
});
|
|
84
|
+
const moveSelectedNodes = useMoveSelectedNodes();
|
|
85
|
+
|
|
86
|
+
if (node.hidden) {
|
|
87
|
+
return null;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
const nodeDimensions = getNodeDimensions(node);
|
|
91
|
+
const inlineDimensions = getNodeInlineStyleDimensions(node);
|
|
92
|
+
|
|
93
|
+
const hasPointerEvents =
|
|
94
|
+
isSelectable || isDraggable || onClick || onMouseEnter || onMouseMove || onMouseLeave;
|
|
95
|
+
|
|
96
|
+
const onMouseEnterHandler = onMouseEnter
|
|
97
|
+
? (event: MouseEvent) => onMouseEnter(event, { ...internals.userNode })
|
|
98
|
+
: undefined;
|
|
99
|
+
const onMouseMoveHandler = onMouseMove
|
|
100
|
+
? (event: MouseEvent) => onMouseMove(event, { ...internals.userNode })
|
|
101
|
+
: undefined;
|
|
102
|
+
const onMouseLeaveHandler = onMouseLeave
|
|
103
|
+
? (event: MouseEvent) => onMouseLeave(event, { ...internals.userNode })
|
|
104
|
+
: undefined;
|
|
105
|
+
const onContextMenuHandler = onContextMenu
|
|
106
|
+
? (event: MouseEvent) => onContextMenu(event, { ...internals.userNode })
|
|
107
|
+
: undefined;
|
|
108
|
+
const onDoubleClickHandler = onDoubleClick
|
|
109
|
+
? (event: MouseEvent) => onDoubleClick(event, { ...internals.userNode })
|
|
110
|
+
: undefined;
|
|
111
|
+
|
|
112
|
+
const onSelectNodeHandler = (event: MouseEvent) => {
|
|
113
|
+
const { selectNodesOnDrag, nodeDragThreshold } = store.getState();
|
|
114
|
+
|
|
115
|
+
if (isSelectable && (!selectNodesOnDrag || !isDraggable || nodeDragThreshold > 0)) {
|
|
116
|
+
/*
|
|
117
|
+
* this handler gets called by XYDrag on drag start when selectNodesOnDrag=true
|
|
118
|
+
* here we only need to call it when selectNodesOnDrag=false
|
|
119
|
+
*/
|
|
120
|
+
handleNodeClick({
|
|
121
|
+
id,
|
|
122
|
+
store,
|
|
123
|
+
nodeRef,
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
if (onClick) {
|
|
128
|
+
onClick(event, { ...internals.userNode });
|
|
129
|
+
}
|
|
130
|
+
};
|
|
131
|
+
|
|
132
|
+
const onKeyDown = (event: KeyboardEvent) => {
|
|
133
|
+
if (isInputDOMNode(event) || disableKeyboardA11y) {
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
if (elementSelectionKeys.includes(event.key) && isSelectable) {
|
|
138
|
+
const unselect = event.key === 'Escape';
|
|
139
|
+
|
|
140
|
+
handleNodeClick({
|
|
141
|
+
id,
|
|
142
|
+
store,
|
|
143
|
+
unselect,
|
|
144
|
+
nodeRef,
|
|
145
|
+
});
|
|
146
|
+
} else if (
|
|
147
|
+
isDraggable && node.selected && Object.prototype.hasOwnProperty.call(arrowKeyDiffs, event.key)
|
|
148
|
+
) {
|
|
149
|
+
// prevent default scrolling behavior on arrow key press when node is moved
|
|
150
|
+
event.preventDefault();
|
|
151
|
+
|
|
152
|
+
const { ariaLabelConfig } = store.getState();
|
|
153
|
+
|
|
154
|
+
store.setState({
|
|
155
|
+
ariaLiveMessage: ariaLabelConfig['node.a11yDescription.ariaLiveMessage']({
|
|
156
|
+
direction: event.key.replace('Arrow', '').toLowerCase(),
|
|
157
|
+
x: ~~internals.positionAbsolute.x,
|
|
158
|
+
y: ~~internals.positionAbsolute.y,
|
|
159
|
+
}),
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
moveSelectedNodes({
|
|
163
|
+
direction: arrowKeyDiffs[event.key],
|
|
164
|
+
factor: event.shiftKey ? 4 : 1,
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
};
|
|
168
|
+
|
|
169
|
+
const onFocus = () => {
|
|
170
|
+
if (disableKeyboardA11y || !nodeRef.current?.matches(':focus-visible')) {
|
|
171
|
+
return;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
const { transform, width, height, autoPanOnNodeFocus, setCenter } = store.getState();
|
|
175
|
+
|
|
176
|
+
if (!autoPanOnNodeFocus) {
|
|
177
|
+
return;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
const withinViewport =
|
|
181
|
+
getNodesInside(new Map([[id, node]]), { x: 0, y: 0, width, height }, transform, true).length >
|
|
182
|
+
0;
|
|
183
|
+
|
|
184
|
+
if (!withinViewport) {
|
|
185
|
+
setCenter(
|
|
186
|
+
node.position.x + nodeDimensions.width / 2,
|
|
187
|
+
node.position.y + nodeDimensions.height / 2,
|
|
188
|
+
{
|
|
189
|
+
zoom: transform[2],
|
|
190
|
+
},
|
|
191
|
+
);
|
|
192
|
+
}
|
|
193
|
+
};
|
|
194
|
+
|
|
195
|
+
return <div
|
|
196
|
+
className={cc([
|
|
197
|
+
'react-flow__node',
|
|
198
|
+
`react-flow__node-${nodeType}`,
|
|
199
|
+
{ [noPanClassName]: isDraggable },
|
|
200
|
+
node.className,
|
|
201
|
+
{
|
|
202
|
+
selected: node.selected,
|
|
203
|
+
selectable: isSelectable,
|
|
204
|
+
parent: isParent,
|
|
205
|
+
draggable: isDraggable,
|
|
206
|
+
dragging,
|
|
207
|
+
},
|
|
208
|
+
])}
|
|
209
|
+
ref={nodeRef}
|
|
210
|
+
style={{
|
|
211
|
+
zIndex: internals.z,
|
|
212
|
+
transform: `translate(${internals.positionAbsolute.x}px,${internals.positionAbsolute.y}px)`,
|
|
213
|
+
pointerEvents: hasPointerEvents ? 'all' : 'none',
|
|
214
|
+
visibility: hasDimensions ? 'visible' : 'hidden',
|
|
215
|
+
...node.style,
|
|
216
|
+
...inlineDimensions,
|
|
217
|
+
}}
|
|
218
|
+
data-id={id}
|
|
219
|
+
data-testid={`rf__node-${id}`}
|
|
220
|
+
onMouseEnter={onMouseEnterHandler}
|
|
221
|
+
onMouseMove={onMouseMoveHandler}
|
|
222
|
+
onMouseLeave={onMouseLeaveHandler}
|
|
223
|
+
onContextMenu={onContextMenuHandler}
|
|
224
|
+
onClick={onSelectNodeHandler}
|
|
225
|
+
onDoubleClick={onDoubleClickHandler}
|
|
226
|
+
onKeyDown={isFocusable ? onKeyDown : undefined}
|
|
227
|
+
tabIndex={isFocusable ? 0 : undefined}
|
|
228
|
+
onFocus={isFocusable ? onFocus : undefined}
|
|
229
|
+
role={node.ariaRole ?? (isFocusable ? 'group' : undefined)}
|
|
230
|
+
aria-roledescription="node"
|
|
231
|
+
aria-describedby={disableKeyboardA11y ? undefined : `${ARIA_NODE_DESC_KEY}-${rfId}`}
|
|
232
|
+
aria-label={node.ariaLabel}
|
|
233
|
+
{...node.domAttributes}
|
|
234
|
+
>
|
|
235
|
+
<Provider value={id}>
|
|
236
|
+
<NodeComponent
|
|
237
|
+
id={id}
|
|
238
|
+
data={node.data}
|
|
239
|
+
type={nodeType}
|
|
240
|
+
positionAbsoluteX={internals.positionAbsolute.x}
|
|
241
|
+
positionAbsoluteY={internals.positionAbsolute.y}
|
|
242
|
+
selected={node.selected ?? false}
|
|
243
|
+
selectable={isSelectable}
|
|
244
|
+
draggable={isDraggable}
|
|
245
|
+
deletable={node.deletable ?? true}
|
|
246
|
+
isConnectable={isConnectable}
|
|
247
|
+
sourcePosition={node.sourcePosition}
|
|
248
|
+
targetPosition={node.targetPosition}
|
|
249
|
+
dragging={dragging}
|
|
250
|
+
dragHandle={node.dragHandle}
|
|
251
|
+
zIndex={internals.z}
|
|
252
|
+
parentId={node.parentId}
|
|
253
|
+
{...nodeDimensions}
|
|
254
|
+
/>
|
|
255
|
+
</Provider>
|
|
256
|
+
</div>;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
export default memo(NodeWrapper) as typeof NodeWrapper;
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { useEffect, useRef } from 'octane';
|
|
2
|
+
|
|
3
|
+
import type { InternalNode } from '../../types';
|
|
4
|
+
import { useStoreApi } from '../../hooks/useStore';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Hook to handle the resize observation + internal updates for the passed node.
|
|
8
|
+
*
|
|
9
|
+
* @internal
|
|
10
|
+
* @returns nodeRef - reference to the node element
|
|
11
|
+
*/
|
|
12
|
+
export function useNodeObserver({
|
|
13
|
+
node,
|
|
14
|
+
nodeType,
|
|
15
|
+
hasDimensions,
|
|
16
|
+
resizeObserver,
|
|
17
|
+
}: {
|
|
18
|
+
node: InternalNode;
|
|
19
|
+
nodeType: string;
|
|
20
|
+
hasDimensions: boolean;
|
|
21
|
+
resizeObserver: ResizeObserver | null;
|
|
22
|
+
}) {
|
|
23
|
+
const store = useStoreApi();
|
|
24
|
+
const nodeRef = useRef<HTMLDivElement | null>(null);
|
|
25
|
+
const observedNode = useRef<HTMLDivElement | null>(null);
|
|
26
|
+
const prevSourcePosition = useRef(node.sourcePosition);
|
|
27
|
+
const prevTargetPosition = useRef(node.targetPosition);
|
|
28
|
+
const prevType = useRef(nodeType);
|
|
29
|
+
const isInitialized = hasDimensions && !!node.internals.handleBounds;
|
|
30
|
+
|
|
31
|
+
useEffect(() => {
|
|
32
|
+
if (
|
|
33
|
+
nodeRef.current &&
|
|
34
|
+
!node.hidden &&
|
|
35
|
+
(!isInitialized || observedNode.current !== nodeRef.current)
|
|
36
|
+
) {
|
|
37
|
+
if (observedNode.current) {
|
|
38
|
+
resizeObserver?.unobserve(observedNode.current);
|
|
39
|
+
}
|
|
40
|
+
resizeObserver?.observe(nodeRef.current);
|
|
41
|
+
observedNode.current = nodeRef.current;
|
|
42
|
+
}
|
|
43
|
+
}, [isInitialized, node.hidden]);
|
|
44
|
+
|
|
45
|
+
useEffect(() => {
|
|
46
|
+
return () => {
|
|
47
|
+
if (observedNode.current) {
|
|
48
|
+
resizeObserver?.unobserve(observedNode.current);
|
|
49
|
+
observedNode.current = null;
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
}, []);
|
|
53
|
+
|
|
54
|
+
useEffect(() => {
|
|
55
|
+
if (nodeRef.current) {
|
|
56
|
+
/*
|
|
57
|
+
* when the user programmatically changes the source or handle position, we need to update the internals
|
|
58
|
+
* to make sure the edges are updated correctly
|
|
59
|
+
*/
|
|
60
|
+
const typeChanged = prevType.current !== nodeType;
|
|
61
|
+
const sourcePosChanged = prevSourcePosition.current !== node.sourcePosition;
|
|
62
|
+
const targetPosChanged = prevTargetPosition.current !== node.targetPosition;
|
|
63
|
+
|
|
64
|
+
if (typeChanged || sourcePosChanged || targetPosChanged) {
|
|
65
|
+
prevType.current = nodeType;
|
|
66
|
+
prevSourcePosition.current = node.sourcePosition;
|
|
67
|
+
prevTargetPosition.current = node.targetPosition;
|
|
68
|
+
|
|
69
|
+
store
|
|
70
|
+
.getState()
|
|
71
|
+
.updateNodeInternals(
|
|
72
|
+
new Map([[node.id, { id: node.id, nodeElement: nodeRef.current, force: true }]]),
|
|
73
|
+
);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
}, [node.id, nodeType, node.sourcePosition, node.targetPosition]);
|
|
77
|
+
|
|
78
|
+
return nodeRef;
|
|
79
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import type { XYPosition } from '@xyflow/system';
|
|
2
|
+
|
|
3
|
+
import { InputNode } from '../Nodes/InputNode.tsrx';
|
|
4
|
+
import { DefaultNode } from '../Nodes/DefaultNode.tsrx';
|
|
5
|
+
import { GroupNode } from '../Nodes/GroupNode.tsrx';
|
|
6
|
+
import { OutputNode } from '../Nodes/OutputNode.tsrx';
|
|
7
|
+
import type { InternalNode, Node, NodeTypes } from '../../types';
|
|
8
|
+
|
|
9
|
+
export const arrowKeyDiffs: Record<string, XYPosition> = {
|
|
10
|
+
ArrowUp: { x: 0, y: -1 },
|
|
11
|
+
ArrowDown: { x: 0, y: 1 },
|
|
12
|
+
ArrowLeft: { x: -1, y: 0 },
|
|
13
|
+
ArrowRight: { x: 1, y: 0 },
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
export const builtinNodeTypes: NodeTypes = {
|
|
17
|
+
input: InputNode,
|
|
18
|
+
default: DefaultNode,
|
|
19
|
+
output: OutputNode,
|
|
20
|
+
group: GroupNode,
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
export function getNodeInlineStyleDimensions<NodeType extends Node = Node>(
|
|
24
|
+
node: InternalNode<NodeType>,
|
|
25
|
+
): {
|
|
26
|
+
width: number | string | undefined;
|
|
27
|
+
height: number | string | undefined;
|
|
28
|
+
} {
|
|
29
|
+
if (node.internals.handleBounds === undefined) {
|
|
30
|
+
return {
|
|
31
|
+
width: node.width ?? node.initialWidth ?? node.style?.width,
|
|
32
|
+
height: node.height ?? node.initialHeight ?? node.style?.height,
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
return {
|
|
37
|
+
width: node.width ?? node.style?.width,
|
|
38
|
+
height: node.height ?? node.style?.height,
|
|
39
|
+
};
|
|
40
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { Position } from '@xyflow/system';
|
|
2
|
+
|
|
3
|
+
import { Handle } from '../../components/Handle/index.tsrx';
|
|
4
|
+
import type { BuiltInNode, NodeProps } from '../../types/nodes';
|
|
5
|
+
|
|
6
|
+
export function DefaultNode({
|
|
7
|
+
data,
|
|
8
|
+
isConnectable,
|
|
9
|
+
targetPosition = Position.Top,
|
|
10
|
+
sourcePosition = Position.Bottom,
|
|
11
|
+
}: NodeProps<BuiltInNode>) {
|
|
12
|
+
return <>
|
|
13
|
+
<Handle type="target" position={targetPosition} isConnectable={isConnectable} />
|
|
14
|
+
{data?.label}
|
|
15
|
+
<Handle type="source" position={sourcePosition} isConnectable={isConnectable} />
|
|
16
|
+
</>;
|
|
17
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { Position } from '@xyflow/system';
|
|
2
|
+
|
|
3
|
+
import { Handle } from '../../components/Handle/index.tsrx';
|
|
4
|
+
import type { BuiltInNode, NodeProps } from '../../types/nodes';
|
|
5
|
+
|
|
6
|
+
export function InputNode({ data, isConnectable, sourcePosition = Position.Bottom }: NodeProps<
|
|
7
|
+
BuiltInNode
|
|
8
|
+
>) {
|
|
9
|
+
return <>
|
|
10
|
+
{data?.label}
|
|
11
|
+
<Handle type="source" position={sourcePosition} isConnectable={isConnectable} />
|
|
12
|
+
</>;
|
|
13
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { Position } from '@xyflow/system';
|
|
2
|
+
|
|
3
|
+
import { Handle } from '../../components/Handle/index.tsrx';
|
|
4
|
+
import type { BuiltInNode, NodeProps } from '../../types/nodes';
|
|
5
|
+
|
|
6
|
+
export function OutputNode({ data, isConnectable, targetPosition = Position.Top }: NodeProps<
|
|
7
|
+
BuiltInNode
|
|
8
|
+
>) {
|
|
9
|
+
return <>
|
|
10
|
+
<Handle type="target" position={targetPosition} isConnectable={isConnectable} />
|
|
11
|
+
{data?.label}
|
|
12
|
+
</>;
|
|
13
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import type { RefObject } from '../../react-shim.js';
|
|
2
|
+
import type { StoreApi } from '@octanejs/zustand';
|
|
3
|
+
import { errorMessages } from '@xyflow/system';
|
|
4
|
+
|
|
5
|
+
import type { ReactFlowState } from '../../types';
|
|
6
|
+
|
|
7
|
+
/*
|
|
8
|
+
* this handler is called by
|
|
9
|
+
* 1. the click handler when node is not draggable or selectNodesOnDrag = false
|
|
10
|
+
* or
|
|
11
|
+
* 2. the on drag start handler when node is draggable and selectNodesOnDrag = true
|
|
12
|
+
*/
|
|
13
|
+
export function handleNodeClick({
|
|
14
|
+
id,
|
|
15
|
+
store,
|
|
16
|
+
unselect = false,
|
|
17
|
+
nodeRef,
|
|
18
|
+
}: {
|
|
19
|
+
id: string;
|
|
20
|
+
store: {
|
|
21
|
+
getState: StoreApi<ReactFlowState>['getState'];
|
|
22
|
+
setState: StoreApi<ReactFlowState>['setState'];
|
|
23
|
+
};
|
|
24
|
+
unselect?: boolean;
|
|
25
|
+
nodeRef?: RefObject<HTMLDivElement>;
|
|
26
|
+
}) {
|
|
27
|
+
const { addSelectedNodes, unselectNodesAndEdges, multiSelectionActive, nodeLookup, onError } =
|
|
28
|
+
store.getState();
|
|
29
|
+
const node = nodeLookup.get(id);
|
|
30
|
+
|
|
31
|
+
if (!node) {
|
|
32
|
+
onError?.('012', errorMessages['error012'](id));
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
store.setState({ nodesSelectionActive: false });
|
|
37
|
+
|
|
38
|
+
if (!node.selected) {
|
|
39
|
+
addSelectedNodes([id]);
|
|
40
|
+
} else if (unselect || (node.selected && multiSelectionActive)) {
|
|
41
|
+
unselectNodesAndEdges({ nodes: [node], edges: [] });
|
|
42
|
+
|
|
43
|
+
requestAnimationFrame(() => nodeRef?.current?.blur());
|
|
44
|
+
}
|
|
45
|
+
}
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The nodes selection rectangle gets displayed when a user
|
|
3
|
+
* made a selection with on or several nodes
|
|
4
|
+
*/
|
|
5
|
+
import { useRef, useEffect } from 'octane';
|
|
6
|
+
import type { KeyboardEvent, MouseEvent } from '../../react-shim.js';
|
|
7
|
+
import cc from 'classcat';
|
|
8
|
+
import { shallow } from '@octanejs/zustand/shallow';
|
|
9
|
+
import { getInternalNodesBounds, isNumeric } from '@xyflow/system';
|
|
10
|
+
|
|
11
|
+
import { useStore, useStoreApi } from '../../hooks/useStore';
|
|
12
|
+
import { useDrag } from '../../hooks/useDrag';
|
|
13
|
+
import { useMoveSelectedNodes } from '../../hooks/useMoveSelectedNodes';
|
|
14
|
+
import { arrowKeyDiffs } from '../NodeWrapper/utils.tsrx';
|
|
15
|
+
import type { Node, ReactFlowState } from '../../types';
|
|
16
|
+
|
|
17
|
+
export type NodesSelectionProps<NodeType> = {
|
|
18
|
+
onSelectionContextMenu?: (event: MouseEvent, nodes: NodeType[]) => void;
|
|
19
|
+
noPanClassName?: string;
|
|
20
|
+
disableKeyboardA11y: boolean;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
const selector = (s: ReactFlowState) => {
|
|
24
|
+
const { width, height, x, y } = getInternalNodesBounds(s.nodeLookup, {
|
|
25
|
+
filter: (node) => !!node.selected,
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
return {
|
|
29
|
+
width: isNumeric(width) ? width : null,
|
|
30
|
+
height: isNumeric(height) ? height : null,
|
|
31
|
+
userSelectionActive: s.userSelectionActive,
|
|
32
|
+
transformString: `translate(${s.transform[0]}px,${s.transform[1]}px) scale(${s.transform[2]}) translate(${x}px,${y}px)`,
|
|
33
|
+
};
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
export function NodesSelection<NodeType extends Node>({
|
|
37
|
+
onSelectionContextMenu,
|
|
38
|
+
noPanClassName,
|
|
39
|
+
disableKeyboardA11y,
|
|
40
|
+
}: NodesSelectionProps<NodeType>) {
|
|
41
|
+
const store = useStoreApi<NodeType>();
|
|
42
|
+
const { width, height, transformString, userSelectionActive } = useStore(selector, shallow);
|
|
43
|
+
const moveSelectedNodes = useMoveSelectedNodes();
|
|
44
|
+
|
|
45
|
+
const nodeRef = useRef<HTMLDivElement | null>(null);
|
|
46
|
+
|
|
47
|
+
useEffect(() => {
|
|
48
|
+
if (!disableKeyboardA11y) {
|
|
49
|
+
nodeRef.current?.focus({
|
|
50
|
+
preventScroll: true,
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
}, [disableKeyboardA11y]);
|
|
54
|
+
|
|
55
|
+
const shouldRender = !userSelectionActive && width !== null && height !== null;
|
|
56
|
+
|
|
57
|
+
useDrag({
|
|
58
|
+
nodeRef,
|
|
59
|
+
disabled: !shouldRender,
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
if (!shouldRender) {
|
|
63
|
+
return null;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const onContextMenu =
|
|
67
|
+
onSelectionContextMenu
|
|
68
|
+
? (event: MouseEvent) => {
|
|
69
|
+
const selectedNodes = store.getState().nodes.filter((n) => n.selected);
|
|
70
|
+
onSelectionContextMenu(event, selectedNodes);
|
|
71
|
+
}
|
|
72
|
+
: undefined;
|
|
73
|
+
|
|
74
|
+
const onKeyDown = (event: KeyboardEvent) => {
|
|
75
|
+
if (Object.prototype.hasOwnProperty.call(arrowKeyDiffs, event.key)) {
|
|
76
|
+
event.preventDefault();
|
|
77
|
+
|
|
78
|
+
moveSelectedNodes({
|
|
79
|
+
direction: arrowKeyDiffs[event.key],
|
|
80
|
+
factor: event.shiftKey ? 4 : 1,
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
return <div
|
|
86
|
+
className={cc(['react-flow__nodesselection', 'react-flow__container', noPanClassName])}
|
|
87
|
+
style={{
|
|
88
|
+
transform: transformString,
|
|
89
|
+
}}
|
|
90
|
+
>
|
|
91
|
+
<div
|
|
92
|
+
ref={nodeRef}
|
|
93
|
+
className="react-flow__nodesselection-rect"
|
|
94
|
+
onContextMenu={onContextMenu}
|
|
95
|
+
tabIndex={disableKeyboardA11y ? undefined : -1}
|
|
96
|
+
onKeyDown={disableKeyboardA11y ? undefined : onKeyDown}
|
|
97
|
+
style={{
|
|
98
|
+
width,
|
|
99
|
+
height,
|
|
100
|
+
}}
|
|
101
|
+
/>
|
|
102
|
+
</div>;
|
|
103
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import cc from 'classcat';
|
|
2
|
+
import type { PanelPosition } from '@xyflow/system';
|
|
3
|
+
import type { HTMLAttributes } from '../../react-shim.js';
|
|
4
|
+
import { fixedForwardRef } from '../../utils/general';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* @expand
|
|
8
|
+
*/
|
|
9
|
+
export type PanelProps =
|
|
10
|
+
HTMLAttributes<HTMLDivElement> & {
|
|
11
|
+
/**
|
|
12
|
+
* The position of the panel.
|
|
13
|
+
* @default "top-left"
|
|
14
|
+
*/
|
|
15
|
+
position?: PanelPosition;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
function PanelComponent(
|
|
19
|
+
{ position = 'top-left', children, className, style, ...rest }: PanelProps,
|
|
20
|
+
ref: import('octane/jsx-runtime').Octane.Ref,
|
|
21
|
+
) {
|
|
22
|
+
const positionClasses = `${position}`.split('-');
|
|
23
|
+
|
|
24
|
+
return <div
|
|
25
|
+
className={cc(['react-flow__panel', className, ...positionClasses])}
|
|
26
|
+
style={style}
|
|
27
|
+
ref={ref}
|
|
28
|
+
{...rest}
|
|
29
|
+
>{children}</div>;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export const Panel = fixedForwardRef(PanelComponent);
|
|
33
|
+
|
|
34
|
+
Panel.displayName = 'Panel';
|