@bigmistqke/workpad 0.18.1 → 0.20.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 (109) hide show
  1. package/README.md +18 -9
  2. package/bin/workpad.ts +412 -58
  3. package/dist/bin/workpad.js +336 -46
  4. package/dist/client/runtime.js +141 -18
  5. package/dist/plugins/badge/README.md +59 -10
  6. package/dist/plugins/badge/index.js +130 -8
  7. package/dist/plugins/badge/public/style.css +132 -12
  8. package/dist/plugins/chat/README.md +7 -3
  9. package/dist/plugins/chat/index.js +8 -1
  10. package/dist/plugins/chat/public/client.js +2 -2
  11. package/dist/plugins/chat/public/style.css +10 -0
  12. package/dist/plugins/claude/README.md +13 -5
  13. package/dist/plugins/claude/channel.js +47 -6
  14. package/dist/plugins/claude/doctor.js +43 -47
  15. package/dist/plugins/claude/index.js +24 -8
  16. package/dist/plugins/claude/session.js +46 -1
  17. package/dist/plugins/comments/README.md +2 -1
  18. package/dist/plugins/comments/index.js +2 -0
  19. package/dist/plugins/comments/public/client.js +6 -5
  20. package/dist/plugins/comments/public/style.css +9 -0
  21. package/dist/plugins/form/README.md +1 -1
  22. package/dist/plugins/index.js +3 -1
  23. package/dist/plugins/opencode/channel.js +43 -7
  24. package/dist/plugins/opencode/index.js +2 -0
  25. package/dist/plugins/progress/README.md +41 -0
  26. package/dist/plugins/progress/index.js +117 -0
  27. package/dist/plugins/progress/public/client.js +1 -0
  28. package/dist/plugins/progress/public/style.css +85 -0
  29. package/dist/plugins/progress/state.js +210 -0
  30. package/dist/solid/index.js +3 -1
  31. package/dist/src/agents.js +13 -4
  32. package/dist/src/anchors.js +25 -1
  33. package/dist/src/build.js +1 -1
  34. package/dist/src/client/page/base.css +48 -2
  35. package/dist/src/client/page/main.js +58 -30
  36. package/dist/src/document.js +6 -4
  37. package/dist/src/events.js +22 -11
  38. package/dist/src/help.js +59 -12
  39. package/dist/src/hub.js +69 -3
  40. package/dist/src/listen.js +36 -4
  41. package/dist/src/listeners.js +14 -4
  42. package/dist/src/loader.js +7 -2
  43. package/dist/src/migrate.js +1 -1
  44. package/dist/src/pages/board.js +9 -4
  45. package/dist/src/pages/index.js +2 -4
  46. package/dist/src/pages/markdown.js +50 -19
  47. package/dist/src/pages/render.js +17 -1
  48. package/dist/src/server.js +94 -18
  49. package/dist/src/set.js +99 -0
  50. package/dist/src/state.js +2 -1
  51. package/dist/src/status.js +57 -9
  52. package/dist/src/turns.js +41 -0
  53. package/package.json +11 -1
  54. package/plugins/README.md +3 -1
  55. package/plugins/badge/README.md +59 -10
  56. package/plugins/badge/index.ts +141 -8
  57. package/plugins/badge/public/style.css +132 -12
  58. package/plugins/chat/README.md +7 -3
  59. package/plugins/chat/client.tsx +20 -3
  60. package/plugins/chat/index.ts +10 -1
  61. package/plugins/chat/public/style.css +10 -0
  62. package/plugins/claude/README.md +13 -5
  63. package/plugins/claude/channel.ts +45 -7
  64. package/plugins/claude/doctor.ts +58 -39
  65. package/plugins/claude/index.ts +30 -8
  66. package/plugins/claude/session.ts +32 -0
  67. package/plugins/comments/README.md +2 -1
  68. package/plugins/comments/client.ts +19 -0
  69. package/plugins/comments/index.ts +7 -1
  70. package/plugins/comments/public/style.css +9 -0
  71. package/plugins/form/README.md +1 -1
  72. package/plugins/index.ts +2 -0
  73. package/plugins/opencode/channel.ts +44 -8
  74. package/plugins/opencode/index.ts +4 -0
  75. package/plugins/progress/README.md +41 -0
  76. package/plugins/progress/client.ts +172 -0
  77. package/plugins/progress/index.ts +166 -0
  78. package/plugins/progress/public/style.css +85 -0
  79. package/plugins/progress/state.ts +278 -0
  80. package/skills/workpad/SKILL.md +149 -18
  81. package/solid/index.tsx +6 -2
  82. package/src/agents.ts +24 -2
  83. package/src/anchors.ts +32 -0
  84. package/src/build.ts +1 -1
  85. package/src/client/delivery.ts +49 -0
  86. package/src/client/forms.ts +18 -11
  87. package/src/client/page/base.css +48 -2
  88. package/src/client/page/waiting.ts +45 -25
  89. package/src/client/read.ts +9 -5
  90. package/src/client/runtime.ts +7 -3
  91. package/src/client/toast.ts +27 -3
  92. package/src/define.ts +120 -9
  93. package/src/document.ts +19 -5
  94. package/src/events.ts +47 -6
  95. package/src/help.ts +58 -11
  96. package/src/hub.ts +80 -2
  97. package/src/listen.ts +47 -4
  98. package/src/listeners.ts +22 -1
  99. package/src/loader.ts +14 -4
  100. package/src/migrate.ts +1 -1
  101. package/src/pages/board.ts +12 -3
  102. package/src/pages/index.ts +8 -10
  103. package/src/pages/markdown.ts +59 -12
  104. package/src/pages/render.ts +20 -2
  105. package/src/server.ts +127 -19
  106. package/src/set.ts +118 -0
  107. package/src/state.ts +3 -0
  108. package/src/status.ts +115 -14
  109. package/src/turns.ts +56 -0
package/src/anchors.ts CHANGED
@@ -83,3 +83,35 @@ export function anchorsOf(html: string): Anchor[] {
83
83
 
84
84
  /** A hash as a link writes it: escaped where it must be, the colon and slash of an id left readable. */
85
85
  export const hashOf = (id: string): string => '#' + encodeURIComponent(id).replace(/%3A/gi, ':').replace(/%2F/gi, '/')
86
+
87
+ /** How many single-character edits turn `a` into `b`. */
88
+ function distance(a: string, b: string): number {
89
+ let row = Array.from({ length: b.length + 1 }, (_, j) => j)
90
+ for (let i = 1; i <= a.length; i++) {
91
+ const next = [i]
92
+ for (let j = 1; j <= b.length; j++)
93
+ next[j] = Math.min(row[j] + 1, next[j - 1] + 1, row[j - 1] + (a[i - 1] === b[j - 1] ? 0 : 1))
94
+ row = next
95
+ }
96
+ return row[b.length]
97
+ }
98
+
99
+ /**
100
+ * Of a page's anchors, the `n` whose ids are nearest `id` by name, nearest first: what a link to a
101
+ * missing anchor most likely meant. One of its own kind (`form:` for `form:…`) comes before one of
102
+ * another at the same distance.
103
+ */
104
+ export function closest(id: string, ids: string[], n = 3): string[] {
105
+ const kind = id.includes(':') ? id.slice(0, id.indexOf(':') + 1) : ''
106
+ return ids
107
+ .map(other => ({ other, d: distance(id, other) - (kind && other.startsWith(kind) ? 0.5 : 0) }))
108
+ .sort((a, b) => a.d - b.d)
109
+ .slice(0, n)
110
+ .map(c => c.other)
111
+ }
112
+
113
+ /** For a message about a missing anchor: `; nearest: #a, #b` of those a page has, or how to list them. */
114
+ export const nearest = (id: string, ids: string[]): string =>
115
+ ids.length
116
+ ? `; nearest: ${closest(id, ids).map(hashOf).join(', ')} (\`workpad anchors\` lists all)`
117
+ : '; it has no anchors'
package/src/build.ts CHANGED
@@ -192,7 +192,7 @@ export async function build(
192
192
  // asked for this page (what is only for the project's own browser has nothing to add here)
193
193
  const parts = await pluginParts(hooks, null, {
194
194
  asset: assetHere,
195
- messages: [],
195
+ messages: {},
196
196
  leaveOut: skip,
197
197
  failed: message => warnings.push(`${path.relative(project.dir, file)}: ${message}`),
198
198
  })
@@ -0,0 +1,49 @@
1
+ // Where each of the reader's events for an agent went (Api.delivery): a form's answer, a chat
2
+ // message, a comment. One rule for all of them (deliveryOf in status.ts), from the log's marks and
3
+ // who listens on the page, kept up to date as those change and as an answer waits long enough to be
4
+ // late. What the page draws of it (a line beside what was sent, a count in the corner) is the page's
5
+ // and the plugins'.
6
+ import type { Delivery, Listener, Reactive, WpEvent } from '../define'
7
+ import { NOT_RECEIVED_AFTER, deliveryOf, fromReader, isQuestionName } from '../status.ts'
8
+ import { reactive } from './reactive'
9
+
10
+ const same = (a: ReadonlyMap<string, Delivery>, b: ReadonlyMap<string, Delivery>) =>
11
+ a.size === b.size && [...a].every(([id, d]) => b.get(id)?.text === d.text)
12
+
13
+ /**
14
+ * The delivery of each of the reader's events for an agent among `events`: a form's answer (a type
15
+ * without a colon) and the plugins' types in `handedOut`.
16
+ */
17
+ export function trackDelivery(
18
+ events: Reactive<WpEvent[]>,
19
+ listeners: Reactive<Listener[]>,
20
+ handedOut: ReadonlySet<string>,
21
+ ): Reactive<ReadonlyMap<string, Delivery>> {
22
+ const forAgent = (e: WpEvent) => fromReader(e) && (isQuestionName(e.type) || handedOut.has(e.type))
23
+ let current: ReadonlyMap<string, Delivery> = new Map()
24
+ let timer: ReturnType<typeof setTimeout> | undefined
25
+ const value = reactive(() => current)
26
+ const update = () => {
27
+ const now = Date.now()
28
+ const listening = listeners()
29
+ const next = new Map<string, Delivery>()
30
+ // when the next answer waiting turns late, to look again then
31
+ let soonest = Infinity
32
+ for (const e of events()) {
33
+ if (!forAgent(e)) continue
34
+ const d = deliveryOf(e, listening, now)
35
+ next.set(e.id, d)
36
+ if (d.state === 'waiting' || d.state === 'unfollowed')
37
+ soonest = Math.min(soonest, Date.parse(e.ts) + NOT_RECEIVED_AFTER - now)
38
+ }
39
+ clearTimeout(timer)
40
+ if (soonest < Infinity) timer = setTimeout(update, Math.max(0, soonest) + 50)
41
+ if (same(current, next)) return
42
+ current = next
43
+ value.notify()
44
+ }
45
+ events.on(update)
46
+ listeners.on(update)
47
+ update()
48
+ return value
49
+ }
@@ -254,33 +254,36 @@ async function onSubmit(e: SubmitEvent) {
254
254
 
255
255
  // What a sent answer came to, said beside the form's last send button, where the eye is when it is
256
256
  // pressed (a line over the form was out of view below a long list of options): `.wp-form-status`,
257
- // "waiting for an agent to receive it", or `data-kind="bad"` "couldn't send it". Never what the
258
- // button says already (sendState): nothing while sending, no "sent".
257
+ // where its answer went (api.delivery: waiting for an agent to receive it, no one follows the page,
258
+ // not received after 10 min, received by whom), with the delivery's state in `data-wp-delivery`; or
259
+ // `data-kind="bad"` "couldn't send it". Never what the button says already (sendState): nothing while
260
+ // sending, no "sent". A late answer's line is in the warning colour (`data-kind="warn"`).
259
261
  const sendButtons = (form: HTMLFormElement) =>
260
262
  [...form.querySelectorAll<HTMLButtonElement | HTMLInputElement>('button, input[type=submit]')].filter(
261
263
  b => (b instanceof HTMLInputElement || (b.type !== 'button' && b.type !== 'reset')) && b.form === form,
262
264
  )
263
265
 
264
266
  function showStatus(form: HTMLFormElement) {
267
+ const last = api.last(form.getAttribute('name')!)
268
+ const delivery = last ? api.delivery().get(last.id) : undefined
269
+ const answered = form.hasAttribute('data-wp-pending') || form.hasAttribute('data-wp-received')
265
270
  const text =
266
271
  form.dataset.wpState === 'failed'
267
272
  ? (["couldn't send it", 'bad'] as const)
268
- : form.dataset.wpState === 'sending'
273
+ : form.dataset.wpState === 'sending' || !answered || !delivery || delivery.state === 'sending'
269
274
  ? null
270
- : form.hasAttribute('data-wp-pending')
271
- ? (['waiting for an agent to receive it', null] as const)
272
- : form.hasAttribute('data-wp-received')
273
- ? (['the agent has it', null] as const)
274
- : null
275
+ : ([delivery.text, delivery.state === 'late' ? 'warn' : null] as const)
275
276
  let el = form.querySelector<HTMLElement>('.wp-form-status')
276
- const last = sendButtons(form).at(-1)
277
- if (!text || !last) return void el?.remove()
277
+ const button = sendButtons(form).at(-1)
278
+ if (!text || !button) return void el?.remove()
278
279
  if (!el) el = Object.assign(document.createElement('span'), { className: 'wp-form-status' })
279
280
  el.setAttribute('role', 'status')
280
- if (el.previousElementSibling !== last) last.after(el)
281
+ if (el.previousElementSibling !== button) button.after(el)
281
282
  el.textContent = text[0]
282
283
  if (text[1]) el.dataset.kind = text[1]
283
284
  else delete el.dataset.kind
285
+ if (text[1] === 'bad') delete el.dataset.wpDelivery
286
+ else el.dataset.wpDelivery = delivery!.state
284
287
  }
285
288
 
286
289
  const ATTR: Record<FormState, string> = {
@@ -388,6 +391,10 @@ export function trackForms(el: HTMLElement, pageId: string, core: Api) {
388
391
  document.addEventListener('submit', onSubmit, { capture: true })
389
392
  api.onRendered(refresh)
390
393
  api.listeners.on(mark)
394
+ // an answer waiting turned late, or who received it is known
395
+ api.delivery.on(() => {
396
+ for (const form of forms()) showStatus(form)
397
+ })
391
398
  api.events.on(() => {
392
399
  restoreLast(false)
393
400
  mark()
@@ -949,8 +949,8 @@ form button[data-wp-send='failed'] {
949
949
  form[data-wp-just-sent] {
950
950
  animation: wp-sent 1.4s ease-out;
951
951
  }
952
- /* what a sent answer came to, beside the form's last send button (forms.ts): waiting for the agent
953
- (muted), couldn't send (red) */
952
+ /* what a sent answer came to, beside the form's last send button (forms.ts): where it went (muted),
953
+ not received for long (the warning colour), couldn't send (red) */
954
954
  .wp-form-status {
955
955
  margin-left: var(--space-md);
956
956
  color: var(--muted);
@@ -960,6 +960,10 @@ form[data-wp-just-sent] {
960
960
  color: var(--bad);
961
961
  font-weight: var(--weight-strong);
962
962
  }
963
+ .wp-form-status[data-kind='warn'] {
964
+ color: var(--warn);
965
+ font-weight: var(--weight-strong);
966
+ }
963
967
 
964
968
  /* a question the reader hasn't answered, or one the agent asks again: ringed, without a label of
965
969
  its own (one laid into the form landed on its content in some layouts; the corner counts them).
@@ -1329,6 +1333,48 @@ input.wp-board-search::placeholder {
1329
1333
  font: var(--ui-small)/var(--ui-line-height) var(--sans);
1330
1334
  color: color-mix(in srgb, var(--text) 72%, var(--muted));
1331
1335
  }
1336
+ /* a running progress on the page (plugins/progress): a small bar and its line */
1337
+ .wp-board-card > .wp-board-progress {
1338
+ flex: 1 1 100%;
1339
+ display: flex;
1340
+ align-items: center;
1341
+ gap: var(--space-sm);
1342
+ min-width: 0;
1343
+ font: var(--ui-small)/var(--ui-line-height) var(--sans);
1344
+ }
1345
+ .wp-board-progress[data-stale] {
1346
+ opacity: 0.55;
1347
+ }
1348
+ .wp-board-progress-text {
1349
+ min-width: 0;
1350
+ overflow: hidden;
1351
+ text-overflow: ellipsis;
1352
+ white-space: nowrap;
1353
+ }
1354
+ .wp-board-progress-bar {
1355
+ position: relative;
1356
+ flex: none;
1357
+ width: 48px;
1358
+ height: 4px;
1359
+ border-radius: 2px;
1360
+ overflow: hidden;
1361
+ background: color-mix(in srgb, var(--muted) 28%, transparent);
1362
+ }
1363
+ .wp-board-progress-bar > span {
1364
+ position: absolute;
1365
+ inset: 0 auto 0 0;
1366
+ background: var(--accent);
1367
+ }
1368
+ .wp-board-progress[data-stale] .wp-board-progress-bar > span {
1369
+ background: var(--muted);
1370
+ }
1371
+ .wp-board-progress-dot {
1372
+ flex: none;
1373
+ width: 7px;
1374
+ height: 7px;
1375
+ border-radius: 50%;
1376
+ background: var(--accent);
1377
+ }
1332
1378
  .wp-board-card > .wp-board-when {
1333
1379
  color: color-mix(in srgb, var(--muted) 72%, var(--surface));
1334
1380
  margin-top: calc(var(--space-xs) * -1);
@@ -6,9 +6,12 @@
6
6
  // until another message comes. A message is found on the page by its
7
7
  // element (`data-wp-message`), or, when it isn't shown, by the element that holds it
8
8
  // (`data-wp-holds`: a comment's badge), which is sent `wp-reveal` to show it. Arriving by a link that
9
- // ends in `#wp-waiting` (the header's menu, the overview), the page goes to the first. An answer no
10
- // agent is listening for says so once it is sent, in a toast: the form's own mark may be out of view.
9
+ // ends in `#wp-waiting` (the header's menu, the overview), the page goes to the first. Where an
10
+ // answer went is said beside it (api.delivery: forms.ts, the chat, the comments); one no agent
11
+ // received for long (late) is counted in a third toast, "→ not received (n)", in the warning colour,
12
+ // which goes to each: an element marked `data-wp-delivery="late"`.
11
13
  import type { Api, FormState, FormStatus, Toast, WpEvent } from '../../define.ts'
14
+ import { isQuestionName } from '../../status.ts'
12
15
  import { answerParts } from '../forms'
13
16
  import { viewBottom } from './view'
14
17
  import { WAITING_HASH } from './elsewhere'
@@ -49,9 +52,11 @@ function targets(kind: 'all' | 'messages' = 'all'): HTMLElement[] {
49
52
  return list.sort((a, b) => top(a) - top(b))
50
53
  }
51
54
 
52
- // the corner's toasts: the questions waiting, and the agent's messages unread; each goes to the next
55
+ // the corner's toasts: the questions waiting, the agent's messages unread, and the reader's answers
56
+ // no agent received for long; each goes to the next
53
57
  let waitingToast: Toast
54
58
  let unreadToast: Toast
59
+ let lateToast: Toast
55
60
  let toastTimer = 0
56
61
  let unreadShown = false
57
62
  // closed by its ✕: the messages unread then, by id. The toast stays away until one more comes; they
@@ -85,7 +90,9 @@ function card(form: HTMLFormElement, name: string, last: WpEvent, state: FormSta
85
90
  const choices = el.querySelector<HTMLElement>('.wp-folded-choices')!
86
91
  choices.replaceChildren(...list.map(c => Object.assign(document.createElement('li'), { textContent: c })))
87
92
  choices.hidden = !list.length
88
- el.querySelector('.wp-folded-change')!.textContent = state === 'received' ? 'received · change' : 'decided · change'
93
+ // who has it, when it said ("received by …")
94
+ const received = api.delivery().get(last.id)?.text ?? 'received'
95
+ el.querySelector('.wp-folded-change')!.textContent = `${state === 'received' ? received : 'decided'} · change`
89
96
  const text = el.querySelector<HTMLElement>('.wp-folded-text')!
90
97
  text.textContent = written.join('\n\n')
91
98
  text.hidden = !written.length
@@ -135,6 +142,11 @@ function show(list: FormStatus[] = forms) {
135
142
  waitingToast.show()
136
143
  } else waitingToast.hide()
137
144
  api.badge('waiting', n ? `!${n}` : null, -1)
145
+ const late = lateAnswers().length
146
+ if (late) {
147
+ lateToast.update(`→ not received (${late})`)
148
+ lateToast.show()
149
+ } else lateToast.hide()
138
150
  // the agent's messages: a toast while there are any. It comes a moment late: a message that
139
151
  // arrives in view (an open thread) is read before it would show.
140
152
  clearTimeout(toastTimer)
@@ -163,6 +175,29 @@ function show(list: FormStatus[] = forms) {
163
175
  }
164
176
  }
165
177
 
178
+ /**
179
+ * The reader's answers no agent received for long (late): each of a form only its latest (an older
180
+ * answer was overtaken by it), every chat message and comment.
181
+ */
182
+ function lateAnswers(): WpEvent[] {
183
+ const delivery = api.delivery()
184
+ return api
185
+ .events()
186
+ .filter(e => delivery.get(e.id)?.state === 'late' && (!isQuestionName(e.type) || api.last(e.type)?.id === e.id))
187
+ }
188
+
189
+ /** Where the late answers are on the page, top to bottom: a form's line goes to its form. */
190
+ function lateTargets(): HTMLElement[] {
191
+ const top = (el: HTMLElement) => el.getBoundingClientRect().top
192
+ return [
193
+ ...new Set(
194
+ [...document.querySelectorAll<HTMLElement>('[data-wp-delivery=late]')]
195
+ .filter(el => el.getClientRects().length)
196
+ .map(el => el.closest<HTMLElement>('form') ?? el),
197
+ ),
198
+ ].sort((a, b) => top(a) - top(b))
199
+ }
200
+
166
201
  let arriving = location.hash === WAITING_HASH
167
202
  // a page whose questions were answered meanwhile has none left to go to
168
203
  setTimeout(() => {
@@ -181,8 +216,8 @@ function next(list: HTMLElement[]) {
181
216
  return list.find(f => f.getBoundingClientRect().top > viewBottom() / 2) ?? list[0]
182
217
  }
183
218
 
184
- function jump(what: 'open' | 'messages') {
185
- const list = what === 'messages' ? targets('messages') : inState('open')
219
+ function jump(what: 'open' | 'messages' | 'late') {
220
+ const list = what === 'messages' ? targets('messages') : what === 'late' ? lateTargets() : inState('open')
186
221
  const target = next(list)
187
222
  if (target) jumpTo(target)
188
223
  }
@@ -219,8 +254,10 @@ export function trackWaiting(root: HTMLElement, core: Api) {
219
254
  unreadShown = false
220
255
  },
221
256
  })
257
+ lateToast = api.toast('', { ms: 0, kind: 'warn', name: 'not-received', order: -1, onClick: () => jump('late') })
222
258
  waitingToast.hide()
223
259
  unreadToast.hide()
260
+ lateToast.hide()
224
261
  // a folded form opens again on a click, to change the answer; not on one in its thread, which
225
262
  // stays open under it
226
263
  main.addEventListener('click', e => {
@@ -255,24 +292,7 @@ export function trackWaiting(root: HTMLElement, core: Api) {
255
292
  api.forms.on(show)
256
293
  // a message read, or a new one
257
294
  api.events.on(() => show())
295
+ // an answer turned late, or who received it is known
296
+ api.delivery.on(() => show())
258
297
  show(api.forms())
259
- document.addEventListener('submit', e => {
260
- const form = e.target as HTMLFormElement
261
- const name = form.getAttribute('name')
262
- if (!name || !main.contains(form) || !forms.some(f => f.form === form)) return
263
- // who listens as it is sent: one that takes it ends its listen right after
264
- if (api.listeners().some(l => l.type === name || l.type === null)) return
265
- const sent = new MutationObserver(() => {
266
- if (form.dataset.wpState === 'sending') return
267
- sent.disconnect()
268
- if (form.dataset.wpState !== 'sent') return
269
- api.toast('Sent · nobody listening', {
270
- kind: 'warn',
271
- name: 'sent',
272
- details: 'Your answer stays in the log until someone listens on this page.',
273
- })
274
- })
275
- sent.observe(form, { attributes: true, attributeFilter: ['data-wp-state'] })
276
- // going down, as the core's own (forms.ts): a form in a form doesn't bubble up to here
277
- }, { capture: true })
278
298
  }
@@ -7,14 +7,15 @@
7
7
  import type { WpEvent } from '../define.ts'
8
8
  import { READ, unreadMessages } from '../status.ts'
9
9
 
10
- let isMessage: (type: string) => boolean = () => false
10
+ // the event types for the reader, each with the `data` fields it requires
11
+ let messages: ReadonlyMap<string, readonly string[]> = new Map()
11
12
  let getEvents: () => WpEvent[] = () => []
12
13
  let send: (ids: string[]) => void = () => {}
13
14
  let chats: () => ReadonlySet<string> | null = () => null
14
15
  // read here but not in the log yet: they don't count as unread meanwhile
15
16
  const sending = new Set<string>()
16
17
 
17
- export const unread = (): WpEvent[] => unreadMessages(getEvents(), isMessage, chats()).filter(e => !sending.has(e.id))
18
+ export const unread = (): WpEvent[] => unreadMessages(getEvents(), messages, chats()).filter(e => !sending.has(e.id))
18
19
 
19
20
  /**
20
21
  * The chats the page's content shows, by name: the one under every form and each chat block; null
@@ -96,9 +97,12 @@ function observeMessages() {
96
97
  }
97
98
  }
98
99
 
99
- export function trackReading(options: { messages: string[]; events: () => WpEvent[]; send: (ids: string[]) => void }) {
100
- const types = new Set(options.messages)
101
- isMessage = type => types.has(type)
100
+ export function trackReading(options: {
101
+ messages: Record<string, string[]>
102
+ events: () => WpEvent[]
103
+ send: (ids: string[]) => void
104
+ }) {
105
+ messages = new Map(Object.entries(options.messages))
102
106
  getEvents = options.events
103
107
  send = options.send
104
108
  chats = chatsShown
@@ -4,6 +4,7 @@
4
4
  import type { Api, PluginMount, Listener, PageHandle, RenderedPage, SlotName, StoredPage, WpEvent } from '../define'
5
5
  import { html } from '../html.ts'
6
6
  import { formsApi, trackForms } from './forms'
7
+ import { trackDelivery } from './delivery'
7
8
  import { createToast } from './toast'
8
9
  import { markRead, messagesChanged, readsArrived, trackReading, unread as unreadNow } from './read'
9
10
  import { READ, SCROLL, fromReader } from '../status.ts'
@@ -20,8 +21,10 @@ interface Handoff {
20
21
  client: string | null
21
22
  /** the plugins' browser modules */
22
23
  plugins: { name: string; client: string }[]
23
- /** the event types that, from the agent, are messages for the reader */
24
- messages: string[]
24
+ /** the event types that, from the agent, are messages for the reader, each with the `data` fields it requires */
25
+ messages: Record<string, string[]>
26
+ /** the plugins' event types that, from the reader, go to an agent */
27
+ handedOut?: string[]
25
28
  /** the page's frontmatter (PageHandle.frontmatter) */
26
29
  frontmatter: Record<string, unknown>
27
30
  /** false on a built page (`workpad build`): a file, no server behind it */
@@ -259,6 +262,7 @@ const api: Api = {
259
262
  },
260
263
  toast: createToast(cornerOfPage),
261
264
  ...formsApi,
265
+ delivery: trackDelivery(eventsValue, listenersValue, new Set(handoff.handedOut ?? [])),
262
266
  }
263
267
 
264
268
  // ---- the page ----------------------------------------------------------------------------------
@@ -445,7 +449,7 @@ if (handoff.client) {
445
449
  if (live) {
446
450
  trackForms(root, page, api)
447
451
  trackReading({
448
- messages: handoff.messages ?? [],
452
+ messages: handoff.messages ?? {},
449
453
  events: () => events,
450
454
  send: ids => {
451
455
  append(READ, { ids }).catch(err => console.error('marking read:', err))
@@ -6,7 +6,7 @@
6
6
  // adds a ✕ that runs it and closes the toast. A toast stays short: what more there is to say goes in
7
7
  // `details`, which a ▸ unfolds under it; unfolded, it doesn't go by itself until folded again. Their look is the core's (server.ts), in the page's
8
8
  // theme colours.
9
- import type { Toast, ToastOptions } from '../define.ts'
9
+ import type { Toast, ToastBar, ToastOptions } from '../define.ts'
10
10
 
11
11
  let box: HTMLElement | null = null
12
12
  function place(corner: HTMLElement) {
@@ -22,7 +22,7 @@ function place(corner: HTMLElement) {
22
22
  export function createToast(corner: () => HTMLElement) {
23
23
  return function toast(
24
24
  text: string,
25
- { kind = 'info', ms = 7000, onClick, onDismiss, name, order = 0, details }: ToastOptions = {},
25
+ { kind = 'info', ms = 7000, onClick, onDismiss, name, order = 0, details, meta, bar }: ToastOptions = {},
26
26
  ): Toast {
27
27
  const el = document.createElement('button')
28
28
  let timer: ReturnType<typeof setTimeout> | undefined
@@ -39,6 +39,28 @@ export function createToast(corner: () => HTMLElement) {
39
39
  label.className = 'wp-toast-text'
40
40
  label.textContent = text
41
41
  el.append(label)
42
+ // a quiet toast's time at the right, and its bar under the line (a progress)
43
+ let metaEl: HTMLElement | null = null
44
+ let barEl: HTMLElement | null = null
45
+ const setMeta = (text: string | null | undefined) => {
46
+ if (!text) return void metaEl?.remove()
47
+ metaEl ??= Object.assign(document.createElement('span'), { className: 'wp-toast-meta' })
48
+ metaEl.textContent = text
49
+ if (!metaEl.isConnected) label.after(metaEl)
50
+ }
51
+ const setBar = (to: ToastBar | null | undefined) => {
52
+ if (!to) return void barEl?.remove()
53
+ if (!barEl) {
54
+ barEl = Object.assign(document.createElement('span'), { className: 'wp-toast-bar' })
55
+ barEl.append(document.createElement('span'))
56
+ }
57
+ barEl.dataset.tone = to.tone ?? 'accent'
58
+ barEl.toggleAttribute('data-indeterminate', to.value == null)
59
+ barEl.style.setProperty('--wp-toast-bar', to.value == null ? '' : `${Math.max(0, Math.min(1, to.value)) * 100}%`)
60
+ if (!barEl.isConnected) el.append(barEl)
61
+ }
62
+ setMeta(meta)
63
+ setBar(bar)
42
64
  let more: HTMLElement | null = null
43
65
  let detail: HTMLElement | null = null
44
66
  if (details) {
@@ -84,8 +106,10 @@ export function createToast(corner: () => HTMLElement) {
84
106
  place(corner()).append(el)
85
107
  return {
86
108
  close,
87
- update: next => {
109
+ update: (next, more) => {
88
110
  label.textContent = next
111
+ if (more && 'meta' in more) setMeta(more.meta)
112
+ if (more && 'bar' in more) setBar(more.bar)
89
113
  },
90
114
  hide: () => {
91
115
  el.hidden = true