react-zoom-pan-pinch 2.3.2 → 2.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/dist/index.esm.js CHANGED
@@ -1,40 +1,4 @@
1
- import React, { useState, useRef, useMemo, useImperativeHandle, useEffect, useContext } from 'react';
2
-
3
- /******************************************************************************
4
- Copyright (c) Microsoft Corporation.
5
-
6
- Permission to use, copy, modify, and/or distribute this software for any
7
- purpose with or without fee is hereby granted.
8
-
9
- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
10
- REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11
- AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
12
- INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13
- LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
14
- OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15
- PERFORMANCE OF THIS SOFTWARE.
16
- ***************************************************************************** */
17
-
18
- var __assign = function() {
19
- __assign = Object.assign || function __assign(t) {
20
- for (var s, i = 1, n = arguments.length; i < n; i++) {
21
- s = arguments[i];
22
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
23
- }
24
- return t;
25
- };
26
- return __assign.apply(this, arguments);
27
- };
28
-
29
- function __spreadArray(to, from, pack) {
30
- if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
31
- if (ar || !(i in from)) {
32
- if (!ar) ar = Array.prototype.slice.call(from, 0, i);
33
- ar[i] = from[i];
34
- }
35
- }
36
- return to.concat(ar || Array.prototype.slice.call(from));
37
- }
1
+ import React, { useState, useRef, useCallback, useImperativeHandle, useEffect, useContext } from 'react';
38
2
 
39
3
  /**
40
4
  * Rounds number to given decimal
@@ -622,16 +586,50 @@ function handleAlignToScaleBounds(contextInstance, mousePositionX, mousePosition
622
586
  }
623
587
  }
624
588
 
589
+ /******************************************************************************
590
+ Copyright (c) Microsoft Corporation.
591
+
592
+ Permission to use, copy, modify, and/or distribute this software for any
593
+ purpose with or without fee is hereby granted.
594
+
595
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
596
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
597
+ AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
598
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
599
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
600
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
601
+ PERFORMANCE OF THIS SOFTWARE.
602
+ ***************************************************************************** */
603
+
604
+ var __assign = function() {
605
+ __assign = Object.assign || function __assign(t) {
606
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
607
+ s = arguments[i];
608
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
609
+ }
610
+ return t;
611
+ };
612
+ return __assign.apply(this, arguments);
613
+ };
614
+
615
+ function __spreadArray(to, from, pack) {
616
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
617
+ if (ar || !(i in from)) {
618
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
619
+ ar[i] = from[i];
620
+ }
621
+ }
622
+ return to.concat(ar || Array.prototype.slice.call(from));
623
+ }
624
+
625
625
  var initialState = {
626
626
  previousScale: 1,
627
627
  scale: 1,
628
628
  positionX: 0,
629
629
  positionY: 0,
630
630
  };
631
- var contextInitialState = __assign(__assign({}, initialState), { setComponents: function () { return undefined; }, contextInstance: null });
632
631
  var initialSetup = {
633
632
  disabled: false,
634
- performance: false,
635
633
  minPositionX: null,
636
634
  maxPositionX: null,
637
635
  minPositionY: null,
@@ -754,7 +752,7 @@ function handleZoomToViewCenter(contextInstance, delta, step, animationTime, ani
754
752
  }
755
753
  animate(contextInstance, targetState, animationTime, animationType);
756
754
  }
757
- function resetTransformations(contextInstance, animationTime, animationType) {
755
+ function resetTransformations(contextInstance, animationTime, animationType, onResetTransformation) {
758
756
  var setup = contextInstance.setup, wrapperComponent = contextInstance.wrapperComponent;
759
757
  var limitToBounds = setup.limitToBounds;
760
758
  var initialTransformation = createState(contextInstance.props);
@@ -773,6 +771,7 @@ function resetTransformations(contextInstance, animationTime, animationType) {
773
771
  positionY === initialTransformation.positionY) {
774
772
  return;
775
773
  }
774
+ onResetTransformation === null || onResetTransformation === void 0 ? void 0 : onResetTransformation();
776
775
  animate(contextInstance, newState, animationTime, animationType);
777
776
  }
778
777
  function getOffset(element) {
@@ -1204,25 +1203,52 @@ var handlePinchStop = function (contextInstance) {
1204
1203
  handleAlignToScaleBounds(contextInstance, pinchMidpoint === null || pinchMidpoint === void 0 ? void 0 : pinchMidpoint.x, pinchMidpoint === null || pinchMidpoint === void 0 ? void 0 : pinchMidpoint.y);
1205
1204
  };
1206
1205
 
1206
+ var handleDoubleClickStop = function (contextInstance, event) {
1207
+ var onZoomStop = contextInstance.props.onZoomStop;
1208
+ var animationTime = contextInstance.setup.doubleClick.animationTime;
1209
+ cancelTimeout(contextInstance.doubleClickStopEventTimer);
1210
+ contextInstance.doubleClickStopEventTimer = setTimeout(function () {
1211
+ contextInstance.doubleClickStopEventTimer = null;
1212
+ handleCallback(getContext(contextInstance), event, onZoomStop);
1213
+ }, animationTime);
1214
+ };
1215
+ var handleDoubleClickResetMode = function (contextInstance, event) {
1216
+ var _a = contextInstance.props, onZoomStart = _a.onZoomStart, onZoom = _a.onZoom;
1217
+ var _b = contextInstance.setup.doubleClick, animationTime = _b.animationTime, animationType = _b.animationType;
1218
+ handleCallback(getContext(contextInstance), event, onZoomStart);
1219
+ resetTransformations(contextInstance, animationTime, animationType, function () {
1220
+ return handleCallback(getContext(contextInstance), event, onZoom);
1221
+ });
1222
+ handleDoubleClickStop(contextInstance, event);
1223
+ };
1207
1224
  function handleDoubleClick(contextInstance, event) {
1208
- var _a = contextInstance.setup.doubleClick, disabled = _a.disabled, mode = _a.mode, step = _a.step, animationTime = _a.animationTime, animationType = _a.animationType;
1225
+ var setup = contextInstance.setup, doubleClickStopEventTimer = contextInstance.doubleClickStopEventTimer, transformState = contextInstance.transformState, contentComponent = contextInstance.contentComponent;
1226
+ var scale = transformState.scale;
1227
+ var _a = contextInstance.props, onZoomStart = _a.onZoomStart, onZoom = _a.onZoom;
1228
+ var _b = setup.doubleClick, disabled = _b.disabled, mode = _b.mode, step = _b.step, animationTime = _b.animationTime, animationType = _b.animationType;
1209
1229
  if (disabled)
1210
1230
  return;
1231
+ if (doubleClickStopEventTimer)
1232
+ return;
1211
1233
  if (mode === "reset") {
1212
- return resetTransformations(contextInstance, animationTime, animationType);
1234
+ return handleDoubleClickResetMode(contextInstance, event);
1213
1235
  }
1214
- var scale = contextInstance.transformState.scale;
1215
- var contentComponent = contextInstance.contentComponent;
1216
1236
  if (!contentComponent)
1217
1237
  return console.error("No ContentComponent found");
1218
1238
  var delta = mode === "zoomOut" ? -1 : 1;
1219
1239
  var newScale = handleCalculateButtonZoom(contextInstance, delta, step);
1240
+ // stop execution when scale didn't change
1241
+ if (scale === newScale)
1242
+ return;
1243
+ handleCallback(getContext(contextInstance), event, onZoomStart);
1220
1244
  var mousePosition = getMousePosition(event, contentComponent, scale);
1221
1245
  var targetState = handleZoomToPoint(contextInstance, newScale, mousePosition.x, mousePosition.y);
1222
1246
  if (!targetState) {
1223
1247
  return console.error("Error during zoom event. New transformation state was not calculated.");
1224
1248
  }
1249
+ handleCallback(getContext(contextInstance), event, onZoom);
1225
1250
  animate(contextInstance, targetState, animationTime, animationType);
1251
+ handleDoubleClickStop(contextInstance, event);
1226
1252
  }
1227
1253
  var isDoubleClickAllowed = function (contextInstance, event) {
1228
1254
  var isInitialized = contextInstance.isInitialized, setup = contextInstance.setup, wrapperComponent = contextInstance.wrapperComponent;
@@ -1235,16 +1261,14 @@ var isDoubleClickAllowed = function (contextInstance, event) {
1235
1261
  var isExcluded = isExcludedNode(target, excluded);
1236
1262
  if (isExcluded)
1237
1263
  return false;
1238
- if (!isAllowed)
1239
- return false;
1240
1264
  return true;
1241
1265
  };
1242
1266
 
1243
1267
  var ZoomPanPinch = /** @class */ (function () {
1244
- function ZoomPanPinch(props, onChange) {
1268
+ function ZoomPanPinch(props) {
1245
1269
  var _this = this;
1246
- this.onChange = onChange;
1247
1270
  this.mounted = true;
1271
+ this.onChangeCallbacks = new Set();
1248
1272
  // Components
1249
1273
  this.wrapperComponent = null;
1250
1274
  this.contentComponent = null;
@@ -1265,6 +1289,8 @@ var ZoomPanPinch = /** @class */ (function () {
1265
1289
  this.pinchStartDistance = null;
1266
1290
  this.pinchStartScale = null;
1267
1291
  this.pinchMidpoint = null;
1292
+ // double click helpers
1293
+ this.doubleClickStopEventTimer = null;
1268
1294
  // velocity helpers
1269
1295
  this.velocity = null;
1270
1296
  this.velocityTime = null;
@@ -1521,7 +1547,6 @@ var ZoomPanPinch = /** @class */ (function () {
1521
1547
  return Boolean(keys.find(function (key) { return _this.pressedKeys[key]; }));
1522
1548
  };
1523
1549
  this.setTransformState = function (scale, positionX, positionY) {
1524
- var _a;
1525
1550
  var onTransformed = _this.props.onTransformed;
1526
1551
  if (!Number.isNaN(scale) &&
1527
1552
  !Number.isNaN(positionX) &&
@@ -1532,9 +1557,9 @@ var ZoomPanPinch = /** @class */ (function () {
1532
1557
  }
1533
1558
  _this.transformState.positionX = positionX;
1534
1559
  _this.transformState.positionY = positionY;
1535
- var ctx = getContext(_this);
1536
- (_a = _this.onChange) === null || _a === void 0 ? void 0 : _a.call(_this, ctx);
1537
- handleCallback(ctx, { scale: scale, positionX: positionX, positionY: positionY }, onTransformed);
1560
+ var ctx_1 = getContext(_this);
1561
+ _this.onChangeCallbacks.forEach(function (callback) { return callback(ctx_1); });
1562
+ handleCallback(ctx_1, { scale: scale, positionX: positionX, positionY: positionY }, onTransformed);
1538
1563
  _this.applyTransformation();
1539
1564
  }
1540
1565
  else {
@@ -1557,6 +1582,14 @@ var ZoomPanPinch = /** @class */ (function () {
1557
1582
  this.getContext = function () {
1558
1583
  return getContext(_this);
1559
1584
  };
1585
+ this.onChange = function (callback) {
1586
+ if (!_this.onChangeCallbacks.has(callback)) {
1587
+ _this.onChangeCallbacks.add(callback);
1588
+ }
1589
+ return function () {
1590
+ _this.onChangeCallbacks.delete(callback);
1591
+ };
1592
+ };
1560
1593
  /**
1561
1594
  * Initialization
1562
1595
  */
@@ -1578,30 +1611,31 @@ var ZoomPanPinch = /** @class */ (function () {
1578
1611
  return ZoomPanPinch;
1579
1612
  }());
1580
1613
 
1581
- var Context = React.createContext(contextInitialState);
1614
+ var Context = React.createContext(null);
1615
+ var getContent = function (children, ctx) {
1616
+ if (typeof children === "function") {
1617
+ return children(ctx);
1618
+ }
1619
+ return children;
1620
+ };
1582
1621
  var TransformWrapper = React.forwardRef(function (props, ref) {
1583
1622
  var _a = useState(0), forceUpdate = _a[1];
1584
- var children = props.children, performance = props.performance;
1585
- var instance = useRef(new ZoomPanPinch(props, function () {
1586
- if (!performance) {
1587
- forceUpdate(function (prev) { return prev + 1; });
1588
- }
1589
- })).current;
1590
- var content = useMemo(function () {
1623
+ var children = props.children;
1624
+ var instance = useRef(new ZoomPanPinch(props)).current;
1625
+ var content = getContent(props.children, getContext(instance));
1626
+ var handleOnChange = useCallback(function () {
1591
1627
  if (typeof children === "function") {
1592
- return children(getContext(instance));
1628
+ forceUpdate(function (prev) { return prev + 1; });
1593
1629
  }
1594
- return children;
1595
- }, [children, instance]);
1596
- var value = useMemo(function () {
1597
- return __assign(__assign({}, instance.transformState), { setComponents: instance.init, contextInstance: instance });
1598
- // eslint-disable-next-line react-hooks/exhaustive-deps
1599
- }, [JSON.stringify(instance.transformState)]);
1630
+ }, [children]);
1600
1631
  useImperativeHandle(ref, function () { return getContext(instance); }, [instance]);
1601
1632
  useEffect(function () {
1602
1633
  instance.update(props);
1603
1634
  }, [instance, props]);
1604
- return React.createElement(Context.Provider, { value: value }, content);
1635
+ useEffect(function () {
1636
+ return instance.onChange(handleOnChange);
1637
+ }, [instance, props, handleOnChange]);
1638
+ return React.createElement(Context.Provider, { value: instance }, content);
1605
1639
  });
1606
1640
 
1607
1641
  function styleInject(css, ref) {
@@ -1637,14 +1671,14 @@ styleInject(css_248z);
1637
1671
 
1638
1672
  var TransformComponent = function (_a) {
1639
1673
  var children = _a.children, _b = _a.wrapperClass, wrapperClass = _b === void 0 ? "" : _b, _c = _a.contentClass, contentClass = _c === void 0 ? "" : _c, wrapperStyle = _a.wrapperStyle, contentStyle = _a.contentStyle, _d = _a.wrapperProps, wrapperProps = _d === void 0 ? {} : _d, _e = _a.contentProps, contentProps = _e === void 0 ? {} : _e;
1640
- var setComponents = useContext(Context).setComponents;
1674
+ var init = useContext(Context).init;
1641
1675
  var wrapperRef = useRef(null);
1642
1676
  var contentRef = useRef(null);
1643
1677
  useEffect(function () {
1644
1678
  var wrapper = wrapperRef.current;
1645
1679
  var content = contentRef.current;
1646
- if (wrapper !== null && content !== null && setComponents) {
1647
- setComponents(wrapper, content);
1680
+ if (wrapper !== null && content !== null && init) {
1681
+ init(wrapper, content);
1648
1682
  }
1649
1683
  // eslint-disable-next-line react-hooks/exhaustive-deps
1650
1684
  }, []);
@@ -1654,10 +1688,16 @@ var TransformComponent = function (_a) {
1654
1688
 
1655
1689
  var useTransformContext = function () {
1656
1690
  var libraryContext = useContext(Context);
1657
- if (!libraryContext.contextInstance) {
1691
+ var _a = useState(getContext(libraryContext || {})), values = _a[0], setValues = _a[1];
1692
+ if (!libraryContext) {
1658
1693
  throw new Error("Transform context mus be placed inside TransformWrapper");
1659
1694
  }
1660
- return getContext(libraryContext.contextInstance);
1695
+ useEffect(function () {
1696
+ libraryContext.onChange(function (ref) {
1697
+ setValues(ref);
1698
+ });
1699
+ }, [libraryContext]);
1700
+ return values;
1661
1701
  };
1662
1702
 
1663
1703
  export { TransformComponent, TransformWrapper, useTransformContext };