Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions src/components/transform-wrapper/transform-wrapper.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import React, { useEffect, useImperativeHandle, useRef } from "react";
import React, {
useContext,
useEffect,
useImperativeHandle,
useRef,
} from "react";

import { ZoomPanPinch } from "../../core/instance.core";
import {
Expand All @@ -9,6 +14,9 @@ import { getControls } from "../../utils";

export const Context = React.createContext<ZoomPanPinch>(null as any);

// Context pour détecter un TransformWrapper parent
const ParentContext = React.createContext<ZoomPanPinch | null>(null);

const getContent = (
children: ReactZoomPanPinchProps["children"],
ctx: ReactZoomPanPinchContentRef,
Expand All @@ -24,7 +32,10 @@ export const TransformWrapper = React.forwardRef(
props: Omit<ReactZoomPanPinchProps, "ref">,
ref: React.Ref<ReactZoomPanPinchContentRef>,
) => {
const instance = useRef(new ZoomPanPinch(props)).current;
// Vérifier s'il y a un TransformWrapper parent via le Context
const parentContext = useContext(ParentContext);

const instance = useRef(new ZoomPanPinch(props, parentContext)).current;

const content = getContent(props.children, getControls(instance));

Expand All @@ -34,6 +45,10 @@ export const TransformWrapper = React.forwardRef(
instance.update(props);
}, [instance, props]);

return <Context.Provider value={instance}>{content}</Context.Provider>;
return (
<ParentContext.Provider value={instance}>
<Context.Provider value={instance}>{content}</Context.Provider>
</ParentContext.Provider>
);
},
);
46 changes: 31 additions & 15 deletions src/core/handlers/handlers.logic.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ReactZoomPanPinchContext } from "../../models";
import { ReactZoomPanPinchContext, StateType } from "../../models";
import {
calculateZoomToNode,
handleZoomToViewCenter,
Expand All @@ -8,6 +8,19 @@ import { animations } from "../animations/animations.constants";
import { animate, handleCancelAnimation } from "../animations/animations.utils";
import { getCenterPosition } from "../../utils";

function applyParentScale(
targetState: StateType,
contextInstance: ReactZoomPanPinchContext,
): StateType {
const result = targetState;
if (contextInstance.parentInstance !== null) {
result.positionX /= contextInstance.parentInstance.transformState.scale;
result.positionY /= contextInstance.parentInstance.transformState.scale;
return applyParentScale(targetState, contextInstance.parentInstance);
}
return result;
}

export const zoomIn =
(contextInstance: ReactZoomPanPinchContext) =>
(
Expand Down Expand Up @@ -55,11 +68,14 @@ export const setTransform =

if (disabled || !wrapperComponent || !contentComponent) return;

const targetState = {
positionX: Number.isNaN(newPositionX) ? positionX : newPositionX,
positionY: Number.isNaN(newPositionY) ? positionY : newPositionY,
scale: Number.isNaN(newScale) ? scale : newScale,
};
const targetState = applyParentScale(
{
positionX: Number.isNaN(newPositionX) ? positionX : newPositionX,
positionY: Number.isNaN(newPositionY) ? positionY : newPositionY,
scale: Number.isNaN(newScale) ? scale : newScale,
},
contextInstance,
);

animate(contextInstance, targetState, animationTime, animationType);
};
Expand All @@ -83,10 +99,13 @@ export const centerView =
const { transformState, wrapperComponent, contentComponent } =
contextInstance;
if (wrapperComponent && contentComponent) {
const targetState = getCenterPosition(
scale || transformState.scale,
wrapperComponent,
contentComponent,
const targetState = applyParentScale(
getCenterPosition(
scale || transformState.scale,
wrapperComponent,
contentComponent,
),
contextInstance,
);

animate(contextInstance, targetState, animationTime, animationType);
Expand All @@ -111,12 +130,9 @@ export const zoomToElement =
typeof node === "string" ? document.getElementById(node) : node;

if (wrapperComponent && target && wrapperComponent.contains(target)) {
const targetState = calculateZoomToNode(
const targetState = applyParentScale(
calculateZoomToNode(contextInstance, target, scale, offsetX, offsetY),
contextInstance,
target,
scale,
offsetX,
offsetY,
);
animate(contextInstance, targetState, animationTime, animationType);
}
Expand Down
20 changes: 19 additions & 1 deletion src/core/instance.core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
ReactZoomPanPinchProps,
ReactZoomPanPinchState,
ReactZoomPanPinchRef,
StateType,
} from "../models";
import {
getContext,
Expand Down Expand Up @@ -101,10 +102,16 @@ export class ZoomPanPinch {
// key press
public pressedKeys: { [key: string]: boolean } = {};

constructor(props: ReactZoomPanPinchProps) {
public parentInstance: ZoomPanPinch | null;

constructor(
props: ReactZoomPanPinchProps,
parentInstance: ZoomPanPinch | null,
) {
this.props = props;
this.setup = createSetup(this.props);
this.transformState = createState(this.props);
this.parentInstance = parentInstance;
}

mount = () => {
Expand Down Expand Up @@ -587,4 +594,15 @@ export class ZoomPanPinch {
const ctx = getContext(this);
handleCallback(ctx, undefined, this.props.onInit);
};

getCenterPosition = (): StateType => {
if (this.wrapperComponent && this.contentComponent) {
return getCenterPosition(
this.transformState.scale,
this.wrapperComponent,
this.contentComponent,
);
}
return { scale: 1, positionX: 0, positionY: 0 };
};
}