-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathscrollOnDragHook.ts
More file actions
286 lines (261 loc) · 10.5 KB
/
Copy pathscrollOnDragHook.ts
File metadata and controls
286 lines (261 loc) · 10.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
import React, { MouseEvent as ReactMouseEvent, useCallback } from "react";
import { OnLoadParams, useStoreState } from "react-flow-renderer";
import {
Edge,
Node,
OnConnectStartFunc,
OnConnectStartParams,
OnConnectStopFunc,
Transform,
} from "react-flow-renderer/dist/types";
import { ReactFlowExtendedScrollProps } from "../ReactFlow/ReactFlow";
import { ReactFlowV9ContainerProps } from "../ReactFlow/ReactFlowV9";
interface IProps extends ReactFlowExtendedScrollProps {
/** The original react-flow props. */
reactFlowProps: ReactFlowV9ContainerProps;
}
export interface ScrollStateShared {
// The current x position of the react-flow view
currentX: number;
// The current y position of the react-flow view
currentY: number;
// The current Zoom level
currentZoom: number;
// The current scroll function callback, when scrolling is active
scrollTaskId?: NodeJS.Timeout;
// If a warning of the react-flow instance with the given ID has not been found
loggedWarning: boolean;
// If the x-axis is currently being scrolled
scrollX: boolean;
// If the y-axis is currently being scrolled
scrollY: boolean;
// Only if this is true the canvas will scroll when moving the mouse past it
draggingOperationActive: boolean;
}
interface ScrollState extends ScrollStateShared {
// The react-flow instance
reactFlowInstance?: OnLoadParams;
}
type ReturnType = Pick<
ReactFlowV9ContainerProps,
| "onLoad"
| "onNodeDragStart"
| "onNodeDragStop"
| "onConnectStart"
| "onConnectStop"
| "onSelectionDragStart"
| "onSelectionDragStop"
| "onEdgeUpdateStart"
| "onEdgeUpdateEnd"
>;
/** Handles the scrolling of the react-flow canvas on all drag operations when the mouse pointer gets near or over the borders.
* The return value contains the wrapped react-flow callback functions that need to be handed over to the react-flow component. */
export const useReactFlowScrollOnDragV9 = ({ reactFlowProps, scrollOnDrag }: IProps): ReturnType => {
/** Tracks the zoom on drag to border functionality. */
const scrollState = React.useRef<ScrollState>({
reactFlowInstance: undefined,
currentX: 0,
currentY: 0,
currentZoom: 1,
loggedWarning: false,
scrollX: false,
scrollY: false,
draggingOperationActive: false,
});
const useStoreStateInternal = (): Transform => {
try {
return useStoreState((state) => state.transform);
} catch (ex) {
if (reactFlowProps.id && scrollOnDrag) {
// eslint-disable-next-line no-console
console.warn("Scroll on drag is not correctly working. Reason: " + ex);
}
return [0, 0, 1];
}
};
/** The current position and zoom factor of the view port. */
const [currentX, currentY, currentZoom] = useStoreStateInternal();
scrollState.current.currentX = currentX;
scrollState.current.currentY = currentY;
scrollState.current.currentZoom = currentZoom;
const originalOnLoad = reactFlowProps.onLoad;
const originalOnNodeDragStart = reactFlowProps.onNodeDragStart;
const originalOnNodeDragStop = reactFlowProps.onNodeDragStop;
const originalOnConnectStart = reactFlowProps.onConnectStart;
const originalOnConnectStop = reactFlowProps.onConnectStop;
const originalOnSelectionDragStart = reactFlowProps.onSelectionDragStart;
const originalOnSelectionDragStop = reactFlowProps.onSelectionDragStop;
const originalOnEdgeUpdateStart = reactFlowProps.onEdgeUpdateStart;
const originalOnEdgeUpdateEnd = reactFlowProps.onEdgeUpdateEnd;
const scrollInterval = scrollOnDrag?.scrollInterval;
const scrollStepSize = scrollOnDrag?.scrollStepSize;
const reactFlowInstanceId = reactFlowProps.id;
const clearIntervalIfExists = React.useCallback(() => {
if (scrollState.current.scrollTaskId) {
clearInterval(scrollState.current.scrollTaskId);
}
}, []);
const setScrolling = React.useCallback(
(active: boolean) => {
scrollState.current.draggingOperationActive = active;
if (!active) {
clearIntervalIfExists();
}
},
[clearIntervalIfExists],
);
// Handle scrolling if any operation is active e.g. connecting or dragging a node
React.useEffect(() => {
if (scrollInterval && scrollStepSize && reactFlowInstanceId) {
const handleScrolling = (event: MouseEvent) => {
const state = scrollState.current;
if (!state.draggingOperationActive) {
clearIntervalIfExists();
return;
}
// Check if mouse pointer is outside of the canvas
const canvasElement = document.getElementById(reactFlowInstanceId);
if (!canvasElement) {
if (!state.loggedWarning) {
// eslint-disable-next-line no-console
console.warn("No element found with ID " + reactFlowInstanceId);
state.loggedWarning = true;
}
return;
}
const boundingRect = canvasElement.getBoundingClientRect();
const xStepSize = boundingRect.width * scrollStepSize;
const yStepSize = boundingRect.height * scrollStepSize;
if (
boundingRect.top > event.clientY ||
boundingRect.bottom < event.clientY ||
boundingRect.left > event.clientX ||
boundingRect.right < event.clientX
) {
const scrollX: number =
boundingRect.left > event.clientX
? xStepSize
: boundingRect.right < event.clientX
? -xStepSize
: 0;
const scrollY: number =
boundingRect.top > event.clientY
? yStepSize
: boundingRect.bottom < event.clientY
? -yStepSize
: 0;
if (state.scrollY === (scrollY !== 0) && state.scrollX === (scrollX !== 0)) {
// Nothing has changed, do not change interval function
return;
}
clearIntervalIfExists();
state.scrollTaskId = setInterval(() => {
state.reactFlowInstance?.setTransform({
x: state.currentX + scrollX,
y: state.currentY + scrollY,
zoom: state.currentZoom,
});
}, scrollInterval);
} else {
clearIntervalIfExists();
}
};
const disableScrollingOnMouseUp = () => {
scrollState.current.draggingOperationActive = false;
clearIntervalIfExists();
};
document.addEventListener("mousemove", handleScrolling);
document.addEventListener("mouseup", disableScrollingOnMouseUp);
return () => {
document.removeEventListener("mousemove", handleScrolling);
document.removeEventListener("mouseup", disableScrollingOnMouseUp);
};
} else {
return undefined;
}
}, [scrollInterval, scrollStepSize, reactFlowInstanceId, clearIntervalIfExists]);
const onLoad = useCallback(
(rfi: OnLoadParams) => {
scrollState.current.reactFlowInstance = rfi;
originalOnLoad?.(rfi);
},
[originalOnLoad],
);
/** Wrap original callbacks to turn scrolling on and off. */
const onConnectStart: OnConnectStartFunc = React.useCallback(
(event: ReactMouseEvent, params: OnConnectStartParams) => {
setScrolling(true);
originalOnConnectStart?.(event, params);
},
[originalOnConnectStart, setScrolling],
);
const onConnectStop: OnConnectStopFunc = React.useCallback(
(event: MouseEvent) => {
setScrolling(false);
originalOnConnectStop?.(event);
},
[originalOnConnectStop, setScrolling],
);
const onNodeDragStart = React.useCallback(
(event: ReactMouseEvent, node: Node) => {
setScrolling(true);
originalOnNodeDragStart?.(event, node);
},
[originalOnNodeDragStart, setScrolling],
);
const onNodeDragStop = React.useCallback(
(event: ReactMouseEvent, node: Node) => {
setScrolling(false);
originalOnNodeDragStop?.(event, node);
},
[originalOnNodeDragStop, setScrolling],
);
const onSelectionDragStart = React.useCallback(
(event: ReactMouseEvent, nodes: Node[]) => {
setScrolling(true);
originalOnSelectionDragStart?.(event, nodes);
},
[originalOnSelectionDragStart, setScrolling],
);
const onSelectionDragStop = React.useCallback(
(event: ReactMouseEvent, nodes: Node[]) => {
setScrolling(false);
originalOnSelectionDragStop?.(event, nodes);
},
[originalOnSelectionDragStop, setScrolling],
);
const onEdgeUpdateStart = React.useCallback(
(event: ReactMouseEvent, edge: Edge) => {
setScrolling(true);
originalOnEdgeUpdateStart?.(event, edge);
},
[originalOnEdgeUpdateStart, setScrolling],
);
const onEdgeUpdateEnd = React.useCallback(
(event: MouseEvent, edge: Edge) => {
setScrolling(false);
originalOnEdgeUpdateEnd?.(event, edge);
},
[originalOnEdgeUpdateEnd, setScrolling],
);
if (!reactFlowProps.id || !scrollOnDrag) {
// No instance ID or config available, return empty object that will not overwrite any react-flow config parameters
return {};
} else {
return {
onLoad,
onNodeDragStart,
onNodeDragStop,
onConnectStart,
onConnectStop,
onSelectionDragStart,
onSelectionDragStop,
onEdgeUpdateStart,
onEdgeUpdateEnd,
};
}
};
/**
* @deprecated (v27) Currently it only supports ReactFlow v9. Better to `useReactFlowScrollOnDragV9` for now.
*/
export const useReactFlowScrollOnDrag = useReactFlowScrollOnDragV9;