react-zoom-pan-pinch 2.2.1 → 2.3.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.
- package/dist/components/transform-component.d.ts +10 -0
- package/dist/components/transform-wrapper.d.ts +8 -0
- package/dist/constants/state.constants.d.ts +6 -0
- package/dist/core/animations/animations.constants.d.ts +16 -0
- package/dist/core/animations/animations.utils.d.ts +4 -0
- package/dist/core/bounds/bounds.types.d.ts +8 -0
- package/dist/core/bounds/bounds.utils.d.ts +15 -0
- package/dist/core/double-click/double-click.logic.d.ts +3 -0
- package/dist/core/handlers/handlers.logic.d.ts +8 -0
- package/dist/core/handlers/handlers.utils.d.ts +13 -0
- package/dist/core/instance.core.d.ts +68 -0
- package/dist/core/pan/panning.logic.d.ts +5 -0
- package/dist/core/pan/panning.utils.d.ts +10 -0
- package/dist/core/pan/velocity.logic.d.ts +5 -0
- package/dist/core/pan/velocity.utils.d.ts +5 -0
- package/dist/core/pinch/pinch.logic.d.ts +4 -0
- package/dist/core/pinch/pinch.utils.d.ts +6 -0
- package/dist/core/wheel/wheel.logic.d.ts +4 -0
- package/dist/core/wheel/wheel.utils.d.ts +7 -0
- package/dist/core/zoom/zoom.logic.d.ts +3 -0
- package/dist/core/zoom/zoom.utils.d.ts +3 -0
- package/dist/hooks/use-transform-context.d.ts +3 -0
- package/dist/index.cjs.js +273 -267
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.d.ts +63 -57
- package/dist/index.esm.js +273 -267
- package/dist/index.esm.js.map +1 -1
- package/dist/models/calculations.model.d.ts +25 -0
- package/dist/models/context.model.d.ts +117 -0
- package/dist/models/helpers.model.d.ts +5 -0
- package/dist/models/index.d.ts +3 -0
- package/dist/stories/docs/handlers.d.ts +12 -0
- package/dist/stories/docs/props.d.ts +14 -0
- package/dist/stories/examples/content-rerendering/example.d.ts +2 -0
- package/dist/stories/examples/stress/example.d.ts +2 -0
- package/dist/stories/examples/zoom-to-element/example.d.ts +2 -0
- package/dist/stories/types/args.types.d.ts +268 -0
- package/dist/stories/utils/args.utils.d.ts +3 -0
- package/dist/stories/utils/code.utils.d.ts +6 -0
- package/dist/stories/utils/controls.utils.d.ts +3 -0
- package/dist/stories/utils/index.d.ts +3 -0
- package/dist/utils/calculations.utils.d.ts +11 -0
- package/dist/utils/callback.utils.d.ts +2 -0
- package/dist/utils/context.utils.d.ts +2 -0
- package/dist/utils/event.utils.d.ts +1 -0
- package/dist/utils/helpers.utils.d.ts +2 -0
- package/dist/utils/index.d.ts +7 -0
- package/dist/utils/state.utils.d.ts +3 -0
- package/dist/utils/styles.utils.d.ts +3 -0
- package/package.json +30 -25
- package/src/.eslintrc.json +90 -0
- package/src/components/transform-component.tsx +2 -1
- package/src/components/transform-wrapper.tsx +31 -5
- package/src/constants/state.constants.ts +0 -2
- package/src/core/animations/animations.constants.ts +14 -12
- package/src/core/animations/animations.utils.ts +15 -11
- package/src/core/bounds/bounds.utils.ts +20 -20
- package/src/core/double-click/double-click.logic.ts +6 -12
- package/src/core/handlers/handlers.logic.ts +98 -89
- package/src/core/handlers/handlers.utils.ts +22 -24
- package/src/{components/transform-context.tsx → core/instance.core.ts} +87 -117
- package/src/core/pan/panning.logic.ts +20 -19
- package/src/core/pan/panning.utils.ts +4 -7
- package/src/core/pan/velocity.logic.ts +1 -0
- package/src/core/pinch/pinch.logic.ts +4 -6
- package/src/core/pinch/pinch.utils.ts +2 -2
- package/src/core/wheel/wheel.logic.ts +1 -0
- package/src/core/wheel/wheel.utils.ts +12 -11
- package/src/core/zoom/zoom.logic.ts +29 -29
- package/src/core/zoom/zoom.utils.ts +2 -2
- package/src/hooks/use-transform-context.tsx +3 -2
- package/src/models/context.model.ts +6 -2
- package/src/stories/docs/handlers.tsx +30 -30
- package/src/stories/examples/content-rerendering/content-updating.stories.mdx +3 -0
- package/src/stories/examples/stress/example.tsx +24 -0
- package/src/stories/examples/stress/stress-test.stories.mdx +28 -0
- package/src/stories/examples/zoom-to-element/example.tsx +4 -0
- package/src/stories/utils/code.utils.tsx +1 -0
- package/src/stories/utils/controls.utils.tsx +21 -4
- package/src/stories/utils/styles.module.css +5 -0
- package/src/.eslintrc.js +0 -36
|
@@ -1,16 +1,12 @@
|
|
|
1
|
-
import React, { Component } from "react";
|
|
2
|
-
|
|
3
1
|
import {
|
|
4
2
|
BoundsType,
|
|
5
3
|
LibrarySetup,
|
|
6
4
|
PositionType,
|
|
7
5
|
VelocityType,
|
|
8
6
|
AnimationType,
|
|
9
|
-
ReactZoomPanPinchRef,
|
|
10
7
|
ReactZoomPanPinchProps,
|
|
11
8
|
ReactZoomPanPinchState,
|
|
12
9
|
} from "../models";
|
|
13
|
-
|
|
14
10
|
import {
|
|
15
11
|
getContext,
|
|
16
12
|
createSetup,
|
|
@@ -20,52 +16,41 @@ import {
|
|
|
20
16
|
makePassiveEventOption,
|
|
21
17
|
getCenterPosition,
|
|
22
18
|
} from "../utils";
|
|
23
|
-
|
|
24
|
-
import {
|
|
25
|
-
|
|
26
|
-
import {
|
|
27
|
-
import { isWheelAllowed } from "../core/wheel/wheel.utils";
|
|
28
|
-
import { isPinchAllowed, isPinchStartAllowed } from "../core/pinch/pinch.utils";
|
|
29
|
-
import { handleCalculateBounds } from "../core/bounds/bounds.utils";
|
|
30
|
-
|
|
19
|
+
import { handleCancelAnimation } from "./animations/animations.utils";
|
|
20
|
+
import { isWheelAllowed } from "./wheel/wheel.utils";
|
|
21
|
+
import { isPinchAllowed, isPinchStartAllowed } from "./pinch/pinch.utils";
|
|
22
|
+
import { handleCalculateBounds } from "./bounds/bounds.utils";
|
|
31
23
|
import {
|
|
32
24
|
handleWheelStart,
|
|
33
25
|
handleWheelZoom,
|
|
34
26
|
handleWheelStop,
|
|
35
|
-
} from "
|
|
36
|
-
import {
|
|
37
|
-
isPanningAllowed,
|
|
38
|
-
isPanningStartAllowed,
|
|
39
|
-
} from "../core/pan/panning.utils";
|
|
27
|
+
} from "./wheel/wheel.logic";
|
|
28
|
+
import { isPanningAllowed, isPanningStartAllowed } from "./pan/panning.utils";
|
|
40
29
|
import {
|
|
41
30
|
handlePanning,
|
|
42
31
|
handlePanningEnd,
|
|
43
32
|
handlePanningStart,
|
|
44
|
-
} from "
|
|
33
|
+
} from "./pan/panning.logic";
|
|
45
34
|
import {
|
|
46
35
|
handlePinchStart,
|
|
47
36
|
handlePinchStop,
|
|
48
37
|
handlePinchZoom,
|
|
49
|
-
} from "
|
|
38
|
+
} from "./pinch/pinch.logic";
|
|
50
39
|
import {
|
|
51
40
|
handleDoubleClick,
|
|
52
41
|
isDoubleClickAllowed,
|
|
53
|
-
} from "
|
|
42
|
+
} from "./double-click/double-click.logic";
|
|
54
43
|
|
|
55
44
|
type StartCoordsType = { x: number; y: number } | null;
|
|
56
45
|
|
|
57
|
-
|
|
46
|
+
export class ZoomPanPinch {
|
|
47
|
+
public props: ReactZoomPanPinchProps;
|
|
58
48
|
|
|
59
|
-
class TransformContext extends Component<
|
|
60
|
-
Omit<ReactZoomPanPinchProps, "ref"> & {
|
|
61
|
-
setRef: (context: ReactZoomPanPinchRef) => void;
|
|
62
|
-
}
|
|
63
|
-
> {
|
|
64
49
|
public mounted = true;
|
|
65
50
|
|
|
66
|
-
public transformState: ReactZoomPanPinchState
|
|
67
|
-
|
|
68
|
-
public
|
|
51
|
+
public transformState: ReactZoomPanPinchState;
|
|
52
|
+
public setup: LibrarySetup;
|
|
53
|
+
public observer?: ResizeObserver;
|
|
69
54
|
|
|
70
55
|
// Components
|
|
71
56
|
public wrapperComponent: HTMLDivElement | null = null;
|
|
@@ -98,20 +83,24 @@ class TransformContext extends Component<
|
|
|
98
83
|
// key press
|
|
99
84
|
public pressedKeys: { [key: string]: boolean } = {};
|
|
100
85
|
|
|
101
|
-
|
|
102
|
-
this.
|
|
86
|
+
constructor(props: ReactZoomPanPinchProps) {
|
|
87
|
+
this.props = props;
|
|
88
|
+
this.setup = createSetup(this.props);
|
|
89
|
+
this.transformState = createState(this.props);
|
|
103
90
|
}
|
|
104
91
|
|
|
105
|
-
|
|
92
|
+
mount = () => {
|
|
93
|
+
this.initializeWindowEvents();
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
unmount = () => {
|
|
106
97
|
this.cleanupWindowEvents();
|
|
107
|
-
}
|
|
98
|
+
};
|
|
108
99
|
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
}
|
|
114
|
-
}
|
|
100
|
+
update = (newProps: ReactZoomPanPinchProps) => {
|
|
101
|
+
handleCalculateBounds(this, this.transformState.scale);
|
|
102
|
+
this.setup = createSetup(newProps);
|
|
103
|
+
};
|
|
115
104
|
|
|
116
105
|
initializeWindowEvents = (): void => {
|
|
117
106
|
const passive = makePassiveEventOption();
|
|
@@ -124,24 +113,31 @@ class TransformContext extends Component<
|
|
|
124
113
|
currentDocument?.addEventListener("mouseleave", this.clearPanning, passive);
|
|
125
114
|
currentWindow?.addEventListener("keyup", this.setKeyUnPressed, passive);
|
|
126
115
|
currentWindow?.addEventListener("keydown", this.setKeyPressed, passive);
|
|
127
|
-
|
|
128
|
-
this.handleRef();
|
|
129
|
-
}
|
|
116
|
+
};
|
|
130
117
|
|
|
131
118
|
cleanupWindowEvents = (): void => {
|
|
132
119
|
const passive = makePassiveEventOption();
|
|
133
120
|
const currentDocument = this.wrapperComponent?.ownerDocument;
|
|
134
121
|
const currentWindow = currentDocument?.defaultView;
|
|
135
|
-
currentWindow?.removeEventListener(
|
|
122
|
+
currentWindow?.removeEventListener(
|
|
123
|
+
"mousedown",
|
|
124
|
+
this.onPanningStart,
|
|
125
|
+
passive,
|
|
126
|
+
);
|
|
136
127
|
currentWindow?.removeEventListener("mousemove", this.onPanning, passive);
|
|
137
128
|
currentWindow?.removeEventListener("mouseup", this.onPanningStop, passive);
|
|
138
|
-
currentDocument?.removeEventListener(
|
|
129
|
+
currentDocument?.removeEventListener(
|
|
130
|
+
"mouseleave",
|
|
131
|
+
this.clearPanning,
|
|
132
|
+
passive,
|
|
133
|
+
);
|
|
139
134
|
currentWindow?.removeEventListener("keyup", this.setKeyUnPressed, passive);
|
|
140
135
|
currentWindow?.removeEventListener("keydown", this.setKeyPressed, passive);
|
|
141
136
|
document.removeEventListener("mouseleave", this.clearPanning, passive);
|
|
142
137
|
|
|
143
138
|
handleCancelAnimation(this);
|
|
144
|
-
|
|
139
|
+
this.observer?.disconnect();
|
|
140
|
+
};
|
|
145
141
|
|
|
146
142
|
handleInitializeWrapperEvents = (wrapper: HTMLDivElement): void => {
|
|
147
143
|
// Zooming events on wrapper
|
|
@@ -154,36 +150,25 @@ class TransformContext extends Component<
|
|
|
154
150
|
wrapper.addEventListener("touchend", this.onTouchPanningStop, passive);
|
|
155
151
|
};
|
|
156
152
|
|
|
157
|
-
handleInitialize = (): void => {
|
|
153
|
+
handleInitialize = (contentComponent: HTMLDivElement): void => {
|
|
158
154
|
const { centerOnInit } = this.setup;
|
|
159
|
-
|
|
160
155
|
this.applyTransformation();
|
|
161
|
-
this.forceUpdate();
|
|
162
156
|
|
|
163
157
|
if (centerOnInit) {
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
if (this.mounted) {
|
|
173
|
-
this.setCenter();
|
|
174
|
-
}
|
|
175
|
-
}, 100);
|
|
176
|
-
setTimeout(() => {
|
|
177
|
-
if (this.mounted) {
|
|
178
|
-
this.setCenter();
|
|
179
|
-
}
|
|
180
|
-
}, 200);
|
|
158
|
+
this.setCenter();
|
|
159
|
+
this.observer = new ResizeObserver(() => {
|
|
160
|
+
this.setCenter();
|
|
161
|
+
this.observer?.disconnect();
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
// Start observing the target node for configured mutations
|
|
165
|
+
this.observer.observe(contentComponent);
|
|
181
166
|
}
|
|
182
167
|
};
|
|
183
168
|
|
|
184
|
-
|
|
169
|
+
/// ///////
|
|
185
170
|
// Zoom
|
|
186
|
-
|
|
171
|
+
/// ///////
|
|
187
172
|
|
|
188
173
|
onWheelZoom = (event: WheelEvent): void => {
|
|
189
174
|
const { disabled } = this.setup;
|
|
@@ -200,9 +185,9 @@ class TransformContext extends Component<
|
|
|
200
185
|
handleWheelStop(this, event);
|
|
201
186
|
};
|
|
202
187
|
|
|
203
|
-
|
|
188
|
+
/// ///////
|
|
204
189
|
// Pan
|
|
205
|
-
|
|
190
|
+
/// ///////
|
|
206
191
|
|
|
207
192
|
onPanningStart = (event: MouseEvent): void => {
|
|
208
193
|
const { disabled } = this.setup;
|
|
@@ -251,9 +236,9 @@ class TransformContext extends Component<
|
|
|
251
236
|
}
|
|
252
237
|
};
|
|
253
238
|
|
|
254
|
-
|
|
239
|
+
/// ///////
|
|
255
240
|
// Pinch
|
|
256
|
-
|
|
241
|
+
/// ///////
|
|
257
242
|
|
|
258
243
|
onPinchStart = (event: TouchEvent): void => {
|
|
259
244
|
const { disabled } = this.setup;
|
|
@@ -297,9 +282,9 @@ class TransformContext extends Component<
|
|
|
297
282
|
}
|
|
298
283
|
};
|
|
299
284
|
|
|
300
|
-
|
|
285
|
+
/// ///////
|
|
301
286
|
// Touch
|
|
302
|
-
|
|
287
|
+
/// ///////
|
|
303
288
|
|
|
304
289
|
onTouchPanningStart = (event: TouchEvent): void => {
|
|
305
290
|
const { disabled } = this.setup;
|
|
@@ -362,9 +347,9 @@ class TransformContext extends Component<
|
|
|
362
347
|
this.onPinchStop(event);
|
|
363
348
|
};
|
|
364
349
|
|
|
365
|
-
|
|
350
|
+
/// ///////
|
|
366
351
|
// Double Click
|
|
367
|
-
|
|
352
|
+
/// ///////
|
|
368
353
|
|
|
369
354
|
onDoubleClick = (event: MouseEvent | TouchEvent): void => {
|
|
370
355
|
const { disabled } = this.setup;
|
|
@@ -376,9 +361,9 @@ class TransformContext extends Component<
|
|
|
376
361
|
handleDoubleClick(this, event);
|
|
377
362
|
};
|
|
378
363
|
|
|
379
|
-
|
|
364
|
+
/// ///////
|
|
380
365
|
// Helpers
|
|
381
|
-
|
|
366
|
+
/// ///////
|
|
382
367
|
|
|
383
368
|
clearPanning = (event: MouseEvent): void => {
|
|
384
369
|
if (this.isPanning) {
|
|
@@ -401,22 +386,6 @@ class TransformContext extends Component<
|
|
|
401
386
|
return Boolean(keys.find((key) => this.pressedKeys[key]));
|
|
402
387
|
};
|
|
403
388
|
|
|
404
|
-
setComponents = (
|
|
405
|
-
wrapperComponent: HTMLDivElement,
|
|
406
|
-
contentComponent: HTMLDivElement,
|
|
407
|
-
): void => {
|
|
408
|
-
this.cleanupWindowEvents();
|
|
409
|
-
this.wrapperComponent = wrapperComponent;
|
|
410
|
-
this.contentComponent = contentComponent;
|
|
411
|
-
handleCalculateBounds(this, this.transformState.scale);
|
|
412
|
-
this.handleInitializeWrapperEvents(wrapperComponent);
|
|
413
|
-
this.initializeWindowEvents();
|
|
414
|
-
this.handleInitialize();
|
|
415
|
-
this.handleRef();
|
|
416
|
-
this.isInitialized = true;
|
|
417
|
-
handleCallback(getContext(this), undefined, this.props.onInit);
|
|
418
|
-
};
|
|
419
|
-
|
|
420
389
|
setTransformState = (
|
|
421
390
|
scale: number,
|
|
422
391
|
positionX: number,
|
|
@@ -424,7 +393,11 @@ class TransformContext extends Component<
|
|
|
424
393
|
): void => {
|
|
425
394
|
const { onTransformed } = this.props;
|
|
426
395
|
|
|
427
|
-
if (
|
|
396
|
+
if (
|
|
397
|
+
!Number.isNaN(scale) &&
|
|
398
|
+
!Number.isNaN(positionX) &&
|
|
399
|
+
!Number.isNaN(positionY)
|
|
400
|
+
) {
|
|
428
401
|
if (scale !== this.transformState.scale) {
|
|
429
402
|
this.transformState.previousScale = this.transformState.scale;
|
|
430
403
|
this.transformState.scale = scale;
|
|
@@ -463,31 +436,28 @@ class TransformContext extends Component<
|
|
|
463
436
|
const { scale, positionX, positionY } = this.transformState;
|
|
464
437
|
const transform = getTransformStyles(positionX, positionY, scale);
|
|
465
438
|
this.contentComponent.style.transform = transform;
|
|
466
|
-
|
|
467
|
-
this.handleRef();
|
|
468
439
|
};
|
|
469
440
|
|
|
470
|
-
|
|
471
|
-
|
|
441
|
+
getContext = () => {
|
|
442
|
+
return getContext(this);
|
|
472
443
|
};
|
|
473
444
|
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
const content = typeof children === "function" ? children(value) : children;
|
|
478
|
-
|
|
479
|
-
return (
|
|
480
|
-
<Context.Provider
|
|
481
|
-
value={{
|
|
482
|
-
...this.transformState,
|
|
483
|
-
setComponents: this.setComponents,
|
|
484
|
-
contextInstance: this,
|
|
485
|
-
}}
|
|
486
|
-
>
|
|
487
|
-
{content}
|
|
488
|
-
</Context.Provider>
|
|
489
|
-
);
|
|
490
|
-
}
|
|
491
|
-
}
|
|
445
|
+
/**
|
|
446
|
+
* Initialization
|
|
447
|
+
*/
|
|
492
448
|
|
|
493
|
-
|
|
449
|
+
init = (
|
|
450
|
+
wrapperComponent: HTMLDivElement,
|
|
451
|
+
contentComponent: HTMLDivElement,
|
|
452
|
+
): void => {
|
|
453
|
+
this.cleanupWindowEvents();
|
|
454
|
+
this.wrapperComponent = wrapperComponent;
|
|
455
|
+
this.contentComponent = contentComponent;
|
|
456
|
+
handleCalculateBounds(this, this.transformState.scale);
|
|
457
|
+
this.handleInitializeWrapperEvents(wrapperComponent);
|
|
458
|
+
this.handleInitialize(contentComponent);
|
|
459
|
+
this.initializeWindowEvents();
|
|
460
|
+
this.isInitialized = true;
|
|
461
|
+
handleCallback(getContext(this), undefined, this.props.onInit);
|
|
462
|
+
};
|
|
463
|
+
}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
/* eslint-disable no-param-reassign */
|
|
1
2
|
import { ReactZoomPanPinchContext } from "../../models/context.model";
|
|
2
3
|
import { animate, handleCancelAnimation } from "../animations/animations.utils";
|
|
3
4
|
import { handleCalculateBounds } from "../bounds/bounds.utils";
|
|
@@ -29,6 +30,25 @@ export function handlePanningStart(
|
|
|
29
30
|
}
|
|
30
31
|
}
|
|
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
|
+
|
|
32
52
|
export function handlePanning(
|
|
33
53
|
contextInstance: ReactZoomPanPinchContext,
|
|
34
54
|
clientX: number,
|
|
@@ -78,22 +98,3 @@ export function handlePanningEnd(
|
|
|
78
98
|
}
|
|
79
99
|
}
|
|
80
100
|
}
|
|
81
|
-
|
|
82
|
-
export function handleAlignToBounds(
|
|
83
|
-
contextInstance: ReactZoomPanPinchContext,
|
|
84
|
-
): void {
|
|
85
|
-
const { scale } = contextInstance.transformState;
|
|
86
|
-
const { minScale, alignmentAnimation } = contextInstance.setup;
|
|
87
|
-
const { disabled, sizeX, sizeY, animationTime, animationType } =
|
|
88
|
-
alignmentAnimation;
|
|
89
|
-
|
|
90
|
-
const isDisabled = disabled || scale < minScale || (!sizeX && !sizeY);
|
|
91
|
-
|
|
92
|
-
if (isDisabled) return;
|
|
93
|
-
|
|
94
|
-
const targetState = handlePanToBounds(contextInstance);
|
|
95
|
-
|
|
96
|
-
if (targetState) {
|
|
97
|
-
animate(contextInstance, targetState, animationTime, animationType);
|
|
98
|
-
}
|
|
99
|
-
}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
/* eslint-disable no-param-reassign */
|
|
1
2
|
import {
|
|
2
3
|
PositionType,
|
|
3
4
|
ReactZoomPanPinchContext,
|
|
@@ -59,7 +60,7 @@ export const handleTouchPanningSetup = (
|
|
|
59
60
|
contextInstance: ReactZoomPanPinchContext,
|
|
60
61
|
event: TouchEvent,
|
|
61
62
|
): void => {
|
|
62
|
-
const touches = event
|
|
63
|
+
const { touches } = event;
|
|
63
64
|
const { positionX, positionY } = contextInstance.transformState;
|
|
64
65
|
|
|
65
66
|
contextInstance.isPanning = true;
|
|
@@ -81,12 +82,8 @@ export function handlePanToBounds(
|
|
|
81
82
|
|
|
82
83
|
if (disabled || !wrapperComponent || !contextInstance.bounds) return;
|
|
83
84
|
|
|
84
|
-
const {
|
|
85
|
-
|
|
86
|
-
minPositionX,
|
|
87
|
-
maxPositionY,
|
|
88
|
-
minPositionY,
|
|
89
|
-
} = contextInstance.bounds;
|
|
85
|
+
const { maxPositionX, minPositionX, maxPositionY, minPositionY } =
|
|
86
|
+
contextInstance.bounds;
|
|
90
87
|
|
|
91
88
|
const xChanged = positionX > maxPositionX || positionX < minPositionX;
|
|
92
89
|
const yChanged = positionY > maxPositionY || positionY < minPositionY;
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
/* eslint-disable no-param-reassign */
|
|
1
2
|
import { ReactZoomPanPinchContext } from "../../models";
|
|
2
3
|
import { handleCancelAnimation } from "../animations/animations.utils";
|
|
3
4
|
import { handleAlignToScaleBounds } from "../zoom/zoom.logic";
|
|
@@ -29,11 +30,8 @@ export const handlePinchZoom = (
|
|
|
29
30
|
): void => {
|
|
30
31
|
const { contentComponent, pinchStartDistance } = contextInstance;
|
|
31
32
|
const { scale } = contextInstance.transformState;
|
|
32
|
-
const {
|
|
33
|
-
|
|
34
|
-
centerZoomedOut,
|
|
35
|
-
zoomAnimation,
|
|
36
|
-
} = contextInstance.setup;
|
|
33
|
+
const { limitToBounds, centerZoomedOut, zoomAnimation } =
|
|
34
|
+
contextInstance.setup;
|
|
37
35
|
const { disabled, size } = zoomAnimation;
|
|
38
36
|
|
|
39
37
|
// if one finger starts from outside of wrapper
|
|
@@ -42,7 +40,7 @@ export const handlePinchZoom = (
|
|
|
42
40
|
const midPoint = calculateTouchMidPoint(event, scale, contentComponent);
|
|
43
41
|
|
|
44
42
|
// if touches goes off of the wrapper element
|
|
45
|
-
if (!isFinite(midPoint.x) || !isFinite(midPoint.y)) return;
|
|
43
|
+
if (!Number.isFinite(midPoint.x) || !Number.isFinite(midPoint.y)) return;
|
|
46
44
|
|
|
47
45
|
const currentDistance = getTouchDistance(event);
|
|
48
46
|
const newScale = calculatePinchZoom(contextInstance, currentDistance);
|
|
@@ -54,8 +54,8 @@ export const calculateTouchMidPoint = (
|
|
|
54
54
|
|
|
55
55
|
export const getTouchDistance = (event: TouchEvent): number => {
|
|
56
56
|
return Math.sqrt(
|
|
57
|
-
|
|
58
|
-
|
|
57
|
+
(event.touches[0].pageX - event.touches[1].pageX) ** 2 +
|
|
58
|
+
(event.touches[0].pageY - event.touches[1].pageY) ** 2,
|
|
59
59
|
);
|
|
60
60
|
};
|
|
61
61
|
|
|
@@ -1,6 +1,4 @@
|
|
|
1
|
-
import { ReactZoomPanPinchContext } from "../../models";
|
|
2
|
-
import { PositionType } from "../../models";
|
|
3
|
-
|
|
1
|
+
import { ReactZoomPanPinchContext, PositionType } from "../../models";
|
|
4
2
|
import { checkIsNumber, isExcludedNode, roundNumber } from "../../utils";
|
|
5
3
|
import { checkZoomBounds } from "../zoom/zoom.utils";
|
|
6
4
|
|
|
@@ -8,12 +6,8 @@ export const isWheelAllowed = (
|
|
|
8
6
|
contextInstance: ReactZoomPanPinchContext,
|
|
9
7
|
event: WheelEvent,
|
|
10
8
|
): boolean => {
|
|
11
|
-
const {
|
|
12
|
-
|
|
13
|
-
wheelDisabled,
|
|
14
|
-
touchPadDisabled,
|
|
15
|
-
excluded,
|
|
16
|
-
} = contextInstance.setup.wheel;
|
|
9
|
+
const { disabled, wheelDisabled, touchPadDisabled, excluded } =
|
|
10
|
+
contextInstance.setup.wheel;
|
|
17
11
|
const { isInitialized, isPanning } = contextInstance;
|
|
18
12
|
|
|
19
13
|
const target = event.target as HTMLElement;
|
|
@@ -31,11 +25,18 @@ export const isWheelAllowed = (
|
|
|
31
25
|
return true;
|
|
32
26
|
};
|
|
33
27
|
|
|
28
|
+
export const getDeltaY = (event?: WheelEvent) => {
|
|
29
|
+
if (event) {
|
|
30
|
+
return event.deltaY < 0 ? 1 : -1;
|
|
31
|
+
}
|
|
32
|
+
return 0;
|
|
33
|
+
};
|
|
34
|
+
|
|
34
35
|
export function getDelta(
|
|
35
36
|
event: WheelEvent,
|
|
36
37
|
customDelta?: number | null,
|
|
37
38
|
): number {
|
|
38
|
-
const deltaY =
|
|
39
|
+
const deltaY = getDeltaY(event);
|
|
39
40
|
const delta = checkIsNumber(customDelta, deltaY);
|
|
40
41
|
return delta;
|
|
41
42
|
}
|
|
@@ -60,7 +61,7 @@ export function getMousePosition(
|
|
|
60
61
|
mouseY = (touch.clientY - contentRect.top) / scale;
|
|
61
62
|
}
|
|
62
63
|
|
|
63
|
-
if (isNaN(mouseX) || isNaN(mouseY))
|
|
64
|
+
if (Number.isNaN(mouseX) || Number.isNaN(mouseY))
|
|
64
65
|
console.error("No mouse or touch offset found");
|
|
65
66
|
|
|
66
67
|
return {
|
|
@@ -5,6 +5,35 @@ import { handleCalculateBounds } from "../bounds/bounds.utils";
|
|
|
5
5
|
import { handleAlignToBounds } from "../pan/panning.logic";
|
|
6
6
|
import { checkZoomBounds, handleCalculateZoomPositions } from "./zoom.utils";
|
|
7
7
|
|
|
8
|
+
export function handleZoomToPoint(
|
|
9
|
+
contextInstance: ReactZoomPanPinchContext,
|
|
10
|
+
scale: number,
|
|
11
|
+
mouseX: number,
|
|
12
|
+
mouseY: number,
|
|
13
|
+
): Omit<ReactZoomPanPinchState, "previousScale"> | undefined {
|
|
14
|
+
const { minScale, maxScale, limitToBounds } = contextInstance.setup;
|
|
15
|
+
|
|
16
|
+
const newScale = checkZoomBounds(
|
|
17
|
+
roundNumber(scale, 2),
|
|
18
|
+
minScale,
|
|
19
|
+
maxScale,
|
|
20
|
+
0,
|
|
21
|
+
false,
|
|
22
|
+
);
|
|
23
|
+
const bounds = handleCalculateBounds(contextInstance, newScale);
|
|
24
|
+
|
|
25
|
+
const { x, y } = handleCalculateZoomPositions(
|
|
26
|
+
contextInstance,
|
|
27
|
+
mouseX,
|
|
28
|
+
mouseY,
|
|
29
|
+
newScale,
|
|
30
|
+
bounds,
|
|
31
|
+
limitToBounds,
|
|
32
|
+
);
|
|
33
|
+
|
|
34
|
+
return { scale: newScale, positionX: x, positionY: y };
|
|
35
|
+
}
|
|
36
|
+
|
|
8
37
|
export function handleAlignToScaleBounds(
|
|
9
38
|
contextInstance: ReactZoomPanPinchContext,
|
|
10
39
|
mousePositionX?: number,
|
|
@@ -38,32 +67,3 @@ export function handleAlignToScaleBounds(
|
|
|
38
67
|
animate(contextInstance, targetState, animationTime, animationType);
|
|
39
68
|
}
|
|
40
69
|
}
|
|
41
|
-
|
|
42
|
-
export function handleZoomToPoint(
|
|
43
|
-
contextInstance: ReactZoomPanPinchContext,
|
|
44
|
-
scale: number,
|
|
45
|
-
mouseX: number,
|
|
46
|
-
mouseY: number,
|
|
47
|
-
): Omit<ReactZoomPanPinchState, "previousScale"> | undefined {
|
|
48
|
-
const { minScale, maxScale, limitToBounds } = contextInstance.setup;
|
|
49
|
-
|
|
50
|
-
const newScale = checkZoomBounds(
|
|
51
|
-
roundNumber(scale, 2),
|
|
52
|
-
minScale,
|
|
53
|
-
maxScale,
|
|
54
|
-
0,
|
|
55
|
-
false,
|
|
56
|
-
);
|
|
57
|
-
const bounds = handleCalculateBounds(contextInstance, newScale);
|
|
58
|
-
|
|
59
|
-
const { x, y } = handleCalculateZoomPositions(
|
|
60
|
-
contextInstance,
|
|
61
|
-
mouseX,
|
|
62
|
-
mouseY,
|
|
63
|
-
newScale,
|
|
64
|
-
bounds,
|
|
65
|
-
limitToBounds,
|
|
66
|
-
);
|
|
67
|
-
|
|
68
|
-
return { scale: newScale, positionX: x, positionY: y };
|
|
69
|
-
}
|
|
@@ -51,8 +51,8 @@ export function checkZoomBounds(
|
|
|
51
51
|
const scalePadding = enablePadding ? zoomPadding : 0;
|
|
52
52
|
const minScaleWithPadding = minScale - scalePadding;
|
|
53
53
|
|
|
54
|
-
if (!isNaN(maxScale) && zoom >= maxScale) return maxScale;
|
|
55
|
-
if (!isNaN(minScale) && zoom <= minScaleWithPadding)
|
|
54
|
+
if (!Number.isNaN(maxScale) && zoom >= maxScale) return maxScale;
|
|
55
|
+
if (!Number.isNaN(minScale) && zoom <= minScaleWithPadding)
|
|
56
56
|
return minScaleWithPadding;
|
|
57
57
|
return zoom;
|
|
58
58
|
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { useContext } from "react";
|
|
2
|
-
|
|
3
|
-
import {
|
|
2
|
+
|
|
3
|
+
import { getContext } from "../utils";
|
|
4
|
+
import { Context } from "../components/transform-wrapper";
|
|
4
5
|
import { ReactZoomPanPinchRef } from "../models";
|
|
5
6
|
|
|
6
7
|
const useTransformContext = (): ReactZoomPanPinchRef => {
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import React from "react";
|
|
2
2
|
|
|
3
|
-
import { TransformContext } from "../components/transform-context";
|
|
4
3
|
import { animations } from "../core/animations/animations.constants";
|
|
5
4
|
import { DeepNonNullable } from "./helpers.model";
|
|
6
5
|
import {
|
|
@@ -11,8 +10,9 @@ import {
|
|
|
11
10
|
setTransform,
|
|
12
11
|
zoomOut,
|
|
13
12
|
} from "../core/handlers/handlers.logic";
|
|
13
|
+
import { ZoomPanPinch } from "../core/instance.core";
|
|
14
14
|
|
|
15
|
-
export type ReactZoomPanPinchContext = typeof
|
|
15
|
+
export type ReactZoomPanPinchContext = typeof ZoomPanPinch.prototype;
|
|
16
16
|
|
|
17
17
|
export type ReactZoomPanPinchRef = {
|
|
18
18
|
instance: ReactZoomPanPinchContext;
|
|
@@ -35,6 +35,10 @@ export type ReactZoomPanPinchHandlers = {
|
|
|
35
35
|
zoomToElement: ReturnType<typeof zoomToElement>;
|
|
36
36
|
};
|
|
37
37
|
|
|
38
|
+
export type ReactZoomPanPinchRefProps = {
|
|
39
|
+
setRef: (context: ReactZoomPanPinchRef) => void;
|
|
40
|
+
} & Omit<ReactZoomPanPinchProps, "ref">;
|
|
41
|
+
|
|
38
42
|
export type ReactZoomPanPinchProps = {
|
|
39
43
|
children?: React.ReactNode | ((ref: ReactZoomPanPinchRef) => React.ReactNode);
|
|
40
44
|
ref?: React.Ref<ReactZoomPanPinchRef>;
|