blockout-editor 0.0.0-stage → 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/LICENSE +20 -0
- package/NOTICE.md +26 -0
- package/README.md +133 -2
- package/blockout-editor.js +3469 -0
- package/engine.js +2839 -0
- package/index.d.ts +13 -0
- package/index.js +606 -0
- package/package.json +63 -4
- package/react.d.ts +4 -0
- package/react.js +21 -0
- package/server/claude.js +334 -0
- package/server/cli.js +15 -0
- package/server/mcp.js +97 -0
- package/server/next.d.ts +5 -0
- package/server/next.js +32 -0
- package/server/store.js +135 -0
- package/server/vite.d.ts +3 -0
- package/server/vite.js +51 -0
package/server/store.js
ADDED
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* The notes Blockout's editor saves in a project: .notes/blockout-notes.json in the app's folder, one note per send. A note says
|
|
3
|
+
* what to change (its words), where (the element picked: its label, CSS path and React components), and what was
|
|
4
|
+
* changed on the page (each element's styles, from → to), for Claude Code to write into the code. Claude Code reads
|
|
5
|
+
* them (`sc`, or the MCP server, mcp.js), answers each and marks it resolved.
|
|
6
|
+
*
|
|
7
|
+
* { "notes": [ { "id", "createdAt", "status": "open" | "resolved", "page", "width", "text",
|
|
8
|
+
* "element": { "label", "path", "components", "text" } | null,
|
|
9
|
+
* "changes": [ { "label", "path", "components", "props": [ { "prop", "from", "to" } ] } ],
|
|
10
|
+
* "replies": [ { "at", "text" } ] } ] }
|
|
11
|
+
*/
|
|
12
|
+
import { promises as fs } from "node:fs";
|
|
13
|
+
import path from "node:path";
|
|
14
|
+
import { randomUUID } from "node:crypto";
|
|
15
|
+
|
|
16
|
+
export const ROUTE = "/api/blockout-notes";
|
|
17
|
+
|
|
18
|
+
const str = (v, max) => (typeof v === "string" ? v.slice(0, max) : "");
|
|
19
|
+
const strOrNull = (v, max) => (typeof v === "string" ? v.slice(0, max) : null);
|
|
20
|
+
const list = (v, max) => (Array.isArray(v) ? v.slice(0, max) : []);
|
|
21
|
+
|
|
22
|
+
function cleanElement(e) {
|
|
23
|
+
if (!e || typeof e !== "object") return null;
|
|
24
|
+
return { label: str(e.label, 200), path: str(e.path, 1000), components: list(e.components, 6).map((c) => str(c, 100)), text: str(e.text, 200) };
|
|
25
|
+
}
|
|
26
|
+
function cleanChange(c) {
|
|
27
|
+
return {
|
|
28
|
+
label: str(c?.label, 200),
|
|
29
|
+
path: str(c?.path, 1000),
|
|
30
|
+
components: list(c?.components, 6).map((x) => str(x, 100)),
|
|
31
|
+
props: list(c?.props, 60).map((p) => ({ prop: str(p?.prop, 80), from: strOrNull(p?.from, 400), to: strOrNull(p?.to, 400) })),
|
|
32
|
+
...(c?.text && typeof c.text === "object" ? { text: { from: str(c.text.from, 500), to: str(c.text.to, 500) } } : {}),
|
|
33
|
+
...(c?.removed === true ? { removed: true } : {}),
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export function createStore({ dir = path.join(process.cwd(), ".notes") } = {}) {
|
|
38
|
+
const file = path.join(dir, "blockout-notes.json");
|
|
39
|
+
let queue = Promise.resolve();
|
|
40
|
+
const read = async () => {
|
|
41
|
+
try {
|
|
42
|
+
const all = JSON.parse(await fs.readFile(file, "utf8"));
|
|
43
|
+
return Array.isArray(all?.notes) ? all : { notes: [] };
|
|
44
|
+
} catch {
|
|
45
|
+
return { notes: [] };
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
// One write at a time, each whole: written aside, then moved into place.
|
|
49
|
+
const change = (fn) => {
|
|
50
|
+
const run = queue.then(async () => {
|
|
51
|
+
const all = await read();
|
|
52
|
+
const out = await fn(all);
|
|
53
|
+
await fs.mkdir(dir, { recursive: true });
|
|
54
|
+
const tmp = `${file}.${process.pid}.tmp`;
|
|
55
|
+
await fs.writeFile(tmp, `${JSON.stringify(all, null, 2)}\n`);
|
|
56
|
+
await fs.rename(tmp, file);
|
|
57
|
+
return out;
|
|
58
|
+
});
|
|
59
|
+
queue = run.catch(() => {});
|
|
60
|
+
return run;
|
|
61
|
+
};
|
|
62
|
+
return {
|
|
63
|
+
file,
|
|
64
|
+
list: async (status) => (await read()).notes.filter((n) => !status || status === "all" || n.status === status),
|
|
65
|
+
add: (input) =>
|
|
66
|
+
change((all) => {
|
|
67
|
+
const added = list(input?.notes, 20)
|
|
68
|
+
.map((n) => ({
|
|
69
|
+
id: randomUUID(),
|
|
70
|
+
createdAt: new Date().toISOString(),
|
|
71
|
+
status: "open",
|
|
72
|
+
page: str(input?.page, 2000),
|
|
73
|
+
width: Number.isFinite(input?.width) ? Math.round(input.width) : null,
|
|
74
|
+
text: str(n?.text, 4000).trim(),
|
|
75
|
+
element: cleanElement(n?.element),
|
|
76
|
+
changes: list(n?.changes, 200).map(cleanChange),
|
|
77
|
+
replies: [],
|
|
78
|
+
}))
|
|
79
|
+
.filter((n) => n.text || n.changes.length);
|
|
80
|
+
all.notes.push(...added);
|
|
81
|
+
return added;
|
|
82
|
+
}),
|
|
83
|
+
update: (id, { status, reply } = {}) =>
|
|
84
|
+
change((all) => {
|
|
85
|
+
const note = all.notes.find((n) => n.id === id);
|
|
86
|
+
if (!note) return null;
|
|
87
|
+
if (status === "open" || status === "resolved") note.status = status;
|
|
88
|
+
if (typeof reply === "string" && reply.trim()) note.replies.push({ at: new Date().toISOString(), text: reply.trim().slice(0, 2000) });
|
|
89
|
+
return note;
|
|
90
|
+
}),
|
|
91
|
+
remove: (id) =>
|
|
92
|
+
change((all) => {
|
|
93
|
+
const before = all.notes.length;
|
|
94
|
+
all.notes = all.notes.filter((n) => n.id !== id);
|
|
95
|
+
return all.notes.length !== before;
|
|
96
|
+
}),
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Only the app's own pages may write notes: a request from another site (its Origin isn't this host, or the browser
|
|
102
|
+
* says it is cross-site) is refused. Returns why, or null.
|
|
103
|
+
*/
|
|
104
|
+
export function refusal(get) {
|
|
105
|
+
const site = get("sec-fetch-site");
|
|
106
|
+
if (site && site !== "same-origin" && site !== "none") return "Only this app's own pages can write its notes.";
|
|
107
|
+
const origin = get("origin");
|
|
108
|
+
const host = get("host");
|
|
109
|
+
if (origin && host) {
|
|
110
|
+
try {
|
|
111
|
+
if (new URL(origin).host !== host) return "Only this app's own pages can write its notes.";
|
|
112
|
+
} catch {
|
|
113
|
+
return "Only this app's own pages can write its notes.";
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
return null;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/** One request to the notes route: answers { status, body }. */
|
|
120
|
+
export async function handle(store, method, url, body) {
|
|
121
|
+
const params = new URL(url, "http://localhost").searchParams;
|
|
122
|
+
if (method === "GET") return { status: 200, body: { notes: await store.list(params.get("status") || "all") } };
|
|
123
|
+
if (method === "POST") {
|
|
124
|
+
const added = await store.add(body);
|
|
125
|
+
return added.length ? { status: 201, body: { notes: added } } : { status: 400, body: { error: "Nothing to save: a note needs words or changes." } };
|
|
126
|
+
}
|
|
127
|
+
if (method === "PATCH") {
|
|
128
|
+
const note = await store.update(str(body?.id, 100), body ?? {});
|
|
129
|
+
return note ? { status: 200, body: { note } } : { status: 404, body: { error: "There's no note with that id." } };
|
|
130
|
+
}
|
|
131
|
+
if (method === "DELETE") {
|
|
132
|
+
return (await store.remove(params.get("id") || "")) ? { status: 200, body: {} } : { status: 404, body: { error: "There's no note with that id." } };
|
|
133
|
+
}
|
|
134
|
+
return { status: 405, body: { error: "GET, POST, PATCH or DELETE." } };
|
|
135
|
+
}
|
package/server/vite.d.ts
ADDED
package/server/vite.js
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* The notes route for a Vite app (Astro, SvelteKit and the rest on Vite too): add blockoutNotes() to the plugins.
|
|
3
|
+
* import blockoutNotes from "blockout-editor/vite";
|
|
4
|
+
* export default { plugins: [blockoutNotes()] };
|
|
5
|
+
* The dev server only: `vite build` leaves it out.
|
|
6
|
+
*/
|
|
7
|
+
import { claudeRefusal, createChats, handleClaude } from "./claude.js";
|
|
8
|
+
import { ROUTE, createStore, handle, refusal } from "./store.js";
|
|
9
|
+
|
|
10
|
+
export function blockoutNotes({ route = ROUTE } = {}) {
|
|
11
|
+
const store = createStore();
|
|
12
|
+
const chats = createChats();
|
|
13
|
+
return {
|
|
14
|
+
name: "blockout-editor-notes",
|
|
15
|
+
apply: "serve",
|
|
16
|
+
configureServer(server) {
|
|
17
|
+
server.middlewares.use(async (req, res, next) => {
|
|
18
|
+
if (!req.url || !req.url.startsWith(route)) return next();
|
|
19
|
+
const send = (status, out) => {
|
|
20
|
+
res.statusCode = status;
|
|
21
|
+
res.setHeader("content-type", "application/json");
|
|
22
|
+
res.end(JSON.stringify(out));
|
|
23
|
+
};
|
|
24
|
+
const no = refusal((name) => req.headers[name]);
|
|
25
|
+
if (no) return send(403, { error: no });
|
|
26
|
+
let raw = "";
|
|
27
|
+
for await (const chunk of req) {
|
|
28
|
+
raw += chunk;
|
|
29
|
+
if (raw.length > 1 << 20) return send(413, { error: "That's too much for one note." });
|
|
30
|
+
}
|
|
31
|
+
let body = null;
|
|
32
|
+
try {
|
|
33
|
+
body = raw ? JSON.parse(raw) : null;
|
|
34
|
+
} catch {
|
|
35
|
+
return send(400, { error: "That isn't JSON." });
|
|
36
|
+
}
|
|
37
|
+
if (new URL(req.url, "http://localhost").searchParams.has("claude")) {
|
|
38
|
+
// The editor's Claude chat (claude.js).
|
|
39
|
+
const not = claudeRefusal((name) => req.headers[name]);
|
|
40
|
+
if (not) return send(403, { error: not });
|
|
41
|
+
const { status, body: out } = await handleClaude(chats, req.method, req.url, body);
|
|
42
|
+
return send(status, out);
|
|
43
|
+
}
|
|
44
|
+
const { status, body: out } = await handle(store, req.method, req.url, body);
|
|
45
|
+
send(status, out);
|
|
46
|
+
});
|
|
47
|
+
},
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export default blockoutNotes;
|