@bigmistqke/whiteboard 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 +21 -0
- package/README.md +628 -2
- package/bin/whiteboard.ts +488 -0
- package/dist/_virtual/_@oxc-project_runtime@0.156.0/helpers/esm/taggedTemplateLiteral.js +5 -0
- package/dist/bin/whiteboard.js +542 -0
- package/dist/client/runtime.js +544 -0
- package/dist/define.js +7 -0
- package/dist/plugins/callout/README.md +13 -0
- package/dist/plugins/callout/index.js +27 -0
- package/dist/plugins/callout/style.css +12 -0
- package/dist/plugins/chat/README.md +46 -0
- package/dist/plugins/chat/client.js +3 -0
- package/dist/plugins/chat/index.js +47 -0
- package/dist/plugins/chat/style.css +24 -0
- package/dist/plugins/claude-code/index.js +120 -0
- package/dist/plugins/code/README.md +42 -0
- package/dist/plugins/code/diff.js +65 -0
- package/dist/plugins/code/index.js +77 -0
- package/dist/plugins/code/style.css +29 -0
- package/dist/plugins/comments/client.js +10 -0
- package/dist/plugins/comments/index.js +54 -0
- package/dist/plugins/comments/style.css +40 -0
- package/dist/plugins/details/README.md +17 -0
- package/dist/plugins/details/index.js +17 -0
- package/dist/plugins/details/style.css +3 -0
- package/dist/plugins/forge/README.md +12 -0
- package/dist/plugins/forge/index.js +83 -0
- package/dist/plugins/form/README.md +111 -0
- package/dist/plugins/form/index.js +102 -0
- package/dist/plugins/form/style.css +41 -0
- package/dist/plugins/gallery/README.md +30 -0
- package/dist/plugins/gallery/client.js +1 -0
- package/dist/plugins/gallery/index.js +74 -0
- package/dist/plugins/gallery/style.css +27 -0
- package/dist/plugins/grid/README.md +33 -0
- package/dist/plugins/grid/index.js +33 -0
- package/dist/plugins/grid/style.css +25 -0
- package/dist/plugins/iframe/README.md +18 -0
- package/dist/plugins/iframe/client.js +1 -0
- package/dist/plugins/iframe/index.js +40 -0
- package/dist/plugins/iframe/style.css +29 -0
- package/dist/plugins/index.js +30 -0
- package/dist/plugins/mermaid/README.md +21 -0
- package/dist/plugins/mermaid/index.js +35 -0
- package/dist/plugins/mermaid/style.css +16 -0
- package/dist/plugins/quiet/README.md +26 -0
- package/dist/plugins/quiet/index.js +17 -0
- package/dist/plugins/terminal/client.js +41 -0
- package/dist/plugins/terminal/host.js +332 -0
- package/dist/plugins/terminal/index.js +171 -0
- package/dist/plugins/terminal/modes.js +32 -0
- package/dist/plugins/terminal/server.js +256 -0
- package/dist/plugins/terminal/sizes.js +57 -0
- package/dist/plugins/terminal/style.css +357 -0
- package/dist/solid/index.js +36 -0
- package/dist/src/auth.js +54 -0
- package/dist/src/client/page/base.css +295 -0
- package/dist/src/client/page/main.js +1228 -0
- package/dist/src/config.js +76 -0
- package/dist/src/events.js +183 -0
- package/dist/src/html.js +34 -0
- package/dist/src/icons.js +6 -0
- package/dist/src/load.js +77 -0
- package/dist/src/loader.js +126 -0
- package/dist/src/package.js +33 -0
- package/dist/src/pages/blocks.js +50 -0
- package/dist/src/pages/index.js +15 -0
- package/dist/src/pages/markdown.js +410 -0
- package/dist/src/pages/render.js +311 -0
- package/dist/src/plugins.js +238 -0
- package/dist/src/running.js +24 -0
- package/dist/src/serve.js +71 -0
- package/dist/src/server.js +725 -0
- package/dist/src/status.js +71 -0
- package/package.json +131 -4
- package/plugins/callout/README.md +13 -0
- package/plugins/callout/index.ts +25 -0
- package/plugins/callout/style.css +12 -0
- package/plugins/chat/README.md +46 -0
- package/plugins/chat/autosize.ts +101 -0
- package/plugins/chat/client.tsx +93 -0
- package/plugins/chat/index.ts +61 -0
- package/plugins/chat/style.css +24 -0
- package/plugins/claude-code/index.ts +136 -0
- package/plugins/code/README.md +42 -0
- package/plugins/code/diff.ts +64 -0
- package/plugins/code/index.ts +94 -0
- package/plugins/code/style.css +29 -0
- package/plugins/comments/client.ts +492 -0
- package/plugins/comments/index.ts +54 -0
- package/plugins/comments/style.css +40 -0
- package/plugins/details/README.md +17 -0
- package/plugins/details/index.ts +21 -0
- package/plugins/details/style.css +3 -0
- package/plugins/forge/README.md +12 -0
- package/plugins/forge/index.ts +71 -0
- package/plugins/form/README.md +111 -0
- package/plugins/form/index.ts +104 -0
- package/plugins/form/style.css +41 -0
- package/plugins/gallery/README.md +30 -0
- package/plugins/gallery/client.tsx +64 -0
- package/plugins/gallery/index.ts +67 -0
- package/plugins/gallery/style.css +27 -0
- package/plugins/grid/README.md +33 -0
- package/plugins/grid/index.ts +36 -0
- package/plugins/grid/style.css +25 -0
- package/plugins/iframe/README.md +18 -0
- package/plugins/iframe/client.ts +46 -0
- package/plugins/iframe/index.ts +54 -0
- package/plugins/iframe/style.css +29 -0
- package/plugins/index.ts +21 -0
- package/plugins/mermaid/README.md +21 -0
- package/plugins/mermaid/index.ts +40 -0
- package/plugins/mermaid/style.css +16 -0
- package/plugins/quiet/README.md +26 -0
- package/plugins/quiet/index.ts +27 -0
- package/plugins/terminal/client.ts +317 -0
- package/plugins/terminal/host.ts +326 -0
- package/plugins/terminal/index.ts +138 -0
- package/plugins/terminal/modes.ts +30 -0
- package/plugins/terminal/server.ts +215 -0
- package/plugins/terminal/sizes.ts +60 -0
- package/plugins/terminal/style.css +71 -0
- package/skills/whiteboard/SKILL.md +302 -0
- package/solid/index.tsx +59 -0
- package/src/auth.ts +66 -0
- package/src/client/forms.ts +185 -0
- package/src/client/markdown.ts +36 -0
- package/src/client/page/base.css +295 -0
- package/src/client/page/elsewhere.ts +70 -0
- package/src/client/page/fresh.ts +205 -0
- package/src/client/page/live.ts +30 -0
- package/src/client/page/main.ts +272 -0
- package/src/client/page/orphans.ts +46 -0
- package/src/client/page/sort.ts +38 -0
- package/src/client/page/theme.ts +32 -0
- package/src/client/page/view.ts +9 -0
- package/src/client/page/waiting.ts +182 -0
- package/src/client/read.ts +73 -0
- package/src/client/runtime.ts +183 -0
- package/src/client/toast.ts +25 -0
- package/src/config.ts +96 -0
- package/src/define.ts +612 -0
- package/src/events.ts +218 -0
- package/src/html.ts +40 -0
- package/src/icons.ts +8 -0
- package/src/load.ts +89 -0
- package/src/loader.ts +112 -0
- package/src/package.ts +39 -0
- package/src/pages/blocks.ts +70 -0
- package/src/pages/index.ts +42 -0
- package/src/pages/markdown.ts +364 -0
- package/src/pages/render.ts +233 -0
- package/src/plugins.ts +197 -0
- package/src/running.ts +22 -0
- package/src/serve.ts +52 -0
- package/src/server.ts +667 -0
- package/src/status.ts +113 -0
|
@@ -0,0 +1,544 @@
|
|
|
1
|
+
// src/html.ts
|
|
2
|
+
/** A piece of HTML that is not escaped again. `String(piece)` is its markup. */
|
|
3
|
+
var Html = class {
|
|
4
|
+
value;
|
|
5
|
+
constructor(value) {
|
|
6
|
+
this.value = value;
|
|
7
|
+
}
|
|
8
|
+
toString() {
|
|
9
|
+
return this.value;
|
|
10
|
+
}
|
|
11
|
+
};
|
|
12
|
+
const ESCAPE = {
|
|
13
|
+
"&": "&",
|
|
14
|
+
"<": "<",
|
|
15
|
+
">": ">",
|
|
16
|
+
"\"": """,
|
|
17
|
+
"'": "'"
|
|
18
|
+
};
|
|
19
|
+
/** Text → HTML that shows it as text. */
|
|
20
|
+
const escape = (value) => String(value).replace(/[&<>"']/g, (c) => ESCAPE[c]);
|
|
21
|
+
function part(value) {
|
|
22
|
+
if (value == null || value === false) return "";
|
|
23
|
+
if (value instanceof Html) return value.value;
|
|
24
|
+
if (Array.isArray(value)) return value.map(part).join("");
|
|
25
|
+
return escape(value);
|
|
26
|
+
}
|
|
27
|
+
function html(strings, ...values) {
|
|
28
|
+
let out = strings[0];
|
|
29
|
+
for (let i = 0; i < values.length; i++) out += part(values[i]) + strings[i + 1];
|
|
30
|
+
return new Html(out);
|
|
31
|
+
}
|
|
32
|
+
// src/status.ts
|
|
33
|
+
/**
|
|
34
|
+
* Can this be a question's name, the type of its answers? Not with a colon: types with one belong
|
|
35
|
+
* to whiteboard (`whiteboard:picked`) or a plugin (`comments:reply`, `chat:message`),
|
|
36
|
+
* named after its owner, so a form's answers never mix with their events.
|
|
37
|
+
*/
|
|
38
|
+
const isQuestionName = (name) => !!name && !name.includes(":");
|
|
39
|
+
/** The states, in the order a reader should see them: what waits on them first. */
|
|
40
|
+
const STATES = [
|
|
41
|
+
"open",
|
|
42
|
+
"pending",
|
|
43
|
+
"decided"
|
|
44
|
+
];
|
|
45
|
+
/**
|
|
46
|
+
* Was this event written by the reader (a form submit, a comment), not by an agent? Agents write
|
|
47
|
+
* through the command line, which marks its events `from: 'cli'`: an agent's reply, a claim, a pick.
|
|
48
|
+
*/
|
|
49
|
+
const fromReader = (event) => event.from !== "cli";
|
|
50
|
+
/**
|
|
51
|
+
* Where the question `name` stands. `events` are the page's, `listeners` the running waits and
|
|
52
|
+
* watches that cover the page (only one asking for it by name counts: it asks again),
|
|
53
|
+
* `pickedUp` whether an event was handed to an agent. `last` is its latest answer: the reader's
|
|
54
|
+
* latest event of its name.
|
|
55
|
+
*/
|
|
56
|
+
function questionState(name, events, listeners, pickedUp) {
|
|
57
|
+
const last = events.findLast((e) => e.type === name && fromReader(e));
|
|
58
|
+
const askedAgain = listeners.some((l) => !l.fallback && l.type === name);
|
|
59
|
+
return {
|
|
60
|
+
state: !last ? "open" : !pickedUp(last.id) ? "pending" : askedAgain ? "open" : "decided",
|
|
61
|
+
last
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
/** The type of the event the page appends once messages were in view: `{ ids }`. */
|
|
65
|
+
const READ = "whiteboard:read";
|
|
66
|
+
/**
|
|
67
|
+
* The agent's messages on a page the reader hasn't read: events from the agent of a type
|
|
68
|
+
* `isMessage` says is for the reader, not named by a `whiteboard:read` from the reader, and not
|
|
69
|
+
* replaced by a later one (`replaces`).
|
|
70
|
+
*/
|
|
71
|
+
function unreadMessages(events, isMessage) {
|
|
72
|
+
const read = /* @__PURE__ */ new Set();
|
|
73
|
+
const replaced = /* @__PURE__ */ new Set();
|
|
74
|
+
for (const e of events) {
|
|
75
|
+
if (e.type === "whiteboard:read" && fromReader(e)) for (const id of e.data?.ids ?? []) read.add(String(id));
|
|
76
|
+
if (e.data?.replaces) replaced.add(String(e.data.replaces));
|
|
77
|
+
}
|
|
78
|
+
return events.filter((e) => !fromReader(e) && isMessage(e.type) && !read.has(e.id) && !replaced.has(e.id));
|
|
79
|
+
}
|
|
80
|
+
// src/client/forms.ts
|
|
81
|
+
let root$1;
|
|
82
|
+
let api$1;
|
|
83
|
+
let page$1 = "";
|
|
84
|
+
const listeners$1 = /* @__PURE__ */ new Set();
|
|
85
|
+
/** The forms the core looks after: named (a name without a colon: see isQuestionName), without an action. */
|
|
86
|
+
const managed = (form) => isQuestionName(form.getAttribute("name") ?? "") && !form.hasAttribute("action");
|
|
87
|
+
const forms = () => [...root$1.querySelectorAll("form")].filter(managed);
|
|
88
|
+
const keepsOwnFields = (form) => !!form.closest("[data-wb-self]");
|
|
89
|
+
function readForm(form) {
|
|
90
|
+
const data = {};
|
|
91
|
+
const elements = [...form.elements];
|
|
92
|
+
const boxes = {};
|
|
93
|
+
for (const el of elements) if (el.name && el.type === "checkbox") boxes[el.name] = (boxes[el.name] ?? 0) + 1;
|
|
94
|
+
for (const el of elements) {
|
|
95
|
+
if (!el.name || el.type === "submit" || el.type === "button" || el.type === "reset" || el.type === "file") continue;
|
|
96
|
+
if (el.type === "checkbox") {
|
|
97
|
+
if (boxes[el.name] > 1) {
|
|
98
|
+
const list = data[el.name] ??= [];
|
|
99
|
+
if (el.checked) list.push(el.value);
|
|
100
|
+
} else data[el.name] = el.checked;
|
|
101
|
+
} else if (el.type === "radio") {
|
|
102
|
+
if (el.checked) data[el.name] = el.value;
|
|
103
|
+
else data[el.name] ??= null;
|
|
104
|
+
} else if (el instanceof HTMLSelectElement && el.multiple) data[el.name] = [...el.selectedOptions].map((o) => o.value);
|
|
105
|
+
else data[el.name] = el.value;
|
|
106
|
+
}
|
|
107
|
+
return data;
|
|
108
|
+
}
|
|
109
|
+
function writeForm(form, data) {
|
|
110
|
+
for (const el of [...form.elements]) {
|
|
111
|
+
if (!el.name || !(el.name in data) || [
|
|
112
|
+
"submit",
|
|
113
|
+
"button",
|
|
114
|
+
"hidden",
|
|
115
|
+
"file"
|
|
116
|
+
].includes(el.type)) continue;
|
|
117
|
+
const v = data[el.name];
|
|
118
|
+
if (el.type === "checkbox") el.checked = Array.isArray(v) ? v.includes(el.value) : !!v;
|
|
119
|
+
else if (el.type === "radio") el.checked = v === el.value;
|
|
120
|
+
else el.value = v == null ? "" : String(v);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
const draftKey = (form) => `wb-draft:${page$1}:${form.getAttribute("name")}`;
|
|
124
|
+
function saveDraft(e) {
|
|
125
|
+
const form = e.target?.closest?.("form");
|
|
126
|
+
if (!form || !managed(form) || !root$1.contains(form) || keepsOwnFields(form)) return;
|
|
127
|
+
try {
|
|
128
|
+
localStorage.setItem(draftKey(form), JSON.stringify(readForm(form)));
|
|
129
|
+
} catch {}
|
|
130
|
+
}
|
|
131
|
+
/** Unsent drafts back into their forms; a form being typed in is left alone. */
|
|
132
|
+
function restoreDrafts() {
|
|
133
|
+
for (const form of forms()) {
|
|
134
|
+
if (keepsOwnFields(form) || form.contains(document.activeElement)) continue;
|
|
135
|
+
let draft = null;
|
|
136
|
+
try {
|
|
137
|
+
draft = JSON.parse(localStorage.getItem(draftKey(form)) ?? "null");
|
|
138
|
+
} catch {}
|
|
139
|
+
if (draft) writeForm(form, draft);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
const dropDraft = (form) => {
|
|
143
|
+
try {
|
|
144
|
+
localStorage.removeItem(draftKey(form));
|
|
145
|
+
} catch {}
|
|
146
|
+
};
|
|
147
|
+
/** Each form's last answer: its button in data-wb-sent, and with `prefill` its fields. */
|
|
148
|
+
function restoreLast(prefill) {
|
|
149
|
+
for (const form of forms()) {
|
|
150
|
+
const last = api$1.last(form.getAttribute("name"));
|
|
151
|
+
if (!last) continue;
|
|
152
|
+
form.dataset.wbSent = last.action ?? "";
|
|
153
|
+
if (prefill && !keepsOwnFields(form)) writeForm(form, last.data);
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
async function onSubmit(e) {
|
|
157
|
+
const form = e.target;
|
|
158
|
+
if (!managed(form) || !root$1.contains(form)) return;
|
|
159
|
+
e.preventDefault();
|
|
160
|
+
const button = e.submitter;
|
|
161
|
+
const action = button ? button.value || button.textContent?.trim() || null : null;
|
|
162
|
+
form.dataset.wbState = "sending";
|
|
163
|
+
try {
|
|
164
|
+
await api$1.append(form.getAttribute("name"), readForm(form), action);
|
|
165
|
+
dropDraft(form);
|
|
166
|
+
form.dataset.wbState = "sent";
|
|
167
|
+
form.dataset.wbSent = action ?? "";
|
|
168
|
+
form.toggleAttribute("data-wb-just-sent", true);
|
|
169
|
+
clearTimeout(justSent.get(form));
|
|
170
|
+
justSent.set(form, setTimeout(() => form.removeAttribute("data-wb-just-sent"), 3e3));
|
|
171
|
+
} catch (err) {
|
|
172
|
+
form.dataset.wbState = "failed";
|
|
173
|
+
console.error(err);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
const ATTR = {
|
|
177
|
+
open: "data-wb-open",
|
|
178
|
+
pending: "data-wb-pending",
|
|
179
|
+
decided: "data-wb-decided"
|
|
180
|
+
};
|
|
181
|
+
/** One line quoting an answer: what was chosen, then what was written. */
|
|
182
|
+
function outcome(data, action) {
|
|
183
|
+
const chosen = [], written = [];
|
|
184
|
+
for (const v of Object.values(data)) {
|
|
185
|
+
if (v == null || v === "" || v === false) continue;
|
|
186
|
+
if (Array.isArray(v)) {
|
|
187
|
+
if (v.length) chosen.push(v.join(", "));
|
|
188
|
+
} else if (v === true) continue;
|
|
189
|
+
else if (String(v).length > 60 || String(v).includes("\n")) written.push(String(v));
|
|
190
|
+
else chosen.push(String(v));
|
|
191
|
+
}
|
|
192
|
+
if (action && !["Send", "Submit"].includes(action)) chosen.unshift(action);
|
|
193
|
+
return [chosen.join(" · ") || "sent", ...written.map((w) => `“${w.length > 240 ? w.slice(0, 240) + "…" : w}”`)].join("\n");
|
|
194
|
+
}
|
|
195
|
+
let current = [];
|
|
196
|
+
const justSent = /* @__PURE__ */ new WeakMap();
|
|
197
|
+
/** Mark every form from the log and the listeners, then tell whoever follows. */
|
|
198
|
+
function mark() {
|
|
199
|
+
const listening = api$1.listeners();
|
|
200
|
+
current = forms().map((form) => {
|
|
201
|
+
const name = form.getAttribute("name");
|
|
202
|
+
const { state, last } = questionState(name, api$1.events(), listening, api$1.pickedUp);
|
|
203
|
+
for (const st of STATES) form.toggleAttribute(ATTR[st], st === state);
|
|
204
|
+
if (state === "decided") form.dataset.wbOutcome = outcome(last.data, last.action);
|
|
205
|
+
else delete form.dataset.wbOutcome;
|
|
206
|
+
return {
|
|
207
|
+
form,
|
|
208
|
+
name,
|
|
209
|
+
state,
|
|
210
|
+
last
|
|
211
|
+
};
|
|
212
|
+
});
|
|
213
|
+
for (const fn of listeners$1) fn(current);
|
|
214
|
+
}
|
|
215
|
+
/** After the page re-rendered: answers, drafts and marks back on its forms. */
|
|
216
|
+
function refresh() {
|
|
217
|
+
restoreLast(false);
|
|
218
|
+
restoreDrafts();
|
|
219
|
+
mark();
|
|
220
|
+
}
|
|
221
|
+
/** Look after the page's named forms; called once the page has mounted. */
|
|
222
|
+
function trackForms(el, pageId, core) {
|
|
223
|
+
root$1 = el;
|
|
224
|
+
page$1 = pageId;
|
|
225
|
+
api$1 = core;
|
|
226
|
+
document.addEventListener("input", saveDraft);
|
|
227
|
+
document.addEventListener("change", saveDraft);
|
|
228
|
+
document.addEventListener("submit", onSubmit);
|
|
229
|
+
api$1.onRendered(refresh);
|
|
230
|
+
api$1.onListeners(mark);
|
|
231
|
+
api$1.onEvents(() => {
|
|
232
|
+
restoreLast(false);
|
|
233
|
+
mark();
|
|
234
|
+
});
|
|
235
|
+
restoreLast(true);
|
|
236
|
+
restoreDrafts();
|
|
237
|
+
mark();
|
|
238
|
+
}
|
|
239
|
+
/** For the api: every named form and where it stands, and following that. */
|
|
240
|
+
const formsApi = {
|
|
241
|
+
forms: () => current,
|
|
242
|
+
onForms(fn) {
|
|
243
|
+
listeners$1.add(fn);
|
|
244
|
+
return () => {
|
|
245
|
+
listeners$1.delete(fn);
|
|
246
|
+
};
|
|
247
|
+
}
|
|
248
|
+
};
|
|
249
|
+
// src/client/toast.ts
|
|
250
|
+
const box = document.createElement("div");
|
|
251
|
+
box.id = "wb-toasts";
|
|
252
|
+
box.setAttribute("role", "status");
|
|
253
|
+
box.setAttribute("aria-live", "polite");
|
|
254
|
+
function toast(text, { kind = "info", ms = 7e3, onClick } = {}) {
|
|
255
|
+
if (!box.isConnected) document.body.append(box);
|
|
256
|
+
const el = document.createElement("div");
|
|
257
|
+
el.className = "wb-toast";
|
|
258
|
+
el.dataset.kind = kind;
|
|
259
|
+
if (onClick) el.dataset.action = "";
|
|
260
|
+
el.textContent = text;
|
|
261
|
+
const close = () => el.remove();
|
|
262
|
+
el.addEventListener("click", () => {
|
|
263
|
+
if (ms) close();
|
|
264
|
+
onClick?.();
|
|
265
|
+
});
|
|
266
|
+
if (ms) setTimeout(close, ms);
|
|
267
|
+
box.append(el);
|
|
268
|
+
return {
|
|
269
|
+
close,
|
|
270
|
+
update: (next) => {
|
|
271
|
+
el.textContent = next;
|
|
272
|
+
}
|
|
273
|
+
};
|
|
274
|
+
}
|
|
275
|
+
// src/client/read.ts
|
|
276
|
+
let isMessage = () => false;
|
|
277
|
+
let getEvents = () => [];
|
|
278
|
+
let send = () => {};
|
|
279
|
+
const sending = /* @__PURE__ */ new Set();
|
|
280
|
+
const unread = () => unreadMessages(getEvents(), isMessage).filter((e) => !sending.has(e.id));
|
|
281
|
+
const queued = /* @__PURE__ */ new Set();
|
|
282
|
+
let timer = 0;
|
|
283
|
+
function markRead(ids) {
|
|
284
|
+
const open = new Set(unread().map((e) => e.id));
|
|
285
|
+
const fresh = ids.filter((id) => open.has(id));
|
|
286
|
+
if (!fresh.length) return;
|
|
287
|
+
for (const id of fresh) {
|
|
288
|
+
sending.add(id);
|
|
289
|
+
queued.add(id);
|
|
290
|
+
}
|
|
291
|
+
clearTimeout(timer);
|
|
292
|
+
timer = window.setTimeout(() => {
|
|
293
|
+
const batch = [...queued];
|
|
294
|
+
queued.clear();
|
|
295
|
+
if (batch.length) send(batch);
|
|
296
|
+
}, 300);
|
|
297
|
+
}
|
|
298
|
+
/** Once the log has the reads, they need no holding. */
|
|
299
|
+
function readsArrived(events) {
|
|
300
|
+
for (const e of events) if (e.type === "whiteboard:read") for (const id of e.data?.ids ?? []) sending.delete(String(id));
|
|
301
|
+
}
|
|
302
|
+
const inView = /* @__PURE__ */ new Set();
|
|
303
|
+
const seen = new IntersectionObserver((entries) => {
|
|
304
|
+
for (const entry of entries) {
|
|
305
|
+
const id = entry.target.dataset.wbMessage;
|
|
306
|
+
if (entry.isIntersecting) inView.add(id);
|
|
307
|
+
else inView.delete(id);
|
|
308
|
+
}
|
|
309
|
+
readVisible();
|
|
310
|
+
}, { threshold: .6 });
|
|
311
|
+
function readVisible() {
|
|
312
|
+
if (document.visibilityState === "visible" && inView.size) markRead([...inView]);
|
|
313
|
+
}
|
|
314
|
+
const observed = /* @__PURE__ */ new WeakSet();
|
|
315
|
+
function observeMessages() {
|
|
316
|
+
const open = new Set(unread().map((e) => e.id));
|
|
317
|
+
for (const el of document.querySelectorAll("[data-wb-message]")) {
|
|
318
|
+
if (observed.has(el) || !open.has(el.dataset.wbMessage)) continue;
|
|
319
|
+
observed.add(el);
|
|
320
|
+
seen.observe(el);
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
function trackReading(options) {
|
|
324
|
+
const types = new Set(options.messages);
|
|
325
|
+
isMessage = (type) => types.has(type);
|
|
326
|
+
getEvents = options.events;
|
|
327
|
+
send = options.send;
|
|
328
|
+
new MutationObserver(observeMessages).observe(document.body, {
|
|
329
|
+
childList: true,
|
|
330
|
+
subtree: true
|
|
331
|
+
});
|
|
332
|
+
document.addEventListener("visibilitychange", readVisible);
|
|
333
|
+
observeMessages();
|
|
334
|
+
}
|
|
335
|
+
/** After the events changed: messages that came since may already be on screen. */
|
|
336
|
+
const messagesChanged = () => observeMessages();
|
|
337
|
+
// src/client/markdown.ts
|
|
338
|
+
const cache = /* @__PURE__ */ new Map();
|
|
339
|
+
let queue = [];
|
|
340
|
+
async function flush() {
|
|
341
|
+
const batch = queue;
|
|
342
|
+
queue = [];
|
|
343
|
+
try {
|
|
344
|
+
const res = await fetch("/api/markdown", {
|
|
345
|
+
method: "POST",
|
|
346
|
+
headers: { "content-type": "application/json" },
|
|
347
|
+
body: JSON.stringify({ texts: batch.map((b) => b.text) })
|
|
348
|
+
});
|
|
349
|
+
if (!res.ok) throw new Error(await res.text());
|
|
350
|
+
const { html, styles } = await res.json();
|
|
351
|
+
for (const href of styles) if (![...document.querySelectorAll("link[rel=stylesheet]")].some((l) => new URL(l.href).pathname === new URL(href, location.href).pathname)) document.head.insertAdjacentHTML("beforeend", `<link rel="stylesheet" href="${href.replace(/"/g, """)}">`);
|
|
352
|
+
batch.forEach((b, i) => b.resolve(html[i] ?? ""));
|
|
353
|
+
} catch (err) {
|
|
354
|
+
for (const b of batch) {
|
|
355
|
+
cache.delete(b.text);
|
|
356
|
+
b.reject(err);
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
function markdown(text) {
|
|
361
|
+
let got = cache.get(text);
|
|
362
|
+
if (!got) {
|
|
363
|
+
got = new Promise((resolve, reject) => {
|
|
364
|
+
if (!queue.length) queueMicrotask(flush);
|
|
365
|
+
queue.push({
|
|
366
|
+
text,
|
|
367
|
+
resolve,
|
|
368
|
+
reject
|
|
369
|
+
});
|
|
370
|
+
});
|
|
371
|
+
cache.set(text, got);
|
|
372
|
+
}
|
|
373
|
+
return got;
|
|
374
|
+
}
|
|
375
|
+
// src/client/runtime.ts
|
|
376
|
+
const handoff = JSON.parse(document.getElementById("wb-page").textContent);
|
|
377
|
+
const root = document.getElementById("wb-root");
|
|
378
|
+
const page = handoff.id;
|
|
379
|
+
const isDir = handoff.kind === "pages" || handoff.kind === "folder";
|
|
380
|
+
let events = [];
|
|
381
|
+
const eventListeners = /* @__PURE__ */ new Set();
|
|
382
|
+
function setEvents(next) {
|
|
383
|
+
events = next;
|
|
384
|
+
readsArrived(events);
|
|
385
|
+
for (const fn of eventListeners) fn(events);
|
|
386
|
+
messagesChanged();
|
|
387
|
+
}
|
|
388
|
+
async function loadEvents() {
|
|
389
|
+
setEvents((await (await fetch(`/api/events?page=${encodeURIComponent(page)}`)).json()).filter((e) => e.page === page));
|
|
390
|
+
}
|
|
391
|
+
async function append(type, data = {}, action = null) {
|
|
392
|
+
const res = await fetch("/api/events", {
|
|
393
|
+
method: "POST",
|
|
394
|
+
headers: { "content-type": "application/json" },
|
|
395
|
+
body: JSON.stringify({
|
|
396
|
+
page,
|
|
397
|
+
type,
|
|
398
|
+
action,
|
|
399
|
+
data
|
|
400
|
+
})
|
|
401
|
+
});
|
|
402
|
+
if (!res.ok) throw new Error(await res.text());
|
|
403
|
+
const saved = await res.json();
|
|
404
|
+
if (!events.some((e) => e.id === saved.id)) setEvents([...events, saved]);
|
|
405
|
+
return saved;
|
|
406
|
+
}
|
|
407
|
+
let listeners = [];
|
|
408
|
+
const listenerListeners = /* @__PURE__ */ new Set();
|
|
409
|
+
async function loadListeners() {
|
|
410
|
+
listeners = await (await fetch(`/api/listeners?page=${encodeURIComponent(page)}`)).json();
|
|
411
|
+
for (const fn of listenerListeners) fn(listeners);
|
|
412
|
+
}
|
|
413
|
+
const renderedListeners = /* @__PURE__ */ new Set();
|
|
414
|
+
const corner = document.getElementById("wb-dock") ?? document.body.appendChild(Object.assign(document.createElement("div"), { id: "wb-dock" }));
|
|
415
|
+
let baseTitle = document.title;
|
|
416
|
+
const badges = /* @__PURE__ */ new Map();
|
|
417
|
+
let badgeCount = 0;
|
|
418
|
+
function writeTitle() {
|
|
419
|
+
const list = [...badges.values()].sort((a, b) => a.order - b.order || a.since - b.since);
|
|
420
|
+
document.title = `${list.map((b) => `(${b.text}) `).join("")}${baseTitle}`;
|
|
421
|
+
}
|
|
422
|
+
const api = {
|
|
423
|
+
page,
|
|
424
|
+
events: () => events,
|
|
425
|
+
onEvents(fn) {
|
|
426
|
+
eventListeners.add(fn);
|
|
427
|
+
return () => eventListeners.delete(fn);
|
|
428
|
+
},
|
|
429
|
+
last: (type) => events.findLast((e) => e.type === type),
|
|
430
|
+
pickedUp: (id) => !!events.find((e) => e.id === id)?.pickedUp,
|
|
431
|
+
unread,
|
|
432
|
+
markRead,
|
|
433
|
+
markdown,
|
|
434
|
+
append,
|
|
435
|
+
listeners: () => listeners,
|
|
436
|
+
onListeners(fn) {
|
|
437
|
+
listenerListeners.add(fn);
|
|
438
|
+
return () => listenerListeners.delete(fn);
|
|
439
|
+
},
|
|
440
|
+
rendered() {
|
|
441
|
+
for (const fn of renderedListeners) fn();
|
|
442
|
+
},
|
|
443
|
+
onRendered(fn) {
|
|
444
|
+
renderedListeners.add(fn);
|
|
445
|
+
return () => renderedListeners.delete(fn);
|
|
446
|
+
},
|
|
447
|
+
corner: () => corner,
|
|
448
|
+
badge(key, text, order = 0) {
|
|
449
|
+
if (text == null) badges.delete(key);
|
|
450
|
+
else badges.set(key, {
|
|
451
|
+
text,
|
|
452
|
+
order,
|
|
453
|
+
since: badges.get(key)?.since ?? badgeCount++
|
|
454
|
+
});
|
|
455
|
+
writeTitle();
|
|
456
|
+
},
|
|
457
|
+
title(title) {
|
|
458
|
+
baseTitle = title;
|
|
459
|
+
writeTitle();
|
|
460
|
+
},
|
|
461
|
+
toast,
|
|
462
|
+
...formsApi
|
|
463
|
+
};
|
|
464
|
+
const changeListeners = /* @__PURE__ */ new Set();
|
|
465
|
+
async function getJson(url) {
|
|
466
|
+
const res = await fetch(url);
|
|
467
|
+
if (!res.ok) throw new Error(`${url}: ${res.status}`);
|
|
468
|
+
return res.json();
|
|
469
|
+
}
|
|
470
|
+
const handle = {
|
|
471
|
+
id: page,
|
|
472
|
+
kind: handoff.kind,
|
|
473
|
+
data: handoff.data,
|
|
474
|
+
onChange(fn) {
|
|
475
|
+
changeListeners.add(fn);
|
|
476
|
+
return () => changeListeners.delete(fn);
|
|
477
|
+
},
|
|
478
|
+
render: () => getJson(`/api/render?page=${encodeURIComponent(page)}`),
|
|
479
|
+
stored: () => getJson(`/api/page?page=${encodeURIComponent(page)}`)
|
|
480
|
+
};
|
|
481
|
+
if (!handoff.client) handle.onChange(async () => {
|
|
482
|
+
root.innerHTML = (await handle.render()).html;
|
|
483
|
+
api.rendered();
|
|
484
|
+
});
|
|
485
|
+
function connect() {
|
|
486
|
+
const es = new EventSource(`/api/watch?page=${encodeURIComponent(page)}${isDir ? "&dir" : ""}`);
|
|
487
|
+
es.onopen = () => {
|
|
488
|
+
document.documentElement.dataset.wbLive = "";
|
|
489
|
+
loadListeners();
|
|
490
|
+
loadEvents();
|
|
491
|
+
};
|
|
492
|
+
es.onerror = () => {
|
|
493
|
+
delete document.documentElement.dataset.wbLive;
|
|
494
|
+
};
|
|
495
|
+
es.addEventListener("change", () => {
|
|
496
|
+
for (const fn of changeListeners) fn();
|
|
497
|
+
});
|
|
498
|
+
es.addEventListener("events", () => loadEvents());
|
|
499
|
+
es.addEventListener("listeners", () => loadListeners());
|
|
500
|
+
es.addEventListener("reload", () => {
|
|
501
|
+
const go = async () => {
|
|
502
|
+
try {
|
|
503
|
+
if ((await fetch("/api/health", { cache: "no-store" })).ok) return location.reload();
|
|
504
|
+
} catch {}
|
|
505
|
+
setTimeout(go, 500);
|
|
506
|
+
};
|
|
507
|
+
go();
|
|
508
|
+
});
|
|
509
|
+
es.addEventListener("style", () => {
|
|
510
|
+
for (const link of document.querySelectorAll("link[rel=stylesheet]")) {
|
|
511
|
+
const url = new URL(link.href);
|
|
512
|
+
if (url.origin !== location.origin) continue;
|
|
513
|
+
url.searchParams.set("t", String(Date.now()));
|
|
514
|
+
link.href = url.pathname + url.search;
|
|
515
|
+
}
|
|
516
|
+
});
|
|
517
|
+
}
|
|
518
|
+
await Promise.all([loadEvents(), loadListeners()]);
|
|
519
|
+
if (handoff.client) try {
|
|
520
|
+
const mount = (await import(handoff.client)).default;
|
|
521
|
+
await mount(root, handle, api);
|
|
522
|
+
} catch (err) {
|
|
523
|
+
console.error("page:", err);
|
|
524
|
+
root.insertAdjacentHTML("afterbegin", String(html`<pre class="wb-error" role="alert">the page failed to start: ${err.message}</pre>`));
|
|
525
|
+
}
|
|
526
|
+
trackForms(root, page, api);
|
|
527
|
+
trackReading({
|
|
528
|
+
messages: handoff.messages ?? [],
|
|
529
|
+
events: () => events,
|
|
530
|
+
send: (ids) => {
|
|
531
|
+
append(READ, { ids }).catch((err) => console.error("marking read:", err));
|
|
532
|
+
}
|
|
533
|
+
});
|
|
534
|
+
await Promise.all((handoff.plugins ?? []).map(async (ext) => {
|
|
535
|
+
try {
|
|
536
|
+
const mount = (await import(ext.client)).default;
|
|
537
|
+
if (typeof mount !== "function") throw new Error("the client module must export a mount(root, page, api) function as default");
|
|
538
|
+
await mount(root, handle, api);
|
|
539
|
+
} catch (err) {
|
|
540
|
+
console.error(`plugin ${ext.name}:`, err);
|
|
541
|
+
root.insertAdjacentHTML("afterbegin", String(html`<pre class="wb-error" role="alert">plugin "${ext.name}" failed in the browser: ${err.message}</pre>`));
|
|
542
|
+
}
|
|
543
|
+
}));
|
|
544
|
+
connect();
|
package/dist/define.js
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { Html, escape, html, raw } from "./src/html.js";
|
|
2
|
+
// src/define.ts
|
|
3
|
+
const defineConfig = (config) => config;
|
|
4
|
+
const definePlugin = (plugin) => plugin;
|
|
5
|
+
/** A block with typed props, for one written apart from its plugin. */
|
|
6
|
+
const defineBlock = (block) => block;
|
|
7
|
+
export { Html, defineBlock, defineConfig, definePlugin, escape, html, raw };
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
A highlighted box with an optional title. Pick the type with `{type=…}` or the
|
|
2
|
+
class shorthand `{.warning}`: `note` (default), `tip`, `success`, `warning`,
|
|
3
|
+
`danger`, `question`.
|
|
4
|
+
|
|
5
|
+
```markdown
|
|
6
|
+
:::callout[Heads up]{.warning}
|
|
7
|
+
Solid 2 is a release candidate; APIs can still move.
|
|
8
|
+
:::
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
:::callout[Heads up]{.warning}
|
|
12
|
+
Solid 2 is a release candidate; APIs can still move.
|
|
13
|
+
:::
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import { definePlugin, html, raw } from "@bigmistqke/whiteboard";
|
|
3
|
+
// plugins/callout/index.ts
|
|
4
|
+
const TYPES = /* @__PURE__ */ new Set([
|
|
5
|
+
"note",
|
|
6
|
+
"tip",
|
|
7
|
+
"success",
|
|
8
|
+
"warning",
|
|
9
|
+
"danger",
|
|
10
|
+
"question"
|
|
11
|
+
]);
|
|
12
|
+
async function render(ctx) {
|
|
13
|
+
const type = [ctx.attrs.type, ...(ctx.attrs.class ?? "").split(/\s+/)].find((t) => t && TYPES.has(t)) ?? "note";
|
|
14
|
+
const title = ctx.label && html`<div class="wb-callout-title">${raw(ctx.label)}</div>`;
|
|
15
|
+
return html`<aside class="wb-callout" data-type="${type}">${title}${raw(await ctx.toHtml(ctx.children))}</aside>`;
|
|
16
|
+
}
|
|
17
|
+
function callout() {
|
|
18
|
+
return definePlugin({
|
|
19
|
+
name: "callout",
|
|
20
|
+
blocks: { callout: {
|
|
21
|
+
html: render,
|
|
22
|
+
style: new URL("./style.css", import.meta.url)
|
|
23
|
+
} },
|
|
24
|
+
docs: () => fs.readFileSync(new URL("./README.md", import.meta.url), "utf8")
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
export { callout as default, render };
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/* a margin note: a coloured rule and a light tint, set in the body face */
|
|
2
|
+
.wb-callout {
|
|
3
|
+
--c: var(--info);
|
|
4
|
+
margin: 3.4em 0; padding: 1em 1.3em; border-left: 3px solid var(--c);
|
|
5
|
+
background: color-mix(in srgb, var(--c) 6%, var(--bg)); border-radius: 0 var(--radius) var(--radius) 0;
|
|
6
|
+
}
|
|
7
|
+
.wb-callout > :first-child { margin-top: 0; }
|
|
8
|
+
.wb-callout > :last-child { margin-bottom: 0; }
|
|
9
|
+
.wb-callout-title { font: 600 .8rem/1.4 var(--sans); color: var(--c); margin-bottom: .5em; letter-spacing: .01em; }
|
|
10
|
+
.wb-callout:is([data-type=tip], [data-type=success]) { --c: var(--ok); }
|
|
11
|
+
.wb-callout:is([data-type=warning], [data-type=question]) { --c: var(--warn); }
|
|
12
|
+
.wb-callout[data-type=danger] { --c: var(--bad); }
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
A chat between the reader and you: the reader types (Enter sends,
|
|
2
|
+
Shift+Enter starts a new line), you answer, the reader's messages on one side
|
|
3
|
+
and yours on the other. Messages are markdown (code, lists, links, `#12`);
|
|
4
|
+
raw HTML and blocks show as written. Every form has one under it, for questions about the
|
|
5
|
+
question; a `chat` block puts one anywhere, for talk that isn't about a form.
|
|
6
|
+
|
|
7
|
+
```markdown
|
|
8
|
+
:::chat{name=notes}
|
|
9
|
+
Anything about the plan that isn't a decision: write it here.
|
|
10
|
+
:::
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
:::chat{name=notes}
|
|
14
|
+
Anything about the plan that isn't a decision: write it here.
|
|
15
|
+
:::
|
|
16
|
+
|
|
17
|
+
What the block says above the chat is ordinary markdown, and optional:
|
|
18
|
+
`::chat{name=page}` alone is a bare chat. Give each chat on a page its own
|
|
19
|
+
name, other than the page's forms' names: a form's chat has the form's name.
|
|
20
|
+
|
|
21
|
+
A message is a `chat:message` event, `{ "chat": "<name>", "text": "…" }`,
|
|
22
|
+
never an answer: under a form it never makes a choice. Answer in the same chat:
|
|
23
|
+
|
|
24
|
+
```sh
|
|
25
|
+
whiteboard append --page <file> --type chat:message --data '{"chat":"<name>","text":"your answer"}'
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
Two more fields, both optional:
|
|
29
|
+
|
|
30
|
+
- `replaces`: the id of an earlier message of yours. Yours takes its place,
|
|
31
|
+
to correct it; the log itself only grows.
|
|
32
|
+
- `replyTo` (in a form's chat): the id of a submit of the form whose written
|
|
33
|
+
text you answer (a remark typed into one of its text fields). That text
|
|
34
|
+
shows as the reader's message, right before yours.
|
|
35
|
+
|
|
36
|
+
Your messages show as the agent's, and `wait` and `watch` skip what you
|
|
37
|
+
append. A wait on the whole page (no `--type`) hands out chat messages too;
|
|
38
|
+
a wait with `--type <form name>` does not. So don't add a free-text field
|
|
39
|
+
for questions or remarks to a decision form: its chat is for that, and a
|
|
40
|
+
submit stays a choice.
|
|
41
|
+
|
|
42
|
+
A chat is found by its name. Remove or rename a form or a chat block and its
|
|
43
|
+
chat moves to the end of the page, read-only, under "Chats no longer on this
|
|
44
|
+
page" (you can still answer there); bring it back under its name and the chat
|
|
45
|
+
is in its place again. So rework a question in its form rather than
|
|
46
|
+
replacing the form.
|