Skip to content

Commit

Permalink
refactor: Add toggle double-click mode (#410)
Browse files Browse the repository at this point in the history
* Add toggle double-click mode

* Extra nested ternary
  • Loading branch information
Cretezy committed Jan 31, 2024
1 parent cad806b commit bc3399d
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 8 deletions.
15 changes: 13 additions & 2 deletions src/core/double-click/double-click.logic.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable no-param-reassign */
import { ReactZoomPanPinchContext } from "../../models";
import { LibrarySetup, ReactZoomPanPinchContext } from "../../models";
import { animate } from "../animations/animations.utils";
import { getMousePosition } from "../wheel/wheel.utils";
import { handleZoomToPoint } from "../zoom/zoom.logic";
Expand Down Expand Up @@ -44,6 +44,17 @@ export const handleDoubleClickResetMode = (
handleDoubleClickStop(contextInstance, event);
};

function getDoubleClickScale(
mode: LibrarySetup["doubleClick"]["mode"],
scale: number,
) {
if (mode === "toggle") {
return scale === 1 ? 1 : -1;
}

return mode === "zoomOut" ? -1 : 1;
}

export function handleDoubleClick(
contextInstance: ReactZoomPanPinchContext,
event: MouseEvent | TouchEvent,
Expand All @@ -65,7 +76,7 @@ export function handleDoubleClick(

if (!contentComponent) return console.error("No ContentComponent found");

const delta = mode === "zoomOut" ? -1 : 1;
const delta = getDoubleClickScale(mode, contextInstance.transformState.scale);

const newScale = handleCalculateButtonZoom(contextInstance, delta, step);

Expand Down
6 changes: 3 additions & 3 deletions src/models/context.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import React from "react";
import { animations } from "../core/animations/animations.constants";
import { DeepNonNullable } from "./helpers.model";
import {
zoomIn,
zoomToElement,
centerView,
resetTransform,
setTransform,
zoomIn,
zoomOut,
zoomToElement,
} from "../core/handlers/handlers.logic";
import { ZoomPanPinch } from "../core/instance.core";

Expand Down Expand Up @@ -96,7 +96,7 @@ export type ReactZoomPanPinchProps = {
doubleClick?: {
disabled?: boolean;
step?: number;
mode?: "zoomIn" | "zoomOut" | "reset";
mode?: "zoomIn" | "zoomOut" | "reset" | "toggle";
animationTime?: number;
animationType?: keyof typeof animations;
excluded?: string[];
Expand Down
4 changes: 2 additions & 2 deletions src/stories/docs/props.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -323,10 +323,10 @@ export const wrapperPropsTable: ComponentProps = {
"The sensitivity of zooming in or out when the double click mode is set to 'zoomIn' or 'zoomOut'.",
},
mode: {
type: ["zoomIn", "zoomOut", "reset"],
type: ["zoomIn", "zoomOut", "reset", "toggle"],
defaultValue: String(initialSetup.doubleClick.mode),
description:
"The mode of the double click feature. Zoom in/Zoom out will change the scale with the given step settings. The reset functionality will take change transform to the initial values.",
"The mode of the double click feature. Zoom in/Zoom out will change the scale with the given step settings. The reset functionality will take change transform to the initial values. The toggle functionality will toggle between zooming in and out.",
},
animationTime: {
type: ["number"],
Expand Down
2 changes: 1 addition & 1 deletion src/typings.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ declare module "*.css" {
export default content;
}

type SvgrComponent = React.StatelessComponent<React.SVGAttributes<SVGElement>>
type SvgrComponent = React.StatelessComponent<React.SVGAttributes<SVGElement>>;

0 comments on commit bc3399d

Please sign in to comment.