easel-canvas 0.1.0 → 0.2.0
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/README.md +64 -20
- package/bin/mcp.mjs +151 -43
- package/bin/server.mjs +69 -6
- package/dist/assets/index-BZFSkKqU.css +1 -0
- package/dist/assets/index-BwyrrnkP.js +65 -0
- package/dist/index.html +3 -3
- package/dist/viewer/viewer.css +1 -0
- package/dist/viewer/viewer.js +49 -0
- package/package.json +2 -2
- package/server/activity.mjs +370 -0
- package/server/agents.mjs +20 -0
- package/server/args.mjs +22 -2
- package/server/board.mjs +33 -33
- package/server/boards.mjs +51 -25
- package/server/browser.mjs +68 -0
- package/server/chat.mjs +446 -51
- package/server/connect.mjs +94 -0
- package/server/copies.mjs +105 -0
- package/server/custom.mjs +101 -0
- package/server/edit.mjs +2 -2
- package/server/export.mjs +627 -0
- package/server/forks.mjs +214 -0
- package/server/fsutil.mjs +25 -0
- package/server/history.mjs +152 -24
- package/server/html.mjs +11 -1
- package/server/index.mjs +162 -17
- package/server/occupancy.mjs +183 -0
- package/server/permissions.mjs +141 -0
- package/server/place.mjs +87 -0
- package/server/registry.mjs +110 -0
- package/server/routes.mjs +37 -38
- package/server/runs.mjs +167 -0
- package/server/snapshot.mjs +21 -9
- package/server/tokens.mjs +15 -3
- package/server/update.mjs +63 -0
- package/server/watch.mjs +12 -1
- package/dist/assets/index-DbJY7lxA.css +0 -1
- package/dist/assets/index-O7RiYHEC.js +0 -55
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "easel-canvas",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
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",
|
|
@@ -20,7 +20,7 @@
|
|
|
20
20
|
"scripts": {
|
|
21
21
|
"start": "node bin/server.mjs",
|
|
22
22
|
"demo": "node bin/server.mjs --dir examples/demo --open",
|
|
23
|
-
"build": "vite build app --config vite.config.mjs",
|
|
23
|
+
"build": "vite build app --config vite.config.mjs && vite build app --config vite.config.mjs --mode viewer",
|
|
24
24
|
"prepack": "npm run build",
|
|
25
25
|
"mcp": "node bin/mcp.mjs",
|
|
26
26
|
"typecheck": "tsc --noEmit",
|
|
@@ -0,0 +1,370 @@
|
|
|
1
|
+
// AI-generated with Claude Code, 2026-11-12, reviewed by Hyun Park
|
|
2
|
+
// Who changed what: the elements a change touched (pure, on the two HTML texts with parse5; board scripts never run), a short name for
|
|
3
|
+
// every history step, board tags (one per author per board, only someone else's changes), and the routes behind the tags and History.
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
|
|
6
|
+
import { isTrackedRel } from "./copies.mjs";
|
|
7
|
+
import { toPosix } from "./fsutil.mjs";
|
|
8
|
+
import { fileList } from "./history.mjs";
|
|
9
|
+
import { elementChildren, findBody, parseHtml, textOf } from "./html.mjs";
|
|
10
|
+
import { readBody, safeJoin, sendJson } from "./index.mjs";
|
|
11
|
+
import { dropMissingAutoPlaced, mergeSteps, undoChanges, withBase } from "./runs.mjs";
|
|
12
|
+
|
|
13
|
+
/** More changed elements than this: no outline, only the tag */
|
|
14
|
+
export const MAX_OUTLINE = 5;
|
|
15
|
+
/** The diff stops collecting past this many items (a rewritten screen) */
|
|
16
|
+
const CAP = 60;
|
|
17
|
+
/** Elements that are never drawn: a change to them is not outlined */
|
|
18
|
+
const HIDDEN = new Set(["script", "style", "template", "meta", "link", "noscript", "title", "base"]);
|
|
19
|
+
|
|
20
|
+
const norm = (s) => String(s ?? "").replace(/\s+/g, " ").trim();
|
|
21
|
+
const clip = (s, n = 80) => (s.length > n ? s.slice(0, n - 1) + "…" : s);
|
|
22
|
+
const srcOf = (html, n) => {
|
|
23
|
+
const l = n?.sourceCodeLocation;
|
|
24
|
+
return l ? html.slice(l.startOffset, l.endOffset) : null;
|
|
25
|
+
};
|
|
26
|
+
const ownText = (n) => norm((n.childNodes ?? []).filter((c) => c.nodeName === "#text").map((c) => c.value).join(" "));
|
|
27
|
+
const attrsOf = (n) => new Map((n.attrs ?? []).map((a) => [a.name, a.value]));
|
|
28
|
+
const clsOf = (n) => n.attrs?.find((a) => a.name === "class")?.value.trim().split(/\s+/)[0] || undefined;
|
|
29
|
+
/** <head> as text (an implied <head> has no source range of its own, its children do) */
|
|
30
|
+
const headText = (doc, html) => {
|
|
31
|
+
const head = doc.childNodes.find((n) => n.tagName === "html")?.childNodes.find((n) => n.tagName === "head");
|
|
32
|
+
return (head?.childNodes ?? []).map((n) => srcOf(html, n) ?? n.value ?? "").join("");
|
|
33
|
+
};
|
|
34
|
+
const parseJson = (t) => {
|
|
35
|
+
try {
|
|
36
|
+
return t ? JSON.parse(t) : {};
|
|
37
|
+
} catch {
|
|
38
|
+
return null;
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Elements that differ between two versions of a screen, by the path /api/outline, /api/element and comments use (element indexes
|
|
44
|
+
* from the children of <body>, in the after version). Children are matched by equal source at both ends; the rest pairs up in order
|
|
45
|
+
* and the surplus is added (paths in after) or removed (the parent's path). items: { path, kind: text|style|attr|added|removed|replaced,
|
|
46
|
+
* tag, cls?, from?, to? }; head: the <head> changed (styles, title). full: more than CAP items, the list is cut
|
|
47
|
+
*/
|
|
48
|
+
export function changedElements(before, after) {
|
|
49
|
+
const a = parseHtml(before);
|
|
50
|
+
const b = parseHtml(after);
|
|
51
|
+
const items = [];
|
|
52
|
+
const same = (x, y) => {
|
|
53
|
+
const sx = srcOf(before, x);
|
|
54
|
+
return sx != null && sx === srcOf(after, y);
|
|
55
|
+
};
|
|
56
|
+
const walk = (x, y, p) => {
|
|
57
|
+
if (items.length >= CAP || same(x, y)) return;
|
|
58
|
+
const at = (i) => (p ? `${p}/${i}` : String(i));
|
|
59
|
+
if (x.tagName !== y.tagName) return void items.push({ path: p, kind: "replaced", tag: y.tagName, cls: clsOf(y) });
|
|
60
|
+
const ax = attrsOf(x);
|
|
61
|
+
const ay = attrsOf(y);
|
|
62
|
+
const names = [...new Set([...ax.keys(), ...ay.keys()])].filter((k) => ax.get(k) !== ay.get(k));
|
|
63
|
+
if (names.length) items.push({ path: p, kind: names.every((k) => k === "style") ? "style" : "attr", tag: y.tagName, cls: clsOf(y) });
|
|
64
|
+
if (HIDDEN.has(y.tagName)) {
|
|
65
|
+
// A <style> or <script> inside body: its text is code, not copy
|
|
66
|
+
if (!names.length && (srcOf(before, x) ?? "") !== (srcOf(after, y) ?? "")) items.push({ path: p, kind: y.tagName === "style" ? "style" : "attr", tag: y.tagName });
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
if (ownText(x) !== ownText(y)) items.push({ path: p, kind: "text", tag: y.tagName, cls: clsOf(y), from: clip(norm(textOf(x))), to: clip(norm(textOf(y))) });
|
|
70
|
+
const cx = elementChildren(x);
|
|
71
|
+
const cy = elementChildren(y);
|
|
72
|
+
let s = 0;
|
|
73
|
+
while (s < cx.length && s < cy.length && same(cx[s], cy[s])) s++;
|
|
74
|
+
let e = 0;
|
|
75
|
+
while (e < cx.length - s && e < cy.length - s && same(cx[cx.length - 1 - e], cy[cy.length - 1 - e])) e++;
|
|
76
|
+
const mx = cx.slice(s, cx.length - e);
|
|
77
|
+
const my = cy.slice(s, cy.length - e);
|
|
78
|
+
const k = Math.min(mx.length, my.length);
|
|
79
|
+
for (let i = 0; i < k; i++) walk(mx[i], my[i], at(s + i));
|
|
80
|
+
for (let i = k; i < my.length && items.length < CAP; i++) items.push({ path: at(s + i), kind: "added", tag: my[i].tagName, cls: clsOf(my[i]) });
|
|
81
|
+
if (mx.length > k) items.push({ path: p, kind: "removed", tag: mx[k].tagName, cls: clsOf(mx[k]), count: mx.length - k });
|
|
82
|
+
};
|
|
83
|
+
const bx = findBody(a);
|
|
84
|
+
const by = findBody(b);
|
|
85
|
+
if (bx && by) walk(bx, by, "");
|
|
86
|
+
return { items, head: headText(a, before) !== headText(b, after), full: items.length >= CAP };
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* One change to one screen in a few fields for the tag popover: kind (text, style, attr, added, removed, replaced, many, page: only
|
|
91
|
+
* <head>, source: only whitespace or comments, new: the board was created, deleted), count of changed elements, the element (tag, cls),
|
|
92
|
+
* from/to for a text change, and paths to outline — none when it cannot be pinned to at most MAX_OUTLINE elements
|
|
93
|
+
*/
|
|
94
|
+
export function describeChange(before, after) {
|
|
95
|
+
if (before == null) return { kind: "new", count: 1, paths: [] };
|
|
96
|
+
if (after == null) return { kind: "deleted", count: 1, paths: [] };
|
|
97
|
+
const { items, head, full } = changedElements(before, after);
|
|
98
|
+
if (!items.length) return { kind: head ? "page" : "source", count: 0, paths: [] };
|
|
99
|
+
const els = [...new Set(items.map((i) => i.path))];
|
|
100
|
+
const shown = els.filter((p) => p !== "" && !items.some((i) => i.path === p && HIDDEN.has(i.tag)));
|
|
101
|
+
const paths = !full && shown.length <= MAX_OUTLINE ? shown : [];
|
|
102
|
+
if (full || els.length > 1) return { kind: "many", count: els.length, ...(full ? { more: true } : {}), paths };
|
|
103
|
+
const first = items[0];
|
|
104
|
+
if (items.length === 1 && first.kind === "text") return { kind: "text", count: 1, tag: first.tag, cls: first.cls, from: first.from, to: first.to, paths };
|
|
105
|
+
// One element, several kinds of change (its text and its style): "edited"
|
|
106
|
+
const kind = items.every((i) => i.kind === "style") ? "style" : items.length === 1 ? first.kind : "edited";
|
|
107
|
+
return { kind, count: first.count ?? 1, tag: first.tag, cls: first.cls, paths };
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/** board.json boards entries that a change added, removed, moved, resized, renamed or otherwise changed (board files) */
|
|
111
|
+
export function boardEntries(beforeText, afterText) {
|
|
112
|
+
const a = parseJson(beforeText)?.boards ?? {};
|
|
113
|
+
const b = parseJson(afterText)?.boards ?? {};
|
|
114
|
+
const out = { added: [], removed: [], moved: [], resized: [], renamed: [], changed: [] };
|
|
115
|
+
for (const f of new Set([...Object.keys(a), ...Object.keys(b)])) {
|
|
116
|
+
const x = a[f];
|
|
117
|
+
const y = b[f];
|
|
118
|
+
if (!x && y) out.added.push(f);
|
|
119
|
+
else if (x && !y) out.removed.push(f);
|
|
120
|
+
else if (JSON.stringify(x) !== JSON.stringify(y)) {
|
|
121
|
+
if (x.w !== y.w || x.h !== y.h) out.resized.push(f);
|
|
122
|
+
else if (x.x !== y.x || x.y !== y.y) out.moved.push(f);
|
|
123
|
+
else if (x.title !== y.title) out.renamed.push(f);
|
|
124
|
+
else out.changed.push(f);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
return out;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/* ---------- Step names ---------- */
|
|
131
|
+
const EDIT_OPS = { setText: "Change text on", setStyle: "Change style on", setAttr: "Change attribute on", delete: "Delete element on", duplicate: "Duplicate element on", move: "Move element on", insert: "Add element on", setTweaks: "Change tweaks on", batch: "Edit", wrap: "Group elements on" };
|
|
132
|
+
/** App labels for board.json steps that are about some boards: named after them */
|
|
133
|
+
const BOARD_LABELS = new Set(["Move board", "Resize board", "Board options", "Bring to front", "Send to back", "Move to page", "Set launch screen"]);
|
|
134
|
+
/** What the text of an element is called in a step name */
|
|
135
|
+
const copyNoun = (tag) => (tag === "button" ? "button copy" : tag === "a" ? "link text" : /^h[1-6]$/.test(tag) ? "heading" : tag === "label" ? "label" : "text");
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* A short human name for a history step: "Change button copy on Welcome", "Move board Inbox", "Add board Pricing", "Add note".
|
|
139
|
+
* Steps the app labels by file and operation, board operations and changes made outside the app (or by a run) are named after the
|
|
140
|
+
* boards; the text wording only when the diff really is one text node. env: { screensDir, boardPath, titles: { file: title } }
|
|
141
|
+
*/
|
|
142
|
+
export function stepName(step, env, describe = describeChange) {
|
|
143
|
+
const label = step.label ?? "";
|
|
144
|
+
// A board this step removed (a version "Use this" sent to trash) is named as board.json had it before the step
|
|
145
|
+
const was = () => parseJson(step.changes.find((c) => c.path === env.boardPath)?.before)?.boards ?? {};
|
|
146
|
+
const title = (f) => env.titles?.[f] || was()[f]?.title || f.replace(/\.html$/, "");
|
|
147
|
+
const rel = (p) => toPosix(path.relative(env.screensDir, p));
|
|
148
|
+
const html = step.changes.filter((c) => c.path.endsWith(".html") && !rel(c.path).startsWith(".."));
|
|
149
|
+
const textOf1 = (c) => {
|
|
150
|
+
const d = c && c.before != null && c.after != null ? describe(c.before, c.after) : null;
|
|
151
|
+
return d?.kind === "text" ? d : null;
|
|
152
|
+
};
|
|
153
|
+
const boardsIn = () => {
|
|
154
|
+
const bj = step.changes.find((c) => c.path === env.boardPath);
|
|
155
|
+
const d = bj ? boardEntries(bj.before, bj.after) : null;
|
|
156
|
+
return d ? [...new Set([...d.moved, ...d.resized, ...d.renamed, ...d.changed])] : [];
|
|
157
|
+
};
|
|
158
|
+
const several = (verb, n) => `${verb} ${n} boards`;
|
|
159
|
+
let m = /^(.+\.html) (\w+)$/.exec(label);
|
|
160
|
+
if (m && EDIT_OPS[m[2]]) {
|
|
161
|
+
const t = html.length === 1 ? textOf1(html[0]) : null;
|
|
162
|
+
return t ? `Change ${copyNoun(t.tag)} on ${title(m[1])}` : `${EDIT_OPS[m[2]]} ${title(m[1])}`;
|
|
163
|
+
}
|
|
164
|
+
if ((m = /^Create board (.+)$/.exec(label))) return `Add board ${title(m[1])}`;
|
|
165
|
+
// One writer per board, and picking a version (server/forks.mjs)
|
|
166
|
+
if ((m = /^Fork (.+\.html) for (.+)$/.exec(label))) return `Fork ${title(m[1])} for ${m[2]}`;
|
|
167
|
+
if ((m = /^Use (.+\.html) for (.+\.html)$/.exec(label))) return `Use ${title(m[1]).replace(/:.*$/, "")} for ${title(m[2])}`;
|
|
168
|
+
if ((m = /^Keep (.+\.html), trash (.+)$/.exec(label))) return `Keep ${title(m[1])}, trash ${fileList(m[2].split(", ").map(title))}`;
|
|
169
|
+
if ((m = /^Keep all (variants|versions) of (.+\.html)$/.exec(label))) return `Keep all ${m[1]} of ${title(m[2])}`;
|
|
170
|
+
if ((m = /^Write board (.+\.html)$/.exec(label))) {
|
|
171
|
+
if (html.length === 1 && html[0].before == null) return `Add board ${title(m[1])}`;
|
|
172
|
+
const t = html.length === 1 ? textOf1(html[0]) : null;
|
|
173
|
+
return t ? `Change ${copyNoun(t.tag)} on ${title(m[1])}` : `Change ${title(m[1])}`;
|
|
174
|
+
}
|
|
175
|
+
if ((m = /^Duplicate board (.+)$/.exec(label))) return `Duplicate board ${title(m[1])}`;
|
|
176
|
+
if ((m = /^Delete board (.+)$/.exec(label))) {
|
|
177
|
+
const files = m[1].split(", ");
|
|
178
|
+
return files.length === 1 ? `Delete board ${title(files[0])}` : several("Delete", files.length);
|
|
179
|
+
}
|
|
180
|
+
if ((m = /^Rename (?!file )(.+\.html)$/.exec(label))) return `Rename board ${title(m[1])}`;
|
|
181
|
+
if (BOARD_LABELS.has(label)) {
|
|
182
|
+
const files = boardsIn();
|
|
183
|
+
if (files.length === 1) return `${label} ${title(files[0])}`;
|
|
184
|
+
if (files.length > 1) return /^(Move|Resize) board$/.test(label) ? several(label.split(" ")[0], files.length) : `${label} · ${files.length} boards`;
|
|
185
|
+
return label;
|
|
186
|
+
}
|
|
187
|
+
if (!step.outside) return label;
|
|
188
|
+
// Changes found on disk (an agent's own file tools, an editor, an in-app run): named after the boards
|
|
189
|
+
const added = html.filter((c) => c.before == null);
|
|
190
|
+
const removed = html.filter((c) => c.after == null);
|
|
191
|
+
const changed = html.filter((c) => c.before != null && c.after != null);
|
|
192
|
+
const fileOf = (c) => rel(c.path);
|
|
193
|
+
if (!html.length) {
|
|
194
|
+
const files = boardsIn();
|
|
195
|
+
return files.length === 1 ? `Change board ${title(files[0])}` : files.length ? several("Change", files.length) : "Change board.json";
|
|
196
|
+
}
|
|
197
|
+
if (added.length === html.length) return added.length === 1 ? `Add board ${title(fileOf(added[0]))}` : several("Add", added.length);
|
|
198
|
+
if (removed.length === html.length) return removed.length === 1 ? `Delete board ${title(fileOf(removed[0]))}` : several("Delete", removed.length);
|
|
199
|
+
if (changed.length === 1 && html.length === 1) {
|
|
200
|
+
const t = textOf1(changed[0]);
|
|
201
|
+
return t ? `Change ${copyNoun(t.tag)} on ${title(fileOf(changed[0]))}` : `Change ${title(fileOf(changed[0]))}`;
|
|
202
|
+
}
|
|
203
|
+
return `Change ${fileList(html.map((c) => title(fileOf(c))))}`;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/** Boards a step touched, for its tags: screens it changed, added or removed (with the change described) and boards it moved or resized */
|
|
207
|
+
export function stepBoards(step, env, describe = describeChange) {
|
|
208
|
+
const out = new Map();
|
|
209
|
+
for (const c of step.changes) {
|
|
210
|
+
if (c.path === env.boardPath) continue;
|
|
211
|
+
const rel = toPosix(path.relative(env.screensDir, c.path));
|
|
212
|
+
if (!c.path.endsWith(".html") || rel.startsWith("..")) continue;
|
|
213
|
+
out.set(rel, { file: rel, kind: c.before == null ? "added" : c.after == null ? "removed" : "changed", change: describe(c.before, c.after), path: c.path, after: c.after });
|
|
214
|
+
}
|
|
215
|
+
const bj = step.changes.find((c) => c.path === env.boardPath);
|
|
216
|
+
if (bj) {
|
|
217
|
+
const d = boardEntries(bj.before, bj.after);
|
|
218
|
+
for (const kind of ["moved", "resized", "renamed", "changed"])
|
|
219
|
+
for (const f of d[kind]) if (!out.has(f)) out.set(f, { file: f, kind: "board", change: { kind: kind === "changed" ? "options" : kind, count: 1, paths: [] } });
|
|
220
|
+
}
|
|
221
|
+
return [...out.values()];
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/**
|
|
225
|
+
* Board tags from the applied steps (oldest first): per board, one tag per author, newest first. Leaves out steps made in the browser
|
|
226
|
+
* asking (client) and steps an applied undo took back (undid, or undoOf for a chat run). Each tag: who (by, person, outside), its step ids (newest first),
|
|
227
|
+
* count, at (newest), newBoard (its oldest step created the board), the newest change and its paths when that change is still on disk (fresh)
|
|
228
|
+
*/
|
|
229
|
+
export function boardTags(steps, client) {
|
|
230
|
+
// Taken back: steps a tag Undo undid, and chat runs undone by Undo this run (until a later Redo of that run)
|
|
231
|
+
const taken = new Set();
|
|
232
|
+
for (const s of steps) {
|
|
233
|
+
for (const id of s.undid ?? []) taken.add(id);
|
|
234
|
+
if (s.undoOf) taken.add(`run:${s.undoOf}`);
|
|
235
|
+
if (s.redoOf) taken.delete(`run:${s.redoOf}`);
|
|
236
|
+
}
|
|
237
|
+
const tags = {};
|
|
238
|
+
for (const s of steps) {
|
|
239
|
+
if (taken.has(s.id) || (s.run && taken.has(`run:${s.run}`)) || (client && s.client === client)) continue;
|
|
240
|
+
const key = s.by ? `a:${s.by}` : s.person ? `p:${s.person}` : s.outside ? "outside" : "app";
|
|
241
|
+
for (const b of s.boards) {
|
|
242
|
+
const list = (tags[b.file] ??= []);
|
|
243
|
+
let t = list.find((x) => x.key === key);
|
|
244
|
+
if (!t) {
|
|
245
|
+
t = { key, ...(s.by ? { by: s.by } : s.person ? { person: s.person } : {}), outside: key === "outside", steps: [], count: 0, at: 0, newBoard: b.kind === "added", change: null, paths: [], runs: new Set() };
|
|
246
|
+
list.push(t);
|
|
247
|
+
}
|
|
248
|
+
t.steps.unshift(s.id);
|
|
249
|
+
t.count++;
|
|
250
|
+
t.at = Math.max(t.at, s.lastAt ?? s.at);
|
|
251
|
+
t.change = b.change;
|
|
252
|
+
t.paths = b.fresh ? (b.change?.paths ?? []) : [];
|
|
253
|
+
t.runs.add(s.run ?? null);
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
for (const f of Object.keys(tags)) {
|
|
257
|
+
// A board that is gone again has nothing to hang a tag on
|
|
258
|
+
if (tags[f].every((t) => t.change?.kind === "deleted")) delete tags[f];
|
|
259
|
+
else tags[f] = tags[f].sort((a, b) => b.at - a.at).map(({ runs, ...t }) => ({ ...t, run: runs.size === 1 ? [...runs][0] : null }));
|
|
260
|
+
}
|
|
261
|
+
return tags;
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
/* ---------- What the app reads ---------- */
|
|
265
|
+
const cache = new WeakMap(); // step → { rev, boards }
|
|
266
|
+
|
|
267
|
+
/** Per step: its name, author and boards (cached while the step is unchanged), as the tags and the History menu need them */
|
|
268
|
+
function stepInfo(ctx, step, env) {
|
|
269
|
+
let c = cache.get(step);
|
|
270
|
+
if (!c || c.rev !== (step.rev ?? 0)) cache.set(step, (c = { rev: step.rev ?? 0, boards: stepBoards(step, env) }));
|
|
271
|
+
const describe = (before, after) => c.boards.find((b) => b.after === after)?.change ?? describeChange(before, after);
|
|
272
|
+
return {
|
|
273
|
+
id: step.id,
|
|
274
|
+
name: stepName(step, env, describe),
|
|
275
|
+
label: step.label,
|
|
276
|
+
...(step.by ? { by: step.by } : step.person ? { person: step.person } : {}),
|
|
277
|
+
...(step.client ? { client: step.client } : {}),
|
|
278
|
+
outside: !!step.outside,
|
|
279
|
+
...(step.run ? { run: step.run } : {}),
|
|
280
|
+
...(step.undid ? { undid: step.undid } : {}),
|
|
281
|
+
...(step.undoOf ? { undoOf: step.undoOf } : {}),
|
|
282
|
+
...(step.redoOf ? { redoOf: step.redoOf } : {}),
|
|
283
|
+
at: step.lastAt ?? step.at,
|
|
284
|
+
boards: c.boards.map(({ path: p, after, ...b }) => ({ ...b, fresh: !!p && ctx.history.current(p) === after })),
|
|
285
|
+
};
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
/** What step names need: where the screens and board.json are, and the board titles now */
|
|
289
|
+
async function envOf(ctx) {
|
|
290
|
+
const board = await ctx.store.readBoard();
|
|
291
|
+
return { screensDir: ctx.store.screensDir, boardPath: ctx.store.boardPath, titles: Object.fromEntries(Object.entries(board.boards).map(([f, b]) => [f, b.title])) };
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
/** Steps for the History menu (newest first, with Now) and board tags for the browser asking (client) */
|
|
295
|
+
export async function activity(ctx, client) {
|
|
296
|
+
const env = await envOf(ctx);
|
|
297
|
+
const { undo, redo } = ctx.history.entries();
|
|
298
|
+
const applied = undo.map((s) => stepInfo(ctx, s, env));
|
|
299
|
+
const undone = redo.map((s) => stepInfo(ctx, s, env));
|
|
300
|
+
const row = ({ boards, client: c, ...s }) => ({ ...s, mine: !!client && c === client });
|
|
301
|
+
return {
|
|
302
|
+
boot: ctx.boot,
|
|
303
|
+
// History menu, newest first: undone steps (the next redo last), then the applied ones; "now" is the newest applied step
|
|
304
|
+
steps: [...undone.map((s) => ({ ...row(s), undone: true })), ...[...applied].reverse().map((s, i) => ({ ...row(s), ...(i === 0 ? { now: true } : {}) }))],
|
|
305
|
+
tags: boardTags(applied, client),
|
|
306
|
+
};
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
export function activityRoutes() {
|
|
310
|
+
return [
|
|
311
|
+
["GET", "/api/activity", async (req, res, url, ctx) => sendJson(res, 200, await activity(ctx, url.searchParams.get("client") ?? ""))],
|
|
312
|
+
// History › Go back to here: a walk along the undo stack, so Redo brings everything back
|
|
313
|
+
[
|
|
314
|
+
"POST",
|
|
315
|
+
"/api/history/goto",
|
|
316
|
+
async (req, res, url, ctx) => {
|
|
317
|
+
const { id } = await readBody(req);
|
|
318
|
+
try {
|
|
319
|
+
const moved = await ctx.history.goTo(String(id ?? ""));
|
|
320
|
+
await dropMissingAutoPlaced(ctx);
|
|
321
|
+
await ctx.store.syncBoardNow();
|
|
322
|
+
sendJson(res, 200, { ok: true, moved });
|
|
323
|
+
} finally {
|
|
324
|
+
ctx.broadcast({ type: "board-changed", source: "server:goto" });
|
|
325
|
+
ctx.broadcast({ type: "history-changed" });
|
|
326
|
+
}
|
|
327
|
+
},
|
|
328
|
+
],
|
|
329
|
+
// Undo from a board tag: the tag's steps, a plain undo when they are the newest, else only their files (409 with the boards changed
|
|
330
|
+
// since unless mode is "rest" or "all") — the rule of Undo this run, through the same helper
|
|
331
|
+
[
|
|
332
|
+
"POST",
|
|
333
|
+
"/api/history/undo-steps",
|
|
334
|
+
async (req, res, url, ctx) => {
|
|
335
|
+
const b = await readBody(req);
|
|
336
|
+
const ids = [...new Set((Array.isArray(b.ids) ? b.ids : []).map(String))].slice(0, 100);
|
|
337
|
+
await ctx.history.observe();
|
|
338
|
+
const { undo } = ctx.history.entries();
|
|
339
|
+
const steps = undo.filter((s) => ids.includes(s.id));
|
|
340
|
+
if (!steps.length || steps.length !== ids.length) return sendJson(res, 409, { error: "These changes are no longer in the undo history" });
|
|
341
|
+
const top = undo.slice(-steps.length);
|
|
342
|
+
const onTop = top.every((s) => steps.includes(s)) ? steps.length : 0;
|
|
343
|
+
const label = steps.length === 1 ? `Undo ${stepName(steps[0], await envOf(ctx))}` : `Undo ${steps.length} changes`;
|
|
344
|
+
const r = await undoChanges(ctx, { onTop, changes: mergeSteps(steps), mode: b.mode, label, mark: { undid: steps.map((s) => s.id) } });
|
|
345
|
+
if (r.conflicts) return sendJson(res, 409, { conflicts: r.conflicts, error: "Some boards changed since" });
|
|
346
|
+
await ctx.store.syncBoardNow();
|
|
347
|
+
ctx.broadcast({ type: "board-changed", source: "server:undo-steps" });
|
|
348
|
+
ctx.broadcast({ type: "history-changed" });
|
|
349
|
+
sendJson(res, 200, { ok: true, changed: r.changed });
|
|
350
|
+
},
|
|
351
|
+
],
|
|
352
|
+
// Read-only: a screen as it was before a history step (Compare's "before" for a tag), from the history in memory
|
|
353
|
+
[
|
|
354
|
+
"GET",
|
|
355
|
+
"/api/history/step-file",
|
|
356
|
+
async (req, res, url, ctx) => {
|
|
357
|
+
const f = url.searchParams.get("f") ?? "";
|
|
358
|
+
const id = url.searchParams.get("id") ?? "";
|
|
359
|
+
const p = isTrackedRel(`screens/${f}`) ? safeJoin(ctx.store.screensDir, f) : null;
|
|
360
|
+
const { undo, redo } = ctx.history.entries();
|
|
361
|
+
const step = p && [...undo, ...redo].find((s) => s.id === id);
|
|
362
|
+
const c = step?.changes.find((x) => x.path === p);
|
|
363
|
+
if (!c || c.before == null) return sendJson(res, 404, { error: "No earlier version of that screen in this step" });
|
|
364
|
+
const out = withBase(c.before, f);
|
|
365
|
+
res.writeHead(200, { "Content-Type": "text/html; charset=utf-8", "Content-Length": Buffer.byteLength(out), "Cache-Control": "no-store" });
|
|
366
|
+
res.end(out);
|
|
367
|
+
},
|
|
368
|
+
],
|
|
369
|
+
];
|
|
370
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
// AI-generated with Claude Code, 2026-11-10, reviewed by Hyun Park
|
|
2
|
+
// Who is calling: MCP client names → display labels used as comment authors and undo-step authors (X-Easel-Agent).
|
|
3
|
+
|
|
4
|
+
/** MCP client name (claude-code, codex-mcp-client, cursor-vscode, gemini-cli-mcp-client, …) → label; unknown names pass through, none is "AI" */
|
|
5
|
+
export function agentLabel(clientName) {
|
|
6
|
+
const n = String(clientName ?? "").trim();
|
|
7
|
+
const l = n.toLowerCase();
|
|
8
|
+
if (l.includes("claude")) return "Claude Code";
|
|
9
|
+
if (l.includes("codex")) return "Codex";
|
|
10
|
+
if (l.includes("cursor")) return "Cursor";
|
|
11
|
+
if (l.includes("gemini")) return "Gemini CLI";
|
|
12
|
+
return cleanAgent(n) || "AI";
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/** Header value safe to store and show: printable, single line, at most 60 chars */
|
|
16
|
+
export const cleanAgent = (v) =>
|
|
17
|
+
String(v ?? "")
|
|
18
|
+
.replace(/[\u0000-\u001f\u007f]+/g, " ")
|
|
19
|
+
.trim()
|
|
20
|
+
.slice(0, 60);
|
package/server/args.mjs
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
// AI-generated with Claude Code, 2026-10-09, reviewed by Hyun Park
|
|
2
|
-
// Command-line args: [--dir <design folder>] [--port 4317] [--open] [--demo] [--init] [--help] [--version]
|
|
2
|
+
// Command-line args: [connect] [--dir <design folder>] [--port 4317] [--open] [--demo] [--init] [--help] [--version],
|
|
3
|
+
// plus --agents a,b and --dry-run for connect, --out, --page, --boards a,b, --single-file, --pdf, --png for export,
|
|
4
|
+
// and --permission for easel-mcp (only the permission tool of an in-app chat run).
|
|
3
5
|
// Default design folder: <repo>/docs/design when Easel lives in <repo>/tools/<name> (submodule); otherwise
|
|
4
6
|
// ./docs/design or the current folder if it has screens/ (npx).
|
|
5
7
|
import { existsSync } from "node:fs";
|
|
@@ -23,7 +25,7 @@ export const DEFAULT_DIR = defaultDir();
|
|
|
23
25
|
const expand = (p) => path.resolve(p.replace(/^~(?=\/)/, process.env.HOME ?? "~"));
|
|
24
26
|
|
|
25
27
|
export function parseArgs(argv = process.argv.slice(2)) {
|
|
26
|
-
const out = { dir: null, port: DEFAULT_PORT, open: false, noOpen: false, demo: false, init: false, help: false, version: false, prod: false };
|
|
28
|
+
const out = { command: null, dir: null, port: DEFAULT_PORT, portGiven: false, dirGiven: false, agents: null, dryRun: false, out: null, page: null, boards: null, singleFile: false, pdf: false, png: false, permission: false, open: false, noOpen: false, demo: false, init: false, help: false, version: false, prod: false };
|
|
27
29
|
for (let i = 0; i < argv.length; i++) {
|
|
28
30
|
const a = argv[i];
|
|
29
31
|
const next = () => argv[++i];
|
|
@@ -31,14 +33,32 @@ export function parseArgs(argv = process.argv.slice(2)) {
|
|
|
31
33
|
else if (a.startsWith("--dir=")) out.dir = expand(a.slice(6));
|
|
32
34
|
else if (a === "--port") out.port = Number(next());
|
|
33
35
|
else if (a.startsWith("--port=")) out.port = Number(a.slice(7));
|
|
36
|
+
else if (a === "--agents") out.agents = next();
|
|
37
|
+
else if (a.startsWith("--agents=")) out.agents = a.slice(9);
|
|
38
|
+
else if (a === "--dry-run") out.dryRun = true;
|
|
39
|
+
else if (a === "--out") out.out = argv[i + 1] ? expand(next()) : null;
|
|
40
|
+
else if (a.startsWith("--out=")) out.out = expand(a.slice(6));
|
|
41
|
+
else if (a === "--page") out.page = next();
|
|
42
|
+
else if (a.startsWith("--page=")) out.page = a.slice(7);
|
|
43
|
+
else if (a === "--boards") out.boards = next();
|
|
44
|
+
else if (a.startsWith("--boards=")) out.boards = a.slice(9);
|
|
45
|
+
else if (a === "--single-file") out.singleFile = true;
|
|
46
|
+
else if (a === "--pdf") out.pdf = true;
|
|
47
|
+
else if (a === "--png") out.png = true;
|
|
48
|
+
else if (i === 0 && (a === "connect" || a === "export")) out.command = a;
|
|
34
49
|
else if (a === "--open") out.open = true;
|
|
35
50
|
else if (a === "--no-open") out.noOpen = true;
|
|
36
51
|
else if (a === "--demo") out.demo = true;
|
|
37
52
|
else if (a === "--init") out.init = true;
|
|
38
53
|
else if (a === "--prod") out.prod = true;
|
|
54
|
+
else if (a === "--permission") out.permission = true;
|
|
39
55
|
else if (a === "-h" || a === "--help") out.help = true;
|
|
40
56
|
else if (a === "-v" || a === "--version") out.version = true;
|
|
41
57
|
}
|
|
58
|
+
out.portGiven = argv.some((a) => a === "--port" || a.startsWith("--port="));
|
|
59
|
+
out.dirGiven = out.dir != null;
|
|
60
|
+
out.agents = out.agents ? out.agents.split(",").map((s) => s.trim().toLowerCase()).filter(Boolean) : null;
|
|
61
|
+
out.boards = out.boards ? out.boards.split(",").map((s) => s.trim()).filter(Boolean) : null;
|
|
42
62
|
out.dir ??= DEFAULT_DIR;
|
|
43
63
|
return out;
|
|
44
64
|
}
|
package/server/board.mjs
CHANGED
|
@@ -4,16 +4,32 @@ import fs from "node:fs/promises";
|
|
|
4
4
|
import path from "node:path";
|
|
5
5
|
|
|
6
6
|
import { formatBoard } from "./boardFormat.mjs";
|
|
7
|
-
import { writeAtomic } from "./fsutil.mjs";
|
|
7
|
+
import { htmlTitle, projectName, writeAtomic } from "./fsutil.mjs";
|
|
8
|
+
import { placeNew } from "./place.mjs";
|
|
8
9
|
|
|
9
10
|
const DEFAULT_SIZE = { w: 390, h: 844 };
|
|
10
|
-
const GAP_X = 80;
|
|
11
|
-
const GAP_Y = 120;
|
|
12
11
|
|
|
13
|
-
|
|
12
|
+
/**
|
|
13
|
+
* order lists each board exactly once: duplicates and names missing from boards are dropped, boards missing from order
|
|
14
|
+
* are appended. Hand edits and agents writing board.json can leave a file twice, which renders two boards with one key.
|
|
15
|
+
* The next save writes the cleaned list back.
|
|
16
|
+
*/
|
|
17
|
+
export function normalizeOrder(order, boards) {
|
|
18
|
+
const out = [...new Set((Array.isArray(order) ? order : []).filter((f) => typeof f === "string" && Object.hasOwn(boards, f)))];
|
|
19
|
+
const seen = new Set(out);
|
|
20
|
+
for (const f of Object.keys(boards)) if (!seen.has(f)) out.push(f);
|
|
21
|
+
return out;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** onWrite(path, text) runs after every board.json save (the undo history keeps it as known content) */
|
|
25
|
+
export function createBoardStore(dir, { onWrite } = {}) {
|
|
14
26
|
const boardPath = path.join(dir, "board.json");
|
|
15
27
|
const screensDir = path.join(dir, "screens");
|
|
16
28
|
|
|
29
|
+
// Canvas name: the project folder. Older versions stored the design folder's name ("design") as the title, so that counts as unset
|
|
30
|
+
const name = projectName(dir);
|
|
31
|
+
const titleOf = (t) => (!t || (t === path.basename(dir) && t !== name) ? name : t);
|
|
32
|
+
|
|
17
33
|
async function readBoard() {
|
|
18
34
|
try {
|
|
19
35
|
const raw = await fs.readFile(boardPath, "utf8");
|
|
@@ -21,14 +37,14 @@ export function createBoardStore(dir) {
|
|
|
21
37
|
return {
|
|
22
38
|
...b,
|
|
23
39
|
version: 1,
|
|
24
|
-
title: b.title
|
|
40
|
+
title: titleOf(b.title),
|
|
25
41
|
pages: b.pages ?? [],
|
|
26
42
|
boards: b.boards ?? {},
|
|
27
|
-
order: b.order
|
|
43
|
+
order: normalizeOrder(b.order, b.boards ?? {}),
|
|
28
44
|
notes: b.notes ?? {},
|
|
29
45
|
};
|
|
30
46
|
} catch (e) {
|
|
31
|
-
if (e.code === "ENOENT") return { version: 1, title:
|
|
47
|
+
if (e.code === "ENOENT") return { version: 1, title: name, pages: [], boards: {}, order: [], notes: {} };
|
|
32
48
|
throw e;
|
|
33
49
|
}
|
|
34
50
|
}
|
|
@@ -41,6 +57,7 @@ export function createBoardStore(dir) {
|
|
|
41
57
|
lastWritten = text;
|
|
42
58
|
writing = writing.then(() => writeAtomic(boardPath, text));
|
|
43
59
|
await writing;
|
|
60
|
+
onWrite?.(boardPath, text);
|
|
44
61
|
}
|
|
45
62
|
|
|
46
63
|
/** .html files under screens/ (including subfolders, ignoring .icloud placeholders) */
|
|
@@ -65,30 +82,13 @@ export function createBoardStore(dir) {
|
|
|
65
82
|
return out.sort();
|
|
66
83
|
}
|
|
67
84
|
|
|
68
|
-
/** <meta name="board-size" content="390x844"> → {w,h} */
|
|
69
|
-
async function
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
if (m) return { w: Number(m[1]), h: Number(m[2]) };
|
|
74
|
-
} catch {
|
|
75
|
-
/* unreadable: use the default size */
|
|
76
|
-
}
|
|
77
|
-
return { ...DEFAULT_SIZE };
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
/** Row for new screens: right of the last auto-placed board, otherwise below everything + 120 */
|
|
81
|
-
function placeNew(board, size) {
|
|
82
|
-
const all = Object.values(board.boards);
|
|
83
|
-
const auto = all.filter((b) => b.autoPlaced);
|
|
84
|
-
if (auto.length) {
|
|
85
|
-
const last = auto.reduce((a, b) => (b.x > a.x ? b : a));
|
|
86
|
-
return { x: last.x + last.w + GAP_X, y: last.y, ...size };
|
|
87
|
-
}
|
|
88
|
-
const bottom = all.reduce((m, b) => Math.max(m, b.y + b.h), 0);
|
|
89
|
-
const left = all.reduce((m, b) => Math.min(m, b.x), 0);
|
|
90
|
-
return { x: left, y: bottom + GAP_Y, ...size };
|
|
85
|
+
/** <meta name="board-size" content="390x844"> → {w,h}, plus the file's <title> (null when missing) */
|
|
86
|
+
async function readBoardInfo(file) {
|
|
87
|
+
const html = await fs.readFile(path.join(screensDir, file), "utf8").catch(() => ""); // unreadable: default size, no title
|
|
88
|
+
const m = html.match(/<meta\s+name=["']board-size["']\s+content=["'](\d+)\s*[x×]\s*(\d+)["']/i);
|
|
89
|
+
return { size: m ? { w: Number(m[1]), h: Number(m[2]) } : { ...DEFAULT_SIZE }, title: htmlTitle(html) };
|
|
91
90
|
}
|
|
91
|
+
const readBoardSize = async (file) => (await readBoardInfo(file)).size;
|
|
92
92
|
|
|
93
93
|
// Queue board/file-changing requests and auto-placement in a single line. If auto-placement slipped in while an undo
|
|
94
94
|
// is writing board.json and deleting files, the deleted board would come back.
|
|
@@ -109,9 +109,9 @@ export function createBoardStore(dir) {
|
|
|
109
109
|
let changed = false;
|
|
110
110
|
for (const f of files) {
|
|
111
111
|
if (board.boards[f]) continue;
|
|
112
|
-
const size = await
|
|
113
|
-
board.boards[f] = { ...placeNew(board, size), title: f.replace(/\.html$/, ""), autoPlaced: true };
|
|
114
|
-
board.order.push(f);
|
|
112
|
+
const { size, title } = await readBoardInfo(f);
|
|
113
|
+
board.boards[f] = { ...placeNew(board, size), title: title ?? f.replace(/\.html$/, ""), autoPlaced: true };
|
|
114
|
+
if (!board.order.includes(f)) board.order.push(f);
|
|
115
115
|
changed = true;
|
|
116
116
|
}
|
|
117
117
|
if (changed) await writeBoard(board);
|