react-zoom-pan-pinch 4.2.0 → 4.4.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 +770 -229
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.d.ts +85 -4
- package/dist/index.esm.js +771 -231
- package/dist/index.esm.js.map +1 -1
- package/dist/styles.css +40 -0
- package/package.json +1 -1
- package/src/components/mini-map/mini-map.tsx +386 -171
- package/src/components/transform-component/transform-component.tsx +25 -2
- package/src/constants/state.constants.ts +3 -0
- package/src/core/bounds/bounds.utils.ts +88 -0
- package/src/core/double-click/double-click.logic.ts +18 -9
- package/src/core/handlers/handlers.utils.ts +89 -2
- package/src/core/instance.core.ts +156 -31
- package/src/core/pan/panning.logic.ts +8 -1
- package/src/core/pan/panning.utils.ts +111 -24
- package/src/core/pan/velocity.logic.ts +10 -3
- package/src/core/wheel/wheel.logic.ts +19 -2
- package/src/core/wheel/wheel.utils.ts +44 -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 +25 -2
- package/src/typings.d.ts +5 -0
- package/src/utils/state.utils.ts +8 -2
- package/src/utils/styles.utils.ts +37 -0
package/dist/index.cjs.js
CHANGED
|
@@ -282,6 +282,61 @@ var calculateBounds = function (contextInstance, newScale) {
|
|
|
282
282
|
}
|
|
283
283
|
return bounds;
|
|
284
284
|
};
|
|
285
|
+
function clamp(v, min, max) {
|
|
286
|
+
return Math.max(min, Math.min(v, max));
|
|
287
|
+
}
|
|
288
|
+
// Based on @aholachek ;)
|
|
289
|
+
// https://twitter.com/chpwn/status/285540192096497664
|
|
290
|
+
// iOS constant = 0.55
|
|
291
|
+
// https://medium.com/@nathangitter/building-fluid-interfaces-ios-swift-9732bb934bf5
|
|
292
|
+
function rubberband(distance, dimension, constant) {
|
|
293
|
+
if (dimension === 0 || Math.abs(dimension) === Infinity)
|
|
294
|
+
return Math.pow(distance, (constant * 5));
|
|
295
|
+
return (distance * dimension * constant) / (dimension + constant * distance);
|
|
296
|
+
}
|
|
297
|
+
/**
|
|
298
|
+
* Furthest a grabbed position is traced back along the rubber band curve, as
|
|
299
|
+
* a fraction of the padding. The curve never reaches the padding itself, so
|
|
300
|
+
* a position at (or past) it has no pointer distance to map back to.
|
|
301
|
+
*/
|
|
302
|
+
var MAX_RUBBER_BAND_RATIO = 0.9;
|
|
303
|
+
/**
|
|
304
|
+
* Resistance of the rubber band, the constant UIScrollView uses: past a bound
|
|
305
|
+
* the content moves at 0.55× the pointer and slows down from there.
|
|
306
|
+
*/
|
|
307
|
+
var RUBBER_BAND_CONSTANT = 0.55;
|
|
308
|
+
/**
|
|
309
|
+
* Where a pan that was dragged past a bound ends up: `(1 - 1 / (x * c / d + 1)) * d`
|
|
310
|
+
* with `d` = `padding`. It trails the pointer from the first pixel past the
|
|
311
|
+
* bound and approaches — never reaches — `padding` px past it.
|
|
312
|
+
*/
|
|
313
|
+
function getRubberBandPosition(position, min, max, padding) {
|
|
314
|
+
if (padding <= 0)
|
|
315
|
+
return clamp(position, min, max);
|
|
316
|
+
if (position < min)
|
|
317
|
+
return min - rubberband(min - position, padding, RUBBER_BAND_CONSTANT);
|
|
318
|
+
if (position > max)
|
|
319
|
+
return max + rubberband(position - max, padding, RUBBER_BAND_CONSTANT);
|
|
320
|
+
return position;
|
|
321
|
+
}
|
|
322
|
+
/**
|
|
323
|
+
* Inverse of `getRubberBandPosition`: the pointer position that puts the
|
|
324
|
+
* content at `position`. Lets a pan that starts inside the padding (grabbed
|
|
325
|
+
* mid return animation, trackpad deltas) continue from where the content is.
|
|
326
|
+
*/
|
|
327
|
+
function getRubberBandSource(position, min, max, padding) {
|
|
328
|
+
if (padding <= 0)
|
|
329
|
+
return position;
|
|
330
|
+
var stretch = function (offset) {
|
|
331
|
+
var limited = Math.min(offset, padding * MAX_RUBBER_BAND_RATIO);
|
|
332
|
+
return (limited * padding) / (RUBBER_BAND_CONSTANT * (padding - limited));
|
|
333
|
+
};
|
|
334
|
+
if (position < min)
|
|
335
|
+
return min - stretch(min - position);
|
|
336
|
+
if (position > max)
|
|
337
|
+
return max + stretch(position - max);
|
|
338
|
+
return position;
|
|
339
|
+
}
|
|
285
340
|
/**
|
|
286
341
|
* Keeps value between given bounds, used for limiting view to given boundaries
|
|
287
342
|
* 1# eg. boundLimiter(2, 0, 3, true) => 2
|
|
@@ -316,6 +371,23 @@ function getMouseBoundedPosition(positionX, positionY, bounds, limitToBounds, pa
|
|
|
316
371
|
var y = boundLimiter(positionY, minPositionY - paddingY, maxPositionY + paddingY, limitToBounds);
|
|
317
372
|
return { x: x, y: y };
|
|
318
373
|
}
|
|
374
|
+
/**
|
|
375
|
+
* Bounded position for a pan gesture: free inside the bounds and, inside the
|
|
376
|
+
* padding around them, rubber banded (`rubberBand`) or 1:1 up to its edge.
|
|
377
|
+
*/
|
|
378
|
+
function getPanBoundedPosition(positionX, positionY, bounds, limitToBounds, paddingX, paddingY, rubberBand) {
|
|
379
|
+
var minPositionX = bounds.minPositionX, minPositionY = bounds.minPositionY, maxPositionX = bounds.maxPositionX, maxPositionY = bounds.maxPositionY;
|
|
380
|
+
var limit = function (position, min, max, padding) {
|
|
381
|
+
if (limitToBounds && rubberBand && padding > 0) {
|
|
382
|
+
return roundNumber(getRubberBandPosition(position, min, max, padding), 2);
|
|
383
|
+
}
|
|
384
|
+
return boundLimiter(position, min - padding, max + padding, limitToBounds);
|
|
385
|
+
};
|
|
386
|
+
return {
|
|
387
|
+
x: limit(positionX, minPositionX, maxPositionX, paddingX),
|
|
388
|
+
y: limit(positionY, minPositionY, maxPositionY, paddingY),
|
|
389
|
+
};
|
|
390
|
+
}
|
|
319
391
|
|
|
320
392
|
function handleCalculateZoomPositions(contextInstance, mouseX, mouseY, newScale, bounds, limitToBounds) {
|
|
321
393
|
var _a = contextInstance.state, scale = _a.scale, positionX = _a.positionX, positionY = _a.positionY;
|
|
@@ -381,8 +453,73 @@ var isPanningAllowed = function (contextInstance) {
|
|
|
381
453
|
return false;
|
|
382
454
|
return true;
|
|
383
455
|
};
|
|
456
|
+
/**
|
|
457
|
+
* Pointer travel in px up to which a press still counts as a click, not a
|
|
458
|
+
* drag: a pan ending within it aligns instead of flinging, and only a pan
|
|
459
|
+
* beyond it swallows the click that follows (#452).
|
|
460
|
+
*/
|
|
461
|
+
var CLICK_DISTANCE = 5;
|
|
462
|
+
/**
|
|
463
|
+
* Whether a pan moving by (`dx`, `dy`) would push the content further into a
|
|
464
|
+
* bound it already rests on, judged on the dominant axis. With
|
|
465
|
+
* `panning.releaseAtBounds` such a gesture is handed back to the page or the
|
|
466
|
+
* parent (a carousel, page scroll) instead of being claimed (#509).
|
|
467
|
+
*/
|
|
468
|
+
function isPanBlockedAtBounds(contextInstance, dx, dy) {
|
|
469
|
+
var bounds = contextInstance.bounds, setup = contextInstance.setup, state = contextInstance.state;
|
|
470
|
+
if (!bounds || !setup.limitToBounds || (!dx && !dy))
|
|
471
|
+
return false;
|
|
472
|
+
var isHorizontal = Math.abs(dx) >= Math.abs(dy);
|
|
473
|
+
if (isHorizontal ? setup.panning.lockAxisX : setup.panning.lockAxisY) {
|
|
474
|
+
return true;
|
|
475
|
+
}
|
|
476
|
+
var delta = isHorizontal ? dx : dy;
|
|
477
|
+
var position = isHorizontal ? state.positionX : state.positionY;
|
|
478
|
+
var min = isHorizontal ? bounds.minPositionX : bounds.minPositionY;
|
|
479
|
+
var max = isHorizontal ? bounds.maxPositionX : bounds.maxPositionY;
|
|
480
|
+
// Moving the pointer right/down moves the content towards its max bound.
|
|
481
|
+
// Half a pixel of slack: alignment animations settle on float values.
|
|
482
|
+
return delta > 0 ? position >= max - 0.5 : position <= min + 0.5;
|
|
483
|
+
}
|
|
484
|
+
var getPaddingValue = function (contextInstance, size, explicitScale) {
|
|
485
|
+
var setup = contextInstance.setup, state = contextInstance.state;
|
|
486
|
+
var minScale = setup.minScale, disablePadding = setup.disablePadding, centerZoomedOut = setup.centerZoomedOut;
|
|
487
|
+
var scale = explicitScale !== null && explicitScale !== void 0 ? explicitScale : state.scale;
|
|
488
|
+
if (size > 0 && scale >= minScale && !disablePadding && !centerZoomedOut) {
|
|
489
|
+
return size;
|
|
490
|
+
}
|
|
491
|
+
return 0;
|
|
492
|
+
};
|
|
493
|
+
/**
|
|
494
|
+
* Dragging past the bounds stretches like a rubber band only when the content
|
|
495
|
+
* is going to return to them on release (`autoAlignment`). Without that return
|
|
496
|
+
* the padding is a plain area the content can be left in, so it follows the
|
|
497
|
+
* pointer 1:1 up to its edge.
|
|
498
|
+
*/
|
|
499
|
+
function isRubberBandEnabled(contextInstance) {
|
|
500
|
+
var _a = contextInstance.setup, limitToBounds = _a.limitToBounds, autoAlignment = _a.autoAlignment;
|
|
501
|
+
return limitToBounds && !autoAlignment.disabled;
|
|
502
|
+
}
|
|
503
|
+
/**
|
|
504
|
+
* The position a pan continues from. Inside the bounds that is the content
|
|
505
|
+
* position; inside the padding the content trails the pointer (rubber band),
|
|
506
|
+
* so the pointer's own position is traced back from it — otherwise the first
|
|
507
|
+
* move of a pan started there would make the content jump.
|
|
508
|
+
*/
|
|
509
|
+
function getPanSourcePosition(contextInstance) {
|
|
510
|
+
var bounds = contextInstance.bounds, setup = contextInstance.setup, state = contextInstance.state;
|
|
511
|
+
var positionX = state.positionX, positionY = state.positionY;
|
|
512
|
+
if (!bounds || !isRubberBandEnabled(contextInstance)) {
|
|
513
|
+
return { x: positionX, y: positionY };
|
|
514
|
+
}
|
|
515
|
+
var _a = setup.autoAlignment, sizeX = _a.sizeX, sizeY = _a.sizeY;
|
|
516
|
+
return {
|
|
517
|
+
x: getRubberBandSource(positionX, bounds.minPositionX, bounds.maxPositionX, getPaddingValue(contextInstance, sizeX)),
|
|
518
|
+
y: getRubberBandSource(positionY, bounds.minPositionY, bounds.maxPositionY, getPaddingValue(contextInstance, sizeY)),
|
|
519
|
+
};
|
|
520
|
+
}
|
|
384
521
|
var handlePanningSetup = function (contextInstance, event) {
|
|
385
|
-
var _a = contextInstance
|
|
522
|
+
var _a = getPanSourcePosition(contextInstance), positionX = _a.x, positionY = _a.y;
|
|
386
523
|
contextInstance.isPanning = true;
|
|
387
524
|
// Panning with mouse
|
|
388
525
|
var x = event.clientX;
|
|
@@ -391,7 +528,7 @@ var handlePanningSetup = function (contextInstance, event) {
|
|
|
391
528
|
};
|
|
392
529
|
var handleTouchPanningSetup = function (contextInstance, event) {
|
|
393
530
|
var touches = event.touches;
|
|
394
|
-
var _a = contextInstance
|
|
531
|
+
var _a = getPanSourcePosition(contextInstance), positionX = _a.x, positionY = _a.y;
|
|
395
532
|
contextInstance.isPanning = true;
|
|
396
533
|
// Panning with touch
|
|
397
534
|
var oneFingerTouch = touches.length === 1;
|
|
@@ -432,14 +569,16 @@ function handleNewPosition(contextInstance, newPositionX, newPositionY, paddingV
|
|
|
432
569
|
(newPositionX === positionX && newPositionY === positionY)) {
|
|
433
570
|
return;
|
|
434
571
|
}
|
|
435
|
-
var _b =
|
|
572
|
+
var _b = getPanBoundedPosition(newPositionX, newPositionY, bounds, limitToBounds, paddingValueX, paddingValueY, isRubberBandEnabled(contextInstance)), x = _b.x, y = _b.y;
|
|
436
573
|
contextInstance.setState(scale, x, y);
|
|
437
574
|
}
|
|
438
575
|
var getPanningClientPosition = function (contextInstance, clientX, clientY) {
|
|
439
|
-
var startCoords = contextInstance.startCoords
|
|
576
|
+
var startCoords = contextInstance.startCoords;
|
|
440
577
|
var panning = contextInstance.setup.panning;
|
|
441
578
|
var lockAxisX = panning.lockAxisX, lockAxisY = panning.lockAxisY;
|
|
442
|
-
|
|
579
|
+
// A locked axis keeps its place: the source maps back onto the position
|
|
580
|
+
// it has now, also when it rests inside the padding.
|
|
581
|
+
var _a = getPanSourcePosition(contextInstance), positionX = _a.x, positionY = _a.y;
|
|
443
582
|
if (!startCoords) {
|
|
444
583
|
return { x: positionX, y: positionY };
|
|
445
584
|
}
|
|
@@ -449,15 +588,6 @@ var getPanningClientPosition = function (contextInstance, clientX, clientY) {
|
|
|
449
588
|
var newPositionY = lockAxisY ? positionY : mouseY;
|
|
450
589
|
return { x: newPositionX, y: newPositionY };
|
|
451
590
|
};
|
|
452
|
-
var getPaddingValue = function (contextInstance, size, explicitScale) {
|
|
453
|
-
var setup = contextInstance.setup, state = contextInstance.state;
|
|
454
|
-
var minScale = setup.minScale, disablePadding = setup.disablePadding, centerZoomedOut = setup.centerZoomedOut;
|
|
455
|
-
var scale = explicitScale !== null && explicitScale !== void 0 ? explicitScale : state.scale;
|
|
456
|
-
if (size > 0 && scale >= minScale && !disablePadding && !centerZoomedOut) {
|
|
457
|
-
return size;
|
|
458
|
-
}
|
|
459
|
-
return 0;
|
|
460
|
-
};
|
|
461
591
|
|
|
462
592
|
exports.DeviceType = void 0;
|
|
463
593
|
(function (DeviceType) {
|
|
@@ -591,8 +721,15 @@ function handleVelocityPanning(contextInstance) {
|
|
|
591
721
|
var finalAnimationTime = Math.max(moveAnimationTime, alignAnimationTime);
|
|
592
722
|
var paddingValueX = getPaddingValue(contextInstance, sizeX);
|
|
593
723
|
var paddingValueY = getPaddingValue(contextInstance, sizeY);
|
|
594
|
-
|
|
595
|
-
|
|
724
|
+
// A fling released inside the rubber band stays within the same stretch a
|
|
725
|
+
// drag could reach (`padding` px), not a share of the wrapper size.
|
|
726
|
+
var isRubberBand = isRubberBandEnabled(contextInstance);
|
|
727
|
+
var paddingX = isRubberBand
|
|
728
|
+
? paddingValueX
|
|
729
|
+
: (paddingValueX * wrapperComponent.clientWidth) / 100;
|
|
730
|
+
var paddingY = isRubberBand
|
|
731
|
+
? paddingValueY
|
|
732
|
+
: (paddingValueY * wrapperComponent.clientHeight) / 100;
|
|
596
733
|
var maxTargetX = maxPositionX + paddingX;
|
|
597
734
|
var minTargetX = minPositionX - paddingX;
|
|
598
735
|
var maxTargetY = maxPositionY + paddingY;
|
|
@@ -679,8 +816,13 @@ function handlePanningEnd(contextInstance, velocityDisabled) {
|
|
|
679
816
|
if (start) {
|
|
680
817
|
var dx = positionX - start.x;
|
|
681
818
|
var dy = positionY - start.y;
|
|
682
|
-
|
|
819
|
+
// A click is not a fling, but it can still land on content left past
|
|
820
|
+
// the bounds — e.g. grabbed mid return animation, which the press
|
|
821
|
+
// cancelled. Finish the return (a no-op inside the bounds).
|
|
822
|
+
if (dx * dx + dy * dy <= Math.pow(CLICK_DISTANCE, 2)) {
|
|
823
|
+
handleAlignToBounds(contextInstance);
|
|
683
824
|
return;
|
|
825
|
+
}
|
|
684
826
|
}
|
|
685
827
|
contextInstance.isAnimating = false;
|
|
686
828
|
contextInstance.animation = null;
|
|
@@ -824,6 +966,7 @@ var initialSetup = {
|
|
|
824
966
|
trackPadPanning: {
|
|
825
967
|
disabled: true,
|
|
826
968
|
velocityDisabled: false,
|
|
969
|
+
speed: 1,
|
|
827
970
|
lockAxisX: false,
|
|
828
971
|
lockAxisY: false,
|
|
829
972
|
activationKeys: [],
|
|
@@ -837,6 +980,8 @@ var initialSetup = {
|
|
|
837
980
|
allowLeftClickPan: true,
|
|
838
981
|
allowMiddleClickPan: true,
|
|
839
982
|
allowRightClickPan: true,
|
|
983
|
+
suppressClickAfterPan: true,
|
|
984
|
+
releaseAtBounds: false,
|
|
840
985
|
activationKeys: [],
|
|
841
986
|
excluded: [],
|
|
842
987
|
},
|
|
@@ -894,13 +1039,19 @@ var baseClasses = {
|
|
|
894
1039
|
};
|
|
895
1040
|
|
|
896
1041
|
var createState = function (props) {
|
|
897
|
-
var _a, _b, _c, _d, _e, _f, _g
|
|
1042
|
+
var _a, _b, _c, _d, _e, _f, _g;
|
|
898
1043
|
var minScale = Math.max((_a = props.minScale) !== null && _a !== void 0 ? _a : initialSetup.minScale, 1e-7);
|
|
899
1044
|
var maxScale = (_b = props.maxScale) !== null && _b !== void 0 ? _b : initialSetup.maxScale;
|
|
900
1045
|
var rawScale = (_c = props.initialScale) !== null && _c !== void 0 ? _c : initialState.scale;
|
|
901
1046
|
var scale = Math.min(Math.max(rawScale, minScale), maxScale);
|
|
902
|
-
|
|
903
|
-
|
|
1047
|
+
// A named initial position ("center", "right", …) needs the sizes, so it
|
|
1048
|
+
// starts at 0 and `applyNamedInitialPosition` places it once laid out.
|
|
1049
|
+
var positionX = boundLimiter(typeof props.initialPositionX === "number"
|
|
1050
|
+
? props.initialPositionX
|
|
1051
|
+
: initialState.positionX, (_d = props.minPositionX) !== null && _d !== void 0 ? _d : -Infinity, (_e = props.maxPositionX) !== null && _e !== void 0 ? _e : Infinity, props.minPositionX != null || props.maxPositionX != null);
|
|
1052
|
+
var positionY = boundLimiter(typeof props.initialPositionY === "number"
|
|
1053
|
+
? props.initialPositionY
|
|
1054
|
+
: initialState.positionY, (_f = props.minPositionY) !== null && _f !== void 0 ? _f : -Infinity, (_g = props.maxPositionY) !== null && _g !== void 0 ? _g : Infinity, props.minPositionY != null || props.maxPositionY != null);
|
|
904
1055
|
return {
|
|
905
1056
|
previousScale: scale,
|
|
906
1057
|
scale: scale,
|
|
@@ -1050,6 +1201,53 @@ function calculateFitToView(contextInstance, mode, minFitScale, maxFitScale) {
|
|
|
1050
1201
|
scale = checkZoomBounds(scale, setup.minScale, setup.maxScale, 0, false);
|
|
1051
1202
|
return getCenterPosition(scale, wrapperComponent, contentComponent);
|
|
1052
1203
|
}
|
|
1204
|
+
// Share of the free space (wrapper minus scaled content) left before the
|
|
1205
|
+
// content for each named initial position (#349).
|
|
1206
|
+
var NAMED_POSITION_SHARE = {
|
|
1207
|
+
left: 0,
|
|
1208
|
+
top: 0,
|
|
1209
|
+
center: 0.5,
|
|
1210
|
+
right: 1,
|
|
1211
|
+
bottom: 1,
|
|
1212
|
+
};
|
|
1213
|
+
function hasNamedInitialPosition(contextInstance) {
|
|
1214
|
+
var _a = contextInstance.props, initialPositionX = _a.initialPositionX, initialPositionY = _a.initialPositionY;
|
|
1215
|
+
return (typeof initialPositionX === "string" || typeof initialPositionY === "string");
|
|
1216
|
+
}
|
|
1217
|
+
/**
|
|
1218
|
+
* Replaces each axis of `target` whose `initialPositionX/Y` is named
|
|
1219
|
+
* (`"right"`, `"center"`, `"top"`…) with the position that puts the content
|
|
1220
|
+
* there at `target.scale`. Numeric axes are left as they are.
|
|
1221
|
+
*/
|
|
1222
|
+
function applyNamedInitialPosition(contextInstance, target) {
|
|
1223
|
+
var wrapperComponent = contextInstance.wrapperComponent, contentComponent = contextInstance.contentComponent, props = contextInstance.props;
|
|
1224
|
+
var initialPositionX = props.initialPositionX, initialPositionY = props.initialPositionY;
|
|
1225
|
+
if (!wrapperComponent || !contentComponent)
|
|
1226
|
+
return target;
|
|
1227
|
+
var place = function (name, wrapperSize, contentSize) { return (wrapperSize - contentSize * target.scale) * NAMED_POSITION_SHARE[name]; };
|
|
1228
|
+
return {
|
|
1229
|
+
scale: target.scale,
|
|
1230
|
+
positionX: typeof initialPositionX === "string"
|
|
1231
|
+
? place(initialPositionX, wrapperComponent.clientWidth, contentComponent.offsetWidth)
|
|
1232
|
+
: target.positionX,
|
|
1233
|
+
positionY: typeof initialPositionY === "string"
|
|
1234
|
+
? place(initialPositionY, wrapperComponent.clientHeight, contentComponent.offsetHeight)
|
|
1235
|
+
: target.positionY,
|
|
1236
|
+
};
|
|
1237
|
+
}
|
|
1238
|
+
/**
|
|
1239
|
+
* The scale `resetTransform` returns to: the fitted one with `fitOnInit`,
|
|
1240
|
+
* otherwise `initialScale` (clamped to min/max).
|
|
1241
|
+
*/
|
|
1242
|
+
function getResetScale(contextInstance) {
|
|
1243
|
+
var fitOnInit = contextInstance.setup.fitOnInit;
|
|
1244
|
+
if (fitOnInit) {
|
|
1245
|
+
var fitted = calculateFitToView(contextInstance, fitOnInit === "cover" ? "cover" : "contain");
|
|
1246
|
+
if (fitted)
|
|
1247
|
+
return fitted.scale;
|
|
1248
|
+
}
|
|
1249
|
+
return createState(contextInstance.props).scale;
|
|
1250
|
+
}
|
|
1053
1251
|
function resetTransformations(contextInstance, animationTime, animationType, onResetTransformation) {
|
|
1054
1252
|
var setup = contextInstance.setup, wrapperComponent = contextInstance.wrapperComponent, contentComponent = contextInstance.contentComponent;
|
|
1055
1253
|
var limitToBounds = setup.limitToBounds, centerOnInit = setup.centerOnInit, fitOnInit = setup.fitOnInit;
|
|
@@ -1060,8 +1258,9 @@ function resetTransformations(contextInstance, animationTime, animationType, onR
|
|
|
1060
1258
|
// With `fitOnInit` the initial layout is the fitted one, so that is what a
|
|
1061
1259
|
// reset returns to (#252).
|
|
1062
1260
|
if (fitOnInit) {
|
|
1063
|
-
var
|
|
1064
|
-
if (
|
|
1261
|
+
var fit_1 = calculateFitToView(contextInstance, fitOnInit === "cover" ? "cover" : "contain");
|
|
1262
|
+
if (fit_1) {
|
|
1263
|
+
var fitted = applyNamedInitialPosition(contextInstance, fit_1);
|
|
1065
1264
|
if (scale === fitted.scale &&
|
|
1066
1265
|
positionX === fitted.positionX &&
|
|
1067
1266
|
positionY === fitted.positionY) {
|
|
@@ -1078,6 +1277,14 @@ function resetTransformations(contextInstance, animationTime, animationType, onR
|
|
|
1078
1277
|
targetPositionX = centered.positionX;
|
|
1079
1278
|
targetPositionY = centered.positionY;
|
|
1080
1279
|
}
|
|
1280
|
+
// Named initial positions win over the centring on their own axis (#349).
|
|
1281
|
+
var named = applyNamedInitialPosition(contextInstance, {
|
|
1282
|
+
scale: initialTransformation.scale,
|
|
1283
|
+
positionX: targetPositionX,
|
|
1284
|
+
positionY: targetPositionY,
|
|
1285
|
+
});
|
|
1286
|
+
targetPositionX = named.positionX;
|
|
1287
|
+
targetPositionY = named.positionY;
|
|
1081
1288
|
var newBounds = calculateBounds(contextInstance, initialTransformation.scale);
|
|
1082
1289
|
var boundedPositions = getMouseBoundedPosition(targetPositionX, targetPositionY, newBounds, limitToBounds, 0, 0, wrapperComponent);
|
|
1083
1290
|
var newState = {
|
|
@@ -1491,6 +1698,39 @@ var cancelTimeout = function (timeout) {
|
|
|
1491
1698
|
var roundScaleForTransform = function (scale) {
|
|
1492
1699
|
return Number.parseFloat(scale.toFixed(8));
|
|
1493
1700
|
};
|
|
1701
|
+
var STYLE_MARKER = "data-react-zoom-pan-pinch";
|
|
1702
|
+
/**
|
|
1703
|
+
* Adds the library stylesheet once per root the wrapper renders into: the
|
|
1704
|
+
* document `<head>`, or the shadow root itself (#371), where a stylesheet in
|
|
1705
|
+
* the head never applies. `nonce` is set on the `<style>` so a CSP with
|
|
1706
|
+
* `style-src 'nonce-…'` accepts it (#403). A `<style>` already holding the
|
|
1707
|
+
* same CSS is reused, so several viewers (or a re-mount) add one copy.
|
|
1708
|
+
*/
|
|
1709
|
+
function injectStylesheet(wrapper, css, nonce) {
|
|
1710
|
+
if (!css || typeof wrapper.getRootNode !== "function")
|
|
1711
|
+
return;
|
|
1712
|
+
var doc = wrapper.ownerDocument;
|
|
1713
|
+
var root = wrapper.getRootNode();
|
|
1714
|
+
// Duck-typed: a ShadowRoot from another window fails `instanceof`.
|
|
1715
|
+
var isShadowRoot = root.nodeType === 11 && "host" in root;
|
|
1716
|
+
var container = isShadowRoot
|
|
1717
|
+
? root
|
|
1718
|
+
: doc.head;
|
|
1719
|
+
if (!container)
|
|
1720
|
+
return;
|
|
1721
|
+
var existing = container.querySelectorAll("style[".concat(STYLE_MARKER, "]"));
|
|
1722
|
+
for (var index = 0; index < existing.length; index += 1) {
|
|
1723
|
+
if (existing[index].textContent === css)
|
|
1724
|
+
return;
|
|
1725
|
+
}
|
|
1726
|
+
var style = doc.createElement("style");
|
|
1727
|
+
style.setAttribute(STYLE_MARKER, "");
|
|
1728
|
+
if (nonce)
|
|
1729
|
+
style.setAttribute("nonce", nonce);
|
|
1730
|
+
style.textContent = css;
|
|
1731
|
+
// Appended, like style-inject did before (the bundle used to add it at load).
|
|
1732
|
+
container.appendChild(style);
|
|
1733
|
+
}
|
|
1494
1734
|
var getTransformStyles = function (x, y, scale) {
|
|
1495
1735
|
// Standard translate prevents blurry svg on the safari
|
|
1496
1736
|
var s = roundScaleForTransform(scale);
|
|
@@ -1519,16 +1759,6 @@ var getCenterPosition = function (scale, wrapperComponent, contentComponent) {
|
|
|
1519
1759
|
};
|
|
1520
1760
|
};
|
|
1521
1761
|
|
|
1522
|
-
function assignRef(ref, value) {
|
|
1523
|
-
if (ref == null)
|
|
1524
|
-
return;
|
|
1525
|
-
if (typeof ref === "function") {
|
|
1526
|
-
ref(value);
|
|
1527
|
-
}
|
|
1528
|
-
else {
|
|
1529
|
-
ref.current = value;
|
|
1530
|
-
}
|
|
1531
|
-
}
|
|
1532
1762
|
function mergeRefs(refs) {
|
|
1533
1763
|
return function (value) {
|
|
1534
1764
|
refs.forEach(function (ref) {
|
|
@@ -1817,9 +2047,10 @@ function handleSizeChange(contextInstance) {
|
|
|
1817
2047
|
|
|
1818
2048
|
var isWheelAllowed = function (contextInstance, event) {
|
|
1819
2049
|
var _a = contextInstance.setup.wheel, disabled = _a.disabled, wheelDisabled = _a.wheelDisabled, touchPadDisabled = _a.touchPadDisabled, excluded = _a.excluded;
|
|
1820
|
-
var isInitialized = contextInstance.isInitialized
|
|
2050
|
+
var isInitialized = contextInstance.isInitialized;
|
|
1821
2051
|
var target = event.target;
|
|
1822
|
-
|
|
2052
|
+
// Zooming mid-drag is allowed (#543): `handleWheelZoom` re-anchors the pan.
|
|
2053
|
+
var isAllowed = isInitialized && !disabled && target;
|
|
1823
2054
|
if (!isAllowed)
|
|
1824
2055
|
return false;
|
|
1825
2056
|
// A purely horizontal wheel/swipe (Magic Mouse, trackpad) carries no zoom
|
|
@@ -1849,6 +2080,10 @@ var isWheelPanningAllowed = function (contextInstance, event) {
|
|
|
1849
2080
|
if (disabled || trackPadPanning.disabled || event.ctrlKey) {
|
|
1850
2081
|
return false;
|
|
1851
2082
|
}
|
|
2083
|
+
// A drag owns the position; a wheel pan on top of it would be undone by
|
|
2084
|
+
// the next pointer move.
|
|
2085
|
+
if (contextInstance.isPanning)
|
|
2086
|
+
return false;
|
|
1852
2087
|
var isAllowed = isWheelAllowed(contextInstance, event);
|
|
1853
2088
|
// Cannot execute at the same time as wheel zoom
|
|
1854
2089
|
if (isAllowed)
|
|
@@ -1862,6 +2097,36 @@ var isWheelPanningAllowed = function (contextInstance, event) {
|
|
|
1862
2097
|
return false;
|
|
1863
2098
|
return true;
|
|
1864
2099
|
};
|
|
2100
|
+
// px per line for `deltaMode === 1` (Firefox with a mouse wheel), close to
|
|
2101
|
+
// what browsers scroll a page per line.
|
|
2102
|
+
var WHEEL_LINE_HEIGHT = 16;
|
|
2103
|
+
/**
|
|
2104
|
+
* The distance in px a wheel event pans by (#500):
|
|
2105
|
+
* - line and page deltas (`deltaMode` 1/2) become pixels, so a wheel that
|
|
2106
|
+
* reports "3 lines" moves 48px instead of 3px;
|
|
2107
|
+
* - Shift turns a vertical wheel horizontal where the browser does not do it
|
|
2108
|
+
* already (a browser that does reports it as `deltaX`);
|
|
2109
|
+
* - `speed` scales the result.
|
|
2110
|
+
*/
|
|
2111
|
+
function getWheelPanDelta(event, wrapperComponent, speed) {
|
|
2112
|
+
var unitX = 1;
|
|
2113
|
+
var unitY = 1;
|
|
2114
|
+
if (event.deltaMode === 1) {
|
|
2115
|
+
unitX = WHEEL_LINE_HEIGHT;
|
|
2116
|
+
unitY = WHEEL_LINE_HEIGHT;
|
|
2117
|
+
}
|
|
2118
|
+
else if (event.deltaMode === 2) {
|
|
2119
|
+
unitX = wrapperComponent.clientWidth;
|
|
2120
|
+
unitY = wrapperComponent.clientHeight;
|
|
2121
|
+
}
|
|
2122
|
+
var x = event.deltaX * unitX;
|
|
2123
|
+
var y = event.deltaY * unitY;
|
|
2124
|
+
if (event.shiftKey && !x) {
|
|
2125
|
+
x = y;
|
|
2126
|
+
y = 0;
|
|
2127
|
+
}
|
|
2128
|
+
return { x: x * speed, y: y * speed };
|
|
2129
|
+
}
|
|
1865
2130
|
var getDeltaY = function (event) {
|
|
1866
2131
|
if (event) {
|
|
1867
2132
|
return event.deltaY < 0 ? 1 : -1;
|
|
@@ -2015,9 +2280,12 @@ var handleWheelZoom = function (contextInstance, event) {
|
|
|
2015
2280
|
event.preventDefault();
|
|
2016
2281
|
}
|
|
2017
2282
|
event.stopPropagation();
|
|
2283
|
+
var isPanning = contextInstance.isPanning;
|
|
2018
2284
|
var delta = getDelta(event, null);
|
|
2019
2285
|
var zoomStep = smooth ? step * Math.abs(event.deltaY) : step;
|
|
2020
|
-
|
|
2286
|
+
// Mid-drag the scale stays inside min/max: the overshoot would need the
|
|
2287
|
+
// settle animation, which cannot run while the pointer owns the position.
|
|
2288
|
+
var newScale = handleCalculateWheelZoom(contextInstance, delta, zoomStep, !event.ctrlKey || isPanning);
|
|
2021
2289
|
// if scale not change
|
|
2022
2290
|
if (scale === newScale)
|
|
2023
2291
|
return;
|
|
@@ -2028,6 +2296,15 @@ var handleWheelZoom = function (contextInstance, event) {
|
|
|
2028
2296
|
var _b = handleCalculateZoomPositions(contextInstance, mousePosition.x, mousePosition.y, newScale, bounds, isLimitedToBounds), x = _b.x, y = _b.y;
|
|
2029
2297
|
contextInstance.previousWheelEvent = event;
|
|
2030
2298
|
contextInstance.setState(newScale, x, y);
|
|
2299
|
+
// Zoom during a drag (#543): the pan maps pointer → position through
|
|
2300
|
+
// `startCoords`, taken at the old scale. Re-anchor it at the zoomed state,
|
|
2301
|
+
// or the next move snaps the content back. The velocity sample from before
|
|
2302
|
+
// the zoom would read the jump as a fling, so drop it too.
|
|
2303
|
+
if (isPanning) {
|
|
2304
|
+
handlePanningSetup(contextInstance, event);
|
|
2305
|
+
contextInstance.lastMousePosition = null;
|
|
2306
|
+
contextInstance.velocity = null;
|
|
2307
|
+
}
|
|
2031
2308
|
handleCallback(getContext(contextInstance), event, onWheel);
|
|
2032
2309
|
handleCallback(getContext(contextInstance), event, onZoom);
|
|
2033
2310
|
};
|
|
@@ -2038,8 +2315,12 @@ var handleWheelStop = function (contextInstance, event) {
|
|
|
2038
2315
|
contextInstance.wheelAnimationTimer = setTimeout(function () {
|
|
2039
2316
|
if (!contextInstance.mounted)
|
|
2040
2317
|
return;
|
|
2041
|
-
handleAlignToScaleBounds(contextInstance, event.x, event.y);
|
|
2042
2318
|
contextInstance.wheelAnimationTimer = null;
|
|
2319
|
+
// Mid-drag the pan end aligns (and flushes a pending resize) instead:
|
|
2320
|
+
// an animation now would fight the pointer (#543).
|
|
2321
|
+
if (contextInstance.isPanning)
|
|
2322
|
+
return;
|
|
2323
|
+
handleAlignToScaleBounds(contextInstance, event.x, event.y);
|
|
2043
2324
|
contextInstance.flushResizeAlignment();
|
|
2044
2325
|
}, wheelAnimationTime);
|
|
2045
2326
|
// Wheel stop event
|
|
@@ -2195,12 +2476,12 @@ var handleDoubleClickResetMode = function (contextInstance, event) {
|
|
|
2195
2476
|
});
|
|
2196
2477
|
handleDoubleClickStop(contextInstance, event);
|
|
2197
2478
|
};
|
|
2198
|
-
function getDoubleClickScale(mode
|
|
2199
|
-
if (mode === "toggle") {
|
|
2200
|
-
return scale === 1 ? 1 : -1;
|
|
2201
|
-
}
|
|
2479
|
+
function getDoubleClickScale(mode) {
|
|
2202
2480
|
return mode === "zoomOut" ? -1 : 1;
|
|
2203
2481
|
}
|
|
2482
|
+
// Scales closer than this count as "at the reset scale": animations and fits
|
|
2483
|
+
// land on float values.
|
|
2484
|
+
var RESET_SCALE_TOLERANCE = 1e-3;
|
|
2204
2485
|
function handleDoubleClick(contextInstance, event) {
|
|
2205
2486
|
var setup = contextInstance.setup, doubleClickStopEventTimer = contextInstance.doubleClickStopEventTimer, state = contextInstance.state, contentComponent = contextInstance.contentComponent;
|
|
2206
2487
|
var scale = state.scale;
|
|
@@ -2213,9 +2494,19 @@ function handleDoubleClick(contextInstance, event) {
|
|
|
2213
2494
|
if (mode === "reset") {
|
|
2214
2495
|
return handleDoubleClickResetMode(contextInstance, event);
|
|
2215
2496
|
}
|
|
2497
|
+
// `toggle` (#125): at the reset scale (`initialScale`, or the fitted one
|
|
2498
|
+
// with `fitOnInit`) zoom in by `step`; anywhere else go back to the reset
|
|
2499
|
+
// state. It used to compare against 1, so with another initial scale the
|
|
2500
|
+
// first double click zoomed out, and the way back only stepped out.
|
|
2501
|
+
if (mode === "toggle") {
|
|
2502
|
+
var resetScale = getResetScale(contextInstance);
|
|
2503
|
+
if (Math.abs(scale - resetScale) > RESET_SCALE_TOLERANCE) {
|
|
2504
|
+
return handleDoubleClickResetMode(contextInstance, event);
|
|
2505
|
+
}
|
|
2506
|
+
}
|
|
2216
2507
|
if (!contentComponent)
|
|
2217
2508
|
return console.error("No ContentComponent found");
|
|
2218
|
-
var delta = getDoubleClickScale(mode
|
|
2509
|
+
var delta = getDoubleClickScale(mode);
|
|
2219
2510
|
var newScale = handleCalculateButtonZoom(contextInstance, delta, step);
|
|
2220
2511
|
// stop execution when scale didn't change
|
|
2221
2512
|
if (scale === newScale)
|
|
@@ -2269,6 +2560,10 @@ var ZoomPanPinch = /** @class */ (function () {
|
|
|
2269
2560
|
this.isWheelPanning = false;
|
|
2270
2561
|
this.startCoords = null;
|
|
2271
2562
|
this.panStartPosition = null;
|
|
2563
|
+
// Pointer travel of the running mouse/touch pan: whether it moved at all
|
|
2564
|
+
// (the first move decides `releaseAtBounds`, #509) and whether it went
|
|
2565
|
+
// past the click distance (the click after it is swallowed, #452).
|
|
2566
|
+
this.panGesture = null;
|
|
2272
2567
|
this.lastTouch = null;
|
|
2273
2568
|
// pinch helpers
|
|
2274
2569
|
this.isPinching = false;
|
|
@@ -2398,6 +2693,7 @@ var ZoomPanPinch = /** @class */ (function () {
|
|
|
2398
2693
|
wrapper.removeEventListener("touchstart", _this.onTouchPanningStart, passive);
|
|
2399
2694
|
wrapper.removeEventListener("touchmove", _this.onTouchPanning, passive);
|
|
2400
2695
|
wrapper.removeEventListener("touchend", _this.onTouchPanningStop, passive);
|
|
2696
|
+
wrapper.removeEventListener("scroll", _this.onWrapperScroll, passive);
|
|
2401
2697
|
};
|
|
2402
2698
|
this.handleInitializeWrapperEvents = function (wrapper) {
|
|
2403
2699
|
// Zooming events on wrapper
|
|
@@ -2408,12 +2704,31 @@ var ZoomPanPinch = /** @class */ (function () {
|
|
|
2408
2704
|
wrapper.addEventListener("touchstart", _this.onTouchPanningStart, passive);
|
|
2409
2705
|
wrapper.addEventListener("touchmove", _this.onTouchPanning, passive);
|
|
2410
2706
|
wrapper.addEventListener("touchend", _this.onTouchPanningStop, passive);
|
|
2707
|
+
wrapper.addEventListener("scroll", _this.onWrapperScroll, passive);
|
|
2708
|
+
};
|
|
2709
|
+
// The wrapper clips with `overflow: hidden`, but the browser still scrolls
|
|
2710
|
+
// it to reveal a focused input (the virtual keyboard on mobile) or for
|
|
2711
|
+
// `scrollIntoView`. Nothing in the transform knows about that offset, so
|
|
2712
|
+
// the content stayed shifted and could not be panned back (#280, #477).
|
|
2713
|
+
// Fold the scroll into the position instead: the target stays in view and
|
|
2714
|
+
// the state matches what is on screen.
|
|
2715
|
+
this.onWrapperScroll = function () {
|
|
2716
|
+
var wrapper = _this.wrapperComponent;
|
|
2717
|
+
if (!wrapper)
|
|
2718
|
+
return;
|
|
2719
|
+
var scrollLeft = wrapper.scrollLeft, scrollTop = wrapper.scrollTop;
|
|
2720
|
+
if (!scrollLeft && !scrollTop)
|
|
2721
|
+
return;
|
|
2722
|
+
wrapper.scrollLeft = 0;
|
|
2723
|
+
wrapper.scrollTop = 0;
|
|
2724
|
+
var _a = _this.state, scale = _a.scale, positionX = _a.positionX, positionY = _a.positionY;
|
|
2725
|
+
_this.setState(scale, positionX - scrollLeft, positionY - scrollTop);
|
|
2411
2726
|
};
|
|
2412
2727
|
this.handleInitialize = function () {
|
|
2413
2728
|
var _a = _this.setup, centerOnInit = _a.centerOnInit, fitOnInit = _a.fitOnInit;
|
|
2414
2729
|
_this.applyTransformation();
|
|
2415
2730
|
_this.onInitCallbacks.forEach(function (callback) { return callback(getContext(_this)); });
|
|
2416
|
-
if (centerOnInit || fitOnInit) {
|
|
2731
|
+
if (centerOnInit || fitOnInit || hasNamedInitialPosition(_this)) {
|
|
2417
2732
|
var applied = _this.applyInitialLayout();
|
|
2418
2733
|
// Content without a size yet (an image still loading) gets its layout
|
|
2419
2734
|
// applied again by `onSizeChange` once it has one. Once a real layout
|
|
@@ -2506,26 +2821,30 @@ var ZoomPanPinch = /** @class */ (function () {
|
|
|
2506
2821
|
this.onWheelPanning = function (event) {
|
|
2507
2822
|
var onPanning = _this.props.onPanning;
|
|
2508
2823
|
var trackPadPanning = _this.setup.trackPadPanning;
|
|
2509
|
-
var lockAxisX = trackPadPanning.lockAxisX, lockAxisY = trackPadPanning.lockAxisY;
|
|
2824
|
+
var lockAxisX = trackPadPanning.lockAxisX, lockAxisY = trackPadPanning.lockAxisY, speed = trackPadPanning.speed;
|
|
2825
|
+
var wrapper = _this.wrapperComponent;
|
|
2510
2826
|
_this.syncModifierKeys(event);
|
|
2511
2827
|
var isAllowed = isWheelPanningAllowed(_this, event);
|
|
2512
|
-
if (!isAllowed)
|
|
2828
|
+
if (!isAllowed || !wrapper)
|
|
2513
2829
|
return;
|
|
2514
2830
|
if (event.cancelable) {
|
|
2515
2831
|
event.preventDefault();
|
|
2516
2832
|
}
|
|
2517
2833
|
event.stopPropagation();
|
|
2518
|
-
var
|
|
2519
|
-
var
|
|
2520
|
-
var
|
|
2521
|
-
|
|
2522
|
-
|
|
2523
|
-
var
|
|
2834
|
+
var delta = getWheelPanDelta(event, wrapper, speed);
|
|
2835
|
+
var deltaX = lockAxisX ? 0 : delta.x;
|
|
2836
|
+
var deltaY = lockAxisY ? 0 : delta.y;
|
|
2837
|
+
if (!deltaX && !deltaY)
|
|
2838
|
+
return;
|
|
2839
|
+
var _a = _this.setup.autoAlignment, sizeX = _a.sizeX, sizeY = _a.sizeY;
|
|
2524
2840
|
var paddingValueX = getPaddingValue(_this, sizeX);
|
|
2525
2841
|
var paddingValueY = getPaddingValue(_this, sizeY);
|
|
2526
|
-
if (newPositionX === positionX && newPositionY === positionY)
|
|
2527
|
-
return;
|
|
2528
2842
|
handleWheelPanningStart(_this, event);
|
|
2843
|
+
// Deltas add up on the pointer's side of the rubber band, the same way
|
|
2844
|
+
// a drag does, so the content slows down towards the padding limit.
|
|
2845
|
+
var source = getPanSourcePosition(_this);
|
|
2846
|
+
var newPositionX = source.x - deltaX;
|
|
2847
|
+
var newPositionY = source.y - deltaY;
|
|
2529
2848
|
handleNewPosition(_this, newPositionX, newPositionY, paddingValueX, paddingValueY);
|
|
2530
2849
|
handleCallback(getContext(_this), event, onPanning);
|
|
2531
2850
|
handleWheelPanningStop(_this, event);
|
|
@@ -2559,6 +2878,7 @@ var ZoomPanPinch = /** @class */ (function () {
|
|
|
2559
2878
|
event.stopPropagation();
|
|
2560
2879
|
handleCancelAnimation(_this);
|
|
2561
2880
|
handlePanningStart(_this, event);
|
|
2881
|
+
_this.startPanGesture(event.clientX, event.clientY);
|
|
2562
2882
|
_this.lockTextSelection();
|
|
2563
2883
|
_this.focusForKeyboard();
|
|
2564
2884
|
handleCallback(getContext(_this), event, onPanningStart);
|
|
@@ -2581,6 +2901,10 @@ var ZoomPanPinch = /** @class */ (function () {
|
|
|
2581
2901
|
var keysPressed = _this.isPressingKeys(_this.setup.panning.activationKeys);
|
|
2582
2902
|
if (!keysPressed)
|
|
2583
2903
|
return;
|
|
2904
|
+
if (_this.trackPanGesture(event.clientX, event.clientY)) {
|
|
2905
|
+
_this.onPanningStop(event);
|
|
2906
|
+
return;
|
|
2907
|
+
}
|
|
2584
2908
|
if (event.cancelable) {
|
|
2585
2909
|
event.preventDefault();
|
|
2586
2910
|
}
|
|
@@ -2589,15 +2913,73 @@ var ZoomPanPinch = /** @class */ (function () {
|
|
|
2589
2913
|
handleCallback(getContext(_this), event, onPanning);
|
|
2590
2914
|
};
|
|
2591
2915
|
this.onPanningStop = function (event) {
|
|
2592
|
-
var
|
|
2916
|
+
var _a;
|
|
2917
|
+
var _b = _this.setup.panning, velocityDisabled = _b.velocityDisabled, suppressClickAfterPan = _b.suppressClickAfterPan;
|
|
2593
2918
|
var onPanningStop = _this.props.onPanningStop;
|
|
2594
2919
|
if (_this.isPanning) {
|
|
2920
|
+
var isDrag = !!((_a = _this.panGesture) === null || _a === void 0 ? void 0 : _a.isDrag);
|
|
2921
|
+
_this.panGesture = null;
|
|
2595
2922
|
_this.unlockTextSelection();
|
|
2596
2923
|
handlePanningEnd(_this, velocityDisabled);
|
|
2597
2924
|
handleCallback(getContext(_this), event, onPanningStop);
|
|
2598
2925
|
_this.flushResizeAlignment();
|
|
2926
|
+
// Only a mouseup is followed by a click on the content; a released or
|
|
2927
|
+
// cancelled gesture (mousemove, mouseleave, touch) is not.
|
|
2928
|
+
if (isDrag && suppressClickAfterPan && event.type === "mouseup") {
|
|
2929
|
+
_this.suppressNextClick();
|
|
2930
|
+
}
|
|
2599
2931
|
}
|
|
2600
2932
|
};
|
|
2933
|
+
this.startPanGesture = function (clientX, clientY) {
|
|
2934
|
+
_this.panGesture = {
|
|
2935
|
+
startX: clientX,
|
|
2936
|
+
startY: clientY,
|
|
2937
|
+
hasMoved: false,
|
|
2938
|
+
isDrag: false,
|
|
2939
|
+
};
|
|
2940
|
+
};
|
|
2941
|
+
// Records the pointer travel of the running pan. Returns true when this is
|
|
2942
|
+
// the first move, it heads into a bound the content already rests on and
|
|
2943
|
+
// `releaseAtBounds` is on: the caller then ends the pan without claiming
|
|
2944
|
+
// the event, so the page scrolls or a parent takes the drag (#509).
|
|
2945
|
+
this.trackPanGesture = function (clientX, clientY) {
|
|
2946
|
+
var gesture = _this.panGesture;
|
|
2947
|
+
if (!gesture)
|
|
2948
|
+
return false;
|
|
2949
|
+
var dx = clientX - gesture.startX;
|
|
2950
|
+
var dy = clientY - gesture.startY;
|
|
2951
|
+
if (!dx && !dy)
|
|
2952
|
+
return false;
|
|
2953
|
+
var isFirstMove = !gesture.hasMoved;
|
|
2954
|
+
gesture.hasMoved = true;
|
|
2955
|
+
if (dx * dx + dy * dy > Math.pow(CLICK_DISTANCE, 2))
|
|
2956
|
+
gesture.isDrag = true;
|
|
2957
|
+
return (isFirstMove &&
|
|
2958
|
+
_this.setup.panning.releaseAtBounds &&
|
|
2959
|
+
isPanBlockedAtBounds(_this, dx, dy));
|
|
2960
|
+
};
|
|
2961
|
+
// The browser fires `click` right after `mouseup`, in the same task, so a
|
|
2962
|
+
// capture listener on the window that removes itself on the next tick
|
|
2963
|
+
// swallows exactly that click and nothing later (#452).
|
|
2964
|
+
this.suppressNextClick = function () {
|
|
2965
|
+
var _a;
|
|
2966
|
+
var wrapper = _this.wrapperComponent;
|
|
2967
|
+
var win = (_a = wrapper === null || wrapper === void 0 ? void 0 : wrapper.ownerDocument) === null || _a === void 0 ? void 0 : _a.defaultView;
|
|
2968
|
+
if (!wrapper || !win)
|
|
2969
|
+
return;
|
|
2970
|
+
var swallow = function (event) {
|
|
2971
|
+
var path = typeof event.composedPath === "function" ? event.composedPath() : [];
|
|
2972
|
+
var isInside = path.includes(wrapper) || wrapper.contains(event.target);
|
|
2973
|
+
if (!isInside)
|
|
2974
|
+
return;
|
|
2975
|
+
event.preventDefault();
|
|
2976
|
+
event.stopPropagation();
|
|
2977
|
+
};
|
|
2978
|
+
win.addEventListener("click", swallow, { capture: true, once: true });
|
|
2979
|
+
win.setTimeout(function () {
|
|
2980
|
+
win.removeEventListener("click", swallow, { capture: true });
|
|
2981
|
+
}, 0);
|
|
2982
|
+
};
|
|
2601
2983
|
/// ///////
|
|
2602
2984
|
// Pinch
|
|
2603
2985
|
/// ///////
|
|
@@ -2664,6 +3046,7 @@ var ZoomPanPinch = /** @class */ (function () {
|
|
|
2664
3046
|
return;
|
|
2665
3047
|
handleCancelAnimation(_this);
|
|
2666
3048
|
handlePanningStart(_this, event);
|
|
3049
|
+
_this.startPanGesture(touches[0].clientX, touches[0].clientY);
|
|
2667
3050
|
_this.lockTextSelection();
|
|
2668
3051
|
_this.focusForKeyboard();
|
|
2669
3052
|
handleCallback(getContext(_this), event, onPanningStart);
|
|
@@ -2682,11 +3065,16 @@ var ZoomPanPinch = /** @class */ (function () {
|
|
|
2682
3065
|
var isAllowed = isPanningAllowed(_this);
|
|
2683
3066
|
if (!isAllowed)
|
|
2684
3067
|
return;
|
|
3068
|
+
var touch = event.touches[0];
|
|
3069
|
+
// Not cancelling the first touchmove is what lets the browser scroll.
|
|
3070
|
+
if (_this.trackPanGesture(touch.clientX, touch.clientY)) {
|
|
3071
|
+
_this.onPanningStop(event);
|
|
3072
|
+
return;
|
|
3073
|
+
}
|
|
2685
3074
|
if (event.cancelable) {
|
|
2686
3075
|
event.preventDefault();
|
|
2687
3076
|
}
|
|
2688
3077
|
event.stopPropagation();
|
|
2689
|
-
var touch = event.touches[0];
|
|
2690
3078
|
handlePanning(_this, touch.clientX, touch.clientY, exports.DeviceType.TOUCH);
|
|
2691
3079
|
handleCallback(getContext(_this), event, onPanning);
|
|
2692
3080
|
}
|
|
@@ -2726,6 +3114,7 @@ var ZoomPanPinch = /** @class */ (function () {
|
|
|
2726
3114
|
if (_this.isPanning) {
|
|
2727
3115
|
_this.isPanning = false;
|
|
2728
3116
|
_this.startCoords = null;
|
|
3117
|
+
_this.panGesture = null;
|
|
2729
3118
|
}
|
|
2730
3119
|
_this.unlockTextSelection();
|
|
2731
3120
|
_this.flushResizeAlignment();
|
|
@@ -2833,24 +3222,30 @@ var ZoomPanPinch = /** @class */ (function () {
|
|
|
2833
3222
|
/// ///////
|
|
2834
3223
|
// `fitOnInit` wins over `centerOnInit` (a fit is always centred). Falls
|
|
2835
3224
|
// back to centring while the content has no size yet; `onSizeChange`
|
|
2836
|
-
// re-runs this once it does.
|
|
2837
|
-
// whether
|
|
3225
|
+
// re-runs this once it does. A named `initialPositionX/Y` then places its
|
|
3226
|
+
// own axis (#349). Returns whether both boxes had a size, i.e. whether
|
|
3227
|
+
// this was a real initial layout.
|
|
2838
3228
|
this.applyInitialLayout = function () {
|
|
2839
|
-
var
|
|
2840
|
-
var
|
|
2841
|
-
|
|
2842
|
-
|
|
2843
|
-
|
|
3229
|
+
var _a = _this.setup, fitOnInit = _a.fitOnInit, centerOnInit = _a.centerOnInit;
|
|
3230
|
+
var _b = _this, wrapperComponent = _b.wrapperComponent, contentComponent = _b.contentComponent;
|
|
3231
|
+
if (!wrapperComponent || !contentComponent)
|
|
3232
|
+
return false;
|
|
3233
|
+
var hasSizes = wrapperComponent.clientWidth > 0 &&
|
|
2844
3234
|
wrapperComponent.clientHeight > 0 &&
|
|
2845
3235
|
(contentComponent.offsetWidth > 0 || contentComponent.offsetHeight > 0);
|
|
2846
|
-
|
|
2847
|
-
|
|
2848
|
-
|
|
2849
|
-
|
|
2850
|
-
|
|
2851
|
-
|
|
3236
|
+
var _c = _this.state, scale = _c.scale, positionX = _c.positionX, positionY = _c.positionY;
|
|
3237
|
+
var target = { scale: scale, positionX: positionX, positionY: positionY };
|
|
3238
|
+
var fitted = fitOnInit
|
|
3239
|
+
? calculateFitToView(_this, fitOnInit === "cover" ? "cover" : "contain")
|
|
3240
|
+
: null;
|
|
3241
|
+
if (fitted) {
|
|
3242
|
+
target = fitted;
|
|
2852
3243
|
}
|
|
2853
|
-
|
|
3244
|
+
else if (fitOnInit || centerOnInit) {
|
|
3245
|
+
target = getCenterPosition(scale, wrapperComponent, contentComponent);
|
|
3246
|
+
}
|
|
3247
|
+
var placed = applyNamedInitialPosition(_this, target);
|
|
3248
|
+
_this.setState(placed.scale, placed.positionX, placed.positionY);
|
|
2854
3249
|
return hasSizes;
|
|
2855
3250
|
};
|
|
2856
3251
|
this.setCenter = function () {
|
|
@@ -2951,7 +3346,6 @@ var ZoomPanPinch = /** @class */ (function () {
|
|
|
2951
3346
|
_this.isInitialized = true;
|
|
2952
3347
|
var ctx = getContext(_this);
|
|
2953
3348
|
handleCallback(ctx, undefined, _this.props.onInit);
|
|
2954
|
-
assignRef(_this.props.ref, ctx);
|
|
2955
3349
|
};
|
|
2956
3350
|
this.props = props;
|
|
2957
3351
|
this.setup = createSetup(this.props);
|
|
@@ -3057,210 +3451,317 @@ var useResize = function (ref, onResize, dependencies) {
|
|
|
3057
3451
|
|
|
3058
3452
|
var previewStyles = {
|
|
3059
3453
|
position: "absolute",
|
|
3060
|
-
zIndex:
|
|
3454
|
+
zIndex: 3,
|
|
3061
3455
|
top: "0px",
|
|
3062
3456
|
left: "0px",
|
|
3063
3457
|
boxSizing: "border-box",
|
|
3064
|
-
border: "
|
|
3458
|
+
border: "2px solid red",
|
|
3065
3459
|
transformOrigin: "0% 0%",
|
|
3066
|
-
boxShadow: "rgba(0,0,0,0.2) 0 0 0 10000000px",
|
|
3067
3460
|
pointerEvents: "none",
|
|
3068
3461
|
};
|
|
3462
|
+
var wrapperStyles = {
|
|
3463
|
+
position: "absolute",
|
|
3464
|
+
top: "0px",
|
|
3465
|
+
left: "0px",
|
|
3466
|
+
transformOrigin: "0% 0%",
|
|
3467
|
+
boxSizing: "border-box",
|
|
3468
|
+
zIndex: 1,
|
|
3469
|
+
pointerEvents: "none",
|
|
3470
|
+
};
|
|
3471
|
+
var maskStyles = {
|
|
3472
|
+
position: "absolute",
|
|
3473
|
+
top: "0px",
|
|
3474
|
+
left: "0px",
|
|
3475
|
+
zIndex: 2,
|
|
3476
|
+
pointerEvents: "none",
|
|
3477
|
+
display: "block",
|
|
3478
|
+
};
|
|
3479
|
+
var getTouchPoint = function (event) {
|
|
3480
|
+
var _a;
|
|
3481
|
+
var touch = (_a = event.touches[0]) !== null && _a !== void 0 ? _a : event.changedTouches[0];
|
|
3482
|
+
if (!touch)
|
|
3483
|
+
return null;
|
|
3484
|
+
return { x: touch.clientX, y: touch.clientY };
|
|
3485
|
+
};
|
|
3486
|
+
var isInside = function (element, target) {
|
|
3487
|
+
return !!element &&
|
|
3488
|
+
!!target &&
|
|
3489
|
+
(target === element || element.contains(target));
|
|
3490
|
+
};
|
|
3069
3491
|
var MiniMap = function (_a) {
|
|
3070
|
-
var _b = _a.width, width = _b === void 0 ? 200 : _b, _c = _a.height, height = _c === void 0 ? 200 : _c, _d = _a.borderColor, borderColor = _d === void 0 ? "red" : _d, previewStyle = _a.previewStyle, wrapperClassName = _a.wrapperClassName, previewClassName = _a.previewClassName, children = _a.children,
|
|
3071
|
-
var
|
|
3492
|
+
var _b = _a.width, width = _b === void 0 ? 200 : _b, _c = _a.height, height = _c === void 0 ? 200 : _c, _d = _a.borderColor, borderColor = _d === void 0 ? "red" : _d, _e = _a.maskColor, maskColor = _e === void 0 ? "rgba(0, 0, 0, 0.2)" : _e, previewStyle = _a.previewStyle, wrapperClassName = _a.wrapperClassName, previewClassName = _a.previewClassName, children = _a.children, _f = _a.panning, panning = _f === void 0 ? true : _f, _g = _a.inversePan, inversePan = _g === void 0 ? false : _g, _h = _a.zoomable, zoomable = _h === void 0 ? false : _h, _j = _a.zoomStep, zoomStep = _j === void 0 ? 0.2 : _j, _k = _a.offsetScale, offsetScale = _k === void 0 ? 5 : _k, onClick = _a.onClick, rest = __rest(_a, ["width", "height", "borderColor", "maskColor", "previewStyle", "wrapperClassName", "previewClassName", "children", "panning", "inversePan", "zoomable", "zoomStep", "offsetScale", "onClick"]);
|
|
3493
|
+
var _l = React.useState(false), initialized = _l[0], setInitialized = _l[1];
|
|
3072
3494
|
var instance = useTransformContext();
|
|
3073
|
-
var _g = React.useState(false), isDown = _g[0], setIsDown = _g[1];
|
|
3074
3495
|
var mainRef = React.useRef(null);
|
|
3075
3496
|
var wrapperRef = React.useRef(null);
|
|
3076
3497
|
var previewRef = React.useRef(null);
|
|
3498
|
+
var maskPathRef = React.useRef(null);
|
|
3499
|
+
var dragRef = React.useRef(null);
|
|
3500
|
+
var layoutRef = React.useRef(null);
|
|
3501
|
+
// Latest props for the document listeners, which are registered once.
|
|
3502
|
+
var optionsRef = React.useRef({ panning: panning, inversePan: inversePan, zoomStep: zoomStep, onClick: onClick });
|
|
3503
|
+
optionsRef.current = { panning: panning, inversePan: inversePan, zoomStep: zoomStep, onClick: onClick };
|
|
3077
3504
|
var getViewportSize = React.useCallback(function () {
|
|
3078
3505
|
if (instance.wrapperComponent) {
|
|
3079
3506
|
var rect = instance.wrapperComponent.getBoundingClientRect();
|
|
3080
|
-
return {
|
|
3081
|
-
width: rect.width,
|
|
3082
|
-
height: rect.height,
|
|
3083
|
-
};
|
|
3507
|
+
return { width: rect.width, height: rect.height };
|
|
3084
3508
|
}
|
|
3085
|
-
return {
|
|
3086
|
-
|
|
3087
|
-
height: 0,
|
|
3088
|
-
};
|
|
3089
|
-
}, [instance.wrapperComponent]);
|
|
3509
|
+
return { width: 0, height: 0 };
|
|
3510
|
+
}, [instance]);
|
|
3090
3511
|
var getContentSize = React.useCallback(function () {
|
|
3091
3512
|
if (instance.contentComponent) {
|
|
3092
3513
|
var rect = instance.contentComponent.getBoundingClientRect();
|
|
3093
|
-
|
|
3094
|
-
|
|
3095
|
-
height: rect.height / instance.state.scale,
|
|
3096
|
-
};
|
|
3097
|
-
}
|
|
3098
|
-
return {
|
|
3099
|
-
width: 0,
|
|
3100
|
-
height: 0,
|
|
3101
|
-
};
|
|
3102
|
-
}, [instance.contentComponent, instance.state.scale]);
|
|
3103
|
-
var computeMiniMapScale = React.useCallback(function () {
|
|
3104
|
-
var contentSize = getContentSize();
|
|
3105
|
-
var scaleX = width / contentSize.width;
|
|
3106
|
-
var scaleY = height / contentSize.height;
|
|
3107
|
-
var scale = scaleY > scaleX ? scaleX : scaleY;
|
|
3108
|
-
return scale;
|
|
3109
|
-
}, [getContentSize, height, width]);
|
|
3110
|
-
var computeMiniMapSize = function () {
|
|
3111
|
-
var contentSize = getContentSize();
|
|
3112
|
-
var scaleX = width / contentSize.width;
|
|
3113
|
-
var scaleY = height / contentSize.height;
|
|
3114
|
-
if (scaleY > scaleX) {
|
|
3115
|
-
return { width: width, height: contentSize.height * scaleX };
|
|
3514
|
+
var scale = instance.state.scale;
|
|
3515
|
+
return { width: rect.width / scale, height: rect.height / scale };
|
|
3116
3516
|
}
|
|
3117
|
-
return { width:
|
|
3118
|
-
};
|
|
3119
|
-
|
|
3120
|
-
|
|
3121
|
-
|
|
3122
|
-
|
|
3123
|
-
|
|
3124
|
-
|
|
3125
|
-
|
|
3126
|
-
|
|
3127
|
-
|
|
3128
|
-
|
|
3129
|
-
if (
|
|
3130
|
-
|
|
3131
|
-
el.transform = style.transform;
|
|
3132
|
-
el.transformOrigin = style.transformOrigin;
|
|
3133
|
-
el.position = style.position;
|
|
3134
|
-
el.boxSizing = style.boxSizing;
|
|
3135
|
-
el.zIndex = String(style.zIndex);
|
|
3517
|
+
return { width: 0, height: 0 };
|
|
3518
|
+
}, [instance]);
|
|
3519
|
+
/**
|
|
3520
|
+
* Like React Flow, the map shows the union of the content and the current
|
|
3521
|
+
* viewport (plus `offsetScale` padding) fitted into the box, so the
|
|
3522
|
+
* viewport indicator never leaves the mini map even when the view is
|
|
3523
|
+
* panned or zoomed past the content.
|
|
3524
|
+
*/
|
|
3525
|
+
var computeLayout = React.useCallback(function () {
|
|
3526
|
+
var content = getContentSize();
|
|
3527
|
+
var viewport = getViewportSize();
|
|
3528
|
+
var _a = instance.state, scale = _a.scale, positionX = _a.positionX, positionY = _a.positionY;
|
|
3529
|
+
if (!content.width || !content.height || !width || !height || !scale) {
|
|
3530
|
+
return null;
|
|
3136
3531
|
}
|
|
3137
|
-
|
|
3138
|
-
|
|
3139
|
-
|
|
3140
|
-
var
|
|
3141
|
-
var
|
|
3532
|
+
var viewX = -positionX / scale;
|
|
3533
|
+
var viewY = -positionY / scale;
|
|
3534
|
+
var viewWidth = viewport.width / scale;
|
|
3535
|
+
var viewHeight = viewport.height / scale;
|
|
3536
|
+
var minX = Math.min(0, viewX);
|
|
3537
|
+
var minY = Math.min(0, viewY);
|
|
3538
|
+
var maxX = Math.max(content.width, viewX + viewWidth);
|
|
3539
|
+
var maxY = Math.max(content.height, viewY + viewHeight);
|
|
3540
|
+
var boundsWidth = maxX - minX;
|
|
3541
|
+
var boundsHeight = maxY - minY;
|
|
3542
|
+
var viewScale = Math.max(boundsWidth / width, boundsHeight / height);
|
|
3543
|
+
var fitWidth = viewScale * width;
|
|
3544
|
+
var fitHeight = viewScale * height;
|
|
3545
|
+
var offset = offsetScale * viewScale;
|
|
3546
|
+
var originX = minX - (fitWidth - boundsWidth) / 2 - offset;
|
|
3547
|
+
var originY = minY - (fitHeight - boundsHeight) / 2 - offset;
|
|
3548
|
+
var mapScale = width / (fitWidth + offset * 2);
|
|
3549
|
+
return {
|
|
3550
|
+
mapScale: mapScale,
|
|
3551
|
+
originX: originX,
|
|
3552
|
+
originY: originY,
|
|
3553
|
+
contentWidth: content.width,
|
|
3554
|
+
contentHeight: content.height,
|
|
3555
|
+
previewX: (viewX - originX) * mapScale,
|
|
3556
|
+
previewY: (viewY - originY) * mapScale,
|
|
3557
|
+
previewWidth: viewWidth * mapScale,
|
|
3558
|
+
previewHeight: viewHeight * mapScale,
|
|
3559
|
+
};
|
|
3560
|
+
}, [getContentSize, getViewportSize, height, instance, offsetScale, width]);
|
|
3561
|
+
var transformMiniMap = React.useCallback(function () {
|
|
3562
|
+
var layout = computeLayout();
|
|
3563
|
+
layoutRef.current = layout;
|
|
3564
|
+
if (!layout)
|
|
3565
|
+
return;
|
|
3566
|
+
var px = function (value) { return "".concat(roundNumber(value, 2), "px"); };
|
|
3142
3567
|
if (wrapperRef.current) {
|
|
3143
|
-
|
|
3144
|
-
|
|
3145
|
-
|
|
3146
|
-
|
|
3147
|
-
mainRef.current.style.width = "".concat(miniSize.width, "px");
|
|
3148
|
-
mainRef.current.style.height = "".concat(miniSize.height, "px");
|
|
3568
|
+
var style = wrapperRef.current.style;
|
|
3569
|
+
style.width = px(layout.contentWidth);
|
|
3570
|
+
style.height = px(layout.contentHeight);
|
|
3571
|
+
style.transform = "translate(".concat(px(-layout.originX * layout.mapScale), ", ").concat(px(-layout.originY * layout.mapScale), ") scale(").concat(roundNumber(layout.mapScale, 4), ")");
|
|
3149
3572
|
}
|
|
3150
3573
|
if (previewRef.current) {
|
|
3151
|
-
var
|
|
3152
|
-
|
|
3153
|
-
|
|
3154
|
-
|
|
3155
|
-
previewRef.current.style.transform = transform;
|
|
3156
|
-
previewRef.current.style.width = "".concat(size.width * previewScale, "px");
|
|
3157
|
-
previewRef.current.style.height = "".concat(size.height * previewScale, "px");
|
|
3574
|
+
var style = previewRef.current.style;
|
|
3575
|
+
style.transform = "translate(".concat(px(layout.previewX), ", ").concat(px(layout.previewY), ")");
|
|
3576
|
+
style.width = px(layout.previewWidth);
|
|
3577
|
+
style.height = px(layout.previewHeight);
|
|
3158
3578
|
}
|
|
3159
|
-
|
|
3160
|
-
|
|
3161
|
-
|
|
3162
|
-
|
|
3163
|
-
|
|
3164
|
-
|
|
3165
|
-
|
|
3579
|
+
if (maskPathRef.current) {
|
|
3580
|
+
var x = roundNumber(layout.previewX, 2);
|
|
3581
|
+
var y = roundNumber(layout.previewY, 2);
|
|
3582
|
+
var w = roundNumber(layout.previewWidth, 2);
|
|
3583
|
+
var h = roundNumber(layout.previewHeight, 2);
|
|
3584
|
+
maskPathRef.current.setAttribute("d", "M0 0H".concat(width, "V").concat(height, "H0Z M").concat(x, " ").concat(y, "h").concat(w, "v").concat(h, "h").concat(-w, "Z"));
|
|
3585
|
+
}
|
|
3586
|
+
}, [computeLayout, height, width]);
|
|
3587
|
+
useTransformEffect(transformMiniMap);
|
|
3166
3588
|
useTransformInit(function () {
|
|
3167
|
-
|
|
3589
|
+
transformMiniMap();
|
|
3168
3590
|
setInitialized(true);
|
|
3169
3591
|
});
|
|
3170
|
-
useResize(instance.contentComponent,
|
|
3592
|
+
useResize(instance.contentComponent, transformMiniMap, [initialized]);
|
|
3593
|
+
React.useEffect(function () {
|
|
3594
|
+
transformMiniMap();
|
|
3595
|
+
}, [transformMiniMap]);
|
|
3171
3596
|
React.useEffect(function () {
|
|
3172
|
-
var
|
|
3173
|
-
|
|
3597
|
+
var element = mainRef.current;
|
|
3598
|
+
if (!element)
|
|
3599
|
+
return;
|
|
3600
|
+
var getMapScale = function () {
|
|
3174
3601
|
var _a, _b;
|
|
3175
|
-
|
|
3176
|
-
|
|
3177
|
-
|
|
3178
|
-
|
|
3179
|
-
|
|
3180
|
-
|
|
3181
|
-
|
|
3182
|
-
|
|
3183
|
-
|
|
3184
|
-
|
|
3185
|
-
|
|
3186
|
-
|
|
3187
|
-
|
|
3188
|
-
|
|
3189
|
-
|
|
3190
|
-
|
|
3191
|
-
|
|
3192
|
-
|
|
3193
|
-
|
|
3194
|
-
|
|
3602
|
+
var layout = (_a = layoutRef.current) !== null && _a !== void 0 ? _a : computeLayout();
|
|
3603
|
+
return (_b = layout === null || layout === void 0 ? void 0 : layout.mapScale) !== null && _b !== void 0 ? _b : 0;
|
|
3604
|
+
};
|
|
3605
|
+
var start = function (point, target) {
|
|
3606
|
+
if (!optionsRef.current.panning)
|
|
3607
|
+
return;
|
|
3608
|
+
if (!isInside(element, target))
|
|
3609
|
+
return;
|
|
3610
|
+
if (!instance.wrapperComponent || !instance.contentComponent)
|
|
3611
|
+
return;
|
|
3612
|
+
handleCancelAnimation(instance);
|
|
3613
|
+
dragRef.current = { x: point.x, y: point.y, moved: false };
|
|
3614
|
+
element.style.cursor = "grabbing";
|
|
3615
|
+
};
|
|
3616
|
+
var move = function (point) {
|
|
3617
|
+
var drag = dragRef.current;
|
|
3618
|
+
if (!drag)
|
|
3619
|
+
return;
|
|
3620
|
+
var deltaX = point.x - drag.x;
|
|
3621
|
+
var deltaY = point.y - drag.y;
|
|
3622
|
+
drag.x = point.x;
|
|
3623
|
+
drag.y = point.y;
|
|
3624
|
+
if (!deltaX && !deltaY)
|
|
3625
|
+
return;
|
|
3626
|
+
drag.moved = true;
|
|
3627
|
+
var mapScale = getMapScale();
|
|
3628
|
+
var wrapperComponent = instance.wrapperComponent, contentComponent = instance.contentComponent, setup = instance.setup, state = instance.state;
|
|
3629
|
+
if (!mapScale || !wrapperComponent || !contentComponent)
|
|
3630
|
+
return;
|
|
3631
|
+
// Dragging by one mini map px moves the viewport by 1 / mapScale
|
|
3632
|
+
// content units, so the indicator follows the pointer 1:1.
|
|
3633
|
+
var direction = optionsRef.current.inversePan ? 1 : -1;
|
|
3634
|
+
var moveScale = (direction * state.scale) / mapScale;
|
|
3635
|
+
var nextX = state.positionX + deltaX * moveScale;
|
|
3636
|
+
var nextY = state.positionY + deltaY * moveScale;
|
|
3637
|
+
var bounds = handleCalculateBounds(instance, state.scale);
|
|
3638
|
+
var _a = getMouseBoundedPosition(nextX, nextY, bounds, setup.limitToBounds, 0, 0, wrapperComponent), x = _a.x, y = _a.y;
|
|
3639
|
+
if (x === state.positionX && y === state.positionY)
|
|
3640
|
+
return;
|
|
3641
|
+
instance.setState(state.scale, x, y);
|
|
3195
3642
|
};
|
|
3196
|
-
var
|
|
3643
|
+
var stop = function (event, point) {
|
|
3197
3644
|
var _a;
|
|
3198
|
-
|
|
3199
|
-
|
|
3200
|
-
|
|
3201
|
-
|
|
3645
|
+
var drag = dragRef.current;
|
|
3646
|
+
if (!drag)
|
|
3647
|
+
return;
|
|
3648
|
+
dragRef.current = null;
|
|
3649
|
+
element.style.cursor = optionsRef.current.panning ? "grab" : "";
|
|
3650
|
+
var handleClick = optionsRef.current.onClick;
|
|
3651
|
+
if (drag.moved || !handleClick || !point)
|
|
3652
|
+
return;
|
|
3653
|
+
var layout = (_a = layoutRef.current) !== null && _a !== void 0 ? _a : computeLayout();
|
|
3654
|
+
if (!layout)
|
|
3655
|
+
return;
|
|
3656
|
+
var rect = element.getBoundingClientRect();
|
|
3657
|
+
handleClick(event, {
|
|
3658
|
+
x: layout.originX + (point.x - rect.left) / layout.mapScale,
|
|
3659
|
+
y: layout.originY + (point.y - rect.top) / layout.mapScale,
|
|
3660
|
+
});
|
|
3661
|
+
};
|
|
3662
|
+
var onMouseDown = function (event) {
|
|
3663
|
+
if (event.button !== 0)
|
|
3664
|
+
return;
|
|
3665
|
+
start({ x: event.clientX, y: event.clientY }, event.target);
|
|
3666
|
+
};
|
|
3667
|
+
var onMouseMove = function (event) {
|
|
3668
|
+
if (!dragRef.current)
|
|
3669
|
+
return;
|
|
3670
|
+
// The mouse was released outside the window: end the drag.
|
|
3671
|
+
if (event.buttons === 0) {
|
|
3672
|
+
stop(event, null);
|
|
3673
|
+
return;
|
|
3202
3674
|
}
|
|
3675
|
+
move({ x: event.clientX, y: event.clientY });
|
|
3203
3676
|
};
|
|
3204
|
-
var
|
|
3205
|
-
|
|
3677
|
+
var onMouseUp = function (event) {
|
|
3678
|
+
stop(event, { x: event.clientX, y: event.clientY });
|
|
3679
|
+
};
|
|
3680
|
+
var onTouchStart = function (event) {
|
|
3681
|
+
if (event.touches.length !== 1)
|
|
3682
|
+
return;
|
|
3683
|
+
var point = getTouchPoint(event);
|
|
3684
|
+
if (point)
|
|
3685
|
+
start(point, event.target);
|
|
3686
|
+
};
|
|
3687
|
+
var onTouchMove = function (event) {
|
|
3688
|
+
if (!dragRef.current)
|
|
3689
|
+
return;
|
|
3690
|
+
if (event.touches.length !== 1) {
|
|
3691
|
+
stop(event, null);
|
|
3692
|
+
return;
|
|
3693
|
+
}
|
|
3694
|
+
var point = getTouchPoint(event);
|
|
3695
|
+
if (point)
|
|
3696
|
+
move(point);
|
|
3697
|
+
};
|
|
3698
|
+
var onTouchEnd = function (event) {
|
|
3699
|
+
stop(event, getTouchPoint(event));
|
|
3206
3700
|
};
|
|
3207
3701
|
// Listen on the mini map's own document so it keeps working inside
|
|
3208
3702
|
// portal windows and iframes.
|
|
3209
|
-
var doc =
|
|
3210
|
-
doc.addEventListener("mousedown",
|
|
3211
|
-
doc.addEventListener("
|
|
3212
|
-
doc.addEventListener("
|
|
3703
|
+
var doc = element.ownerDocument;
|
|
3704
|
+
doc.addEventListener("mousedown", onMouseDown);
|
|
3705
|
+
doc.addEventListener("mousemove", onMouseMove);
|
|
3706
|
+
doc.addEventListener("mouseup", onMouseUp);
|
|
3707
|
+
doc.addEventListener("touchstart", onTouchStart);
|
|
3708
|
+
doc.addEventListener("touchmove", onTouchMove);
|
|
3709
|
+
doc.addEventListener("touchend", onTouchEnd);
|
|
3710
|
+
doc.addEventListener("touchcancel", onTouchEnd);
|
|
3213
3711
|
return function () {
|
|
3214
|
-
doc.removeEventListener("mousedown",
|
|
3215
|
-
doc.removeEventListener("mousemove",
|
|
3216
|
-
doc.removeEventListener("mouseup",
|
|
3712
|
+
doc.removeEventListener("mousedown", onMouseDown);
|
|
3713
|
+
doc.removeEventListener("mousemove", onMouseMove);
|
|
3714
|
+
doc.removeEventListener("mouseup", onMouseUp);
|
|
3715
|
+
doc.removeEventListener("touchstart", onTouchStart);
|
|
3716
|
+
doc.removeEventListener("touchmove", onTouchMove);
|
|
3717
|
+
doc.removeEventListener("touchend", onTouchEnd);
|
|
3718
|
+
doc.removeEventListener("touchcancel", onTouchEnd);
|
|
3719
|
+
dragRef.current = null;
|
|
3720
|
+
};
|
|
3721
|
+
}, [computeLayout, instance]);
|
|
3722
|
+
React.useEffect(function () {
|
|
3723
|
+
var element = mainRef.current;
|
|
3724
|
+
if (!element || !zoomable)
|
|
3725
|
+
return;
|
|
3726
|
+
var onWheel = function (event) {
|
|
3727
|
+
var wrapperComponent = instance.wrapperComponent, contentComponent = instance.contentComponent, setup = instance.setup, state = instance.state;
|
|
3728
|
+
if (setup.disabled || !wrapperComponent || !contentComponent)
|
|
3729
|
+
return;
|
|
3730
|
+
if (!event.deltaY)
|
|
3731
|
+
return;
|
|
3732
|
+
if (event.cancelable)
|
|
3733
|
+
event.preventDefault();
|
|
3734
|
+
event.stopPropagation();
|
|
3735
|
+
var delta = event.deltaY < 0 ? 1 : -1;
|
|
3736
|
+
var newScale = handleCalculateButtonZoom(instance, delta, optionsRef.current.zoomStep);
|
|
3737
|
+
if (newScale === state.scale)
|
|
3738
|
+
return;
|
|
3739
|
+
// Zoom around the centre of the current viewport.
|
|
3740
|
+
var centerX = (wrapperComponent.offsetWidth / 2 - state.positionX) / state.scale;
|
|
3741
|
+
var centerY = (wrapperComponent.offsetHeight / 2 - state.positionY) / state.scale;
|
|
3742
|
+
handleCancelAnimation(instance);
|
|
3743
|
+
var target = handleZoomToPoint(instance, newScale, centerX, centerY);
|
|
3744
|
+
if (!target)
|
|
3745
|
+
return;
|
|
3746
|
+
instance.setState(target.scale, target.positionX, target.positionY);
|
|
3217
3747
|
};
|
|
3218
|
-
|
|
3219
|
-
|
|
3220
|
-
|
|
3221
|
-
position: "relative",
|
|
3222
|
-
zIndex: 2,
|
|
3223
|
-
// overflow: "hidden",
|
|
3224
|
-
userSelect: "none",
|
|
3748
|
+
element.addEventListener("wheel", onWheel, { passive: false });
|
|
3749
|
+
return function () {
|
|
3750
|
+
element.removeEventListener("wheel", onWheel);
|
|
3225
3751
|
};
|
|
3226
|
-
}, []);
|
|
3227
|
-
|
|
3752
|
+
}, [instance, zoomable]);
|
|
3753
|
+
var containerStyle = __assign({ position: "relative", zIndex: 2, width: "".concat(width, "px"), height: "".concat(height, "px"), overflow: "hidden", userSelect: "none", WebkitUserSelect: "none", touchAction: "none", cursor: panning ? "grab" : undefined }, rest.style);
|
|
3754
|
+
return (jsxRuntime.jsxs("div", __assign({}, rest, { ref: mainRef, style: containerStyle, className: "rzpp-mini-map ".concat(rest.className || "").trim() }, { children: [jsxRuntime.jsx("div", __assign({ style: wrapperStyles, ref: wrapperRef, className: "rzpp-minimap-wrapper ".concat(wrapperClassName || "").trim() }, { children: children })), jsxRuntime.jsx("svg", __assign({ className: "rzpp-minimap-mask", style: maskStyles, width: width, height: height, viewBox: "0 0 ".concat(width, " ").concat(height), "aria-hidden": "true" }, { children: jsxRuntime.jsx("path", { ref: maskPathRef, fill: maskColor, fillRule: "evenodd", d: "M0 0H".concat(width, "V").concat(height, "H0Z") }) })), jsxRuntime.jsx("div", { className: "rzpp-minimap-preview ".concat(previewClassName || "").trim(), ref: previewRef, style: __assign(__assign(__assign({}, previewStyles), { borderColor: borderColor }), previewStyle) })] })));
|
|
3228
3755
|
};
|
|
3229
3756
|
|
|
3230
|
-
|
|
3231
|
-
|
|
3232
|
-
var insertAt = ref.insertAt;
|
|
3233
|
-
|
|
3234
|
-
if (!css || typeof document === 'undefined') { return; }
|
|
3235
|
-
|
|
3236
|
-
var head = document.head || document.getElementsByTagName('head')[0];
|
|
3237
|
-
var style = document.createElement('style');
|
|
3238
|
-
style.type = 'text/css';
|
|
3239
|
-
|
|
3240
|
-
if (insertAt === 'top') {
|
|
3241
|
-
if (head.firstChild) {
|
|
3242
|
-
head.insertBefore(style, head.firstChild);
|
|
3243
|
-
} else {
|
|
3244
|
-
head.appendChild(style);
|
|
3245
|
-
}
|
|
3246
|
-
} else {
|
|
3247
|
-
head.appendChild(style);
|
|
3248
|
-
}
|
|
3249
|
-
|
|
3250
|
-
if (style.styleSheet) {
|
|
3251
|
-
style.styleSheet.cssText = css;
|
|
3252
|
-
} else {
|
|
3253
|
-
style.appendChild(document.createTextNode(css));
|
|
3254
|
-
}
|
|
3255
|
-
}
|
|
3256
|
-
|
|
3257
|
-
var css_248z = ".transform-component-module_wrapper__SPB86 {\n position: relative;\n width: -moz-fit-content;\n width: fit-content;\n height: -moz-fit-content;\n height: fit-content;\n overflow: hidden;\n -webkit-touch-callout: none; /* iOS Safari */\n /* Text stays selectable (#467); the instance applies an inline lock\n only while a pan gesture is active. */\n margin: 0;\n padding: 0;\n transform: translate3d(0, 0, 0);\n}\n.transform-component-module_content__FBWxo {\n display: flex;\n flex-wrap: wrap;\n width: -moz-fit-content;\n width: fit-content;\n height: -moz-fit-content;\n height: fit-content;\n margin: 0;\n padding: 0;\n transform-origin: 0% 0%;\n}\n.transform-component-module_content__FBWxo img {\n pointer-events: none;\n}\n.transform-component-module_infiniteGrid__Z-aP3 {\n position: absolute;\n inset: 0;\n pointer-events: none;\n background-image: radial-gradient(\n circle,\n rgba(0, 0, 0, 0.12) 1px,\n transparent 1px\n );\n background-size: 20px 20px;\n background-position: 0 0;\n}\n";
|
|
3258
|
-
var styles = {"wrapper":"transform-component-module_wrapper__SPB86","content":"transform-component-module_content__FBWxo","infiniteGrid":"transform-component-module_infiniteGrid__Z-aP3"};
|
|
3259
|
-
styleInject(css_248z);
|
|
3757
|
+
var transformComponent_module = {"wrapper":"transform-component-module_wrapper__SPB86","content":"transform-component-module_content__FBWxo","infiniteGrid":"transform-component-module_infiniteGrid__Z-aP3"};
|
|
3758
|
+
var stylesheet$1=".transform-component-module_wrapper__SPB86 {\n position: relative;\n width: -moz-fit-content;\n width: fit-content;\n height: -moz-fit-content;\n height: fit-content;\n overflow: hidden;\n -webkit-touch-callout: none; /* iOS Safari */\n /* Text stays selectable (#467); the instance applies an inline lock\n only while a pan gesture is active. */\n margin: 0;\n padding: 0;\n transform: translate3d(0, 0, 0);\n}\n.transform-component-module_content__FBWxo {\n display: flex;\n flex-wrap: wrap;\n width: -moz-fit-content;\n width: fit-content;\n height: -moz-fit-content;\n height: fit-content;\n margin: 0;\n padding: 0;\n transform-origin: 0% 0%;\n}\n.transform-component-module_content__FBWxo img {\n pointer-events: none;\n}\n.transform-component-module_infiniteGrid__Z-aP3 {\n position: absolute;\n inset: 0;\n pointer-events: none;\n background-image: radial-gradient(\n circle,\n rgba(0, 0, 0, 0.12) 1px,\n transparent 1px\n );\n background-size: 20px 20px;\n background-position: 0 0;\n}\n";
|
|
3260
3759
|
|
|
3760
|
+
var styles = transformComponent_module;
|
|
3761
|
+
var stylesheet = stylesheet$1;
|
|
3261
3762
|
var TransformComponent = function (_a) {
|
|
3262
3763
|
var _b;
|
|
3263
|
-
var children = _a.children, _c = _a.wrapperClass, wrapperClass = _c === void 0 ? "" : _c, _d = _a.contentClass, contentClass = _d === void 0 ? "" : _d, wrapperStyle = _a.wrapperStyle, contentStyle = _a.contentStyle, _e = _a.wrapperProps, wrapperProps = _e === void 0 ? {} : _e, _f = _a.contentProps, contentProps = _f === void 0 ? {} : _f, _g = _a.infinite, infinite = _g === void 0 ? false : _g;
|
|
3764
|
+
var children = _a.children, _c = _a.wrapperClass, wrapperClass = _c === void 0 ? "" : _c, _d = _a.contentClass, contentClass = _d === void 0 ? "" : _d, wrapperStyle = _a.wrapperStyle, contentStyle = _a.contentStyle, _e = _a.wrapperProps, wrapperProps = _e === void 0 ? {} : _e, _f = _a.contentProps, contentProps = _f === void 0 ? {} : _f, _g = _a.infinite, infinite = _g === void 0 ? false : _g, nonce = _a.nonce, _h = _a.disableStyleInjection, disableStyleInjection = _h === void 0 ? false : _h;
|
|
3264
3765
|
var instance = React.useContext(Context);
|
|
3265
3766
|
var init = instance.init, cleanupWindowEvents = instance.cleanupWindowEvents;
|
|
3266
3767
|
var wrapperRef = React.useRef(null);
|
|
@@ -3272,6 +3773,10 @@ var TransformComponent = function (_a) {
|
|
|
3272
3773
|
useIsomorphicLayoutEffect(function () {
|
|
3273
3774
|
var wrapper = wrapperRef.current;
|
|
3274
3775
|
var content = contentRef.current;
|
|
3776
|
+
// Before `init` measures anything: the sizes depend on these rules.
|
|
3777
|
+
if (wrapper !== null && !disableStyleInjection) {
|
|
3778
|
+
injectStylesheet(wrapper, stylesheet, nonce);
|
|
3779
|
+
}
|
|
3275
3780
|
if (wrapper !== null && content !== null && init) {
|
|
3276
3781
|
init === null || init === void 0 ? void 0 : init(wrapper, content);
|
|
3277
3782
|
}
|
|
@@ -3447,13 +3952,37 @@ var useTransformInit = function (callback) {
|
|
|
3447
3952
|
}, []);
|
|
3448
3953
|
};
|
|
3449
3954
|
|
|
3955
|
+
var takeSnapshot = function (state) { return ({
|
|
3956
|
+
scale: state.scale,
|
|
3957
|
+
positionX: state.positionX,
|
|
3958
|
+
positionY: state.positionY,
|
|
3959
|
+
}); };
|
|
3960
|
+
var isSameSnapshot = function (snapshot, state) {
|
|
3961
|
+
return snapshot !== null &&
|
|
3962
|
+
snapshot.scale === state.scale &&
|
|
3963
|
+
snapshot.positionX === state.positionX &&
|
|
3964
|
+
snapshot.positionY === state.positionY;
|
|
3965
|
+
};
|
|
3966
|
+
|
|
3450
3967
|
var useTransformEffect = function (callback) {
|
|
3451
3968
|
var libraryContext = useTransformContext();
|
|
3969
|
+
// Starts as the state the consumer rendered with, so a transform applied
|
|
3970
|
+
// during mount (initialPosition*, centerOnInit, fitOnInit) — which happens
|
|
3971
|
+
// in a layout effect, before this subscription exists — is delivered once.
|
|
3972
|
+
var lastSeen = React.useRef(null);
|
|
3973
|
+
if (lastSeen.current === null) {
|
|
3974
|
+
lastSeen.current = takeSnapshot(libraryContext.state);
|
|
3975
|
+
}
|
|
3452
3976
|
React.useEffect(function () {
|
|
3453
3977
|
var unmountCallback;
|
|
3454
3978
|
var unmount = libraryContext.onChange(function (ref) {
|
|
3979
|
+
lastSeen.current = takeSnapshot(ref.instance.state);
|
|
3455
3980
|
unmountCallback = callback(getState(ref.instance));
|
|
3456
3981
|
});
|
|
3982
|
+
if (!isSameSnapshot(lastSeen.current, libraryContext.state)) {
|
|
3983
|
+
lastSeen.current = takeSnapshot(libraryContext.state);
|
|
3984
|
+
unmountCallback = callback(getState(libraryContext));
|
|
3985
|
+
}
|
|
3457
3986
|
return function () {
|
|
3458
3987
|
unmount();
|
|
3459
3988
|
unmountCallback === null || unmountCallback === void 0 ? void 0 : unmountCallback();
|
|
@@ -3463,16 +3992,27 @@ var useTransformEffect = function (callback) {
|
|
|
3463
3992
|
|
|
3464
3993
|
function useTransformComponent(callback) {
|
|
3465
3994
|
var libraryContext = useTransformContext();
|
|
3995
|
+
var lastSeen = React.useRef(null);
|
|
3466
3996
|
var _a = React.useState(function () {
|
|
3997
|
+
lastSeen.current = takeSnapshot(libraryContext.state);
|
|
3467
3998
|
return callback(getState(libraryContext));
|
|
3468
3999
|
}), transformRender = _a[0], setTransformRender = _a[1];
|
|
3469
4000
|
React.useEffect(function () {
|
|
3470
4001
|
var mounted = true;
|
|
3471
4002
|
var unmount = libraryContext.onChange(function (ref) {
|
|
3472
4003
|
if (mounted) {
|
|
4004
|
+
lastSeen.current = takeSnapshot(ref.instance.state);
|
|
3473
4005
|
setTransformRender(callback(getState(ref.instance)));
|
|
3474
4006
|
}
|
|
3475
4007
|
});
|
|
4008
|
+
// The instance initializes in a layout effect, so a transform applied on
|
|
4009
|
+
// mount (initialPosition*, centerOnInit, fitOnInit) can land before this
|
|
4010
|
+
// subscription exists. Catch up when the state moved since the last
|
|
4011
|
+
// render; the snapshot check keeps this from re-rendering in a loop.
|
|
4012
|
+
if (!isSameSnapshot(lastSeen.current, libraryContext.state)) {
|
|
4013
|
+
lastSeen.current = takeSnapshot(libraryContext.state);
|
|
4014
|
+
setTransformRender(callback(getState(libraryContext)));
|
|
4015
|
+
}
|
|
3476
4016
|
return function () {
|
|
3477
4017
|
unmount();
|
|
3478
4018
|
mounted = false;
|
|
@@ -3526,6 +4066,7 @@ exports.Virtualize = Virtualize;
|
|
|
3526
4066
|
exports.getCenterPosition = getCenterPosition;
|
|
3527
4067
|
exports.getMatrixTransformStyles = getMatrixTransformStyles;
|
|
3528
4068
|
exports.getTransformStyles = getTransformStyles;
|
|
4069
|
+
exports.injectStylesheet = injectStylesheet;
|
|
3529
4070
|
exports.roundScaleForTransform = roundScaleForTransform;
|
|
3530
4071
|
exports.useControls = useControls;
|
|
3531
4072
|
exports.useTransformComponent = useTransformComponent;
|