react-zoom-pan-pinch 4.1.0 → 4.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/README.md +2 -2
- package/dist/index.cjs.js +646 -69
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.d.ts +67 -2
- package/dist/index.esm.js +646 -69
- package/dist/index.esm.js.map +1 -1
- package/package.json +1 -1
- 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 +2 -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 +210 -32
- package/src/core/keyboard/keyboard.logic.ts +84 -0
- package/src/core/pan/panning.logic.ts +17 -3
- package/src/core/pan/panning.utils.ts +19 -12
- package/src/core/pan/velocity.logic.ts +3 -3
- 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/models/context.model.ts +34 -0
- package/src/utils/context.utils.ts +4 -0
- package/src/utils/styles.utils.ts +2 -2
|
@@ -20,6 +20,15 @@ import {
|
|
|
20
20
|
assignRef,
|
|
21
21
|
} from "../utils";
|
|
22
22
|
import { handleCancelAnimation } from "./animations/animations.utils";
|
|
23
|
+
import { handleKeyboardNavigation } from "./keyboard/keyboard.logic";
|
|
24
|
+
import {
|
|
25
|
+
handleResizeAlignment,
|
|
26
|
+
handleSizeChange,
|
|
27
|
+
isGestureActive,
|
|
28
|
+
measureSizes,
|
|
29
|
+
} from "./resize/resize.logic";
|
|
30
|
+
import { MeasuredSizesType } from "./resize/resize.types";
|
|
31
|
+
import { calculateFitToView } from "./handlers/handlers.utils";
|
|
23
32
|
import { isWheelAllowed, isWheelPanningAllowed } from "./wheel/wheel.utils";
|
|
24
33
|
import { isPinchAllowed, isPinchStartAllowed } from "./pinch/pinch.utils";
|
|
25
34
|
import { handleCalculateBounds } from "./bounds/bounds.utils";
|
|
@@ -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);
|
|
333
|
+
|
|
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
|
+
};
|
|
284
340
|
|
|
285
|
-
|
|
286
|
-
|
|
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
|
/// ///////
|
|
@@ -354,9 +441,11 @@ export class ZoomPanPinch {
|
|
|
354
441
|
/// ///////
|
|
355
442
|
|
|
356
443
|
onPanningStart = (event: MouseEvent): void => {
|
|
357
|
-
const { disabled } = this.setup;
|
|
444
|
+
const { disabled, panning } = this.setup;
|
|
358
445
|
const { onPanningStart } = this.props;
|
|
359
|
-
|
|
446
|
+
// With panning off the mousedown is not ours: no pan state, no
|
|
447
|
+
// preventDefault, and native text selection keeps working (#467).
|
|
448
|
+
if (disabled || panning.disabled) return;
|
|
360
449
|
|
|
361
450
|
this.syncModifierKeys(event);
|
|
362
451
|
|
|
@@ -377,6 +466,8 @@ export class ZoomPanPinch {
|
|
|
377
466
|
|
|
378
467
|
handleCancelAnimation(this);
|
|
379
468
|
handlePanningStart(this, event);
|
|
469
|
+
this.lockTextSelection();
|
|
470
|
+
this.focusForKeyboard();
|
|
380
471
|
handleCallback(getContext(this), event, onPanningStart);
|
|
381
472
|
};
|
|
382
473
|
|
|
@@ -415,8 +506,10 @@ export class ZoomPanPinch {
|
|
|
415
506
|
const { onPanningStop } = this.props;
|
|
416
507
|
|
|
417
508
|
if (this.isPanning) {
|
|
509
|
+
this.unlockTextSelection();
|
|
418
510
|
handlePanningEnd(this, velocityDisabled);
|
|
419
511
|
handleCallback(getContext(this), event, onPanningStop);
|
|
512
|
+
this.flushResizeAlignment();
|
|
420
513
|
}
|
|
421
514
|
};
|
|
422
515
|
|
|
@@ -462,6 +555,7 @@ export class ZoomPanPinch {
|
|
|
462
555
|
if (this.pinchStartScale) {
|
|
463
556
|
handlePinchStop(this);
|
|
464
557
|
handleCallback(getContext(this), event, onPinchStop);
|
|
558
|
+
this.flushResizeAlignment();
|
|
465
559
|
}
|
|
466
560
|
};
|
|
467
561
|
|
|
@@ -495,9 +589,11 @@ export class ZoomPanPinch {
|
|
|
495
589
|
|
|
496
590
|
const isAllowed = isPanningStartAllowed(this, event);
|
|
497
591
|
if (isPanningAction) {
|
|
498
|
-
if (!isAllowed) return;
|
|
592
|
+
if (!isAllowed || this.setup.panning.disabled) return;
|
|
499
593
|
handleCancelAnimation(this);
|
|
500
594
|
handlePanningStart(this, event);
|
|
595
|
+
this.lockTextSelection();
|
|
596
|
+
this.focusForKeyboard();
|
|
501
597
|
handleCallback(getContext(this), event, onPanningStart);
|
|
502
598
|
}
|
|
503
599
|
if (isPinchAction) {
|
|
@@ -567,6 +663,8 @@ export class ZoomPanPinch {
|
|
|
567
663
|
this.isPanning = false;
|
|
568
664
|
this.startCoords = null;
|
|
569
665
|
}
|
|
666
|
+
this.unlockTextSelection();
|
|
667
|
+
this.flushResizeAlignment();
|
|
570
668
|
};
|
|
571
669
|
|
|
572
670
|
// Iframe focus problem (e.g. Storybook):
|
|
@@ -619,6 +717,85 @@ export class ZoomPanPinch {
|
|
|
619
717
|
return Boolean(keys.every((key) => this.pressedKeys[key]));
|
|
620
718
|
};
|
|
621
719
|
|
|
720
|
+
/// ///////
|
|
721
|
+
// Keyboard
|
|
722
|
+
/// ///////
|
|
723
|
+
|
|
724
|
+
onKeyboardNavigation = (event: KeyboardEvent): void => {
|
|
725
|
+
handleKeyboardNavigation(this, event);
|
|
726
|
+
};
|
|
727
|
+
|
|
728
|
+
// A pan start cancels the mousedown, which also cancels the focus the
|
|
729
|
+
// click would have given the wrapper — so with keyboard navigation on,
|
|
730
|
+
// focus it explicitly or the arrow keys never reach it.
|
|
731
|
+
focusForKeyboard = (): void => {
|
|
732
|
+
const wrapper = this.wrapperComponent;
|
|
733
|
+
if (!wrapper || this.setup.keyboard.disabled) return;
|
|
734
|
+
const active = wrapper.ownerDocument?.activeElement;
|
|
735
|
+
if (active && wrapper.contains(active)) return;
|
|
736
|
+
if (typeof wrapper.focus === "function") {
|
|
737
|
+
wrapper.focus({ preventScroll: true });
|
|
738
|
+
}
|
|
739
|
+
};
|
|
740
|
+
|
|
741
|
+
/// ///////
|
|
742
|
+
// Text selection (#467)
|
|
743
|
+
/// ///////
|
|
744
|
+
|
|
745
|
+
// The stylesheet no longer disables text selection globally, so users can
|
|
746
|
+
// select and copy content. While a pan gesture is active the wrapper gets
|
|
747
|
+
// an inline `user-select: none` so the drag does not also select text.
|
|
748
|
+
lockTextSelection = (): void => {
|
|
749
|
+
const wrapper = this.wrapperComponent;
|
|
750
|
+
if (!wrapper || this.selectionLock) return;
|
|
751
|
+
this.selectionLock = {
|
|
752
|
+
userSelect: wrapper.style.userSelect,
|
|
753
|
+
webkitUserSelect: wrapper.style.webkitUserSelect,
|
|
754
|
+
};
|
|
755
|
+
wrapper.style.userSelect = "none";
|
|
756
|
+
wrapper.style.webkitUserSelect = "none";
|
|
757
|
+
};
|
|
758
|
+
|
|
759
|
+
unlockTextSelection = (): void => {
|
|
760
|
+
const lock = this.selectionLock;
|
|
761
|
+
this.selectionLock = null;
|
|
762
|
+
const wrapper = this.wrapperComponent;
|
|
763
|
+
if (!wrapper || !lock) return;
|
|
764
|
+
wrapper.style.userSelect = lock.userSelect;
|
|
765
|
+
wrapper.style.webkitUserSelect = lock.webkitUserSelect;
|
|
766
|
+
};
|
|
767
|
+
|
|
768
|
+
/// ///////
|
|
769
|
+
// Initial layout
|
|
770
|
+
/// ///////
|
|
771
|
+
|
|
772
|
+
// `fitOnInit` wins over `centerOnInit` (a fit is always centred). Falls
|
|
773
|
+
// back to centring while the content has no size yet; `onSizeChange`
|
|
774
|
+
// re-runs this once it does. Returns whether both boxes had a size, i.e.
|
|
775
|
+
// whether this was a real initial layout.
|
|
776
|
+
applyInitialLayout = (): boolean => {
|
|
777
|
+
const { fitOnInit } = this.setup;
|
|
778
|
+
const { wrapperComponent, contentComponent } = this;
|
|
779
|
+
const hasSizes =
|
|
780
|
+
!!wrapperComponent &&
|
|
781
|
+
!!contentComponent &&
|
|
782
|
+
wrapperComponent.clientWidth > 0 &&
|
|
783
|
+
wrapperComponent.clientHeight > 0 &&
|
|
784
|
+
(contentComponent.offsetWidth > 0 || contentComponent.offsetHeight > 0);
|
|
785
|
+
if (fitOnInit) {
|
|
786
|
+
const fitted = calculateFitToView(
|
|
787
|
+
this,
|
|
788
|
+
fitOnInit === "cover" ? "cover" : "contain",
|
|
789
|
+
);
|
|
790
|
+
if (fitted) {
|
|
791
|
+
this.setState(fitted.scale, fitted.positionX, fitted.positionY);
|
|
792
|
+
return hasSizes;
|
|
793
|
+
}
|
|
794
|
+
}
|
|
795
|
+
this.setCenter();
|
|
796
|
+
return hasSizes;
|
|
797
|
+
};
|
|
798
|
+
|
|
622
799
|
setCenter = (): void => {
|
|
623
800
|
if (this.wrapperComponent && this.contentComponent) {
|
|
624
801
|
const targetState = getCenterPosition(
|
|
@@ -750,7 +927,8 @@ export class ZoomPanPinch {
|
|
|
750
927
|
this.contentComponent = contentComponent;
|
|
751
928
|
handleCalculateBounds(this, this.state.scale);
|
|
752
929
|
this.handleInitializeWrapperEvents(wrapperComponent);
|
|
753
|
-
this.handleInitialize(
|
|
930
|
+
this.handleInitialize();
|
|
931
|
+
this.observeSizes(wrapperComponent, contentComponent);
|
|
754
932
|
this.initializeWindowEvents();
|
|
755
933
|
this.isInitialized = true;
|
|
756
934
|
const ctx = getContext(this);
|
|
@@ -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;
|
|
@@ -107,8 +115,14 @@ export function handlePanningEnd(
|
|
|
107
115
|
contextInstance.isAnimating = false;
|
|
108
116
|
contextInstance.animation = null;
|
|
109
117
|
|
|
110
|
-
|
|
111
|
-
|
|
118
|
+
// See `handleAlignToBounds`: the velocity animation clamps against
|
|
119
|
+
// `bounds`, so they must reflect a size change seen during the pan.
|
|
120
|
+
if (contextInstance.isResizeAlignmentPending) {
|
|
121
|
+
handleCalculateBounds(contextInstance, scale);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
const wrapperWidth = wrapperComponent?.clientWidth || 0;
|
|
125
|
+
const wrapperHeight = wrapperComponent?.clientHeight || 0;
|
|
112
126
|
const contentWidth = (contentComponent?.offsetWidth || 0) * scale;
|
|
113
127
|
const contentHeight = (contentComponent?.offsetHeight || 0) * scale;
|
|
114
128
|
const isContentOverflowing =
|
|
@@ -20,16 +20,23 @@ export const isPanningStartAllowed = (
|
|
|
20
20
|
const { isInitialized, wrapperComponent } = contextInstance;
|
|
21
21
|
|
|
22
22
|
const target = event.target as HTMLElement;
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
23
|
+
// `composedPath()` also lists the nodes inside a shadow root (#371) and the
|
|
24
|
+
// real target in another window (portal, popup) whose nodes fail an
|
|
25
|
+
// `instanceof Element` check from this realm (#290, PR #552): only
|
|
26
|
+
// duck-typed node checks are safe here.
|
|
27
|
+
const isElementNode = (node: unknown): node is Element =>
|
|
28
|
+
!!node &&
|
|
29
|
+
typeof node === "object" &&
|
|
30
|
+
(node as Node).nodeType === 1 &&
|
|
31
|
+
typeof (node as Element).contains === "function";
|
|
32
|
+
const isWrapperChild =
|
|
33
|
+
typeof event.composedPath === "function"
|
|
34
|
+
? event
|
|
35
|
+
.composedPath()
|
|
36
|
+
.some(
|
|
37
|
+
(node) => isElementNode(node) && !!wrapperComponent?.contains(node),
|
|
38
|
+
)
|
|
39
|
+
: !!wrapperComponent?.contains(target);
|
|
33
40
|
|
|
34
41
|
const isAllowed = isInitialized && target && isWrapperChild;
|
|
35
42
|
|
|
@@ -108,11 +115,11 @@ export function handlePanToBounds(
|
|
|
108
115
|
|
|
109
116
|
const mousePosX =
|
|
110
117
|
positionX > maxPositionX
|
|
111
|
-
? wrapperComponent.
|
|
118
|
+
? wrapperComponent.clientWidth
|
|
112
119
|
: contextInstance.setup.minPositionX || 0;
|
|
113
120
|
const mousePosY =
|
|
114
121
|
positionY > maxPositionY
|
|
115
|
-
? wrapperComponent.
|
|
122
|
+
? wrapperComponent.clientHeight
|
|
116
123
|
: contextInstance.setup.minPositionY || 0;
|
|
117
124
|
|
|
118
125
|
const { x, y } = handleCalculateZoomPositions(
|
|
@@ -14,7 +14,7 @@ import {
|
|
|
14
14
|
|
|
15
15
|
export function getSizeMultiplier(wrapperComponent: HTMLDivElement): number {
|
|
16
16
|
const defaultMultiplier = 1;
|
|
17
|
-
const value = wrapperComponent.
|
|
17
|
+
const value = wrapperComponent.clientWidth / window.innerWidth;
|
|
18
18
|
|
|
19
19
|
if (Number.isNaN(value)) {
|
|
20
20
|
return defaultMultiplier;
|
|
@@ -123,8 +123,8 @@ export function handleVelocityPanning(
|
|
|
123
123
|
|
|
124
124
|
const paddingValueX = getPaddingValue(contextInstance, sizeX);
|
|
125
125
|
const paddingValueY = getPaddingValue(contextInstance, sizeY);
|
|
126
|
-
const paddingX = (paddingValueX * wrapperComponent.
|
|
127
|
-
const paddingY = (paddingValueY * wrapperComponent.
|
|
126
|
+
const paddingX = (paddingValueX * wrapperComponent.clientWidth) / 100;
|
|
127
|
+
const paddingY = (paddingValueY * wrapperComponent.clientHeight) / 100;
|
|
128
128
|
const maxTargetX = maxPositionX + paddingX;
|
|
129
129
|
const minTargetX = minPositionX - paddingX;
|
|
130
130
|
|
|
@@ -15,8 +15,8 @@ export const isVelocityCalculationAllowed = (
|
|
|
15
15
|
if (!limitToBounds) return true;
|
|
16
16
|
|
|
17
17
|
const contentOverflows =
|
|
18
|
-
wrapperComponent.
|
|
19
|
-
wrapperComponent.
|
|
18
|
+
wrapperComponent.clientWidth < contentComponent.offsetWidth * scale ||
|
|
19
|
+
wrapperComponent.clientHeight < contentComponent.offsetHeight * scale;
|
|
20
20
|
|
|
21
21
|
return contentOverflows;
|
|
22
22
|
};
|
|
File without changes
|