Skip to content

CanvasAnvil is an open source AI diagram maker and flowchart generator - an infinite-canvas diagram editor and self-hosted drawio alternative that writes editable .drawio files. Text to diagram, then fix nodes, layout and labels in place. Also AI presentations, interior design CAD/BOM, posters and infographics.

refactor(ppt): make the resize-handle geometry a tested function

codingFeng101b7778e532ef4
diff --git a/client/workspaces/ppt/canvas/PptCanvas.tsx b/client/workspaces/ppt/canvas/PptCanvas.tsx
index ddba525..d9b69c2 100644
--- a/client/workspaces/ppt/canvas/PptCanvas.tsx
+++ b/client/workspaces/ppt/canvas/PptCanvas.tsx
@@ -6,7 +6,11 @@ import { runInParallel, sleep } from "./lib/concurrency";
import { parseJsonLoose } from "./lib/parse-json";
import { extractPdfPagesAsImages, isPdfFile } from "./lib/deck-source";
import { getSlideshowDimensions } from "./lib/slideshow-size";
-import { clampTextBlockRect, withTextBlocks } from "./lib/render-layers";
+import {
+ clampTextBlockRect,
+ resizeRectFromHandle,
+ withTextBlocks,
+} from "./lib/render-layers";
import { GenerationProgress } from "./views/GenerationProgress";
import { OutlineReview } from "./views/OutlineReview";
import { CreationStart } from "./views/CreationStart";
@@ -1418,36 +1422,13 @@ export function PptCanvas({
resize.versionId,
);
} else {
- const minW = 0.05;
- const minH = 0.04;
- let nextX = resize.startX;
- let nextY = resize.startY;
- let nextW = resize.startW;
- let nextH = resize.startH;
- if (resize.handle.includes("e")) nextW = resize.startW + dw;
- if (resize.handle.includes("s")) nextH = resize.startH + dh;
- if (resize.handle.includes("w")) {
- nextX = resize.startX + dw;
- nextW = resize.startW - dw;
- }
- if (resize.handle.includes("n")) {
- nextY = resize.startY + dh;
- nextH = resize.startH - dh;
- }
- if (nextW < minW) {
- if (resize.handle.includes("w")) nextX -= minW - nextW;
- nextW = minW;
- }
- if (nextH < minH) {
- if (resize.handle.includes("n")) nextY -= minH - nextH;
- nextH = minH;
- }
- updateSlideTextBlockRect(
- resize.slideId,
- resize.blockId,
- { x: nextX, y: nextY, w: nextW, h: nextH },
- resize.versionId,
+ const next = resizeRectFromHandle(
+ { x: resize.startX, y: resize.startY, w: resize.startW, h: resize.startH },
+ resize.handle,
+ dw,
+ dh,
);
+ updateSlideTextBlockRect(resize.slideId, resize.blockId, next, resize.versionId);
}
}
};
diff --git a/client/workspaces/ppt/canvas/lib/render-layers.ts b/client/workspaces/ppt/canvas/lib/render-layers.ts
index c6769f0..6c8760f 100644
--- a/client/workspaces/ppt/canvas/lib/render-layers.ts
+++ b/client/workspaces/ppt/canvas/lib/render-layers.ts
@@ -75,3 +75,53 @@ export function clampTextBlockRect(
return { ...block, x, y, w, h };
}
+
+/** Which edges a resize handle moves, named by compass point. */
+export type ResizeHandle = "n" | "s" | "e" | "w" | "ne" | "nw" | "se" | "sw";
+
+export interface Rect {
+ x: number;
+ y: number;
+ w: number;
+ h: number;
+}
+
+/**
+ * The rectangle a drag on `handle` produces, given how far the pointer has
+ * moved as a fraction of the slide.
+ *
+ * Dragging a west or north edge moves the origin as well as the size, so on
+ * hitting the minimum the origin has to be pushed back by the shortfall --
+ * without that the opposite edge creeps along with the pointer and the box
+ * slides across the slide instead of stopping.
+ */
+export function resizeRectFromHandle(
+ start: Rect,
+ handle: ResizeHandle,
+ dw: number,
+ dh: number,
+): Rect {
+ let { x, y, w, h } = start;
+
+ if (handle.includes("e")) w = start.w + dw;
+ if (handle.includes("s")) h = start.h + dh;
+ if (handle.includes("w")) {
+ x = start.x + dw;
+ w = start.w - dw;
+ }
+ if (handle.includes("n")) {
+ y = start.y + dh;
+ h = start.h - dh;
+ }
+
+ if (w < FLOOR.w) {
+ if (handle.includes("w")) x -= FLOOR.w - w;
+ w = FLOOR.w;
+ }
+ if (h < FLOOR.h) {
+ if (handle.includes("n")) y -= FLOOR.h - h;
+ h = FLOOR.h;
+ }
+
+ return { x, y, w, h };
+}
diff --git a/tests/ppt-render-layers.test.ts b/tests/ppt-render-layers.test.ts
index 1c0b24e..d0cc173 100644
--- a/tests/ppt-render-layers.test.ts
+++ b/tests/ppt-render-layers.test.ts
@@ -2,6 +2,7 @@ import assert from "node:assert/strict";
import { test } from "node:test";
import {
clampTextBlockRect,
+ resizeRectFromHandle,
withTextBlocks,
type RenderLayerMap,
} from "../client/workspaces/ppt/canvas/lib/render-layers";
@@ -127,3 +128,60 @@ test("clamping returns a new block rather than mutating", () => {
assert.equal(before.x, 0.1);
assert.equal(after.text, "hello", "everything else is carried over");
});
+
+/**
+ * Resizing a text box by one of its eight handles. The compass letters say
+ * which edges move; a west or north drag moves the origin too, which is where
+ * the arithmetic stops being obvious.
+ */
+
+const rect = { x: 0.2, y: 0.2, w: 0.4, h: 0.4 };
+
+test("an east or south handle moves only the size", () => {
+ assert.deepEqual(resizeRectFromHandle(rect, "e", 0.1, 0.1), { x: 0.2, y: 0.2, w: 0.5, h: 0.4 });
+ assert.deepEqual(resizeRectFromHandle(rect, "s", 0.1, 0.1), { x: 0.2, y: 0.2, w: 0.4, h: 0.5 });
+});
+
+test("a west or north handle moves the origin as well", () => {
+ const west = resizeRectFromHandle(rect, "w", 0.1, 0);
+ assert.ok(Math.abs(west.x - 0.3) < 1e-9, `x ${west.x}`);
+ assert.ok(Math.abs(west.w - 0.3) < 1e-9, "the right edge stays put");
+
+ const north = resizeRectFromHandle(rect, "n", 0, 0.1);
+ assert.ok(Math.abs(north.y - 0.3) < 1e-9);
+ assert.ok(Math.abs(north.h - 0.3) < 1e-9, "the bottom edge stays put");
+});
+
+test("a corner handle moves both axes", () => {
+ const nw = resizeRectFromHandle(rect, "nw", 0.1, 0.1);
+ assert.ok(Math.abs(nw.x - 0.3) < 1e-9);
+ assert.ok(Math.abs(nw.y - 0.3) < 1e-9);
+ assert.ok(Math.abs(nw.w - 0.3) < 1e-9);
+ assert.ok(Math.abs(nw.h - 0.3) < 1e-9);
+});
+
+test("a west drag past the minimum stops rather than sliding the box along", () => {
+ // Without the origin compensation the right edge would keep following the
+ // pointer and the whole box would travel across the slide.
+ const squashed = resizeRectFromHandle(rect, "w", 0.5, 0);
+ assert.equal(squashed.w, 0.05, "clamped to the floor");
+ assert.ok(Math.abs(squashed.x - 0.55) < 1e-9, "the right edge stays at 0.6");
+ assert.ok(Math.abs(squashed.x + squashed.w - 0.6) < 1e-9);
+});
+
+test("a north drag past the minimum keeps the bottom edge in place", () => {
+ const squashed = resizeRectFromHandle(rect, "n", 0, 0.5);
+ assert.equal(squashed.h, 0.04);
+ assert.ok(Math.abs(squashed.y + squashed.h - 0.6) < 1e-9, "the bottom edge stays at 0.6");
+});
+
+test("an east drag past the minimum does not move the origin", () => {
+ const squashed = resizeRectFromHandle(rect, "e", -0.5, 0);
+ assert.equal(squashed.w, 0.05);
+ assert.equal(squashed.x, 0.2, "the left edge is the anchor");
+});
+
+test("a zero drag returns the rectangle unchanged", () => {
+ assert.deepEqual(resizeRectFromHandle(rect, "se", 0, 0), rect);
+ assert.deepEqual(resizeRectFromHandle(rect, "nw", 0, 0), rect);
+});