Skip to content

Commit f3b5afd

Browse files
committed
Merge branch 'master' into v4.0.0
2 parents 90807f6 + a0dac69 commit f3b5afd

7 files changed

Lines changed: 28 additions & 18 deletions

File tree

jest.setup.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ global.ResizeObserver = class ResizeObserver {
1919
// Trigger callback once synchronously so centerOnInit flows complete.
2020
this.callback([], this);
2121
}
22-
unobserve() {}
23-
disconnect() {}
22+
unobserve() { }
23+
disconnect() { }
2424
};
2525
// ./config/jest/setupTests.js
2626

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"scripts": {
1717
"build": "rollup -c",
1818
"build:docs": "yarn storybook build",
19-
"start": "yarn storybook dev -p 6006 DISABLE_ESLINT_PLUGIN=true",
19+
"start": "cross-env DISABLE_ESLINT_PLUGIN=true yarn storybook dev -p 6006",
2020
"test": "jest",
2121
"test:debug": "jest-preview",
2222
"lint": "eslint . --ext .js,.jsx,.tsx,.ts --fix",

src/core/handlers/handlers.logic.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ export const zoomToElement =
9999
scale?: number,
100100
animationTime = 600,
101101
animationType: keyof typeof animations = "easeOut",
102+
offsetX = 0,
103+
offsetY = 0,
102104
): void => {
103105
handleCancelAnimation(contextInstance);
104106

@@ -108,7 +110,13 @@ export const zoomToElement =
108110
typeof node === "string" ? document.getElementById(node) : node;
109111

110112
if (wrapperComponent && target && wrapperComponent.contains(target)) {
111-
const targetState = calculateZoomToNode(contextInstance, target, scale);
113+
const targetState = calculateZoomToNode(
114+
contextInstance,
115+
target,
116+
scale,
117+
offsetX,
118+
offsetY,
119+
);
112120
animate(contextInstance, targetState, animationTime, animationType);
113121
}
114122
};

src/core/handlers/handlers.utils.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,8 @@ export function calculateZoomToNode(
192192
contextInstance: ReactZoomPanPinchContext,
193193
node: HTMLElement,
194194
customZoom?: number,
195+
customOffsetX = 0,
196+
customOffsetY = 0,
195197
): { positionX: number; positionY: number; scale: number } {
196198
const { wrapperComponent, contentComponent, state } = contextInstance;
197199
const { limitToBounds, minScale, maxScale } = contextInstance.setup;
@@ -220,8 +222,10 @@ export function calculateZoomToNode(
220222
const offsetX = (wrapperRect.width - nodeWidth * newScale) / 2;
221223
const offsetY = (wrapperRect.height - nodeHeight * newScale) / 2;
222224

223-
const newPositionX = (wrapperRect.left - nodeLeft) * newScale + offsetX;
224-
const newPositionY = (wrapperRect.top - nodeTop) * newScale + offsetY;
225+
const newPositionX =
226+
(wrapperRect.left - nodeLeft) * newScale + offsetX + customOffsetX;
227+
const newPositionY =
228+
(wrapperRect.top - nodeTop) * newScale + offsetY + customOffsetY;
225229

226230
const bounds = calculateBounds(contextInstance, newScale);
227231

src/core/instance.core.ts

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -213,21 +213,11 @@ export class ZoomPanPinch {
213213

214214
handleInitialize = (contentComponent: HTMLDivElement): void => {
215215
const { centerOnInit } = this.setup;
216-
217-
if (centerOnInit && this.wrapperComponent) {
218-
const targetState = getCenterPosition(
219-
this.state.scale,
220-
this.wrapperComponent,
221-
contentComponent,
222-
);
223-
this.state.positionX = targetState.positionX;
224-
this.state.positionY = targetState.positionY;
225-
}
226-
227216
this.applyTransformation();
228217
this.onInitCallbacks.forEach((callback) => callback(getContext(this)));
229218

230219
if (centerOnInit) {
220+
this.setCenter();
231221
this.observer = new ResizeObserver(() => {
232222
const currentWidth = contentComponent.offsetWidth;
233223
const currentHeight = contentComponent.offsetHeight;

src/core/pan/panning.logic.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ export function handlePanningStart(
3434

3535
export function handleAlignToBounds(
3636
contextInstance: ReactZoomPanPinchContext,
37+
customAnimationTime?: number,
3738
): void {
3839
const { scale } = contextInstance.state;
3940
const { minScale, autoAlignment } = contextInstance.setup;
@@ -47,7 +48,12 @@ export function handleAlignToBounds(
4748
const targetState = handlePanToBounds(contextInstance);
4849

4950
if (targetState) {
50-
animate(contextInstance, targetState, animationTime, animationType);
51+
animate(
52+
contextInstance,
53+
targetState,
54+
customAnimationTime ?? animationTime,
55+
animationType,
56+
);
5157
}
5258
}
5359

src/core/pan/panning.utils.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ export const handlePanningSetup = (
7272
const y = event.clientY;
7373

7474
contextInstance.startCoords = { x: x - positionX, y: y - positionY };
75+
contextInstance.clientCoords = {x: x, y: y};
7576
};
7677

7778
export const handleTouchPanningSetup = (
@@ -89,6 +90,7 @@ export const handleTouchPanningSetup = (
8990
const x = touches[0].clientX;
9091
const y = touches[0].clientY;
9192
contextInstance.startCoords = { x: x - positionX, y: y - positionY };
93+
contextInstance.clientCoords = {x: x, y: y};
9294
}
9395
};
9496
export function handlePanToBounds(

0 commit comments

Comments
 (0)