Skip to content

Commit 258fc16

Browse files
committed
fix: some more changes regarding zoom
1 parent fc18ae3 commit 258fc16

1 file changed

Lines changed: 22 additions & 6 deletions

File tree

src/components/newPdfViewer.tsx

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { downloadFile } from "../lib/utils/download";
1515
import { Button } from "./ui/button";
1616
import ShareButton from "./ShareButton";
1717
import ReportButton from "./ReportButton";
18+
import path from 'path/win32';
1819

1920
interface ControlProps {
2021
documentId: string;
@@ -252,24 +253,39 @@ function WheelZoom({ documentId, viewerRef }: WheelZoomProps) {
252253
const { provides: zoomProv } = useZoom(documentId);
253254
const accumulatedDelta = useRef(0);
254255
const rafId = useRef<number | null>(null);
256+
const curZoom = useRef(1);
257+
258+
// Stay in sync with zoom from anywhere (buttons, keyboard, etc.)
259+
useEffect(() => {
260+
if (!zoomProv) return;
261+
curZoom.current = zoomProv.getState().currentZoomLevel;
262+
const unsub = zoomProv.onZoomChange((e) => {
263+
curZoom.current = e.newZoom;
264+
});
265+
return unsub;
266+
}, [zoomProv]);
255267

256268
const handleWheel = useCallback(
257269
(e: WheelEvent) => {
258270
if (!e.ctrlKey || !zoomProv) return;
259271
e.preventDefault();
260272

261-
// Trackpad pinch sends fractional deltaY (often < 1),
262-
// mouse wheel sends large discrete steps (typically 100–120).
263273
const isTrackpad = Math.abs(e.deltaY) < 50;
264-
const scaleFactor = isTrackpad ? 0.007 : 0.08;
265-
274+
const scaleFactor = isTrackpad ? 0.008 : 0.08;
266275
accumulatedDelta.current += -e.deltaY * scaleFactor;
267276

268-
// Flush on the next animation frame to batch rapid events
269277
if (rafId.current !== null) cancelAnimationFrame(rafId.current);
270278
rafId.current = requestAnimationFrame(() => {
271279
const clamped = Math.max(-0.15, Math.min(accumulatedDelta.current, 0.15));
272-
zoomProv.requestZoomBy(clamped);
280+
281+
if (isTrackpad) {
282+
const target = Math.max(0.25, Math.min(curZoom.current + clamped, 4));
283+
zoomProv.requestZoom(target)
284+
curZoom.current = target;
285+
} else {
286+
zoomProv.requestZoomBy(clamped); // mouse wheel, relative is fine
287+
}
288+
273289
accumulatedDelta.current = 0;
274290
rafId.current = null;
275291
});

0 commit comments

Comments
 (0)