@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,274 @@
|
|
|
1
|
+
import { useState, useMemo, useRef, useCallback, memo } from 'octane';
|
|
2
|
+
import type { KeyboardEvent, MouseEvent as ReactMouseEvent } from '../../react-shim.js';
|
|
3
|
+
import type { Octane } from 'octane/jsx-runtime';
|
|
4
|
+
import cc from 'classcat';
|
|
5
|
+
import { shallow } from '@octanejs/zustand/shallow';
|
|
6
|
+
import {
|
|
7
|
+
getMarkerId,
|
|
8
|
+
elementSelectionKeys,
|
|
9
|
+
getEdgePosition,
|
|
10
|
+
errorMessages,
|
|
11
|
+
getElevatedEdgeZIndex,
|
|
12
|
+
} from '@xyflow/system';
|
|
13
|
+
|
|
14
|
+
import { useStoreApi, useStore } from '../../hooks/useStore';
|
|
15
|
+
import { ARIA_EDGE_DESC_KEY } from '../A11yDescriptions/index.tsrx';
|
|
16
|
+
import { builtinEdgeTypes, nullPosition } from './utils';
|
|
17
|
+
import { EdgeUpdateAnchors } from './EdgeUpdateAnchors.tsrx';
|
|
18
|
+
import type { Edge, EdgeWrapperProps } from '../../types';
|
|
19
|
+
|
|
20
|
+
function EdgeWrapper<EdgeType extends Edge = Edge>({
|
|
21
|
+
id,
|
|
22
|
+
edgesFocusable,
|
|
23
|
+
edgesReconnectable,
|
|
24
|
+
elementsSelectable,
|
|
25
|
+
onClick,
|
|
26
|
+
onDoubleClick,
|
|
27
|
+
onContextMenu,
|
|
28
|
+
onMouseEnter,
|
|
29
|
+
onMouseMove,
|
|
30
|
+
onMouseLeave,
|
|
31
|
+
reconnectRadius,
|
|
32
|
+
onReconnect,
|
|
33
|
+
onReconnectStart,
|
|
34
|
+
onReconnectEnd,
|
|
35
|
+
rfId,
|
|
36
|
+
edgeTypes,
|
|
37
|
+
noPanClassName,
|
|
38
|
+
onError,
|
|
39
|
+
disableKeyboardA11y,
|
|
40
|
+
}: EdgeWrapperProps<EdgeType>): Octane.JSX.Element | null {
|
|
41
|
+
let edge = useStore((s) => s.edgeLookup.get(id)!) as EdgeType;
|
|
42
|
+
const defaultEdgeOptions = useStore((s) => s.defaultEdgeOptions);
|
|
43
|
+
edge = defaultEdgeOptions ? { ...defaultEdgeOptions, ...edge } : edge;
|
|
44
|
+
|
|
45
|
+
let edgeType = edge.type || 'default';
|
|
46
|
+
let EdgeComponent = edgeTypes?.[edgeType] || builtinEdgeTypes[edgeType];
|
|
47
|
+
|
|
48
|
+
if (EdgeComponent === undefined) {
|
|
49
|
+
onError?.('011', errorMessages['error011'](edgeType));
|
|
50
|
+
edgeType = 'default';
|
|
51
|
+
EdgeComponent = edgeTypes?.['default'] || builtinEdgeTypes.default;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const isFocusable = !!(edge.focusable || edgesFocusable && typeof edge.focusable === 'undefined');
|
|
55
|
+
const isReconnectable =
|
|
56
|
+
typeof onReconnect !== 'undefined' &&
|
|
57
|
+
(edge.reconnectable || edgesReconnectable && typeof edge.reconnectable === 'undefined');
|
|
58
|
+
const isSelectable = !!(edge.selectable ||
|
|
59
|
+
elementsSelectable && typeof edge.selectable === 'undefined');
|
|
60
|
+
|
|
61
|
+
const edgeRef = useRef<SVGGElement | null>(null);
|
|
62
|
+
const [updateHover, setUpdateHover] = useState<boolean>(false);
|
|
63
|
+
const [reconnecting, setReconnecting] = useState<boolean>(false);
|
|
64
|
+
const store = useStoreApi();
|
|
65
|
+
|
|
66
|
+
const {
|
|
67
|
+
zIndex = edge.zIndex,
|
|
68
|
+
sourceX,
|
|
69
|
+
sourceY,
|
|
70
|
+
targetX,
|
|
71
|
+
targetY,
|
|
72
|
+
sourcePosition,
|
|
73
|
+
targetPosition,
|
|
74
|
+
} = useStore(
|
|
75
|
+
useCallback((store) => {
|
|
76
|
+
const sourceNode = store.nodeLookup.get(edge.source);
|
|
77
|
+
const targetNode = store.nodeLookup.get(edge.target);
|
|
78
|
+
|
|
79
|
+
if (!sourceNode || !targetNode) {
|
|
80
|
+
return nullPosition;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
const edgePosition = getEdgePosition({
|
|
84
|
+
id,
|
|
85
|
+
sourceNode,
|
|
86
|
+
targetNode,
|
|
87
|
+
sourceHandle: edge.sourceHandle || null,
|
|
88
|
+
targetHandle: edge.targetHandle || null,
|
|
89
|
+
connectionMode: store.connectionMode,
|
|
90
|
+
onError,
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
const zIndex = getElevatedEdgeZIndex({
|
|
94
|
+
selected: edge.selected,
|
|
95
|
+
zIndex: edge.zIndex,
|
|
96
|
+
sourceNode,
|
|
97
|
+
targetNode,
|
|
98
|
+
elevateOnSelect: store.elevateEdgesOnSelect,
|
|
99
|
+
zIndexMode: store.zIndexMode,
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
return {
|
|
103
|
+
...edgePosition || nullPosition,
|
|
104
|
+
zIndex,
|
|
105
|
+
};
|
|
106
|
+
}, [edge.source, edge.target, edge.sourceHandle, edge.targetHandle, edge.selected, edge.zIndex]),
|
|
107
|
+
shallow,
|
|
108
|
+
);
|
|
109
|
+
|
|
110
|
+
const markerStartUrl = useMemo(
|
|
111
|
+
() => (edge.markerStart ? `url('#${getMarkerId(edge.markerStart, rfId)}')` : undefined),
|
|
112
|
+
[edge.markerStart, rfId],
|
|
113
|
+
);
|
|
114
|
+
|
|
115
|
+
const markerEndUrl = useMemo(
|
|
116
|
+
() => (edge.markerEnd ? `url('#${getMarkerId(edge.markerEnd, rfId)}')` : undefined),
|
|
117
|
+
[edge.markerEnd, rfId],
|
|
118
|
+
);
|
|
119
|
+
|
|
120
|
+
if (edge.hidden || sourceX === null || sourceY === null || targetX === null || targetY === null) {
|
|
121
|
+
return null;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
const onEdgeClick = (event: ReactMouseEvent<SVGGElement, MouseEvent>): void => {
|
|
125
|
+
const { addSelectedEdges, unselectNodesAndEdges, multiSelectionActive } = store.getState();
|
|
126
|
+
|
|
127
|
+
if (isSelectable) {
|
|
128
|
+
store.setState({ nodesSelectionActive: false });
|
|
129
|
+
|
|
130
|
+
if (edge.selected && multiSelectionActive) {
|
|
131
|
+
unselectNodesAndEdges({ nodes: [], edges: [edge] });
|
|
132
|
+
edgeRef.current?.blur();
|
|
133
|
+
} else {
|
|
134
|
+
addSelectedEdges([id]);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
if (onClick) {
|
|
139
|
+
onClick(event, edge);
|
|
140
|
+
}
|
|
141
|
+
};
|
|
142
|
+
|
|
143
|
+
const onEdgeDoubleClick =
|
|
144
|
+
onDoubleClick
|
|
145
|
+
? (event: ReactMouseEvent) => {
|
|
146
|
+
onDoubleClick(event, { ...edge });
|
|
147
|
+
}
|
|
148
|
+
: undefined;
|
|
149
|
+
const onEdgeContextMenu =
|
|
150
|
+
onContextMenu
|
|
151
|
+
? (event: ReactMouseEvent) => {
|
|
152
|
+
onContextMenu(event, { ...edge });
|
|
153
|
+
}
|
|
154
|
+
: undefined;
|
|
155
|
+
const onEdgeMouseEnter =
|
|
156
|
+
onMouseEnter
|
|
157
|
+
? (event: ReactMouseEvent) => {
|
|
158
|
+
onMouseEnter(event, { ...edge });
|
|
159
|
+
}
|
|
160
|
+
: undefined;
|
|
161
|
+
const onEdgeMouseMove =
|
|
162
|
+
onMouseMove
|
|
163
|
+
? (event: ReactMouseEvent) => {
|
|
164
|
+
onMouseMove(event, { ...edge });
|
|
165
|
+
}
|
|
166
|
+
: undefined;
|
|
167
|
+
const onEdgeMouseLeave =
|
|
168
|
+
onMouseLeave
|
|
169
|
+
? (event: ReactMouseEvent) => {
|
|
170
|
+
onMouseLeave(event, { ...edge });
|
|
171
|
+
}
|
|
172
|
+
: undefined;
|
|
173
|
+
|
|
174
|
+
const onKeyDown = (event: KeyboardEvent) => {
|
|
175
|
+
if (!disableKeyboardA11y && elementSelectionKeys.includes(event.key) && isSelectable) {
|
|
176
|
+
const { unselectNodesAndEdges, addSelectedEdges } = store.getState();
|
|
177
|
+
const unselect = event.key === 'Escape';
|
|
178
|
+
|
|
179
|
+
if (unselect) {
|
|
180
|
+
edgeRef.current?.blur();
|
|
181
|
+
unselectNodesAndEdges({ edges: [edge] });
|
|
182
|
+
} else {
|
|
183
|
+
addSelectedEdges([id]);
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
};
|
|
187
|
+
|
|
188
|
+
return <svg style={{ zIndex }}>
|
|
189
|
+
<g
|
|
190
|
+
className={cc([
|
|
191
|
+
'react-flow__edge',
|
|
192
|
+
`react-flow__edge-${edgeType}`,
|
|
193
|
+
edge.className,
|
|
194
|
+
noPanClassName,
|
|
195
|
+
{
|
|
196
|
+
selected: edge.selected,
|
|
197
|
+
animated: edge.animated,
|
|
198
|
+
inactive: !isSelectable && !onClick,
|
|
199
|
+
updating: updateHover,
|
|
200
|
+
selectable: isSelectable,
|
|
201
|
+
},
|
|
202
|
+
])}
|
|
203
|
+
onClick={onEdgeClick}
|
|
204
|
+
onDoubleClick={onEdgeDoubleClick}
|
|
205
|
+
onContextMenu={onEdgeContextMenu}
|
|
206
|
+
onMouseEnter={onEdgeMouseEnter}
|
|
207
|
+
onMouseMove={onEdgeMouseMove}
|
|
208
|
+
onMouseLeave={onEdgeMouseLeave}
|
|
209
|
+
onKeyDown={isFocusable ? onKeyDown : undefined}
|
|
210
|
+
tabIndex={isFocusable ? 0 : undefined}
|
|
211
|
+
role={edge.ariaRole ?? (isFocusable ? 'group' : 'img')}
|
|
212
|
+
aria-roledescription="edge"
|
|
213
|
+
data-id={id}
|
|
214
|
+
data-testid={`rf__edge-${id}`}
|
|
215
|
+
aria-label={edge.ariaLabel === null
|
|
216
|
+
? undefined
|
|
217
|
+
: edge.ariaLabel || `Edge from ${edge.source} to ${edge.target}`}
|
|
218
|
+
aria-describedby={isFocusable ? `${ARIA_EDGE_DESC_KEY}-${rfId}` : undefined}
|
|
219
|
+
ref={edgeRef}
|
|
220
|
+
{...edge.domAttributes}
|
|
221
|
+
>
|
|
222
|
+
{!reconnecting &&
|
|
223
|
+
<EdgeComponent
|
|
224
|
+
id={id}
|
|
225
|
+
source={edge.source}
|
|
226
|
+
target={edge.target}
|
|
227
|
+
type={edge.type}
|
|
228
|
+
selected={edge.selected}
|
|
229
|
+
animated={edge.animated}
|
|
230
|
+
selectable={isSelectable}
|
|
231
|
+
deletable={edge.deletable ?? true}
|
|
232
|
+
label={edge.label}
|
|
233
|
+
labelStyle={edge.labelStyle}
|
|
234
|
+
labelShowBg={edge.labelShowBg}
|
|
235
|
+
labelBgStyle={edge.labelBgStyle}
|
|
236
|
+
labelBgPadding={edge.labelBgPadding}
|
|
237
|
+
labelBgBorderRadius={edge.labelBgBorderRadius}
|
|
238
|
+
sourceX={sourceX}
|
|
239
|
+
sourceY={sourceY}
|
|
240
|
+
targetX={targetX}
|
|
241
|
+
targetY={targetY}
|
|
242
|
+
sourcePosition={sourcePosition}
|
|
243
|
+
targetPosition={targetPosition}
|
|
244
|
+
data={edge.data}
|
|
245
|
+
style={edge.style}
|
|
246
|
+
sourceHandleId={edge.sourceHandle}
|
|
247
|
+
targetHandleId={edge.targetHandle}
|
|
248
|
+
markerStart={markerStartUrl}
|
|
249
|
+
markerEnd={markerEndUrl}
|
|
250
|
+
pathOptions={'pathOptions' in edge ? edge.pathOptions : undefined}
|
|
251
|
+
interactionWidth={edge.interactionWidth}
|
|
252
|
+
/>}
|
|
253
|
+
{isReconnectable &&
|
|
254
|
+
<EdgeUpdateAnchors
|
|
255
|
+
edge={edge}
|
|
256
|
+
isReconnectable={isReconnectable}
|
|
257
|
+
reconnectRadius={reconnectRadius}
|
|
258
|
+
onReconnect={onReconnect}
|
|
259
|
+
onReconnectStart={onReconnectStart}
|
|
260
|
+
onReconnectEnd={onReconnectEnd}
|
|
261
|
+
sourceX={sourceX}
|
|
262
|
+
sourceY={sourceY}
|
|
263
|
+
targetX={targetX}
|
|
264
|
+
targetY={targetY}
|
|
265
|
+
sourcePosition={sourcePosition}
|
|
266
|
+
targetPosition={targetPosition}
|
|
267
|
+
setUpdateHover={setUpdateHover}
|
|
268
|
+
setReconnecting={setReconnecting}
|
|
269
|
+
/>}
|
|
270
|
+
</g>
|
|
271
|
+
</svg>;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
export default memo(EdgeWrapper) as typeof EdgeWrapper;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { EdgeTypes } from '../../types';
|
|
2
|
+
import {
|
|
3
|
+
BezierEdgeInternal,
|
|
4
|
+
StraightEdgeInternal,
|
|
5
|
+
StepEdgeInternal,
|
|
6
|
+
SmoothStepEdgeInternal,
|
|
7
|
+
SimpleBezierEdgeInternal,
|
|
8
|
+
} from '../Edges';
|
|
9
|
+
|
|
10
|
+
export const builtinEdgeTypes: EdgeTypes = {
|
|
11
|
+
default: BezierEdgeInternal,
|
|
12
|
+
straight: StraightEdgeInternal,
|
|
13
|
+
step: StepEdgeInternal,
|
|
14
|
+
smoothstep: SmoothStepEdgeInternal,
|
|
15
|
+
simplebezier: SimpleBezierEdgeInternal,
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export const nullPosition = {
|
|
19
|
+
sourceX: null,
|
|
20
|
+
sourceY: null,
|
|
21
|
+
targetX: null,
|
|
22
|
+
targetY: null,
|
|
23
|
+
sourcePosition: null,
|
|
24
|
+
targetPosition: null,
|
|
25
|
+
zIndex: undefined,
|
|
26
|
+
};
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { isNumeric } from '@xyflow/system';
|
|
2
|
+
import cc from 'classcat';
|
|
3
|
+
|
|
4
|
+
import { EdgeText } from './EdgeText.tsrx';
|
|
5
|
+
import type { BaseEdgeProps } from '../../types';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* The `<BaseEdge />` component gets used internally for all the edges. It can be
|
|
9
|
+
* used inside a custom edge and handles the invisible helper edge and the edge label
|
|
10
|
+
* for you.
|
|
11
|
+
*
|
|
12
|
+
* @public
|
|
13
|
+
* @example
|
|
14
|
+
* ```jsx
|
|
15
|
+
*import { BaseEdge } from '@xyflow/react';
|
|
16
|
+
*
|
|
17
|
+
*export function CustomEdge({ sourceX, sourceY, targetX, targetY, ...props }) {
|
|
18
|
+
* const [edgePath] = getStraightPath({
|
|
19
|
+
* sourceX,
|
|
20
|
+
* sourceY,
|
|
21
|
+
* targetX,
|
|
22
|
+
* targetY,
|
|
23
|
+
* });
|
|
24
|
+
*
|
|
25
|
+
* return <BaseEdge path={edgePath} {...props} />;
|
|
26
|
+
*}
|
|
27
|
+
*```
|
|
28
|
+
*
|
|
29
|
+
* @remarks If you want to use an edge marker with the [`<BaseEdge />`](/api-reference/components/base-edge) component,
|
|
30
|
+
* you can pass the `markerStart` or `markerEnd` props passed to your custom edge
|
|
31
|
+
* through to the [`<BaseEdge />`](/api-reference/components/base-edge) component.
|
|
32
|
+
* You can see all the props passed to a custom edge by looking at the [`EdgeProps`](/api-reference/types/edge-props) type.
|
|
33
|
+
*/
|
|
34
|
+
export function BaseEdge({
|
|
35
|
+
path,
|
|
36
|
+
labelX,
|
|
37
|
+
labelY,
|
|
38
|
+
label,
|
|
39
|
+
labelStyle,
|
|
40
|
+
labelShowBg,
|
|
41
|
+
labelBgStyle,
|
|
42
|
+
labelBgPadding,
|
|
43
|
+
labelBgBorderRadius,
|
|
44
|
+
interactionWidth = 20,
|
|
45
|
+
...props
|
|
46
|
+
}: BaseEdgeProps) {
|
|
47
|
+
return <>
|
|
48
|
+
<path
|
|
49
|
+
{...props}
|
|
50
|
+
d={path}
|
|
51
|
+
fill="none"
|
|
52
|
+
className={cc(['react-flow__edge-path', props.className])}
|
|
53
|
+
/>
|
|
54
|
+
{interactionWidth
|
|
55
|
+
? <path
|
|
56
|
+
d={path}
|
|
57
|
+
fill="none"
|
|
58
|
+
strokeOpacity={0}
|
|
59
|
+
strokeWidth={interactionWidth}
|
|
60
|
+
className="react-flow__edge-interaction"
|
|
61
|
+
/>
|
|
62
|
+
: null}
|
|
63
|
+
{label && isNumeric(labelX) && isNumeric(labelY)
|
|
64
|
+
? <EdgeText
|
|
65
|
+
x={labelX}
|
|
66
|
+
y={labelY}
|
|
67
|
+
label={label}
|
|
68
|
+
labelStyle={labelStyle}
|
|
69
|
+
labelShowBg={labelShowBg}
|
|
70
|
+
labelBgStyle={labelBgStyle}
|
|
71
|
+
labelBgPadding={labelBgPadding}
|
|
72
|
+
labelBgBorderRadius={labelBgBorderRadius}
|
|
73
|
+
/>
|
|
74
|
+
: null}
|
|
75
|
+
</>;
|
|
76
|
+
}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { memo } from 'octane';
|
|
2
|
+
import { Position, getBezierPath } from '@xyflow/system';
|
|
3
|
+
|
|
4
|
+
import { BaseEdge } from './BaseEdge.tsrx';
|
|
5
|
+
import type { BezierEdgeProps } from '../../types';
|
|
6
|
+
|
|
7
|
+
function createBezierEdge(params: { isInternal: boolean }) {
|
|
8
|
+
// eslint-disable-next-line react/display-name
|
|
9
|
+
return memo(({
|
|
10
|
+
id,
|
|
11
|
+
sourceX,
|
|
12
|
+
sourceY,
|
|
13
|
+
targetX,
|
|
14
|
+
targetY,
|
|
15
|
+
sourcePosition = Position.Bottom,
|
|
16
|
+
targetPosition = Position.Top,
|
|
17
|
+
label,
|
|
18
|
+
labelStyle,
|
|
19
|
+
labelShowBg,
|
|
20
|
+
labelBgStyle,
|
|
21
|
+
labelBgPadding,
|
|
22
|
+
labelBgBorderRadius,
|
|
23
|
+
style,
|
|
24
|
+
markerEnd,
|
|
25
|
+
markerStart,
|
|
26
|
+
pathOptions,
|
|
27
|
+
interactionWidth,
|
|
28
|
+
}: BezierEdgeProps) => {
|
|
29
|
+
const [path, labelX, labelY] = getBezierPath({
|
|
30
|
+
sourceX,
|
|
31
|
+
sourceY,
|
|
32
|
+
sourcePosition,
|
|
33
|
+
targetX,
|
|
34
|
+
targetY,
|
|
35
|
+
targetPosition,
|
|
36
|
+
curvature: pathOptions?.curvature,
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
const _id = params.isInternal ? undefined : id;
|
|
40
|
+
|
|
41
|
+
return <BaseEdge
|
|
42
|
+
id={_id}
|
|
43
|
+
path={path}
|
|
44
|
+
labelX={labelX}
|
|
45
|
+
labelY={labelY}
|
|
46
|
+
label={label}
|
|
47
|
+
labelStyle={labelStyle}
|
|
48
|
+
labelShowBg={labelShowBg}
|
|
49
|
+
labelBgStyle={labelBgStyle}
|
|
50
|
+
labelBgPadding={labelBgPadding}
|
|
51
|
+
labelBgBorderRadius={labelBgBorderRadius}
|
|
52
|
+
style={style}
|
|
53
|
+
markerEnd={markerEnd}
|
|
54
|
+
markerStart={markerStart}
|
|
55
|
+
interactionWidth={interactionWidth}
|
|
56
|
+
/>;
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Component that can be used inside a custom edge to render a bezier curve.
|
|
62
|
+
*
|
|
63
|
+
* @public
|
|
64
|
+
* @example
|
|
65
|
+
*
|
|
66
|
+
* ```tsx
|
|
67
|
+
* import { BezierEdge } from '@xyflow/react';
|
|
68
|
+
*
|
|
69
|
+
* function CustomEdge({ sourceX, sourceY, targetX, targetY, sourcePosition, targetPosition }) {
|
|
70
|
+
* return (
|
|
71
|
+
* <BezierEdge
|
|
72
|
+
* sourceX={sourceX}
|
|
73
|
+
* sourceY={sourceY}
|
|
74
|
+
* targetX={targetX}
|
|
75
|
+
* targetY={targetY}
|
|
76
|
+
* sourcePosition={sourcePosition}
|
|
77
|
+
* targetPosition={targetPosition}
|
|
78
|
+
* />
|
|
79
|
+
* );
|
|
80
|
+
* }
|
|
81
|
+
* ```
|
|
82
|
+
*/
|
|
83
|
+
const BezierEdge = createBezierEdge({ isInternal: false });
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* @internal
|
|
87
|
+
*/
|
|
88
|
+
const BezierEdgeInternal = createBezierEdge({ isInternal: true });
|
|
89
|
+
|
|
90
|
+
(BezierEdge as typeof BezierEdge & { displayName?: string }).displayName = 'BezierEdge';
|
|
91
|
+
(BezierEdgeInternal as typeof BezierEdgeInternal & { displayName?: string }).displayName =
|
|
92
|
+
'BezierEdgeInternal';
|
|
93
|
+
|
|
94
|
+
export { BezierEdge, BezierEdgeInternal };
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import type { MouseEvent as ReactMouseEvent, SVGAttributes } from '../../react-shim.js';
|
|
2
|
+
import cc from 'classcat';
|
|
3
|
+
import { Position } from '@xyflow/system';
|
|
4
|
+
|
|
5
|
+
const shiftX = (x: number, shift: number, position: Position): number => {
|
|
6
|
+
if (position === Position.Left) return x - shift;
|
|
7
|
+
if (position === Position.Right) return x + shift;
|
|
8
|
+
return x;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
const shiftY = (y: number, shift: number, position: Position): number => {
|
|
12
|
+
if (position === Position.Top) return y - shift;
|
|
13
|
+
if (position === Position.Bottom) return y + shift;
|
|
14
|
+
return y;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
export interface EdgeAnchorProps extends SVGAttributes<SVGGElement> {
|
|
18
|
+
position: Position;
|
|
19
|
+
centerX: number;
|
|
20
|
+
centerY: number;
|
|
21
|
+
radius?: number;
|
|
22
|
+
onMouseDown: (event: ReactMouseEvent<SVGGElement, MouseEvent>) => void;
|
|
23
|
+
onMouseEnter: (event: ReactMouseEvent<SVGGElement, MouseEvent>) => void;
|
|
24
|
+
onMouseOut: (event: ReactMouseEvent<SVGGElement, MouseEvent>) => void;
|
|
25
|
+
type: string;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const EdgeUpdaterClassName = 'react-flow__edgeupdater';
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* @internal
|
|
32
|
+
*/
|
|
33
|
+
export function EdgeAnchor({
|
|
34
|
+
position,
|
|
35
|
+
centerX,
|
|
36
|
+
centerY,
|
|
37
|
+
radius = 10,
|
|
38
|
+
onMouseDown,
|
|
39
|
+
onMouseEnter,
|
|
40
|
+
onMouseOut,
|
|
41
|
+
type,
|
|
42
|
+
}: EdgeAnchorProps) {
|
|
43
|
+
return <circle
|
|
44
|
+
onMouseDown={onMouseDown}
|
|
45
|
+
onMouseEnter={onMouseEnter}
|
|
46
|
+
onMouseOut={onMouseOut}
|
|
47
|
+
className={cc([EdgeUpdaterClassName, `${EdgeUpdaterClassName}-${type}`])}
|
|
48
|
+
cx={shiftX(centerX, radius, position)}
|
|
49
|
+
cy={shiftY(centerY, radius, position)}
|
|
50
|
+
r={radius}
|
|
51
|
+
stroke="transparent"
|
|
52
|
+
fill="transparent"
|
|
53
|
+
/>;
|
|
54
|
+
}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { memo, useState, useEffect, useRef } from 'octane';
|
|
2
|
+
import cc from 'classcat';
|
|
3
|
+
import type { Rect } from '@xyflow/system';
|
|
4
|
+
|
|
5
|
+
import type { EdgeTextProps } from '../../types';
|
|
6
|
+
|
|
7
|
+
function EdgeTextComponent({
|
|
8
|
+
x,
|
|
9
|
+
y,
|
|
10
|
+
label,
|
|
11
|
+
labelStyle,
|
|
12
|
+
labelShowBg = true,
|
|
13
|
+
labelBgStyle,
|
|
14
|
+
labelBgPadding = [2, 4],
|
|
15
|
+
labelBgBorderRadius = 2,
|
|
16
|
+
children,
|
|
17
|
+
className,
|
|
18
|
+
...rest
|
|
19
|
+
}: EdgeTextProps) {
|
|
20
|
+
const [edgeTextBbox, setEdgeTextBbox] = useState<Rect>({ x: 1, y: 0, width: 0, height: 0 });
|
|
21
|
+
const edgeTextClasses = cc(['react-flow__edge-textwrapper', className]);
|
|
22
|
+
const edgeTextRef = useRef<SVGTextElement | null>(null);
|
|
23
|
+
|
|
24
|
+
useEffect(() => {
|
|
25
|
+
if (edgeTextRef.current) {
|
|
26
|
+
const textBbox = edgeTextRef.current.getBBox();
|
|
27
|
+
|
|
28
|
+
setEdgeTextBbox({
|
|
29
|
+
x: textBbox.x,
|
|
30
|
+
y: textBbox.y,
|
|
31
|
+
width: textBbox.width,
|
|
32
|
+
height: textBbox.height,
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
}, [label]);
|
|
36
|
+
|
|
37
|
+
if (!label) {
|
|
38
|
+
return null;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
return <g
|
|
42
|
+
transform={`translate(${x - edgeTextBbox.width / 2} ${y - edgeTextBbox.height / 2})`}
|
|
43
|
+
className={edgeTextClasses}
|
|
44
|
+
visibility={edgeTextBbox.width ? 'visible' : 'hidden'}
|
|
45
|
+
{...rest}
|
|
46
|
+
>
|
|
47
|
+
{labelShowBg &&
|
|
48
|
+
<rect
|
|
49
|
+
width={edgeTextBbox.width + 2 * labelBgPadding[0]}
|
|
50
|
+
x={-labelBgPadding[0]}
|
|
51
|
+
y={-labelBgPadding[1]}
|
|
52
|
+
height={edgeTextBbox.height + 2 * labelBgPadding[1]}
|
|
53
|
+
className="react-flow__edge-textbg"
|
|
54
|
+
style={labelBgStyle}
|
|
55
|
+
rx={labelBgBorderRadius}
|
|
56
|
+
ry={labelBgBorderRadius}
|
|
57
|
+
/>}
|
|
58
|
+
<text
|
|
59
|
+
className="react-flow__edge-text"
|
|
60
|
+
y={edgeTextBbox.height / 2}
|
|
61
|
+
dy="0.3em"
|
|
62
|
+
ref={edgeTextRef}
|
|
63
|
+
style={labelStyle}
|
|
64
|
+
>{label}</text>
|
|
65
|
+
{children}
|
|
66
|
+
</g>;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
EdgeTextComponent.displayName = 'EdgeText';
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* You can use the `<EdgeText />` component as a helper component to display text
|
|
73
|
+
* within your custom edges.
|
|
74
|
+
*
|
|
75
|
+
* @public
|
|
76
|
+
*
|
|
77
|
+
* @example
|
|
78
|
+
* ```jsx
|
|
79
|
+
* import { EdgeText } from '@xyflow/react';
|
|
80
|
+
*
|
|
81
|
+
* export function CustomEdgeLabel({ label }) {
|
|
82
|
+
* return (
|
|
83
|
+
* <EdgeText
|
|
84
|
+
* x={100}
|
|
85
|
+
* y={100}
|
|
86
|
+
* label={label}
|
|
87
|
+
* labelStyle={{ fill: 'white' }}
|
|
88
|
+
* labelShowBg
|
|
89
|
+
* labelBgStyle={{ fill: 'red' }}
|
|
90
|
+
* labelBgPadding={[2, 4]}
|
|
91
|
+
* labelBgBorderRadius={2}
|
|
92
|
+
* />
|
|
93
|
+
* );
|
|
94
|
+
* }
|
|
95
|
+
*```
|
|
96
|
+
*/
|
|
97
|
+
export const EdgeText = memo(EdgeTextComponent);
|