react-zoom-pan-pinch 3.6.1 → 4.0.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.cjs.js +770 -277
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.d.ts +116 -22
- package/dist/index.esm.js +766 -269
- package/dist/index.esm.js.map +1 -1
- package/package.json +73 -60
- package/src/components/index.ts +5 -0
- package/src/components/keep-scale/keep-scale.tsx +28 -0
- package/src/components/mini-map/mini-map.tsx +292 -0
- package/src/components/mini-map/use-resize.hook.ts +74 -0
- package/src/components/transform-component/transform-component.module.css +43 -0
- package/src/components/transform-component/transform-component.tsx +96 -0
- package/src/components/transform-wrapper/transform-wrapper.tsx +39 -0
- package/src/components/virtualize/virtualize.tsx +133 -0
- package/src/components/virtualize/virtualize.utils.ts +104 -0
- package/src/constants/state.constants.ts +99 -0
- package/src/core/animations/animations.constants.ts +78 -0
- package/src/core/animations/animations.utils.ts +115 -0
- package/src/core/bounds/bounds.types.ts +8 -0
- package/src/core/bounds/bounds.utils.ts +226 -0
- package/src/core/double-click/double-click.logic.ts +127 -0
- package/src/core/handlers/handlers.logic.ts +122 -0
- package/src/core/handlers/handlers.utils.ts +243 -0
- package/src/core/instance.core.ts +704 -0
- package/src/core/pan/panning.logic.ts +122 -0
- package/src/core/pan/panning.utils.ts +216 -0
- package/src/core/pan/velocity.logic.ts +187 -0
- package/src/core/pan/velocity.utils.ts +83 -0
- package/src/core/pinch/pinch.logic.ts +139 -0
- package/src/core/pinch/pinch.utils.ts +95 -0
- package/src/core/wheel/wheel.logic.ts +161 -0
- package/src/core/wheel/wheel.utils.ts +157 -0
- package/src/core/zoom/zoom.logic.ts +71 -0
- package/src/core/zoom/zoom.utils.ts +62 -0
- package/src/hooks/index.ts +5 -0
- package/src/hooks/use-controls.tsx +9 -0
- package/src/hooks/use-transform-component.tsx +30 -0
- package/src/hooks/use-transform-context.tsx +13 -0
- package/src/hooks/use-transform-effect.tsx +22 -0
- package/src/hooks/use-transform-init.tsx +28 -0
- package/src/hooks/use-zoom-pan-pinch.tsx +48 -0
- package/src/index.ts +6 -0
- package/src/models/calculations.model.ts +34 -0
- package/src/models/context.model.ts +218 -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/map.jpg +0 -0
- package/src/stories/assets/medium-image.jpg +0 -0
- package/src/stories/assets/pin.svg +1 -0
- package/src/stories/assets/small-image.jpg +0 -0
- package/src/stories/assets/svg-example.svg +14 -0
- package/src/stories/docs/DocTable.tsx +59 -0
- package/src/stories/docs/HandlerCards.tsx +102 -0
- package/src/stories/docs/Introduction.stories.mdx +135 -0
- package/src/stories/docs/PropTable.tsx +116 -0
- package/src/stories/docs/docs.module.css +370 -0
- package/src/stories/docs/handlers.stories.mdx +7 -0
- package/src/stories/docs/handlers.tsx +111 -0
- package/src/stories/docs/props.stories.mdx +11 -0
- package/src/stories/docs/props.tsx +571 -0
- package/src/stories/examples/axis-lock/axis-lock.stories.mdx +35 -0
- package/src/stories/examples/axis-lock/example.tsx +114 -0
- package/src/stories/examples/bounds/bounds.data.tsx +191 -0
- package/src/stories/examples/bounds/bounds.stories.mdx +99 -0
- package/src/stories/examples/center-zoomed-out/center-zoomed-out.stories.mdx +206 -0
- package/src/stories/examples/cinema/cinema-data.tsx +578 -0
- package/src/stories/examples/cinema/cinema.stories.mdx +30 -0
- package/src/stories/examples/cinema/example.tsx +378 -0
- package/src/stories/examples/cinema/image.png +0 -0
- package/src/stories/examples/content/content.stories.mdx +172 -0
- package/src/stories/examples/content/mixed-content.data.tsx +624 -0
- package/src/stories/examples/content-rerendering/content-updating.stories.mdx +34 -0
- package/src/stories/examples/content-rerendering/example.tsx +631 -0
- package/src/stories/examples/controls/controls.stories.mdx +120 -0
- package/src/stories/examples/image/example.tsx +149 -0
- package/src/stories/examples/image/image.stories.mdx +46 -0
- package/src/stories/examples/image-annotations/example.tsx +1442 -0
- package/src/stories/examples/image-annotations/image-annotations.stories.mdx +39 -0
- package/src/stories/examples/image-responsive/example.tsx +152 -0
- package/src/stories/examples/image-responsive/image-responsive.stories.mdx +51 -0
- package/src/stories/examples/image-zoomed-out/image-zoomed-out.stories.mdx +107 -0
- package/src/stories/examples/initial-transform/example.tsx +233 -0
- package/src/stories/examples/initial-transform/initial-transform.stories.mdx +37 -0
- package/src/stories/examples/keep-scale/example.tsx +882 -0
- package/src/stories/examples/keep-scale/keep-scale.stories.mdx +30 -0
- package/src/stories/examples/keep-scale/map.stories.mdx +28 -0
- package/src/stories/examples/medical-viewer/chest-xray.tsx +246 -0
- package/src/stories/examples/medical-viewer/example.tsx +690 -0
- package/src/stories/examples/medical-viewer/medical-viewer.stories.mdx +49 -0
- package/src/stories/examples/mini-map/example.tsx +122 -0
- package/src/stories/examples/mini-map/mini-map.stories.mdx +26 -0
- package/src/stories/examples/miro/example.tsx +1573 -0
- package/src/stories/examples/miro/miro.stories.mdx +31 -0
- package/src/stories/examples/padding/example.tsx +147 -0
- package/src/stories/examples/padding/padding.stories.mdx +35 -0
- package/src/stories/examples/product-card/example.tsx +594 -0
- package/src/stories/examples/product-card/product-card.stories.mdx +29 -0
- package/src/stories/examples/stadium/example.tsx +1040 -0
- package/src/stories/examples/stadium/stadium-data.ts +224 -0
- package/src/stories/examples/stadium/stadium.stories.mdx +32 -0
- package/src/stories/examples/stress/example.tsx +59 -0
- package/src/stories/examples/stress/stress-test.stories.mdx +28 -0
- package/src/stories/examples/svg-zoom-to-element/creativity.svg +143 -0
- package/src/stories/examples/svg-zoom-to-element/example.tsx +95 -0
- package/src/stories/examples/svg-zoom-to-element/svg-zoom-to-element.stories.mdx +28 -0
- package/src/stories/examples/text-selection/example.tsx +221 -0
- package/src/stories/examples/text-selection/text-selection.stories.mdx +31 -0
- package/src/stories/examples/velocity/example.tsx +908 -0
- package/src/stories/examples/velocity/velocity.stories.mdx +35 -0
- package/src/stories/examples/virtualize/example.tsx +239 -0
- package/src/stories/examples/virtualize/virtualize.stories.mdx +84 -0
- package/src/stories/examples/zoom-to-element/example.tsx +219 -0
- package/src/stories/examples/zoom-to-element/zoom-to-element.stories.mdx +31 -0
- package/src/stories/hooks/use-controls.stories.mdx +86 -0
- package/src/stories/hooks/use-transform-component.stories.mdx +108 -0
- package/src/stories/hooks/use-transform-context.stories.mdx +92 -0
- package/src/stories/hooks/use-transform-effect.stories.mdx +114 -0
- package/src/stories/hooks/use-transform-init.stories.mdx +105 -0
- package/src/stories/types/args.types.ts +192 -0
- package/src/stories/utils/args.utils.ts +35 -0
- package/src/stories/utils/bounds-overlay.tsx +168 -0
- package/src/stories/utils/code.utils.tsx +22 -0
- package/src/stories/utils/controls.utils.tsx +347 -0
- package/src/stories/utils/index.ts +5 -0
- package/src/stories/utils/styles.module.css +223 -0
- package/src/stories/utils/viewer.styles.ts +34 -0
- package/src/styles.css +8 -0
- package/src/typings.d.ts +21 -0
- package/src/utils/calculations.utils.ts +16 -0
- package/src/utils/callback.utils.ts +11 -0
- package/src/utils/context.utils.ts +49 -0
- package/src/utils/event.utils.ts +38 -0
- package/src/utils/helpers.utils.ts +22 -0
- package/src/utils/index.ts +8 -0
- package/src/utils/ref.utils.ts +32 -0
- package/src/utils/state.utils.ts +77 -0
- package/src/utils/styles.utils.ts +49 -0
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
/* eslint-disable no-param-reassign */
|
|
2
|
+
import { roundNumber } from "../../utils";
|
|
3
|
+
import {
|
|
4
|
+
BoundsType,
|
|
5
|
+
PositionType,
|
|
6
|
+
ReactZoomPanPinchContext,
|
|
7
|
+
} from "../../models";
|
|
8
|
+
import { ComponentsSizesType } from "./bounds.types";
|
|
9
|
+
|
|
10
|
+
export function getComponentsSizes(
|
|
11
|
+
wrapperComponent: HTMLDivElement,
|
|
12
|
+
contentComponent: HTMLDivElement,
|
|
13
|
+
newScale: number,
|
|
14
|
+
): ComponentsSizesType {
|
|
15
|
+
const wrapperWidth = wrapperComponent.offsetWidth;
|
|
16
|
+
const wrapperHeight = wrapperComponent.offsetHeight;
|
|
17
|
+
|
|
18
|
+
const contentWidth = contentComponent.offsetWidth;
|
|
19
|
+
const contentHeight = contentComponent.offsetHeight;
|
|
20
|
+
|
|
21
|
+
const newContentWidth = contentWidth * newScale;
|
|
22
|
+
const newContentHeight = contentHeight * newScale;
|
|
23
|
+
const newDiffWidth = wrapperWidth - newContentWidth;
|
|
24
|
+
const newDiffHeight = wrapperHeight - newContentHeight;
|
|
25
|
+
|
|
26
|
+
return {
|
|
27
|
+
wrapperWidth,
|
|
28
|
+
wrapperHeight,
|
|
29
|
+
newContentWidth,
|
|
30
|
+
newDiffWidth,
|
|
31
|
+
newContentHeight,
|
|
32
|
+
newDiffHeight,
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export const getBounds = (
|
|
37
|
+
wrapperWidth: number,
|
|
38
|
+
newContentWidth: number,
|
|
39
|
+
diffWidth: number,
|
|
40
|
+
wrapperHeight: number,
|
|
41
|
+
newContentHeight: number,
|
|
42
|
+
diffHeight: number,
|
|
43
|
+
centerZoomedOut: boolean,
|
|
44
|
+
): BoundsType => {
|
|
45
|
+
const scaleWidthFactor =
|
|
46
|
+
wrapperWidth > newContentWidth
|
|
47
|
+
? diffWidth * (centerZoomedOut ? 0.5 : 1)
|
|
48
|
+
: 0;
|
|
49
|
+
const scaleHeightFactor =
|
|
50
|
+
wrapperHeight > newContentHeight
|
|
51
|
+
? diffHeight * (centerZoomedOut ? 0.5 : 1)
|
|
52
|
+
: 0;
|
|
53
|
+
|
|
54
|
+
const minPositionX = wrapperWidth - newContentWidth - scaleWidthFactor;
|
|
55
|
+
const maxPositionX = scaleWidthFactor;
|
|
56
|
+
const minPositionY = wrapperHeight - newContentHeight - scaleHeightFactor;
|
|
57
|
+
const maxPositionY = scaleHeightFactor;
|
|
58
|
+
|
|
59
|
+
return {
|
|
60
|
+
minPositionX,
|
|
61
|
+
maxPositionX,
|
|
62
|
+
minPositionY,
|
|
63
|
+
maxPositionY,
|
|
64
|
+
scaleWidthFactor,
|
|
65
|
+
scaleHeightFactor,
|
|
66
|
+
};
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
export const calculateBounds = (
|
|
70
|
+
contextInstance: ReactZoomPanPinchContext,
|
|
71
|
+
newScale: number,
|
|
72
|
+
): BoundsType => {
|
|
73
|
+
const { wrapperComponent, contentComponent } = contextInstance;
|
|
74
|
+
const { centerZoomedOut, disablePadding } = contextInstance.setup;
|
|
75
|
+
|
|
76
|
+
if (!wrapperComponent || !contentComponent) {
|
|
77
|
+
throw new Error("Components are not mounted");
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const {
|
|
81
|
+
wrapperWidth,
|
|
82
|
+
wrapperHeight,
|
|
83
|
+
newContentWidth,
|
|
84
|
+
newContentHeight,
|
|
85
|
+
newDiffWidth,
|
|
86
|
+
newDiffHeight,
|
|
87
|
+
} = getComponentsSizes(wrapperComponent, contentComponent, newScale);
|
|
88
|
+
|
|
89
|
+
const bounds = getBounds(
|
|
90
|
+
wrapperWidth,
|
|
91
|
+
newContentWidth,
|
|
92
|
+
newDiffWidth,
|
|
93
|
+
wrapperHeight,
|
|
94
|
+
newContentHeight,
|
|
95
|
+
newDiffHeight,
|
|
96
|
+
Boolean(centerZoomedOut),
|
|
97
|
+
);
|
|
98
|
+
|
|
99
|
+
const contentFitsCompletely =
|
|
100
|
+
wrapperWidth >= newContentWidth && wrapperHeight >= newContentHeight;
|
|
101
|
+
if (disablePadding && contentFitsCompletely && !centerZoomedOut) {
|
|
102
|
+
bounds.minPositionX = 0;
|
|
103
|
+
bounds.maxPositionX = 0;
|
|
104
|
+
bounds.minPositionY = 0;
|
|
105
|
+
bounds.maxPositionY = 0;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
const {
|
|
109
|
+
minPositionX: propMinX,
|
|
110
|
+
maxPositionX: propMaxX,
|
|
111
|
+
minPositionY: propMinY,
|
|
112
|
+
maxPositionY: propMaxY,
|
|
113
|
+
} = contextInstance.setup;
|
|
114
|
+
|
|
115
|
+
// Explicit position props define content-space boundaries at scale=1.
|
|
116
|
+
// Scale them so the same content region stays reachable at every zoom level.
|
|
117
|
+
if (propMinX != null) {
|
|
118
|
+
bounds.minPositionX = wrapperWidth * (1 - newScale) + propMinX * newScale;
|
|
119
|
+
}
|
|
120
|
+
if (propMaxX != null) {
|
|
121
|
+
bounds.maxPositionX = propMaxX * newScale;
|
|
122
|
+
}
|
|
123
|
+
if (propMinY != null) {
|
|
124
|
+
bounds.minPositionY = wrapperHeight * (1 - newScale) + propMinY * newScale;
|
|
125
|
+
}
|
|
126
|
+
if (propMaxY != null) {
|
|
127
|
+
bounds.maxPositionY = propMaxY * newScale;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
return bounds;
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
export function clamp(v: number, min: number, max: number) {
|
|
134
|
+
return Math.max(min, Math.min(v, max));
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
// Based on @aholachek ;)
|
|
138
|
+
// https://twitter.com/chpwn/status/285540192096497664
|
|
139
|
+
// iOS constant = 0.55
|
|
140
|
+
|
|
141
|
+
// https://medium.com/@nathangitter/building-fluid-interfaces-ios-swift-9732bb934bf5
|
|
142
|
+
|
|
143
|
+
function rubberband(distance: number, dimension: number, constant: number) {
|
|
144
|
+
if (dimension === 0 || Math.abs(dimension) === Infinity)
|
|
145
|
+
return distance ** (constant * 5);
|
|
146
|
+
return (distance * dimension * constant) / (dimension + constant * distance);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
export function rubberbandIfOutOfBounds(
|
|
150
|
+
position: number,
|
|
151
|
+
min: number,
|
|
152
|
+
max: number,
|
|
153
|
+
constant = 0.15,
|
|
154
|
+
) {
|
|
155
|
+
if (constant === 0) return clamp(position, min, max);
|
|
156
|
+
if (position < min)
|
|
157
|
+
return -rubberband(min - position, max - min, constant) + min;
|
|
158
|
+
if (position > max)
|
|
159
|
+
return +rubberband(position - max, max - min, constant) + max;
|
|
160
|
+
return position;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* Keeps value between given bounds, used for limiting view to given boundaries
|
|
165
|
+
* 1# eg. boundLimiter(2, 0, 3, true) => 2
|
|
166
|
+
* 2# eg. boundLimiter(4, 0, 3, true) => 3
|
|
167
|
+
* 3# eg. boundLimiter(-2, 0, 3, true) => 0
|
|
168
|
+
* 4# eg. boundLimiter(10, 0, 3, false) => 10
|
|
169
|
+
*/
|
|
170
|
+
export const boundLimiter = (
|
|
171
|
+
value: number,
|
|
172
|
+
minBound: number,
|
|
173
|
+
maxBound: number,
|
|
174
|
+
isActive: boolean,
|
|
175
|
+
): number => {
|
|
176
|
+
if (!isActive) return roundNumber(value, 2);
|
|
177
|
+
if (value < minBound) return roundNumber(minBound, 2);
|
|
178
|
+
if (value > maxBound) return roundNumber(maxBound, 2);
|
|
179
|
+
return roundNumber(value, 2);
|
|
180
|
+
};
|
|
181
|
+
|
|
182
|
+
export const handleCalculateBounds = (
|
|
183
|
+
contextInstance: ReactZoomPanPinchContext,
|
|
184
|
+
newScale: number,
|
|
185
|
+
): BoundsType => {
|
|
186
|
+
const bounds = calculateBounds(contextInstance, newScale);
|
|
187
|
+
|
|
188
|
+
// Save bounds
|
|
189
|
+
contextInstance.bounds = bounds;
|
|
190
|
+
return bounds;
|
|
191
|
+
};
|
|
192
|
+
|
|
193
|
+
export function getMouseBoundedPosition(
|
|
194
|
+
positionX: number,
|
|
195
|
+
positionY: number,
|
|
196
|
+
bounds: BoundsType,
|
|
197
|
+
limitToBounds: boolean,
|
|
198
|
+
paddingValueX: number,
|
|
199
|
+
paddingValueY: number,
|
|
200
|
+
wrapperComponent: HTMLDivElement | null,
|
|
201
|
+
): PositionType {
|
|
202
|
+
const { minPositionX, minPositionY, maxPositionX, maxPositionY } = bounds;
|
|
203
|
+
|
|
204
|
+
let paddingX = 0;
|
|
205
|
+
let paddingY = 0;
|
|
206
|
+
|
|
207
|
+
if (wrapperComponent) {
|
|
208
|
+
paddingX = paddingValueX;
|
|
209
|
+
paddingY = paddingValueY;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
const x = boundLimiter(
|
|
213
|
+
positionX,
|
|
214
|
+
minPositionX - paddingX,
|
|
215
|
+
maxPositionX + paddingX,
|
|
216
|
+
limitToBounds,
|
|
217
|
+
);
|
|
218
|
+
|
|
219
|
+
const y = boundLimiter(
|
|
220
|
+
positionY,
|
|
221
|
+
minPositionY - paddingY,
|
|
222
|
+
maxPositionY + paddingY,
|
|
223
|
+
limitToBounds,
|
|
224
|
+
);
|
|
225
|
+
return { x, y };
|
|
226
|
+
}
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
/* eslint-disable no-param-reassign */
|
|
2
|
+
import { LibrarySetup, ReactZoomPanPinchContext } from "../../models";
|
|
3
|
+
import { animate } from "../animations/animations.utils";
|
|
4
|
+
import { getMousePosition } from "../wheel/wheel.utils";
|
|
5
|
+
import { handleZoomToPoint } from "../zoom/zoom.logic";
|
|
6
|
+
import {
|
|
7
|
+
cancelTimeout,
|
|
8
|
+
getContext,
|
|
9
|
+
handleCallback,
|
|
10
|
+
isExcludedNode,
|
|
11
|
+
} from "../../utils";
|
|
12
|
+
import {
|
|
13
|
+
handleCalculateButtonZoom,
|
|
14
|
+
resetTransformations,
|
|
15
|
+
} from "../handlers/handlers.utils";
|
|
16
|
+
|
|
17
|
+
export const handleDoubleClickStop = (
|
|
18
|
+
contextInstance: ReactZoomPanPinchContext,
|
|
19
|
+
event: MouseEvent | TouchEvent,
|
|
20
|
+
): void => {
|
|
21
|
+
const { onZoomStop } = contextInstance.props;
|
|
22
|
+
const { animationTime } = contextInstance.setup.doubleClick;
|
|
23
|
+
|
|
24
|
+
cancelTimeout(contextInstance.doubleClickStopEventTimer);
|
|
25
|
+
contextInstance.doubleClickStopEventTimer = setTimeout(() => {
|
|
26
|
+
contextInstance.doubleClickStopEventTimer = null;
|
|
27
|
+
handleCallback(getContext(contextInstance), event, onZoomStop);
|
|
28
|
+
}, animationTime);
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
export const handleDoubleClickResetMode = (
|
|
32
|
+
contextInstance: ReactZoomPanPinchContext,
|
|
33
|
+
event: MouseEvent | TouchEvent,
|
|
34
|
+
) => {
|
|
35
|
+
const { onZoomStart, onZoom } = contextInstance.props;
|
|
36
|
+
const { animationTime, animationType } = contextInstance.setup.doubleClick;
|
|
37
|
+
|
|
38
|
+
handleCallback(getContext(contextInstance), event, onZoomStart);
|
|
39
|
+
|
|
40
|
+
resetTransformations(contextInstance, animationTime, animationType, () =>
|
|
41
|
+
handleCallback(getContext(contextInstance), event, onZoom),
|
|
42
|
+
);
|
|
43
|
+
|
|
44
|
+
handleDoubleClickStop(contextInstance, event);
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
function getDoubleClickScale(
|
|
48
|
+
mode: LibrarySetup["doubleClick"]["mode"],
|
|
49
|
+
scale: number,
|
|
50
|
+
) {
|
|
51
|
+
if (mode === "toggle") {
|
|
52
|
+
return scale === 1 ? 1 : -1;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
return mode === "zoomOut" ? -1 : 1;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export function handleDoubleClick(
|
|
59
|
+
contextInstance: ReactZoomPanPinchContext,
|
|
60
|
+
event: MouseEvent | TouchEvent,
|
|
61
|
+
): void {
|
|
62
|
+
const { setup, doubleClickStopEventTimer, state, contentComponent } =
|
|
63
|
+
contextInstance;
|
|
64
|
+
|
|
65
|
+
const { scale } = state;
|
|
66
|
+
const { onZoomStart, onZoom } = contextInstance.props;
|
|
67
|
+
const { disabled, mode, step, animationTime, animationType } =
|
|
68
|
+
setup.doubleClick;
|
|
69
|
+
|
|
70
|
+
if (disabled) return;
|
|
71
|
+
if (doubleClickStopEventTimer) return;
|
|
72
|
+
|
|
73
|
+
if (mode === "reset") {
|
|
74
|
+
return handleDoubleClickResetMode(contextInstance, event);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
if (!contentComponent) return console.error("No ContentComponent found");
|
|
78
|
+
|
|
79
|
+
const delta = getDoubleClickScale(mode, contextInstance.state.scale);
|
|
80
|
+
|
|
81
|
+
const newScale = handleCalculateButtonZoom(contextInstance, delta, step);
|
|
82
|
+
|
|
83
|
+
// stop execution when scale didn't change
|
|
84
|
+
if (scale === newScale) return;
|
|
85
|
+
|
|
86
|
+
handleCallback(getContext(contextInstance), event, onZoomStart);
|
|
87
|
+
|
|
88
|
+
const mousePosition = getMousePosition(event, contentComponent, scale);
|
|
89
|
+
const targetState = handleZoomToPoint(
|
|
90
|
+
contextInstance,
|
|
91
|
+
newScale,
|
|
92
|
+
mousePosition.x,
|
|
93
|
+
mousePosition.y,
|
|
94
|
+
);
|
|
95
|
+
|
|
96
|
+
if (!targetState) {
|
|
97
|
+
return console.error(
|
|
98
|
+
"Error during zoom event. New transformation state was not calculated.",
|
|
99
|
+
);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
handleCallback(getContext(contextInstance), event, onZoom);
|
|
103
|
+
|
|
104
|
+
animate(contextInstance, targetState, animationTime, animationType);
|
|
105
|
+
|
|
106
|
+
handleDoubleClickStop(contextInstance, event);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
export const isDoubleClickAllowed = (
|
|
110
|
+
contextInstance: ReactZoomPanPinchContext,
|
|
111
|
+
event: MouseEvent | TouchEvent,
|
|
112
|
+
): boolean => {
|
|
113
|
+
const { isInitialized, setup, wrapperComponent } = contextInstance;
|
|
114
|
+
const { disabled, excluded } = setup.doubleClick;
|
|
115
|
+
|
|
116
|
+
const target = event.target as HTMLElement;
|
|
117
|
+
const isWrapperChild = wrapperComponent?.contains(target);
|
|
118
|
+
const isAllowed = isInitialized && target && isWrapperChild && !disabled;
|
|
119
|
+
|
|
120
|
+
if (!isAllowed) return false;
|
|
121
|
+
|
|
122
|
+
const isExcluded = isExcludedNode(target, excluded);
|
|
123
|
+
|
|
124
|
+
if (isExcluded) return false;
|
|
125
|
+
|
|
126
|
+
return true;
|
|
127
|
+
};
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import { ReactZoomPanPinchContext } from "../../models";
|
|
2
|
+
import {
|
|
3
|
+
calculateZoomToNode,
|
|
4
|
+
handleZoomToViewCenter,
|
|
5
|
+
resetTransformations,
|
|
6
|
+
} from "./handlers.utils";
|
|
7
|
+
import { animations } from "../animations/animations.constants";
|
|
8
|
+
import { animate, handleCancelAnimation } from "../animations/animations.utils";
|
|
9
|
+
import { getCenterPosition } from "../../utils";
|
|
10
|
+
|
|
11
|
+
export const zoomIn =
|
|
12
|
+
(contextInstance: ReactZoomPanPinchContext) =>
|
|
13
|
+
(
|
|
14
|
+
step = 0.5,
|
|
15
|
+
animationTime = 300,
|
|
16
|
+
animationType: keyof typeof animations = "easeOut",
|
|
17
|
+
): void => {
|
|
18
|
+
handleZoomToViewCenter(
|
|
19
|
+
contextInstance,
|
|
20
|
+
1,
|
|
21
|
+
step,
|
|
22
|
+
animationTime,
|
|
23
|
+
animationType,
|
|
24
|
+
);
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
export const zoomOut =
|
|
28
|
+
(contextInstance: ReactZoomPanPinchContext) =>
|
|
29
|
+
(
|
|
30
|
+
step = 0.5,
|
|
31
|
+
animationTime = 300,
|
|
32
|
+
animationType: keyof typeof animations = "easeOut",
|
|
33
|
+
): void => {
|
|
34
|
+
handleZoomToViewCenter(
|
|
35
|
+
contextInstance,
|
|
36
|
+
-1,
|
|
37
|
+
step,
|
|
38
|
+
animationTime,
|
|
39
|
+
animationType,
|
|
40
|
+
);
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
export const setTransform =
|
|
44
|
+
(contextInstance: ReactZoomPanPinchContext) =>
|
|
45
|
+
(
|
|
46
|
+
newPositionX: number,
|
|
47
|
+
newPositionY: number,
|
|
48
|
+
newScale: number,
|
|
49
|
+
animationTime = 300,
|
|
50
|
+
animationType: keyof typeof animations = "easeOut",
|
|
51
|
+
): void => {
|
|
52
|
+
const { positionX, positionY, scale } = contextInstance.state;
|
|
53
|
+
const { wrapperComponent, contentComponent } = contextInstance;
|
|
54
|
+
const { disabled } = contextInstance.setup;
|
|
55
|
+
|
|
56
|
+
if (disabled || !wrapperComponent || !contentComponent) return;
|
|
57
|
+
|
|
58
|
+
const targetState = {
|
|
59
|
+
positionX: Number.isNaN(newPositionX) ? positionX : newPositionX,
|
|
60
|
+
positionY: Number.isNaN(newPositionY) ? positionY : newPositionY,
|
|
61
|
+
scale: Number.isNaN(newScale) ? scale : newScale,
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
animate(contextInstance, targetState, animationTime, animationType);
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
export const resetTransform =
|
|
68
|
+
(contextInstance: ReactZoomPanPinchContext) =>
|
|
69
|
+
(
|
|
70
|
+
animationTime = 200,
|
|
71
|
+
animationType: keyof typeof animations = "easeOut",
|
|
72
|
+
): void => {
|
|
73
|
+
resetTransformations(contextInstance, animationTime, animationType);
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
export const centerView =
|
|
77
|
+
(contextInstance: ReactZoomPanPinchContext) =>
|
|
78
|
+
(
|
|
79
|
+
scale?: number,
|
|
80
|
+
animationTime = 200,
|
|
81
|
+
animationType: keyof typeof animations = "easeOut",
|
|
82
|
+
): void => {
|
|
83
|
+
const { state, wrapperComponent, contentComponent } = contextInstance;
|
|
84
|
+
if (wrapperComponent && contentComponent) {
|
|
85
|
+
const targetState = getCenterPosition(
|
|
86
|
+
scale || state.scale,
|
|
87
|
+
wrapperComponent,
|
|
88
|
+
contentComponent,
|
|
89
|
+
);
|
|
90
|
+
|
|
91
|
+
animate(contextInstance, targetState, animationTime, animationType);
|
|
92
|
+
}
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
export const zoomToElement =
|
|
96
|
+
(contextInstance: ReactZoomPanPinchContext) =>
|
|
97
|
+
(
|
|
98
|
+
node: HTMLElement | string,
|
|
99
|
+
scale?: number,
|
|
100
|
+
animationTime = 600,
|
|
101
|
+
animationType: keyof typeof animations = "easeOut",
|
|
102
|
+
offsetX = 0,
|
|
103
|
+
offsetY = 0,
|
|
104
|
+
): void => {
|
|
105
|
+
handleCancelAnimation(contextInstance);
|
|
106
|
+
|
|
107
|
+
const { wrapperComponent } = contextInstance;
|
|
108
|
+
|
|
109
|
+
const target: HTMLElement | null =
|
|
110
|
+
typeof node === "string" ? document.getElementById(node) : node;
|
|
111
|
+
|
|
112
|
+
if (wrapperComponent && target && wrapperComponent.contains(target)) {
|
|
113
|
+
const targetState = calculateZoomToNode(
|
|
114
|
+
contextInstance,
|
|
115
|
+
target,
|
|
116
|
+
scale,
|
|
117
|
+
offsetX,
|
|
118
|
+
offsetY,
|
|
119
|
+
);
|
|
120
|
+
animate(contextInstance, targetState, animationTime, animationType);
|
|
121
|
+
}
|
|
122
|
+
};
|
|
@@ -0,0 +1,243 @@
|
|
|
1
|
+
import { ReactZoomPanPinchContext, ReactZoomPanPinchState } from "../../models";
|
|
2
|
+
import { animations } from "../animations/animations.constants";
|
|
3
|
+
import { handleZoomToPoint } from "../zoom/zoom.logic";
|
|
4
|
+
import { animate } from "../animations/animations.utils";
|
|
5
|
+
import { createState } from "../../utils/state.utils";
|
|
6
|
+
import { checkZoomBounds } from "../zoom/zoom.utils";
|
|
7
|
+
import {
|
|
8
|
+
getContext,
|
|
9
|
+
getCenterPosition,
|
|
10
|
+
handleCallback,
|
|
11
|
+
roundNumber,
|
|
12
|
+
} from "../../utils";
|
|
13
|
+
import {
|
|
14
|
+
calculateBounds,
|
|
15
|
+
getMouseBoundedPosition,
|
|
16
|
+
} from "../bounds/bounds.utils";
|
|
17
|
+
|
|
18
|
+
export const handleCalculateButtonZoom = (
|
|
19
|
+
contextInstance: ReactZoomPanPinchContext,
|
|
20
|
+
delta: number,
|
|
21
|
+
step: number,
|
|
22
|
+
): number => {
|
|
23
|
+
const { scale } = contextInstance.state;
|
|
24
|
+
const { wrapperComponent, setup } = contextInstance;
|
|
25
|
+
const { maxScale, minScale, zoomAnimation, smooth } = setup;
|
|
26
|
+
const { size } = zoomAnimation;
|
|
27
|
+
|
|
28
|
+
if (!wrapperComponent) {
|
|
29
|
+
throw new Error("Wrapper is not mounted");
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const targetScale = smooth
|
|
33
|
+
? scale * Math.exp(delta * step)
|
|
34
|
+
: scale + delta * step;
|
|
35
|
+
|
|
36
|
+
const newScale = checkZoomBounds(
|
|
37
|
+
roundNumber(targetScale, 3),
|
|
38
|
+
minScale,
|
|
39
|
+
maxScale,
|
|
40
|
+
size,
|
|
41
|
+
false,
|
|
42
|
+
);
|
|
43
|
+
return newScale;
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
export function handleZoomToViewCenter(
|
|
47
|
+
contextInstance: ReactZoomPanPinchContext,
|
|
48
|
+
delta: number,
|
|
49
|
+
step: number,
|
|
50
|
+
animationTime: number,
|
|
51
|
+
animationType: keyof typeof animations,
|
|
52
|
+
): void {
|
|
53
|
+
const { wrapperComponent } = contextInstance;
|
|
54
|
+
const { scale, positionX, positionY } = contextInstance.state;
|
|
55
|
+
const { zoomAnimation } = contextInstance.setup;
|
|
56
|
+
|
|
57
|
+
if (!wrapperComponent) return console.error("No WrapperComponent found");
|
|
58
|
+
|
|
59
|
+
const effectiveAnimationTime = zoomAnimation.disabled ? 0 : animationTime;
|
|
60
|
+
|
|
61
|
+
const wrapperWidth = wrapperComponent.offsetWidth;
|
|
62
|
+
const wrapperHeight = wrapperComponent.offsetHeight;
|
|
63
|
+
const mouseX = (wrapperWidth / 2 - positionX) / scale;
|
|
64
|
+
const mouseY = (wrapperHeight / 2 - positionY) / scale;
|
|
65
|
+
|
|
66
|
+
const newScale = handleCalculateButtonZoom(contextInstance, delta, step);
|
|
67
|
+
|
|
68
|
+
const targetState = handleZoomToPoint(
|
|
69
|
+
contextInstance,
|
|
70
|
+
newScale,
|
|
71
|
+
mouseX,
|
|
72
|
+
mouseY,
|
|
73
|
+
);
|
|
74
|
+
|
|
75
|
+
if (!targetState) {
|
|
76
|
+
return console.error(
|
|
77
|
+
"Error during zoom event. New transformation state was not calculated.",
|
|
78
|
+
);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const { onZoomStart, onZoom, onZoomStop } = contextInstance.props;
|
|
82
|
+
const event = new MouseEvent("mousemove", { bubbles: true });
|
|
83
|
+
const ctx = getContext(contextInstance);
|
|
84
|
+
handleCallback(ctx, event, onZoomStart);
|
|
85
|
+
handleCallback(ctx, event, onZoom);
|
|
86
|
+
animate(contextInstance, targetState, effectiveAnimationTime, animationType);
|
|
87
|
+
const win =
|
|
88
|
+
wrapperComponent.ownerDocument?.defaultView ??
|
|
89
|
+
(typeof window !== "undefined" ? window : null);
|
|
90
|
+
if (win) {
|
|
91
|
+
win.setTimeout(() => {
|
|
92
|
+
if (!contextInstance.mounted) return;
|
|
93
|
+
handleCallback(getContext(contextInstance), event, onZoomStop);
|
|
94
|
+
}, effectiveAnimationTime);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
export function resetTransformations(
|
|
99
|
+
contextInstance: ReactZoomPanPinchContext,
|
|
100
|
+
animationTime: number,
|
|
101
|
+
animationType: keyof typeof animations,
|
|
102
|
+
onResetTransformation?: () => void,
|
|
103
|
+
): void {
|
|
104
|
+
const { setup, wrapperComponent, contentComponent } = contextInstance;
|
|
105
|
+
const { limitToBounds, centerOnInit } = setup;
|
|
106
|
+
const initialTransformation = createState(contextInstance.props);
|
|
107
|
+
const { scale, positionX, positionY } = contextInstance.state;
|
|
108
|
+
|
|
109
|
+
if (!wrapperComponent) return;
|
|
110
|
+
|
|
111
|
+
let targetPositionX = initialTransformation.positionX;
|
|
112
|
+
let targetPositionY = initialTransformation.positionY;
|
|
113
|
+
|
|
114
|
+
if (centerOnInit && contentComponent) {
|
|
115
|
+
const centered = getCenterPosition(
|
|
116
|
+
initialTransformation.scale,
|
|
117
|
+
wrapperComponent,
|
|
118
|
+
contentComponent,
|
|
119
|
+
);
|
|
120
|
+
targetPositionX = centered.positionX;
|
|
121
|
+
targetPositionY = centered.positionY;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
const newBounds = calculateBounds(
|
|
125
|
+
contextInstance,
|
|
126
|
+
initialTransformation.scale,
|
|
127
|
+
);
|
|
128
|
+
|
|
129
|
+
const boundedPositions = getMouseBoundedPosition(
|
|
130
|
+
targetPositionX,
|
|
131
|
+
targetPositionY,
|
|
132
|
+
newBounds,
|
|
133
|
+
limitToBounds,
|
|
134
|
+
0,
|
|
135
|
+
0,
|
|
136
|
+
wrapperComponent,
|
|
137
|
+
);
|
|
138
|
+
|
|
139
|
+
const newState = {
|
|
140
|
+
scale: initialTransformation.scale,
|
|
141
|
+
positionX: boundedPositions.x,
|
|
142
|
+
positionY: boundedPositions.y,
|
|
143
|
+
};
|
|
144
|
+
|
|
145
|
+
if (
|
|
146
|
+
scale === initialTransformation.scale &&
|
|
147
|
+
positionX === initialTransformation.positionX &&
|
|
148
|
+
positionY === initialTransformation.positionY
|
|
149
|
+
) {
|
|
150
|
+
return;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
onResetTransformation?.();
|
|
154
|
+
|
|
155
|
+
const { onZoomStart, onZoom, onZoomStop } = contextInstance.props;
|
|
156
|
+
const event = new MouseEvent("mousemove", { bubbles: true });
|
|
157
|
+
const ctx = getContext(contextInstance);
|
|
158
|
+
handleCallback(ctx, event, onZoomStart);
|
|
159
|
+
handleCallback(ctx, event, onZoom);
|
|
160
|
+
animate(contextInstance, newState, animationTime, animationType);
|
|
161
|
+
const win =
|
|
162
|
+
wrapperComponent.ownerDocument?.defaultView ??
|
|
163
|
+
(typeof window !== "undefined" ? window : null);
|
|
164
|
+
if (win) {
|
|
165
|
+
win.setTimeout(() => {
|
|
166
|
+
if (!contextInstance.mounted) return;
|
|
167
|
+
handleCallback(getContext(contextInstance), event, onZoomStop);
|
|
168
|
+
}, animationTime);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
export function getOffset(
|
|
173
|
+
element: HTMLElement,
|
|
174
|
+
wrapper: HTMLElement,
|
|
175
|
+
content: HTMLElement,
|
|
176
|
+
state: ReactZoomPanPinchState,
|
|
177
|
+
) {
|
|
178
|
+
const offset = element.getBoundingClientRect();
|
|
179
|
+
const wrapperOffset = wrapper.getBoundingClientRect();
|
|
180
|
+
const contentOffset = content.getBoundingClientRect();
|
|
181
|
+
|
|
182
|
+
const xOff = wrapperOffset.x * state.scale;
|
|
183
|
+
const yOff = wrapperOffset.y * state.scale;
|
|
184
|
+
|
|
185
|
+
return {
|
|
186
|
+
x: (offset.x - contentOffset.x + xOff) / state.scale,
|
|
187
|
+
y: (offset.y - contentOffset.y + yOff) / state.scale,
|
|
188
|
+
};
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
export function calculateZoomToNode(
|
|
192
|
+
contextInstance: ReactZoomPanPinchContext,
|
|
193
|
+
node: HTMLElement,
|
|
194
|
+
customZoom?: number,
|
|
195
|
+
customOffsetX = 0,
|
|
196
|
+
customOffsetY = 0,
|
|
197
|
+
): { positionX: number; positionY: number; scale: number } {
|
|
198
|
+
const { wrapperComponent, contentComponent, state } = contextInstance;
|
|
199
|
+
const { limitToBounds, minScale, maxScale } = contextInstance.setup;
|
|
200
|
+
if (!wrapperComponent || !contentComponent) return state;
|
|
201
|
+
|
|
202
|
+
const wrapperRect = wrapperComponent.getBoundingClientRect();
|
|
203
|
+
const nodeRect = node.getBoundingClientRect();
|
|
204
|
+
const nodeOffset = getOffset(node, wrapperComponent, contentComponent, state);
|
|
205
|
+
|
|
206
|
+
const nodeLeft = nodeOffset.x;
|
|
207
|
+
const nodeTop = nodeOffset.y;
|
|
208
|
+
const nodeWidth = nodeRect.width / state.scale;
|
|
209
|
+
const nodeHeight = nodeRect.height / state.scale;
|
|
210
|
+
|
|
211
|
+
const scaleX = wrapperComponent.offsetWidth / nodeWidth;
|
|
212
|
+
const scaleY = wrapperComponent.offsetHeight / nodeHeight;
|
|
213
|
+
|
|
214
|
+
const newScale = checkZoomBounds(
|
|
215
|
+
customZoom || Math.min(scaleX, scaleY),
|
|
216
|
+
minScale,
|
|
217
|
+
maxScale,
|
|
218
|
+
0,
|
|
219
|
+
false,
|
|
220
|
+
);
|
|
221
|
+
|
|
222
|
+
const offsetX = (wrapperRect.width - nodeWidth * newScale) / 2;
|
|
223
|
+
const offsetY = (wrapperRect.height - nodeHeight * newScale) / 2;
|
|
224
|
+
|
|
225
|
+
const newPositionX =
|
|
226
|
+
(wrapperRect.left - nodeLeft) * newScale + offsetX + customOffsetX;
|
|
227
|
+
const newPositionY =
|
|
228
|
+
(wrapperRect.top - nodeTop) * newScale + offsetY + customOffsetY;
|
|
229
|
+
|
|
230
|
+
const bounds = calculateBounds(contextInstance, newScale);
|
|
231
|
+
|
|
232
|
+
const { x, y } = getMouseBoundedPosition(
|
|
233
|
+
newPositionX,
|
|
234
|
+
newPositionY,
|
|
235
|
+
bounds,
|
|
236
|
+
limitToBounds,
|
|
237
|
+
0,
|
|
238
|
+
0,
|
|
239
|
+
wrapperComponent,
|
|
240
|
+
);
|
|
241
|
+
|
|
242
|
+
return { positionX: x, positionY: y, scale: newScale };
|
|
243
|
+
}
|