@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.
Files changed (101) hide show
  1. package/README.md +23 -216
  2. package/bin/whiteboard.ts +82 -239
  3. package/dist/bin/whiteboard.js +70 -200
  4. package/dist/client/runtime.js +297 -61
  5. package/dist/plugins/badge/README.md +3 -1
  6. package/dist/plugins/badge/public/style.css +17 -0
  7. package/dist/plugins/chat/public/client.js +2 -2
  8. package/dist/plugins/chat/public/style.css +19 -6
  9. package/dist/plugins/code/README.md +10 -0
  10. package/dist/plugins/code/index.js +31 -22
  11. package/dist/plugins/code/public/client.js +1 -0
  12. package/dist/plugins/code/public/style.css +33 -2
  13. package/dist/plugins/comments/public/client.js +7 -2
  14. package/dist/plugins/comments/public/style.css +4 -0
  15. package/dist/plugins/form/public/style.css +2 -0
  16. package/dist/plugins/grid/public/style.css +0 -5
  17. package/dist/plugins/iframe/index.js +2 -2
  18. package/dist/plugins/index.js +5 -1
  19. package/dist/plugins/lightbox/public/client.js +1 -1
  20. package/dist/plugins/terminal/index.js +7 -9
  21. package/dist/plugins/terminal/public/client.js +1 -1
  22. package/dist/plugins/terminal/public/style.css +3 -3
  23. package/dist/plugins/timeline/README.md +40 -0
  24. package/dist/plugins/timeline/index.js +82 -0
  25. package/dist/plugins/timeline/public/style.css +118 -0
  26. package/dist/plugins/toc/README.md +13 -13
  27. package/dist/plugins/toc/index.js +6 -4
  28. package/dist/plugins/toc/public/client.js +1 -1
  29. package/dist/plugins/toc/public/style.css +19 -2
  30. package/dist/plugins/window/index.js +4 -4
  31. package/dist/solid/index.js +2 -1
  32. package/dist/src/auth.js +7 -3
  33. package/dist/src/build.js +159 -0
  34. package/dist/src/client/page/base.css +74 -35
  35. package/dist/src/client/page/main.js +16 -5
  36. package/dist/src/document.js +93 -0
  37. package/dist/src/events.js +7 -15
  38. package/dist/src/hub.js +90 -53
  39. package/dist/src/listen.js +100 -0
  40. package/dist/src/listeners.js +67 -0
  41. package/dist/src/pages/index.js +1 -1
  42. package/dist/src/pages/markdown.js +23 -11
  43. package/dist/src/pages/render.js +13 -8
  44. package/dist/src/serve.js +2 -0
  45. package/dist/src/server.js +204 -514
  46. package/dist/src/site.js +173 -0
  47. package/dist/src/status.js +63 -1
  48. package/dist/src/ws.js +128 -0
  49. package/package.json +1 -1
  50. package/plugins/README.md +258 -0
  51. package/plugins/badge/README.md +3 -1
  52. package/plugins/badge/public/style.css +17 -0
  53. package/plugins/chat/client.tsx +34 -6
  54. package/plugins/chat/public/style.css +19 -6
  55. package/plugins/code/README.md +10 -0
  56. package/plugins/code/client.ts +73 -0
  57. package/plugins/code/index.ts +38 -26
  58. package/plugins/code/public/style.css +33 -2
  59. package/plugins/comments/client.ts +56 -6
  60. package/plugins/comments/public/style.css +4 -0
  61. package/plugins/form/public/style.css +2 -0
  62. package/plugins/grid/public/style.css +0 -5
  63. package/plugins/iframe/index.ts +2 -2
  64. package/plugins/index.ts +6 -1
  65. package/plugins/lightbox/client.ts +13 -3
  66. package/plugins/terminal/client.ts +10 -2
  67. package/plugins/terminal/index.ts +8 -6
  68. package/plugins/terminal/public/style.css +3 -3
  69. package/plugins/timeline/README.md +40 -0
  70. package/plugins/timeline/index.ts +92 -0
  71. package/plugins/timeline/public/style.css +118 -0
  72. package/plugins/toc/README.md +13 -13
  73. package/plugins/toc/client.ts +58 -32
  74. package/plugins/toc/index.ts +13 -13
  75. package/plugins/toc/public/style.css +19 -2
  76. package/plugins/window/index.ts +4 -4
  77. package/skills/whiteboard/SKILL.md +3 -1
  78. package/solid/index.tsx +3 -0
  79. package/src/auth.ts +7 -2
  80. package/src/build.ts +213 -0
  81. package/src/client/forms.ts +102 -5
  82. package/src/client/page/base.css +74 -35
  83. package/src/client/page/elsewhere.ts +14 -4
  84. package/src/client/page/waiting.ts +10 -5
  85. package/src/client/read.ts +16 -3
  86. package/src/client/runtime.ts +219 -52
  87. package/src/client/toast.ts +36 -6
  88. package/src/define.ts +44 -18
  89. package/src/document.ts +191 -0
  90. package/src/events.ts +18 -17
  91. package/src/hub.ts +102 -56
  92. package/src/listen.ts +125 -0
  93. package/src/listeners.ts +95 -0
  94. package/src/pages/index.ts +1 -1
  95. package/src/pages/markdown.ts +29 -18
  96. package/src/pages/render.ts +13 -8
  97. package/src/serve.ts +2 -0
  98. package/src/server.ts +223 -611
  99. package/src/site.ts +165 -0
  100. package/src/status.ts +100 -0
  101. package/src/ws.ts +155 -0
@@ -14,6 +14,8 @@ interface Message {
14
14
  id: string
15
15
  from: 'you' | 'agent'
16
16
  text: string
17
+ /** shown at once, but the server hasn't confirmed it yet */
18
+ sending?: boolean
17
19
  }
18
20
 
19
21
  /**
@@ -49,7 +51,12 @@ function Chat(props: ChatProps) {
49
51
  props.form && e.data.replyTo ? events.find(x => x.id === e.data.replyTo && x.type === props.form) : undefined
50
52
  const asked = to ? written(to, props.choices) : ''
51
53
  if (to && asked && !out.some(m => m.id === to.id)) out.push({ id: to.id, from: 'you', text: asked })
52
- out.push({ id: e.id, from: e.from === 'cli' ? 'agent' : 'you', text: String(e.data.text ?? '') })
54
+ out.push({
55
+ id: e.id,
56
+ from: e.from === 'cli' ? 'agent' : 'you',
57
+ text: String(e.data.text ?? ''),
58
+ sending: !!e.sending,
59
+ })
53
60
  }
54
61
  return out
55
62
  })
@@ -57,10 +64,26 @@ function Chat(props: ChatProps) {
57
64
  const post = async () => {
58
65
  const text = box?.value.trim()
59
66
  if (!text) return
60
- await wb.append(MESSAGE, { chat: props.chat, text })
61
- box!.value = ''
62
- // measure again: a value set from code fires no input event
63
- box!.dispatchEvent(new Event('input'))
67
+ // optimistic: the message is in the chat at once, grey until the server has it, and the box is
68
+ // empty, so a second Send has nothing to send again
69
+ const write = (value: string) => {
70
+ box!.value = value
71
+ // measure again: a value set from code fires no input event
72
+ box!.dispatchEvent(new Event('input'))
73
+ }
74
+ write('')
75
+ try {
76
+ await wb.append(MESSAGE, { chat: props.chat, text })
77
+ } catch (err) {
78
+ console.error(err)
79
+ // it never got there: back into the box, unless something else was written meanwhile
80
+ if (!box!.value.trim()) write(text)
81
+ wb.toast("Couldn't send the message", {
82
+ kind: 'alert',
83
+ name: 'chat-failed',
84
+ details: "It's back in the box: send it again.",
85
+ })
86
+ }
64
87
  }
65
88
  return (
66
89
  // a div, not a form: forms don't nest, and the box has no name, so it is never part of an answer
@@ -69,7 +92,12 @@ function Chat(props: ChatProps) {
69
92
  {m => (
70
93
  // the reader's messages on the right, the agent's on the left
71
94
  // the agent's: read once in view (the core marks it, see EventSpec.forReader)
72
- <div class="wb-chat-message" data-from={m.from} data-wb-message={m.from === 'agent' ? m.id : undefined}>
95
+ <div
96
+ class="wb-chat-message"
97
+ data-from={m.from}
98
+ data-sending={m.sending ? '' : undefined}
99
+ data-wb-message={m.from === 'agent' ? m.id : undefined}
100
+ >
73
101
  <span class="wb-chat-who">{m.from}</span>
74
102
  <Bubble text={m.text} />
75
103
  </div>
@@ -23,6 +23,15 @@
23
23
  align-self: flex-end;
24
24
  align-items: flex-end;
25
25
  }
26
+ /* shown at once, grey until the server has it */
27
+ .wb-chat-message[data-from='you'][data-sending] .wb-chat-bubble {
28
+ background: var(--wb-sending, #9a9fa7);
29
+ border-color: var(--wb-sending, #9a9fa7);
30
+ color: var(--bg);
31
+ transition:
32
+ background-color 0.2s,
33
+ border-color 0.2s;
34
+ }
26
35
  .wb-chat-who {
27
36
  font: var(--weight-strong) var(--ui-tiny)/1.3 var(--sans);
28
37
  letter-spacing: var(--caps-spacing);
@@ -35,6 +44,7 @@
35
44
  }
36
45
  .wb-chat-bubble {
37
46
  max-width: 100%;
47
+ min-width: 0;
38
48
  box-sizing: border-box;
39
49
  white-space: pre-wrap;
40
50
  padding: var(--space-md) var(--space-xl);
@@ -53,18 +63,19 @@
53
63
  .wb-chat-bubble[data-md] > :last-child {
54
64
  margin-bottom: 0;
55
65
  }
56
- .wb-chat-bubble[data-md] :is(p, ul, ol, pre, blockquote) {
66
+ .wb-chat-bubble[data-md] :is(p, ul, ol, pre, blockquote, table) {
57
67
  margin: 0 0 0.5em;
58
68
  }
59
- /* code wraps at the bubble's edge, as the text around it does: a long line would otherwise make the
60
- bubble as wide (it fits its content), and the chat and the page with it; and a scrollbar under it
61
- is drawn over the text below in WebKit (its height left out when the block first lays out) */
69
+ /* a table's cells wrap between words, not inside them (the bubble's own wrapping anywhere would
70
+ squeeze its columns down to a letter); one too wide for the bubble scrolls sideways */
71
+ .wb-chat-bubble[data-md] table {
72
+ overflow-wrap: normal;
73
+ }
74
+ /* code keeps its lines whole and scrolls sideways, as on a page (code/style.css: {wrap} wraps it) */
62
75
  .wb-chat-bubble[data-md] pre {
63
76
  font-size: var(--ui-small);
64
77
  padding: var(--space-md) var(--space-lg);
65
78
  border-radius: var(--radius-sm);
66
- white-space: pre-wrap;
67
- overflow-wrap: anywhere;
68
79
  }
69
80
  .wb-chat-message[data-from='you'] .wb-chat-bubble[data-md] a {
70
81
  color: inherit;
@@ -93,6 +104,8 @@
93
104
  border-top: var(--hairline) solid var(--border);
94
105
  border-bottom: var(--hairline) solid var(--border);
95
106
  display: grid;
107
+ /* one column, as wide as the block: wide content (a code block) scrolls inside it, not past it */
108
+ grid-template-columns: minmax(0, 1fr);
96
109
  gap: var(--space-2xl);
97
110
  }
98
111
  .wb-chat-block > :first-child {
@@ -22,6 +22,16 @@ bare `@@`: its lines show without numbers. `---` and `+++` lines name the file
22
22
  without a `diff --git` above them. Use four backticks around it, since a diff
23
23
  can hold three.
24
24
 
25
+ A line longer than the block scrolls it sideways, on a page and in a chat
26
+ message alike. A fence that says `{wrap}` wraps its long lines at the block's
27
+ edge instead, a diff's rows too:
28
+
29
+ ````markdown
30
+ ```sh {wrap}
31
+ whiteboard append --page plan --type chat:message --data '{"chat":"notes","text":"a long line"}'
32
+ ```
33
+ ````
34
+
25
35
  A missing or unknown language renders as plain text. Fences named after
26
36
  another block, such as `mermaid`, still go to that block.
27
37
 
@@ -0,0 +1,73 @@
1
+ // Whether each code block overflows sideways, measured, so style.css can choose its scrollbar in
2
+ // advance: `data-overflow="true"` gives the code `overflow-x: scroll`, `"false"` `overflow-x: hidden`.
3
+ //
4
+ // Why: WebKit defers the scrollbar of an `overflow-x: auto` element while a flex or grid ancestor
5
+ // is laid out. Once the outermost flex/grid layout is done, it adds the scrollbar and lays out only
6
+ // the scroller again, so every ancestor keeps the height it had without the bar: the bar hangs
7
+ // below the block's background, over what follows. Whiteboard's chat (flex), forms and grid cards
8
+ // (grid) put code blocks right there. Reading a size afterwards doesn't fix it (to WebKit the
9
+ // ancestors are laid out already), nor does a wrapper (it is one more ancestor that keeps its
10
+ // height). A scrollbar chosen in style is laid out with the ancestors.
11
+ //
12
+ // - block/buzz-app#1677, the same bug for tables in chat messages, traced into WebKit's source
13
+ // (RenderBlock::endAndCommitUpdateScrollInfoAfterLayoutTransaction →
14
+ // relayoutRenderBlockForScrollbarChange, MarkOnlyThis), and this fix:
15
+ // https://github.com/block/buzz-app/pull/1677
16
+ // - WebKit bug 51148, overflow: auto not accounting for its scrollbars after a size change:
17
+ // https://bugs.webkit.org/show_bug.cgi?id=51148
18
+ // - WebKit source, where the deferred scrollbar update is committed:
19
+ // https://github.com/WebKit/WebKit/blob/main/Source/WebCore/rendering/RenderBlock.cpp
20
+ //
21
+ // Always `overflow-x: scroll` would do without measuring, but leaves an empty scrollbar track
22
+ // under every block that fits wherever scrollbars take space (macOS set to always show them).
23
+ //
24
+ // Measured when a block arrives (before the browser lays it out: a MutationObserver's callback
25
+ // runs before the next layout), when it changes size, and when its text changes: a block at its
26
+ // widest can start or stop overflowing without changing size. Blocks nothing measured (a page
27
+ // without scripts) keep `overflow-x: auto`.
28
+ import type { PluginMount } from '../../src/define.ts'
29
+
30
+ const CODE = 'pre.shiki > code'
31
+
32
+ const mount: PluginMount = root => {
33
+ const measure = (code: HTMLElement) => {
34
+ // a scrollbar of its own leaves clientWidth as it was: this reads the same either way
35
+ const overflow = String(code.scrollWidth > code.clientWidth)
36
+ if (code.dataset.overflow !== overflow) code.dataset.overflow = overflow
37
+ }
38
+ const sizes = new ResizeObserver(entries => {
39
+ for (const { target } of entries) measure(target as HTMLElement)
40
+ })
41
+ const known = new WeakSet<HTMLElement>()
42
+ const arrive = (within: Element) => {
43
+ const codes = within.matches(CODE) ? [within as HTMLElement] : within.querySelectorAll<HTMLElement>(CODE)
44
+ for (const code of codes) {
45
+ measure(code)
46
+ if (known.has(code)) continue
47
+ known.add(code)
48
+ sizes.observe(code)
49
+ }
50
+ }
51
+ arrive(root)
52
+ // a font that loads after a block was measured widens its text without resizing the block or
53
+ // changing its content: measured again, else one marked as fitting would clip what it can't show
54
+ document.fonts?.addEventListener('loadingdone', () => {
55
+ for (const code of root.querySelectorAll<HTMLElement>(CODE)) measure(code)
56
+ })
57
+ // what arrives later (a chat message, a page's update), and text changing inside a block; the
58
+ // attribute itself is not watched, so setting it never comes back here
59
+ new MutationObserver(records => {
60
+ for (const r of records) {
61
+ if (r.type === 'characterData') {
62
+ const code = r.target.parentElement?.closest<HTMLElement>(CODE)
63
+ if (code) measure(code)
64
+ continue
65
+ }
66
+ for (const n of r.addedNodes) if (n instanceof Element) arrive(n)
67
+ const code = r.target instanceof Element ? r.target.closest<HTMLElement>(CODE) : null
68
+ if (code) measure(code)
69
+ }
70
+ }).observe(root, { childList: true, characterData: true, subtree: true })
71
+ }
72
+
73
+ export default mount
@@ -46,8 +46,11 @@ export default function code(options: Options = {}): Plugin {
46
46
  let shiki: typeof Shiki | undefined
47
47
  const loaded = new Set<string>()
48
48
 
49
- /** Highlight `code` as `lang`; an unknown or missing language renders as plain text. */
50
- async function highlight(code: string, lang: string | null) {
49
+ /**
50
+ * Highlight `code` as `lang`; an unknown or missing language renders as plain text. A long line
51
+ * scrolls the block sideways; with `wrap` it wraps at the block's edge.
52
+ */
53
+ async function highlight(code: string, lang: string | null, wrap = false) {
51
54
  shiki ??= await import('shiki')
52
55
  highlighter ??= shiki.createHighlighter({ themes: Object.values(themes), langs: [] })
53
56
  const h = await highlighter
@@ -59,45 +62,54 @@ export default function code(options: Options = {}): Plugin {
59
62
  }
60
63
  } else language = 'text'
61
64
  const diff = language === 'diff' ? diffLines(code) : null
65
+ const transformers: Shiki.ShikiTransformer[] = []
66
+ // a fence that says {wrap}: its long lines wrap (style.css)
67
+ if (wrap)
68
+ transformers.push({
69
+ pre(node) {
70
+ node.properties['data-wrap'] = ''
71
+ },
72
+ })
73
+ if (diff)
74
+ transformers.push({
75
+ line(node, n) {
76
+ const info = diff[n - 1]
77
+ if (!info) return
78
+ node.properties['data-diff'] = info.kind
79
+ if (info.n != null) node.properties['data-n'] = String(info.n)
80
+ if (info.at) node.properties['data-wb-at'] = info.at
81
+ // a file's headers read as its name; a hunk's header as nothing but the gutter's `⋯`
82
+ if (info.label != null) node.children = [{ type: 'text', value: info.label }]
83
+ },
84
+ // the headers nobody reads go, with the line break after each
85
+ code(node) {
86
+ node.children = node.children.filter((child, i, all) => {
87
+ const isMeta = (c: typeof child | undefined) =>
88
+ c?.type === 'element' && c.properties['data-diff'] === 'meta'
89
+ return !isMeta(child) && !(child.type === 'text' && isMeta(all[i - 1]))
90
+ })
91
+ },
92
+ })
62
93
  return h.codeToHtml(code, {
63
94
  lang: language,
64
95
  themes,
65
96
  defaultColor: false,
66
97
  colorReplacements,
67
- transformers: diff
68
- ? [
69
- {
70
- line(node, n) {
71
- const info = diff[n - 1]
72
- if (!info) return
73
- node.properties['data-diff'] = info.kind
74
- if (info.n != null) node.properties['data-n'] = String(info.n)
75
- if (info.at) node.properties['data-wb-at'] = info.at
76
- // a file's headers read as its name; a hunk's header as nothing but the gutter's `⋯`
77
- if (info.label != null) node.children = [{ type: 'text', value: info.label }]
78
- },
79
- // the headers nobody reads go, with the line break after each
80
- code(node) {
81
- node.children = node.children.filter((child, i, all) => {
82
- const isMeta = (c: typeof child | undefined) =>
83
- c?.type === 'element' && c.properties['data-diff'] === 'meta'
84
- return !isMeta(child) && !(child.type === 'text' && isMeta(all[i - 1]))
85
- })
86
- },
87
- },
88
- ]
89
- : [],
98
+ transformers,
90
99
  })
91
100
  }
92
101
 
93
102
  return definePlugin({
94
103
  name: 'code',
95
104
  public: builtinPublic('code'),
105
+ // measures whether each code block overflows, so its scrollbar is chosen in advance (client.ts:
106
+ // a WebKit layout bug)
107
+ setup: () => ({ client: 'client.js' }),
96
108
  blocks: {
97
109
  code: {
98
110
  // every code fence no other block claims by name, and the code view of repository files
99
111
  fence: 'any',
100
- html: ctx => highlight((ctx as FenceContext).text, (ctx as FenceContext).lang),
112
+ html: ctx => highlight((ctx as FenceContext).text, (ctx as FenceContext).lang, 'wrap' in ctx.attrs),
101
113
  style: 'style.css',
102
114
  },
103
115
  },
@@ -24,12 +24,39 @@ pre.shiki {
24
24
  text-decoration: var(--shiki-dark-text-decoration);
25
25
  }
26
26
 
27
+ /* a long line scrolls the block sideways, wherever it is; a fence that says {wrap} wraps it. The
28
+ code inside the block scrolls, not the block: WebKit sizes a block that scrolls sideways without
29
+ its scrollbar, so the bar ate into its height (a vertical scrollbar came with it) */
30
+ pre.shiki {
31
+ white-space: pre;
32
+ overflow-wrap: normal;
33
+ overflow: visible;
34
+ }
35
+ pre.shiki > code {
36
+ display: block;
37
+ overflow-x: auto;
38
+ overflow-y: hidden;
39
+ }
40
+ /* the scrollbar chosen in advance, from measured overflow (client.ts): WebKit lays out an `auto`
41
+ scrollbar under a flex or grid ancestor after the ancestors, which keep the height they had
42
+ without it (https://github.com/block/buzz-app/pull/1677,
43
+ https://bugs.webkit.org/show_bug.cgi?id=51148) */
44
+ pre.shiki > code[data-overflow='true'] {
45
+ overflow-x: scroll;
46
+ }
47
+ pre.shiki > code[data-overflow='false'] {
48
+ overflow-x: hidden;
49
+ }
50
+ pre.shiki[data-wrap] {
51
+ white-space: pre-wrap;
52
+ overflow-wrap: anywhere;
53
+ }
54
+
27
55
  /* a diff: one row per line, as wide as the longest (a grid leaves out the line breaks between them,
28
56
  which stay in the text), tinted by what it is, with its number in a gutter */
29
57
  .shiki code:has(> .line[data-diff]) {
30
58
  display: grid;
31
- width: max-content;
32
- min-width: 100%;
59
+ grid-template-columns: minmax(max-content, 1fr);
33
60
  }
34
61
  .shiki .line[data-diff='add'] {
35
62
  background: color-mix(in srgb, #16a34a var(--tint-soft), transparent);
@@ -66,3 +93,7 @@ pre.shiki {
66
93
  .shiki .line[data-diff='file'] + .line[data-diff='hunk'] {
67
94
  margin-top: 0;
68
95
  }
96
+ /* a diff that wraps: its rows fill the block, and a long one wraps inside its row */
97
+ pre.shiki[data-wrap] code:has(> .line[data-diff]) {
98
+ grid-template-columns: minmax(0, 1fr);
99
+ }
@@ -230,7 +230,13 @@ function renderPopover() {
230
230
  const message = (e: WbEvent) => {
231
231
  const text = String(e.data.text ?? '')
232
232
  const md = rendered.get(text)
233
- return html`<div class="wb-thread-message" data-from="${who(e)}" data-wb-message="${e.id}">
233
+ // shown at once, not yet confirmed by the server
234
+ return html`<div
235
+ class="wb-thread-message"
236
+ data-from="${who(e)}"
237
+ data-wb-message="${e.id}"
238
+ ${e.sending ? raw(' data-sending') : ''}
239
+ >
234
240
  <span class="wb-thread-who">${who(e)}</span>
235
241
  <div class="wb-thread-text" ${md != null ? raw(' data-md') : ''}>${md != null ? raw(md) : text}</div>
236
242
  </div>`
@@ -470,7 +476,14 @@ const mount: PluginMount = (el: HTMLElement, handle: PageHandle, core: Api) => {
470
476
  root = el
471
477
  main = root.querySelector('main') ?? root
472
478
  api = core
473
- document.body.append(layer, startButton, lineButton, popover, composer)
479
+ // over the page, in document coordinates: its element in the overlay slot is laid out as if these
480
+ // were at the end of <body>
481
+ core.slot('overlay', place => {
482
+ place.append(layer, startButton, lineButton, popover, composer)
483
+ return () => {
484
+ for (const part of [layer, startButton, lineButton, popover, composer]) part.remove()
485
+ }
486
+ })
474
487
 
475
488
  document.addEventListener('mousemove', onLinePointer)
476
489
  // the pointer left the page: no line is hovered any more (a drag keeps its lines until mouseup)
@@ -519,13 +532,31 @@ const mount: PluginMount = (el: HTMLElement, handle: PageHandle, core: Api) => {
519
532
  e.preventDefault()
520
533
  const text = composer.querySelector('textarea')!.value.trim()
521
534
  if (!text || !pending) return
522
- const saved = await api.append(NEW, { ...pending, text })
535
+ // optimistic: the thread is on the page at once (append puts the comment in the events), open
536
+ const where = pending
537
+ const sent = api.append(NEW, { ...where, text })
538
+ const shown = api.events().findLast(e => e.type === NEW && e.sending && e.data.text === text)
523
539
  composer.querySelector('textarea')!.value = ''
524
540
  composer.hidden = true
525
541
  pending = null
526
542
  getSelection()?.removeAllRanges()
527
543
  refreshComments()
528
- openPopover(saved.id)
544
+ if (shown) openPopover(shown.id)
545
+ try {
546
+ await sent
547
+ } catch (err) {
548
+ console.error('comments:', err)
549
+ // it never got there: the composer again, with the passage and the text
550
+ if (!composer.hidden) return
551
+ pending = where
552
+ composer.querySelector('textarea')!.value = text
553
+ composer.hidden = false
554
+ api.toast("Couldn't send the comment", {
555
+ kind: 'alert',
556
+ name: 'comment-failed',
557
+ details: "It's back in the box: send it again.",
558
+ })
559
+ }
529
560
  })
530
561
  composer.querySelector('[data-cancel]')!.addEventListener('click', () => {
531
562
  composer.hidden = true
@@ -541,9 +572,23 @@ const mount: PluginMount = (el: HTMLElement, handle: PageHandle, core: Api) => {
541
572
  const box = popover.querySelector('textarea')!
542
573
  const text = box.value.trim()
543
574
  if (!text || !openThread) return
544
- await api.append(REPLY, { replyTo: openThread, text })
575
+ // optimistic: the reply is in the thread at once, marked sending, and the box is free
576
+ const thread = openThread
545
577
  box.value = ''
578
+ const sent = api.append(REPLY, { replyTo: thread, text })
546
579
  refreshComments()
580
+ try {
581
+ await sent
582
+ } catch (err) {
583
+ console.error('comments:', err)
584
+ const again = popover.querySelector('textarea')
585
+ if (openThread === thread && again && !again.value.trim()) again.value = text
586
+ api.toast("Couldn't send the reply", {
587
+ kind: 'alert',
588
+ name: 'comment-failed',
589
+ details: "It's back in the box: send it again.",
590
+ })
591
+ }
547
592
  })
548
593
  popover.addEventListener('click', async e => {
549
594
  if (!(e.target as HTMLElement).closest('[data-close]') || !openThread) return
@@ -551,8 +596,13 @@ const mount: PluginMount = (el: HTMLElement, handle: PageHandle, core: Api) => {
551
596
  closePopover()
552
597
  // closed, the thread's replies ask nothing of the reader any more
553
598
  api.markRead(threads.find(t => t.id === id)?.replies.map(r => r.id) ?? [])
554
- await api.append(RESOLVE, { replyTo: id })
599
+ // optimistic: the thread leaves the page at once; should that not get through, it comes back
600
+ const sent = api.append(RESOLVE, { replyTo: id })
555
601
  refreshComments()
602
+ sent.catch(err => {
603
+ console.error('comments:', err)
604
+ api.toast("Couldn't close the thread", { kind: 'alert', name: 'comment-failed', details: "It's open again." })
605
+ })
556
606
  })
557
607
  // the page goes to an unread reply here: open its thread
558
608
  layer.addEventListener('wb-reveal', e => {
@@ -183,3 +183,7 @@
183
183
  font: var(--ui-small) var(--mono, monospace);
184
184
  color: var(--muted, #696e76);
185
185
  }
186
+ /* shown at once, grey until the server has it */
187
+ .wb-thread-message[data-sending] .wb-thread-text {
188
+ color: var(--wb-sending, #9a9fa7);
189
+ }
@@ -5,6 +5,8 @@
5
5
  border-top: var(--hairline) solid var(--border);
6
6
  border-bottom: var(--hairline) solid var(--border);
7
7
  display: grid;
8
+ /* one column, as wide as the form: wide content (a code block) scrolls inside it, not past it */
9
+ grid-template-columns: minmax(0, 1fr);
8
10
  gap: var(--space-2xl);
9
11
  }
10
12
  .wb-form-pending {
@@ -35,8 +35,3 @@
35
35
  margin-bottom: 0;
36
36
  }
37
37
 
38
- /* a badge that ends the heading (`### Parser :badge[done]`) sits at the card's right edge */
39
- .wb-card h3 > .wb-badge:last-child {
40
- float: right;
41
- margin: 0.45em 0 0 var(--space-lg);
42
- }
@@ -25,14 +25,14 @@ export async function render(ctx: DirectiveContext) {
25
25
  const title = ctx.labelText ?? ctx.attrs.title ?? src
26
26
  // the frame, which the reader can drag taller
27
27
  return html`<div class="wb-iframe-frame" style="height: ${height(ctx.attrs.height)}">
28
- <iframe src="${src}" title="${title}" loading="lazy" referrerpolicy="no-referrer"></iframe>
28
+ <iframe src="${ctx.fileUrl(src)}" title="${title}" loading="lazy" referrerpolicy="no-referrer"></iframe>
29
29
  </div>`
30
30
  }
31
31
 
32
32
  /** What it shows on its own, for the ↗ of a window around it: its address. */
33
33
  export function open(ctx: DirectiveContext) {
34
34
  const src = (ctx.attrs.src ?? '').trim()
35
- return src && safeSrc(src) ? src : null
35
+ return src && safeSrc(src) ? ctx.fileUrl(src) : null
36
36
  }
37
37
 
38
38
  export default function iframe(): Plugin {
package/plugins/index.ts CHANGED
@@ -1,7 +1,8 @@
1
1
  // The built-in plugins: what every project has, before its config's `plugins` (which add to them,
2
2
  // and replace one of the same name: `code({ themes })` for the default `code`). The terminal and the
3
3
  // Claude Code plugin are not among them: they run code, or are about one agent, so a project adds
4
- // them itself.
4
+ // them itself. The table of contents is, but shows only on a page that asks for it, unless the
5
+ // config replaces it with `toc({ always: true })`.
5
6
  import type { Plugin } from '../src/define.ts'
6
7
  import callout from './callout/index.ts'
7
8
  import chat from './chat/index.ts'
@@ -20,6 +21,8 @@ import draft from './draft/index.ts'
20
21
  import lightbox from './lightbox/index.ts'
21
22
  import badge from './badge/index.ts'
22
23
  import ansi from './ansi/index.ts'
24
+ import timeline from './timeline/index.ts'
25
+ import toc from './toc/index.ts'
23
26
 
24
27
  export function builtins(): Plugin[] {
25
28
  return [
@@ -40,5 +43,7 @@ export function builtins(): Plugin[] {
40
43
  lightbox(),
41
44
  badge(),
42
45
  ansi(),
46
+ timeline(),
47
+ toc(),
43
48
  ]
44
49
  }
@@ -61,7 +61,7 @@ function open(scope: Element, picture: HTMLImageElement) {
61
61
  else if (target.closest('.wb-lightbox-next')) step(1)
62
62
  else if (target.closest('.wb-lightbox-close') || target === overlay || target.matches('.wb-lightbox-stage')) close()
63
63
  })
64
- document.body.append(overlay)
64
+ place?.append(overlay)
65
65
  show()
66
66
  }
67
67
 
@@ -76,14 +76,24 @@ function close() {
76
76
  overlay = null
77
77
  }
78
78
 
79
+ /** Where an open lightbox goes: this plugin's element in the overlay slot. */
80
+ let place: HTMLElement | null = null
81
+
79
82
  let listening = false
80
- const mount: PluginMount = () => {
83
+ const mount: PluginMount = (_root, _page, api) => {
84
+ api.slot('overlay', el => {
85
+ place = el
86
+ return () => {
87
+ close()
88
+ place = null
89
+ }
90
+ })
81
91
  if (listening) return
82
92
  listening = true
83
93
  document.addEventListener('click', e => {
84
94
  const picture = (e.target as Element).closest?.('img')
85
95
  const scope = picture?.closest('[data-lightbox]')
86
- if (!picture || !scope || picture.closest('a, .wb-lightbox')) return
96
+ if (!picture || !scope || !place || picture.closest('a, .wb-lightbox')) return
87
97
  e.preventDefault()
88
98
  open(scope, picture)
89
99
  })
@@ -8,6 +8,7 @@
8
8
  // its tab, so switching pages loses nothing.
9
9
  import { Terminal } from '@xterm/xterm'
10
10
  import { FitAddon } from '@xterm/addon-fit'
11
+ import type { PluginMount } from '../../src/define.ts'
11
12
 
12
13
  const store = {
13
14
  get: (k: string) => {
@@ -379,5 +380,12 @@ function post(url: string, body: unknown) {
379
380
  return fetch(url, { method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify(body) })
380
381
  }
381
382
 
382
- const drawer = document.getElementById('wb-terminal')
383
- if (drawer) init(drawer)
383
+ /** Mounts on the drawer the server rendered into this plugin's element in the bottom slot. */
384
+ const mount: PluginMount = (_root, _page, api) => {
385
+ api.slot('bottom', el => {
386
+ const drawer = el.querySelector<HTMLElement>('#wb-terminal')
387
+ if (drawer) init(drawer)
388
+ })
389
+ }
390
+
391
+ export default mount
@@ -29,7 +29,7 @@ const STYLE = 'style.css'
29
29
  * render, and each session's screen is in it as HTML, so a page shows it from its first render on;
30
30
  * client.js puts the live terminal in its place.
31
31
  */
32
- const drawer = (state: Sessions, screens: Record<string, string>, client: string) =>
32
+ const drawer = (state: Sessions, screens: Record<string, string>) =>
33
33
  html`<aside id="wb-terminal" aria-label="Terminal" data-sessions="${JSON.stringify(state)}">
34
34
  <div class="wb-term-grip" title="drag to resize"></div>
35
35
  <div class="wb-term-bar">
@@ -61,8 +61,7 @@ const drawer = (state: Sessions, screens: Record<string, string>, client: string
61
61
  t.querySelector('.wb-term-placeholder[data-id="' + a.dataset.id + '"]')?.classList.add('wb-current')
62
62
  }
63
63
  } catch {}
64
- </script>
65
- <script type="module" src="${client}"></script>`
64
+ </script>`
66
65
 
67
66
  export default function terminal(): Plugin {
68
67
  return {
@@ -72,8 +71,9 @@ export default function terminal(): Plugin {
72
71
  style: STYLE,
73
72
  setup(ctx) {
74
73
  const term = createTerminal({ dir: ctx.project.dir, home: ctx.dataDir })
75
- const client = ctx.asset(CLIENT)
76
74
  return {
75
+ // the drawer comes alive: client.js mounts on it in its slot
76
+ client: CLIENT,
77
77
  // all of them run code or show what runs, so all need the project's token
78
78
  routes: {
79
79
  'GET /sessions': {
@@ -132,10 +132,12 @@ export default function terminal(): Plugin {
132
132
  run: ({ body }) => term.open(body.id as string | undefined, body.path as string),
133
133
  },
134
134
  },
135
- async shell(req) {
135
+ // the drawer, in the bottom slot, there on the first render: the view transition between two
136
+ // pages morphs it, and the page leaves room for it from the first paint
137
+ async slots(req) {
136
138
  if (!ctx.authorized(req)) return null
137
139
  const state = await term.sessions()
138
- return { body: drawer(state, await term.screens(state.sessions), client) }
140
+ return { bottom: drawer(state, await term.screens(state.sessions)) }
139
141
  },
140
142
  }
141
143
  },