@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,128 @@
|
|
|
1
|
+
import { useState, type OctaneNode } from 'octane';
|
|
2
|
+
|
|
3
|
+
import { Provider } from '../../contexts/StoreContext';
|
|
4
|
+
import { createStore } from '../../store';
|
|
5
|
+
import { BatchProvider } from '../BatchProvider/index.tsrx';
|
|
6
|
+
import { HandleConfigProvider } from '../../contexts/HandleConfigContext.tsrx';
|
|
7
|
+
import type { Node, Edge, FitViewOptions } from '../../types';
|
|
8
|
+
import { CoordinateExtent, NodeOrigin, ZIndexMode } from '@xyflow/system';
|
|
9
|
+
|
|
10
|
+
export type ReactFlowProviderProps = {
|
|
11
|
+
/** These nodes are used to initialize the flow. They are not dynamic. */
|
|
12
|
+
initialNodes?: Node[];
|
|
13
|
+
/** These edges are used to initialize the flow. They are not dynamic. */
|
|
14
|
+
initialEdges?: Edge[];
|
|
15
|
+
/** These nodes are used to initialize the flow. They are not dynamic. */
|
|
16
|
+
defaultNodes?: Node[];
|
|
17
|
+
/** These edges are used to initialize the flow. They are not dynamic. */
|
|
18
|
+
defaultEdges?: Edge[];
|
|
19
|
+
/** The initial width is necessary to be able to use fitView on the server */
|
|
20
|
+
initialWidth?: number;
|
|
21
|
+
/** The initial height is necessary to be able to use fitView on the server */
|
|
22
|
+
initialHeight?: number;
|
|
23
|
+
/** When `true`, the flow will be zoomed and panned to fit all the nodes initially provided. */
|
|
24
|
+
fitView?: boolean;
|
|
25
|
+
/**
|
|
26
|
+
* You can provide an object of options to customize the initial fitView behavior.
|
|
27
|
+
*/
|
|
28
|
+
initialFitViewOptions?: FitViewOptions;
|
|
29
|
+
/** Initial minimum zoom level */
|
|
30
|
+
initialMinZoom?: number;
|
|
31
|
+
/** Initial maximum zoom level */
|
|
32
|
+
initialMaxZoom?: number;
|
|
33
|
+
/**
|
|
34
|
+
* The origin of the node to use when placing it in the flow or looking up its `x` and `y`
|
|
35
|
+
* position. An origin of `[0, 0]` means that a node's top left corner will be placed at the `x`
|
|
36
|
+
* and `y` position.
|
|
37
|
+
* @default [0, 0]
|
|
38
|
+
* @example
|
|
39
|
+
* [0, 0] // default, top left
|
|
40
|
+
* [0.5, 0.5] // center
|
|
41
|
+
* [1, 1] // bottom right
|
|
42
|
+
*/
|
|
43
|
+
nodeOrigin?: NodeOrigin;
|
|
44
|
+
/**
|
|
45
|
+
* By default, nodes can be placed on an infinite flow. You can use this prop to set a boundary.
|
|
46
|
+
*
|
|
47
|
+
* The first pair of coordinates is the top left boundary and the second pair is the bottom right.
|
|
48
|
+
* @example [[-1000, -10000], [1000, 1000]]
|
|
49
|
+
*/
|
|
50
|
+
nodeExtent?: CoordinateExtent;
|
|
51
|
+
children: OctaneNode;
|
|
52
|
+
zIndexMode?: ZIndexMode;
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* The `<ReactFlowProvider />` component is a [context provider](https://react.dev/learn/passing-data-deeply-with-context#)
|
|
57
|
+
* that makes it possible to access a flow's internal state outside of the
|
|
58
|
+
* [`<ReactFlow />`](/api-reference/react-flow) component. Many of the hooks we
|
|
59
|
+
* provide rely on this component to work.
|
|
60
|
+
* @public
|
|
61
|
+
*
|
|
62
|
+
* @example
|
|
63
|
+
* ```tsx
|
|
64
|
+
*import { ReactFlow, ReactFlowProvider, useNodes } from '@xyflow/react'
|
|
65
|
+
*
|
|
66
|
+
*export default function Flow() {
|
|
67
|
+
* return (
|
|
68
|
+
* <ReactFlowProvider>
|
|
69
|
+
* <ReactFlow nodes={...} edges={...} />
|
|
70
|
+
* <Sidebar />
|
|
71
|
+
* </ReactFlowProvider>
|
|
72
|
+
* );
|
|
73
|
+
*}
|
|
74
|
+
*
|
|
75
|
+
*function Sidebar() {
|
|
76
|
+
* // This hook will only work if the component it's used in is a child of a
|
|
77
|
+
* // <ReactFlowProvider />.
|
|
78
|
+
* const nodes = useNodes()
|
|
79
|
+
*
|
|
80
|
+
* return <aside>do something with nodes</aside>;
|
|
81
|
+
*}
|
|
82
|
+
*```
|
|
83
|
+
*
|
|
84
|
+
* @remarks If you're using a router and want your flow's state to persist across routes,
|
|
85
|
+
* it's vital that you place the `<ReactFlowProvider />` component _outside_ of
|
|
86
|
+
* your router. If you have multiple flows on the same page you will need to use a separate
|
|
87
|
+
* `<ReactFlowProvider />` for each flow.
|
|
88
|
+
*/
|
|
89
|
+
export function ReactFlowProvider({
|
|
90
|
+
initialNodes: nodes,
|
|
91
|
+
initialEdges: edges,
|
|
92
|
+
defaultNodes,
|
|
93
|
+
defaultEdges,
|
|
94
|
+
initialWidth: width,
|
|
95
|
+
initialHeight: height,
|
|
96
|
+
initialMinZoom: minZoom,
|
|
97
|
+
initialMaxZoom: maxZoom,
|
|
98
|
+
initialFitViewOptions: fitViewOptions,
|
|
99
|
+
fitView,
|
|
100
|
+
nodeOrigin,
|
|
101
|
+
nodeExtent,
|
|
102
|
+
zIndexMode,
|
|
103
|
+
children,
|
|
104
|
+
}: ReactFlowProviderProps) {
|
|
105
|
+
const [store] = useState(
|
|
106
|
+
() => createStore({
|
|
107
|
+
nodes,
|
|
108
|
+
edges,
|
|
109
|
+
defaultNodes,
|
|
110
|
+
defaultEdges,
|
|
111
|
+
width,
|
|
112
|
+
height,
|
|
113
|
+
fitView,
|
|
114
|
+
minZoom,
|
|
115
|
+
maxZoom,
|
|
116
|
+
fitViewOptions,
|
|
117
|
+
nodeOrigin,
|
|
118
|
+
nodeExtent,
|
|
119
|
+
zIndexMode,
|
|
120
|
+
}),
|
|
121
|
+
);
|
|
122
|
+
|
|
123
|
+
return <Provider value={store}>
|
|
124
|
+
<BatchProvider>
|
|
125
|
+
<HandleConfigProvider>{children}</HandleConfigProvider>
|
|
126
|
+
</BatchProvider>
|
|
127
|
+
</Provider>;
|
|
128
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* This is a helper component for calling the onSelectionChange listener.
|
|
3
|
+
* It will only be mounted if the user has passed an onSelectionChange listener
|
|
4
|
+
* or is using the useOnSelectionChange hook.
|
|
5
|
+
* @TODO: Now that we have the onNodesChange and on EdgesChange listeners, do we still need this component?
|
|
6
|
+
*/
|
|
7
|
+
import { useEffect } from 'octane';
|
|
8
|
+
import { shallow } from '@octanejs/zustand/shallow';
|
|
9
|
+
|
|
10
|
+
import { useStore, useStoreApi } from '../../hooks/useStore';
|
|
11
|
+
import type { ReactFlowState, OnSelectionChangeFunc, Node, Edge } from '../../types';
|
|
12
|
+
|
|
13
|
+
type SelectionListenerProps<NodeType extends Node = Node, EdgeType extends Edge = Edge> = {
|
|
14
|
+
onSelectionChange?: OnSelectionChangeFunc<NodeType, EdgeType>;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
const selector = (s: ReactFlowState) => {
|
|
18
|
+
const selectedNodes = [];
|
|
19
|
+
const selectedEdges = [];
|
|
20
|
+
|
|
21
|
+
for (const [, node] of s.nodeLookup) {
|
|
22
|
+
if (node.selected) {
|
|
23
|
+
selectedNodes.push(node.internals.userNode);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
for (const [, edge] of s.edgeLookup) {
|
|
28
|
+
if (edge.selected) {
|
|
29
|
+
selectedEdges.push(edge);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
return { selectedNodes, selectedEdges };
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
type SelectorSlice = ReturnType<typeof selector>;
|
|
37
|
+
|
|
38
|
+
const selectId = (obj: Node | Edge) => obj.id;
|
|
39
|
+
|
|
40
|
+
function areEqual(a: SelectorSlice, b: SelectorSlice) {
|
|
41
|
+
return shallow(a.selectedNodes.map(selectId), b.selectedNodes.map(selectId)) &&
|
|
42
|
+
shallow(a.selectedEdges.map(selectId), b.selectedEdges.map(selectId));
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function SelectionListenerInner<NodeType extends Node = Node, EdgeType extends Edge = Edge>({
|
|
46
|
+
onSelectionChange,
|
|
47
|
+
}: SelectionListenerProps<NodeType, EdgeType>) {
|
|
48
|
+
const store = useStoreApi<NodeType, EdgeType>();
|
|
49
|
+
const { selectedNodes, selectedEdges } = useStore(selector, areEqual);
|
|
50
|
+
|
|
51
|
+
useEffect(() => {
|
|
52
|
+
const params = { nodes: selectedNodes as NodeType[], edges: selectedEdges as EdgeType[] };
|
|
53
|
+
|
|
54
|
+
onSelectionChange?.(params);
|
|
55
|
+
store.getState().onSelectionChangeHandlers.forEach((fn) => fn(params));
|
|
56
|
+
}, [selectedNodes, selectedEdges, onSelectionChange]);
|
|
57
|
+
|
|
58
|
+
return null;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const changeSelector = (s: ReactFlowState) => !!s.onSelectionChangeHandlers;
|
|
62
|
+
|
|
63
|
+
export function SelectionListener<NodeType extends Node = Node, EdgeType extends Edge = Edge>({
|
|
64
|
+
onSelectionChange,
|
|
65
|
+
}: SelectionListenerProps<NodeType, EdgeType>) {
|
|
66
|
+
const storeHasSelectionChangeHandlers = useStore(changeSelector);
|
|
67
|
+
|
|
68
|
+
if (onSelectionChange || storeHasSelectionChangeHandlers) {
|
|
69
|
+
return <SelectionListenerInner onSelectionChange={onSelectionChange} />;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
return null;
|
|
73
|
+
}
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* This component helps us to update the store with the values coming from the user.
|
|
3
|
+
* We distinguish between values we can update directly with `useDirectStoreUpdater` (like `snapGrid`)
|
|
4
|
+
* and values that have a dedicated setter function in the store (like `setNodes`).
|
|
5
|
+
*/
|
|
6
|
+
import { useEffect, useRef } from 'octane';
|
|
7
|
+
import { shallow } from '@octanejs/zustand/shallow';
|
|
8
|
+
import {
|
|
9
|
+
infiniteExtent,
|
|
10
|
+
type CoordinateExtent,
|
|
11
|
+
mergeAriaLabelConfig,
|
|
12
|
+
AriaLabelConfig,
|
|
13
|
+
} from '@xyflow/system';
|
|
14
|
+
|
|
15
|
+
import { useStore, useStoreApi } from '../../hooks/useStore';
|
|
16
|
+
import type { Node, Edge, ReactFlowState, ReactFlowProps, FitViewOptions } from '../../types';
|
|
17
|
+
import { defaultNodeOrigin } from '../../container/ReactFlow/init-values';
|
|
18
|
+
|
|
19
|
+
// These fields exist in the global store, and we need to keep them up to date
|
|
20
|
+
const reactFlowFieldsToTrack = [
|
|
21
|
+
'nodes',
|
|
22
|
+
'edges',
|
|
23
|
+
'defaultNodes',
|
|
24
|
+
'defaultEdges',
|
|
25
|
+
'onConnect',
|
|
26
|
+
'onConnectStart',
|
|
27
|
+
'onConnectEnd',
|
|
28
|
+
'onClickConnectStart',
|
|
29
|
+
'onClickConnectEnd',
|
|
30
|
+
'nodesDraggable',
|
|
31
|
+
'autoPanOnNodeFocus',
|
|
32
|
+
'nodesConnectable',
|
|
33
|
+
'nodesFocusable',
|
|
34
|
+
'edgesFocusable',
|
|
35
|
+
'edgesReconnectable',
|
|
36
|
+
'elevateNodesOnSelect',
|
|
37
|
+
'elevateEdgesOnSelect',
|
|
38
|
+
'minZoom',
|
|
39
|
+
'maxZoom',
|
|
40
|
+
'nodeExtent',
|
|
41
|
+
'onNodesChange',
|
|
42
|
+
'onEdgesChange',
|
|
43
|
+
'elementsSelectable',
|
|
44
|
+
'connectionMode',
|
|
45
|
+
'snapGrid',
|
|
46
|
+
'snapToGrid',
|
|
47
|
+
'translateExtent',
|
|
48
|
+
'connectOnClick',
|
|
49
|
+
'defaultEdgeOptions',
|
|
50
|
+
'fitView',
|
|
51
|
+
'fitViewOptions',
|
|
52
|
+
'onNodesDelete',
|
|
53
|
+
'onEdgesDelete',
|
|
54
|
+
'onDelete',
|
|
55
|
+
'onNodeDrag',
|
|
56
|
+
'onNodeDragStart',
|
|
57
|
+
'onNodeDragStop',
|
|
58
|
+
'onSelectionDrag',
|
|
59
|
+
'onSelectionDragStart',
|
|
60
|
+
'onSelectionDragStop',
|
|
61
|
+
'onMoveStart',
|
|
62
|
+
'onMove',
|
|
63
|
+
'onMoveEnd',
|
|
64
|
+
'noPanClassName',
|
|
65
|
+
'nodeOrigin',
|
|
66
|
+
'autoPanOnConnect',
|
|
67
|
+
'autoPanOnNodeDrag',
|
|
68
|
+
'onError',
|
|
69
|
+
'connectionRadius',
|
|
70
|
+
'isValidConnection',
|
|
71
|
+
'selectNodesOnDrag',
|
|
72
|
+
'nodeDragThreshold',
|
|
73
|
+
'connectionDragThreshold',
|
|
74
|
+
'onBeforeDelete',
|
|
75
|
+
'debug',
|
|
76
|
+
'autoPanSpeed',
|
|
77
|
+
'ariaLabelConfig',
|
|
78
|
+
'zIndexMode',
|
|
79
|
+
] as const;
|
|
80
|
+
|
|
81
|
+
type ReactFlowFieldsToTrack = (typeof reactFlowFieldsToTrack)[number];
|
|
82
|
+
type StoreUpdaterProps<NodeType extends Node = Node, EdgeType extends Edge = Edge> =
|
|
83
|
+
Pick<ReactFlowProps<NodeType, EdgeType>, ReactFlowFieldsToTrack> & {
|
|
84
|
+
rfId: string;
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
// rfId doesn't exist in ReactFlowProps, but it's one of the fields we want to update
|
|
88
|
+
const fieldsToTrack = [...reactFlowFieldsToTrack, 'rfId'] as const;
|
|
89
|
+
|
|
90
|
+
const selector = (s: ReactFlowState) => ({
|
|
91
|
+
setNodes: s.setNodes,
|
|
92
|
+
setEdges: s.setEdges,
|
|
93
|
+
setMinZoom: s.setMinZoom,
|
|
94
|
+
setMaxZoom: s.setMaxZoom,
|
|
95
|
+
setTranslateExtent: s.setTranslateExtent,
|
|
96
|
+
setNodeExtent: s.setNodeExtent,
|
|
97
|
+
reset: s.reset,
|
|
98
|
+
setDefaultNodesAndEdges: s.setDefaultNodesAndEdges,
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
const initPrevValues = {
|
|
102
|
+
/*
|
|
103
|
+
* these are values that are also passed directly to other components
|
|
104
|
+
* than the StoreUpdater. We can reduce the number of setStore calls
|
|
105
|
+
* by setting the same values here as prev fields.
|
|
106
|
+
*/
|
|
107
|
+
translateExtent: infiniteExtent,
|
|
108
|
+
nodeOrigin: defaultNodeOrigin,
|
|
109
|
+
minZoom: 0.5,
|
|
110
|
+
maxZoom: 2,
|
|
111
|
+
elementsSelectable: true,
|
|
112
|
+
noPanClassName: 'nopan',
|
|
113
|
+
rfId: '1',
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
export function StoreUpdater<NodeType extends Node = Node, EdgeType extends Edge = Edge>(
|
|
117
|
+
props: StoreUpdaterProps<NodeType, EdgeType>,
|
|
118
|
+
) {
|
|
119
|
+
const {
|
|
120
|
+
setNodes,
|
|
121
|
+
setEdges,
|
|
122
|
+
setMinZoom,
|
|
123
|
+
setMaxZoom,
|
|
124
|
+
setTranslateExtent,
|
|
125
|
+
setNodeExtent,
|
|
126
|
+
reset,
|
|
127
|
+
setDefaultNodesAndEdges,
|
|
128
|
+
} = useStore(selector, shallow);
|
|
129
|
+
const store = useStoreApi<NodeType, EdgeType>();
|
|
130
|
+
|
|
131
|
+
useEffect(() => {
|
|
132
|
+
setDefaultNodesAndEdges(props.defaultNodes, props.defaultEdges);
|
|
133
|
+
|
|
134
|
+
return () => {
|
|
135
|
+
// when we reset the store we also need to reset the previous fields
|
|
136
|
+
previousFields.current = initPrevValues;
|
|
137
|
+
reset();
|
|
138
|
+
};
|
|
139
|
+
}, []);
|
|
140
|
+
|
|
141
|
+
const previousFields = useRef<Partial<StoreUpdaterProps<NodeType, EdgeType>>>(initPrevValues);
|
|
142
|
+
|
|
143
|
+
useEffect(() => {
|
|
144
|
+
for (const fieldName of fieldsToTrack) {
|
|
145
|
+
const fieldValue = props[fieldName];
|
|
146
|
+
const previousFieldValue = previousFields.current[fieldName];
|
|
147
|
+
|
|
148
|
+
if (fieldValue === previousFieldValue) continue;
|
|
149
|
+
if (typeof props[fieldName] === 'undefined') continue;
|
|
150
|
+
// Custom handling with dedicated setters for some fields
|
|
151
|
+
if (fieldName === 'nodes') setNodes(fieldValue as Node[]);
|
|
152
|
+
else if (fieldName === 'edges') setEdges(fieldValue as Edge[]);
|
|
153
|
+
else if (fieldName === 'minZoom') setMinZoom(fieldValue as number);
|
|
154
|
+
else if (fieldName === 'maxZoom') setMaxZoom(fieldValue as number);
|
|
155
|
+
else if (fieldName === 'translateExtent') setTranslateExtent(fieldValue as CoordinateExtent);
|
|
156
|
+
else if (fieldName === 'nodeExtent') setNodeExtent(fieldValue as CoordinateExtent);
|
|
157
|
+
else if (fieldName === 'ariaLabelConfig') store.setState({
|
|
158
|
+
ariaLabelConfig: mergeAriaLabelConfig(fieldValue as AriaLabelConfig),
|
|
159
|
+
});
|
|
160
|
+
else // Renamed fields
|
|
161
|
+
if (fieldName === 'fitView') store.setState({ fitViewQueued: fieldValue as boolean });
|
|
162
|
+
else if (fieldName === 'fitViewOptions') store.setState({
|
|
163
|
+
fitViewOptions: fieldValue as FitViewOptions,
|
|
164
|
+
});
|
|
165
|
+
else // General case
|
|
166
|
+
store.setState({ [fieldName]: fieldValue });
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
previousFields.current = props;
|
|
170
|
+
}, fieldsToTrack.map((fieldName) => props[fieldName])); // Only re-run the effect if one of the fields we track changes
|
|
171
|
+
|
|
172
|
+
return null;
|
|
173
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { shallow } from '@octanejs/zustand/shallow';
|
|
2
|
+
|
|
3
|
+
import { useStore } from '../../hooks/useStore';
|
|
4
|
+
import type { ReactFlowState } from '../../types';
|
|
5
|
+
|
|
6
|
+
const selector = (s: ReactFlowState) => ({
|
|
7
|
+
userSelectionActive: s.userSelectionActive,
|
|
8
|
+
userSelectionRect: s.userSelectionRect,
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
export function UserSelection() {
|
|
12
|
+
const { userSelectionActive, userSelectionRect } = useStore(selector, shallow);
|
|
13
|
+
const isActive = userSelectionActive && userSelectionRect;
|
|
14
|
+
|
|
15
|
+
if (!isActive) {
|
|
16
|
+
return null;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
return <div
|
|
20
|
+
className="react-flow__selection react-flow__container"
|
|
21
|
+
style={{
|
|
22
|
+
width: userSelectionRect.width,
|
|
23
|
+
height: userSelectionRect.height,
|
|
24
|
+
transform: `translate(${userSelectionRect.x}px, ${userSelectionRect.y}px)`,
|
|
25
|
+
}}
|
|
26
|
+
/>;
|
|
27
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
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__viewport-portal');
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* The `<ViewportPortal />` component can be used to add components to the same viewport
|
|
11
|
+
* of the flow where nodes and edges are rendered. This is useful when you want to render
|
|
12
|
+
* your own components that are adhere to the same coordinate system as the nodes & edges
|
|
13
|
+
* and are also affected by zooming and panning
|
|
14
|
+
* @public
|
|
15
|
+
* @example
|
|
16
|
+
*
|
|
17
|
+
* ```jsx
|
|
18
|
+
*import React from 'octane';
|
|
19
|
+
*import { ViewportPortal } from '@xyflow/react';
|
|
20
|
+
*
|
|
21
|
+
*export default function () {
|
|
22
|
+
* return (
|
|
23
|
+
* <ViewportPortal>
|
|
24
|
+
* <div
|
|
25
|
+
* style={{ transform: 'translate(100px, 100px)', position: 'absolute' }}
|
|
26
|
+
* >
|
|
27
|
+
* This div is positioned at [100, 100] on the flow.
|
|
28
|
+
* </div>
|
|
29
|
+
* </ViewportPortal>
|
|
30
|
+
* );
|
|
31
|
+
*}
|
|
32
|
+
*```
|
|
33
|
+
*/
|
|
34
|
+
export function ViewportPortal({ children }: { children: OctaneNode }) {
|
|
35
|
+
const viewPortalDiv = useStore(selector);
|
|
36
|
+
|
|
37
|
+
if (!viewPortalDiv) {
|
|
38
|
+
return null;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
return createPortal(children, viewPortalDiv);
|
|
42
|
+
}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { memo, useMemo } from 'octane';
|
|
2
|
+
import { type MarkerProps, createMarkerIds } from '@xyflow/system';
|
|
3
|
+
|
|
4
|
+
import { useStore } from '../../hooks/useStore';
|
|
5
|
+
import { useMarkerSymbol } from './MarkerSymbols.tsrx';
|
|
6
|
+
|
|
7
|
+
type MarkerDefinitionsProps = {
|
|
8
|
+
defaultColor: string | null;
|
|
9
|
+
rfId?: string;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
const Marker = ({
|
|
13
|
+
id,
|
|
14
|
+
type,
|
|
15
|
+
color,
|
|
16
|
+
width = 12.5,
|
|
17
|
+
height = 12.5,
|
|
18
|
+
markerUnits = 'strokeWidth',
|
|
19
|
+
strokeWidth,
|
|
20
|
+
orient = 'auto-start-reverse',
|
|
21
|
+
}: MarkerProps) => {
|
|
22
|
+
const Symbol = useMarkerSymbol(type);
|
|
23
|
+
|
|
24
|
+
if (!Symbol) {
|
|
25
|
+
return null;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
return <marker
|
|
29
|
+
className="react-flow__arrowhead"
|
|
30
|
+
id={id}
|
|
31
|
+
markerWidth={`${width}`}
|
|
32
|
+
markerHeight={`${height}`}
|
|
33
|
+
viewBox="-10 -10 20 20"
|
|
34
|
+
markerUnits={markerUnits}
|
|
35
|
+
orient={orient}
|
|
36
|
+
refX="0"
|
|
37
|
+
refY="0"
|
|
38
|
+
>
|
|
39
|
+
<Symbol color={color} strokeWidth={strokeWidth} />
|
|
40
|
+
</marker>;
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
/*
|
|
44
|
+
* when you have multiple flows on a page and you hide the first one, the other ones have no markers anymore
|
|
45
|
+
* when they do have markers with the same ids. To prevent this the user can pass a unique id to the react flow wrapper
|
|
46
|
+
* that we can then use for creating our unique marker ids
|
|
47
|
+
*/
|
|
48
|
+
const MarkerDefinitions = ({ defaultColor, rfId }: MarkerDefinitionsProps) => {
|
|
49
|
+
const edges = useStore((s) => s.edges);
|
|
50
|
+
const defaultEdgeOptions = useStore((s) => s.defaultEdgeOptions);
|
|
51
|
+
|
|
52
|
+
const markers = useMemo(() => {
|
|
53
|
+
const markers = createMarkerIds(edges, {
|
|
54
|
+
id: rfId,
|
|
55
|
+
defaultColor,
|
|
56
|
+
defaultMarkerStart: defaultEdgeOptions?.markerStart,
|
|
57
|
+
defaultMarkerEnd: defaultEdgeOptions?.markerEnd,
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
return markers;
|
|
61
|
+
}, [edges, defaultEdgeOptions, rfId, defaultColor]);
|
|
62
|
+
|
|
63
|
+
if (!markers.length) {
|
|
64
|
+
return null;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
return <svg className="react-flow__marker" aria-hidden="true">
|
|
68
|
+
<defs>{markers.map(
|
|
69
|
+
(marker: MarkerProps) =>
|
|
70
|
+
<Marker
|
|
71
|
+
id={marker.id}
|
|
72
|
+
key={marker.id}
|
|
73
|
+
type={marker.type}
|
|
74
|
+
color={marker.color}
|
|
75
|
+
width={marker.width}
|
|
76
|
+
height={marker.height}
|
|
77
|
+
markerUnits={marker.markerUnits}
|
|
78
|
+
strokeWidth={marker.strokeWidth}
|
|
79
|
+
orient={marker.orient}
|
|
80
|
+
/>,
|
|
81
|
+
)}</defs>
|
|
82
|
+
</svg>;
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
MarkerDefinitions.displayName = 'MarkerDefinitions';
|
|
86
|
+
|
|
87
|
+
export default memo(MarkerDefinitions);
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { useMemo } from 'octane';
|
|
2
|
+
import { errorMessages, MarkerType, type EdgeMarker } from '@xyflow/system';
|
|
3
|
+
|
|
4
|
+
import { useStoreApi } from '../../hooks/useStore';
|
|
5
|
+
|
|
6
|
+
type SymbolProps = Omit<EdgeMarker, 'type'>;
|
|
7
|
+
|
|
8
|
+
const ArrowSymbol = ({ color = 'none', strokeWidth = 1 }: SymbolProps) => {
|
|
9
|
+
const style = {
|
|
10
|
+
strokeWidth,
|
|
11
|
+
...color && { stroke: color },
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
return <polyline
|
|
15
|
+
className="arrow"
|
|
16
|
+
style={style}
|
|
17
|
+
strokeLinecap="round"
|
|
18
|
+
fill="none"
|
|
19
|
+
strokeLinejoin="round"
|
|
20
|
+
points="-5,-4 0,0 -5,4"
|
|
21
|
+
/>;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
const ArrowClosedSymbol = ({ color = 'none', strokeWidth = 1 }: SymbolProps) => {
|
|
25
|
+
const style = {
|
|
26
|
+
strokeWidth,
|
|
27
|
+
...color && { stroke: color, fill: color },
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
return <polyline
|
|
31
|
+
className="arrowclosed"
|
|
32
|
+
style={style}
|
|
33
|
+
strokeLinecap="round"
|
|
34
|
+
strokeLinejoin="round"
|
|
35
|
+
points="-5,-4 0,0 -5,4 -5,-4"
|
|
36
|
+
/>;
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
export const MarkerSymbols = {
|
|
40
|
+
[MarkerType.Arrow]: ArrowSymbol,
|
|
41
|
+
[MarkerType.ArrowClosed]: ArrowClosedSymbol,
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
export function useMarkerSymbol(type: MarkerType | `${MarkerType}`) {
|
|
45
|
+
const store = useStoreApi();
|
|
46
|
+
|
|
47
|
+
const symbol = useMemo(() => {
|
|
48
|
+
const symbolExists = Object.prototype.hasOwnProperty.call(MarkerSymbols, type);
|
|
49
|
+
|
|
50
|
+
if (!symbolExists) {
|
|
51
|
+
store.getState().onError?.('009', errorMessages['error009'](type));
|
|
52
|
+
|
|
53
|
+
return null;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
return MarkerSymbols[type];
|
|
57
|
+
}, [type]);
|
|
58
|
+
|
|
59
|
+
return symbol;
|
|
60
|
+
}
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { memo, OctaneNode } from 'octane';
|
|
2
|
+
import { shallow } from '@octanejs/zustand/shallow';
|
|
3
|
+
|
|
4
|
+
import { useStore } from '../../hooks/useStore';
|
|
5
|
+
import { useVisibleEdgeIds } from '../../hooks/useVisibleEdgeIds';
|
|
6
|
+
import MarkerDefinitions from './MarkerDefinitions.tsrx';
|
|
7
|
+
import { GraphViewProps } from '../GraphView/index.tsrx';
|
|
8
|
+
import EdgeWrapper from '../../components/EdgeWrapper/index.tsrx';
|
|
9
|
+
import type { Edge, ReactFlowState, Node } from '../../types';
|
|
10
|
+
|
|
11
|
+
type EdgeRendererProps<EdgeType extends Edge = Edge> =
|
|
12
|
+
Pick<
|
|
13
|
+
GraphViewProps<Node, EdgeType>,
|
|
14
|
+
| 'onEdgeClick'
|
|
15
|
+
| 'onEdgeDoubleClick'
|
|
16
|
+
| 'defaultMarkerColor'
|
|
17
|
+
| 'onlyRenderVisibleElements'
|
|
18
|
+
| 'onReconnect'
|
|
19
|
+
| 'onEdgeContextMenu'
|
|
20
|
+
| 'onEdgeMouseEnter'
|
|
21
|
+
| 'onEdgeMouseMove'
|
|
22
|
+
| 'onEdgeMouseLeave'
|
|
23
|
+
| 'onReconnectStart'
|
|
24
|
+
| 'onReconnectEnd'
|
|
25
|
+
| 'reconnectRadius'
|
|
26
|
+
| 'noPanClassName'
|
|
27
|
+
| 'rfId'
|
|
28
|
+
| 'disableKeyboardA11y'
|
|
29
|
+
| 'edgeTypes'
|
|
30
|
+
> & {
|
|
31
|
+
children?: OctaneNode;
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
const selector = (s: ReactFlowState) => ({
|
|
35
|
+
edgesFocusable: s.edgesFocusable,
|
|
36
|
+
edgesReconnectable: s.edgesReconnectable,
|
|
37
|
+
elementsSelectable: s.elementsSelectable,
|
|
38
|
+
connectionMode: s.connectionMode,
|
|
39
|
+
onError: s.onError,
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
function EdgeRendererComponent<EdgeType extends Edge = Edge>({
|
|
43
|
+
defaultMarkerColor,
|
|
44
|
+
onlyRenderVisibleElements,
|
|
45
|
+
rfId,
|
|
46
|
+
edgeTypes,
|
|
47
|
+
noPanClassName,
|
|
48
|
+
onReconnect,
|
|
49
|
+
onEdgeContextMenu,
|
|
50
|
+
onEdgeMouseEnter,
|
|
51
|
+
onEdgeMouseMove,
|
|
52
|
+
onEdgeMouseLeave,
|
|
53
|
+
onEdgeClick,
|
|
54
|
+
reconnectRadius,
|
|
55
|
+
onEdgeDoubleClick,
|
|
56
|
+
onReconnectStart,
|
|
57
|
+
onReconnectEnd,
|
|
58
|
+
disableKeyboardA11y,
|
|
59
|
+
}: EdgeRendererProps<EdgeType>) {
|
|
60
|
+
const { edgesFocusable, edgesReconnectable, elementsSelectable, onError } = useStore(
|
|
61
|
+
selector,
|
|
62
|
+
shallow,
|
|
63
|
+
);
|
|
64
|
+
const edgeIds = useVisibleEdgeIds(onlyRenderVisibleElements);
|
|
65
|
+
|
|
66
|
+
return <div className="react-flow__edges">
|
|
67
|
+
<MarkerDefinitions defaultColor={defaultMarkerColor} rfId={rfId} />
|
|
68
|
+
|
|
69
|
+
{edgeIds.map((id) => {
|
|
70
|
+
return <EdgeWrapper
|
|
71
|
+
key={id}
|
|
72
|
+
id={id}
|
|
73
|
+
edgesFocusable={edgesFocusable}
|
|
74
|
+
edgesReconnectable={edgesReconnectable}
|
|
75
|
+
elementsSelectable={elementsSelectable}
|
|
76
|
+
noPanClassName={noPanClassName}
|
|
77
|
+
onReconnect={onReconnect}
|
|
78
|
+
onContextMenu={onEdgeContextMenu}
|
|
79
|
+
onMouseEnter={onEdgeMouseEnter}
|
|
80
|
+
onMouseMove={onEdgeMouseMove}
|
|
81
|
+
onMouseLeave={onEdgeMouseLeave}
|
|
82
|
+
onClick={onEdgeClick}
|
|
83
|
+
reconnectRadius={reconnectRadius}
|
|
84
|
+
onDoubleClick={onEdgeDoubleClick}
|
|
85
|
+
onReconnectStart={onReconnectStart}
|
|
86
|
+
onReconnectEnd={onReconnectEnd}
|
|
87
|
+
rfId={rfId}
|
|
88
|
+
onError={onError}
|
|
89
|
+
edgeTypes={edgeTypes}
|
|
90
|
+
disableKeyboardA11y={disableKeyboardA11y}
|
|
91
|
+
/>;
|
|
92
|
+
})}
|
|
93
|
+
</div>;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
EdgeRendererComponent.displayName = 'EdgeRenderer';
|
|
97
|
+
|
|
98
|
+
export const EdgeRenderer = memo(EdgeRendererComponent) as typeof EdgeRendererComponent;
|