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): refine canvas wheel zoom anchoring (RX-200)
Amruth Pillaicbe83c6ce843
diff --git a/apps/web/src/features/resume/editor/chrome.test.tsx b/apps/web/src/features/resume/editor/chrome.test.tsxindex 84953d6e0..117c615ab 100644--- a/apps/web/src/features/resume/editor/chrome.test.tsx+++ b/apps/web/src/features/resume/editor/chrome.test.tsx@@ -5,11 +5,15 @@ import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test"import { usePageScale } from "./chrome";import { useEditorStore } from "./store";-function Canvas({ withPage = false }: { withPage?: boolean }) {+function Canvas({ pages = 0 }: { pages?: number }) {const { canvasRef } = usePageScale("a4");return (<section ref={canvasRef} data-testid="canvas">- {withPage && <div data-testid="page" />}+ {Array.from({ length: pages }, (_, index) => (+ <div key={index} data-testid={`page-${index + 1}`}>+ <canvas />+ </div>+ ))}</section>);}@@ -98,19 +102,66 @@ describe("canvas zoom", () => {const frames: FrameRequestCallback[] = [];vi.stubGlobal("requestAnimationFrame", (callback: FrameRequestCallback) => frames.push(callback));- const { getByTestId } = render(<Canvas withPage />);+ const { getByTestId } = render(<Canvas pages={1} />);const canvas = getByTestId("canvas");- const page = getByTestId("page");+ const page = getByTestId("page-1");canvas.getBoundingClientRect = () => bounds(0, 0);page.getBoundingClientRect = () => bounds(180, 100);wheel(canvas, { deltaY: -100, ctrlKey: true, clientX: 400, clientY: 300 });+ // Layout moves the page after the zoom; the scroll must follow its new position.+ page.getBoundingClientRect = () => bounds(200, 130);act(() => {for (const callback of frames.splice(0)) callback(0);});- expect(canvas.scrollLeft).toBeCloseTo(48.4, 5);- expect(canvas.scrollTop).toBeCloseTo(44, 5);+ expect(canvas.scrollLeft).toBeCloseTo(68.4, 5);+ expect(canvas.scrollTop).toBeCloseTo(74, 5);+ });++ it("keeps every pending scale change when the pointer moves to another page", () => {+ const frames: FrameRequestCallback[] = [];+ vi.stubGlobal("requestAnimationFrame", (callback: FrameRequestCallback) => frames.push(callback));++ const { getByTestId } = render(<Canvas pages={2} />);+ const canvas = getByTestId("canvas");+ const first = getByTestId("page-1");+ const second = getByTestId("page-2");+ canvas.getBoundingClientRect = () => bounds(0, 0);+ first.getBoundingClientRect = () => bounds(180, 100);+ second.getBoundingClientRect = () => bounds(180, 900);++ wheel(canvas, { deltaY: -100, ctrlKey: true, clientX: 400, clientY: 300 });+ wheel(canvas, { deltaY: -100, ctrlKey: true, clientX: 400, clientY: 1000 });+ act(() => {+ for (const callback of frames.splice(0)) callback(0);+ });++ expect(canvas.scrollLeft).toBeCloseTo(107.8, 5);+ expect(canvas.scrollTop).toBeCloseTo(49, 5);+ });++ it("leaves zoom on the first reverse notch after an overshoot at the maximum", () => {+ useEditorStore.setState({ zoom: 3 });+ const { getByTestId } = render(<Canvas />);+ const canvas = getByTestId("canvas");++ wheel(canvas, { deltaY: -100, ctrlKey: true });+ expect(useEditorStore.getState().zoom).toBe(3);++ wheel(canvas, { deltaY: 100, ctrlKey: true });+ expect(useEditorStore.getState().zoom).toBe(2.46);+ });++ it("starts a fresh gesture when the zoom was set elsewhere", () => {+ const { getByTestId } = render(<Canvas />);+ const canvas = getByTestId("canvas");++ wheel(canvas, { deltaY: -1, ctrlKey: true });+ useEditorStore.setState({ zoom: 1.2 });+ wheel(canvas, { deltaY: -1, ctrlKey: true });++ expect(useEditorStore.getState().zoom).toBe(1.2);});it("keeps the zoom inside the editor's bounds, still without browser zoom", () => {diff --git a/apps/web/src/features/resume/editor/chrome.tsx b/apps/web/src/features/resume/editor/chrome.tsxindex d3687bb00..a7f8801ad 100644--- a/apps/web/src/features/resume/editor/chrome.tsx+++ b/apps/web/src/features/resume/editor/chrome.tsx@@ -57,6 +57,32 @@ function useCanvasWidth() {return [ref, width] as const;}+/**+ * The rendered page under the pointer, so zoom holds the point on that page rather than the first page. A pointer in+ * the gap between pages gets the nearest page; while the preview is still loading, its only child.+ */+function pageUnderPointer(canvas: HTMLElement, clientX: number, clientY: number) {+ const pages = Array.from(canvas.querySelectorAll("canvas"))+ .map((element) => element.parentElement)+ .filter((element): element is HTMLElement => element !== null);+ if (pages.length === 0) return canvas.firstElementChild;++ let nearest: HTMLElement | null = null;+ let nearestDistance = Number.POSITIVE_INFINITY;+ for (const page of pages) {+ const bounds = page.getBoundingClientRect();+ const dx = clientX < bounds.left ? bounds.left - clientX : clientX > bounds.right ? clientX - bounds.right : 0;+ const dy = clientY < bounds.top ? bounds.top - clientY : clientY > bounds.bottom ? clientY - bounds.bottom : 0;+ const distance = dx + dy;+ if (distance === 0) return page;+ if (distance < nearestDistance) {+ nearest = page;+ nearestDistance = distance;+ }+ }+ return nearest;+}+/*** The canvas ref, the fit scale for the page format, and the scale the page is drawn at (zoom, or fit). `covered` is* the canvas width something lies over (a sheet), which fit leaves out.@@ -75,8 +101,8 @@ 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);+ // A gesture's unrounded scale, the zoom it was last rounded to, and the page point it holds under the pointer.+ const wheelZoomRef = useRef<{ scale: number; stored: number } | null>(null);const zoomAnchorRef = useRef<ZoomAnchor | null>(null);const zoomFrameRef = useRef<number | null>(null);@@ -94,25 +120,37 @@ export function usePageScale(format: keyof typeof PAGE_WIDTH, covered = 0) {const { zoom, setZoom } = useEditorStore.getState();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;+ // Keep the unrounded scale between events, so a slow trackpad pinch adds up instead of rounding away. A+ // zoom set anywhere else (Fit, the zoom bar) starts a fresh gesture.+ const previous = wheelZoomRef.current;+ const accumulated = previous?.stored === stored ? previous.scale : null;+ const before = 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 changed = after !== stored && !(raw < before && after > stored);+ // Hold a scale inside the bounds, so an overshoot does not swallow the first reverse notch. A Fit below the+ // minimum keeps its own scale until a zoom-in raises it into range.+ const retained = changed+ ? after+ : raw < before && stored < ZOOM_MIN+ ? before+ : Math.min(ZOOM_MAX, Math.max(ZOOM_MIN, raw));+ wheelZoomRef.current = { scale: retained, stored: changed ? after : stored };+ if (!changed) return;++ // The page under the pointer, not the canvas's corner, is what stays put while it grows or shrinks.+ const page = pageUnderPointer(canvas, event.clientX, event.clientY);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.+ if (anchor) {+ // Several wheel events can land before one frame: keep every scale change, and follow the latest+ // pointer and the page now under it.anchor.ratio *= after / stored;+ anchor.page = page;+ anchor.contentX = event.clientX - pageBounds.left;+ anchor.contentY = event.clientY - pageBounds.top;anchor.pointerX = event.clientX;anchor.pointerY = event.clientY;} else {diff --git a/apps/web/src/features/resume/editor/zoom-wheel.test.ts b/apps/web/src/features/resume/editor/zoom-wheel.test.tsindex 70861fc52..663d5b49c 100644--- a/apps/web/src/features/resume/editor/zoom-wheel.test.ts+++ b/apps/web/src/features/resume/editor/zoom-wheel.test.ts@@ -43,9 +43,9 @@ describe("anchoredScroll", () => {});});- it("never scrolls past the start of the canvas", () => {+ it("keeps negative horizontal offsets for RTL canvases but never scrolls above the top", () => {expect(anchoredScroll(anchor(100, 100, 1, 400, 300), { left: 0, top: 0 }, { left: 180, top: 100 })).toEqual({- left: 0,+ left: -120,top: 0,});});diff --git a/apps/web/src/features/resume/editor/zoom-wheel.ts b/apps/web/src/features/resume/editor/zoom-wheel.tsindex 43a18f779..5341ac8f0 100644--- a/apps/web/src/features/resume/editor/zoom-wheel.ts+++ b/apps/web/src/features/resume/editor/zoom-wheel.ts@@ -32,7 +32,9 @@ export function anchoredScroll(pageBounds: { left: number; top: number },) {return {- left: Math.max(0, scroll.left + pageBounds.left + anchor.contentX * anchor.ratio - anchor.pointerX),+ // No lower bound: RTL canvases scroll to negative `scrollLeft` values, and the browser clamps out-of-range+ // writes on its own.+ left: scroll.left + pageBounds.left + anchor.contentX * anchor.ratio - anchor.pointerX,top: Math.max(0, scroll.top + pageBounds.top + anchor.contentY * anchor.ratio - anchor.pointerY),};}