react-zoom-pan-pinch 3.3.0 → 3.4.1

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.
Files changed (186) hide show
  1. package/dist/index.cjs.js +46 -3
  2. package/dist/index.cjs.js.map +1 -1
  3. package/dist/index.d.ts +4 -1
  4. package/dist/index.esm.js +46 -3
  5. package/dist/index.esm.js.map +1 -1
  6. package/package.json +4 -1
  7. package/.eslintignore +0 -6
  8. package/.github/CODEOWNERS +0 -36
  9. package/.github/FUNDING.yml +0 -13
  10. package/.github/ISSUE_TEMPLATE/bug_report.md +0 -38
  11. package/.github/ISSUE_TEMPLATE/feature_request.md +0 -20
  12. package/.github/workflows/docs.yml +0 -34
  13. package/.github/workflows/release.yml +0 -46
  14. package/.github/workflows/tests.yml +0 -42
  15. package/.prettierrc +0 -10
  16. package/.releaserc.json +0 -63
  17. package/.storybook/main.js +0 -23
  18. package/.storybook/preview-head.html +0 -5
  19. package/.storybook/preview.js +0 -11
  20. package/.storybook/theme-overrides.css +0 -49
  21. package/CHANGELOG.md +0 -26
  22. package/Code_of_Conduct.md +0 -61
  23. package/Contributing.md +0 -236
  24. package/__tests__/features/props.spec.tsx +0 -11
  25. package/__tests__/features/rendering.spec.tsx +0 -36
  26. package/__tests__/shared/render.shared.tsx +0 -41
  27. package/__tests__/utils/controls.utils.tsx +0 -25
  28. package/__tests__/utils/example.tsx +0 -81
  29. package/commitlint.config.js +0 -1
  30. package/dist/__tests__/features/props.spec.d.ts +0 -1
  31. package/dist/__tests__/features/rendering.spec.d.ts +0 -1
  32. package/dist/__tests__/shared/render.shared.d.ts +0 -11
  33. package/dist/__tests__/utils/controls.utils.d.ts +0 -3
  34. package/dist/__tests__/utils/example.d.ts +0 -6
  35. package/dist/src/components/index.d.ts +0 -4
  36. package/dist/src/components/keep-scale/keep-scale.d.ts +0 -2
  37. package/dist/src/components/mini-map/mini-map.d.ts +0 -8
  38. package/dist/src/components/mini-map/use-resize.hook.d.ts +0 -8
  39. package/dist/src/components/transform-component/transform-component.d.ts +0 -12
  40. package/dist/src/components/transform-wrapper/transform-wrapper.d.ts +0 -5
  41. package/dist/src/constants/state.constants.d.ts +0 -3
  42. package/dist/src/core/animations/animations.constants.d.ts +0 -16
  43. package/dist/src/core/animations/animations.utils.d.ts +0 -4
  44. package/dist/src/core/bounds/bounds.types.d.ts +0 -8
  45. package/dist/src/core/bounds/bounds.utils.d.ts +0 -17
  46. package/dist/src/core/double-click/double-click.logic.d.ts +0 -5
  47. package/dist/src/core/handlers/handlers.logic.d.ts +0 -8
  48. package/dist/src/core/handlers/handlers.utils.d.ts +0 -14
  49. package/dist/src/core/instance.core.d.ts +0 -77
  50. package/dist/src/core/pan/panning.logic.d.ts +0 -5
  51. package/dist/src/core/pan/panning.utils.d.ts +0 -10
  52. package/dist/src/core/pan/velocity.logic.d.ts +0 -5
  53. package/dist/src/core/pan/velocity.utils.d.ts +0 -5
  54. package/dist/src/core/pinch/pinch.logic.d.ts +0 -4
  55. package/dist/src/core/pinch/pinch.utils.d.ts +0 -6
  56. package/dist/src/core/wheel/wheel.logic.d.ts +0 -4
  57. package/dist/src/core/wheel/wheel.utils.d.ts +0 -7
  58. package/dist/src/core/zoom/zoom.logic.d.ts +0 -3
  59. package/dist/src/core/zoom/zoom.utils.d.ts +0 -3
  60. package/dist/src/hooks/index.d.ts +0 -4
  61. package/dist/src/hooks/use-controls.d.ts +0 -2
  62. package/dist/src/hooks/use-transform-context.d.ts +0 -1
  63. package/dist/src/hooks/use-transform-effect.d.ts +0 -2
  64. package/dist/src/hooks/use-transform-init.d.ts +0 -2
  65. package/dist/src/index.d.ts +0 -5
  66. package/dist/src/models/calculations.model.d.ts +0 -25
  67. package/dist/src/models/context.model.d.ts +0 -127
  68. package/dist/src/models/helpers.model.d.ts +0 -5
  69. package/dist/src/models/index.d.ts +0 -3
  70. package/dist/src/stories/docs/handlers.d.ts +0 -12
  71. package/dist/src/stories/docs/props.d.ts +0 -14
  72. package/dist/src/stories/examples/content-rerendering/example.d.ts +0 -2
  73. package/dist/src/stories/examples/image-responsive/example.d.ts +0 -2
  74. package/dist/src/stories/examples/keep-scale/example.d.ts +0 -2
  75. package/dist/src/stories/examples/mini-map/example.d.ts +0 -1
  76. package/dist/src/stories/examples/stress/example.d.ts +0 -2
  77. package/dist/src/stories/examples/svg-zoom-to-element/example.d.ts +0 -2
  78. package/dist/src/stories/examples/zoom-to-element/example.d.ts +0 -2
  79. package/dist/src/stories/types/args.types.d.ts +0 -280
  80. package/dist/src/stories/utils/args.utils.d.ts +0 -3
  81. package/dist/src/stories/utils/code.utils.d.ts +0 -6
  82. package/dist/src/stories/utils/controls.utils.d.ts +0 -3
  83. package/dist/src/stories/utils/index.d.ts +0 -3
  84. package/dist/src/utils/calculations.utils.d.ts +0 -11
  85. package/dist/src/utils/callback.utils.d.ts +0 -2
  86. package/dist/src/utils/context.utils.d.ts +0 -4
  87. package/dist/src/utils/event.utils.d.ts +0 -1
  88. package/dist/src/utils/helpers.utils.d.ts +0 -2
  89. package/dist/src/utils/index.d.ts +0 -8
  90. package/dist/src/utils/ref.utils.d.ts +0 -2
  91. package/dist/src/utils/state.utils.d.ts +0 -3
  92. package/dist/src/utils/styles.utils.d.ts +0 -4
  93. package/jest.config.ts +0 -45
  94. package/rollup.config.js +0 -51
  95. package/src/.eslintrc.json +0 -90
  96. package/src/components/index.ts +0 -4
  97. package/src/components/keep-scale/keep-scale.tsx +0 -28
  98. package/src/components/mini-map/mini-map.tsx +0 -188
  99. package/src/components/mini-map/use-resize.hook.ts +0 -69
  100. package/src/components/transform-component/transform-component.module.css +0 -30
  101. package/src/components/transform-component/transform-component.tsx +0 -61
  102. package/src/components/transform-wrapper/transform-wrapper.tsx +0 -39
  103. package/src/constants/state.constants.ts +0 -77
  104. package/src/core/animations/animations.constants.ts +0 -78
  105. package/src/core/animations/animations.utils.ts +0 -114
  106. package/src/core/bounds/bounds.types.ts +0 -8
  107. package/src/core/bounds/bounds.utils.ts +0 -187
  108. package/src/core/double-click/double-click.logic.ts +0 -116
  109. package/src/core/handlers/handlers.logic.ts +0 -115
  110. package/src/core/handlers/handlers.utils.ts +0 -195
  111. package/src/core/instance.core.ts +0 -503
  112. package/src/core/pan/panning.logic.ts +0 -100
  113. package/src/core/pan/panning.utils.ts +0 -196
  114. package/src/core/pan/velocity.logic.ts +0 -149
  115. package/src/core/pan/velocity.utils.ts +0 -79
  116. package/src/core/pinch/pinch.logic.ts +0 -81
  117. package/src/core/pinch/pinch.utils.ts +0 -88
  118. package/src/core/wheel/wheel.logic.ts +0 -121
  119. package/src/core/wheel/wheel.utils.ts +0 -122
  120. package/src/core/zoom/zoom.logic.ts +0 -69
  121. package/src/core/zoom/zoom.utils.ts +0 -58
  122. package/src/hooks/index.ts +0 -4
  123. package/src/hooks/use-controls.tsx +0 -9
  124. package/src/hooks/use-transform-context.tsx +0 -13
  125. package/src/hooks/use-transform-effect.tsx +0 -22
  126. package/src/hooks/use-transform-init.tsx +0 -27
  127. package/src/index.ts +0 -6
  128. package/src/models/calculations.model.ts +0 -26
  129. package/src/models/context.model.ts +0 -199
  130. package/src/models/helpers.model.ts +0 -15
  131. package/src/models/index.ts +0 -3
  132. package/src/react-app-env.d.ts +0 -1
  133. package/src/stories/assets/big-image.jpeg +0 -0
  134. package/src/stories/assets/map.jpg +0 -0
  135. package/src/stories/assets/medium-image.jpg +0 -0
  136. package/src/stories/assets/pin.svg +0 -1
  137. package/src/stories/assets/small-image.jpg +0 -0
  138. package/src/stories/assets/svg-example.svg +0 -14
  139. package/src/stories/docs/Introduction.stories.mdx +0 -174
  140. package/src/stories/docs/handlers.stories.mdx +0 -48
  141. package/src/stories/docs/handlers.tsx +0 -107
  142. package/src/stories/docs/props.stories.mdx +0 -92
  143. package/src/stories/docs/props.tsx +0 -526
  144. package/src/stories/examples/content/content.stories.mdx +0 -121
  145. package/src/stories/examples/content-rerendering/content-updating.stories.mdx +0 -45
  146. package/src/stories/examples/content-rerendering/example.tsx +0 -73
  147. package/src/stories/examples/controls/controls.stories.mdx +0 -71
  148. package/src/stories/examples/image-big/image-big.stories.mdx +0 -43
  149. package/src/stories/examples/image-medium/image-medium.stories.mdx +0 -41
  150. package/src/stories/examples/image-responsive/example.tsx +0 -103
  151. package/src/stories/examples/image-responsive/image-responsive.stories.mdx +0 -32
  152. package/src/stories/examples/image-small/image-small.stories.mdx +0 -41
  153. package/src/stories/examples/image-zoomed-out/image-zoomed-out.stories.mdx +0 -46
  154. package/src/stories/examples/keep-scale/example.tsx +0 -92
  155. package/src/stories/examples/keep-scale/keep-scale.stories.mdx +0 -45
  156. package/src/stories/examples/mini-map/example.tsx +0 -54
  157. package/src/stories/examples/mini-map/mini-map.stories.mdx +0 -33
  158. package/src/stories/examples/stress/example.tsx +0 -23
  159. package/src/stories/examples/stress/stress-test.stories.mdx +0 -28
  160. package/src/stories/examples/svg-zoom-to-element/creativity.svg +0 -143
  161. package/src/stories/examples/svg-zoom-to-element/example.tsx +0 -57
  162. package/src/stories/examples/svg-zoom-to-element/svg-zoom-to-element.stories.mdx +0 -28
  163. package/src/stories/examples/zoom-to-element/example.tsx +0 -94
  164. package/src/stories/examples/zoom-to-element/zoom-to-element.stories.mdx +0 -28
  165. package/src/stories/hooks/use-controls.stories.mdx +0 -24
  166. package/src/stories/hooks/use-transform-context.stories.mdx +0 -15
  167. package/src/stories/hooks/use-transform-effect.stories.mdx +0 -29
  168. package/src/stories/hooks/use-transform-init.stories.mdx +0 -25
  169. package/src/stories/types/args.types.ts +0 -211
  170. package/src/stories/utils/args.utils.ts +0 -34
  171. package/src/stories/utils/code.utils.tsx +0 -24
  172. package/src/stories/utils/controls.utils.tsx +0 -43
  173. package/src/stories/utils/index.ts +0 -3
  174. package/src/stories/utils/styles.module.css +0 -34
  175. package/src/styles.css +0 -8
  176. package/src/typings.d.ts +0 -10
  177. package/src/utils/calculations.utils.ts +0 -16
  178. package/src/utils/callback.utils.ts +0 -11
  179. package/src/utils/context.utils.ts +0 -48
  180. package/src/utils/event.utils.ts +0 -22
  181. package/src/utils/helpers.utils.ts +0 -27
  182. package/src/utils/index.ts +0 -8
  183. package/src/utils/ref.utils.ts +0 -16
  184. package/src/utils/state.utils.ts +0 -40
  185. package/src/utils/styles.utils.ts +0 -44
  186. package/tsconfig.json +0 -33
@@ -1,503 +0,0 @@
1
- import {
2
- BoundsType,
3
- LibrarySetup,
4
- PositionType,
5
- VelocityType,
6
- AnimationType,
7
- ReactZoomPanPinchProps,
8
- ReactZoomPanPinchState,
9
- ReactZoomPanPinchRef,
10
- } from "../models";
11
- import {
12
- getContext,
13
- createSetup,
14
- createState,
15
- handleCallback,
16
- getTransformStyles,
17
- makePassiveEventOption,
18
- getCenterPosition,
19
- } from "../utils";
20
- import { handleCancelAnimation } from "./animations/animations.utils";
21
- import { isWheelAllowed } from "./wheel/wheel.utils";
22
- import { isPinchAllowed, isPinchStartAllowed } from "./pinch/pinch.utils";
23
- import { handleCalculateBounds } from "./bounds/bounds.utils";
24
- import {
25
- handleWheelStart,
26
- handleWheelZoom,
27
- handleWheelStop,
28
- } from "./wheel/wheel.logic";
29
- import { isPanningAllowed, isPanningStartAllowed } from "./pan/panning.utils";
30
- import {
31
- handlePanning,
32
- handlePanningEnd,
33
- handlePanningStart,
34
- } from "./pan/panning.logic";
35
- import {
36
- handlePinchStart,
37
- handlePinchStop,
38
- handlePinchZoom,
39
- } from "./pinch/pinch.logic";
40
- import {
41
- handleDoubleClick,
42
- isDoubleClickAllowed,
43
- } from "./double-click/double-click.logic";
44
-
45
- type StartCoordsType = { x: number; y: number } | null;
46
-
47
- export class ZoomPanPinch {
48
- public props: ReactZoomPanPinchProps;
49
-
50
- public mounted = true;
51
-
52
- public transformState: ReactZoomPanPinchState;
53
- public setup: LibrarySetup;
54
- public observer?: ResizeObserver;
55
- public onChangeCallbacks: Set<(ctx: ReactZoomPanPinchRef) => void> =
56
- new Set();
57
- public onInitCallbacks: Set<(ctx: ReactZoomPanPinchRef) => void> = new Set();
58
-
59
- // Components
60
- public wrapperComponent: HTMLDivElement | null = null;
61
- public contentComponent: HTMLDivElement | null = null;
62
- // Initialization
63
- public isInitialized = false;
64
- public bounds: BoundsType | null = null;
65
- // wheel helpers
66
- public previousWheelEvent: WheelEvent | null = null;
67
- public wheelStopEventTimer: ReturnType<typeof setTimeout> | null = null;
68
- public wheelAnimationTimer: ReturnType<typeof setTimeout> | null = null;
69
- // panning helpers
70
- public isPanning = false;
71
- public startCoords: StartCoordsType = null;
72
- public lastTouch: number | null = null;
73
- // pinch helpers
74
- public distance: null | number = null;
75
- public lastDistance: null | number = null;
76
- public pinchStartDistance: null | number = null;
77
- public pinchStartScale: null | number = null;
78
- public pinchMidpoint: null | PositionType = null;
79
- // double click helpers
80
- public doubleClickStopEventTimer: ReturnType<typeof setTimeout> | null = null;
81
- // velocity helpers
82
- public velocity: VelocityType | null = null;
83
- public velocityTime: number | null = null;
84
- public lastMousePosition: PositionType | null = null;
85
- // animations helpers
86
- public animate = false;
87
- public animation: AnimationType | null = null;
88
- public maxBounds: BoundsType | null = null;
89
- // key press
90
- public pressedKeys: { [key: string]: boolean } = {};
91
-
92
- constructor(props: ReactZoomPanPinchProps) {
93
- this.props = props;
94
- this.setup = createSetup(this.props);
95
- this.transformState = createState(this.props);
96
- }
97
-
98
- mount = () => {
99
- this.initializeWindowEvents();
100
- };
101
-
102
- unmount = () => {
103
- this.cleanupWindowEvents();
104
- };
105
-
106
- update = (newProps: ReactZoomPanPinchProps) => {
107
- handleCalculateBounds(this, this.transformState.scale);
108
- this.setup = createSetup(newProps);
109
- };
110
-
111
- initializeWindowEvents = (): void => {
112
- const passive = makePassiveEventOption();
113
- const currentDocument = this.wrapperComponent?.ownerDocument;
114
- const currentWindow = currentDocument?.defaultView;
115
- // Panning on window to allow panning when mouse is out of component wrapper
116
- currentWindow?.addEventListener("mousedown", this.onPanningStart, passive);
117
- currentWindow?.addEventListener("mousemove", this.onPanning, passive);
118
- currentWindow?.addEventListener("mouseup", this.onPanningStop, passive);
119
- currentDocument?.addEventListener("mouseleave", this.clearPanning, passive);
120
- currentWindow?.addEventListener("keyup", this.setKeyUnPressed, passive);
121
- currentWindow?.addEventListener("keydown", this.setKeyPressed, passive);
122
- };
123
-
124
- cleanupWindowEvents = (): void => {
125
- const passive = makePassiveEventOption();
126
- const currentDocument = this.wrapperComponent?.ownerDocument;
127
- const currentWindow = currentDocument?.defaultView;
128
- currentWindow?.removeEventListener(
129
- "mousedown",
130
- this.onPanningStart,
131
- passive,
132
- );
133
- currentWindow?.removeEventListener("mousemove", this.onPanning, passive);
134
- currentWindow?.removeEventListener("mouseup", this.onPanningStop, passive);
135
- currentDocument?.removeEventListener(
136
- "mouseleave",
137
- this.clearPanning,
138
- passive,
139
- );
140
- currentWindow?.removeEventListener("keyup", this.setKeyUnPressed, passive);
141
- currentWindow?.removeEventListener("keydown", this.setKeyPressed, passive);
142
- document.removeEventListener("mouseleave", this.clearPanning, passive);
143
-
144
- handleCancelAnimation(this);
145
- this.observer?.disconnect();
146
- };
147
-
148
- handleInitializeWrapperEvents = (wrapper: HTMLDivElement): void => {
149
- // Zooming events on wrapper
150
- const passive = makePassiveEventOption();
151
-
152
- wrapper.addEventListener("wheel", this.onWheelZoom, passive);
153
- wrapper.addEventListener("dblclick", this.onDoubleClick, passive);
154
- wrapper.addEventListener("touchstart", this.onTouchPanningStart, passive);
155
- wrapper.addEventListener("touchmove", this.onTouchPanning, passive);
156
- wrapper.addEventListener("touchend", this.onTouchPanningStop, passive);
157
- };
158
-
159
- handleInitialize = (contentComponent: HTMLDivElement): void => {
160
- const { centerOnInit } = this.setup;
161
- this.applyTransformation();
162
- this.onInitCallbacks.forEach((callback) => callback(getContext(this)));
163
-
164
- if (centerOnInit) {
165
- this.setCenter();
166
- this.observer = new ResizeObserver(() => {
167
- this.onInitCallbacks.forEach((callback) => callback(getContext(this)));
168
- this.setCenter();
169
- this.observer?.disconnect();
170
- });
171
-
172
- // Start observing the target node for configured mutations
173
- this.observer.observe(contentComponent);
174
- }
175
- };
176
-
177
- /// ///////
178
- // Zoom
179
- /// ///////
180
-
181
- onWheelZoom = (event: WheelEvent): void => {
182
- const { disabled } = this.setup;
183
- if (disabled) return;
184
-
185
- const isAllowed = isWheelAllowed(this, event);
186
- if (!isAllowed) return;
187
-
188
- const keysPressed = this.isPressingKeys(this.setup.wheel.activationKeys);
189
- if (!keysPressed) return;
190
-
191
- handleWheelStart(this, event);
192
- handleWheelZoom(this, event);
193
- handleWheelStop(this, event);
194
- };
195
-
196
- /// ///////
197
- // Pan
198
- /// ///////
199
-
200
- onPanningStart = (event: MouseEvent): void => {
201
- const { disabled } = this.setup;
202
- const { onPanningStart } = this.props;
203
- if (disabled) return;
204
-
205
- const isAllowed = isPanningStartAllowed(this, event);
206
- if (!isAllowed) return;
207
-
208
- const keysPressed = this.isPressingKeys(this.setup.panning.activationKeys);
209
- if (!keysPressed) return;
210
-
211
- if (event.button === 0 && !this.setup.panning.allowLeftClickPan) return;
212
- if (event.button === 1 && !this.setup.panning.allowMiddleClickPan) return;
213
- if (event.button === 2 && !this.setup.panning.allowRightClickPan) return;
214
-
215
- event.preventDefault();
216
- event.stopPropagation();
217
-
218
- handleCancelAnimation(this);
219
- handlePanningStart(this, event);
220
- handleCallback(getContext(this), event, onPanningStart);
221
- };
222
-
223
- onPanning = (event: MouseEvent): void => {
224
- const { disabled } = this.setup;
225
- const { onPanning } = this.props;
226
-
227
- if (disabled) return;
228
-
229
- const isAllowed = isPanningAllowed(this);
230
- if (!isAllowed) return;
231
-
232
- const keysPressed = this.isPressingKeys(this.setup.panning.activationKeys);
233
- if (!keysPressed) return;
234
-
235
- event.preventDefault();
236
- event.stopPropagation();
237
-
238
- handlePanning(this, event.clientX, event.clientY);
239
- handleCallback(getContext(this), event, onPanning);
240
- };
241
-
242
- onPanningStop = (event: MouseEvent | TouchEvent): void => {
243
- const { onPanningStop } = this.props;
244
-
245
- if (this.isPanning) {
246
- handlePanningEnd(this);
247
- handleCallback(getContext(this), event, onPanningStop);
248
- }
249
- };
250
-
251
- /// ///////
252
- // Pinch
253
- /// ///////
254
-
255
- onPinchStart = (event: TouchEvent): void => {
256
- const { disabled } = this.setup;
257
- const { onPinchingStart, onZoomStart } = this.props;
258
-
259
- if (disabled) return;
260
-
261
- const isAllowed = isPinchStartAllowed(this, event);
262
- if (!isAllowed) return;
263
-
264
- handlePinchStart(this, event);
265
- handleCancelAnimation(this);
266
- handleCallback(getContext(this), event, onPinchingStart);
267
- handleCallback(getContext(this), event, onZoomStart);
268
- };
269
-
270
- onPinch = (event: TouchEvent): void => {
271
- const { disabled } = this.setup;
272
- const { onPinching, onZoom } = this.props;
273
-
274
- if (disabled) return;
275
-
276
- const isAllowed = isPinchAllowed(this);
277
- if (!isAllowed) return;
278
-
279
- event.preventDefault();
280
- event.stopPropagation();
281
-
282
- handlePinchZoom(this, event);
283
- handleCallback(getContext(this), event, onPinching);
284
- handleCallback(getContext(this), event, onZoom);
285
- };
286
-
287
- onPinchStop = (event: TouchEvent): void => {
288
- const { onPinchingStop, onZoomStop } = this.props;
289
-
290
- if (this.pinchStartScale) {
291
- handlePinchStop(this);
292
- handleCallback(getContext(this), event, onPinchingStop);
293
- handleCallback(getContext(this), event, onZoomStop);
294
- }
295
- };
296
-
297
- /// ///////
298
- // Touch
299
- /// ///////
300
-
301
- onTouchPanningStart = (event: TouchEvent): void => {
302
- const { disabled } = this.setup;
303
- const { onPanningStart } = this.props;
304
-
305
- if (disabled) return;
306
-
307
- const isAllowed = isPanningStartAllowed(this, event);
308
-
309
- if (!isAllowed) return;
310
-
311
- const isDoubleTap = this.lastTouch && +new Date() - this.lastTouch < 200;
312
-
313
- if (isDoubleTap && event.touches.length === 1) {
314
- this.onDoubleClick(event);
315
- } else {
316
- this.lastTouch = +new Date();
317
-
318
- handleCancelAnimation(this);
319
-
320
- const { touches } = event;
321
-
322
- const isPanningAction = touches.length === 1;
323
- const isPinchAction = touches.length === 2;
324
-
325
- if (isPanningAction) {
326
- handleCancelAnimation(this);
327
- handlePanningStart(this, event);
328
- handleCallback(getContext(this), event, onPanningStart);
329
- }
330
- if (isPinchAction) {
331
- this.onPinchStart(event);
332
- }
333
- }
334
- };
335
-
336
- onTouchPanning = (event: TouchEvent): void => {
337
- const { disabled } = this.setup;
338
- const { onPanning } = this.props;
339
-
340
- if (this.isPanning && event.touches.length === 1) {
341
- if (disabled) return;
342
-
343
- const isAllowed = isPanningAllowed(this);
344
- if (!isAllowed) return;
345
-
346
- event.preventDefault();
347
- event.stopPropagation();
348
-
349
- const touch = event.touches[0];
350
- handlePanning(this, touch.clientX, touch.clientY);
351
- handleCallback(getContext(this), event, onPanning);
352
- } else if (event.touches.length > 1) {
353
- this.onPinch(event);
354
- }
355
- };
356
-
357
- onTouchPanningStop = (event: TouchEvent): void => {
358
- this.onPanningStop(event);
359
- this.onPinchStop(event);
360
- };
361
-
362
- /// ///////
363
- // Double Click
364
- /// ///////
365
-
366
- onDoubleClick = (event: MouseEvent | TouchEvent): void => {
367
- const { disabled } = this.setup;
368
- if (disabled) return;
369
-
370
- const isAllowed = isDoubleClickAllowed(this, event);
371
- if (!isAllowed) return;
372
-
373
- handleDoubleClick(this, event);
374
- };
375
-
376
- /// ///////
377
- // Helpers
378
- /// ///////
379
-
380
- clearPanning = (event: MouseEvent): void => {
381
- if (this.isPanning) {
382
- this.onPanningStop(event);
383
- }
384
- };
385
-
386
- setKeyPressed = (e: KeyboardEvent): void => {
387
- this.pressedKeys[e.key] = true;
388
- };
389
-
390
- setKeyUnPressed = (e: KeyboardEvent): void => {
391
- this.pressedKeys[e.key] = false;
392
- };
393
-
394
- isPressingKeys = (keys: string[]): boolean => {
395
- if (!keys.length) {
396
- return true;
397
- }
398
- return Boolean(keys.find((key) => this.pressedKeys[key]));
399
- };
400
-
401
- setTransformState = (
402
- scale: number,
403
- positionX: number,
404
- positionY: number,
405
- ): void => {
406
- const { onTransformed } = this.props;
407
-
408
- if (
409
- !Number.isNaN(scale) &&
410
- !Number.isNaN(positionX) &&
411
- !Number.isNaN(positionY)
412
- ) {
413
- if (scale !== this.transformState.scale) {
414
- this.transformState.previousScale = this.transformState.scale;
415
- this.transformState.scale = scale;
416
- }
417
- this.transformState.positionX = positionX;
418
- this.transformState.positionY = positionY;
419
-
420
- this.applyTransformation();
421
- const ctx = getContext(this);
422
- this.onChangeCallbacks.forEach((callback) => callback(ctx));
423
- handleCallback(ctx, { scale, positionX, positionY }, onTransformed);
424
- } else {
425
- console.error("Detected NaN set state values");
426
- }
427
- };
428
-
429
- setCenter = (): void => {
430
- if (this.wrapperComponent && this.contentComponent) {
431
- const targetState = getCenterPosition(
432
- this.transformState.scale,
433
- this.wrapperComponent,
434
- this.contentComponent,
435
- );
436
- this.setTransformState(
437
- targetState.scale,
438
- targetState.positionX,
439
- targetState.positionY,
440
- );
441
- }
442
- };
443
-
444
- handleTransformStyles = (x: number, y: number, scale: number) => {
445
- if (this.props.customTransform) {
446
- return this.props.customTransform(x, y, scale);
447
- }
448
- return getTransformStyles(x, y, scale);
449
- };
450
-
451
- applyTransformation = (): void => {
452
- if (!this.mounted || !this.contentComponent) return;
453
- const { scale, positionX, positionY } = this.transformState;
454
- const transform = this.handleTransformStyles(positionX, positionY, scale);
455
- this.contentComponent.style.transform = transform;
456
- };
457
-
458
- getContext = () => {
459
- return getContext(this);
460
- };
461
-
462
- /**
463
- * Hooks
464
- */
465
-
466
- onChange = (callback: (ref: ReactZoomPanPinchRef) => void) => {
467
- if (!this.onChangeCallbacks.has(callback)) {
468
- this.onChangeCallbacks.add(callback);
469
- }
470
- return () => {
471
- this.onChangeCallbacks.delete(callback);
472
- };
473
- };
474
-
475
- onInit = (callback: (ref: ReactZoomPanPinchRef) => void) => {
476
- if (!this.onInitCallbacks.has(callback)) {
477
- this.onInitCallbacks.add(callback);
478
- }
479
- return () => {
480
- this.onInitCallbacks.delete(callback);
481
- };
482
- };
483
-
484
- /**
485
- * Initialization
486
- */
487
-
488
- init = (
489
- wrapperComponent: HTMLDivElement,
490
- contentComponent: HTMLDivElement,
491
- ): void => {
492
- this.cleanupWindowEvents();
493
- this.wrapperComponent = wrapperComponent;
494
- this.contentComponent = contentComponent;
495
- handleCalculateBounds(this, this.transformState.scale);
496
- this.handleInitializeWrapperEvents(wrapperComponent);
497
- this.handleInitialize(contentComponent);
498
- this.initializeWindowEvents();
499
- this.isInitialized = true;
500
- const ctx = getContext(this);
501
- handleCallback(ctx, undefined, this.props.onInit);
502
- };
503
- }
@@ -1,100 +0,0 @@
1
- /* eslint-disable no-param-reassign */
2
- import { ReactZoomPanPinchContext } from "../../models/context.model";
3
- import { animate, handleCancelAnimation } from "../animations/animations.utils";
4
- import { handleCalculateBounds } from "../bounds/bounds.utils";
5
- import {
6
- getPaddingValue,
7
- getPanningClientPosition,
8
- handleNewPosition,
9
- handlePanningSetup,
10
- handlePanToBounds,
11
- handleTouchPanningSetup,
12
- } from "./panning.utils";
13
- import {
14
- handleVelocityPanning,
15
- handleCalculateVelocity,
16
- } from "./velocity.logic";
17
-
18
- export function handlePanningStart(
19
- contextInstance: ReactZoomPanPinchContext,
20
- event: MouseEvent | TouchEvent,
21
- ): void {
22
- const { scale } = contextInstance.transformState;
23
-
24
- handleCancelAnimation(contextInstance);
25
- handleCalculateBounds(contextInstance, scale);
26
- if (window.TouchEvent !== undefined && event instanceof TouchEvent) {
27
- handleTouchPanningSetup(contextInstance, event as TouchEvent);
28
- } else {
29
- handlePanningSetup(contextInstance, event as MouseEvent);
30
- }
31
- }
32
-
33
- export function handleAlignToBounds(
34
- contextInstance: ReactZoomPanPinchContext,
35
- ): void {
36
- const { scale } = contextInstance.transformState;
37
- const { minScale, alignmentAnimation } = contextInstance.setup;
38
- const { disabled, sizeX, sizeY, animationTime, animationType } =
39
- alignmentAnimation;
40
-
41
- const isDisabled = disabled || scale < minScale || (!sizeX && !sizeY);
42
-
43
- if (isDisabled) return;
44
-
45
- const targetState = handlePanToBounds(contextInstance);
46
-
47
- if (targetState) {
48
- animate(contextInstance, targetState, animationTime, animationType);
49
- }
50
- }
51
-
52
- export function handlePanning(
53
- contextInstance: ReactZoomPanPinchContext,
54
- clientX: number,
55
- clientY: number,
56
- ): void {
57
- const { startCoords, setup } = contextInstance;
58
- const { sizeX, sizeY } = setup.alignmentAnimation;
59
-
60
- if (!startCoords) return;
61
-
62
- const { x, y } = getPanningClientPosition(contextInstance, clientX, clientY);
63
- const paddingValueX = getPaddingValue(contextInstance, sizeX);
64
- const paddingValueY = getPaddingValue(contextInstance, sizeY);
65
-
66
- handleCalculateVelocity(contextInstance, { x, y });
67
- handleNewPosition(contextInstance, x, y, paddingValueX, paddingValueY);
68
- }
69
-
70
- export function handlePanningEnd(
71
- contextInstance: ReactZoomPanPinchContext,
72
- ): void {
73
- if (contextInstance.isPanning) {
74
- const { velocityDisabled } = contextInstance.setup.panning;
75
- const { velocity, wrapperComponent, contentComponent } = contextInstance;
76
-
77
- contextInstance.isPanning = false;
78
- contextInstance.animate = false;
79
- contextInstance.animation = null;
80
-
81
- const wrapperRect = wrapperComponent?.getBoundingClientRect();
82
- const contentRect = contentComponent?.getBoundingClientRect();
83
-
84
- const wrapperWidth = wrapperRect?.width || 0;
85
- const wrapperHeight = wrapperRect?.height || 0;
86
- const contentWidth = contentRect?.width || 0;
87
- const contentHeight = contentRect?.height || 0;
88
- const isZoomed =
89
- wrapperWidth < contentWidth || wrapperHeight < contentHeight;
90
-
91
- const shouldAnimate =
92
- !velocityDisabled && velocity && velocity?.total > 0.1 && isZoomed;
93
-
94
- if (shouldAnimate) {
95
- handleVelocityPanning(contextInstance);
96
- } else {
97
- handleAlignToBounds(contextInstance);
98
- }
99
- }
100
- }