Skip to content

Commit 1c3de4a

Browse files
committed
docs: ✏️ Stories preparations
1 parent b8660d0 commit 1c3de4a

25 files changed

Lines changed: 406 additions & 94 deletions

File tree

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
"@babel/runtime": "^7.23.8",
3636
"@commitlint/cli": "^18.4.4",
3737
"@commitlint/config-conventional": "^18.4.4",
38+
"@rollup/plugin-babel": "^6.0.4",
39+
"@rollup/plugin-typescript": "^11.1.6",
3840
"@semantic-release/changelog": "^6.0.3",
3941
"@semantic-release/commit-analyzer": "^11.1.0",
4042
"@semantic-release/git": "^10.0.1",

rollup.config.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import del from "rollup-plugin-delete";
44
import typescript from "@rollup/plugin-typescript";
55
import postcss from "rollup-plugin-postcss";
66
import dts from "rollup-plugin-dts";
7-
import pkg from "./package.json";
7+
import pkg from "./package.json" with { type: "json" };
88

99
export default [
1010
{

src/constants/state.constants.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,14 @@ export const initialSetup: LibrarySetup = {
2929
activationKeys: [],
3030
excluded: [],
3131
},
32+
trackPadPanning: {
33+
disabled: true,
34+
velocityDisabled: false,
35+
lockAxisX: false,
36+
lockAxisY: false,
37+
activationKeys: [],
38+
excluded: [],
39+
},
3240
panning: {
3341
disabled: false,
3442
velocityDisabled: false,
@@ -37,7 +45,6 @@ export const initialSetup: LibrarySetup = {
3745
allowLeftClickPan: true,
3846
allowMiddleClickPan: true,
3947
allowRightClickPan: true,
40-
wheelPanning: false,
4148
activationKeys: [],
4249
excluded: [],
4350
},

src/core/instance.core.ts

Lines changed: 53 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,18 @@ import {
1717
getTransformStyles,
1818
makePassiveEventOption,
1919
getCenterPosition,
20+
isExcludedNode,
2021
} from "../utils";
2122
import { handleCancelAnimation } from "./animations/animations.utils";
22-
import { isWheelAllowed } from "./wheel/wheel.utils";
23+
import { isWheelAllowed, isWheelPanningAllowed } from "./wheel/wheel.utils";
2324
import { isPinchAllowed, isPinchStartAllowed } from "./pinch/pinch.utils";
2425
import { handleCalculateBounds } from "./bounds/bounds.utils";
2526
import {
2627
handleWheelStart,
2728
handleWheelZoom,
2829
handleWheelStop,
30+
handleWheelPanningStop,
31+
handleWheelPanningStart,
2932
} from "./wheel/wheel.logic";
3033
import {
3134
getPaddingValue,
@@ -135,6 +138,16 @@ export class ZoomPanPinch {
135138
this.onWheelPanning,
136139
passive,
137140
);
141+
this.wrapperComponent?.addEventListener(
142+
"keyup",
143+
this.setKeyUnPressed,
144+
passive,
145+
);
146+
this.wrapperComponent?.addEventListener(
147+
"keydown",
148+
this.setKeyPressed,
149+
passive,
150+
);
138151
// Panning on window to allow panning when mouse is out of component wrapper
139152
currentWindow?.addEventListener("mousedown", this.onPanningStart, passive);
140153
currentWindow?.addEventListener("mousemove", this.onPanning, passive);
@@ -163,7 +176,21 @@ export class ZoomPanPinch {
163176
currentWindow?.removeEventListener("keyup", this.setKeyUnPressed, passive);
164177
currentWindow?.removeEventListener("keydown", this.setKeyPressed, passive);
165178
document.removeEventListener("mouseleave", this.clearPanning, passive);
166-
179+
this.wrapperComponent?.removeEventListener(
180+
"wheel",
181+
this.onWheelPanning,
182+
passive,
183+
);
184+
this.wrapperComponent?.removeEventListener(
185+
"keyup",
186+
this.setKeyUnPressed,
187+
passive,
188+
);
189+
this.wrapperComponent?.removeEventListener(
190+
"keydown",
191+
this.setKeyPressed,
192+
passive,
193+
);
167194
handleCancelAnimation(this);
168195
this.observer?.disconnect();
169196
};
@@ -222,56 +249,55 @@ export class ZoomPanPinch {
222249
const isAllowed = isWheelAllowed(this, event);
223250
if (!isAllowed) return;
224251

225-
const keysPressed = this.isPressingKeys(this.setup.wheel.activationKeys);
226-
if (!keysPressed) return;
227-
228252
handleWheelStart(this, event);
229253
handleWheelZoom(this, event);
230254
handleWheelStop(this, event);
231255
};
232256

233257
/// ///////
234-
// Pan
258+
// Track Pad Panning
235259
/// ///////
236260

237261
onWheelPanning = (event: WheelEvent): void => {
238-
const { disabled, wheel, panning } = this.setup;
239-
if (
240-
!this.wrapperComponent ||
241-
!this.contentComponent ||
242-
disabled ||
243-
!wheel.wheelDisabled ||
244-
panning.disabled ||
245-
!panning.wheelPanning ||
246-
event.ctrlKey
247-
) {
248-
return;
249-
}
262+
const { onPanning } = this.props;
263+
const { trackPadPanning } = this.setup;
264+
const { lockAxisX, lockAxisY } = trackPadPanning;
265+
266+
const isAllowed = isWheelPanningAllowed(this, event);
267+
268+
if (!isAllowed) return;
250269

251270
event.preventDefault();
252271
event.stopPropagation();
253272

254273
const { positionX, positionY } = this.state;
255274
const mouseX = positionX - event.deltaX;
256275
const mouseY = positionY - event.deltaY;
257-
const newPositionX = panning.lockAxisX ? positionX : mouseX;
258-
const newPositionY = panning.lockAxisY ? positionY : mouseY;
276+
const newPositionX = lockAxisX ? positionX : mouseX;
277+
const newPositionY = lockAxisY ? positionY : mouseY;
259278

260279
const { sizeX, sizeY } = this.setup.autoAlignment;
261280
const paddingValueX = getPaddingValue(this, sizeX);
262281
const paddingValueY = getPaddingValue(this, sizeY);
263282

264283
if (newPositionX === positionX && newPositionY === positionY) return;
265284

285+
handleWheelPanningStart(this, event);
266286
handleNewPosition(
267287
this,
268288
newPositionX,
269289
newPositionY,
270290
paddingValueX,
271291
paddingValueY,
272292
);
293+
handleCallback(getContext(this), event, onPanning);
294+
handleWheelPanningStop(this, event);
273295
};
274296

297+
/// ///////
298+
// Pan
299+
/// ///////
300+
275301
onPanningStart = (event: MouseEvent): void => {
276302
const { disabled } = this.setup;
277303
const { onPanningStart } = this.props;
@@ -315,10 +341,11 @@ export class ZoomPanPinch {
315341
};
316342

317343
onPanningStop = (event: MouseEvent | TouchEvent): void => {
344+
const { velocityDisabled } = this.setup.panning;
318345
const { onPanningStop } = this.props;
319346

320347
if (this.isPanning) {
321-
handlePanningEnd(this);
348+
handlePanningEnd(this, velocityDisabled);
322349
handleCallback(getContext(this), event, onPanningStop);
323350
}
324351
};
@@ -456,6 +483,11 @@ export class ZoomPanPinch {
456483

457484
setKeyPressed = (e: KeyboardEvent): void => {
458485
this.pressedKeys[e.key] = true;
486+
console.log(
487+
Object.entries(this.pressedKeys)
488+
.filter(([, pressed]) => pressed)
489+
.map(([key]) => key),
490+
);
459491
};
460492

461493
setKeyUnPressed = (e: KeyboardEvent): void => {

src/core/pan/panning.logic.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,9 @@ export function handlePanning(
7171

7272
export function handlePanningEnd(
7373
contextInstance: ReactZoomPanPinchContext,
74+
velocityDisabled: boolean,
7475
): void {
7576
if (contextInstance.isPanning) {
76-
const { velocityDisabled } = contextInstance.setup.panning;
7777
const { velocity, wrapperComponent, contentComponent } = contextInstance;
7878

7979
contextInstance.isPanning = false;

src/core/wheel/wheel.logic.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,3 +119,43 @@ export const handleWheelStop = (
119119
}, wheelStopEventTime);
120120
}
121121
};
122+
123+
export const handleWheelPanningStart = (
124+
contextInstance: ReactZoomPanPinchContext,
125+
event: WheelEvent,
126+
): void => {
127+
const { onWheelStart, onPanningStart } = contextInstance.props;
128+
129+
if (!contextInstance.wheelStopEventTimer) {
130+
handleCancelAnimation(contextInstance);
131+
handleCallback(getContext(contextInstance), event, onWheelStart);
132+
handleCallback(getContext(contextInstance), event, onPanningStart);
133+
}
134+
};
135+
136+
export const handleWheelPanningStop = (
137+
contextInstance: ReactZoomPanPinchContext,
138+
event: WheelEvent,
139+
): void => {
140+
const { onWheelStop, onPanningStop } = contextInstance.props;
141+
142+
// fire animation
143+
cancelTimeout(contextInstance.wheelAnimationTimer);
144+
contextInstance.wheelAnimationTimer = setTimeout(() => {
145+
if (!contextInstance.mounted) return;
146+
handleAlignToScaleBounds(contextInstance, event.x, event.y);
147+
contextInstance.wheelAnimationTimer = null;
148+
}, wheelAnimationTime);
149+
150+
// Wheel stop event
151+
const hasStoppedZooming = handleWheelZoomStop(contextInstance, event);
152+
if (hasStoppedZooming) {
153+
cancelTimeout(contextInstance.wheelStopEventTimer);
154+
contextInstance.wheelStopEventTimer = setTimeout(() => {
155+
if (!contextInstance.mounted) return;
156+
contextInstance.wheelStopEventTimer = null;
157+
handleCallback(getContext(contextInstance), event, onWheelStop);
158+
handleCallback(getContext(contextInstance), event, onPanningStop);
159+
}, wheelStopEventTime);
160+
}
161+
};

src/core/wheel/wheel.utils.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,41 @@ export const isWheelAllowed = (
2222

2323
if (isExcluded) return false;
2424

25+
const keysPressed = contextInstance.isPressingKeys(
26+
contextInstance.setup.wheel.activationKeys,
27+
);
28+
29+
if (!keysPressed) return false;
30+
31+
return true;
32+
};
33+
34+
export const isWheelPanningAllowed = (
35+
contextInstance: ReactZoomPanPinchContext,
36+
event: WheelEvent,
37+
): boolean => {
38+
const { disabled, trackPadPanning } = contextInstance.setup;
39+
const { activationKeys, excluded } = trackPadPanning;
40+
41+
if (!contextInstance.wrapperComponent || !contextInstance.contentComponent) {
42+
return false;
43+
}
44+
45+
if (disabled || trackPadPanning.disabled || event.ctrlKey) {
46+
return false;
47+
}
48+
49+
const isAllowed = isWheelAllowed(contextInstance, event);
50+
// Cannot execute at the same time as wheel zoom
51+
if (isAllowed) return false;
52+
53+
const target = event.target as HTMLElement;
54+
const isExcluded = isExcludedNode(target, excluded);
55+
if (isExcluded) return false;
56+
57+
const keysPressed = contextInstance.isPressingKeys(activationKeys);
58+
if (!keysPressed) return false;
59+
2560
return true;
2661
};
2762

src/models/context.model.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,14 +86,21 @@ export type ReactZoomPanPinchProps = {
8686
allowRightClickPan?: boolean;
8787
activationKeys?: string[] | ((keys: string[]) => boolean);
8888
excluded?: string[];
89-
wheelPanning?: boolean;
9089
};
9190
pinch?: {
9291
step?: number;
9392
disabled?: boolean;
9493
allowPanning?: boolean;
9594
excluded?: string[];
9695
};
96+
trackPadPanning: {
97+
disabled: boolean;
98+
velocityDisabled?: boolean;
99+
lockAxisX?: boolean;
100+
lockAxisY?: boolean;
101+
activationKeys: string[] | ((keys: string[]) => boolean);
102+
excluded?: string[];
103+
};
97104
doubleClick?: {
98105
disabled?: boolean;
99106
step?: number;

src/stories/docs/props.tsx

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -230,17 +230,6 @@ export const wrapperPropsTable: ComponentProps = {
230230
defaultValue: String(initialSetup.panning.disabled),
231231
description: "Disable the panning feature.",
232232
},
233-
wheelPanning: {
234-
type: ["boolean"],
235-
defaultValue: String(initialSetup.panning.wheelPanning),
236-
description: "Enable wheel/trackpad panning.",
237-
},
238-
velocityDisabled: {
239-
type: ["boolean"],
240-
defaultValue: String(initialSetup.panning.velocityDisabled),
241-
description:
242-
"Disable the panning velocity feature. It's triggered when you release the mouse button so the content is still moving after it and slowing down with calculated time.",
243-
},
244233
lockAxisX: {
245234
type: ["boolean"],
246235
defaultValue: String(initialSetup.panning.lockAxisX),

src/stories/examples/bounds/bounds.stories.mdx

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,18 @@ export const Template = (args) => (
1616
{...normalizeArgs(args)}
1717
>
1818
<TransformComponent
19-
wrapperStyle={{ maxWidth: "100%", maxHeight: "calc(100vh - 50px)" }}
19+
wrapperStyle={{
20+
width: "400px",
21+
height: "400px",
22+
maxWidth: "70vw",
23+
maxHeight: "70vh",
24+
}}
25+
contentStyle={{
26+
width: "400px",
27+
height: "400px",
28+
maxWidth: "70vw",
29+
maxHeight: "70vh",
30+
}}
2031
>
2132
<div style={{ background: "#444", color: "white", padding: "50px" }}>
2233
<p>

0 commit comments

Comments
 (0)