easel-canvas 0.2.1 → 0.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "easel-canvas",
3
- "version": "0.2.1",
3
+ "version": "0.2.2",
4
4
  "description": "Easel: an HTML design canvas for AI agents. Shows screens/*.html as boards and edits the source directly, with Claude Code / Codex via MCP",
5
5
  "license": "MIT",
6
6
  "author": "Hyun Park",
package/server/boards.mjs CHANGED
@@ -174,6 +174,27 @@ export function unlinkOrigins(board, gone) {
174
174
  for (const b of Object.values(board.boards)) if (b.origin && gone.includes(b.origin.of)) delete b.origin;
175
175
  }
176
176
 
177
+ /** Top-level board.json keys the app owns: a save takes them from the app (a missing optional one is removed) */
178
+ const APP_KEYS = ["version", "title", "pages", "boards", "order", "notes", "launch", "shapes", "guides", "groupNames"];
179
+
180
+ /**
181
+ * The board.json a save from the app writes. Keys the app owns come from the app; any other top-level key (a project's or an
182
+ * agent's own data) comes from the file on disk, so a save never drops it, and an app that loaded before it was added does not
183
+ * either. diskText is board.json as it is now (null when missing or not JSON)
184
+ */
185
+ export function savedBoard(rest, diskText) {
186
+ let disk = {};
187
+ try {
188
+ const d = diskText == null ? null : JSON.parse(diskText);
189
+ if (d && typeof d === "object" && !Array.isArray(d)) disk = d;
190
+ } catch {}
191
+ const next = {};
192
+ for (const k of Object.keys(disk)) if (!APP_KEYS.includes(k)) next[k] = disk[k];
193
+ Object.assign(next, { version: 1, title: rest.title ?? "", pages: Array.isArray(rest.pages) ? rest.pages : [], boards: rest.boards, order: Array.isArray(rest.order) ? rest.order : Object.keys(rest.boards), notes: rest.notes ?? {} });
194
+ for (const k of ["launch", "shapes", "guides", "groupNames"]) if (rest[k] !== undefined) next[k] = rest[k];
195
+ return next;
196
+ }
197
+
177
198
  export function boardRoutes() {
178
199
  return [
179
200
  [
@@ -183,9 +204,8 @@ export function boardRoutes() {
183
204
  const body = await readBody(req);
184
205
  if (!body || typeof body.boards !== "object") return sendJson(res, 400, { error: "boards is required" });
185
206
  const { clientId, label, ...rest } = body;
186
- const next = { version: 1, title: rest.title ?? "", pages: Array.isArray(rest.pages) ? rest.pages : [], boards: rest.boards, order: Array.isArray(rest.order) ? rest.order : Object.keys(rest.boards), notes: rest.notes ?? {} };
187
- for (const k of ["launch", "shapes", "guides"]) if (rest[k] !== undefined) next[k] = rest[k];
188
207
  const before = await read(ctx.store.boardPath);
208
+ const next = savedBoard(rest, before);
189
209
  await ctx.store.writeBoard(next);
190
210
  if (label) {
191
211
  ctx.history.push(label, [{ path: ctx.store.boardPath, before, after: formatBoard(next) }]);
package/server/forks.mjs CHANGED
@@ -12,7 +12,7 @@ import { isTrackedRel } from "./copies.mjs";
12
12
  import { rootOf, toPosix, writeAtomic } from "./fsutil.mjs";
13
13
  import { requestScope } from "./history.mjs";
14
14
  import { readBody, safeJoin, sendJson } from "./index.mjs";
15
- import { besideSpot } from "./place.mjs";
15
+ import { forkSpot } from "./place.mjs";
16
16
 
17
17
  const read = (p) => fs.readFile(p, "utf8").catch(() => null);
18
18
  /** A board name a request may use: an .html under screens/, no dot segments */
@@ -49,7 +49,19 @@ export function choiceGroup(board, file) {
49
49
  return alts.length ? { original: file, kind: "fork", chosen: file, alts } : null;
50
50
  }
51
51
 
52
- /** Creates the fork of file for agent by (html: its first content) beside the original, as one history step; returns its file name */
52
+ /** The forks of file already on the canvas, on the original's page, whose files exist: where the next fork joins them */
53
+ export function forkSiblings(board, file, exists = () => true) {
54
+ const first = board.pages?.[0]?.id;
55
+ const pageOf = (it) => it?.page ?? first;
56
+ return Object.entries(board.boards)
57
+ .filter(([f, b]) => b.origin?.kind === "fork" && b.origin.of === file && pageOf(b) === pageOf(board.boards[file]) && exists(f))
58
+ .map(([, b]) => ({ x: b.x, y: b.y, w: b.w, h: b.h, at: b.origin.at }));
59
+ }
60
+
61
+ /**
62
+ * Creates the fork of file for agent by (html: its first content) beside the original, or in one block with its earlier forks, as one
63
+ * history step; returns its file name. Callers run inside store.exclusive, so a second fork made right after sees the first one
64
+ */
53
65
  export async function createFork(ctx, file, { by, whileBy, html }) {
54
66
  const { board } = await ctx.store.syncBoardNow();
55
67
  const { origin, autoPlaced, x, y, title, ...o } = board.boards[file] ?? { x: 0, y: 0, w: 390, h: 844 };
@@ -57,8 +69,9 @@ export async function createFork(ctx, file, { by, whileBy, html }) {
57
69
  const name = await uniqueName(ctx.store.screensDir, `${stem}-${slug(by)}.html`);
58
70
  const p = safeJoin(ctx.store.screensDir, name);
59
71
  await fs.mkdir(path.dirname(p), { recursive: true });
72
+ const sibs = forkSiblings(board, file, (f) => existsSync(path.join(ctx.store.screensDir, f)));
60
73
  await writeAtomic(p, html);
61
- const at = besideSpot(board, { ...o, x, y });
74
+ const at = forkSpot(board, { ...o, x, y }, sibs);
62
75
  await commitBoard(
63
76
  ctx,
64
77
  `Fork ${file} for ${by}`,
package/server/place.mjs CHANGED
@@ -4,8 +4,10 @@
4
4
 
5
5
  export const GAP_X = 80;
6
6
  export const GAP_Y = 120;
7
- /** Clearance kept around boards and notes (board labels and tags sit above a board) */
8
- const CLEAR = 80;
7
+ /** Clearance kept beside boards and notes, and above and below them: the extra room vertically is for the label row above a board
8
+ * (one 24px row fits 120 from 23% zoom up) */
9
+ const CLEAR_X = 80;
10
+ const CLEAR_Y = 120;
9
11
 
10
12
  /** Rough width of one character at a font size: wide scripts (CJK, Hangul) take a full em */
11
13
  const charW = (ch, size) => ((ch.codePointAt(0) ?? 0) >= 0x2e80 ? size : size * 0.6);
@@ -40,7 +42,7 @@ export function obstacles(board, page, ignore = []) {
40
42
  return boxes;
41
43
  }
42
44
 
43
- const hits = (r, o) => r.x < o.x + o.w + CLEAR && r.x + r.w + CLEAR > o.x && r.y < o.y + o.h + CLEAR && r.y + r.h + CLEAR > o.y;
45
+ const hits = (r, o) => r.x < o.x + o.w + CLEAR_X && r.x + r.w + CLEAR_X > o.x && r.y < o.y + o.h + CLEAR_Y && r.y + r.h + CLEAR_Y > o.y;
44
46
 
45
47
  /**
46
48
  * The first spot for rect (x, y, w, h) that is clear of the page's boards and notes, moving right past whatever is in the way
@@ -52,8 +54,8 @@ export function freeSpot(board, rect, { dir = "right", page, ignore = [] } = {})
52
54
  for (let k = 0; k < 1000; k++) {
53
55
  const hit = obs.filter((o) => hits(r, o));
54
56
  if (!hit.length) return r;
55
- if (dir === "down") r.y = Math.round(Math.max(...hit.map((o) => o.y + o.h)) + GAP_Y);
56
- else r.x = Math.round(Math.max(...hit.map((o) => o.x + o.w)) + GAP_X);
57
+ if (dir === "down") r.y = Math.ceil(Math.max(...hit.map((o) => o.y + o.h)) + GAP_Y); // up, so a note's fractional height never leaves it short
58
+ else r.x = Math.ceil(Math.max(...hit.map((o) => o.x + o.w)) + GAP_X);
57
59
  }
58
60
  return r;
59
61
  }
@@ -67,6 +69,32 @@ export function besideSpot(board, o) {
67
69
  return down.y - o.y < right.x - o.x ? down : right;
68
70
  }
69
71
 
72
+ /** Two boxes count as one block when the gap between them is at most half their sizes (forks placed side by side or in rows are).
73
+ * app/src/forkBlocks.ts has a copy for the canvas; test/forkBlocks.test.mjs keeps the two the same */
74
+ export const near = (a, b) => a.x < b.x + b.w + (a.w + b.w) / 2 && b.x < a.x + a.w + (a.w + b.w) / 2 && a.y < b.y + b.h + (a.h + b.h) / 2 && b.y < a.y + a.h + (a.h + b.h) / 2;
75
+
76
+ /**
77
+ * Spot for a new fork of board o, given its existing forks on the same page (sibs: { x, y, w, h, at }). The forks of one board form
78
+ * one block: the first goes beside the original (besideSpot); a later one goes right of the last fork in the block's lowest row when
79
+ * that exact spot is free, otherwise it starts the block's next row, under the block's first column, at the first free spot going down.
80
+ * The block is the group of forks lying near one another that holds the last-made one, so it follows forks the person has moved, and a
81
+ * fork dragged away on its own does not pull the next one after it unless it is the newest.
82
+ */
83
+ export function forkSpot(board, o, sibs = []) {
84
+ if (!sibs.length) return besideSpot(board, o);
85
+ const latest = sibs.reduce((a, b) => (String(b.at ?? "") > String(a.at ?? "") ? b : a));
86
+ const block = [latest];
87
+ for (let i = 0; i < block.length; i++) for (const s of sibs) if (!block.includes(s) && near(s, block[i])) block.push(s);
88
+ const low = block.reduce((a, b) => (b.y > a.y ? b : a));
89
+ const last = block.filter((s) => Math.abs(s.y - low.y) < low.h / 2).reduce((a, b) => (b.x > a.x ? b : a));
90
+ const x = last.x + last.w + GAP_X;
91
+ const right = freeSpot(board, { x, y: last.y, w: o.w, h: o.h }, { page: o.page });
92
+ if (right.x === Math.round(x) && right.y === Math.round(last.y)) return right;
93
+ const left = Math.min(...block.map((s) => s.x));
94
+ const bottom = Math.max(...block.map((s) => s.y + s.h));
95
+ return freeSpot(board, { x: left, y: bottom + GAP_Y, w: o.w, h: o.h }, { dir: "down", page: o.page });
96
+ }
97
+
70
98
  /** Spot for a new screen: right of the last auto-placed board, otherwise a new row below everything; either way clear of boards and notes */
71
99
  export function placeNew(board, size, page) {
72
100
  const all = Object.values(board.boards);