react-zoom-pan-pinch 3.7.0 → 4.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs.js +785 -310
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.d.ts +117 -23
- package/dist/index.esm.js +781 -302
- package/dist/index.esm.js.map +1 -1
- package/package.json +73 -62
- package/src/components/index.ts +5 -0
- package/src/components/keep-scale/keep-scale.tsx +28 -0
- package/src/components/mini-map/mini-map.tsx +292 -0
- package/src/components/mini-map/use-resize.hook.ts +74 -0
- package/src/components/transform-component/transform-component.module.css +43 -0
- package/src/components/transform-component/transform-component.tsx +96 -0
- package/src/components/transform-wrapper/transform-wrapper.tsx +39 -0
- package/src/components/virtualize/virtualize.tsx +133 -0
- package/src/components/virtualize/virtualize.utils.ts +104 -0
- package/src/constants/state.constants.ts +99 -0
- package/src/core/animations/animations.constants.ts +78 -0
- package/src/core/animations/animations.utils.ts +115 -0
- package/src/core/bounds/bounds.types.ts +8 -0
- package/src/core/bounds/bounds.utils.ts +226 -0
- package/src/core/double-click/double-click.logic.ts +127 -0
- package/src/core/handlers/handlers.logic.ts +122 -0
- package/src/core/handlers/handlers.utils.ts +243 -0
- package/src/core/instance.core.ts +704 -0
- package/src/core/pan/panning.logic.ts +122 -0
- package/src/core/pan/panning.utils.ts +216 -0
- package/src/core/pan/velocity.logic.ts +187 -0
- package/src/core/pan/velocity.utils.ts +83 -0
- package/src/core/pinch/pinch.logic.ts +139 -0
- package/src/core/pinch/pinch.utils.ts +95 -0
- package/src/core/wheel/wheel.logic.ts +161 -0
- package/src/core/wheel/wheel.utils.ts +157 -0
- package/src/core/zoom/zoom.logic.ts +71 -0
- package/src/core/zoom/zoom.utils.ts +62 -0
- package/src/hooks/index.ts +5 -0
- package/src/hooks/use-controls.tsx +9 -0
- package/src/hooks/use-transform-component.tsx +30 -0
- package/src/hooks/use-transform-context.tsx +13 -0
- package/src/hooks/use-transform-effect.tsx +22 -0
- package/src/hooks/use-transform-init.tsx +28 -0
- package/src/hooks/use-zoom-pan-pinch.tsx +48 -0
- package/src/index.ts +6 -0
- package/src/models/calculations.model.ts +34 -0
- package/src/models/context.model.ts +218 -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/map.jpg +0 -0
- package/src/stories/assets/medium-image.jpg +0 -0
- package/src/stories/assets/pin.svg +1 -0
- package/src/stories/assets/small-image.jpg +0 -0
- package/src/stories/assets/svg-example.svg +14 -0
- package/src/stories/docs/DocTable.tsx +59 -0
- package/src/stories/docs/HandlerCards.tsx +102 -0
- package/src/stories/docs/Introduction.stories.mdx +135 -0
- package/src/stories/docs/PropTable.tsx +116 -0
- package/src/stories/docs/docs.module.css +370 -0
- package/src/stories/docs/handlers.stories.mdx +7 -0
- package/src/stories/docs/handlers.tsx +111 -0
- package/src/stories/docs/props.stories.mdx +11 -0
- package/src/stories/docs/props.tsx +571 -0
- package/src/stories/examples/axis-lock/axis-lock.stories.mdx +35 -0
- package/src/stories/examples/axis-lock/example.tsx +114 -0
- package/src/stories/examples/bounds/bounds.data.tsx +191 -0
- package/src/stories/examples/bounds/bounds.stories.mdx +99 -0
- package/src/stories/examples/center-zoomed-out/center-zoomed-out.stories.mdx +206 -0
- package/src/stories/examples/cinema/cinema-data.tsx +578 -0
- package/src/stories/examples/cinema/cinema.stories.mdx +30 -0
- package/src/stories/examples/cinema/example.tsx +378 -0
- package/src/stories/examples/cinema/image.png +0 -0
- package/src/stories/examples/content/content.stories.mdx +172 -0
- package/src/stories/examples/content/mixed-content.data.tsx +624 -0
- package/src/stories/examples/content-rerendering/content-updating.stories.mdx +34 -0
- package/src/stories/examples/content-rerendering/example.tsx +631 -0
- package/src/stories/examples/controls/controls.stories.mdx +120 -0
- package/src/stories/examples/image/example.tsx +149 -0
- package/src/stories/examples/image/image.stories.mdx +46 -0
- package/src/stories/examples/image-annotations/example.tsx +1442 -0
- package/src/stories/examples/image-annotations/image-annotations.stories.mdx +39 -0
- package/src/stories/examples/image-responsive/example.tsx +152 -0
- package/src/stories/examples/image-responsive/image-responsive.stories.mdx +51 -0
- package/src/stories/examples/image-zoomed-out/image-zoomed-out.stories.mdx +107 -0
- package/src/stories/examples/initial-transform/example.tsx +233 -0
- package/src/stories/examples/initial-transform/initial-transform.stories.mdx +37 -0
- package/src/stories/examples/keep-scale/example.tsx +882 -0
- package/src/stories/examples/keep-scale/keep-scale.stories.mdx +30 -0
- package/src/stories/examples/keep-scale/map.stories.mdx +28 -0
- package/src/stories/examples/medical-viewer/chest-xray.tsx +246 -0
- package/src/stories/examples/medical-viewer/example.tsx +690 -0
- package/src/stories/examples/medical-viewer/medical-viewer.stories.mdx +49 -0
- package/src/stories/examples/mini-map/example.tsx +122 -0
- package/src/stories/examples/mini-map/mini-map.stories.mdx +26 -0
- package/src/stories/examples/miro/example.tsx +1573 -0
- package/src/stories/examples/miro/miro.stories.mdx +31 -0
- package/src/stories/examples/padding/example.tsx +147 -0
- package/src/stories/examples/padding/padding.stories.mdx +35 -0
- package/src/stories/examples/product-card/example.tsx +594 -0
- package/src/stories/examples/product-card/product-card.stories.mdx +29 -0
- package/src/stories/examples/stadium/example.tsx +1040 -0
- package/src/stories/examples/stadium/stadium-data.ts +224 -0
- package/src/stories/examples/stadium/stadium.stories.mdx +32 -0
- package/src/stories/examples/stress/example.tsx +59 -0
- package/src/stories/examples/stress/stress-test.stories.mdx +28 -0
- package/src/stories/examples/svg-zoom-to-element/creativity.svg +143 -0
- package/src/stories/examples/svg-zoom-to-element/example.tsx +95 -0
- package/src/stories/examples/svg-zoom-to-element/svg-zoom-to-element.stories.mdx +28 -0
- package/src/stories/examples/text-selection/example.tsx +221 -0
- package/src/stories/examples/text-selection/text-selection.stories.mdx +31 -0
- package/src/stories/examples/velocity/example.tsx +908 -0
- package/src/stories/examples/velocity/velocity.stories.mdx +35 -0
- package/src/stories/examples/virtualize/example.tsx +239 -0
- package/src/stories/examples/virtualize/virtualize.stories.mdx +84 -0
- package/src/stories/examples/zoom-to-element/example.tsx +219 -0
- package/src/stories/examples/zoom-to-element/zoom-to-element.stories.mdx +31 -0
- package/src/stories/hooks/use-controls.stories.mdx +86 -0
- package/src/stories/hooks/use-transform-component.stories.mdx +108 -0
- package/src/stories/hooks/use-transform-context.stories.mdx +92 -0
- package/src/stories/hooks/use-transform-effect.stories.mdx +114 -0
- package/src/stories/hooks/use-transform-init.stories.mdx +105 -0
- package/src/stories/types/args.types.ts +192 -0
- package/src/stories/utils/args.utils.ts +35 -0
- package/src/stories/utils/bounds-overlay.tsx +168 -0
- package/src/stories/utils/code.utils.tsx +22 -0
- package/src/stories/utils/controls.utils.tsx +347 -0
- package/src/stories/utils/index.ts +5 -0
- package/src/stories/utils/styles.module.css +223 -0
- package/src/stories/utils/viewer.styles.ts +34 -0
- package/src/styles.css +8 -0
- package/src/typings.d.ts +21 -0
- package/src/utils/calculations.utils.ts +16 -0
- package/src/utils/callback.utils.ts +11 -0
- package/src/utils/context.utils.ts +49 -0
- package/src/utils/event.utils.ts +38 -0
- package/src/utils/helpers.utils.ts +22 -0
- package/src/utils/index.ts +8 -0
- package/src/utils/ref.utils.ts +32 -0
- package/src/utils/state.utils.ts +77 -0
- package/src/utils/styles.utils.ts +49 -0
|
@@ -0,0 +1,704 @@
|
|
|
1
|
+
import {
|
|
2
|
+
BoundsType,
|
|
3
|
+
LibrarySetup,
|
|
4
|
+
PositionType,
|
|
5
|
+
VelocityType,
|
|
6
|
+
AnimationType,
|
|
7
|
+
ReactZoomPanPinchProps,
|
|
8
|
+
ReactZoomPanPinchState,
|
|
9
|
+
ReactZoomPanPinchRef,
|
|
10
|
+
DeviceType,
|
|
11
|
+
} from "../models";
|
|
12
|
+
import {
|
|
13
|
+
getContext,
|
|
14
|
+
createSetup,
|
|
15
|
+
createState,
|
|
16
|
+
handleCallback,
|
|
17
|
+
getTransformStyles,
|
|
18
|
+
makePassiveEventOption,
|
|
19
|
+
getCenterPosition,
|
|
20
|
+
assignRef,
|
|
21
|
+
} from "../utils";
|
|
22
|
+
import { handleCancelAnimation } from "./animations/animations.utils";
|
|
23
|
+
import { isWheelAllowed, isWheelPanningAllowed } from "./wheel/wheel.utils";
|
|
24
|
+
import { isPinchAllowed, isPinchStartAllowed } from "./pinch/pinch.utils";
|
|
25
|
+
import { handleCalculateBounds } from "./bounds/bounds.utils";
|
|
26
|
+
import {
|
|
27
|
+
handleWheelStart,
|
|
28
|
+
handleWheelZoom,
|
|
29
|
+
handleWheelStop,
|
|
30
|
+
handleWheelPanningStop,
|
|
31
|
+
handleWheelPanningStart,
|
|
32
|
+
} from "./wheel/wheel.logic";
|
|
33
|
+
import {
|
|
34
|
+
getPaddingValue,
|
|
35
|
+
handleNewPosition,
|
|
36
|
+
isPanningAllowed,
|
|
37
|
+
isPanningStartAllowed,
|
|
38
|
+
} from "./pan/panning.utils";
|
|
39
|
+
import {
|
|
40
|
+
handlePanning,
|
|
41
|
+
handlePanningEnd,
|
|
42
|
+
handlePanningStart,
|
|
43
|
+
} from "./pan/panning.logic";
|
|
44
|
+
import {
|
|
45
|
+
handlePinchStart,
|
|
46
|
+
handlePinchStop,
|
|
47
|
+
handlePinchZoom,
|
|
48
|
+
} from "./pinch/pinch.logic";
|
|
49
|
+
import {
|
|
50
|
+
handleDoubleClick,
|
|
51
|
+
isDoubleClickAllowed,
|
|
52
|
+
} from "./double-click/double-click.logic";
|
|
53
|
+
|
|
54
|
+
type StartCoordsType = { x: number; y: number } | null;
|
|
55
|
+
|
|
56
|
+
export class ZoomPanPinch {
|
|
57
|
+
public props: ReactZoomPanPinchProps;
|
|
58
|
+
|
|
59
|
+
public mounted = true;
|
|
60
|
+
|
|
61
|
+
public state: ReactZoomPanPinchState;
|
|
62
|
+
public setup: LibrarySetup;
|
|
63
|
+
public observer?: ResizeObserver;
|
|
64
|
+
public onChangeCallbacks: Set<(ctx: ReactZoomPanPinchRef) => void> =
|
|
65
|
+
new Set();
|
|
66
|
+
public onInitCallbacks: Set<(ctx: ReactZoomPanPinchRef) => void> = new Set();
|
|
67
|
+
public onTransformCallbacks: Set<
|
|
68
|
+
(data: {
|
|
69
|
+
scale: number;
|
|
70
|
+
positionX: number;
|
|
71
|
+
positionY: number;
|
|
72
|
+
previousScale: number;
|
|
73
|
+
ref: ReactZoomPanPinchRef;
|
|
74
|
+
}) => void
|
|
75
|
+
> = new Set();
|
|
76
|
+
|
|
77
|
+
// Components
|
|
78
|
+
public wrapperComponent: HTMLDivElement | null = null;
|
|
79
|
+
public contentComponent: HTMLDivElement | null = null;
|
|
80
|
+
// Initialization
|
|
81
|
+
public isInitialized = false;
|
|
82
|
+
public bounds: BoundsType | null = null;
|
|
83
|
+
// wheel helpers
|
|
84
|
+
public previousWheelEvent: WheelEvent | null = null;
|
|
85
|
+
public wheelStopEventTimer: ReturnType<typeof setTimeout> | null = null;
|
|
86
|
+
public wheelAnimationTimer: ReturnType<typeof setTimeout> | null = null;
|
|
87
|
+
// panning helpers
|
|
88
|
+
public isPanning = false;
|
|
89
|
+
public isWheelPanning = false;
|
|
90
|
+
public startCoords: StartCoordsType = null;
|
|
91
|
+
public panStartPosition: { x: number; y: number } | null = null;
|
|
92
|
+
public lastTouch: number | null = null;
|
|
93
|
+
// pinch helpers
|
|
94
|
+
public isPinching = false;
|
|
95
|
+
public distance: null | number = null;
|
|
96
|
+
public lastDistance: null | number = null;
|
|
97
|
+
public pinchStartDistance: null | number = null;
|
|
98
|
+
public pinchStartScale: null | number = null;
|
|
99
|
+
public pinchMidpoint: null | PositionType = null;
|
|
100
|
+
public pinchPreviousCenter: null | PositionType = null;
|
|
101
|
+
// double click helpers
|
|
102
|
+
public doubleClickStopEventTimer: ReturnType<typeof setTimeout> | null = null;
|
|
103
|
+
// velocity helpers
|
|
104
|
+
public velocity: VelocityType | null = null;
|
|
105
|
+
public velocityTime: number | null = null;
|
|
106
|
+
public lastMousePosition: PositionType | null = null;
|
|
107
|
+
// animations helpers
|
|
108
|
+
public isAnimating = false;
|
|
109
|
+
public animation: AnimationType | null = null;
|
|
110
|
+
// key press
|
|
111
|
+
public pressedKeys: { [key: string]: boolean } = {};
|
|
112
|
+
|
|
113
|
+
constructor(props: ReactZoomPanPinchProps) {
|
|
114
|
+
this.props = props;
|
|
115
|
+
this.setup = createSetup(this.props);
|
|
116
|
+
this.state = createState(this.props);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
mount = () => {
|
|
120
|
+
this.initializeWindowEvents();
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
unmount = () => {
|
|
124
|
+
this.cleanupWindowEvents();
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
update = (newProps: ReactZoomPanPinchProps) => {
|
|
128
|
+
this.props = newProps;
|
|
129
|
+
if (this.wrapperComponent && this.contentComponent) {
|
|
130
|
+
handleCalculateBounds(this, this.state.scale);
|
|
131
|
+
}
|
|
132
|
+
this.setup = createSetup(newProps);
|
|
133
|
+
};
|
|
134
|
+
|
|
135
|
+
initializeWindowEvents = (): void => {
|
|
136
|
+
const passive = makePassiveEventOption();
|
|
137
|
+
const currentDocument = this.wrapperComponent?.ownerDocument;
|
|
138
|
+
const currentWindow = currentDocument?.defaultView;
|
|
139
|
+
this.wrapperComponent?.addEventListener(
|
|
140
|
+
"wheel",
|
|
141
|
+
this.onWheelPanning,
|
|
142
|
+
passive,
|
|
143
|
+
);
|
|
144
|
+
this.wrapperComponent?.addEventListener(
|
|
145
|
+
"keyup",
|
|
146
|
+
this.setKeyUnPressed,
|
|
147
|
+
passive,
|
|
148
|
+
);
|
|
149
|
+
this.wrapperComponent?.addEventListener(
|
|
150
|
+
"keydown",
|
|
151
|
+
this.setKeyPressed,
|
|
152
|
+
passive,
|
|
153
|
+
);
|
|
154
|
+
// Panning on window to allow panning when mouse is out of component wrapper
|
|
155
|
+
currentWindow?.addEventListener("mousedown", this.onPanningStart, passive);
|
|
156
|
+
currentWindow?.addEventListener("mousemove", this.onPanning, passive);
|
|
157
|
+
currentWindow?.addEventListener("mouseup", this.onPanningStop, passive);
|
|
158
|
+
currentDocument?.addEventListener("mouseleave", this.clearPanning, passive);
|
|
159
|
+
currentWindow?.addEventListener("keyup", this.setKeyUnPressed, passive);
|
|
160
|
+
currentWindow?.addEventListener("keydown", this.setKeyPressed, passive);
|
|
161
|
+
currentWindow?.addEventListener("blur", this.handleWindowBlur);
|
|
162
|
+
};
|
|
163
|
+
|
|
164
|
+
cleanupWindowEvents = (): void => {
|
|
165
|
+
const passive = makePassiveEventOption();
|
|
166
|
+
const currentDocument = this.wrapperComponent?.ownerDocument;
|
|
167
|
+
const currentWindow = currentDocument?.defaultView;
|
|
168
|
+
currentWindow?.removeEventListener(
|
|
169
|
+
"mousedown",
|
|
170
|
+
this.onPanningStart,
|
|
171
|
+
passive,
|
|
172
|
+
);
|
|
173
|
+
currentWindow?.removeEventListener("mousemove", this.onPanning, passive);
|
|
174
|
+
currentWindow?.removeEventListener("mouseup", this.onPanningStop, passive);
|
|
175
|
+
currentDocument?.removeEventListener(
|
|
176
|
+
"mouseleave",
|
|
177
|
+
this.clearPanning,
|
|
178
|
+
passive,
|
|
179
|
+
);
|
|
180
|
+
currentWindow?.removeEventListener("keyup", this.setKeyUnPressed, passive);
|
|
181
|
+
currentWindow?.removeEventListener("keydown", this.setKeyPressed, passive);
|
|
182
|
+
currentWindow?.removeEventListener("blur", this.handleWindowBlur);
|
|
183
|
+
document.removeEventListener("mouseleave", this.clearPanning, passive);
|
|
184
|
+
this.wrapperComponent?.removeEventListener(
|
|
185
|
+
"wheel",
|
|
186
|
+
this.onWheelPanning,
|
|
187
|
+
passive,
|
|
188
|
+
);
|
|
189
|
+
this.wrapperComponent?.removeEventListener(
|
|
190
|
+
"keyup",
|
|
191
|
+
this.setKeyUnPressed,
|
|
192
|
+
passive,
|
|
193
|
+
);
|
|
194
|
+
this.wrapperComponent?.removeEventListener(
|
|
195
|
+
"keydown",
|
|
196
|
+
this.setKeyPressed,
|
|
197
|
+
passive,
|
|
198
|
+
);
|
|
199
|
+
handleCancelAnimation(this);
|
|
200
|
+
this.observer?.disconnect();
|
|
201
|
+
};
|
|
202
|
+
|
|
203
|
+
handleInitializeWrapperEvents = (wrapper: HTMLDivElement): void => {
|
|
204
|
+
// Zooming events on wrapper
|
|
205
|
+
const passive = makePassiveEventOption();
|
|
206
|
+
|
|
207
|
+
wrapper.addEventListener("wheel", this.onWheelZoom, passive);
|
|
208
|
+
wrapper.addEventListener("dblclick", this.onDoubleClick, passive);
|
|
209
|
+
wrapper.addEventListener("touchstart", this.onTouchPanningStart, passive);
|
|
210
|
+
wrapper.addEventListener("touchmove", this.onTouchPanning, passive);
|
|
211
|
+
wrapper.addEventListener("touchend", this.onTouchPanningStop, passive);
|
|
212
|
+
};
|
|
213
|
+
|
|
214
|
+
handleInitialize = (contentComponent: HTMLDivElement): void => {
|
|
215
|
+
const { centerOnInit } = this.setup;
|
|
216
|
+
this.applyTransformation();
|
|
217
|
+
this.onInitCallbacks.forEach((callback) => callback(getContext(this)));
|
|
218
|
+
|
|
219
|
+
if (centerOnInit) {
|
|
220
|
+
this.setCenter();
|
|
221
|
+
this.observer = new ResizeObserver(() => {
|
|
222
|
+
const currentWidth = contentComponent.offsetWidth;
|
|
223
|
+
const currentHeight = contentComponent.offsetHeight;
|
|
224
|
+
|
|
225
|
+
if (currentWidth > 0 || currentHeight > 0) {
|
|
226
|
+
this.onInitCallbacks.forEach((callback) =>
|
|
227
|
+
callback(getContext(this)),
|
|
228
|
+
);
|
|
229
|
+
this.setCenter();
|
|
230
|
+
|
|
231
|
+
this.observer?.disconnect();
|
|
232
|
+
}
|
|
233
|
+
});
|
|
234
|
+
|
|
235
|
+
// TODO: CHANGE to first interaction?
|
|
236
|
+
// if nothing about the contentComponent has changed after 5 seconds, disconnect the observer
|
|
237
|
+
setTimeout(() => {
|
|
238
|
+
this.observer?.disconnect();
|
|
239
|
+
}, 5000);
|
|
240
|
+
|
|
241
|
+
// Start observing the target node for configured mutations
|
|
242
|
+
this.observer.observe(contentComponent);
|
|
243
|
+
}
|
|
244
|
+
};
|
|
245
|
+
|
|
246
|
+
/// ///////
|
|
247
|
+
// Zoom
|
|
248
|
+
/// ///////
|
|
249
|
+
|
|
250
|
+
onWheelZoom = (event: WheelEvent): void => {
|
|
251
|
+
const { disabled } = this.setup;
|
|
252
|
+
if (disabled) return;
|
|
253
|
+
|
|
254
|
+
this.syncModifierKeys(event);
|
|
255
|
+
|
|
256
|
+
const isAllowed = isWheelAllowed(this, event);
|
|
257
|
+
if (!isAllowed) return;
|
|
258
|
+
|
|
259
|
+
handleWheelStart(this, event);
|
|
260
|
+
handleWheelZoom(this, event);
|
|
261
|
+
handleWheelStop(this, event);
|
|
262
|
+
};
|
|
263
|
+
|
|
264
|
+
/// ///////
|
|
265
|
+
// Track Pad Panning
|
|
266
|
+
/// ///////
|
|
267
|
+
|
|
268
|
+
onWheelPanning = (event: WheelEvent): void => {
|
|
269
|
+
const { onPanning } = this.props;
|
|
270
|
+
const { trackPadPanning } = this.setup;
|
|
271
|
+
const { lockAxisX, lockAxisY } = trackPadPanning;
|
|
272
|
+
|
|
273
|
+
this.syncModifierKeys(event);
|
|
274
|
+
|
|
275
|
+
const isAllowed = isWheelPanningAllowed(this, event);
|
|
276
|
+
|
|
277
|
+
if (!isAllowed) return;
|
|
278
|
+
|
|
279
|
+
event.preventDefault();
|
|
280
|
+
event.stopPropagation();
|
|
281
|
+
|
|
282
|
+
const { positionX, positionY } = this.state;
|
|
283
|
+
const mouseX = positionX - event.deltaX;
|
|
284
|
+
const mouseY = positionY - event.deltaY;
|
|
285
|
+
const newPositionX = lockAxisX ? positionX : mouseX;
|
|
286
|
+
const newPositionY = lockAxisY ? positionY : mouseY;
|
|
287
|
+
|
|
288
|
+
const { sizeX, sizeY } = this.setup.autoAlignment;
|
|
289
|
+
const paddingValueX = getPaddingValue(this, sizeX);
|
|
290
|
+
const paddingValueY = getPaddingValue(this, sizeY);
|
|
291
|
+
|
|
292
|
+
if (newPositionX === positionX && newPositionY === positionY) return;
|
|
293
|
+
|
|
294
|
+
handleWheelPanningStart(this, event);
|
|
295
|
+
handleNewPosition(
|
|
296
|
+
this,
|
|
297
|
+
newPositionX,
|
|
298
|
+
newPositionY,
|
|
299
|
+
paddingValueX,
|
|
300
|
+
paddingValueY,
|
|
301
|
+
);
|
|
302
|
+
handleCallback(getContext(this), event, onPanning);
|
|
303
|
+
handleWheelPanningStop(this, event);
|
|
304
|
+
};
|
|
305
|
+
|
|
306
|
+
/// ///////
|
|
307
|
+
// Pan
|
|
308
|
+
/// ///////
|
|
309
|
+
|
|
310
|
+
onPanningStart = (event: MouseEvent): void => {
|
|
311
|
+
const { disabled } = this.setup;
|
|
312
|
+
const { onPanningStart } = this.props;
|
|
313
|
+
if (disabled) return;
|
|
314
|
+
|
|
315
|
+
this.syncModifierKeys(event);
|
|
316
|
+
|
|
317
|
+
const isAllowed = isPanningStartAllowed(this, event);
|
|
318
|
+
if (!isAllowed) return;
|
|
319
|
+
|
|
320
|
+
const keysPressed = this.isPressingKeys(this.setup.panning.activationKeys);
|
|
321
|
+
if (!keysPressed) return;
|
|
322
|
+
|
|
323
|
+
if (event.button === 0 && !this.setup.panning.allowLeftClickPan) return;
|
|
324
|
+
if (event.button === 1 && !this.setup.panning.allowMiddleClickPan) return;
|
|
325
|
+
if (event.button === 2 && !this.setup.panning.allowRightClickPan) return;
|
|
326
|
+
|
|
327
|
+
event.preventDefault();
|
|
328
|
+
event.stopPropagation();
|
|
329
|
+
|
|
330
|
+
handleCancelAnimation(this);
|
|
331
|
+
handlePanningStart(this, event);
|
|
332
|
+
handleCallback(getContext(this), event, onPanningStart);
|
|
333
|
+
};
|
|
334
|
+
|
|
335
|
+
onPanning = (event: MouseEvent): void => {
|
|
336
|
+
const { disabled } = this.setup;
|
|
337
|
+
const { onPanning } = this.props;
|
|
338
|
+
|
|
339
|
+
if (disabled) return;
|
|
340
|
+
|
|
341
|
+
this.syncModifierKeys(event);
|
|
342
|
+
|
|
343
|
+
// Detect missed mouseup — e.g. when the mouse was released outside an
|
|
344
|
+
// iframe boundary where the host frame swallows the mouseup event.
|
|
345
|
+
if (this.isPanning && event.buttons === 0) {
|
|
346
|
+
this.clearPanning(event);
|
|
347
|
+
return;
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
const isAllowed = isPanningAllowed(this);
|
|
351
|
+
if (!isAllowed) return;
|
|
352
|
+
|
|
353
|
+
const keysPressed = this.isPressingKeys(this.setup.panning.activationKeys);
|
|
354
|
+
if (!keysPressed) return;
|
|
355
|
+
|
|
356
|
+
event.preventDefault();
|
|
357
|
+
event.stopPropagation();
|
|
358
|
+
|
|
359
|
+
handlePanning(this, event.clientX, event.clientY, DeviceType.MOUSE);
|
|
360
|
+
handleCallback(getContext(this), event, onPanning);
|
|
361
|
+
};
|
|
362
|
+
|
|
363
|
+
onPanningStop = (event: MouseEvent | TouchEvent): void => {
|
|
364
|
+
const { velocityDisabled } = this.setup.panning;
|
|
365
|
+
const { onPanningStop } = this.props;
|
|
366
|
+
|
|
367
|
+
if (this.isPanning) {
|
|
368
|
+
handlePanningEnd(this, velocityDisabled);
|
|
369
|
+
handleCallback(getContext(this), event, onPanningStop);
|
|
370
|
+
}
|
|
371
|
+
};
|
|
372
|
+
|
|
373
|
+
/// ///////
|
|
374
|
+
// Pinch
|
|
375
|
+
/// ///////
|
|
376
|
+
|
|
377
|
+
onPinchStart = (event: TouchEvent): void => {
|
|
378
|
+
const { disabled } = this.setup;
|
|
379
|
+
const { onPinchStart } = this.props;
|
|
380
|
+
|
|
381
|
+
if (disabled) return;
|
|
382
|
+
|
|
383
|
+
const isAllowed = isPinchStartAllowed(this, event);
|
|
384
|
+
if (!isAllowed) return;
|
|
385
|
+
|
|
386
|
+
handlePinchStart(this, event);
|
|
387
|
+
handleCancelAnimation(this);
|
|
388
|
+
handleCallback(getContext(this), event, onPinchStart);
|
|
389
|
+
};
|
|
390
|
+
|
|
391
|
+
onPinch = (event: TouchEvent): void => {
|
|
392
|
+
const { disabled } = this.setup;
|
|
393
|
+
const { onPinch } = this.props;
|
|
394
|
+
|
|
395
|
+
if (disabled) return;
|
|
396
|
+
|
|
397
|
+
const isAllowed = isPinchAllowed(this);
|
|
398
|
+
if (!isAllowed) return;
|
|
399
|
+
|
|
400
|
+
event.preventDefault();
|
|
401
|
+
event.stopPropagation();
|
|
402
|
+
|
|
403
|
+
handlePinchZoom(this, event);
|
|
404
|
+
handleCallback(getContext(this), event, onPinch);
|
|
405
|
+
};
|
|
406
|
+
|
|
407
|
+
onPinchStop = (event: TouchEvent): void => {
|
|
408
|
+
const { onPinchStop } = this.props;
|
|
409
|
+
|
|
410
|
+
if (this.pinchStartScale) {
|
|
411
|
+
handlePinchStop(this);
|
|
412
|
+
handleCallback(getContext(this), event, onPinchStop);
|
|
413
|
+
}
|
|
414
|
+
};
|
|
415
|
+
|
|
416
|
+
/// ///////
|
|
417
|
+
// Touch
|
|
418
|
+
/// ///////
|
|
419
|
+
|
|
420
|
+
onTouchPanningStart = (event: TouchEvent): void => {
|
|
421
|
+
const { disabled, doubleClick } = this.setup;
|
|
422
|
+
const { onPanningStart } = this.props;
|
|
423
|
+
|
|
424
|
+
if (disabled) return;
|
|
425
|
+
|
|
426
|
+
const isDoubleTapAllowed = !doubleClick?.disabled;
|
|
427
|
+
const isDoubleTap = this.lastTouch && +new Date() - this.lastTouch < 200;
|
|
428
|
+
|
|
429
|
+
if (isDoubleTapAllowed && isDoubleTap && event.touches.length === 1) {
|
|
430
|
+
this.onDoubleClick(event);
|
|
431
|
+
} else {
|
|
432
|
+
this.lastTouch = +new Date();
|
|
433
|
+
|
|
434
|
+
handleCancelAnimation(this);
|
|
435
|
+
|
|
436
|
+
const { touches } = event;
|
|
437
|
+
|
|
438
|
+
const isPanningAction = touches.length === 1;
|
|
439
|
+
const isPinchAction = touches.length === 2;
|
|
440
|
+
|
|
441
|
+
const isAllowed = isPanningStartAllowed(this, event);
|
|
442
|
+
if (isPanningAction) {
|
|
443
|
+
if (!isAllowed) return;
|
|
444
|
+
handleCancelAnimation(this);
|
|
445
|
+
handlePanningStart(this, event);
|
|
446
|
+
handleCallback(getContext(this), event, onPanningStart);
|
|
447
|
+
}
|
|
448
|
+
if (isPinchAction) {
|
|
449
|
+
this.onPinchStart(event);
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
};
|
|
453
|
+
|
|
454
|
+
onTouchPanning = (event: TouchEvent): void => {
|
|
455
|
+
const { disabled } = this.setup;
|
|
456
|
+
const { onPanning } = this.props;
|
|
457
|
+
|
|
458
|
+
if (this.isPanning && event.touches.length === 1) {
|
|
459
|
+
if (disabled) return;
|
|
460
|
+
|
|
461
|
+
const isAllowed = isPanningAllowed(this);
|
|
462
|
+
if (!isAllowed) return;
|
|
463
|
+
|
|
464
|
+
if (event.cancelable) {
|
|
465
|
+
event.preventDefault();
|
|
466
|
+
}
|
|
467
|
+
event.stopPropagation();
|
|
468
|
+
|
|
469
|
+
const touch = event.touches[0];
|
|
470
|
+
handlePanning(this, touch.clientX, touch.clientY, DeviceType.TOUCH);
|
|
471
|
+
handleCallback(getContext(this), event, onPanning);
|
|
472
|
+
} else if (event.touches.length > 1) {
|
|
473
|
+
this.onPinch(event);
|
|
474
|
+
}
|
|
475
|
+
};
|
|
476
|
+
|
|
477
|
+
onTouchPanningStop = (event: TouchEvent): void => {
|
|
478
|
+
this.onPanningStop(event);
|
|
479
|
+
this.onPinchStop(event);
|
|
480
|
+
};
|
|
481
|
+
|
|
482
|
+
/// ///////
|
|
483
|
+
// Double Click
|
|
484
|
+
/// ///////
|
|
485
|
+
|
|
486
|
+
onDoubleClick = (event: MouseEvent | TouchEvent): void => {
|
|
487
|
+
const { disabled } = this.setup;
|
|
488
|
+
if (disabled) return;
|
|
489
|
+
|
|
490
|
+
const isAllowed = isDoubleClickAllowed(this, event);
|
|
491
|
+
if (!isAllowed) return;
|
|
492
|
+
|
|
493
|
+
handleDoubleClick(this, event);
|
|
494
|
+
};
|
|
495
|
+
|
|
496
|
+
/// ///////
|
|
497
|
+
// Helpers
|
|
498
|
+
/// ///////
|
|
499
|
+
|
|
500
|
+
clearPanning = (event: MouseEvent): void => {
|
|
501
|
+
if (this.isPanning) {
|
|
502
|
+
this.onPanningStop(event);
|
|
503
|
+
}
|
|
504
|
+
};
|
|
505
|
+
|
|
506
|
+
// When the window loses focus (e.g. user clicks outside an iframe),
|
|
507
|
+
// keyup and mouseup events are swallowed by the parent frame. Clear all
|
|
508
|
+
// tracked state to prevent stale activation keys or ghost panning.
|
|
509
|
+
handleWindowBlur = (): void => {
|
|
510
|
+
this.pressedKeys = {};
|
|
511
|
+
if (this.isPanning) {
|
|
512
|
+
this.isPanning = false;
|
|
513
|
+
this.startCoords = null;
|
|
514
|
+
}
|
|
515
|
+
};
|
|
516
|
+
|
|
517
|
+
// Iframe focus problem (e.g. Storybook):
|
|
518
|
+
//
|
|
519
|
+
// When the library runs inside an iframe, keyboard events (keydown/keyup)
|
|
520
|
+
// only reach the iframe's window when it has focus. If the user navigates
|
|
521
|
+
// via the host UI (e.g. Storybook sidebar) the iframe never receives
|
|
522
|
+
// focus, so keydown never fires and activationKeys like Cmd/Ctrl are
|
|
523
|
+
// invisible to pressedKeys.
|
|
524
|
+
//
|
|
525
|
+
// Mouse and wheel events, however, DO reach the iframe regardless of
|
|
526
|
+
// focus — and they carry modifier flags (ctrlKey, metaKey, shiftKey,
|
|
527
|
+
// altKey) that always reflect the real physical key state at event time.
|
|
528
|
+
//
|
|
529
|
+
// We sync those flags into pressedKeys on every interaction event so
|
|
530
|
+
// that activationKeys checks work without requiring iframe focus.
|
|
531
|
+
// Both pressed (true) AND released (false) states must be written;
|
|
532
|
+
// writing only `true` would leave stale keys after release because
|
|
533
|
+
// keyup never fires in an unfocused iframe.
|
|
534
|
+
syncModifierKeys = (event: MouseEvent | WheelEvent | TouchEvent): void => {
|
|
535
|
+
const { ctrlKey, metaKey, shiftKey, altKey } = event;
|
|
536
|
+
|
|
537
|
+
if (typeof ctrlKey === "boolean") this.pressedKeys.Control = ctrlKey;
|
|
538
|
+
if (typeof metaKey === "boolean") this.pressedKeys.Meta = metaKey;
|
|
539
|
+
if (typeof shiftKey === "boolean") this.pressedKeys.Shift = shiftKey;
|
|
540
|
+
if (typeof altKey === "boolean") this.pressedKeys.Alt = altKey;
|
|
541
|
+
};
|
|
542
|
+
|
|
543
|
+
setKeyPressed = (e: KeyboardEvent): void => {
|
|
544
|
+
this.pressedKeys[e.key] = true;
|
|
545
|
+
};
|
|
546
|
+
|
|
547
|
+
setKeyUnPressed = (e: KeyboardEvent): void => {
|
|
548
|
+
this.pressedKeys[e.key] = false;
|
|
549
|
+
};
|
|
550
|
+
|
|
551
|
+
isPressingKeys = (
|
|
552
|
+
keys: string[] | ((keys: string[]) => boolean),
|
|
553
|
+
): boolean => {
|
|
554
|
+
if (typeof keys === "function") {
|
|
555
|
+
return keys(
|
|
556
|
+
Object.entries(this.pressedKeys)
|
|
557
|
+
.filter(([, pressed]) => pressed)
|
|
558
|
+
.map(([key]) => key),
|
|
559
|
+
);
|
|
560
|
+
}
|
|
561
|
+
if (!keys.length) {
|
|
562
|
+
return true;
|
|
563
|
+
}
|
|
564
|
+
return Boolean(keys.every((key) => this.pressedKeys[key]));
|
|
565
|
+
};
|
|
566
|
+
|
|
567
|
+
setCenter = (): void => {
|
|
568
|
+
if (this.wrapperComponent && this.contentComponent) {
|
|
569
|
+
const targetState = getCenterPosition(
|
|
570
|
+
this.state.scale,
|
|
571
|
+
this.wrapperComponent,
|
|
572
|
+
this.contentComponent,
|
|
573
|
+
);
|
|
574
|
+
this.setState(
|
|
575
|
+
targetState.scale,
|
|
576
|
+
targetState.positionX,
|
|
577
|
+
targetState.positionY,
|
|
578
|
+
);
|
|
579
|
+
}
|
|
580
|
+
};
|
|
581
|
+
|
|
582
|
+
handleTransformStyles = (x: number, y: number, scale: number) => {
|
|
583
|
+
if (this.props.customTransform) {
|
|
584
|
+
return this.props.customTransform(x, y, scale);
|
|
585
|
+
}
|
|
586
|
+
return getTransformStyles(x, y, scale);
|
|
587
|
+
};
|
|
588
|
+
|
|
589
|
+
getContext = () => {
|
|
590
|
+
return getContext(this);
|
|
591
|
+
};
|
|
592
|
+
|
|
593
|
+
applyTransformation = (): void => {
|
|
594
|
+
if (!this.mounted || !this.contentComponent) return;
|
|
595
|
+
const { scale, positionX, positionY } = this.state;
|
|
596
|
+
const transform = this.handleTransformStyles(positionX, positionY, scale);
|
|
597
|
+
|
|
598
|
+
// Detached mode do not apply transformation directly to content component
|
|
599
|
+
if (!this.props.detached) {
|
|
600
|
+
this.contentComponent.style.transform = transform;
|
|
601
|
+
}
|
|
602
|
+
|
|
603
|
+
this.onTransformCallbacks.forEach((callback) =>
|
|
604
|
+
callback({
|
|
605
|
+
scale,
|
|
606
|
+
positionX,
|
|
607
|
+
positionY,
|
|
608
|
+
previousScale: this.state.previousScale,
|
|
609
|
+
ref: getContext(this),
|
|
610
|
+
}),
|
|
611
|
+
);
|
|
612
|
+
};
|
|
613
|
+
|
|
614
|
+
setState = (scale: number, positionX: number, positionY: number): void => {
|
|
615
|
+
const { onTransform } = this.props;
|
|
616
|
+
|
|
617
|
+
if (
|
|
618
|
+
!Number.isNaN(scale) &&
|
|
619
|
+
!Number.isNaN(positionX) &&
|
|
620
|
+
!Number.isNaN(positionY)
|
|
621
|
+
) {
|
|
622
|
+
const safeScale = Math.max(scale, 1e-7);
|
|
623
|
+
|
|
624
|
+
if (safeScale !== this.state.scale) {
|
|
625
|
+
this.state.previousScale = this.state.scale;
|
|
626
|
+
this.state.scale = safeScale;
|
|
627
|
+
}
|
|
628
|
+
|
|
629
|
+
this.state.positionX = positionX;
|
|
630
|
+
this.state.positionY = positionY;
|
|
631
|
+
|
|
632
|
+
this.applyTransformation();
|
|
633
|
+
const ctx = getContext(this);
|
|
634
|
+
this.onChangeCallbacks.forEach((callback) => callback(ctx));
|
|
635
|
+
handleCallback(
|
|
636
|
+
ctx,
|
|
637
|
+
{ scale: this.state.scale, positionX, positionY },
|
|
638
|
+
onTransform,
|
|
639
|
+
);
|
|
640
|
+
} else {
|
|
641
|
+
console.error("Detected NaN set state values");
|
|
642
|
+
}
|
|
643
|
+
};
|
|
644
|
+
|
|
645
|
+
/**
|
|
646
|
+
* Hooks
|
|
647
|
+
*/
|
|
648
|
+
|
|
649
|
+
onTransform = (
|
|
650
|
+
callback: (data: {
|
|
651
|
+
scale: number;
|
|
652
|
+
positionX: number;
|
|
653
|
+
positionY: number;
|
|
654
|
+
previousScale: number;
|
|
655
|
+
ref: ReactZoomPanPinchRef;
|
|
656
|
+
}) => void,
|
|
657
|
+
) => {
|
|
658
|
+
if (!this.onTransformCallbacks.has(callback)) {
|
|
659
|
+
this.onTransformCallbacks.add(callback);
|
|
660
|
+
}
|
|
661
|
+
return () => {
|
|
662
|
+
this.onTransformCallbacks.delete(callback);
|
|
663
|
+
};
|
|
664
|
+
};
|
|
665
|
+
|
|
666
|
+
onChange = (callback: (ref: ReactZoomPanPinchRef) => void) => {
|
|
667
|
+
if (!this.onChangeCallbacks.has(callback)) {
|
|
668
|
+
this.onChangeCallbacks.add(callback);
|
|
669
|
+
}
|
|
670
|
+
return () => {
|
|
671
|
+
this.onChangeCallbacks.delete(callback);
|
|
672
|
+
};
|
|
673
|
+
};
|
|
674
|
+
|
|
675
|
+
onInit = (callback: (ref: ReactZoomPanPinchRef) => void) => {
|
|
676
|
+
if (!this.onInitCallbacks.has(callback)) {
|
|
677
|
+
this.onInitCallbacks.add(callback);
|
|
678
|
+
}
|
|
679
|
+
return () => {
|
|
680
|
+
this.onInitCallbacks.delete(callback);
|
|
681
|
+
};
|
|
682
|
+
};
|
|
683
|
+
|
|
684
|
+
/**
|
|
685
|
+
* Initialization
|
|
686
|
+
*/
|
|
687
|
+
|
|
688
|
+
init = (
|
|
689
|
+
wrapperComponent: HTMLDivElement,
|
|
690
|
+
contentComponent: HTMLDivElement,
|
|
691
|
+
): void => {
|
|
692
|
+
this.cleanupWindowEvents();
|
|
693
|
+
this.wrapperComponent = wrapperComponent;
|
|
694
|
+
this.contentComponent = contentComponent;
|
|
695
|
+
handleCalculateBounds(this, this.state.scale);
|
|
696
|
+
this.handleInitializeWrapperEvents(wrapperComponent);
|
|
697
|
+
this.handleInitialize(contentComponent);
|
|
698
|
+
this.initializeWindowEvents();
|
|
699
|
+
this.isInitialized = true;
|
|
700
|
+
const ctx = getContext(this);
|
|
701
|
+
handleCallback(ctx, undefined, this.props.onInit);
|
|
702
|
+
assignRef(this.props.ref, ctx);
|
|
703
|
+
};
|
|
704
|
+
}
|