react-zoom-pan-pinch 2.3.1 → 2.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/dist/components/transform-component.d.ts +2 -0
- package/dist/components/transform-wrapper.d.ts +1 -3
- package/dist/constants/state.constants.d.ts +1 -4
- package/dist/core/double-click/double-click.logic.d.ts +2 -0
- package/dist/core/handlers/handlers.utils.d.ts +1 -1
- package/dist/core/instance.core.d.ts +5 -2
- package/dist/index.cjs.js +114 -64
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.d.ts +72 -67
- package/dist/index.esm.js +115 -65
- package/dist/index.esm.js.map +1 -1
- package/package.json +3 -2
- package/src/components/transform-component.tsx +10 -3
- package/src/components/transform-wrapper.tsx +29 -17
- package/src/constants/state.constants.ts +1 -15
- package/src/core/animations/animations.utils.ts +1 -0
- package/src/core/double-click/double-click.logic.ts +55 -8
- package/src/core/handlers/handlers.utils.ts +2 -0
- package/src/core/instance.core.ts +18 -5
- package/src/hooks/use-transform-context.tsx +10 -3
- package/src/stories/examples/controls/controls.stories.mdx +1 -0
|
@@ -1,34 +1,79 @@
|
|
|
1
|
+
/* eslint-disable no-param-reassign */
|
|
1
2
|
import { ReactZoomPanPinchContext } from "../../models";
|
|
2
3
|
import { animate } from "../animations/animations.utils";
|
|
3
4
|
import { getMousePosition } from "../wheel/wheel.utils";
|
|
4
5
|
import { handleZoomToPoint } from "../zoom/zoom.logic";
|
|
5
|
-
import {
|
|
6
|
+
import {
|
|
7
|
+
cancelTimeout,
|
|
8
|
+
getContext,
|
|
9
|
+
handleCallback,
|
|
10
|
+
isExcludedNode,
|
|
11
|
+
} from "../../utils";
|
|
6
12
|
import {
|
|
7
13
|
handleCalculateButtonZoom,
|
|
8
14
|
resetTransformations,
|
|
9
15
|
} from "../handlers/handlers.utils";
|
|
10
16
|
|
|
17
|
+
export const handleDoubleClickStop = (
|
|
18
|
+
contextInstance: ReactZoomPanPinchContext,
|
|
19
|
+
event: MouseEvent | TouchEvent,
|
|
20
|
+
): void => {
|
|
21
|
+
const { onZoomStop } = contextInstance.props;
|
|
22
|
+
const { animationTime } = contextInstance.setup.doubleClick;
|
|
23
|
+
|
|
24
|
+
cancelTimeout(contextInstance.doubleClickStopEventTimer);
|
|
25
|
+
contextInstance.doubleClickStopEventTimer = setTimeout(() => {
|
|
26
|
+
contextInstance.doubleClickStopEventTimer = null;
|
|
27
|
+
handleCallback(getContext(contextInstance), event, onZoomStop);
|
|
28
|
+
}, animationTime);
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
export const handleDoubleClickResetMode = (
|
|
32
|
+
contextInstance: ReactZoomPanPinchContext,
|
|
33
|
+
event: MouseEvent | TouchEvent,
|
|
34
|
+
) => {
|
|
35
|
+
const { onZoomStart, onZoom } = contextInstance.props;
|
|
36
|
+
const { animationTime, animationType } = contextInstance.setup.doubleClick;
|
|
37
|
+
|
|
38
|
+
handleCallback(getContext(contextInstance), event, onZoomStart);
|
|
39
|
+
|
|
40
|
+
resetTransformations(contextInstance, animationTime, animationType, () =>
|
|
41
|
+
handleCallback(getContext(contextInstance), event, onZoom),
|
|
42
|
+
);
|
|
43
|
+
|
|
44
|
+
handleDoubleClickStop(contextInstance, event);
|
|
45
|
+
};
|
|
46
|
+
|
|
11
47
|
export function handleDoubleClick(
|
|
12
48
|
contextInstance: ReactZoomPanPinchContext,
|
|
13
49
|
event: MouseEvent | TouchEvent,
|
|
14
50
|
): void {
|
|
51
|
+
const { setup, doubleClickStopEventTimer, transformState, contentComponent } =
|
|
52
|
+
contextInstance;
|
|
53
|
+
|
|
54
|
+
const { scale } = transformState;
|
|
55
|
+
const { onZoomStart, onZoom } = contextInstance.props;
|
|
15
56
|
const { disabled, mode, step, animationTime, animationType } =
|
|
16
|
-
|
|
57
|
+
setup.doubleClick;
|
|
17
58
|
|
|
18
59
|
if (disabled) return;
|
|
60
|
+
if (doubleClickStopEventTimer) return;
|
|
19
61
|
|
|
20
62
|
if (mode === "reset") {
|
|
21
|
-
return
|
|
63
|
+
return handleDoubleClickResetMode(contextInstance, event);
|
|
22
64
|
}
|
|
23
65
|
|
|
24
|
-
const { scale } = contextInstance.transformState;
|
|
25
|
-
const { contentComponent } = contextInstance;
|
|
26
|
-
|
|
27
66
|
if (!contentComponent) return console.error("No ContentComponent found");
|
|
28
67
|
|
|
29
68
|
const delta = mode === "zoomOut" ? -1 : 1;
|
|
30
69
|
|
|
31
70
|
const newScale = handleCalculateButtonZoom(contextInstance, delta, step);
|
|
71
|
+
|
|
72
|
+
// stop execution when scale didn't change
|
|
73
|
+
if (scale === newScale) return;
|
|
74
|
+
|
|
75
|
+
handleCallback(getContext(contextInstance), event, onZoomStart);
|
|
76
|
+
|
|
32
77
|
const mousePosition = getMousePosition(event, contentComponent, scale);
|
|
33
78
|
const targetState = handleZoomToPoint(
|
|
34
79
|
contextInstance,
|
|
@@ -43,7 +88,11 @@ export function handleDoubleClick(
|
|
|
43
88
|
);
|
|
44
89
|
}
|
|
45
90
|
|
|
91
|
+
handleCallback(getContext(contextInstance), event, onZoom);
|
|
92
|
+
|
|
46
93
|
animate(contextInstance, targetState, animationTime, animationType);
|
|
94
|
+
|
|
95
|
+
handleDoubleClickStop(contextInstance, event);
|
|
47
96
|
}
|
|
48
97
|
|
|
49
98
|
export const isDoubleClickAllowed = (
|
|
@@ -63,7 +112,5 @@ export const isDoubleClickAllowed = (
|
|
|
63
112
|
|
|
64
113
|
if (isExcluded) return false;
|
|
65
114
|
|
|
66
|
-
if (!isAllowed) return false;
|
|
67
|
-
|
|
68
115
|
return true;
|
|
69
116
|
};
|
|
@@ -77,6 +77,7 @@ export function resetTransformations(
|
|
|
77
77
|
contextInstance: ReactZoomPanPinchContext,
|
|
78
78
|
animationTime: number,
|
|
79
79
|
animationType: keyof typeof animations,
|
|
80
|
+
onResetTransformation?: () => void,
|
|
80
81
|
): void {
|
|
81
82
|
const { setup, wrapperComponent } = contextInstance;
|
|
82
83
|
const { limitToBounds } = setup;
|
|
@@ -114,6 +115,7 @@ export function resetTransformations(
|
|
|
114
115
|
return;
|
|
115
116
|
}
|
|
116
117
|
|
|
118
|
+
onResetTransformation?.();
|
|
117
119
|
animate(contextInstance, newState, animationTime, animationType);
|
|
118
120
|
}
|
|
119
121
|
|
|
@@ -6,6 +6,7 @@ import {
|
|
|
6
6
|
AnimationType,
|
|
7
7
|
ReactZoomPanPinchProps,
|
|
8
8
|
ReactZoomPanPinchState,
|
|
9
|
+
ReactZoomPanPinchRef,
|
|
9
10
|
} from "../models";
|
|
10
11
|
import {
|
|
11
12
|
getContext,
|
|
@@ -51,6 +52,8 @@ export class ZoomPanPinch {
|
|
|
51
52
|
public transformState: ReactZoomPanPinchState;
|
|
52
53
|
public setup: LibrarySetup;
|
|
53
54
|
public observer?: ResizeObserver;
|
|
55
|
+
public onChangeCallbacks: Set<(ctx: ReactZoomPanPinchRef) => void> =
|
|
56
|
+
new Set();
|
|
54
57
|
|
|
55
58
|
// Components
|
|
56
59
|
public wrapperComponent: HTMLDivElement | null = null;
|
|
@@ -72,6 +75,8 @@ export class ZoomPanPinch {
|
|
|
72
75
|
public pinchStartDistance: null | number = null;
|
|
73
76
|
public pinchStartScale: null | number = null;
|
|
74
77
|
public pinchMidpoint: null | PositionType = null;
|
|
78
|
+
// double click helpers
|
|
79
|
+
public doubleClickStopEventTimer: ReturnType<typeof setTimeout> | null = null;
|
|
75
80
|
// velocity helpers
|
|
76
81
|
public velocity: VelocityType | null = null;
|
|
77
82
|
public velocityTime: number | null = null;
|
|
@@ -405,11 +410,10 @@ export class ZoomPanPinch {
|
|
|
405
410
|
this.transformState.positionX = positionX;
|
|
406
411
|
this.transformState.positionY = positionY;
|
|
407
412
|
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
);
|
|
413
|
+
const ctx = getContext(this);
|
|
414
|
+
this.onChangeCallbacks.forEach((callback) => callback(ctx));
|
|
415
|
+
|
|
416
|
+
handleCallback(ctx, { scale, positionX, positionY }, onTransformed);
|
|
413
417
|
this.applyTransformation();
|
|
414
418
|
} else {
|
|
415
419
|
console.error("Detected NaN set state values");
|
|
@@ -442,6 +446,15 @@ export class ZoomPanPinch {
|
|
|
442
446
|
return getContext(this);
|
|
443
447
|
};
|
|
444
448
|
|
|
449
|
+
onChange = (callback: (ref: ReactZoomPanPinchRef) => void) => {
|
|
450
|
+
if (!this.onChangeCallbacks.has(callback)) {
|
|
451
|
+
this.onChangeCallbacks.add(callback);
|
|
452
|
+
}
|
|
453
|
+
return () => {
|
|
454
|
+
this.onChangeCallbacks.delete(callback);
|
|
455
|
+
};
|
|
456
|
+
};
|
|
457
|
+
|
|
445
458
|
/**
|
|
446
459
|
* Initialization
|
|
447
460
|
*/
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { useContext } from "react";
|
|
1
|
+
import { useContext, useEffect, useState } from "react";
|
|
2
2
|
|
|
3
3
|
import { getContext } from "../utils";
|
|
4
4
|
import { Context } from "../components/transform-wrapper";
|
|
@@ -6,12 +6,19 @@ import { ReactZoomPanPinchRef } from "../models";
|
|
|
6
6
|
|
|
7
7
|
const useTransformContext = (): ReactZoomPanPinchRef => {
|
|
8
8
|
const libraryContext = useContext(Context);
|
|
9
|
+
const [values, setValues] = useState(getContext(libraryContext || {}));
|
|
9
10
|
|
|
10
|
-
if (!libraryContext
|
|
11
|
+
if (!libraryContext) {
|
|
11
12
|
throw new Error("Transform context mus be placed inside TransformWrapper");
|
|
12
13
|
}
|
|
13
14
|
|
|
14
|
-
|
|
15
|
+
useEffect(() => {
|
|
16
|
+
libraryContext.onChange((ref) => {
|
|
17
|
+
setValues(ref);
|
|
18
|
+
});
|
|
19
|
+
}, [libraryContext]);
|
|
20
|
+
|
|
21
|
+
return values;
|
|
15
22
|
};
|
|
16
23
|
|
|
17
24
|
export default useTransformContext;
|
|
@@ -17,6 +17,7 @@ export const Template = (args) => (
|
|
|
17
17
|
wrapperStyle={{ maxWidth: "100%", maxHeight: "calc(100vh - 50px)" }}
|
|
18
18
|
>
|
|
19
19
|
<div style={{ background: "#444", color: "white", padding: "50px" }}>
|
|
20
|
+
{JSON.stringify(utils.state, null, 4)}
|
|
20
21
|
<h1>No scrollbars needed</h1>
|
|
21
22
|
<h2>Use mouse or gestures to move around</h2>
|
|
22
23
|
<button
|