Skip to content

Commit bc3399d

Browse files
authored
refactor: Add toggle double-click mode (#410)
* Add toggle double-click mode * Extra nested ternary
1 parent cad806b commit bc3399d

4 files changed

Lines changed: 19 additions & 8 deletions

File tree

src/core/double-click/double-click.logic.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* eslint-disable no-param-reassign */
2-
import { ReactZoomPanPinchContext } from "../../models";
2+
import { LibrarySetup, ReactZoomPanPinchContext } from "../../models";
33
import { animate } from "../animations/animations.utils";
44
import { getMousePosition } from "../wheel/wheel.utils";
55
import { handleZoomToPoint } from "../zoom/zoom.logic";
@@ -44,6 +44,17 @@ export const handleDoubleClickResetMode = (
4444
handleDoubleClickStop(contextInstance, event);
4545
};
4646

47+
function getDoubleClickScale(
48+
mode: LibrarySetup["doubleClick"]["mode"],
49+
scale: number,
50+
) {
51+
if (mode === "toggle") {
52+
return scale === 1 ? 1 : -1;
53+
}
54+
55+
return mode === "zoomOut" ? -1 : 1;
56+
}
57+
4758
export function handleDoubleClick(
4859
contextInstance: ReactZoomPanPinchContext,
4960
event: MouseEvent | TouchEvent,
@@ -65,7 +76,7 @@ export function handleDoubleClick(
6576

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

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

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

src/models/context.model.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ import React from "react";
33
import { animations } from "../core/animations/animations.constants";
44
import { DeepNonNullable } from "./helpers.model";
55
import {
6-
zoomIn,
7-
zoomToElement,
86
centerView,
97
resetTransform,
108
setTransform,
9+
zoomIn,
1110
zoomOut,
11+
zoomToElement,
1212
} from "../core/handlers/handlers.logic";
1313
import { ZoomPanPinch } from "../core/instance.core";
1414

@@ -96,7 +96,7 @@ export type ReactZoomPanPinchProps = {
9696
doubleClick?: {
9797
disabled?: boolean;
9898
step?: number;
99-
mode?: "zoomIn" | "zoomOut" | "reset";
99+
mode?: "zoomIn" | "zoomOut" | "reset" | "toggle";
100100
animationTime?: number;
101101
animationType?: keyof typeof animations;
102102
excluded?: string[];

src/stories/docs/props.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -323,10 +323,10 @@ export const wrapperPropsTable: ComponentProps = {
323323
"The sensitivity of zooming in or out when the double click mode is set to 'zoomIn' or 'zoomOut'.",
324324
},
325325
mode: {
326-
type: ["zoomIn", "zoomOut", "reset"],
326+
type: ["zoomIn", "zoomOut", "reset", "toggle"],
327327
defaultValue: String(initialSetup.doubleClick.mode),
328328
description:
329-
"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.",
329+
"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.",
330330
},
331331
animationTime: {
332332
type: ["number"],

src/typings.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ declare module "*.css" {
77
export default content;
88
}
99

10-
type SvgrComponent = React.StatelessComponent<React.SVGAttributes<SVGElement>>
10+
type SvgrComponent = React.StatelessComponent<React.SVGAttributes<SVGElement>>;

0 commit comments

Comments
 (0)