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.
@@ -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 { isExcludedNode } from "../../utils";
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
- contextInstance.setup.doubleClick;
57
+ setup.doubleClick;
17
58
 
18
59
  if (disabled) return;
60
+ if (doubleClickStopEventTimer) return;
19
61
 
20
62
  if (mode === "reset") {
21
- return resetTransformations(contextInstance, animationTime, animationType);
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.onChange?.(ctx);
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.contextInstance) {
11
+ if (!libraryContext) {
11
12
  throw new Error("Transform context mus be placed inside TransformWrapper");
12
13
  }
13
14
 
14
- return getContext(libraryContext.contextInstance);
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