@bigmistqke/whiteboard 0.5.0 → 0.6.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 (106) hide show
  1. package/README.md +125 -25
  2. package/bin/whiteboard.ts +299 -16
  3. package/dist/bin/whiteboard.js +247 -17
  4. package/dist/client/runtime.js +22 -10
  5. package/dist/plugins/ansi/README.md +36 -0
  6. package/dist/plugins/ansi/index.js +64 -0
  7. package/dist/plugins/ansi/public/style.css +28 -0
  8. package/dist/plugins/badge/README.md +28 -0
  9. package/dist/plugins/badge/index.js +24 -0
  10. package/dist/plugins/badge/public/style.css +30 -0
  11. package/dist/plugins/chat/public/style.css +7 -2
  12. package/dist/plugins/code/README.md +4 -2
  13. package/dist/plugins/code/diff.js +12 -2
  14. package/dist/plugins/grid/README.md +12 -12
  15. package/dist/plugins/grid/index.js +7 -19
  16. package/dist/plugins/grid/public/style.css +4 -33
  17. package/dist/plugins/iframe/README.md +7 -7
  18. package/dist/plugins/iframe/index.js +11 -11
  19. package/dist/plugins/iframe/public/style.css +15 -0
  20. package/dist/plugins/index.js +7 -3
  21. package/dist/plugins/lightbox/README.md +22 -0
  22. package/dist/plugins/lightbox/index.js +20 -0
  23. package/dist/plugins/lightbox/public/client.js +4 -0
  24. package/dist/plugins/lightbox/public/style.css +66 -0
  25. package/dist/plugins/steps/README.md +3 -3
  26. package/dist/plugins/steps/index.js +3 -42
  27. package/dist/plugins/steps/public/client.js +1 -1
  28. package/dist/plugins/terminal/host.js +1 -1
  29. package/dist/plugins/terminal/public/client.js +1 -1
  30. package/dist/plugins/window/README.md +34 -14
  31. package/dist/plugins/window/index.js +21 -13
  32. package/dist/src/auth.js +4 -3
  33. package/dist/src/client/page/base.css +61 -1
  34. package/dist/src/client/page/main.js +60 -14
  35. package/dist/src/entries.js +211 -0
  36. package/dist/src/events.js +1 -1
  37. package/dist/src/hub.js +272 -0
  38. package/dist/src/loader.js +4 -1
  39. package/dist/src/package.js +23 -1
  40. package/dist/src/pages/blocks.js +1 -0
  41. package/dist/src/pages/index.js +1 -1
  42. package/dist/src/pages/markdown.js +87 -33
  43. package/dist/src/pages/render.js +116 -14
  44. package/dist/src/pages/window.js +16 -18
  45. package/dist/src/running.js +37 -2
  46. package/dist/src/serve.js +111 -2
  47. package/dist/src/server.js +49 -14
  48. package/package.json +1 -1
  49. package/plugins/ansi/README.md +36 -0
  50. package/plugins/ansi/index.ts +95 -0
  51. package/plugins/ansi/public/style.css +28 -0
  52. package/plugins/badge/README.md +28 -0
  53. package/plugins/badge/index.ts +35 -0
  54. package/plugins/badge/public/style.css +30 -0
  55. package/plugins/chat/public/style.css +7 -2
  56. package/plugins/code/README.md +4 -2
  57. package/plugins/code/diff.ts +14 -2
  58. package/plugins/grid/README.md +12 -12
  59. package/plugins/grid/index.ts +11 -26
  60. package/plugins/grid/public/style.css +4 -33
  61. package/plugins/iframe/README.md +7 -7
  62. package/plugins/iframe/index.ts +15 -15
  63. package/plugins/iframe/public/style.css +15 -0
  64. package/plugins/index.ts +6 -2
  65. package/plugins/lightbox/README.md +22 -0
  66. package/plugins/lightbox/client.ts +99 -0
  67. package/plugins/lightbox/index.ts +25 -0
  68. package/plugins/lightbox/public/style.css +66 -0
  69. package/plugins/steps/README.md +3 -3
  70. package/plugins/steps/client.ts +15 -6
  71. package/plugins/steps/index.ts +3 -46
  72. package/plugins/terminal/client.ts +16 -7
  73. package/plugins/window/README.md +34 -14
  74. package/plugins/window/index.ts +20 -13
  75. package/skills/whiteboard/SKILL.md +18 -10
  76. package/src/auth.ts +5 -3
  77. package/src/client/base.ts +19 -0
  78. package/src/client/forms.ts +2 -1
  79. package/src/client/markdown.ts +2 -1
  80. package/src/client/page/base.css +61 -1
  81. package/src/client/page/elsewhere.ts +72 -16
  82. package/src/client/page/fresh.ts +2 -1
  83. package/src/client/page/sort.ts +2 -1
  84. package/src/client/runtime.ts +11 -9
  85. package/src/define.ts +66 -7
  86. package/src/entries.ts +258 -0
  87. package/src/events.ts +2 -1
  88. package/src/hub.ts +355 -0
  89. package/src/loader.ts +7 -1
  90. package/src/package.ts +22 -0
  91. package/src/pages/blocks.ts +3 -0
  92. package/src/pages/index.ts +4 -2
  93. package/src/pages/markdown.ts +89 -32
  94. package/src/pages/render.ts +134 -13
  95. package/src/pages/window.ts +16 -25
  96. package/src/running.ts +47 -1
  97. package/src/serve.ts +114 -1
  98. package/src/server.ts +80 -14
  99. package/dist/plugins/gallery/README.md +0 -30
  100. package/dist/plugins/gallery/index.js +0 -75
  101. package/dist/plugins/gallery/public/client.js +0 -1
  102. package/dist/plugins/gallery/public/style.css +0 -90
  103. package/plugins/gallery/README.md +0 -30
  104. package/plugins/gallery/client.tsx +0 -70
  105. package/plugins/gallery/index.ts +0 -89
  106. package/plugins/gallery/public/style.css +0 -90
@@ -0,0 +1,99 @@
1
+ // The lightbox in the browser (index.ts): a click on a picture inside a container marked
2
+ // `data-lightbox` opens it large, over the page, with the container's other pictures and videos to
3
+ // step through (‹ ›, ← →); Esc or a click beside it closes it. It lives outside the page's content,
4
+ // so the page's updates leave it as it is.
5
+ import type { PluginMount } from '../../src/define'
6
+
7
+ const icon = (d: string) =>
8
+ `<svg viewBox="0 0 16 16" width="18" height="18" aria-hidden="true" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><path d="${d}"/></svg>`
9
+ const PREV = icon('M10 3.5 5.5 8l4.5 4.5')
10
+ const NEXT = icon('M6 3.5 10.5 8 6 12.5')
11
+ const CLOSE = icon('M4 4l8 8M12 4l-8 8')
12
+
13
+ /** What the lightbox shows of a container: its pictures and videos, in order; not a picture that is a link. */
14
+ const mediaIn = (scope: Element) =>
15
+ [...scope.querySelectorAll<HTMLImageElement | HTMLVideoElement>('img, video')].filter(
16
+ m => !m.closest('a, .wb-window-bar, .wb-lightbox'),
17
+ )
18
+
19
+ let overlay: HTMLElement | null = null
20
+ let items: (HTMLImageElement | HTMLVideoElement)[] = []
21
+ let at = 0
22
+
23
+ function show() {
24
+ if (!overlay) return
25
+ const item = items[at]!
26
+ const stage = overlay.querySelector('.wb-lightbox-stage')!
27
+ let shown: HTMLElement
28
+ if (item instanceof HTMLVideoElement) {
29
+ const video = item.cloneNode(true) as HTMLVideoElement
30
+ video.controls = true
31
+ video.removeAttribute('autoplay')
32
+ shown = video
33
+ } else {
34
+ const img = document.createElement('img')
35
+ img.src = item.currentSrc || item.src
36
+ img.alt = item.alt
37
+ shown = img
38
+ }
39
+ stage.replaceChildren(shown)
40
+ overlay.querySelector('.wb-lightbox-caption')!.textContent =
41
+ (item instanceof HTMLImageElement ? item.alt : item.getAttribute('aria-label')) ?? ''
42
+ for (const button of overlay.querySelectorAll<HTMLElement>('.wb-lightbox-prev, .wb-lightbox-next'))
43
+ button.hidden = items.length < 2
44
+ }
45
+
46
+ function open(scope: Element, picture: HTMLImageElement) {
47
+ items = mediaIn(scope)
48
+ at = Math.max(0, items.indexOf(picture))
49
+ overlay?.remove()
50
+ overlay = document.createElement('div')
51
+ overlay.className = 'wb-lightbox'
52
+ overlay.setAttribute('role', 'dialog')
53
+ overlay.setAttribute('aria-modal', 'true')
54
+ overlay.innerHTML = `<div class="wb-lightbox-stage"></div><div class="wb-lightbox-caption"></div>
55
+ <button type="button" class="wb-lightbox-prev" aria-label="Previous">${PREV}</button>
56
+ <button type="button" class="wb-lightbox-next" aria-label="Next">${NEXT}</button>
57
+ <button type="button" class="wb-lightbox-close" aria-label="Close">${CLOSE}</button>`
58
+ overlay.addEventListener('click', e => {
59
+ const target = e.target as Element
60
+ if (target.closest('.wb-lightbox-prev')) step(-1)
61
+ else if (target.closest('.wb-lightbox-next')) step(1)
62
+ else if (target.closest('.wb-lightbox-close') || target === overlay || target.matches('.wb-lightbox-stage')) close()
63
+ })
64
+ document.body.append(overlay)
65
+ show()
66
+ }
67
+
68
+ function step(by: number) {
69
+ if (items.length < 2) return
70
+ at = (at + by + items.length) % items.length
71
+ show()
72
+ }
73
+
74
+ function close() {
75
+ overlay?.remove()
76
+ overlay = null
77
+ }
78
+
79
+ let listening = false
80
+ const mount: PluginMount = () => {
81
+ if (listening) return
82
+ listening = true
83
+ document.addEventListener('click', e => {
84
+ const picture = (e.target as Element).closest?.('img')
85
+ const scope = picture?.closest('[data-lightbox]')
86
+ if (!picture || !scope || picture.closest('a, .wb-lightbox')) return
87
+ e.preventDefault()
88
+ open(scope, picture)
89
+ })
90
+ document.addEventListener('keydown', e => {
91
+ if (!overlay) return
92
+ if (e.key === 'Escape') close()
93
+ else if (e.key === 'ArrowLeft') step(-1)
94
+ else if (e.key === 'ArrowRight') step(1)
95
+ else return
96
+ e.preventDefault()
97
+ })
98
+ }
99
+ export default mount
@@ -0,0 +1,25 @@
1
+ // The lightbox: composed around a container (`:::grid{compose=lightbox}`), it makes the pictures in
2
+ // it open large on a click, and ← → step through the container's pictures and videos. The pictures
3
+ // themselves know nothing of it: outside one, a picture is just a picture. It marks the container
4
+ // (its outer element gets `data-lightbox`) rather than wrapping it, so the page lays the container
5
+ // out as before (a wide grid stays wide); the browser module (client.ts) does the rest.
6
+ import fs from 'node:fs'
7
+ import { definePlugin, html, raw, type Plugin } from '@bigmistqke/whiteboard'
8
+ import { builtinPublic } from '../../src/package.ts'
9
+
10
+ /** The container's HTML with its outer element marked; around it, when it starts with no element. */
11
+ export function mark(children: string): string {
12
+ const marked = children.replace(/^(\s*<[a-zA-Z][\w-]*)(?=[\s>/])/, '$1 data-lightbox')
13
+ return marked !== children ? marked : String(html`<div data-lightbox>${raw(children)}</div>`)
14
+ }
15
+
16
+ export default function lightbox(): Plugin {
17
+ return definePlugin({
18
+ name: 'lightbox',
19
+ public: builtinPublic('lightbox'),
20
+ style: 'style.css',
21
+ compose: ({ children }) => mark(String(children)),
22
+ setup: () => ({ client: 'client.js' }),
23
+ docs: () => fs.readFileSync(new URL('./README.md', import.meta.url), 'utf8'),
24
+ })
25
+ }
@@ -0,0 +1,66 @@
1
+ /* The lightbox (index.ts, client.ts): pictures in a container composed with it open on a click */
2
+ [data-lightbox] img:not(a img) {
3
+ cursor: zoom-in;
4
+ }
5
+ /* over the page: the picture or video as large as fits, its caption under it */
6
+ .wb-lightbox {
7
+ position: fixed;
8
+ inset: 0;
9
+ z-index: var(--layer-top);
10
+ background: rgb(0 0 0 / 0.86);
11
+ display: grid;
12
+ place-items: center;
13
+ grid-template-rows: 1fr auto;
14
+ padding: var(--space-3xl) var(--space-2xl);
15
+ }
16
+ .wb-lightbox-stage {
17
+ display: grid;
18
+ place-items: center;
19
+ min-height: 0;
20
+ width: 100%;
21
+ height: 100%;
22
+ }
23
+ .wb-lightbox-stage > :is(img, video) {
24
+ max-width: 100%;
25
+ max-height: calc(100vh - 110px);
26
+ object-fit: contain;
27
+ border-radius: var(--radius-sm);
28
+ }
29
+ .wb-lightbox-caption {
30
+ color: #eee;
31
+ text-align: center;
32
+ padding-top: var(--space-lg);
33
+ }
34
+ .wb-lightbox button {
35
+ position: absolute;
36
+ display: grid;
37
+ place-items: center;
38
+ background: rgb(255 255 255 / 0.12);
39
+ color: #fff;
40
+ border: 0;
41
+ width: 44px;
42
+ height: 44px;
43
+ border-radius: 50%;
44
+ padding: 0;
45
+ cursor: pointer;
46
+ }
47
+ .wb-lightbox button:hover {
48
+ background: rgb(255 255 255 / 0.22);
49
+ }
50
+ .wb-lightbox-prev {
51
+ left: 12px;
52
+ top: 50%;
53
+ translate: 0 -50%;
54
+ }
55
+ .wb-lightbox-next {
56
+ right: 12px;
57
+ top: 50%;
58
+ translate: 0 -50%;
59
+ }
60
+ .wb-lightbox button[hidden] {
61
+ display: none;
62
+ }
63
+ .wb-lightbox-close {
64
+ right: 12px;
65
+ top: 12px;
66
+ }
@@ -2,14 +2,14 @@ One pane, one item at a time, with ‹ › between them: an A/B, the steps of a
2
2
  procedure, versions of a design. It is a window, so ⤢ fills the browser
3
3
  window with it (for going through steps, large).
4
4
 
5
- Each heading at the shallowest level starts an item; without headings, `---`
6
- separates them. With up to four items, all titled, the header shows the
5
+ A heading at the block's shallowest heading level starts an item, titled by
6
+ it, and so does a `---`, untitled. With up to four items, all titled, the header shows the
7
7
  titles as tabs; else it counts (`2 / 5`) and each item shows its title.
8
8
 
9
9
  The items are laid over each other, so the pane is as tall as the tallest
10
10
  and flipping doesn't move the page. ← and → step when it has the focus (a
11
11
  click in it) or fills the window. Which item shows stays when the page
12
- updates.
12
+ updates, even when a step is added before it.
13
13
 
14
14
  Attributes: `count` (count, never tabs), `wide`, `height`.
15
15
 
@@ -1,5 +1,6 @@
1
1
  // Steps in the browser: which item shows, by the ‹ › buttons, the tabs and ← →, and kept across
2
- // the page's updates (which write the block anew) by the block's place among the page's steps. It
2
+ // the page's updates (which write the block anew): the block by its place among the page's steps,
3
+ // the step by its key (a step the agent adds before it doesn't move what you see). It
3
4
  // marks only data-wb-* attributes, which a block's signature leaves out: flipping through items is
4
5
  // not a change to the page.
5
6
  import type { Api, PageHandle, PluginMount } from '../../src/define'
@@ -10,14 +11,22 @@ const items = (win: HTMLElement) => [
10
11
  ]
11
12
  const marks = (win: HTMLElement) => [...win.querySelectorAll<HTMLElement>(':scope > .wb-window-bar .wb-steps-mark')]
12
13
 
13
- /** which item shows, by the block's place */
14
- const at = new Map<number, number>()
14
+ /** which item shows, by the block's place: its key (Item.key), so a step added before it keeps it showing, and where it was */
15
+ const at = new Map<number, { key: string | null; index: number }>()
16
+
17
+ /** Where the step it showed is now: the one with its key, else the same place. */
18
+ function current(win: HTMLElement, place: number): number {
19
+ const was = at.get(place)
20
+ if (!was) return 0
21
+ const byKey = was.key ? items(win).findIndex(item => item.dataset.key === was.key) : -1
22
+ return byKey >= 0 ? byKey : was.index
23
+ }
15
24
 
16
25
  function show(win: HTMLElement, place: number, i: number) {
17
26
  const all = items(win)
18
27
  const n = Math.max(all.length, 1)
19
28
  i = Math.min(Math.max(i, 0), n - 1)
20
- at.set(place, i)
29
+ at.set(place, { key: all[i]?.dataset.key ?? null, index: i })
21
30
  win.setAttribute('data-wb-step', String(i))
22
31
  all.forEach((item, k) => item.toggleAttribute('data-wb-current', k === i))
23
32
  marks(win).forEach((mark, k) => mark.toggleAttribute('data-wb-current', k === i))
@@ -25,12 +34,12 @@ function show(win: HTMLElement, place: number, i: number) {
25
34
  win.toggleAttribute('data-wb-last', i === n - 1)
26
35
  }
27
36
 
28
- const apply = () => windows().forEach((win, place) => show(win, place, at.get(place) ?? 0))
37
+ const apply = () => windows().forEach((win, place) => show(win, place, current(win, place)))
29
38
 
30
39
  function step(win: HTMLElement, to: 'prev' | 'next' | number) {
31
40
  const place = windows().indexOf(win)
32
41
  if (place < 0) return
33
- const now = at.get(place) ?? 0
42
+ const now = current(win, place)
34
43
  show(win, place, to === 'prev' ? now - 1 : to === 'next' ? now + 1 : to)
35
44
  }
36
45
 
@@ -6,47 +6,6 @@ import fs from 'node:fs'
6
6
  import { definePlugin, html, raw, windowFrame, type DirectiveContext, type Plugin } from '@bigmistqke/whiteboard'
7
7
  import { builtinPublic } from '../../src/package.ts'
8
8
 
9
- interface Node {
10
- type: string
11
- depth?: number
12
- value?: string
13
- children?: Node[]
14
- }
15
-
16
- export interface Part {
17
- /** the heading's content, or none */
18
- title: Node[] | null
19
- body: Node[]
20
- }
21
-
22
- /**
23
- * The items of a block: one per heading at its shallowest level (what comes before the first is an
24
- * item of its own); without headings, `---` separates them; else it is one item.
25
- */
26
- export function split(children: Node[]): Part[] {
27
- const content = children.filter(n => !(n.type === 'text' && !n.value?.trim()))
28
- const heads = content.filter(n => n.type === 'heading')
29
- const parts: Part[] = []
30
- if (heads.length) {
31
- const depth = Math.min(...heads.map(h => h.depth!))
32
- for (const n of content) {
33
- if (n.type === 'heading' && n.depth === depth) parts.push({ title: n.children ?? [], body: [] })
34
- else if (parts.length) parts.at(-1)!.body.push(n)
35
- else parts.push({ title: null, body: [n] })
36
- }
37
- return parts
38
- }
39
- let current: Part = { title: null, body: [] }
40
- for (const n of content) {
41
- if (n.type === 'thematicBreak') {
42
- if (current.body.length) parts.push(current)
43
- current = { title: null, body: [] }
44
- } else current.body.push(n)
45
- }
46
- if (current.body.length || !parts.length) parts.push(current)
47
- return parts
48
- }
49
-
50
9
  /** A height in px (a bare number) or a CSS length; else none. */
51
10
  const height = (value: string | undefined) =>
52
11
  value && /^\d+(\.\d+)?$/.test(value)
@@ -66,10 +25,8 @@ const NEXT = svg('M6 3.5 10.5 8 6 12.5')
66
25
  const TABS = 4
67
26
 
68
27
  export async function render(ctx: DirectiveContext) {
69
- const parts = split(ctx.children as Node[])
70
- const items = await Promise.all(
71
- parts.map(async p => ({ title: p.title ? await ctx.toHtml(p.title) : null, html: await ctx.toHtml(p.body) })),
72
- )
28
+ // one per item (DirectiveContext.items): a heading, or a `---`, starts one
29
+ const items = await ctx.items()
73
30
  const tabs = !('count' in ctx.attrs) && items.length <= TABS && items.every(i => i.title)
74
31
  const n = items.length
75
32
  const marks = tabs
@@ -100,7 +57,7 @@ export async function render(ctx: DirectiveContext) {
100
57
  const body = html`<div class="wb-steps" tabindex="0">
101
58
  ${items.map(
102
59
  item =>
103
- html`<section class="wb-steps-item">
60
+ html`<section class="wb-steps-item" data-key="${item.key}">
104
61
  ${!tabs && item.title ? html`<div class="wb-steps-title">${raw(item.title)}</div>` : ''} ${raw(item.html)}
105
62
  </section>`,
106
63
  )}
@@ -33,6 +33,15 @@ interface Session {
33
33
  exited: number | null
34
34
  }
35
35
 
36
+ // the path the project is served under (a shared server serves it below one: see Entries)
37
+ const BASE: string = (() => {
38
+ try {
39
+ return JSON.parse(document.getElementById('wb-page')?.textContent ?? '{}').base ?? ''
40
+ } catch {
41
+ return ''
42
+ }
43
+ })()
44
+
36
45
  function init(drawer: HTMLElement) {
37
46
  const screen = drawer.querySelector<HTMLElement>('.wb-term-screen')!
38
47
  const tabs = drawer.querySelector<HTMLElement>('.wb-term-tabs')!
@@ -97,7 +106,7 @@ function init(drawer: HTMLElement) {
97
106
  const key = `${current} ${dims.cols}x${dims.rows} ${claim}`
98
107
  if (key !== sent) {
99
108
  sent = key
100
- post('/api/terminal/resize', { id: current, viewer, cols: dims.cols, rows: dims.rows, active: claim })
109
+ post(BASE + '/api/terminal/resize', { id: current, viewer, cols: dims.cols, rows: dims.rows, active: claim })
101
110
  // it is ours from now on; the host confirms with a `size`
102
111
  if (claim) {
103
112
  active = viewer
@@ -183,7 +192,7 @@ function init(drawer: HTMLElement) {
183
192
  const data = pending
184
193
  pending = ''
185
194
  chain = chain
186
- .then(() => post('/api/terminal/input', { id, data }))
195
+ .then(() => post(BASE + '/api/terminal/input', { id, data }))
187
196
  .then(
188
197
  () => {},
189
198
  () => {},
@@ -268,7 +277,7 @@ function init(drawer: HTMLElement) {
268
277
  }
269
278
 
270
279
  async function closeTab(id: string) {
271
- await post('/api/terminal/close', { id })
280
+ await post(BASE + '/api/terminal/close', { id })
272
281
  sessions = sessions.filter(x => x.id !== id)
273
282
  if (current === id) select(sessions.at(-1)?.id ?? null)
274
283
  else drawTabs()
@@ -277,7 +286,7 @@ function init(drawer: HTMLElement) {
277
286
  /** Open a tab with the shell, or start tab `id` again as it was. */
278
287
  async function start(body: { id?: string; restart?: boolean }) {
279
288
  setStatus('starting…')
280
- const res = await post('/api/terminal/start', body)
289
+ const res = await post(BASE + '/api/terminal/start', body)
281
290
  const reply = await res.json().catch(() => ({}))
282
291
  if (!res.ok) return setStatus(reply.error ?? 'could not start', [['Try again', () => start(body)]])
283
292
  await loadSessions()
@@ -288,7 +297,7 @@ function init(drawer: HTMLElement) {
288
297
 
289
298
  async function loadSessions() {
290
299
  try {
291
- const res = await fetch('/api/terminal/sessions')
300
+ const res = await fetch(BASE + '/api/terminal/sessions')
292
301
  if (res.ok) ({ sessions } = await res.json())
293
302
  } catch {
294
303
  /* the server is away: the stream reconnects */
@@ -304,7 +313,7 @@ function init(drawer: HTMLElement) {
304
313
  if (!current) showStatus()
305
314
  // without a tab too: the host tells it when one appears (started from the command line, say)
306
315
  stream = new EventSource(
307
- `/api/terminal/stream?${current ? `id=${encodeURIComponent(current)}&` : ''}viewer=${viewer}`,
316
+ `${BASE}/api/terminal/stream?${current ? `id=${encodeURIComponent(current)}&` : ''}viewer=${viewer}`,
308
317
  )
309
318
  stream.addEventListener('sessions', e => {
310
319
  sessions = JSON.parse((e as MessageEvent).data).sessions
@@ -336,7 +345,7 @@ function init(drawer: HTMLElement) {
336
345
  // a program in this tab asked to show a page here (`whiteboard terminal open`): the drawer comes along
337
346
  stream.addEventListener('navigate', e => {
338
347
  const { path } = JSON.parse((e as MessageEvent).data)
339
- if (typeof path === 'string' && path.startsWith('/') && !path.startsWith('//')) location.assign(path)
348
+ if (typeof path === 'string' && path.startsWith('/') && !path.startsWith('//')) location.assign(BASE + path)
340
349
  })
341
350
  // the session's size changed, or another drawer became the active one
342
351
  stream.addEventListener('size', e => {
@@ -1,9 +1,26 @@
1
- Windows: a header with a label, a button that fills the browser window with
1
+ A window: a header with a label, a button that fills the browser window with
2
2
  what it shows (the same button, or Esc, puts it back), and one that opens it on
3
- its own. `bare` leaves the header out; `wide` makes it wider than the text
4
- column; `height` (pixels or a CSS length) fixes its height, scrolling inside.
3
+ its own.
5
4
 
6
- **One image or video**, its label the title, ↗ opening the file:
5
+ **Any block goes in one with `compose=window`**: an image, a video, another
6
+ page, a diagram, a grid. Its label is the window's title (or `title="A
7
+ title"`, for a block without a label, such as a fence), and ↗ opens what it
8
+ shows on its own, an image's file or a page's address. `wide` makes it wider
9
+ than the text column.
10
+
11
+ ````markdown
12
+ ::image[The new onboarding]{src="onboarding.png" compose=window}
13
+
14
+ ```mermaid {compose=window title="How a page is served"}
15
+ graph LR; file --> server --> browser
16
+ ```
17
+
18
+ :::grid{cols=2 compose=window title="Two options"}
19
+ …
20
+ :::
21
+ ````
22
+
23
+ **One image or video**, without it, is just itself:
7
24
 
8
25
  ```markdown
9
26
  ::image[The new onboarding]{src="onboarding.png"}
@@ -13,17 +30,20 @@ column; `height` (pixels or a CSS length) fixes its height, scrolling inside.
13
30
 
14
31
  A video has its controls; `loop`, `autoplay` (which starts it muted, as
15
32
  browsers require) and `poster` are as for a `<video>`. A plain markdown image,
16
- `![…](shot.png)`, stays an image without a window.
33
+ `![…](shot.png)`, is the same as `::image` without it.
17
34
 
18
- **Any markdown**, in `:::window`: a diagram, a table, a screenshot with words
19
- under it. When it holds one image or video, ↗ opens that file.
35
+ **Any markdown**, in `:::window`: a table, a screenshot with words under it,
36
+ what has no block of its own. When it holds one image or video, ↗ opens that
37
+ file. `height` (pixels or a CSS length) fixes its height, scrolling inside.
20
38
 
21
- ````markdown
22
- :::window[How a page is served]{wide}
23
- ```mermaid
24
- graph LR; file --> server --> browser
25
- ```
39
+ ```markdown
40
+ :::window[What each one takes]
41
+ | block | what |
42
+ | --- | --- |
43
+ | `::image` | one image |
26
44
  :::
27
- ````
45
+ ```
28
46
 
29
- An iframe (`::iframe`) is a window around another page.
47
+ `compose=window` is this plugin's `compose`: any plugin can compose itself
48
+ around blocks the same way, and a block names several in order:
49
+ `compose=lightbox,window`.
@@ -1,7 +1,8 @@
1
1
  // Windows (src/pages/window.ts): a header with a label, a button that fills the browser window, and
2
2
  // one that opens what it shows on its own. `:::window[label] … :::` puts any markdown in one (a
3
- // screenshot, a diagram, a table to look at large); `::image[label]{src}` and `::video[label]{src}`
4
- // are one image or video in one. `bare` leaves the header out.
3
+ // table, a screenshot with text); any other block is put in one with `compose=window` (this plugin's
4
+ // compose). `::image[label]{src}` and `::video[label]{src}` are one image or video: alone, or in a
5
+ // window with `compose=window`.
5
6
  import fs from 'node:fs'
6
7
  import { definePlugin, html, raw, windowFrame, type DirectiveContext, type Plugin } from '@bigmistqke/whiteboard'
7
8
 
@@ -34,7 +35,7 @@ const height = (value: string | undefined) =>
34
35
  /** http(s), protocol-relative, or a path on this server; never `javascript:` or `data:`. */
35
36
  const safeSrc = (src: string) => /^(https?:)?\/\//i.test(src) || !/^[a-z][a-z0-9+.-]*:/i.test(src)
36
37
 
37
- /** ::image[label]{src} and ::video[label]{src loop autoplay muted poster}: one file, in a window. */
38
+ /** ::image[label]{src} and ::video[label]{src loop autoplay muted poster}: one file (in a window with `compose=window`). */
38
39
  const media = (kind: 'image' | 'video') => (ctx: DirectiveContext) => {
39
40
  const src = (ctx.attrs.src ?? '').trim()
40
41
  if (!src)
@@ -55,14 +56,13 @@ ${kind}: needs a src, like ::${kind}[a label]{src="${kind === 'image' ? 'shot.pn
55
56
  preload="metadata"
56
57
  ${poster}${alt ? html` aria-label="${alt}"` : ''}
57
58
  ></video>`
58
- return windowFrame({
59
- label: ctx.label,
60
- open: src,
61
- wide: 'wide' in ctx.attrs,
62
- bare: 'bare' in ctx.attrs,
63
- height: height(ctx.attrs.height),
64
- body,
65
- })
59
+ return body
60
+ }
61
+
62
+ /** What an image or video shows on its own, for the ↗ of a window around it: its file. */
63
+ const mediaOpen = (ctx: DirectiveContext) => {
64
+ const src = (ctx.attrs.src ?? '').trim()
65
+ return src && safeSrc(src) ? src : null
66
66
  }
67
67
 
68
68
  export async function render(ctx: DirectiveContext) {
@@ -70,7 +70,6 @@ export async function render(ctx: DirectiveContext) {
70
70
  label: ctx.label,
71
71
  open: openOf(ctx.children as Node[]),
72
72
  wide: 'wide' in ctx.attrs,
73
- bare: 'bare' in ctx.attrs,
74
73
  height: height(ctx.attrs.height),
75
74
  body: await ctx.toHtml(ctx.children),
76
75
  })
@@ -79,7 +78,15 @@ export async function render(ctx: DirectiveContext) {
79
78
  export default function windows(): Plugin {
80
79
  return definePlugin({
81
80
  name: 'window',
82
- blocks: { window: { html: render }, image: { html: media('image') }, video: { html: media('video') } },
81
+ // any block that says `compose=window` goes in one: its `title` or label the window's title,
82
+ // its ↗ what the block shows on its own
83
+ compose: ({ children, label, open, attrs }) =>
84
+ windowFrame({ label: attrs.title ? html`${attrs.title}` : label, open, wide: 'wide' in attrs, body: children }),
85
+ blocks: {
86
+ window: { html: render },
87
+ image: { html: media('image'), open: mediaOpen },
88
+ video: { html: media('video'), open: mediaOpen },
89
+ },
83
90
  docs: () => fs.readFileSync(new URL('./README.md', import.meta.url), 'utf8'),
84
91
  })
85
92
  }
@@ -6,8 +6,9 @@ description: Show the user a live HTML page written in markdown, and get answers
6
6
  # whiteboard
7
7
 
8
8
  Pages are markdown files. In a repository set up for whiteboard they live in
9
- `.whiteboard/pages/` and are served by the project's own server, at a port
10
- `whiteboard show` prints. The user keeps them open in a browser; every save
9
+ `.whiteboard/pages/` and are served by the project's server, at the address
10
+ `whiteboard show` prints: the project's own, or one server the user runs for
11
+ many projects (their worktrees and job folders), under the project's path. The user keeps them open in a browser; every save
11
12
  of the file updates the open page in place, and the blocks that changed are
12
13
  marked until the user has seen them. Forms on a page write the user's answers
13
14
  to an ndjson log you can wait on.
@@ -29,9 +30,13 @@ PATH, the project has it installed locally: run them as `npx whiteboard …`.
29
30
  agents may be writing pages in the same project, so never write a page you
30
31
  did not get from `whiteboard new`, and never pick a page name yourself.
31
32
  3. Write the page into that file.
32
- 4. Run `whiteboard show <file>`: it starts the server if needed and prints the
33
- URL. Give the user the URL the first time; after that, just keep editing
34
- the file — the open page follows.
33
+ 4. Run `whiteboard show <file>`: it starts the project's server if needed and
34
+ prints the URL. Give the user the URL the first time; after that, just
35
+ keep editing the file — the open page follows. When the project is served
36
+ by the user's shared server and that isn't running, `show` says so and
37
+ starts nothing: tell the user, don't try to start it yourself (it is
38
+ started outside your sandbox). A `wait` meanwhile still works: it waits
39
+ for the server, and answers in the log reach it either way.
35
40
  5. A page that was only for the moment (one question, a check, a demo):
36
41
  once it has done its job and you've acted on its answers, close it with
37
42
  `whiteboard close <file>`. That removes the page and its events (answers,
@@ -235,11 +240,14 @@ page (`--page plan` also works, and includes the pages below `plan/`).
235
240
  every answer on the page once, also those given while no watch ran. Use it
236
241
  only with something that reports each printed line as it comes (a
237
242
  monitor), and restart it when that expires.
238
- - Without `--page`, `wait` and `watch` are the project's fallback: they hand
239
- out only what no other wait or watch would take, on any page (also
240
- answers left from sessions that ended). One runs per project; a second
241
- exits 1. Use it only when asked to look after the whole board; for your
242
- own pages, always pass `--page`.
243
+ - Without `--page`, `wait` and `watch` are the fallback: they hand out only
244
+ what no other wait or watch would take, on any page (also answers left
245
+ from sessions that ended). In a project of the user's shared server it
246
+ covers every project served with it (all their worktrees and job folders),
247
+ and each answer says where it is from: `project` (its folder) and `url`;
248
+ act on it in that folder. One runs at a time (per project, or per shared
249
+ group); a second exits 1. Use it only when asked to look after the whole
250
+ board; for your own pages, always pass `--page`.
243
251
  - `whiteboard events --page <file> [--type <name>] [--last 1]` lists what was
244
252
  submitted so far, for when you need the history rather than the next
245
253
  answer. With `--follow` it then prints each new event as it comes, without
package/src/auth.ts CHANGED
@@ -32,7 +32,8 @@ export function projectToken(wb: string): string {
32
32
  return token
33
33
  }
34
34
 
35
- export function createAuth(wb: string) {
35
+ /** `base`: the path the project is served under (see ServerOptions), which its cookie is kept to. */
36
+ export function createAuth(wb: string, base = '') {
36
37
  const token = projectToken(wb)
37
38
  const same = (a: unknown, b: string): boolean =>
38
39
  typeof a === 'string' && a.length === b.length && crypto.timingSafeEqual(Buffer.from(a), Buffer.from(b))
@@ -74,8 +75,9 @@ export function createAuth(wb: string) {
74
75
  }
75
76
  const to = typeof q.to === 'string' && q.to.startsWith('/') && !q.to.startsWith('//') ? q.to : '/'
76
77
  res.writeHead(302, {
77
- 'set-cookie': `${COOKIE}=${token}; Path=/; HttpOnly; SameSite=Lax; Max-Age=31536000`,
78
- location: to,
78
+ // under a shared server, each project's token is sent only to its own path
79
+ 'set-cookie': `${COOKIE}=${token}; Path=${base || '/'}; HttpOnly; SameSite=Lax; Max-Age=31536000`,
80
+ location: base + to,
79
81
  })
80
82
  res.end()
81
83
  return true
@@ -0,0 +1,19 @@
1
+ // The path the page's project is served under: '' on a server of its own, `/rg/develop` under a
2
+ // shared server (see Entries in DESIGN.md). The server says it in the page's handoff. Every request
3
+ // the page makes, and every link it writes, starts with it; what the browser keeps (drafts, what
4
+ // was seen) is kept per project with it, since the projects of a shared server share one origin.
5
+ const handoff = (() => {
6
+ try {
7
+ return JSON.parse(document.getElementById('wb-page')?.textContent ?? '{}') as { base?: string }
8
+ } catch {
9
+ return {}
10
+ }
11
+ })()
12
+
13
+ export const base: string = handoff.base ?? ''
14
+
15
+ /** A path of the project's server (`/api/events`) as this page requests it. */
16
+ export const url = (path: string): string => base + path
17
+
18
+ /** A localStorage key, kept apart from other projects' on the same origin. */
19
+ export const storageKey = (key: string): string => (base ? `${base}|${key}` : key)
@@ -21,6 +21,7 @@
21
21
  import type { Api, FormState, FormStatus } from '../define'
22
22
  import { STATES, isQuestionName, questionState } from '../status.ts'
23
23
  import { reactive } from './reactive'
24
+ import { storageKey } from './base.ts'
24
25
 
25
26
  type Data = Record<string, unknown>
26
27
 
@@ -107,7 +108,7 @@ function onOtherInput(e: Event) {
107
108
 
108
109
  // ---- drafts ------------------------------------------------------------------------------------
109
110
 
110
- const draftKey = (form: HTMLFormElement) => `wb-draft:${page}:${form.getAttribute('name')}`
111
+ const draftKey = (form: HTMLFormElement) => storageKey(`wb-draft:${page}:${form.getAttribute('name')}`)
111
112
 
112
113
  function saveDraft(e: Event) {
113
114
  const form = (e.target as Element | null)?.closest?.('form')