react-zoom-pan-pinch 2.1.2 → 2.2.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/CHANGELOG.md +14 -0
- package/README.md +43 -0
- package/dist/index.cjs.js +87 -60
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.d.ts +11 -2
- package/dist/index.esm.js +87 -60
- package/dist/index.esm.js.map +1 -1
- package/package.json +13 -12
- package/src/.eslintrc.js +36 -0
- package/src/components/transform-component.module.css +30 -0
- package/src/components/transform-component.tsx +52 -0
- package/src/components/transform-context.tsx +493 -0
- package/src/components/transform-wrapper.tsx +18 -0
- package/src/constants/state.constants.ts +88 -0
- package/src/core/animations/animations.constants.ts +76 -0
- package/src/core/animations/animations.utils.ts +109 -0
- package/src/core/bounds/bounds.types.ts +8 -0
- package/src/core/bounds/bounds.utils.ts +157 -0
- package/src/core/double-click/double-click.logic.ts +75 -0
- package/src/core/handlers/handlers.logic.ts +112 -0
- package/src/core/handlers/handlers.utils.ts +202 -0
- package/src/core/pan/panning.logic.ts +99 -0
- package/src/core/pan/panning.utils.ts +199 -0
- package/src/core/pan/velocity.logic.ts +148 -0
- package/src/core/pan/velocity.utils.ts +79 -0
- package/src/core/pinch/pinch.logic.ts +83 -0
- package/src/core/pinch/pinch.utils.ts +88 -0
- package/src/core/wheel/wheel.logic.ts +118 -0
- package/src/core/wheel/wheel.utils.ts +121 -0
- package/src/core/zoom/zoom.logic.ts +69 -0
- package/src/core/zoom/zoom.utils.ts +58 -0
- package/src/hooks/use-transform-context.tsx +16 -0
- package/src/index.ts +6 -0
- package/src/models/calculations.model.ts +26 -0
- package/src/models/context.model.ts +179 -0
- package/src/models/helpers.model.ts +15 -0
- package/src/models/index.ts +3 -0
- package/src/react-app-env.d.ts +1 -0
- package/src/stories/assets/big-image.jpeg +0 -0
- package/src/stories/assets/medium-image.jpg +0 -0
- package/src/stories/assets/small-image.jpg +0 -0
- package/src/stories/assets/svg-example.svg +14 -0
- package/src/stories/docs/Introduction.stories.mdx +97 -0
- package/src/stories/docs/handlers.stories.mdx +48 -0
- package/src/stories/docs/handlers.tsx +107 -0
- package/src/stories/docs/props.stories.mdx +92 -0
- package/src/stories/docs/props.tsx +490 -0
- package/src/stories/examples/content/content.stories.mdx +122 -0
- package/src/stories/examples/content-rerendering/content-updating.stories.mdx +42 -0
- package/src/stories/examples/content-rerendering/example.tsx +74 -0
- package/src/stories/examples/controls/controls.stories.mdx +71 -0
- package/src/stories/examples/image-big/image-big.stories.mdx +44 -0
- package/src/stories/examples/image-medium/image-medium.stories.mdx +42 -0
- package/src/stories/examples/image-small/image-small.stories.mdx +42 -0
- package/src/stories/examples/image-zoomed-out/image-zoomed-out.stories.mdx +47 -0
- package/src/stories/examples/zoom-to-element/example.tsx +91 -0
- package/src/stories/examples/zoom-to-element/zoom-to-element.stories.mdx +28 -0
- package/src/stories/types/args.types.ts +201 -0
- package/src/stories/utils/args.utils.ts +34 -0
- package/src/stories/utils/code.utils.tsx +21 -0
- package/src/stories/utils/controls.utils.tsx +26 -0
- package/src/stories/utils/index.ts +3 -0
- package/src/stories/utils/styles.module.css +29 -0
- package/src/styles.css +8 -0
- package/src/typings.d.ts +10 -0
- package/src/utils/calculations.utils.ts +16 -0
- package/src/utils/callback.utils.ts +11 -0
- package/src/utils/context.utils.ts +27 -0
- package/src/utils/event.utils.ts +22 -0
- package/src/utils/helpers.utils.ts +27 -0
- package/src/utils/index.ts +7 -0
- package/src/utils/state.utils.ts +40 -0
- package/src/utils/styles.utils.ts +27 -0
|
@@ -0,0 +1,493 @@
|
|
|
1
|
+
import React, { Component } from "react";
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
BoundsType,
|
|
5
|
+
LibrarySetup,
|
|
6
|
+
PositionType,
|
|
7
|
+
VelocityType,
|
|
8
|
+
AnimationType,
|
|
9
|
+
ReactZoomPanPinchRef,
|
|
10
|
+
ReactZoomPanPinchProps,
|
|
11
|
+
ReactZoomPanPinchState,
|
|
12
|
+
} from "../models";
|
|
13
|
+
|
|
14
|
+
import {
|
|
15
|
+
getContext,
|
|
16
|
+
createSetup,
|
|
17
|
+
createState,
|
|
18
|
+
handleCallback,
|
|
19
|
+
getTransformStyles,
|
|
20
|
+
makePassiveEventOption,
|
|
21
|
+
getCenterPosition,
|
|
22
|
+
} from "../utils";
|
|
23
|
+
|
|
24
|
+
import { contextInitialState } from "../constants/state.constants";
|
|
25
|
+
|
|
26
|
+
import { handleCancelAnimation } from "../core/animations/animations.utils";
|
|
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
|
+
|
|
31
|
+
import {
|
|
32
|
+
handleWheelStart,
|
|
33
|
+
handleWheelZoom,
|
|
34
|
+
handleWheelStop,
|
|
35
|
+
} from "../core/wheel/wheel.logic";
|
|
36
|
+
import {
|
|
37
|
+
isPanningAllowed,
|
|
38
|
+
isPanningStartAllowed,
|
|
39
|
+
} from "../core/pan/panning.utils";
|
|
40
|
+
import {
|
|
41
|
+
handlePanning,
|
|
42
|
+
handlePanningEnd,
|
|
43
|
+
handlePanningStart,
|
|
44
|
+
} from "../core/pan/panning.logic";
|
|
45
|
+
import {
|
|
46
|
+
handlePinchStart,
|
|
47
|
+
handlePinchStop,
|
|
48
|
+
handlePinchZoom,
|
|
49
|
+
} from "../core/pinch/pinch.logic";
|
|
50
|
+
import {
|
|
51
|
+
handleDoubleClick,
|
|
52
|
+
isDoubleClickAllowed,
|
|
53
|
+
} from "../core/double-click/double-click.logic";
|
|
54
|
+
|
|
55
|
+
type StartCoordsType = { x: number; y: number } | null;
|
|
56
|
+
|
|
57
|
+
const Context = React.createContext(contextInitialState);
|
|
58
|
+
|
|
59
|
+
class TransformContext extends Component<
|
|
60
|
+
Omit<ReactZoomPanPinchProps, "ref"> & {
|
|
61
|
+
setRef: (context: ReactZoomPanPinchRef) => void;
|
|
62
|
+
}
|
|
63
|
+
> {
|
|
64
|
+
public mounted = true;
|
|
65
|
+
|
|
66
|
+
public transformState: ReactZoomPanPinchState = createState(this.props);
|
|
67
|
+
|
|
68
|
+
public setup: LibrarySetup = createSetup(this.props);
|
|
69
|
+
|
|
70
|
+
// Components
|
|
71
|
+
public wrapperComponent: HTMLDivElement | null = null;
|
|
72
|
+
public contentComponent: HTMLDivElement | null = null;
|
|
73
|
+
// Initialization
|
|
74
|
+
public isInitialized = false;
|
|
75
|
+
public bounds: BoundsType | null = null;
|
|
76
|
+
// wheel helpers
|
|
77
|
+
public previousWheelEvent: WheelEvent | null = null;
|
|
78
|
+
public wheelStopEventTimer: ReturnType<typeof setTimeout> | null = null;
|
|
79
|
+
public wheelAnimationTimer: ReturnType<typeof setTimeout> | null = null;
|
|
80
|
+
// panning helpers
|
|
81
|
+
public isPanning = false;
|
|
82
|
+
public startCoords: StartCoordsType = null;
|
|
83
|
+
public lastTouch: number | null = null;
|
|
84
|
+
// pinch helpers
|
|
85
|
+
public distance: null | number = null;
|
|
86
|
+
public lastDistance: null | number = null;
|
|
87
|
+
public pinchStartDistance: null | number = null;
|
|
88
|
+
public pinchStartScale: null | number = null;
|
|
89
|
+
public pinchMidpoint: null | PositionType = null;
|
|
90
|
+
// velocity helpers
|
|
91
|
+
public velocity: VelocityType | null = null;
|
|
92
|
+
public velocityTime: number | null = null;
|
|
93
|
+
public lastMousePosition: PositionType | null = null;
|
|
94
|
+
// animations helpers
|
|
95
|
+
public animate = false;
|
|
96
|
+
public animation: AnimationType | null = null;
|
|
97
|
+
public maxBounds: BoundsType | null = null;
|
|
98
|
+
// key press
|
|
99
|
+
public pressedKeys: { [key: string]: boolean } = {};
|
|
100
|
+
|
|
101
|
+
componentDidMount(): void {
|
|
102
|
+
this.initializeWindowEvents();
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
componentWillUnmount(): void {
|
|
106
|
+
this.cleanupWindowEvents();
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
componentDidUpdate(oldProps: ReactZoomPanPinchProps): void {
|
|
110
|
+
if (oldProps !== this.props) {
|
|
111
|
+
handleCalculateBounds(this, this.transformState.scale);
|
|
112
|
+
this.setup = createSetup(this.props);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
initializeWindowEvents = (): void => {
|
|
117
|
+
const passive = makePassiveEventOption();
|
|
118
|
+
const currentDocument = this.wrapperComponent?.ownerDocument;
|
|
119
|
+
const currentWindow = currentDocument?.defaultView;
|
|
120
|
+
// Panning on window to allow panning when mouse is out of component wrapper
|
|
121
|
+
currentWindow?.addEventListener("mousedown", this.onPanningStart, passive);
|
|
122
|
+
currentWindow?.addEventListener("mousemove", this.onPanning, passive);
|
|
123
|
+
currentWindow?.addEventListener("mouseup", this.onPanningStop, passive);
|
|
124
|
+
currentDocument?.addEventListener("mouseleave", this.clearPanning, passive);
|
|
125
|
+
currentWindow?.addEventListener("keyup", this.setKeyUnPressed, passive);
|
|
126
|
+
currentWindow?.addEventListener("keydown", this.setKeyPressed, passive);
|
|
127
|
+
|
|
128
|
+
this.handleRef();
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
cleanupWindowEvents = (): void => {
|
|
132
|
+
const passive = makePassiveEventOption();
|
|
133
|
+
const currentDocument = this.wrapperComponent?.ownerDocument;
|
|
134
|
+
const currentWindow = currentDocument?.defaultView;
|
|
135
|
+
currentWindow?.removeEventListener("mousedown", this.onPanningStart, passive);
|
|
136
|
+
currentWindow?.removeEventListener("mousemove", this.onPanning, passive);
|
|
137
|
+
currentWindow?.removeEventListener("mouseup", this.onPanningStop, passive);
|
|
138
|
+
currentDocument?.removeEventListener("mouseleave", this.clearPanning, passive);
|
|
139
|
+
currentWindow?.removeEventListener("keyup", this.setKeyUnPressed, passive);
|
|
140
|
+
currentWindow?.removeEventListener("keydown", this.setKeyPressed, passive);
|
|
141
|
+
document.removeEventListener("mouseleave", this.clearPanning, passive);
|
|
142
|
+
|
|
143
|
+
handleCancelAnimation(this);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
handleInitializeWrapperEvents = (wrapper: HTMLDivElement): void => {
|
|
147
|
+
// Zooming events on wrapper
|
|
148
|
+
const passive = makePassiveEventOption();
|
|
149
|
+
|
|
150
|
+
wrapper.addEventListener("wheel", this.onWheelZoom, passive);
|
|
151
|
+
wrapper.addEventListener("dblclick", this.onDoubleClick, passive);
|
|
152
|
+
wrapper.addEventListener("touchstart", this.onTouchPanningStart, passive);
|
|
153
|
+
wrapper.addEventListener("touchmove", this.onTouchPanning, passive);
|
|
154
|
+
wrapper.addEventListener("touchend", this.onTouchPanningStop, passive);
|
|
155
|
+
};
|
|
156
|
+
|
|
157
|
+
handleInitialize = (): void => {
|
|
158
|
+
const { centerOnInit } = this.setup;
|
|
159
|
+
|
|
160
|
+
this.applyTransformation();
|
|
161
|
+
this.forceUpdate();
|
|
162
|
+
|
|
163
|
+
if (centerOnInit) {
|
|
164
|
+
// this has to be redone once the right solution is found
|
|
165
|
+
// problem is - we need to execute it after mounted component specify it's height / width, images are fetched async so it's tricky
|
|
166
|
+
setTimeout(() => {
|
|
167
|
+
if (this.mounted) {
|
|
168
|
+
this.setCenter();
|
|
169
|
+
}
|
|
170
|
+
}, 50);
|
|
171
|
+
setTimeout(() => {
|
|
172
|
+
if (this.mounted) {
|
|
173
|
+
this.setCenter();
|
|
174
|
+
}
|
|
175
|
+
}, 100);
|
|
176
|
+
setTimeout(() => {
|
|
177
|
+
if (this.mounted) {
|
|
178
|
+
this.setCenter();
|
|
179
|
+
}
|
|
180
|
+
}, 200);
|
|
181
|
+
}
|
|
182
|
+
};
|
|
183
|
+
|
|
184
|
+
//////////
|
|
185
|
+
// Zoom
|
|
186
|
+
//////////
|
|
187
|
+
|
|
188
|
+
onWheelZoom = (event: WheelEvent): void => {
|
|
189
|
+
const { disabled } = this.setup;
|
|
190
|
+
if (disabled) return;
|
|
191
|
+
|
|
192
|
+
const isAllowed = isWheelAllowed(this, event);
|
|
193
|
+
if (!isAllowed) return;
|
|
194
|
+
|
|
195
|
+
const keysPressed = this.isPressingKeys(this.setup.wheel.activationKeys);
|
|
196
|
+
if (!keysPressed) return;
|
|
197
|
+
|
|
198
|
+
handleWheelStart(this, event);
|
|
199
|
+
handleWheelZoom(this, event);
|
|
200
|
+
handleWheelStop(this, event);
|
|
201
|
+
};
|
|
202
|
+
|
|
203
|
+
//////////
|
|
204
|
+
// Pan
|
|
205
|
+
//////////
|
|
206
|
+
|
|
207
|
+
onPanningStart = (event: MouseEvent): void => {
|
|
208
|
+
const { disabled } = this.setup;
|
|
209
|
+
const { onPanningStart } = this.props;
|
|
210
|
+
if (disabled) return;
|
|
211
|
+
|
|
212
|
+
const isAllowed = isPanningStartAllowed(this, event);
|
|
213
|
+
if (!isAllowed) return;
|
|
214
|
+
|
|
215
|
+
const keysPressed = this.isPressingKeys(this.setup.panning.activationKeys);
|
|
216
|
+
if (!keysPressed) return;
|
|
217
|
+
|
|
218
|
+
event.preventDefault();
|
|
219
|
+
event.stopPropagation();
|
|
220
|
+
|
|
221
|
+
handleCancelAnimation(this);
|
|
222
|
+
handlePanningStart(this, event);
|
|
223
|
+
handleCallback(getContext(this), event, onPanningStart);
|
|
224
|
+
};
|
|
225
|
+
|
|
226
|
+
onPanning = (event: MouseEvent): void => {
|
|
227
|
+
const { disabled } = this.setup;
|
|
228
|
+
const { onPanning } = this.props;
|
|
229
|
+
|
|
230
|
+
if (disabled) return;
|
|
231
|
+
|
|
232
|
+
const isAllowed = isPanningAllowed(this);
|
|
233
|
+
if (!isAllowed) return;
|
|
234
|
+
|
|
235
|
+
const keysPressed = this.isPressingKeys(this.setup.panning.activationKeys);
|
|
236
|
+
if (!keysPressed) return;
|
|
237
|
+
|
|
238
|
+
event.preventDefault();
|
|
239
|
+
event.stopPropagation();
|
|
240
|
+
|
|
241
|
+
handlePanning(this, event.clientX, event.clientY);
|
|
242
|
+
handleCallback(getContext(this), event, onPanning);
|
|
243
|
+
};
|
|
244
|
+
|
|
245
|
+
onPanningStop = (event: MouseEvent | TouchEvent): void => {
|
|
246
|
+
const { onPanningStop } = this.props;
|
|
247
|
+
|
|
248
|
+
if (this.isPanning) {
|
|
249
|
+
handlePanningEnd(this);
|
|
250
|
+
handleCallback(getContext(this), event, onPanningStop);
|
|
251
|
+
}
|
|
252
|
+
};
|
|
253
|
+
|
|
254
|
+
//////////
|
|
255
|
+
// Pinch
|
|
256
|
+
//////////
|
|
257
|
+
|
|
258
|
+
onPinchStart = (event: TouchEvent): void => {
|
|
259
|
+
const { disabled } = this.setup;
|
|
260
|
+
const { onPinchingStart, onZoomStart } = this.props;
|
|
261
|
+
|
|
262
|
+
if (disabled) return;
|
|
263
|
+
|
|
264
|
+
const isAllowed = isPinchStartAllowed(this, event);
|
|
265
|
+
if (!isAllowed) return;
|
|
266
|
+
|
|
267
|
+
handlePinchStart(this, event);
|
|
268
|
+
handleCancelAnimation(this);
|
|
269
|
+
handleCallback(getContext(this), event, onPinchingStart);
|
|
270
|
+
handleCallback(getContext(this), event, onZoomStart);
|
|
271
|
+
};
|
|
272
|
+
|
|
273
|
+
onPinch = (event: TouchEvent): void => {
|
|
274
|
+
const { disabled } = this.setup;
|
|
275
|
+
const { onPinching, onZoom } = this.props;
|
|
276
|
+
|
|
277
|
+
if (disabled) return;
|
|
278
|
+
|
|
279
|
+
const isAllowed = isPinchAllowed(this);
|
|
280
|
+
if (!isAllowed) return;
|
|
281
|
+
|
|
282
|
+
event.preventDefault();
|
|
283
|
+
event.stopPropagation();
|
|
284
|
+
|
|
285
|
+
handlePinchZoom(this, event);
|
|
286
|
+
handleCallback(getContext(this), event, onPinching);
|
|
287
|
+
handleCallback(getContext(this), event, onZoom);
|
|
288
|
+
};
|
|
289
|
+
|
|
290
|
+
onPinchStop = (event: TouchEvent): void => {
|
|
291
|
+
const { onPinchingStop, onZoomStop } = this.props;
|
|
292
|
+
|
|
293
|
+
if (this.pinchStartScale) {
|
|
294
|
+
handlePinchStop(this);
|
|
295
|
+
handleCallback(getContext(this), event, onPinchingStop);
|
|
296
|
+
handleCallback(getContext(this), event, onZoomStop);
|
|
297
|
+
}
|
|
298
|
+
};
|
|
299
|
+
|
|
300
|
+
//////////
|
|
301
|
+
// Touch
|
|
302
|
+
//////////
|
|
303
|
+
|
|
304
|
+
onTouchPanningStart = (event: TouchEvent): void => {
|
|
305
|
+
const { disabled } = this.setup;
|
|
306
|
+
const { onPanningStart } = this.props;
|
|
307
|
+
|
|
308
|
+
if (disabled) return;
|
|
309
|
+
|
|
310
|
+
const isAllowed = isPanningStartAllowed(this, event);
|
|
311
|
+
|
|
312
|
+
if (!isAllowed) return;
|
|
313
|
+
|
|
314
|
+
const isDoubleTap = this.lastTouch && +new Date() - this.lastTouch < 200;
|
|
315
|
+
|
|
316
|
+
if (isDoubleTap && event.touches.length === 1) {
|
|
317
|
+
this.onDoubleClick(event);
|
|
318
|
+
} else {
|
|
319
|
+
this.lastTouch = +new Date();
|
|
320
|
+
|
|
321
|
+
handleCancelAnimation(this);
|
|
322
|
+
|
|
323
|
+
const { touches } = event;
|
|
324
|
+
|
|
325
|
+
const isPanningAction = touches.length === 1;
|
|
326
|
+
const isPinchAction = touches.length === 2;
|
|
327
|
+
|
|
328
|
+
if (isPanningAction) {
|
|
329
|
+
handleCancelAnimation(this);
|
|
330
|
+
handlePanningStart(this, event);
|
|
331
|
+
handleCallback(getContext(this), event, onPanningStart);
|
|
332
|
+
}
|
|
333
|
+
if (isPinchAction) {
|
|
334
|
+
this.onPinchStart(event);
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
};
|
|
338
|
+
|
|
339
|
+
onTouchPanning = (event: TouchEvent): void => {
|
|
340
|
+
const { disabled } = this.setup;
|
|
341
|
+
const { onPanning } = this.props;
|
|
342
|
+
|
|
343
|
+
if (this.isPanning && event.touches.length === 1) {
|
|
344
|
+
if (disabled) return;
|
|
345
|
+
|
|
346
|
+
const isAllowed = isPanningAllowed(this);
|
|
347
|
+
if (!isAllowed) return;
|
|
348
|
+
|
|
349
|
+
event.preventDefault();
|
|
350
|
+
event.stopPropagation();
|
|
351
|
+
|
|
352
|
+
const touch = event.touches[0];
|
|
353
|
+
handlePanning(this, touch.clientX, touch.clientY);
|
|
354
|
+
handleCallback(getContext(this), event, onPanning);
|
|
355
|
+
} else if (event.touches.length > 1) {
|
|
356
|
+
this.onPinch(event);
|
|
357
|
+
}
|
|
358
|
+
};
|
|
359
|
+
|
|
360
|
+
onTouchPanningStop = (event: TouchEvent): void => {
|
|
361
|
+
this.onPanningStop(event);
|
|
362
|
+
this.onPinchStop(event);
|
|
363
|
+
};
|
|
364
|
+
|
|
365
|
+
//////////
|
|
366
|
+
// Double Click
|
|
367
|
+
//////////
|
|
368
|
+
|
|
369
|
+
onDoubleClick = (event: MouseEvent | TouchEvent): void => {
|
|
370
|
+
const { disabled } = this.setup;
|
|
371
|
+
if (disabled) return;
|
|
372
|
+
|
|
373
|
+
const isAllowed = isDoubleClickAllowed(this, event);
|
|
374
|
+
if (!isAllowed) return;
|
|
375
|
+
|
|
376
|
+
handleDoubleClick(this, event);
|
|
377
|
+
};
|
|
378
|
+
|
|
379
|
+
//////////
|
|
380
|
+
// Helpers
|
|
381
|
+
//////////
|
|
382
|
+
|
|
383
|
+
clearPanning = (event: MouseEvent): void => {
|
|
384
|
+
if (this.isPanning) {
|
|
385
|
+
this.onPanningStop(event);
|
|
386
|
+
}
|
|
387
|
+
};
|
|
388
|
+
|
|
389
|
+
setKeyPressed = (e: KeyboardEvent): void => {
|
|
390
|
+
this.pressedKeys[e.key] = true;
|
|
391
|
+
};
|
|
392
|
+
|
|
393
|
+
setKeyUnPressed = (e: KeyboardEvent): void => {
|
|
394
|
+
this.pressedKeys[e.key] = false;
|
|
395
|
+
};
|
|
396
|
+
|
|
397
|
+
isPressingKeys = (keys: string[]): boolean => {
|
|
398
|
+
if (!keys.length) {
|
|
399
|
+
return true;
|
|
400
|
+
}
|
|
401
|
+
return Boolean(keys.find((key) => this.pressedKeys[key]));
|
|
402
|
+
};
|
|
403
|
+
|
|
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
|
+
setTransformState = (
|
|
421
|
+
scale: number,
|
|
422
|
+
positionX: number,
|
|
423
|
+
positionY: number,
|
|
424
|
+
): void => {
|
|
425
|
+
const { onTransformed } = this.props;
|
|
426
|
+
|
|
427
|
+
if (!isNaN(scale) && !isNaN(positionX) && !isNaN(positionY)) {
|
|
428
|
+
if (scale !== this.transformState.scale) {
|
|
429
|
+
this.transformState.previousScale = this.transformState.scale;
|
|
430
|
+
this.transformState.scale = scale;
|
|
431
|
+
}
|
|
432
|
+
this.transformState.positionX = positionX;
|
|
433
|
+
this.transformState.positionY = positionY;
|
|
434
|
+
|
|
435
|
+
handleCallback(
|
|
436
|
+
getContext(this),
|
|
437
|
+
{ scale, positionX, positionY },
|
|
438
|
+
onTransformed,
|
|
439
|
+
);
|
|
440
|
+
this.applyTransformation();
|
|
441
|
+
} else {
|
|
442
|
+
console.error("Detected NaN set state values");
|
|
443
|
+
}
|
|
444
|
+
};
|
|
445
|
+
|
|
446
|
+
setCenter = (): void => {
|
|
447
|
+
if (this.wrapperComponent && this.contentComponent) {
|
|
448
|
+
const targetState = getCenterPosition(
|
|
449
|
+
this.transformState.scale,
|
|
450
|
+
this.wrapperComponent,
|
|
451
|
+
this.contentComponent,
|
|
452
|
+
);
|
|
453
|
+
this.setTransformState(
|
|
454
|
+
targetState.scale,
|
|
455
|
+
targetState.positionX,
|
|
456
|
+
targetState.positionY,
|
|
457
|
+
);
|
|
458
|
+
}
|
|
459
|
+
};
|
|
460
|
+
|
|
461
|
+
applyTransformation = (): void => {
|
|
462
|
+
if (!this.mounted || !this.contentComponent) return;
|
|
463
|
+
const { scale, positionX, positionY } = this.transformState;
|
|
464
|
+
const transform = getTransformStyles(positionX, positionY, scale);
|
|
465
|
+
this.contentComponent.style.transform = transform;
|
|
466
|
+
|
|
467
|
+
this.handleRef();
|
|
468
|
+
};
|
|
469
|
+
|
|
470
|
+
handleRef = (): void => {
|
|
471
|
+
this.props.setRef(getContext(this));
|
|
472
|
+
};
|
|
473
|
+
|
|
474
|
+
render(): JSX.Element {
|
|
475
|
+
const value = getContext(this);
|
|
476
|
+
const { children } = this.props;
|
|
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
|
+
}
|
|
492
|
+
|
|
493
|
+
export { Context, TransformContext };
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import React, { useImperativeHandle, useState } from "react";
|
|
2
|
+
import { ReactZoomPanPinchProps, ReactZoomPanPinchRef } from "../models";
|
|
3
|
+
import { TransformContext } from "./transform-context";
|
|
4
|
+
|
|
5
|
+
export const TransformWrapper = React.forwardRef(
|
|
6
|
+
(
|
|
7
|
+
props: Omit<ReactZoomPanPinchProps, "ref">,
|
|
8
|
+
ref: React.Ref<ReactZoomPanPinchRef>,
|
|
9
|
+
) => {
|
|
10
|
+
const [innerRef, setRef] = useState<ReactZoomPanPinchRef | null>(null);
|
|
11
|
+
|
|
12
|
+
useImperativeHandle(ref, () => innerRef as any, [innerRef]);
|
|
13
|
+
|
|
14
|
+
return <TransformContext {...props} setRef={setRef as any} />;
|
|
15
|
+
},
|
|
16
|
+
);
|
|
17
|
+
|
|
18
|
+
export default TransformWrapper;
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import {
|
|
2
|
+
LibrarySetup,
|
|
3
|
+
ReactZoomPanPinchContext,
|
|
4
|
+
} from "../models/context.model";
|
|
5
|
+
import {
|
|
6
|
+
ReactZoomPanPinchComponentHelpers,
|
|
7
|
+
ReactZoomPanPinchState,
|
|
8
|
+
} from "../models/context.model";
|
|
9
|
+
|
|
10
|
+
export const initialState: ReactZoomPanPinchState = {
|
|
11
|
+
previousScale: 1,
|
|
12
|
+
scale: 1,
|
|
13
|
+
positionX: 0,
|
|
14
|
+
positionY: 0,
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
export const contextInitialState: ReactZoomPanPinchComponentHelpers &
|
|
18
|
+
ReactZoomPanPinchState & {
|
|
19
|
+
contextInstance: ReactZoomPanPinchContext | null;
|
|
20
|
+
} = {
|
|
21
|
+
...initialState,
|
|
22
|
+
setComponents: () => undefined,
|
|
23
|
+
contextInstance: null,
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
export const initialSetup: LibrarySetup = {
|
|
27
|
+
disabled: false,
|
|
28
|
+
minPositionX: null,
|
|
29
|
+
maxPositionX: null,
|
|
30
|
+
minPositionY: null,
|
|
31
|
+
maxPositionY: null,
|
|
32
|
+
minScale: 1,
|
|
33
|
+
maxScale: 8,
|
|
34
|
+
limitToBounds: true,
|
|
35
|
+
centerZoomedOut: false,
|
|
36
|
+
centerOnInit: false,
|
|
37
|
+
disablePadding: false,
|
|
38
|
+
wheel: {
|
|
39
|
+
step: 0.2,
|
|
40
|
+
disabled: false,
|
|
41
|
+
wheelDisabled: false,
|
|
42
|
+
touchPadDisabled: false,
|
|
43
|
+
activationKeys: [],
|
|
44
|
+
excluded: [],
|
|
45
|
+
},
|
|
46
|
+
panning: {
|
|
47
|
+
disabled: false,
|
|
48
|
+
velocityDisabled: false,
|
|
49
|
+
lockAxisX: false,
|
|
50
|
+
lockAxisY: false,
|
|
51
|
+
activationKeys: [],
|
|
52
|
+
excluded: [],
|
|
53
|
+
},
|
|
54
|
+
pinch: {
|
|
55
|
+
step: 5,
|
|
56
|
+
disabled: false,
|
|
57
|
+
excluded: [],
|
|
58
|
+
},
|
|
59
|
+
doubleClick: {
|
|
60
|
+
disabled: false,
|
|
61
|
+
step: 0.7,
|
|
62
|
+
mode: "zoomIn",
|
|
63
|
+
animationType: "easeOut",
|
|
64
|
+
animationTime: 200,
|
|
65
|
+
excluded: [],
|
|
66
|
+
},
|
|
67
|
+
zoomAnimation: {
|
|
68
|
+
disabled: false,
|
|
69
|
+
size: 0.4,
|
|
70
|
+
animationTime: 200,
|
|
71
|
+
animationType: "easeOut",
|
|
72
|
+
},
|
|
73
|
+
alignmentAnimation: {
|
|
74
|
+
disabled: false,
|
|
75
|
+
sizeX: 100,
|
|
76
|
+
sizeY: 100,
|
|
77
|
+
animationTime: 200,
|
|
78
|
+
velocityAlignmentTime: 400,
|
|
79
|
+
animationType: "easeOut",
|
|
80
|
+
},
|
|
81
|
+
velocityAnimation: {
|
|
82
|
+
disabled: false,
|
|
83
|
+
sensitivity: 1,
|
|
84
|
+
animationTime: 400,
|
|
85
|
+
animationType: "easeOut",
|
|
86
|
+
equalToMove: true,
|
|
87
|
+
},
|
|
88
|
+
};
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Functions should return denominator of the target value, which is the next animation step.
|
|
3
|
+
* t is a value from 0 to 1, reflecting the percentage of animation status.
|
|
4
|
+
*/
|
|
5
|
+
const easeOut = function (t: number): number {
|
|
6
|
+
return -Math.cos(t * Math.PI) / 2 + 0.5;
|
|
7
|
+
};
|
|
8
|
+
// linear
|
|
9
|
+
const linear = function (t: number): number {
|
|
10
|
+
return t;
|
|
11
|
+
};
|
|
12
|
+
// accelerating from zero velocity
|
|
13
|
+
const easeInQuad = function (t: number): number {
|
|
14
|
+
return t * t;
|
|
15
|
+
};
|
|
16
|
+
// decelerating to zero velocity
|
|
17
|
+
const easeOutQuad = function (t: number): number {
|
|
18
|
+
return t * (2 - t);
|
|
19
|
+
};
|
|
20
|
+
// acceleration until halfway, then deceleration
|
|
21
|
+
const easeInOutQuad = function (t: number): number {
|
|
22
|
+
return t < 0.5 ? 2 * t * t : -1 + (4 - 2 * t) * t;
|
|
23
|
+
};
|
|
24
|
+
// accelerating from zero velocity
|
|
25
|
+
const easeInCubic = function (t: number): number {
|
|
26
|
+
return t * t * t;
|
|
27
|
+
};
|
|
28
|
+
// decelerating to zero velocity
|
|
29
|
+
const easeOutCubic = function (t: number): number {
|
|
30
|
+
return --t * t * t + 1;
|
|
31
|
+
};
|
|
32
|
+
// acceleration until halfway, then deceleration
|
|
33
|
+
const easeInOutCubic = function (t: number): number {
|
|
34
|
+
return t < 0.5 ? 4 * t * t * t : (t - 1) * (2 * t - 2) * (2 * t - 2) + 1;
|
|
35
|
+
};
|
|
36
|
+
// accelerating from zero velocity
|
|
37
|
+
const easeInQuart = function (t: number): number {
|
|
38
|
+
return t * t * t * t;
|
|
39
|
+
};
|
|
40
|
+
// decelerating to zero velocity
|
|
41
|
+
const easeOutQuart = function (t: number): number {
|
|
42
|
+
return 1 - --t * t * t * t;
|
|
43
|
+
};
|
|
44
|
+
// acceleration until halfway, then deceleration
|
|
45
|
+
const easeInOutQuart = function (t: number): number {
|
|
46
|
+
return t < 0.5 ? 8 * t * t * t * t : 1 - 8 * --t * t * t * t;
|
|
47
|
+
};
|
|
48
|
+
// accelerating from zero velocity
|
|
49
|
+
const easeInQuint = function (t: number): number {
|
|
50
|
+
return t * t * t * t * t;
|
|
51
|
+
};
|
|
52
|
+
// decelerating to zero velocity
|
|
53
|
+
const easeOutQuint = function (t: number): number {
|
|
54
|
+
return 1 + --t * t * t * t * t;
|
|
55
|
+
};
|
|
56
|
+
// acceleration until halfway, then deceleration
|
|
57
|
+
const easeInOutQuint = function (t: number): number {
|
|
58
|
+
return t < 0.5 ? 16 * t * t * t * t * t : 1 + 16 * --t * t * t * t * t;
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
export const animations = {
|
|
62
|
+
easeOut,
|
|
63
|
+
linear,
|
|
64
|
+
easeInQuad,
|
|
65
|
+
easeOutQuad,
|
|
66
|
+
easeInOutQuad,
|
|
67
|
+
easeInCubic,
|
|
68
|
+
easeOutCubic,
|
|
69
|
+
easeInOutCubic,
|
|
70
|
+
easeInQuart,
|
|
71
|
+
easeOutQuart,
|
|
72
|
+
easeInOutQuart,
|
|
73
|
+
easeInQuint,
|
|
74
|
+
easeOutQuint,
|
|
75
|
+
easeInOutQuint,
|
|
76
|
+
};
|