react-zoom-pan-pinch 2.3.2 → 2.5.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,33 +771,32 @@ 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
- function getOffset(element) {
779
- var el = element;
780
- var offsetLeft = 0;
781
- var offsetTop = 0;
782
- while (el) {
783
- offsetLeft += el.offsetLeft;
784
- offsetTop += el.offsetTop;
785
- el = el.offsetParent;
786
- }
777
+ function getOffset(element, wrapper, content, state) {
778
+ var offset = element.getBoundingClientRect();
779
+ var wrapperOffset = wrapper.getBoundingClientRect();
780
+ var contentOffset = content.getBoundingClientRect();
781
+ var xOff = wrapperOffset.x * state.scale;
782
+ var yOff = wrapperOffset.y * state.scale;
787
783
  return {
788
- x: offsetLeft - window.scrollX,
789
- y: offsetTop - window.scrollY,
784
+ x: (offset.x - contentOffset.x + xOff) / state.scale,
785
+ y: (offset.y - contentOffset.y + yOff) / state.scale,
790
786
  };
791
787
  }
792
788
  function calculateZoomToNode(contextInstance, node, customZoom) {
793
- var wrapperComponent = contextInstance.wrapperComponent;
789
+ var wrapperComponent = contextInstance.wrapperComponent, contentComponent = contextInstance.contentComponent, transformState = contextInstance.transformState;
794
790
  var _a = contextInstance.setup, limitToBounds = _a.limitToBounds, minScale = _a.minScale, maxScale = _a.maxScale;
795
- if (!wrapperComponent)
796
- return initialState;
791
+ if (!wrapperComponent || !contentComponent)
792
+ return transformState;
797
793
  var wrapperRect = wrapperComponent.getBoundingClientRect();
798
- var nodeRect = getOffset(node);
799
- var nodeLeft = nodeRect.x;
800
- var nodeTop = nodeRect.y;
801
- var nodeWidth = node.offsetWidth;
802
- var nodeHeight = node.offsetHeight;
794
+ var nodeRect = node.getBoundingClientRect();
795
+ var nodeOffset = getOffset(node, wrapperComponent, contentComponent, transformState);
796
+ var nodeLeft = nodeOffset.x;
797
+ var nodeTop = nodeOffset.y;
798
+ var nodeWidth = nodeRect.width / transformState.scale;
799
+ var nodeHeight = nodeRect.height / transformState.scale;
803
800
  var scaleX = wrapperComponent.offsetWidth / nodeWidth;
804
801
  var scaleY = wrapperComponent.offsetHeight / nodeHeight;
805
802
  var newScale = checkZoomBounds(customZoom || Math.min(scaleX, scaleY), minScale, maxScale, 0, false);
@@ -811,17 +808,6 @@ function calculateZoomToNode(contextInstance, node, customZoom) {
811
808
  var _b = getMouseBoundedPosition(newPositionX, newPositionY, bounds, limitToBounds, 0, 0, wrapperComponent), x = _b.x, y = _b.y;
812
809
  return { positionX: x, positionY: y, scale: newScale };
813
810
  }
814
- function isValidZoomNode(node) {
815
- if (!node) {
816
- console.error("Zoom node not found");
817
- return false;
818
- }
819
- if ((node === null || node === void 0 ? void 0 : node.offsetWidth) === undefined || (node === null || node === void 0 ? void 0 : node.offsetHeight) === undefined) {
820
- console.error("Zoom node is not valid - it must contain offsetWidth and offsetHeight");
821
- return false;
822
- }
823
- return true;
824
- }
825
811
 
826
812
  var zoomIn = function (contextInstance) {
827
813
  return function (step, animationTime, animationType) {
@@ -881,10 +867,7 @@ var zoomToElement = function (contextInstance) {
881
867
  handleCancelAnimation(contextInstance);
882
868
  var wrapperComponent = contextInstance.wrapperComponent;
883
869
  var target = typeof node === "string" ? document.getElementById(node) : node;
884
- if (wrapperComponent &&
885
- isValidZoomNode(target) &&
886
- target &&
887
- wrapperComponent.contains(target)) {
870
+ if (wrapperComponent && target && wrapperComponent.contains(target)) {
888
871
  var targetState = calculateZoomToNode(contextInstance, target, scale);
889
872
  animate(contextInstance, targetState, animationTime, animationType);
890
873
  }
@@ -1204,25 +1187,52 @@ var handlePinchStop = function (contextInstance) {
1204
1187
  handleAlignToScaleBounds(contextInstance, pinchMidpoint === null || pinchMidpoint === void 0 ? void 0 : pinchMidpoint.x, pinchMidpoint === null || pinchMidpoint === void 0 ? void 0 : pinchMidpoint.y);
1205
1188
  };
1206
1189
 
1190
+ var handleDoubleClickStop = function (contextInstance, event) {
1191
+ var onZoomStop = contextInstance.props.onZoomStop;
1192
+ var animationTime = contextInstance.setup.doubleClick.animationTime;
1193
+ cancelTimeout(contextInstance.doubleClickStopEventTimer);
1194
+ contextInstance.doubleClickStopEventTimer = setTimeout(function () {
1195
+ contextInstance.doubleClickStopEventTimer = null;
1196
+ handleCallback(getContext(contextInstance), event, onZoomStop);
1197
+ }, animationTime);
1198
+ };
1199
+ var handleDoubleClickResetMode = function (contextInstance, event) {
1200
+ var _a = contextInstance.props, onZoomStart = _a.onZoomStart, onZoom = _a.onZoom;
1201
+ var _b = contextInstance.setup.doubleClick, animationTime = _b.animationTime, animationType = _b.animationType;
1202
+ handleCallback(getContext(contextInstance), event, onZoomStart);
1203
+ resetTransformations(contextInstance, animationTime, animationType, function () {
1204
+ return handleCallback(getContext(contextInstance), event, onZoom);
1205
+ });
1206
+ handleDoubleClickStop(contextInstance, event);
1207
+ };
1207
1208
  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;
1209
+ var setup = contextInstance.setup, doubleClickStopEventTimer = contextInstance.doubleClickStopEventTimer, transformState = contextInstance.transformState, contentComponent = contextInstance.contentComponent;
1210
+ var scale = transformState.scale;
1211
+ var _a = contextInstance.props, onZoomStart = _a.onZoomStart, onZoom = _a.onZoom;
1212
+ var _b = setup.doubleClick, disabled = _b.disabled, mode = _b.mode, step = _b.step, animationTime = _b.animationTime, animationType = _b.animationType;
1209
1213
  if (disabled)
1210
1214
  return;
1215
+ if (doubleClickStopEventTimer)
1216
+ return;
1211
1217
  if (mode === "reset") {
1212
- return resetTransformations(contextInstance, animationTime, animationType);
1218
+ return handleDoubleClickResetMode(contextInstance, event);
1213
1219
  }
1214
- var scale = contextInstance.transformState.scale;
1215
- var contentComponent = contextInstance.contentComponent;
1216
1220
  if (!contentComponent)
1217
1221
  return console.error("No ContentComponent found");
1218
1222
  var delta = mode === "zoomOut" ? -1 : 1;
1219
1223
  var newScale = handleCalculateButtonZoom(contextInstance, delta, step);
1224
+ // stop execution when scale didn't change
1225
+ if (scale === newScale)
1226
+ return;
1227
+ handleCallback(getContext(contextInstance), event, onZoomStart);
1220
1228
  var mousePosition = getMousePosition(event, contentComponent, scale);
1221
1229
  var targetState = handleZoomToPoint(contextInstance, newScale, mousePosition.x, mousePosition.y);
1222
1230
  if (!targetState) {
1223
1231
  return console.error("Error during zoom event. New transformation state was not calculated.");
1224
1232
  }
1233
+ handleCallback(getContext(contextInstance), event, onZoom);
1225
1234
  animate(contextInstance, targetState, animationTime, animationType);
1235
+ handleDoubleClickStop(contextInstance, event);
1226
1236
  }
1227
1237
  var isDoubleClickAllowed = function (contextInstance, event) {
1228
1238
  var isInitialized = contextInstance.isInitialized, setup = contextInstance.setup, wrapperComponent = contextInstance.wrapperComponent;
@@ -1235,16 +1245,14 @@ var isDoubleClickAllowed = function (contextInstance, event) {
1235
1245
  var isExcluded = isExcludedNode(target, excluded);
1236
1246
  if (isExcluded)
1237
1247
  return false;
1238
- if (!isAllowed)
1239
- return false;
1240
1248
  return true;
1241
1249
  };
1242
1250
 
1243
1251
  var ZoomPanPinch = /** @class */ (function () {
1244
- function ZoomPanPinch(props, onChange) {
1252
+ function ZoomPanPinch(props) {
1245
1253
  var _this = this;
1246
- this.onChange = onChange;
1247
1254
  this.mounted = true;
1255
+ this.onChangeCallbacks = new Set();
1248
1256
  // Components
1249
1257
  this.wrapperComponent = null;
1250
1258
  this.contentComponent = null;
@@ -1265,6 +1273,8 @@ var ZoomPanPinch = /** @class */ (function () {
1265
1273
  this.pinchStartDistance = null;
1266
1274
  this.pinchStartScale = null;
1267
1275
  this.pinchMidpoint = null;
1276
+ // double click helpers
1277
+ this.doubleClickStopEventTimer = null;
1268
1278
  // velocity helpers
1269
1279
  this.velocity = null;
1270
1280
  this.velocityTime = null;
@@ -1521,7 +1531,6 @@ var ZoomPanPinch = /** @class */ (function () {
1521
1531
  return Boolean(keys.find(function (key) { return _this.pressedKeys[key]; }));
1522
1532
  };
1523
1533
  this.setTransformState = function (scale, positionX, positionY) {
1524
- var _a;
1525
1534
  var onTransformed = _this.props.onTransformed;
1526
1535
  if (!Number.isNaN(scale) &&
1527
1536
  !Number.isNaN(positionX) &&
@@ -1532,9 +1541,9 @@ var ZoomPanPinch = /** @class */ (function () {
1532
1541
  }
1533
1542
  _this.transformState.positionX = positionX;
1534
1543
  _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);
1544
+ var ctx_1 = getContext(_this);
1545
+ _this.onChangeCallbacks.forEach(function (callback) { return callback(ctx_1); });
1546
+ handleCallback(ctx_1, { scale: scale, positionX: positionX, positionY: positionY }, onTransformed);
1538
1547
  _this.applyTransformation();
1539
1548
  }
1540
1549
  else {
@@ -1557,6 +1566,14 @@ var ZoomPanPinch = /** @class */ (function () {
1557
1566
  this.getContext = function () {
1558
1567
  return getContext(_this);
1559
1568
  };
1569
+ this.onChange = function (callback) {
1570
+ if (!_this.onChangeCallbacks.has(callback)) {
1571
+ _this.onChangeCallbacks.add(callback);
1572
+ }
1573
+ return function () {
1574
+ _this.onChangeCallbacks.delete(callback);
1575
+ };
1576
+ };
1560
1577
  /**
1561
1578
  * Initialization
1562
1579
  */
@@ -1578,30 +1595,31 @@ var ZoomPanPinch = /** @class */ (function () {
1578
1595
  return ZoomPanPinch;
1579
1596
  }());
1580
1597
 
1581
- var Context = React.createContext(contextInitialState);
1598
+ var Context = React.createContext(null);
1599
+ var getContent = function (children, ctx) {
1600
+ if (typeof children === "function") {
1601
+ return children(ctx);
1602
+ }
1603
+ return children;
1604
+ };
1582
1605
  var TransformWrapper = React.forwardRef(function (props, ref) {
1583
1606
  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 () {
1607
+ var children = props.children;
1608
+ var instance = useRef(new ZoomPanPinch(props)).current;
1609
+ var content = getContent(props.children, getContext(instance));
1610
+ var handleOnChange = useCallback(function () {
1591
1611
  if (typeof children === "function") {
1592
- return children(getContext(instance));
1612
+ forceUpdate(function (prev) { return prev + 1; });
1593
1613
  }
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)]);
1614
+ }, [children]);
1600
1615
  useImperativeHandle(ref, function () { return getContext(instance); }, [instance]);
1601
1616
  useEffect(function () {
1602
1617
  instance.update(props);
1603
1618
  }, [instance, props]);
1604
- return React.createElement(Context.Provider, { value: value }, content);
1619
+ useEffect(function () {
1620
+ return instance.onChange(handleOnChange);
1621
+ }, [instance, props, handleOnChange]);
1622
+ return React.createElement(Context.Provider, { value: instance }, content);
1605
1623
  });
1606
1624
 
1607
1625
  function styleInject(css, ref) {
@@ -1637,14 +1655,14 @@ styleInject(css_248z);
1637
1655
 
1638
1656
  var TransformComponent = function (_a) {
1639
1657
  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;
1658
+ var init = useContext(Context).init;
1641
1659
  var wrapperRef = useRef(null);
1642
1660
  var contentRef = useRef(null);
1643
1661
  useEffect(function () {
1644
1662
  var wrapper = wrapperRef.current;
1645
1663
  var content = contentRef.current;
1646
- if (wrapper !== null && content !== null && setComponents) {
1647
- setComponents(wrapper, content);
1664
+ if (wrapper !== null && content !== null && init) {
1665
+ init(wrapper, content);
1648
1666
  }
1649
1667
  // eslint-disable-next-line react-hooks/exhaustive-deps
1650
1668
  }, []);
@@ -1654,10 +1672,16 @@ var TransformComponent = function (_a) {
1654
1672
 
1655
1673
  var useTransformContext = function () {
1656
1674
  var libraryContext = useContext(Context);
1657
- if (!libraryContext.contextInstance) {
1675
+ var _a = useState(getContext(libraryContext || {})), values = _a[0], setValues = _a[1];
1676
+ if (!libraryContext) {
1658
1677
  throw new Error("Transform context mus be placed inside TransformWrapper");
1659
1678
  }
1660
- return getContext(libraryContext.contextInstance);
1679
+ useEffect(function () {
1680
+ libraryContext.onChange(function (ref) {
1681
+ setValues(ref);
1682
+ });
1683
+ }, [libraryContext]);
1684
+ return values;
1661
1685
  };
1662
1686
 
1663
1687
  export { TransformComponent, TransformWrapper, useTransformContext };