react-zoom-pan-pinch 2.1.2 → 2.2.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/CHANGELOG.md +14 -0
- package/README.md +43 -0
- package/dist/index.cjs.js +87 -60
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.d.ts +11 -2
- package/dist/index.esm.js +87 -60
- package/dist/index.esm.js.map +1 -1
- package/package.json +13 -12
- package/src/.eslintrc.js +36 -0
- package/src/components/transform-component.module.css +30 -0
- package/src/components/transform-component.tsx +52 -0
- package/src/components/transform-context.tsx +493 -0
- package/src/components/transform-wrapper.tsx +18 -0
- package/src/constants/state.constants.ts +88 -0
- package/src/core/animations/animations.constants.ts +76 -0
- package/src/core/animations/animations.utils.ts +109 -0
- package/src/core/bounds/bounds.types.ts +8 -0
- package/src/core/bounds/bounds.utils.ts +157 -0
- package/src/core/double-click/double-click.logic.ts +75 -0
- package/src/core/handlers/handlers.logic.ts +112 -0
- package/src/core/handlers/handlers.utils.ts +202 -0
- package/src/core/pan/panning.logic.ts +99 -0
- package/src/core/pan/panning.utils.ts +199 -0
- package/src/core/pan/velocity.logic.ts +148 -0
- package/src/core/pan/velocity.utils.ts +79 -0
- package/src/core/pinch/pinch.logic.ts +83 -0
- package/src/core/pinch/pinch.utils.ts +88 -0
- package/src/core/wheel/wheel.logic.ts +118 -0
- package/src/core/wheel/wheel.utils.ts +121 -0
- package/src/core/zoom/zoom.logic.ts +69 -0
- package/src/core/zoom/zoom.utils.ts +58 -0
- package/src/hooks/use-transform-context.tsx +16 -0
- package/src/index.ts +6 -0
- package/src/models/calculations.model.ts +26 -0
- package/src/models/context.model.ts +179 -0
- package/src/models/helpers.model.ts +15 -0
- package/src/models/index.ts +3 -0
- package/src/react-app-env.d.ts +1 -0
- package/src/stories/assets/big-image.jpeg +0 -0
- package/src/stories/assets/medium-image.jpg +0 -0
- package/src/stories/assets/small-image.jpg +0 -0
- package/src/stories/assets/svg-example.svg +14 -0
- package/src/stories/docs/Introduction.stories.mdx +97 -0
- package/src/stories/docs/handlers.stories.mdx +48 -0
- package/src/stories/docs/handlers.tsx +107 -0
- package/src/stories/docs/props.stories.mdx +92 -0
- package/src/stories/docs/props.tsx +490 -0
- package/src/stories/examples/content/content.stories.mdx +122 -0
- package/src/stories/examples/content-rerendering/content-updating.stories.mdx +42 -0
- package/src/stories/examples/content-rerendering/example.tsx +74 -0
- package/src/stories/examples/controls/controls.stories.mdx +71 -0
- package/src/stories/examples/image-big/image-big.stories.mdx +44 -0
- package/src/stories/examples/image-medium/image-medium.stories.mdx +42 -0
- package/src/stories/examples/image-small/image-small.stories.mdx +42 -0
- package/src/stories/examples/image-zoomed-out/image-zoomed-out.stories.mdx +47 -0
- package/src/stories/examples/zoom-to-element/example.tsx +91 -0
- package/src/stories/examples/zoom-to-element/zoom-to-element.stories.mdx +28 -0
- package/src/stories/types/args.types.ts +201 -0
- package/src/stories/utils/args.utils.ts +34 -0
- package/src/stories/utils/code.utils.tsx +21 -0
- package/src/stories/utils/controls.utils.tsx +26 -0
- package/src/stories/utils/index.ts +3 -0
- package/src/stories/utils/styles.module.css +29 -0
- package/src/styles.css +8 -0
- package/src/typings.d.ts +10 -0
- package/src/utils/calculations.utils.ts +16 -0
- package/src/utils/callback.utils.ts +11 -0
- package/src/utils/context.utils.ts +27 -0
- package/src/utils/event.utils.ts +22 -0
- package/src/utils/helpers.utils.ts +27 -0
- package/src/utils/index.ts +7 -0
- package/src/utils/state.utils.ts +40 -0
- package/src/utils/styles.utils.ts +27 -0
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import { ReactZoomPanPinchContext } from "../../models";
|
|
2
|
+
import { handleCallback } from "../../utils/callback.utils";
|
|
3
|
+
import { getContext } from "../../utils/context.utils";
|
|
4
|
+
import { cancelTimeout } from "../../utils/helpers.utils";
|
|
5
|
+
import { handleCancelAnimation } from "../animations/animations.utils";
|
|
6
|
+
import { handleCalculateBounds } from "../bounds/bounds.utils";
|
|
7
|
+
import {
|
|
8
|
+
getDelta,
|
|
9
|
+
handleCalculateWheelZoom,
|
|
10
|
+
handleWheelZoomStop,
|
|
11
|
+
getMousePosition,
|
|
12
|
+
} from "./wheel.utils";
|
|
13
|
+
import { handleAlignToScaleBounds } from "../zoom/zoom.logic";
|
|
14
|
+
import { handleCalculateZoomPositions } from "../zoom/zoom.utils";
|
|
15
|
+
|
|
16
|
+
const wheelStopEventTime = 160;
|
|
17
|
+
const wheelAnimationTime = 100;
|
|
18
|
+
|
|
19
|
+
export const handleWheelStart = (
|
|
20
|
+
contextInstance: ReactZoomPanPinchContext,
|
|
21
|
+
event: WheelEvent,
|
|
22
|
+
): void => {
|
|
23
|
+
const { onWheelStart, onZoomStart } = contextInstance.props;
|
|
24
|
+
|
|
25
|
+
if (!contextInstance.wheelStopEventTimer) {
|
|
26
|
+
handleCancelAnimation(contextInstance);
|
|
27
|
+
handleCallback(getContext(contextInstance), event, onWheelStart);
|
|
28
|
+
handleCallback(getContext(contextInstance), event, onZoomStart);
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
export const handleWheelZoom = (
|
|
33
|
+
contextInstance: ReactZoomPanPinchContext,
|
|
34
|
+
event: WheelEvent,
|
|
35
|
+
): void => {
|
|
36
|
+
const { onWheel, onZoom } = contextInstance.props;
|
|
37
|
+
|
|
38
|
+
const { contentComponent, setup, transformState } = contextInstance;
|
|
39
|
+
const { scale } = transformState;
|
|
40
|
+
const {
|
|
41
|
+
limitToBounds,
|
|
42
|
+
centerZoomedOut,
|
|
43
|
+
zoomAnimation,
|
|
44
|
+
wheel,
|
|
45
|
+
disablePadding,
|
|
46
|
+
} = setup;
|
|
47
|
+
const { size, disabled } = zoomAnimation;
|
|
48
|
+
const { step } = wheel;
|
|
49
|
+
|
|
50
|
+
if (!contentComponent) {
|
|
51
|
+
throw new Error("Component not mounted");
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
event.preventDefault();
|
|
55
|
+
event.stopPropagation();
|
|
56
|
+
|
|
57
|
+
const delta = getDelta(event, null);
|
|
58
|
+
const newScale = handleCalculateWheelZoom(
|
|
59
|
+
contextInstance,
|
|
60
|
+
delta,
|
|
61
|
+
step,
|
|
62
|
+
!event.ctrlKey,
|
|
63
|
+
);
|
|
64
|
+
|
|
65
|
+
// if scale not change
|
|
66
|
+
if (scale === newScale) return;
|
|
67
|
+
|
|
68
|
+
const bounds = handleCalculateBounds(contextInstance, newScale);
|
|
69
|
+
|
|
70
|
+
const mousePosition = getMousePosition(event, contentComponent, scale);
|
|
71
|
+
|
|
72
|
+
const isPaddingDisabled =
|
|
73
|
+
disabled || size === 0 || centerZoomedOut || disablePadding;
|
|
74
|
+
const isLimitedToBounds = limitToBounds && isPaddingDisabled;
|
|
75
|
+
|
|
76
|
+
const { x, y } = handleCalculateZoomPositions(
|
|
77
|
+
contextInstance,
|
|
78
|
+
mousePosition.x,
|
|
79
|
+
mousePosition.y,
|
|
80
|
+
newScale,
|
|
81
|
+
bounds,
|
|
82
|
+
isLimitedToBounds,
|
|
83
|
+
);
|
|
84
|
+
|
|
85
|
+
contextInstance.previousWheelEvent = event;
|
|
86
|
+
|
|
87
|
+
contextInstance.setTransformState(newScale, x, y);
|
|
88
|
+
|
|
89
|
+
handleCallback(getContext(contextInstance), event, onWheel);
|
|
90
|
+
handleCallback(getContext(contextInstance), event, onZoom);
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
export const handleWheelStop = (
|
|
94
|
+
contextInstance: ReactZoomPanPinchContext,
|
|
95
|
+
event: WheelEvent,
|
|
96
|
+
): void => {
|
|
97
|
+
const { onWheelStop, onZoomStop } = contextInstance.props;
|
|
98
|
+
|
|
99
|
+
// fire animation
|
|
100
|
+
cancelTimeout(contextInstance.wheelAnimationTimer);
|
|
101
|
+
contextInstance.wheelAnimationTimer = setTimeout(() => {
|
|
102
|
+
if (!contextInstance.mounted) return;
|
|
103
|
+
handleAlignToScaleBounds(contextInstance, event.x, event.y);
|
|
104
|
+
contextInstance.wheelAnimationTimer = null;
|
|
105
|
+
}, wheelAnimationTime);
|
|
106
|
+
|
|
107
|
+
// Wheel stop event
|
|
108
|
+
const hasStoppedZooming = handleWheelZoomStop(contextInstance, event);
|
|
109
|
+
if (hasStoppedZooming) {
|
|
110
|
+
cancelTimeout(contextInstance.wheelStopEventTimer);
|
|
111
|
+
contextInstance.wheelStopEventTimer = setTimeout(() => {
|
|
112
|
+
if (!contextInstance.mounted) return;
|
|
113
|
+
contextInstance.wheelStopEventTimer = null;
|
|
114
|
+
handleCallback(getContext(contextInstance), event, onWheelStop);
|
|
115
|
+
handleCallback(getContext(contextInstance), event, onZoomStop);
|
|
116
|
+
}, wheelStopEventTime);
|
|
117
|
+
}
|
|
118
|
+
};
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import { ReactZoomPanPinchContext } from "../../models";
|
|
2
|
+
import { PositionType } from "../../models";
|
|
3
|
+
|
|
4
|
+
import { checkIsNumber, isExcludedNode, roundNumber } from "../../utils";
|
|
5
|
+
import { checkZoomBounds } from "../zoom/zoom.utils";
|
|
6
|
+
|
|
7
|
+
export const isWheelAllowed = (
|
|
8
|
+
contextInstance: ReactZoomPanPinchContext,
|
|
9
|
+
event: WheelEvent,
|
|
10
|
+
): boolean => {
|
|
11
|
+
const {
|
|
12
|
+
disabled,
|
|
13
|
+
wheelDisabled,
|
|
14
|
+
touchPadDisabled,
|
|
15
|
+
excluded,
|
|
16
|
+
} = contextInstance.setup.wheel;
|
|
17
|
+
const { isInitialized, isPanning } = contextInstance;
|
|
18
|
+
|
|
19
|
+
const target = event.target as HTMLElement;
|
|
20
|
+
const isAllowed = isInitialized && !isPanning && !disabled && target;
|
|
21
|
+
|
|
22
|
+
if (!isAllowed) return false;
|
|
23
|
+
// Event ctrlKey detects if touchpad action is executing wheel or pinch gesture
|
|
24
|
+
if (wheelDisabled && !event.ctrlKey) return false;
|
|
25
|
+
if (touchPadDisabled && event.ctrlKey) return false;
|
|
26
|
+
|
|
27
|
+
const isExcluded = isExcludedNode(target, excluded);
|
|
28
|
+
|
|
29
|
+
if (isExcluded) return false;
|
|
30
|
+
|
|
31
|
+
return true;
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
export function getDelta(
|
|
35
|
+
event: WheelEvent,
|
|
36
|
+
customDelta?: number | null,
|
|
37
|
+
): number {
|
|
38
|
+
const deltaY = event ? (event.deltaY < 0 ? 1 : -1) : 0;
|
|
39
|
+
const delta = checkIsNumber(customDelta, deltaY);
|
|
40
|
+
return delta;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export function getMousePosition(
|
|
44
|
+
event: WheelEvent | MouseEvent | TouchEvent,
|
|
45
|
+
contentComponent: HTMLDivElement,
|
|
46
|
+
scale: number,
|
|
47
|
+
): PositionType {
|
|
48
|
+
const contentRect = contentComponent.getBoundingClientRect();
|
|
49
|
+
|
|
50
|
+
let mouseX = 0;
|
|
51
|
+
let mouseY = 0;
|
|
52
|
+
|
|
53
|
+
if ("clientX" in event) {
|
|
54
|
+
// mouse position x, y over wrapper component
|
|
55
|
+
mouseX = (event.clientX - contentRect.left) / scale;
|
|
56
|
+
mouseY = (event.clientY - contentRect.top) / scale;
|
|
57
|
+
} else {
|
|
58
|
+
const touch = event.touches[0];
|
|
59
|
+
mouseX = (touch.clientX - contentRect.left) / scale;
|
|
60
|
+
mouseY = (touch.clientY - contentRect.top) / scale;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
if (isNaN(mouseX) || isNaN(mouseY))
|
|
64
|
+
console.error("No mouse or touch offset found");
|
|
65
|
+
|
|
66
|
+
return {
|
|
67
|
+
x: mouseX,
|
|
68
|
+
y: mouseY,
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export const handleCalculateWheelZoom = (
|
|
73
|
+
contextInstance: ReactZoomPanPinchContext,
|
|
74
|
+
delta: number,
|
|
75
|
+
step: number,
|
|
76
|
+
disable: boolean,
|
|
77
|
+
getTarget?: boolean,
|
|
78
|
+
): number => {
|
|
79
|
+
const { scale } = contextInstance.transformState;
|
|
80
|
+
const { wrapperComponent, setup } = contextInstance;
|
|
81
|
+
const { maxScale, minScale, zoomAnimation, disablePadding } = setup;
|
|
82
|
+
const { size, disabled } = zoomAnimation;
|
|
83
|
+
|
|
84
|
+
if (!wrapperComponent) {
|
|
85
|
+
throw new Error("Wrapper is not mounted");
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const targetScale = scale + delta * (scale - scale * step) * step;
|
|
89
|
+
|
|
90
|
+
if (getTarget) return targetScale;
|
|
91
|
+
const paddingEnabled = disable ? false : !disabled;
|
|
92
|
+
const newScale = checkZoomBounds(
|
|
93
|
+
roundNumber(targetScale, 3),
|
|
94
|
+
minScale,
|
|
95
|
+
maxScale,
|
|
96
|
+
size,
|
|
97
|
+
paddingEnabled && !disablePadding,
|
|
98
|
+
);
|
|
99
|
+
return newScale;
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
export const handleWheelZoomStop = (
|
|
103
|
+
contextInstance: ReactZoomPanPinchContext,
|
|
104
|
+
event: WheelEvent,
|
|
105
|
+
): boolean => {
|
|
106
|
+
const { previousWheelEvent } = contextInstance;
|
|
107
|
+
const { scale } = contextInstance.transformState;
|
|
108
|
+
const { maxScale, minScale } = contextInstance.setup;
|
|
109
|
+
|
|
110
|
+
if (!previousWheelEvent) return false;
|
|
111
|
+
if (scale < maxScale || scale > minScale) return true;
|
|
112
|
+
if (Math.sign(previousWheelEvent.deltaY) !== Math.sign(event.deltaY))
|
|
113
|
+
return true;
|
|
114
|
+
if (previousWheelEvent.deltaY > 0 && previousWheelEvent.deltaY < event.deltaY)
|
|
115
|
+
return true;
|
|
116
|
+
if (previousWheelEvent.deltaY < 0 && previousWheelEvent.deltaY > event.deltaY)
|
|
117
|
+
return true;
|
|
118
|
+
if (Math.sign(previousWheelEvent.deltaY) !== Math.sign(event.deltaY))
|
|
119
|
+
return true;
|
|
120
|
+
return false;
|
|
121
|
+
};
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { ReactZoomPanPinchContext, ReactZoomPanPinchState } from "../../models";
|
|
2
|
+
import { roundNumber } from "../../utils";
|
|
3
|
+
import { animate } from "../animations/animations.utils";
|
|
4
|
+
import { handleCalculateBounds } from "../bounds/bounds.utils";
|
|
5
|
+
import { handleAlignToBounds } from "../pan/panning.logic";
|
|
6
|
+
import { checkZoomBounds, handleCalculateZoomPositions } from "./zoom.utils";
|
|
7
|
+
|
|
8
|
+
export function handleAlignToScaleBounds(
|
|
9
|
+
contextInstance: ReactZoomPanPinchContext,
|
|
10
|
+
mousePositionX?: number,
|
|
11
|
+
mousePositionY?: number,
|
|
12
|
+
): void {
|
|
13
|
+
const { scale } = contextInstance.transformState;
|
|
14
|
+
const { wrapperComponent } = contextInstance;
|
|
15
|
+
const { minScale, limitToBounds, zoomAnimation } = contextInstance.setup;
|
|
16
|
+
const { disabled, animationTime, animationType } = zoomAnimation;
|
|
17
|
+
|
|
18
|
+
const isDisabled = disabled || scale >= minScale;
|
|
19
|
+
|
|
20
|
+
if (scale >= 1 || limitToBounds) {
|
|
21
|
+
// fire fit to bounds animation
|
|
22
|
+
handleAlignToBounds(contextInstance);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
if (isDisabled || !wrapperComponent || !contextInstance.mounted) return;
|
|
26
|
+
|
|
27
|
+
const mouseX = mousePositionX || wrapperComponent.offsetWidth / 2;
|
|
28
|
+
const mouseY = mousePositionY || wrapperComponent.offsetHeight / 2;
|
|
29
|
+
|
|
30
|
+
const targetState = handleZoomToPoint(
|
|
31
|
+
contextInstance,
|
|
32
|
+
minScale,
|
|
33
|
+
mouseX,
|
|
34
|
+
mouseY,
|
|
35
|
+
);
|
|
36
|
+
|
|
37
|
+
if (targetState) {
|
|
38
|
+
animate(contextInstance, targetState, animationTime, animationType);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export function handleZoomToPoint(
|
|
43
|
+
contextInstance: ReactZoomPanPinchContext,
|
|
44
|
+
scale: number,
|
|
45
|
+
mouseX: number,
|
|
46
|
+
mouseY: number,
|
|
47
|
+
): Omit<ReactZoomPanPinchState, "previousScale"> | undefined {
|
|
48
|
+
const { minScale, maxScale, limitToBounds } = contextInstance.setup;
|
|
49
|
+
|
|
50
|
+
const newScale = checkZoomBounds(
|
|
51
|
+
roundNumber(scale, 2),
|
|
52
|
+
minScale,
|
|
53
|
+
maxScale,
|
|
54
|
+
0,
|
|
55
|
+
false,
|
|
56
|
+
);
|
|
57
|
+
const bounds = handleCalculateBounds(contextInstance, newScale);
|
|
58
|
+
|
|
59
|
+
const { x, y } = handleCalculateZoomPositions(
|
|
60
|
+
contextInstance,
|
|
61
|
+
mouseX,
|
|
62
|
+
mouseY,
|
|
63
|
+
newScale,
|
|
64
|
+
bounds,
|
|
65
|
+
limitToBounds,
|
|
66
|
+
);
|
|
67
|
+
|
|
68
|
+
return { scale: newScale, positionX: x, positionY: y };
|
|
69
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import {
|
|
2
|
+
BoundsType,
|
|
3
|
+
PositionType,
|
|
4
|
+
ReactZoomPanPinchContext,
|
|
5
|
+
} from "../../models";
|
|
6
|
+
import { getMouseBoundedPosition } from "../bounds/bounds.utils";
|
|
7
|
+
|
|
8
|
+
export function handleCalculateZoomPositions(
|
|
9
|
+
contextInstance: ReactZoomPanPinchContext,
|
|
10
|
+
mouseX: number,
|
|
11
|
+
mouseY: number,
|
|
12
|
+
newScale: number,
|
|
13
|
+
bounds: BoundsType,
|
|
14
|
+
limitToBounds: boolean,
|
|
15
|
+
): PositionType {
|
|
16
|
+
const { scale, positionX, positionY } = contextInstance.transformState;
|
|
17
|
+
|
|
18
|
+
const scaleDifference = newScale - scale;
|
|
19
|
+
|
|
20
|
+
if (typeof mouseX !== "number" || typeof mouseY !== "number") {
|
|
21
|
+
console.error("Mouse X and Y position were not provided!");
|
|
22
|
+
return { x: positionX, y: positionY };
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const calculatedPositionX = positionX - mouseX * scaleDifference;
|
|
26
|
+
const calculatedPositionY = positionY - mouseY * scaleDifference;
|
|
27
|
+
|
|
28
|
+
// do not limit to bounds when there is padding animation,
|
|
29
|
+
// it causes animation strange behaviour
|
|
30
|
+
|
|
31
|
+
const newPositions = getMouseBoundedPosition(
|
|
32
|
+
calculatedPositionX,
|
|
33
|
+
calculatedPositionY,
|
|
34
|
+
bounds,
|
|
35
|
+
limitToBounds,
|
|
36
|
+
0,
|
|
37
|
+
0,
|
|
38
|
+
null,
|
|
39
|
+
);
|
|
40
|
+
|
|
41
|
+
return newPositions;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export function checkZoomBounds(
|
|
45
|
+
zoom: number,
|
|
46
|
+
minScale: number,
|
|
47
|
+
maxScale: number,
|
|
48
|
+
zoomPadding: number,
|
|
49
|
+
enablePadding: boolean,
|
|
50
|
+
): number {
|
|
51
|
+
const scalePadding = enablePadding ? zoomPadding : 0;
|
|
52
|
+
const minScaleWithPadding = minScale - scalePadding;
|
|
53
|
+
|
|
54
|
+
if (!isNaN(maxScale) && zoom >= maxScale) return maxScale;
|
|
55
|
+
if (!isNaN(minScale) && zoom <= minScaleWithPadding)
|
|
56
|
+
return minScaleWithPadding;
|
|
57
|
+
return zoom;
|
|
58
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { useContext } from "react";
|
|
2
|
+
import { getContext } from "utils";
|
|
3
|
+
import { Context } from "../components/transform-context";
|
|
4
|
+
import { ReactZoomPanPinchRef } from "../models";
|
|
5
|
+
|
|
6
|
+
const useTransformContext = (): ReactZoomPanPinchRef => {
|
|
7
|
+
const libraryContext = useContext(Context);
|
|
8
|
+
|
|
9
|
+
if (!libraryContext.contextInstance) {
|
|
10
|
+
throw new Error("Transform context mus be placed inside TransformWrapper");
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
return getContext(libraryContext.contextInstance);
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
export default useTransformContext;
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export { default as TransformWrapper } from "./components/transform-wrapper";
|
|
2
|
+
export { default as TransformComponent } from "./components/transform-component";
|
|
3
|
+
export { default as useTransformContext } from "./hooks/use-transform-context";
|
|
4
|
+
|
|
5
|
+
export * from "./models/context.model";
|
|
6
|
+
export * from "./models/calculations.model";
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
export type SizeType = {
|
|
2
|
+
width: number;
|
|
3
|
+
height: number;
|
|
4
|
+
};
|
|
5
|
+
|
|
6
|
+
export type PositionType = {
|
|
7
|
+
x: number;
|
|
8
|
+
y: number;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
export type StateType = { scale: number; positionX: number; positionY: number };
|
|
12
|
+
|
|
13
|
+
export type VelocityType = {
|
|
14
|
+
velocityX: number;
|
|
15
|
+
velocityY: number;
|
|
16
|
+
total: number;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
export type BoundsType = {
|
|
20
|
+
minPositionX: number;
|
|
21
|
+
maxPositionX: number;
|
|
22
|
+
minPositionY: number;
|
|
23
|
+
maxPositionY: number;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
export type AnimationType = () => void | number;
|
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
|
|
3
|
+
import { TransformContext } from "../components/transform-context";
|
|
4
|
+
import { animations } from "../core/animations/animations.constants";
|
|
5
|
+
import { DeepNonNullable } from "./helpers.model";
|
|
6
|
+
import {
|
|
7
|
+
zoomIn,
|
|
8
|
+
zoomToElement,
|
|
9
|
+
centerView,
|
|
10
|
+
resetTransform,
|
|
11
|
+
setTransform,
|
|
12
|
+
zoomOut,
|
|
13
|
+
} from "../core/handlers/handlers.logic";
|
|
14
|
+
|
|
15
|
+
export type ReactZoomPanPinchContext = typeof TransformContext.prototype;
|
|
16
|
+
|
|
17
|
+
export type ReactZoomPanPinchRef = {
|
|
18
|
+
instance: ReactZoomPanPinchContext;
|
|
19
|
+
state: ReactZoomPanPinchState;
|
|
20
|
+
} & ReactZoomPanPinchHandlers;
|
|
21
|
+
|
|
22
|
+
export type ReactZoomPanPinchState = {
|
|
23
|
+
previousScale: number;
|
|
24
|
+
scale: number;
|
|
25
|
+
positionX: number;
|
|
26
|
+
positionY: number;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
export type ReactZoomPanPinchHandlers = {
|
|
30
|
+
zoomIn: ReturnType<typeof zoomIn>;
|
|
31
|
+
zoomOut: ReturnType<typeof zoomOut>;
|
|
32
|
+
setTransform: ReturnType<typeof setTransform>;
|
|
33
|
+
resetTransform: ReturnType<typeof resetTransform>;
|
|
34
|
+
centerView: ReturnType<typeof centerView>;
|
|
35
|
+
zoomToElement: ReturnType<typeof zoomToElement>;
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
export type ReactZoomPanPinchProps = {
|
|
39
|
+
children?: React.ReactNode | ((ref: ReactZoomPanPinchRef) => React.ReactNode);
|
|
40
|
+
ref?: React.Ref<ReactZoomPanPinchRef>;
|
|
41
|
+
initialScale?: number;
|
|
42
|
+
initialPositionX?: number;
|
|
43
|
+
initialPositionY?: number;
|
|
44
|
+
disabled?: boolean;
|
|
45
|
+
minPositionX?: null | number;
|
|
46
|
+
maxPositionX?: null | number;
|
|
47
|
+
minPositionY?: null | number;
|
|
48
|
+
maxPositionY?: null | number;
|
|
49
|
+
minScale?: number;
|
|
50
|
+
maxScale?: number;
|
|
51
|
+
limitToBounds?: boolean;
|
|
52
|
+
centerZoomedOut?: boolean;
|
|
53
|
+
centerOnInit?: boolean;
|
|
54
|
+
disablePadding?: boolean;
|
|
55
|
+
wheel?: {
|
|
56
|
+
step?: number;
|
|
57
|
+
disabled?: boolean;
|
|
58
|
+
wheelDisabled?: boolean;
|
|
59
|
+
touchPadDisabled?: boolean;
|
|
60
|
+
activationKeys?: string[];
|
|
61
|
+
excluded?: string[];
|
|
62
|
+
};
|
|
63
|
+
panning?: {
|
|
64
|
+
disabled?: boolean;
|
|
65
|
+
velocityDisabled?: boolean;
|
|
66
|
+
lockAxisX?: boolean;
|
|
67
|
+
lockAxisY?: boolean;
|
|
68
|
+
activationKeys?: string[];
|
|
69
|
+
excluded?: string[];
|
|
70
|
+
};
|
|
71
|
+
pinch?: {
|
|
72
|
+
step?: number;
|
|
73
|
+
disabled?: boolean;
|
|
74
|
+
excluded?: string[];
|
|
75
|
+
};
|
|
76
|
+
doubleClick?: {
|
|
77
|
+
disabled?: boolean;
|
|
78
|
+
step?: number;
|
|
79
|
+
mode?: "zoomIn" | "zoomOut" | "reset";
|
|
80
|
+
animationTime?: number;
|
|
81
|
+
animationType?: keyof typeof animations;
|
|
82
|
+
excluded?: string[];
|
|
83
|
+
};
|
|
84
|
+
zoomAnimation?: {
|
|
85
|
+
disabled?: boolean;
|
|
86
|
+
size?: number;
|
|
87
|
+
animationTime?: number;
|
|
88
|
+
animationType?: keyof typeof animations;
|
|
89
|
+
};
|
|
90
|
+
alignmentAnimation?: {
|
|
91
|
+
disabled?: boolean;
|
|
92
|
+
sizeX?: number;
|
|
93
|
+
sizeY?: number;
|
|
94
|
+
animationTime?: number;
|
|
95
|
+
velocityAlignmentTime?: number;
|
|
96
|
+
animationType?: keyof typeof animations;
|
|
97
|
+
};
|
|
98
|
+
velocityAnimation?: {
|
|
99
|
+
disabled?: boolean;
|
|
100
|
+
sensitivity?: number;
|
|
101
|
+
animationTime?: number;
|
|
102
|
+
animationType?: keyof typeof animations;
|
|
103
|
+
equalToMove?: boolean;
|
|
104
|
+
};
|
|
105
|
+
onWheelStart?: (ref: ReactZoomPanPinchRef, event: WheelEvent) => void;
|
|
106
|
+
onWheel?: (ref: ReactZoomPanPinchRef, event: WheelEvent) => void;
|
|
107
|
+
onWheelStop?: (ref: ReactZoomPanPinchRef, event: WheelEvent) => void;
|
|
108
|
+
onPanningStart?: (
|
|
109
|
+
ref: ReactZoomPanPinchRef,
|
|
110
|
+
event: TouchEvent | MouseEvent,
|
|
111
|
+
) => void;
|
|
112
|
+
onPanning?: (
|
|
113
|
+
ref: ReactZoomPanPinchRef,
|
|
114
|
+
event: TouchEvent | MouseEvent,
|
|
115
|
+
) => void;
|
|
116
|
+
onPanningStop?: (
|
|
117
|
+
ref: ReactZoomPanPinchRef,
|
|
118
|
+
event: TouchEvent | MouseEvent,
|
|
119
|
+
) => void;
|
|
120
|
+
onPinchingStart?: (ref: ReactZoomPanPinchRef, event: TouchEvent) => void;
|
|
121
|
+
onPinching?: (ref: ReactZoomPanPinchRef, event: TouchEvent) => void;
|
|
122
|
+
onPinchingStop?: (ref: ReactZoomPanPinchRef, event: TouchEvent) => void;
|
|
123
|
+
onZoomStart?: (
|
|
124
|
+
ref: ReactZoomPanPinchRef,
|
|
125
|
+
event: TouchEvent | MouseEvent,
|
|
126
|
+
) => void;
|
|
127
|
+
onZoom?: (ref: ReactZoomPanPinchRef, event: TouchEvent | MouseEvent) => void;
|
|
128
|
+
onZoomStop?: (
|
|
129
|
+
ref: ReactZoomPanPinchRef,
|
|
130
|
+
event: TouchEvent | MouseEvent,
|
|
131
|
+
) => void;
|
|
132
|
+
onTransformed?: (
|
|
133
|
+
ref: ReactZoomPanPinchRef,
|
|
134
|
+
state: { scale: number; positionX: number; positionY: number },
|
|
135
|
+
) => void;
|
|
136
|
+
onInit?: (ref: ReactZoomPanPinchRef) => void;
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
export type ReactZoomPanPinchComponentHelpers = {
|
|
140
|
+
setComponents: (wrapper: HTMLDivElement, content: HTMLDivElement) => void;
|
|
141
|
+
};
|
|
142
|
+
|
|
143
|
+
export type LibrarySetup = Pick<
|
|
144
|
+
ReactZoomPanPinchProps,
|
|
145
|
+
"minPositionX" | "maxPositionX" | "minPositionY" | "maxPositionY"
|
|
146
|
+
> &
|
|
147
|
+
DeepNonNullable<
|
|
148
|
+
Omit<
|
|
149
|
+
ReactZoomPanPinchProps,
|
|
150
|
+
| "ref"
|
|
151
|
+
| "initialScale"
|
|
152
|
+
| "initialPositionX"
|
|
153
|
+
| "initialPositionY"
|
|
154
|
+
| "minPositionX"
|
|
155
|
+
| "maxPositionX"
|
|
156
|
+
| "minPositionY"
|
|
157
|
+
| "maxPositionY"
|
|
158
|
+
| "children"
|
|
159
|
+
| "defaultPositionX"
|
|
160
|
+
| "defaultPositionY"
|
|
161
|
+
| "defaultScale"
|
|
162
|
+
| "wrapperClass"
|
|
163
|
+
| "contentClass"
|
|
164
|
+
| "onWheelStart"
|
|
165
|
+
| "onWheel"
|
|
166
|
+
| "onWheelStop"
|
|
167
|
+
| "onPanningStart"
|
|
168
|
+
| "onPanning"
|
|
169
|
+
| "onPanningStop"
|
|
170
|
+
| "onPinchingStart"
|
|
171
|
+
| "onPinching"
|
|
172
|
+
| "onPinchingStop"
|
|
173
|
+
| "onZoomStart"
|
|
174
|
+
| "onZoom"
|
|
175
|
+
| "onZoomStop"
|
|
176
|
+
| "onTransformed"
|
|
177
|
+
| "onInit"
|
|
178
|
+
>
|
|
179
|
+
>;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/ban-types */
|
|
2
|
+
|
|
3
|
+
export type DeepNonNullable<T> = T extends (...args: any[]) => any
|
|
4
|
+
? T
|
|
5
|
+
: T extends any[]
|
|
6
|
+
? DeepNonNullableArray<T[number]>
|
|
7
|
+
: T extends object
|
|
8
|
+
? DeepNonNullableObject<T>
|
|
9
|
+
: T;
|
|
10
|
+
|
|
11
|
+
export type DeepNonNullableArray<T> = Array<DeepNonNullable<NonNullable<T>>>;
|
|
12
|
+
|
|
13
|
+
export type DeepNonNullableObject<T> = {
|
|
14
|
+
[P in keyof T]-?: DeepNonNullable<NonNullable<T[P]>>;
|
|
15
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
/// <reference types="react-scripts" />
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
<svg version="1.1"
|
|
2
|
+
baseProfile="full"
|
|
3
|
+
width="300" height="200"
|
|
4
|
+
xmlns="http://www.w3.org/2000/svg"
|
|
5
|
+
id="example-svg"
|
|
6
|
+
>
|
|
7
|
+
|
|
8
|
+
<rect width="100%" height="100%" fill="red" />
|
|
9
|
+
|
|
10
|
+
<circle cx="150" cy="100" r="80" fill="green" />
|
|
11
|
+
|
|
12
|
+
<text x="150" y="125" font-size="30" text-anchor="middle" fill="white" id="text-svg">Example</text>
|
|
13
|
+
|
|
14
|
+
</svg>
|