@bigmistqke/whiteboard 0.3.1 → 0.4.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 +67 -17
- package/bin/whiteboard.ts +268 -71
- package/dist/client/runtime.js +88 -41
- package/dist/define.js +21 -2
- package/dist/plugins/callout/index.js +3 -1
- package/dist/plugins/callout/public/style.css +30 -0
- package/dist/plugins/chat/index.js +7 -4
- package/dist/plugins/chat/{client.js → public/client.js} +1 -1
- package/dist/plugins/chat/public/style.css +98 -0
- package/dist/plugins/code/index.js +3 -1
- package/dist/plugins/code/public/style.css +68 -0
- package/dist/plugins/comments/index.js +4 -5
- package/dist/plugins/comments/public/client.js +19 -0
- package/dist/plugins/comments/public/style.css +185 -0
- package/dist/plugins/details/index.js +7 -2
- package/dist/plugins/details/public/style.css +7 -0
- package/dist/plugins/form/index.js +37 -8
- package/dist/plugins/form/public/style.css +126 -0
- package/dist/plugins/gallery/index.js +4 -3
- package/dist/plugins/gallery/public/client.js +1 -0
- package/dist/plugins/gallery/public/style.css +90 -0
- package/dist/plugins/grid/index.js +9 -3
- package/dist/plugins/grid/public/style.css +71 -0
- package/dist/plugins/iframe/index.js +13 -14
- package/dist/plugins/iframe/public/style.css +13 -0
- package/dist/plugins/index.js +3 -1
- package/dist/plugins/mermaid/index.js +3 -1
- package/dist/plugins/mermaid/{style.css → public/style.css} +7 -2
- package/dist/plugins/terminal/index.js +10 -9
- package/dist/plugins/terminal/{style.css → public/style.css} +199 -45
- package/dist/plugins/toc/index.js +4 -3
- package/dist/plugins/toc/public/client.js +1 -0
- package/dist/plugins/toc/public/style.css +93 -0
- package/dist/plugins/window/README.md +29 -0
- package/dist/plugins/window/index.js +64 -0
- package/dist/solid/index.js +14 -11
- package/dist/src/client/page/base.css +1078 -199
- package/dist/src/client/page/main.js +174 -33
- package/dist/src/loader.js +7 -2
- package/dist/src/package.js +3 -6
- package/dist/src/pages/blocks.js +8 -13
- package/dist/src/pages/markdown.js +1 -1
- package/dist/src/pages/render.js +74 -15
- package/dist/src/pages/window.js +30 -0
- package/dist/src/public.js +24 -0
- package/dist/src/server.js +36 -14
- package/package.json +3 -1
- package/plugins/callout/index.ts +3 -1
- package/plugins/callout/public/style.css +30 -0
- package/plugins/chat/autosize.ts +43 -9
- package/plugins/chat/client.tsx +22 -5
- package/plugins/chat/index.ts +22 -8
- package/plugins/chat/public/style.css +98 -0
- package/plugins/claude-code/index.ts +49 -14
- package/plugins/code/diff.ts +28 -8
- package/plugins/code/index.ts +30 -20
- package/plugins/code/public/style.css +68 -0
- package/plugins/comments/client.ts +139 -71
- package/plugins/comments/index.ts +9 -7
- package/plugins/comments/public/style.css +185 -0
- package/plugins/details/index.ts +7 -2
- package/plugins/details/public/style.css +7 -0
- package/plugins/forge/index.ts +16 -3
- package/plugins/form/index.ts +75 -27
- package/plugins/form/public/style.css +126 -0
- package/plugins/gallery/client.tsx +9 -3
- package/plugins/gallery/index.ts +31 -9
- package/plugins/gallery/public/style.css +90 -0
- package/plugins/grid/index.ts +19 -5
- package/plugins/grid/public/style.css +71 -0
- package/plugins/iframe/index.ts +20 -23
- package/plugins/iframe/public/style.css +13 -0
- package/plugins/index.ts +16 -1
- package/plugins/mermaid/index.ts +3 -1
- package/plugins/mermaid/{style.css → public/style.css} +7 -2
- package/plugins/terminal/client.ts +105 -48
- package/plugins/terminal/host.ts +131 -29
- package/plugins/terminal/index.ts +84 -25
- package/plugins/terminal/public/style.css +225 -0
- package/plugins/terminal/server.ts +64 -13
- package/plugins/terminal/sizes.ts +13 -3
- package/plugins/toc/client.ts +45 -26
- package/plugins/toc/index.ts +9 -6
- package/plugins/toc/public/style.css +93 -0
- package/plugins/window/README.md +29 -0
- package/plugins/window/index.ts +85 -0
- package/skills/whiteboard/SKILL.md +3 -1
- package/solid/index.tsx +24 -9
- package/src/auth.ts +25 -7
- package/src/client/forms.ts +74 -28
- package/src/client/markdown.ts +15 -4
- package/src/client/page/base.css +1078 -199
- package/src/client/page/elsewhere.ts +32 -15
- package/src/client/page/fresh.ts +91 -23
- package/src/client/page/hold.ts +11 -2
- package/src/client/page/live.ts +10 -6
- package/src/client/page/main.ts +106 -36
- package/src/client/page/orphans.ts +25 -10
- package/src/client/page/sort.ts +14 -4
- package/src/client/page/sticky.ts +11 -3
- package/src/client/page/theme.ts +11 -7
- package/src/client/page/waiting.ts +141 -34
- package/src/client/page/window.ts +40 -0
- package/src/client/reactive.ts +19 -0
- package/src/client/read.ts +17 -10
- package/src/client/runtime.ts +130 -35
- package/src/client/scroll.ts +14 -4
- package/src/client/toast.ts +19 -5
- package/src/config.ts +20 -5
- package/src/define.ts +65 -25
- package/src/events.ts +111 -24
- package/src/html.ts +6 -2
- package/src/icons.ts +4 -2
- package/src/load.ts +10 -2
- package/src/loader.ts +75 -11
- package/src/package.ts +9 -8
- package/src/pages/blocks.ts +20 -14
- package/src/pages/markdown.ts +113 -34
- package/src/pages/render.ts +199 -39
- package/src/pages/window.ts +71 -0
- package/src/plugins.ts +90 -24
- package/src/public.ts +39 -0
- package/src/running.ts +8 -2
- package/src/serve.ts +29 -7
- package/src/server.ts +348 -107
- package/src/status.ts +15 -9
- package/dist/plugins/callout/style.css +0 -12
- package/dist/plugins/chat/style.css +0 -24
- package/dist/plugins/code/style.css +0 -29
- package/dist/plugins/comments/client.js +0 -10
- package/dist/plugins/comments/style.css +0 -40
- package/dist/plugins/details/style.css +0 -3
- package/dist/plugins/form/style.css +0 -44
- package/dist/plugins/gallery/client.js +0 -1
- package/dist/plugins/gallery/style.css +0 -27
- package/dist/plugins/grid/style.css +0 -25
- package/dist/plugins/iframe/client.js +0 -1
- package/dist/plugins/iframe/style.css +0 -29
- package/dist/plugins/toc/client.js +0 -1
- package/dist/plugins/toc/style.css +0 -19
- package/plugins/callout/style.css +0 -12
- package/plugins/chat/style.css +0 -24
- package/plugins/code/style.css +0 -29
- package/plugins/comments/style.css +0 -40
- package/plugins/details/style.css +0 -3
- package/plugins/form/style.css +0 -44
- package/plugins/gallery/style.css +0 -27
- package/plugins/grid/style.css +0 -25
- package/plugins/iframe/client.ts +0 -46
- package/plugins/iframe/style.css +0 -29
- package/plugins/terminal/style.css +0 -71
- package/plugins/toc/style.css +0 -19
- /package/dist/plugins/terminal/{client.js → public/client.js} +0 -0
package/src/events.ts
CHANGED
|
@@ -61,7 +61,11 @@ export function parseLines(text: string): WbEvent[] {
|
|
|
61
61
|
for (const line of text.split('\n')) {
|
|
62
62
|
if (!line.trim()) continue
|
|
63
63
|
let event
|
|
64
|
-
try {
|
|
64
|
+
try {
|
|
65
|
+
event = JSON.parse(line)
|
|
66
|
+
} catch {
|
|
67
|
+
continue /* a corrupt line */
|
|
68
|
+
}
|
|
65
69
|
out.push(event)
|
|
66
70
|
}
|
|
67
71
|
return out
|
|
@@ -78,7 +82,11 @@ export function readEvents(file: string): WbEvent[] {
|
|
|
78
82
|
*/
|
|
79
83
|
export function read(file: string): { events: WbEvent[]; end: number } {
|
|
80
84
|
let buf: Buffer
|
|
81
|
-
try {
|
|
85
|
+
try {
|
|
86
|
+
buf = fs.readFileSync(file)
|
|
87
|
+
} catch {
|
|
88
|
+
return { events: [], end: 0 }
|
|
89
|
+
}
|
|
82
90
|
const end = buf.lastIndexOf(0x0a) + 1
|
|
83
91
|
return { events: parseLines(buf.subarray(0, end).toString('utf8')), end }
|
|
84
92
|
}
|
|
@@ -92,31 +100,54 @@ export function read(file: string): { events: WbEvent[]; end: number } {
|
|
|
92
100
|
* shorter one. Then it reads the log again and goes on after the last event it had passed on (by
|
|
93
101
|
* time, and by id among those of the same time), so nothing is passed on twice.
|
|
94
102
|
*/
|
|
95
|
-
export function follow(
|
|
103
|
+
export function follow(
|
|
104
|
+
file: string,
|
|
105
|
+
from: number,
|
|
106
|
+
onEvent: (event: WbEvent) => void,
|
|
107
|
+
{ interval = 200 } = {},
|
|
108
|
+
): () => void {
|
|
96
109
|
let offset = from
|
|
97
110
|
let partial = ''
|
|
98
111
|
let inode: number | null = null
|
|
99
|
-
try {
|
|
112
|
+
try {
|
|
113
|
+
inode = fs.statSync(file).ino
|
|
114
|
+
} catch {
|
|
115
|
+
/* not there yet */
|
|
116
|
+
}
|
|
100
117
|
// where it is in the log, to find its place again after a rewrite
|
|
101
118
|
let lastTs = ''
|
|
102
119
|
let atLastTs = new Set<string>()
|
|
103
120
|
// the events before `from` count as passed on: the place starts after the last of them
|
|
104
121
|
if (from > 0) {
|
|
105
122
|
let before: WbEvent[] = []
|
|
106
|
-
try {
|
|
123
|
+
try {
|
|
124
|
+
before = parseLines(fs.readFileSync(file).subarray(0, from).toString('utf8'))
|
|
125
|
+
} catch {
|
|
126
|
+
/* not there */
|
|
127
|
+
}
|
|
107
128
|
for (const e of before) {
|
|
108
|
-
if (e.ts > lastTs) {
|
|
129
|
+
if (e.ts > lastTs) {
|
|
130
|
+
lastTs = e.ts
|
|
131
|
+
atLastTs = new Set()
|
|
132
|
+
}
|
|
109
133
|
if (e.ts === lastTs) atLastTs.add(e.id)
|
|
110
134
|
}
|
|
111
135
|
}
|
|
112
136
|
const pass = (e: WbEvent) => {
|
|
113
|
-
if (e.ts > lastTs) {
|
|
137
|
+
if (e.ts > lastTs) {
|
|
138
|
+
lastTs = e.ts
|
|
139
|
+
atLastTs = new Set()
|
|
140
|
+
}
|
|
114
141
|
atLastTs.add(e.id)
|
|
115
142
|
onEvent(e)
|
|
116
143
|
}
|
|
117
144
|
const poll = () => {
|
|
118
145
|
let stat: fs.Stats
|
|
119
|
-
try {
|
|
146
|
+
try {
|
|
147
|
+
stat = fs.statSync(file)
|
|
148
|
+
} catch {
|
|
149
|
+
return
|
|
150
|
+
}
|
|
120
151
|
if ((inode != null && stat.ino !== inode) || stat.size < offset) {
|
|
121
152
|
// rewritten: read it again, and go on after the place reached
|
|
122
153
|
inode = stat.ino
|
|
@@ -149,19 +180,37 @@ export function follow(file: string, from: number, onEvent: (event: WbEvent) =>
|
|
|
149
180
|
* reads it again and goes on after the last event it passed on (see follow). Lines that aren't
|
|
150
181
|
* events are kept as they are. Returns how many were dropped and changed.
|
|
151
182
|
*/
|
|
152
|
-
export function rewrite(
|
|
183
|
+
export function rewrite(
|
|
184
|
+
file: string,
|
|
185
|
+
change: (event: WbEvent) => WbEvent | null,
|
|
186
|
+
): { removed: number; changed: number } {
|
|
153
187
|
for (let attempt = 0; attempt < 10; attempt++) {
|
|
154
188
|
let buf: Buffer
|
|
155
|
-
try {
|
|
189
|
+
try {
|
|
190
|
+
buf = fs.readFileSync(file)
|
|
191
|
+
} catch {
|
|
192
|
+
return { removed: 0, changed: 0 }
|
|
193
|
+
}
|
|
156
194
|
const keep: string[] = []
|
|
157
|
-
let removed = 0,
|
|
195
|
+
let removed = 0,
|
|
196
|
+
changed = 0
|
|
158
197
|
for (const line of buf.toString('utf8').split('\n')) {
|
|
159
198
|
if (!line.trim()) continue
|
|
160
199
|
let ev: WbEvent | null = null
|
|
161
|
-
try {
|
|
162
|
-
|
|
200
|
+
try {
|
|
201
|
+
ev = JSON.parse(line)
|
|
202
|
+
} catch {
|
|
203
|
+
/* not an event: kept */
|
|
204
|
+
}
|
|
205
|
+
if (!ev) {
|
|
206
|
+
keep.push(line)
|
|
207
|
+
continue
|
|
208
|
+
}
|
|
163
209
|
const next = change(ev)
|
|
164
|
-
if (!next) {
|
|
210
|
+
if (!next) {
|
|
211
|
+
removed++
|
|
212
|
+
continue
|
|
213
|
+
}
|
|
165
214
|
const text = JSON.stringify(next)
|
|
166
215
|
if (text !== JSON.stringify(ev)) changed++
|
|
167
216
|
keep.push(next === ev ? line : text)
|
|
@@ -170,7 +219,10 @@ export function rewrite(file: string, change: (event: WbEvent) => WbEvent | null
|
|
|
170
219
|
const tmp = `${file}.${process.pid}.tmp`
|
|
171
220
|
fs.writeFileSync(tmp, keep.length ? keep.join('\n') + '\n' : '')
|
|
172
221
|
// appended to meanwhile: that event would be lost with the old file
|
|
173
|
-
if (fs.statSync(file).size !== buf.length) {
|
|
222
|
+
if (fs.statSync(file).size !== buf.length) {
|
|
223
|
+
fs.rmSync(tmp, { force: true })
|
|
224
|
+
continue
|
|
225
|
+
}
|
|
174
226
|
fs.renameSync(tmp, file)
|
|
175
227
|
return { removed, changed }
|
|
176
228
|
}
|
|
@@ -192,7 +244,10 @@ export function prune(file: string, page: string, which: (event: WbEvent) => boo
|
|
|
192
244
|
export function editEvent(file: string, id: string, data: Record<string, unknown>): WbEvent {
|
|
193
245
|
const found = readEvents(file).find(e => e.id === id)
|
|
194
246
|
if (!found) throw new Error(`no event ${id} in the log`)
|
|
195
|
-
if (fromReader(found))
|
|
247
|
+
if (fromReader(found))
|
|
248
|
+
throw new Error(
|
|
249
|
+
`event ${id} is the reader's (${found.type}): what the reader sent stays as they sent it; an agent edits only its own`,
|
|
250
|
+
)
|
|
196
251
|
let edited: WbEvent | null = null
|
|
197
252
|
rewrite(file, e => (e.id === id ? (edited = { ...e, data: { ...e.data, ...data } }) : e))
|
|
198
253
|
return edited ?? found
|
|
@@ -228,8 +283,10 @@ export function pick(file: string, event: WbEvent): WbEvent {
|
|
|
228
283
|
* Which event types reach an agent unasked: a form's answers (types without a colon) always, and of
|
|
229
284
|
* the plugins' events (`<plugin>:<what>`) those declared `handOut: true`, given in `handedOut`.
|
|
230
285
|
*/
|
|
231
|
-
export const forAgent =
|
|
232
|
-
|
|
286
|
+
export const forAgent =
|
|
287
|
+
(handedOut: ReadonlySet<string> = new Set()) =>
|
|
288
|
+
(type: string): boolean =>
|
|
289
|
+
!type.includes(':') || handedOut.has(type)
|
|
233
290
|
|
|
234
291
|
export interface HandOutOptions {
|
|
235
292
|
/** which event types it hands out unasked (see forAgent); a filter's `type` is handed out always */
|
|
@@ -262,12 +319,29 @@ export interface HandOutOptions {
|
|
|
262
319
|
* Resolves with how many were handed out: once `count` is reached, after the backlog without
|
|
263
320
|
* `follow`, or when `signal` aborts.
|
|
264
321
|
*/
|
|
265
|
-
export function handOut(
|
|
266
|
-
|
|
322
|
+
export function handOut(
|
|
323
|
+
file: string,
|
|
324
|
+
filter: EventFilter,
|
|
325
|
+
onEvent: (event: WbEvent) => void,
|
|
326
|
+
options: HandOutOptions = {},
|
|
327
|
+
): Promise<number> {
|
|
328
|
+
const {
|
|
329
|
+
count = Infinity,
|
|
330
|
+
follow: following = true,
|
|
331
|
+
interval,
|
|
332
|
+
signal,
|
|
333
|
+
forAgent: isForAgent = forAgent(),
|
|
334
|
+
skip,
|
|
335
|
+
recheck,
|
|
336
|
+
} = options
|
|
267
337
|
return new Promise(resolve => {
|
|
268
338
|
let got = 0
|
|
269
339
|
let stop = () => {}
|
|
270
|
-
const finish = () => {
|
|
340
|
+
const finish = () => {
|
|
341
|
+
stop()
|
|
342
|
+
signal?.removeEventListener('abort', finish)
|
|
343
|
+
resolve(got)
|
|
344
|
+
}
|
|
271
345
|
// the reader's events, and of those, the ones for an agent, unless the filter names a type
|
|
272
346
|
const wanted = (e: WbEvent) => fromReader(e) && matches(e, filter) && (!!filter.type || isForAgent(e.type))
|
|
273
347
|
// false once enough were handed out
|
|
@@ -280,7 +354,10 @@ export function handOut(file: string, filter: EventFilter, onEvent: (event: WbEv
|
|
|
280
354
|
const left = new Map<string, WbEvent>()
|
|
281
355
|
// false once enough were handed out; a left event is kept to look at again
|
|
282
356
|
const offer = (e: WbEvent) => {
|
|
283
|
-
if (skip?.(e)) {
|
|
357
|
+
if (skip?.(e)) {
|
|
358
|
+
left.set(e.id, e)
|
|
359
|
+
return true
|
|
360
|
+
}
|
|
284
361
|
return hand(e)
|
|
285
362
|
}
|
|
286
363
|
if (count <= 0 || signal?.aborted) return finish()
|
|
@@ -295,12 +372,22 @@ export function handOut(file: string, filter: EventFilter, onEvent: (event: WbEv
|
|
|
295
372
|
if (!left.size || got >= count) return
|
|
296
373
|
const picked = pickedUp(read(file).events)
|
|
297
374
|
for (const [id, e] of left) {
|
|
298
|
-
if (picked.has(id)) {
|
|
375
|
+
if (picked.has(id)) {
|
|
376
|
+
left.delete(id)
|
|
377
|
+
continue
|
|
378
|
+
}
|
|
299
379
|
if (skip?.(e)) continue
|
|
300
380
|
left.delete(id)
|
|
301
381
|
if (!hand(e)) return finish()
|
|
302
382
|
}
|
|
303
383
|
})
|
|
304
|
-
stop = follow(
|
|
384
|
+
stop = follow(
|
|
385
|
+
file,
|
|
386
|
+
end,
|
|
387
|
+
e => {
|
|
388
|
+
if (got < count && wanted(e) && !offer(e)) finish()
|
|
389
|
+
},
|
|
390
|
+
{ interval },
|
|
391
|
+
)
|
|
305
392
|
})
|
|
306
393
|
}
|
package/src/html.ts
CHANGED
|
@@ -14,8 +14,12 @@
|
|
|
14
14
|
/** A piece of HTML that is not escaped again. `String(piece)` is its markup. */
|
|
15
15
|
export class Html {
|
|
16
16
|
value: string
|
|
17
|
-
constructor(value: string) {
|
|
18
|
-
|
|
17
|
+
constructor(value: string) {
|
|
18
|
+
this.value = value
|
|
19
|
+
}
|
|
20
|
+
toString() {
|
|
21
|
+
return this.value
|
|
22
|
+
}
|
|
19
23
|
}
|
|
20
24
|
|
|
21
25
|
const ESCAPE: Record<string, string> = { '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' }
|
package/src/icons.ts
CHANGED
|
@@ -2,7 +2,9 @@
|
|
|
2
2
|
// imports it too.
|
|
3
3
|
|
|
4
4
|
/** An inbox: what awaits the reader (the header's menu, the overview). */
|
|
5
|
-
export const INBOX_ICON =
|
|
5
|
+
export const INBOX_ICON =
|
|
6
|
+
'<svg viewBox="0 0 16 16" width="13" height="13" aria-hidden="true"><path d="M2 9.5 3.8 3.2A1 1 0 0 1 4.8 2.5h6.4a1 1 0 0 1 1 .7L14 9.5V13a.5.5 0 0 1-.5.5h-11A.5.5 0 0 1 2 13Zm0 0h3.5l1 1.5h3l1-1.5H14" fill="none" stroke="currentColor" stroke-width="1.4" stroke-linejoin="round"/></svg>'
|
|
6
7
|
|
|
7
8
|
/** "Awaiting you: 2 things", and where: what the inbox says to a reader who hovers it. */
|
|
8
|
-
export const awaitingLabel = (n: number, where = '') =>
|
|
9
|
+
export const awaitingLabel = (n: number, where = '') =>
|
|
10
|
+
`Awaiting you: ${n === 1 ? '1 question or message' : `${n} questions and messages`}${where}`
|
package/src/load.ts
CHANGED
|
@@ -22,7 +22,11 @@ const PACKAGE = '@bigmistqke/whiteboard'
|
|
|
22
22
|
function ownFile(specifier: string): string | null {
|
|
23
23
|
if (specifier !== PACKAGE && !specifier.startsWith(PACKAGE + '/')) return null
|
|
24
24
|
let exports: Record<string, string | { source?: string; default?: string }> | undefined
|
|
25
|
-
try {
|
|
25
|
+
try {
|
|
26
|
+
exports = JSON.parse(fs.readFileSync(path.join(ENGINE_DIR, 'package.json'), 'utf8')).exports
|
|
27
|
+
} catch {
|
|
28
|
+
return null
|
|
29
|
+
}
|
|
26
30
|
const target = exports?.[specifier === PACKAGE ? '.' : `./${specifier.slice(PACKAGE.length + 1)}`]
|
|
27
31
|
const file = typeof target === 'string' ? target : (FROM_SOURCE && target?.source) || target?.default
|
|
28
32
|
return file ? path.join(ENGINE_DIR, file) : null
|
|
@@ -57,7 +61,11 @@ export function installHooks() {
|
|
|
57
61
|
// would load the config as CommonJS and fail on its imports. What is in .whiteboard/ is always
|
|
58
62
|
// a module, by its extension; other local files keep the format their package gives them.
|
|
59
63
|
const inProject = url.pathname.split('/').includes('.whiteboard')
|
|
60
|
-
return {
|
|
64
|
+
return {
|
|
65
|
+
...resolved,
|
|
66
|
+
url: url.href,
|
|
67
|
+
format: inProject ? moduleFormat(url.pathname) : (resolved.format ?? moduleFormat(url.pathname)),
|
|
68
|
+
}
|
|
61
69
|
},
|
|
62
70
|
})
|
|
63
71
|
}
|
package/src/loader.ts
CHANGED
|
@@ -8,6 +8,7 @@ import { fileURLToPath } from 'node:url'
|
|
|
8
8
|
import { importFresh, importOwn } from './load.ts'
|
|
9
9
|
import { createPages, type Pages } from './pages/index.ts'
|
|
10
10
|
import { pluginCommands } from './plugins.ts'
|
|
11
|
+
import { inPublic, publicDirOf } from './public.ts'
|
|
11
12
|
import type { Config, Plugin } from './define.ts'
|
|
12
13
|
import type { Project } from './config.ts'
|
|
13
14
|
|
|
@@ -20,13 +21,35 @@ export interface LoadedConfig {
|
|
|
20
21
|
handedOut: Set<string>
|
|
21
22
|
/** the plugins' event types declared `forReader: true`: from the agent, a message for the reader */
|
|
22
23
|
forReader: Set<string>
|
|
24
|
+
/** each plugin's public folder, by name: the one place its browser files are served from */
|
|
25
|
+
publicDirs: Map<string, string>
|
|
23
26
|
files: boolean
|
|
24
27
|
errors: unknown[]
|
|
25
28
|
}
|
|
26
29
|
|
|
27
30
|
const NAME = /^[\w-]+$/
|
|
28
31
|
/** The command line's own commands: a plugin can't be named like one, since its commands are `whiteboard <name>`. */
|
|
29
|
-
export const BUILTIN_COMMANDS = new Set([
|
|
32
|
+
export const BUILTIN_COMMANDS = new Set([
|
|
33
|
+
'serve',
|
|
34
|
+
'init',
|
|
35
|
+
'root',
|
|
36
|
+
'new',
|
|
37
|
+
'show',
|
|
38
|
+
'url',
|
|
39
|
+
'docs',
|
|
40
|
+
'events',
|
|
41
|
+
'wait',
|
|
42
|
+
'watch',
|
|
43
|
+
'append',
|
|
44
|
+
'pick',
|
|
45
|
+
'status',
|
|
46
|
+
'scroll',
|
|
47
|
+
'close',
|
|
48
|
+
'prune',
|
|
49
|
+
'edit',
|
|
50
|
+
'skill',
|
|
51
|
+
'help',
|
|
52
|
+
])
|
|
30
53
|
// folders in .whiteboard/ that are the core's or meant for the project's own plugins (a plugin's own
|
|
31
54
|
// folder holds its data, ignored by git), and the built-in commands
|
|
32
55
|
const RESERVED = new Set(['pages', 'plugins', ...BUILTIN_COMMANDS])
|
|
@@ -54,11 +77,14 @@ async function builtinPlugins(): Promise<Plugin[]> {
|
|
|
54
77
|
|
|
55
78
|
/** Check a plugin, as the config gives it. */
|
|
56
79
|
function checkPlugin(e: Plugin, where: string | null) {
|
|
57
|
-
if (typeof e !== 'object' || !NAME.test(e?.name ?? ''))
|
|
58
|
-
|
|
80
|
+
if (typeof e !== 'object' || !NAME.test(e?.name ?? ''))
|
|
81
|
+
throw new Error(`a plugin in ${where} needs a name: letters, digits, - and _`)
|
|
82
|
+
if (RESERVED.has(e.name))
|
|
83
|
+
throw new Error(`plugin name "${e.name}" in ${where} is taken by the core (a folder in .whiteboard/ or a command)`)
|
|
59
84
|
pluginCommands(e) // checks them
|
|
60
85
|
for (const type of Object.keys(e.events ?? {}))
|
|
61
|
-
if (!type.startsWith(`${e.name}:`) || type.length <= e.name.length + 1)
|
|
86
|
+
if (!type.startsWith(`${e.name}:`) || type.length <= e.name.length + 1)
|
|
87
|
+
throw new Error(`plugin ${e.name}: event type "${type}" must be named after it: ${e.name}:<what>`)
|
|
62
88
|
}
|
|
63
89
|
|
|
64
90
|
/**
|
|
@@ -92,21 +118,59 @@ export function createConfigLoader(proj: Project, { pages: withPages = true } =
|
|
|
92
118
|
if (builtin.has(e.name)) plugins[plugins.findIndex(p => p.name === e.name)] = e
|
|
93
119
|
else plugins.push(e)
|
|
94
120
|
}
|
|
95
|
-
|
|
96
|
-
const
|
|
97
|
-
const
|
|
121
|
+
// each plugin's browser files are served from its public folder alone: its files are named in it
|
|
122
|
+
const publicDirs = new Map(plugins.map(p => [p.name, publicDirOf(p, proj.wb)]))
|
|
123
|
+
const blocks = plugins.flatMap(p =>
|
|
124
|
+
Object.entries(p.blocks ?? {}).map(([name, block]) => ({
|
|
125
|
+
name,
|
|
126
|
+
block,
|
|
127
|
+
plugin: p.name,
|
|
128
|
+
public: publicDirs.get(p.name)!,
|
|
129
|
+
})),
|
|
130
|
+
)
|
|
131
|
+
const styles = plugins
|
|
132
|
+
.filter(p => p.style)
|
|
133
|
+
.map(p => inPublic(publicDirs.get(p.name)!, p.style!, `plugin ${p.name}: style`))
|
|
134
|
+
const links = plugins.flatMap(p => (p.links ? [p.links.bind(p)] : []))
|
|
98
135
|
const pages = withPages ? createPages({ blocks, style: config?.style, styles, links }) : null
|
|
99
|
-
const declared = (flag: 'handOut' | 'forReader') =>
|
|
100
|
-
|
|
136
|
+
const declared = (flag: 'handOut' | 'forReader') =>
|
|
137
|
+
new Set(
|
|
138
|
+
plugins.flatMap(p =>
|
|
139
|
+
Object.entries(p.events ?? {})
|
|
140
|
+
.filter(([, spec]) => spec?.[flag])
|
|
141
|
+
.map(([type]) => type),
|
|
142
|
+
),
|
|
143
|
+
)
|
|
144
|
+
return {
|
|
145
|
+
pages,
|
|
146
|
+
plugins,
|
|
147
|
+
handedOut: declared('handOut'),
|
|
148
|
+
forReader: declared('forReader'),
|
|
149
|
+
publicDirs,
|
|
150
|
+
files: config?.files !== false,
|
|
151
|
+
}
|
|
101
152
|
}
|
|
102
153
|
|
|
103
154
|
function config(): Promise<LoadedConfig> {
|
|
104
155
|
cached ??= load().then(
|
|
105
156
|
value => ({ ...value, errors: [] }),
|
|
106
|
-
error => ({
|
|
157
|
+
error => ({
|
|
158
|
+
pages: null,
|
|
159
|
+
plugins: [],
|
|
160
|
+
handedOut: new Set<string>(),
|
|
161
|
+
forReader: new Set<string>(),
|
|
162
|
+
publicDirs: new Map<string, string>(),
|
|
163
|
+
files: false,
|
|
164
|
+
errors: [error],
|
|
165
|
+
}),
|
|
107
166
|
)
|
|
108
167
|
return cached
|
|
109
168
|
}
|
|
110
169
|
|
|
111
|
-
return {
|
|
170
|
+
return {
|
|
171
|
+
config,
|
|
172
|
+
invalidate: () => {
|
|
173
|
+
cached = null
|
|
174
|
+
},
|
|
175
|
+
}
|
|
112
176
|
}
|
package/src/package.ts
CHANGED
|
@@ -8,9 +8,13 @@ import { fileURLToPath } from 'node:url'
|
|
|
8
8
|
function findRoot(from: string): string {
|
|
9
9
|
for (let dir = from; ; dir = path.dirname(dir)) {
|
|
10
10
|
try {
|
|
11
|
-
if (JSON.parse(fs.readFileSync(path.join(dir, 'package.json'), 'utf8')).name === '@bigmistqke/whiteboard')
|
|
12
|
-
|
|
13
|
-
|
|
11
|
+
if (JSON.parse(fs.readFileSync(path.join(dir, 'package.json'), 'utf8')).name === '@bigmistqke/whiteboard')
|
|
12
|
+
return dir
|
|
13
|
+
} catch {
|
|
14
|
+
/* no package.json here, or not ours */
|
|
15
|
+
}
|
|
16
|
+
if (path.dirname(dir) === dir)
|
|
17
|
+
throw new Error(`whiteboard: no package.json of @bigmistqke/whiteboard above ${from}`)
|
|
14
18
|
}
|
|
15
19
|
}
|
|
16
20
|
|
|
@@ -32,8 +36,5 @@ export const FROM_SOURCE = fileURLToPath(import.meta.url).endsWith('.ts')
|
|
|
32
36
|
* dist/. The same whether the module runs from source (plugins/form/index.ts gives
|
|
33
37
|
* dist/plugins/form/client.js) or from dist/ itself.
|
|
34
38
|
*/
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
const inDist = rel === 'dist' || rel.startsWith('dist' + path.sep)
|
|
38
|
-
return path.join(PACKAGE_DIR, inDist ? '' : 'dist', rel, name)
|
|
39
|
-
}
|
|
39
|
+
/** A built-in plugin's public folder: its built browser modules and copied styles (build.ts). */
|
|
40
|
+
export const builtinPublic = (name: string): string => packagePath('dist', 'plugins', name, 'public')
|
package/src/pages/blocks.ts
CHANGED
|
@@ -4,9 +4,16 @@ import path from 'node:path'
|
|
|
4
4
|
import { fileURLToPath } from 'node:url'
|
|
5
5
|
import type { Html } from '../html.ts'
|
|
6
6
|
import type { Block } from '../define.ts'
|
|
7
|
+
import { inPublic } from '../public.ts'
|
|
7
8
|
|
|
8
9
|
/** A block as the plugins bring it: its name, and the plugin it comes from. */
|
|
9
|
-
|
|
10
|
+
/** A block of a plugin, with the plugin's public folder, where its files are. */
|
|
11
|
+
export interface BlockEntry {
|
|
12
|
+
name: string
|
|
13
|
+
block: Block<any>
|
|
14
|
+
plugin: string
|
|
15
|
+
public: string
|
|
16
|
+
}
|
|
10
17
|
|
|
11
18
|
const NAME = /^[\w-]+$/
|
|
12
19
|
|
|
@@ -25,23 +32,22 @@ export interface ResolvedBlock {
|
|
|
25
32
|
source: string
|
|
26
33
|
}
|
|
27
34
|
|
|
28
|
-
export interface ResolvedBlocks {
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
function toPath(ref: unknown, field: string, block: string): string | null {
|
|
32
|
-
if (ref == null) return null
|
|
33
|
-
if (ref instanceof URL || (typeof ref === 'string' && ref.startsWith('file:'))) return fileURLToPath(ref)
|
|
34
|
-
if (typeof ref === 'string' && path.isAbsolute(ref)) return ref
|
|
35
|
-
throw new Error(`block "${block}": ${field} must be a URL, e.g. new URL('./${field}.js', import.meta.url)`)
|
|
35
|
+
export interface ResolvedBlocks {
|
|
36
|
+
blocks: Map<string, ResolvedBlock>
|
|
37
|
+
code: ResolvedBlock | null
|
|
36
38
|
}
|
|
37
39
|
|
|
38
|
-
/** Check and resolve one block into what the engine uses. */
|
|
39
|
-
function normalize({ name, block: b, plugin }: BlockEntry): ResolvedBlock {
|
|
40
|
+
/** Check and resolve one block into what the engine uses: its files in its plugin's public folder. */
|
|
41
|
+
function normalize({ name, block: b, plugin, public: dir }: BlockEntry): ResolvedBlock {
|
|
40
42
|
if (typeof b !== 'object' || !b) throw new Error(`plugin ${plugin}: block "${name}" is not an object`)
|
|
41
|
-
if (!NAME.test(name))
|
|
42
|
-
|
|
43
|
+
if (!NAME.test(name))
|
|
44
|
+
throw new Error(`plugin ${plugin}: block name ${JSON.stringify(name)}: use letters, digits, - and _`)
|
|
45
|
+
const file = (ref: URL | string | undefined, field: string) =>
|
|
46
|
+
ref == null ? null : inPublic(dir, ref, `plugin ${plugin}, block "${name}": ${field}`)
|
|
47
|
+
const files = { client: file(b.client, 'client'), style: file(b.style, 'style') }
|
|
43
48
|
for (const [field, file] of Object.entries(files))
|
|
44
|
-
if (file && !fs.existsSync(file))
|
|
49
|
+
if (file && !fs.existsSync(file))
|
|
50
|
+
throw new Error(`plugin ${plugin}, block "${name}": ${field} file not found: ${file}`)
|
|
45
51
|
return {
|
|
46
52
|
name,
|
|
47
53
|
code: b.fence === 'any',
|