From 95e5e61924a76c41b6e84c6dd1a8d88d3a7b6b6b Mon Sep 17 00:00:00 2001 From: repo-worker-code Date: Tue, 27 Oct 2026 23:56:26 -0500 Subject: [PATCH 1/2] id(web): fix inverted west resize handles for images (#1389) startResize discarded the corner and onResizeMove always computed startWidth + dx. The nw/sw handles sit on the left edge, so for them dragging outward shrank the image and dragging inward grew it, and mouse-up persisted the wrong width/height attrs. Store a dx sign per drag (west corners invert) and apply it in onResizeMove before the existing 50px min-width and editor max-width clamps; east handles are unchanged. Adds image-node-view.test.ts (the component's first tests): all four corners against a real EditorView, both clamps after the sign fix, and the committed width/height attrs on mouse-up. happy-dom has no layout, so the tests stub offsetWidth/offsetHeight from the inline style, which is what a real browser layout would report. --- pkgs/id/web/src/image-node-view.test.ts | 187 ++++++++++++++++++++++++ pkgs/id/web/src/image-node-view.ts | 14 +- 2 files changed, 198 insertions(+), 3 deletions(-) create mode 100644 pkgs/id/web/src/image-node-view.test.ts diff --git a/pkgs/id/web/src/image-node-view.test.ts b/pkgs/id/web/src/image-node-view.test.ts new file mode 100644 index 0000000000..34c14f2344 --- /dev/null +++ b/pkgs/id/web/src/image-node-view.test.ts @@ -0,0 +1,187 @@ +/** + * Tests for the image NodeView resize handles (#1389). + * + * The west handles (nw/sw) sit on the LEFT edge of the image, so their + * drag direction is inverted relative to the east handles: dragging + * outward (left, negative dx) must grow the image, dragging inward must + * shrink it. Before the fix, startResize discarded the corner and every + * handle used `startWidth + dx`, so nw/sw dragged in the wrong direction + * and mouse-up persisted the wrong width/height attrs. + * + * happy-dom has no layout engine, so the tests stub offsetWidth / + * offsetHeight on the img to reflect its inline style — exactly what a + * real browser's layout would report — and on the editor DOM for the + * max-width clamp. + */ + +import type { Node as PMNode } from "prosemirror-model"; +import { EditorState } from "prosemirror-state"; +import { EditorView } from "prosemirror-view"; +import { afterEach, describe, expect, it } from "vitest"; +import { richSchema } from "./editor"; +import { ImageNodeView } from "./image-node-view"; + +const START_W = 200; +const START_H = 100; +const EDITOR_W = 1000; + +/** Build a view whose doc is one paragraph holding one 200×100 image. */ +function createViewWithImage(): { view: EditorView; container: HTMLElement; imageNode: PMNode } { + const container = document.createElement("div"); + document.body.appendChild(container); + const imageNode = richSchema.nodes.image.create({ + src: "/api/images/abc123", + alt: "cat", + width: START_W, + height: START_H, + }); + const doc = richSchema.node("doc", null, [richSchema.node("paragraph", null, [imageNode])]); + const state = EditorState.create({ schema: richSchema, doc }); + const view = new EditorView(container, { state }); + Object.defineProperty(view.dom, "offsetWidth", { configurable: true, get: () => EDITOR_W }); + return { view, container, imageNode }; +} + +/** Create the NodeView under test and stub layout sizes from inline style. */ +function createNodeView(view: EditorView, imageNode: PMNode): { nv: ImageNodeView; img: HTMLImageElement } { + // The image node is the first child of the paragraph at position 0. + const nv = new ImageNodeView(imageNode, view, () => 1); + document.body.appendChild(nv.dom); + const img = nv.dom.querySelector("img"); + if (!img) throw new Error("node view did not render an img"); + Object.defineProperty(img, "offsetWidth", { + configurable: true, + get() { + return Number.parseFloat(this.style.width) || 0; + }, + }); + Object.defineProperty(img, "offsetHeight", { + configurable: true, + get() { + return Number.parseFloat(this.style.height) || 0; + }, + }); + return { nv, img }; +} + +function mouse(target: Document | Element, type: string, clientX: number): void { + target.dispatchEvent(new MouseEvent(type, { bubbles: true, clientX, clientY: 0 })); +} + +/** Drag a corner handle from x=100 by `dx`, returning the live img size. */ +function drag(nv: ImageNodeView, img: HTMLImageElement, corner: string, dx: number): { w: number; h: number } { + const handle = nv.dom.querySelector(`.pm-image-resize-${corner}`); + expect(handle).not.toBeNull(); + mouse(handle as Element, "mousedown", 100); + mouse(document, "mousemove", 100 + dx); + const size = { w: img.offsetWidth, h: img.offsetHeight }; + mouse(document, "mouseup", 100 + dx); + return size; +} + +const views: EditorView[] = []; +const nodeViews: ImageNodeView[] = []; + +function setup(): { view: EditorView; nv: ImageNodeView; img: HTMLImageElement } { + const { view, imageNode } = createViewWithImage(); + views.push(view); + const { nv, img } = createNodeView(view, imageNode); + nodeViews.push(nv); + return { view, nv, img }; +} + +afterEach(() => { + for (const nv of nodeViews) { + nv.destroy(); + nv.dom.remove(); + } + nodeViews.length = 0; + for (const view of views) { + view.destroy(); + } + views.length = 0; + document.body.innerHTML = ""; +}); + +// ── East handles: unchanged behaviour ────────────────────────────── + +describe("east resize handles (ne/se)", () => { + it("se: dragging right widens, maintaining aspect ratio", () => { + const { nv, img } = setup(); + expect(drag(nv, img, "se", 40)).toEqual({ w: 240, h: 120 }); + }); + + it("se: dragging left shrinks", () => { + const { nv, img } = setup(); + expect(drag(nv, img, "se", -40)).toEqual({ w: 160, h: 80 }); + }); + + it("ne: dragging right widens", () => { + const { nv, img } = setup(); + expect(drag(nv, img, "ne", 40)).toEqual({ w: 240, h: 120 }); + }); +}); + +// ── West handles: inverted direction (#1389) ─────────────────────── + +describe("west resize handles (nw/sw)", () => { + it("nw: dragging outward (left) widens, maintaining aspect ratio", () => { + const { nv, img } = setup(); + expect(drag(nv, img, "nw", -40)).toEqual({ w: 240, h: 120 }); + }); + + it("nw: dragging inward (right) shrinks", () => { + const { nv, img } = setup(); + expect(drag(nv, img, "nw", 40)).toEqual({ w: 160, h: 80 }); + }); + + it("sw: dragging outward (left) widens", () => { + const { nv, img } = setup(); + expect(drag(nv, img, "sw", -40)).toEqual({ w: 240, h: 120 }); + }); + + it("sw: dragging inward (right) shrinks", () => { + const { nv, img } = setup(); + expect(drag(nv, img, "sw", 40)).toEqual({ w: 160, h: 80 }); + }); +}); + +// ── Clamps apply after the sign fix ──────────────────────────────── + +describe("resize clamps", () => { + it("min-width 50px applies to a west drag inward", () => { + const { nv, img } = setup(); + expect(drag(nv, img, "nw", 1000)).toEqual({ w: 50, h: 25 }); + }); + + it("editor max-width applies to a west drag outward", () => { + const { nv, img } = setup(); + // Max width is editor width - 40 = 960; height follows the 2:1 ratio. + expect(drag(nv, img, "sw", -5000)).toEqual({ w: 960, h: 480 }); + }); + + it("editor max-width still applies to an east drag", () => { + const { nv, img } = setup(); + expect(drag(nv, img, "se", 5000)).toEqual({ w: 960, h: 480 }); + }); +}); + +// ── Committed attrs on mouse-up ──────────────────────────────────── + +describe("committed attrs on mouse-up", () => { + it("nw outward drag commits the GROWN width/height to the node attrs", () => { + const { view, nv, img } = setup(); + drag(nv, img, "nw", -40); + const committed = view.state.doc.firstChild?.firstChild; + expect(committed?.attrs.width).toBe(240); + expect(committed?.attrs.height).toBe(120); + }); + + it("se drag commits its width/height to the node attrs", () => { + const { view, nv, img } = setup(); + drag(nv, img, "se", 40); + const committed = view.state.doc.firstChild?.firstChild; + expect(committed?.attrs.width).toBe(240); + expect(committed?.attrs.height).toBe(120); + }); +}); diff --git a/pkgs/id/web/src/image-node-view.ts b/pkgs/id/web/src/image-node-view.ts index 3460063c37..2f400be580 100644 --- a/pkgs/id/web/src/image-node-view.ts +++ b/pkgs/id/web/src/image-node-view.ts @@ -25,6 +25,11 @@ export class ImageNodeView implements NodeView { // Resize state private resizing = false; + // Sign applied to the horizontal drag delta: the west handles (nw/sw) + // sit on the LEFT edge, so for them dragging left (negative dx) must + // GROW the image and dragging right must shrink it — the opposite of + // the east handles (#1389). + private dxSign = 1; private startX = 0; private startY = 0; private startWidth = 0; @@ -161,8 +166,9 @@ export class ImageNodeView implements NodeView { // ── Resize ── - private startResize(e: MouseEvent, _corner: string): void { + private startResize(e: MouseEvent, corner: string): void { this.resizing = true; + this.dxSign = corner === "nw" || corner === "sw" ? -1 : 1; this.startX = e.clientX; this.startY = e.clientY; this.startWidth = this.img.offsetWidth || this.img.naturalWidth || 200; @@ -181,8 +187,10 @@ export class ImageNodeView implements NodeView { if (!this.resizing) return; const dx = e.clientX - this.startX; - // Use horizontal delta to maintain aspect ratio - let newWidth = Math.max(50, this.startWidth + dx); + // Use horizontal delta to maintain aspect ratio. West handles invert + // the delta (#1389); the min/max clamps below apply after the sign + // fix, so they bound the final width for every corner. + let newWidth = Math.max(50, this.startWidth + this.dxSign * dx); let newHeight = Math.round(newWidth / this.aspectRatio); // Clamp to reasonable bounds From bf7b97192dbcae3285e4499584703b9eaff354af Mon Sep 17 00:00:00 2001 From: Drewry Pope Date: Wed, 28 Oct 2026 02:06:52 -0500 Subject: [PATCH 2/2] =?UTF-8?q?test(id):=20dispatch=20mouse=20events=20via?= =?UTF-8?q?=20EventTarget=20=E2=80=94=20Document|Element=20overload=20fail?= =?UTF-8?q?s=20typecheck=20TS2345=20(#1389)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkgs/id/web/src/image-node-view.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkgs/id/web/src/image-node-view.test.ts b/pkgs/id/web/src/image-node-view.test.ts index 34c14f2344..9643435a3b 100644 --- a/pkgs/id/web/src/image-node-view.test.ts +++ b/pkgs/id/web/src/image-node-view.test.ts @@ -65,7 +65,7 @@ function createNodeView(view: EditorView, imageNode: PMNode): { nv: ImageNodeVie } function mouse(target: Document | Element, type: string, clientX: number): void { - target.dispatchEvent(new MouseEvent(type, { bubbles: true, clientX, clientY: 0 })); + (target as EventTarget).dispatchEvent(new MouseEvent(type, { bubbles: true, clientX, clientY: 0 })); } /** Drag a corner handle from x=100 by `dx`, returning the live img size. */