@bigmistqke/whiteboard 0.3.1 → 0.5.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 +119 -18
- package/bin/whiteboard.ts +300 -71
- package/dist/bin/whiteboard.js +21 -0
- package/dist/client/runtime.js +132 -54
- 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/draft/README.md +14 -0
- package/dist/plugins/draft/index.js +21 -0
- package/dist/plugins/draft/public/style.css +21 -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 +7 -1
- package/dist/plugins/mermaid/index.js +3 -1
- package/dist/plugins/mermaid/{style.css → public/style.css} +7 -2
- package/dist/plugins/steps/README.md +42 -0
- package/dist/plugins/steps/index.js +99 -0
- package/dist/plugins/steps/public/client.js +1 -0
- package/dist/plugins/steps/public/style.css +77 -0
- package/dist/plugins/terminal/host.js +9 -3
- package/dist/plugins/terminal/index.js +10 -9
- package/dist/plugins/terminal/{style.css → public/style.css} +199 -45
- package/dist/plugins/terminal/sizes.js +2 -1
- package/dist/plugins/toc/README.md +21 -3
- package/dist/plugins/toc/index.js +12 -5
- package/dist/plugins/toc/public/client.js +1 -0
- package/dist/plugins/toc/public/style.css +99 -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/auth.js +2 -1
- package/dist/src/client/page/base.css +1078 -199
- package/dist/src/client/page/main.js +188 -39
- package/dist/src/loader.js +14 -3
- 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 +106 -20
- package/dist/src/pages/window.js +31 -0
- package/dist/src/public.js +24 -0
- package/dist/src/server.js +251 -23
- package/package.json +5 -2
- 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 +33 -21
- 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/draft/README.md +14 -0
- package/plugins/draft/index.ts +23 -0
- package/plugins/draft/public/style.css +21 -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 +20 -1
- package/plugins/mermaid/index.ts +7 -3
- package/plugins/mermaid/{style.css → public/style.css} +7 -2
- package/plugins/steps/README.md +42 -0
- package/plugins/steps/client.ts +66 -0
- package/plugins/steps/index.ts +127 -0
- package/plugins/steps/public/style.css +77 -0
- package/plugins/terminal/client.ts +105 -48
- package/plugins/terminal/host.ts +143 -35
- 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 +16 -4
- package/plugins/toc/README.md +21 -3
- package/plugins/toc/client.ts +90 -30
- package/plugins/toc/index.ts +26 -10
- package/plugins/toc/public/style.css +99 -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 +28 -9
- 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 +124 -42
- 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 +170 -40
- package/src/client/scroll.ts +14 -4
- package/src/client/toast.ts +19 -5
- package/src/config.ts +20 -5
- package/src/define.ts +119 -31
- 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 +81 -12
- package/src/package.ts +9 -8
- package/src/pages/blocks.ts +20 -14
- package/src/pages/index.ts +2 -2
- package/src/pages/markdown.ts +113 -34
- package/src/pages/render.ts +233 -41
- package/src/pages/window.ts +78 -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 +591 -110
- 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/client/runtime.ts
CHANGED
|
@@ -5,44 +5,65 @@ import type { Api, PluginMount, Listener, PageHandle, RenderedPage, StoredPage,
|
|
|
5
5
|
import { html } from '../html.ts'
|
|
6
6
|
import { formsApi, trackForms } from './forms'
|
|
7
7
|
import { createToast } from './toast'
|
|
8
|
-
import { markRead, messagesChanged, readsArrived, trackReading, unread } from './read'
|
|
8
|
+
import { markRead, messagesChanged, readsArrived, trackReading, unread as unreadNow } from './read'
|
|
9
9
|
import { READ } from '../status.ts'
|
|
10
10
|
import { markdown } from './markdown'
|
|
11
11
|
import { scrollToTarget } from './scroll'
|
|
12
|
+
import { reactive } from './reactive'
|
|
12
13
|
|
|
13
14
|
interface Handoff {
|
|
14
|
-
id: string
|
|
15
|
+
id: string
|
|
16
|
+
kind: StoredPage['kind']
|
|
17
|
+
data: unknown
|
|
15
18
|
/** the page module; null for raw text */
|
|
16
19
|
client: string | null
|
|
17
20
|
/** the plugins' browser modules */
|
|
18
21
|
plugins: { name: string; client: string }[]
|
|
19
22
|
/** the event types that, from the agent, are messages for the reader */
|
|
20
23
|
messages: string[]
|
|
24
|
+
/** the page's frontmatter (PageHandle.frontmatter) */
|
|
25
|
+
frontmatter: Record<string, unknown>
|
|
26
|
+
/** false on a built page (`whiteboard build`): a file, no server behind it */
|
|
27
|
+
live?: boolean
|
|
21
28
|
}
|
|
22
29
|
|
|
23
30
|
const handoff: Handoff = JSON.parse(document.getElementById('wb-page')!.textContent!)
|
|
24
31
|
const root = document.getElementById('wb-root')!
|
|
25
32
|
const page = handoff.id
|
|
26
33
|
const isDir = handoff.kind === 'pages' || handoff.kind === 'folder'
|
|
34
|
+
// a built page has no server: no events, no listeners, no live channel, nothing to record
|
|
35
|
+
const live = handoff.live !== false
|
|
36
|
+
// a module's path as the page names it: a built page's are relative to the page, which an import()
|
|
37
|
+
// here would take as relative to this module
|
|
38
|
+
const moduleUrl = (path: string) => new URL(path, document.baseURI).href
|
|
27
39
|
|
|
28
40
|
// ---- events ------------------------------------------------------------------------------------
|
|
29
41
|
|
|
30
42
|
let events: WbEvent[] = []
|
|
31
|
-
const
|
|
43
|
+
const eventsValue = reactive(() => events)
|
|
44
|
+
// what is unread changes with the events, and with a message read here before the log has it
|
|
45
|
+
const unread = reactive(unreadNow)
|
|
32
46
|
function setEvents(next: WbEvent[]) {
|
|
33
47
|
events = next
|
|
34
48
|
readsArrived(events)
|
|
35
|
-
|
|
49
|
+
eventsValue.notify()
|
|
50
|
+
unread.notify()
|
|
36
51
|
messagesChanged()
|
|
37
52
|
}
|
|
38
53
|
|
|
39
54
|
async function loadEvents() {
|
|
55
|
+
if (!live) return
|
|
40
56
|
const res = await fetch(`/api/events?page=${encodeURIComponent(page)}`)
|
|
41
57
|
const all: WbEvent[] = await res.json()
|
|
42
58
|
setEvents(all.filter(e => e.page === page))
|
|
43
59
|
}
|
|
44
60
|
|
|
45
|
-
async function append(
|
|
61
|
+
async function append(
|
|
62
|
+
type: string,
|
|
63
|
+
data: Record<string, unknown> = {},
|
|
64
|
+
action: string | null = null,
|
|
65
|
+
): Promise<WbEvent> {
|
|
66
|
+
if (!live) throw new Error('a built page is a file: there is no log to append to')
|
|
46
67
|
const res = await fetch('/api/events', {
|
|
47
68
|
method: 'POST',
|
|
48
69
|
headers: { 'content-type': 'application/json' },
|
|
@@ -55,11 +76,12 @@ async function append(type: string, data: Record<string, unknown> = {}, action:
|
|
|
55
76
|
}
|
|
56
77
|
|
|
57
78
|
let listeners: Listener[] = []
|
|
58
|
-
const
|
|
79
|
+
const listenersValue = reactive(() => listeners)
|
|
59
80
|
|
|
60
81
|
async function loadListeners() {
|
|
82
|
+
if (!live) return
|
|
61
83
|
listeners = await (await fetch(`/api/listeners?page=${encodeURIComponent(page)}`)).json()
|
|
62
|
-
|
|
84
|
+
listenersValue.notify()
|
|
63
85
|
}
|
|
64
86
|
|
|
65
87
|
const renderedListeners = new Set<() => void>()
|
|
@@ -67,36 +89,54 @@ const renderedListeners = new Set<() => void>()
|
|
|
67
89
|
// ---- the corner and the title ------------------------------------------------------------------
|
|
68
90
|
|
|
69
91
|
// the server puts it in the page; a page from a server older than this runtime has none
|
|
70
|
-
const corner =
|
|
92
|
+
const corner =
|
|
93
|
+
document.getElementById('wb-dock') ??
|
|
94
|
+
document.body.appendChild(Object.assign(document.createElement('div'), { id: 'wb-dock' }))
|
|
71
95
|
let baseTitle = document.title
|
|
72
96
|
const badges = new Map<string, { text: string; order: number; since: number }>()
|
|
73
97
|
let badgeCount = 0
|
|
74
98
|
function writeTitle() {
|
|
75
|
-
const list = [...badges.values()].
|
|
99
|
+
const list = [...badges.values()].toSorted((a, b) => a.order - b.order || a.since - b.since)
|
|
76
100
|
document.title = `${list.map(b => `(${b.text}) `).join('')}${baseTitle}`
|
|
77
101
|
}
|
|
78
102
|
|
|
79
103
|
const api: Api = {
|
|
80
104
|
page,
|
|
81
|
-
events:
|
|
82
|
-
onEvents(fn) { eventListeners.add(fn); return () => eventListeners.delete(fn) },
|
|
105
|
+
events: eventsValue,
|
|
83
106
|
last: type => events.findLast(e => e.type === type),
|
|
84
107
|
pickedUp: id => !!events.find(e => e.id === id)?.pickedUp,
|
|
85
108
|
unread,
|
|
86
|
-
markRead
|
|
109
|
+
markRead: ids => {
|
|
110
|
+
markRead(ids)
|
|
111
|
+
unread.notify()
|
|
112
|
+
},
|
|
87
113
|
markdown,
|
|
88
114
|
append,
|
|
89
|
-
listeners:
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
115
|
+
listeners: listenersValue,
|
|
116
|
+
rendered() {
|
|
117
|
+
// the new page is in place: what came with its render is the page's now, before anyone hears
|
|
118
|
+
// of the update, so a listener reads the new values
|
|
119
|
+
const frontmatterChanged = JSON.stringify(nextFrontmatter) !== JSON.stringify(frontmatter)
|
|
120
|
+
data = nextData
|
|
121
|
+
frontmatter = nextFrontmatter
|
|
122
|
+
for (const fn of renderedListeners) fn()
|
|
123
|
+
dataValue.notify()
|
|
124
|
+
if (frontmatterChanged) frontmatterValue.notify()
|
|
125
|
+
},
|
|
126
|
+
onRendered(fn) {
|
|
127
|
+
renderedListeners.add(fn)
|
|
128
|
+
return () => renderedListeners.delete(fn)
|
|
129
|
+
},
|
|
93
130
|
corner: () => corner,
|
|
94
131
|
badge(key, text, order = 0) {
|
|
95
132
|
if (text == null) badges.delete(key)
|
|
96
133
|
else badges.set(key, { text, order, since: badges.get(key)?.since ?? badgeCount++ })
|
|
97
134
|
writeTitle()
|
|
98
135
|
},
|
|
99
|
-
title(title) {
|
|
136
|
+
title(title) {
|
|
137
|
+
baseTitle = title
|
|
138
|
+
writeTitle()
|
|
139
|
+
},
|
|
100
140
|
toast: createToast(() => corner),
|
|
101
141
|
...formsApi,
|
|
102
142
|
}
|
|
@@ -111,17 +151,55 @@ async function getJson<T>(url: string): Promise<T> {
|
|
|
111
151
|
return res.json()
|
|
112
152
|
}
|
|
113
153
|
|
|
154
|
+
// what came with the page, and what came with its last render: the page's once the new page is in
|
|
155
|
+
// place (api.rendered), so a listener never sees new values over the old page
|
|
156
|
+
let data: unknown = handoff.data
|
|
157
|
+
let nextData: unknown = data
|
|
158
|
+
let frontmatter: Record<string, unknown> = handoff.frontmatter ?? {}
|
|
159
|
+
let nextFrontmatter = frontmatter
|
|
160
|
+
const dataValue = reactive(() => data)
|
|
161
|
+
const frontmatterValue = reactive(() => frontmatter)
|
|
162
|
+
const nothing = reactive((): unknown => undefined)
|
|
114
163
|
const handle: PageHandle = {
|
|
164
|
+
settings: nothing,
|
|
115
165
|
id: page,
|
|
116
166
|
kind: handoff.kind,
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
167
|
+
live,
|
|
168
|
+
data: dataValue,
|
|
169
|
+
frontmatter: frontmatterValue,
|
|
170
|
+
onChange(fn) {
|
|
171
|
+
changeListeners.add(fn)
|
|
172
|
+
return () => changeListeners.delete(fn)
|
|
173
|
+
},
|
|
174
|
+
render: async () => {
|
|
175
|
+
const next = await getJson<RenderedPage>(`/api/render?page=${encodeURIComponent(page)}`)
|
|
176
|
+
nextData = next.data ?? null
|
|
177
|
+
nextFrontmatter = next.frontmatter ?? {}
|
|
178
|
+
return next
|
|
179
|
+
},
|
|
120
180
|
stored: () => getJson<StoredPage>(`/api/page?page=${encodeURIComponent(page)}`),
|
|
121
181
|
}
|
|
122
182
|
|
|
183
|
+
/** Did the page change since it was rendered here (its file's time, or it came to be)? Then say so. */
|
|
184
|
+
async function caughtUp() {
|
|
185
|
+
let now: number | null
|
|
186
|
+
try {
|
|
187
|
+
now =
|
|
188
|
+
(await getJson<RenderedPage & { mtime: number | null }>(`/api/render?page=${encodeURIComponent(page)}`)).mtime ??
|
|
189
|
+
null
|
|
190
|
+
} catch {
|
|
191
|
+
now = null
|
|
192
|
+
}
|
|
193
|
+
const shown = (data as { mtime?: number | null } | null)?.mtime ?? null
|
|
194
|
+
if (now !== shown) for (const fn of changeListeners) fn()
|
|
195
|
+
}
|
|
196
|
+
|
|
123
197
|
// raw text: the server's HTML is the page, replaced when the page changes
|
|
124
|
-
if (!handoff.client)
|
|
198
|
+
if (!handoff.client)
|
|
199
|
+
handle.onChange(async () => {
|
|
200
|
+
root.innerHTML = (await handle.render()).html
|
|
201
|
+
api.rendered()
|
|
202
|
+
})
|
|
125
203
|
|
|
126
204
|
// ---- live channel ------------------------------------------------------------------------------
|
|
127
205
|
|
|
@@ -132,9 +210,17 @@ function connect() {
|
|
|
132
210
|
// after a server restart: who is listening now, and what was answered meanwhile
|
|
133
211
|
loadListeners()
|
|
134
212
|
loadEvents()
|
|
213
|
+
// and the page, if it changed while the channel wasn't open yet (just after loading, or while
|
|
214
|
+
// the server restarted): that change was never announced. Only then: an update that changes
|
|
215
|
+
// nothing still redraws, sorts and scrolls, under the reader's hands
|
|
216
|
+
caughtUp()
|
|
217
|
+
}
|
|
218
|
+
es.onerror = () => {
|
|
219
|
+
delete document.documentElement.dataset.wbLive
|
|
135
220
|
}
|
|
136
|
-
es.
|
|
137
|
-
|
|
221
|
+
es.addEventListener('change', () => {
|
|
222
|
+
for (const fn of changeListeners) fn()
|
|
223
|
+
})
|
|
138
224
|
es.addEventListener('events', () => loadEvents())
|
|
139
225
|
es.addEventListener('listeners', () => loadListeners())
|
|
140
226
|
// browser code changed; the page keeps what matters across a reload itself. Reload only once
|
|
@@ -142,7 +228,11 @@ function connect() {
|
|
|
142
228
|
// has nothing left in it to recover with.
|
|
143
229
|
es.addEventListener('reload', () => {
|
|
144
230
|
const go = async () => {
|
|
145
|
-
try {
|
|
231
|
+
try {
|
|
232
|
+
if ((await fetch('/api/health', { cache: 'no-store' })).ok) return location.reload()
|
|
233
|
+
} catch {
|
|
234
|
+
/* not up yet */
|
|
235
|
+
}
|
|
146
236
|
setTimeout(go, 500)
|
|
147
237
|
}
|
|
148
238
|
go()
|
|
@@ -165,25 +255,65 @@ function connect() {
|
|
|
165
255
|
await Promise.all([loadEvents(), loadListeners()])
|
|
166
256
|
if (handoff.client) {
|
|
167
257
|
try {
|
|
168
|
-
const mount: PluginMount = (await import(handoff.client)).default
|
|
258
|
+
const mount: PluginMount = (await import(moduleUrl(handoff.client))).default
|
|
169
259
|
await mount(root, handle, api)
|
|
170
260
|
} catch (err) {
|
|
171
261
|
console.error('page:', err)
|
|
172
|
-
root.insertAdjacentHTML(
|
|
262
|
+
root.insertAdjacentHTML(
|
|
263
|
+
'afterbegin',
|
|
264
|
+
String(html`<pre class="wb-error" role="alert">the page failed to start: ${(err as Error).message}</pre>`),
|
|
265
|
+
)
|
|
173
266
|
}
|
|
174
267
|
}
|
|
175
|
-
// named forms, once the page is on the page
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
|
|
268
|
+
// named forms, once the page is on the page, and what the reader read: neither on a built page
|
|
269
|
+
if (live) {
|
|
270
|
+
trackForms(root, page, api)
|
|
271
|
+
trackReading({
|
|
272
|
+
messages: handoff.messages ?? [],
|
|
273
|
+
events: () => events,
|
|
274
|
+
send: ids => {
|
|
275
|
+
append(READ, { ids }).catch(err => console.error('marking read:', err))
|
|
276
|
+
},
|
|
277
|
+
})
|
|
278
|
+
}
|
|
279
|
+
/**
|
|
280
|
+
* A plugin's page handle: the page's, with its own settings, the page's frontmatter under `plugins:`
|
|
281
|
+
* by its name, heard of only when they changed.
|
|
282
|
+
*/
|
|
283
|
+
function handleFor(name: string): PageHandle {
|
|
284
|
+
const own = () => {
|
|
285
|
+
const plugins = frontmatter.plugins
|
|
286
|
+
return plugins && typeof plugins === 'object' ? (plugins as Record<string, unknown>)[name] : undefined
|
|
187
287
|
}
|
|
188
|
-
|
|
189
|
-
|
|
288
|
+
const settings = reactive(own)
|
|
289
|
+
let last = JSON.stringify(own() ?? null)
|
|
290
|
+
frontmatterValue.on(() => {
|
|
291
|
+
const now = JSON.stringify(own() ?? null)
|
|
292
|
+
if (now === last) return
|
|
293
|
+
last = now
|
|
294
|
+
settings.notify()
|
|
295
|
+
})
|
|
296
|
+
return { ...handle, settings }
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
// the plugins' modules after the page, so that what they work on is there; one failing leaves the others
|
|
300
|
+
await Promise.all(
|
|
301
|
+
(handoff.plugins ?? []).map(async ext => {
|
|
302
|
+
try {
|
|
303
|
+
const mount: PluginMount = (await import(moduleUrl(ext.client))).default
|
|
304
|
+
if (typeof mount !== 'function')
|
|
305
|
+
throw new Error('the client module must export a mount(root, page, api) function as default')
|
|
306
|
+
await mount(root, handleFor(ext.name), api)
|
|
307
|
+
} catch (err) {
|
|
308
|
+
console.error(`plugin ${ext.name}:`, err)
|
|
309
|
+
root.insertAdjacentHTML(
|
|
310
|
+
'afterbegin',
|
|
311
|
+
String(
|
|
312
|
+
html`<pre class="wb-error" role="alert">
|
|
313
|
+
plugin "${ext.name}" failed in the browser: ${(err as Error).message}</pre>`,
|
|
314
|
+
),
|
|
315
|
+
)
|
|
316
|
+
}
|
|
317
|
+
}),
|
|
318
|
+
)
|
|
319
|
+
if (live) connect()
|
package/src/client/scroll.ts
CHANGED
|
@@ -9,15 +9,20 @@ const norm = (s: string) => s.replace(/\s+/g, ' ').trim().toLowerCase()
|
|
|
9
9
|
export function findTarget(root: ParentNode, where: string): HTMLElement | null {
|
|
10
10
|
const want = norm(where)
|
|
11
11
|
const headings = [...root.querySelectorAll<HTMLElement>('h1, h2, h3, h4, h5, h6, summary')]
|
|
12
|
-
const heading =
|
|
12
|
+
const heading =
|
|
13
|
+
headings.find(h => norm(h.textContent ?? '') === want) ??
|
|
14
|
+
headings.find(h => norm(h.textContent ?? '').includes(want))
|
|
13
15
|
if (heading) return heading
|
|
14
|
-
const byName = [...root.querySelectorAll<HTMLElement>('form[name]')].find(
|
|
16
|
+
const byName = [...root.querySelectorAll<HTMLElement>('form[name]')].find(
|
|
17
|
+
f => f.getAttribute('name') === where.trim(),
|
|
18
|
+
)
|
|
15
19
|
if (byName) return byName
|
|
16
20
|
const byId = root.querySelector<HTMLElement>(`#${CSS.escape(where.trim())}`)
|
|
17
21
|
if (byId) return byId
|
|
18
22
|
const walker = document.createTreeWalker(root as Node, NodeFilter.SHOW_TEXT)
|
|
19
23
|
for (let n = walker.nextNode(); n; n = walker.nextNode()) {
|
|
20
|
-
if (norm(n.textContent ?? '').includes(want) && n.parentElement && !n.parentElement.closest('script, style'))
|
|
24
|
+
if (norm(n.textContent ?? '').includes(want) && n.parentElement && !n.parentElement.closest('script, style'))
|
|
25
|
+
return n.parentElement
|
|
21
26
|
}
|
|
22
27
|
return null
|
|
23
28
|
}
|
|
@@ -26,7 +31,12 @@ export function scrollToTarget(root: ParentNode, where: string): boolean {
|
|
|
26
31
|
const target = findTarget(root, where)
|
|
27
32
|
if (!target) return false
|
|
28
33
|
// a <details> around it opens (a summary's own details stays as it is)
|
|
29
|
-
for (
|
|
34
|
+
for (
|
|
35
|
+
let d = (target.tagName === 'SUMMARY' ? target.parentElement?.parentElement : target)?.closest('details');
|
|
36
|
+
d;
|
|
37
|
+
d = d.parentElement?.closest('details')
|
|
38
|
+
)
|
|
39
|
+
d.open = true
|
|
30
40
|
requestAnimationFrame(() => {
|
|
31
41
|
// below the header, and below the titles of the open details around it, which stick
|
|
32
42
|
let offset = (document.querySelector<HTMLElement>('.wb-nav')?.getBoundingClientRect().height ?? 0) + 12
|
package/src/client/toast.ts
CHANGED
|
@@ -19,7 +19,10 @@ function place(corner: HTMLElement) {
|
|
|
19
19
|
}
|
|
20
20
|
|
|
21
21
|
export function createToast(corner: () => HTMLElement) {
|
|
22
|
-
return function toast(
|
|
22
|
+
return function toast(
|
|
23
|
+
text: string,
|
|
24
|
+
{ kind = 'info', ms = 7000, onClick, onDismiss, name, order = 0 }: ToastOptions = {},
|
|
25
|
+
): Toast {
|
|
23
26
|
const el = document.createElement('button')
|
|
24
27
|
el.type = 'button'
|
|
25
28
|
el.className = 'wb-toast'
|
|
@@ -39,7 +42,11 @@ export function createToast(corner: () => HTMLElement) {
|
|
|
39
42
|
}
|
|
40
43
|
const close = () => el.remove()
|
|
41
44
|
el.addEventListener('click', e => {
|
|
42
|
-
if (onDismiss && (e.target as HTMLElement).closest('.wb-toast-dismiss')) {
|
|
45
|
+
if (onDismiss && (e.target as HTMLElement).closest('.wb-toast-dismiss')) {
|
|
46
|
+
close()
|
|
47
|
+
onDismiss()
|
|
48
|
+
return
|
|
49
|
+
}
|
|
43
50
|
if (ms) close()
|
|
44
51
|
onClick?.()
|
|
45
52
|
})
|
|
@@ -47,9 +54,16 @@ export function createToast(corner: () => HTMLElement) {
|
|
|
47
54
|
place(corner()).append(el)
|
|
48
55
|
return {
|
|
49
56
|
close,
|
|
50
|
-
update: next => {
|
|
51
|
-
|
|
52
|
-
|
|
57
|
+
update: next => {
|
|
58
|
+
label.textContent = next
|
|
59
|
+
},
|
|
60
|
+
hide: () => {
|
|
61
|
+
el.hidden = true
|
|
62
|
+
},
|
|
63
|
+
show: () => {
|
|
64
|
+
if (!el.isConnected) place(corner()).append(el)
|
|
65
|
+
el.hidden = false
|
|
66
|
+
},
|
|
53
67
|
}
|
|
54
68
|
}
|
|
55
69
|
}
|
package/src/config.ts
CHANGED
|
@@ -39,7 +39,13 @@ export function configFileIn(whiteboardDir: string): string | null {
|
|
|
39
39
|
return null
|
|
40
40
|
}
|
|
41
41
|
|
|
42
|
-
const isDir = (p: string) => {
|
|
42
|
+
const isDir = (p: string) => {
|
|
43
|
+
try {
|
|
44
|
+
return fs.statSync(p).isDirectory()
|
|
45
|
+
} catch {
|
|
46
|
+
return false
|
|
47
|
+
}
|
|
48
|
+
}
|
|
43
49
|
|
|
44
50
|
/** The project folder a path is in: the nearest folder upward with a `.whiteboard/` folder. */
|
|
45
51
|
export function findProjectDir(from: string = process.cwd()): string | null {
|
|
@@ -61,7 +67,9 @@ export function project(dir: string | null | undefined): Project | null {
|
|
|
61
67
|
dir,
|
|
62
68
|
wb,
|
|
63
69
|
/** the config file, or null: looked up each time, since one can be added or removed while a server runs */
|
|
64
|
-
get configFile() {
|
|
70
|
+
get configFile() {
|
|
71
|
+
return configFileIn(wb)
|
|
72
|
+
},
|
|
65
73
|
pagesDir: path.join(wb, 'pages'),
|
|
66
74
|
eventsFile: path.join(wb, 'events.ndjson'),
|
|
67
75
|
serverFile: path.join(wb, 'server.json'),
|
|
@@ -69,14 +77,20 @@ export function project(dir: string | null | undefined): Project | null {
|
|
|
69
77
|
/** shown in titles and the overview: `name` in the config if it is a plain literal, else the folder name */
|
|
70
78
|
get name() {
|
|
71
79
|
const file = configFileIn(wb)
|
|
72
|
-
return (
|
|
80
|
+
return (
|
|
81
|
+
(file && (/\bname\s*:\s*['"]([^'"]+)['"]/.exec(fs.readFileSync(file, 'utf8')) ?? [])[1]) || path.basename(dir)
|
|
82
|
+
)
|
|
73
83
|
},
|
|
74
84
|
}
|
|
75
85
|
}
|
|
76
86
|
|
|
77
87
|
// ---- page ids ----------------------------------------------------------------------------------
|
|
78
88
|
|
|
79
|
-
const strip = (p: string) =>
|
|
89
|
+
const strip = (p: string) =>
|
|
90
|
+
p
|
|
91
|
+
.replace(/\\/g, '/')
|
|
92
|
+
.replace(/\.md$/, '')
|
|
93
|
+
.replace(/^\/+|\/+$/g, '')
|
|
80
94
|
|
|
81
95
|
/**
|
|
82
96
|
* A file path or page name → the page id used in URLs and events.
|
|
@@ -91,6 +105,7 @@ export function pageId(proj: Project | null, input: unknown, cwd: string = proce
|
|
|
91
105
|
if (!looksLikeFile || !proj) return strip(s)
|
|
92
106
|
const abs = path.resolve(cwd, s)
|
|
93
107
|
if (abs.startsWith(proj.pagesDir + path.sep)) return strip(path.relative(proj.pagesDir, abs))
|
|
94
|
-
if (abs === proj.dir || abs.startsWith(proj.dir + path.sep))
|
|
108
|
+
if (abs === proj.dir || abs.startsWith(proj.dir + path.sep))
|
|
109
|
+
return `-/${path.relative(proj.dir, abs).replace(/\\/g, '/')}`.replace(/\/$/, '')
|
|
95
110
|
return strip(s)
|
|
96
111
|
}
|