react-zoom-pan-pinch 2.1.3 → 2.2.1
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/README.md +43 -0
- package/dist/index.cjs.js +60 -34
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.d.ts +9 -1
- package/dist/index.esm.js +60 -34
- package/dist/index.esm.js.map +1 -1
- package/package.json +5 -4
- 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 +35 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-zoom-pan-pinch",
|
|
3
|
-
"version": "2.1
|
|
3
|
+
"version": "2.2.1",
|
|
4
4
|
"description": "Zoom and pan html elements in easy way",
|
|
5
5
|
"author": "prc5",
|
|
6
6
|
"license": "MIT",
|
|
@@ -27,8 +27,8 @@
|
|
|
27
27
|
"test:prettier": "npm run prettier -check \"**/*.+(js|json|ts|tsx)\""
|
|
28
28
|
},
|
|
29
29
|
"peerDependencies": {
|
|
30
|
-
"react": "
|
|
31
|
-
"react-dom": "
|
|
30
|
+
"react": "*",
|
|
31
|
+
"react-dom": "*"
|
|
32
32
|
},
|
|
33
33
|
"devDependencies": {
|
|
34
34
|
"@babel/core": "^7.13.14",
|
|
@@ -78,7 +78,8 @@
|
|
|
78
78
|
"webpack-cli": "^4.6.0"
|
|
79
79
|
},
|
|
80
80
|
"files": [
|
|
81
|
-
"dist"
|
|
81
|
+
"dist",
|
|
82
|
+
"src"
|
|
82
83
|
],
|
|
83
84
|
"eslintConfig": {
|
|
84
85
|
"extends": [
|
package/src/.eslintrc.js
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
module.exports = {
|
|
2
|
+
root: true,
|
|
3
|
+
parser: "@typescript-eslint/parser",
|
|
4
|
+
plugins: ["react", "react-hooks", "prettier", "@typescript-eslint"],
|
|
5
|
+
extends: [
|
|
6
|
+
"plugin:react/recommended",
|
|
7
|
+
"plugin:@typescript-eslint/recommended",
|
|
8
|
+
],
|
|
9
|
+
parserOptions: {
|
|
10
|
+
sourceType: "module",
|
|
11
|
+
ecmaFeatures: {
|
|
12
|
+
jsx: true,
|
|
13
|
+
},
|
|
14
|
+
},
|
|
15
|
+
rules: {
|
|
16
|
+
"react/jsx-uses-react": "error",
|
|
17
|
+
"react/jsx-uses-vars": "error",
|
|
18
|
+
"no-console": ["error", { allow: ["warn", "error"] }],
|
|
19
|
+
"no-unused-vars": "off",
|
|
20
|
+
"@typescript-eslint/no-unused-vars": "off",
|
|
21
|
+
"@typescript-eslint/no-unused-vars-experimental": "error",
|
|
22
|
+
"react/no-unescaped-entities": 0,
|
|
23
|
+
"react/display-name": 0,
|
|
24
|
+
"react/jsx-filename-extension": [
|
|
25
|
+
2,
|
|
26
|
+
{ extensions: [".js", ".jsx", ".ts", ".tsx"] },
|
|
27
|
+
],
|
|
28
|
+
},
|
|
29
|
+
settings: {
|
|
30
|
+
"import/resolver": {
|
|
31
|
+
node: {
|
|
32
|
+
moduleDirectory: ["node_modules", "src/"],
|
|
33
|
+
},
|
|
34
|
+
},
|
|
35
|
+
},
|
|
36
|
+
};
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
.wrapper {
|
|
2
|
+
position: relative;
|
|
3
|
+
width: -moz-fit-content;
|
|
4
|
+
width: fit-content;
|
|
5
|
+
height: -moz-fit-content;
|
|
6
|
+
height: fit-content;
|
|
7
|
+
overflow: hidden;
|
|
8
|
+
-webkit-touch-callout: none; /* iOS Safari */
|
|
9
|
+
-webkit-user-select: none; /* Safari */
|
|
10
|
+
-khtml-user-select: none; /* Konqueror HTML */
|
|
11
|
+
-moz-user-select: none; /* Firefox */
|
|
12
|
+
-ms-user-select: none; /* Internet Explorer/Edge */
|
|
13
|
+
user-select: none;
|
|
14
|
+
margin: 0;
|
|
15
|
+
padding: 0;
|
|
16
|
+
}
|
|
17
|
+
.content {
|
|
18
|
+
display: flex;
|
|
19
|
+
flex-wrap: wrap;
|
|
20
|
+
width: -moz-fit-content;
|
|
21
|
+
width: fit-content;
|
|
22
|
+
height: -moz-fit-content;
|
|
23
|
+
height: fit-content;
|
|
24
|
+
margin: 0;
|
|
25
|
+
padding: 0;
|
|
26
|
+
transform-origin: 0% 0%;
|
|
27
|
+
}
|
|
28
|
+
.content img {
|
|
29
|
+
pointer-events: none;
|
|
30
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import React, { useContext, useEffect, useRef } from "react";
|
|
2
|
+
|
|
3
|
+
import { Context } from "./transform-context";
|
|
4
|
+
|
|
5
|
+
import styles from "./transform-component.module.css";
|
|
6
|
+
|
|
7
|
+
type Props = {
|
|
8
|
+
children: React.ReactNode;
|
|
9
|
+
wrapperClass?: string;
|
|
10
|
+
contentClass?: string;
|
|
11
|
+
wrapperStyle?: React.CSSProperties;
|
|
12
|
+
contentStyle?: React.CSSProperties;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
export const TransformComponent: React.FC<Props> = ({
|
|
16
|
+
children,
|
|
17
|
+
wrapperClass = "",
|
|
18
|
+
contentClass = "",
|
|
19
|
+
wrapperStyle,
|
|
20
|
+
contentStyle,
|
|
21
|
+
}: Props) => {
|
|
22
|
+
const { setComponents } = useContext(Context);
|
|
23
|
+
|
|
24
|
+
const wrapperRef = useRef<HTMLDivElement | null>(null);
|
|
25
|
+
const contentRef = useRef<HTMLDivElement | null>(null);
|
|
26
|
+
|
|
27
|
+
useEffect(() => {
|
|
28
|
+
const wrapper = wrapperRef.current;
|
|
29
|
+
const content = contentRef.current;
|
|
30
|
+
if (wrapper !== null && content !== null && setComponents) {
|
|
31
|
+
setComponents(wrapper, content);
|
|
32
|
+
}
|
|
33
|
+
}, []);
|
|
34
|
+
|
|
35
|
+
return (
|
|
36
|
+
<div
|
|
37
|
+
ref={wrapperRef}
|
|
38
|
+
className={`react-transform-wrapper ${styles.wrapper} ${wrapperClass}`}
|
|
39
|
+
style={wrapperStyle}
|
|
40
|
+
>
|
|
41
|
+
<div
|
|
42
|
+
ref={contentRef}
|
|
43
|
+
className={`react-transform-component ${styles.content} ${contentClass}`}
|
|
44
|
+
style={contentStyle}
|
|
45
|
+
>
|
|
46
|
+
{children}
|
|
47
|
+
</div>
|
|
48
|
+
</div>
|
|
49
|
+
);
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
export default TransformComponent;
|
|
@@ -0,0 +1,493 @@
|
|
|
1
|
+
import React, { Component } from "react";
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
BoundsType,
|
|
5
|
+
LibrarySetup,
|
|
6
|
+
PositionType,
|
|
7
|
+
VelocityType,
|
|
8
|
+
AnimationType,
|
|
9
|
+
ReactZoomPanPinchRef,
|
|
10
|
+
ReactZoomPanPinchProps,
|
|
11
|
+
ReactZoomPanPinchState,
|
|
12
|
+
} from "../models";
|
|
13
|
+
|
|
14
|
+
import {
|
|
15
|
+
getContext,
|
|
16
|
+
createSetup,
|
|
17
|
+
createState,
|
|
18
|
+
handleCallback,
|
|
19
|
+
getTransformStyles,
|
|
20
|
+
makePassiveEventOption,
|
|
21
|
+
getCenterPosition,
|
|
22
|
+
} from "../utils";
|
|
23
|
+
|
|
24
|
+
import { contextInitialState } from "../constants/state.constants";
|
|
25
|
+
|
|
26
|
+
import { handleCancelAnimation } from "../core/animations/animations.utils";
|
|
27
|
+
import { isWheelAllowed } from "../core/wheel/wheel.utils";
|
|
28
|
+
import { isPinchAllowed, isPinchStartAllowed } from "../core/pinch/pinch.utils";
|
|
29
|
+
import { handleCalculateBounds } from "../core/bounds/bounds.utils";
|
|
30
|
+
|
|
31
|
+
import {
|
|
32
|
+
handleWheelStart,
|
|
33
|
+
handleWheelZoom,
|
|
34
|
+
handleWheelStop,
|
|
35
|
+
} from "../core/wheel/wheel.logic";
|
|
36
|
+
import {
|
|
37
|
+
isPanningAllowed,
|
|
38
|
+
isPanningStartAllowed,
|
|
39
|
+
} from "../core/pan/panning.utils";
|
|
40
|
+
import {
|
|
41
|
+
handlePanning,
|
|
42
|
+
handlePanningEnd,
|
|
43
|
+
handlePanningStart,
|
|
44
|
+
} from "../core/pan/panning.logic";
|
|
45
|
+
import {
|
|
46
|
+
handlePinchStart,
|
|
47
|
+
handlePinchStop,
|
|
48
|
+
handlePinchZoom,
|
|
49
|
+
} from "../core/pinch/pinch.logic";
|
|
50
|
+
import {
|
|
51
|
+
handleDoubleClick,
|
|
52
|
+
isDoubleClickAllowed,
|
|
53
|
+
} from "../core/double-click/double-click.logic";
|
|
54
|
+
|
|
55
|
+
type StartCoordsType = { x: number; y: number } | null;
|
|
56
|
+
|
|
57
|
+
const Context = React.createContext(contextInitialState);
|
|
58
|
+
|
|
59
|
+
class TransformContext extends Component<
|
|
60
|
+
Omit<ReactZoomPanPinchProps, "ref"> & {
|
|
61
|
+
setRef: (context: ReactZoomPanPinchRef) => void;
|
|
62
|
+
}
|
|
63
|
+
> {
|
|
64
|
+
public mounted = true;
|
|
65
|
+
|
|
66
|
+
public transformState: ReactZoomPanPinchState = createState(this.props);
|
|
67
|
+
|
|
68
|
+
public setup: LibrarySetup = createSetup(this.props);
|
|
69
|
+
|
|
70
|
+
// Components
|
|
71
|
+
public wrapperComponent: HTMLDivElement | null = null;
|
|
72
|
+
public contentComponent: HTMLDivElement | null = null;
|
|
73
|
+
// Initialization
|
|
74
|
+
public isInitialized = false;
|
|
75
|
+
public bounds: BoundsType | null = null;
|
|
76
|
+
// wheel helpers
|
|
77
|
+
public previousWheelEvent: WheelEvent | null = null;
|
|
78
|
+
public wheelStopEventTimer: ReturnType<typeof setTimeout> | null = null;
|
|
79
|
+
public wheelAnimationTimer: ReturnType<typeof setTimeout> | null = null;
|
|
80
|
+
// panning helpers
|
|
81
|
+
public isPanning = false;
|
|
82
|
+
public startCoords: StartCoordsType = null;
|
|
83
|
+
public lastTouch: number | null = null;
|
|
84
|
+
// pinch helpers
|
|
85
|
+
public distance: null | number = null;
|
|
86
|
+
public lastDistance: null | number = null;
|
|
87
|
+
public pinchStartDistance: null | number = null;
|
|
88
|
+
public pinchStartScale: null | number = null;
|
|
89
|
+
public pinchMidpoint: null | PositionType = null;
|
|
90
|
+
// velocity helpers
|
|
91
|
+
public velocity: VelocityType | null = null;
|
|
92
|
+
public velocityTime: number | null = null;
|
|
93
|
+
public lastMousePosition: PositionType | null = null;
|
|
94
|
+
// animations helpers
|
|
95
|
+
public animate = false;
|
|
96
|
+
public animation: AnimationType | null = null;
|
|
97
|
+
public maxBounds: BoundsType | null = null;
|
|
98
|
+
// key press
|
|
99
|
+
public pressedKeys: { [key: string]: boolean } = {};
|
|
100
|
+
|
|
101
|
+
componentDidMount(): void {
|
|
102
|
+
this.initializeWindowEvents();
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
componentWillUnmount(): void {
|
|
106
|
+
this.cleanupWindowEvents();
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
componentDidUpdate(oldProps: ReactZoomPanPinchProps): void {
|
|
110
|
+
if (oldProps !== this.props) {
|
|
111
|
+
handleCalculateBounds(this, this.transformState.scale);
|
|
112
|
+
this.setup = createSetup(this.props);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
initializeWindowEvents = (): void => {
|
|
117
|
+
const passive = makePassiveEventOption();
|
|
118
|
+
const currentDocument = this.wrapperComponent?.ownerDocument;
|
|
119
|
+
const currentWindow = currentDocument?.defaultView;
|
|
120
|
+
// Panning on window to allow panning when mouse is out of component wrapper
|
|
121
|
+
currentWindow?.addEventListener("mousedown", this.onPanningStart, passive);
|
|
122
|
+
currentWindow?.addEventListener("mousemove", this.onPanning, passive);
|
|
123
|
+
currentWindow?.addEventListener("mouseup", this.onPanningStop, passive);
|
|
124
|
+
currentDocument?.addEventListener("mouseleave", this.clearPanning, passive);
|
|
125
|
+
currentWindow?.addEventListener("keyup", this.setKeyUnPressed, passive);
|
|
126
|
+
currentWindow?.addEventListener("keydown", this.setKeyPressed, passive);
|
|
127
|
+
|
|
128
|
+
this.handleRef();
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
cleanupWindowEvents = (): void => {
|
|
132
|
+
const passive = makePassiveEventOption();
|
|
133
|
+
const currentDocument = this.wrapperComponent?.ownerDocument;
|
|
134
|
+
const currentWindow = currentDocument?.defaultView;
|
|
135
|
+
currentWindow?.removeEventListener("mousedown", this.onPanningStart, passive);
|
|
136
|
+
currentWindow?.removeEventListener("mousemove", this.onPanning, passive);
|
|
137
|
+
currentWindow?.removeEventListener("mouseup", this.onPanningStop, passive);
|
|
138
|
+
currentDocument?.removeEventListener("mouseleave", this.clearPanning, passive);
|
|
139
|
+
currentWindow?.removeEventListener("keyup", this.setKeyUnPressed, passive);
|
|
140
|
+
currentWindow?.removeEventListener("keydown", this.setKeyPressed, passive);
|
|
141
|
+
document.removeEventListener("mouseleave", this.clearPanning, passive);
|
|
142
|
+
|
|
143
|
+
handleCancelAnimation(this);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
handleInitializeWrapperEvents = (wrapper: HTMLDivElement): void => {
|
|
147
|
+
// Zooming events on wrapper
|
|
148
|
+
const passive = makePassiveEventOption();
|
|
149
|
+
|
|
150
|
+
wrapper.addEventListener("wheel", this.onWheelZoom, passive);
|
|
151
|
+
wrapper.addEventListener("dblclick", this.onDoubleClick, passive);
|
|
152
|
+
wrapper.addEventListener("touchstart", this.onTouchPanningStart, passive);
|
|
153
|
+
wrapper.addEventListener("touchmove", this.onTouchPanning, passive);
|
|
154
|
+
wrapper.addEventListener("touchend", this.onTouchPanningStop, passive);
|
|
155
|
+
};
|
|
156
|
+
|
|
157
|
+
handleInitialize = (): void => {
|
|
158
|
+
const { centerOnInit } = this.setup;
|
|
159
|
+
|
|
160
|
+
this.applyTransformation();
|
|
161
|
+
this.forceUpdate();
|
|
162
|
+
|
|
163
|
+
if (centerOnInit) {
|
|
164
|
+
// this has to be redone once the right solution is found
|
|
165
|
+
// problem is - we need to execute it after mounted component specify it's height / width, images are fetched async so it's tricky
|
|
166
|
+
setTimeout(() => {
|
|
167
|
+
if (this.mounted) {
|
|
168
|
+
this.setCenter();
|
|
169
|
+
}
|
|
170
|
+
}, 50);
|
|
171
|
+
setTimeout(() => {
|
|
172
|
+
if (this.mounted) {
|
|
173
|
+
this.setCenter();
|
|
174
|
+
}
|
|
175
|
+
}, 100);
|
|
176
|
+
setTimeout(() => {
|
|
177
|
+
if (this.mounted) {
|
|
178
|
+
this.setCenter();
|
|
179
|
+
}
|
|
180
|
+
}, 200);
|
|
181
|
+
}
|
|
182
|
+
};
|
|
183
|
+
|
|
184
|
+
//////////
|
|
185
|
+
// Zoom
|
|
186
|
+
//////////
|
|
187
|
+
|
|
188
|
+
onWheelZoom = (event: WheelEvent): void => {
|
|
189
|
+
const { disabled } = this.setup;
|
|
190
|
+
if (disabled) return;
|
|
191
|
+
|
|
192
|
+
const isAllowed = isWheelAllowed(this, event);
|
|
193
|
+
if (!isAllowed) return;
|
|
194
|
+
|
|
195
|
+
const keysPressed = this.isPressingKeys(this.setup.wheel.activationKeys);
|
|
196
|
+
if (!keysPressed) return;
|
|
197
|
+
|
|
198
|
+
handleWheelStart(this, event);
|
|
199
|
+
handleWheelZoom(this, event);
|
|
200
|
+
handleWheelStop(this, event);
|
|
201
|
+
};
|
|
202
|
+
|
|
203
|
+
//////////
|
|
204
|
+
// Pan
|
|
205
|
+
//////////
|
|
206
|
+
|
|
207
|
+
onPanningStart = (event: MouseEvent): void => {
|
|
208
|
+
const { disabled } = this.setup;
|
|
209
|
+
const { onPanningStart } = this.props;
|
|
210
|
+
if (disabled) return;
|
|
211
|
+
|
|
212
|
+
const isAllowed = isPanningStartAllowed(this, event);
|
|
213
|
+
if (!isAllowed) return;
|
|
214
|
+
|
|
215
|
+
const keysPressed = this.isPressingKeys(this.setup.panning.activationKeys);
|
|
216
|
+
if (!keysPressed) return;
|
|
217
|
+
|
|
218
|
+
event.preventDefault();
|
|
219
|
+
event.stopPropagation();
|
|
220
|
+
|
|
221
|
+
handleCancelAnimation(this);
|
|
222
|
+
handlePanningStart(this, event);
|
|
223
|
+
handleCallback(getContext(this), event, onPanningStart);
|
|
224
|
+
};
|
|
225
|
+
|
|
226
|
+
onPanning = (event: MouseEvent): void => {
|
|
227
|
+
const { disabled } = this.setup;
|
|
228
|
+
const { onPanning } = this.props;
|
|
229
|
+
|
|
230
|
+
if (disabled) return;
|
|
231
|
+
|
|
232
|
+
const isAllowed = isPanningAllowed(this);
|
|
233
|
+
if (!isAllowed) return;
|
|
234
|
+
|
|
235
|
+
const keysPressed = this.isPressingKeys(this.setup.panning.activationKeys);
|
|
236
|
+
if (!keysPressed) return;
|
|
237
|
+
|
|
238
|
+
event.preventDefault();
|
|
239
|
+
event.stopPropagation();
|
|
240
|
+
|
|
241
|
+
handlePanning(this, event.clientX, event.clientY);
|
|
242
|
+
handleCallback(getContext(this), event, onPanning);
|
|
243
|
+
};
|
|
244
|
+
|
|
245
|
+
onPanningStop = (event: MouseEvent | TouchEvent): void => {
|
|
246
|
+
const { onPanningStop } = this.props;
|
|
247
|
+
|
|
248
|
+
if (this.isPanning) {
|
|
249
|
+
handlePanningEnd(this);
|
|
250
|
+
handleCallback(getContext(this), event, onPanningStop);
|
|
251
|
+
}
|
|
252
|
+
};
|
|
253
|
+
|
|
254
|
+
//////////
|
|
255
|
+
// Pinch
|
|
256
|
+
//////////
|
|
257
|
+
|
|
258
|
+
onPinchStart = (event: TouchEvent): void => {
|
|
259
|
+
const { disabled } = this.setup;
|
|
260
|
+
const { onPinchingStart, onZoomStart } = this.props;
|
|
261
|
+
|
|
262
|
+
if (disabled) return;
|
|
263
|
+
|
|
264
|
+
const isAllowed = isPinchStartAllowed(this, event);
|
|
265
|
+
if (!isAllowed) return;
|
|
266
|
+
|
|
267
|
+
handlePinchStart(this, event);
|
|
268
|
+
handleCancelAnimation(this);
|
|
269
|
+
handleCallback(getContext(this), event, onPinchingStart);
|
|
270
|
+
handleCallback(getContext(this), event, onZoomStart);
|
|
271
|
+
};
|
|
272
|
+
|
|
273
|
+
onPinch = (event: TouchEvent): void => {
|
|
274
|
+
const { disabled } = this.setup;
|
|
275
|
+
const { onPinching, onZoom } = this.props;
|
|
276
|
+
|
|
277
|
+
if (disabled) return;
|
|
278
|
+
|
|
279
|
+
const isAllowed = isPinchAllowed(this);
|
|
280
|
+
if (!isAllowed) return;
|
|
281
|
+
|
|
282
|
+
event.preventDefault();
|
|
283
|
+
event.stopPropagation();
|
|
284
|
+
|
|
285
|
+
handlePinchZoom(this, event);
|
|
286
|
+
handleCallback(getContext(this), event, onPinching);
|
|
287
|
+
handleCallback(getContext(this), event, onZoom);
|
|
288
|
+
};
|
|
289
|
+
|
|
290
|
+
onPinchStop = (event: TouchEvent): void => {
|
|
291
|
+
const { onPinchingStop, onZoomStop } = this.props;
|
|
292
|
+
|
|
293
|
+
if (this.pinchStartScale) {
|
|
294
|
+
handlePinchStop(this);
|
|
295
|
+
handleCallback(getContext(this), event, onPinchingStop);
|
|
296
|
+
handleCallback(getContext(this), event, onZoomStop);
|
|
297
|
+
}
|
|
298
|
+
};
|
|
299
|
+
|
|
300
|
+
//////////
|
|
301
|
+
// Touch
|
|
302
|
+
//////////
|
|
303
|
+
|
|
304
|
+
onTouchPanningStart = (event: TouchEvent): void => {
|
|
305
|
+
const { disabled } = this.setup;
|
|
306
|
+
const { onPanningStart } = this.props;
|
|
307
|
+
|
|
308
|
+
if (disabled) return;
|
|
309
|
+
|
|
310
|
+
const isAllowed = isPanningStartAllowed(this, event);
|
|
311
|
+
|
|
312
|
+
if (!isAllowed) return;
|
|
313
|
+
|
|
314
|
+
const isDoubleTap = this.lastTouch && +new Date() - this.lastTouch < 200;
|
|
315
|
+
|
|
316
|
+
if (isDoubleTap && event.touches.length === 1) {
|
|
317
|
+
this.onDoubleClick(event);
|
|
318
|
+
} else {
|
|
319
|
+
this.lastTouch = +new Date();
|
|
320
|
+
|
|
321
|
+
handleCancelAnimation(this);
|
|
322
|
+
|
|
323
|
+
const { touches } = event;
|
|
324
|
+
|
|
325
|
+
const isPanningAction = touches.length === 1;
|
|
326
|
+
const isPinchAction = touches.length === 2;
|
|
327
|
+
|
|
328
|
+
if (isPanningAction) {
|
|
329
|
+
handleCancelAnimation(this);
|
|
330
|
+
handlePanningStart(this, event);
|
|
331
|
+
handleCallback(getContext(this), event, onPanningStart);
|
|
332
|
+
}
|
|
333
|
+
if (isPinchAction) {
|
|
334
|
+
this.onPinchStart(event);
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
};
|
|
338
|
+
|
|
339
|
+
onTouchPanning = (event: TouchEvent): void => {
|
|
340
|
+
const { disabled } = this.setup;
|
|
341
|
+
const { onPanning } = this.props;
|
|
342
|
+
|
|
343
|
+
if (this.isPanning && event.touches.length === 1) {
|
|
344
|
+
if (disabled) return;
|
|
345
|
+
|
|
346
|
+
const isAllowed = isPanningAllowed(this);
|
|
347
|
+
if (!isAllowed) return;
|
|
348
|
+
|
|
349
|
+
event.preventDefault();
|
|
350
|
+
event.stopPropagation();
|
|
351
|
+
|
|
352
|
+
const touch = event.touches[0];
|
|
353
|
+
handlePanning(this, touch.clientX, touch.clientY);
|
|
354
|
+
handleCallback(getContext(this), event, onPanning);
|
|
355
|
+
} else if (event.touches.length > 1) {
|
|
356
|
+
this.onPinch(event);
|
|
357
|
+
}
|
|
358
|
+
};
|
|
359
|
+
|
|
360
|
+
onTouchPanningStop = (event: TouchEvent): void => {
|
|
361
|
+
this.onPanningStop(event);
|
|
362
|
+
this.onPinchStop(event);
|
|
363
|
+
};
|
|
364
|
+
|
|
365
|
+
//////////
|
|
366
|
+
// Double Click
|
|
367
|
+
//////////
|
|
368
|
+
|
|
369
|
+
onDoubleClick = (event: MouseEvent | TouchEvent): void => {
|
|
370
|
+
const { disabled } = this.setup;
|
|
371
|
+
if (disabled) return;
|
|
372
|
+
|
|
373
|
+
const isAllowed = isDoubleClickAllowed(this, event);
|
|
374
|
+
if (!isAllowed) return;
|
|
375
|
+
|
|
376
|
+
handleDoubleClick(this, event);
|
|
377
|
+
};
|
|
378
|
+
|
|
379
|
+
//////////
|
|
380
|
+
// Helpers
|
|
381
|
+
//////////
|
|
382
|
+
|
|
383
|
+
clearPanning = (event: MouseEvent): void => {
|
|
384
|
+
if (this.isPanning) {
|
|
385
|
+
this.onPanningStop(event);
|
|
386
|
+
}
|
|
387
|
+
};
|
|
388
|
+
|
|
389
|
+
setKeyPressed = (e: KeyboardEvent): void => {
|
|
390
|
+
this.pressedKeys[e.key] = true;
|
|
391
|
+
};
|
|
392
|
+
|
|
393
|
+
setKeyUnPressed = (e: KeyboardEvent): void => {
|
|
394
|
+
this.pressedKeys[e.key] = false;
|
|
395
|
+
};
|
|
396
|
+
|
|
397
|
+
isPressingKeys = (keys: string[]): boolean => {
|
|
398
|
+
if (!keys.length) {
|
|
399
|
+
return true;
|
|
400
|
+
}
|
|
401
|
+
return Boolean(keys.find((key) => this.pressedKeys[key]));
|
|
402
|
+
};
|
|
403
|
+
|
|
404
|
+
setComponents = (
|
|
405
|
+
wrapperComponent: HTMLDivElement,
|
|
406
|
+
contentComponent: HTMLDivElement,
|
|
407
|
+
): void => {
|
|
408
|
+
this.cleanupWindowEvents();
|
|
409
|
+
this.wrapperComponent = wrapperComponent;
|
|
410
|
+
this.contentComponent = contentComponent;
|
|
411
|
+
handleCalculateBounds(this, this.transformState.scale);
|
|
412
|
+
this.handleInitializeWrapperEvents(wrapperComponent);
|
|
413
|
+
this.initializeWindowEvents();
|
|
414
|
+
this.handleInitialize();
|
|
415
|
+
this.handleRef();
|
|
416
|
+
this.isInitialized = true;
|
|
417
|
+
handleCallback(getContext(this), undefined, this.props.onInit);
|
|
418
|
+
};
|
|
419
|
+
|
|
420
|
+
setTransformState = (
|
|
421
|
+
scale: number,
|
|
422
|
+
positionX: number,
|
|
423
|
+
positionY: number,
|
|
424
|
+
): void => {
|
|
425
|
+
const { onTransformed } = this.props;
|
|
426
|
+
|
|
427
|
+
if (!isNaN(scale) && !isNaN(positionX) && !isNaN(positionY)) {
|
|
428
|
+
if (scale !== this.transformState.scale) {
|
|
429
|
+
this.transformState.previousScale = this.transformState.scale;
|
|
430
|
+
this.transformState.scale = scale;
|
|
431
|
+
}
|
|
432
|
+
this.transformState.positionX = positionX;
|
|
433
|
+
this.transformState.positionY = positionY;
|
|
434
|
+
|
|
435
|
+
handleCallback(
|
|
436
|
+
getContext(this),
|
|
437
|
+
{ scale, positionX, positionY },
|
|
438
|
+
onTransformed,
|
|
439
|
+
);
|
|
440
|
+
this.applyTransformation();
|
|
441
|
+
} else {
|
|
442
|
+
console.error("Detected NaN set state values");
|
|
443
|
+
}
|
|
444
|
+
};
|
|
445
|
+
|
|
446
|
+
setCenter = (): void => {
|
|
447
|
+
if (this.wrapperComponent && this.contentComponent) {
|
|
448
|
+
const targetState = getCenterPosition(
|
|
449
|
+
this.transformState.scale,
|
|
450
|
+
this.wrapperComponent,
|
|
451
|
+
this.contentComponent,
|
|
452
|
+
);
|
|
453
|
+
this.setTransformState(
|
|
454
|
+
targetState.scale,
|
|
455
|
+
targetState.positionX,
|
|
456
|
+
targetState.positionY,
|
|
457
|
+
);
|
|
458
|
+
}
|
|
459
|
+
};
|
|
460
|
+
|
|
461
|
+
applyTransformation = (): void => {
|
|
462
|
+
if (!this.mounted || !this.contentComponent) return;
|
|
463
|
+
const { scale, positionX, positionY } = this.transformState;
|
|
464
|
+
const transform = getTransformStyles(positionX, positionY, scale);
|
|
465
|
+
this.contentComponent.style.transform = transform;
|
|
466
|
+
|
|
467
|
+
this.handleRef();
|
|
468
|
+
};
|
|
469
|
+
|
|
470
|
+
handleRef = (): void => {
|
|
471
|
+
this.props.setRef(getContext(this));
|
|
472
|
+
};
|
|
473
|
+
|
|
474
|
+
render(): JSX.Element {
|
|
475
|
+
const value = getContext(this);
|
|
476
|
+
const { children } = this.props;
|
|
477
|
+
const content = typeof children === "function" ? children(value) : children;
|
|
478
|
+
|
|
479
|
+
return (
|
|
480
|
+
<Context.Provider
|
|
481
|
+
value={{
|
|
482
|
+
...this.transformState,
|
|
483
|
+
setComponents: this.setComponents,
|
|
484
|
+
contextInstance: this,
|
|
485
|
+
}}
|
|
486
|
+
>
|
|
487
|
+
{content}
|
|
488
|
+
</Context.Provider>
|
|
489
|
+
);
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
export { Context, TransformContext };
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import React, { useImperativeHandle, useState } from "react";
|
|
2
|
+
import { ReactZoomPanPinchProps, ReactZoomPanPinchRef } from "../models";
|
|
3
|
+
import { TransformContext } from "./transform-context";
|
|
4
|
+
|
|
5
|
+
export const TransformWrapper = React.forwardRef(
|
|
6
|
+
(
|
|
7
|
+
props: Omit<ReactZoomPanPinchProps, "ref">,
|
|
8
|
+
ref: React.Ref<ReactZoomPanPinchRef>,
|
|
9
|
+
) => {
|
|
10
|
+
const [innerRef, setRef] = useState<ReactZoomPanPinchRef | null>(null);
|
|
11
|
+
|
|
12
|
+
useImperativeHandle(ref, () => innerRef as any, [innerRef]);
|
|
13
|
+
|
|
14
|
+
return <TransformContext {...props} setRef={setRef as any} />;
|
|
15
|
+
},
|
|
16
|
+
);
|
|
17
|
+
|
|
18
|
+
export default TransformWrapper;
|