react-zoom-pan-pinch 4.2.0 → 4.3.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/README.md +2 -2
- package/dist/index.cjs.js +438 -165
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.d.ts +32 -1
- package/dist/index.esm.js +439 -166
- package/dist/index.esm.js.map +1 -1
- package/package.json +1 -1
- package/src/components/mini-map/mini-map.tsx +386 -171
- package/src/core/bounds/bounds.utils.ts +88 -0
- package/src/core/instance.core.ts +12 -9
- package/src/core/pan/panning.logic.ts +7 -1
- package/src/core/pan/panning.utils.ts +76 -24
- package/src/core/pan/velocity.logic.ts +10 -3
- package/src/hooks/initial-layout.utils.ts +21 -0
- package/src/hooks/use-transform-component.tsx +22 -4
- package/src/hooks/use-transform-effect.tsx +20 -1
package/package.json
CHANGED
|
@@ -1,276 +1,491 @@
|
|
|
1
1
|
/* eslint-disable react/require-default-props */
|
|
2
|
-
import React, {
|
|
3
|
-
useCallback,
|
|
4
|
-
useEffect,
|
|
5
|
-
useMemo,
|
|
6
|
-
useRef,
|
|
7
|
-
useState,
|
|
8
|
-
} from "react";
|
|
2
|
+
import React, { useCallback, useEffect, useRef, useState } from "react";
|
|
9
3
|
|
|
10
4
|
import {
|
|
11
5
|
useTransformContext,
|
|
12
6
|
useTransformEffect,
|
|
13
7
|
useTransformInit,
|
|
14
8
|
} from "hooks";
|
|
9
|
+
import { PositionType } from "models";
|
|
15
10
|
import { useResize } from "./use-resize.hook";
|
|
16
|
-
import {
|
|
11
|
+
import {
|
|
12
|
+
getMouseBoundedPosition,
|
|
13
|
+
handleCalculateBounds,
|
|
14
|
+
} from "core/bounds/bounds.utils";
|
|
15
|
+
import { handleCancelAnimation } from "core/animations/animations.utils";
|
|
16
|
+
import { handleCalculateButtonZoom } from "core/handlers/handlers.utils";
|
|
17
|
+
import { handleZoomToPoint } from "core/zoom/zoom.logic";
|
|
18
|
+
import { roundNumber } from "utils";
|
|
17
19
|
|
|
18
20
|
export type MiniMapProps = {
|
|
19
21
|
children: React.ReactNode;
|
|
22
|
+
/** Width of the mini map box in px. */
|
|
20
23
|
width?: number;
|
|
24
|
+
/** Height of the mini map box in px. */
|
|
21
25
|
height?: number;
|
|
26
|
+
/** Border colour of the viewport indicator. */
|
|
22
27
|
borderColor?: string;
|
|
28
|
+
/**
|
|
29
|
+
* Colour of the overlay that dims everything outside the viewport
|
|
30
|
+
* (`.rzpp-minimap-mask`).
|
|
31
|
+
*/
|
|
32
|
+
maskColor?: string;
|
|
23
33
|
/** Merged onto the viewport indicator (`.rzpp-minimap-preview`). */
|
|
24
34
|
previewStyle?: React.CSSProperties;
|
|
25
35
|
/** Extra class name(s) appended to `.rzpp-minimap-wrapper`. */
|
|
26
36
|
wrapperClassName?: string;
|
|
27
37
|
/** Extra class name(s) appended to `.rzpp-minimap-preview`. */
|
|
28
38
|
previewClassName?: string;
|
|
39
|
+
/**
|
|
40
|
+
* Dragging anywhere on the mini map grabs the viewport indicator and moves
|
|
41
|
+
* it with the pointer (mouse and touch).
|
|
42
|
+
*/
|
|
29
43
|
panning?: boolean;
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
44
|
+
/**
|
|
45
|
+
* Flips the drag direction: instead of grabbing the viewport indicator the
|
|
46
|
+
* drag moves the content, like panning the main view.
|
|
47
|
+
*/
|
|
48
|
+
inversePan?: boolean;
|
|
49
|
+
/** Wheel over the mini map zooms the main view around its centre. */
|
|
50
|
+
zoomable?: boolean;
|
|
51
|
+
/** Scale increment applied per wheel notch when `zoomable` is on. */
|
|
52
|
+
zoomStep?: number;
|
|
53
|
+
/**
|
|
54
|
+
* Padding around the mapped area, in mini map px at the current map scale
|
|
55
|
+
* (same meaning as React Flow's `offsetScale`).
|
|
56
|
+
*/
|
|
57
|
+
offsetScale?: number;
|
|
58
|
+
/**
|
|
59
|
+
* Fired for a click (or tap) that did not turn into a drag. `position` is
|
|
60
|
+
* the clicked point in content coordinates, ready for `zoomToPoint`.
|
|
61
|
+
*/
|
|
62
|
+
onClick?: (event: MouseEvent | TouchEvent, position: PositionType) => void;
|
|
63
|
+
} & Omit<
|
|
64
|
+
React.DetailedHTMLProps<React.HTMLAttributes<HTMLDivElement>, HTMLDivElement>,
|
|
65
|
+
"onClick"
|
|
33
66
|
>;
|
|
34
67
|
|
|
68
|
+
type MiniMapLayout = {
|
|
69
|
+
/** Mini map px per content unit. */
|
|
70
|
+
mapScale: number;
|
|
71
|
+
/** Content coordinates of the mini map's top-left corner. */
|
|
72
|
+
originX: number;
|
|
73
|
+
originY: number;
|
|
74
|
+
contentWidth: number;
|
|
75
|
+
contentHeight: number;
|
|
76
|
+
/** Viewport indicator in mini map px. */
|
|
77
|
+
previewX: number;
|
|
78
|
+
previewY: number;
|
|
79
|
+
previewWidth: number;
|
|
80
|
+
previewHeight: number;
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
type DragState = {
|
|
84
|
+
x: number;
|
|
85
|
+
y: number;
|
|
86
|
+
moved: boolean;
|
|
87
|
+
};
|
|
88
|
+
|
|
35
89
|
const previewStyles = {
|
|
36
90
|
position: "absolute",
|
|
37
|
-
zIndex:
|
|
91
|
+
zIndex: 3,
|
|
38
92
|
top: "0px",
|
|
39
93
|
left: "0px",
|
|
40
94
|
boxSizing: "border-box",
|
|
41
|
-
border: "
|
|
95
|
+
border: "2px solid red",
|
|
42
96
|
transformOrigin: "0% 0%",
|
|
43
|
-
boxShadow: "rgba(0,0,0,0.2) 0 0 0 10000000px",
|
|
44
97
|
pointerEvents: "none",
|
|
45
98
|
} as const;
|
|
46
99
|
|
|
100
|
+
const wrapperStyles = {
|
|
101
|
+
position: "absolute",
|
|
102
|
+
top: "0px",
|
|
103
|
+
left: "0px",
|
|
104
|
+
transformOrigin: "0% 0%",
|
|
105
|
+
boxSizing: "border-box",
|
|
106
|
+
zIndex: 1,
|
|
107
|
+
pointerEvents: "none",
|
|
108
|
+
} as const;
|
|
109
|
+
|
|
110
|
+
const maskStyles = {
|
|
111
|
+
position: "absolute",
|
|
112
|
+
top: "0px",
|
|
113
|
+
left: "0px",
|
|
114
|
+
zIndex: 2,
|
|
115
|
+
pointerEvents: "none",
|
|
116
|
+
display: "block",
|
|
117
|
+
} as const;
|
|
118
|
+
|
|
119
|
+
const getTouchPoint = (event: TouchEvent): PositionType | null => {
|
|
120
|
+
const touch = event.touches[0] ?? event.changedTouches[0];
|
|
121
|
+
if (!touch) return null;
|
|
122
|
+
return { x: touch.clientX, y: touch.clientY };
|
|
123
|
+
};
|
|
124
|
+
|
|
125
|
+
const isInside = (element: HTMLElement | null, target: EventTarget | null) =>
|
|
126
|
+
!!element &&
|
|
127
|
+
!!target &&
|
|
128
|
+
(target === element || element.contains(target as Node));
|
|
129
|
+
|
|
47
130
|
export const MiniMap: React.FC<MiniMapProps> = ({
|
|
48
131
|
width = 200,
|
|
49
132
|
height = 200,
|
|
50
133
|
borderColor = "red",
|
|
134
|
+
maskColor = "rgba(0, 0, 0, 0.2)",
|
|
51
135
|
previewStyle,
|
|
52
136
|
wrapperClassName,
|
|
53
137
|
previewClassName,
|
|
54
138
|
children,
|
|
55
139
|
panning = true,
|
|
140
|
+
inversePan = false,
|
|
141
|
+
zoomable = false,
|
|
142
|
+
zoomStep = 0.2,
|
|
143
|
+
offsetScale = 5,
|
|
144
|
+
onClick,
|
|
56
145
|
...rest
|
|
57
146
|
}) => {
|
|
58
147
|
const [initialized, setInitialized] = useState(false);
|
|
59
148
|
const instance = useTransformContext();
|
|
60
|
-
const [isDown, setIsDown] = useState(false);
|
|
61
149
|
|
|
62
150
|
const mainRef = useRef<HTMLDivElement | null>(null);
|
|
63
151
|
const wrapperRef = useRef<HTMLDivElement | null>(null);
|
|
64
152
|
const previewRef = useRef<HTMLDivElement | null>(null);
|
|
153
|
+
const maskPathRef = useRef<SVGPathElement | null>(null);
|
|
154
|
+
const dragRef = useRef<DragState | null>(null);
|
|
155
|
+
const layoutRef = useRef<MiniMapLayout | null>(null);
|
|
156
|
+
|
|
157
|
+
// Latest props for the document listeners, which are registered once.
|
|
158
|
+
const optionsRef = useRef({ panning, inversePan, zoomStep, onClick });
|
|
159
|
+
optionsRef.current = { panning, inversePan, zoomStep, onClick };
|
|
65
160
|
|
|
66
161
|
const getViewportSize = useCallback(() => {
|
|
67
162
|
if (instance.wrapperComponent) {
|
|
68
163
|
const rect = instance.wrapperComponent.getBoundingClientRect();
|
|
69
|
-
|
|
70
|
-
return {
|
|
71
|
-
width: rect.width,
|
|
72
|
-
height: rect.height,
|
|
73
|
-
};
|
|
164
|
+
return { width: rect.width, height: rect.height };
|
|
74
165
|
}
|
|
75
|
-
return {
|
|
76
|
-
|
|
77
|
-
height: 0,
|
|
78
|
-
};
|
|
79
|
-
}, [instance.wrapperComponent]);
|
|
166
|
+
return { width: 0, height: 0 };
|
|
167
|
+
}, [instance]);
|
|
80
168
|
|
|
81
169
|
const getContentSize = useCallback(() => {
|
|
82
170
|
if (instance.contentComponent) {
|
|
83
171
|
const rect = instance.contentComponent.getBoundingClientRect();
|
|
172
|
+
const { scale } = instance.state;
|
|
173
|
+
return { width: rect.width / scale, height: rect.height / scale };
|
|
174
|
+
}
|
|
175
|
+
return { width: 0, height: 0 };
|
|
176
|
+
}, [instance]);
|
|
84
177
|
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
178
|
+
/**
|
|
179
|
+
* Like React Flow, the map shows the union of the content and the current
|
|
180
|
+
* viewport (plus `offsetScale` padding) fitted into the box, so the
|
|
181
|
+
* viewport indicator never leaves the mini map even when the view is
|
|
182
|
+
* panned or zoomed past the content.
|
|
183
|
+
*/
|
|
184
|
+
const computeLayout = useCallback((): MiniMapLayout | null => {
|
|
185
|
+
const content = getContentSize();
|
|
186
|
+
const viewport = getViewportSize();
|
|
187
|
+
const { scale, positionX, positionY } = instance.state;
|
|
188
|
+
|
|
189
|
+
if (!content.width || !content.height || !width || !height || !scale) {
|
|
190
|
+
return null;
|
|
89
191
|
}
|
|
192
|
+
|
|
193
|
+
const viewX = -positionX / scale;
|
|
194
|
+
const viewY = -positionY / scale;
|
|
195
|
+
const viewWidth = viewport.width / scale;
|
|
196
|
+
const viewHeight = viewport.height / scale;
|
|
197
|
+
|
|
198
|
+
const minX = Math.min(0, viewX);
|
|
199
|
+
const minY = Math.min(0, viewY);
|
|
200
|
+
const maxX = Math.max(content.width, viewX + viewWidth);
|
|
201
|
+
const maxY = Math.max(content.height, viewY + viewHeight);
|
|
202
|
+
const boundsWidth = maxX - minX;
|
|
203
|
+
const boundsHeight = maxY - minY;
|
|
204
|
+
|
|
205
|
+
const viewScale = Math.max(boundsWidth / width, boundsHeight / height);
|
|
206
|
+
const fitWidth = viewScale * width;
|
|
207
|
+
const fitHeight = viewScale * height;
|
|
208
|
+
const offset = offsetScale * viewScale;
|
|
209
|
+
|
|
210
|
+
const originX = minX - (fitWidth - boundsWidth) / 2 - offset;
|
|
211
|
+
const originY = minY - (fitHeight - boundsHeight) / 2 - offset;
|
|
212
|
+
const mapScale = width / (fitWidth + offset * 2);
|
|
213
|
+
|
|
90
214
|
return {
|
|
91
|
-
|
|
92
|
-
|
|
215
|
+
mapScale,
|
|
216
|
+
originX,
|
|
217
|
+
originY,
|
|
218
|
+
contentWidth: content.width,
|
|
219
|
+
contentHeight: content.height,
|
|
220
|
+
previewX: (viewX - originX) * mapScale,
|
|
221
|
+
previewY: (viewY - originY) * mapScale,
|
|
222
|
+
previewWidth: viewWidth * mapScale,
|
|
223
|
+
previewHeight: viewHeight * mapScale,
|
|
93
224
|
};
|
|
94
|
-
}, [instance
|
|
95
|
-
|
|
96
|
-
const computeMiniMapScale = useCallback(() => {
|
|
97
|
-
const contentSize = getContentSize();
|
|
98
|
-
const scaleX = width / contentSize.width;
|
|
99
|
-
const scaleY = height / contentSize.height;
|
|
100
|
-
const scale = scaleY > scaleX ? scaleX : scaleY;
|
|
101
|
-
|
|
102
|
-
return scale;
|
|
103
|
-
}, [getContentSize, height, width]);
|
|
104
|
-
|
|
105
|
-
const computeMiniMapSize = () => {
|
|
106
|
-
const contentSize = getContentSize();
|
|
107
|
-
const scaleX = width / contentSize.width;
|
|
108
|
-
const scaleY = height / contentSize.height;
|
|
109
|
-
if (scaleY > scaleX) {
|
|
110
|
-
return { width, height: contentSize.height * scaleX };
|
|
111
|
-
}
|
|
112
|
-
return { width: contentSize.width * scaleY, height };
|
|
113
|
-
};
|
|
225
|
+
}, [getContentSize, getViewportSize, height, instance, offsetScale, width]);
|
|
114
226
|
|
|
115
|
-
const
|
|
116
|
-
const
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
transformOrigin: "0% 0%",
|
|
120
|
-
position: "absolute",
|
|
121
|
-
boxSizing: "border-box",
|
|
122
|
-
zIndex: 1,
|
|
123
|
-
// overflow: "hidden",
|
|
124
|
-
} as const;
|
|
227
|
+
const transformMiniMap = useCallback(() => {
|
|
228
|
+
const layout = computeLayout();
|
|
229
|
+
layoutRef.current = layout;
|
|
230
|
+
if (!layout) return;
|
|
125
231
|
|
|
126
|
-
|
|
127
|
-
const el = wrapperRef.current.style;
|
|
128
|
-
el.transform = style.transform;
|
|
129
|
-
el.transformOrigin = style.transformOrigin;
|
|
130
|
-
el.position = style.position;
|
|
131
|
-
el.boxSizing = style.boxSizing;
|
|
132
|
-
el.zIndex = String(style.zIndex);
|
|
133
|
-
}
|
|
134
|
-
};
|
|
232
|
+
const px = (value: number) => `${roundNumber(value, 2)}px`;
|
|
135
233
|
|
|
136
|
-
const transformMiniMap = () => {
|
|
137
|
-
computeMiniMapStyle();
|
|
138
|
-
const miniSize = computeMiniMapSize();
|
|
139
|
-
const wrapSize = getContentSize();
|
|
140
234
|
if (wrapperRef.current) {
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
235
|
+
const { style } = wrapperRef.current;
|
|
236
|
+
style.width = px(layout.contentWidth);
|
|
237
|
+
style.height = px(layout.contentHeight);
|
|
238
|
+
style.transform = `translate(${px(-layout.originX * layout.mapScale)}, ${px(
|
|
239
|
+
-layout.originY * layout.mapScale,
|
|
240
|
+
)}) scale(${roundNumber(layout.mapScale, 4)})`;
|
|
147
241
|
}
|
|
148
|
-
if (previewRef.current) {
|
|
149
|
-
const size = getViewportSize();
|
|
150
|
-
const scale = computeMiniMapScale();
|
|
151
|
-
const previewScale = scale * (1 / instance.state.scale);
|
|
152
|
-
const transform = instance.handleTransformStyles(
|
|
153
|
-
-instance.state.positionX * previewScale,
|
|
154
|
-
-instance.state.positionY * previewScale,
|
|
155
|
-
1,
|
|
156
|
-
);
|
|
157
242
|
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
243
|
+
if (previewRef.current) {
|
|
244
|
+
const { style } = previewRef.current;
|
|
245
|
+
style.transform = `translate(${px(layout.previewX)}, ${px(layout.previewY)})`;
|
|
246
|
+
style.width = px(layout.previewWidth);
|
|
247
|
+
style.height = px(layout.previewHeight);
|
|
161
248
|
}
|
|
162
|
-
};
|
|
163
249
|
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
250
|
+
if (maskPathRef.current) {
|
|
251
|
+
const x = roundNumber(layout.previewX, 2);
|
|
252
|
+
const y = roundNumber(layout.previewY, 2);
|
|
253
|
+
const w = roundNumber(layout.previewWidth, 2);
|
|
254
|
+
const h = roundNumber(layout.previewHeight, 2);
|
|
255
|
+
maskPathRef.current.setAttribute(
|
|
256
|
+
"d",
|
|
257
|
+
`M0 0H${width}V${height}H0Z M${x} ${y}h${w}v${h}h${-w}Z`,
|
|
258
|
+
);
|
|
259
|
+
}
|
|
260
|
+
}, [computeLayout, height, width]);
|
|
167
261
|
|
|
168
|
-
useTransformEffect(
|
|
169
|
-
transformMiniMap();
|
|
170
|
-
});
|
|
262
|
+
useTransformEffect(transformMiniMap);
|
|
171
263
|
|
|
172
264
|
useTransformInit(() => {
|
|
173
|
-
|
|
265
|
+
transformMiniMap();
|
|
174
266
|
setInitialized(true);
|
|
175
267
|
});
|
|
176
268
|
|
|
177
|
-
useResize(instance.contentComponent,
|
|
269
|
+
useResize(instance.contentComponent, transformMiniMap, [initialized]);
|
|
178
270
|
|
|
179
271
|
useEffect(() => {
|
|
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
|
-
|
|
272
|
+
transformMiniMap();
|
|
273
|
+
}, [transformMiniMap]);
|
|
274
|
+
|
|
275
|
+
useEffect(() => {
|
|
276
|
+
const element = mainRef.current;
|
|
277
|
+
if (!element) return;
|
|
278
|
+
|
|
279
|
+
const getMapScale = () => {
|
|
280
|
+
const layout = layoutRef.current ?? computeLayout();
|
|
281
|
+
return layout?.mapScale ?? 0;
|
|
282
|
+
};
|
|
283
|
+
|
|
284
|
+
const start = (point: PositionType, target: EventTarget | null) => {
|
|
285
|
+
if (!optionsRef.current.panning) return;
|
|
286
|
+
if (!isInside(element, target)) return;
|
|
287
|
+
if (!instance.wrapperComponent || !instance.contentComponent) return;
|
|
288
|
+
|
|
289
|
+
handleCancelAnimation(instance);
|
|
290
|
+
dragRef.current = { x: point.x, y: point.y, moved: false };
|
|
291
|
+
element.style.cursor = "grabbing";
|
|
292
|
+
};
|
|
293
|
+
|
|
294
|
+
const move = (point: PositionType) => {
|
|
295
|
+
const drag = dragRef.current;
|
|
296
|
+
if (!drag) return;
|
|
297
|
+
|
|
298
|
+
const deltaX = point.x - drag.x;
|
|
299
|
+
const deltaY = point.y - drag.y;
|
|
300
|
+
drag.x = point.x;
|
|
301
|
+
drag.y = point.y;
|
|
302
|
+
if (!deltaX && !deltaY) return;
|
|
303
|
+
drag.moved = true;
|
|
304
|
+
|
|
305
|
+
const mapScale = getMapScale();
|
|
306
|
+
const { wrapperComponent, contentComponent, setup, state } = instance;
|
|
307
|
+
if (!mapScale || !wrapperComponent || !contentComponent) return;
|
|
308
|
+
|
|
309
|
+
// Dragging by one mini map px moves the viewport by 1 / mapScale
|
|
310
|
+
// content units, so the indicator follows the pointer 1:1.
|
|
311
|
+
const direction = optionsRef.current.inversePan ? 1 : -1;
|
|
312
|
+
const moveScale = (direction * state.scale) / mapScale;
|
|
313
|
+
const nextX = state.positionX + deltaX * moveScale;
|
|
314
|
+
const nextY = state.positionY + deltaY * moveScale;
|
|
315
|
+
|
|
316
|
+
const bounds = handleCalculateBounds(instance, state.scale);
|
|
317
|
+
const { x, y } = getMouseBoundedPosition(
|
|
318
|
+
nextX,
|
|
319
|
+
nextY,
|
|
320
|
+
bounds,
|
|
321
|
+
setup.limitToBounds,
|
|
322
|
+
0,
|
|
323
|
+
0,
|
|
324
|
+
wrapperComponent,
|
|
325
|
+
);
|
|
326
|
+
|
|
327
|
+
if (x === state.positionX && y === state.positionY) return;
|
|
328
|
+
instance.setState(state.scale, x, y);
|
|
329
|
+
};
|
|
330
|
+
|
|
331
|
+
const stop = (
|
|
332
|
+
event: MouseEvent | TouchEvent,
|
|
333
|
+
point: PositionType | null,
|
|
334
|
+
) => {
|
|
335
|
+
const drag = dragRef.current;
|
|
336
|
+
if (!drag) return;
|
|
337
|
+
dragRef.current = null;
|
|
338
|
+
element.style.cursor = optionsRef.current.panning ? "grab" : "";
|
|
339
|
+
|
|
340
|
+
const { onClick: handleClick } = optionsRef.current;
|
|
341
|
+
if (drag.moved || !handleClick || !point) return;
|
|
342
|
+
|
|
343
|
+
const layout = layoutRef.current ?? computeLayout();
|
|
344
|
+
if (!layout) return;
|
|
345
|
+
const rect = element.getBoundingClientRect();
|
|
346
|
+
handleClick(event, {
|
|
347
|
+
x: layout.originX + (point.x - rect.left) / layout.mapScale,
|
|
348
|
+
y: layout.originY + (point.y - rect.top) / layout.mapScale,
|
|
349
|
+
});
|
|
350
|
+
};
|
|
351
|
+
|
|
352
|
+
const onMouseDown = (event: MouseEvent) => {
|
|
353
|
+
if (event.button !== 0) return;
|
|
354
|
+
start({ x: event.clientX, y: event.clientY }, event.target);
|
|
355
|
+
};
|
|
356
|
+
const onMouseMove = (event: MouseEvent) => {
|
|
357
|
+
if (!dragRef.current) return;
|
|
358
|
+
// The mouse was released outside the window: end the drag.
|
|
359
|
+
if (event.buttons === 0) {
|
|
360
|
+
stop(event, null);
|
|
361
|
+
return;
|
|
223
362
|
}
|
|
363
|
+
move({ x: event.clientX, y: event.clientY });
|
|
364
|
+
};
|
|
365
|
+
const onMouseUp = (event: MouseEvent) => {
|
|
366
|
+
stop(event, { x: event.clientX, y: event.clientY });
|
|
224
367
|
};
|
|
225
368
|
|
|
226
|
-
const
|
|
227
|
-
if (
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
369
|
+
const onTouchStart = (event: TouchEvent) => {
|
|
370
|
+
if (event.touches.length !== 1) return;
|
|
371
|
+
const point = getTouchPoint(event);
|
|
372
|
+
if (point) start(point, event.target);
|
|
373
|
+
};
|
|
374
|
+
const onTouchMove = (event: TouchEvent) => {
|
|
375
|
+
if (!dragRef.current) return;
|
|
376
|
+
if (event.touches.length !== 1) {
|
|
377
|
+
stop(event, null);
|
|
378
|
+
return;
|
|
233
379
|
}
|
|
380
|
+
const point = getTouchPoint(event);
|
|
381
|
+
if (point) move(point);
|
|
234
382
|
};
|
|
235
|
-
const
|
|
236
|
-
|
|
383
|
+
const onTouchEnd = (event: TouchEvent) => {
|
|
384
|
+
stop(event, getTouchPoint(event));
|
|
237
385
|
};
|
|
386
|
+
|
|
238
387
|
// Listen on the mini map's own document so it keeps working inside
|
|
239
388
|
// portal windows and iframes.
|
|
240
|
-
const doc =
|
|
241
|
-
doc.addEventListener("mousedown",
|
|
242
|
-
doc.addEventListener("
|
|
243
|
-
doc.addEventListener("
|
|
389
|
+
const doc = element.ownerDocument;
|
|
390
|
+
doc.addEventListener("mousedown", onMouseDown);
|
|
391
|
+
doc.addEventListener("mousemove", onMouseMove);
|
|
392
|
+
doc.addEventListener("mouseup", onMouseUp);
|
|
393
|
+
doc.addEventListener("touchstart", onTouchStart);
|
|
394
|
+
doc.addEventListener("touchmove", onTouchMove);
|
|
395
|
+
doc.addEventListener("touchend", onTouchEnd);
|
|
396
|
+
doc.addEventListener("touchcancel", onTouchEnd);
|
|
244
397
|
return () => {
|
|
245
|
-
doc.removeEventListener("mousedown",
|
|
246
|
-
doc.removeEventListener("mousemove",
|
|
247
|
-
doc.removeEventListener("mouseup",
|
|
398
|
+
doc.removeEventListener("mousedown", onMouseDown);
|
|
399
|
+
doc.removeEventListener("mousemove", onMouseMove);
|
|
400
|
+
doc.removeEventListener("mouseup", onMouseUp);
|
|
401
|
+
doc.removeEventListener("touchstart", onTouchStart);
|
|
402
|
+
doc.removeEventListener("touchmove", onTouchMove);
|
|
403
|
+
doc.removeEventListener("touchend", onTouchEnd);
|
|
404
|
+
doc.removeEventListener("touchcancel", onTouchEnd);
|
|
405
|
+
dragRef.current = null;
|
|
248
406
|
};
|
|
249
|
-
}, [
|
|
407
|
+
}, [computeLayout, instance]);
|
|
250
408
|
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
409
|
+
useEffect(() => {
|
|
410
|
+
const element = mainRef.current;
|
|
411
|
+
if (!element || !zoomable) return;
|
|
412
|
+
|
|
413
|
+
const onWheel = (event: WheelEvent) => {
|
|
414
|
+
const { wrapperComponent, contentComponent, setup, state } = instance;
|
|
415
|
+
if (setup.disabled || !wrapperComponent || !contentComponent) return;
|
|
416
|
+
if (!event.deltaY) return;
|
|
417
|
+
|
|
418
|
+
if (event.cancelable) event.preventDefault();
|
|
419
|
+
event.stopPropagation();
|
|
420
|
+
|
|
421
|
+
const delta = event.deltaY < 0 ? 1 : -1;
|
|
422
|
+
const newScale = handleCalculateButtonZoom(
|
|
423
|
+
instance,
|
|
424
|
+
delta,
|
|
425
|
+
optionsRef.current.zoomStep,
|
|
426
|
+
);
|
|
427
|
+
if (newScale === state.scale) return;
|
|
428
|
+
|
|
429
|
+
// Zoom around the centre of the current viewport.
|
|
430
|
+
const centerX =
|
|
431
|
+
(wrapperComponent.offsetWidth / 2 - state.positionX) / state.scale;
|
|
432
|
+
const centerY =
|
|
433
|
+
(wrapperComponent.offsetHeight / 2 - state.positionY) / state.scale;
|
|
434
|
+
|
|
435
|
+
handleCancelAnimation(instance);
|
|
436
|
+
const target = handleZoomToPoint(instance, newScale, centerX, centerY);
|
|
437
|
+
if (!target) return;
|
|
438
|
+
instance.setState(target.scale, target.positionX, target.positionY);
|
|
439
|
+
};
|
|
440
|
+
|
|
441
|
+
element.addEventListener("wheel", onWheel, { passive: false });
|
|
442
|
+
return () => {
|
|
443
|
+
element.removeEventListener("wheel", onWheel);
|
|
444
|
+
};
|
|
445
|
+
}, [instance, zoomable]);
|
|
446
|
+
|
|
447
|
+
const containerStyle: React.CSSProperties = {
|
|
448
|
+
position: "relative",
|
|
449
|
+
zIndex: 2,
|
|
450
|
+
width: `${width}px`,
|
|
451
|
+
height: `${height}px`,
|
|
452
|
+
overflow: "hidden",
|
|
453
|
+
userSelect: "none",
|
|
454
|
+
WebkitUserSelect: "none",
|
|
455
|
+
touchAction: "none",
|
|
456
|
+
cursor: panning ? "grab" : undefined,
|
|
457
|
+
...rest.style,
|
|
458
|
+
};
|
|
259
459
|
|
|
260
460
|
return (
|
|
261
461
|
<div
|
|
262
462
|
{...rest}
|
|
263
463
|
ref={mainRef}
|
|
264
|
-
style={
|
|
464
|
+
style={containerStyle}
|
|
265
465
|
className={`rzpp-mini-map ${rest.className || ""}`.trim()}
|
|
266
466
|
>
|
|
267
467
|
<div
|
|
268
|
-
style={
|
|
468
|
+
style={wrapperStyles}
|
|
269
469
|
ref={wrapperRef}
|
|
270
470
|
className={`rzpp-minimap-wrapper ${wrapperClassName || ""}`.trim()}
|
|
271
471
|
>
|
|
272
472
|
{children}
|
|
273
473
|
</div>
|
|
474
|
+
<svg
|
|
475
|
+
className="rzpp-minimap-mask"
|
|
476
|
+
style={maskStyles}
|
|
477
|
+
width={width}
|
|
478
|
+
height={height}
|
|
479
|
+
viewBox={`0 0 ${width} ${height}`}
|
|
480
|
+
aria-hidden="true"
|
|
481
|
+
>
|
|
482
|
+
<path
|
|
483
|
+
ref={maskPathRef}
|
|
484
|
+
fill={maskColor}
|
|
485
|
+
fillRule="evenodd"
|
|
486
|
+
d={`M0 0H${width}V${height}H0Z`}
|
|
487
|
+
/>
|
|
488
|
+
</svg>
|
|
274
489
|
<div
|
|
275
490
|
className={`rzpp-minimap-preview ${previewClassName || ""}`.trim()}
|
|
276
491
|
ref={previewRef}
|