react-zoom-pan-pinch 4.2.0 → 4.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.esm.js CHANGED
@@ -1,4 +1,4 @@
1
- import React, { useLayoutEffect, useEffect, useRef, useImperativeHandle, useContext, useState, useCallback, useMemo } from 'react';
1
+ import React, { useLayoutEffect, useEffect, useRef, useImperativeHandle, useContext, useState, useCallback } from 'react';
2
2
  import { jsx, jsxs, Fragment } from 'react/jsx-runtime';
3
3
 
4
4
  /**
@@ -280,6 +280,61 @@ var calculateBounds = function (contextInstance, newScale) {
280
280
  }
281
281
  return bounds;
282
282
  };
283
+ function clamp(v, min, max) {
284
+ return Math.max(min, Math.min(v, max));
285
+ }
286
+ // Based on @aholachek ;)
287
+ // https://twitter.com/chpwn/status/285540192096497664
288
+ // iOS constant = 0.55
289
+ // https://medium.com/@nathangitter/building-fluid-interfaces-ios-swift-9732bb934bf5
290
+ function rubberband(distance, dimension, constant) {
291
+ if (dimension === 0 || Math.abs(dimension) === Infinity)
292
+ return Math.pow(distance, (constant * 5));
293
+ return (distance * dimension * constant) / (dimension + constant * distance);
294
+ }
295
+ /**
296
+ * Furthest a grabbed position is traced back along the rubber band curve, as
297
+ * a fraction of the padding. The curve never reaches the padding itself, so
298
+ * a position at (or past) it has no pointer distance to map back to.
299
+ */
300
+ var MAX_RUBBER_BAND_RATIO = 0.9;
301
+ /**
302
+ * Resistance of the rubber band, the constant UIScrollView uses: past a bound
303
+ * the content moves at 0.55× the pointer and slows down from there.
304
+ */
305
+ var RUBBER_BAND_CONSTANT = 0.55;
306
+ /**
307
+ * Where a pan that was dragged past a bound ends up: `(1 - 1 / (x * c / d + 1)) * d`
308
+ * with `d` = `padding`. It trails the pointer from the first pixel past the
309
+ * bound and approaches — never reaches — `padding` px past it.
310
+ */
311
+ function getRubberBandPosition(position, min, max, padding) {
312
+ if (padding <= 0)
313
+ return clamp(position, min, max);
314
+ if (position < min)
315
+ return min - rubberband(min - position, padding, RUBBER_BAND_CONSTANT);
316
+ if (position > max)
317
+ return max + rubberband(position - max, padding, RUBBER_BAND_CONSTANT);
318
+ return position;
319
+ }
320
+ /**
321
+ * Inverse of `getRubberBandPosition`: the pointer position that puts the
322
+ * content at `position`. Lets a pan that starts inside the padding (grabbed
323
+ * mid return animation, trackpad deltas) continue from where the content is.
324
+ */
325
+ function getRubberBandSource(position, min, max, padding) {
326
+ if (padding <= 0)
327
+ return position;
328
+ var stretch = function (offset) {
329
+ var limited = Math.min(offset, padding * MAX_RUBBER_BAND_RATIO);
330
+ return (limited * padding) / (RUBBER_BAND_CONSTANT * (padding - limited));
331
+ };
332
+ if (position < min)
333
+ return min - stretch(min - position);
334
+ if (position > max)
335
+ return max + stretch(position - max);
336
+ return position;
337
+ }
283
338
  /**
284
339
  * Keeps value between given bounds, used for limiting view to given boundaries
285
340
  * 1# eg. boundLimiter(2, 0, 3, true) => 2
@@ -314,6 +369,23 @@ function getMouseBoundedPosition(positionX, positionY, bounds, limitToBounds, pa
314
369
  var y = boundLimiter(positionY, minPositionY - paddingY, maxPositionY + paddingY, limitToBounds);
315
370
  return { x: x, y: y };
316
371
  }
372
+ /**
373
+ * Bounded position for a pan gesture: free inside the bounds and, inside the
374
+ * padding around them, rubber banded (`rubberBand`) or 1:1 up to its edge.
375
+ */
376
+ function getPanBoundedPosition(positionX, positionY, bounds, limitToBounds, paddingX, paddingY, rubberBand) {
377
+ var minPositionX = bounds.minPositionX, minPositionY = bounds.minPositionY, maxPositionX = bounds.maxPositionX, maxPositionY = bounds.maxPositionY;
378
+ var limit = function (position, min, max, padding) {
379
+ if (limitToBounds && rubberBand && padding > 0) {
380
+ return roundNumber(getRubberBandPosition(position, min, max, padding), 2);
381
+ }
382
+ return boundLimiter(position, min - padding, max + padding, limitToBounds);
383
+ };
384
+ return {
385
+ x: limit(positionX, minPositionX, maxPositionX, paddingX),
386
+ y: limit(positionY, minPositionY, maxPositionY, paddingY),
387
+ };
388
+ }
317
389
 
318
390
  function handleCalculateZoomPositions(contextInstance, mouseX, mouseY, newScale, bounds, limitToBounds) {
319
391
  var _a = contextInstance.state, scale = _a.scale, positionX = _a.positionX, positionY = _a.positionY;
@@ -379,8 +451,45 @@ var isPanningAllowed = function (contextInstance) {
379
451
  return false;
380
452
  return true;
381
453
  };
454
+ var getPaddingValue = function (contextInstance, size, explicitScale) {
455
+ var setup = contextInstance.setup, state = contextInstance.state;
456
+ var minScale = setup.minScale, disablePadding = setup.disablePadding, centerZoomedOut = setup.centerZoomedOut;
457
+ var scale = explicitScale !== null && explicitScale !== void 0 ? explicitScale : state.scale;
458
+ if (size > 0 && scale >= minScale && !disablePadding && !centerZoomedOut) {
459
+ return size;
460
+ }
461
+ return 0;
462
+ };
463
+ /**
464
+ * Dragging past the bounds stretches like a rubber band only when the content
465
+ * is going to return to them on release (`autoAlignment`). Without that return
466
+ * the padding is a plain area the content can be left in, so it follows the
467
+ * pointer 1:1 up to its edge.
468
+ */
469
+ function isRubberBandEnabled(contextInstance) {
470
+ var _a = contextInstance.setup, limitToBounds = _a.limitToBounds, autoAlignment = _a.autoAlignment;
471
+ return limitToBounds && !autoAlignment.disabled;
472
+ }
473
+ /**
474
+ * The position a pan continues from. Inside the bounds that is the content
475
+ * position; inside the padding the content trails the pointer (rubber band),
476
+ * so the pointer's own position is traced back from it — otherwise the first
477
+ * move of a pan started there would make the content jump.
478
+ */
479
+ function getPanSourcePosition(contextInstance) {
480
+ var bounds = contextInstance.bounds, setup = contextInstance.setup, state = contextInstance.state;
481
+ var positionX = state.positionX, positionY = state.positionY;
482
+ if (!bounds || !isRubberBandEnabled(contextInstance)) {
483
+ return { x: positionX, y: positionY };
484
+ }
485
+ var _a = setup.autoAlignment, sizeX = _a.sizeX, sizeY = _a.sizeY;
486
+ return {
487
+ x: getRubberBandSource(positionX, bounds.minPositionX, bounds.maxPositionX, getPaddingValue(contextInstance, sizeX)),
488
+ y: getRubberBandSource(positionY, bounds.minPositionY, bounds.maxPositionY, getPaddingValue(contextInstance, sizeY)),
489
+ };
490
+ }
382
491
  var handlePanningSetup = function (contextInstance, event) {
383
- var _a = contextInstance.state, positionX = _a.positionX, positionY = _a.positionY;
492
+ var _a = getPanSourcePosition(contextInstance), positionX = _a.x, positionY = _a.y;
384
493
  contextInstance.isPanning = true;
385
494
  // Panning with mouse
386
495
  var x = event.clientX;
@@ -389,7 +498,7 @@ var handlePanningSetup = function (contextInstance, event) {
389
498
  };
390
499
  var handleTouchPanningSetup = function (contextInstance, event) {
391
500
  var touches = event.touches;
392
- var _a = contextInstance.state, positionX = _a.positionX, positionY = _a.positionY;
501
+ var _a = getPanSourcePosition(contextInstance), positionX = _a.x, positionY = _a.y;
393
502
  contextInstance.isPanning = true;
394
503
  // Panning with touch
395
504
  var oneFingerTouch = touches.length === 1;
@@ -430,14 +539,16 @@ function handleNewPosition(contextInstance, newPositionX, newPositionY, paddingV
430
539
  (newPositionX === positionX && newPositionY === positionY)) {
431
540
  return;
432
541
  }
433
- var _b = getMouseBoundedPosition(newPositionX, newPositionY, bounds, limitToBounds, paddingValueX, paddingValueY, wrapperComponent), x = _b.x, y = _b.y;
542
+ var _b = getPanBoundedPosition(newPositionX, newPositionY, bounds, limitToBounds, paddingValueX, paddingValueY, isRubberBandEnabled(contextInstance)), x = _b.x, y = _b.y;
434
543
  contextInstance.setState(scale, x, y);
435
544
  }
436
545
  var getPanningClientPosition = function (contextInstance, clientX, clientY) {
437
- var startCoords = contextInstance.startCoords, state = contextInstance.state;
546
+ var startCoords = contextInstance.startCoords;
438
547
  var panning = contextInstance.setup.panning;
439
548
  var lockAxisX = panning.lockAxisX, lockAxisY = panning.lockAxisY;
440
- var positionX = state.positionX, positionY = state.positionY;
549
+ // A locked axis keeps its place: the source maps back onto the position
550
+ // it has now, also when it rests inside the padding.
551
+ var _a = getPanSourcePosition(contextInstance), positionX = _a.x, positionY = _a.y;
441
552
  if (!startCoords) {
442
553
  return { x: positionX, y: positionY };
443
554
  }
@@ -447,15 +558,6 @@ var getPanningClientPosition = function (contextInstance, clientX, clientY) {
447
558
  var newPositionY = lockAxisY ? positionY : mouseY;
448
559
  return { x: newPositionX, y: newPositionY };
449
560
  };
450
- var getPaddingValue = function (contextInstance, size, explicitScale) {
451
- var setup = contextInstance.setup, state = contextInstance.state;
452
- var minScale = setup.minScale, disablePadding = setup.disablePadding, centerZoomedOut = setup.centerZoomedOut;
453
- var scale = explicitScale !== null && explicitScale !== void 0 ? explicitScale : state.scale;
454
- if (size > 0 && scale >= minScale && !disablePadding && !centerZoomedOut) {
455
- return size;
456
- }
457
- return 0;
458
- };
459
561
 
460
562
  var DeviceType;
461
563
  (function (DeviceType) {
@@ -589,8 +691,15 @@ function handleVelocityPanning(contextInstance) {
589
691
  var finalAnimationTime = Math.max(moveAnimationTime, alignAnimationTime);
590
692
  var paddingValueX = getPaddingValue(contextInstance, sizeX);
591
693
  var paddingValueY = getPaddingValue(contextInstance, sizeY);
592
- var paddingX = (paddingValueX * wrapperComponent.clientWidth) / 100;
593
- var paddingY = (paddingValueY * wrapperComponent.clientHeight) / 100;
694
+ // A fling released inside the rubber band stays within the same stretch a
695
+ // drag could reach (`padding` px), not a share of the wrapper size.
696
+ var isRubberBand = isRubberBandEnabled(contextInstance);
697
+ var paddingX = isRubberBand
698
+ ? paddingValueX
699
+ : (paddingValueX * wrapperComponent.clientWidth) / 100;
700
+ var paddingY = isRubberBand
701
+ ? paddingValueY
702
+ : (paddingValueY * wrapperComponent.clientHeight) / 100;
594
703
  var maxTargetX = maxPositionX + paddingX;
595
704
  var minTargetX = minPositionX - paddingX;
596
705
  var maxTargetY = maxPositionY + paddingY;
@@ -677,8 +786,13 @@ function handlePanningEnd(contextInstance, velocityDisabled) {
677
786
  if (start) {
678
787
  var dx = positionX - start.x;
679
788
  var dy = positionY - start.y;
680
- if (dx * dx + dy * dy <= 25)
789
+ // A click is not a fling, but it can still land on content left past
790
+ // the bounds — e.g. grabbed mid return animation, which the press
791
+ // cancelled. Finish the return (a no-op inside the bounds).
792
+ if (dx * dx + dy * dy <= 25) {
793
+ handleAlignToBounds(contextInstance);
681
794
  return;
795
+ }
682
796
  }
683
797
  contextInstance.isAnimating = false;
684
798
  contextInstance.animation = null;
@@ -1517,16 +1631,6 @@ var getCenterPosition = function (scale, wrapperComponent, contentComponent) {
1517
1631
  };
1518
1632
  };
1519
1633
 
1520
- function assignRef(ref, value) {
1521
- if (ref == null)
1522
- return;
1523
- if (typeof ref === "function") {
1524
- ref(value);
1525
- }
1526
- else {
1527
- ref.current = value;
1528
- }
1529
- }
1530
1634
  function mergeRefs(refs) {
1531
1635
  return function (value) {
1532
1636
  refs.forEach(function (ref) {
@@ -2513,17 +2617,19 @@ var ZoomPanPinch = /** @class */ (function () {
2513
2617
  event.preventDefault();
2514
2618
  }
2515
2619
  event.stopPropagation();
2516
- var _a = _this.state, positionX = _a.positionX, positionY = _a.positionY;
2517
- var mouseX = positionX - event.deltaX;
2518
- var mouseY = positionY - event.deltaY;
2519
- var newPositionX = lockAxisX ? positionX : mouseX;
2520
- var newPositionY = lockAxisY ? positionY : mouseY;
2521
- var _b = _this.setup.autoAlignment, sizeX = _b.sizeX, sizeY = _b.sizeY;
2620
+ var deltaX = lockAxisX ? 0 : event.deltaX;
2621
+ var deltaY = lockAxisY ? 0 : event.deltaY;
2622
+ if (!deltaX && !deltaY)
2623
+ return;
2624
+ var _a = _this.setup.autoAlignment, sizeX = _a.sizeX, sizeY = _a.sizeY;
2522
2625
  var paddingValueX = getPaddingValue(_this, sizeX);
2523
2626
  var paddingValueY = getPaddingValue(_this, sizeY);
2524
- if (newPositionX === positionX && newPositionY === positionY)
2525
- return;
2526
2627
  handleWheelPanningStart(_this, event);
2628
+ // Deltas add up on the pointer's side of the rubber band, the same way
2629
+ // a drag does, so the content slows down towards the padding limit.
2630
+ var source = getPanSourcePosition(_this);
2631
+ var newPositionX = source.x - deltaX;
2632
+ var newPositionY = source.y - deltaY;
2527
2633
  handleNewPosition(_this, newPositionX, newPositionY, paddingValueX, paddingValueY);
2528
2634
  handleCallback(getContext(_this), event, onPanning);
2529
2635
  handleWheelPanningStop(_this, event);
@@ -2949,7 +3055,6 @@ var ZoomPanPinch = /** @class */ (function () {
2949
3055
  _this.isInitialized = true;
2950
3056
  var ctx = getContext(_this);
2951
3057
  handleCallback(ctx, undefined, _this.props.onInit);
2952
- assignRef(_this.props.ref, ctx);
2953
3058
  };
2954
3059
  this.props = props;
2955
3060
  this.setup = createSetup(this.props);
@@ -3055,174 +3160,307 @@ var useResize = function (ref, onResize, dependencies) {
3055
3160
 
3056
3161
  var previewStyles = {
3057
3162
  position: "absolute",
3058
- zIndex: 2,
3163
+ zIndex: 3,
3059
3164
  top: "0px",
3060
3165
  left: "0px",
3061
3166
  boxSizing: "border-box",
3062
- border: "3px solid red",
3167
+ border: "2px solid red",
3168
+ transformOrigin: "0% 0%",
3169
+ pointerEvents: "none",
3170
+ };
3171
+ var wrapperStyles = {
3172
+ position: "absolute",
3173
+ top: "0px",
3174
+ left: "0px",
3063
3175
  transformOrigin: "0% 0%",
3064
- boxShadow: "rgba(0,0,0,0.2) 0 0 0 10000000px",
3176
+ boxSizing: "border-box",
3177
+ zIndex: 1,
3178
+ pointerEvents: "none",
3179
+ };
3180
+ var maskStyles = {
3181
+ position: "absolute",
3182
+ top: "0px",
3183
+ left: "0px",
3184
+ zIndex: 2,
3065
3185
  pointerEvents: "none",
3186
+ display: "block",
3187
+ };
3188
+ var getTouchPoint = function (event) {
3189
+ var _a;
3190
+ var touch = (_a = event.touches[0]) !== null && _a !== void 0 ? _a : event.changedTouches[0];
3191
+ if (!touch)
3192
+ return null;
3193
+ return { x: touch.clientX, y: touch.clientY };
3194
+ };
3195
+ var isInside = function (element, target) {
3196
+ return !!element &&
3197
+ !!target &&
3198
+ (target === element || element.contains(target));
3066
3199
  };
3067
3200
  var MiniMap = function (_a) {
3068
- 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, _e = _a.panning, panning = _e === void 0 ? true : _e, rest = __rest(_a, ["width", "height", "borderColor", "previewStyle", "wrapperClassName", "previewClassName", "children", "panning"]);
3069
- var _f = useState(false), initialized = _f[0], setInitialized = _f[1];
3201
+ 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"]);
3202
+ var _l = useState(false), initialized = _l[0], setInitialized = _l[1];
3070
3203
  var instance = useTransformContext();
3071
- var _g = useState(false), isDown = _g[0], setIsDown = _g[1];
3072
3204
  var mainRef = useRef(null);
3073
3205
  var wrapperRef = useRef(null);
3074
3206
  var previewRef = useRef(null);
3207
+ var maskPathRef = useRef(null);
3208
+ var dragRef = useRef(null);
3209
+ var layoutRef = useRef(null);
3210
+ // Latest props for the document listeners, which are registered once.
3211
+ var optionsRef = useRef({ panning: panning, inversePan: inversePan, zoomStep: zoomStep, onClick: onClick });
3212
+ optionsRef.current = { panning: panning, inversePan: inversePan, zoomStep: zoomStep, onClick: onClick };
3075
3213
  var getViewportSize = useCallback(function () {
3076
3214
  if (instance.wrapperComponent) {
3077
3215
  var rect = instance.wrapperComponent.getBoundingClientRect();
3078
- return {
3079
- width: rect.width,
3080
- height: rect.height,
3081
- };
3216
+ return { width: rect.width, height: rect.height };
3082
3217
  }
3083
- return {
3084
- width: 0,
3085
- height: 0,
3086
- };
3087
- }, [instance.wrapperComponent]);
3218
+ return { width: 0, height: 0 };
3219
+ }, [instance]);
3088
3220
  var getContentSize = useCallback(function () {
3089
3221
  if (instance.contentComponent) {
3090
3222
  var rect = instance.contentComponent.getBoundingClientRect();
3091
- return {
3092
- width: rect.width / instance.state.scale,
3093
- height: rect.height / instance.state.scale,
3094
- };
3223
+ var scale = instance.state.scale;
3224
+ return { width: rect.width / scale, height: rect.height / scale };
3095
3225
  }
3096
- return {
3097
- width: 0,
3098
- height: 0,
3099
- };
3100
- }, [instance.contentComponent, instance.state.scale]);
3101
- var computeMiniMapScale = useCallback(function () {
3102
- var contentSize = getContentSize();
3103
- var scaleX = width / contentSize.width;
3104
- var scaleY = height / contentSize.height;
3105
- var scale = scaleY > scaleX ? scaleX : scaleY;
3106
- return scale;
3107
- }, [getContentSize, height, width]);
3108
- var computeMiniMapSize = function () {
3109
- var contentSize = getContentSize();
3110
- var scaleX = width / contentSize.width;
3111
- var scaleY = height / contentSize.height;
3112
- if (scaleY > scaleX) {
3113
- return { width: width, height: contentSize.height * scaleX };
3114
- }
3115
- return { width: contentSize.width * scaleY, height: height };
3116
- };
3117
- var computeMiniMapStyle = function () {
3118
- var scale = computeMiniMapScale();
3119
- var style = {
3120
- transform: "scale(".concat(scale || 1, ")"),
3121
- transformOrigin: "0% 0%",
3122
- position: "absolute",
3123
- boxSizing: "border-box",
3124
- zIndex: 1,
3125
- // overflow: "hidden",
3126
- };
3127
- if (wrapperRef.current) {
3128
- var el = wrapperRef.current.style;
3129
- el.transform = style.transform;
3130
- el.transformOrigin = style.transformOrigin;
3131
- el.position = style.position;
3132
- el.boxSizing = style.boxSizing;
3133
- el.zIndex = String(style.zIndex);
3226
+ return { width: 0, height: 0 };
3227
+ }, [instance]);
3228
+ /**
3229
+ * Like React Flow, the map shows the union of the content and the current
3230
+ * viewport (plus `offsetScale` padding) fitted into the box, so the
3231
+ * viewport indicator never leaves the mini map even when the view is
3232
+ * panned or zoomed past the content.
3233
+ */
3234
+ var computeLayout = useCallback(function () {
3235
+ var content = getContentSize();
3236
+ var viewport = getViewportSize();
3237
+ var _a = instance.state, scale = _a.scale, positionX = _a.positionX, positionY = _a.positionY;
3238
+ if (!content.width || !content.height || !width || !height || !scale) {
3239
+ return null;
3134
3240
  }
3135
- };
3136
- var transformMiniMap = function () {
3137
- computeMiniMapStyle();
3138
- var miniSize = computeMiniMapSize();
3139
- var wrapSize = getContentSize();
3241
+ var viewX = -positionX / scale;
3242
+ var viewY = -positionY / scale;
3243
+ var viewWidth = viewport.width / scale;
3244
+ var viewHeight = viewport.height / scale;
3245
+ var minX = Math.min(0, viewX);
3246
+ var minY = Math.min(0, viewY);
3247
+ var maxX = Math.max(content.width, viewX + viewWidth);
3248
+ var maxY = Math.max(content.height, viewY + viewHeight);
3249
+ var boundsWidth = maxX - minX;
3250
+ var boundsHeight = maxY - minY;
3251
+ var viewScale = Math.max(boundsWidth / width, boundsHeight / height);
3252
+ var fitWidth = viewScale * width;
3253
+ var fitHeight = viewScale * height;
3254
+ var offset = offsetScale * viewScale;
3255
+ var originX = minX - (fitWidth - boundsWidth) / 2 - offset;
3256
+ var originY = minY - (fitHeight - boundsHeight) / 2 - offset;
3257
+ var mapScale = width / (fitWidth + offset * 2);
3258
+ return {
3259
+ mapScale: mapScale,
3260
+ originX: originX,
3261
+ originY: originY,
3262
+ contentWidth: content.width,
3263
+ contentHeight: content.height,
3264
+ previewX: (viewX - originX) * mapScale,
3265
+ previewY: (viewY - originY) * mapScale,
3266
+ previewWidth: viewWidth * mapScale,
3267
+ previewHeight: viewHeight * mapScale,
3268
+ };
3269
+ }, [getContentSize, getViewportSize, height, instance, offsetScale, width]);
3270
+ var transformMiniMap = useCallback(function () {
3271
+ var layout = computeLayout();
3272
+ layoutRef.current = layout;
3273
+ if (!layout)
3274
+ return;
3275
+ var px = function (value) { return "".concat(roundNumber(value, 2), "px"); };
3140
3276
  if (wrapperRef.current) {
3141
- wrapperRef.current.style.width = "".concat(wrapSize.width, "px");
3142
- wrapperRef.current.style.height = "".concat(wrapSize.height, "px");
3143
- }
3144
- if (mainRef.current) {
3145
- mainRef.current.style.width = "".concat(miniSize.width, "px");
3146
- mainRef.current.style.height = "".concat(miniSize.height, "px");
3277
+ var style = wrapperRef.current.style;
3278
+ style.width = px(layout.contentWidth);
3279
+ style.height = px(layout.contentHeight);
3280
+ style.transform = "translate(".concat(px(-layout.originX * layout.mapScale), ", ").concat(px(-layout.originY * layout.mapScale), ") scale(").concat(roundNumber(layout.mapScale, 4), ")");
3147
3281
  }
3148
3282
  if (previewRef.current) {
3149
- var size = getViewportSize();
3150
- var scale = computeMiniMapScale();
3151
- var previewScale = scale * (1 / instance.state.scale);
3152
- var transform = instance.handleTransformStyles(-instance.state.positionX * previewScale, -instance.state.positionY * previewScale, 1);
3153
- previewRef.current.style.transform = transform;
3154
- previewRef.current.style.width = "".concat(size.width * previewScale, "px");
3155
- previewRef.current.style.height = "".concat(size.height * previewScale, "px");
3283
+ var style = previewRef.current.style;
3284
+ style.transform = "translate(".concat(px(layout.previewX), ", ").concat(px(layout.previewY), ")");
3285
+ style.width = px(layout.previewWidth);
3286
+ style.height = px(layout.previewHeight);
3156
3287
  }
3157
- };
3158
- var initialize = function () {
3159
- transformMiniMap();
3160
- };
3161
- useTransformEffect(function () {
3162
- transformMiniMap();
3163
- });
3288
+ if (maskPathRef.current) {
3289
+ var x = roundNumber(layout.previewX, 2);
3290
+ var y = roundNumber(layout.previewY, 2);
3291
+ var w = roundNumber(layout.previewWidth, 2);
3292
+ var h = roundNumber(layout.previewHeight, 2);
3293
+ 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"));
3294
+ }
3295
+ }, [computeLayout, height, width]);
3296
+ useTransformEffect(transformMiniMap);
3164
3297
  useTransformInit(function () {
3165
- initialize();
3298
+ transformMiniMap();
3166
3299
  setInitialized(true);
3167
3300
  });
3168
- useResize(instance.contentComponent, initialize, [initialized]);
3301
+ useResize(instance.contentComponent, transformMiniMap, [initialized]);
3302
+ useEffect(function () {
3303
+ transformMiniMap();
3304
+ }, [transformMiniMap]);
3169
3305
  useEffect(function () {
3170
- var _a, _b;
3171
- var move = function (e) {
3306
+ var element = mainRef.current;
3307
+ if (!element)
3308
+ return;
3309
+ var getMapScale = function () {
3172
3310
  var _a, _b;
3173
- if (panning &&
3174
- isDown &&
3175
- instance.contentComponent &&
3176
- previewRef.current &&
3177
- mainRef.current) {
3178
- var scale = computeMiniMapScale();
3179
- var previewRect = previewRef.current.getBoundingClientRect();
3180
- var mainRect = mainRef.current.getBoundingClientRect();
3181
- var relativeX = (e.clientX - mainRect.left) / scale;
3182
- var relativeY = (e.clientY - mainRect.top) / scale;
3183
- var x = relativeX - previewRect.width / 2;
3184
- var y = relativeY - previewRect.height / 2;
3185
- var instanceWidth = (((_a = instance.wrapperComponent) === null || _a === void 0 ? void 0 : _a.offsetWidth) || 0) * instance.state.scale;
3186
- var instanceHeight = (((_b = instance.wrapperComponent) === null || _b === void 0 ? void 0 : _b.offsetHeight) || 0) * instance.state.scale;
3187
- var limitWidth = instanceWidth - previewRect.width * 2 * instance.state.scale;
3188
- var limitHeight = instanceHeight - previewRect.height * 2 * instance.state.scale;
3189
- var boundedX = boundLimiter(x * instance.state.scale, 0, limitWidth, true);
3190
- var boundedY = boundLimiter(y * instance.state.scale, 0, limitHeight, true);
3191
- instance.setState(instance.state.scale, -boundedX, -boundedY);
3192
- }
3311
+ var layout = (_a = layoutRef.current) !== null && _a !== void 0 ? _a : computeLayout();
3312
+ return (_b = layout === null || layout === void 0 ? void 0 : layout.mapScale) !== null && _b !== void 0 ? _b : 0;
3193
3313
  };
3194
- var setDown = function (e) {
3314
+ var start = function (point, target) {
3315
+ if (!optionsRef.current.panning)
3316
+ return;
3317
+ if (!isInside(element, target))
3318
+ return;
3319
+ if (!instance.wrapperComponent || !instance.contentComponent)
3320
+ return;
3321
+ handleCancelAnimation(instance);
3322
+ dragRef.current = { x: point.x, y: point.y, moved: false };
3323
+ element.style.cursor = "grabbing";
3324
+ };
3325
+ var move = function (point) {
3326
+ var drag = dragRef.current;
3327
+ if (!drag)
3328
+ return;
3329
+ var deltaX = point.x - drag.x;
3330
+ var deltaY = point.y - drag.y;
3331
+ drag.x = point.x;
3332
+ drag.y = point.y;
3333
+ if (!deltaX && !deltaY)
3334
+ return;
3335
+ drag.moved = true;
3336
+ var mapScale = getMapScale();
3337
+ var wrapperComponent = instance.wrapperComponent, contentComponent = instance.contentComponent, setup = instance.setup, state = instance.state;
3338
+ if (!mapScale || !wrapperComponent || !contentComponent)
3339
+ return;
3340
+ // Dragging by one mini map px moves the viewport by 1 / mapScale
3341
+ // content units, so the indicator follows the pointer 1:1.
3342
+ var direction = optionsRef.current.inversePan ? 1 : -1;
3343
+ var moveScale = (direction * state.scale) / mapScale;
3344
+ var nextX = state.positionX + deltaX * moveScale;
3345
+ var nextY = state.positionY + deltaY * moveScale;
3346
+ var bounds = handleCalculateBounds(instance, state.scale);
3347
+ var _a = getMouseBoundedPosition(nextX, nextY, bounds, setup.limitToBounds, 0, 0, wrapperComponent), x = _a.x, y = _a.y;
3348
+ if (x === state.positionX && y === state.positionY)
3349
+ return;
3350
+ instance.setState(state.scale, x, y);
3351
+ };
3352
+ var stop = function (event, point) {
3195
3353
  var _a;
3196
- if (((_a = mainRef.current) === null || _a === void 0 ? void 0 : _a.contains(e.target)) ||
3197
- e.target === mainRef.current) {
3198
- move(e);
3199
- setIsDown(true);
3354
+ var drag = dragRef.current;
3355
+ if (!drag)
3356
+ return;
3357
+ dragRef.current = null;
3358
+ element.style.cursor = optionsRef.current.panning ? "grab" : "";
3359
+ var handleClick = optionsRef.current.onClick;
3360
+ if (drag.moved || !handleClick || !point)
3361
+ return;
3362
+ var layout = (_a = layoutRef.current) !== null && _a !== void 0 ? _a : computeLayout();
3363
+ if (!layout)
3364
+ return;
3365
+ var rect = element.getBoundingClientRect();
3366
+ handleClick(event, {
3367
+ x: layout.originX + (point.x - rect.left) / layout.mapScale,
3368
+ y: layout.originY + (point.y - rect.top) / layout.mapScale,
3369
+ });
3370
+ };
3371
+ var onMouseDown = function (event) {
3372
+ if (event.button !== 0)
3373
+ return;
3374
+ start({ x: event.clientX, y: event.clientY }, event.target);
3375
+ };
3376
+ var onMouseMove = function (event) {
3377
+ if (!dragRef.current)
3378
+ return;
3379
+ // The mouse was released outside the window: end the drag.
3380
+ if (event.buttons === 0) {
3381
+ stop(event, null);
3382
+ return;
3383
+ }
3384
+ move({ x: event.clientX, y: event.clientY });
3385
+ };
3386
+ var onMouseUp = function (event) {
3387
+ stop(event, { x: event.clientX, y: event.clientY });
3388
+ };
3389
+ var onTouchStart = function (event) {
3390
+ if (event.touches.length !== 1)
3391
+ return;
3392
+ var point = getTouchPoint(event);
3393
+ if (point)
3394
+ start(point, event.target);
3395
+ };
3396
+ var onTouchMove = function (event) {
3397
+ if (!dragRef.current)
3398
+ return;
3399
+ if (event.touches.length !== 1) {
3400
+ stop(event, null);
3401
+ return;
3200
3402
  }
3403
+ var point = getTouchPoint(event);
3404
+ if (point)
3405
+ move(point);
3201
3406
  };
3202
- var setUp = function () {
3203
- setIsDown(false);
3407
+ var onTouchEnd = function (event) {
3408
+ stop(event, getTouchPoint(event));
3204
3409
  };
3205
3410
  // Listen on the mini map's own document so it keeps working inside
3206
3411
  // portal windows and iframes.
3207
- var doc = (_b = (_a = mainRef.current) === null || _a === void 0 ? void 0 : _a.ownerDocument) !== null && _b !== void 0 ? _b : document;
3208
- doc.addEventListener("mousedown", setDown);
3209
- doc.addEventListener("mouseup", setUp);
3210
- doc.addEventListener("mousemove", move);
3412
+ var doc = element.ownerDocument;
3413
+ doc.addEventListener("mousedown", onMouseDown);
3414
+ doc.addEventListener("mousemove", onMouseMove);
3415
+ doc.addEventListener("mouseup", onMouseUp);
3416
+ doc.addEventListener("touchstart", onTouchStart);
3417
+ doc.addEventListener("touchmove", onTouchMove);
3418
+ doc.addEventListener("touchend", onTouchEnd);
3419
+ doc.addEventListener("touchcancel", onTouchEnd);
3211
3420
  return function () {
3212
- doc.removeEventListener("mousedown", setDown);
3213
- doc.removeEventListener("mousemove", move);
3214
- doc.removeEventListener("mouseup", setUp);
3421
+ doc.removeEventListener("mousedown", onMouseDown);
3422
+ doc.removeEventListener("mousemove", onMouseMove);
3423
+ doc.removeEventListener("mouseup", onMouseUp);
3424
+ doc.removeEventListener("touchstart", onTouchStart);
3425
+ doc.removeEventListener("touchmove", onTouchMove);
3426
+ doc.removeEventListener("touchend", onTouchEnd);
3427
+ doc.removeEventListener("touchcancel", onTouchEnd);
3428
+ dragRef.current = null;
3429
+ };
3430
+ }, [computeLayout, instance]);
3431
+ useEffect(function () {
3432
+ var element = mainRef.current;
3433
+ if (!element || !zoomable)
3434
+ return;
3435
+ var onWheel = function (event) {
3436
+ var wrapperComponent = instance.wrapperComponent, contentComponent = instance.contentComponent, setup = instance.setup, state = instance.state;
3437
+ if (setup.disabled || !wrapperComponent || !contentComponent)
3438
+ return;
3439
+ if (!event.deltaY)
3440
+ return;
3441
+ if (event.cancelable)
3442
+ event.preventDefault();
3443
+ event.stopPropagation();
3444
+ var delta = event.deltaY < 0 ? 1 : -1;
3445
+ var newScale = handleCalculateButtonZoom(instance, delta, optionsRef.current.zoomStep);
3446
+ if (newScale === state.scale)
3447
+ return;
3448
+ // Zoom around the centre of the current viewport.
3449
+ var centerX = (wrapperComponent.offsetWidth / 2 - state.positionX) / state.scale;
3450
+ var centerY = (wrapperComponent.offsetHeight / 2 - state.positionY) / state.scale;
3451
+ handleCancelAnimation(instance);
3452
+ var target = handleZoomToPoint(instance, newScale, centerX, centerY);
3453
+ if (!target)
3454
+ return;
3455
+ instance.setState(target.scale, target.positionX, target.positionY);
3215
3456
  };
3216
- }, [computeMiniMapScale, instance, isDown, panning]);
3217
- var wrapperStyle = useMemo(function () {
3218
- return {
3219
- position: "relative",
3220
- zIndex: 2,
3221
- // overflow: "hidden",
3222
- userSelect: "none",
3457
+ element.addEventListener("wheel", onWheel, { passive: false });
3458
+ return function () {
3459
+ element.removeEventListener("wheel", onWheel);
3223
3460
  };
3224
- }, []);
3225
- return (jsxs("div", __assign({}, rest, { ref: mainRef, style: wrapperStyle, className: "rzpp-mini-map ".concat(rest.className || "").trim() }, { children: [jsx("div", __assign({ style: { pointerEvents: "none" }, ref: wrapperRef, className: "rzpp-minimap-wrapper ".concat(wrapperClassName || "").trim() }, { children: children })), jsx("div", { className: "rzpp-minimap-preview ".concat(previewClassName || "").trim(), ref: previewRef, style: __assign(__assign(__assign({}, previewStyles), { borderColor: borderColor }), previewStyle) })] })));
3461
+ }, [instance, zoomable]);
3462
+ 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);
3463
+ return (jsxs("div", __assign({}, rest, { ref: mainRef, style: containerStyle, className: "rzpp-mini-map ".concat(rest.className || "").trim() }, { children: [jsx("div", __assign({ style: wrapperStyles, ref: wrapperRef, className: "rzpp-minimap-wrapper ".concat(wrapperClassName || "").trim() }, { children: children })), jsx("svg", __assign({ className: "rzpp-minimap-mask", style: maskStyles, width: width, height: height, viewBox: "0 0 ".concat(width, " ").concat(height), "aria-hidden": "true" }, { children: jsx("path", { ref: maskPathRef, fill: maskColor, fillRule: "evenodd", d: "M0 0H".concat(width, "V").concat(height, "H0Z") }) })), jsx("div", { className: "rzpp-minimap-preview ".concat(previewClassName || "").trim(), ref: previewRef, style: __assign(__assign(__assign({}, previewStyles), { borderColor: borderColor }), previewStyle) })] })));
3226
3464
  };
3227
3465
 
3228
3466
  function styleInject(css, ref) {
@@ -3445,13 +3683,37 @@ var useTransformInit = function (callback) {
3445
3683
  }, []);
3446
3684
  };
3447
3685
 
3686
+ var takeSnapshot = function (state) { return ({
3687
+ scale: state.scale,
3688
+ positionX: state.positionX,
3689
+ positionY: state.positionY,
3690
+ }); };
3691
+ var isSameSnapshot = function (snapshot, state) {
3692
+ return snapshot !== null &&
3693
+ snapshot.scale === state.scale &&
3694
+ snapshot.positionX === state.positionX &&
3695
+ snapshot.positionY === state.positionY;
3696
+ };
3697
+
3448
3698
  var useTransformEffect = function (callback) {
3449
3699
  var libraryContext = useTransformContext();
3700
+ // Starts as the state the consumer rendered with, so a transform applied
3701
+ // during mount (initialPosition*, centerOnInit, fitOnInit) — which happens
3702
+ // in a layout effect, before this subscription exists — is delivered once.
3703
+ var lastSeen = useRef(null);
3704
+ if (lastSeen.current === null) {
3705
+ lastSeen.current = takeSnapshot(libraryContext.state);
3706
+ }
3450
3707
  useEffect(function () {
3451
3708
  var unmountCallback;
3452
3709
  var unmount = libraryContext.onChange(function (ref) {
3710
+ lastSeen.current = takeSnapshot(ref.instance.state);
3453
3711
  unmountCallback = callback(getState(ref.instance));
3454
3712
  });
3713
+ if (!isSameSnapshot(lastSeen.current, libraryContext.state)) {
3714
+ lastSeen.current = takeSnapshot(libraryContext.state);
3715
+ unmountCallback = callback(getState(libraryContext));
3716
+ }
3455
3717
  return function () {
3456
3718
  unmount();
3457
3719
  unmountCallback === null || unmountCallback === void 0 ? void 0 : unmountCallback();
@@ -3461,16 +3723,27 @@ var useTransformEffect = function (callback) {
3461
3723
 
3462
3724
  function useTransformComponent(callback) {
3463
3725
  var libraryContext = useTransformContext();
3726
+ var lastSeen = useRef(null);
3464
3727
  var _a = useState(function () {
3728
+ lastSeen.current = takeSnapshot(libraryContext.state);
3465
3729
  return callback(getState(libraryContext));
3466
3730
  }), transformRender = _a[0], setTransformRender = _a[1];
3467
3731
  useEffect(function () {
3468
3732
  var mounted = true;
3469
3733
  var unmount = libraryContext.onChange(function (ref) {
3470
3734
  if (mounted) {
3735
+ lastSeen.current = takeSnapshot(ref.instance.state);
3471
3736
  setTransformRender(callback(getState(ref.instance)));
3472
3737
  }
3473
3738
  });
3739
+ // The instance initializes in a layout effect, so a transform applied on
3740
+ // mount (initialPosition*, centerOnInit, fitOnInit) can land before this
3741
+ // subscription exists. Catch up when the state moved since the last
3742
+ // render; the snapshot check keeps this from re-rendering in a loop.
3743
+ if (!isSameSnapshot(lastSeen.current, libraryContext.state)) {
3744
+ lastSeen.current = takeSnapshot(libraryContext.state);
3745
+ setTransformRender(callback(getState(libraryContext)));
3746
+ }
3474
3747
  return function () {
3475
3748
  unmount();
3476
3749
  mounted = false;