blockout-editor 0.0.0-stage → 0.1.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/index.d.ts ADDED
@@ -0,0 +1,13 @@
1
+ export interface EditorOptions {
2
+ /** Where Blockout is: its editor panel loads from there. Default https://blockout-cyan.vercel.app. */
3
+ blockout?: string;
4
+ /** The dev server's notes route. Default /api/blockout-notes. */
5
+ notesApi?: string;
6
+ /** Start with the panel open. */
7
+ open?: boolean;
8
+ /** How far the Edit button sits from the bottom right corner, in px. Default 16 each. */
9
+ button?: { right?: number; bottom?: number };
10
+ }
11
+ /** Starts Blockout's editor in this page (development only). Returns a function that takes it out again. */
12
+ export function startEditor(options?: EditorOptions): () => void;
13
+ export default startEditor;
package/index.js ADDED
@@ -0,0 +1,411 @@
1
+ /*
2
+ * Blockout's editor in a project's own dev page. startEditor() puts the editor's engine (engine.js, the same one
3
+ * Blockout's file view uses) in this page and docks Blockout's editor panel beside it, in a frame from Blockout
4
+ * (/editor), behind a small Edit button in the corner. Pick anything on the page and change it live against the page's
5
+ * own tokens; Send to Claude Code saves what changed, with a note, through the dev server into .notes/blockout-notes.json
6
+ * (server/next.js, server/vite.js), where Claude Code picks it up (`sc`, or `npx blockout-editor mcp`).
7
+ *
8
+ * Development only: call it from a dev build (react.js does). Nothing here reaches Blockout but what the panel needs
9
+ * to draw: values, token names and labels from the page, never its content.
10
+ */
11
+
12
+ const BLOCKOUT = "https://blockout-cyan.vercel.app";
13
+ const FRAME = "blockout-frame-";
14
+ const MARK =
15
+ "M25.65 4.81 39.34 7.72C42.04 8.29 43.77 10.95 43.19 13.65L40.28 27.34C39.71 30.04 37.05 31.77 34.35 31.19L20.66 28.28C17.96 27.71 16.23 25.05 16.81 22.35L19.72 8.66C20.29 5.96 22.95 4.23 25.65 4.81ZM11 15H15.81L14.75 20H12C10.9 20 10 20.9 10 22V36C10 37.1 10.9 38 12 38H26C27.1 38 28 37.1 28 36V32.4L33 33.46V37C33 40.31 30.31 43 27 43H11C7.69 43 5 40.31 5 37V21C5 17.69 7.69 15 11 15Z";
16
+
17
+ /**
18
+ * Starts the editor in this page. Returns a function that takes it out again.
19
+ * @param {{ blockout?: string, notesApi?: string, open?: boolean, button?: { right?: number, bottom?: number } }} [options]
20
+ * blockout: where Blockout is (its panel loads from there); notesApi: the dev server's notes route; open: start open;
21
+ * button: how far the Edit button sits from the bottom right corner, in px, clear of the app's own corner buttons.
22
+ */
23
+ export function startEditor({ blockout = BLOCKOUT, notesApi = "/api/blockout-notes", open = false, button = {} } = {}) {
24
+ if (typeof window === "undefined" || window.__blockoutEditorKit) return () => {};
25
+ window.__blockoutEditorKit = true;
26
+ const origin = new URL(blockout).origin;
27
+ // A frame of the canvas (below): the page at another width. Only the engine, talking to the page around it.
28
+ if (window.name.startsWith(FRAME)) {
29
+ window.__blockoutEditorConfig = { origin, frame: true };
30
+ import("./engine.js");
31
+ return () => {
32
+ window.__blockoutEditorKit = false;
33
+ };
34
+ }
35
+ // The engine reads where its panel is before it starts.
36
+ window.__blockoutEditorConfig = { origin };
37
+ import("./engine.js");
38
+
39
+ const host = document.createElement("div");
40
+ host.setAttribute("data-blockout-editor", "");
41
+ host.style.cssText =
42
+ "position:fixed;top:8px;right:8px;bottom:8px;width:360px;max-width:calc(100vw - 16px);z-index:2147483646;display:none;" +
43
+ "border-radius:16px;overflow:hidden;background:#fff;box-shadow:0 0 0 1px rgba(16,16,16,.1),0 12px 40px rgba(16,16,16,.18)";
44
+ const frame = document.createElement("iframe");
45
+ frame.src = `${origin}/editor`;
46
+ frame.title = "Blockout editor";
47
+ frame.allow = "clipboard-write";
48
+ frame.style.cssText = "display:block;width:100%;height:100%;border:0";
49
+ host.appendChild(frame);
50
+ // The page's layers, as Figma's left panel, docked on the other side (not on a narrow window).
51
+ const left = document.createElement("div");
52
+ left.setAttribute("data-blockout-editor", "");
53
+ left.style.cssText = host.style.cssText;
54
+ left.style.right = "auto";
55
+ left.style.left = "8px";
56
+ left.style.width = "264px";
57
+ const layers = document.createElement("iframe");
58
+ layers.src = `${origin}/editor?panel=layers`;
59
+ layers.title = "Blockout layers";
60
+ layers.style.cssText = frame.style.cssText;
61
+ left.appendChild(layers);
62
+ // Layers hidden (its own button): a small tab on the left brings them back. Remembered on this page's origin.
63
+ let layersShown = (localStorage.getItem("blockout-editor:layers") ?? (window.innerWidth >= 900 ? "shown" : "hidden")) !== "hidden";
64
+ const tab = document.createElement("button");
65
+ tab.type = "button";
66
+ tab.setAttribute("data-blockout-editor", "");
67
+ tab.textContent = "Layers";
68
+ tab.style.cssText =
69
+ "position:fixed;top:8px;left:8px;z-index:2147483646;display:none;padding:7px 12px;border:0;border-radius:12px;background:#fff;color:#101010;" +
70
+ "font:500 13px/1 ui-sans-serif,system-ui,sans-serif;cursor:pointer;box-shadow:0 0 0 1px rgba(16,16,16,.1),0 6px 20px rgba(16,16,16,.14)";
71
+ const showLayers = (on) => {
72
+ layersShown = on;
73
+ try {
74
+ localStorage.setItem("blockout-editor:layers", on ? "shown" : "hidden");
75
+ } catch {}
76
+ setOpen(isOpen);
77
+ };
78
+ tab.addEventListener("click", () => showLayers(true));
79
+
80
+ const pill = document.createElement("button");
81
+ pill.type = "button";
82
+ pill.setAttribute("data-blockout-editor", "");
83
+ pill.setAttribute("aria-label", "Open Blockout's editor");
84
+ pill.innerHTML = `<svg viewBox="5 4.7 38.3 38.3" width="14" height="14" fill="currentColor" aria-hidden="true"><path d="${MARK}"/></svg><span>Edit</span>`;
85
+ pill.style.cssText =
86
+ `position:fixed;right:${button.right ?? 16}px;bottom:${button.bottom ?? 16}px;z-index:2147483646;` +
87
+ "display:flex;align-items:center;gap:6px;padding:8px 14px;border:0;" +
88
+ "border-radius:999px;background:#101010;color:#fff;font:600 13px/1 ui-sans-serif,system-ui,sans-serif;cursor:pointer;" +
89
+ "box-shadow:0 4px 14px rgba(16,16,16,.24)";
90
+
91
+ let isOpen = false;
92
+ const toPanel = (msg) => frame.contentWindow?.postMessage({ ...msg, blockoutEditorKit: 1 }, origin);
93
+ // On a phone the panels are a sheet at the bottom, the page above them; Layers take the sheet's place when shown.
94
+ const place = (el, sheet, side) => {
95
+ Object.assign(el.style, sheet
96
+ ? { top: "auto", left: "8px", right: "8px", bottom: "8px", width: "auto", height: "55vh" }
97
+ : { top: "8px", bottom: "8px", height: "auto", left: side === "left" ? "8px" : "auto", right: side === "left" ? "auto" : "8px", width: side === "left" ? "264px" : "360px" });
98
+ };
99
+ const setOpen = (on) => {
100
+ isOpen = on;
101
+ host.style.display = on ? "block" : "none";
102
+ const narrow = window.innerWidth < 640;
103
+ const room = narrow || window.innerWidth >= 900;
104
+ place(host, narrow, "right");
105
+ place(left, narrow, "left");
106
+ // Above the page's outlines (the engine's, 2147483645); on a phone the Layers panel sits over the Design one.
107
+ left.style.zIndex = narrow ? "2147483647" : "2147483646";
108
+ left.style.display = on && room && layersShown ? "block" : "none";
109
+ tab.style.display = on && room && !layersShown ? "block" : "none";
110
+ pill.style.display = on ? "none" : "flex";
111
+ toPanel({ type: "open", open: on });
112
+ if (!on) window.__blockoutEditorSetOn?.(false);
113
+ };
114
+ pill.addEventListener("click", () => setOpen(true));
115
+ window.addEventListener("resize", () => isOpen && setOpen(true));
116
+ frame.addEventListener("load", () => toPanel({ type: "open", open: isOpen }));
117
+
118
+ const onMessage = async (event) => {
119
+ if (event.origin !== origin) return;
120
+ const m = event.data;
121
+ if (!m || m.blockoutEditorKit !== 1) return;
122
+ if (event.source === layers.contentWindow && m.type === "layers") return showLayers(!!m.open);
123
+ if (event.source !== frame.contentWindow) return;
124
+ if (m.type === "close") setOpen(false);
125
+ // The panel listens now: it is told whether it is open (a word sent at its frame's load may have come too soon).
126
+ if (m.type === "ready") toPanel({ type: "open", open: isOpen });
127
+ // Blockout's theme, so the canvas around the frames matches the panels.
128
+ if (m.type === "theme") canvas.setDark(m.dark === true);
129
+ if (m.type === "send") {
130
+ try {
131
+ const res = await fetch(notesApi, {
132
+ method: "POST",
133
+ headers: { "content-type": "application/json" },
134
+ body: JSON.stringify({ page: location.href, width: window.innerWidth, notes: m.notes }),
135
+ });
136
+ // No notes route here (a static page's server answers 404, 405 or 501): the panel puts the request on the
137
+ // clipboard instead, to paste into Claude Code.
138
+ if ([404, 405, 501].includes(res.status)) return toPanel({ type: "sent", ok: false, copy: true });
139
+ const said = res.ok ? {} : await res.json().catch(() => ({}));
140
+ toPanel({ type: "sent", ok: res.ok, error: res.ok ? undefined : said.error || `The notes route answered ${res.status}.` });
141
+ } catch {
142
+ toPanel({ type: "sent", ok: false, copy: true });
143
+ }
144
+ }
145
+ };
146
+ window.addEventListener("message", onMessage);
147
+ document.body.append(left, tab, host, pill);
148
+ setOpen(open);
149
+ const canvas = makeCanvas({
150
+ isOpen: () => isOpen,
151
+ insets: () => (window.innerWidth < 640 ? { left: 0, right: 0 } : { left: layersShown && window.innerWidth >= 900 ? 280 : 0, right: 376 }),
152
+ });
153
+ // Taken out (React's development mount runs an effect, undoes it and runs it again), it can start afresh; the engine
154
+ // stays in the page and the new panel greets it.
155
+ return () => {
156
+ window.removeEventListener("message", onMessage);
157
+ window.__blockoutEditorSetOn?.(false);
158
+ canvas.remove();
159
+ host.remove();
160
+ left.remove();
161
+ tab.remove();
162
+ pill.remove();
163
+ window.__blockoutEditorKit = false;
164
+ };
165
+ }
166
+
167
+ /*
168
+ * The canvas, as Figma's: zoomed out (a pinch, Cmd-scroll, Cmd-, Shift+1; from inside the page too), the page is a frame
169
+ * on a canvas over it, and frames at tablet and phone widths can join it, each this page loaded again at that width
170
+ * (its own engine, started by the kit in frame mode). The frame pressed in is the one the panels work on: the page's
171
+ * engine passes their messages to it, and its changes are made the same in the other frames by CSS path. Back at 100%
172
+ * with the desktop frame alone, the canvas goes and the page has what was changed there.
173
+ */
174
+ const DEVICES = [
175
+ { id: "desktop", name: "Desktop", w: 0, h: 0 },
176
+ { id: "tablet", name: "Tablet", w: 768, h: 1024 },
177
+ { id: "phone", name: "Phone", w: 390, h: 844 },
178
+ ];
179
+ const GAP = 80;
180
+
181
+ function makeCanvas({ isOpen, insets }) {
182
+ let shell = null;
183
+ let world = null;
184
+ let bar = null;
185
+ let zoomLabel = null;
186
+ const frames = {};
187
+ let devices = ["desktop"];
188
+ let active = "desktop";
189
+ let view = { z: 1, x: 0, y: 0 };
190
+ let lastChanges = [];
191
+ // The canvas in Blockout's light or dark colours (paper and ink, as the panels have them), as the panel says.
192
+ const THEMES = {
193
+ light: { canvas: "#ece8e1", bar: "#fff", ring: "rgba(16,16,16,.1)", ink: "#101010", pressed: "#efebe4", label: "#6b665f", active: "#e12c05" },
194
+ dark: { canvas: "#101010", bar: "#131211", ring: "rgba(245,242,236,.12)", ink: "#f5f2ec", pressed: "#282522", label: "#948d84", active: "#ff6b4f" },
195
+ };
196
+ let theme = THEMES.light;
197
+ const paint = () => {
198
+ if (!shell) return;
199
+ for (const [k, v] of Object.entries(theme)) shell.style.setProperty(`--bo-${k}`, v);
200
+ };
201
+ window.__blockoutEditorFrames = [];
202
+
203
+ const size = () => ({ w: window.innerWidth, h: window.innerHeight });
204
+ const layout = () => {
205
+ let x = 0;
206
+ return DEVICES.filter((d) => devices.includes(d.id)).map((d) => {
207
+ const w = d.id === "desktop" ? size().w : d.w;
208
+ const h = d.id === "desktop" ? size().h : d.h;
209
+ const f = { ...d, x, w, h };
210
+ x += w + GAP;
211
+ return f;
212
+ });
213
+ };
214
+ const draw = () => {
215
+ if (!shell) return;
216
+ world.style.transform = `translate(${view.x}px, ${view.y}px) scale(${view.z})`;
217
+ for (const f of layout()) {
218
+ const el = frames[f.id];
219
+ if (!el) continue;
220
+ el.box.style.left = `${f.x}px`;
221
+ el.frame.style.width = `${f.w}px`;
222
+ el.frame.style.height = `${f.h}px`;
223
+ el.label.textContent = `${f.name} · ${f.w}`;
224
+ el.label.style.fontSize = `${12 / view.z}px`;
225
+ el.label.style.paddingBottom = `${6 / view.z}px`;
226
+ el.label.style.color = f.id === active ? "var(--bo-active)" : "var(--bo-label)";
227
+ el.frame.style.outline = f.id === active ? `${2 / view.z}px solid #ff4d2e` : "none";
228
+ }
229
+ zoomLabel.textContent = `${Math.round(view.z * 100)}%`;
230
+ for (const b of bar.querySelectorAll("[data-device]")) b.setAttribute("aria-pressed", String(devices.includes(b.dataset.device)));
231
+ };
232
+ const relayTo = (id) => {
233
+ active = id;
234
+ const w = frames[id]?.frame.contentWindow;
235
+ window.__blockoutEditorRelay?.(w);
236
+ w?.postMessage({ blockoutEditor: 1, type: "hello" }, location.origin);
237
+ draw();
238
+ };
239
+ const addFrame = (d) => {
240
+ const box = document.createElement("div");
241
+ box.style.cssText = "position:absolute;top:0";
242
+ const label = document.createElement("p");
243
+ label.style.cssText = "position:absolute;bottom:100%;left:0;margin:0;white-space:nowrap;font:500 12px/1.4 ui-sans-serif,system-ui,sans-serif";
244
+ const frame = document.createElement("iframe");
245
+ frame.name = FRAME + d.id;
246
+ frame.title = `${d.name} width`;
247
+ frame.src = location.href;
248
+ frame.style.cssText = "display:block;border:0;background:#fff;box-shadow:0 0 0 1px rgba(16,16,16,.08),0 12px 40px rgba(16,16,16,.12)";
249
+ // The frame's engine says it has started (it loads after the frame does): it is turned on, and either worked in or
250
+ // given the changes made so far.
251
+ box.append(label, frame);
252
+ world.appendChild(box);
253
+ frames[d.id] = { box, frame, label };
254
+ window.__blockoutEditorFrames = Object.values(frames).map((f) => f.frame.contentWindow);
255
+ };
256
+ const fit = () => {
257
+ const list = layout();
258
+ const all = { w: Math.max(...list.map((f) => f.x + f.w)), h: Math.max(...list.map((f) => f.h)) };
259
+ const ins = insets();
260
+ const room = { w: size().w - ins.left - ins.right - 96, h: size().h - 160 };
261
+ const z = Math.min(1, room.w / all.w, room.h / all.h);
262
+ view = { z, x: ins.left + 48 + (room.w - all.w * z) / 2, y: 96 + (room.h - all.h * z) / 2 };
263
+ draw();
264
+ };
265
+ const zoomAt = (factor, cx, cy) => {
266
+ if (!shell) open();
267
+ const z = Math.min(4, Math.max(0.05, view.z * factor));
268
+ view = { z, x: cx - ((cx - view.x) / view.z) * z, y: cy - ((cy - view.y) / view.z) * z };
269
+ // Back at 100%, the desktop frame alone, where the page is: the canvas goes.
270
+ if (Math.abs(z - 1) < 0.02 && devices.length === 1 && Math.abs(view.x) < 40 && Math.abs(view.y) < 40) return close();
271
+ draw();
272
+ };
273
+ const key = (k) => {
274
+ const ins = insets();
275
+ const cx = ins.left + (size().w - ins.left - ins.right) / 2;
276
+ if (k === "=") zoomAt(1.25, cx, size().h / 2);
277
+ else if (k === "-") zoomAt(0.8, cx, size().h / 2);
278
+ else if (k === "0") close();
279
+ else {
280
+ if (!shell) open();
281
+ fit();
282
+ }
283
+ };
284
+ const toggle = (id) => {
285
+ const next = devices.includes(id) ? devices.filter((x) => x !== id) : DEVICES.map((d) => d.id).filter((x) => x === id || devices.includes(x));
286
+ if (!next.length) return;
287
+ for (const d of DEVICES)
288
+ if (devices.includes(d.id) && !next.includes(d.id)) {
289
+ frames[d.id].box.remove();
290
+ delete frames[d.id];
291
+ }
292
+ devices = next;
293
+ for (const d of DEVICES) if (devices.includes(d.id) && !frames[d.id]) addFrame(d);
294
+ window.__blockoutEditorFrames = Object.values(frames).map((f) => f.frame.contentWindow);
295
+ if (!devices.includes(active)) relayTo(devices[0]);
296
+ fit();
297
+ };
298
+ const button = (text, label, onClick) => {
299
+ const b = document.createElement("button");
300
+ b.type = "button";
301
+ b.textContent = text;
302
+ b.setAttribute("aria-label", label);
303
+ b.title = label;
304
+ b.style.cssText =
305
+ "border:0;border-radius:8px;background:transparent;color:var(--bo-ink);padding:5px 9px;font:500 12px/1 ui-sans-serif,system-ui,sans-serif;cursor:pointer";
306
+ b.addEventListener("click", onClick);
307
+ return b;
308
+ };
309
+ const open = () => {
310
+ if (shell) return;
311
+ shell = document.createElement("div");
312
+ shell.setAttribute("data-blockout-editor", "");
313
+ shell.style.cssText = "position:fixed;inset:0;z-index:2147483644;overflow:hidden;background:var(--bo-canvas)";
314
+ world = document.createElement("div");
315
+ world.style.cssText = "position:absolute;left:0;top:0;transform-origin:0 0";
316
+ bar = document.createElement("div");
317
+ bar.style.cssText =
318
+ "position:absolute;top:12px;left:50%;transform:translateX(-50%);display:flex;gap:2px;align-items:center;padding:4px;border-radius:14px;background:var(--bo-bar);" +
319
+ "box-shadow:0 0 0 1px var(--bo-ring),0 8px 24px rgba(16,16,16,.14)";
320
+ for (const d of DEVICES) {
321
+ const b = button(d.name, `${d.name} frame`, () => toggle(d.id));
322
+ b.dataset.device = d.id;
323
+ bar.appendChild(b);
324
+ }
325
+ bar.append(button("−", "Zoom out", () => key("-")));
326
+ zoomLabel = button("100%", "Zoom to fit", () => key("fit"));
327
+ bar.append(zoomLabel, button("+", "Zoom in", () => key("=")), button("100%", "Back to the page", () => key("0")));
328
+ const style = document.createElement("style");
329
+ style.textContent = '[data-blockout-editor] [aria-pressed="true"]{background:var(--bo-pressed)}';
330
+ shell.append(style, world, bar);
331
+ shell.addEventListener(
332
+ "wheel",
333
+ (e) => {
334
+ e.preventDefault();
335
+ if (e.ctrlKey || e.metaKey) zoomAt(Math.exp(-e.deltaY * 0.01), e.clientX, e.clientY);
336
+ else {
337
+ view = { ...view, x: view.x - e.deltaX, y: view.y - e.deltaY };
338
+ draw();
339
+ }
340
+ },
341
+ { passive: false },
342
+ );
343
+ paint();
344
+ document.body.appendChild(shell);
345
+ for (const d of DEVICES) if (devices.includes(d.id)) addFrame(d);
346
+ draw();
347
+ };
348
+ const close = () => {
349
+ if (!shell) return;
350
+ shell.remove();
351
+ shell = null;
352
+ for (const k of Object.keys(frames)) delete frames[k];
353
+ window.__blockoutEditorFrames = [];
354
+ devices = ["desktop"];
355
+ active = "desktop";
356
+ view = { z: 1, x: 0, y: 0 };
357
+ window.__blockoutEditorRelay?.(null, lastChanges);
358
+ };
359
+
360
+ // Zooms from the page (its engine) and from the frames; which frame is pressed in; its changes, for the others.
361
+ const onZoom = (e) => {
362
+ if (!isOpen()) return;
363
+ const m = e.detail;
364
+ if (m.type === "zoomKey") key(m.key);
365
+ else zoomAt(Math.exp(-m.dy * 0.01), m.x, m.y);
366
+ };
367
+ const onFrame = (e) => {
368
+ const { msg, from } = e.detail;
369
+ const id = Object.keys(frames).find((k) => frames[k].frame.contentWindow === from);
370
+ if (!id) return;
371
+ if (msg.type === "boot") {
372
+ from.postMessage({ blockoutEditor: 1, type: "on", on: true }, location.origin);
373
+ if (id === active) relayTo(id);
374
+ else from.postMessage({ blockoutEditor: 1, type: "mirror", changes: lastChanges }, location.origin);
375
+ } else if (msg.type === "focus" && id !== active) relayTo(id);
376
+ else if (msg.type === "zoomKey") key(msg.key);
377
+ else if (msg.type === "zoom") {
378
+ const f = layout().find((l) => l.id === id);
379
+ if (f) zoomAt(Math.exp(-msg.dy * 0.01), view.x + (f.x + msg.x) * view.z, view.y + msg.y * view.z);
380
+ } else if (msg.type === "changes" && id === active) {
381
+ lastChanges = msg.changes || [];
382
+ for (const k of Object.keys(frames))
383
+ if (k !== id) frames[k].frame.contentWindow?.postMessage({ blockoutEditor: 1, type: "mirror", changes: lastChanges }, location.origin);
384
+ }
385
+ };
386
+ // With the canvas open the page's engine stands still, so the zoom keys are heard here.
387
+ const onKey = (e) => {
388
+ if (!shell) return;
389
+ const k = (e.metaKey || e.ctrlKey) && /^(=|\+|-|0)$/.test(e.key) ? (e.key === "+" ? "=" : e.key) : e.shiftKey && e.code === "Digit1" ? "fit" : null;
390
+ if (!k) return;
391
+ e.preventDefault();
392
+ key(k);
393
+ };
394
+ window.addEventListener("keydown", onKey, true);
395
+ window.addEventListener("blockout-editor:zoom", onZoom);
396
+ window.addEventListener("blockout-editor:frame", onFrame);
397
+ return {
398
+ setDark(dark) {
399
+ theme = dark ? THEMES.dark : THEMES.light;
400
+ paint();
401
+ },
402
+ remove() {
403
+ close();
404
+ window.removeEventListener("keydown", onKey, true);
405
+ window.removeEventListener("blockout-editor:zoom", onZoom);
406
+ window.removeEventListener("blockout-editor:frame", onFrame);
407
+ },
408
+ };
409
+ }
410
+
411
+ export default startEditor;
package/package.json CHANGED
@@ -1,6 +1,65 @@
1
1
  {
2
2
  "name": "blockout-editor",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.0",
4
+ "description": "Blockout's editor in your app's dev page: pick anything, change its spacing, type, colour and layout against your own tokens, and send it to Claude Code to write into your code.",
5
+ "license": "SEE LICENSE IN LICENSE",
6
+ "author": "Yash Gidwani (https://github.com/ux-yash)",
7
+ "keywords": [
8
+ "figma",
9
+ "design",
10
+ "editor",
11
+ "claude-code",
12
+ "design-tokens",
13
+ "devtools"
14
+ ],
15
+ "type": "module",
16
+ "exports": {
17
+ ".": {
18
+ "types": "./index.d.ts",
19
+ "default": "./index.js"
20
+ },
21
+ "./react": {
22
+ "types": "./react.d.ts",
23
+ "default": "./react.js"
24
+ },
25
+ "./next": {
26
+ "types": "./server/next.d.ts",
27
+ "default": "./server/next.js"
28
+ },
29
+ "./vite": {
30
+ "types": "./server/vite.d.ts",
31
+ "default": "./server/vite.js"
32
+ },
33
+ "./bundle": "./blockout-editor.js"
34
+ },
35
+ "bin": {
36
+ "blockout-editor": "server/cli.js"
37
+ },
38
+ "files": [
39
+ "index.js",
40
+ "index.d.ts",
41
+ "react.js",
42
+ "react.d.ts",
43
+ "engine.js",
44
+ "server",
45
+ "README.md",
46
+ "NOTICE.md",
47
+ "LICENSE",
48
+ "blockout-editor.js"
49
+ ],
50
+ "peerDependencies": {
51
+ "react": ">=18"
52
+ },
53
+ "peerDependenciesMeta": {
54
+ "react": {
55
+ "optional": true
56
+ }
57
+ },
58
+ "engines": {
59
+ "node": ">=18"
60
+ },
61
+ "scripts": {
62
+ "test": "node --test test/notes.test.mjs test/bundle.test.mjs",
63
+ "build": "node scripts/bundle.mjs"
64
+ }
65
+ }
package/react.d.ts ADDED
@@ -0,0 +1,4 @@
1
+ import type { EditorOptions } from "./index.js";
2
+ /** Blockout's editor in a React app's dev page: render once, beside the app. Renders nothing; does nothing in production. */
3
+ export function BlockoutEditor(props: EditorOptions): null;
4
+ export default BlockoutEditor;
package/react.js ADDED
@@ -0,0 +1,21 @@
1
+ "use client";
2
+
3
+ /*
4
+ * <BlockoutEditor />: Blockout's editor in a React app's dev page. Render it once, beside the app (Next.js: in the root
5
+ * layout; Vite: next to <App />). It renders nothing, and in a production build it does nothing.
6
+ */
7
+ import { useEffect } from "react";
8
+ import { startEditor } from "./index.js";
9
+
10
+ /** @param {{ blockout?: string, notesApi?: string, open?: boolean }} props */
11
+ export function BlockoutEditor(props) {
12
+ useEffect(() => {
13
+ if (process.env.NODE_ENV === "production") return;
14
+ return startEditor(props);
15
+ // Started once; its options are read then.
16
+ // eslint-disable-next-line react-hooks/exhaustive-deps
17
+ }, []);
18
+ return null;
19
+ }
20
+
21
+ export default BlockoutEditor;
package/server/cli.js ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env node
2
+ // blockout-editor mcp [--dir path/to/.notes]: the notes' MCP server (mcp.js), over stdio.
3
+ import { readFileSync } from "node:fs";
4
+ import path from "node:path";
5
+ import { runMcp } from "./mcp.js";
6
+
7
+ const args = process.argv.slice(2);
8
+ if (args[0] === "mcp") {
9
+ const at = args.indexOf("--dir");
10
+ const { version } = JSON.parse(readFileSync(new URL("../package.json", import.meta.url), "utf8"));
11
+ await runMcp({ version, ...(at !== -1 && args[at + 1] ? { dir: path.resolve(args[at + 1]) } : {}) });
12
+ } else {
13
+ process.stdout.write("Usage: blockout-editor mcp [--dir path/to/.notes]\n An MCP server for the notes Blockout's editor saves in this app.\n");
14
+ process.exit(args.length ? 1 : 0);
15
+ }
package/server/mcp.js ADDED
@@ -0,0 +1,97 @@
1
+ /*
2
+ * An MCP server over stdio for the notes Blockout's editor saves (store.js), so Claude Code (or any MCP client) can
3
+ * read them, answer each and mark it resolved without being told where the file is:
4
+ * claude mcp add blockout-editor -- npx blockout-editor mcp
5
+ * Run from the app's folder, or pass --dir to point at its .notes.
6
+ */
7
+ import { createInterface } from "node:readline";
8
+ import { createStore } from "./store.js";
9
+
10
+ /** A note as Claude Code reads it: what to change, where, and what was changed on the page. */
11
+ export function noteText(n) {
12
+ const lines = [`Note ${n.id} (${n.status}) on ${n.page || "the page"}${n.width ? `, window ${n.width}px wide` : ""}`];
13
+ if (n.text) lines.push(` Asks: ${n.text}`);
14
+ if (n.element) {
15
+ lines.push(` About: ${n.element.label}${n.element.text ? ` ("${n.element.text}")` : ""}`);
16
+ if (n.element.components?.length) lines.push(` in: ${n.element.components.join(" < ")}`);
17
+ if (n.element.path) lines.push(` at: ${n.element.path}`);
18
+ }
19
+ if (n.changes?.length) {
20
+ lines.push(" Changed on the page (make these in the code, with the project's own tokens and classes):");
21
+ for (const c of n.changes) {
22
+ lines.push(` ${c.label}${c.components?.length ? ` in ${c.components.join(" < ")}` : ""}${c.path ? ` at ${c.path}` : ""}`);
23
+ for (const p of c.props) lines.push(` ${p.prop}: ${p.from ?? "(not set)"} → ${p.to ?? "(removed)"}`);
24
+ if (c.text) lines.push(` text: "${c.text.from}" → "${c.text.to}"`);
25
+ if (c.removed) lines.push(" deleted from the page");
26
+ }
27
+ }
28
+ for (const r of n.replies ?? []) lines.push(` Reply: ${r.text}`);
29
+ return lines.join("\n");
30
+ }
31
+
32
+ const TOOLS = [
33
+ {
34
+ name: "list_notes",
35
+ description:
36
+ "The notes left with Blockout's editor on this app's pages: what to change, on which element (its CSS path and React components), and the style, text and layer changes made live on the page, to write into the code with the project's own tokens and classes (not inline styles), keeping the app consistent: when the element shares a component, class or pattern with others, decide whether the change is for all of them or this one, and say which.",
37
+ inputSchema: { type: "object", properties: { status: { type: "string", enum: ["open", "resolved", "all"], description: "Default open." } } },
38
+ },
39
+ {
40
+ name: "reply_to_note",
41
+ description: "Answers a note, in one line: what was done, or why not.",
42
+ inputSchema: { type: "object", properties: { id: { type: "string" }, text: { type: "string" } }, required: ["id", "text"] },
43
+ },
44
+ {
45
+ name: "resolve_note",
46
+ description: "Marks a note done, with a one-line reply saying what changed.",
47
+ inputSchema: { type: "object", properties: { id: { type: "string" }, reply: { type: "string" } }, required: ["id"] },
48
+ },
49
+ ];
50
+
51
+ export async function runMcp({ dir, version = "0.0.0", input = process.stdin, output = process.stdout } = {}) {
52
+ const store = createStore(dir ? { dir } : undefined);
53
+ const write = (msg) => output.write(`${JSON.stringify(msg)}\n`);
54
+ const text = (t, isError = false) => ({ content: [{ type: "text", text: t }], ...(isError ? { isError: true } : {}) });
55
+ const call = async (name, args) => {
56
+ if (name === "list_notes") {
57
+ const notes = await store.list(args.status || "open");
58
+ return text(notes.length ? notes.map(noteText).join("\n\n") : `No ${args.status === "all" ? "" : `${args.status || "open"} `}notes.`);
59
+ }
60
+ if (name === "reply_to_note") {
61
+ const note = await store.update(String(args.id ?? ""), { reply: String(args.text ?? "") });
62
+ return note ? text(`Replied to ${note.id}.`) : text("There's no note with that id.", true);
63
+ }
64
+ if (name === "resolve_note") {
65
+ const note = await store.update(String(args.id ?? ""), { status: "resolved", reply: typeof args.reply === "string" ? args.reply : undefined });
66
+ return note ? text(`Resolved ${note.id}.`) : text("There's no note with that id.", true);
67
+ }
68
+ return text(`There's no tool called ${name}.`, true);
69
+ };
70
+ for await (const line of createInterface({ input })) {
71
+ if (!line.trim()) continue;
72
+ let msg;
73
+ try {
74
+ msg = JSON.parse(line);
75
+ } catch {
76
+ write({ jsonrpc: "2.0", id: null, error: { code: -32700, message: "Parse error" } });
77
+ continue;
78
+ }
79
+ // Notifications (initialized, cancelled) want no answer.
80
+ if (msg.id === undefined || msg.id === null) continue;
81
+ try {
82
+ let result;
83
+ if (msg.method === "initialize")
84
+ result = { protocolVersion: msg.params?.protocolVersion || "2025-06-18", capabilities: { tools: {} }, serverInfo: { name: "blockout-editor", version } };
85
+ else if (msg.method === "ping") result = {};
86
+ else if (msg.method === "tools/list") result = { tools: TOOLS };
87
+ else if (msg.method === "tools/call") result = await call(msg.params?.name, msg.params?.arguments || {});
88
+ else {
89
+ write({ jsonrpc: "2.0", id: msg.id, error: { code: -32601, message: `No method ${msg.method}` } });
90
+ continue;
91
+ }
92
+ write({ jsonrpc: "2.0", id: msg.id, result });
93
+ } catch (err) {
94
+ write({ jsonrpc: "2.0", id: msg.id, error: { code: -32603, message: String(err?.message || err) } });
95
+ }
96
+ }
97
+ }
@@ -0,0 +1,5 @@
1
+ type Handler = (request: Request) => Promise<Response>;
2
+ export const GET: Handler;
3
+ export const POST: Handler;
4
+ export const PATCH: Handler;
5
+ export const DELETE: Handler;
package/server/next.js ADDED
@@ -0,0 +1,23 @@
1
+ /*
2
+ * The notes route for a Next.js app (App Router). Add app/api/blockout-notes/route.js with:
3
+ * export { GET, POST, PATCH, DELETE } from "blockout-editor/next";
4
+ * It works on the dev server only: in production every method answers 404.
5
+ */
6
+ import { createStore, handle, refusal } from "./store.js";
7
+
8
+ const store = createStore();
9
+
10
+ async function respond(request) {
11
+ if (process.env.NODE_ENV !== "development") return Response.json({ error: "Blockout's editor notes only work on the dev server." }, { status: 404 });
12
+ const no = refusal((name) => request.headers.get(name));
13
+ if (no) return Response.json({ error: no }, { status: 403 });
14
+ if (Number(request.headers.get("content-length") || 0) > 1 << 20) return Response.json({ error: "That's too much for one note." }, { status: 413 });
15
+ const body = request.method === "GET" || request.method === "DELETE" ? null : await request.json().catch(() => null);
16
+ const { status, body: out } = await handle(store, request.method, request.url, body);
17
+ return Response.json(out, { status });
18
+ }
19
+
20
+ export const GET = respond;
21
+ export const POST = respond;
22
+ export const PATCH = respond;
23
+ export const DELETE = respond;