feat(workspace): integrate persisted canvas, element editing and session renewal

This commit is contained in:
2026-11-06 12:50:43 +08:00
parent 45e78f2e43
commit 373582906d
36 changed files with 1028 additions and 193 deletions
+37 -1
View File
@@ -1,7 +1,7 @@
import { test } from "node:test";
import assert from "node:assert/strict";
import { CanvasElement, CardElement, TextCardElement, ImageCardElement, ScriptCardElement, PlainTextElement, elementCapabilities } from "../src/lib/canvas-elements.ts";
import { nodeCenter, nodeSize, resizeFromPointer, rotateVector, rotationFromPointer } from "../src/lib/canvas-geometry.ts";
import { nodeConnector, connectorBezier, nodeCenter, nodeSize, resizeFromPointer, rotateVector, rotationFromPointer } from "../src/lib/canvas-geometry.ts";
import { branchCanvasNode, canvasReducer, decodeCanvas, emptyCanvas } from "../src/lib/canvas.ts";
function close(actual, expected) { assert(Math.abs(actual - expected) < 0.00001, `${actual} != ${expected}`); }
@@ -93,3 +93,39 @@ test("old text-card drafts remain cards and invalid transform values are rejecte
assert.throws(() => decodeCanvas(JSON.stringify({ ...document, nodes: [{ ...old, ...patch }] })));
}
});
test("connection endpoints stay on the exact border centers after resize and rotation", () => {
const node = new TextCardElement({ x: 20, y: 30 }).toNode();
const expected = {
0: { top: [150, 30], right: [280, 130], bottom: [150, 230], left: [20, 130] },
90: { top: [250, 130], right: [150, 260], bottom: [50, 130], left: [150, 0] },
180: { top: [150, 230], right: [20, 130], bottom: [150, 30], left: [280, 130] },
};
for (const [rotation, sides] of Object.entries(expected)) {
node.data.rotation = Number(rotation);
for (const [side, point] of Object.entries(sides)) {
const anchor = nodeConnector(node, side);
close(anchor.point.x, point[0]); close(anchor.point.y, point[1]);
close(Math.hypot(anchor.normal.x, anchor.normal.y), 1);
}
}
node.data.rotation = 0;
const resized = transformed(node, resizeFromPointer(node, "bottom-right", { x: 100, y: 50 }));
assert.deepEqual(nodeConnector(resized, "right").point, { x: 380, y: 155 });
assert.deepEqual(nodeConnector(resized, "bottom").point, { x: 200, y: 280 });
});
test("Bezier paths include both exact card anchors, independent of selected handle sizes", () => {
const source = new TextCardElement({ x: 20, y: 30 }).toNode();
const target = new TextCardElement({ x: 500, y: 180 }).toNode();
for (const angle of [0, 37, 90, 180, 270]) {
source.data.rotation = angle;
target.data.rotation = (angle + 45) % 360;
const from = nodeConnector(source, "right"), to = nodeConnector(target, "left");
const path = connectorBezier(from, to);
assert(path.startsWith(`M ${from.point.x},${from.point.y} C `));
assert(path.endsWith(` ${to.point.x},${to.point.y}`));
source.selected = true; target.selected = true;
assert.equal(connectorBezier(nodeConnector(source, "right"), nodeConnector(target, "left")), path);
}
});