@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.
Files changed (174) hide show
  1. package/README.md +119 -18
  2. package/bin/whiteboard.ts +300 -71
  3. package/dist/bin/whiteboard.js +21 -0
  4. package/dist/client/runtime.js +132 -54
  5. package/dist/define.js +21 -2
  6. package/dist/plugins/callout/index.js +3 -1
  7. package/dist/plugins/callout/public/style.css +30 -0
  8. package/dist/plugins/chat/index.js +7 -4
  9. package/dist/plugins/chat/{client.js → public/client.js} +1 -1
  10. package/dist/plugins/chat/public/style.css +98 -0
  11. package/dist/plugins/code/index.js +3 -1
  12. package/dist/plugins/code/public/style.css +68 -0
  13. package/dist/plugins/comments/index.js +4 -5
  14. package/dist/plugins/comments/public/client.js +19 -0
  15. package/dist/plugins/comments/public/style.css +185 -0
  16. package/dist/plugins/details/index.js +7 -2
  17. package/dist/plugins/details/public/style.css +7 -0
  18. package/dist/plugins/draft/README.md +14 -0
  19. package/dist/plugins/draft/index.js +21 -0
  20. package/dist/plugins/draft/public/style.css +21 -0
  21. package/dist/plugins/form/index.js +37 -8
  22. package/dist/plugins/form/public/style.css +126 -0
  23. package/dist/plugins/gallery/index.js +4 -3
  24. package/dist/plugins/gallery/public/client.js +1 -0
  25. package/dist/plugins/gallery/public/style.css +90 -0
  26. package/dist/plugins/grid/index.js +9 -3
  27. package/dist/plugins/grid/public/style.css +71 -0
  28. package/dist/plugins/iframe/index.js +13 -14
  29. package/dist/plugins/iframe/public/style.css +13 -0
  30. package/dist/plugins/index.js +7 -1
  31. package/dist/plugins/mermaid/index.js +3 -1
  32. package/dist/plugins/mermaid/{style.css → public/style.css} +7 -2
  33. package/dist/plugins/steps/README.md +42 -0
  34. package/dist/plugins/steps/index.js +99 -0
  35. package/dist/plugins/steps/public/client.js +1 -0
  36. package/dist/plugins/steps/public/style.css +77 -0
  37. package/dist/plugins/terminal/host.js +9 -3
  38. package/dist/plugins/terminal/index.js +10 -9
  39. package/dist/plugins/terminal/{style.css → public/style.css} +199 -45
  40. package/dist/plugins/terminal/sizes.js +2 -1
  41. package/dist/plugins/toc/README.md +21 -3
  42. package/dist/plugins/toc/index.js +12 -5
  43. package/dist/plugins/toc/public/client.js +1 -0
  44. package/dist/plugins/toc/public/style.css +99 -0
  45. package/dist/plugins/window/README.md +29 -0
  46. package/dist/plugins/window/index.js +64 -0
  47. package/dist/solid/index.js +14 -11
  48. package/dist/src/auth.js +2 -1
  49. package/dist/src/client/page/base.css +1078 -199
  50. package/dist/src/client/page/main.js +188 -39
  51. package/dist/src/loader.js +14 -3
  52. package/dist/src/package.js +3 -6
  53. package/dist/src/pages/blocks.js +8 -13
  54. package/dist/src/pages/markdown.js +1 -1
  55. package/dist/src/pages/render.js +106 -20
  56. package/dist/src/pages/window.js +31 -0
  57. package/dist/src/public.js +24 -0
  58. package/dist/src/server.js +251 -23
  59. package/package.json +5 -2
  60. package/plugins/callout/index.ts +3 -1
  61. package/plugins/callout/public/style.css +30 -0
  62. package/plugins/chat/autosize.ts +43 -9
  63. package/plugins/chat/client.tsx +22 -5
  64. package/plugins/chat/index.ts +22 -8
  65. package/plugins/chat/public/style.css +98 -0
  66. package/plugins/claude-code/index.ts +49 -14
  67. package/plugins/code/diff.ts +28 -8
  68. package/plugins/code/index.ts +33 -21
  69. package/plugins/code/public/style.css +68 -0
  70. package/plugins/comments/client.ts +139 -71
  71. package/plugins/comments/index.ts +9 -7
  72. package/plugins/comments/public/style.css +185 -0
  73. package/plugins/details/index.ts +7 -2
  74. package/plugins/details/public/style.css +7 -0
  75. package/plugins/draft/README.md +14 -0
  76. package/plugins/draft/index.ts +23 -0
  77. package/plugins/draft/public/style.css +21 -0
  78. package/plugins/forge/index.ts +16 -3
  79. package/plugins/form/index.ts +75 -27
  80. package/plugins/form/public/style.css +126 -0
  81. package/plugins/gallery/client.tsx +9 -3
  82. package/plugins/gallery/index.ts +31 -9
  83. package/plugins/gallery/public/style.css +90 -0
  84. package/plugins/grid/index.ts +19 -5
  85. package/plugins/grid/public/style.css +71 -0
  86. package/plugins/iframe/index.ts +20 -23
  87. package/plugins/iframe/public/style.css +13 -0
  88. package/plugins/index.ts +20 -1
  89. package/plugins/mermaid/index.ts +7 -3
  90. package/plugins/mermaid/{style.css → public/style.css} +7 -2
  91. package/plugins/steps/README.md +42 -0
  92. package/plugins/steps/client.ts +66 -0
  93. package/plugins/steps/index.ts +127 -0
  94. package/plugins/steps/public/style.css +77 -0
  95. package/plugins/terminal/client.ts +105 -48
  96. package/plugins/terminal/host.ts +143 -35
  97. package/plugins/terminal/index.ts +84 -25
  98. package/plugins/terminal/public/style.css +225 -0
  99. package/plugins/terminal/server.ts +64 -13
  100. package/plugins/terminal/sizes.ts +16 -4
  101. package/plugins/toc/README.md +21 -3
  102. package/plugins/toc/client.ts +90 -30
  103. package/plugins/toc/index.ts +26 -10
  104. package/plugins/toc/public/style.css +99 -0
  105. package/plugins/window/README.md +29 -0
  106. package/plugins/window/index.ts +85 -0
  107. package/skills/whiteboard/SKILL.md +3 -1
  108. package/solid/index.tsx +24 -9
  109. package/src/auth.ts +28 -9
  110. package/src/client/forms.ts +74 -28
  111. package/src/client/markdown.ts +15 -4
  112. package/src/client/page/base.css +1078 -199
  113. package/src/client/page/elsewhere.ts +32 -15
  114. package/src/client/page/fresh.ts +91 -23
  115. package/src/client/page/hold.ts +11 -2
  116. package/src/client/page/live.ts +10 -6
  117. package/src/client/page/main.ts +124 -42
  118. package/src/client/page/orphans.ts +25 -10
  119. package/src/client/page/sort.ts +14 -4
  120. package/src/client/page/sticky.ts +11 -3
  121. package/src/client/page/theme.ts +11 -7
  122. package/src/client/page/waiting.ts +141 -34
  123. package/src/client/page/window.ts +40 -0
  124. package/src/client/reactive.ts +19 -0
  125. package/src/client/read.ts +17 -10
  126. package/src/client/runtime.ts +170 -40
  127. package/src/client/scroll.ts +14 -4
  128. package/src/client/toast.ts +19 -5
  129. package/src/config.ts +20 -5
  130. package/src/define.ts +119 -31
  131. package/src/events.ts +111 -24
  132. package/src/html.ts +6 -2
  133. package/src/icons.ts +4 -2
  134. package/src/load.ts +10 -2
  135. package/src/loader.ts +81 -12
  136. package/src/package.ts +9 -8
  137. package/src/pages/blocks.ts +20 -14
  138. package/src/pages/index.ts +2 -2
  139. package/src/pages/markdown.ts +113 -34
  140. package/src/pages/render.ts +233 -41
  141. package/src/pages/window.ts +78 -0
  142. package/src/plugins.ts +90 -24
  143. package/src/public.ts +39 -0
  144. package/src/running.ts +8 -2
  145. package/src/serve.ts +29 -7
  146. package/src/server.ts +591 -110
  147. package/src/status.ts +15 -9
  148. package/dist/plugins/callout/style.css +0 -12
  149. package/dist/plugins/chat/style.css +0 -24
  150. package/dist/plugins/code/style.css +0 -29
  151. package/dist/plugins/comments/client.js +0 -10
  152. package/dist/plugins/comments/style.css +0 -40
  153. package/dist/plugins/details/style.css +0 -3
  154. package/dist/plugins/form/style.css +0 -44
  155. package/dist/plugins/gallery/client.js +0 -1
  156. package/dist/plugins/gallery/style.css +0 -27
  157. package/dist/plugins/grid/style.css +0 -25
  158. package/dist/plugins/iframe/client.js +0 -1
  159. package/dist/plugins/iframe/style.css +0 -29
  160. package/dist/plugins/toc/client.js +0 -1
  161. package/dist/plugins/toc/style.css +0 -19
  162. package/plugins/callout/style.css +0 -12
  163. package/plugins/chat/style.css +0 -24
  164. package/plugins/code/style.css +0 -29
  165. package/plugins/comments/style.css +0 -40
  166. package/plugins/details/style.css +0 -3
  167. package/plugins/form/style.css +0 -44
  168. package/plugins/gallery/style.css +0 -27
  169. package/plugins/grid/style.css +0 -25
  170. package/plugins/iframe/client.ts +0 -46
  171. package/plugins/iframe/style.css +0 -29
  172. package/plugins/terminal/style.css +0 -71
  173. package/plugins/toc/style.css +0 -19
  174. /package/dist/plugins/terminal/{client.js → public/client.js} +0 -0
@@ -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; kind: StoredPage['kind']; data: unknown
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 eventListeners = new Set<(events: WbEvent[]) => void>()
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
- for (const fn of eventListeners) fn(events)
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(type: string, data: Record<string, unknown> = {}, action: string | null = null): Promise<WbEvent> {
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 listenerListeners = new Set<(listeners: Listener[]) => void>()
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
- for (const fn of listenerListeners) fn(listeners)
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 = document.getElementById('wb-dock') ?? document.body.appendChild(Object.assign(document.createElement('div'), { id: 'wb-dock' }))
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()].sort((a, b) => a.order - b.order || a.since - b.since)
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: () => 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: () => listeners,
90
- onListeners(fn) { listenerListeners.add(fn); return () => listenerListeners.delete(fn) },
91
- rendered() { for (const fn of renderedListeners) fn() },
92
- onRendered(fn) { renderedListeners.add(fn); return () => renderedListeners.delete(fn) },
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) { baseTitle = title; writeTitle() },
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
- data: handoff.data,
118
- onChange(fn) { changeListeners.add(fn); return () => changeListeners.delete(fn) },
119
- render: () => getJson<RenderedPage>(`/api/render?page=${encodeURIComponent(page)}`),
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) handle.onChange(async () => { root.innerHTML = (await handle.render()).html; api.rendered() })
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.onerror = () => { delete document.documentElement.dataset.wbLive }
137
- es.addEventListener('change', () => { for (const fn of changeListeners) fn() })
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 { if ((await fetch('/api/health', { cache: 'no-store' })).ok) return location.reload() } catch { /* not up yet */ }
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('afterbegin', String(html`<pre class="wb-error" role="alert">the page failed to start: ${(err as Error).message}</pre>`))
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
- trackForms(root, page, api)
177
- trackReading({ messages: handoff.messages ?? [], events: () => events, send: ids => { append(READ, { ids }).catch(err => console.error('marking read:', err)) } })
178
- // the plugins' modules after the page, so that what they work on is there; one failing leaves the others
179
- await Promise.all((handoff.plugins ?? []).map(async ext => {
180
- try {
181
- const mount: PluginMount = (await import(ext.client)).default
182
- if (typeof mount !== 'function') throw new Error('the client module must export a mount(root, page, api) function as default')
183
- await mount(root, handle, api)
184
- } catch (err) {
185
- console.error(`plugin ${ext.name}:`, err)
186
- root.insertAdjacentHTML('afterbegin', String(html`<pre class="wb-error" role="alert">plugin "${ext.name}" failed in the browser: ${(err as Error).message}</pre>`))
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
- connect()
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()
@@ -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 = headings.find(h => norm(h.textContent ?? '') === want) ?? headings.find(h => norm(h.textContent ?? '').includes(want))
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(f => f.getAttribute('name') === where.trim())
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')) return n.parentElement
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 (let d = (target.tagName === 'SUMMARY' ? target.parentElement?.parentElement : target)?.closest('details'); d; d = d.parentElement?.closest('details')) d.open = true
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
@@ -19,7 +19,10 @@ function place(corner: HTMLElement) {
19
19
  }
20
20
 
21
21
  export function createToast(corner: () => HTMLElement) {
22
- return function toast(text: string, { kind = 'info', ms = 7000, onClick, onDismiss, name, order = 0 }: ToastOptions = {}): 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')) { close(); onDismiss(); return }
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 => { label.textContent = next },
51
- hide: () => { el.hidden = true },
52
- show: () => { if (!el.isConnected) place(corner()).append(el); el.hidden = false },
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) => { try { return fs.statSync(p).isDirectory() } catch { return false } }
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() { return configFileIn(wb) },
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 (file && (/\bname\s*:\s*['"]([^'"]+)['"]/.exec(fs.readFileSync(file, 'utf8')) ?? [])[1]) || path.basename(dir)
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) => p.replace(/\\/g, '/').replace(/\.md$/, '').replace(/^\/+|\/+$/g, '')
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)) return `-/${path.relative(proj.dir, abs).replace(/\\/g, '/')}`.replace(/\/$/, '')
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
  }