react-zoom-pan-pinch 2.3.2 → 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-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 +4 -2
- package/dist/index.cjs.js +112 -72
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.d.ts +70 -69
- package/dist/index.esm.js +113 -73
- package/dist/index.esm.js.map +1 -1
- package/dist/models/context.model.d.ts +0 -1
- package/package.json +1 -1
- package/src/components/transform-component.tsx +3 -3
- package/src/components/transform-wrapper.tsx +25 -26
- package/src/constants/state.constants.ts +1 -16
- 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 +15 -5
- package/src/hooks/use-transform-context.tsx +10 -3
- package/src/models/context.model.ts +0 -1
- package/src/stories/docs/props.tsx +0 -6
- 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
|
|
|
@@ -52,6 +52,8 @@ export class ZoomPanPinch {
|
|
|
52
52
|
public transformState: ReactZoomPanPinchState;
|
|
53
53
|
public setup: LibrarySetup;
|
|
54
54
|
public observer?: ResizeObserver;
|
|
55
|
+
public onChangeCallbacks: Set<(ctx: ReactZoomPanPinchRef) => void> =
|
|
56
|
+
new Set();
|
|
55
57
|
|
|
56
58
|
// Components
|
|
57
59
|
public wrapperComponent: HTMLDivElement | null = null;
|
|
@@ -73,6 +75,8 @@ export class ZoomPanPinch {
|
|
|
73
75
|
public pinchStartDistance: null | number = null;
|
|
74
76
|
public pinchStartScale: null | number = null;
|
|
75
77
|
public pinchMidpoint: null | PositionType = null;
|
|
78
|
+
// double click helpers
|
|
79
|
+
public doubleClickStopEventTimer: ReturnType<typeof setTimeout> | null = null;
|
|
76
80
|
// velocity helpers
|
|
77
81
|
public velocity: VelocityType | null = null;
|
|
78
82
|
public velocityTime: number | null = null;
|
|
@@ -84,10 +88,7 @@ export class ZoomPanPinch {
|
|
|
84
88
|
// key press
|
|
85
89
|
public pressedKeys: { [key: string]: boolean } = {};
|
|
86
90
|
|
|
87
|
-
constructor(
|
|
88
|
-
props: ReactZoomPanPinchProps,
|
|
89
|
-
private onChange?: (ctx: ReactZoomPanPinchRef) => void,
|
|
90
|
-
) {
|
|
91
|
+
constructor(props: ReactZoomPanPinchProps) {
|
|
91
92
|
this.props = props;
|
|
92
93
|
this.setup = createSetup(this.props);
|
|
93
94
|
this.transformState = createState(this.props);
|
|
@@ -410,7 +411,7 @@ export class ZoomPanPinch {
|
|
|
410
411
|
this.transformState.positionY = positionY;
|
|
411
412
|
|
|
412
413
|
const ctx = getContext(this);
|
|
413
|
-
this.
|
|
414
|
+
this.onChangeCallbacks.forEach((callback) => callback(ctx));
|
|
414
415
|
|
|
415
416
|
handleCallback(ctx, { scale, positionX, positionY }, onTransformed);
|
|
416
417
|
this.applyTransformation();
|
|
@@ -445,6 +446,15 @@ export class ZoomPanPinch {
|
|
|
445
446
|
return getContext(this);
|
|
446
447
|
};
|
|
447
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
|
+
|
|
448
458
|
/**
|
|
449
459
|
* Initialization
|
|
450
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;
|
|
@@ -42,7 +42,6 @@ export type ReactZoomPanPinchRefProps = {
|
|
|
42
42
|
export type ReactZoomPanPinchProps = {
|
|
43
43
|
children?: React.ReactNode | ((ref: ReactZoomPanPinchRef) => React.ReactNode);
|
|
44
44
|
ref?: React.Ref<ReactZoomPanPinchRef>;
|
|
45
|
-
performance?: boolean;
|
|
46
45
|
initialScale?: number;
|
|
47
46
|
initialPositionX?: number;
|
|
48
47
|
initialPositionY?: number;
|
|
@@ -101,12 +101,6 @@ export const wrapperPropsTable: ComponentProps = {
|
|
|
101
101
|
description:
|
|
102
102
|
"This is 'global' disable props. Setting it as a 'true' value blocks all functionalities of library. For disabling parts of it like panning, zooming, pinch - checkout the dedicated sections below.",
|
|
103
103
|
},
|
|
104
|
-
performance: {
|
|
105
|
-
type: ["boolean"],
|
|
106
|
-
defaultValue: String(initialSetup.performance),
|
|
107
|
-
description:
|
|
108
|
-
"Enable this prop to switch to limited rendering performance mode",
|
|
109
|
-
},
|
|
110
104
|
minPositionX: {
|
|
111
105
|
type: ["number"],
|
|
112
106
|
defaultValue: String(initialSetup.minPositionX),
|
|
@@ -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
|