react-zoom-pan-pinch 4.0.6 → 4.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +6 -1
- package/dist/index.cjs.js +486 -132
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.d.ts +68 -7
- package/dist/index.esm.js +487 -134
- package/dist/index.esm.js.map +1 -1
- package/package.json +1 -1
- package/src/components/keep-scale/keep-scale.tsx +13 -4
- package/src/components/mini-map/mini-map.tsx +10 -21
- package/src/components/transform-component/transform-component.tsx +8 -1
- package/src/components/transform-wrapper/transform-wrapper.tsx +14 -1
- package/src/components/virtualize/virtualize.tsx +32 -19
- package/src/core/animations/animations.utils.ts +50 -18
- package/src/core/double-click/double-click.logic.ts +5 -0
- package/src/core/handlers/handlers.logic.ts +160 -28
- package/src/core/handlers/handlers.utils.ts +182 -55
- package/src/core/instance.core.ts +51 -3
- package/src/core/pan/panning.logic.ts +10 -1
- package/src/core/pan/panning.utils.ts +6 -6
- package/src/core/wheel/wheel.logic.ts +13 -2
- package/src/core/wheel/wheel.utils.ts +4 -0
- package/src/hooks/index.ts +1 -0
- package/src/hooks/use-transform-component.tsx +1 -1
- package/src/hooks/use-zoom-pan-pinch.tsx +9 -3
- package/src/models/context.model.ts +21 -0
- package/src/utils/context.utils.ts +6 -0
- package/src/utils/effect.utils.ts +11 -0
- package/src/utils/helpers.utils.ts +37 -0
- package/src/utils/index.ts +1 -0
package/package.json
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import React, { useContext,
|
|
1
|
+
import React, { useContext, useRef } from "react";
|
|
2
2
|
|
|
3
3
|
import { mergeRefs } from "utils/ref.utils";
|
|
4
|
+
import { useIsomorphicLayoutEffect } from "utils/effect.utils";
|
|
4
5
|
import { Context } from "../transform-wrapper/transform-wrapper";
|
|
5
6
|
|
|
6
7
|
export const KeepScale = React.forwardRef<
|
|
@@ -10,17 +11,25 @@ export const KeepScale = React.forwardRef<
|
|
|
10
11
|
const localRef = useRef<HTMLDivElement>(null);
|
|
11
12
|
const instance = useContext(Context);
|
|
12
13
|
|
|
13
|
-
|
|
14
|
-
|
|
14
|
+
useIsomorphicLayoutEffect(() => {
|
|
15
|
+
const applyInverseScale = (scale: number) => {
|
|
15
16
|
if (localRef.current) {
|
|
16
17
|
const positionX = 0;
|
|
17
18
|
const positionY = 0;
|
|
18
19
|
localRef.current.style.transform = instance.handleTransformStyles(
|
|
19
20
|
positionX,
|
|
20
21
|
positionY,
|
|
21
|
-
1 /
|
|
22
|
+
1 / scale,
|
|
22
23
|
);
|
|
23
24
|
}
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
// Apply immediately: an element mounted while already zoomed must not
|
|
28
|
+
// wait for the next transform change to get its counter-scale.
|
|
29
|
+
applyInverseScale(instance.state.scale);
|
|
30
|
+
|
|
31
|
+
return instance.onChange((ctx) => {
|
|
32
|
+
applyInverseScale(ctx.instance.state.scale);
|
|
24
33
|
});
|
|
25
34
|
}, [instance]);
|
|
26
35
|
|
|
@@ -13,7 +13,6 @@ import {
|
|
|
13
13
|
useTransformInit,
|
|
14
14
|
} from "hooks";
|
|
15
15
|
import { useResize } from "./use-resize.hook";
|
|
16
|
-
import { ReactZoomPanPinchRef } from "models";
|
|
17
16
|
import { boundLimiter } from "core/bounds/bounds.utils";
|
|
18
17
|
|
|
19
18
|
export type MiniMapProps = {
|
|
@@ -59,7 +58,6 @@ export const MiniMap: React.FC<MiniMapProps> = ({
|
|
|
59
58
|
const [initialized, setInitialized] = useState(false);
|
|
60
59
|
const instance = useTransformContext();
|
|
61
60
|
const [isDown, setIsDown] = useState(false);
|
|
62
|
-
const miniMapInstance = useRef<ReactZoomPanPinchRef>(null);
|
|
63
61
|
|
|
64
62
|
const mainRef = useRef<HTMLDivElement | null>(null);
|
|
65
63
|
const wrapperRef = useRef<HTMLDivElement | null>(null);
|
|
@@ -178,19 +176,6 @@ export const MiniMap: React.FC<MiniMapProps> = ({
|
|
|
178
176
|
|
|
179
177
|
useResize(instance.contentComponent, initialize, [initialized]);
|
|
180
178
|
|
|
181
|
-
useEffect(() => {
|
|
182
|
-
return instance.onChange((zpp) => {
|
|
183
|
-
const scale = computeMiniMapScale();
|
|
184
|
-
if (miniMapInstance.current) {
|
|
185
|
-
miniMapInstance.current.instance.state.scale = zpp.instance.state.scale;
|
|
186
|
-
miniMapInstance.current.instance.state.positionX =
|
|
187
|
-
zpp.instance.state.positionX * scale;
|
|
188
|
-
miniMapInstance.current.instance.state.positionY =
|
|
189
|
-
zpp.instance.state.positionY * scale;
|
|
190
|
-
}
|
|
191
|
-
});
|
|
192
|
-
}, [computeMiniMapScale, instance, miniMapInstance]);
|
|
193
|
-
|
|
194
179
|
useEffect(() => {
|
|
195
180
|
const move = (e: MouseEvent) => {
|
|
196
181
|
if (
|
|
@@ -250,14 +235,18 @@ export const MiniMap: React.FC<MiniMapProps> = ({
|
|
|
250
235
|
const setUp = () => {
|
|
251
236
|
setIsDown(false);
|
|
252
237
|
};
|
|
253
|
-
document
|
|
254
|
-
|
|
255
|
-
|
|
238
|
+
// Listen on the mini map's own document so it keeps working inside
|
|
239
|
+
// portal windows and iframes.
|
|
240
|
+
const doc = mainRef.current?.ownerDocument ?? document;
|
|
241
|
+
doc.addEventListener("mousedown", setDown);
|
|
242
|
+
doc.addEventListener("mouseup", setUp);
|
|
243
|
+
doc.addEventListener("mousemove", move);
|
|
256
244
|
return () => {
|
|
257
|
-
|
|
258
|
-
|
|
245
|
+
doc.removeEventListener("mousedown", setDown);
|
|
246
|
+
doc.removeEventListener("mousemove", move);
|
|
247
|
+
doc.removeEventListener("mouseup", setUp);
|
|
259
248
|
};
|
|
260
|
-
});
|
|
249
|
+
}, [computeMiniMapScale, instance, isDown, panning]);
|
|
261
250
|
|
|
262
251
|
const wrapperStyle = useMemo(() => {
|
|
263
252
|
return {
|
|
@@ -4,6 +4,7 @@ import React, { useContext, useEffect, useRef } from "react";
|
|
|
4
4
|
import { Context } from "../transform-wrapper/transform-wrapper";
|
|
5
5
|
import { baseClasses } from "../../constants/state.constants";
|
|
6
6
|
import { getTransformStyles } from "../../utils/styles.utils";
|
|
7
|
+
import { useIsomorphicLayoutEffect } from "../../utils/effect.utils";
|
|
7
8
|
|
|
8
9
|
import styles from "./transform-component.module.css";
|
|
9
10
|
|
|
@@ -40,7 +41,10 @@ export const TransformComponent: React.FC<Props> = ({
|
|
|
40
41
|
const contentRef = useRef<HTMLDivElement | null>(null);
|
|
41
42
|
const gridRef = useRef<HTMLDivElement | null>(null);
|
|
42
43
|
|
|
43
|
-
|
|
44
|
+
// Layout effect: the initial transform (initialPosition*, centerOnInit)
|
|
45
|
+
// and the first visibility check of virtualized children happen before the
|
|
46
|
+
// browser paints, so nothing jumps into place a frame late.
|
|
47
|
+
useIsomorphicLayoutEffect(() => {
|
|
44
48
|
const wrapper = wrapperRef.current;
|
|
45
49
|
const content = contentRef.current;
|
|
46
50
|
if (wrapper !== null && content !== null && init) {
|
|
@@ -81,6 +85,9 @@ export const TransformComponent: React.FC<Props> = ({
|
|
|
81
85
|
ref={contentRef}
|
|
82
86
|
className={`${baseClasses.contentClass} ${styles.content} ${contentClass}`}
|
|
83
87
|
style={{
|
|
88
|
+
// Inline so the zoom anchor survives environments where the
|
|
89
|
+
// stylesheet is not applied, e.g. a shadow root (#371, #444).
|
|
90
|
+
transformOrigin: "0 0",
|
|
84
91
|
...contentStyle,
|
|
85
92
|
transform: getTransformStyles(
|
|
86
93
|
instance.state.positionX,
|
|
@@ -24,12 +24,25 @@ export const TransformWrapper = React.forwardRef(
|
|
|
24
24
|
props: Omit<ReactZoomPanPinchProps, "ref">,
|
|
25
25
|
ref: React.Ref<ReactZoomPanPinchContentRef>,
|
|
26
26
|
) => {
|
|
27
|
-
|
|
27
|
+
// Lazy so a fresh (immediately discarded) instance is not built on every
|
|
28
|
+
// render.
|
|
29
|
+
const instanceRef = useRef<ZoomPanPinch | null>(null);
|
|
30
|
+
if (instanceRef.current === null) {
|
|
31
|
+
instanceRef.current = new ZoomPanPinch(props);
|
|
32
|
+
}
|
|
33
|
+
const instance = instanceRef.current;
|
|
28
34
|
|
|
29
35
|
const content = getContent(props.children, getControls(instance));
|
|
30
36
|
|
|
31
37
|
useImperativeHandle(ref, () => getControls(instance), [instance]);
|
|
32
38
|
|
|
39
|
+
useEffect(() => {
|
|
40
|
+
instance.mount();
|
|
41
|
+
return () => {
|
|
42
|
+
instance.unmount();
|
|
43
|
+
};
|
|
44
|
+
}, [instance]);
|
|
45
|
+
|
|
33
46
|
useEffect(() => {
|
|
34
47
|
instance.update(props);
|
|
35
48
|
}, [instance, props]);
|
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import React, { useContext,
|
|
1
|
+
import React, { useContext, useRef, useState } from "react";
|
|
2
2
|
|
|
3
3
|
import { Context } from "../transform-wrapper/transform-wrapper";
|
|
4
|
+
import { useIsomorphicLayoutEffect } from "../../utils/effect.utils";
|
|
4
5
|
import { isElementVisible } from "./virtualize.utils";
|
|
5
6
|
|
|
6
7
|
/* eslint-disable react/require-default-props */
|
|
@@ -67,7 +68,30 @@ export const Virtualize = React.forwardRef<HTMLDivElement, VirtualizeProps>(
|
|
|
67
68
|
ref,
|
|
68
69
|
) => {
|
|
69
70
|
const instance = useContext(Context);
|
|
70
|
-
|
|
71
|
+
|
|
72
|
+
const computeVisible = () => {
|
|
73
|
+
const wrapper = instance.wrapperComponent;
|
|
74
|
+
if (!wrapper) return false;
|
|
75
|
+
|
|
76
|
+
return isElementVisible({
|
|
77
|
+
elementX: x,
|
|
78
|
+
elementY: y,
|
|
79
|
+
elementWidth: width,
|
|
80
|
+
elementHeight: height,
|
|
81
|
+
scale: instance.state.scale,
|
|
82
|
+
positionX: instance.state.positionX,
|
|
83
|
+
positionY: instance.state.positionY,
|
|
84
|
+
viewportWidth: wrapper.offsetWidth,
|
|
85
|
+
viewportHeight: wrapper.offsetHeight,
|
|
86
|
+
margin,
|
|
87
|
+
threshold,
|
|
88
|
+
});
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
// Elements mounted after the wrapper is initialized (items added to a
|
|
92
|
+
// canvas at runtime) render their children in the very first commit
|
|
93
|
+
// instead of one frame later.
|
|
94
|
+
const [visible, setVisible] = useState(computeVisible);
|
|
71
95
|
const visibleRef = useRef(false);
|
|
72
96
|
|
|
73
97
|
const onShowRef = useRef(onShow);
|
|
@@ -75,24 +99,13 @@ export const Virtualize = React.forwardRef<HTMLDivElement, VirtualizeProps>(
|
|
|
75
99
|
onShowRef.current = onShow;
|
|
76
100
|
onHideRef.current = onHide;
|
|
77
101
|
|
|
78
|
-
|
|
102
|
+
// Layout effect so the check triggered by the wrapper's init (also a
|
|
103
|
+
// layout effect) re-renders before the browser paints.
|
|
104
|
+
useIsomorphicLayoutEffect(() => {
|
|
79
105
|
const check = () => {
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
const nowVisible = isElementVisible({
|
|
84
|
-
elementX: x,
|
|
85
|
-
elementY: y,
|
|
86
|
-
elementWidth: width,
|
|
87
|
-
elementHeight: height,
|
|
88
|
-
scale: instance.state.scale,
|
|
89
|
-
positionX: instance.state.positionX,
|
|
90
|
-
positionY: instance.state.positionY,
|
|
91
|
-
viewportWidth: wrapper.offsetWidth,
|
|
92
|
-
viewportHeight: wrapper.offsetHeight,
|
|
93
|
-
margin,
|
|
94
|
-
threshold,
|
|
95
|
-
});
|
|
106
|
+
if (!instance.wrapperComponent) return;
|
|
107
|
+
|
|
108
|
+
const nowVisible = computeVisible();
|
|
96
109
|
|
|
97
110
|
if (nowVisible !== visibleRef.current) {
|
|
98
111
|
visibleRef.current = nowVisible;
|
|
@@ -6,21 +6,26 @@ import {
|
|
|
6
6
|
StateType,
|
|
7
7
|
} from "../../models";
|
|
8
8
|
|
|
9
|
-
const
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
9
|
+
const settleAnimation = (contextInstance: ReactZoomPanPinchContext): void => {
|
|
10
|
+
const resolve = contextInstance.animationResolve;
|
|
11
|
+
contextInstance.animationResolve = null;
|
|
12
|
+
resolve?.();
|
|
13
13
|
};
|
|
14
14
|
|
|
15
15
|
export const handleCancelAnimation = (
|
|
16
16
|
contextInstance: ReactZoomPanPinchContext,
|
|
17
17
|
): void => {
|
|
18
|
-
if (
|
|
19
|
-
|
|
18
|
+
if (contextInstance.animationFrame !== null) {
|
|
19
|
+
cancelAnimationFrame(contextInstance.animationFrame);
|
|
20
|
+
}
|
|
20
21
|
// Clear animation state
|
|
22
|
+
contextInstance.animationFrame = null;
|
|
21
23
|
contextInstance.isAnimating = false;
|
|
22
24
|
contextInstance.animation = null;
|
|
23
25
|
contextInstance.velocity = null;
|
|
26
|
+
// An interrupted animation still settles its promise so callers awaiting
|
|
27
|
+
// `zoomIn()` & co. are never left hanging (#214).
|
|
28
|
+
settleAnimation(contextInstance);
|
|
24
29
|
};
|
|
25
30
|
|
|
26
31
|
export function handleSetupAnimation(
|
|
@@ -28,8 +33,12 @@ export function handleSetupAnimation(
|
|
|
28
33
|
animationName: string,
|
|
29
34
|
animationTime: number,
|
|
30
35
|
callback: (step: number) => void,
|
|
36
|
+
onFinish?: () => void,
|
|
31
37
|
): void {
|
|
32
|
-
if (!contextInstance.mounted)
|
|
38
|
+
if (!contextInstance.mounted) {
|
|
39
|
+
onFinish?.();
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
33
42
|
const startTime = new Date().getTime();
|
|
34
43
|
const lastStep = 1;
|
|
35
44
|
|
|
@@ -37,9 +46,12 @@ export function handleSetupAnimation(
|
|
|
37
46
|
handleCancelAnimation(contextInstance);
|
|
38
47
|
|
|
39
48
|
// new animation
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
49
|
+
const animation: AnimationType = () => {
|
|
50
|
+
// A frame that was already queued when this animation got cancelled or
|
|
51
|
+
// replaced must not run: it would apply a stale target and re-schedule
|
|
52
|
+
// the replacement animation a second time per frame.
|
|
53
|
+
if (!contextInstance.mounted || contextInstance.animation !== animation) {
|
|
54
|
+
return;
|
|
43
55
|
}
|
|
44
56
|
|
|
45
57
|
const frameTime = new Date().getTime() - startTime;
|
|
@@ -49,15 +61,23 @@ export function handleSetupAnimation(
|
|
|
49
61
|
const step = animationType(animationProgress);
|
|
50
62
|
|
|
51
63
|
if (frameTime >= animationTime) {
|
|
52
|
-
callback
|
|
64
|
+
// Clear before the final step so a callback that starts a new
|
|
65
|
+
// animation (e.g. from onTransform) is not wiped out afterwards.
|
|
53
66
|
contextInstance.animation = null;
|
|
54
|
-
|
|
67
|
+
contextInstance.animationFrame = null;
|
|
68
|
+
const resolve = contextInstance.animationResolve;
|
|
69
|
+
contextInstance.animationResolve = null;
|
|
70
|
+
callback(lastStep);
|
|
71
|
+
resolve?.();
|
|
72
|
+
} else {
|
|
55
73
|
callback(step);
|
|
56
|
-
requestAnimationFrame(
|
|
74
|
+
contextInstance.animationFrame = requestAnimationFrame(animation);
|
|
57
75
|
}
|
|
58
76
|
};
|
|
59
77
|
|
|
60
|
-
|
|
78
|
+
contextInstance.animation = animation;
|
|
79
|
+
contextInstance.animationResolve = onFinish ?? null;
|
|
80
|
+
contextInstance.animationFrame = requestAnimationFrame(animation);
|
|
61
81
|
}
|
|
62
82
|
|
|
63
83
|
function isValidTargetState(targetState: StateType): boolean {
|
|
@@ -74,14 +94,22 @@ function isValidTargetState(targetState: StateType): boolean {
|
|
|
74
94
|
return true;
|
|
75
95
|
}
|
|
76
96
|
|
|
97
|
+
/**
|
|
98
|
+
* Animates the transform to `targetState`.
|
|
99
|
+
*
|
|
100
|
+
* Resolves once the final frame has been applied. It also resolves — never
|
|
101
|
+
* rejects — when the animation is interrupted by another animation or a user
|
|
102
|
+
* gesture, when the instance unmounts, or when the target is invalid, so the
|
|
103
|
+
* returned promise is always safe to await (#214).
|
|
104
|
+
*/
|
|
77
105
|
export function animate(
|
|
78
106
|
contextInstance: ReactZoomPanPinchContext,
|
|
79
107
|
targetState: StateType,
|
|
80
108
|
animationTime: number,
|
|
81
109
|
animationName: string,
|
|
82
|
-
): void {
|
|
110
|
+
): Promise<void> {
|
|
83
111
|
const isValid = isValidTargetState(targetState);
|
|
84
|
-
if (!contextInstance.mounted || !isValid) return;
|
|
112
|
+
if (!contextInstance.mounted || !isValid) return Promise.resolve();
|
|
85
113
|
|
|
86
114
|
const { setState } = contextInstance;
|
|
87
115
|
const { scale, positionX, positionY } = contextInstance.state;
|
|
@@ -92,7 +120,10 @@ export function animate(
|
|
|
92
120
|
|
|
93
121
|
if (animationTime === 0) {
|
|
94
122
|
setState(targetState.scale, targetState.positionX, targetState.positionY);
|
|
95
|
-
|
|
123
|
+
return Promise.resolve();
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
return new Promise<void>((resolve) => {
|
|
96
127
|
// animation start timestamp
|
|
97
128
|
handleSetupAnimation(
|
|
98
129
|
contextInstance,
|
|
@@ -110,6 +141,7 @@ export function animate(
|
|
|
110
141
|
|
|
111
142
|
setState(newScale, newPositionX, newPositionY);
|
|
112
143
|
},
|
|
144
|
+
resolve,
|
|
113
145
|
);
|
|
114
|
-
}
|
|
146
|
+
});
|
|
115
147
|
}
|
|
@@ -7,6 +7,7 @@ import {
|
|
|
7
7
|
cancelTimeout,
|
|
8
8
|
getContext,
|
|
9
9
|
handleCallback,
|
|
10
|
+
isEditableTarget,
|
|
10
11
|
isExcludedNode,
|
|
11
12
|
} from "../../utils";
|
|
12
13
|
import {
|
|
@@ -24,6 +25,7 @@ export const handleDoubleClickStop = (
|
|
|
24
25
|
cancelTimeout(contextInstance.doubleClickStopEventTimer);
|
|
25
26
|
contextInstance.doubleClickStopEventTimer = setTimeout(() => {
|
|
26
27
|
contextInstance.doubleClickStopEventTimer = null;
|
|
28
|
+
if (!contextInstance.mounted) return;
|
|
27
29
|
handleCallback(getContext(contextInstance), event, onZoomStop);
|
|
28
30
|
}, animationTime);
|
|
29
31
|
};
|
|
@@ -123,5 +125,8 @@ export const isDoubleClickAllowed = (
|
|
|
123
125
|
|
|
124
126
|
if (isExcluded) return false;
|
|
125
127
|
|
|
128
|
+
// Double-clicking a word inside an input selects it; it must not zoom.
|
|
129
|
+
if (isEditableTarget(target)) return false;
|
|
130
|
+
|
|
126
131
|
return true;
|
|
127
132
|
};
|
|
@@ -1,11 +1,21 @@
|
|
|
1
|
-
import { ReactZoomPanPinchContext } from "../../models";
|
|
2
1
|
import {
|
|
3
|
-
|
|
2
|
+
PositionType,
|
|
3
|
+
ReactZoomPanPinchContext,
|
|
4
|
+
ZoomToElementOptions,
|
|
5
|
+
ZoomToElementTarget,
|
|
6
|
+
} from "../../models";
|
|
7
|
+
import {
|
|
8
|
+
calculateZoomToRect,
|
|
9
|
+
clientToContentPoint,
|
|
10
|
+
contentToClientPoint,
|
|
11
|
+
getUnionRect,
|
|
4
12
|
handleZoomToViewCenter,
|
|
5
13
|
resetTransformations,
|
|
14
|
+
runZoomAnimation,
|
|
6
15
|
} from "./handlers.utils";
|
|
7
16
|
import { animations } from "../animations/animations.constants";
|
|
8
17
|
import { animate, handleCancelAnimation } from "../animations/animations.utils";
|
|
18
|
+
import { handleZoomToPoint } from "../zoom/zoom.logic";
|
|
9
19
|
import { getCenterPosition } from "../../utils";
|
|
10
20
|
|
|
11
21
|
export const zoomIn =
|
|
@@ -14,8 +24,8 @@ export const zoomIn =
|
|
|
14
24
|
step = 0.5,
|
|
15
25
|
animationTime = 300,
|
|
16
26
|
animationType: keyof typeof animations = "easeOut",
|
|
17
|
-
): void => {
|
|
18
|
-
handleZoomToViewCenter(
|
|
27
|
+
): Promise<void> => {
|
|
28
|
+
return handleZoomToViewCenter(
|
|
19
29
|
contextInstance,
|
|
20
30
|
1,
|
|
21
31
|
step,
|
|
@@ -30,8 +40,8 @@ export const zoomOut =
|
|
|
30
40
|
step = 0.5,
|
|
31
41
|
animationTime = 300,
|
|
32
42
|
animationType: keyof typeof animations = "easeOut",
|
|
33
|
-
): void => {
|
|
34
|
-
handleZoomToViewCenter(
|
|
43
|
+
): Promise<void> => {
|
|
44
|
+
return handleZoomToViewCenter(
|
|
35
45
|
contextInstance,
|
|
36
46
|
-1,
|
|
37
47
|
step,
|
|
@@ -48,12 +58,14 @@ export const setTransform =
|
|
|
48
58
|
newScale: number,
|
|
49
59
|
animationTime = 300,
|
|
50
60
|
animationType: keyof typeof animations = "easeOut",
|
|
51
|
-
): void => {
|
|
61
|
+
): Promise<void> => {
|
|
52
62
|
const { positionX, positionY, scale } = contextInstance.state;
|
|
53
63
|
const { wrapperComponent, contentComponent } = contextInstance;
|
|
54
64
|
const { disabled } = contextInstance.setup;
|
|
55
65
|
|
|
56
|
-
if (disabled || !wrapperComponent || !contentComponent)
|
|
66
|
+
if (disabled || !wrapperComponent || !contentComponent) {
|
|
67
|
+
return Promise.resolve();
|
|
68
|
+
}
|
|
57
69
|
|
|
58
70
|
const targetState = {
|
|
59
71
|
positionX: Number.isNaN(newPositionX) ? positionX : newPositionX,
|
|
@@ -61,7 +73,7 @@ export const setTransform =
|
|
|
61
73
|
scale: Number.isNaN(newScale) ? scale : newScale,
|
|
62
74
|
};
|
|
63
75
|
|
|
64
|
-
animate(contextInstance, targetState, animationTime, animationType);
|
|
76
|
+
return animate(contextInstance, targetState, animationTime, animationType);
|
|
65
77
|
};
|
|
66
78
|
|
|
67
79
|
export const resetTransform =
|
|
@@ -69,8 +81,8 @@ export const resetTransform =
|
|
|
69
81
|
(
|
|
70
82
|
animationTime = 200,
|
|
71
83
|
animationType: keyof typeof animations = "easeOut",
|
|
72
|
-
): void => {
|
|
73
|
-
resetTransformations(contextInstance, animationTime, animationType);
|
|
84
|
+
): Promise<void> => {
|
|
85
|
+
return resetTransformations(contextInstance, animationTime, animationType);
|
|
74
86
|
};
|
|
75
87
|
|
|
76
88
|
export const centerView =
|
|
@@ -79,7 +91,7 @@ export const centerView =
|
|
|
79
91
|
scale?: number,
|
|
80
92
|
animationTime = 200,
|
|
81
93
|
animationType: keyof typeof animations = "easeOut",
|
|
82
|
-
): void => {
|
|
94
|
+
): Promise<void> => {
|
|
83
95
|
const { state, wrapperComponent, contentComponent } = contextInstance;
|
|
84
96
|
if (wrapperComponent && contentComponent) {
|
|
85
97
|
const targetState = getCenterPosition(
|
|
@@ -88,35 +100,155 @@ export const centerView =
|
|
|
88
100
|
contentComponent,
|
|
89
101
|
);
|
|
90
102
|
|
|
91
|
-
animate(
|
|
103
|
+
return animate(
|
|
104
|
+
contextInstance,
|
|
105
|
+
targetState,
|
|
106
|
+
animationTime,
|
|
107
|
+
animationType,
|
|
108
|
+
);
|
|
109
|
+
}
|
|
110
|
+
return Promise.resolve();
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Zooms so that the content point currently under the client (viewport)
|
|
115
|
+
* coordinates `clientX`/`clientY` stays fixed on screen — the same anchoring a
|
|
116
|
+
* wheel zoom does at the cursor. Useful for click-to-zoom and focal zooms
|
|
117
|
+
* (#353).
|
|
118
|
+
*/
|
|
119
|
+
export const zoomToPoint =
|
|
120
|
+
(contextInstance: ReactZoomPanPinchContext) =>
|
|
121
|
+
(
|
|
122
|
+
scale: number,
|
|
123
|
+
clientX: number,
|
|
124
|
+
clientY: number,
|
|
125
|
+
animationTime = 300,
|
|
126
|
+
animationType: keyof typeof animations = "easeOut",
|
|
127
|
+
): Promise<void> => {
|
|
128
|
+
const { wrapperComponent, contentComponent, setup } = contextInstance;
|
|
129
|
+
if (setup.disabled || !wrapperComponent || !contentComponent) {
|
|
130
|
+
return Promise.resolve();
|
|
92
131
|
}
|
|
132
|
+
|
|
133
|
+
const point = clientToContentPoint(contextInstance, clientX, clientY);
|
|
134
|
+
const targetState = handleZoomToPoint(
|
|
135
|
+
contextInstance,
|
|
136
|
+
scale,
|
|
137
|
+
point.x,
|
|
138
|
+
point.y,
|
|
139
|
+
);
|
|
140
|
+
|
|
141
|
+
if (!targetState) return Promise.resolve();
|
|
142
|
+
|
|
143
|
+
const effectiveAnimationTime = setup.zoomAnimation.disabled
|
|
144
|
+
? 0
|
|
145
|
+
: animationTime;
|
|
146
|
+
|
|
147
|
+
return runZoomAnimation(
|
|
148
|
+
contextInstance,
|
|
149
|
+
targetState,
|
|
150
|
+
effectiveAnimationTime,
|
|
151
|
+
animationType,
|
|
152
|
+
);
|
|
93
153
|
};
|
|
94
154
|
|
|
155
|
+
/**
|
|
156
|
+
* Converts client (viewport) coordinates — e.g. `event.clientX/Y` — into
|
|
157
|
+
* content coordinates: the unscaled position inside `TransformComponent`
|
|
158
|
+
* regardless of the current pan and zoom (#378).
|
|
159
|
+
*/
|
|
160
|
+
export const clientToContent =
|
|
161
|
+
(contextInstance: ReactZoomPanPinchContext) =>
|
|
162
|
+
(clientX: number, clientY: number): PositionType => {
|
|
163
|
+
return clientToContentPoint(contextInstance, clientX, clientY);
|
|
164
|
+
};
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* Inverse of `clientToContent`: converts unscaled content coordinates into
|
|
168
|
+
* client (viewport) coordinates for the current transform.
|
|
169
|
+
*/
|
|
170
|
+
export const contentToClient =
|
|
171
|
+
(contextInstance: ReactZoomPanPinchContext) =>
|
|
172
|
+
(x: number, y: number): PositionType => {
|
|
173
|
+
return contentToClientPoint(contextInstance, x, y);
|
|
174
|
+
};
|
|
175
|
+
|
|
176
|
+
const resolveTargets = (
|
|
177
|
+
contextInstance: ReactZoomPanPinchContext,
|
|
178
|
+
node: ZoomToElementTarget | ZoomToElementTarget[],
|
|
179
|
+
): HTMLElement[] => {
|
|
180
|
+
const { wrapperComponent } = contextInstance;
|
|
181
|
+
if (!wrapperComponent) return [];
|
|
182
|
+
|
|
183
|
+
// Ids are resolved in the wrapper's own root so this works inside portal
|
|
184
|
+
// windows and iframes (#290) as well as shadow roots (#371).
|
|
185
|
+
const rootNode = wrapperComponent.getRootNode();
|
|
186
|
+
const root: { getElementById(id: string): Element | null } | null =
|
|
187
|
+
rootNode && "getElementById" in rootNode
|
|
188
|
+
? (rootNode as unknown as { getElementById(id: string): Element | null })
|
|
189
|
+
: wrapperComponent.ownerDocument ??
|
|
190
|
+
(typeof document !== "undefined" ? document : null);
|
|
191
|
+
|
|
192
|
+
const list = Array.isArray(node) ? node : [node];
|
|
193
|
+
|
|
194
|
+
return list
|
|
195
|
+
.map((item) =>
|
|
196
|
+
typeof item === "string"
|
|
197
|
+
? (root?.getElementById(item) as HTMLElement | null)
|
|
198
|
+
: item,
|
|
199
|
+
)
|
|
200
|
+
.filter(
|
|
201
|
+
(element): element is HTMLElement =>
|
|
202
|
+
!!element && wrapperComponent.contains(element),
|
|
203
|
+
);
|
|
204
|
+
};
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* Zooms and pans so the target element(s) fill the viewport.
|
|
208
|
+
*
|
|
209
|
+
* - `node` may be an element, an element id, or an array of either; with
|
|
210
|
+
* several targets the union of their boxes is framed (#388).
|
|
211
|
+
* - The second argument is either the target `scale` (legacy positional
|
|
212
|
+
* form) or an options object. `minScale`/`maxScale` cap the automatically
|
|
213
|
+
* computed fit scale (#515).
|
|
214
|
+
*/
|
|
95
215
|
export const zoomToElement =
|
|
96
216
|
(contextInstance: ReactZoomPanPinchContext) =>
|
|
97
217
|
(
|
|
98
|
-
node:
|
|
99
|
-
|
|
218
|
+
node: ZoomToElementTarget | ZoomToElementTarget[],
|
|
219
|
+
scaleOrOptions?: number | ZoomToElementOptions,
|
|
100
220
|
animationTime = 600,
|
|
101
221
|
animationType: keyof typeof animations = "easeOut",
|
|
102
222
|
offsetX = 0,
|
|
103
223
|
offsetY = 0,
|
|
104
|
-
): void => {
|
|
224
|
+
): Promise<void> => {
|
|
105
225
|
handleCancelAnimation(contextInstance);
|
|
106
226
|
|
|
107
|
-
const
|
|
227
|
+
const options: ZoomToElementOptions =
|
|
228
|
+
typeof scaleOrOptions === "object" && scaleOrOptions !== null
|
|
229
|
+
? scaleOrOptions
|
|
230
|
+
: { scale: scaleOrOptions };
|
|
108
231
|
|
|
109
|
-
const
|
|
110
|
-
typeof node === "string" ? document.getElementById(node) : node;
|
|
232
|
+
const targets = resolveTargets(contextInstance, node);
|
|
111
233
|
|
|
112
|
-
if (wrapperComponent
|
|
113
|
-
|
|
114
|
-
contextInstance,
|
|
115
|
-
target,
|
|
116
|
-
scale,
|
|
117
|
-
offsetX,
|
|
118
|
-
offsetY,
|
|
119
|
-
);
|
|
120
|
-
animate(contextInstance, targetState, animationTime, animationType);
|
|
234
|
+
if (!contextInstance.wrapperComponent || targets.length === 0) {
|
|
235
|
+
return Promise.resolve();
|
|
121
236
|
}
|
|
237
|
+
|
|
238
|
+
const targetState = calculateZoomToRect(
|
|
239
|
+
contextInstance,
|
|
240
|
+
getUnionRect(targets),
|
|
241
|
+
options.scale,
|
|
242
|
+
options.offsetX ?? offsetX,
|
|
243
|
+
options.offsetY ?? offsetY,
|
|
244
|
+
options.minScale,
|
|
245
|
+
options.maxScale,
|
|
246
|
+
);
|
|
247
|
+
|
|
248
|
+
return animate(
|
|
249
|
+
contextInstance,
|
|
250
|
+
targetState,
|
|
251
|
+
options.animationTime ?? animationTime,
|
|
252
|
+
options.animationType ?? animationType,
|
|
253
|
+
);
|
|
122
254
|
};
|