react-zoom-pan-pinch 2.3.1 → 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/components/transform-component.d.ts +2 -0
- package/dist/components/transform-wrapper.d.ts +1 -3
- package/dist/constants/state.constants.d.ts +1 -4
- package/dist/core/double-click/double-click.logic.d.ts +2 -0
- package/dist/core/handlers/handlers.utils.d.ts +1 -1
- package/dist/core/instance.core.d.ts +5 -2
- package/dist/index.cjs.js +114 -64
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.d.ts +72 -67
- package/dist/index.esm.js +115 -65
- package/dist/index.esm.js.map +1 -1
- package/package.json +3 -2
- package/src/components/transform-component.tsx +10 -3
- package/src/components/transform-wrapper.tsx +29 -17
- package/src/constants/state.constants.ts +1 -15
- package/src/core/animations/animations.utils.ts +1 -0
- package/src/core/double-click/double-click.logic.ts +55 -8
- package/src/core/handlers/handlers.utils.ts +2 -0
- package/src/core/instance.core.ts +18 -5
- package/src/hooks/use-transform-context.tsx +10 -3
- package/src/stories/examples/controls/controls.stories.mdx +1 -0
package/dist/index.esm.js
CHANGED
|
@@ -1,40 +1,4 @@
|
|
|
1
|
-
import React, { useRef,
|
|
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,13 +586,48 @@ 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
633
|
minPositionX: null,
|
|
@@ -753,7 +752,7 @@ function handleZoomToViewCenter(contextInstance, delta, step, animationTime, ani
|
|
|
753
752
|
}
|
|
754
753
|
animate(contextInstance, targetState, animationTime, animationType);
|
|
755
754
|
}
|
|
756
|
-
function resetTransformations(contextInstance, animationTime, animationType) {
|
|
755
|
+
function resetTransformations(contextInstance, animationTime, animationType, onResetTransformation) {
|
|
757
756
|
var setup = contextInstance.setup, wrapperComponent = contextInstance.wrapperComponent;
|
|
758
757
|
var limitToBounds = setup.limitToBounds;
|
|
759
758
|
var initialTransformation = createState(contextInstance.props);
|
|
@@ -772,6 +771,7 @@ function resetTransformations(contextInstance, animationTime, animationType) {
|
|
|
772
771
|
positionY === initialTransformation.positionY) {
|
|
773
772
|
return;
|
|
774
773
|
}
|
|
774
|
+
onResetTransformation === null || onResetTransformation === void 0 ? void 0 : onResetTransformation();
|
|
775
775
|
animate(contextInstance, newState, animationTime, animationType);
|
|
776
776
|
}
|
|
777
777
|
function getOffset(element) {
|
|
@@ -1203,25 +1203,52 @@ var handlePinchStop = function (contextInstance) {
|
|
|
1203
1203
|
handleAlignToScaleBounds(contextInstance, pinchMidpoint === null || pinchMidpoint === void 0 ? void 0 : pinchMidpoint.x, pinchMidpoint === null || pinchMidpoint === void 0 ? void 0 : pinchMidpoint.y);
|
|
1204
1204
|
};
|
|
1205
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
|
+
};
|
|
1206
1224
|
function handleDoubleClick(contextInstance, event) {
|
|
1207
|
-
var
|
|
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;
|
|
1208
1229
|
if (disabled)
|
|
1209
1230
|
return;
|
|
1231
|
+
if (doubleClickStopEventTimer)
|
|
1232
|
+
return;
|
|
1210
1233
|
if (mode === "reset") {
|
|
1211
|
-
return
|
|
1234
|
+
return handleDoubleClickResetMode(contextInstance, event);
|
|
1212
1235
|
}
|
|
1213
|
-
var scale = contextInstance.transformState.scale;
|
|
1214
|
-
var contentComponent = contextInstance.contentComponent;
|
|
1215
1236
|
if (!contentComponent)
|
|
1216
1237
|
return console.error("No ContentComponent found");
|
|
1217
1238
|
var delta = mode === "zoomOut" ? -1 : 1;
|
|
1218
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);
|
|
1219
1244
|
var mousePosition = getMousePosition(event, contentComponent, scale);
|
|
1220
1245
|
var targetState = handleZoomToPoint(contextInstance, newScale, mousePosition.x, mousePosition.y);
|
|
1221
1246
|
if (!targetState) {
|
|
1222
1247
|
return console.error("Error during zoom event. New transformation state was not calculated.");
|
|
1223
1248
|
}
|
|
1249
|
+
handleCallback(getContext(contextInstance), event, onZoom);
|
|
1224
1250
|
animate(contextInstance, targetState, animationTime, animationType);
|
|
1251
|
+
handleDoubleClickStop(contextInstance, event);
|
|
1225
1252
|
}
|
|
1226
1253
|
var isDoubleClickAllowed = function (contextInstance, event) {
|
|
1227
1254
|
var isInitialized = contextInstance.isInitialized, setup = contextInstance.setup, wrapperComponent = contextInstance.wrapperComponent;
|
|
@@ -1234,8 +1261,6 @@ var isDoubleClickAllowed = function (contextInstance, event) {
|
|
|
1234
1261
|
var isExcluded = isExcludedNode(target, excluded);
|
|
1235
1262
|
if (isExcluded)
|
|
1236
1263
|
return false;
|
|
1237
|
-
if (!isAllowed)
|
|
1238
|
-
return false;
|
|
1239
1264
|
return true;
|
|
1240
1265
|
};
|
|
1241
1266
|
|
|
@@ -1243,6 +1268,7 @@ var ZoomPanPinch = /** @class */ (function () {
|
|
|
1243
1268
|
function ZoomPanPinch(props) {
|
|
1244
1269
|
var _this = this;
|
|
1245
1270
|
this.mounted = true;
|
|
1271
|
+
this.onChangeCallbacks = new Set();
|
|
1246
1272
|
// Components
|
|
1247
1273
|
this.wrapperComponent = null;
|
|
1248
1274
|
this.contentComponent = null;
|
|
@@ -1263,6 +1289,8 @@ var ZoomPanPinch = /** @class */ (function () {
|
|
|
1263
1289
|
this.pinchStartDistance = null;
|
|
1264
1290
|
this.pinchStartScale = null;
|
|
1265
1291
|
this.pinchMidpoint = null;
|
|
1292
|
+
// double click helpers
|
|
1293
|
+
this.doubleClickStopEventTimer = null;
|
|
1266
1294
|
// velocity helpers
|
|
1267
1295
|
this.velocity = null;
|
|
1268
1296
|
this.velocityTime = null;
|
|
@@ -1529,7 +1557,9 @@ var ZoomPanPinch = /** @class */ (function () {
|
|
|
1529
1557
|
}
|
|
1530
1558
|
_this.transformState.positionX = positionX;
|
|
1531
1559
|
_this.transformState.positionY = positionY;
|
|
1532
|
-
|
|
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);
|
|
1533
1563
|
_this.applyTransformation();
|
|
1534
1564
|
}
|
|
1535
1565
|
else {
|
|
@@ -1552,6 +1582,14 @@ var ZoomPanPinch = /** @class */ (function () {
|
|
|
1552
1582
|
this.getContext = function () {
|
|
1553
1583
|
return getContext(_this);
|
|
1554
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
|
+
};
|
|
1555
1593
|
/**
|
|
1556
1594
|
* Initialization
|
|
1557
1595
|
*/
|
|
@@ -1573,25 +1611,31 @@ var ZoomPanPinch = /** @class */ (function () {
|
|
|
1573
1611
|
return ZoomPanPinch;
|
|
1574
1612
|
}());
|
|
1575
1613
|
|
|
1576
|
-
var Context = React.createContext(
|
|
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
|
+
};
|
|
1577
1621
|
var TransformWrapper = React.forwardRef(function (props, ref) {
|
|
1622
|
+
var _a = useState(0), forceUpdate = _a[1];
|
|
1578
1623
|
var children = props.children;
|
|
1579
1624
|
var instance = useRef(new ZoomPanPinch(props)).current;
|
|
1580
|
-
var content =
|
|
1625
|
+
var content = getContent(props.children, getContext(instance));
|
|
1626
|
+
var handleOnChange = useCallback(function () {
|
|
1581
1627
|
if (typeof children === "function") {
|
|
1582
|
-
|
|
1628
|
+
forceUpdate(function (prev) { return prev + 1; });
|
|
1583
1629
|
}
|
|
1584
|
-
|
|
1585
|
-
}, [children, instance]);
|
|
1586
|
-
var value = useMemo(function () {
|
|
1587
|
-
return __assign(__assign({}, instance.transformState), { setComponents: instance.init, contextInstance: instance });
|
|
1588
|
-
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
1589
|
-
}, [JSON.stringify(instance.transformState)]);
|
|
1630
|
+
}, [children]);
|
|
1590
1631
|
useImperativeHandle(ref, function () { return getContext(instance); }, [instance]);
|
|
1591
1632
|
useEffect(function () {
|
|
1592
1633
|
instance.update(props);
|
|
1593
1634
|
}, [instance, props]);
|
|
1594
|
-
|
|
1635
|
+
useEffect(function () {
|
|
1636
|
+
return instance.onChange(handleOnChange);
|
|
1637
|
+
}, [instance, props, handleOnChange]);
|
|
1638
|
+
return React.createElement(Context.Provider, { value: instance }, content);
|
|
1595
1639
|
});
|
|
1596
1640
|
|
|
1597
1641
|
function styleInject(css, ref) {
|
|
@@ -1625,29 +1669,35 @@ var css_248z = ".transform-component-module_wrapper__7HFJe {\n position: relati
|
|
|
1625
1669
|
var styles = {"wrapper":"transform-component-module_wrapper__7HFJe","content":"transform-component-module_content__uCDPE"};
|
|
1626
1670
|
styleInject(css_248z);
|
|
1627
1671
|
|
|
1628
|
-
/* eslint-disable react/require-default-props */
|
|
1629
1672
|
var TransformComponent = function (_a) {
|
|
1630
|
-
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;
|
|
1631
|
-
var
|
|
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;
|
|
1674
|
+
var init = useContext(Context).init;
|
|
1632
1675
|
var wrapperRef = useRef(null);
|
|
1633
1676
|
var contentRef = useRef(null);
|
|
1634
1677
|
useEffect(function () {
|
|
1635
1678
|
var wrapper = wrapperRef.current;
|
|
1636
1679
|
var content = contentRef.current;
|
|
1637
|
-
if (wrapper !== null && content !== null &&
|
|
1638
|
-
|
|
1680
|
+
if (wrapper !== null && content !== null && init) {
|
|
1681
|
+
init(wrapper, content);
|
|
1639
1682
|
}
|
|
1683
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
1640
1684
|
}, []);
|
|
1641
|
-
return (React.createElement("div", { ref: wrapperRef, className: "react-transform-wrapper ".concat(styles.wrapper, " ").concat(wrapperClass), style: wrapperStyle },
|
|
1642
|
-
React.createElement("div", { ref: contentRef, className: "react-transform-component ".concat(styles.content, " ").concat(contentClass), style: contentStyle }, children)));
|
|
1685
|
+
return (React.createElement("div", __assign({}, wrapperProps, { ref: wrapperRef, className: "react-transform-wrapper ".concat(styles.wrapper, " ").concat(wrapperClass), style: wrapperStyle }),
|
|
1686
|
+
React.createElement("div", __assign({}, contentProps, { ref: contentRef, className: "react-transform-component ".concat(styles.content, " ").concat(contentClass), style: contentStyle }), children)));
|
|
1643
1687
|
};
|
|
1644
1688
|
|
|
1645
1689
|
var useTransformContext = function () {
|
|
1646
1690
|
var libraryContext = useContext(Context);
|
|
1647
|
-
|
|
1691
|
+
var _a = useState(getContext(libraryContext || {})), values = _a[0], setValues = _a[1];
|
|
1692
|
+
if (!libraryContext) {
|
|
1648
1693
|
throw new Error("Transform context mus be placed inside TransformWrapper");
|
|
1649
1694
|
}
|
|
1650
|
-
|
|
1695
|
+
useEffect(function () {
|
|
1696
|
+
libraryContext.onChange(function (ref) {
|
|
1697
|
+
setValues(ref);
|
|
1698
|
+
});
|
|
1699
|
+
}, [libraryContext]);
|
|
1700
|
+
return values;
|
|
1651
1701
|
};
|
|
1652
1702
|
|
|
1653
1703
|
export { TransformComponent, TransformWrapper, useTransformContext };
|