@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.
Files changed (135) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +26 -0
  3. package/package.json +69 -0
  4. package/src/additional-components/Background/Background.tsrx +144 -0
  5. package/src/additional-components/Background/Patterns.tsrx +32 -0
  6. package/src/additional-components/Background/index.tsrx +2 -0
  7. package/src/additional-components/Background/types.ts +60 -0
  8. package/src/additional-components/Controls/ControlButton.tsrx +34 -0
  9. package/src/additional-components/Controls/Controls.tsrx +148 -0
  10. package/src/additional-components/Controls/Icons/FitView.tsrx +7 -0
  11. package/src/additional-components/Controls/Icons/Lock.tsrx +7 -0
  12. package/src/additional-components/Controls/Icons/Minus.tsrx +5 -0
  13. package/src/additional-components/Controls/Icons/Plus.tsrx +5 -0
  14. package/src/additional-components/Controls/Icons/Unlock.tsrx +7 -0
  15. package/src/additional-components/Controls/index.tsrx +3 -0
  16. package/src/additional-components/Controls/types.ts +68 -0
  17. package/src/additional-components/EdgeToolbar/EdgeToolbar.tsrx +79 -0
  18. package/src/additional-components/EdgeToolbar/index.tsrx +2 -0
  19. package/src/additional-components/EdgeToolbar/types.ts +14 -0
  20. package/src/additional-components/MiniMap/MiniMap.tsrx +249 -0
  21. package/src/additional-components/MiniMap/MiniMapNode.tsrx +43 -0
  22. package/src/additional-components/MiniMap/MiniMapNodes.tsrx +136 -0
  23. package/src/additional-components/MiniMap/index.ts +3 -0
  24. package/src/additional-components/MiniMap/types.ts +139 -0
  25. package/src/additional-components/NodeResizer/NodeResizeControl.tsrx +250 -0
  26. package/src/additional-components/NodeResizer/NodeResizer.tsrx +102 -0
  27. package/src/additional-components/NodeResizer/index.ts +4 -0
  28. package/src/additional-components/NodeResizer/types.ts +122 -0
  29. package/src/additional-components/NodeToolbar/NodeToolbar.tsrx +141 -0
  30. package/src/additional-components/NodeToolbar/NodeToolbarPortal.tsrx +17 -0
  31. package/src/additional-components/NodeToolbar/index.tsrx +2 -0
  32. package/src/additional-components/NodeToolbar/types.ts +32 -0
  33. package/src/additional-components/index.ts +6 -0
  34. package/src/components/A11yDescriptions/index.tsrx +58 -0
  35. package/src/components/Attribution/index.tsrx +31 -0
  36. package/src/components/BatchProvider/index.tsrx +122 -0
  37. package/src/components/BatchProvider/types.ts +7 -0
  38. package/src/components/BatchProvider/useQueue.ts +62 -0
  39. package/src/components/ConnectionLine/index.tsrx +140 -0
  40. package/src/components/EdgeLabelRenderer/index.tsrx +62 -0
  41. package/src/components/EdgeWrapper/EdgeUpdateAnchors.tsrx +151 -0
  42. package/src/components/EdgeWrapper/index.tsrx +274 -0
  43. package/src/components/EdgeWrapper/utils.ts +26 -0
  44. package/src/components/Edges/BaseEdge.tsrx +76 -0
  45. package/src/components/Edges/BezierEdge.tsrx +94 -0
  46. package/src/components/Edges/EdgeAnchor.tsrx +54 -0
  47. package/src/components/Edges/EdgeText.tsrx +97 -0
  48. package/src/components/Edges/SimpleBezierEdge.tsrx +150 -0
  49. package/src/components/Edges/SmoothStepEdge.tsrx +96 -0
  50. package/src/components/Edges/StepEdge.tsrx +55 -0
  51. package/src/components/Edges/StraightEdge.tsrx +81 -0
  52. package/src/components/Edges/index.ts +11 -0
  53. package/src/components/Handle/index.tsrx +314 -0
  54. package/src/components/NodeWrapper/index.tsrx +259 -0
  55. package/src/components/NodeWrapper/useNodeObserver.ts +79 -0
  56. package/src/components/NodeWrapper/utils.tsrx +40 -0
  57. package/src/components/Nodes/DefaultNode.tsrx +17 -0
  58. package/src/components/Nodes/GroupNode.tsrx +3 -0
  59. package/src/components/Nodes/InputNode.tsrx +13 -0
  60. package/src/components/Nodes/OutputNode.tsrx +13 -0
  61. package/src/components/Nodes/utils.ts +45 -0
  62. package/src/components/NodesSelection/index.tsrx +103 -0
  63. package/src/components/Panel/index.tsrx +34 -0
  64. package/src/components/ReactFlowProvider/index.tsrx +128 -0
  65. package/src/components/SelectionListener/index.tsrx +73 -0
  66. package/src/components/StoreUpdater/index.tsrx +173 -0
  67. package/src/components/UserSelection/index.tsrx +27 -0
  68. package/src/components/ViewportPortal/index.tsrx +42 -0
  69. package/src/container/EdgeRenderer/MarkerDefinitions.tsrx +87 -0
  70. package/src/container/EdgeRenderer/MarkerSymbols.tsrx +60 -0
  71. package/src/container/EdgeRenderer/index.tsrx +98 -0
  72. package/src/container/FlowRenderer/index.tsrx +146 -0
  73. package/src/container/GraphView/index.tsrx +212 -0
  74. package/src/container/GraphView/useNodeOrEdgeTypesWarning.ts +39 -0
  75. package/src/container/GraphView/useStylesLoadedWarning.ts +26 -0
  76. package/src/container/NodeRenderer/index.tsrx +103 -0
  77. package/src/container/NodeRenderer/useResizeObserver.ts +38 -0
  78. package/src/container/Pane/index.tsrx +397 -0
  79. package/src/container/ReactFlow/Wrapper.tsrx +66 -0
  80. package/src/container/ReactFlow/index.tsrx +352 -0
  81. package/src/container/ReactFlow/init-values.ts +4 -0
  82. package/src/container/Viewport/index.tsrx +52 -0
  83. package/src/container/ZoomPane/index.tsrx +159 -0
  84. package/src/contexts/HandleConfigContext.tsrx +38 -0
  85. package/src/contexts/NodeIdContext.ts +39 -0
  86. package/src/contexts/StoreContext.ts +8 -0
  87. package/src/custom.d.ts +13 -0
  88. package/src/hooks/slot.ts +22 -0
  89. package/src/hooks/useColorModeClass.ts +50 -0
  90. package/src/hooks/useConnection.ts +69 -0
  91. package/src/hooks/useDrag.ts +95 -0
  92. package/src/hooks/useEdges.ts +32 -0
  93. package/src/hooks/useGlobalKeyHandler.ts +55 -0
  94. package/src/hooks/useHandleConnections.ts +77 -0
  95. package/src/hooks/useInternalNode.ts +47 -0
  96. package/src/hooks/useIsomorphicLayoutEffect.ts +5 -0
  97. package/src/hooks/useKeyPress.ts +217 -0
  98. package/src/hooks/useMoveSelectedNodes.ts +82 -0
  99. package/src/hooks/useNodeConnections.ts +86 -0
  100. package/src/hooks/useNodes.ts +33 -0
  101. package/src/hooks/useNodesData.ts +66 -0
  102. package/src/hooks/useNodesEdgesState.ts +138 -0
  103. package/src/hooks/useNodesInitialized.ts +74 -0
  104. package/src/hooks/useOnEdgesChangeMiddleware.ts +46 -0
  105. package/src/hooks/useOnInitHandler.ts +27 -0
  106. package/src/hooks/useOnNodesChangeMiddleware.ts +46 -0
  107. package/src/hooks/useOnSelectionChange.ts +78 -0
  108. package/src/hooks/useOnViewportChange.ts +68 -0
  109. package/src/hooks/useReactFlow.ts +347 -0
  110. package/src/hooks/useResizeHandler.ts +54 -0
  111. package/src/hooks/useStore.ts +58 -0
  112. package/src/hooks/useUpdateNodeInternals.ts +73 -0
  113. package/src/hooks/useViewport.ts +48 -0
  114. package/src/hooks/useViewportHelper.ts +103 -0
  115. package/src/hooks/useViewportSync.ts +33 -0
  116. package/src/hooks/useVisibleEdgeIds.ts +52 -0
  117. package/src/hooks/useVisibleNodeIds.ts +36 -0
  118. package/src/index.ts +151 -0
  119. package/src/react-shim.ts +93 -0
  120. package/src/store/index.ts +516 -0
  121. package/src/store/initialState.ts +162 -0
  122. package/src/styles/base.css +516 -0
  123. package/src/styles/style.css +648 -0
  124. package/src/styles/utils.ts +9 -0
  125. package/src/types/component-props.ts +716 -0
  126. package/src/types/edges.ts +312 -0
  127. package/src/types/general.ts +239 -0
  128. package/src/types/index.ts +6 -0
  129. package/src/types/instance.ts +265 -0
  130. package/src/types/nodes.ts +144 -0
  131. package/src/types/store.ts +198 -0
  132. package/src/utils/changes.ts +329 -0
  133. package/src/utils/edges.ts +34 -0
  134. package/src/utils/general.ts +72 -0
  135. package/src/utils/index.ts +3 -0
@@ -0,0 +1,58 @@
1
+ import type { CSSProperties } from '../../react-shim.js';
2
+
3
+ import { useStore } from '../../hooks/useStore';
4
+ import type { ReactFlowState } from '../../types';
5
+
6
+ const style: CSSProperties = { display: 'none' };
7
+ const ariaLiveStyle: CSSProperties = {
8
+ position: 'absolute',
9
+ width: 1,
10
+ height: 1,
11
+ margin: -1,
12
+ border: 0,
13
+ padding: 0,
14
+ overflow: 'hidden',
15
+ clip: 'rect(0px, 0px, 0px, 0px)',
16
+ clipPath: 'inset(100%)',
17
+ };
18
+
19
+ export const ARIA_NODE_DESC_KEY = 'react-flow__node-desc';
20
+ export const ARIA_EDGE_DESC_KEY = 'react-flow__edge-desc';
21
+ export const ARIA_LIVE_MESSAGE = 'react-flow__aria-live';
22
+
23
+ const ariaLiveSelector = (s: ReactFlowState) => s.ariaLiveMessage;
24
+ const ariaLabelConfigSelector = (s: ReactFlowState) => s.ariaLabelConfig;
25
+
26
+ function AriaLiveMessage({ rfId }: { rfId: string }) {
27
+ const ariaLiveMessage = useStore(ariaLiveSelector);
28
+
29
+ return <div
30
+ id={`${ARIA_LIVE_MESSAGE}-${rfId}`}
31
+ aria-live="assertive"
32
+ aria-atomic="true"
33
+ style={ariaLiveStyle}
34
+ >{ariaLiveMessage}</div>;
35
+ }
36
+
37
+ export function A11yDescriptions({
38
+ rfId,
39
+ disableKeyboardA11y,
40
+ }: {
41
+ rfId: string;
42
+ disableKeyboardA11y: boolean;
43
+ }) {
44
+ const ariaLabelConfig = useStore(ariaLabelConfigSelector);
45
+
46
+ return <>
47
+ <div id={`${ARIA_NODE_DESC_KEY}-${rfId}`} style={style}>
48
+ {disableKeyboardA11y
49
+ ? ariaLabelConfig['node.a11yDescription.default']
50
+ : ariaLabelConfig['node.a11yDescription.keyboardDisabled']}
51
+ </div>
52
+ <div
53
+ id={`${ARIA_EDGE_DESC_KEY}-${rfId}`}
54
+ style={style}
55
+ >{ariaLabelConfig['edge.a11yDescription.default']}</div>
56
+ {!disableKeyboardA11y && <AriaLiveMessage rfId={rfId} />}
57
+ </>;
58
+ }
@@ -0,0 +1,31 @@
1
+ import type { PanelPosition, ProOptions } from '@xyflow/system';
2
+
3
+ import { Panel } from '../Panel/index.tsrx';
4
+
5
+ // Kept module-local so browser consumers do not need `@types/node`.
6
+ declare const process: { env: { NODE_ENV?: string } };
7
+
8
+ type AttributionProps = {
9
+ proOptions?: ProOptions;
10
+ position?: PanelPosition;
11
+ };
12
+
13
+ const link = `https://reactflow.dev${
14
+ process.env.NODE_ENV === 'production' ? '?utm_source=attribution' : '/attribution'
15
+ }`;
16
+
17
+ export function Attribution({ proOptions, position = 'bottom-right' }: AttributionProps) {
18
+ if (proOptions?.hideAttribution) {
19
+ return null;
20
+ }
21
+
22
+ return <Panel
23
+ position={position}
24
+ className="react-flow__attribution"
25
+ data-message={`Please only hide this attribution when you are subscribed to React Flow Pro: ${link}`}
26
+ >
27
+ <a href={link} target="_blank" rel="noopener noreferrer" aria-label="React Flow attribution">
28
+ React Flow
29
+ </a>
30
+ </Panel>;
31
+ }
@@ -0,0 +1,122 @@
1
+ import { createContext, OctaneNode, useCallback, useContext, useMemo } from 'octane';
2
+ import { EdgeChange, NodeChange } from '@xyflow/system';
3
+
4
+ import { useStoreApi } from '../../hooks/useStore';
5
+ import { getElementsDiffChanges } from '../../utils';
6
+ import { Queue, QueueItem } from './types';
7
+ import type { Edge, Node } from '../../types';
8
+ import { useQueue } from './useQueue';
9
+
10
+ const BatchContext = createContext<
11
+ | {
12
+ nodeQueue: Queue<any>;
13
+ edgeQueue: Queue<any>;
14
+ }
15
+ | null
16
+ >(
17
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
18
+
19
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
20
+
21
+ null,
22
+ );
23
+
24
+ /**
25
+ * This is a context provider that holds and processes the node and edge update queues
26
+ * that are needed to handle setNodes, addNodes, setEdges and addEdges.
27
+ *
28
+ * @internal
29
+ */
30
+ export function BatchProvider<NodeType extends Node = Node, EdgeType extends Edge = Edge>({
31
+ children,
32
+ }: {
33
+ children: OctaneNode;
34
+ }) {
35
+ const store = useStoreApi<NodeType, EdgeType>();
36
+
37
+ const nodeQueueHandler = useCallback((queueItems: QueueItem<NodeType>[]) => {
38
+ const {
39
+ nodes = [],
40
+ setNodes,
41
+ hasDefaultNodes,
42
+ onNodesChange,
43
+ nodeLookup,
44
+ fitViewQueued,
45
+ onNodesChangeMiddlewareMap,
46
+ } = store.getState();
47
+
48
+ /*
49
+ * This is essentially an `Array.reduce` in imperative clothing. Processing
50
+ * this queue is a relatively hot path so we'd like to avoid the overhead of
51
+ * array methods where we can.
52
+ */
53
+ let next = nodes;
54
+ for (const payload of queueItems) {
55
+ next = typeof payload === 'function' ? payload(next) : payload;
56
+ }
57
+
58
+ let changes = getElementsDiffChanges({
59
+ items: next,
60
+ lookup: nodeLookup,
61
+ }) as NodeChange<NodeType>[];
62
+
63
+ for (const middleware of onNodesChangeMiddlewareMap.values()) {
64
+ changes = middleware(changes);
65
+ }
66
+
67
+ if (hasDefaultNodes) {
68
+ setNodes(next);
69
+ }
70
+
71
+ // We only want to fire onNodesChange if there are changes to the nodes
72
+ if (changes.length > 0) {
73
+ onNodesChange?.(changes);
74
+ } else if (fitViewQueued) {
75
+ // If there are no changes to the nodes, we still need to call setNodes
76
+ // to trigger a re-render and fitView.
77
+ window.requestAnimationFrame(() => {
78
+ const { fitViewQueued, nodes, setNodes } = store.getState();
79
+ if (fitViewQueued) {
80
+ setNodes(nodes);
81
+ }
82
+ });
83
+ }
84
+ }, []);
85
+
86
+ const nodeQueue = useQueue<NodeType>(nodeQueueHandler);
87
+
88
+ const edgeQueueHandler = useCallback((queueItems: QueueItem<EdgeType>[]) => {
89
+ const { edges = [], setEdges, hasDefaultEdges, onEdgesChange, edgeLookup } = store.getState();
90
+
91
+ let next = edges;
92
+ for (const payload of queueItems) {
93
+ next = typeof payload === 'function' ? payload(next) : payload;
94
+ }
95
+
96
+ if (hasDefaultEdges) {
97
+ setEdges(next);
98
+ } else if (onEdgesChange) {
99
+ onEdgesChange(
100
+ getElementsDiffChanges({
101
+ items: next,
102
+ lookup: edgeLookup,
103
+ }) as EdgeChange<EdgeType>[],
104
+ );
105
+ }
106
+ }, []);
107
+ const edgeQueue = useQueue<EdgeType>(edgeQueueHandler);
108
+
109
+ const value = useMemo(() => ({ nodeQueue, edgeQueue }), []);
110
+
111
+ return <BatchContext.Provider value={value}>{children}</BatchContext.Provider>;
112
+ }
113
+
114
+ export function useBatchContext(..._rest: [slot?: symbol]) {
115
+ const batchContext = useContext(BatchContext);
116
+
117
+ if (!batchContext) {
118
+ throw new Error('useBatchContext must be used within a BatchProvider');
119
+ }
120
+
121
+ return batchContext;
122
+ }
@@ -0,0 +1,7 @@
1
+ export type QueueItem<T> = T[] | ((items: T[]) => T[]);
2
+
3
+ export type Queue<T> = {
4
+ get: () => QueueItem<T>[];
5
+ reset: () => void;
6
+ push: (item: QueueItem<T>) => void;
7
+ };
@@ -0,0 +1,62 @@
1
+ import { useState } from 'octane';
2
+
3
+ import { useIsomorphicLayoutEffect } from '../../hooks/useIsomorphicLayoutEffect';
4
+ import { Queue, QueueItem } from './types';
5
+
6
+ /**
7
+ * This hook returns a queue that can be used to batch updates.
8
+ *
9
+ * @param runQueue - a function that gets called when the queue is flushed
10
+ * @internal
11
+ *
12
+ * @returns a Queue object
13
+ */
14
+ export function useQueue<T>(runQueue: (items: QueueItem<T>[]) => void) {
15
+ /*
16
+ * Because we're using a ref above, we need some way to let React know when to
17
+ * actually process the queue. We increment this number any time we mutate the
18
+ * queue, creating a new state to trigger the layout effect below.
19
+ * Using a boolean dirty flag here instead would lead to issues related to
20
+ * automatic batching. (https://github.com/xyflow/xyflow/issues/4779)
21
+ */
22
+ const [serial, setSerial] = useState(BigInt(0));
23
+
24
+ /*
25
+ * A reference of all the batched updates to process before the next render. We
26
+ * want a reference here so multiple synchronous calls to `setNodes` etc can be
27
+ * batched together.
28
+ */
29
+ const [queue] = useState(() => createQueue<T>(() => setSerial((n) => n + BigInt(1))));
30
+
31
+ /*
32
+ * Layout effects are guaranteed to run before the next render which means we
33
+ * shouldn't run into any issues with stale state or weird issues that come from
34
+ * rendering things one frame later than expected (we used to use `setTimeout`).
35
+ */
36
+ useIsomorphicLayoutEffect(() => {
37
+ const queueItems = queue.get();
38
+
39
+ if (queueItems.length) {
40
+ runQueue(queueItems);
41
+
42
+ queue.reset();
43
+ }
44
+ }, [serial]);
45
+
46
+ return queue;
47
+ }
48
+
49
+ function createQueue<T>(cb: () => void): Queue<T> {
50
+ let queue: QueueItem<T>[] = [];
51
+
52
+ return {
53
+ get: () => queue,
54
+ reset: () => {
55
+ queue = [];
56
+ },
57
+ push: (item) => {
58
+ queue.push(item);
59
+ cb();
60
+ },
61
+ };
62
+ }
@@ -0,0 +1,140 @@
1
+ import type { CSSProperties } from '../../react-shim.js';
2
+ import { shallow } from '@octanejs/zustand/shallow';
3
+ import cc from 'classcat';
4
+ import {
5
+ ConnectionLineType,
6
+ getBezierPath,
7
+ getSmoothStepPath,
8
+ getConnectionStatus,
9
+ getStraightPath,
10
+ } from '@xyflow/system';
11
+
12
+ import { useStore } from '../../hooks/useStore';
13
+ import { getSimpleBezierPath } from '../Edges/SimpleBezierEdge.tsrx';
14
+ import type { ConnectionLineComponent, Node, ReactFlowState } from '../../types';
15
+ import { useConnection } from '../../hooks/useConnection';
16
+
17
+ type ConnectionLineWrapperProps<NodeType extends Node = Node> = {
18
+ type: ConnectionLineType;
19
+ component?: ConnectionLineComponent<NodeType>;
20
+ containerStyle?: CSSProperties;
21
+ style?: CSSProperties;
22
+ };
23
+
24
+ const selector = (s: ReactFlowState) => ({
25
+ nodesConnectable: s.nodesConnectable,
26
+ isValid: s.connection.isValid,
27
+ inProgress: s.connection.inProgress,
28
+ width: s.width,
29
+ height: s.height,
30
+ });
31
+
32
+ export function ConnectionLineWrapper<NodeType extends Node = Node>({
33
+ containerStyle,
34
+ style,
35
+ type,
36
+ component,
37
+ }: ConnectionLineWrapperProps<NodeType>) {
38
+ const { nodesConnectable, width, height, isValid, inProgress } = useStore(selector, shallow);
39
+ const renderConnection = !!(width && nodesConnectable && inProgress);
40
+
41
+ if (!renderConnection) {
42
+ return null;
43
+ }
44
+
45
+ return <svg
46
+ style={containerStyle}
47
+ width={width}
48
+ height={height}
49
+ className="react-flow__connectionline react-flow__container"
50
+ >
51
+ <g className={cc(['react-flow__connection', getConnectionStatus(isValid)])}>
52
+ <ConnectionLine style={style} type={type} CustomComponent={component} isValid={isValid} />
53
+ </g>
54
+ </svg>;
55
+ }
56
+
57
+ type ConnectionLineProps<NodeType extends Node = Node> = {
58
+ type: ConnectionLineType;
59
+ style?: CSSProperties;
60
+ CustomComponent?: ConnectionLineComponent<NodeType>;
61
+ isValid: boolean | null;
62
+ };
63
+
64
+ const ConnectionLine = <NodeType extends Node = Node,>({
65
+ style,
66
+ type = ConnectionLineType.Bezier,
67
+ CustomComponent,
68
+ isValid,
69
+ }: ConnectionLineProps<NodeType>) => {
70
+ const {
71
+ inProgress,
72
+ from,
73
+ fromNode,
74
+ fromHandle,
75
+ fromPosition,
76
+ to,
77
+ toNode,
78
+ toHandle,
79
+ toPosition,
80
+ pointer,
81
+ } = useConnection<NodeType>();
82
+
83
+ if (!inProgress) {
84
+ return;
85
+ }
86
+
87
+ if (CustomComponent) {
88
+ return <CustomComponent
89
+ connectionLineType={type}
90
+ connectionLineStyle={style}
91
+ fromNode={fromNode}
92
+ fromHandle={fromHandle}
93
+ fromX={from.x}
94
+ fromY={from.y}
95
+ toX={to.x}
96
+ toY={to.y}
97
+ fromPosition={fromPosition}
98
+ toPosition={toPosition}
99
+ connectionStatus={getConnectionStatus(isValid)}
100
+ toNode={toNode}
101
+ toHandle={toHandle}
102
+ pointer={pointer}
103
+ />;
104
+ }
105
+
106
+ let path = '';
107
+
108
+ const pathParams = {
109
+ sourceX: from.x,
110
+ sourceY: from.y,
111
+ sourcePosition: fromPosition,
112
+ targetX: to.x,
113
+ targetY: to.y,
114
+ targetPosition: toPosition,
115
+ };
116
+
117
+ switch (type) {
118
+ case ConnectionLineType.Bezier:
119
+ [path] = getBezierPath(pathParams);
120
+ break;
121
+ case ConnectionLineType.SimpleBezier:
122
+ [path] = getSimpleBezierPath(pathParams);
123
+ break;
124
+ case ConnectionLineType.Step:
125
+ [path] = getSmoothStepPath({
126
+ ...pathParams,
127
+ borderRadius: 0,
128
+ });
129
+ break;
130
+ case ConnectionLineType.SmoothStep:
131
+ [path] = getSmoothStepPath(pathParams);
132
+ break;
133
+ default:
134
+ [path] = getStraightPath(pathParams);
135
+ }
136
+
137
+ return <path d={path} fill="none" className="react-flow__connection-path" style={style} />;
138
+ };
139
+
140
+ ConnectionLine.displayName = 'ConnectionLine';
@@ -0,0 +1,62 @@
1
+ import type { OctaneNode } from '../../react-shim.js';
2
+ import { createPortal } from 'octane';
3
+
4
+ import { useStore } from '../../hooks/useStore';
5
+ import type { ReactFlowState } from '../../types';
6
+
7
+ const selector = (s: ReactFlowState) => s.domNode?.querySelector('.react-flow__edgelabel-renderer');
8
+
9
+ export type EdgeLabelRendererProps = {
10
+ children: OctaneNode;
11
+ };
12
+
13
+ /**
14
+ * Edges are SVG-based. If you want to render more complex labels you can use the
15
+ * `<EdgeLabelRenderer />` component to access a div based renderer. This component
16
+ * is a portal that renders the label in a `<div />` that is positioned on top of
17
+ * the edges. You can see an example usage of the component in the
18
+ * [edge label renderer example](/examples/edges/edge-label-renderer).
19
+ * @public
20
+ *
21
+ * @example
22
+ * ```jsx
23
+ * import React from 'octane';
24
+ * import { getBezierPath, EdgeLabelRenderer, BaseEdge } from '@xyflow/react';
25
+ *
26
+ * export function CustomEdge({ id, data, ...props }) {
27
+ * const [edgePath, labelX, labelY] = getBezierPath(props);
28
+ *
29
+ * return (
30
+ * <>
31
+ * <BaseEdge id={id} path={edgePath} />
32
+ * <EdgeLabelRenderer>
33
+ * <div
34
+ * style={{
35
+ * position: 'absolute',
36
+ * transform: `translate(-50%, -50%) translate(${labelX}px,${labelY}px)`,
37
+ * background: '#ffcc00',
38
+ * padding: 10,
39
+ * }}
40
+ * className="nodrag nopan"
41
+ * >
42
+ * {data.label}
43
+ * </div>
44
+ * </EdgeLabelRenderer>
45
+ * </>
46
+ * );
47
+ * };
48
+ * ```
49
+ *
50
+ * @remarks The `<EdgeLabelRenderer />` has no pointer events by default. If you want to
51
+ * add mouse interactions you need to set the style `pointerEvents: all` and add
52
+ * the `nopan` class on the label or the element you want to interact with.
53
+ */
54
+ export function EdgeLabelRenderer({ children }: EdgeLabelRendererProps) {
55
+ const edgeLabelRenderer = useStore(selector);
56
+
57
+ if (!edgeLabelRenderer) {
58
+ return null;
59
+ }
60
+
61
+ return createPortal(children, edgeLabelRenderer);
62
+ }
@@ -0,0 +1,151 @@
1
+ // Reconnectable edges have a anchors around their handles to reconnect the edge.
2
+ import {
3
+ XYHandle,
4
+ type EdgePosition,
5
+ type FinalConnectionState,
6
+ type HandleType,
7
+ type OnConnectStart,
8
+ type Connection,
9
+ } from '@xyflow/system';
10
+
11
+ import { EdgeAnchor } from '../Edges/EdgeAnchor.tsrx';
12
+ import type { MouseEvent as ReactMouseEvent } from '../../react-shim.js';
13
+ import type { EdgeWrapperProps, Edge } from '../../types/edges';
14
+ import { useStoreApi } from '../../hooks/useStore';
15
+
16
+ type EdgeUpdateAnchorsProps<EdgeType extends Edge = Edge> =
17
+ {
18
+ edge: EdgeType;
19
+ isReconnectable: boolean | 'source' | 'target';
20
+ reconnectRadius: EdgeWrapperProps['reconnectRadius'];
21
+ onReconnect: EdgeWrapperProps<EdgeType>['onReconnect'];
22
+ onReconnectStart: EdgeWrapperProps<EdgeType>['onReconnectStart'];
23
+ onReconnectEnd: EdgeWrapperProps<EdgeType>['onReconnectEnd'];
24
+ setUpdateHover: (hover: boolean) => void;
25
+ setReconnecting: (updating: boolean) => void;
26
+ } & EdgePosition;
27
+
28
+ export function EdgeUpdateAnchors<EdgeType extends Edge = Edge>({
29
+ isReconnectable,
30
+ reconnectRadius,
31
+ edge,
32
+ sourceX,
33
+ sourceY,
34
+ targetX,
35
+ targetY,
36
+ sourcePosition,
37
+ targetPosition,
38
+ onReconnect,
39
+ onReconnectStart,
40
+ onReconnectEnd,
41
+ setReconnecting,
42
+ setUpdateHover,
43
+ }: EdgeUpdateAnchorsProps<EdgeType>) {
44
+ const store = useStoreApi();
45
+
46
+ const handleEdgeUpdater = (
47
+ event: ReactMouseEvent<SVGGElement, MouseEvent>,
48
+ oppositeHandle: { nodeId: string; id: string | null; type: HandleType },
49
+ ) => {
50
+ // avoid triggering edge updater if mouse btn is not left
51
+ if (event.button !== 0) {
52
+ return;
53
+ }
54
+
55
+ const {
56
+ autoPanOnConnect,
57
+ domNode,
58
+ connectionMode,
59
+ connectionRadius,
60
+ lib,
61
+ onConnectStart,
62
+ cancelConnection,
63
+ nodeLookup,
64
+ rfId: flowId,
65
+ panBy,
66
+ updateConnection,
67
+ } = store.getState();
68
+ const isTarget = oppositeHandle.type === 'target';
69
+
70
+ const _onReconnectEnd = (
71
+ evt: MouseEvent | TouchEvent,
72
+ connectionState: FinalConnectionState,
73
+ ) => {
74
+ setReconnecting(false);
75
+ onReconnectEnd?.(evt, edge, oppositeHandle.type, connectionState);
76
+ };
77
+
78
+ const onConnectEdge = (connection: Connection) => onReconnect?.(edge, connection);
79
+ const _onConnectStart: OnConnectStart = (_event, params) => {
80
+ setReconnecting(true);
81
+ onReconnectStart?.(event, edge, oppositeHandle.type);
82
+ onConnectStart?.(_event, params);
83
+ };
84
+
85
+ XYHandle.onPointerDown(event, {
86
+ autoPanOnConnect,
87
+ connectionMode,
88
+ connectionRadius,
89
+ domNode,
90
+ handleId: oppositeHandle.id,
91
+ nodeId: oppositeHandle.nodeId,
92
+ nodeLookup,
93
+ isTarget,
94
+ edgeUpdaterType: oppositeHandle.type,
95
+ lib,
96
+ flowId,
97
+ cancelConnection,
98
+ panBy,
99
+ isValidConnection: (...args) => store.getState().isValidConnection?.(...args) ?? true,
100
+ onConnect: onConnectEdge,
101
+ onConnectStart: _onConnectStart,
102
+ onConnectEnd: (...args) => store.getState().onConnectEnd?.(...args),
103
+ onReconnectEnd: _onReconnectEnd,
104
+ updateConnection,
105
+ getTransform: () => store.getState().transform,
106
+ getFromHandle: () => store.getState().connection.fromHandle,
107
+ dragThreshold: store.getState().connectionDragThreshold,
108
+ handleDomNode: event.currentTarget,
109
+ });
110
+ };
111
+
112
+ const onReconnectSourceMouseDown = (event: ReactMouseEvent<SVGGElement, MouseEvent>): void =>
113
+ handleEdgeUpdater(event, {
114
+ nodeId: edge.target,
115
+ id: edge.targetHandle ?? null,
116
+ type: 'target',
117
+ });
118
+ const onReconnectTargetMouseDown = (event: ReactMouseEvent<SVGGElement, MouseEvent>): void =>
119
+ handleEdgeUpdater(event, {
120
+ nodeId: edge.source,
121
+ id: edge.sourceHandle ?? null,
122
+ type: 'source',
123
+ });
124
+ const onReconnectMouseEnter = () => setUpdateHover(true);
125
+ const onReconnectMouseOut = () => setUpdateHover(false);
126
+
127
+ return <>
128
+ {(isReconnectable === true || isReconnectable === 'source') &&
129
+ <EdgeAnchor
130
+ position={sourcePosition}
131
+ centerX={sourceX}
132
+ centerY={sourceY}
133
+ radius={reconnectRadius}
134
+ onMouseDown={onReconnectSourceMouseDown}
135
+ onMouseEnter={onReconnectMouseEnter}
136
+ onMouseOut={onReconnectMouseOut}
137
+ type="source"
138
+ />}
139
+ {(isReconnectable === true || isReconnectable === 'target') &&
140
+ <EdgeAnchor
141
+ position={targetPosition}
142
+ centerX={targetX}
143
+ centerY={targetY}
144
+ radius={reconnectRadius}
145
+ onMouseDown={onReconnectTargetMouseDown}
146
+ onMouseEnter={onReconnectMouseEnter}
147
+ onMouseOut={onReconnectMouseOut}
148
+ type="target"
149
+ />}
150
+ </>;
151
+ }