Skip to content

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!

fix(builder): cover loading and external zoom in wheel anchoring (RX-200)

Amruth Pillai32f27b0f1b60
diff --git a/apps/web/src/features/resume/editor/chrome.test.tsx b/apps/web/src/features/resume/editor/chrome.test.tsx
index 117c615ab..3b4f6a508 100644
--- a/apps/web/src/features/resume/editor/chrome.test.tsx
+++ b/apps/web/src/features/resume/editor/chrome.test.tsx
@@ -5,7 +5,7 @@ import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test"
import { usePageScale } from "./chrome";
import { useEditorStore } from "./store";
-function Canvas({ pages = 0 }: { pages?: number }) {
+function Canvas({ pages = 0, loaderPages = 0 }: { pages?: number; loaderPages?: number }) {
const { canvasRef } = usePageScale("a4");
return (
<section ref={canvasRef} data-testid="canvas">
@@ -14,6 +14,11 @@ function Canvas({ pages = 0 }: { pages?: number }) {
<canvas />
</div>
))}
+ {Array.from({ length: loaderPages }, (_, index) => (
+ <figure key={index} data-testid={`loader-page-${index + 1}`}>
+ <div />
+ </figure>
+ ))}
</section>
);
}
@@ -119,6 +124,48 @@ describe("canvas zoom", () => {
expect(canvas.scrollTop).toBeCloseTo(74, 5);
});
+ it("anchors to the loader's page while the preview is still loading", () => {
+ const frames: FrameRequestCallback[] = [];
+ vi.stubGlobal("requestAnimationFrame", (callback: FrameRequestCallback) => frames.push(callback));
+
+ const { getByTestId } = render(<Canvas loaderPages={1} />);
+ const canvas = getByTestId("canvas");
+ const page = getByTestId("loader-page-1").lastElementChild as HTMLElement;
+ 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("drops a pending anchor when zoom changes elsewhere", () => {
+ const frames: FrameRequestCallback[] = [];
+ vi.stubGlobal("requestAnimationFrame", (callback: FrameRequestCallback) => frames.push(callback));
+ vi.stubGlobal("cancelAnimationFrame", () => {
+ frames.length = 0;
+ });
+
+ const { getByTestId } = render(<Canvas pages={1} />);
+ const canvas = getByTestId("canvas");
+ 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 });
+ act(() => {
+ useEditorStore.setState({ zoom: 3 });
+ });
+
+ expect(frames).toHaveLength(0);
+ expect(canvas.scrollLeft).toBe(0);
+ expect(canvas.scrollTop).toBe(0);
+ });
+
it("keeps every pending scale change when the pointer moves to another page", () => {
const frames: FrameRequestCallback[] = [];
vi.stubGlobal("requestAnimationFrame", (callback: FrameRequestCallback) => frames.push(callback));
diff --git a/apps/web/src/features/resume/editor/chrome.tsx b/apps/web/src/features/resume/editor/chrome.tsx
index a7f8801ad..64e614a17 100644
--- a/apps/web/src/features/resume/editor/chrome.tsx
+++ b/apps/web/src/features/resume/editor/chrome.tsx
@@ -62,10 +62,16 @@ function useCanvasWidth() {
* 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;
+ // A rendered page is the canvas's box; the loading placeholder has a figure per page instead. Both keep the page
+ // itself last in the figure, after its caption.
+ const pages = new Set<HTMLElement>();
+ for (const element of canvas.querySelectorAll("canvas")) {
+ if (element.parentElement) pages.add(element.parentElement);
+ }
+ for (const figure of canvas.querySelectorAll("figure")) {
+ if (figure.lastElementChild instanceof HTMLElement) pages.add(figure.lastElementChild);
+ }
+ if (pages.size === 0) return canvas.firstElementChild;
let nearest: HTMLElement | null = null;
let nearestDistance = Number.POSITIVE_INFINITY;
@@ -105,6 +111,8 @@ export function usePageScale(format: keyof typeof PAGE_WIDTH, covered = 0) {
const wheelZoomRef = useRef<{ scale: number; stored: number } | null>(null);
const zoomAnchorRef = useRef<ZoomAnchor | null>(null);
const zoomFrameRef = useRef<number | null>(null);
+ /** The last zoom this handler set, so a change from Fit or the zoom bar can be told apart. */
+ const wheelAppliedRef = useRef<number | null>(null);
useEffect(() => {
const canvas = canvasRef.current;
@@ -181,16 +189,30 @@ export function usePageScale(format: keyof typeof PAGE_WIDTH, covered = 0) {
}
}
+ wheelAppliedRef.current = after;
setZoom(after);
};
+ // A zoom from Fit or the zoom bar replaces the gesture: its pending frame must not scroll the new layout by the
+ // old scale ratio.
+ const unsubscribe = useEditorStore.subscribe((state) => {
+ if (state.zoom === wheelAppliedRef.current) return;
+ wheelAppliedRef.current = null;
+ wheelZoomRef.current = null;
+ zoomAnchorRef.current = null;
+ if (zoomFrameRef.current !== null) cancelAnimationFrame(zoomFrameRef.current);
+ zoomFrameRef.current = null;
+ });
+
canvas.addEventListener("wheel", zoomByWheel, { passive: false });
return () => {
+ unsubscribe();
canvas.removeEventListener("wheel", zoomByWheel);
if (zoomFrameRef.current !== null) cancelAnimationFrame(zoomFrameRef.current);
zoomFrameRef.current = null;
zoomAnchorRef.current = null;
wheelZoomRef.current = null;
+ wheelAppliedRef.current = null;
};
}, [canvasRef]);