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.
@@ -1,8 +1,6 @@
1
1
  import React from "react";
2
2
  import { ZoomPanPinch } from "../core/instance.core";
3
3
  import { ReactZoomPanPinchProps, ReactZoomPanPinchRef } from "../models";
4
- export declare const Context: React.Context<import("../models").ReactZoomPanPinchComponentHelpers & import("../models").ReactZoomPanPinchState & {
5
- contextInstance: ZoomPanPinch | null;
6
- }>;
4
+ export declare const Context: React.Context<ZoomPanPinch>;
7
5
  export declare const TransformWrapper: React.ForwardRefExoticComponent<Omit<ReactZoomPanPinchProps, "ref"> & React.RefAttributes<ReactZoomPanPinchRef>>;
8
6
  export default TransformWrapper;
@@ -1,6 +1,3 @@
1
- import { LibrarySetup, ReactZoomPanPinchContext, ReactZoomPanPinchComponentHelpers, ReactZoomPanPinchState } from "../models/context.model";
1
+ import { LibrarySetup, ReactZoomPanPinchState } from "../models/context.model";
2
2
  export declare const initialState: ReactZoomPanPinchState;
3
- export declare const contextInitialState: ReactZoomPanPinchComponentHelpers & ReactZoomPanPinchState & {
4
- contextInstance: ReactZoomPanPinchContext | null;
5
- };
6
3
  export declare const initialSetup: LibrarySetup;
@@ -1,3 +1,5 @@
1
1
  import { ReactZoomPanPinchContext } from "../../models";
2
+ export declare const handleDoubleClickStop: (contextInstance: ReactZoomPanPinchContext, event: MouseEvent | TouchEvent) => void;
3
+ export declare const handleDoubleClickResetMode: (contextInstance: ReactZoomPanPinchContext, event: MouseEvent | TouchEvent) => void;
2
4
  export declare function handleDoubleClick(contextInstance: ReactZoomPanPinchContext, event: MouseEvent | TouchEvent): void;
3
5
  export declare const isDoubleClickAllowed: (contextInstance: ReactZoomPanPinchContext, event: MouseEvent | TouchEvent) => boolean;
@@ -3,7 +3,7 @@ import { animations } from "../animations/animations.constants";
3
3
  import { PositionType } from "../../models/calculations.model";
4
4
  export declare const handleCalculateButtonZoom: (contextInstance: ReactZoomPanPinchContext, delta: number, step: number) => number;
5
5
  export declare function handleZoomToViewCenter(contextInstance: ReactZoomPanPinchContext, delta: number, step: number, animationTime: number, animationType: keyof typeof animations): void;
6
- export declare function resetTransformations(contextInstance: ReactZoomPanPinchContext, animationTime: number, animationType: keyof typeof animations): void;
6
+ export declare function resetTransformations(contextInstance: ReactZoomPanPinchContext, animationTime: number, animationType: keyof typeof animations, onResetTransformation?: () => void): void;
7
7
  export declare function getOffset(element: HTMLElement): PositionType;
8
8
  export declare function calculateZoomToNode(contextInstance: ReactZoomPanPinchContext, node: HTMLElement, customZoom?: number): {
9
9
  positionX: number;
@@ -4,12 +4,12 @@ type StartCoordsType = {
4
4
  y: number;
5
5
  } | null;
6
6
  export declare class ZoomPanPinch {
7
- private onChange?;
8
7
  props: ReactZoomPanPinchProps;
9
8
  mounted: boolean;
10
9
  transformState: ReactZoomPanPinchState;
11
10
  setup: LibrarySetup;
12
11
  observer?: ResizeObserver;
12
+ onChangeCallbacks: Set<(ctx: ReactZoomPanPinchRef) => void>;
13
13
  wrapperComponent: HTMLDivElement | null;
14
14
  contentComponent: HTMLDivElement | null;
15
15
  isInitialized: boolean;
@@ -25,6 +25,7 @@ export declare class ZoomPanPinch {
25
25
  pinchStartDistance: null | number;
26
26
  pinchStartScale: null | number;
27
27
  pinchMidpoint: null | PositionType;
28
+ doubleClickStopEventTimer: ReturnType<typeof setTimeout> | null;
28
29
  velocity: VelocityType | null;
29
30
  velocityTime: number | null;
30
31
  lastMousePosition: PositionType | null;
@@ -34,7 +35,7 @@ export declare class ZoomPanPinch {
34
35
  pressedKeys: {
35
36
  [key: string]: boolean;
36
37
  };
37
- constructor(props: ReactZoomPanPinchProps, onChange?: ((ctx: ReactZoomPanPinchRef) => void) | undefined);
38
+ constructor(props: ReactZoomPanPinchProps);
38
39
  mount: () => void;
39
40
  unmount: () => void;
40
41
  update: (newProps: ReactZoomPanPinchProps) => void;
@@ -61,6 +62,7 @@ export declare class ZoomPanPinch {
61
62
  setCenter: () => void;
62
63
  applyTransformation: () => void;
63
64
  getContext: () => ReactZoomPanPinchRef;
65
+ onChange: (callback: (ref: ReactZoomPanPinchRef) => void) => () => void;
64
66
  /**
65
67
  * Initialization
66
68
  */
package/dist/index.cjs.js CHANGED
@@ -8,42 +8,6 @@ function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'defau
8
8
 
9
9
  var React__default = /*#__PURE__*/_interopDefaultLegacy(React);
10
10
 
11
- /******************************************************************************
12
- Copyright (c) Microsoft Corporation.
13
-
14
- Permission to use, copy, modify, and/or distribute this software for any
15
- purpose with or without fee is hereby granted.
16
-
17
- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
18
- REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
19
- AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
20
- INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
21
- LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
22
- OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
23
- PERFORMANCE OF THIS SOFTWARE.
24
- ***************************************************************************** */
25
-
26
- var __assign = function() {
27
- __assign = Object.assign || function __assign(t) {
28
- for (var s, i = 1, n = arguments.length; i < n; i++) {
29
- s = arguments[i];
30
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
31
- }
32
- return t;
33
- };
34
- return __assign.apply(this, arguments);
35
- };
36
-
37
- function __spreadArray(to, from, pack) {
38
- if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
39
- if (ar || !(i in from)) {
40
- if (!ar) ar = Array.prototype.slice.call(from, 0, i);
41
- ar[i] = from[i];
42
- }
43
- }
44
- return to.concat(ar || Array.prototype.slice.call(from));
45
- }
46
-
47
11
  /**
48
12
  * Rounds number to given decimal
49
13
  * eg. roundNumber(2.34343, 1) => 2.3
@@ -630,16 +594,50 @@ function handleAlignToScaleBounds(contextInstance, mousePositionX, mousePosition
630
594
  }
631
595
  }
632
596
 
597
+ /******************************************************************************
598
+ Copyright (c) Microsoft Corporation.
599
+
600
+ Permission to use, copy, modify, and/or distribute this software for any
601
+ purpose with or without fee is hereby granted.
602
+
603
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
604
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
605
+ AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
606
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
607
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
608
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
609
+ PERFORMANCE OF THIS SOFTWARE.
610
+ ***************************************************************************** */
611
+
612
+ var __assign = function() {
613
+ __assign = Object.assign || function __assign(t) {
614
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
615
+ s = arguments[i];
616
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
617
+ }
618
+ return t;
619
+ };
620
+ return __assign.apply(this, arguments);
621
+ };
622
+
623
+ function __spreadArray(to, from, pack) {
624
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
625
+ if (ar || !(i in from)) {
626
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
627
+ ar[i] = from[i];
628
+ }
629
+ }
630
+ return to.concat(ar || Array.prototype.slice.call(from));
631
+ }
632
+
633
633
  var initialState = {
634
634
  previousScale: 1,
635
635
  scale: 1,
636
636
  positionX: 0,
637
637
  positionY: 0,
638
638
  };
639
- var contextInitialState = __assign(__assign({}, initialState), { setComponents: function () { return undefined; }, contextInstance: null });
640
639
  var initialSetup = {
641
640
  disabled: false,
642
- performance: false,
643
641
  minPositionX: null,
644
642
  maxPositionX: null,
645
643
  minPositionY: null,
@@ -762,7 +760,7 @@ function handleZoomToViewCenter(contextInstance, delta, step, animationTime, ani
762
760
  }
763
761
  animate(contextInstance, targetState, animationTime, animationType);
764
762
  }
765
- function resetTransformations(contextInstance, animationTime, animationType) {
763
+ function resetTransformations(contextInstance, animationTime, animationType, onResetTransformation) {
766
764
  var setup = contextInstance.setup, wrapperComponent = contextInstance.wrapperComponent;
767
765
  var limitToBounds = setup.limitToBounds;
768
766
  var initialTransformation = createState(contextInstance.props);
@@ -781,6 +779,7 @@ function resetTransformations(contextInstance, animationTime, animationType) {
781
779
  positionY === initialTransformation.positionY) {
782
780
  return;
783
781
  }
782
+ onResetTransformation === null || onResetTransformation === void 0 ? void 0 : onResetTransformation();
784
783
  animate(contextInstance, newState, animationTime, animationType);
785
784
  }
786
785
  function getOffset(element) {
@@ -1212,25 +1211,52 @@ var handlePinchStop = function (contextInstance) {
1212
1211
  handleAlignToScaleBounds(contextInstance, pinchMidpoint === null || pinchMidpoint === void 0 ? void 0 : pinchMidpoint.x, pinchMidpoint === null || pinchMidpoint === void 0 ? void 0 : pinchMidpoint.y);
1213
1212
  };
1214
1213
 
1214
+ var handleDoubleClickStop = function (contextInstance, event) {
1215
+ var onZoomStop = contextInstance.props.onZoomStop;
1216
+ var animationTime = contextInstance.setup.doubleClick.animationTime;
1217
+ cancelTimeout(contextInstance.doubleClickStopEventTimer);
1218
+ contextInstance.doubleClickStopEventTimer = setTimeout(function () {
1219
+ contextInstance.doubleClickStopEventTimer = null;
1220
+ handleCallback(getContext(contextInstance), event, onZoomStop);
1221
+ }, animationTime);
1222
+ };
1223
+ var handleDoubleClickResetMode = function (contextInstance, event) {
1224
+ var _a = contextInstance.props, onZoomStart = _a.onZoomStart, onZoom = _a.onZoom;
1225
+ var _b = contextInstance.setup.doubleClick, animationTime = _b.animationTime, animationType = _b.animationType;
1226
+ handleCallback(getContext(contextInstance), event, onZoomStart);
1227
+ resetTransformations(contextInstance, animationTime, animationType, function () {
1228
+ return handleCallback(getContext(contextInstance), event, onZoom);
1229
+ });
1230
+ handleDoubleClickStop(contextInstance, event);
1231
+ };
1215
1232
  function handleDoubleClick(contextInstance, event) {
1216
- var _a = contextInstance.setup.doubleClick, disabled = _a.disabled, mode = _a.mode, step = _a.step, animationTime = _a.animationTime, animationType = _a.animationType;
1233
+ var setup = contextInstance.setup, doubleClickStopEventTimer = contextInstance.doubleClickStopEventTimer, transformState = contextInstance.transformState, contentComponent = contextInstance.contentComponent;
1234
+ var scale = transformState.scale;
1235
+ var _a = contextInstance.props, onZoomStart = _a.onZoomStart, onZoom = _a.onZoom;
1236
+ var _b = setup.doubleClick, disabled = _b.disabled, mode = _b.mode, step = _b.step, animationTime = _b.animationTime, animationType = _b.animationType;
1217
1237
  if (disabled)
1218
1238
  return;
1239
+ if (doubleClickStopEventTimer)
1240
+ return;
1219
1241
  if (mode === "reset") {
1220
- return resetTransformations(contextInstance, animationTime, animationType);
1242
+ return handleDoubleClickResetMode(contextInstance, event);
1221
1243
  }
1222
- var scale = contextInstance.transformState.scale;
1223
- var contentComponent = contextInstance.contentComponent;
1224
1244
  if (!contentComponent)
1225
1245
  return console.error("No ContentComponent found");
1226
1246
  var delta = mode === "zoomOut" ? -1 : 1;
1227
1247
  var newScale = handleCalculateButtonZoom(contextInstance, delta, step);
1248
+ // stop execution when scale didn't change
1249
+ if (scale === newScale)
1250
+ return;
1251
+ handleCallback(getContext(contextInstance), event, onZoomStart);
1228
1252
  var mousePosition = getMousePosition(event, contentComponent, scale);
1229
1253
  var targetState = handleZoomToPoint(contextInstance, newScale, mousePosition.x, mousePosition.y);
1230
1254
  if (!targetState) {
1231
1255
  return console.error("Error during zoom event. New transformation state was not calculated.");
1232
1256
  }
1257
+ handleCallback(getContext(contextInstance), event, onZoom);
1233
1258
  animate(contextInstance, targetState, animationTime, animationType);
1259
+ handleDoubleClickStop(contextInstance, event);
1234
1260
  }
1235
1261
  var isDoubleClickAllowed = function (contextInstance, event) {
1236
1262
  var isInitialized = contextInstance.isInitialized, setup = contextInstance.setup, wrapperComponent = contextInstance.wrapperComponent;
@@ -1243,16 +1269,14 @@ var isDoubleClickAllowed = function (contextInstance, event) {
1243
1269
  var isExcluded = isExcludedNode(target, excluded);
1244
1270
  if (isExcluded)
1245
1271
  return false;
1246
- if (!isAllowed)
1247
- return false;
1248
1272
  return true;
1249
1273
  };
1250
1274
 
1251
1275
  var ZoomPanPinch = /** @class */ (function () {
1252
- function ZoomPanPinch(props, onChange) {
1276
+ function ZoomPanPinch(props) {
1253
1277
  var _this = this;
1254
- this.onChange = onChange;
1255
1278
  this.mounted = true;
1279
+ this.onChangeCallbacks = new Set();
1256
1280
  // Components
1257
1281
  this.wrapperComponent = null;
1258
1282
  this.contentComponent = null;
@@ -1273,6 +1297,8 @@ var ZoomPanPinch = /** @class */ (function () {
1273
1297
  this.pinchStartDistance = null;
1274
1298
  this.pinchStartScale = null;
1275
1299
  this.pinchMidpoint = null;
1300
+ // double click helpers
1301
+ this.doubleClickStopEventTimer = null;
1276
1302
  // velocity helpers
1277
1303
  this.velocity = null;
1278
1304
  this.velocityTime = null;
@@ -1529,7 +1555,6 @@ var ZoomPanPinch = /** @class */ (function () {
1529
1555
  return Boolean(keys.find(function (key) { return _this.pressedKeys[key]; }));
1530
1556
  };
1531
1557
  this.setTransformState = function (scale, positionX, positionY) {
1532
- var _a;
1533
1558
  var onTransformed = _this.props.onTransformed;
1534
1559
  if (!Number.isNaN(scale) &&
1535
1560
  !Number.isNaN(positionX) &&
@@ -1540,9 +1565,9 @@ var ZoomPanPinch = /** @class */ (function () {
1540
1565
  }
1541
1566
  _this.transformState.positionX = positionX;
1542
1567
  _this.transformState.positionY = positionY;
1543
- var ctx = getContext(_this);
1544
- (_a = _this.onChange) === null || _a === void 0 ? void 0 : _a.call(_this, ctx);
1545
- handleCallback(ctx, { scale: scale, positionX: positionX, positionY: positionY }, onTransformed);
1568
+ var ctx_1 = getContext(_this);
1569
+ _this.onChangeCallbacks.forEach(function (callback) { return callback(ctx_1); });
1570
+ handleCallback(ctx_1, { scale: scale, positionX: positionX, positionY: positionY }, onTransformed);
1546
1571
  _this.applyTransformation();
1547
1572
  }
1548
1573
  else {
@@ -1565,6 +1590,14 @@ var ZoomPanPinch = /** @class */ (function () {
1565
1590
  this.getContext = function () {
1566
1591
  return getContext(_this);
1567
1592
  };
1593
+ this.onChange = function (callback) {
1594
+ if (!_this.onChangeCallbacks.has(callback)) {
1595
+ _this.onChangeCallbacks.add(callback);
1596
+ }
1597
+ return function () {
1598
+ _this.onChangeCallbacks.delete(callback);
1599
+ };
1600
+ };
1568
1601
  /**
1569
1602
  * Initialization
1570
1603
  */
@@ -1586,30 +1619,31 @@ var ZoomPanPinch = /** @class */ (function () {
1586
1619
  return ZoomPanPinch;
1587
1620
  }());
1588
1621
 
1589
- var Context = React__default["default"].createContext(contextInitialState);
1622
+ var Context = React__default["default"].createContext(null);
1623
+ var getContent = function (children, ctx) {
1624
+ if (typeof children === "function") {
1625
+ return children(ctx);
1626
+ }
1627
+ return children;
1628
+ };
1590
1629
  var TransformWrapper = React__default["default"].forwardRef(function (props, ref) {
1591
1630
  var _a = React.useState(0), forceUpdate = _a[1];
1592
- var children = props.children, performance = props.performance;
1593
- var instance = React.useRef(new ZoomPanPinch(props, function () {
1594
- if (!performance) {
1595
- forceUpdate(function (prev) { return prev + 1; });
1596
- }
1597
- })).current;
1598
- var content = React.useMemo(function () {
1631
+ var children = props.children;
1632
+ var instance = React.useRef(new ZoomPanPinch(props)).current;
1633
+ var content = getContent(props.children, getContext(instance));
1634
+ var handleOnChange = React.useCallback(function () {
1599
1635
  if (typeof children === "function") {
1600
- return children(getContext(instance));
1636
+ forceUpdate(function (prev) { return prev + 1; });
1601
1637
  }
1602
- return children;
1603
- }, [children, instance]);
1604
- var value = React.useMemo(function () {
1605
- return __assign(__assign({}, instance.transformState), { setComponents: instance.init, contextInstance: instance });
1606
- // eslint-disable-next-line react-hooks/exhaustive-deps
1607
- }, [JSON.stringify(instance.transformState)]);
1638
+ }, [children]);
1608
1639
  React.useImperativeHandle(ref, function () { return getContext(instance); }, [instance]);
1609
1640
  React.useEffect(function () {
1610
1641
  instance.update(props);
1611
1642
  }, [instance, props]);
1612
- return React__default["default"].createElement(Context.Provider, { value: value }, content);
1643
+ React.useEffect(function () {
1644
+ return instance.onChange(handleOnChange);
1645
+ }, [instance, props, handleOnChange]);
1646
+ return React__default["default"].createElement(Context.Provider, { value: instance }, content);
1613
1647
  });
1614
1648
 
1615
1649
  function styleInject(css, ref) {
@@ -1645,14 +1679,14 @@ styleInject(css_248z);
1645
1679
 
1646
1680
  var TransformComponent = function (_a) {
1647
1681
  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;
1648
- var setComponents = React.useContext(Context).setComponents;
1682
+ var init = React.useContext(Context).init;
1649
1683
  var wrapperRef = React.useRef(null);
1650
1684
  var contentRef = React.useRef(null);
1651
1685
  React.useEffect(function () {
1652
1686
  var wrapper = wrapperRef.current;
1653
1687
  var content = contentRef.current;
1654
- if (wrapper !== null && content !== null && setComponents) {
1655
- setComponents(wrapper, content);
1688
+ if (wrapper !== null && content !== null && init) {
1689
+ init(wrapper, content);
1656
1690
  }
1657
1691
  // eslint-disable-next-line react-hooks/exhaustive-deps
1658
1692
  }, []);
@@ -1662,10 +1696,16 @@ var TransformComponent = function (_a) {
1662
1696
 
1663
1697
  var useTransformContext = function () {
1664
1698
  var libraryContext = React.useContext(Context);
1665
- if (!libraryContext.contextInstance) {
1699
+ var _a = React.useState(getContext(libraryContext || {})), values = _a[0], setValues = _a[1];
1700
+ if (!libraryContext) {
1666
1701
  throw new Error("Transform context mus be placed inside TransformWrapper");
1667
1702
  }
1668
- return getContext(libraryContext.contextInstance);
1703
+ React.useEffect(function () {
1704
+ libraryContext.onChange(function (ref) {
1705
+ setValues(ref);
1706
+ });
1707
+ }, [libraryContext]);
1708
+ return values;
1669
1709
  };
1670
1710
 
1671
1711
  exports.TransformComponent = TransformComponent;