@bigmistqke/whiteboard 0.6.1 → 0.8.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 +23 -216
- package/bin/whiteboard.ts +82 -239
- package/dist/bin/whiteboard.js +70 -200
- package/dist/client/runtime.js +297 -61
- package/dist/plugins/badge/README.md +3 -1
- package/dist/plugins/badge/public/style.css +17 -0
- package/dist/plugins/chat/public/client.js +2 -2
- package/dist/plugins/chat/public/style.css +19 -6
- package/dist/plugins/code/README.md +10 -0
- package/dist/plugins/code/index.js +31 -22
- package/dist/plugins/code/public/client.js +1 -0
- package/dist/plugins/code/public/style.css +33 -2
- package/dist/plugins/comments/public/client.js +7 -2
- package/dist/plugins/comments/public/style.css +4 -0
- package/dist/plugins/form/public/style.css +2 -0
- package/dist/plugins/grid/public/style.css +0 -5
- package/dist/plugins/iframe/index.js +2 -2
- package/dist/plugins/index.js +5 -1
- package/dist/plugins/lightbox/public/client.js +1 -1
- package/dist/plugins/terminal/index.js +7 -9
- package/dist/plugins/terminal/public/client.js +1 -1
- package/dist/plugins/terminal/public/style.css +3 -3
- package/dist/plugins/timeline/README.md +40 -0
- package/dist/plugins/timeline/index.js +82 -0
- package/dist/plugins/timeline/public/style.css +118 -0
- package/dist/plugins/toc/README.md +13 -13
- package/dist/plugins/toc/index.js +6 -4
- package/dist/plugins/toc/public/client.js +1 -1
- package/dist/plugins/toc/public/style.css +19 -2
- package/dist/plugins/window/index.js +4 -4
- package/dist/solid/index.js +2 -1
- package/dist/src/auth.js +7 -3
- package/dist/src/build.js +159 -0
- package/dist/src/client/page/base.css +74 -35
- package/dist/src/client/page/main.js +16 -5
- package/dist/src/document.js +93 -0
- package/dist/src/events.js +7 -15
- package/dist/src/hub.js +90 -53
- package/dist/src/listen.js +100 -0
- package/dist/src/listeners.js +67 -0
- package/dist/src/pages/index.js +1 -1
- package/dist/src/pages/markdown.js +23 -11
- package/dist/src/pages/render.js +13 -8
- package/dist/src/serve.js +2 -0
- package/dist/src/server.js +204 -514
- package/dist/src/site.js +173 -0
- package/dist/src/status.js +63 -1
- package/dist/src/ws.js +128 -0
- package/package.json +1 -1
- package/plugins/README.md +258 -0
- package/plugins/badge/README.md +3 -1
- package/plugins/badge/public/style.css +17 -0
- package/plugins/chat/client.tsx +34 -6
- package/plugins/chat/public/style.css +19 -6
- package/plugins/code/README.md +10 -0
- package/plugins/code/client.ts +73 -0
- package/plugins/code/index.ts +38 -26
- package/plugins/code/public/style.css +33 -2
- package/plugins/comments/client.ts +56 -6
- package/plugins/comments/public/style.css +4 -0
- package/plugins/form/public/style.css +2 -0
- package/plugins/grid/public/style.css +0 -5
- package/plugins/iframe/index.ts +2 -2
- package/plugins/index.ts +6 -1
- package/plugins/lightbox/client.ts +13 -3
- package/plugins/terminal/client.ts +10 -2
- package/plugins/terminal/index.ts +8 -6
- package/plugins/terminal/public/style.css +3 -3
- package/plugins/timeline/README.md +40 -0
- package/plugins/timeline/index.ts +92 -0
- package/plugins/timeline/public/style.css +118 -0
- package/plugins/toc/README.md +13 -13
- package/plugins/toc/client.ts +58 -32
- package/plugins/toc/index.ts +13 -13
- package/plugins/toc/public/style.css +19 -2
- package/plugins/window/index.ts +4 -4
- package/skills/whiteboard/SKILL.md +3 -1
- package/solid/index.tsx +3 -0
- package/src/auth.ts +7 -2
- package/src/build.ts +213 -0
- package/src/client/forms.ts +102 -5
- package/src/client/page/base.css +74 -35
- package/src/client/page/elsewhere.ts +14 -4
- package/src/client/page/waiting.ts +10 -5
- package/src/client/read.ts +16 -3
- package/src/client/runtime.ts +219 -52
- package/src/client/toast.ts +36 -6
- package/src/define.ts +44 -18
- package/src/document.ts +191 -0
- package/src/events.ts +18 -17
- package/src/hub.ts +102 -56
- package/src/listen.ts +125 -0
- package/src/listeners.ts +95 -0
- package/src/pages/index.ts +1 -1
- package/src/pages/markdown.ts +29 -18
- package/src/pages/render.ts +13 -8
- package/src/serve.ts +2 -0
- package/src/server.ts +223 -611
- package/src/site.ts +165 -0
- package/src/status.ts +100 -0
- package/src/ws.ts +155 -0
package/src/client/runtime.ts
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
// The core in the browser: the page's events, the live channel, and handing the page to the page
|
|
2
2
|
// module (./page: markdown live, the blocks' islands) and the plugins' modules. When the config failed
|
|
3
3
|
// to load, the page is its raw text: the server's HTML, replaced when it changes.
|
|
4
|
-
import type { Api, PluginMount, Listener, PageHandle, RenderedPage, StoredPage, WbEvent } from '../define'
|
|
4
|
+
import type { Api, PluginMount, Listener, PageHandle, RenderedPage, SlotName, StoredPage, WbEvent } from '../define'
|
|
5
5
|
import { html } from '../html.ts'
|
|
6
6
|
import { formsApi, trackForms } from './forms'
|
|
7
7
|
import { createToast } from './toast'
|
|
8
8
|
import { markRead, messagesChanged, readsArrived, trackReading, unread as unreadNow } from './read'
|
|
9
|
-
import { READ } from '../status.ts'
|
|
9
|
+
import { READ, SCROLL, fromReader } from '../status.ts'
|
|
10
10
|
import { markdown } from './markdown'
|
|
11
11
|
import { scrollToTarget } from './scroll'
|
|
12
12
|
import { reactive } from './reactive'
|
|
@@ -26,6 +26,8 @@ interface Handoff {
|
|
|
26
26
|
frontmatter: Record<string, unknown>
|
|
27
27
|
/** false on a built page (`whiteboard build`): a file, no server behind it */
|
|
28
28
|
live?: boolean
|
|
29
|
+
/** what the browser runs and how it looks, as the server served them (see /api/stamps) */
|
|
30
|
+
stamps?: { code: string; style: string }
|
|
29
31
|
}
|
|
30
32
|
|
|
31
33
|
const handoff: Handoff = JSON.parse(document.getElementById('wb-page')!.textContent!)
|
|
@@ -55,27 +57,91 @@ function setEvents(next: WbEvent[]) {
|
|
|
55
57
|
async function loadEvents() {
|
|
56
58
|
if (!live) return
|
|
57
59
|
const res = await fetch(url(`/api/events?page=${encodeURIComponent(page)}`))
|
|
58
|
-
const all: WbEvent[] = await res.json()
|
|
59
|
-
|
|
60
|
+
const all: WbEvent[] = (await res.json()).filter((e: WbEvent) => e.page === page)
|
|
61
|
+
// what is still on its way stays: the log doesn't have it yet
|
|
62
|
+
const known = new Set(all.map(e => e.id))
|
|
63
|
+
for (const [id, e] of sending) if (known.has(id)) sending.delete(id)
|
|
64
|
+
setEvents([...all, ...[...sending.values()]])
|
|
65
|
+
scrollFromLog()
|
|
60
66
|
}
|
|
61
67
|
|
|
68
|
+
// `whiteboard scroll` is an event in the log (SCROLL), so a scroll sent while this page wasn't
|
|
69
|
+
// connected still happens once it is. The latest one counts; the first load only notes which that
|
|
70
|
+
// is, so a page loaded later doesn't jump to a place shown before it.
|
|
71
|
+
let scrolledTo: string | null | undefined
|
|
72
|
+
function scrollFromLog() {
|
|
73
|
+
const last = events.findLast(e => e.type === SCROLL && !fromReader(e))
|
|
74
|
+
if (scrolledTo === undefined) return void (scrolledTo = last?.id ?? null)
|
|
75
|
+
if (!last || last.id === scrolledTo) return
|
|
76
|
+
scrolledTo = last.id
|
|
77
|
+
const to = String(last.data?.to ?? '')
|
|
78
|
+
if (!scrollToTarget(root, to)) console.warn(`whiteboard scroll: nothing on the page matches ${JSON.stringify(to)}`)
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// Appending is optimistic: the event is in the page's events at once, marked `sending`, so the page
|
|
82
|
+
// shows it without waiting; the server's copy replaces it once it is in the log. Its id is made
|
|
83
|
+
// here and the server keeps it, so a send that heard nothing back can be sent again without
|
|
84
|
+
// appending it twice: it is retried, and only when none goes through is it taken out again and the
|
|
85
|
+
// append fails.
|
|
86
|
+
|
|
87
|
+
/** How long one send may take before it is tried again, in ms; and how many tries in all. */
|
|
88
|
+
const SEND_TIMEOUT = 8_000
|
|
89
|
+
const TRIES = 4
|
|
90
|
+
|
|
91
|
+
/** Events appended here that the server hasn't confirmed: kept in the events when they are loaded. */
|
|
92
|
+
const sending = new Map<string, WbEvent>()
|
|
93
|
+
|
|
94
|
+
/** An id as the server makes them (isEventId): the time in base 36, a dash, random hex. */
|
|
95
|
+
const newId = () =>
|
|
96
|
+
`${Date.now().toString(36)}-${[...crypto.getRandomValues(new Uint8Array(6))].map(b => b.toString(16).padStart(2, '0')).join('')}`
|
|
97
|
+
|
|
62
98
|
async function append(
|
|
63
99
|
type: string,
|
|
64
100
|
data: Record<string, unknown> = {},
|
|
65
101
|
action: string | null = null,
|
|
66
102
|
): Promise<WbEvent> {
|
|
67
103
|
if (!live) throw new Error('a built page is a file: there is no log to append to')
|
|
68
|
-
const
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
})
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
104
|
+
const id = newId()
|
|
105
|
+
const shown: WbEvent = { id, ts: new Date().toISOString(), page, type, action, data, sending: true }
|
|
106
|
+
sending.set(id, shown)
|
|
107
|
+
setEvents([...events, shown])
|
|
108
|
+
const body = JSON.stringify({ id, page, type, action, data })
|
|
109
|
+
let failure: unknown
|
|
110
|
+
for (let attempt = 0; attempt < TRIES; attempt++) {
|
|
111
|
+
if (attempt) await new Promise(r => setTimeout(r, 500 * 2 ** attempt))
|
|
112
|
+
try {
|
|
113
|
+
const res = await fetch(url('/api/events'), {
|
|
114
|
+
method: 'POST',
|
|
115
|
+
headers: { 'content-type': 'application/json' },
|
|
116
|
+
body,
|
|
117
|
+
// never left hanging: a browser holds a request while it has no connection to send it on
|
|
118
|
+
signal: AbortSignal.timeout(SEND_TIMEOUT),
|
|
119
|
+
})
|
|
120
|
+
// refused: sending it again changes nothing
|
|
121
|
+
if (res.status >= 400 && res.status < 500) {
|
|
122
|
+
failure = new Error(await res.text())
|
|
123
|
+
break
|
|
124
|
+
}
|
|
125
|
+
if (!res.ok) throw new Error(await res.text())
|
|
126
|
+
const saved: WbEvent = await res.json()
|
|
127
|
+
sending.delete(id)
|
|
128
|
+
// the server's copy in its place (a load may have brought it already)
|
|
129
|
+
setEvents([...events.filter(e => e.id !== id && e.id !== saved.id), saved].toSorted(byTime))
|
|
130
|
+
return saved
|
|
131
|
+
} catch (err) {
|
|
132
|
+
failure = err
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
// the log has it already (a load brought it), though no answer to a send came back
|
|
136
|
+
const arrived = !sending.has(id) && events.find(e => e.id === id && !e.sending)
|
|
137
|
+
if (arrived) return arrived
|
|
138
|
+
sending.delete(id)
|
|
139
|
+
setEvents(events.filter(e => e.id !== id))
|
|
140
|
+
throw failure
|
|
77
141
|
}
|
|
78
142
|
|
|
143
|
+
const byTime = (a: WbEvent, b: WbEvent) => (a.ts < b.ts ? -1 : a.ts > b.ts ? 1 : 0)
|
|
144
|
+
|
|
79
145
|
let listeners: Listener[] = []
|
|
80
146
|
const listenersValue = reactive(() => listeners)
|
|
81
147
|
|
|
@@ -101,6 +167,50 @@ function writeTitle() {
|
|
|
101
167
|
document.title = `${list.map(b => `(${b.text}) `).join('')}${baseTitle}`
|
|
102
168
|
}
|
|
103
169
|
|
|
170
|
+
// ---- slots -------------------------------------------------------------------------------------
|
|
171
|
+
|
|
172
|
+
/** The plugins in their order, which is the order of their elements in a slot. */
|
|
173
|
+
const pluginOrder = (handoff.plugins ?? []).map(p => p.name)
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* Mount `owner`'s element in a slot (Api.slot): the one the server rendered with its markup, or a
|
|
177
|
+
* new one, put among the others in plugin order. What `mount` returns runs when it is taken away.
|
|
178
|
+
*/
|
|
179
|
+
function slotFor(owner: string, name: SlotName, mount: (el: HTMLElement) => void | (() => void)): () => void {
|
|
180
|
+
const holder =
|
|
181
|
+
name === 'corner'
|
|
182
|
+
? corner
|
|
183
|
+
: name === 'header'
|
|
184
|
+
? root.querySelector<HTMLElement>('[data-wb-slot=header]')
|
|
185
|
+
: document.querySelector<HTMLElement>(`body > [data-wb-slot=${name}]`)
|
|
186
|
+
if (!holder) return () => {}
|
|
187
|
+
let el = [...holder.children].find((c): c is HTMLElement => c instanceof HTMLElement && c.dataset.plugin === owner)
|
|
188
|
+
if (!el) {
|
|
189
|
+
el = document.createElement('div')
|
|
190
|
+
el.dataset.plugin = owner
|
|
191
|
+
const at = pluginOrder.indexOf(owner)
|
|
192
|
+
const next = [...holder.children].find(
|
|
193
|
+
c => c instanceof HTMLElement && c.dataset.plugin != null && pluginOrder.indexOf(c.dataset.plugin) > at,
|
|
194
|
+
)
|
|
195
|
+
holder.insertBefore(el, next ?? null)
|
|
196
|
+
}
|
|
197
|
+
const unmount = mount(el)
|
|
198
|
+
let gone = false
|
|
199
|
+
return () => {
|
|
200
|
+
if (gone) return
|
|
201
|
+
gone = true
|
|
202
|
+
unmount?.()
|
|
203
|
+
el.remove()
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/** The page's own element in the corner (its toasts), made the first time it is asked for. */
|
|
208
|
+
let pageCorner: HTMLElement | null = null
|
|
209
|
+
const cornerOfPage = (): HTMLElement => {
|
|
210
|
+
if (!pageCorner) slotFor('', 'corner', el => void (pageCorner = el))
|
|
211
|
+
return pageCorner ?? corner
|
|
212
|
+
}
|
|
213
|
+
|
|
104
214
|
const api: Api = {
|
|
105
215
|
page,
|
|
106
216
|
url,
|
|
@@ -129,7 +239,8 @@ const api: Api = {
|
|
|
129
239
|
renderedListeners.add(fn)
|
|
130
240
|
return () => renderedListeners.delete(fn)
|
|
131
241
|
},
|
|
132
|
-
|
|
242
|
+
// the page's own (not a plugin's): before the plugins' in a slot
|
|
243
|
+
slot: (name, mount) => slotFor('', name, mount),
|
|
133
244
|
badge(key, text, order = 0) {
|
|
134
245
|
if (text == null) badges.delete(key)
|
|
135
246
|
else badges.set(key, { text, order, since: badges.get(key)?.since ?? badgeCount++ })
|
|
@@ -139,7 +250,7 @@ const api: Api = {
|
|
|
139
250
|
baseTitle = title
|
|
140
251
|
writeTitle()
|
|
141
252
|
},
|
|
142
|
-
toast: createToast(
|
|
253
|
+
toast: createToast(cornerOfPage),
|
|
143
254
|
...formsApi,
|
|
144
255
|
}
|
|
145
256
|
|
|
@@ -205,54 +316,110 @@ if (!handoff.client)
|
|
|
205
316
|
|
|
206
317
|
// ---- live channel ------------------------------------------------------------------------------
|
|
207
318
|
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
// nothing still redraws, sorts and scrolls, under the reader's hands
|
|
218
|
-
caughtUp()
|
|
219
|
-
}
|
|
220
|
-
es.onerror = () => {
|
|
221
|
-
delete document.documentElement.dataset.wbLive
|
|
319
|
+
// A WebSocket, not server-sent events: a browser keeps six HTTP/1.1 connections to an origin, and
|
|
320
|
+
// every whiteboard tab is on the same one; one stream per tab left none for a form's answer (see
|
|
321
|
+
// src/ws.ts). A WebSocket doesn't reconnect by itself: this does, sooner at first, then every few
|
|
322
|
+
// seconds, and at once when the tab is shown or the network comes back.
|
|
323
|
+
const reloadWhenUp = async () => {
|
|
324
|
+
try {
|
|
325
|
+
if ((await fetch(url('/api/health'), { cache: 'no-store' })).ok) return location.reload()
|
|
326
|
+
} catch {
|
|
327
|
+
/* not up yet */
|
|
222
328
|
}
|
|
223
|
-
|
|
329
|
+
setTimeout(reloadWhenUp, 500)
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
/** What the server says on the live channel, by its name. */
|
|
333
|
+
const onLive: Record<string, (data: unknown) => void> = {
|
|
334
|
+
change: () => {
|
|
224
335
|
for (const fn of changeListeners) fn()
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
|
|
336
|
+
},
|
|
337
|
+
events: () => loadEvents(),
|
|
338
|
+
listeners: () => loadListeners(),
|
|
339
|
+
// under a shared server: what waits in another project changed (the page's menu of it listens)
|
|
340
|
+
elsewhere: () => document.dispatchEvent(new Event('wb-elsewhere')),
|
|
228
341
|
// browser code changed; the page keeps what matters across a reload itself. Reload only once
|
|
229
342
|
// the server answers: reloading while it restarts would leave the browser's error page, which
|
|
230
343
|
// has nothing left in it to recover with.
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
if ((await fetch(url('/api/health'), { cache: 'no-store' })).ok) return location.reload()
|
|
235
|
-
} catch {
|
|
236
|
-
/* not up yet */
|
|
237
|
-
}
|
|
238
|
-
setTimeout(go, 500)
|
|
239
|
-
}
|
|
240
|
-
go()
|
|
241
|
-
})
|
|
242
|
-
// an agent shows the reader a place on the page
|
|
243
|
-
es.addEventListener('scroll', e => {
|
|
244
|
-
const { to } = JSON.parse((e as MessageEvent).data) as { to: string }
|
|
245
|
-
if (!scrollToTarget(root, to)) console.warn(`whiteboard scroll: nothing on the page matches ${JSON.stringify(to)}`)
|
|
246
|
-
})
|
|
247
|
-
es.addEventListener('style', () => {
|
|
344
|
+
reload: () => reloadWhenUp(),
|
|
345
|
+
style: () => {
|
|
346
|
+
stamps = null
|
|
248
347
|
for (const link of document.querySelectorAll<HTMLLinkElement>('link[rel=stylesheet]')) {
|
|
249
348
|
const url = new URL(link.href)
|
|
250
349
|
if (url.origin !== location.origin) continue
|
|
251
350
|
url.searchParams.set('t', String(Date.now()))
|
|
252
351
|
link.href = url.pathname + url.search
|
|
253
352
|
}
|
|
254
|
-
}
|
|
353
|
+
},
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
// What the browser runs and how it looks, as this page has them; null once a style message refreshed
|
|
357
|
+
// the stylesheets without a stamp to go with it (the next comparison takes the server's then).
|
|
358
|
+
let stamps: { code: string; style: string } | null = handoff.stamps ?? null
|
|
359
|
+
|
|
360
|
+
/**
|
|
361
|
+
* Back on the live channel: what changed while this page wasn't on it, it never heard of. Browser
|
|
362
|
+
* code that changed reloads the page; stylesheets that changed are refreshed in place.
|
|
363
|
+
*/
|
|
364
|
+
async function compareStamps() {
|
|
365
|
+
let now: { code: string; style: string }
|
|
366
|
+
try {
|
|
367
|
+
now = await (await fetch(url('/api/stamps'), { cache: 'no-store' })).json()
|
|
368
|
+
} catch {
|
|
369
|
+
return
|
|
370
|
+
}
|
|
371
|
+
if (stamps && now.code !== stamps.code) return reloadWhenUp()
|
|
372
|
+
if (stamps && now.style !== stamps.style) onLive.style!(null)
|
|
373
|
+
stamps = now
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
let socket: WebSocket | null = null
|
|
377
|
+
let retries = 0
|
|
378
|
+
let retryTimer: ReturnType<typeof setTimeout> | undefined
|
|
379
|
+
|
|
380
|
+
function connect() {
|
|
381
|
+
clearTimeout(retryTimer)
|
|
382
|
+
if (socket && socket.readyState <= WebSocket.OPEN) return
|
|
383
|
+
const at = new URL(url(`/api/live?page=${encodeURIComponent(page)}${isDir ? '&dir' : ''}`), location.href)
|
|
384
|
+
at.protocol = location.protocol === 'https:' ? 'wss:' : 'ws:'
|
|
385
|
+
const ws = (socket = new WebSocket(at))
|
|
386
|
+
ws.onopen = () => {
|
|
387
|
+
retries = 0
|
|
388
|
+
document.documentElement.dataset.wbLive = ''
|
|
389
|
+
// after a server restart: who is listening now, and what was answered meanwhile, here and
|
|
390
|
+
// elsewhere
|
|
391
|
+
loadListeners()
|
|
392
|
+
loadEvents()
|
|
393
|
+
void compareStamps()
|
|
394
|
+
document.dispatchEvent(new Event('wb-elsewhere'))
|
|
395
|
+
// and the page, if it changed while the channel wasn't open yet (just after loading, or while
|
|
396
|
+
// the server restarted): that change was never announced. Only then: an update that changes
|
|
397
|
+
// nothing still redraws, sorts and scrolls, under the reader's hands
|
|
398
|
+
caughtUp()
|
|
399
|
+
}
|
|
400
|
+
ws.onmessage = e => {
|
|
401
|
+
let message: { event?: string; data?: unknown }
|
|
402
|
+
try {
|
|
403
|
+
message = JSON.parse(String(e.data))
|
|
404
|
+
} catch {
|
|
405
|
+
return
|
|
406
|
+
}
|
|
407
|
+
if (message.event) onLive[message.event]?.(message.data)
|
|
408
|
+
}
|
|
409
|
+
ws.onclose = () => {
|
|
410
|
+
if (socket !== ws) return
|
|
411
|
+
socket = null
|
|
412
|
+
delete document.documentElement.dataset.wbLive
|
|
413
|
+
retryTimer = setTimeout(connect, Math.min(250 * 2 ** retries++, 5000))
|
|
414
|
+
}
|
|
255
415
|
}
|
|
416
|
+
// back at once: a tab shown again (a browser may have let its connection go), the network back
|
|
417
|
+
document.addEventListener('visibilitychange', () => {
|
|
418
|
+
if (document.visibilityState === 'visible' && !socket) connect()
|
|
419
|
+
})
|
|
420
|
+
addEventListener('online', () => {
|
|
421
|
+
if (!socket) connect()
|
|
422
|
+
})
|
|
256
423
|
|
|
257
424
|
await Promise.all([loadEvents(), loadListeners()])
|
|
258
425
|
if (handoff.client) {
|
|
@@ -305,7 +472,7 @@ await Promise.all(
|
|
|
305
472
|
const mount: PluginMount = (await import(moduleUrl(ext.client))).default
|
|
306
473
|
if (typeof mount !== 'function')
|
|
307
474
|
throw new Error('the client module must export a mount(root, page, api) function as default')
|
|
308
|
-
await mount(root, handleFor(ext.name), api)
|
|
475
|
+
await mount(root, handleFor(ext.name), { ...api, slot: (name, mount) => slotFor(ext.name, name, mount) })
|
|
309
476
|
} catch (err) {
|
|
310
477
|
console.error(`plugin ${ext.name}:`, err)
|
|
311
478
|
root.insertAdjacentHTML(
|
package/src/client/toast.ts
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
|
-
// Toasts (api.toast): short labels in the page's corner (
|
|
1
|
+
// Toasts (api.toast): short labels in the page's corner (its element in the corner slot, above what covers the bottom
|
|
2
2
|
// of the window) that say what is going on and take the reader there: "→ unanswered (2)",
|
|
3
3
|
// "↓ new (3)", "Sent, but no agent is listening". One goes after a few seconds, or on a click; one
|
|
4
4
|
// with `ms: 0` stays until closed, and changes its text, or hides and shows again, through what
|
|
5
5
|
// toast() returns. A click runs `onClick` (and closes a toast that goes by itself); `onDismiss`
|
|
6
|
-
// adds a ✕ that runs it and closes the toast.
|
|
6
|
+
// adds a ✕ that runs it and closes the toast. A toast stays short: what more there is to say goes in
|
|
7
|
+
// `details`, which a ▸ unfolds under it; unfolded, it doesn't go by itself until folded again. Their look is the core's (server.ts), in the page's
|
|
7
8
|
// theme colours.
|
|
8
9
|
import type { Toast, ToastOptions } from '../define.ts'
|
|
9
10
|
|
|
@@ -21,9 +22,14 @@ function place(corner: HTMLElement) {
|
|
|
21
22
|
export function createToast(corner: () => HTMLElement) {
|
|
22
23
|
return function toast(
|
|
23
24
|
text: string,
|
|
24
|
-
{ kind = 'info', ms = 7000, onClick, onDismiss, name, order = 0 }: ToastOptions = {},
|
|
25
|
+
{ kind = 'info', ms = 7000, onClick, onDismiss, name, order = 0, details }: ToastOptions = {},
|
|
25
26
|
): Toast {
|
|
26
27
|
const el = document.createElement('button')
|
|
28
|
+
let timer: ReturnType<typeof setTimeout> | undefined
|
|
29
|
+
const close = () => {
|
|
30
|
+
clearTimeout(timer)
|
|
31
|
+
el.remove()
|
|
32
|
+
}
|
|
27
33
|
el.type = 'button'
|
|
28
34
|
el.className = 'wb-toast'
|
|
29
35
|
el.dataset.kind = kind
|
|
@@ -33,6 +39,28 @@ export function createToast(corner: () => HTMLElement) {
|
|
|
33
39
|
label.className = 'wb-toast-text'
|
|
34
40
|
label.textContent = text
|
|
35
41
|
el.append(label)
|
|
42
|
+
let more: HTMLElement | null = null
|
|
43
|
+
let detail: HTMLElement | null = null
|
|
44
|
+
if (details) {
|
|
45
|
+
more = document.createElement('span')
|
|
46
|
+
more.className = 'wb-toast-more'
|
|
47
|
+
detail = document.createElement('span')
|
|
48
|
+
detail.className = 'wb-toast-details'
|
|
49
|
+
detail.textContent = details
|
|
50
|
+
// read out with the toast, folded or not
|
|
51
|
+
el.setAttribute('aria-description', details)
|
|
52
|
+
el.append(more, detail)
|
|
53
|
+
fold(false)
|
|
54
|
+
}
|
|
55
|
+
function fold(open: boolean) {
|
|
56
|
+
el.toggleAttribute('data-open', open)
|
|
57
|
+
detail!.hidden = !open
|
|
58
|
+
more!.textContent = open ? '▾' : '▸'
|
|
59
|
+
more!.title = open ? 'Less' : 'More'
|
|
60
|
+
// being read: it stays; folded again, it goes as it would have
|
|
61
|
+
clearTimeout(timer)
|
|
62
|
+
if (!open && ms) timer = setTimeout(close, ms)
|
|
63
|
+
}
|
|
36
64
|
if (onDismiss) {
|
|
37
65
|
const x = document.createElement('span')
|
|
38
66
|
x.className = 'wb-toast-dismiss'
|
|
@@ -40,17 +68,19 @@ export function createToast(corner: () => HTMLElement) {
|
|
|
40
68
|
x.title = x.ariaLabel = 'Dismiss'
|
|
41
69
|
el.append(x)
|
|
42
70
|
}
|
|
43
|
-
const close = () => el.remove()
|
|
44
71
|
el.addEventListener('click', e => {
|
|
45
|
-
|
|
72
|
+
const target = e.target as HTMLElement
|
|
73
|
+
if (onDismiss && target.closest('.wb-toast-dismiss')) {
|
|
46
74
|
close()
|
|
47
75
|
onDismiss()
|
|
48
76
|
return
|
|
49
77
|
}
|
|
78
|
+
// the ▸, or the details themselves: fold or unfold, nothing else
|
|
79
|
+
if (details && target.closest('.wb-toast-more, .wb-toast-details')) return fold(!el.hasAttribute('data-open'))
|
|
50
80
|
if (ms) close()
|
|
51
81
|
onClick?.()
|
|
52
82
|
})
|
|
53
|
-
if (ms) setTimeout(close, ms)
|
|
83
|
+
if (ms && !details) timer = setTimeout(close, ms)
|
|
54
84
|
place(corner()).append(el)
|
|
55
85
|
return {
|
|
56
86
|
close,
|
package/src/define.ts
CHANGED
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
import type { IncomingMessage, ServerResponse } from 'node:http'
|
|
10
10
|
import type { Html } from './html.ts'
|
|
11
11
|
|
|
12
|
-
// HTML from a template literal, escaped by default: for a block, a plugin's `
|
|
12
|
+
// HTML from a template literal, escaped by default: for a block, a plugin's `slots`
|
|
13
13
|
export { html, raw, escape, Html } from './html.ts'
|
|
14
14
|
export { windowFrame, type WindowOptions } from './pages/window.ts'
|
|
15
15
|
|
|
@@ -185,11 +185,9 @@ export interface PluginContext {
|
|
|
185
185
|
dataDir: string
|
|
186
186
|
/**
|
|
187
187
|
* whether a request carries the project's token: for a check of its own, beyond a route's `auth`.
|
|
188
|
-
* No request (`
|
|
188
|
+
* No request (`slots` for a page being built) has none.
|
|
189
189
|
*/
|
|
190
190
|
authorized(req: IncomingMessage | null): boolean
|
|
191
|
-
/** the URL that serves a file of the plugin (its browser module, a stylesheet) */
|
|
192
|
-
asset(file: URL | string): string
|
|
193
191
|
}
|
|
194
192
|
|
|
195
193
|
/**
|
|
@@ -198,13 +196,29 @@ export interface PluginContext {
|
|
|
198
196
|
*/
|
|
199
197
|
export type FieldType = 'string' | 'number' | 'boolean' | 'string[]' | 'string?' | 'number?' | 'boolean?' | 'string[]?'
|
|
200
198
|
|
|
199
|
+
/**
|
|
200
|
+
* The places on a page the core owns, for what a plugin puts there (each plugin its own element in
|
|
201
|
+
* each, in plugin order; see Api.slot): `header`, in the page's header bar before the theme button;
|
|
202
|
+
* `aside`, `bottom` and `overlay`, at the end of <body>, laid out as their content says (a panel
|
|
203
|
+
* beside the text, a drawer, something over the page); `corner`, the column of floating labels at
|
|
204
|
+
* the bottom right.
|
|
205
|
+
*/
|
|
206
|
+
export type SlotName = 'header' | 'aside' | 'bottom' | 'overlay' | 'corner'
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* What a plugin's `slots` gives a page on the server: `head` at the end of <head>, and markup for
|
|
210
|
+
* its element in each slot but `header` (the header bar is the page's own render; a plugin adds to
|
|
211
|
+
* it in the browser).
|
|
212
|
+
*/
|
|
213
|
+
export type SlotMarkup = { [K in 'head' | Exclude<SlotName, 'header'>]?: string | Html }
|
|
214
|
+
|
|
201
215
|
export interface PluginHooks {
|
|
202
216
|
/** by method and path, `'POST /start'`: mounted at `/api/<name>/start` */
|
|
203
217
|
routes?: Record<string, Route>
|
|
204
218
|
/**
|
|
205
|
-
* Markup for the page
|
|
206
|
-
*
|
|
207
|
-
*
|
|
219
|
+
* Markup for the page's slots (SlotMarkup), on the server: there on the first render, so an
|
|
220
|
+
* element in it can take part in the view transition between two pages. The browser module then
|
|
221
|
+
* mounts on its element in each slot (Api.slot). Called for every page; null adds nothing.
|
|
208
222
|
*
|
|
209
223
|
* Something that covers an edge of the window (a drawer at the bottom) says how much of it, as a
|
|
210
224
|
* length in `--wb-inset-bottom` on :root, registered with `@property` so that it computes to
|
|
@@ -215,17 +229,12 @@ export interface PluginHooks {
|
|
|
215
229
|
* browser asked for: what is only for the project's own browser (the terminal's drawer, behind
|
|
216
230
|
* `authorized`) has nothing to add there.
|
|
217
231
|
*/
|
|
218
|
-
|
|
219
|
-
req: IncomingMessage | null,
|
|
220
|
-
):
|
|
221
|
-
| { head?: string | Html; body?: string | Html }
|
|
222
|
-
| null
|
|
223
|
-
| Promise<{ head?: string | Html; body?: string | Html } | null>
|
|
232
|
+
slots?(req: IncomingMessage | null): SlotMarkup | null | Promise<SlotMarkup | null>
|
|
224
233
|
/**
|
|
225
234
|
* A browser module whose default export is an `PluginMount`: on every page, the core imports
|
|
226
235
|
* it once the page has mounted, and hands it the page and its api. For
|
|
227
236
|
* behaviour on the page's own content (its forms, say); markup of the plugin's own can come
|
|
228
|
-
* from `
|
|
237
|
+
* from `slots` instead. A file in the plugin's public folder: `'client.js'`.
|
|
229
238
|
*/
|
|
230
239
|
client?: URL | string
|
|
231
240
|
}
|
|
@@ -479,6 +488,11 @@ export interface WbEvent {
|
|
|
479
488
|
from?: 'cli'
|
|
480
489
|
/** in the browser: an agent has picked this event up (`whiteboard wait` or `watch` handed it out) */
|
|
481
490
|
pickedUp?: true
|
|
491
|
+
/**
|
|
492
|
+
* in the browser: appended here and shown at once, but the server hasn't confirmed it yet. Show
|
|
493
|
+
* it as on its way ("sending…"), not as done. Should it fail, it leaves the events again.
|
|
494
|
+
*/
|
|
495
|
+
sending?: true
|
|
482
496
|
}
|
|
483
497
|
|
|
484
498
|
/**
|
|
@@ -511,6 +525,11 @@ export interface ToastOptions {
|
|
|
511
525
|
name?: string
|
|
512
526
|
/** where it is among the corner's toasts: lower first (default 0) */
|
|
513
527
|
order?: number
|
|
528
|
+
/**
|
|
529
|
+
* more about it, for whoever wants to know: the toast stays a short line, with a ▸ that unfolds
|
|
530
|
+
* this under it (and keeps it open while it is read). Keep `text` a few words, and put the why here.
|
|
531
|
+
*/
|
|
532
|
+
details?: string
|
|
514
533
|
}
|
|
515
534
|
|
|
516
535
|
/** A toast on the screen: change what it says (a count), hide it and show it again, or close it. */
|
|
@@ -572,11 +591,12 @@ export interface Api {
|
|
|
572
591
|
/** called after each `rendered()`; returns an unsubscribe */
|
|
573
592
|
onRendered(fn: () => void): () => void
|
|
574
593
|
/**
|
|
575
|
-
*
|
|
576
|
-
*
|
|
577
|
-
*
|
|
594
|
+
* Mount on this plugin's own element in a slot (SlotName): `mount` is called with it, holding
|
|
595
|
+
* what the plugin's `slots` rendered there on the server, or empty, and what it returns is called
|
|
596
|
+
* when the element is taken away. The plugin never makes or looks up the element. Returns what
|
|
597
|
+
* takes it away. A page without the slot (raw text has no header bar) mounts nothing.
|
|
578
598
|
*/
|
|
579
|
-
|
|
599
|
+
slot(name: SlotName, mount: (el: HTMLElement) => void | (() => void)): () => void
|
|
580
600
|
/**
|
|
581
601
|
* A count in the tab title, under `key`: `(text)` in front of the page's title; null removes it.
|
|
582
602
|
* Lower `order` comes first (default 0), so what waits for the reader can lead.
|
|
@@ -694,6 +714,12 @@ interface BaseContext {
|
|
|
694
714
|
/** absolute path of the markdown file, and its folder (resolve files next to the page with it) */
|
|
695
715
|
file: string
|
|
696
716
|
dir: string
|
|
717
|
+
/**
|
|
718
|
+
* The address of a file named relative to the page (an image's `src`): served, the server's; in a
|
|
719
|
+
* build, of a copy beside the built page. A URL, an address from the root, or a file that isn't
|
|
720
|
+
* there comes back as written.
|
|
721
|
+
*/
|
|
722
|
+
fileUrl(src: string): string
|
|
697
723
|
name: string
|
|
698
724
|
attrs: Record<string, string>
|
|
699
725
|
parseAttrs(s: string): Record<string, string>
|