@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
@@ -14,7 +14,8 @@ const toggle = document.createElement('button')
14
14
  toggle.type = 'button'
15
15
  toggle.className = 'wb-toc-toggle'
16
16
  toggle.title = toggle.ariaLabel = 'Contents'
17
- toggle.innerHTML = '<svg viewBox="0 0 16 16" width="14" height="14" aria-hidden="true" fill="none" stroke="currentColor" stroke-width="1.4" stroke-linecap="round"><path d="M2.5 4h11M2.5 8h11M2.5 12h7"/></svg>'
17
+ toggle.innerHTML =
18
+ '<svg viewBox="0 0 16 16" width="14" height="14" aria-hidden="true" fill="none" stroke="currentColor" stroke-width="1.4" stroke-linecap="round"><path d="M2.5 4h11M2.5 8h11M2.5 12h7"/></svg>'
18
19
 
19
20
  let main: HTMLElement
20
21
  let api: Api
@@ -22,40 +23,84 @@ let headings: HTMLElement[] = []
22
23
 
23
24
  const navHeight = () => document.querySelector<HTMLElement>('.wb-nav')?.getBoundingClientRect().height ?? 0
24
25
 
25
- /** Is there room left of the text for the panel? Else it opens from the header. */
26
+ /** The gap between the panel and the text, and between the panel and the window's edge. */
27
+ const GAP = 40
28
+ const EDGE = 16
29
+
30
+ /** Is there room left of the text for the panel at its narrowest? Else it opens from the header. */
26
31
  function fits() {
32
+ const narrowest = parseFloat(getComputedStyle(panel).minWidth) || 0
33
+ return main.getBoundingClientRect().left >= narrowest + GAP + EDGE
34
+ }
35
+
36
+ /** A width as the settings or the options give it: a CSS length, or a number of pixels. */
37
+ const length = (value: unknown): string | null => {
38
+ const m = /^(\d+(?:\.\d+)?)(px|em|rem|ch|vw)?$/.exec(String(value ?? '').trim())
39
+ return m && Number(m[1]) > 0 ? m[1] + (m[2] ?? 'px') : null
40
+ }
41
+
42
+ /**
43
+ * How wide the panel may be: it fits its items, between a minimum and a maximum, which the page's
44
+ * settings can set (`plugins: { toc: { minWidth: 160px, maxWidth: 320px } }`), else the project's options.
45
+ */
46
+ function bounds() {
47
+ const meta = document.querySelector<HTMLMetaElement>('meta[name=wb-toc]')
48
+ const said = handle.settings()
49
+ const own = said && typeof said === 'object' ? (said as Record<string, unknown>) : {}
50
+ for (const [name, prop] of [
51
+ ['minWidth', '--wb-toc-min-width'],
52
+ ['maxWidth', '--wb-toc-max-width'],
53
+ ] as const) {
54
+ const value = length(own[name]) ?? length(meta?.dataset[name])
55
+ if (value) panel.style.setProperty(prop, value)
56
+ else panel.style.removeProperty(prop)
57
+ }
58
+ }
59
+
60
+ /** Docked: its right edge a gap left of the text, so a wider panel grows away from it. */
61
+ function place() {
27
62
  const left = main.getBoundingClientRect().left
28
- return left >= 230
63
+ panel.style.setProperty('--wb-toc-right', `${document.documentElement.clientWidth - left + GAP}px`)
64
+ panel.style.setProperty('--wb-toc-room', `${left - GAP - EDGE}px`)
29
65
  }
30
66
 
31
67
  let handle: PageHandle
32
68
 
33
- /** Does this page have it: what its frontmatter says (`toc:`), and whether it is on everywhere. */
69
+ /** Does this page have it: what its settings say, and whether it is on everywhere. */
34
70
  function wanted() {
35
- const said = ((handle.data as { frontmatter?: Record<string, string> } | null)?.frontmatter ?? {}).toc?.trim().toLowerCase()
71
+ // its settings (`plugins: { toc: … }`): true, or { minWidth, maxWidth }: on; false: off; else as
72
+ // the project says
73
+ const said = handle.settings()
36
74
  const always = document.querySelector('meta[name=wb-toc]')?.getAttribute('content') === 'always'
37
- return always ? said !== 'false' : said === 'true'
75
+ if (said === false) return false
76
+ if (said === true || (said && typeof said === 'object')) return true
77
+ return always
38
78
  }
39
79
 
40
80
  function build() {
41
81
  headings = wanted() ? [...main.querySelectorAll<HTMLElement>(':scope > h2, :scope > h3')] : []
42
- let h2 = 0, h3 = 0
43
- list.replaceChildren(...headings.map((h, i) => {
44
- // the page's numbers: 1, 1.1, 1.2, 2 (base.css counts ## and ### alike)
45
- const number = h.tagName === 'H2' ? `${++h2}` : `${h2}.${++h3}`
46
- if (h.tagName === 'H2') h3 = 0
47
- const li = document.createElement('li')
48
- li.dataset.level = h.tagName === 'H2' ? '2' : '3'
49
- const a = document.createElement('a')
50
- a.href = '#'
51
- a.dataset.index = String(i)
52
- a.innerHTML = `<span class="wb-toc-number"></span><span class="wb-toc-text"></span>`
53
- a.querySelector('.wb-toc-number')!.textContent = number
54
- a.querySelector('.wb-toc-text')!.textContent = h.textContent ?? ''
55
- li.append(a)
56
- return li
57
- }))
82
+ let h2 = 0,
83
+ h3 = 0
84
+ list.replaceChildren(
85
+ ...headings.map((h, i) => {
86
+ // the page's numbers: 1, 1.1, 1.2, 2 (base.css counts ## and ### alike)
87
+ const number = h.tagName === 'H2' ? `${++h2}` : `${h2}.${++h3}`
88
+ if (h.tagName === 'H2') h3 = 0
89
+ const li = document.createElement('li')
90
+ li.dataset.level = h.tagName === 'H2' ? '2' : '3'
91
+ const a = document.createElement('a')
92
+ a.href = '#'
93
+ a.dataset.index = String(i)
94
+ a.innerHTML = `<span class="wb-toc-number"></span><span class="wb-toc-text"></span>`
95
+ a.querySelector('.wb-toc-number')!.textContent = number
96
+ a.querySelector('.wb-toc-text')!.textContent = h.textContent ?? ''
97
+ li.append(a)
98
+ return li
99
+ }),
100
+ )
58
101
  const any = headings.length > 1
102
+ bounds()
103
+ place()
59
104
  toggle.hidden = !any || fits()
60
105
  panel.hidden = !any || (!fits() && !panel.classList.contains('wb-toc-open'))
61
106
  panel.classList.toggle('wb-toc-docked', fits())
@@ -87,7 +132,9 @@ function mark() {
87
132
  if (!badge.dataset.wbHolds!.split(' ').some(id => unread.has(id))) continue
88
133
  const y = badge.getBoundingClientRect().top
89
134
  let at = -1
90
- headings.forEach((h, i) => { if (h.getBoundingClientRect().top <= y) at = i })
135
+ headings.forEach((h, i) => {
136
+ if (h.getBoundingClientRect().top <= y) at = i
137
+ })
91
138
  waiting.add(at)
92
139
  }
93
140
  list.querySelectorAll<HTMLElement>('a').forEach((a, i) => a.toggleAttribute('data-waiting', waiting.has(i)))
@@ -97,15 +144,22 @@ function mark() {
97
144
  function current() {
98
145
  const line = navHeight() + 40
99
146
  let at = 0
100
- headings.forEach((h, i) => { if (h.getBoundingClientRect().top <= line) at = i })
101
- list.querySelectorAll<HTMLElement>('a').forEach((a, i) => a.toggleAttribute('aria-current', i === at && headings.length > 0))
147
+ headings.forEach((h, i) => {
148
+ if (h.getBoundingClientRect().top <= line) at = i
149
+ })
150
+ list
151
+ .querySelectorAll<HTMLElement>('a')
152
+ .forEach((a, i) => a.toggleAttribute('aria-current', i === at && headings.length > 0))
102
153
  }
103
154
 
104
155
  function go(i: number) {
105
156
  const h = headings[i]
106
157
  if (!h) return
107
158
  scrollTo({ top: Math.max(0, scrollY + h.getBoundingClientRect().top - navHeight() - 12), behavior: 'smooth' })
108
- if (!fits()) { panel.classList.remove('wb-toc-open'); panel.hidden = true }
159
+ if (!fits()) {
160
+ panel.classList.remove('wb-toc-open')
161
+ panel.hidden = true
162
+ }
109
163
  }
110
164
 
111
165
  const mount: PluginMount = (root, page, core) => {
@@ -126,14 +180,20 @@ const mount: PluginMount = (root, page, core) => {
126
180
  panel.classList.toggle('wb-toc-open', open)
127
181
  panel.hidden = !open
128
182
  })
129
- document.addEventListener('keydown', e => { if (e.key === 'Escape' && panel.classList.contains('wb-toc-open')) toggle.click() })
183
+ document.addEventListener('keydown', e => {
184
+ if (e.key === 'Escape' && panel.classList.contains('wb-toc-open')) toggle.click()
185
+ })
130
186
  let frame = 0
131
- const soon = (fn: () => void) => () => { cancelAnimationFrame(frame); frame = requestAnimationFrame(fn) }
187
+ const soon = (fn: () => void) => () => {
188
+ cancelAnimationFrame(frame)
189
+ frame = requestAnimationFrame(fn)
190
+ }
132
191
  addEventListener('scroll', soon(current), { passive: true })
133
192
  addEventListener('resize', soon(build))
134
193
  api.onRendered(build)
135
- api.onForms(() => mark())
136
- api.onEvents(() => requestAnimationFrame(mark))
194
+ page.settings.on(build)
195
+ api.forms.on(() => mark())
196
+ api.events.on(() => requestAnimationFrame(mark))
137
197
  build()
138
198
  }
139
199
 
@@ -6,9 +6,10 @@
6
6
  // import toc from '@bigmistqke/whiteboard/toc'
7
7
  // export default defineConfig({ plugins: [toc()] })
8
8
  //
9
- // A page has it when its frontmatter says `toc: true`. With `toc({ always: true })`, every page has
10
- // it, unless its frontmatter says `toc: false`.
11
- import { packagePath } from '../../src/package.ts'
9
+ // A page has it when its frontmatter says so, under `plugins:` (`plugins: { toc: true }`). With
10
+ // `toc({ always: true })`, every page has it, unless its frontmatter says `toc: false` there.
11
+ import { builtinPublic } from '../../src/package.ts'
12
+ import { html } from '../../src/html.ts'
12
13
  import type { Plugin } from '../../src/define.ts'
13
14
 
14
15
  const DOCS = `## Table of contents
@@ -19,21 +20,36 @@ in it. A section with an unanswered question or an unread message has a dot.
19
20
  `
20
21
 
21
22
  export interface TocOptions {
22
- /** on every page, not only the ones whose frontmatter says `toc: true` (`toc: false` still goes without) */
23
+ /** on every page, not only the ones that ask (`plugins: { toc: true }`); `toc: false` there still goes without */
23
24
  always?: boolean
25
+ /** the panel fits its items, but never narrower than this: a CSS length, or pixels (default 160px) */
26
+ minWidth?: string | number
27
+ /** nor wider than this (default 320px); a page can say otherwise: `plugins: { toc: { maxWidth: 280px } }` */
28
+ maxWidth?: string | number
24
29
  }
25
30
 
26
31
  export default function toc(options: TocOptions = {}): Plugin {
27
32
  return {
28
33
  name: 'toc',
29
- docs: () => options.always
30
- ? DOCS + '\nIn this project every page has it; a page without it says `toc: false` in its frontmatter.\n'
31
- : DOCS + '\nA page has it when its frontmatter says `toc: true`:\n\n```markdown\n---\ntoc: true\n---\n# A long page\n```\n',
32
- style: packagePath('dist/plugins/toc/style.css'),
34
+ docs: () =>
35
+ options.always
36
+ ? DOCS +
37
+ '\nIn this project every page has it; a page goes without with `toc: false` under `plugins:` in its frontmatter:\n\n```markdown\n---\nplugins:\n toc: false\n---\n```\n'
38
+ : DOCS +
39
+ '\nA page has it when its frontmatter asks, under `plugins:`:\n\n```markdown\n---\nplugins:\n toc: true\n---\n# A long page\n```\n',
40
+ public: builtinPublic('toc'),
41
+ style: 'style.css',
33
42
  setup: () => ({
34
- client: packagePath('dist/plugins/toc/client.js'),
43
+ client: 'client.js',
35
44
  // how the browser part knows: on every page, or only where the frontmatter asks
36
- shell: () => ({ head: `<meta name="wb-toc" content="${options.always ? 'always' : 'opt-in'}">` }),
45
+ shell: () => ({
46
+ head: html`<meta
47
+ name="wb-toc"
48
+ content="${options.always ? 'always' : 'opt-in'}"
49
+ ${options.minWidth != null ? html` data-min-width="${String(options.minWidth)}"` : ''}
50
+ ${options.maxWidth != null ? html` data-max-width="${String(options.maxWidth)}"` : ''}
51
+ />`,
52
+ }),
37
53
  }),
38
54
  }
39
55
  }
@@ -0,0 +1,99 @@
1
+ /* the table of contents (client.ts): in the margin left of the text, or opened from the header */
2
+ /* it fits its items, between a minimum and a maximum (client.ts sets them from the options and the
3
+ page's frontmatter); docked, never wider than the room left of the text */
4
+ .wb-toc {
5
+ position: fixed;
6
+ z-index: var(--layer-aside);
7
+ font: var(--ui-small) / var(--ui-line-height) var(--sans, sans-serif);
8
+ box-sizing: border-box;
9
+ width: max-content;
10
+ min-width: var(--wb-toc-min-width, 160px);
11
+ max-width: var(--wb-toc-max-width, 320px);
12
+ }
13
+ .wb-toc[hidden],
14
+ .wb-toc-toggle[hidden] {
15
+ display: none;
16
+ }
17
+ .wb-toc-docked {
18
+ top: 72px;
19
+ right: var(--wb-toc-right);
20
+ max-width: min(var(--wb-toc-max-width, 320px), var(--wb-toc-room, 320px));
21
+ max-height: calc(100vh - 120px - var(--wb-inset-bottom, 0px));
22
+ overflow: auto;
23
+ }
24
+ .wb-toc-open:not(.wb-toc-docked) {
25
+ top: 52px;
26
+ left: 16px;
27
+ max-width: min(var(--wb-toc-max-width, 320px), 100vw - 32px);
28
+ max-height: calc(100vh - 100px - var(--wb-inset-bottom, 0px));
29
+ overflow: auto;
30
+ padding: var(--space-sm);
31
+ background: var(--bg, Canvas);
32
+ border: var(--hairline) solid var(--border, #e3e5e9);
33
+ border-radius: var(--radius);
34
+ box-shadow: var(--shadow-pop);
35
+ }
36
+ .wb-toc ol {
37
+ list-style: none;
38
+ margin: 0;
39
+ padding: 0;
40
+ }
41
+ .wb-toc li[data-level='3'] {
42
+ padding-left: var(--space-xl);
43
+ }
44
+ .wb-toc a {
45
+ display: flex;
46
+ gap: var(--space-md);
47
+ align-items: baseline;
48
+ padding: var(--space-2xs) var(--space-md);
49
+ border-radius: var(--radius-sm);
50
+ color: var(--muted, #696e76);
51
+ text-decoration: none;
52
+ border-left: 2px solid transparent;
53
+ }
54
+ .wb-toc a:hover {
55
+ color: var(--text, CanvasText);
56
+ background: color-mix(in srgb, var(--text, CanvasText) var(--tint-faint), transparent);
57
+ }
58
+ .wb-toc a[aria-current] {
59
+ color: var(--text, CanvasText);
60
+ border-left-color: var(--accent, #1d5bbf);
61
+ font-weight: var(--weight-strong);
62
+ }
63
+ .wb-toc-number {
64
+ font-variant-numeric: tabular-nums;
65
+ opacity: 0.7;
66
+ flex: none;
67
+ }
68
+ .wb-toc-text {
69
+ overflow: hidden;
70
+ text-overflow: ellipsis;
71
+ }
72
+ /* something waits for the reader in this section */
73
+ .wb-toc a[data-waiting]::after {
74
+ content: '';
75
+ flex: none;
76
+ width: 7px;
77
+ height: 7px;
78
+ margin-left: auto;
79
+ align-self: center;
80
+ border-radius: 50%;
81
+ background: var(--accent, #1d5bbf);
82
+ }
83
+ .wb-toc-toggle {
84
+ flex: none;
85
+ display: inline-grid;
86
+ place-items: center;
87
+ height: 24px;
88
+ box-sizing: border-box;
89
+ padding: 0 var(--space-sm);
90
+ border-radius: var(--radius-pill);
91
+ border: var(--hairline) solid var(--border, #e3e5e9);
92
+ background: none;
93
+ color: var(--muted, #696e76);
94
+ cursor: pointer;
95
+ }
96
+ .wb-toc-toggle:hover {
97
+ color: var(--text, CanvasText);
98
+ border-color: var(--muted, #696e76);
99
+ }
@@ -0,0 +1,29 @@
1
+ Windows: a header with a label, a button that fills the browser window with
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.
5
+
6
+ **One image or video**, its label the title, ↗ opening the file:
7
+
8
+ ```markdown
9
+ ::image[The new onboarding]{src="onboarding.png"}
10
+
11
+ ::video[The bug, recorded]{src="bug.mp4" loop muted poster="bug.png"}
12
+ ```
13
+
14
+ A video has its controls; `loop`, `autoplay` (which starts it muted, as
15
+ browsers require) and `poster` are as for a `<video>`. A plain markdown image,
16
+ `![…](shot.png)`, stays an image without a window.
17
+
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.
20
+
21
+ ````markdown
22
+ :::window[How a page is served]{wide}
23
+ ```mermaid
24
+ graph LR; file --> server --> browser
25
+ ```
26
+ :::
27
+ ````
28
+
29
+ An iframe (`::iframe`) is a window around another page.
@@ -0,0 +1,85 @@
1
+ // Windows (src/pages/window.ts): a header with a label, a button that fills the browser window, and
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.
5
+ import fs from 'node:fs'
6
+ import { definePlugin, html, raw, windowFrame, type DirectiveContext, type Plugin } from '@bigmistqke/whiteboard'
7
+
8
+ interface Node {
9
+ type: string
10
+ url?: string
11
+ value?: string
12
+ children?: Node[]
13
+ }
14
+
15
+ /** What it shows on its own: the file of one image or video, its only content. */
16
+ export function openOf(children: Node[]): string | null {
17
+ const content = children.filter(n => !(n.type === 'text' && !n.value?.trim()))
18
+ if (content.length !== 1) return null
19
+ const only = content[0]
20
+ if (only.type === 'paragraph') return openOf(only.children ?? [])
21
+ if (only.type === 'image') return only.url ?? null
22
+ if (only.type === 'html') return /^\s*<(?:video|img|audio)\b[^>]*\bsrc="([^"]+)"/i.exec(only.value ?? '')?.[1] ?? null
23
+ return null
24
+ }
25
+
26
+ /** A height in px (a bare number) or a CSS length; else none. */
27
+ const height = (value: string | undefined) =>
28
+ value && /^\d+(\.\d+)?$/.test(value)
29
+ ? `${value}px`
30
+ : value && /^\d+(\.\d+)?(px|em|rem|vh|svh|dvh)$/.test(value)
31
+ ? value
32
+ : undefined
33
+
34
+ /** http(s), protocol-relative, or a path on this server; never `javascript:` or `data:`. */
35
+ const safeSrc = (src: string) => /^(https?:)?\/\//i.test(src) || !/^[a-z][a-z0-9+.-]*:/i.test(src)
36
+
37
+ /** ::image[label]{src} and ::video[label]{src loop autoplay muted poster}: one file, in a window. */
38
+ const media = (kind: 'image' | 'video') => (ctx: DirectiveContext) => {
39
+ const src = (ctx.attrs.src ?? '').trim()
40
+ if (!src)
41
+ return html`<pre class="wb-error">
42
+ ${kind}: needs a src, like ::${kind}[a label]{src="${kind === 'image' ? 'shot.png' : 'demo.mp4'}"}</pre>`
43
+ if (!safeSrc(src))
44
+ return html`<pre class="wb-error">${kind}: src must be an http(s) URL or a path, not ${src.split(':')[0]}:</pre>`
45
+ const alt = ctx.labelText ?? ctx.attrs.alt ?? ''
46
+ const flag = (name: string) => name in ctx.attrs && raw(` ${name}`)
47
+ const poster = ctx.attrs.poster && safeSrc(ctx.attrs.poster) ? html` poster="${ctx.attrs.poster}"` : ''
48
+ const body =
49
+ kind === 'image'
50
+ ? html`<img src="${src}" alt="${alt}" loading="lazy" />`
51
+ : html`<video
52
+ src="${src}"
53
+ controls${flag('loop')}${flag('autoplay')}${'autoplay' in ctx.attrs || 'muted' in ctx.attrs ? raw(' muted') : ''}
54
+ playsinline
55
+ preload="metadata"
56
+ ${poster}${alt ? html` aria-label="${alt}"` : ''}
57
+ ></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
+ })
66
+ }
67
+
68
+ export async function render(ctx: DirectiveContext) {
69
+ return windowFrame({
70
+ label: ctx.label,
71
+ open: openOf(ctx.children as Node[]),
72
+ wide: 'wide' in ctx.attrs,
73
+ bare: 'bare' in ctx.attrs,
74
+ height: height(ctx.attrs.height),
75
+ body: await ctx.toHtml(ctx.children),
76
+ })
77
+ }
78
+
79
+ export default function windows(): Plugin {
80
+ return definePlugin({
81
+ name: 'window',
82
+ blocks: { window: { html: render }, image: { html: media('image') }, video: { html: media('video') } },
83
+ docs: () => fs.readFileSync(new URL('./README.md', import.meta.url), 'utf8'),
84
+ })
85
+ }
@@ -256,7 +256,9 @@ your wait runs doesn't change that, so answers given while none runs wait in
256
256
  the log for you. So:
257
257
 
258
258
  - An answer counts as picked up once your `wait` or `watch` hands it to you;
259
- the form then folds into one line quoting it, as decided. Answers no wait
259
+ the form then folds into a card showing the answer, as decided, with its
260
+ chat folded into "💬 n messages" (your messages there still open it for
261
+ the user). Answers no wait
260
262
  has handed out show as waiting for you to pick them up. Waiting on a
261
263
  decided form by name (`--type`) asks the user again: it is open again.
262
264
  - Keep a `wait` or `watch` running for as long as you want answers, and only
package/solid/index.tsx CHANGED
@@ -9,7 +9,7 @@
9
9
  // the api's events and theme as signals through context.
10
10
  import { createContext, createSignal, onSettled, useContext, type Component } from 'solid-js'
11
11
  import { render } from '@solidjs/web'
12
- import type { ClientApi, Mount, Theme, WbEvent } from '@bigmistqke/whiteboard'
12
+ import type { ClientApi, FormStatus, Listener, Mount, Reactive, Theme, WbEvent } from '@bigmistqke/whiteboard'
13
13
 
14
14
  export interface Whiteboard {
15
15
  /** this page's id */
@@ -22,32 +22,47 @@ export interface Whiteboard {
22
22
  append: ClientApi['append']
23
23
  /** the theme in effect. Reactive. */
24
24
  theme(): Theme
25
+ /** the agents waiting for answers on this page. Reactive. */
26
+ listeners(): Listener[]
27
+ /** the page's named forms and where each stands. Reactive. */
28
+ forms(): FormStatus[]
29
+ /** the agent's messages the reader hasn't read. Reactive. */
30
+ unread(): WbEvent[]
25
31
  /** a message's markdown as HTML, rendered by the server (see Api.markdown) */
26
32
  markdown: ClientApi['markdown']
27
33
  }
28
34
 
29
35
  const WhiteboardContext = createContext<Whiteboard>()
30
36
 
31
- /** The page's events and theme, as signals. Use inside a component passed to solid(). */
37
+ /** The page's events, theme, listeners, forms and unread messages, as signals. Use inside a component passed to solid(). */
32
38
  export const useWhiteboard = (): Whiteboard => useContext(WhiteboardContext)
33
39
 
34
40
  /** Turn a Solid component into a template client: `export default solid(MyComponent)`. */
35
41
  export function solid<Props extends object>(Component: Component<Props>): Mount<Props> {
36
42
  return (element: HTMLElement, props: Props, api: ClientApi) =>
37
43
  render(() => {
38
- const [events, setEvents] = createSignal<WbEvent[]>(api.events())
39
- const [theme, setTheme] = createSignal<Theme>(api.theme())
40
- onSettled(() => {
41
- const offEvents = api.onEvents(list => setEvents(() => list))
42
- const offTheme = api.onTheme(setTheme)
43
- return () => { offEvents(); offTheme() }
44
- })
44
+ // each of the api's changing values (Reactive) as a signal, set on each change; what .on
45
+ // returns stops it when the component goes
46
+ // (boxed: a T that is a function would read as createSignal's function form)
47
+ const signal = <T,>(value: Reactive<T>) => {
48
+ const [get, set] = createSignal({ v: value() })
49
+ onSettled(() => value.on(next => set({ v: next })))
50
+ return () => get().v
51
+ }
52
+ const events = signal(api.events)
53
+ const theme = signal(api.theme)
54
+ const listeners = signal(api.listeners)
55
+ const forms = signal(api.forms)
56
+ const unread = signal(api.unread)
45
57
  const value: Whiteboard = {
46
58
  page: api.page,
47
59
  events,
48
60
  last: type => events().findLast(e => e.type === type),
49
61
  append: api.append,
50
62
  theme,
63
+ listeners,
64
+ forms,
65
+ unread,
51
66
  markdown: api.markdown,
52
67
  }
53
68
  return (
package/src/auth.ts CHANGED
@@ -24,7 +24,9 @@ const COOKIE = 'wb-token'
24
24
  /** The project's token, made on first use. */
25
25
  export function projectToken(wb: string): string {
26
26
  const file = path.join(wb, 'token')
27
- try { return fs.readFileSync(file, 'utf8').trim() } catch {}
27
+ try {
28
+ return fs.readFileSync(file, 'utf8').trim()
29
+ } catch {}
28
30
  const token = crypto.randomBytes(24).toString('base64url')
29
31
  fs.writeFileSync(file, token + '\n', { mode: 0o600 })
30
32
  return token
@@ -32,12 +34,19 @@ export function projectToken(wb: string): string {
32
34
 
33
35
  export function createAuth(wb: string) {
34
36
  const token = projectToken(wb)
35
- const same = (a: unknown, b: string): boolean => typeof a === 'string' && a.length === b.length && crypto.timingSafeEqual(Buffer.from(a), Buffer.from(b))
37
+ const same = (a: unknown, b: string): boolean =>
38
+ typeof a === 'string' && a.length === b.length && crypto.timingSafeEqual(Buffer.from(a), Buffer.from(b))
36
39
  const cookies = (req: IncomingMessage): Record<string, string | undefined> =>
37
- Object.fromEntries((req.headers.cookie ?? '').split(';').map(c => c.trim().split('=')).filter(([k]) => k))
40
+ Object.fromEntries(
41
+ (req.headers.cookie ?? '')
42
+ .split(';')
43
+ .map(c => c.trim().split('='))
44
+ .filter(([k]) => k),
45
+ )
38
46
 
39
- /** Has this request the token, from this origin? */
40
- function authorized(req: IncomingMessage): boolean {
47
+ /** Has this request the token, from this origin? No request (a page being built) has none. */
48
+ function authorized(req: IncomingMessage | null): boolean {
49
+ if (!req) return false
41
50
  const jar = cookies(req)
42
51
  const has = same(jar[COOKIE], token) || same(req.headers['x-whiteboard-token'], token)
43
52
  if (!has) return false
@@ -49,15 +58,25 @@ export function createAuth(wb: string) {
49
58
  }
50
59
 
51
60
  /** Opening a page as the tab's document: no frame, no fetch, and not an API route. */
52
- const opensPage = (req: IncomingMessage) => req.method === 'GET' && req.headers['sec-fetch-mode'] === 'navigate'
53
- && req.headers['sec-fetch-dest'] === 'document' && !(req.url ?? '').startsWith('/api/')
61
+ const opensPage = (req: IncomingMessage) =>
62
+ req.method === 'GET' &&
63
+ req.headers['sec-fetch-mode'] === 'navigate' &&
64
+ req.headers['sec-fetch-dest'] === 'document' &&
65
+ !(req.url ?? '').startsWith('/api/')
54
66
 
55
67
  /** The /_/auth route; false for any other path. */
56
68
  function handle(req: IncomingMessage, res: ServerResponse, p: string, q: Record<string, unknown>): boolean {
57
69
  if (p !== '/_/auth') return false
58
- if (!same(q.token, token)) { res.writeHead(403, { 'content-type': 'text/plain' }); res.end('wrong token'); return true }
70
+ if (!same(q.token, token)) {
71
+ res.writeHead(403, { 'content-type': 'text/plain' })
72
+ res.end('wrong token')
73
+ return true
74
+ }
59
75
  const to = typeof q.to === 'string' && q.to.startsWith('/') && !q.to.startsWith('//') ? q.to : '/'
60
- res.writeHead(302, { 'set-cookie': `${COOKIE}=${token}; Path=/; HttpOnly; SameSite=Lax; Max-Age=31536000`, location: to })
76
+ res.writeHead(302, {
77
+ 'set-cookie': `${COOKIE}=${token}; Path=/; HttpOnly; SameSite=Lax; Max-Age=31536000`,
78
+ location: to,
79
+ })
61
80
  res.end()
62
81
  return true
63
82
  }