@bigmistqke/whiteboard 0.3.1 → 0.4.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 (153) hide show
  1. package/README.md +67 -17
  2. package/bin/whiteboard.ts +268 -71
  3. package/dist/client/runtime.js +88 -41
  4. package/dist/define.js +21 -2
  5. package/dist/plugins/callout/index.js +3 -1
  6. package/dist/plugins/callout/public/style.css +30 -0
  7. package/dist/plugins/chat/index.js +7 -4
  8. package/dist/plugins/chat/{client.js → public/client.js} +1 -1
  9. package/dist/plugins/chat/public/style.css +98 -0
  10. package/dist/plugins/code/index.js +3 -1
  11. package/dist/plugins/code/public/style.css +68 -0
  12. package/dist/plugins/comments/index.js +4 -5
  13. package/dist/plugins/comments/public/client.js +19 -0
  14. package/dist/plugins/comments/public/style.css +185 -0
  15. package/dist/plugins/details/index.js +7 -2
  16. package/dist/plugins/details/public/style.css +7 -0
  17. package/dist/plugins/form/index.js +37 -8
  18. package/dist/plugins/form/public/style.css +126 -0
  19. package/dist/plugins/gallery/index.js +4 -3
  20. package/dist/plugins/gallery/public/client.js +1 -0
  21. package/dist/plugins/gallery/public/style.css +90 -0
  22. package/dist/plugins/grid/index.js +9 -3
  23. package/dist/plugins/grid/public/style.css +71 -0
  24. package/dist/plugins/iframe/index.js +13 -14
  25. package/dist/plugins/iframe/public/style.css +13 -0
  26. package/dist/plugins/index.js +3 -1
  27. package/dist/plugins/mermaid/index.js +3 -1
  28. package/dist/plugins/mermaid/{style.css → public/style.css} +7 -2
  29. package/dist/plugins/terminal/index.js +10 -9
  30. package/dist/plugins/terminal/{style.css → public/style.css} +199 -45
  31. package/dist/plugins/toc/index.js +4 -3
  32. package/dist/plugins/toc/public/client.js +1 -0
  33. package/dist/plugins/toc/public/style.css +93 -0
  34. package/dist/plugins/window/README.md +29 -0
  35. package/dist/plugins/window/index.js +64 -0
  36. package/dist/solid/index.js +14 -11
  37. package/dist/src/client/page/base.css +1078 -199
  38. package/dist/src/client/page/main.js +174 -33
  39. package/dist/src/loader.js +7 -2
  40. package/dist/src/package.js +3 -6
  41. package/dist/src/pages/blocks.js +8 -13
  42. package/dist/src/pages/markdown.js +1 -1
  43. package/dist/src/pages/render.js +74 -15
  44. package/dist/src/pages/window.js +30 -0
  45. package/dist/src/public.js +24 -0
  46. package/dist/src/server.js +36 -14
  47. package/package.json +3 -1
  48. package/plugins/callout/index.ts +3 -1
  49. package/plugins/callout/public/style.css +30 -0
  50. package/plugins/chat/autosize.ts +43 -9
  51. package/plugins/chat/client.tsx +22 -5
  52. package/plugins/chat/index.ts +22 -8
  53. package/plugins/chat/public/style.css +98 -0
  54. package/plugins/claude-code/index.ts +49 -14
  55. package/plugins/code/diff.ts +28 -8
  56. package/plugins/code/index.ts +30 -20
  57. package/plugins/code/public/style.css +68 -0
  58. package/plugins/comments/client.ts +139 -71
  59. package/plugins/comments/index.ts +9 -7
  60. package/plugins/comments/public/style.css +185 -0
  61. package/plugins/details/index.ts +7 -2
  62. package/plugins/details/public/style.css +7 -0
  63. package/plugins/forge/index.ts +16 -3
  64. package/plugins/form/index.ts +75 -27
  65. package/plugins/form/public/style.css +126 -0
  66. package/plugins/gallery/client.tsx +9 -3
  67. package/plugins/gallery/index.ts +31 -9
  68. package/plugins/gallery/public/style.css +90 -0
  69. package/plugins/grid/index.ts +19 -5
  70. package/plugins/grid/public/style.css +71 -0
  71. package/plugins/iframe/index.ts +20 -23
  72. package/plugins/iframe/public/style.css +13 -0
  73. package/plugins/index.ts +16 -1
  74. package/plugins/mermaid/index.ts +3 -1
  75. package/plugins/mermaid/{style.css → public/style.css} +7 -2
  76. package/plugins/terminal/client.ts +105 -48
  77. package/plugins/terminal/host.ts +131 -29
  78. package/plugins/terminal/index.ts +84 -25
  79. package/plugins/terminal/public/style.css +225 -0
  80. package/plugins/terminal/server.ts +64 -13
  81. package/plugins/terminal/sizes.ts +13 -3
  82. package/plugins/toc/client.ts +45 -26
  83. package/plugins/toc/index.ts +9 -6
  84. package/plugins/toc/public/style.css +93 -0
  85. package/plugins/window/README.md +29 -0
  86. package/plugins/window/index.ts +85 -0
  87. package/skills/whiteboard/SKILL.md +3 -1
  88. package/solid/index.tsx +24 -9
  89. package/src/auth.ts +25 -7
  90. package/src/client/forms.ts +74 -28
  91. package/src/client/markdown.ts +15 -4
  92. package/src/client/page/base.css +1078 -199
  93. package/src/client/page/elsewhere.ts +32 -15
  94. package/src/client/page/fresh.ts +91 -23
  95. package/src/client/page/hold.ts +11 -2
  96. package/src/client/page/live.ts +10 -6
  97. package/src/client/page/main.ts +106 -36
  98. package/src/client/page/orphans.ts +25 -10
  99. package/src/client/page/sort.ts +14 -4
  100. package/src/client/page/sticky.ts +11 -3
  101. package/src/client/page/theme.ts +11 -7
  102. package/src/client/page/waiting.ts +141 -34
  103. package/src/client/page/window.ts +40 -0
  104. package/src/client/reactive.ts +19 -0
  105. package/src/client/read.ts +17 -10
  106. package/src/client/runtime.ts +130 -35
  107. package/src/client/scroll.ts +14 -4
  108. package/src/client/toast.ts +19 -5
  109. package/src/config.ts +20 -5
  110. package/src/define.ts +65 -25
  111. package/src/events.ts +111 -24
  112. package/src/html.ts +6 -2
  113. package/src/icons.ts +4 -2
  114. package/src/load.ts +10 -2
  115. package/src/loader.ts +75 -11
  116. package/src/package.ts +9 -8
  117. package/src/pages/blocks.ts +20 -14
  118. package/src/pages/markdown.ts +113 -34
  119. package/src/pages/render.ts +199 -39
  120. package/src/pages/window.ts +71 -0
  121. package/src/plugins.ts +90 -24
  122. package/src/public.ts +39 -0
  123. package/src/running.ts +8 -2
  124. package/src/serve.ts +29 -7
  125. package/src/server.ts +348 -107
  126. package/src/status.ts +15 -9
  127. package/dist/plugins/callout/style.css +0 -12
  128. package/dist/plugins/chat/style.css +0 -24
  129. package/dist/plugins/code/style.css +0 -29
  130. package/dist/plugins/comments/client.js +0 -10
  131. package/dist/plugins/comments/style.css +0 -40
  132. package/dist/plugins/details/style.css +0 -3
  133. package/dist/plugins/form/style.css +0 -44
  134. package/dist/plugins/gallery/client.js +0 -1
  135. package/dist/plugins/gallery/style.css +0 -27
  136. package/dist/plugins/grid/style.css +0 -25
  137. package/dist/plugins/iframe/client.js +0 -1
  138. package/dist/plugins/iframe/style.css +0 -29
  139. package/dist/plugins/toc/client.js +0 -1
  140. package/dist/plugins/toc/style.css +0 -19
  141. package/plugins/callout/style.css +0 -12
  142. package/plugins/chat/style.css +0 -24
  143. package/plugins/code/style.css +0 -29
  144. package/plugins/comments/style.css +0 -40
  145. package/plugins/details/style.css +0 -3
  146. package/plugins/form/style.css +0 -44
  147. package/plugins/gallery/style.css +0 -27
  148. package/plugins/grid/style.css +0 -25
  149. package/plugins/iframe/client.ts +0 -46
  150. package/plugins/iframe/style.css +0 -29
  151. package/plugins/terminal/style.css +0 -71
  152. package/plugins/toc/style.css +0 -19
  153. /package/dist/plugins/terminal/{client.js → public/client.js} +0 -0
@@ -8,10 +8,12 @@ function sortList(select: HTMLSelectElement) {
8
8
  const list = select.closest('section')?.querySelector('ul')
9
9
  if (!list) return
10
10
  const by = select.value
11
- const value = (li: HTMLElement) => by === 'title' ? li.dataset.title ?? '' : Number(by === 'updated' ? li.dataset.updated : li.dataset.order)
11
+ const value = (li: HTMLElement) =>
12
+ by === 'title' ? (li.dataset.title ?? '') : Number(by === 'updated' ? li.dataset.updated : li.dataset.order)
12
13
  const rows = [...list.children] as HTMLElement[]
13
14
  rows.sort((a, b) => {
14
- const x = value(a), y = value(b)
15
+ const x = value(a),
16
+ y = value(b)
15
17
  if (by === 'updated') return (y as number) - (x as number)
16
18
  return typeof x === 'string' ? x.localeCompare(y as string) : (x as number) - (y as number)
17
19
  })
@@ -21,7 +23,11 @@ function sortList(select: HTMLSelectElement) {
21
23
  export function applySorts() {
22
24
  for (const select of document.querySelectorAll<HTMLSelectElement>('select[data-wb-sort]')) {
23
25
  let kept: string | null = null
24
- try { kept = localStorage.getItem(KEY(select.dataset.wbSort!)) } catch { /* private window */ }
26
+ try {
27
+ kept = localStorage.getItem(KEY(select.dataset.wbSort!))
28
+ } catch {
29
+ /* private window */
30
+ }
25
31
  if (kept && [...select.options].some(o => o.value === kept)) select.value = kept
26
32
  sortList(select)
27
33
  }
@@ -31,7 +37,11 @@ export function trackSorts() {
31
37
  document.addEventListener('change', e => {
32
38
  const select = e.target as HTMLSelectElement
33
39
  if (!select.matches?.('select[data-wb-sort]')) return
34
- try { localStorage.setItem(KEY(select.dataset.wbSort!), select.value) } catch { /* private window */ }
40
+ try {
41
+ localStorage.setItem(KEY(select.dataset.wbSort!), select.value)
42
+ } catch {
43
+ /* private window */
44
+ }
35
45
  sortList(select)
36
46
  })
37
47
  applySorts()
@@ -10,8 +10,13 @@ function place(main: HTMLElement) {
10
10
  const nav = document.querySelector<HTMLElement>('.wb-nav')
11
11
  const base = nav ? nav.getBoundingClientRect().height : 0
12
12
  for (const summary of main.querySelectorAll<HTMLElement>('details > summary')) {
13
- let top = base, depth = 0
14
- for (let d = summary.parentElement!.parentElement?.closest('details'); d && main.contains(d); d = d.parentElement?.closest('details')) {
13
+ let top = base,
14
+ depth = 0
15
+ for (
16
+ let d = summary.parentElement!.parentElement?.closest('details');
17
+ d && main.contains(d);
18
+ d = d.parentElement?.closest('details')
19
+ ) {
15
20
  const outer = d.querySelector<HTMLElement>(':scope > summary')
16
21
  if (outer && d.open) top += outer.getBoundingClientRect().height
17
22
  depth++
@@ -36,7 +41,10 @@ function keepTitle(e: MouseEvent) {
36
41
  export function trackSticky(main: HTMLElement, onRendered: (fn: () => void) => void) {
37
42
  main.addEventListener('click', keepTitle)
38
43
  let frame = 0
39
- const later = () => { cancelAnimationFrame(frame); frame = requestAnimationFrame(() => place(main)) }
44
+ const later = () => {
45
+ cancelAnimationFrame(frame)
46
+ frame = requestAnimationFrame(() => place(main))
47
+ }
40
48
  // toggle doesn't bubble: listened for on the way down
41
49
  main.addEventListener('toggle', later, true)
42
50
  addEventListener('resize', later)
@@ -1,25 +1,27 @@
1
1
  // The page's light and dark theme: the reader's choice (kept in localStorage), else the system's.
2
2
  import type { Theme } from '../../define.ts'
3
+ import { reactive } from '../reactive'
3
4
 
4
5
  export type ThemePreference = Theme | 'auto'
5
6
 
6
7
  const media = matchMedia('(prefers-color-scheme: dark)')
7
- const listeners = new Set<(theme: Theme) => void>()
8
- const resolve = (): Theme => (document.documentElement.dataset.theme as Theme | undefined) ?? (media.matches ? 'dark' : 'light')
8
+ const resolve = (): Theme =>
9
+ (document.documentElement.dataset.theme as Theme | undefined) ?? (media.matches ? 'dark' : 'light')
9
10
  let theme = resolve()
10
11
 
11
12
  function update() {
12
13
  const next = resolve()
13
14
  if (next === theme) return
14
15
  theme = next
15
- for (const fn of listeners) fn(theme)
16
+ currentTheme.notify()
16
17
  }
17
18
  media.addEventListener('change', update)
18
19
 
19
- export const currentTheme = () => theme
20
- export function onTheme(fn: (theme: Theme) => void) { listeners.add(fn); return () => listeners.delete(fn) }
20
+ /** The theme in effect, as it changes (api.theme). */
21
+ export const currentTheme = reactive(() => theme)
21
22
 
22
- export const themePreference = (): ThemePreference => (document.documentElement.dataset.theme as Theme | undefined) ?? 'auto'
23
+ export const themePreference = (): ThemePreference =>
24
+ (document.documentElement.dataset.theme as Theme | undefined) ?? 'auto'
23
25
 
24
26
  export function setThemePreference(pref: ThemePreference) {
25
27
  if (pref === 'auto') delete document.documentElement.dataset.theme
@@ -27,6 +29,8 @@ export function setThemePreference(pref: ThemePreference) {
27
29
  try {
28
30
  if (pref === 'auto') localStorage.removeItem('wb-theme')
29
31
  else localStorage.setItem('wb-theme', pref)
30
- } catch { /* the choice still applies to this visit */ }
32
+ } catch {
33
+ /* the choice still applies to this visit */
34
+ }
31
35
  update()
32
36
  }
@@ -1,6 +1,6 @@
1
1
  // How the page shows what waits for the reader: its open questions and the agent's messages they
2
2
  // haven't read (status.ts). The core marks each named form with where it stands (open, pending,
3
- // decided; see `api.onForms`); the page styles those marks (base.css), folds a decided form into
3
+ // decided; see `api.forms.on`); the page styles those marks (base.css), folds a decided form into
4
4
  // one line quoting its answer, sums them up in a line above the page, counts them in the tab title,
5
5
  // and puts two toasts in the page's corner, "→ unanswered (n)" for the questions and "→ unread
6
6
  // (n)" for the messages, each going from one to the next. A message is found on the page by its
@@ -8,7 +8,8 @@
8
8
  // (`data-wb-holds`: a comment's badge), which is sent `wb-reveal` to show it. Arriving by a link that
9
9
  // ends in `#wb-waiting` (the header's menu, the overview), the page goes to the first. An answer no
10
10
  // agent is listening for says so once it is sent, in a toast: the form's own mark may be out of view.
11
- import type { Api, FormState, FormStatus, Toast } from '../../define.ts'
11
+ import type { Api, FormState, FormStatus, Toast, WbEvent } from '../../define.ts'
12
+ import { answerParts } from '../forms'
12
13
  import { viewBottom } from './view'
13
14
  import { STATES } from '../../status.ts'
14
15
  import { WAITING_HASH } from './elsewhere'
@@ -25,7 +26,6 @@ let main: HTMLElement
25
26
  let api: Api
26
27
  let forms: FormStatus[] = []
27
28
 
28
-
29
29
  // above the page's content, not in it: #wb-main is compared block by block on every update
30
30
  const summary = document.createElement('div')
31
31
  summary.className = 'wb-summary'
@@ -41,15 +41,24 @@ const inState = (state: FormState) => forms.filter(f => f.state === state && f.f
41
41
  function messageTarget(id: string): HTMLElement | null {
42
42
  const own = document.querySelector<HTMLElement>(`[data-wb-message="${CSS.escape(id)}"]`)
43
43
  if (own && own.getClientRects().length) return own
44
- return [...document.querySelectorAll<HTMLElement>('[data-wb-holds]')].find(el => el.dataset.wbHolds!.split(' ').includes(id)) ?? null
44
+ return (
45
+ [...document.querySelectorAll<HTMLElement>('[data-wb-holds]')].find(el =>
46
+ el.dataset.wbHolds!.split(' ').includes(id),
47
+ ) ?? null
48
+ )
45
49
  }
46
50
 
47
51
  /** What waits for the reader, top to bottom: open forms and unread messages. */
48
52
  function targets(kind: 'all' | 'messages' = 'all'): HTMLElement[] {
49
- const list = [...new Set([
50
- ...(kind === 'all' ? inState('open') : []),
51
- ...api.unread().map(e => messageTarget(e.id)).filter((el): el is HTMLElement => !!el),
52
- ])]
53
+ const list = [
54
+ ...new Set([
55
+ ...(kind === 'all' ? inState('open') : []),
56
+ ...api
57
+ .unread()
58
+ .map(e => messageTarget(e.id))
59
+ .filter((el): el is HTMLElement => !!el),
60
+ ]),
61
+ ]
53
62
  const top = (el: HTMLElement) => el.getBoundingClientRect().top
54
63
  return list.sort((a, b) => top(a) - top(b))
55
64
  }
@@ -60,51 +69,127 @@ let unreadToast: Toast
60
69
  let toastTimer = 0
61
70
  let unreadShown = false
62
71
 
72
+ // A decided form folds into a card: what was chosen, what was written under it (a few lines), and
73
+ // "decided · change" (a click on the card opens the form again). Its chat folds too, into
74
+ // "💬 n messages", opened by a click; a message from the agent the reader hasn't read opens it, and
75
+ // it stays open once read, until the reader folds it, so a reply never hides in the fold. The card is the page's, put back in the
76
+ // form after each update.
77
+ const chatsOpen = new Set<string>()
78
+
79
+ function card(form: HTMLFormElement, name: string, last: WbEvent) {
80
+ let el = form.querySelector<HTMLElement>(':scope > .wb-folded')
81
+ if (!el) {
82
+ el = document.createElement('div')
83
+ el.className = 'wb-folded'
84
+ el.innerHTML =
85
+ '<div class="wb-folded-head"><span class="wb-folded-choice"></span><span class="wb-folded-change"></span></div><ul class="wb-folded-choices"></ul><div class="wb-folded-text"></div><button type="button" class="wb-folded-chat"></button>'
86
+ form.prepend(el)
87
+ }
88
+ const { action, chosen, written } = answerParts(last.data, last.action)
89
+ // One answer and no button: it is the title. Else the button it was sent with is the title (or
90
+ // "answered"), and each answer a line under it. Nothing chosen, only written: the text is the
91
+ // answer, said once.
92
+ const list = action || chosen.length > 1 ? chosen : []
93
+ const head = action ?? (chosen.length > 1 ? 'answered' : (chosen[0] ?? written.shift() ?? 'answered'))
94
+ el.querySelector('.wb-folded-choice')!.textContent = `✓ ${head}`
95
+ const choices = el.querySelector<HTMLElement>('.wb-folded-choices')!
96
+ choices.replaceChildren(...list.map(c => Object.assign(document.createElement('li'), { textContent: c })))
97
+ choices.hidden = !list.length
98
+ el.querySelector('.wb-folded-change')!.textContent = form.hasAttribute('data-wb-just-sent')
99
+ ? '✓ the agent has it'
100
+ : 'decided · change'
101
+ const text = el.querySelector<HTMLElement>('.wb-folded-text')!
102
+ text.textContent = written.join('\n\n')
103
+ text.hidden = !written.length
104
+ // the chat: how many messages, and open or folded
105
+ const messages = api.events().filter(e => e.type === 'chat:message' && e.data.chat === name).length
106
+ const thread = form.querySelector<HTMLElement>(':scope > [data-block=chat-thread]')
107
+ const unreadHere =
108
+ !!thread &&
109
+ api
110
+ .unread()
111
+ .some(
112
+ m =>
113
+ thread.querySelector(`[data-wb-message="${CSS.escape(m.id)}"]`) ||
114
+ (m.type === 'chat:message' && m.data.chat === name),
115
+ )
116
+ // a message to read opens it, and it stays open once read, until the reader folds it; and it
117
+ // doesn't fold from under a message being written: its box has the focus, or text not sent yet
118
+ const box = thread?.querySelector<HTMLTextAreaElement>('textarea')
119
+ const writing = !!box && (document.activeElement === box || box.value.trim() !== '')
120
+ if (unreadHere || writing) chatsOpen.add(name)
121
+ const open = chatsOpen.has(name)
122
+ form.toggleAttribute('data-wb-chat-open', open)
123
+ const chat = el.querySelector<HTMLButtonElement>('.wb-folded-chat')!
124
+ chat.hidden = !messages
125
+ chat.textContent = `💬 ${messages} message${messages === 1 ? '' : 's'} ${open ? '▾' : '▸'}`
126
+ chat.setAttribute('aria-expanded', String(open))
127
+ }
128
+
63
129
  function show(list: FormStatus[] = forms) {
64
130
  forms = list
65
131
  for (const { form, name, state, last } of forms) {
66
132
  if (last && reopened.has(name) && reopened.get(name) !== last.id && state === 'decided') reopened.delete(name)
67
- form.toggleAttribute('data-wb-folded', state === 'decided' && !reopened.has(name))
133
+ const folded = state === 'decided' && !reopened.has(name)
134
+ form.toggleAttribute('data-wb-folded', folded)
135
+ if (folded && last) card(form, name, last)
136
+ else {
137
+ form.querySelector(':scope > .wb-folded')?.remove()
138
+ form.removeAttribute('data-wb-chat-open')
139
+ }
68
140
  }
69
141
  const unread = api.unread().length
70
- const parts: [string, string][] = ORDER.map(state => [state, inState(state).length] as const).filter(([, count]) => count).map(([state, count]) => [state, LABEL[state](count)])
142
+ const parts: [string, string][] = ORDER.map(state => [state, inState(state).length] as const)
143
+ .filter(([, count]) => count)
144
+ .map(([state, count]) => [state, LABEL[state](count)])
71
145
  if (unread) parts.splice(inState('open').length ? 1 : 0, 0, ['messages', messagesLabel(unread)])
72
146
  summary.hidden = !parts.length
73
- summary.replaceChildren(...parts.flatMap(([state, text], i) => {
74
- const link = document.createElement('button')
75
- link.type = 'button'
76
- link.dataset.state = state
77
- link.textContent = text
78
- return i ? [document.createTextNode(' · '), link] : [link]
79
- }))
147
+ summary.replaceChildren(
148
+ ...parts.flatMap(([state, text], i) => {
149
+ const link = document.createElement('button')
150
+ link.type = 'button'
151
+ link.dataset.state = state
152
+ link.textContent = text
153
+ return i ? [document.createTextNode(' · '), link] : [link]
154
+ }),
155
+ )
80
156
  const n = inState('open').length + unread
81
157
  const questions = inState('open').length
82
- if (questions) { waitingToast.update(`→ unanswered (${questions})`); waitingToast.show() } else waitingToast.hide()
158
+ if (questions) {
159
+ waitingToast.update(`→ unanswered (${questions})`)
160
+ waitingToast.show()
161
+ } else waitingToast.hide()
83
162
  api.badge('waiting', n ? `!${n}` : null, -1)
84
163
  // the agent's messages: a toast while there are any. It comes a moment late: a message that
85
164
  // arrives in view (an open thread) is read before it would show.
86
165
  clearTimeout(toastTimer)
87
166
  if (!unread) unreadToast.hide()
88
167
  else if (unreadShown) unreadToast.update(`→ unread (${unread})`)
89
- else toastTimer = window.setTimeout(() => {
90
- const left = api.unread().length
91
- if (!left) return
92
- unreadToast.update(`→ unread (${left})`)
93
- unreadToast.show()
94
- unreadShown = true
95
- }, 1000)
168
+ else
169
+ toastTimer = window.setTimeout(() => {
170
+ const left = api.unread().length
171
+ if (!left) return
172
+ unreadToast.update(`→ unread (${left})`)
173
+ unreadToast.show()
174
+ unreadShown = true
175
+ }, 1000)
96
176
  if (!unread) unreadShown = false
97
177
  // arrived to answer: the first thing waiting, once the page knows (the log loads after the page)
98
178
  if (arriving && n) {
99
179
  arriving = false
100
180
  history.replaceState(history.state, '', location.pathname + location.search)
101
- requestAnimationFrame(() => { const first = targets()[0]; if (first) jumpTo(first) })
181
+ requestAnimationFrame(() => {
182
+ const first = targets()[0]
183
+ if (first) jumpTo(first)
184
+ })
102
185
  }
103
186
  }
104
187
 
105
188
  let arriving = location.hash === WAITING_HASH
106
189
  // a page whose questions were answered meanwhile has none left to go to
107
- setTimeout(() => { arriving = false }, 5000)
190
+ setTimeout(() => {
191
+ arriving = false
192
+ }, 5000)
108
193
 
109
194
  // What a jump went to last, and where the page was scrolled then: jumping again goes to the one
110
195
  // after it, round to the first. Once the reader scrolled themselves, the next one is the first
@@ -129,10 +214,16 @@ function jumpTo(target: HTMLElement) {
129
214
  // centred in the part of the window the page is seen in (above the terminal drawer), as far as
130
215
  // the page allows
131
216
  const r = target.getBoundingClientRect()
132
- scrolledTo = Math.max(0, Math.min(scrollY + r.top + r.height / 2 - viewBottom() / 2, document.documentElement.scrollHeight - innerHeight))
217
+ scrolledTo = Math.max(
218
+ 0,
219
+ Math.min(scrollY + r.top + r.height / 2 - viewBottom() / 2, document.documentElement.scrollHeight - innerHeight),
220
+ )
133
221
  scrollTo({ top: scrolledTo, behavior: 'smooth' })
134
222
  if (target instanceof HTMLFormElement) {
135
- if (target.hasAttribute('data-wb-open')) target.querySelector<HTMLElement>('input:not([type=hidden]), textarea, select, button')?.focus({ preventScroll: true })
223
+ if (target.hasAttribute('data-wb-open'))
224
+ target
225
+ .querySelector<HTMLElement>('input:not([type=hidden]), textarea, select, button')
226
+ ?.focus({ preventScroll: true })
136
227
  } else target.dispatchEvent(new CustomEvent('wb-reveal', { bubbles: true }))
137
228
  }
138
229
 
@@ -145,7 +236,8 @@ export function trackWaiting(root: HTMLElement, core: Api) {
145
236
  unreadToast.hide()
146
237
  main.before(summary)
147
238
  summary.addEventListener('click', e => {
148
- const state = (e.target as HTMLElement).closest<HTMLElement>('button[data-state]')?.dataset.state as FormState | 'messages' | undefined
239
+ const state = (e.target as HTMLElement).closest<HTMLElement>('button[data-state]')?.dataset.state as
240
+ FormState | 'messages' | undefined
149
241
  if (state) jump(state)
150
242
  })
151
243
  // a folded form opens again on a click, to change the answer; not on one in its thread, which
@@ -155,22 +247,33 @@ export function trackWaiting(root: HTMLElement, core: Api) {
155
247
  const form = target.closest<HTMLFormElement>('form[data-wb-folded]')
156
248
  if (!form || target.closest('[data-block=chat-thread]')) return
157
249
  e.preventDefault()
250
+ // its chat, folded or open: not the form
251
+ if (target.closest('.wb-folded-chat')) {
252
+ const name = form.getAttribute('name')!
253
+ if (chatsOpen.has(name) || form.hasAttribute('data-wb-chat-open')) {
254
+ chatsOpen.delete(name)
255
+ form.removeAttribute('data-wb-chat-open')
256
+ } else chatsOpen.add(name)
257
+ show(forms)
258
+ return
259
+ }
158
260
  const name = form.getAttribute('name')!
159
261
  const last = api.last(name)
160
262
  if (last) reopened.set(name, last.id)
161
263
  show(forms)
162
264
  form.querySelector<HTMLElement>('input:not([type=hidden]), textarea, select')?.focus({ preventScroll: true })
163
265
  })
164
- api.onForms(show)
266
+ api.forms.on(show)
165
267
  // a message read, or a new one
166
- api.onEvents(() => show())
268
+ api.events.on(() => show())
167
269
  show(api.forms())
168
270
  document.addEventListener('submit', e => {
169
271
  const form = e.target as HTMLFormElement
170
272
  const name = form.getAttribute('name')
171
273
  if (!name || !main.contains(form) || !forms.some(f => f.form === form)) return
172
274
  // who listens as it is sent: an agent that takes it ends its wait right after
173
- const takes = (fallback: boolean) => api.listeners().some(l => !!l.fallback === fallback && (l.type === name || l.type === null))
275
+ const takes = (fallback: boolean) =>
276
+ api.listeners().some(l => !!l.fallback === fallback && (l.type === name || l.type === null))
174
277
  if (takes(false)) return
175
278
  const fallback = takes(true)
176
279
  const sent = new MutationObserver(() => {
@@ -178,7 +281,11 @@ export function trackWaiting(root: HTMLElement, core: Api) {
178
281
  sent.disconnect()
179
282
  if (form.dataset.wbState !== 'sent') return
180
283
  if (fallback) api.toast('Sent. No agent waits on this page; the fallback agent picks it up.', { name: 'sent' })
181
- else api.toast('Sent, but no agent is listening right now. Your answer stays in the log until one checks this page.', { kind: 'warn', name: 'sent' })
284
+ else
285
+ api.toast(
286
+ 'Sent, but no agent is listening right now. Your answer stays in the log until one checks this page.',
287
+ { kind: 'warn', name: 'sent' },
288
+ )
182
289
  })
183
290
  sent.observe(form, { attributes: true, attributeFilter: ['data-wb-state'] })
184
291
  })
@@ -0,0 +1,40 @@
1
+ // A window (src/pages/window.ts: the iframe block, `:::window`, any block that draws one) filling the
2
+ // browser window, and back: its header's button, or Esc. Which one is kept across the page's
3
+ // updates, which can write the block anew: by its place among the page's windows.
4
+ import type { Api } from '../../define.ts'
5
+
6
+ const windows = () => [...document.querySelectorAll<HTMLElement>('#wb-main .wb-window')]
7
+ let maximized: number | null = null
8
+
9
+ function apply() {
10
+ const all = windows()
11
+ if (maximized != null && !all[maximized]) maximized = null
12
+ all.forEach((win, i) => {
13
+ const on = i === maximized
14
+ win.toggleAttribute('data-wb-maximized', on)
15
+ const button = win.querySelector<HTMLButtonElement>('[data-wb-maximize]')
16
+ if (button) {
17
+ button.title = on ? 'Back into the page (Esc)' : 'Fill the window'
18
+ button.setAttribute('aria-label', button.title)
19
+ button.setAttribute('aria-pressed', String(on))
20
+ }
21
+ })
22
+ document.documentElement.toggleAttribute('data-wb-window-maximized', maximized != null)
23
+ }
24
+
25
+ export function trackWindows(api: Api) {
26
+ document.addEventListener('click', e => {
27
+ const win = (e.target as Element).closest?.('[data-wb-maximize]')?.closest<HTMLElement>('.wb-window')
28
+ if (!win) return
29
+ const i = windows().indexOf(win)
30
+ maximized = maximized === i ? null : i
31
+ apply()
32
+ })
33
+ document.addEventListener('keydown', e => {
34
+ if (e.key === 'Escape' && maximized != null) {
35
+ maximized = null
36
+ apply()
37
+ }
38
+ })
39
+ api.onRendered(apply)
40
+ }
@@ -0,0 +1,19 @@
1
+ // The api's changing values (Reactive in define.ts): a function for what it is now, `.on` to hear
2
+ // each change. Whoever owns the value calls `notify()` after it changed.
3
+ import type { Reactive } from '../define.ts'
4
+
5
+ export function reactive<T>(get: () => T): Reactive<T> & { notify(): void } {
6
+ const listeners = new Set<(value: T) => void>()
7
+ const value = (() => get()) as Reactive<T> & { notify(): void }
8
+ value.on = fn => {
9
+ listeners.add(fn)
10
+ return () => {
11
+ listeners.delete(fn)
12
+ }
13
+ }
14
+ value.notify = () => {
15
+ const now = get()
16
+ for (const fn of [...listeners]) fn(now)
17
+ }
18
+ return value
19
+ }
@@ -20,7 +20,10 @@ export function markRead(ids: string[]) {
20
20
  const open = new Set(unread().map(e => e.id))
21
21
  const fresh = ids.filter(id => open.has(id))
22
22
  if (!fresh.length) return
23
- for (const id of fresh) { sending.add(id); queued.add(id) }
23
+ for (const id of fresh) {
24
+ sending.add(id)
25
+ queued.add(id)
26
+ }
24
27
  clearTimeout(timer)
25
28
  timer = window.setTimeout(() => {
26
29
  const batch = [...queued]
@@ -31,19 +34,23 @@ export function markRead(ids: string[]) {
31
34
 
32
35
  /** Once the log has the reads, they need no holding. */
33
36
  export function readsArrived(events: WbEvent[]) {
34
- for (const e of events) if (e.type === READ) for (const id of (e.data?.ids as unknown[] | undefined) ?? []) sending.delete(String(id))
37
+ for (const e of events)
38
+ if (e.type === READ) for (const id of (e.data?.ids as unknown[] | undefined) ?? []) sending.delete(String(id))
35
39
  }
36
40
 
37
41
  // what is in view now, read once the tab is visible too
38
42
  const inView = new Set<string>()
39
- const seen = new IntersectionObserver(entries => {
40
- for (const entry of entries) {
41
- const id = (entry.target as HTMLElement).dataset.wbMessage!
42
- if (entry.isIntersecting) inView.add(id)
43
- else inView.delete(id)
44
- }
45
- readVisible()
46
- }, { threshold: 0.6 })
43
+ const seen = new IntersectionObserver(
44
+ entries => {
45
+ for (const entry of entries) {
46
+ const id = (entry.target as HTMLElement).dataset.wbMessage!
47
+ if (entry.isIntersecting) inView.add(id)
48
+ else inView.delete(id)
49
+ }
50
+ readVisible()
51
+ },
52
+ { threshold: 0.6 },
53
+ )
47
54
 
48
55
  function readVisible() {
49
56
  if (document.visibilityState === 'visible' && inView.size) markRead([...inView])