A one-of-a-kind resume builder that keeps your privacy in mind. Completely secure, customizable, portable, open-source and free forever. Try it out today!
TypeScript agent-skillsaidsh-pluginhacktoberfestjavascriptmcp-serverreactresume-builderself-hostedtypescript
feat(builder): zoom the canvas with Ctrl/Cmd+wheel (RX-200)
Amruth Pillai316d5216d257
diff --git a/apps/web/src/features/resume/editor/chrome.test.tsx b/apps/web/src/features/resume/editor/chrome.test.tsxnew file mode 100644index 000000000..7d2c59b7d--- /dev/null+++ b/apps/web/src/features/resume/editor/chrome.test.tsx@@ -0,0 +1,74 @@+// @vitest-environment happy-dom++import { act, render } from "@testing-library/react";+import { beforeEach, describe, expect, it } from "vite-plus/test";+import { usePageScale } from "./chrome";+import { useEditorStore } from "./store";++function Canvas() {+ const { canvasRef } = usePageScale("a4");+ return <section ref={canvasRef} data-testid="canvas" />;+}++function wheel(canvas: HTMLElement, init: WheelEventInit) {+ const event = new WheelEvent("wheel", { bubbles: true, cancelable: true, ...init });+ // happy-dom's WheelEvent extends UIEvent and drops the modifier keys a browser reports on it.+ Object.assign(event, init);+ act(() => {+ canvas.dispatchEvent(event);+ });+ return event;+}++describe("canvas zoom", () => {+ beforeEach(() => {+ useEditorStore.setState({ zoom: "fit" });+ });++ it("zooms the canvas on Ctrl+wheel, and stops the browser from zooming instead", () => {+ const { getByTestId } = render(<Canvas />);+ const event = wheel(getByTestId("canvas"), { deltaY: -100, ctrlKey: true });++ expect(event.defaultPrevented).toBe(true);+ expect(useEditorStore.getState().zoom).toBeGreaterThan(1);+ });++ it("zooms on Cmd+wheel too", () => {+ const { getByTestId } = render(<Canvas />);+ wheel(getByTestId("canvas"), { deltaY: -100, metaKey: true });++ expect(useEditorStore.getState().zoom).toBeGreaterThan(1);+ });++ it("zooms out from Fit to an explicit scale", () => {+ const { getByTestId } = render(<Canvas />);+ wheel(getByTestId("canvas"), { deltaY: 100, ctrlKey: true });++ expect(useEditorStore.getState().zoom).toBeLessThan(1);+ });++ it("leaves a plain wheel to scroll the canvas", () => {+ const { getByTestId } = render(<Canvas />);+ const event = wheel(getByTestId("canvas"), { deltaY: -100 });++ expect(event.defaultPrevented).toBe(false);+ expect(useEditorStore.getState().zoom).toBe("fit");+ });++ it("leaves Ctrl+wheel outside the canvas to the browser", () => {+ render(<Canvas />);+ const event = wheel(document.body, { deltaY: -100, ctrlKey: true });++ expect(event.defaultPrevented).toBe(false);+ expect(useEditorStore.getState().zoom).toBe("fit");+ });++ it("keeps the zoom inside the editor's bounds, still without browser zoom", () => {+ useEditorStore.setState({ zoom: 3 });+ const { getByTestId } = render(<Canvas />);+ const event = wheel(getByTestId("canvas"), { deltaY: -100, ctrlKey: true });++ expect(event.defaultPrevented).toBe(true);+ expect(useEditorStore.getState().zoom).toBe(3);+ });+});diff --git a/apps/web/src/features/resume/editor/chrome.tsx b/apps/web/src/features/resume/editor/chrome.tsxindex a7acf360d..08fde9241 100644--- a/apps/web/src/features/resume/editor/chrome.tsx+++ b/apps/web/src/features/resume/editor/chrome.tsx@@ -12,7 +12,8 @@ import { IconButton } from "@reactive-resume/ui/components/icon-button";import { Tooltip, TooltipContent, TooltipTrigger } from "@reactive-resume/ui/components/tooltip";import { useBreakpoint } from "@reactive-resume/ui/hooks/use-breakpoint";import { cn } from "@reactive-resume/utils/style";-import { FIT_MAX, useEditorStore, ZOOM_MAX, ZOOM_MIN, ZOOM_STEP } from "./store";+import { clampZoom, FIT_MAX, useEditorStore, ZOOM_MAX, ZOOM_MIN, ZOOM_STEP } from "./store";+import { scrollAfterZoom, wheelZoom } from "./zoom-wheel";import { applicationsListQueryOptions } from "@/features/applications/queries";import { useLetterEditorStore } from "@/features/letters/store";import { useResumeStore } from "@/features/resume/builder/draft";@@ -68,6 +69,48 @@ export function usePageScale(format: keyof typeof PAGE_WIDTH, covered = 0) {const fitScale = canvasWidth > 0 ? Math.min(FIT_MAX, (canvasWidth - gutter - covered) / PAGE_WIDTH[format]) : 1;const pageScale = zoom === "fit" ? Math.max(0.25, fitScale) : zoom;+ // The wheel handler outlives renders: keep the current fit scale where it can read it.+ const fitScaleRef = useRef(fitScale);+ useEffect(() => {+ fitScaleRef.current = fitScale;+ }, [fitScale]);++ useEffect(() => {+ const canvas = canvasRef.current;+ if (!canvas) return;++ const zoomByWheel = (event: WheelEvent) => {+ // Ctrl/Cmd+wheel is the browser's zoom gesture (a trackpad pinch sends it too): keep it on the canvas. Every+ // other wheel is left alone, so the page scrolls normally, and the panel and the rest of the app are untouched.+ if (!(event.ctrlKey || event.metaKey) || event.altKey || event.deltaY === 0) return;++ // Even at a zoom limit the browser must not step in and zoom the whole app.+ event.preventDefault();++ const { zoom, setZoom } = useEditorStore.getState();+ const before = zoom === "fit" ? Math.max(0.25, fitScaleRef.current) : zoom;+ const after = clampZoom(wheelZoom(before, event.deltaY, event.deltaMode, canvas.clientHeight));+ if (after === before) return;++ const bounds = canvas.getBoundingClientRect();+ const pointerX = event.clientX - bounds.left;+ const pointerY = event.clientY - bounds.top;+ const { scrollLeft, scrollTop } = canvas;++ setZoom(after);++ // Zoom around the pointer, not the page's corner, once the new scale has been laid out.+ requestAnimationFrame(() => {+ if (!canvas.isConnected) return;+ canvas.scrollLeft = scrollAfterZoom(scrollLeft, pointerX, before, after);+ canvas.scrollTop = scrollAfterZoom(scrollTop, pointerY, before, after);+ });+ };++ canvas.addEventListener("wheel", zoomByWheel, { passive: false });+ return () => canvas.removeEventListener("wheel", zoomByWheel);+ }, [canvasRef]);+return { canvasRef, fitScale, pageScale };}@@ -192,7 +235,10 @@ type ZoomBarProps = {pageCount: number;};-/** − · Fit · + and the page count, floating 18px above the bottom of the canvas. Zoom runs from 60% to 150%. */+/**+ * − · Fit · + and the page count, floating 18px above the bottom of the canvas. Zoom runs from 60% to 300%, and+ * Ctrl/Cmd+wheel over the canvas zooms around the pointer.+ */export function ZoomBar({ fitScale, pageCount }: ZoomBarProps) {const { t } = useLingui();const zoom = useEditorStore((state) => state.zoom);diff --git a/apps/web/src/features/resume/editor/store.ts b/apps/web/src/features/resume/editor/store.tsindex ed6fe784f..ba7ae4a7b 100644--- a/apps/web/src/features/resume/editor/store.ts+++ b/apps/web/src/features/resume/editor/store.ts@@ -119,7 +119,8 @@ type EditorStore = {reset: () => void;};-const clampZoom = (zoom: number) => Math.min(ZOOM_MAX, Math.max(ZOOM_MIN, Math.round(zoom * 100) / 100));+/** Keep an explicit scale inside the editor's zoom bounds, at whole percentage steps. */+export const clampZoom = (zoom: number) => Math.min(ZOOM_MAX, Math.max(ZOOM_MIN, Math.round(zoom * 100) / 100));export const isSameSelection = (a: EditorSelection | null, b: EditorSelection | null) => {if (!a || !b) return a === b;diff --git a/apps/web/src/features/resume/editor/zoom-wheel.test.ts b/apps/web/src/features/resume/editor/zoom-wheel.test.tsnew file mode 100644index 000000000..7554a9cd6--- /dev/null+++ b/apps/web/src/features/resume/editor/zoom-wheel.test.ts@@ -0,0 +1,31 @@+import { describe, expect, it } from "vite-plus/test";+import { clampZoom } from "./store";+import { scrollAfterZoom, wheelZoom } from "./zoom-wheel";++describe("wheelZoom", () => {+ it("zooms in when the wheel turns up and out when it turns down", () => {+ expect(wheelZoom(1, -100)).toBeCloseTo(1.22, 2);+ expect(wheelZoom(1, 100)).toBeCloseTo(0.82, 2);+ });++ it("reads line and page deltas as pixels, so every browser asks for the same change", () => {+ expect(wheelZoom(1, -3, 1)).toBeCloseTo(wheelZoom(1, -48), 5);+ expect(wheelZoom(1, -1, 2, 900)).toBeCloseTo(wheelZoom(1, -900), 5);+ });++ it("stays inside the editor's zoom bounds once clamped", () => {+ expect(clampZoom(wheelZoom(2.9, -1000))).toBe(3);+ expect(clampZoom(wheelZoom(0.7, 1000))).toBe(0.6);+ });+});++describe("scrollAfterZoom", () => {+ it("keeps the content point under the pointer", () => {+ expect(scrollAfterZoom(0, 300, 1, 2)).toBe(300);+ expect(scrollAfterZoom(200, 100, 1, 2)).toBe(500);+ });++ it("never scrolls past the start of the canvas", () => {+ expect(scrollAfterZoom(0, 0, 2, 1)).toBe(0);+ });+});diff --git a/apps/web/src/features/resume/editor/zoom-wheel.ts b/apps/web/src/features/resume/editor/zoom-wheel.tsnew file mode 100644index 000000000..c2cd0bd08--- /dev/null+++ b/apps/web/src/features/resume/editor/zoom-wheel.ts@@ -0,0 +1,22 @@+// A wheel notch (deltaY 100) changes the scale by about 22%; a trackpad pinch sends smaller deltas, so it stays smooth+// instead of jumping a whole step per event.+const WHEEL_ZOOM_INTENSITY = 0.002;+const WHEEL_LINE_HEIGHT = 16;++/** Wheel deltas in pixels: Firefox reports lines, some browsers report pages. */+function wheelDeltaPixels(deltaY: number, deltaMode: number, viewportHeight: number) {+ if (deltaMode === 1) return deltaY * WHEEL_LINE_HEIGHT;+ if (deltaMode === 2) return deltaY * Math.max(1, viewportHeight);+ return deltaY;+}++/** The explicit scale a wheel scroll asks for: up (negative deltaY) zooms in, down zooms out. */+export function wheelZoom(current: number, deltaY: number, deltaMode = 0, viewportHeight = 0) {+ return current * Math.exp(-wheelDeltaPixels(deltaY, deltaMode, viewportHeight) * WHEEL_ZOOM_INTENSITY);+}++/** Where to scroll a canvas so the content point under the pointer stays there after a scale change. */+export function scrollAfterZoom(scroll: number, pointer: number, before: number, after: number) {+ const next = (scroll + pointer) * (after / before) - pointer;+ return Number.isFinite(next) ? Math.max(0, next) : scroll;+}diff --git a/docs/guides/editor-overview.mdx b/docs/guides/editor-overview.mdxindex 9f76f1a2e..ef80b3db0 100644--- a/docs/guides/editor-overview.mdx+++ b/docs/guides/editor-overview.mdx@@ -95,8 +95,9 @@ A small zoom bar floats at the bottom of the canvas.<img src="/images/guides/editor-overview/zoom-bar.webp" alt="Zoom bar with Zoom out, Fit, Zoom in and page count" /></Frame>-- Select **−** or **+** to zoom out or in, in 10% steps from 60% to 150%.+- Select **−** or **+** to zoom out or in, in 10% steps from 60% to 300%.- Select the middle button to fit the page to the width of the canvas. It reads **Fit** when the page is fitted, and the zoom level otherwise. You can also press <kbd>⌘</kbd> <kbd>0</kbd> (<kbd>Ctrl</kbd> <kbd>0</kbd> on Windows and Linux).+- Hold <kbd>Ctrl</kbd> (<kbd>⌘</kbd> on Mac) and scroll over the page to zoom in or out around the pointer. Scrolling over the panel or the rest of the app works as usual.- The number on the right is how many pages the resume has.Zoom only changes what you see in the editor. It has no effect on the PDF or any other download.