react-zoom-pan-pinch 4.2.0 → 4.4.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 +770 -229
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.d.ts +85 -4
- package/dist/index.esm.js +771 -231
- package/dist/index.esm.js.map +1 -1
- package/dist/styles.css +40 -0
- package/package.json +1 -1
- package/src/components/mini-map/mini-map.tsx +386 -171
- package/src/components/transform-component/transform-component.tsx +25 -2
- package/src/constants/state.constants.ts +3 -0
- package/src/core/bounds/bounds.utils.ts +88 -0
- package/src/core/double-click/double-click.logic.ts +18 -9
- package/src/core/handlers/handlers.utils.ts +89 -2
- package/src/core/instance.core.ts +156 -31
- package/src/core/pan/panning.logic.ts +8 -1
- package/src/core/pan/panning.utils.ts +111 -24
- package/src/core/pan/velocity.logic.ts +10 -3
- package/src/core/wheel/wheel.logic.ts +19 -2
- package/src/core/wheel/wheel.utils.ts +44 -2
- 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/src/models/context.model.ts +25 -2
- package/src/typings.d.ts +5 -0
- package/src/utils/state.utils.ts +8 -2
- package/src/utils/styles.utils.ts +37 -0
|
@@ -3,10 +3,17 @@ import React, { useContext, useEffect, useRef } from "react";
|
|
|
3
3
|
|
|
4
4
|
import { Context } from "../transform-wrapper/transform-wrapper";
|
|
5
5
|
import { baseClasses } from "../../constants/state.constants";
|
|
6
|
-
import { getTransformStyles } from "../../utils/styles.utils";
|
|
6
|
+
import { getTransformStyles, injectStylesheet } from "../../utils/styles.utils";
|
|
7
7
|
import { useIsomorphicLayoutEffect } from "../../utils/effect.utils";
|
|
8
8
|
|
|
9
|
-
import
|
|
9
|
+
// Namespace import: only the library build (rollup-plugin-postcss with
|
|
10
|
+
// `inject: false`) exports `stylesheet`. A named import would be a hard
|
|
11
|
+
// error under bundlers that load the CSS module themselves (Vite in
|
|
12
|
+
// Storybook); a missing property is just undefined.
|
|
13
|
+
import * as cssModule from "./transform-component.module.css";
|
|
14
|
+
|
|
15
|
+
const styles = cssModule.default;
|
|
16
|
+
const { stylesheet } = cssModule;
|
|
10
17
|
|
|
11
18
|
type Props = {
|
|
12
19
|
children: React.ReactNode;
|
|
@@ -22,6 +29,16 @@ type Props = {
|
|
|
22
29
|
* `limitToBounds={false}` on TransformWrapper for full effect.
|
|
23
30
|
*/
|
|
24
31
|
infinite?: boolean;
|
|
32
|
+
/**
|
|
33
|
+
* Nonce for the `<style>` the component adds, for a Content Security
|
|
34
|
+
* Policy with `style-src 'nonce-…'`.
|
|
35
|
+
*/
|
|
36
|
+
nonce?: string;
|
|
37
|
+
/**
|
|
38
|
+
* Do not add the stylesheet; load `react-zoom-pan-pinch/dist/styles.css`
|
|
39
|
+
* yourself instead (a CSP that allows no inline `<style>`).
|
|
40
|
+
*/
|
|
41
|
+
disableStyleInjection?: boolean;
|
|
25
42
|
};
|
|
26
43
|
|
|
27
44
|
export const TransformComponent: React.FC<Props> = ({
|
|
@@ -33,6 +50,8 @@ export const TransformComponent: React.FC<Props> = ({
|
|
|
33
50
|
wrapperProps = {},
|
|
34
51
|
contentProps = {},
|
|
35
52
|
infinite = false,
|
|
53
|
+
nonce,
|
|
54
|
+
disableStyleInjection = false,
|
|
36
55
|
}: Props) => {
|
|
37
56
|
const instance = useContext(Context);
|
|
38
57
|
const { init, cleanupWindowEvents } = instance;
|
|
@@ -47,6 +66,10 @@ export const TransformComponent: React.FC<Props> = ({
|
|
|
47
66
|
useIsomorphicLayoutEffect(() => {
|
|
48
67
|
const wrapper = wrapperRef.current;
|
|
49
68
|
const content = contentRef.current;
|
|
69
|
+
// Before `init` measures anything: the sizes depend on these rules.
|
|
70
|
+
if (wrapper !== null && !disableStyleInjection) {
|
|
71
|
+
injectStylesheet(wrapper, stylesheet, nonce);
|
|
72
|
+
}
|
|
50
73
|
if (wrapper !== null && content !== null && init) {
|
|
51
74
|
init?.(wrapper, content);
|
|
52
75
|
}
|
|
@@ -37,6 +37,7 @@ export const initialSetup: LibrarySetup = {
|
|
|
37
37
|
trackPadPanning: {
|
|
38
38
|
disabled: true,
|
|
39
39
|
velocityDisabled: false,
|
|
40
|
+
speed: 1,
|
|
40
41
|
lockAxisX: false,
|
|
41
42
|
lockAxisY: false,
|
|
42
43
|
activationKeys: [],
|
|
@@ -50,6 +51,8 @@ export const initialSetup: LibrarySetup = {
|
|
|
50
51
|
allowLeftClickPan: true,
|
|
51
52
|
allowMiddleClickPan: true,
|
|
52
53
|
allowRightClickPan: true,
|
|
54
|
+
suppressClickAfterPan: true,
|
|
55
|
+
releaseAtBounds: false,
|
|
53
56
|
activationKeys: [],
|
|
54
57
|
excluded: [],
|
|
55
58
|
},
|
|
@@ -160,6 +160,61 @@ export function rubberbandIfOutOfBounds(
|
|
|
160
160
|
return position;
|
|
161
161
|
}
|
|
162
162
|
|
|
163
|
+
/**
|
|
164
|
+
* Furthest a grabbed position is traced back along the rubber band curve, as
|
|
165
|
+
* a fraction of the padding. The curve never reaches the padding itself, so
|
|
166
|
+
* a position at (or past) it has no pointer distance to map back to.
|
|
167
|
+
*/
|
|
168
|
+
const MAX_RUBBER_BAND_RATIO = 0.9;
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* Resistance of the rubber band, the constant UIScrollView uses: past a bound
|
|
172
|
+
* the content moves at 0.55× the pointer and slows down from there.
|
|
173
|
+
*/
|
|
174
|
+
export const RUBBER_BAND_CONSTANT = 0.55;
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* Where a pan that was dragged past a bound ends up: `(1 - 1 / (x * c / d + 1)) * d`
|
|
178
|
+
* with `d` = `padding`. It trails the pointer from the first pixel past the
|
|
179
|
+
* bound and approaches — never reaches — `padding` px past it.
|
|
180
|
+
*/
|
|
181
|
+
export function getRubberBandPosition(
|
|
182
|
+
position: number,
|
|
183
|
+
min: number,
|
|
184
|
+
max: number,
|
|
185
|
+
padding: number,
|
|
186
|
+
): number {
|
|
187
|
+
if (padding <= 0) return clamp(position, min, max);
|
|
188
|
+
if (position < min)
|
|
189
|
+
return min - rubberband(min - position, padding, RUBBER_BAND_CONSTANT);
|
|
190
|
+
if (position > max)
|
|
191
|
+
return max + rubberband(position - max, padding, RUBBER_BAND_CONSTANT);
|
|
192
|
+
return position;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* Inverse of `getRubberBandPosition`: the pointer position that puts the
|
|
197
|
+
* content at `position`. Lets a pan that starts inside the padding (grabbed
|
|
198
|
+
* mid return animation, trackpad deltas) continue from where the content is.
|
|
199
|
+
*/
|
|
200
|
+
export function getRubberBandSource(
|
|
201
|
+
position: number,
|
|
202
|
+
min: number,
|
|
203
|
+
max: number,
|
|
204
|
+
padding: number,
|
|
205
|
+
): number {
|
|
206
|
+
if (padding <= 0) return position;
|
|
207
|
+
|
|
208
|
+
const stretch = (offset: number) => {
|
|
209
|
+
const limited = Math.min(offset, padding * MAX_RUBBER_BAND_RATIO);
|
|
210
|
+
return (limited * padding) / (RUBBER_BAND_CONSTANT * (padding - limited));
|
|
211
|
+
};
|
|
212
|
+
|
|
213
|
+
if (position < min) return min - stretch(min - position);
|
|
214
|
+
if (position > max) return max + stretch(position - max);
|
|
215
|
+
return position;
|
|
216
|
+
}
|
|
217
|
+
|
|
163
218
|
/**
|
|
164
219
|
* Keeps value between given bounds, used for limiting view to given boundaries
|
|
165
220
|
* 1# eg. boundLimiter(2, 0, 3, true) => 2
|
|
@@ -224,3 +279,36 @@ export function getMouseBoundedPosition(
|
|
|
224
279
|
);
|
|
225
280
|
return { x, y };
|
|
226
281
|
}
|
|
282
|
+
|
|
283
|
+
/**
|
|
284
|
+
* Bounded position for a pan gesture: free inside the bounds and, inside the
|
|
285
|
+
* padding around them, rubber banded (`rubberBand`) or 1:1 up to its edge.
|
|
286
|
+
*/
|
|
287
|
+
export function getPanBoundedPosition(
|
|
288
|
+
positionX: number,
|
|
289
|
+
positionY: number,
|
|
290
|
+
bounds: BoundsType,
|
|
291
|
+
limitToBounds: boolean,
|
|
292
|
+
paddingX: number,
|
|
293
|
+
paddingY: number,
|
|
294
|
+
rubberBand: boolean,
|
|
295
|
+
): PositionType {
|
|
296
|
+
const { minPositionX, minPositionY, maxPositionX, maxPositionY } = bounds;
|
|
297
|
+
|
|
298
|
+
const limit = (
|
|
299
|
+
position: number,
|
|
300
|
+
min: number,
|
|
301
|
+
max: number,
|
|
302
|
+
padding: number,
|
|
303
|
+
) => {
|
|
304
|
+
if (limitToBounds && rubberBand && padding > 0) {
|
|
305
|
+
return roundNumber(getRubberBandPosition(position, min, max, padding), 2);
|
|
306
|
+
}
|
|
307
|
+
return boundLimiter(position, min - padding, max + padding, limitToBounds);
|
|
308
|
+
};
|
|
309
|
+
|
|
310
|
+
return {
|
|
311
|
+
x: limit(positionX, minPositionX, maxPositionX, paddingX),
|
|
312
|
+
y: limit(positionY, minPositionY, maxPositionY, paddingY),
|
|
313
|
+
};
|
|
314
|
+
}
|
|
@@ -11,6 +11,7 @@ import {
|
|
|
11
11
|
isExcludedNode,
|
|
12
12
|
} from "../../utils";
|
|
13
13
|
import {
|
|
14
|
+
getResetScale,
|
|
14
15
|
handleCalculateButtonZoom,
|
|
15
16
|
resetTransformations,
|
|
16
17
|
} from "../handlers/handlers.utils";
|
|
@@ -46,17 +47,14 @@ export const handleDoubleClickResetMode = (
|
|
|
46
47
|
handleDoubleClickStop(contextInstance, event);
|
|
47
48
|
};
|
|
48
49
|
|
|
49
|
-
function getDoubleClickScale(
|
|
50
|
-
mode: LibrarySetup["doubleClick"]["mode"],
|
|
51
|
-
scale: number,
|
|
52
|
-
) {
|
|
53
|
-
if (mode === "toggle") {
|
|
54
|
-
return scale === 1 ? 1 : -1;
|
|
55
|
-
}
|
|
56
|
-
|
|
50
|
+
function getDoubleClickScale(mode: LibrarySetup["doubleClick"]["mode"]) {
|
|
57
51
|
return mode === "zoomOut" ? -1 : 1;
|
|
58
52
|
}
|
|
59
53
|
|
|
54
|
+
// Scales closer than this count as "at the reset scale": animations and fits
|
|
55
|
+
// land on float values.
|
|
56
|
+
const RESET_SCALE_TOLERANCE = 1e-3;
|
|
57
|
+
|
|
60
58
|
export function handleDoubleClick(
|
|
61
59
|
contextInstance: ReactZoomPanPinchContext,
|
|
62
60
|
event: MouseEvent | TouchEvent,
|
|
@@ -76,9 +74,20 @@ export function handleDoubleClick(
|
|
|
76
74
|
return handleDoubleClickResetMode(contextInstance, event);
|
|
77
75
|
}
|
|
78
76
|
|
|
77
|
+
// `toggle` (#125): at the reset scale (`initialScale`, or the fitted one
|
|
78
|
+
// with `fitOnInit`) zoom in by `step`; anywhere else go back to the reset
|
|
79
|
+
// state. It used to compare against 1, so with another initial scale the
|
|
80
|
+
// first double click zoomed out, and the way back only stepped out.
|
|
81
|
+
if (mode === "toggle") {
|
|
82
|
+
const resetScale = getResetScale(contextInstance);
|
|
83
|
+
if (Math.abs(scale - resetScale) > RESET_SCALE_TOLERANCE) {
|
|
84
|
+
return handleDoubleClickResetMode(contextInstance, event);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
79
88
|
if (!contentComponent) return console.error("No ContentComponent found");
|
|
80
89
|
|
|
81
|
-
const delta = getDoubleClickScale(mode
|
|
90
|
+
const delta = getDoubleClickScale(mode);
|
|
82
91
|
|
|
83
92
|
const newScale = handleCalculateButtonZoom(contextInstance, delta, step);
|
|
84
93
|
|
|
@@ -210,6 +210,83 @@ export function calculateFitToView(
|
|
|
210
210
|
return getCenterPosition(scale, wrapperComponent, contentComponent);
|
|
211
211
|
}
|
|
212
212
|
|
|
213
|
+
// Share of the free space (wrapper minus scaled content) left before the
|
|
214
|
+
// content for each named initial position (#349).
|
|
215
|
+
const NAMED_POSITION_SHARE = {
|
|
216
|
+
left: 0,
|
|
217
|
+
top: 0,
|
|
218
|
+
center: 0.5,
|
|
219
|
+
right: 1,
|
|
220
|
+
bottom: 1,
|
|
221
|
+
} as const;
|
|
222
|
+
|
|
223
|
+
export function hasNamedInitialPosition(
|
|
224
|
+
contextInstance: ReactZoomPanPinchContext,
|
|
225
|
+
): boolean {
|
|
226
|
+
const { initialPositionX, initialPositionY } = contextInstance.props;
|
|
227
|
+
return (
|
|
228
|
+
typeof initialPositionX === "string" || typeof initialPositionY === "string"
|
|
229
|
+
);
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* Replaces each axis of `target` whose `initialPositionX/Y` is named
|
|
234
|
+
* (`"right"`, `"center"`, `"top"`…) with the position that puts the content
|
|
235
|
+
* there at `target.scale`. Numeric axes are left as they are.
|
|
236
|
+
*/
|
|
237
|
+
export function applyNamedInitialPosition(
|
|
238
|
+
contextInstance: ReactZoomPanPinchContext,
|
|
239
|
+
target: StateType,
|
|
240
|
+
): StateType {
|
|
241
|
+
const { wrapperComponent, contentComponent, props } = contextInstance;
|
|
242
|
+
const { initialPositionX, initialPositionY } = props;
|
|
243
|
+
if (!wrapperComponent || !contentComponent) return target;
|
|
244
|
+
|
|
245
|
+
const place = (
|
|
246
|
+
name: keyof typeof NAMED_POSITION_SHARE,
|
|
247
|
+
wrapperSize: number,
|
|
248
|
+
contentSize: number,
|
|
249
|
+
) => (wrapperSize - contentSize * target.scale) * NAMED_POSITION_SHARE[name];
|
|
250
|
+
|
|
251
|
+
return {
|
|
252
|
+
scale: target.scale,
|
|
253
|
+
positionX:
|
|
254
|
+
typeof initialPositionX === "string"
|
|
255
|
+
? place(
|
|
256
|
+
initialPositionX,
|
|
257
|
+
wrapperComponent.clientWidth,
|
|
258
|
+
contentComponent.offsetWidth,
|
|
259
|
+
)
|
|
260
|
+
: target.positionX,
|
|
261
|
+
positionY:
|
|
262
|
+
typeof initialPositionY === "string"
|
|
263
|
+
? place(
|
|
264
|
+
initialPositionY,
|
|
265
|
+
wrapperComponent.clientHeight,
|
|
266
|
+
contentComponent.offsetHeight,
|
|
267
|
+
)
|
|
268
|
+
: target.positionY,
|
|
269
|
+
};
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
/**
|
|
273
|
+
* The scale `resetTransform` returns to: the fitted one with `fitOnInit`,
|
|
274
|
+
* otherwise `initialScale` (clamped to min/max).
|
|
275
|
+
*/
|
|
276
|
+
export function getResetScale(
|
|
277
|
+
contextInstance: ReactZoomPanPinchContext,
|
|
278
|
+
): number {
|
|
279
|
+
const { fitOnInit } = contextInstance.setup;
|
|
280
|
+
if (fitOnInit) {
|
|
281
|
+
const fitted = calculateFitToView(
|
|
282
|
+
contextInstance,
|
|
283
|
+
fitOnInit === "cover" ? "cover" : "contain",
|
|
284
|
+
);
|
|
285
|
+
if (fitted) return fitted.scale;
|
|
286
|
+
}
|
|
287
|
+
return createState(contextInstance.props).scale;
|
|
288
|
+
}
|
|
289
|
+
|
|
213
290
|
export function resetTransformations(
|
|
214
291
|
contextInstance: ReactZoomPanPinchContext,
|
|
215
292
|
animationTime: number,
|
|
@@ -226,11 +303,12 @@ export function resetTransformations(
|
|
|
226
303
|
// With `fitOnInit` the initial layout is the fitted one, so that is what a
|
|
227
304
|
// reset returns to (#252).
|
|
228
305
|
if (fitOnInit) {
|
|
229
|
-
const
|
|
306
|
+
const fit = calculateFitToView(
|
|
230
307
|
contextInstance,
|
|
231
308
|
fitOnInit === "cover" ? "cover" : "contain",
|
|
232
309
|
);
|
|
233
|
-
if (
|
|
310
|
+
if (fit) {
|
|
311
|
+
const fitted = applyNamedInitialPosition(contextInstance, fit);
|
|
234
312
|
if (
|
|
235
313
|
scale === fitted.scale &&
|
|
236
314
|
positionX === fitted.positionX &&
|
|
@@ -261,6 +339,15 @@ export function resetTransformations(
|
|
|
261
339
|
targetPositionY = centered.positionY;
|
|
262
340
|
}
|
|
263
341
|
|
|
342
|
+
// Named initial positions win over the centring on their own axis (#349).
|
|
343
|
+
const named = applyNamedInitialPosition(contextInstance, {
|
|
344
|
+
scale: initialTransformation.scale,
|
|
345
|
+
positionX: targetPositionX,
|
|
346
|
+
positionY: targetPositionY,
|
|
347
|
+
});
|
|
348
|
+
targetPositionX = named.positionX;
|
|
349
|
+
targetPositionY = named.positionY;
|
|
350
|
+
|
|
264
351
|
const newBounds = calculateBounds(
|
|
265
352
|
contextInstance,
|
|
266
353
|
initialTransformation.scale,
|
|
@@ -8,6 +8,7 @@ import {
|
|
|
8
8
|
ReactZoomPanPinchState,
|
|
9
9
|
ReactZoomPanPinchRef,
|
|
10
10
|
DeviceType,
|
|
11
|
+
StateType,
|
|
11
12
|
} from "../models";
|
|
12
13
|
import {
|
|
13
14
|
getContext,
|
|
@@ -17,7 +18,6 @@ import {
|
|
|
17
18
|
getTransformStyles,
|
|
18
19
|
makePassiveEventOption,
|
|
19
20
|
getCenterPosition,
|
|
20
|
-
assignRef,
|
|
21
21
|
} from "../utils";
|
|
22
22
|
import { handleCancelAnimation } from "./animations/animations.utils";
|
|
23
23
|
import { handleKeyboardNavigation } from "./keyboard/keyboard.logic";
|
|
@@ -28,8 +28,16 @@ import {
|
|
|
28
28
|
measureSizes,
|
|
29
29
|
} from "./resize/resize.logic";
|
|
30
30
|
import { MeasuredSizesType } from "./resize/resize.types";
|
|
31
|
-
import {
|
|
32
|
-
|
|
31
|
+
import {
|
|
32
|
+
applyNamedInitialPosition,
|
|
33
|
+
calculateFitToView,
|
|
34
|
+
hasNamedInitialPosition,
|
|
35
|
+
} from "./handlers/handlers.utils";
|
|
36
|
+
import {
|
|
37
|
+
getWheelPanDelta,
|
|
38
|
+
isWheelAllowed,
|
|
39
|
+
isWheelPanningAllowed,
|
|
40
|
+
} from "./wheel/wheel.utils";
|
|
33
41
|
import { isPinchAllowed, isPinchStartAllowed } from "./pinch/pinch.utils";
|
|
34
42
|
import { handleCalculateBounds } from "./bounds/bounds.utils";
|
|
35
43
|
import {
|
|
@@ -40,8 +48,11 @@ import {
|
|
|
40
48
|
handleWheelPanningStart,
|
|
41
49
|
} from "./wheel/wheel.logic";
|
|
42
50
|
import {
|
|
51
|
+
CLICK_DISTANCE,
|
|
43
52
|
getPaddingValue,
|
|
53
|
+
getPanSourcePosition,
|
|
44
54
|
handleNewPosition,
|
|
55
|
+
isPanBlockedAtBounds,
|
|
45
56
|
isPanningAllowed,
|
|
46
57
|
isPanningStartAllowed,
|
|
47
58
|
} from "./pan/panning.utils";
|
|
@@ -98,6 +109,15 @@ export class ZoomPanPinch {
|
|
|
98
109
|
public isWheelPanning = false;
|
|
99
110
|
public startCoords: StartCoordsType = null;
|
|
100
111
|
public panStartPosition: { x: number; y: number } | null = null;
|
|
112
|
+
// Pointer travel of the running mouse/touch pan: whether it moved at all
|
|
113
|
+
// (the first move decides `releaseAtBounds`, #509) and whether it went
|
|
114
|
+
// past the click distance (the click after it is swallowed, #452).
|
|
115
|
+
public panGesture: {
|
|
116
|
+
startX: number;
|
|
117
|
+
startY: number;
|
|
118
|
+
hasMoved: boolean;
|
|
119
|
+
isDrag: boolean;
|
|
120
|
+
} | null = null;
|
|
101
121
|
public lastTouch: number | null = null;
|
|
102
122
|
// pinch helpers
|
|
103
123
|
public isPinching = false;
|
|
@@ -276,6 +296,7 @@ export class ZoomPanPinch {
|
|
|
276
296
|
);
|
|
277
297
|
wrapper.removeEventListener("touchmove", this.onTouchPanning, passive);
|
|
278
298
|
wrapper.removeEventListener("touchend", this.onTouchPanningStop, passive);
|
|
299
|
+
wrapper.removeEventListener("scroll", this.onWrapperScroll, passive);
|
|
279
300
|
};
|
|
280
301
|
|
|
281
302
|
handleInitializeWrapperEvents = (wrapper: HTMLDivElement): void => {
|
|
@@ -288,6 +309,25 @@ export class ZoomPanPinch {
|
|
|
288
309
|
wrapper.addEventListener("touchstart", this.onTouchPanningStart, passive);
|
|
289
310
|
wrapper.addEventListener("touchmove", this.onTouchPanning, passive);
|
|
290
311
|
wrapper.addEventListener("touchend", this.onTouchPanningStop, passive);
|
|
312
|
+
wrapper.addEventListener("scroll", this.onWrapperScroll, passive);
|
|
313
|
+
};
|
|
314
|
+
|
|
315
|
+
// The wrapper clips with `overflow: hidden`, but the browser still scrolls
|
|
316
|
+
// it to reveal a focused input (the virtual keyboard on mobile) or for
|
|
317
|
+
// `scrollIntoView`. Nothing in the transform knows about that offset, so
|
|
318
|
+
// the content stayed shifted and could not be panned back (#280, #477).
|
|
319
|
+
// Fold the scroll into the position instead: the target stays in view and
|
|
320
|
+
// the state matches what is on screen.
|
|
321
|
+
onWrapperScroll = (): void => {
|
|
322
|
+
const wrapper = this.wrapperComponent;
|
|
323
|
+
if (!wrapper) return;
|
|
324
|
+
const { scrollLeft, scrollTop } = wrapper;
|
|
325
|
+
if (!scrollLeft && !scrollTop) return;
|
|
326
|
+
|
|
327
|
+
wrapper.scrollLeft = 0;
|
|
328
|
+
wrapper.scrollTop = 0;
|
|
329
|
+
const { scale, positionX, positionY } = this.state;
|
|
330
|
+
this.setState(scale, positionX - scrollLeft, positionY - scrollTop);
|
|
291
331
|
};
|
|
292
332
|
|
|
293
333
|
handleInitialize = (): void => {
|
|
@@ -295,7 +335,7 @@ export class ZoomPanPinch {
|
|
|
295
335
|
this.applyTransformation();
|
|
296
336
|
this.onInitCallbacks.forEach((callback) => callback(getContext(this)));
|
|
297
337
|
|
|
298
|
-
if (centerOnInit || fitOnInit) {
|
|
338
|
+
if (centerOnInit || fitOnInit || hasNamedInitialPosition(this)) {
|
|
299
339
|
const applied = this.applyInitialLayout();
|
|
300
340
|
// Content without a size yet (an image still loading) gets its layout
|
|
301
341
|
// applied again by `onSizeChange` once it has one. Once a real layout
|
|
@@ -399,32 +439,38 @@ export class ZoomPanPinch {
|
|
|
399
439
|
onWheelPanning = (event: WheelEvent): void => {
|
|
400
440
|
const { onPanning } = this.props;
|
|
401
441
|
const { trackPadPanning } = this.setup;
|
|
402
|
-
const { lockAxisX, lockAxisY } = trackPadPanning;
|
|
442
|
+
const { lockAxisX, lockAxisY, speed } = trackPadPanning;
|
|
443
|
+
const wrapper = this.wrapperComponent;
|
|
403
444
|
|
|
404
445
|
this.syncModifierKeys(event);
|
|
405
446
|
|
|
406
447
|
const isAllowed = isWheelPanningAllowed(this, event);
|
|
407
448
|
|
|
408
|
-
if (!isAllowed) return;
|
|
449
|
+
if (!isAllowed || !wrapper) return;
|
|
409
450
|
|
|
410
451
|
if (event.cancelable) {
|
|
411
452
|
event.preventDefault();
|
|
412
453
|
}
|
|
413
454
|
event.stopPropagation();
|
|
414
455
|
|
|
415
|
-
const
|
|
416
|
-
const
|
|
417
|
-
const
|
|
418
|
-
|
|
419
|
-
|
|
456
|
+
const delta = getWheelPanDelta(event, wrapper, speed);
|
|
457
|
+
const deltaX = lockAxisX ? 0 : delta.x;
|
|
458
|
+
const deltaY = lockAxisY ? 0 : delta.y;
|
|
459
|
+
|
|
460
|
+
if (!deltaX && !deltaY) return;
|
|
420
461
|
|
|
421
462
|
const { sizeX, sizeY } = this.setup.autoAlignment;
|
|
422
463
|
const paddingValueX = getPaddingValue(this, sizeX);
|
|
423
464
|
const paddingValueY = getPaddingValue(this, sizeY);
|
|
424
465
|
|
|
425
|
-
if (newPositionX === positionX && newPositionY === positionY) return;
|
|
426
|
-
|
|
427
466
|
handleWheelPanningStart(this, event);
|
|
467
|
+
|
|
468
|
+
// Deltas add up on the pointer's side of the rubber band, the same way
|
|
469
|
+
// a drag does, so the content slows down towards the padding limit.
|
|
470
|
+
const source = getPanSourcePosition(this);
|
|
471
|
+
const newPositionX = source.x - deltaX;
|
|
472
|
+
const newPositionY = source.y - deltaY;
|
|
473
|
+
|
|
428
474
|
handleNewPosition(
|
|
429
475
|
this,
|
|
430
476
|
newPositionX,
|
|
@@ -466,6 +512,7 @@ export class ZoomPanPinch {
|
|
|
466
512
|
|
|
467
513
|
handleCancelAnimation(this);
|
|
468
514
|
handlePanningStart(this, event);
|
|
515
|
+
this.startPanGesture(event.clientX, event.clientY);
|
|
469
516
|
this.lockTextSelection();
|
|
470
517
|
this.focusForKeyboard();
|
|
471
518
|
handleCallback(getContext(this), event, onPanningStart);
|
|
@@ -492,6 +539,11 @@ export class ZoomPanPinch {
|
|
|
492
539
|
const keysPressed = this.isPressingKeys(this.setup.panning.activationKeys);
|
|
493
540
|
if (!keysPressed) return;
|
|
494
541
|
|
|
542
|
+
if (this.trackPanGesture(event.clientX, event.clientY)) {
|
|
543
|
+
this.onPanningStop(event);
|
|
544
|
+
return;
|
|
545
|
+
}
|
|
546
|
+
|
|
495
547
|
if (event.cancelable) {
|
|
496
548
|
event.preventDefault();
|
|
497
549
|
}
|
|
@@ -502,17 +554,80 @@ export class ZoomPanPinch {
|
|
|
502
554
|
};
|
|
503
555
|
|
|
504
556
|
onPanningStop = (event: MouseEvent | TouchEvent): void => {
|
|
505
|
-
const { velocityDisabled } = this.setup.panning;
|
|
557
|
+
const { velocityDisabled, suppressClickAfterPan } = this.setup.panning;
|
|
506
558
|
const { onPanningStop } = this.props;
|
|
507
559
|
|
|
508
560
|
if (this.isPanning) {
|
|
561
|
+
const isDrag = !!this.panGesture?.isDrag;
|
|
562
|
+
this.panGesture = null;
|
|
509
563
|
this.unlockTextSelection();
|
|
510
564
|
handlePanningEnd(this, velocityDisabled);
|
|
511
565
|
handleCallback(getContext(this), event, onPanningStop);
|
|
512
566
|
this.flushResizeAlignment();
|
|
567
|
+
// Only a mouseup is followed by a click on the content; a released or
|
|
568
|
+
// cancelled gesture (mousemove, mouseleave, touch) is not.
|
|
569
|
+
if (isDrag && suppressClickAfterPan && event.type === "mouseup") {
|
|
570
|
+
this.suppressNextClick();
|
|
571
|
+
}
|
|
513
572
|
}
|
|
514
573
|
};
|
|
515
574
|
|
|
575
|
+
startPanGesture = (clientX: number, clientY: number): void => {
|
|
576
|
+
this.panGesture = {
|
|
577
|
+
startX: clientX,
|
|
578
|
+
startY: clientY,
|
|
579
|
+
hasMoved: false,
|
|
580
|
+
isDrag: false,
|
|
581
|
+
};
|
|
582
|
+
};
|
|
583
|
+
|
|
584
|
+
// Records the pointer travel of the running pan. Returns true when this is
|
|
585
|
+
// the first move, it heads into a bound the content already rests on and
|
|
586
|
+
// `releaseAtBounds` is on: the caller then ends the pan without claiming
|
|
587
|
+
// the event, so the page scrolls or a parent takes the drag (#509).
|
|
588
|
+
trackPanGesture = (clientX: number, clientY: number): boolean => {
|
|
589
|
+
const gesture = this.panGesture;
|
|
590
|
+
if (!gesture) return false;
|
|
591
|
+
|
|
592
|
+
const dx = clientX - gesture.startX;
|
|
593
|
+
const dy = clientY - gesture.startY;
|
|
594
|
+
if (!dx && !dy) return false;
|
|
595
|
+
|
|
596
|
+
const isFirstMove = !gesture.hasMoved;
|
|
597
|
+
gesture.hasMoved = true;
|
|
598
|
+
if (dx * dx + dy * dy > CLICK_DISTANCE ** 2) gesture.isDrag = true;
|
|
599
|
+
|
|
600
|
+
return (
|
|
601
|
+
isFirstMove &&
|
|
602
|
+
this.setup.panning.releaseAtBounds &&
|
|
603
|
+
isPanBlockedAtBounds(this, dx, dy)
|
|
604
|
+
);
|
|
605
|
+
};
|
|
606
|
+
|
|
607
|
+
// The browser fires `click` right after `mouseup`, in the same task, so a
|
|
608
|
+
// capture listener on the window that removes itself on the next tick
|
|
609
|
+
// swallows exactly that click and nothing later (#452).
|
|
610
|
+
suppressNextClick = (): void => {
|
|
611
|
+
const wrapper = this.wrapperComponent;
|
|
612
|
+
const win = wrapper?.ownerDocument?.defaultView;
|
|
613
|
+
if (!wrapper || !win) return;
|
|
614
|
+
|
|
615
|
+
const swallow = (event: MouseEvent) => {
|
|
616
|
+
const path =
|
|
617
|
+
typeof event.composedPath === "function" ? event.composedPath() : [];
|
|
618
|
+
const isInside =
|
|
619
|
+
path.includes(wrapper) || wrapper.contains(event.target as Node);
|
|
620
|
+
if (!isInside) return;
|
|
621
|
+
event.preventDefault();
|
|
622
|
+
event.stopPropagation();
|
|
623
|
+
};
|
|
624
|
+
|
|
625
|
+
win.addEventListener("click", swallow, { capture: true, once: true });
|
|
626
|
+
win.setTimeout(() => {
|
|
627
|
+
win.removeEventListener("click", swallow, { capture: true });
|
|
628
|
+
}, 0);
|
|
629
|
+
};
|
|
630
|
+
|
|
516
631
|
/// ///////
|
|
517
632
|
// Pinch
|
|
518
633
|
/// ///////
|
|
@@ -592,6 +707,7 @@ export class ZoomPanPinch {
|
|
|
592
707
|
if (!isAllowed || this.setup.panning.disabled) return;
|
|
593
708
|
handleCancelAnimation(this);
|
|
594
709
|
handlePanningStart(this, event);
|
|
710
|
+
this.startPanGesture(touches[0].clientX, touches[0].clientY);
|
|
595
711
|
this.lockTextSelection();
|
|
596
712
|
this.focusForKeyboard();
|
|
597
713
|
handleCallback(getContext(this), event, onPanningStart);
|
|
@@ -612,12 +728,18 @@ export class ZoomPanPinch {
|
|
|
612
728
|
const isAllowed = isPanningAllowed(this);
|
|
613
729
|
if (!isAllowed) return;
|
|
614
730
|
|
|
731
|
+
const touch = event.touches[0];
|
|
732
|
+
// Not cancelling the first touchmove is what lets the browser scroll.
|
|
733
|
+
if (this.trackPanGesture(touch.clientX, touch.clientY)) {
|
|
734
|
+
this.onPanningStop(event);
|
|
735
|
+
return;
|
|
736
|
+
}
|
|
737
|
+
|
|
615
738
|
if (event.cancelable) {
|
|
616
739
|
event.preventDefault();
|
|
617
740
|
}
|
|
618
741
|
event.stopPropagation();
|
|
619
742
|
|
|
620
|
-
const touch = event.touches[0];
|
|
621
743
|
handlePanning(this, touch.clientX, touch.clientY, DeviceType.TOUCH);
|
|
622
744
|
handleCallback(getContext(this), event, onPanning);
|
|
623
745
|
} else if (event.touches.length > 1) {
|
|
@@ -662,6 +784,7 @@ export class ZoomPanPinch {
|
|
|
662
784
|
if (this.isPanning) {
|
|
663
785
|
this.isPanning = false;
|
|
664
786
|
this.startCoords = null;
|
|
787
|
+
this.panGesture = null;
|
|
665
788
|
}
|
|
666
789
|
this.unlockTextSelection();
|
|
667
790
|
this.flushResizeAlignment();
|
|
@@ -771,28 +894,31 @@ export class ZoomPanPinch {
|
|
|
771
894
|
|
|
772
895
|
// `fitOnInit` wins over `centerOnInit` (a fit is always centred). Falls
|
|
773
896
|
// back to centring while the content has no size yet; `onSizeChange`
|
|
774
|
-
// re-runs this once it does.
|
|
775
|
-
// whether
|
|
897
|
+
// re-runs this once it does. A named `initialPositionX/Y` then places its
|
|
898
|
+
// own axis (#349). Returns whether both boxes had a size, i.e. whether
|
|
899
|
+
// this was a real initial layout.
|
|
776
900
|
applyInitialLayout = (): boolean => {
|
|
777
|
-
const { fitOnInit } = this.setup;
|
|
901
|
+
const { fitOnInit, centerOnInit } = this.setup;
|
|
778
902
|
const { wrapperComponent, contentComponent } = this;
|
|
903
|
+
if (!wrapperComponent || !contentComponent) return false;
|
|
779
904
|
const hasSizes =
|
|
780
|
-
!!wrapperComponent &&
|
|
781
|
-
!!contentComponent &&
|
|
782
905
|
wrapperComponent.clientWidth > 0 &&
|
|
783
906
|
wrapperComponent.clientHeight > 0 &&
|
|
784
907
|
(contentComponent.offsetWidth > 0 || contentComponent.offsetHeight > 0);
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
)
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
908
|
+
|
|
909
|
+
const { scale, positionX, positionY } = this.state;
|
|
910
|
+
let target: StateType = { scale, positionX, positionY };
|
|
911
|
+
const fitted = fitOnInit
|
|
912
|
+
? calculateFitToView(this, fitOnInit === "cover" ? "cover" : "contain")
|
|
913
|
+
: null;
|
|
914
|
+
if (fitted) {
|
|
915
|
+
target = fitted;
|
|
916
|
+
} else if (fitOnInit || centerOnInit) {
|
|
917
|
+
target = getCenterPosition(scale, wrapperComponent, contentComponent);
|
|
794
918
|
}
|
|
795
|
-
|
|
919
|
+
|
|
920
|
+
const placed = applyNamedInitialPosition(this, target);
|
|
921
|
+
this.setState(placed.scale, placed.positionX, placed.positionY);
|
|
796
922
|
return hasSizes;
|
|
797
923
|
};
|
|
798
924
|
|
|
@@ -933,6 +1059,5 @@ export class ZoomPanPinch {
|
|
|
933
1059
|
this.isInitialized = true;
|
|
934
1060
|
const ctx = getContext(this);
|
|
935
1061
|
handleCallback(ctx, undefined, this.props.onInit);
|
|
936
|
-
assignRef(this.props.ref, ctx);
|
|
937
1062
|
};
|
|
938
1063
|
}
|