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
fix(builder): address canvas wheel zoom review findings (RX-200)
Amruth Pillaib2f21eac3178
diff --git a/apps/web/src/features/resume/editor/chrome.test.tsx b/apps/web/src/features/resume/editor/chrome.test.tsxindex 7d2c59b7d..84953d6e0 100644--- a/apps/web/src/features/resume/editor/chrome.test.tsx+++ b/apps/web/src/features/resume/editor/chrome.test.tsx@@ -1,15 +1,31 @@// @vitest-environment happy-domimport { act, render } from "@testing-library/react";-import { beforeEach, describe, expect, it } from "vite-plus/test";+import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";import { usePageScale } from "./chrome";import { useEditorStore } from "./store";-function Canvas() {+function Canvas({ withPage = false }: { withPage?: boolean }) {const { canvasRef } = usePageScale("a4");- return <section ref={canvasRef} data-testid="canvas" />;+ return (+ <section ref={canvasRef} data-testid="canvas">+ {withPage && <div data-testid="page" />}+ </section>+ );}+const bounds = (left: number, top: number): DOMRect => ({+ left,+ top,+ width: 0,+ height: 0,+ right: left,+ bottom: top,+ x: left,+ y: top,+ toJSON: () => ({}),+});+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.@@ -25,6 +41,10 @@ describe("canvas zoom", () => {useEditorStore.setState({ zoom: "fit" });});+ afterEach(() => {+ vi.unstubAllGlobals();+ });+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 });@@ -63,6 +83,36 @@ describe("canvas zoom", () => {expect(useEditorStore.getState().zoom).toBe("fit");});+ it("adds up small wheel deltas instead of rounding each one away", () => {+ const { getByTestId } = render(<Canvas />);+ const canvas = getByTestId("canvas");++ wheel(canvas, { deltaY: -1, ctrlKey: true });+ wheel(canvas, { deltaY: -1, ctrlKey: true });+ wheel(canvas, { deltaY: -1, ctrlKey: true });++ expect(useEditorStore.getState().zoom).toBe(1.01);+ });++ it("keeps the page point under the pointer and scrolls the canvas with it", () => {+ const frames: FrameRequestCallback[] = [];+ vi.stubGlobal("requestAnimationFrame", (callback: FrameRequestCallback) => frames.push(callback));++ const { getByTestId } = render(<Canvas withPage />);+ const canvas = getByTestId("canvas");+ const page = getByTestId("page");+ canvas.getBoundingClientRect = () => bounds(0, 0);+ page.getBoundingClientRect = () => bounds(180, 100);++ wheel(canvas, { deltaY: -100, ctrlKey: true, clientX: 400, clientY: 300 });+ act(() => {+ for (const callback of frames.splice(0)) callback(0);+ });++ expect(canvas.scrollLeft).toBeCloseTo(48.4, 5);+ expect(canvas.scrollTop).toBeCloseTo(44, 5);+ });+it("keeps the zoom inside the editor's bounds, still without browser zoom", () => {useEditorStore.setState({ zoom: 3 });const { getByTestId } = render(<Canvas />);@@ -71,4 +121,32 @@ describe("canvas zoom", () => {expect(event.defaultPrevented).toBe(true);expect(useEditorStore.getState().zoom).toBe(3);});++ it("stops at the minimum scale without zooming the browser", () => {+ useEditorStore.setState({ zoom: 0.6 });+ const { getByTestId } = render(<Canvas />);+ const event = wheel(getByTestId("canvas"), { deltaY: 100, ctrlKey: true });++ expect(event.defaultPrevented).toBe(true);+ expect(useEditorStore.getState().zoom).toBe(0.6);+ });++ it("does not enlarge a Fit that is already below the minimum", () => {+ vi.stubGlobal(+ "ResizeObserver",+ class {+ constructor(private readonly callback: ResizeObserverCallback) {}+ observe() {+ this.callback([{ contentRect: { width: 318 } } as ResizeObserverEntry], this as never);+ }+ disconnect() {}+ },+ );++ const { getByTestId } = render(<Canvas />);+ const event = wheel(getByTestId("canvas"), { deltaY: 100, ctrlKey: true });++ expect(event.defaultPrevented).toBe(true);+ expect(useEditorStore.getState().zoom).toBe("fit");+ });});diff --git a/apps/web/src/features/resume/editor/chrome.tsx b/apps/web/src/features/resume/editor/chrome.tsxindex 08fde9241..d3687bb00 100644--- a/apps/web/src/features/resume/editor/chrome.tsx+++ b/apps/web/src/features/resume/editor/chrome.tsx@@ -13,7 +13,7 @@ import { Tooltip, TooltipContent, TooltipTrigger } from "@reactive-resume/ui/comimport { useBreakpoint } from "@reactive-resume/ui/hooks/use-breakpoint";import { cn } from "@reactive-resume/utils/style";import { clampZoom, FIT_MAX, useEditorStore, ZOOM_MAX, ZOOM_MIN, ZOOM_STEP } from "./store";-import { scrollAfterZoom, wheelZoom } from "./zoom-wheel";+import { anchoredScroll, wheelZoom, type ZoomAnchor } from "./zoom-wheel";import { applicationsListQueryOptions } from "@/features/applications/queries";import { useLetterEditorStore } from "@/features/letters/store";import { useResumeStore } from "@/features/resume/builder/draft";@@ -75,6 +75,11 @@ export function usePageScale(format: keyof typeof PAGE_WIDTH, covered = 0) {fitScaleRef.current = fitScale;}, [fitScale]);+ // A gesture's unrounded scale and the page point it holds under the pointer, kept between wheel events.+ const wheelZoomRef = useRef<number | null>(null);+ const zoomAnchorRef = useRef<ZoomAnchor | null>(null);+ const zoomFrameRef = useRef<number | null>(null);+useEffect(() => {const canvas = canvasRef.current;if (!canvas) return;@@ -88,27 +93,67 @@ export function usePageScale(format: keyof typeof PAGE_WIDTH, covered = 0) {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 stored = zoom === "fit" ? Math.max(0.25, fitScaleRef.current) : zoom;+ // Keep the unrounded scale between events, so a slow trackpad pinch adds up instead of rounding away.+ const accumulated = wheelZoomRef.current;+ const before = accumulated !== null && clampZoom(accumulated) === stored ? accumulated : stored;+ const raw = wheelZoom(before, event.deltaY, event.deltaMode, canvas.clientHeight);+ const after = clampZoom(raw);+ wheelZoomRef.current = raw;++ // Fit can sit below the explicit minimum on a small canvas: zooming out must not jump up to it.+ if (raw < before && after > stored) return;+ if (after === stored) return;++ // The page, not its corner, is what stays under the pointer while it grows or shrinks.+ const page = canvas.querySelector("canvas")?.parentElement ?? canvas.firstElementChild;+ const pageBounds = page?.getBoundingClientRect();+ if (page && pageBounds) {+ const anchor = zoomAnchorRef.current;+ if (anchor?.page === page) {+ // Several wheel events can land before one frame: combine their scale changes on one anchor.+ anchor.ratio *= after / stored;+ anchor.pointerX = event.clientX;+ anchor.pointerY = event.clientY;+ } else {+ zoomAnchorRef.current = {+ page,+ contentX: event.clientX - pageBounds.left,+ contentY: event.clientY - pageBounds.top,+ ratio: after / stored,+ pointerX: event.clientX,+ pointerY: event.clientY,+ };+ }- const bounds = canvas.getBoundingClientRect();- const pointerX = event.clientX - bounds.left;- const pointerY = event.clientY - bounds.top;- const { scrollLeft, scrollTop } = canvas;+ if (zoomFrameRef.current === null) {+ zoomFrameRef.current = requestAnimationFrame(() => {+ zoomFrameRef.current = null;+ const anchor = zoomAnchorRef.current;+ zoomAnchorRef.current = null;+ if (!anchor?.page.isConnected) return;+ const { left, top } = anchoredScroll(+ anchor,+ { left: canvas.scrollLeft, top: canvas.scrollTop },+ anchor.page.getBoundingClientRect(),+ );+ canvas.scrollLeft = left;+ canvas.scrollTop = top;+ });+ }+ }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);+ return () => {+ canvas.removeEventListener("wheel", zoomByWheel);+ if (zoomFrameRef.current !== null) cancelAnimationFrame(zoomFrameRef.current);+ zoomFrameRef.current = null;+ zoomAnchorRef.current = null;+ wheelZoomRef.current = null;+ };}, [canvasRef]);return { canvasRef, fitScale, pageScale };diff --git a/apps/web/src/features/resume/editor/zoom-wheel.test.ts b/apps/web/src/features/resume/editor/zoom-wheel.test.tsindex 7554a9cd6..70861fc52 100644--- a/apps/web/src/features/resume/editor/zoom-wheel.test.ts+++ b/apps/web/src/features/resume/editor/zoom-wheel.test.ts@@ -1,6 +1,6 @@import { describe, expect, it } from "vite-plus/test";import { clampZoom } from "./store";-import { scrollAfterZoom, wheelZoom } from "./zoom-wheel";+import { anchoredScroll, wheelZoom } from "./zoom-wheel";describe("wheelZoom", () => {it("zooms in when the wheel turns up and out when it turns down", () => {@@ -19,13 +19,34 @@ describe("wheelZoom", () => {});});-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);+describe("anchoredScroll", () => {+ const anchor = (contentX: number, contentY: number, ratio: number, pointerX: number, pointerY: number) => ({+ page: {} as Element,+ contentX,+ contentY,+ ratio,+ pointerX,+ pointerY,+ });++ it("keeps the page point under the pointer, including the page's place on the canvas", () => {+ expect(anchoredScroll(anchor(220, 200, 2, 400, 300), { left: 0, top: 0 }, { left: 180, top: 100 })).toEqual({+ left: 220,+ top: 200,+ });+ });++ it("adds the change to the scroll already there", () => {+ expect(anchoredScroll(anchor(220, 200, 2, 400, 300), { left: 100, top: 50 }, { left: 80, top: 50 })).toEqual({+ left: 220,+ top: 200,+ });});it("never scrolls past the start of the canvas", () => {- expect(scrollAfterZoom(0, 0, 2, 1)).toBe(0);+ expect(anchoredScroll(anchor(100, 100, 1, 400, 300), { left: 0, top: 0 }, { left: 180, top: 100 })).toEqual({+ left: 0,+ top: 0,+ });});});diff --git a/apps/web/src/features/resume/editor/zoom-wheel.ts b/apps/web/src/features/resume/editor/zoom-wheel.tsindex c2cd0bd08..43a18f779 100644--- a/apps/web/src/features/resume/editor/zoom-wheel.ts+++ b/apps/web/src/features/resume/editor/zoom-wheel.ts@@ -15,8 +15,24 @@ export function wheelZoom(current: number, deltaY: number, deltaMode = 0, viewporeturn 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;+/** A page point held under the pointer while its scale changes, before the new layout is measured. */+export type ZoomAnchor = {+ page: Element;+ contentX: number;+ contentY: number;+ ratio: number;+ pointerX: number;+ pointerY: number;+};++/** Scroll offsets that keep the anchored page point under the pointer after the scale change is laid out. */+export function anchoredScroll(+ anchor: ZoomAnchor,+ scroll: { left: number; top: number },+ pageBounds: { left: number; top: number },+) {+ return {+ left: Math.max(0, scroll.left + pageBounds.left + anchor.contentX * anchor.ratio - anchor.pointerX),+ top: Math.max(0, scroll.top + pageBounds.top + anchor.contentY * anchor.ratio - anchor.pointerY),+ };}