@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
@@ -15,21 +15,22 @@
15
15
  // data-wb-decided an agent picked up its latest answer; data-wb-outcome quotes it in one line
16
16
  // and, for a few seconds after a send went through, `data-wb-just-sent`, to confirm it
17
17
  //
18
- // How that looks is the page's: it styles the attributes, and can follow `api.onForms`. A form
18
+ // How that looks is the page's: it styles the attributes, and can follow `api.forms.on`. A form
19
19
  // marked `data-wb-self` (or inside an element that is) keeps its own fields: no drafts and no last
20
20
  // answer are written into it, for a block's component that shows them itself.
21
21
  import type { Api, FormState, FormStatus } from '../define'
22
22
  import { STATES, isQuestionName, questionState } from '../status.ts'
23
+ import { reactive } from './reactive'
23
24
 
24
25
  type Data = Record<string, unknown>
25
26
 
26
27
  let root: HTMLElement
27
28
  let api: Api
28
29
  let page = ''
29
- const listeners = new Set<(forms: FormStatus[]) => void>()
30
30
 
31
31
  /** The forms the core looks after: named (a name without a colon: see isQuestionName), without an action. */
32
- const managed = (form: HTMLFormElement) => isQuestionName(form.getAttribute('name') ?? '') && !form.hasAttribute('action')
32
+ const managed = (form: HTMLFormElement) =>
33
+ isQuestionName(form.getAttribute('name') ?? '') && !form.hasAttribute('action')
33
34
  const forms = () => [...root.querySelectorAll('form')].filter(managed)
34
35
  const keepsOwnFields = (form: HTMLFormElement) => !!form.closest('[data-wb-self]')
35
36
 
@@ -39,7 +40,8 @@ const keepsOwnFields = (form: HTMLFormElement) => !!form.closest('[data-wb-self]
39
40
  // marked `data-wb-other`, with a text box (`data-wb-other-text`, no name of its own) in its label.
40
41
  // It sends what was typed; typing picks it; an answer that is none of the other options goes back
41
42
  // into its box.
42
- const otherText = (option: HTMLInputElement) => option.closest('label')?.querySelector<HTMLInputElement>('[data-wb-other-text]') ?? null
43
+ const otherText = (option: HTMLInputElement) =>
44
+ option.closest('label')?.querySelector<HTMLInputElement>('[data-wb-other-text]') ?? null
43
45
  const isOther = (el: Element): boolean => el instanceof HTMLInputElement && el.hasAttribute('data-wb-other')
44
46
 
45
47
  function readForm(form: HTMLFormElement): Data {
@@ -49,12 +51,16 @@ function readForm(form: HTMLFormElement): Data {
49
51
  for (const el of elements) if (el.name && el.type === 'checkbox') boxes[el.name] = (boxes[el.name] ?? 0) + 1
50
52
  for (const el of elements) {
51
53
  if (!el.name || el.type === 'submit' || el.type === 'button' || el.type === 'reset' || el.type === 'file') continue
52
- if (isOther(el)) el.value = otherText(el)?.value.trim() ?? ''
54
+ // an other-option sends what was typed in its box: read, not written into the option (for a
55
+ // radio or checkbox that would change its value attribute, and so the page's HTML)
56
+ const value = isOther(el) ? (otherText(el)?.value.trim() ?? '') : el.value
53
57
  if (el.type === 'checkbox') {
54
- if (boxes[el.name] > 1) { const list = (data[el.name] ??= []) as string[]; if (el.checked) list.push(el.value) }
55
- else data[el.name] = el.checked
58
+ if (boxes[el.name] > 1) {
59
+ const list = (data[el.name] ??= []) as string[]
60
+ if (el.checked) list.push(value)
61
+ } else data[el.name] = el.checked
56
62
  } else if (el.type === 'radio') {
57
- if (el.checked) data[el.name] = el.value
63
+ if (el.checked) data[el.name] = value
58
64
  else data[el.name] ??= null
59
65
  } else if ((el as unknown) instanceof HTMLSelectElement && (el as unknown as HTMLSelectElement).multiple) {
60
66
  data[el.name] = [...(el as unknown as HTMLSelectElement).selectedOptions].map(o => o.value)
@@ -66,13 +72,18 @@ function readForm(form: HTMLFormElement): Data {
66
72
  function writeForm(form: HTMLFormElement, data: Data) {
67
73
  const elements = [...form.elements] as HTMLInputElement[]
68
74
  // what the listed options of a name send, so an answer that is none of them is the other one's
69
- const listed = (name: string) => elements.filter(e => e.name === name && !isOther(e) && (e.type === 'radio' || e.type === 'checkbox')).map(e => e.value)
75
+ const listed = (name: string) =>
76
+ elements
77
+ .filter(e => e.name === name && !isOther(e) && (e.type === 'radio' || e.type === 'checkbox'))
78
+ .map(e => e.value)
70
79
  for (const el of elements) {
71
80
  if (!el.name || !(el.name in data) || ['submit', 'button', 'hidden', 'file'].includes(el.type)) continue
72
81
  const v = data[el.name]
73
82
  if (isOther(el)) {
74
83
  const own = listed(el.name)
75
- const rest = (Array.isArray(v) ? v : [v]).filter(x => typeof x === 'string' && x !== '' && !own.includes(x)) as string[]
84
+ const rest = (Array.isArray(v) ? v : [v]).filter(
85
+ x => typeof x === 'string' && x !== '' && !own.includes(x),
86
+ ) as string[]
76
87
  el.checked = rest.length > 0
77
88
  const text = otherText(el)
78
89
  if (text && text !== document.activeElement) text.value = rest.join(', ')
@@ -101,7 +112,11 @@ const draftKey = (form: HTMLFormElement) => `wb-draft:${page}:${form.getAttribut
101
112
  function saveDraft(e: Event) {
102
113
  const form = (e.target as Element | null)?.closest?.('form')
103
114
  if (!form || !managed(form) || !root.contains(form) || keepsOwnFields(form)) return
104
- try { localStorage.setItem(draftKey(form), JSON.stringify(readForm(form))) } catch { /* storage unavailable */ }
115
+ try {
116
+ localStorage.setItem(draftKey(form), JSON.stringify(readForm(form)))
117
+ } catch {
118
+ /* storage unavailable */
119
+ }
105
120
  }
106
121
 
107
122
  /** Unsent drafts back into their forms; a form being typed in is left alone. */
@@ -109,12 +124,22 @@ function restoreDrafts() {
109
124
  for (const form of forms()) {
110
125
  if (keepsOwnFields(form) || form.contains(document.activeElement)) continue
111
126
  let draft: Data | null = null
112
- try { draft = JSON.parse(localStorage.getItem(draftKey(form)) ?? 'null') } catch { /* storage unavailable */ }
127
+ try {
128
+ draft = JSON.parse(localStorage.getItem(draftKey(form)) ?? 'null')
129
+ } catch {
130
+ /* storage unavailable */
131
+ }
113
132
  if (draft) writeForm(form, draft)
114
133
  }
115
134
  }
116
135
 
117
- const dropDraft = (form: HTMLFormElement) => { try { localStorage.removeItem(draftKey(form)) } catch { /* storage unavailable */ } }
136
+ const dropDraft = (form: HTMLFormElement) => {
137
+ try {
138
+ localStorage.removeItem(draftKey(form))
139
+ } catch {
140
+ /* storage unavailable */
141
+ }
142
+ }
118
143
 
119
144
  /** Each form's last answer: its button in data-wb-sent, and with `prefill` its fields. */
120
145
  function restoreLast(prefill: boolean) {
@@ -142,7 +167,10 @@ async function onSubmit(e: SubmitEvent) {
142
167
  // folds because an agent picked the answer up at once
143
168
  form.toggleAttribute('data-wb-just-sent', true)
144
169
  clearTimeout(justSent.get(form))
145
- justSent.set(form, setTimeout(() => form.removeAttribute('data-wb-just-sent'), 3000))
170
+ justSent.set(
171
+ form,
172
+ setTimeout(() => form.removeAttribute('data-wb-just-sent'), 3000),
173
+ )
146
174
  } catch (err) {
147
175
  form.dataset.wbState = 'failed'
148
176
  console.error(err)
@@ -154,17 +182,34 @@ async function onSubmit(e: SubmitEvent) {
154
182
  const ATTR: Record<FormState, string> = { open: 'data-wb-open', pending: 'data-wb-pending', decided: 'data-wb-decided' }
155
183
 
156
184
  /** One line quoting an answer: what was chosen, then what was written. */
157
- function outcome(data: Data, action: string | null) {
158
- const chosen: string[] = [], written: string[] = []
185
+ /**
186
+ * An answer, told apart: the button it was sent with (none for a plain Send), what was chosen
187
+ * (options, short values), and what was written.
188
+ */
189
+ export function answerParts(
190
+ data: Data,
191
+ action: string | null,
192
+ ): { action: string | null; chosen: string[]; written: string[] } {
193
+ const chosen: string[] = [],
194
+ written: string[] = []
159
195
  for (const v of Object.values(data)) {
160
196
  if (v == null || v === '' || v === false) continue
161
- if (Array.isArray(v)) { if (v.length) chosen.push(v.join(', ')) }
162
- else if (v === true) continue
197
+ if (Array.isArray(v)) {
198
+ if (v.length) chosen.push(v.join(', '))
199
+ } else if (v === true) continue
163
200
  else if (String(v).length > 60 || String(v).includes('\n')) written.push(String(v))
164
201
  else chosen.push(String(v))
165
202
  }
166
- if (action && !['Send', 'Submit'].includes(action)) chosen.unshift(action)
167
- return [chosen.join(' · ') || 'sent', ...written.map(w => `“${w.length > 240 ? w.slice(0, 240) + '…' : w}”`)].join('\n')
203
+ return { action: action && !['Send', 'Submit'].includes(action) ? action : null, chosen, written }
204
+ }
205
+
206
+ /** One line quoting an answer: what was chosen, then what was written. */
207
+ function outcome(data: Data, action: string | null) {
208
+ const { action: button, chosen, written } = answerParts(data, action)
209
+ return [
210
+ [button, ...chosen].filter(Boolean).join(' · ') || 'sent',
211
+ ...written.map(w => `“${w.length > 240 ? w.slice(0, 240) + '…' : w}”`),
212
+ ].join('\n')
168
213
  }
169
214
 
170
215
  let current: FormStatus[] = []
@@ -181,7 +226,7 @@ function mark() {
181
226
  else delete form.dataset.wbOutcome
182
227
  return { form, name, state, last }
183
228
  })
184
- for (const fn of listeners) fn(current)
229
+ forms_.notify()
185
230
  }
186
231
 
187
232
  /** After the page re-rendered: answers, drafts and marks back on its forms. */
@@ -202,15 +247,16 @@ export function trackForms(el: HTMLElement, pageId: string, core: Api) {
202
247
  document.addEventListener('change', saveDraft)
203
248
  document.addEventListener('submit', onSubmit)
204
249
  api.onRendered(refresh)
205
- api.onListeners(mark)
206
- api.onEvents(() => { restoreLast(false); mark() })
250
+ api.listeners.on(mark)
251
+ api.events.on(() => {
252
+ restoreLast(false)
253
+ mark()
254
+ })
207
255
  restoreLast(true)
208
256
  restoreDrafts()
209
257
  mark()
210
258
  }
211
259
 
212
- /** For the api: every named form and where it stands, and following that. */
213
- export const formsApi = {
214
- forms: (): FormStatus[] => current,
215
- onForms(fn: (forms: FormStatus[]) => void) { listeners.add(fn); return () => { listeners.delete(fn) } },
216
- }
260
+ /** For the api: every named form and where it stands, as it changes. */
261
+ const forms_ = reactive((): FormStatus[] => current)
262
+ export const formsApi = { forms: forms_ }
@@ -10,16 +10,27 @@ async function flush() {
10
10
  const batch = queue
11
11
  queue = []
12
12
  try {
13
- const res = await fetch('/api/markdown', { method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify({ texts: batch.map(b => b.text) }) })
13
+ const res = await fetch('/api/markdown', {
14
+ method: 'POST',
15
+ headers: { 'content-type': 'application/json' },
16
+ body: JSON.stringify({ texts: batch.map(b => b.text) }),
17
+ })
14
18
  if (!res.ok) throw new Error(await res.text())
15
- const { html, styles } = await res.json() as { html: string[]; styles: string[] }
19
+ const { html, styles } = (await res.json()) as { html: string[]; styles: string[] }
16
20
  for (const href of styles) {
17
- if (![...document.querySelectorAll<HTMLLinkElement>('link[rel=stylesheet]')].some(l => new URL(l.href).pathname === new URL(href, location.href).pathname))
21
+ if (
22
+ ![...document.querySelectorAll<HTMLLinkElement>('link[rel=stylesheet]')].some(
23
+ l => new URL(l.href).pathname === new URL(href, location.href).pathname,
24
+ )
25
+ )
18
26
  document.head.insertAdjacentHTML('beforeend', `<link rel="stylesheet" href="${href.replace(/"/g, '&quot;')}">`)
19
27
  }
20
28
  batch.forEach((b, i) => b.resolve(html[i] ?? ''))
21
29
  } catch (err) {
22
- for (const b of batch) { cache.delete(b.text); b.reject(err) }
30
+ for (const b of batch) {
31
+ cache.delete(b.text)
32
+ b.reject(err)
33
+ }
23
34
  }
24
35
  }
25
36