react-zoom-pan-pinch 3.3.0 → 3.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.
Files changed (186) hide show
  1. package/dist/index.cjs.js +38 -2
  2. package/dist/index.cjs.js.map +1 -1
  3. package/dist/index.d.ts +3 -0
  4. package/dist/index.esm.js +38 -2
  5. package/dist/index.esm.js.map +1 -1
  6. package/package.json +4 -1
  7. package/.eslintignore +0 -6
  8. package/.github/CODEOWNERS +0 -36
  9. package/.github/FUNDING.yml +0 -13
  10. package/.github/ISSUE_TEMPLATE/bug_report.md +0 -38
  11. package/.github/ISSUE_TEMPLATE/feature_request.md +0 -20
  12. package/.github/workflows/docs.yml +0 -34
  13. package/.github/workflows/release.yml +0 -46
  14. package/.github/workflows/tests.yml +0 -42
  15. package/.prettierrc +0 -10
  16. package/.releaserc.json +0 -63
  17. package/.storybook/main.js +0 -23
  18. package/.storybook/preview-head.html +0 -5
  19. package/.storybook/preview.js +0 -11
  20. package/.storybook/theme-overrides.css +0 -49
  21. package/CHANGELOG.md +0 -26
  22. package/Code_of_Conduct.md +0 -61
  23. package/Contributing.md +0 -236
  24. package/__tests__/features/props.spec.tsx +0 -11
  25. package/__tests__/features/rendering.spec.tsx +0 -36
  26. package/__tests__/shared/render.shared.tsx +0 -41
  27. package/__tests__/utils/controls.utils.tsx +0 -25
  28. package/__tests__/utils/example.tsx +0 -81
  29. package/commitlint.config.js +0 -1
  30. package/dist/__tests__/features/props.spec.d.ts +0 -1
  31. package/dist/__tests__/features/rendering.spec.d.ts +0 -1
  32. package/dist/__tests__/shared/render.shared.d.ts +0 -11
  33. package/dist/__tests__/utils/controls.utils.d.ts +0 -3
  34. package/dist/__tests__/utils/example.d.ts +0 -6
  35. package/dist/src/components/index.d.ts +0 -4
  36. package/dist/src/components/keep-scale/keep-scale.d.ts +0 -2
  37. package/dist/src/components/mini-map/mini-map.d.ts +0 -8
  38. package/dist/src/components/mini-map/use-resize.hook.d.ts +0 -8
  39. package/dist/src/components/transform-component/transform-component.d.ts +0 -12
  40. package/dist/src/components/transform-wrapper/transform-wrapper.d.ts +0 -5
  41. package/dist/src/constants/state.constants.d.ts +0 -3
  42. package/dist/src/core/animations/animations.constants.d.ts +0 -16
  43. package/dist/src/core/animations/animations.utils.d.ts +0 -4
  44. package/dist/src/core/bounds/bounds.types.d.ts +0 -8
  45. package/dist/src/core/bounds/bounds.utils.d.ts +0 -17
  46. package/dist/src/core/double-click/double-click.logic.d.ts +0 -5
  47. package/dist/src/core/handlers/handlers.logic.d.ts +0 -8
  48. package/dist/src/core/handlers/handlers.utils.d.ts +0 -14
  49. package/dist/src/core/instance.core.d.ts +0 -77
  50. package/dist/src/core/pan/panning.logic.d.ts +0 -5
  51. package/dist/src/core/pan/panning.utils.d.ts +0 -10
  52. package/dist/src/core/pan/velocity.logic.d.ts +0 -5
  53. package/dist/src/core/pan/velocity.utils.d.ts +0 -5
  54. package/dist/src/core/pinch/pinch.logic.d.ts +0 -4
  55. package/dist/src/core/pinch/pinch.utils.d.ts +0 -6
  56. package/dist/src/core/wheel/wheel.logic.d.ts +0 -4
  57. package/dist/src/core/wheel/wheel.utils.d.ts +0 -7
  58. package/dist/src/core/zoom/zoom.logic.d.ts +0 -3
  59. package/dist/src/core/zoom/zoom.utils.d.ts +0 -3
  60. package/dist/src/hooks/index.d.ts +0 -4
  61. package/dist/src/hooks/use-controls.d.ts +0 -2
  62. package/dist/src/hooks/use-transform-context.d.ts +0 -1
  63. package/dist/src/hooks/use-transform-effect.d.ts +0 -2
  64. package/dist/src/hooks/use-transform-init.d.ts +0 -2
  65. package/dist/src/index.d.ts +0 -5
  66. package/dist/src/models/calculations.model.d.ts +0 -25
  67. package/dist/src/models/context.model.d.ts +0 -127
  68. package/dist/src/models/helpers.model.d.ts +0 -5
  69. package/dist/src/models/index.d.ts +0 -3
  70. package/dist/src/stories/docs/handlers.d.ts +0 -12
  71. package/dist/src/stories/docs/props.d.ts +0 -14
  72. package/dist/src/stories/examples/content-rerendering/example.d.ts +0 -2
  73. package/dist/src/stories/examples/image-responsive/example.d.ts +0 -2
  74. package/dist/src/stories/examples/keep-scale/example.d.ts +0 -2
  75. package/dist/src/stories/examples/mini-map/example.d.ts +0 -1
  76. package/dist/src/stories/examples/stress/example.d.ts +0 -2
  77. package/dist/src/stories/examples/svg-zoom-to-element/example.d.ts +0 -2
  78. package/dist/src/stories/examples/zoom-to-element/example.d.ts +0 -2
  79. package/dist/src/stories/types/args.types.d.ts +0 -280
  80. package/dist/src/stories/utils/args.utils.d.ts +0 -3
  81. package/dist/src/stories/utils/code.utils.d.ts +0 -6
  82. package/dist/src/stories/utils/controls.utils.d.ts +0 -3
  83. package/dist/src/stories/utils/index.d.ts +0 -3
  84. package/dist/src/utils/calculations.utils.d.ts +0 -11
  85. package/dist/src/utils/callback.utils.d.ts +0 -2
  86. package/dist/src/utils/context.utils.d.ts +0 -4
  87. package/dist/src/utils/event.utils.d.ts +0 -1
  88. package/dist/src/utils/helpers.utils.d.ts +0 -2
  89. package/dist/src/utils/index.d.ts +0 -8
  90. package/dist/src/utils/ref.utils.d.ts +0 -2
  91. package/dist/src/utils/state.utils.d.ts +0 -3
  92. package/dist/src/utils/styles.utils.d.ts +0 -4
  93. package/jest.config.ts +0 -45
  94. package/rollup.config.js +0 -51
  95. package/src/.eslintrc.json +0 -90
  96. package/src/components/index.ts +0 -4
  97. package/src/components/keep-scale/keep-scale.tsx +0 -28
  98. package/src/components/mini-map/mini-map.tsx +0 -188
  99. package/src/components/mini-map/use-resize.hook.ts +0 -69
  100. package/src/components/transform-component/transform-component.module.css +0 -30
  101. package/src/components/transform-component/transform-component.tsx +0 -61
  102. package/src/components/transform-wrapper/transform-wrapper.tsx +0 -39
  103. package/src/constants/state.constants.ts +0 -77
  104. package/src/core/animations/animations.constants.ts +0 -78
  105. package/src/core/animations/animations.utils.ts +0 -114
  106. package/src/core/bounds/bounds.types.ts +0 -8
  107. package/src/core/bounds/bounds.utils.ts +0 -187
  108. package/src/core/double-click/double-click.logic.ts +0 -116
  109. package/src/core/handlers/handlers.logic.ts +0 -115
  110. package/src/core/handlers/handlers.utils.ts +0 -195
  111. package/src/core/instance.core.ts +0 -503
  112. package/src/core/pan/panning.logic.ts +0 -100
  113. package/src/core/pan/panning.utils.ts +0 -196
  114. package/src/core/pan/velocity.logic.ts +0 -149
  115. package/src/core/pan/velocity.utils.ts +0 -79
  116. package/src/core/pinch/pinch.logic.ts +0 -81
  117. package/src/core/pinch/pinch.utils.ts +0 -88
  118. package/src/core/wheel/wheel.logic.ts +0 -121
  119. package/src/core/wheel/wheel.utils.ts +0 -122
  120. package/src/core/zoom/zoom.logic.ts +0 -69
  121. package/src/core/zoom/zoom.utils.ts +0 -58
  122. package/src/hooks/index.ts +0 -4
  123. package/src/hooks/use-controls.tsx +0 -9
  124. package/src/hooks/use-transform-context.tsx +0 -13
  125. package/src/hooks/use-transform-effect.tsx +0 -22
  126. package/src/hooks/use-transform-init.tsx +0 -27
  127. package/src/index.ts +0 -6
  128. package/src/models/calculations.model.ts +0 -26
  129. package/src/models/context.model.ts +0 -199
  130. package/src/models/helpers.model.ts +0 -15
  131. package/src/models/index.ts +0 -3
  132. package/src/react-app-env.d.ts +0 -1
  133. package/src/stories/assets/big-image.jpeg +0 -0
  134. package/src/stories/assets/map.jpg +0 -0
  135. package/src/stories/assets/medium-image.jpg +0 -0
  136. package/src/stories/assets/pin.svg +0 -1
  137. package/src/stories/assets/small-image.jpg +0 -0
  138. package/src/stories/assets/svg-example.svg +0 -14
  139. package/src/stories/docs/Introduction.stories.mdx +0 -174
  140. package/src/stories/docs/handlers.stories.mdx +0 -48
  141. package/src/stories/docs/handlers.tsx +0 -107
  142. package/src/stories/docs/props.stories.mdx +0 -92
  143. package/src/stories/docs/props.tsx +0 -526
  144. package/src/stories/examples/content/content.stories.mdx +0 -121
  145. package/src/stories/examples/content-rerendering/content-updating.stories.mdx +0 -45
  146. package/src/stories/examples/content-rerendering/example.tsx +0 -73
  147. package/src/stories/examples/controls/controls.stories.mdx +0 -71
  148. package/src/stories/examples/image-big/image-big.stories.mdx +0 -43
  149. package/src/stories/examples/image-medium/image-medium.stories.mdx +0 -41
  150. package/src/stories/examples/image-responsive/example.tsx +0 -103
  151. package/src/stories/examples/image-responsive/image-responsive.stories.mdx +0 -32
  152. package/src/stories/examples/image-small/image-small.stories.mdx +0 -41
  153. package/src/stories/examples/image-zoomed-out/image-zoomed-out.stories.mdx +0 -46
  154. package/src/stories/examples/keep-scale/example.tsx +0 -92
  155. package/src/stories/examples/keep-scale/keep-scale.stories.mdx +0 -45
  156. package/src/stories/examples/mini-map/example.tsx +0 -54
  157. package/src/stories/examples/mini-map/mini-map.stories.mdx +0 -33
  158. package/src/stories/examples/stress/example.tsx +0 -23
  159. package/src/stories/examples/stress/stress-test.stories.mdx +0 -28
  160. package/src/stories/examples/svg-zoom-to-element/creativity.svg +0 -143
  161. package/src/stories/examples/svg-zoom-to-element/example.tsx +0 -57
  162. package/src/stories/examples/svg-zoom-to-element/svg-zoom-to-element.stories.mdx +0 -28
  163. package/src/stories/examples/zoom-to-element/example.tsx +0 -94
  164. package/src/stories/examples/zoom-to-element/zoom-to-element.stories.mdx +0 -28
  165. package/src/stories/hooks/use-controls.stories.mdx +0 -24
  166. package/src/stories/hooks/use-transform-context.stories.mdx +0 -15
  167. package/src/stories/hooks/use-transform-effect.stories.mdx +0 -29
  168. package/src/stories/hooks/use-transform-init.stories.mdx +0 -25
  169. package/src/stories/types/args.types.ts +0 -211
  170. package/src/stories/utils/args.utils.ts +0 -34
  171. package/src/stories/utils/code.utils.tsx +0 -24
  172. package/src/stories/utils/controls.utils.tsx +0 -43
  173. package/src/stories/utils/index.ts +0 -3
  174. package/src/stories/utils/styles.module.css +0 -34
  175. package/src/styles.css +0 -8
  176. package/src/typings.d.ts +0 -10
  177. package/src/utils/calculations.utils.ts +0 -16
  178. package/src/utils/callback.utils.ts +0 -11
  179. package/src/utils/context.utils.ts +0 -48
  180. package/src/utils/event.utils.ts +0 -22
  181. package/src/utils/helpers.utils.ts +0 -27
  182. package/src/utils/index.ts +0 -8
  183. package/src/utils/ref.utils.ts +0 -16
  184. package/src/utils/state.utils.ts +0 -40
  185. package/src/utils/styles.utils.ts +0 -44
  186. package/tsconfig.json +0 -33
@@ -1,187 +0,0 @@
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 ? 1 : 0.5)
48
- : 0;
49
- const scaleHeightFactor =
50
- wrapperHeight > newContentHeight
51
- ? diffHeight * (centerZoomedOut ? 1 : 0.5)
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 { minPositionX, maxPositionX, minPositionY, maxPositionY };
60
- };
61
-
62
- export const calculateBounds = (
63
- contextInstance: ReactZoomPanPinchContext,
64
- newScale: number,
65
- ): BoundsType => {
66
- const { wrapperComponent, contentComponent } = contextInstance;
67
- const { centerZoomedOut } = contextInstance.setup;
68
-
69
- if (!wrapperComponent || !contentComponent) {
70
- throw new Error("Components are not mounted");
71
- }
72
-
73
- const {
74
- wrapperWidth,
75
- wrapperHeight,
76
- newContentWidth,
77
- newDiffWidth,
78
- newContentHeight,
79
- newDiffHeight,
80
- } = getComponentsSizes(wrapperComponent, contentComponent, newScale);
81
-
82
- const bounds = getBounds(
83
- wrapperWidth,
84
- newContentWidth,
85
- newDiffWidth,
86
- wrapperHeight,
87
- newContentHeight,
88
- newDiffHeight,
89
- Boolean(centerZoomedOut),
90
- );
91
- return bounds;
92
- };
93
-
94
- export function clamp(v: number, min: number, max: number) {
95
- return Math.max(min, Math.min(v, max));
96
- }
97
-
98
- // Based on @aholachek ;)
99
- // https://twitter.com/chpwn/status/285540192096497664
100
- // iOS constant = 0.55
101
-
102
- // https://medium.com/@nathangitter/building-fluid-interfaces-ios-swift-9732bb934bf5
103
-
104
- function rubberband(distance: number, dimension: number, constant: number) {
105
- if (dimension === 0 || Math.abs(dimension) === Infinity)
106
- return distance ** (constant * 5);
107
- return (distance * dimension * constant) / (dimension + constant * distance);
108
- }
109
-
110
- export function rubberbandIfOutOfBounds(
111
- position: number,
112
- min: number,
113
- max: number,
114
- constant = 0.15,
115
- ) {
116
- if (constant === 0) return clamp(position, min, max);
117
- if (position < min)
118
- return -rubberband(min - position, max - min, constant) + min;
119
- if (position > max)
120
- return +rubberband(position - max, max - min, constant) + max;
121
- return position;
122
- }
123
-
124
- /**
125
- * Keeps value between given bounds, used for limiting view to given boundaries
126
- * 1# eg. boundLimiter(2, 0, 3, true) => 2
127
- * 2# eg. boundLimiter(4, 0, 3, true) => 3
128
- * 3# eg. boundLimiter(-2, 0, 3, true) => 0
129
- * 4# eg. boundLimiter(10, 0, 3, false) => 10
130
- */
131
- export const boundLimiter = (
132
- value: number,
133
- minBound: number,
134
- maxBound: number,
135
- isActive: boolean,
136
- ): number => {
137
- if (!isActive) return roundNumber(value, 2);
138
- if (value < minBound) return roundNumber(minBound, 2);
139
- if (value > maxBound) return roundNumber(maxBound, 2);
140
- return roundNumber(value, 2);
141
- };
142
-
143
- export const handleCalculateBounds = (
144
- contextInstance: ReactZoomPanPinchContext,
145
- newScale: number,
146
- ): BoundsType => {
147
- const bounds = calculateBounds(contextInstance, newScale);
148
-
149
- // Save bounds
150
- contextInstance.bounds = bounds;
151
- return bounds;
152
- };
153
-
154
- export function getMouseBoundedPosition(
155
- positionX: number,
156
- positionY: number,
157
- bounds: BoundsType,
158
- limitToBounds: boolean,
159
- paddingValueX: number,
160
- paddingValueY: number,
161
- wrapperComponent: HTMLDivElement | null,
162
- ): PositionType {
163
- const { minPositionX, minPositionY, maxPositionX, maxPositionY } = bounds;
164
-
165
- let paddingX = 0;
166
- let paddingY = 0;
167
-
168
- if (wrapperComponent) {
169
- paddingX = paddingValueX;
170
- paddingY = paddingValueY;
171
- }
172
-
173
- const x = boundLimiter(
174
- positionX,
175
- minPositionX - paddingX,
176
- maxPositionX + paddingX,
177
- limitToBounds,
178
- );
179
-
180
- const y = boundLimiter(
181
- positionY,
182
- minPositionY - paddingY,
183
- maxPositionY + paddingY,
184
- limitToBounds,
185
- );
186
- return { x, y };
187
- }
@@ -1,116 +0,0 @@
1
- /* eslint-disable no-param-reassign */
2
- import { 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
- export function handleDoubleClick(
48
- contextInstance: ReactZoomPanPinchContext,
49
- event: MouseEvent | TouchEvent,
50
- ): void {
51
- const { setup, doubleClickStopEventTimer, transformState, contentComponent } =
52
- contextInstance;
53
-
54
- const { scale } = transformState;
55
- const { onZoomStart, onZoom } = contextInstance.props;
56
- const { disabled, mode, step, animationTime, animationType } =
57
- setup.doubleClick;
58
-
59
- if (disabled) return;
60
- if (doubleClickStopEventTimer) return;
61
-
62
- if (mode === "reset") {
63
- return handleDoubleClickResetMode(contextInstance, event);
64
- }
65
-
66
- if (!contentComponent) return console.error("No ContentComponent found");
67
-
68
- const delta = mode === "zoomOut" ? -1 : 1;
69
-
70
- const newScale = handleCalculateButtonZoom(contextInstance, delta, step);
71
-
72
- // stop execution when scale didn't change
73
- if (scale === newScale) return;
74
-
75
- handleCallback(getContext(contextInstance), event, onZoomStart);
76
-
77
- const mousePosition = getMousePosition(event, contentComponent, scale);
78
- const targetState = handleZoomToPoint(
79
- contextInstance,
80
- newScale,
81
- mousePosition.x,
82
- mousePosition.y,
83
- );
84
-
85
- if (!targetState) {
86
- return console.error(
87
- "Error during zoom event. New transformation state was not calculated.",
88
- );
89
- }
90
-
91
- handleCallback(getContext(contextInstance), event, onZoom);
92
-
93
- animate(contextInstance, targetState, animationTime, animationType);
94
-
95
- handleDoubleClickStop(contextInstance, event);
96
- }
97
-
98
- export const isDoubleClickAllowed = (
99
- contextInstance: ReactZoomPanPinchContext,
100
- event: MouseEvent | TouchEvent,
101
- ): boolean => {
102
- const { isInitialized, setup, wrapperComponent } = contextInstance;
103
- const { disabled, excluded } = setup.doubleClick;
104
-
105
- const target = event.target as HTMLElement;
106
- const isWrapperChild = wrapperComponent?.contains(target);
107
- const isAllowed = isInitialized && target && isWrapperChild && !disabled;
108
-
109
- if (!isAllowed) return false;
110
-
111
- const isExcluded = isExcludedNode(target, excluded);
112
-
113
- if (isExcluded) return false;
114
-
115
- return true;
116
- };
@@ -1,115 +0,0 @@
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.transformState;
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 { transformState, wrapperComponent, contentComponent } =
84
- contextInstance;
85
- if (wrapperComponent && contentComponent) {
86
- const targetState = getCenterPosition(
87
- scale || transformState.scale,
88
- wrapperComponent,
89
- contentComponent,
90
- );
91
-
92
- animate(contextInstance, targetState, animationTime, animationType);
93
- }
94
- };
95
-
96
- export const zoomToElement =
97
- (contextInstance: ReactZoomPanPinchContext) =>
98
- (
99
- node: HTMLElement | string,
100
- scale?: number,
101
- animationTime = 600,
102
- animationType: keyof typeof animations = "easeOut",
103
- ): void => {
104
- handleCancelAnimation(contextInstance);
105
-
106
- const { wrapperComponent } = contextInstance;
107
-
108
- const target: HTMLElement | null =
109
- typeof node === "string" ? document.getElementById(node) : node;
110
-
111
- if (wrapperComponent && target && wrapperComponent.contains(target)) {
112
- const targetState = calculateZoomToNode(contextInstance, target, scale);
113
- animate(contextInstance, targetState, animationTime, animationType);
114
- }
115
- };
@@ -1,195 +0,0 @@
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 { roundNumber } from "../../utils";
8
- import {
9
- calculateBounds,
10
- getMouseBoundedPosition,
11
- } from "../bounds/bounds.utils";
12
-
13
- export const handleCalculateButtonZoom = (
14
- contextInstance: ReactZoomPanPinchContext,
15
- delta: number,
16
- step: number,
17
- ): number => {
18
- const { scale } = contextInstance.transformState;
19
- const { wrapperComponent, setup } = contextInstance;
20
- const { maxScale, minScale, zoomAnimation, smooth } = setup;
21
- const { size } = zoomAnimation;
22
-
23
- if (!wrapperComponent) {
24
- throw new Error("Wrapper is not mounted");
25
- }
26
-
27
- const targetScale = smooth
28
- ? scale * Math.exp(delta * step)
29
- : scale + delta * step;
30
-
31
- const newScale = checkZoomBounds(
32
- roundNumber(targetScale, 3),
33
- minScale,
34
- maxScale,
35
- size,
36
- false,
37
- );
38
- return newScale;
39
- };
40
-
41
- export function handleZoomToViewCenter(
42
- contextInstance: ReactZoomPanPinchContext,
43
- delta: number,
44
- step: number,
45
- animationTime: number,
46
- animationType: keyof typeof animations,
47
- ): void {
48
- const { wrapperComponent } = contextInstance;
49
- const { scale, positionX, positionY } = contextInstance.transformState;
50
-
51
- if (!wrapperComponent) return console.error("No WrapperComponent found");
52
-
53
- const wrapperWidth = wrapperComponent.offsetWidth;
54
- const wrapperHeight = wrapperComponent.offsetHeight;
55
- const mouseX = (wrapperWidth / 2 - positionX) / scale;
56
- const mouseY = (wrapperHeight / 2 - positionY) / scale;
57
-
58
- const newScale = handleCalculateButtonZoom(contextInstance, delta, step);
59
-
60
- const targetState = handleZoomToPoint(
61
- contextInstance,
62
- newScale,
63
- mouseX,
64
- mouseY,
65
- );
66
-
67
- if (!targetState) {
68
- return console.error(
69
- "Error during zoom event. New transformation state was not calculated.",
70
- );
71
- }
72
-
73
- animate(contextInstance, targetState, animationTime, animationType);
74
- }
75
-
76
- export function resetTransformations(
77
- contextInstance: ReactZoomPanPinchContext,
78
- animationTime: number,
79
- animationType: keyof typeof animations,
80
- onResetTransformation?: () => void,
81
- ): void {
82
- const { setup, wrapperComponent } = contextInstance;
83
- const { limitToBounds } = setup;
84
- const initialTransformation = createState(contextInstance.props);
85
- const { scale, positionX, positionY } = contextInstance.transformState;
86
-
87
- if (!wrapperComponent) return;
88
-
89
- const newBounds = calculateBounds(
90
- contextInstance,
91
- initialTransformation.scale,
92
- );
93
-
94
- const boundedPositions = getMouseBoundedPosition(
95
- initialTransformation.positionX,
96
- initialTransformation.positionY,
97
- newBounds,
98
- limitToBounds,
99
- 0,
100
- 0,
101
- wrapperComponent,
102
- );
103
-
104
- const newState = {
105
- scale: initialTransformation.scale,
106
- positionX: boundedPositions.x,
107
- positionY: boundedPositions.y,
108
- };
109
-
110
- if (
111
- scale === initialTransformation.scale &&
112
- positionX === initialTransformation.positionX &&
113
- positionY === initialTransformation.positionY
114
- ) {
115
- return;
116
- }
117
-
118
- onResetTransformation?.();
119
- animate(contextInstance, newState, animationTime, animationType);
120
- }
121
-
122
- export function getOffset(
123
- element: HTMLElement,
124
- wrapper: HTMLElement,
125
- content: HTMLElement,
126
- state: ReactZoomPanPinchState,
127
- ) {
128
- const offset = element.getBoundingClientRect();
129
- const wrapperOffset = wrapper.getBoundingClientRect();
130
- const contentOffset = content.getBoundingClientRect();
131
-
132
- const xOff = wrapperOffset.x * state.scale;
133
- const yOff = wrapperOffset.y * state.scale;
134
-
135
- return {
136
- x: (offset.x - contentOffset.x + xOff) / state.scale,
137
- y: (offset.y - contentOffset.y + yOff) / state.scale,
138
- };
139
- }
140
-
141
- export function calculateZoomToNode(
142
- contextInstance: ReactZoomPanPinchContext,
143
- node: HTMLElement,
144
- customZoom?: number,
145
- ): { positionX: number; positionY: number; scale: number } {
146
- const { wrapperComponent, contentComponent, transformState } =
147
- contextInstance;
148
- const { limitToBounds, minScale, maxScale } = contextInstance.setup;
149
- if (!wrapperComponent || !contentComponent) return transformState;
150
-
151
- const wrapperRect = wrapperComponent.getBoundingClientRect();
152
- const nodeRect = node.getBoundingClientRect();
153
- const nodeOffset = getOffset(
154
- node,
155
- wrapperComponent,
156
- contentComponent,
157
- transformState,
158
- );
159
-
160
- const nodeLeft = nodeOffset.x;
161
- const nodeTop = nodeOffset.y;
162
- const nodeWidth = nodeRect.width / transformState.scale;
163
- const nodeHeight = nodeRect.height / transformState.scale;
164
-
165
- const scaleX = wrapperComponent.offsetWidth / nodeWidth;
166
- const scaleY = wrapperComponent.offsetHeight / nodeHeight;
167
-
168
- const newScale = checkZoomBounds(
169
- customZoom || Math.min(scaleX, scaleY),
170
- minScale,
171
- maxScale,
172
- 0,
173
- false,
174
- );
175
-
176
- const offsetX = (wrapperRect.width - nodeWidth * newScale) / 2;
177
- const offsetY = (wrapperRect.height - nodeHeight * newScale) / 2;
178
-
179
- const newPositionX = (wrapperRect.left - nodeLeft) * newScale + offsetX;
180
- const newPositionY = (wrapperRect.top - nodeTop) * newScale + offsetY;
181
-
182
- const bounds = calculateBounds(contextInstance, newScale);
183
-
184
- const { x, y } = getMouseBoundedPosition(
185
- newPositionX,
186
- newPositionY,
187
- bounds,
188
- limitToBounds,
189
- 0,
190
- 0,
191
- wrapperComponent,
192
- );
193
-
194
- return { positionX: x, positionY: y, scale: newScale };
195
- }