deictic 0.6.0 → 0.8.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 (111) hide show
  1. package/API.md +156 -31
  2. package/README.md +6 -4
  3. package/dist/annotations/client.d.ts +1 -1
  4. package/dist/annotations/client.d.ts.map +1 -1
  5. package/dist/annotations/client.js +161 -176
  6. package/dist/annotations/client.js.map +1 -1
  7. package/dist/annotations/index.d.ts +2 -11
  8. package/dist/bun.js +1 -1
  9. package/dist/client/index.d.ts +7 -6
  10. package/dist/client/index.d.ts.map +1 -1
  11. package/dist/client/index.js +1077 -236
  12. package/dist/client/index.js.map +1 -1
  13. package/dist/index.d.ts +2 -2
  14. package/dist/next.js +1 -1
  15. package/dist/recommended/index.d.ts +2 -2
  16. package/dist/recordings/client.d.ts.map +1 -1
  17. package/dist/recordings/client.js +89 -154
  18. package/dist/recordings/client.js.map +1 -1
  19. package/dist/screenshots/client.d.ts.map +1 -1
  20. package/dist/shared/annotations.js +3 -4
  21. package/dist/shared/annotations.js.map +1 -1
  22. package/dist/shared/board.js +1 -1
  23. package/dist/shared/client.js +45 -2
  24. package/dist/shared/client.js.map +1 -1
  25. package/dist/shared/component.js +62 -0
  26. package/dist/shared/component.js.map +1 -0
  27. package/dist/shared/dom.js +79 -0
  28. package/dist/shared/dom.js.map +1 -0
  29. package/dist/shared/drawer.js +217 -0
  30. package/dist/shared/drawer.js.map +1 -0
  31. package/dist/shared/endpoints.js +26 -6
  32. package/dist/shared/endpoints.js.map +1 -1
  33. package/dist/shared/index.d.ts +150 -9
  34. package/dist/shared/index.d.ts.map +1 -1
  35. package/dist/shared/index2.d.ts +9 -19
  36. package/dist/shared/index2.d.ts.map +1 -1
  37. package/dist/shared/index7.d.ts +123 -26
  38. package/dist/shared/index7.d.ts.map +1 -1
  39. package/dist/shared/index9.d.ts +1 -1
  40. package/dist/shared/integration.d.ts +1 -1
  41. package/dist/shared/integration.js +59 -5
  42. package/dist/shared/integration.js.map +1 -1
  43. package/dist/shared/package.js +23 -0
  44. package/dist/shared/package.js.map +1 -0
  45. package/dist/shared/packages.js +14 -23
  46. package/dist/shared/packages.js.map +1 -1
  47. package/dist/shared/playwright.js +11 -3
  48. package/dist/shared/playwright.js.map +1 -1
  49. package/dist/shared/recording.js +31 -3
  50. package/dist/shared/recording.js.map +1 -1
  51. package/dist/shared/src.js.map +1 -1
  52. package/dist/shared/task.js +55 -31
  53. package/dist/shared/task.js.map +1 -1
  54. package/dist/shared/tasks.js +279 -61
  55. package/dist/shared/tasks.js.map +1 -1
  56. package/dist/shared/vb.js +1 -1
  57. package/dist/shared/voice2.js +6 -4
  58. package/dist/shared/voice2.js.map +1 -1
  59. package/dist/shared/watch.js +15 -14
  60. package/dist/shared/watch.js.map +1 -1
  61. package/dist/tailwind/index.js +7 -2
  62. package/dist/tailwind/index.js.map +1 -1
  63. package/dist/tasks/client.d.ts +2 -2
  64. package/dist/tasks/client.d.ts.map +1 -1
  65. package/dist/tasks/client.js +1870 -741
  66. package/dist/tasks/client.js.map +1 -1
  67. package/dist/tasks/index.d.ts +2 -2
  68. package/dist/voice/client.d.ts.map +1 -1
  69. package/dist/voice/client.js +154 -57
  70. package/dist/voice/client.js.map +1 -1
  71. package/dist/webpack.js +1 -1
  72. package/package.json +2 -1
  73. package/skills/deictic/SKILL.md +22 -28
  74. package/src/cli/recording.ts +34 -3
  75. package/src/cli/task.ts +78 -33
  76. package/src/cli/voice.ts +9 -4
  77. package/src/cli/watch.ts +16 -13
  78. package/src/client/away.ts +3 -1
  79. package/src/client/component.ts +98 -0
  80. package/src/client/dom.ts +70 -0
  81. package/src/client/drawer.ts +258 -0
  82. package/src/client/index.ts +290 -165
  83. package/src/client/lightbox.ts +8 -8
  84. package/src/client/lighting.ts +279 -0
  85. package/src/client/style.ts +263 -44
  86. package/src/client/tools.ts +279 -0
  87. package/src/client/tooltip.ts +25 -5
  88. package/src/client/tray.ts +48 -0
  89. package/src/index.ts +148 -8
  90. package/src/plugins/annotations/client.ts +103 -129
  91. package/src/plugins/annotations/draw.ts +68 -72
  92. package/src/plugins/annotations/index.ts +12 -19
  93. package/src/plugins/recordings/client.ts +96 -168
  94. package/src/plugins/screenshots/client.ts +53 -2
  95. package/src/plugins/screenshots/playwright.ts +8 -3
  96. package/src/plugins/tailwind/index.ts +3 -2
  97. package/src/plugins/tasks/client.ts +229 -748
  98. package/src/plugins/tasks/composer.ts +797 -0
  99. package/src/plugins/tasks/drawer.ts +516 -0
  100. package/src/plugins/tasks/index.ts +436 -80
  101. package/src/plugins/tasks/pins.ts +280 -0
  102. package/src/plugins/tasks/store.ts +132 -0
  103. package/src/plugins/tasks/which.ts +34 -0
  104. package/src/plugins/voice/client.ts +96 -5
  105. package/src/tools/endpoints.ts +31 -5
  106. package/src/tools/packages.ts +12 -4
  107. package/src/tools/plugins.ts +18 -4
  108. package/src/tools/problems.ts +60 -0
  109. package/skills/deictic/references/recordings.md +0 -13
  110. package/skills/deictic/references/tasks.md +0 -22
  111. package/skills/deictic/references/watch.md +0 -17
@@ -0,0 +1,280 @@
1
+ /**
2
+ * The page's tasks, each pinned where its element is: what's shown of them on the page itself, beside the Task
3
+ * tab's list of one element's. A pin is the element's outline, faint, and a badge on its corner with how many
4
+ * tasks are still to do there, in the colour of the furthest along, and a dot where one has something unread. They
5
+ * show while a shortcut's keys are held and while the Task tab is open: a pin under the pointer reads its tasks, and
6
+ * a click opens them in the drawer.
7
+ *
8
+ * In a layer of the overlay's root, which the tab's own styles don't reach once it's closed: so its styles are its own.
9
+ */
10
+ import { named, tell } from '#src/client/component.ts';
11
+ import { div, place, reduced, sameAs } from '#src/client/dom.ts';
12
+ import type { ClientContext, Point } from '#src/index.ts';
13
+
14
+ import { drawerOf } from './drawer.ts';
15
+ import type { Placed } from './index.ts';
16
+ import { OVER, storeOf } from './store.ts';
17
+ import { elementOf } from './which.ts';
18
+
19
+ const STYLE = `
20
+ /* No box of its own, which its pins would stack in: each is under the popup, and the one pointed at over it, with its card. */
21
+ .pins { display: contents; font: 12px/1.4 system-ui, sans-serif; color: var(--deictic-fg); }
22
+ .pin { --pin: var(--deictic-accent); position: fixed; z-index: 2147483646; box-sizing: border-box; pointer-events: none; border: 1px solid color-mix(in srgb, var(--pin) 45%, transparent); border-radius: 6px; transition: border-color 0.15s, box-shadow 0.15s; }
23
+ .pin.claimed { --pin: #60a5fa; }
24
+ .pin.blocked { --pin: #fbbf24; }
25
+ .pin[hidden] { display: none; }
26
+ .pin.spot, .pin.spot:has(.pin-badge:hover, .pin-badge:focus-visible) { border: 0; box-shadow: none; }
27
+ .pin:has(.pin-badge:hover, .pin-badge:focus-visible) { z-index: 2147483647; border-color: var(--pin); box-shadow: 0 0 0 3px color-mix(in srgb, var(--pin) 18%, transparent), 0 0 28px color-mix(in srgb, var(--pin) 45%, transparent); }
28
+ /* A drop, its point on the element's corner. */
29
+ .pin-badge {
30
+ position: absolute;
31
+ display: flex;
32
+ align-items: center;
33
+ justify-content: center;
34
+ box-sizing: border-box;
35
+ min-width: 22px;
36
+ height: 22px;
37
+ padding: 0 6px;
38
+ border: 0;
39
+ border-radius: 11px 11px 11px 3px;
40
+ font: 700 12px/22px system-ui, sans-serif;
41
+ font-variant-numeric: tabular-nums;
42
+ color: #09090b;
43
+ background: var(--pin);
44
+ box-shadow: 0 0 0 3px color-mix(in srgb, var(--pin) 22%, transparent), 0 0 18px color-mix(in srgb, var(--pin) 60%, transparent), 0 4px 12px rgb(0 0 0 / 0.4);
45
+ pointer-events: auto;
46
+ cursor: pointer;
47
+ }
48
+ .pin-badge:focus-visible { outline: 2px solid #fff; outline-offset: 2px; }
49
+ /* Something unread there: a dot on the badge's corner. */
50
+ .pin.unread .pin-badge::after { content: ''; position: absolute; top: -3px; right: -3px; width: 8px; height: 8px; border: 2px solid #09090b; border-radius: 50%; background: #fff; }
51
+ .pin-card {
52
+ position: absolute;
53
+ left: 0;
54
+ top: calc(100% + 8px);
55
+ display: none;
56
+ flex-direction: column;
57
+ gap: 4px;
58
+ width: max-content;
59
+ max-width: 300px;
60
+ padding: 8px 10px;
61
+ border: 1px solid rgb(255 255 255 / 0.12);
62
+ border-radius: 10px;
63
+ font: 12px/1.4 system-ui, sans-serif;
64
+ text-align: left;
65
+ color: var(--deictic-fg);
66
+ background: rgb(24 24 27 / 0.9);
67
+ backdrop-filter: blur(16px) saturate(1.6);
68
+ box-shadow: 0 12px 32px rgb(0 0 0 / 0.45);
69
+ cursor: pointer;
70
+ }
71
+ .pin.left .pin-card { left: auto; right: 0; }
72
+ .pin.up .pin-card { top: auto; bottom: calc(100% + 8px); }
73
+ .pin-badge:hover .pin-card, .pin-badge:focus-visible .pin-card { display: flex; }
74
+ .pin-where { font: 11px ui-monospace, monospace; color: var(--deictic-muted); }
75
+ .pin-where b { color: var(--pin); }
76
+ .pin-task { display: flex; gap: 6px; align-items: baseline; min-width: 0; }
77
+ .pin-task span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
78
+ .pin-task i { flex: none; align-self: center; box-sizing: border-box; width: 8px; height: 8px; border-radius: 50%; border: 1.5px solid var(--deictic-muted); }
79
+ .pin-task i.claimed { border-color: #60a5fa; background: #60a5fa; }
80
+ .pin-task i.blocked { border-color: #fbbf24; background: #fbbf24; }
81
+ .pin-task i.waiting { border-style: dotted; }
82
+ .pin-task em { flex: none; font-style: normal; color: var(--deictic-muted); }
83
+ .pin-more { color: var(--deictic-muted); }
84
+ @media (prefers-reduced-motion: no-preference) {
85
+ .pin { animation: deictic-pin-area 0.3s ease-out backwards; }
86
+ .pin-badge { animation: deictic-pin 0.34s cubic-bezier(0.2, 0.9, 0.25, 1.4) backwards; animation-delay: inherit; transform-origin: 0 100%; }
87
+ }
88
+ @keyframes deictic-pin-area {
89
+ from { opacity: 0; }
90
+ }
91
+ @keyframes deictic-pin {
92
+ from { opacity: 0; scale: 0.2; }
93
+ }
94
+ `;
95
+
96
+ /** How long the pins outlast what showed them, in ms: the popup opens a moment after the pointing that led to it ends. */
97
+ const LINGER = 200;
98
+ /** A badge's size, and how far out of the element's corner it sits, in pixels. */
99
+ const BADGE = 22;
100
+ const OUT = 10;
101
+ const EDGE = 4;
102
+ /** The tasks a pin's card names, before it only counts them. */
103
+ const NAMED = 4;
104
+ /** The states by how far along, the furthest first: a pin is the colour of its furthest. */
105
+ const ALONG = ['blocked', 'claimed', 'ready', 'waiting'];
106
+
107
+ export interface Pins {
108
+ /** Shows them: every place's, to read under the pointer and open with a click. Answers what takes them away. */
109
+ glance: () => () => void;
110
+ }
111
+
112
+ interface Pin {
113
+ place: string;
114
+ /** The spot in the element it's on, for a task about one: the pin is there, and has no outline. */
115
+ at?: Point;
116
+ el: Element;
117
+ box: HTMLElement;
118
+ badge: HTMLButtonElement;
119
+ count: HTMLElement;
120
+ card: HTMLElement;
121
+ /** The tasks its badge counts, as last drawn: what a click opens. */
122
+ tasks: Placed[];
123
+ }
124
+
125
+ const made = new WeakMap<ShadowRoot, Pins>();
126
+
127
+ /** The pins of the overlay `context` is of: one set, whoever shows it. */
128
+ export function pinsIn(context: ClientContext): Pins {
129
+ const kept = made.get(context.root);
130
+ if (kept !== undefined) return kept;
131
+
132
+ const store = storeOf(context);
133
+ let glances = 0;
134
+ let layer: HTMLElement | undefined;
135
+ let frame = 0;
136
+ let leaving: ReturnType<typeof setTimeout> | undefined;
137
+ const pins = new Map<string, Pin>();
138
+ let frames = 0;
139
+
140
+ const pinOf = (at: string, el: Element, spot: Point | undefined): Pin => {
141
+ const box = div('pin');
142
+ const badge = Object.assign(document.createElement('button'), { type: 'button', className: 'pin-badge' });
143
+ const count = document.createElement('span');
144
+ const card = Object.assign(document.createElement('span'), { className: 'pin-card' });
145
+ badge.append(count, card);
146
+ box.append(badge);
147
+ // One after another, as they come: where motion is reduced there's no animation to delay.
148
+ if (!reduced()) box.style.animationDelay = `${Math.min(pins.size, 12) * 30}ms`;
149
+ const pin: Pin = { place: at, ...(spot === undefined ? {} : { at: spot }), el, box, badge, count, card, tasks: [] };
150
+ // In the drawer, where a task is read and changed: the popup is for a new one.
151
+ badge.addEventListener('click', () => drawerOf(context).open(pin.tasks));
152
+ return pin;
153
+ };
154
+
155
+ /** A pin as its tasks are now: how many, the colour of the furthest along, and its card. */
156
+ const fill = (pin: Pin, list: Placed[]) => {
157
+ const state = ALONG.find((s) => list.some((t) => t.state === s)) ?? 'ready';
158
+ pin.tasks = list;
159
+ pin.box.className = `pin ${state}${pin.at === undefined ? '' : ' spot'}${list.some(store.unread) ? ' unread' : ''}`;
160
+ pin.count.textContent = String(list.length);
161
+ const [, file = '', line = ''] = /^(.*):(\d+):\d+$/.exec(pin.place) ?? [];
162
+ const tag = tell(named(pin.el));
163
+ pin.badge.setAttribute('aria-label', `${list.length} ${list.length === 1 ? 'task' : 'tasks'} on ${tag}, line ${line}: open`);
164
+ const where = Object.assign(document.createElement('span'), { className: 'pin-where' });
165
+ where.append(Object.assign(document.createElement('b'), { textContent: tag }), ` ${file.split('/').at(-1)}:${line}`);
166
+ const rows = list.slice(0, NAMED).map((t) => {
167
+ const row = Object.assign(document.createElement('span'), { className: 'pin-task' });
168
+ const dot = Object.assign(document.createElement('i'), { className: t.state });
169
+ const by = t.by === undefined ? [] : [Object.assign(document.createElement('em'), { textContent: t.by })];
170
+ row.append(dot, Object.assign(document.createElement('span'), { textContent: t.title }), ...by);
171
+ return row;
172
+ });
173
+ const rest = list.length - rows.length;
174
+ const more = rest === 0 ? [] : [Object.assign(document.createElement('span'), { className: 'pin-more', textContent: `+${rest} more` })];
175
+ pin.card.replaceChildren(where, ...rows, ...more);
176
+ };
177
+
178
+ /** The pins as the tasks read last have them: one per place with an element on the page, for those still to do. */
179
+ const draw = () => {
180
+ if (layer === undefined) return;
181
+ const tasks = store.tasks().filter((t): t is Placed => t.place !== undefined && !OVER.has(t.state));
182
+ // One for an element's tasks, and one of its own for each spot a task is about.
183
+ // And one for each of several elements from one place, as the page has them now.
184
+ const keyOf = (t: Placed) => {
185
+ const el = elementOf(t);
186
+ const of = el === null ? t.place : `${t.place}#${sameAs(el).indexOf(el)}`;
187
+ return t.point === undefined ? of : `${of}@${t.point.x},${t.point.y}`;
188
+ };
189
+ const byPlace = new Map<string, Placed[]>();
190
+ for (const t of tasks) byPlace.set(keyOf(t), [...(byPlace.get(keyOf(t)) ?? []), t]);
191
+ for (const [at, pin] of pins) {
192
+ if (byPlace.has(at)) continue;
193
+ pin.box.remove();
194
+ pins.delete(at);
195
+ }
196
+ for (const [at, list] of byPlace) {
197
+ let pin = pins.get(at);
198
+ if (pin === undefined) {
199
+ const [first] = list as [Placed, ...Placed[]];
200
+ const { place: where, point } = first;
201
+ const el = elementOf(first);
202
+ if (el === null) continue;
203
+ pin = pinOf(where, el, point);
204
+ pins.set(at, pin);
205
+ layer.append(pin.box);
206
+ }
207
+ fill(pin, list);
208
+ }
209
+ follow();
210
+ };
211
+
212
+ /** Each pin over its element as the page has it now: read each frame, as nothing tells of an element moving. */
213
+ const follow = () => {
214
+ const width = document.documentElement.clientWidth;
215
+ const height = document.documentElement.clientHeight;
216
+ // An element rendered again is another: looked for now and then, not every frame.
217
+ const look = frames++ % 20 === 0;
218
+ for (const pin of pins.values()) {
219
+ if (!pin.el.isConnected && look && pin.tasks[0] !== undefined) pin.el = elementOf(pin.tasks[0]) ?? pin.el;
220
+ const whole = pin.el.getBoundingClientRect();
221
+ // A spot's is a box of no size at the spot: nothing to outline, and the badge's point is on it.
222
+ const r = pin.at === undefined ? whole : new DOMRect(whole.left + pin.at.x, whole.top + pin.at.y, 0, 0);
223
+ const seen =
224
+ pin.el.isConnected && whole.width > 0 && whole.height > 0 && r.bottom > 0 && r.top < height && r.right > 0 && r.left < width;
225
+ pin.box.hidden = !seen;
226
+ if (!seen) continue;
227
+ place(pin.box, r);
228
+ // On the top right corner, or the badge's point on the spot, and in the window where that isn't.
229
+ const out = pin.at === undefined ? OUT : 0;
230
+ const x = Math.max(EDGE, Math.min(r.right - out, width - EDGE - pin.badge.offsetWidth));
231
+ const y = Math.max(EDGE, Math.min(r.top - BADGE + out, height - EDGE - BADGE));
232
+ // From the box's padding edge, which is inside its border, where it has one.
233
+ const edge = pin.at === undefined ? 1 : 0;
234
+ Object.assign(pin.badge.style, { left: `${x - r.left - edge}px`, top: `${y - r.top - edge}px` });
235
+ // Its card opens toward the middle of the window.
236
+ pin.box.classList.toggle('left', x > width / 2);
237
+ pin.box.classList.toggle('up', y > height / 2);
238
+ }
239
+ };
240
+
241
+ store.subscribe(draw);
242
+
243
+ const sync = () => {
244
+ clearTimeout(leaving);
245
+ if (glances === 0) {
246
+ leaving = setTimeout(() => {
247
+ cancelAnimationFrame(frame);
248
+ layer?.remove();
249
+ layer = undefined;
250
+ pins.clear();
251
+ }, LINGER);
252
+ return;
253
+ }
254
+ if (layer === undefined) {
255
+ layer = div('pins');
256
+ layer.append(Object.assign(document.createElement('style'), { textContent: STYLE }));
257
+ frame = requestAnimationFrame(function each() {
258
+ frame = requestAnimationFrame(each);
259
+ follow();
260
+ });
261
+ }
262
+ // After a popup that opened since: at the same height, the later is the upper, and a pin's card is over it.
263
+ context.root.append(layer);
264
+ draw();
265
+ store.load();
266
+ };
267
+
268
+ const shown: Pins = {
269
+ glance: () => {
270
+ glances++;
271
+ sync();
272
+ return () => {
273
+ glances--;
274
+ sync();
275
+ };
276
+ },
277
+ };
278
+ made.set(context.root, shown);
279
+ return shown;
280
+ }
@@ -0,0 +1,132 @@
1
+ /**
2
+ * The channel's tasks as the page has them: read once for the pins and the drawer both, and again when one of
3
+ * them changes a task. And which of them have something unread: a reply since the task was last opened here, or an
4
+ * end no one has looked at. What's been read is the browser's to keep, as a board keeps it for no one.
5
+ */
6
+ import type { ClientContext } from '#src/index.ts';
7
+
8
+ import type { Listed } from './index.ts';
9
+
10
+ /** How long the tasks read last are shown as they are, in ms: the keys are held again and again, and each is no reason to ask the board. */
11
+ const FRESH = 4000;
12
+ /** Where what's been read is kept: each task's ID, and how its thread stood when it was last opened. */
13
+ const SEEN = 'deictic:tasks:seen';
14
+ /** Where the finished tasks put away are kept: each one's ID, and how its thread stood then. */
15
+ const ARCHIVED = 'deictic:tasks:archived';
16
+ /** The states of a finished task, as a board names them: nothing more happens to it. */
17
+ export const OVER = new Set(['completed', 'failed']);
18
+ /** The states whose coming is news in itself, whoever said nothing as it came. */
19
+ const NEWS = new Set(['blocked', 'completed', 'failed']);
20
+
21
+ export interface Store {
22
+ /** The tasks as read last: those still to do, oldest first, then the latest finished. */
23
+ tasks: () => Listed[];
24
+ /** The channel, and its page on the board where there's one to link to: nothing before the first read. */
25
+ channel: () => { name: string; link?: string } | undefined;
26
+ /** Reads them again, unless they were a moment ago: `again` whatever, after a change made here. */
27
+ load: (again?: boolean) => void;
28
+ /** Hears each read; answers what stops it. */
29
+ subscribe: (listener: () => void) => () => void;
30
+ /** Whether a task has something no one has read here. */
31
+ unread: (task: Listed) => boolean;
32
+ /** The reply that was its newest when it was last read here, where it had one. */
33
+ read: (task: Listed) => string | undefined;
34
+ /** Says a task is read, as its thread stands. */
35
+ seen: (task: Listed) => void;
36
+ /** Whether a finished task was put away here, and nothing has happened to it since. */
37
+ archived: (task: Listed) => boolean;
38
+ /** Puts finished tasks away, read: out of the drawer until something happens to one. */
39
+ archive: (tasks: Listed[]) => void;
40
+ }
41
+
42
+ /** How a task's thread stands: its newest reply, and its state where that's news. */
43
+ const markOf = (task: Listed) => `${task.last ?? ''}|${NEWS.has(task.state) ? task.state : ''}`;
44
+
45
+ /** What's kept under `key` (what's been read, what's put away), or nothing where this browser has kept none yet. */
46
+ function kept(key = SEEN): Record<string, string> | undefined {
47
+ try {
48
+ const seen: unknown = JSON.parse(localStorage.getItem(key) ?? 'null');
49
+ return typeof seen === 'object' && seen !== null ? (seen as Record<string, string>) : undefined;
50
+ } catch {
51
+ // Storage can be off, or hold something else: nothing's been kept, then.
52
+ return undefined;
53
+ }
54
+ }
55
+
56
+ const made = new WeakMap<ShadowRoot, Store>();
57
+
58
+ /** The tasks of the overlay `context` is of: one store, whoever asks. */
59
+ export function storeOf(context: ClientContext): Store {
60
+ const known = made.get(context.root);
61
+ if (known !== undefined) return known;
62
+
63
+ let tasks: Listed[] = [];
64
+ let channel: { name: string; link?: string } | undefined;
65
+ let read = 0;
66
+ let asked = 0;
67
+ let seen = kept();
68
+ const listeners = new Set<() => void>();
69
+ const tell = () => {
70
+ for (const listener of listeners) listener();
71
+ };
72
+ let away = kept(ARCHIVED) ?? {};
73
+ const keep = (key = SEEN, value: unknown = seen) => {
74
+ try {
75
+ localStorage.setItem(key, JSON.stringify(value));
76
+ } catch {
77
+ // Not kept past the page, then.
78
+ }
79
+ };
80
+
81
+ const store: Store = {
82
+ tasks: () => tasks,
83
+ channel: () => channel,
84
+ load: (again = false) => {
85
+ if (!again && Date.now() - read < FRESH) return;
86
+ const ask = ++asked;
87
+ read = Date.now();
88
+ void context
89
+ .request('page')
90
+ .then(async (res) => {
91
+ if (!res.ok || ask !== asked) return;
92
+ const answer = (await res.json()) as { channel?: string; link?: string; tasks: Listed[] };
93
+ ({ tasks } = answer);
94
+ // The first read in this browser: what the channel has by now is old news, and only what comes after is unread.
95
+ if (seen === undefined) {
96
+ seen = Object.fromEntries(tasks.map((task) => [task.id, markOf(task)]));
97
+ keep();
98
+ }
99
+ if (answer.channel !== undefined) channel = { name: answer.channel, ...(answer.link === undefined ? {} : { link: answer.link }) };
100
+ tell();
101
+ })
102
+ // As they were, then: whatever opens a task says why the board can't be reached.
103
+ .catch(() => {});
104
+ },
105
+ subscribe: (listener) => {
106
+ listeners.add(listener);
107
+ return () => listeners.delete(listener);
108
+ },
109
+ unread: (task) => seen !== undefined && markOf(task) !== '|' && seen[task.id] !== markOf(task),
110
+ read: (task) => seen?.[task.id]?.split('|', 1)[0] || undefined,
111
+ seen: (task) => {
112
+ if (seen?.[task.id] === markOf(task)) return;
113
+ // Another tab may have read others since: theirs are kept too.
114
+ seen = { ...seen, ...kept(), [task.id]: markOf(task) };
115
+ keep();
116
+ tell();
117
+ },
118
+ archived: (task) => OVER.has(task.state) && away[task.id] === markOf(task),
119
+ archive: (finished) => {
120
+ const marks = Object.fromEntries(finished.filter((task) => OVER.has(task.state)).map((task) => [task.id, markOf(task)]));
121
+ // Only what the board still lists: the rest have gone from it, and needn't be kept.
122
+ const listed = new Set(tasks.map((task) => task.id));
123
+ away = Object.fromEntries(Object.entries({ ...away, ...kept(ARCHIVED), ...marks }).filter(([id]) => listed.has(id)));
124
+ keep(ARCHIVED, away);
125
+ seen = { ...seen, ...kept(), ...marks };
126
+ keep();
127
+ tell();
128
+ },
129
+ };
130
+ made.set(context.root, store);
131
+ return store;
132
+ }
@@ -0,0 +1,34 @@
1
+ /**
2
+ * Which element a task is about, where one place in the source renders several (a list's items, a `map`): the mark
3
+ * alone finds the first of them, so a task keeps its place among them and its text, and is found again by those.
4
+ */
5
+ import { ATTRIBUTE } from '#src/client/dom.ts';
6
+
7
+ import type { Task, Which } from './index.ts';
8
+
9
+ /** As much of an element's text as tells it from its siblings, and as a label holds. */
10
+ const SAID = 120;
11
+
12
+ const textOf = (el: Element) => (el.textContent ?? '').replace(/\s+/g, ' ').trim().slice(0, SAID);
13
+
14
+ /** Which of `alike` (the elements with its mark, in the page's order) `el` is; its text as `said` has it, which may be masked. */
15
+ export function whichOf(el: Element, alike: Element[], said: Element = el): Which {
16
+ const text = textOf(said);
17
+ return { nth: alike.indexOf(el) + 1, ...(text === '' ? {} : { text }) };
18
+ }
19
+
20
+ /**
21
+ * The element a task is about on this page: the only one with its mark, or the one of several with its text, else the
22
+ * one at its place among them. None where it was one of more than the page has now and its text is on none of them.
23
+ */
24
+ export function elementOf(task: Pick<Task, 'which'> & { place?: string }): Element | null {
25
+ if (task.place === undefined) return null;
26
+ const alike = [...document.querySelectorAll(`[${ATTRIBUTE}="${CSS.escape(task.place)}"]`)];
27
+ const { which } = task;
28
+ if (which === undefined || alike.length <= 1) return alike[0] ?? null;
29
+ const at = alike[which.nth - 1];
30
+ const said = which.text === undefined ? [] : alike.filter((el) => textOf(el) === which.text);
31
+ // Text that's on several (`Learn more`) is told apart by the place among them, where that's one of them.
32
+ if (said.length > 0) return at !== undefined && said.includes(at) ? at : said[0]!;
33
+ return at ?? null;
34
+ }
@@ -4,10 +4,32 @@
4
4
  * the rest to `more/<id>`. So a take the page leaves in the middle of (a recording that goes on to another
5
5
  * page) is kept up to there. `stop` sends the last of it and lets the mic go, so the browser's recording
6
6
  * light goes out between takes.
7
+ *
8
+ * Its tool in the tool panel is the mic: on, it's a take, with its time and the mic's live waveform, so it's plain
9
+ * it hears; off again, the clip goes in the tray, for the next task. While a recording is going the mic is the
10
+ * recording's own, and this one is away.
7
11
  */
12
+ import { toolButton } from '#src/client/dom.ts';
13
+ import { playMark, trayed } from '#src/client/drawer.ts';
14
+ import { liveWaveform } from '#src/client/waveform.ts';
8
15
  import { defineClientPlugin } from '#src/index.ts';
9
16
 
10
- import type { Clip, Voice } from './index.ts';
17
+ import type { Clip, Take, Voice } from './index.ts';
18
+
19
+ const STYLE = `
20
+ .voice-tool .time { font: 12px ui-monospace, monospace; }
21
+ .voice-tool .wave { width: 44px; height: 18px; }
22
+ .tool-panel.vertical .voice-tool .wave { width: 28px; }
23
+ .voice-tool .mic[aria-pressed='true'], .voice-tool .mic[aria-pressed='true']:hover { background: #ef4444; color: #fff; }
24
+ /* The recordings plugin's own mic is in the panel while one is going (its \`.recorder.going\`): one mic at a time. */
25
+ .tool-panel:has(.recorder.going) :is(.voice-tool, .sep:has(+ .voice-tool)) { display: none; }
26
+ `;
27
+ const MIC = '<rect x="9" y="3" width="6" height="11" rx="3"/><path d="M5 11a7 7 0 0 0 14 0M12 18v3"/>';
28
+
29
+ const clock = (ms: number) => {
30
+ const s = Math.floor(ms / 1000);
31
+ return `${Math.floor(s / 60)}:${String(s % 60).padStart(2, '0')}`;
32
+ };
11
33
 
12
34
  /** What the browser can record, best first: Opus in WebM where it can (Chrome, Firefox), else MP4 (Safari). */
13
35
  const TYPES = ['audio/webm;codecs=opus', 'audio/webm', 'audio/ogg;codecs=opus', 'audio/mp4'];
@@ -16,8 +38,8 @@ const EVERY = 1000;
16
38
 
17
39
  export default defineClientPlugin<never, never, Voice>({
18
40
  name: 'voice',
19
- setup: ({ base, request }) => ({
20
- start: async () => {
41
+ setup: ({ base, request, tools, tray, drawer, lightbox }) => {
42
+ const start = async (): Promise<Take> => {
21
43
  if (typeof MediaRecorder === 'undefined' || navigator.mediaDevices?.getUserMedia === undefined)
22
44
  throw new Error("voice: this browser can't record the mic here: it needs a secure page (https or localhost)");
23
45
  const stream = await navigator.mediaDevices.getUserMedia({ audio: true }).catch((e: unknown) => {
@@ -75,6 +97,75 @@ export default defineClientPlugin<never, never, Voice>({
75
97
  return { id: id!, url: url ?? `${base}__deictic/plugins/voice/clip/${id}`, duration, type: typeOf() };
76
98
  },
77
99
  };
78
- },
79
- }),
100
+ };
101
+
102
+ const group = Object.assign(document.createElement('div'), { className: 'tool-group voice-tool' });
103
+ const time = Object.assign(document.createElement('span'), { className: 'time', hidden: true });
104
+ const wave = Object.assign(document.createElement('canvas'), { className: 'wave', hidden: true });
105
+ let take: Take | undefined;
106
+ // What the mic is doing (asking for it, keeping a take): the next press waits for it.
107
+ let busy: Promise<void> = Promise.resolve();
108
+ const problem = (e: unknown) => {
109
+ mic.classList.add('problem');
110
+ mic.title = e instanceof Error ? e.message : String(e);
111
+ };
112
+ const on = async () => {
113
+ const mine = await start();
114
+ take = mine;
115
+ // The panel stays while the mic is on: its button is what turns it off.
116
+ const unkeep = tools.keep();
117
+ const stopWave = liveWaveform(wave, mine.stream, { played: '#ef4444', rest: '#52525b' });
118
+ const tick = () => (time.textContent = clock(Date.now() - mine.started));
119
+ tick();
120
+ const ticking = setInterval(tick, 250);
121
+ time.hidden = wave.hidden = false;
122
+ mic.setAttribute('aria-pressed', 'true');
123
+ mic.title = 'Stop';
124
+ return async () => {
125
+ take = undefined;
126
+ clearInterval(ticking);
127
+ stopWave();
128
+ time.hidden = wave.hidden = true;
129
+ mic.setAttribute('aria-pressed', 'false');
130
+ mic.title = 'Voice';
131
+ unkeep();
132
+ const clip = await mine.stop();
133
+ tray.add('voice', clip.id, clip);
134
+ };
135
+ };
136
+ let off: (() => Promise<void>) | undefined;
137
+ const mic = toolButton(MIC, 'Voice', 'K', () => {
138
+ busy = busy.then(async () => {
139
+ mic.classList.remove('problem');
140
+ try {
141
+ if (off === undefined) {
142
+ off = await on();
143
+ } else {
144
+ const ending = off;
145
+ off = undefined;
146
+ await ending();
147
+ }
148
+ } catch (e) {
149
+ problem(e);
150
+ }
151
+ });
152
+ });
153
+ mic.classList.add('mic');
154
+ mic.setAttribute('aria-pressed', 'false');
155
+ group.append(Object.assign(document.createElement('style'), { textContent: STYLE }), mic, time, wave);
156
+ // Beside the other tools that capture the page: a shot, a recording, the mic.
157
+ tools.add(group, { section: 'capture' });
158
+ // In the drawer until a task takes them: each plays in the lightbox.
159
+ drawer.add(
160
+ trayed({ tray }, 'voice', 'Voice notes', (item, i, all) => {
161
+ const { url, duration } = item.value as Clip;
162
+ const name = `${all.length === 1 ? 'Voice' : `Voice ${i + 1}`} ${clock(duration)}`;
163
+ return { label: [playMark(), name], open: (from) => lightbox([{ kind: 'audio', url, title: name }], { from }) };
164
+ }),
165
+ );
166
+ // A take the page leaves in the middle of is kept as far as it's sent, and the mic let go.
167
+ addEventListener('pagehide', () => take?.cancel());
168
+
169
+ return { start };
170
+ },
80
171
  });
@@ -8,6 +8,8 @@
8
8
  *
9
9
  * Everything but `open` and `history` writes or may, so only the page itself may send it: a POST of
10
10
  * JSON from the same origin, which a form or a script on another site can't send without asking first.
11
+ * `problem` is the page's way to report one (`Problem`): it and the server's own, and any request that fails with
12
+ * something other than a `Refusal`, are said once and handed to the plugins that hear of them (`problems.ts`).
11
13
  *
12
14
  * Each plugin's own `routes` are under `plugins/<name>/`. A `GET` there reaches the plugin as it
13
15
  * comes; anything else only from the page's own origin, in whatever type it sends (an image, say).
@@ -18,8 +20,9 @@ import type { IncomingMessage, ServerResponse } from 'node:http';
18
20
  import { type Loc, Refusal, type ServerPlugin, type Source, type Target } from '#src/index.ts';
19
21
 
20
22
  import { History, type Scope } from './history.ts';
21
- import { missingMessage } from './packages.ts';
23
+ import { missingProblem } from './packages.ts';
22
24
  import { setUp } from './plugins.ts';
25
+ import { messageOf, problemIn, reporter, stackOf } from './problems.ts';
23
26
  import { applyEdits, resolveFile, sourceAt } from './source.ts';
24
27
 
25
28
  export interface EndpointOptions {
@@ -47,10 +50,12 @@ const scopeIn = ({ plugin, target }: Partial<Body>) =>
47
50
  export function endpoints({ root, plugins, base = '/', open, history = new History(root, { plugins }) }: EndpointOptions) {
48
51
  const byName = new Map(plugins.map((p) => [p.name, p]));
49
52
  const prefix = `${base}__deictic/`;
53
+ // What goes wrong that nothing in the page caused: said, and handed to the plugins that hear of it once set up.
54
+ const report = reporter(plugins, () => ready);
50
55
  // Started now, awaited by every request: a plugin's API is there before anything asks for it.
51
- const ready = setUp(root, plugins);
52
- const gone = missingMessage(root, plugins);
53
- if (gone !== undefined) console.warn(gone);
56
+ const ready = setUp(root, plugins, report);
57
+ const gone = missingProblem(root, plugins);
58
+ if (gone !== undefined) report(gone);
54
59
 
55
60
  const pluginOf = (name: string) => {
56
61
  const plugin = byName.get(name);
@@ -105,10 +110,15 @@ export function endpoints({ root, plugins, base = '/', open, history = new Histo
105
110
  const route = url.pathname.slice(prefix.length);
106
111
  const origin = req.headers.get('origin');
107
112
  const sameOrigin = origin !== null && new URL(origin).host === url.host;
113
+ // What failed, for a problem's key: a plugin's route by its first segment, not the IDs after it.
114
+ let where = route;
115
+ let plugin: string | undefined;
108
116
  try {
109
117
  const context = await ready;
110
118
  if (route.startsWith('plugins/')) {
111
119
  const [name = '', first = ''] = route.slice('plugins/'.length).split('/');
120
+ where = `plugins/${name}/${first}`;
121
+ plugin = name;
112
122
  const handle = byName.get(name)?.routes?.[first];
113
123
  if (handle === undefined) return new Response(`no endpoint ${url.pathname}`, { status: 404 });
114
124
  if (req.method !== 'GET' && req.method !== 'HEAD' && !sameOrigin)
@@ -126,13 +136,29 @@ export function endpoints({ root, plugins, base = '/', open, history = new Histo
126
136
  }
127
137
  if (route === 'open') return await open(resolveFile(root, loc.file), loc);
128
138
  const handler = routes[route];
129
- if (handler === undefined) return new Response(`no endpoint ${url.pathname}`, { status: 404 });
139
+ if (handler === undefined && route !== 'problem') return new Response(`no endpoint ${url.pathname}`, { status: 404 });
130
140
  if (req.method !== 'POST' || !sameOrigin || req.headers.get('content-type')?.startsWith('application/json') !== true)
131
141
  return Response.json({ problem: 'edits come from the page, as JSON' }, { status: 403 });
142
+ if (handler === undefined) {
143
+ const problem = problemIn(await req.json());
144
+ if (problem === undefined) return Response.json({ problem: 'a problem has a key, a kind and a message' }, { status: 400 });
145
+ report(problem);
146
+ return Response.json({});
147
+ }
132
148
  const body = (await req.json()) as Body;
149
+ plugin = body.plugin;
133
150
  return Response.json(await (route === 'read' ? handler(body) : serially(() => handler(body))));
134
151
  } catch (e) {
135
152
  if (e instanceof Refusal) return Response.json({ problem: e.message }, { status: 422 });
153
+ // Not a refusal, which says what to do instead: something broke, which no one in the page can fix.
154
+ const message = messageOf(e);
155
+ report({
156
+ key: `error:${where}:${message.split('\n', 1)[0]!.slice(0, 200)}`,
157
+ kind: 'bug',
158
+ ...(plugin === undefined ? {} : { plugin }),
159
+ message: `${prefix}${where} failed: ${message}`,
160
+ ...stackOf(e),
161
+ });
136
162
  return Response.json({ problem: e instanceof Error ? (e.stack ?? e.message) : String(e) }, { status: 500 });
137
163
  }
138
164
  };