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.
TypeScript ai-agentsai-diagramai-presentationbomcadcanvasdiagramdiagram-generatordrawioflowchartinfographicspptppt-generatorpresentation-generatorreactsvgedittext-to-diagramtypescriptworkflow
refactor(ppt): give the render layer one place to be rewritten
codingFeng101f051024826e4
diff --git a/client/workspaces/ppt/canvas/PptCanvas.tsx b/client/workspaces/ppt/canvas/PptCanvas.tsxindex a8b1528..70cf67e 100644--- a/client/workspaces/ppt/canvas/PptCanvas.tsx+++ b/client/workspaces/ppt/canvas/PptCanvas.tsx@@ -6,6 +6,7 @@ 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 {getOriginalSlideVersion as originalVersionOf,getTextlessBackgroundVersion as textlessBackgroundOf,@@ -1396,21 +1397,7 @@ export function PptCanvas({const nextBlocks = (currentLayer?.textBlocks || []).map((block) =>block.id === blockId ? { ...block, text: nextText } : block);- setRenderLayers((prev) => {- const layer = prev[slideId]?.[versionId];- if (!layer) return prev;- return {- ...prev,- [slideId]: {- ...(prev[slideId] || {}),- [versionId]: {- ...layer,- textBlocks: nextBlocks,- elements: mergeTextBlocksIntoElements(nextBlocks, layer.elements),- },- },- };- });+ setRenderLayers((prev) => withTextBlocks(prev, slideId, versionId, () => nextBlocks));setLocalSlides((prev) =>prev.map((slide) => {if (slide.id !== slideId) return slide;@@ -1435,75 +1422,29 @@ export function PptCanvas({const { versionId: currentVersionId } = getSlideVersionMeta(slideId);const versionId = targetVersionId || currentVersionId;if (!versionId) return;- setRenderLayers((prev) => {- const layer = prev[slideId]?.[versionId];- if (!layer) return prev;- const nextBlocks = layer.textBlocks.map((block) => {- if (block.id !== blockId) return block;- const minW = Math.max(0.05, block.role === "title" ? 0.18 : block.role === "tag" ? 0.07 : 0.1);- const minH = Math.max(0.04, block.role === "title" ? 0.08 : block.role === "tag" ? 0.045 : 0.06);- let x = typeof nextRect.x === "number" ? nextRect.x : block.x;- let y = typeof nextRect.y === "number" ? nextRect.y : block.y;- let w = typeof nextRect.w === "number" ? nextRect.w : block.w;- let h = typeof nextRect.h === "number" ? nextRect.h : block.h;- w = Math.max(minW, Math.min(1, w));- h = Math.max(minH, Math.min(1, h));- x = Math.max(0, Math.min(1 - w, x));- y = Math.max(0, Math.min(1 - h, y));- return { ...block, x, y, w, h };- });- return {- ...prev,- [slideId]: {- ...(prev[slideId] || {}),- [versionId]: {- ...layer,- textBlocks: nextBlocks,- elements: mergeTextBlocksIntoElements(nextBlocks, layer.elements),- },- },- };- });+ setRenderLayers((prev) =>+ withTextBlocks(prev, slideId, versionId, (blocks) =>+ blocks.map((block) =>+ block.id === blockId ? clampTextBlockRect(block, nextRect) : block,+ ),+ ),+ );};const appendSlideTextBlock = (slideId: string, block: PptTextBlock) => {const { versionId } = getSlideVersionMeta(slideId);if (!versionId) return;- setRenderLayers((prev) => {- const layer = prev[slideId]?.[versionId];- if (!layer) return prev;- const nextBlocks = [...layer.textBlocks, block];- return {- ...prev,- [slideId]: {- ...(prev[slideId] || {}),- [versionId]: {- ...layer,- textBlocks: nextBlocks,- elements: mergeTextBlocksIntoElements(nextBlocks, layer.elements),- },- },- };- });+ setRenderLayers((prev) =>+ withTextBlocks(prev, slideId, versionId, (blocks) => [...blocks, block]),+ );};const deleteSlideTextBlock = (slideId: string, blockId: string) => {const { versionId } = getSlideVersionMeta(slideId);if (!versionId) return;- setRenderLayers((prev) => {- const layer = prev[slideId]?.[versionId];- if (!layer) return prev;- const nextBlocks = layer.textBlocks.filter((block) => block.id !== blockId);- return {- ...prev,- [slideId]: {- ...(prev[slideId] || {}),- [versionId]: {- ...layer,- textBlocks: nextBlocks,- elements: mergeTextBlocksIntoElements(nextBlocks, layer.elements),- },- },- };- });+ setRenderLayers((prev) =>+ withTextBlocks(prev, slideId, versionId, (blocks) =>+ blocks.filter((block) => block.id !== blockId),+ ),+ );setSelectedReviewTextBlockId((current) => (current === blockId ? null : current));};diff --git a/client/workspaces/ppt/canvas/lib/render-layers.ts b/client/workspaces/ppt/canvas/lib/render-layers.tsnew file mode 100644index 0000000..c6769f0--- /dev/null+++ b/client/workspaces/ppt/canvas/lib/render-layers.ts@@ -0,0 +1,77 @@+import { mergeTextBlocksIntoElements } from "@/workspaces/ppt/canvas/slide-content";+import type { SlideRenderLayer } from "@/workspaces/ppt/canvas/types";+import type { PptTextBlock } from "@/workspaces/ppt/lib/ppt-service";++/**+ * The editable text layer sitting over a slide's rendered image, keyed by+ * slide and then by the version of the image it was extracted from.+ */+export type RenderLayerMap = Record<string, Record<string, SlideRenderLayer>>;++/**+ * Rewrites one layer's text blocks, keeping its rendered elements in step.+ *+ * Every mutation has to do both: `textBlocks` is what the review UI edits and+ * `elements` is what the exporter draws, so updating one without the other+ * silently ships a deck that does not match what the user saw. Routing them+ * all through here is what makes that impossible to forget.+ *+ * Returns the map unchanged when the slide or version has no layer yet.+ */+export function withTextBlocks(+ layers: RenderLayerMap,+ slideId: string,+ versionId: string,+ update: (blocks: PptTextBlock[]) => PptTextBlock[],+): RenderLayerMap {+ const layer = layers[slideId]?.[versionId];+ if (!layer) return layers;++ const textBlocks = update(layer.textBlocks || []);+ return {+ ...layers,+ [slideId]: {+ ...(layers[slideId] || {}),+ [versionId]: {+ ...layer,+ textBlocks,+ elements: mergeTextBlocksIntoElements(textBlocks, layer.elements),+ },+ },+ };+}++/** Smallest a block of each role may be dragged, as a fraction of the slide. */+const MIN_SIZE: Record<string, { w: number; h: number }> = {+ title: { w: 0.18, h: 0.08 },+ tag: { w: 0.07, h: 0.045 },+};+const DEFAULT_MIN = { w: 0.1, h: 0.06 };+const FLOOR = { w: 0.05, h: 0.04 };++/**+ * Applies a drag or resize, keeping the block on the slide and large enough+ * to still be grabbed. A title needs more room than a tag, so the floor+ * depends on the role.+ */+export function clampTextBlockRect(+ block: PptTextBlock,+ nextRect: Partial<Pick<PptTextBlock, "x" | "y" | "w" | "h">>,+): PptTextBlock {+ const role = MIN_SIZE[block.role] || DEFAULT_MIN;+ const minW = Math.max(FLOOR.w, role.w);+ const minH = Math.max(FLOOR.h, role.h);++ let x = typeof nextRect.x === "number" ? nextRect.x : block.x;+ let y = typeof nextRect.y === "number" ? nextRect.y : block.y;+ let w = typeof nextRect.w === "number" ? nextRect.w : block.w;+ let h = typeof nextRect.h === "number" ? nextRect.h : block.h;++ // Size is clamped first, because the position budget depends on it.+ w = Math.max(minW, Math.min(1, w));+ h = Math.max(minH, Math.min(1, h));+ x = Math.max(0, Math.min(1 - w, x));+ y = Math.max(0, Math.min(1 - h, y));++ return { ...block, x, y, w, h };+}diff --git a/tests/ppt-render-layers.test.ts b/tests/ppt-render-layers.test.tsnew file mode 100644index 0000000..1c0b24e--- /dev/null+++ b/tests/ppt-render-layers.test.ts@@ -0,0 +1,129 @@+import assert from "node:assert/strict";+import { test } from "node:test";+import {+ clampTextBlockRect,+ withTextBlocks,+ type RenderLayerMap,+} from "../client/workspaces/ppt/canvas/lib/render-layers";+import type { PptTextBlock } from "../client/workspaces/ppt/lib/ppt-service";++/**+ * The editable text layer over a rendered slide. `textBlocks` is what the+ * review UI edits and `elements` is what the exporter draws, so any mutation+ * that updates one without the other ships a deck that does not match what+ * the user was looking at.+ */++const block = (over: Partial<PptTextBlock> = {}): PptTextBlock =>+ ({+ id: "b1",+ text: "hello",+ role: "bullet",+ x: 0.1,+ y: 0.1,+ w: 0.3,+ h: 0.1,+ ...over,+ }) as PptTextBlock;++const layers = (blocks: PptTextBlock[]): RenderLayerMap => ({+ "slide-1": {+ v1: {+ backgroundImageUrl: "bg.png",+ textBlocks: blocks,+ elements: [],+ status: "ready",+ },+ },+});++test("a rewrite keeps elements in step with the blocks", () => {+ const next = withTextBlocks(layers([block()]), "slide-1", "v1", (blocks) =>+ blocks.map((b) => ({ ...b, text: "changed" })),+ );+ const layer = next["slide-1"].v1;+ assert.equal(layer.textBlocks[0].text, "changed");+ assert.equal(layer.elements.length, 1, "the exporter's copy was rebuilt too");+});++test("adding and removing blocks both flow through", () => {+ const added = withTextBlocks(layers([block()]), "slide-1", "v1", (blocks) => [+ ...blocks,+ block({ id: "b2" }),+ ]);+ assert.equal(added["slide-1"].v1.textBlocks.length, 2);++ const removed = withTextBlocks(added, "slide-1", "v1", (blocks) =>+ blocks.filter((b) => b.id !== "b1"),+ );+ assert.deepEqual(+ removed["slide-1"].v1.textBlocks.map((b) => b.id),+ ["b2"],+ );+});++test("a slide or version with no layer is left alone", () => {+ const before = layers([block()]);+ assert.equal(withTextBlocks(before, "slide-9", "v1", () => []), before);+ assert.equal(withTextBlocks(before, "slide-1", "v9", () => []), before);+});++test("other slides and versions are untouched by a rewrite", () => {+ const before: RenderLayerMap = {+ ...layers([block()]),+ "slide-2": { v1: { backgroundImageUrl: "b.png", textBlocks: [], elements: [], status: "ready" } },+ };+ before["slide-1"].v2 = { backgroundImageUrl: "old.png", textBlocks: [], elements: [], status: "ready" };++ const next = withTextBlocks(before, "slide-1", "v1", (blocks) => blocks);+ assert.equal(next["slide-2"], before["slide-2"], "a sibling slide keeps its identity");+ assert.equal(next["slide-1"].v2, before["slide-1"].v2, "a sibling version too");+});++test("the layer's other fields survive a rewrite", () => {+ const next = withTextBlocks(layers([block()]), "slide-1", "v1", (blocks) => blocks);+ assert.equal(next["slide-1"].v1.backgroundImageUrl, "bg.png");+ assert.equal(next["slide-1"].v1.status, "ready");+});++test("a dragged block cannot leave the slide", () => {+ const moved = clampTextBlockRect(block({ w: 0.3, h: 0.1 }), { x: 0.95, y: 0.99 });+ assert.equal(moved.x, 0.7, "stops where its right edge meets the slide edge");+ assert.equal(moved.y, 0.9);++ const back = clampTextBlockRect(block(), { x: -0.5, y: -0.5 });+ assert.equal(back.x, 0);+ assert.equal(back.y, 0);+});++test("a block cannot be resized past the slide", () => {+ const wide = clampTextBlockRect(block(), { w: 5, h: 5 });+ assert.equal(wide.w, 1);+ assert.equal(wide.h, 1);+ assert.equal(wide.x, 0, "and is pulled back into view");+});++test("a block cannot be shrunk out of reach, per role", () => {+ // Shrink to nothing and the user can never grab it again.+ assert.equal(clampTextBlockRect(block({ role: "title" }), { w: 0, h: 0 }).w, 0.18);+ assert.equal(clampTextBlockRect(block({ role: "title" }), { w: 0, h: 0 }).h, 0.08);+ assert.equal(clampTextBlockRect(block({ role: "tag" }), { w: 0, h: 0 }).w, 0.07);+ assert.equal(clampTextBlockRect(block({ role: "bullet" }), { w: 0, h: 0 }).w, 0.1);+ assert.equal(clampTextBlockRect(block({ role: "summary" }), { w: 0, h: 0 }).w, 0.1, "unknown roles get the default");+});++test("only the named edges move", () => {+ const moved = clampTextBlockRect(block({ x: 0.1, y: 0.2, w: 0.3, h: 0.4 }), { x: 0.5 });+ assert.equal(moved.x, 0.5);+ assert.equal(moved.y, 0.2);+ assert.equal(moved.w, 0.3);+ assert.equal(moved.h, 0.4);+});++test("clamping returns a new block rather than mutating", () => {+ const before = block();+ const after = clampTextBlockRect(before, { x: 0.5 });+ assert.notEqual(after, before);+ assert.equal(before.x, 0.1);+ assert.equal(after.text, "hello", "everything else is carried over");+});