@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,79 @@
1
+ import { useCallback } from 'octane';
2
+ import cc from 'classcat';
3
+ import { shallow } from '@octanejs/zustand/shallow';
4
+ import { getEdgeToolbarTransform } from '@xyflow/system';
5
+
6
+ import { EdgeLabelRenderer } from '../../components/EdgeLabelRenderer/index.tsrx';
7
+ import { useStore } from '../../hooks/useStore';
8
+ import { Edge, ReactFlowState } from '../../types';
9
+ import type { EdgeToolbarProps } from './types';
10
+
11
+ const zoomSelector = (state: ReactFlowState) => state.transform[2];
12
+
13
+ /**
14
+ * This component can render a toolbar or tooltip to one side of a custom edge. This
15
+ * toolbar doesn't scale with the viewport so that the content stays the same size.
16
+ *
17
+ * @public
18
+ * @example
19
+ * ```jsx
20
+ * import { EdgeToolbar, BaseEdge, getBezierPath, type EdgeProps } from "@xyflow/react";
21
+ *
22
+ * export function CustomEdge({ id, data, ...props }: EdgeProps) {
23
+ * const [edgePath, centerX, centerY] = getBezierPath(props);
24
+ *
25
+ * return (
26
+ * <>
27
+ * <BaseEdge id={id} path={edgePath} />
28
+ * <EdgeToolbar edgeId={id} x={centerX} y={centerY} isVisible>
29
+ * <button onClick={() => console.log('edge', id, 'click')}}>Click me</button>
30
+ * </EdgeToolbar>
31
+ * </>
32
+ * );
33
+ * }
34
+ * ```
35
+ */
36
+ export function EdgeToolbar({
37
+ edgeId,
38
+ x,
39
+ y,
40
+ children,
41
+ className,
42
+ style,
43
+ isVisible,
44
+ alignX = 'center',
45
+ alignY = 'center',
46
+ ...rest
47
+ }: EdgeToolbarProps) {
48
+ const edgeSelector = useCallback(
49
+ (state: ReactFlowState): Edge | undefined => state.edgeLookup.get(edgeId),
50
+ [edgeId],
51
+ );
52
+ const edge = useStore(edgeSelector, shallow);
53
+ const isActive =
54
+ typeof isVisible === 'boolean' ? isVisible : edge?.selected;
55
+ const zoom = useStore(zoomSelector);
56
+
57
+ if (!isActive) {
58
+ return null;
59
+ }
60
+
61
+ const zIndex = (edge?.zIndex ?? 0) + 1;
62
+ const transform = getEdgeToolbarTransform(x, y, zoom, alignX, alignY);
63
+
64
+ return <EdgeLabelRenderer>
65
+ <div
66
+ style={{
67
+ position: 'absolute',
68
+ transform,
69
+ zIndex,
70
+ pointerEvents: 'all',
71
+ transformOrigin: '0 0',
72
+ ...style,
73
+ }}
74
+ className={cc(['react-flow__edge-toolbar', className])}
75
+ data-id={edge?.id ?? ''}
76
+ {...rest}
77
+ >{children}</div>
78
+ </EdgeLabelRenderer>;
79
+ }
@@ -0,0 +1,2 @@
1
+ export { EdgeToolbar } from './EdgeToolbar.tsrx';
2
+ export type { EdgeToolbarProps } from './types';
@@ -0,0 +1,14 @@
1
+ import type { HTMLAttributes, OctaneNode } from '../../react-shim.js';
2
+ import type { EdgeToolbarBaseProps } from '@xyflow/system';
3
+
4
+ /**
5
+ * @inline
6
+ */
7
+ export type EdgeToolbarProps = EdgeToolbarBaseProps &
8
+ HTMLAttributes<HTMLDivElement> & {
9
+ /**
10
+ * An edge toolbar must be attached to an edge.
11
+ */
12
+ edgeId: string;
13
+ children?: OctaneNode;
14
+ };
@@ -0,0 +1,249 @@
1
+ /* eslint-disable @typescript-eslint/ban-ts-comment */
2
+ /* eslint-disable @typescript-eslint/no-explicit-any */
3
+ import { memo, useEffect, useRef, useCallback } from 'octane';
4
+ import type { CSSProperties, MouseEvent } from '../../react-shim.js';
5
+ import cc from 'classcat';
6
+ import {
7
+ getInternalNodesBounds,
8
+ getBoundsOfRects,
9
+ XYMinimap,
10
+ type Rect,
11
+ type XYMinimapInstance,
12
+ } from '@xyflow/system';
13
+
14
+ import { useStore, useStoreApi } from '../../hooks/useStore';
15
+ import { Panel } from '../../components/Panel/index.tsrx';
16
+ import type { ReactFlowState, Node } from '../../types';
17
+
18
+ import MiniMapNodes from './MiniMapNodes.tsrx';
19
+ import type { MiniMapProps } from './types';
20
+
21
+ const defaultWidth = 200;
22
+ const defaultHeight = 150;
23
+
24
+ const filterHidden = (node: Node) => !node.hidden;
25
+
26
+ const selector = (s: ReactFlowState) => {
27
+ const viewBB: Rect = {
28
+ x: -s.transform[0] / s.transform[2],
29
+ y: -s.transform[1] / s.transform[2],
30
+ width: s.width / s.transform[2],
31
+ height: s.height / s.transform[2],
32
+ };
33
+
34
+ return {
35
+ viewBB,
36
+ boundingRect: s.nodeLookup.size > 0
37
+ ? getBoundsOfRects(getInternalNodesBounds(s.nodeLookup, { filter: filterHidden }), viewBB)
38
+ : viewBB,
39
+ rfId: s.rfId,
40
+ panZoom: s.panZoom,
41
+ translateExtent: s.translateExtent,
42
+ flowWidth: s.width,
43
+ flowHeight: s.height,
44
+ ariaLabelConfig: s.ariaLabelConfig,
45
+ };
46
+ };
47
+
48
+ type MiniMapSlice = ReturnType<typeof selector>;
49
+
50
+ const rectEqual = (a: Rect, b: Rect) =>
51
+ a.x === b.x && a.y === b.y && a.width === b.width && a.height === b.height;
52
+
53
+ // the selector builds new viewBB/boundingRect objects every call, so the default shallow equality always
54
+ // treats them as changed; comparing the rects by value lets the minimap skip re-renders when nothing moved
55
+ const areEqual = (a: MiniMapSlice, b: MiniMapSlice) =>
56
+ rectEqual(a.viewBB, b.viewBB) && rectEqual(a.boundingRect, b.boundingRect) && a.rfId === b.rfId &&
57
+ a.panZoom === b.panZoom &&
58
+ a.translateExtent === b.translateExtent &&
59
+ a.flowWidth === b.flowWidth &&
60
+ a.flowHeight === b.flowHeight &&
61
+ a.ariaLabelConfig === b.ariaLabelConfig;
62
+
63
+ const ARIA_LABEL_KEY = 'react-flow__minimap-desc';
64
+ function MiniMapComponent<NodeType extends Node = Node>({
65
+ style,
66
+ className,
67
+ nodeStrokeColor,
68
+ nodeColor,
69
+ nodeClassName = '',
70
+ nodeBorderRadius = 5,
71
+ nodeStrokeWidth,
72
+ /*
73
+ * We need to rename the prop to be `CapitalCase` so that JSX will render it as
74
+ * a component properly.
75
+ */
76
+ nodeComponent,
77
+ bgColor,
78
+ maskColor,
79
+ maskStrokeColor,
80
+ maskStrokeWidth,
81
+ position = 'bottom-right',
82
+ onClick,
83
+ onNodeClick,
84
+ pannable = false,
85
+ zoomable = false,
86
+ ariaLabel,
87
+ inversePan,
88
+ zoomStep = 1,
89
+ offsetScale = 5,
90
+ }: MiniMapProps<NodeType>) {
91
+ const store = useStoreApi<NodeType>();
92
+ const svg = useRef<SVGSVGElement | null>(null);
93
+ const {
94
+ boundingRect,
95
+ viewBB,
96
+ rfId,
97
+ panZoom,
98
+ translateExtent,
99
+ flowWidth,
100
+ flowHeight,
101
+ ariaLabelConfig,
102
+ } = useStore(selector, areEqual);
103
+ const elementWidth = style?.width as number ?? defaultWidth;
104
+ const elementHeight = style?.height as number ?? defaultHeight;
105
+ const scaledWidth = boundingRect.width / elementWidth;
106
+ const scaledHeight = boundingRect.height / elementHeight;
107
+ const viewScale = Math.max(scaledWidth, scaledHeight);
108
+ const viewWidth = viewScale * elementWidth;
109
+ const viewHeight = viewScale * elementHeight;
110
+ const offset = offsetScale * viewScale;
111
+ const x = boundingRect.x - (viewWidth - boundingRect.width) / 2 - offset;
112
+ const y = boundingRect.y - (viewHeight - boundingRect.height) / 2 - offset;
113
+ const width = viewWidth + offset * 2;
114
+ const height = viewHeight + offset * 2;
115
+ const labelledBy = `${ARIA_LABEL_KEY}-${rfId}`;
116
+ const viewScaleRef = useRef(0);
117
+ const minimapInstance = useRef<XYMinimapInstance | undefined>(undefined);
118
+
119
+ viewScaleRef.current = viewScale;
120
+
121
+ useEffect(() => {
122
+ if (svg.current && panZoom) {
123
+ minimapInstance.current = XYMinimap({
124
+ domNode: svg.current,
125
+ panZoom,
126
+ getTransform: () => store.getState().transform,
127
+ getViewScale: () => viewScaleRef.current,
128
+ });
129
+
130
+ return () => {
131
+ minimapInstance.current?.destroy();
132
+ };
133
+ }
134
+ }, [panZoom]);
135
+
136
+ useEffect(() => {
137
+ minimapInstance.current?.update({
138
+ translateExtent,
139
+ width: flowWidth,
140
+ height: flowHeight,
141
+ inversePan,
142
+ pannable,
143
+ zoomStep,
144
+ zoomable,
145
+ });
146
+ }, [pannable, zoomable, inversePan, zoomStep, translateExtent, flowWidth, flowHeight]);
147
+
148
+ const onSvgClick =
149
+ onClick
150
+ ? (event: MouseEvent) => {
151
+ const [x, y] = minimapInstance.current?.pointer(event) || [0, 0];
152
+ onClick(event, { x, y });
153
+ }
154
+ : undefined;
155
+
156
+ const onSvgNodeClick =
157
+ onNodeClick
158
+ ? useCallback((event: MouseEvent, nodeId: string) => {
159
+ const node: NodeType = store.getState().nodeLookup.get(nodeId)!.internals.userNode;
160
+ onNodeClick(event, node);
161
+ }, [])
162
+ : undefined;
163
+
164
+ const _ariaLabel = ariaLabel ?? ariaLabelConfig['minimap.ariaLabel'];
165
+
166
+ return <Panel
167
+ position={position}
168
+ style={{
169
+ ...style,
170
+ '--xy-minimap-background-color-props': typeof bgColor === 'string' ? bgColor : undefined,
171
+ '--xy-minimap-mask-background-color-props': typeof maskColor === 'string'
172
+ ? maskColor
173
+ : undefined,
174
+ '--xy-minimap-mask-stroke-color-props': typeof maskStrokeColor === 'string'
175
+ ? maskStrokeColor
176
+ : undefined,
177
+ '--xy-minimap-mask-stroke-width-props': typeof maskStrokeWidth === 'number'
178
+ ? maskStrokeWidth * viewScale
179
+ : undefined,
180
+ '--xy-minimap-node-background-color-props': typeof nodeColor === 'string'
181
+ ? nodeColor
182
+ : undefined,
183
+ '--xy-minimap-node-stroke-color-props': typeof nodeStrokeColor === 'string'
184
+ ? nodeStrokeColor
185
+ : undefined,
186
+ '--xy-minimap-node-stroke-width-props': typeof nodeStrokeWidth === 'number'
187
+ ? nodeStrokeWidth
188
+ : undefined,
189
+ } as CSSProperties}
190
+ className={cc(['react-flow__minimap', className])}
191
+ data-testid="rf__minimap"
192
+ >
193
+ <svg
194
+ width={elementWidth}
195
+ height={elementHeight}
196
+ viewBox={`${x} ${y} ${width} ${height}`}
197
+ className="react-flow__minimap-svg"
198
+ role="img"
199
+ aria-labelledby={labelledBy}
200
+ ref={svg}
201
+ onClick={onSvgClick}
202
+ >
203
+ {_ariaLabel && <title id={labelledBy}>{_ariaLabel}</title>}
204
+
205
+ <MiniMapNodes
206
+ onClick={onSvgNodeClick}
207
+ nodeColor={nodeColor}
208
+ nodeStrokeColor={nodeStrokeColor}
209
+ nodeBorderRadius={nodeBorderRadius}
210
+ nodeClassName={nodeClassName}
211
+ nodeStrokeWidth={nodeStrokeWidth}
212
+ nodeComponent={nodeComponent}
213
+ />
214
+ <path
215
+ className="react-flow__minimap-mask"
216
+ d={`M${x - offset},${y - offset}h${width + offset * 2}v${height + offset * 2}h${
217
+ -width - offset * 2
218
+ }z
219
+ M${viewBB.x},${viewBB.y}h${viewBB.width}v${viewBB.height}h${-viewBB.width}z`}
220
+ fillRule="evenodd"
221
+ pointerEvents="none"
222
+ />
223
+ </svg>
224
+ </Panel>;
225
+ }
226
+
227
+ MiniMapComponent.displayName = 'MiniMap';
228
+
229
+ /**
230
+ * The `<MiniMap />` component can be used to render an overview of your flow. It
231
+ * renders each node as an SVG element and visualizes where the current viewport is
232
+ * in relation to the rest of the flow.
233
+ *
234
+ * @public
235
+ * @example
236
+ *
237
+ * ```jsx
238
+ *import { ReactFlow, MiniMap } from '@xyflow/react';
239
+ *
240
+ *export default function Flow() {
241
+ * return (
242
+ * <ReactFlow nodes={[...]} edges={[...]}>
243
+ * <MiniMap nodeStrokeWidth={3} />
244
+ * </ReactFlow>
245
+ * );
246
+ *}
247
+ *```
248
+ */
249
+ export const MiniMap = memo(MiniMapComponent) as typeof MiniMapComponent;
@@ -0,0 +1,43 @@
1
+ import { memo } from 'octane';
2
+ import cc from 'classcat';
3
+
4
+ import type { MiniMapNodeProps } from './types';
5
+
6
+ function MiniMapNodeComponent({
7
+ id,
8
+ x,
9
+ y,
10
+ width,
11
+ height,
12
+ style,
13
+ color,
14
+ strokeColor,
15
+ strokeWidth,
16
+ className,
17
+ borderRadius,
18
+ shapeRendering,
19
+ selected,
20
+ onClick,
21
+ }: MiniMapNodeProps) {
22
+ const { background, backgroundColor } = style || {};
23
+ const fill = (color || background || backgroundColor) as string;
24
+
25
+ return <rect
26
+ className={cc(['react-flow__minimap-node', { selected }, className])}
27
+ x={x}
28
+ y={y}
29
+ rx={borderRadius}
30
+ ry={borderRadius}
31
+ width={width}
32
+ height={height}
33
+ style={{
34
+ fill,
35
+ stroke: strokeColor,
36
+ strokeWidth,
37
+ }}
38
+ shapeRendering={shapeRendering}
39
+ onClick={onClick ? (event) => onClick(event, id) : undefined}
40
+ />;
41
+ }
42
+
43
+ export const MiniMapNode = memo(MiniMapNodeComponent);
@@ -0,0 +1,136 @@
1
+ /* eslint-disable @typescript-eslint/ban-ts-comment */
2
+ /* eslint-disable @typescript-eslint/no-explicit-any */
3
+ import { memo } from 'octane';
4
+ import type { ComponentType } from '../../react-shim.js';
5
+ import { getNodeDimensions, nodeHasDimensions } from '@xyflow/system';
6
+ import { shallow } from '@octanejs/zustand/shallow';
7
+
8
+ import { useStore } from '../../hooks/useStore';
9
+ import { MiniMapNode } from './MiniMapNode.tsrx';
10
+ import type { ReactFlowState, Node } from '../../types';
11
+ import type {
12
+ MiniMapNodes as MiniMapNodesProps,
13
+ GetMiniMapNodeAttribute,
14
+ MiniMapNodeProps,
15
+ } from './types';
16
+
17
+ declare const window: any;
18
+
19
+ const selectorNodeIds = (s: ReactFlowState) => s.nodes.map((node) => node.id);
20
+ const getAttrFunction = <NodeType extends Node,>(func: any): GetMiniMapNodeAttribute<NodeType> =>
21
+ func instanceof Function ? func : () => func;
22
+
23
+ function MiniMapNodes<NodeType extends Node>({
24
+ nodeStrokeColor,
25
+ nodeColor,
26
+ nodeClassName = '',
27
+ nodeBorderRadius = 5,
28
+ nodeStrokeWidth,
29
+ /*
30
+ * We need to rename the prop to be `CapitalCase` so that JSX will render it as
31
+ * a component properly.
32
+ */
33
+ nodeComponent: NodeComponent = MiniMapNode,
34
+ onClick,
35
+ }: MiniMapNodesProps<NodeType>) {
36
+ const nodeIds = useStore(selectorNodeIds, shallow);
37
+ const nodeColorFunc = getAttrFunction<NodeType>(nodeColor);
38
+ const nodeStrokeColorFunc = getAttrFunction<NodeType>(nodeStrokeColor);
39
+ const nodeClassNameFunc = getAttrFunction<NodeType>(nodeClassName);
40
+
41
+ const shapeRendering =
42
+ typeof window === 'undefined' || !!window.chrome ? 'crispEdges' : 'geometricPrecision';
43
+
44
+ return <>
45
+ {nodeIds.map(
46
+ (
47
+ nodeId,
48
+ ) => /*
49
+ * The split of responsibilities between MiniMapNodes and
50
+ * NodeComponentWrapper may appear weird. However, it’s designed to
51
+ * minimize the cost of updates when individual nodes change.
52
+ *
53
+ * For more details, see a similar commit in `NodeRenderer/index.tsx`.
54
+ */
55
+ <NodeComponentWrapper
56
+ key={nodeId}
57
+ id={nodeId}
58
+ nodeColorFunc={nodeColorFunc}
59
+ nodeStrokeColorFunc={nodeStrokeColorFunc}
60
+ nodeClassNameFunc={nodeClassNameFunc}
61
+ nodeBorderRadius={nodeBorderRadius}
62
+ nodeStrokeWidth={nodeStrokeWidth}
63
+ NodeComponent={NodeComponent}
64
+ onClick={onClick}
65
+ shapeRendering={shapeRendering}
66
+ />,
67
+ )}
68
+ </>;
69
+ }
70
+
71
+ function NodeComponentWrapperInner<NodeType extends Node>({
72
+ id,
73
+ nodeColorFunc,
74
+ nodeStrokeColorFunc,
75
+ nodeClassNameFunc,
76
+ nodeBorderRadius,
77
+ nodeStrokeWidth,
78
+ shapeRendering,
79
+ NodeComponent,
80
+ onClick,
81
+ }: {
82
+ id: string;
83
+ nodeColorFunc: GetMiniMapNodeAttribute<NodeType>;
84
+ nodeStrokeColorFunc: GetMiniMapNodeAttribute<NodeType>;
85
+ nodeClassNameFunc: GetMiniMapNodeAttribute<NodeType>;
86
+ nodeBorderRadius: number;
87
+ nodeStrokeWidth?: number;
88
+ NodeComponent: ComponentType<MiniMapNodeProps>;
89
+ onClick: MiniMapNodesProps['onClick'];
90
+ shapeRendering: string;
91
+ }) {
92
+ const { node, x, y, width, height } = useStore((s) => {
93
+ const node = s.nodeLookup.get(id);
94
+
95
+ if (!node) {
96
+ return { node: undefined, x: 0, y: 0, width: 0, height: 0 };
97
+ }
98
+
99
+ const userNode = node.internals.userNode as NodeType;
100
+ const { x, y } = node.internals.positionAbsolute;
101
+ const { width, height } = getNodeDimensions(userNode);
102
+
103
+ return {
104
+ node: userNode,
105
+ x,
106
+ y,
107
+ width,
108
+ height,
109
+ };
110
+ }, shallow);
111
+
112
+ if (!node || node.hidden || !nodeHasDimensions(node)) {
113
+ return null;
114
+ }
115
+
116
+ return <NodeComponent
117
+ x={x}
118
+ y={y}
119
+ width={width}
120
+ height={height}
121
+ style={node.style}
122
+ selected={!!node.selected}
123
+ className={nodeClassNameFunc(node)}
124
+ color={nodeColorFunc(node)}
125
+ borderRadius={nodeBorderRadius}
126
+ strokeColor={nodeStrokeColorFunc(node)}
127
+ strokeWidth={nodeStrokeWidth}
128
+ shapeRendering={shapeRendering}
129
+ onClick={onClick}
130
+ id={node.id}
131
+ />;
132
+ }
133
+
134
+ const NodeComponentWrapper = memo(NodeComponentWrapperInner) as typeof NodeComponentWrapperInner;
135
+
136
+ export default memo(MiniMapNodes) as typeof MiniMapNodes;
@@ -0,0 +1,3 @@
1
+ export { MiniMap } from './MiniMap.tsrx';
2
+ export { MiniMapNode } from './MiniMapNode.tsrx';
3
+ export * from './types';
@@ -0,0 +1,139 @@
1
+ /* eslint-disable @typescript-eslint/no-explicit-any */
2
+ import type { ComponentType, CSSProperties, HTMLAttributes, MouseEvent } from '../../react-shim.js';
3
+ import type { PanelPosition, XYPosition } from '@xyflow/system';
4
+
5
+ import type { Node } from '../../types';
6
+
7
+ export type GetMiniMapNodeAttribute<NodeType extends Node = Node> = (node: NodeType) => string;
8
+
9
+ /**
10
+ * @expand
11
+ */
12
+ export type MiniMapProps<NodeType extends Node = Node> = Omit<
13
+ HTMLAttributes<SVGSVGElement>,
14
+ 'onClick'
15
+ > & {
16
+ /**
17
+ * Color of nodes on minimap.
18
+ * @default "#e2e2e2"
19
+ */
20
+ nodeColor?: string | GetMiniMapNodeAttribute<NodeType>;
21
+ /**
22
+ * Stroke color of nodes on minimap.
23
+ * @default "transparent"
24
+ */
25
+ nodeStrokeColor?: string | GetMiniMapNodeAttribute<NodeType>;
26
+ /**
27
+ * Class name applied to nodes on minimap.
28
+ * @default ""
29
+ */
30
+ nodeClassName?: string | GetMiniMapNodeAttribute<NodeType>;
31
+ /**
32
+ * Border radius of nodes on minimap.
33
+ * @default 5
34
+ */
35
+ nodeBorderRadius?: number;
36
+ /**
37
+ * Stroke width of nodes on minimap.
38
+ * @default 2
39
+ */
40
+ nodeStrokeWidth?: number;
41
+ /**
42
+ * A custom component to render the nodes in the minimap. This component must render an SVG
43
+ * element!
44
+ */
45
+ nodeComponent?: ComponentType<MiniMapNodeProps>;
46
+ /** Background color of minimap. */
47
+ bgColor?: string;
48
+ /**
49
+ * The color of the mask that covers the portion of the minimap not currently visible in the
50
+ * viewport.
51
+ * @default "rgba(240, 240, 240, 0.6)"
52
+ */
53
+ maskColor?: string;
54
+ /**
55
+ * Stroke color of mask representing viewport.
56
+ * @default transparent
57
+ */
58
+ maskStrokeColor?: string;
59
+ /**
60
+ * Stroke width of mask representing viewport.
61
+ * @default 1
62
+ */
63
+ maskStrokeWidth?: number;
64
+ /**
65
+ * Position of minimap on pane.
66
+ * @default PanelPosition.BottomRight
67
+ * @example PanelPosition.TopLeft, PanelPosition.TopRight,
68
+ * PanelPosition.BottomLeft, PanelPosition.BottomRight
69
+ */
70
+ position?: PanelPosition;
71
+ /** Callback called when minimap is clicked. */
72
+ onClick?: (event: MouseEvent, position: XYPosition) => void;
73
+ /** Callback called when node on minimap is clicked. */
74
+ onNodeClick?: (event: MouseEvent, node: NodeType) => void;
75
+ /**
76
+ * Determines whether you can pan the viewport by dragging inside the minimap.
77
+ * @default false
78
+ */
79
+ pannable?: boolean;
80
+ /**
81
+ * Determines whether you can zoom the viewport by scrolling inside the minimap.
82
+ * @default false
83
+ */
84
+ zoomable?: boolean;
85
+ /**
86
+ * There is no text inside the minimap for a screen reader to use as an accessible name, so it's
87
+ * important we provide one to make the minimap accessible. The default is sufficient, but you may
88
+ * want to replace it with something more relevant to your app or product.
89
+ * @default "Mini Map"
90
+ */
91
+ ariaLabel?: string | null;
92
+ /** Invert direction when panning the minimap viewport. */
93
+ inversePan?: boolean;
94
+ /**
95
+ * Step size for zooming in/out on minimap.
96
+ * @default 10
97
+ */
98
+ zoomStep?: number;
99
+ /**
100
+ * Offset the viewport on the minimap, acts like a padding.
101
+ * @default 5
102
+ */
103
+ offsetScale?: number;
104
+ };
105
+
106
+ export type MiniMapNodes<NodeType extends Node = Node> = Pick<
107
+ MiniMapProps<NodeType>,
108
+ | 'nodeColor'
109
+ | 'nodeStrokeColor'
110
+ | 'nodeClassName'
111
+ | 'nodeBorderRadius'
112
+ | 'nodeStrokeWidth'
113
+ | 'nodeComponent'
114
+ > & {
115
+ onClick?: (event: MouseEvent, nodeId: string) => void;
116
+ };
117
+
118
+ /**
119
+ * The props that are passed to the MiniMapNode component
120
+ *
121
+ * @public
122
+ * @expand
123
+ */
124
+ export type MiniMapNodeProps = {
125
+ id: string;
126
+ x: number;
127
+ y: number;
128
+ width: number;
129
+ height: number;
130
+ borderRadius: number;
131
+ className: string;
132
+ color?: string;
133
+ shapeRendering: string;
134
+ strokeColor?: string;
135
+ strokeWidth?: number;
136
+ style?: CSSProperties;
137
+ selected: boolean;
138
+ onClick?: (event: MouseEvent, id: string) => void;
139
+ };