react-zoom-pan-pinch 4.1.1 → 4.3.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/README.md +4 -4
- package/dist/index.cjs.js +1081 -231
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.d.ts +99 -3
- package/dist/index.esm.js +1082 -232
- package/dist/index.esm.js.map +1 -1
- package/package.json +1 -1
- package/src/components/mini-map/mini-map.tsx +386 -171
- package/src/components/transform-component/transform-component.module.css +2 -5
- package/src/components/transform-component/transform-component.tsx +7 -0
- package/src/components/virtualize/virtualize.tsx +2 -2
- package/src/constants/state.constants.ts +9 -0
- package/src/core/animations/animations.utils.ts +2 -0
- package/src/core/bounds/bounds.utils.ts +90 -2
- package/src/core/handlers/handlers.logic.ts +85 -0
- package/src/core/handlers/handlers.utils.ts +102 -5
- package/src/core/instance.core.ts +222 -41
- package/src/core/keyboard/keyboard.logic.ts +84 -0
- package/src/core/pan/panning.logic.ts +24 -4
- package/src/core/pan/panning.utils.ts +95 -36
- package/src/core/pan/velocity.logic.ts +11 -4
- package/src/core/pan/velocity.utils.ts +2 -2
- package/src/core/resize/index.ts +0 -0
- package/src/core/resize/resize.logic.ts +245 -0
- package/src/core/resize/resize.types.ts +6 -0
- package/src/core/wheel/wheel.logic.ts +11 -2
- package/src/core/zoom/zoom.logic.ts +2 -2
- package/src/hooks/initial-layout.utils.ts +21 -0
- package/src/hooks/use-transform-component.tsx +22 -4
- package/src/hooks/use-transform-effect.tsx +20 -1
- package/src/models/context.model.ts +34 -0
- package/src/utils/context.utils.ts +4 -0
- package/src/utils/styles.utils.ts +2 -2
|
@@ -17,9 +17,17 @@ import {
|
|
|
17
17
|
getTransformStyles,
|
|
18
18
|
makePassiveEventOption,
|
|
19
19
|
getCenterPosition,
|
|
20
|
-
assignRef,
|
|
21
20
|
} from "../utils";
|
|
22
21
|
import { handleCancelAnimation } from "./animations/animations.utils";
|
|
22
|
+
import { handleKeyboardNavigation } from "./keyboard/keyboard.logic";
|
|
23
|
+
import {
|
|
24
|
+
handleResizeAlignment,
|
|
25
|
+
handleSizeChange,
|
|
26
|
+
isGestureActive,
|
|
27
|
+
measureSizes,
|
|
28
|
+
} from "./resize/resize.logic";
|
|
29
|
+
import { MeasuredSizesType } from "./resize/resize.types";
|
|
30
|
+
import { calculateFitToView } from "./handlers/handlers.utils";
|
|
23
31
|
import { isWheelAllowed, isWheelPanningAllowed } from "./wheel/wheel.utils";
|
|
24
32
|
import { isPinchAllowed, isPinchStartAllowed } from "./pinch/pinch.utils";
|
|
25
33
|
import { handleCalculateBounds } from "./bounds/bounds.utils";
|
|
@@ -32,6 +40,7 @@ import {
|
|
|
32
40
|
} from "./wheel/wheel.logic";
|
|
33
41
|
import {
|
|
34
42
|
getPaddingValue,
|
|
43
|
+
getPanSourcePosition,
|
|
35
44
|
handleNewPosition,
|
|
36
45
|
isPanningAllowed,
|
|
37
46
|
isPanningStartAllowed,
|
|
@@ -112,8 +121,23 @@ export class ZoomPanPinch {
|
|
|
112
121
|
public animationResolve: (() => void) | null = null;
|
|
113
122
|
// init helpers
|
|
114
123
|
public initObserverTimer: ReturnType<typeof setTimeout> | null = null;
|
|
124
|
+
// `centerOnInit`/`fitOnInit` wait for content that has no size yet
|
|
125
|
+
public isInitialLayoutPending = false;
|
|
126
|
+
// resize helpers: last measured sizes and an alignment postponed until the
|
|
127
|
+
// running animation settles
|
|
128
|
+
public measuredSizes: MeasuredSizesType | null = null;
|
|
129
|
+
public isResizeAlignmentPending = false;
|
|
130
|
+
// The alignment animation started by a resize, so further size changes can
|
|
131
|
+
// retarget it instead of waiting for it (compared by identity to
|
|
132
|
+
// `animation`, which is null once it has finished).
|
|
133
|
+
public resizeAnimation: AnimationType | null = null;
|
|
115
134
|
// key press
|
|
116
135
|
public pressedKeys: { [key: string]: boolean } = {};
|
|
136
|
+
// text selection lock while a gesture is active (#467)
|
|
137
|
+
public selectionLock: {
|
|
138
|
+
userSelect: string;
|
|
139
|
+
webkitUserSelect: string;
|
|
140
|
+
} | null = null;
|
|
117
141
|
|
|
118
142
|
constructor(props: ReactZoomPanPinchProps) {
|
|
119
143
|
this.props = props;
|
|
@@ -138,7 +162,14 @@ export class ZoomPanPinch {
|
|
|
138
162
|
// Setup first: bounds depend on the new props (min/max positions,
|
|
139
163
|
// centerZoomedOut, disablePadding).
|
|
140
164
|
this.setup = createSetup(newProps);
|
|
141
|
-
|
|
165
|
+
// A gesture owns the bounds until it ends (see `handleResizeAlignment`):
|
|
166
|
+
// replacing them mid-drag makes the next move clamp against a new limit
|
|
167
|
+
// and jump. The gesture start recalculates them anyway.
|
|
168
|
+
if (
|
|
169
|
+
this.wrapperComponent &&
|
|
170
|
+
this.contentComponent &&
|
|
171
|
+
!isGestureActive(this)
|
|
172
|
+
) {
|
|
142
173
|
handleCalculateBounds(this, this.state.scale);
|
|
143
174
|
}
|
|
144
175
|
};
|
|
@@ -222,7 +253,12 @@ export class ZoomPanPinch {
|
|
|
222
253
|
this.cleanupWrapperEvents();
|
|
223
254
|
this.clearTimers();
|
|
224
255
|
handleCancelAnimation(this);
|
|
256
|
+
this.unlockTextSelection();
|
|
225
257
|
this.observer?.disconnect();
|
|
258
|
+
this.observer = undefined;
|
|
259
|
+
this.isInitialLayoutPending = false;
|
|
260
|
+
this.isResizeAlignmentPending = false;
|
|
261
|
+
this.resizeAnimation = null;
|
|
226
262
|
};
|
|
227
263
|
|
|
228
264
|
cleanupWrapperEvents = (): void => {
|
|
@@ -232,6 +268,7 @@ export class ZoomPanPinch {
|
|
|
232
268
|
|
|
233
269
|
wrapper.removeEventListener("wheel", this.onWheelZoom, passive);
|
|
234
270
|
wrapper.removeEventListener("dblclick", this.onDoubleClick, passive);
|
|
271
|
+
wrapper.removeEventListener("keydown", this.onKeyboardNavigation, passive);
|
|
235
272
|
wrapper.removeEventListener(
|
|
236
273
|
"touchstart",
|
|
237
274
|
this.onTouchPanningStart,
|
|
@@ -247,44 +284,94 @@ export class ZoomPanPinch {
|
|
|
247
284
|
|
|
248
285
|
wrapper.addEventListener("wheel", this.onWheelZoom, passive);
|
|
249
286
|
wrapper.addEventListener("dblclick", this.onDoubleClick, passive);
|
|
287
|
+
wrapper.addEventListener("keydown", this.onKeyboardNavigation, passive);
|
|
250
288
|
wrapper.addEventListener("touchstart", this.onTouchPanningStart, passive);
|
|
251
289
|
wrapper.addEventListener("touchmove", this.onTouchPanning, passive);
|
|
252
290
|
wrapper.addEventListener("touchend", this.onTouchPanningStop, passive);
|
|
253
291
|
};
|
|
254
292
|
|
|
255
|
-
handleInitialize = (
|
|
256
|
-
const { centerOnInit } = this.setup;
|
|
293
|
+
handleInitialize = (): void => {
|
|
294
|
+
const { centerOnInit, fitOnInit } = this.setup;
|
|
257
295
|
this.applyTransformation();
|
|
258
296
|
this.onInitCallbacks.forEach((callback) => callback(getContext(this)));
|
|
259
297
|
|
|
260
|
-
if (centerOnInit) {
|
|
261
|
-
this.
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
callback(getContext(this)),
|
|
270
|
-
);
|
|
271
|
-
this.setCenter();
|
|
272
|
-
|
|
273
|
-
this.observer?.disconnect();
|
|
274
|
-
}
|
|
275
|
-
});
|
|
276
|
-
|
|
277
|
-
// TODO: CHANGE to first interaction?
|
|
278
|
-
// if nothing about the contentComponent has changed after 5 seconds, disconnect the observer
|
|
298
|
+
if (centerOnInit || fitOnInit) {
|
|
299
|
+
const applied = this.applyInitialLayout();
|
|
300
|
+
// Content without a size yet (an image still loading) gets its layout
|
|
301
|
+
// applied again by `onSizeChange` once it has one. Once a real layout
|
|
302
|
+
// exists, stop waiting after 5 seconds so a later resize aligns instead
|
|
303
|
+
// of re-centring. Until then keep waiting: in a hidden tab the observer
|
|
304
|
+
// may not deliver for a long time and the first layout must still be
|
|
305
|
+
// the fitted/centred one.
|
|
306
|
+
this.isInitialLayoutPending = true;
|
|
279
307
|
if (this.initObserverTimer) clearTimeout(this.initObserverTimer);
|
|
280
|
-
|
|
281
|
-
this.initObserverTimer =
|
|
282
|
-
|
|
283
|
-
|
|
308
|
+
if (applied) {
|
|
309
|
+
this.initObserverTimer = setTimeout(() => {
|
|
310
|
+
this.initObserverTimer = null;
|
|
311
|
+
this.isInitialLayoutPending = false;
|
|
312
|
+
}, 5000);
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
};
|
|
316
|
+
|
|
317
|
+
/// ///////
|
|
318
|
+
// Size changes
|
|
319
|
+
/// ///////
|
|
320
|
+
|
|
321
|
+
// One observer for the whole lifetime of the instance: it finishes the
|
|
322
|
+
// initial layout for late-sized content and, after that, keeps the
|
|
323
|
+
// transform inside the bounds whenever the content or the wrapper changes
|
|
324
|
+
// size (children re-rendering with another size, images loading, the
|
|
325
|
+
// viewport resizing).
|
|
326
|
+
observeSizes = (
|
|
327
|
+
wrapperComponent: HTMLDivElement,
|
|
328
|
+
contentComponent: HTMLDivElement,
|
|
329
|
+
): void => {
|
|
330
|
+
this.observer?.disconnect();
|
|
331
|
+
this.observer = undefined;
|
|
332
|
+
this.measuredSizes = measureSizes(wrapperComponent, contentComponent);
|
|
284
333
|
|
|
285
|
-
|
|
286
|
-
|
|
334
|
+
if (typeof ResizeObserver === "undefined") return;
|
|
335
|
+
|
|
336
|
+
this.observer = new ResizeObserver(this.onSizeChange);
|
|
337
|
+
this.observer.observe(wrapperComponent);
|
|
338
|
+
this.observer.observe(contentComponent);
|
|
339
|
+
};
|
|
340
|
+
|
|
341
|
+
onSizeChange = (): void => {
|
|
342
|
+
const { wrapperComponent, contentComponent } = this;
|
|
343
|
+
if (!this.mounted || !wrapperComponent || !contentComponent) return;
|
|
344
|
+
|
|
345
|
+
if (this.isInitialLayoutPending) {
|
|
346
|
+
// Both boxes must be laid out: a wrapper that is sized late (hidden
|
|
347
|
+
// tab, collapsed panel) would otherwise get a fit against a 0px box.
|
|
348
|
+
const hasSize =
|
|
349
|
+
(contentComponent.offsetWidth > 0 ||
|
|
350
|
+
contentComponent.offsetHeight > 0) &&
|
|
351
|
+
wrapperComponent.clientWidth > 0 &&
|
|
352
|
+
wrapperComponent.clientHeight > 0;
|
|
353
|
+
if (!hasSize) return;
|
|
354
|
+
|
|
355
|
+
this.isInitialLayoutPending = false;
|
|
356
|
+
if (this.initObserverTimer) clearTimeout(this.initObserverTimer);
|
|
357
|
+
this.initObserverTimer = null;
|
|
358
|
+
this.onInitCallbacks.forEach((callback) => callback(getContext(this)));
|
|
359
|
+
this.applyInitialLayout();
|
|
360
|
+
this.measuredSizes = measureSizes(wrapperComponent, contentComponent);
|
|
361
|
+
handleCalculateBounds(this, this.state.scale);
|
|
362
|
+
return;
|
|
287
363
|
}
|
|
364
|
+
|
|
365
|
+
handleSizeChange(this);
|
|
366
|
+
};
|
|
367
|
+
|
|
368
|
+
// Called once a gesture or an animation has ended, so a resize seen while
|
|
369
|
+
// it ran is checked against the settled state (it may postpone itself
|
|
370
|
+
// again when another gesture or animation is still running).
|
|
371
|
+
flushResizeAlignment = (): void => {
|
|
372
|
+
if (!this.isResizeAlignmentPending) return;
|
|
373
|
+
this.isResizeAlignmentPending = false;
|
|
374
|
+
handleResizeAlignment(this);
|
|
288
375
|
};
|
|
289
376
|
|
|
290
377
|
/// ///////
|
|
@@ -325,19 +412,23 @@ export class ZoomPanPinch {
|
|
|
325
412
|
}
|
|
326
413
|
event.stopPropagation();
|
|
327
414
|
|
|
328
|
-
const
|
|
329
|
-
const
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
const newPositionY = lockAxisY ? positionY : mouseY;
|
|
415
|
+
const deltaX = lockAxisX ? 0 : event.deltaX;
|
|
416
|
+
const deltaY = lockAxisY ? 0 : event.deltaY;
|
|
417
|
+
|
|
418
|
+
if (!deltaX && !deltaY) return;
|
|
333
419
|
|
|
334
420
|
const { sizeX, sizeY } = this.setup.autoAlignment;
|
|
335
421
|
const paddingValueX = getPaddingValue(this, sizeX);
|
|
336
422
|
const paddingValueY = getPaddingValue(this, sizeY);
|
|
337
423
|
|
|
338
|
-
if (newPositionX === positionX && newPositionY === positionY) return;
|
|
339
|
-
|
|
340
424
|
handleWheelPanningStart(this, event);
|
|
425
|
+
|
|
426
|
+
// Deltas add up on the pointer's side of the rubber band, the same way
|
|
427
|
+
// a drag does, so the content slows down towards the padding limit.
|
|
428
|
+
const source = getPanSourcePosition(this);
|
|
429
|
+
const newPositionX = source.x - deltaX;
|
|
430
|
+
const newPositionY = source.y - deltaY;
|
|
431
|
+
|
|
341
432
|
handleNewPosition(
|
|
342
433
|
this,
|
|
343
434
|
newPositionX,
|
|
@@ -354,9 +445,11 @@ export class ZoomPanPinch {
|
|
|
354
445
|
/// ///////
|
|
355
446
|
|
|
356
447
|
onPanningStart = (event: MouseEvent): void => {
|
|
357
|
-
const { disabled } = this.setup;
|
|
448
|
+
const { disabled, panning } = this.setup;
|
|
358
449
|
const { onPanningStart } = this.props;
|
|
359
|
-
|
|
450
|
+
// With panning off the mousedown is not ours: no pan state, no
|
|
451
|
+
// preventDefault, and native text selection keeps working (#467).
|
|
452
|
+
if (disabled || panning.disabled) return;
|
|
360
453
|
|
|
361
454
|
this.syncModifierKeys(event);
|
|
362
455
|
|
|
@@ -377,6 +470,8 @@ export class ZoomPanPinch {
|
|
|
377
470
|
|
|
378
471
|
handleCancelAnimation(this);
|
|
379
472
|
handlePanningStart(this, event);
|
|
473
|
+
this.lockTextSelection();
|
|
474
|
+
this.focusForKeyboard();
|
|
380
475
|
handleCallback(getContext(this), event, onPanningStart);
|
|
381
476
|
};
|
|
382
477
|
|
|
@@ -415,8 +510,10 @@ export class ZoomPanPinch {
|
|
|
415
510
|
const { onPanningStop } = this.props;
|
|
416
511
|
|
|
417
512
|
if (this.isPanning) {
|
|
513
|
+
this.unlockTextSelection();
|
|
418
514
|
handlePanningEnd(this, velocityDisabled);
|
|
419
515
|
handleCallback(getContext(this), event, onPanningStop);
|
|
516
|
+
this.flushResizeAlignment();
|
|
420
517
|
}
|
|
421
518
|
};
|
|
422
519
|
|
|
@@ -462,6 +559,7 @@ export class ZoomPanPinch {
|
|
|
462
559
|
if (this.pinchStartScale) {
|
|
463
560
|
handlePinchStop(this);
|
|
464
561
|
handleCallback(getContext(this), event, onPinchStop);
|
|
562
|
+
this.flushResizeAlignment();
|
|
465
563
|
}
|
|
466
564
|
};
|
|
467
565
|
|
|
@@ -495,9 +593,11 @@ export class ZoomPanPinch {
|
|
|
495
593
|
|
|
496
594
|
const isAllowed = isPanningStartAllowed(this, event);
|
|
497
595
|
if (isPanningAction) {
|
|
498
|
-
if (!isAllowed) return;
|
|
596
|
+
if (!isAllowed || this.setup.panning.disabled) return;
|
|
499
597
|
handleCancelAnimation(this);
|
|
500
598
|
handlePanningStart(this, event);
|
|
599
|
+
this.lockTextSelection();
|
|
600
|
+
this.focusForKeyboard();
|
|
501
601
|
handleCallback(getContext(this), event, onPanningStart);
|
|
502
602
|
}
|
|
503
603
|
if (isPinchAction) {
|
|
@@ -567,6 +667,8 @@ export class ZoomPanPinch {
|
|
|
567
667
|
this.isPanning = false;
|
|
568
668
|
this.startCoords = null;
|
|
569
669
|
}
|
|
670
|
+
this.unlockTextSelection();
|
|
671
|
+
this.flushResizeAlignment();
|
|
570
672
|
};
|
|
571
673
|
|
|
572
674
|
// Iframe focus problem (e.g. Storybook):
|
|
@@ -619,6 +721,85 @@ export class ZoomPanPinch {
|
|
|
619
721
|
return Boolean(keys.every((key) => this.pressedKeys[key]));
|
|
620
722
|
};
|
|
621
723
|
|
|
724
|
+
/// ///////
|
|
725
|
+
// Keyboard
|
|
726
|
+
/// ///////
|
|
727
|
+
|
|
728
|
+
onKeyboardNavigation = (event: KeyboardEvent): void => {
|
|
729
|
+
handleKeyboardNavigation(this, event);
|
|
730
|
+
};
|
|
731
|
+
|
|
732
|
+
// A pan start cancels the mousedown, which also cancels the focus the
|
|
733
|
+
// click would have given the wrapper — so with keyboard navigation on,
|
|
734
|
+
// focus it explicitly or the arrow keys never reach it.
|
|
735
|
+
focusForKeyboard = (): void => {
|
|
736
|
+
const wrapper = this.wrapperComponent;
|
|
737
|
+
if (!wrapper || this.setup.keyboard.disabled) return;
|
|
738
|
+
const active = wrapper.ownerDocument?.activeElement;
|
|
739
|
+
if (active && wrapper.contains(active)) return;
|
|
740
|
+
if (typeof wrapper.focus === "function") {
|
|
741
|
+
wrapper.focus({ preventScroll: true });
|
|
742
|
+
}
|
|
743
|
+
};
|
|
744
|
+
|
|
745
|
+
/// ///////
|
|
746
|
+
// Text selection (#467)
|
|
747
|
+
/// ///////
|
|
748
|
+
|
|
749
|
+
// The stylesheet no longer disables text selection globally, so users can
|
|
750
|
+
// select and copy content. While a pan gesture is active the wrapper gets
|
|
751
|
+
// an inline `user-select: none` so the drag does not also select text.
|
|
752
|
+
lockTextSelection = (): void => {
|
|
753
|
+
const wrapper = this.wrapperComponent;
|
|
754
|
+
if (!wrapper || this.selectionLock) return;
|
|
755
|
+
this.selectionLock = {
|
|
756
|
+
userSelect: wrapper.style.userSelect,
|
|
757
|
+
webkitUserSelect: wrapper.style.webkitUserSelect,
|
|
758
|
+
};
|
|
759
|
+
wrapper.style.userSelect = "none";
|
|
760
|
+
wrapper.style.webkitUserSelect = "none";
|
|
761
|
+
};
|
|
762
|
+
|
|
763
|
+
unlockTextSelection = (): void => {
|
|
764
|
+
const lock = this.selectionLock;
|
|
765
|
+
this.selectionLock = null;
|
|
766
|
+
const wrapper = this.wrapperComponent;
|
|
767
|
+
if (!wrapper || !lock) return;
|
|
768
|
+
wrapper.style.userSelect = lock.userSelect;
|
|
769
|
+
wrapper.style.webkitUserSelect = lock.webkitUserSelect;
|
|
770
|
+
};
|
|
771
|
+
|
|
772
|
+
/// ///////
|
|
773
|
+
// Initial layout
|
|
774
|
+
/// ///////
|
|
775
|
+
|
|
776
|
+
// `fitOnInit` wins over `centerOnInit` (a fit is always centred). Falls
|
|
777
|
+
// back to centring while the content has no size yet; `onSizeChange`
|
|
778
|
+
// re-runs this once it does. Returns whether both boxes had a size, i.e.
|
|
779
|
+
// whether this was a real initial layout.
|
|
780
|
+
applyInitialLayout = (): boolean => {
|
|
781
|
+
const { fitOnInit } = this.setup;
|
|
782
|
+
const { wrapperComponent, contentComponent } = this;
|
|
783
|
+
const hasSizes =
|
|
784
|
+
!!wrapperComponent &&
|
|
785
|
+
!!contentComponent &&
|
|
786
|
+
wrapperComponent.clientWidth > 0 &&
|
|
787
|
+
wrapperComponent.clientHeight > 0 &&
|
|
788
|
+
(contentComponent.offsetWidth > 0 || contentComponent.offsetHeight > 0);
|
|
789
|
+
if (fitOnInit) {
|
|
790
|
+
const fitted = calculateFitToView(
|
|
791
|
+
this,
|
|
792
|
+
fitOnInit === "cover" ? "cover" : "contain",
|
|
793
|
+
);
|
|
794
|
+
if (fitted) {
|
|
795
|
+
this.setState(fitted.scale, fitted.positionX, fitted.positionY);
|
|
796
|
+
return hasSizes;
|
|
797
|
+
}
|
|
798
|
+
}
|
|
799
|
+
this.setCenter();
|
|
800
|
+
return hasSizes;
|
|
801
|
+
};
|
|
802
|
+
|
|
622
803
|
setCenter = (): void => {
|
|
623
804
|
if (this.wrapperComponent && this.contentComponent) {
|
|
624
805
|
const targetState = getCenterPosition(
|
|
@@ -750,11 +931,11 @@ export class ZoomPanPinch {
|
|
|
750
931
|
this.contentComponent = contentComponent;
|
|
751
932
|
handleCalculateBounds(this, this.state.scale);
|
|
752
933
|
this.handleInitializeWrapperEvents(wrapperComponent);
|
|
753
|
-
this.handleInitialize(
|
|
934
|
+
this.handleInitialize();
|
|
935
|
+
this.observeSizes(wrapperComponent, contentComponent);
|
|
754
936
|
this.initializeWindowEvents();
|
|
755
937
|
this.isInitialized = true;
|
|
756
938
|
const ctx = getContext(this);
|
|
757
939
|
handleCallback(ctx, undefined, this.props.onInit);
|
|
758
|
-
assignRef(this.props.ref, ctx);
|
|
759
940
|
};
|
|
760
941
|
}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { ReactZoomPanPinchContext } from "../../models";
|
|
2
|
+
import { isEditableTarget, isExcludedNode } from "../../utils/helpers.utils";
|
|
3
|
+
import { getControls } from "../../utils/context.utils";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Keys handled by the keyboard navigation (#254). Arrows pan by
|
|
7
|
+
* `keyboard.panStep`, plus/minus zoom by `keyboard.zoomStep`, `0` resets.
|
|
8
|
+
*/
|
|
9
|
+
export const KEYBOARD_KEYS = {
|
|
10
|
+
panLeft: ["ArrowLeft"],
|
|
11
|
+
panRight: ["ArrowRight"],
|
|
12
|
+
panUp: ["ArrowUp"],
|
|
13
|
+
panDown: ["ArrowDown"],
|
|
14
|
+
zoomIn: ["+", "="],
|
|
15
|
+
zoomOut: ["-", "_"],
|
|
16
|
+
reset: ["0"],
|
|
17
|
+
} as const;
|
|
18
|
+
|
|
19
|
+
const isHandledKey = (key: string): boolean =>
|
|
20
|
+
Object.values(KEYBOARD_KEYS).some((keys) =>
|
|
21
|
+
(keys as readonly string[]).includes(key),
|
|
22
|
+
);
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Wrapper `keydown` handler. Returns `true` when the key was consumed (and
|
|
26
|
+
* its default action prevented), `false` when it was left to the browser.
|
|
27
|
+
*
|
|
28
|
+
* Ignored on purpose: modifier combos (ctrl/cmd/alt, so browser shortcuts
|
|
29
|
+
* such as cmd+0 / cmd+- keep working), editable targets (typing in an input
|
|
30
|
+
* inside the content) and `keyboard.excluded` targets.
|
|
31
|
+
*/
|
|
32
|
+
export function handleKeyboardNavigation(
|
|
33
|
+
contextInstance: ReactZoomPanPinchContext,
|
|
34
|
+
event: KeyboardEvent,
|
|
35
|
+
): boolean {
|
|
36
|
+
const { disabled, keyboard, panning } = contextInstance.setup;
|
|
37
|
+
if (disabled || keyboard.disabled) return false;
|
|
38
|
+
if (event.defaultPrevented) return false;
|
|
39
|
+
if (event.ctrlKey || event.metaKey || event.altKey) return false;
|
|
40
|
+
if (!isHandledKey(event.key)) return false;
|
|
41
|
+
|
|
42
|
+
const target = event.target as HTMLElement | null;
|
|
43
|
+
if (isEditableTarget(target)) return false;
|
|
44
|
+
if (
|
|
45
|
+
target &&
|
|
46
|
+
typeof target.matches === "function" &&
|
|
47
|
+
isExcludedNode(target, keyboard.excluded)
|
|
48
|
+
) {
|
|
49
|
+
return false;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const { panStep, zoomStep, animationTime, animationType } = keyboard;
|
|
53
|
+
const controls = getControls(contextInstance);
|
|
54
|
+
const { key } = event;
|
|
55
|
+
|
|
56
|
+
// Arrow keys move the *viewport*, like scrolling: pressing left reveals
|
|
57
|
+
// content on the left, so the content itself shifts right (position grows).
|
|
58
|
+
const stepX = panning.lockAxisX ? 0 : panStep;
|
|
59
|
+
const stepY = panning.lockAxisY ? 0 : panStep;
|
|
60
|
+
|
|
61
|
+
if (key === "ArrowLeft") {
|
|
62
|
+
controls.panBy(stepX, 0, animationTime, animationType);
|
|
63
|
+
} else if (key === "ArrowRight") {
|
|
64
|
+
controls.panBy(-stepX, 0, animationTime, animationType);
|
|
65
|
+
} else if (key === "ArrowUp") {
|
|
66
|
+
controls.panBy(0, stepY, animationTime, animationType);
|
|
67
|
+
} else if (key === "ArrowDown") {
|
|
68
|
+
controls.panBy(0, -stepY, animationTime, animationType);
|
|
69
|
+
} else if (key === "+" || key === "=") {
|
|
70
|
+
controls.zoomIn(zoomStep, animationTime, animationType);
|
|
71
|
+
} else if (key === "-" || key === "_") {
|
|
72
|
+
controls.zoomOut(zoomStep, animationTime, animationType);
|
|
73
|
+
} else if (key === "0") {
|
|
74
|
+
controls.resetTransform(animationTime, animationType);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
if (event.cancelable) {
|
|
78
|
+
event.preventDefault();
|
|
79
|
+
}
|
|
80
|
+
// Consumed: hosts that forward key presses (Storybook's manager, app-level
|
|
81
|
+
// shortcuts) must not also act on them.
|
|
82
|
+
event.stopPropagation();
|
|
83
|
+
return true;
|
|
84
|
+
}
|
|
@@ -25,7 +25,8 @@ export function handlePanningStart(
|
|
|
25
25
|
contextInstance.panStartPosition = { x: positionX, y: positionY };
|
|
26
26
|
handleCancelAnimation(contextInstance);
|
|
27
27
|
handleCalculateBounds(contextInstance, scale);
|
|
28
|
-
|
|
28
|
+
// Duck-typed: a TouchEvent from another window fails `instanceof` (#290).
|
|
29
|
+
if ("touches" in event) {
|
|
29
30
|
handleTouchPanningSetup(contextInstance, event as TouchEvent);
|
|
30
31
|
} else {
|
|
31
32
|
handlePanningSetup(contextInstance, event as MouseEvent);
|
|
@@ -45,6 +46,13 @@ export function handleAlignToBounds(
|
|
|
45
46
|
|
|
46
47
|
if (isDisabled) return;
|
|
47
48
|
|
|
49
|
+
// The content or the wrapper changed size during the gesture (the resize
|
|
50
|
+
// observer postpones its bounds refresh, see `handleResizeAlignment`):
|
|
51
|
+
// align against the current limits so this single animation lands right.
|
|
52
|
+
if (contextInstance.isResizeAlignmentPending) {
|
|
53
|
+
handleCalculateBounds(contextInstance, scale);
|
|
54
|
+
}
|
|
55
|
+
|
|
48
56
|
const targetState = handlePanToBounds(contextInstance);
|
|
49
57
|
|
|
50
58
|
if (!targetState) return;
|
|
@@ -101,14 +109,26 @@ export function handlePanningEnd(
|
|
|
101
109
|
if (start) {
|
|
102
110
|
const dx = positionX - start.x;
|
|
103
111
|
const dy = positionY - start.y;
|
|
104
|
-
|
|
112
|
+
// A click is not a fling, but it can still land on content left past
|
|
113
|
+
// the bounds — e.g. grabbed mid return animation, which the press
|
|
114
|
+
// cancelled. Finish the return (a no-op inside the bounds).
|
|
115
|
+
if (dx * dx + dy * dy <= 25) {
|
|
116
|
+
handleAlignToBounds(contextInstance);
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
105
119
|
}
|
|
106
120
|
|
|
107
121
|
contextInstance.isAnimating = false;
|
|
108
122
|
contextInstance.animation = null;
|
|
109
123
|
|
|
110
|
-
|
|
111
|
-
|
|
124
|
+
// See `handleAlignToBounds`: the velocity animation clamps against
|
|
125
|
+
// `bounds`, so they must reflect a size change seen during the pan.
|
|
126
|
+
if (contextInstance.isResizeAlignmentPending) {
|
|
127
|
+
handleCalculateBounds(contextInstance, scale);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
const wrapperWidth = wrapperComponent?.clientWidth || 0;
|
|
131
|
+
const wrapperHeight = wrapperComponent?.clientHeight || 0;
|
|
112
132
|
const contentWidth = (contentComponent?.offsetWidth || 0) * scale;
|
|
113
133
|
const contentHeight = (contentComponent?.offsetHeight || 0) * scale;
|
|
114
134
|
const isContentOverflowing =
|