react-zoom-pan-pinch 4.1.1 → 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 +4 -4
- package/dist/index.cjs.js +1081 -231
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.d.ts +99 -3
- package/dist/index.esm.js +1082 -232
- 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/components/transform-component/transform-component.module.css +2 -5
- package/src/components/transform-component/transform-component.tsx +7 -0
- package/src/components/virtualize/virtualize.tsx +2 -2
- package/src/constants/state.constants.ts +9 -0
- package/src/core/animations/animations.utils.ts +2 -0
- package/src/core/bounds/bounds.utils.ts +90 -2
- package/src/core/handlers/handlers.logic.ts +85 -0
- package/src/core/handlers/handlers.utils.ts +102 -5
- package/src/core/instance.core.ts +222 -41
- package/src/core/keyboard/keyboard.logic.ts +84 -0
- package/src/core/pan/panning.logic.ts +24 -4
- package/src/core/pan/panning.utils.ts +95 -36
- package/src/core/pan/velocity.logic.ts +11 -4
- package/src/core/pan/velocity.utils.ts +2 -2
- package/src/core/resize/index.ts +0 -0
- package/src/core/resize/resize.logic.ts +245 -0
- package/src/core/resize/resize.types.ts +6 -0
- package/src/core/wheel/wheel.logic.ts +11 -2
- package/src/core/zoom/zoom.logic.ts +2 -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 +34 -0
- package/src/utils/context.utils.ts +4 -0
- package/src/utils/styles.utils.ts +2 -2
|
@@ -9,7 +9,10 @@ import {
|
|
|
9
9
|
isEditableTarget,
|
|
10
10
|
isExcludedNode,
|
|
11
11
|
} from "../../utils";
|
|
12
|
-
import {
|
|
12
|
+
import {
|
|
13
|
+
getPanBoundedPosition,
|
|
14
|
+
getRubberBandSource,
|
|
15
|
+
} from "../bounds/bounds.utils";
|
|
13
16
|
import { handleCalculateZoomPositions } from "../zoom/zoom.utils";
|
|
14
17
|
|
|
15
18
|
export const isPanningStartAllowed = (
|
|
@@ -20,16 +23,23 @@ export const isPanningStartAllowed = (
|
|
|
20
23
|
const { isInitialized, wrapperComponent } = contextInstance;
|
|
21
24
|
|
|
22
25
|
const target = event.target as HTMLElement;
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
26
|
+
// `composedPath()` also lists the nodes inside a shadow root (#371) and the
|
|
27
|
+
// real target in another window (portal, popup) whose nodes fail an
|
|
28
|
+
// `instanceof Element` check from this realm (#290, PR #552): only
|
|
29
|
+
// duck-typed node checks are safe here.
|
|
30
|
+
const isElementNode = (node: unknown): node is Element =>
|
|
31
|
+
!!node &&
|
|
32
|
+
typeof node === "object" &&
|
|
33
|
+
(node as Node).nodeType === 1 &&
|
|
34
|
+
typeof (node as Element).contains === "function";
|
|
35
|
+
const isWrapperChild =
|
|
36
|
+
typeof event.composedPath === "function"
|
|
37
|
+
? event
|
|
38
|
+
.composedPath()
|
|
39
|
+
.some(
|
|
40
|
+
(node) => isElementNode(node) && !!wrapperComponent?.contains(node),
|
|
41
|
+
)
|
|
42
|
+
: !!wrapperComponent?.contains(target);
|
|
33
43
|
|
|
34
44
|
const isAllowed = isInitialized && target && isWrapperChild;
|
|
35
45
|
|
|
@@ -59,11 +69,75 @@ export const isPanningAllowed = (
|
|
|
59
69
|
return true;
|
|
60
70
|
};
|
|
61
71
|
|
|
72
|
+
export const getPaddingValue = (
|
|
73
|
+
contextInstance: ReactZoomPanPinchContext,
|
|
74
|
+
size: number,
|
|
75
|
+
explicitScale?: number,
|
|
76
|
+
): number => {
|
|
77
|
+
const { setup, state } = contextInstance;
|
|
78
|
+
const { minScale, disablePadding, centerZoomedOut } = setup;
|
|
79
|
+
|
|
80
|
+
const scale = explicitScale ?? state.scale;
|
|
81
|
+
|
|
82
|
+
if (size > 0 && scale >= minScale && !disablePadding && !centerZoomedOut) {
|
|
83
|
+
return size;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
return 0;
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Dragging past the bounds stretches like a rubber band only when the content
|
|
91
|
+
* is going to return to them on release (`autoAlignment`). Without that return
|
|
92
|
+
* the padding is a plain area the content can be left in, so it follows the
|
|
93
|
+
* pointer 1:1 up to its edge.
|
|
94
|
+
*/
|
|
95
|
+
export function isRubberBandEnabled(
|
|
96
|
+
contextInstance: ReactZoomPanPinchContext,
|
|
97
|
+
): boolean {
|
|
98
|
+
const { limitToBounds, autoAlignment } = contextInstance.setup;
|
|
99
|
+
return limitToBounds && !autoAlignment.disabled;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* The position a pan continues from. Inside the bounds that is the content
|
|
104
|
+
* position; inside the padding the content trails the pointer (rubber band),
|
|
105
|
+
* so the pointer's own position is traced back from it — otherwise the first
|
|
106
|
+
* move of a pan started there would make the content jump.
|
|
107
|
+
*/
|
|
108
|
+
export function getPanSourcePosition(
|
|
109
|
+
contextInstance: ReactZoomPanPinchContext,
|
|
110
|
+
): PositionType {
|
|
111
|
+
const { bounds, setup, state } = contextInstance;
|
|
112
|
+
const { positionX, positionY } = state;
|
|
113
|
+
|
|
114
|
+
if (!bounds || !isRubberBandEnabled(contextInstance)) {
|
|
115
|
+
return { x: positionX, y: positionY };
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
const { sizeX, sizeY } = setup.autoAlignment;
|
|
119
|
+
|
|
120
|
+
return {
|
|
121
|
+
x: getRubberBandSource(
|
|
122
|
+
positionX,
|
|
123
|
+
bounds.minPositionX,
|
|
124
|
+
bounds.maxPositionX,
|
|
125
|
+
getPaddingValue(contextInstance, sizeX),
|
|
126
|
+
),
|
|
127
|
+
y: getRubberBandSource(
|
|
128
|
+
positionY,
|
|
129
|
+
bounds.minPositionY,
|
|
130
|
+
bounds.maxPositionY,
|
|
131
|
+
getPaddingValue(contextInstance, sizeY),
|
|
132
|
+
),
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
|
|
62
136
|
export const handlePanningSetup = (
|
|
63
137
|
contextInstance: ReactZoomPanPinchContext,
|
|
64
138
|
event: MouseEvent,
|
|
65
139
|
): void => {
|
|
66
|
-
const { positionX, positionY } = contextInstance
|
|
140
|
+
const { x: positionX, y: positionY } = getPanSourcePosition(contextInstance);
|
|
67
141
|
|
|
68
142
|
contextInstance.isPanning = true;
|
|
69
143
|
|
|
@@ -79,7 +153,7 @@ export const handleTouchPanningSetup = (
|
|
|
79
153
|
event: TouchEvent,
|
|
80
154
|
): void => {
|
|
81
155
|
const { touches } = event;
|
|
82
|
-
const { positionX, positionY } = contextInstance
|
|
156
|
+
const { x: positionX, y: positionY } = getPanSourcePosition(contextInstance);
|
|
83
157
|
|
|
84
158
|
contextInstance.isPanning = true;
|
|
85
159
|
|
|
@@ -108,11 +182,11 @@ export function handlePanToBounds(
|
|
|
108
182
|
|
|
109
183
|
const mousePosX =
|
|
110
184
|
positionX > maxPositionX
|
|
111
|
-
? wrapperComponent.
|
|
185
|
+
? wrapperComponent.clientWidth
|
|
112
186
|
: contextInstance.setup.minPositionX || 0;
|
|
113
187
|
const mousePosY =
|
|
114
188
|
positionY > maxPositionY
|
|
115
|
-
? wrapperComponent.
|
|
189
|
+
? wrapperComponent.clientHeight
|
|
116
190
|
: contextInstance.setup.minPositionY || 0;
|
|
117
191
|
|
|
118
192
|
const { x, y } = handleCalculateZoomPositions(
|
|
@@ -163,14 +237,14 @@ export function handleNewPosition(
|
|
|
163
237
|
return;
|
|
164
238
|
}
|
|
165
239
|
|
|
166
|
-
const { x, y } =
|
|
240
|
+
const { x, y } = getPanBoundedPosition(
|
|
167
241
|
newPositionX,
|
|
168
242
|
newPositionY,
|
|
169
243
|
bounds,
|
|
170
244
|
limitToBounds,
|
|
171
245
|
paddingValueX,
|
|
172
246
|
paddingValueY,
|
|
173
|
-
|
|
247
|
+
isRubberBandEnabled(contextInstance),
|
|
174
248
|
);
|
|
175
249
|
|
|
176
250
|
contextInstance.setState(scale, x, y);
|
|
@@ -181,10 +255,12 @@ export const getPanningClientPosition = (
|
|
|
181
255
|
clientX: number,
|
|
182
256
|
clientY: number,
|
|
183
257
|
): PositionType => {
|
|
184
|
-
const { startCoords
|
|
258
|
+
const { startCoords } = contextInstance;
|
|
185
259
|
const { panning } = contextInstance.setup;
|
|
186
260
|
const { lockAxisX, lockAxisY } = panning;
|
|
187
|
-
|
|
261
|
+
// A locked axis keeps its place: the source maps back onto the position
|
|
262
|
+
// it has now, also when it rests inside the padding.
|
|
263
|
+
const { x: positionX, y: positionY } = getPanSourcePosition(contextInstance);
|
|
188
264
|
|
|
189
265
|
if (!startCoords) {
|
|
190
266
|
return { x: positionX, y: positionY };
|
|
@@ -197,20 +273,3 @@ export const getPanningClientPosition = (
|
|
|
197
273
|
|
|
198
274
|
return { x: newPositionX, y: newPositionY };
|
|
199
275
|
};
|
|
200
|
-
|
|
201
|
-
export const getPaddingValue = (
|
|
202
|
-
contextInstance: ReactZoomPanPinchContext,
|
|
203
|
-
size: number,
|
|
204
|
-
explicitScale?: number,
|
|
205
|
-
): number => {
|
|
206
|
-
const { setup, state } = contextInstance;
|
|
207
|
-
const { minScale, disablePadding, centerZoomedOut } = setup;
|
|
208
|
-
|
|
209
|
-
const scale = explicitScale ?? state.scale;
|
|
210
|
-
|
|
211
|
-
if (size > 0 && scale >= minScale && !disablePadding && !centerZoomedOut) {
|
|
212
|
-
return size;
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
return 0;
|
|
216
|
-
};
|
|
@@ -4,7 +4,7 @@ import { ReactZoomPanPinchContext } from "../../models/context.model";
|
|
|
4
4
|
import { getContext } from "../../utils";
|
|
5
5
|
import { animations } from "../animations/animations.constants";
|
|
6
6
|
import { handleSetupAnimation } from "../animations/animations.utils";
|
|
7
|
-
import { getPaddingValue } from "./panning.utils";
|
|
7
|
+
import { getPaddingValue, isRubberBandEnabled } from "./panning.utils";
|
|
8
8
|
import {
|
|
9
9
|
getVelocityPosition,
|
|
10
10
|
getVelocityMoveTime,
|
|
@@ -14,7 +14,7 @@ import {
|
|
|
14
14
|
|
|
15
15
|
export function getSizeMultiplier(wrapperComponent: HTMLDivElement): number {
|
|
16
16
|
const defaultMultiplier = 1;
|
|
17
|
-
const value = wrapperComponent.
|
|
17
|
+
const value = wrapperComponent.clientWidth / window.innerWidth;
|
|
18
18
|
|
|
19
19
|
if (Number.isNaN(value)) {
|
|
20
20
|
return defaultMultiplier;
|
|
@@ -123,8 +123,15 @@ export function handleVelocityPanning(
|
|
|
123
123
|
|
|
124
124
|
const paddingValueX = getPaddingValue(contextInstance, sizeX);
|
|
125
125
|
const paddingValueY = getPaddingValue(contextInstance, sizeY);
|
|
126
|
-
|
|
127
|
-
|
|
126
|
+
// A fling released inside the rubber band stays within the same stretch a
|
|
127
|
+
// drag could reach (`padding` px), not a share of the wrapper size.
|
|
128
|
+
const isRubberBand = isRubberBandEnabled(contextInstance);
|
|
129
|
+
const paddingX = isRubberBand
|
|
130
|
+
? paddingValueX
|
|
131
|
+
: (paddingValueX * wrapperComponent.clientWidth) / 100;
|
|
132
|
+
const paddingY = isRubberBand
|
|
133
|
+
? paddingValueY
|
|
134
|
+
: (paddingValueY * wrapperComponent.clientHeight) / 100;
|
|
128
135
|
const maxTargetX = maxPositionX + paddingX;
|
|
129
136
|
const minTargetX = minPositionX - paddingX;
|
|
130
137
|
|
|
@@ -15,8 +15,8 @@ export const isVelocityCalculationAllowed = (
|
|
|
15
15
|
if (!limitToBounds) return true;
|
|
16
16
|
|
|
17
17
|
const contentOverflows =
|
|
18
|
-
wrapperComponent.
|
|
19
|
-
wrapperComponent.
|
|
18
|
+
wrapperComponent.clientWidth < contentComponent.offsetWidth * scale ||
|
|
19
|
+
wrapperComponent.clientHeight < contentComponent.offsetHeight * scale;
|
|
20
20
|
|
|
21
21
|
return contentOverflows;
|
|
22
22
|
};
|
|
File without changes
|
|
@@ -0,0 +1,245 @@
|
|
|
1
|
+
/* eslint-disable no-param-reassign */
|
|
2
|
+
import { ReactZoomPanPinchContext, StateType } from "../../models";
|
|
3
|
+
import { roundNumber } from "../../utils";
|
|
4
|
+
import { handleCancelAnimation } from "../animations/animations.utils";
|
|
5
|
+
import {
|
|
6
|
+
getMouseBoundedPosition,
|
|
7
|
+
handleCalculateBounds,
|
|
8
|
+
} from "../bounds/bounds.utils";
|
|
9
|
+
import { MeasuredSizesType } from "./resize.types";
|
|
10
|
+
|
|
11
|
+
export const measureSizes = (
|
|
12
|
+
wrapperComponent: HTMLElement,
|
|
13
|
+
contentComponent: HTMLElement,
|
|
14
|
+
): MeasuredSizesType => ({
|
|
15
|
+
wrapperWidth: wrapperComponent.offsetWidth,
|
|
16
|
+
wrapperHeight: wrapperComponent.offsetHeight,
|
|
17
|
+
contentWidth: contentComponent.offsetWidth,
|
|
18
|
+
contentHeight: contentComponent.offsetHeight,
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
export const hasSizeChanged = (
|
|
22
|
+
previous: MeasuredSizesType | null,
|
|
23
|
+
current: MeasuredSizesType,
|
|
24
|
+
): boolean =>
|
|
25
|
+
!previous ||
|
|
26
|
+
previous.wrapperWidth !== current.wrapperWidth ||
|
|
27
|
+
previous.wrapperHeight !== current.wrapperHeight ||
|
|
28
|
+
previous.contentWidth !== current.contentWidth ||
|
|
29
|
+
previous.contentHeight !== current.contentHeight;
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* A pointer is physically held: a mouse/touch pan or a pinch. The bounds
|
|
33
|
+
* belong to that gesture until it is released (moving the content under a
|
|
34
|
+
* held cursor is what feels like a jump).
|
|
35
|
+
*/
|
|
36
|
+
export const isPointerGestureActive = (
|
|
37
|
+
contextInstance: ReactZoomPanPinchContext,
|
|
38
|
+
): boolean => contextInstance.isPanning || contextInstance.isPinching;
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Any gesture is in progress: a held pointer, or a wheel sequence whose stop
|
|
42
|
+
* timers have not fired yet.
|
|
43
|
+
*/
|
|
44
|
+
export const isGestureActive = (
|
|
45
|
+
contextInstance: ReactZoomPanPinchContext,
|
|
46
|
+
): boolean =>
|
|
47
|
+
isPointerGestureActive(contextInstance) ||
|
|
48
|
+
contextInstance.wheelStopEventTimer !== null ||
|
|
49
|
+
contextInstance.wheelAnimationTimer !== null;
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Recomputes the bounds for the current scale and content/wrapper sizes and
|
|
53
|
+
* returns the state the transform has to move to so it sits inside them.
|
|
54
|
+
* Returns `null` when the transform is already inside the bounds or when
|
|
55
|
+
* nothing limits it (`limitToBounds` and `centerZoomedOut` both off).
|
|
56
|
+
*
|
|
57
|
+
* The bounds are stored on the instance as a side effect, so gestures that
|
|
58
|
+
* follow a resize clamp against the fresh sizes even when no move is needed.
|
|
59
|
+
*/
|
|
60
|
+
export function calculateResizeAlignment(
|
|
61
|
+
contextInstance: ReactZoomPanPinchContext,
|
|
62
|
+
): StateType | null {
|
|
63
|
+
const { wrapperComponent, contentComponent, setup, state } = contextInstance;
|
|
64
|
+
if (!wrapperComponent || !contentComponent) return null;
|
|
65
|
+
|
|
66
|
+
const bounds = handleCalculateBounds(contextInstance, state.scale);
|
|
67
|
+
|
|
68
|
+
// Same rule as the post-pan alignment (`handlePanToBounds`).
|
|
69
|
+
const { limitToBounds, centerZoomedOut } = setup;
|
|
70
|
+
if (!limitToBounds && !centerZoomedOut) return null;
|
|
71
|
+
|
|
72
|
+
const { x, y } = getMouseBoundedPosition(
|
|
73
|
+
state.positionX,
|
|
74
|
+
state.positionY,
|
|
75
|
+
bounds,
|
|
76
|
+
true,
|
|
77
|
+
0,
|
|
78
|
+
0,
|
|
79
|
+
wrapperComponent,
|
|
80
|
+
);
|
|
81
|
+
|
|
82
|
+
// `boundLimiter` rounds to two decimals; compare against the same rounding
|
|
83
|
+
// so an in-bounds transform never starts a zero-length animation.
|
|
84
|
+
const isAligned =
|
|
85
|
+
x === roundNumber(state.positionX, 2) &&
|
|
86
|
+
y === roundNumber(state.positionY, 2);
|
|
87
|
+
if (isAligned) return null;
|
|
88
|
+
|
|
89
|
+
return { scale: state.scale, positionX: x, positionY: y };
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
const stopResizeTracking = (contextInstance: ReactZoomPanPinchContext) => {
|
|
93
|
+
contextInstance.animation = null;
|
|
94
|
+
contextInstance.animationFrame = null;
|
|
95
|
+
contextInstance.isAnimating = false;
|
|
96
|
+
contextInstance.resizeAnimation = null;
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Eases the transform towards the current bounds, frame by frame, until it
|
|
101
|
+
* rests on them.
|
|
102
|
+
*
|
|
103
|
+
* The bounds are re-read every frame, so a size that keeps changing (a CSS
|
|
104
|
+
* height transition, a layout settling over several frames) is followed
|
|
105
|
+
* smoothly — the content edge trails the moving bound by a few pixels
|
|
106
|
+
* instead of lagging behind a stale target and bursting after it. Each
|
|
107
|
+
* frame closes a fixed fraction of the remaining gap (exponential
|
|
108
|
+
* smoothing), tuned so a one-off step still settles within
|
|
109
|
+
* `autoAlignment.animationTime`.
|
|
110
|
+
*/
|
|
111
|
+
function startResizeTracking(contextInstance: ReactZoomPanPinchContext): void {
|
|
112
|
+
const { animationTime } = contextInstance.setup.autoAlignment;
|
|
113
|
+
// Over 99% of a step is covered after `animationTime`; the sub-pixel rest
|
|
114
|
+
// snaps (see `isSettled`).
|
|
115
|
+
const tau = Math.max(animationTime / 5, 1);
|
|
116
|
+
|
|
117
|
+
handleCancelAnimation(contextInstance);
|
|
118
|
+
|
|
119
|
+
let lastFrameTime = Date.now();
|
|
120
|
+
const track = () => {
|
|
121
|
+
if (!contextInstance.mounted || contextInstance.animation !== track) {
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
const now = Date.now();
|
|
125
|
+
const frameTime = Math.max(now - lastFrameTime, 1);
|
|
126
|
+
lastFrameTime = now;
|
|
127
|
+
|
|
128
|
+
const targetState = calculateResizeAlignment(contextInstance);
|
|
129
|
+
if (!targetState) {
|
|
130
|
+
// Back inside the bounds (the content grew again).
|
|
131
|
+
stopResizeTracking(contextInstance);
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
const { scale, positionX, positionY } = contextInstance.state;
|
|
136
|
+
const factor = 1 - Math.exp(-frameTime / tau);
|
|
137
|
+
let x = positionX + (targetState.positionX - positionX) * factor;
|
|
138
|
+
let y = positionY + (targetState.positionY - positionY) * factor;
|
|
139
|
+
|
|
140
|
+
const isSettled =
|
|
141
|
+
Math.abs(targetState.positionX - x) < 0.5 &&
|
|
142
|
+
Math.abs(targetState.positionY - y) < 0.5;
|
|
143
|
+
if (isSettled) {
|
|
144
|
+
x = targetState.positionX;
|
|
145
|
+
y = targetState.positionY;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
contextInstance.isAnimating = !isSettled;
|
|
149
|
+
contextInstance.setState(scale, x, y);
|
|
150
|
+
|
|
151
|
+
if (isSettled) {
|
|
152
|
+
stopResizeTracking(contextInstance);
|
|
153
|
+
return;
|
|
154
|
+
}
|
|
155
|
+
contextInstance.animationFrame = requestAnimationFrame(track);
|
|
156
|
+
};
|
|
157
|
+
|
|
158
|
+
contextInstance.animation = track;
|
|
159
|
+
contextInstance.resizeAnimation = track;
|
|
160
|
+
contextInstance.animationResolve = null;
|
|
161
|
+
contextInstance.animationFrame = requestAnimationFrame(track);
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* Brings the transform back into the bounds after the content or the
|
|
166
|
+
* wrapper changed size — immediately: a move still in flight (inertia, an
|
|
167
|
+
* alignment, a programmatic animation) is cancelled and a trackpad sequence
|
|
168
|
+
* simply continues against the fresh bounds, so a layout that collapses
|
|
169
|
+
* while the user scrolls aligns right away.
|
|
170
|
+
*
|
|
171
|
+
* Two things are left alone. A pointer that is still held owns the bounds
|
|
172
|
+
* until it is released (replacing them mid-drag makes the next move clamp
|
|
173
|
+
* against a new limit and jump under the cursor); the release then aligns
|
|
174
|
+
* against the fresh bounds (`handleAlignToBounds`). And an animation that is
|
|
175
|
+
* bringing the scale back inside `minScale`/`maxScale` must finish, because
|
|
176
|
+
* the tracking only moves the position. Both come back here afterwards via
|
|
177
|
+
* `flushResizeAlignment`.
|
|
178
|
+
*/
|
|
179
|
+
export function handleResizeAlignment(
|
|
180
|
+
contextInstance: ReactZoomPanPinchContext,
|
|
181
|
+
): void {
|
|
182
|
+
if (contextInstance.setup.disabled) {
|
|
183
|
+
// Nothing may move, but the next gesture must see the right limits.
|
|
184
|
+
handleCalculateBounds(contextInstance, contextInstance.state.scale);
|
|
185
|
+
return;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
if (isPointerGestureActive(contextInstance)) {
|
|
189
|
+
contextInstance.isResizeAlignmentPending = true;
|
|
190
|
+
return;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
const isOwnAnimation =
|
|
194
|
+
contextInstance.animation !== null &&
|
|
195
|
+
contextInstance.animation === contextInstance.resizeAnimation;
|
|
196
|
+
|
|
197
|
+
if (contextInstance.animation && !isOwnAnimation) {
|
|
198
|
+
const { scale } = contextInstance.state;
|
|
199
|
+
const { minScale, maxScale } = contextInstance.setup;
|
|
200
|
+
if (scale < minScale || scale > maxScale) {
|
|
201
|
+
contextInstance.isResizeAlignmentPending = true;
|
|
202
|
+
return;
|
|
203
|
+
}
|
|
204
|
+
handleCancelAnimation(contextInstance);
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
const targetState = calculateResizeAlignment(contextInstance);
|
|
208
|
+
|
|
209
|
+
if (!targetState) {
|
|
210
|
+
if (isOwnAnimation) stopResizeTracking(contextInstance);
|
|
211
|
+
return;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
// The tracking loop re-reads the bounds itself; it just keeps running.
|
|
215
|
+
if (isOwnAnimation) return;
|
|
216
|
+
|
|
217
|
+
if (contextInstance.setup.autoAlignment.animationTime === 0) {
|
|
218
|
+
contextInstance.setState(
|
|
219
|
+
targetState.scale,
|
|
220
|
+
targetState.positionX,
|
|
221
|
+
targetState.positionY,
|
|
222
|
+
);
|
|
223
|
+
return;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
startResizeTracking(contextInstance);
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* Entry point for the size observer: ignores notifications that carry no
|
|
231
|
+
* size change (the observer's initial delivery, style-only changes) and
|
|
232
|
+
* aligns on the ones that do.
|
|
233
|
+
*/
|
|
234
|
+
export function handleSizeChange(
|
|
235
|
+
contextInstance: ReactZoomPanPinchContext,
|
|
236
|
+
): void {
|
|
237
|
+
const { wrapperComponent, contentComponent } = contextInstance;
|
|
238
|
+
if (!wrapperComponent || !contentComponent) return;
|
|
239
|
+
|
|
240
|
+
const sizes = measureSizes(wrapperComponent, contentComponent);
|
|
241
|
+
if (!hasSizeChanged(contextInstance.measuredSizes, sizes)) return;
|
|
242
|
+
|
|
243
|
+
contextInstance.measuredSizes = sizes;
|
|
244
|
+
handleResizeAlignment(contextInstance);
|
|
245
|
+
}
|
|
@@ -113,6 +113,7 @@ export const handleWheelStop = (
|
|
|
113
113
|
if (!contextInstance.mounted) return;
|
|
114
114
|
handleAlignToScaleBounds(contextInstance, event.x, event.y);
|
|
115
115
|
contextInstance.wheelAnimationTimer = null;
|
|
116
|
+
contextInstance.flushResizeAlignment();
|
|
116
117
|
}, wheelAnimationTime);
|
|
117
118
|
|
|
118
119
|
// Wheel stop event
|
|
@@ -124,6 +125,7 @@ export const handleWheelStop = (
|
|
|
124
125
|
contextInstance.wheelStopEventTimer = null;
|
|
125
126
|
handleCallback(getContext(contextInstance), event, onWheelStop);
|
|
126
127
|
handleCallback(getContext(contextInstance), event, onZoomStop);
|
|
128
|
+
contextInstance.flushResizeAlignment();
|
|
127
129
|
}, wheelStopEventTime);
|
|
128
130
|
}
|
|
129
131
|
};
|
|
@@ -139,8 +141,13 @@ export const handleWheelPanningStart = (
|
|
|
139
141
|
|
|
140
142
|
if (!contextInstance.wheelStopEventTimer) {
|
|
141
143
|
// Same as a mouse pan start: bounds must reflect the current scale and
|
|
142
|
-
// content size, not the ones measured at mount (#396, #433).
|
|
143
|
-
|
|
144
|
+
// content size, not the ones measured at mount (#396, #433). Only at the
|
|
145
|
+
// start of a wheel sequence though — mid-sequence the gesture owns them
|
|
146
|
+
// (see `handleResizeAlignment`), otherwise a content resize would make
|
|
147
|
+
// the next event clamp against a new limit and jump.
|
|
148
|
+
if (!contextInstance.wheelAnimationTimer) {
|
|
149
|
+
handleCalculateBounds(contextInstance, contextInstance.state.scale);
|
|
150
|
+
}
|
|
144
151
|
handleCallback(getContext(contextInstance), event, onWheelStart);
|
|
145
152
|
handleCallback(getContext(contextInstance), event, onPanningStart);
|
|
146
153
|
}
|
|
@@ -158,6 +165,7 @@ export const handleWheelPanningStop = (
|
|
|
158
165
|
if (!contextInstance.mounted) return;
|
|
159
166
|
handleAlignToScaleBounds(contextInstance, event.x, event.y);
|
|
160
167
|
contextInstance.wheelAnimationTimer = null;
|
|
168
|
+
contextInstance.flushResizeAlignment();
|
|
161
169
|
}, wheelAnimationTime);
|
|
162
170
|
|
|
163
171
|
// Wheel stop event
|
|
@@ -169,6 +177,7 @@ export const handleWheelPanningStop = (
|
|
|
169
177
|
contextInstance.wheelStopEventTimer = null;
|
|
170
178
|
handleCallback(getContext(contextInstance), event, onWheelStop);
|
|
171
179
|
handleCallback(getContext(contextInstance), event, onPanningStop);
|
|
180
|
+
contextInstance.flushResizeAlignment();
|
|
172
181
|
}, wheelStopEventTime);
|
|
173
182
|
}
|
|
174
183
|
};
|
|
@@ -54,8 +54,8 @@ export function handleAlignToScaleBounds(
|
|
|
54
54
|
|
|
55
55
|
if (isDisabled || !wrapperComponent || !contextInstance.mounted) return;
|
|
56
56
|
|
|
57
|
-
const mouseX = mousePositionX || wrapperComponent.
|
|
58
|
-
const mouseY = mousePositionY || wrapperComponent.
|
|
57
|
+
const mouseX = mousePositionX || wrapperComponent.clientWidth / 2;
|
|
58
|
+
const mouseY = mousePositionY || wrapperComponent.clientHeight / 2;
|
|
59
59
|
|
|
60
60
|
const targetScale = scale < minScale ? minScale : maxScale;
|
|
61
61
|
const targetState = handleZoomToPoint(
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { ReactZoomPanPinchState } from "../models";
|
|
2
|
+
|
|
3
|
+
export type StateSnapshot = Pick<
|
|
4
|
+
ReactZoomPanPinchState,
|
|
5
|
+
"scale" | "positionX" | "positionY"
|
|
6
|
+
>;
|
|
7
|
+
|
|
8
|
+
export const takeSnapshot = (state: ReactZoomPanPinchState): StateSnapshot => ({
|
|
9
|
+
scale: state.scale,
|
|
10
|
+
positionX: state.positionX,
|
|
11
|
+
positionY: state.positionY,
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
export const isSameSnapshot = (
|
|
15
|
+
snapshot: StateSnapshot | null,
|
|
16
|
+
state: ReactZoomPanPinchState,
|
|
17
|
+
): boolean =>
|
|
18
|
+
snapshot !== null &&
|
|
19
|
+
snapshot.scale === state.scale &&
|
|
20
|
+
snapshot.positionX === state.positionX &&
|
|
21
|
+
snapshot.positionY === state.positionY;
|
|
@@ -1,25 +1,43 @@
|
|
|
1
|
-
import { useEffect, useState } from "react";
|
|
1
|
+
import { useEffect, useRef, useState } from "react";
|
|
2
2
|
|
|
3
3
|
import { useTransformContext } from "./use-transform-context";
|
|
4
4
|
import { getState } from "utils";
|
|
5
5
|
import { ReactZoomPanPinchContextState } from "../models";
|
|
6
|
+
import {
|
|
7
|
+
StateSnapshot,
|
|
8
|
+
isSameSnapshot,
|
|
9
|
+
takeSnapshot,
|
|
10
|
+
} from "./initial-layout.utils";
|
|
6
11
|
|
|
7
12
|
export function useTransformComponent<T>(
|
|
8
13
|
callback: (state: ReactZoomPanPinchContextState) => T,
|
|
9
14
|
): T {
|
|
10
15
|
const libraryContext = useTransformContext();
|
|
16
|
+
const lastSeen = useRef<StateSnapshot | null>(null);
|
|
11
17
|
|
|
12
|
-
const [transformRender, setTransformRender] = useState<T>(() =>
|
|
13
|
-
|
|
14
|
-
|
|
18
|
+
const [transformRender, setTransformRender] = useState<T>(() => {
|
|
19
|
+
lastSeen.current = takeSnapshot(libraryContext.state);
|
|
20
|
+
return callback(getState(libraryContext));
|
|
21
|
+
});
|
|
15
22
|
|
|
16
23
|
useEffect(() => {
|
|
17
24
|
let mounted = true;
|
|
18
25
|
const unmount = libraryContext.onChange((ref) => {
|
|
19
26
|
if (mounted) {
|
|
27
|
+
lastSeen.current = takeSnapshot(ref.instance.state);
|
|
20
28
|
setTransformRender(callback(getState(ref.instance)));
|
|
21
29
|
}
|
|
22
30
|
});
|
|
31
|
+
|
|
32
|
+
// The instance initializes in a layout effect, so a transform applied on
|
|
33
|
+
// mount (initialPosition*, centerOnInit, fitOnInit) can land before this
|
|
34
|
+
// subscription exists. Catch up when the state moved since the last
|
|
35
|
+
// render; the snapshot check keeps this from re-rendering in a loop.
|
|
36
|
+
if (!isSameSnapshot(lastSeen.current, libraryContext.state)) {
|
|
37
|
+
lastSeen.current = takeSnapshot(libraryContext.state);
|
|
38
|
+
setTransformRender(callback(getState(libraryContext)));
|
|
39
|
+
}
|
|
40
|
+
|
|
23
41
|
return () => {
|
|
24
42
|
unmount();
|
|
25
43
|
mounted = false;
|
|
@@ -1,19 +1,38 @@
|
|
|
1
|
-
import { useEffect } from "react";
|
|
1
|
+
import { useEffect, useRef } from "react";
|
|
2
2
|
|
|
3
3
|
import { getState } from "utils";
|
|
4
4
|
import { ReactZoomPanPinchContextState } from "../models";
|
|
5
5
|
import { useTransformContext } from "./use-transform-context";
|
|
6
|
+
import {
|
|
7
|
+
StateSnapshot,
|
|
8
|
+
isSameSnapshot,
|
|
9
|
+
takeSnapshot,
|
|
10
|
+
} from "./initial-layout.utils";
|
|
6
11
|
|
|
7
12
|
export const useTransformEffect = (
|
|
8
13
|
callback: (ref: ReactZoomPanPinchContextState) => void | (() => void),
|
|
9
14
|
): void => {
|
|
10
15
|
const libraryContext = useTransformContext();
|
|
16
|
+
// Starts as the state the consumer rendered with, so a transform applied
|
|
17
|
+
// during mount (initialPosition*, centerOnInit, fitOnInit) — which happens
|
|
18
|
+
// in a layout effect, before this subscription exists — is delivered once.
|
|
19
|
+
const lastSeen = useRef<StateSnapshot | null>(null);
|
|
20
|
+
if (lastSeen.current === null) {
|
|
21
|
+
lastSeen.current = takeSnapshot(libraryContext.state);
|
|
22
|
+
}
|
|
11
23
|
|
|
12
24
|
useEffect(() => {
|
|
13
25
|
let unmountCallback: void | (() => void);
|
|
14
26
|
const unmount = libraryContext.onChange((ref) => {
|
|
27
|
+
lastSeen.current = takeSnapshot(ref.instance.state);
|
|
15
28
|
unmountCallback = callback(getState(ref.instance));
|
|
16
29
|
});
|
|
30
|
+
|
|
31
|
+
if (!isSameSnapshot(lastSeen.current, libraryContext.state)) {
|
|
32
|
+
lastSeen.current = takeSnapshot(libraryContext.state);
|
|
33
|
+
unmountCallback = callback(getState(libraryContext));
|
|
34
|
+
}
|
|
35
|
+
|
|
17
36
|
return () => {
|
|
18
37
|
unmount();
|
|
19
38
|
unmountCallback?.();
|