deictic 0.0.1 → 0.1.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 (173) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +144 -0
  3. package/dist/annotations/client.d.ts +4 -0
  4. package/dist/annotations/client.d.ts.map +1 -0
  5. package/dist/annotations/client.js +497 -0
  6. package/dist/annotations/client.js.map +1 -0
  7. package/dist/annotations/index.d.ts +110 -0
  8. package/dist/annotations/index.d.ts.map +1 -0
  9. package/dist/annotations/index.js +22 -0
  10. package/dist/annotations/index.js.map +1 -0
  11. package/dist/astro.d.ts +29 -0
  12. package/dist/astro.d.ts.map +1 -0
  13. package/dist/astro.js +24 -0
  14. package/dist/astro.js.map +1 -0
  15. package/dist/bun.d.ts +12 -0
  16. package/dist/bun.d.ts.map +1 -0
  17. package/dist/bun.js +67 -0
  18. package/dist/bun.js.map +1 -0
  19. package/dist/client/index.d.ts +93 -0
  20. package/dist/client/index.d.ts.map +1 -0
  21. package/dist/client/index.js +1329 -0
  22. package/dist/client/index.js.map +1 -0
  23. package/dist/endpoints.js +354 -0
  24. package/dist/endpoints.js.map +1 -0
  25. package/dist/index.d.ts +220 -0
  26. package/dist/index.d.ts.map +1 -0
  27. package/dist/index.js +10 -0
  28. package/dist/index.js.map +1 -0
  29. package/dist/index2.d.ts +23 -0
  30. package/dist/index3.d.ts +98 -0
  31. package/dist/index3.d.ts.map +1 -0
  32. package/dist/index4.d.ts +74 -0
  33. package/dist/index4.d.ts.map +1 -0
  34. package/dist/index5.d.ts +70 -0
  35. package/dist/index5.d.ts.map +1 -0
  36. package/dist/integration.d.ts +19 -0
  37. package/dist/integration.d.ts.map +1 -0
  38. package/dist/integration.js +277 -0
  39. package/dist/integration.js.map +1 -0
  40. package/dist/kept.js +23 -0
  41. package/dist/kept.js.map +1 -0
  42. package/dist/loader.d.ts +15 -0
  43. package/dist/loader.d.ts.map +1 -0
  44. package/dist/loader.js +17 -0
  45. package/dist/loader.js.map +1 -0
  46. package/dist/marks.js +14 -0
  47. package/dist/marks.js.map +1 -0
  48. package/dist/next.d.ts +17 -0
  49. package/dist/next.d.ts.map +1 -0
  50. package/dist/next.js +150 -0
  51. package/dist/next.js.map +1 -0
  52. package/dist/playwright.js +105 -0
  53. package/dist/playwright.js.map +1 -0
  54. package/dist/recommended/index.d.ts +25 -0
  55. package/dist/recommended/index.d.ts.map +1 -0
  56. package/dist/recommended/index.js +19 -0
  57. package/dist/recommended/index.js.map +1 -0
  58. package/dist/recordings/client.d.ts +4 -0
  59. package/dist/recordings/client.d.ts.map +1 -0
  60. package/dist/recordings/client.js +376 -0
  61. package/dist/recordings/client.js.map +1 -0
  62. package/dist/recordings/index.d.ts +2 -0
  63. package/dist/recordings/index.js +3 -0
  64. package/dist/recordings.js +235 -0
  65. package/dist/recordings.js.map +1 -0
  66. package/dist/screenshots/client.d.ts +23 -0
  67. package/dist/screenshots/client.d.ts.map +1 -0
  68. package/dist/screenshots/client.js +76 -0
  69. package/dist/screenshots/client.js.map +1 -0
  70. package/dist/screenshots/dom.d.ts +4 -0
  71. package/dist/screenshots/dom.d.ts.map +1 -0
  72. package/dist/screenshots/dom.js +92 -0
  73. package/dist/screenshots/dom.js.map +1 -0
  74. package/dist/screenshots/index.d.ts +2 -0
  75. package/dist/screenshots/index.js +98 -0
  76. package/dist/screenshots/index.js.map +1 -0
  77. package/dist/screenshots/rrweb.d.ts +9 -0
  78. package/dist/screenshots/rrweb.d.ts.map +1 -0
  79. package/dist/screenshots/rrweb.js +60 -0
  80. package/dist/screenshots/rrweb.js.map +1 -0
  81. package/dist/shortcut.js +63 -0
  82. package/dist/shortcut.js.map +1 -0
  83. package/dist/tailwind/client.d.ts +4 -0
  84. package/dist/tailwind/client.d.ts.map +1 -0
  85. package/dist/tailwind/client.js +613 -0
  86. package/dist/tailwind/client.js.map +1 -0
  87. package/dist/tailwind/index.d.ts +2 -0
  88. package/dist/tailwind/index.js +557 -0
  89. package/dist/tailwind/index.js.map +1 -0
  90. package/dist/tasks/client.d.ts +6 -0
  91. package/dist/tasks/client.d.ts.map +1 -0
  92. package/dist/tasks/client.js +642 -0
  93. package/dist/tasks/client.js.map +1 -0
  94. package/dist/tasks/index.d.ts +159 -0
  95. package/dist/tasks/index.d.ts.map +1 -0
  96. package/dist/tasks/index.js +348 -0
  97. package/dist/tasks/index.js.map +1 -0
  98. package/dist/vite.d.ts +17 -0
  99. package/dist/vite.d.ts.map +1 -0
  100. package/dist/vite.js +44 -0
  101. package/dist/vite.js.map +1 -0
  102. package/dist/voice/client.d.ts +4 -0
  103. package/dist/voice/client.d.ts.map +1 -0
  104. package/dist/voice/client.js +66 -0
  105. package/dist/voice/client.js.map +1 -0
  106. package/dist/voice/index.d.ts +47 -0
  107. package/dist/voice/index.d.ts.map +1 -0
  108. package/dist/voice/index.js +74 -0
  109. package/dist/voice/index.js.map +1 -0
  110. package/dist/waveform.js +93 -0
  111. package/dist/waveform.js.map +1 -0
  112. package/dist/webpack.d.ts +50 -0
  113. package/dist/webpack.d.ts.map +1 -0
  114. package/dist/webpack.js +78 -0
  115. package/dist/webpack.js.map +1 -0
  116. package/package.json +307 -7
  117. package/skills/deictic/SKILL.md +47 -0
  118. package/skills/deictic/references/install.md +81 -0
  119. package/skills/deictic/references/recordings.md +83 -0
  120. package/skills/deictic/references/tasks.md +79 -0
  121. package/skills/deictic/references/watch.md +43 -0
  122. package/skills/deictic/scripts/frame.mjs +62 -0
  123. package/skills/deictic/scripts/lib.mjs +193 -0
  124. package/skills/deictic/scripts/recording.mjs +186 -0
  125. package/skills/deictic/scripts/task.mjs +84 -0
  126. package/skills/deictic/scripts/transcribe.mjs +93 -0
  127. package/skills/deictic/scripts/voice.mjs +50 -0
  128. package/skills/deictic/scripts/watch.mjs +51 -0
  129. package/src/astro.ts +34 -0
  130. package/src/bun.ts +79 -0
  131. package/src/client/index.ts +833 -0
  132. package/src/client/lightbox.ts +256 -0
  133. package/src/client/style.ts +243 -0
  134. package/src/client/tooltip.ts +78 -0
  135. package/src/client/waveform.ts +98 -0
  136. package/src/index.ts +240 -0
  137. package/src/loader.ts +28 -0
  138. package/src/next.ts +133 -0
  139. package/src/plugins/annotations/client.ts +301 -0
  140. package/src/plugins/annotations/draw.ts +169 -0
  141. package/src/plugins/annotations/index.ts +105 -0
  142. package/src/plugins/annotations/svg.ts +61 -0
  143. package/src/plugins/recommended/index.ts +35 -0
  144. package/src/plugins/recordings/client.ts +315 -0
  145. package/src/plugins/recordings/index.ts +174 -0
  146. package/src/plugins/recordings/marks.ts +11 -0
  147. package/src/plugins/recordings/ours.ts +70 -0
  148. package/src/plugins/recordings/replay.ts +139 -0
  149. package/src/plugins/screenshots/client.ts +63 -0
  150. package/src/plugins/screenshots/dom.ts +79 -0
  151. package/src/plugins/screenshots/index.ts +194 -0
  152. package/src/plugins/screenshots/playwright.ts +119 -0
  153. package/src/plugins/screenshots/rrweb.ts +45 -0
  154. package/src/plugins/tailwind/catalog.ts +396 -0
  155. package/src/plugins/tailwind/client.ts +669 -0
  156. package/src/plugins/tailwind/index.ts +267 -0
  157. package/src/plugins/tasks/client.ts +622 -0
  158. package/src/plugins/tasks/index.ts +513 -0
  159. package/src/plugins/voice/client.ts +57 -0
  160. package/src/plugins/voice/index.ts +114 -0
  161. package/src/shortcut.ts +83 -0
  162. package/src/tools/astro.ts +81 -0
  163. package/src/tools/editor.ts +22 -0
  164. package/src/tools/endpoints.ts +163 -0
  165. package/src/tools/history.ts +203 -0
  166. package/src/tools/integration.ts +106 -0
  167. package/src/tools/kept.ts +21 -0
  168. package/src/tools/locate.ts +94 -0
  169. package/src/tools/plugins.ts +41 -0
  170. package/src/tools/source.ts +72 -0
  171. package/src/vite.ts +62 -0
  172. package/src/webpack.ts +107 -0
  173. package/index.js +0 -1
@@ -0,0 +1,622 @@
1
+ /**
2
+ * The tasks panel: the tasks already about this element and their state, and a field to add one. Each
3
+ * task's ID opens it on the board, where the board has a page; one no one has claimed yet can be edited
4
+ * in place (its words) or deleted (canceled on the board), both changes the popup's undo takes back.
5
+ * Ctrl+Enter or Add sends it with the page's URL, the viewport and the element as it rendered, which
6
+ * the source can't tell; and where the annotations plugin is loaded, what's marked on the page.
7
+ *
8
+ * Where the screenshots plugin is loaded, a shot of the page goes with it, unless that's unticked. With
9
+ * the annotations plugin too, Annotate takes the shot now and opens it to draw on: saved, it's a chip
10
+ * under the field, which opens it again to change the marks, and Add another takes one more. Any chip
11
+ * (a recording's too) takes the box's place; removing the last brings it back unticked, so a task goes
12
+ * without a shot unless asked again.
13
+ *
14
+ * Where the recordings plugin is loaded, Record leaves the popup (`away`) to record the page, keeping
15
+ * what's typed and attached, and comes back with the recording as a chip too, which plays it back.
16
+ * Where the voice plugin is, Voice records the mic here in the panel: stopped, it's a chip that plays it.
17
+ */
18
+ import { liveWaveform } from '#src/client/waveform.ts';
19
+ import { defineClientPlugin, type LightboxItem, type Target } from '#src/index.ts';
20
+
21
+ // For `use()`'s types: the plugins may not be loaded, and nothing of them runs here.
22
+ import type { Annotation, Annotations, Picture } from '#src/plugins/annotations/index.ts';
23
+ import type { Recording } from '#src/plugins/recordings/index.ts';
24
+ import type { Shot } from '#src/plugins/screenshots/index.ts';
25
+ import type { Clip, Take } from '#src/plugins/voice/index.ts';
26
+
27
+ import type { Change, Marks, Model, Op, Task, TaskAdded } from './index.ts';
28
+
29
+ const STYLE = `
30
+ .tasks { list-style: none; margin: 0 0 8px; padding: 0; display: grid; gap: 4px; }
31
+ .tasks li { display: flex; gap: 6px; align-items: baseline; }
32
+ .tasks .title { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
33
+ .tasks .state { font: 11px ui-monospace, monospace; color: var(--deictic-muted); }
34
+ .tasks .state.completed { color: #4ade80; }
35
+ .tasks .state.failed { color: var(--deictic-problem); }
36
+ .tasks a { color: inherit; text-decoration: none; }
37
+ .tasks a:hover { text-decoration: underline; }
38
+ .tasks .act { align-self: center; display: inline-flex; padding: 2px; border: 0; color: var(--deictic-muted); }
39
+ .tasks .act:hover { color: var(--deictic-fg); }
40
+ .tasks .act svg { width: 12px; height: 12px; }
41
+ .tasks li.editing { display: grid; }
42
+ .tasks li.editing .row { margin-top: 0; }
43
+ .tasks li.editing .actions { display: flex; gap: 6px; }
44
+ textarea { min-height: 64px; resize: vertical; font: 13px/1.4 system-ui, sans-serif; }
45
+ .row { display: flex; gap: 6px; margin-top: 8px; justify-content: space-between; align-items: center; }
46
+ .hint { color: var(--deictic-muted); font-size: 12px; }
47
+ .hint a { color: inherit; }
48
+ .hint a:hover { color: var(--deictic-fg); }
49
+ .shot { display: flex; gap: 6px; align-items: center; margin-top: 6px; font-size: 12px; color: var(--deictic-muted); }
50
+ .shot label { display: flex; gap: 6px; align-items: center; }
51
+ .shot input { width: auto; margin: 0; }
52
+ .shot .actions { display: flex; gap: 6px; margin-left: auto; }
53
+ .chips { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 6px; align-items: center; font-size: 12px; }
54
+ .chip { display: inline-flex; align-items: center; border: 1px solid var(--deictic-line); border-radius: 6px; }
55
+ .chip button { border: 0; }
56
+ .chip .open { display: inline-flex; align-items: center; gap: 6px; padding: 2px 4px 2px 2px; }
57
+ .chip img { display: block; height: 28px; border-radius: 4px; }
58
+ .chip .play { display: inline-flex; align-items: center; justify-content: center; width: 28px; height: 28px; border-radius: 4px; background: var(--deictic-line); }
59
+ .chip .play svg { width: 12px; height: 12px; }
60
+ .chip .remove { padding: 0 6px; color: var(--deictic-muted); }
61
+ .chip .remove:hover { color: var(--deictic-fg); }
62
+ .voice-on { display: inline-flex; align-items: center; gap: 4px; color: #fff; background: #ef4444; border-color: #ef4444; font-variant-numeric: tabular-nums; }
63
+ .voice-on .wave { width: 36px; height: 14px; }
64
+ .shot-problem { margin-top: 4px; font-size: 12px; color: var(--deictic-problem); }
65
+ .shot-problem:empty { display: none; }
66
+ .shot[hidden], .chips[hidden], label[hidden] { display: none; }
67
+ `;
68
+
69
+ const ICON = (path: string) =>
70
+ `<svg viewBox="0 0 24 24" aria-hidden="true" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="${path}"/></svg>`;
71
+ const EDIT = ICON('M4 20h4L19 9l-4-4L4 16z');
72
+ const DELETE = ICON('M4 7h16M10 7V4h4v3M6 7l1 13h10l1-13');
73
+ const PLAY = '<svg viewBox="0 0 24 24" aria-hidden="true"><path d="M7 4v16l13-8z" fill="currentColor"/></svg>';
74
+
75
+ /** Marks as a task tells of them: how many, and what the notes say. */
76
+ function marksOf(list: readonly Annotation[]): Marks {
77
+ return { count: list.length, notes: list.flatMap(({ shape }) => (shape.kind === 'note' ? [shape.text] : [])) };
78
+ }
79
+
80
+ /** The page's annotations for the op, where there are any. */
81
+ function annotationsOf(marks: Annotations | undefined): Pick<Op, 'annotations'> {
82
+ const list = marks?.list() ?? [];
83
+ return list.length === 0 ? {} : { annotations: marksOf(list) };
84
+ }
85
+
86
+ /** An annotated shot: the shot as taken, the marks drawn on it, and the PNG of both. */
87
+ interface Drawn {
88
+ shot: Shot;
89
+ marks: Annotation[];
90
+ png: Blob;
91
+ }
92
+
93
+ /** A drawn shot as the panel shows it, with a URL for its thumbnail, revoked with it. */
94
+ interface Chip extends Drawn {
95
+ url: string;
96
+ }
97
+
98
+ /** What the panel keeps while it's away recording: what's typed, ticked and attached. */
99
+ interface Kept {
100
+ text: string;
101
+ attach: boolean;
102
+ drawn: Drawn[];
103
+ recordings: Recording[];
104
+ voices: Clip[];
105
+ }
106
+
107
+ const clock = (ms: number) => {
108
+ const s = Math.round(ms / 1000);
109
+ return `${Math.floor(s / 60)}:${String(s % 60).padStart(2, '0')}`;
110
+ };
111
+
112
+ const messageOf = (e: unknown) => (e instanceof Error ? e.message : String(e));
113
+
114
+ /**
115
+ * `#shadow-root > div.panel > div.head > button.tab:nth-of-type(2)`: where `inner` is inside `outer`, through
116
+ * shadow roots, by tag, classes and, among siblings alike, place, for whoever finds it in the code.
117
+ */
118
+ function pathTo(inner: Element, outer: Element): string {
119
+ const steps: string[] = [];
120
+ for (let at: Element | undefined = inner; at !== undefined && at !== outer;) {
121
+ const nameOf = (el: Element) => `${el.localName}${[...el.classList].map((c) => `.${c}`).join('')}`;
122
+ const name = nameOf(at);
123
+ const siblings = at.parentElement === null ? [] : [...at.parentElement.children];
124
+ // Its place only where a sibling reads the same.
125
+ const same = siblings.filter((c) => nameOf(c) === name).length > 1;
126
+ const alike = siblings.filter((c) => c.localName === at!.localName);
127
+ steps.unshift(same ? `${name}:nth-of-type(${alike.indexOf(at) + 1})` : name);
128
+ if (at.parentElement !== null) {
129
+ at = at.parentElement;
130
+ } else {
131
+ const root = at.getRootNode();
132
+ if (!(root instanceof ShadowRoot)) break;
133
+ steps.unshift('#shadow-root');
134
+ at = root.host;
135
+ }
136
+ }
137
+ return steps.join(' > ');
138
+ }
139
+
140
+ function element<K extends keyof HTMLElementTagNameMap>(tag: K, props: Partial<HTMLElementTagNameMap[K]> = {}): HTMLElementTagNameMap[K] {
141
+ return Object.assign(document.createElement(tag), props);
142
+ }
143
+
144
+ /** The event the page hears of each added task by, on `window`: its `detail` is a `TaskAdded`. */
145
+ export const TASK_ADDED = 'deictic:task';
146
+
147
+ /** Tells the page of a task just added: the newest of the element's tasks, as the board answered them. */
148
+ function announce(model: Model, op: Op, target: Target): void {
149
+ const task = model.tasks.at(-1);
150
+ if (task === undefined) return;
151
+ const marks = (op.annotations?.count ?? 0) + (op.screenshots ?? []).reduce((n, shot) => n + (shot.marks?.count ?? 0), 0);
152
+ const detail: TaskAdded = {
153
+ id: task.id,
154
+ title: task.title,
155
+ state: task.state,
156
+ channel: model.channel,
157
+ target,
158
+ page: op.page,
159
+ attached: {
160
+ screenshots: op.screenshots?.length ?? 0,
161
+ recordings: op.recordings?.length ?? 0,
162
+ voice: op.voice?.length ?? 0,
163
+ marks,
164
+ },
165
+ };
166
+ window.dispatchEvent(new CustomEvent(TASK_ADDED, { detail }));
167
+ }
168
+
169
+ export default defineClientPlugin<Model, Change>({
170
+ name: 'tasks',
171
+ label: 'Task',
172
+ mount: (panel, context) => {
173
+ const style = element('style', { textContent: STYLE });
174
+ const list = element('ul', { className: 'tasks' });
175
+ const text = element('textarea', { placeholder: 'What should change here?' });
176
+ const add = element('button', { type: 'button', className: 'primary', textContent: 'Add task' });
177
+ const hint = element('span', { className: 'hint' });
178
+ const row = element('div', { className: 'row' });
179
+ row.append(hint, add);
180
+ panel.append(style, list, text);
181
+
182
+ const shots = context.use('screenshots');
183
+ const notes = context.use('annotations');
184
+ const recorder = context.use('recordings');
185
+ const voice = context.use('voice');
186
+ // Closes an open editor with the panel.
187
+ const aborts = new AbortController();
188
+
189
+ const attach = element('input', { type: 'checkbox', checked: true });
190
+ const toggle = element('label', { hidden: shots === undefined });
191
+ toggle.append(attach, 'Attach a screenshot');
192
+ const annotate = element('button', { type: 'button', textContent: 'Annotate', title: 'Take the screenshot now and draw on it' });
193
+ const record = element('button', {
194
+ type: 'button',
195
+ textContent: 'Record',
196
+ title: 'Record the page, with a toolbar to draw on it, then come back here',
197
+ });
198
+ const speak = element('button', { type: 'button', textContent: 'Voice', title: 'Record your voice, here' });
199
+ const actions = element('span', { className: 'actions' });
200
+ actions.append(...(shots !== undefined && notes !== undefined ? [annotate] : []));
201
+ const shot = element('div', { className: 'shot' });
202
+ shot.append(toggle, actions);
203
+ const chipsEl = element('div', { className: 'chips' });
204
+ const another = element('button', { type: 'button', textContent: 'Add another', title: 'Take another screenshot and draw on it' });
205
+ const problem = element('div', { className: 'shot-problem' });
206
+ if (shots !== undefined || recorder !== undefined || voice !== undefined) panel.append(shot, chipsEl, problem);
207
+ panel.append(row);
208
+
209
+ let chips: Chip[] = [];
210
+ let recordings: Recording[] = [];
211
+ let voices: Clip[] = [];
212
+ let busy = false;
213
+ const attachedCount = () => chips.length + recordings.length + voices.length;
214
+ // A task goes with words, or with something attached that says it; not while one is going.
215
+ let sending = false;
216
+ const able = () => (add.disabled = sending || (text.value.trim() === '' && attachedCount() === 0));
217
+ // Record and Voice, where their plugins are: beside Annotate, or after the chips while the box is away.
218
+ const more = () => [...(recorder !== undefined ? [record] : []), ...(voice !== undefined ? [speak] : [])];
219
+ const chip = (content: (string | Node)[], title: string, at: number, remove: string, drop: () => void) => {
220
+ const opener = element('button', { type: 'button', className: 'open', title });
221
+ opener.append(...content);
222
+ // Every attachment in one lightbox, opened at this one, growing out of its chip.
223
+ opener.addEventListener('click', () => context.lightbox(viewed(), { at, from: el }));
224
+ const remover = element('button', { type: 'button', className: 'remove', textContent: '×', title: remove });
225
+ remover.addEventListener('click', drop);
226
+ const el = element('span', { className: 'chip' });
227
+ el.append(opener, remover);
228
+ return el;
229
+ };
230
+ const shotName = (i: number) => `Screenshot ${i + 1}`;
231
+ const recordingName = (i: number) => (recordings.length === 1 ? 'Recording' : `Recording ${i + 1}`);
232
+ const voiceName = (i: number) => (voices.length === 1 ? 'Voice' : `Voice ${i + 1}`);
233
+ /** What's attached, as the lightbox shows it, in the chips' order. */
234
+ const viewed = (): LightboxItem[] => [
235
+ ...chips.map((c, i) => ({
236
+ kind: 'image' as const,
237
+ url: c.url,
238
+ title: shotName(i),
239
+ actions: [{ label: 'Change marks', title: 'Draw on it again', run: () => void draw(c) }],
240
+ })),
241
+ ...recordings.map((r, i) => ({
242
+ kind: 'page' as const,
243
+ // The replay filling the frame, without a header of its own: the lightbox names it.
244
+ url: `${r.url}${r.url.includes('?') ? '&' : '?'}embed`,
245
+ title: `${recordingName(i)} · ${clock(r.duration)}`,
246
+ actions: [
247
+ { label: 'New tab', title: 'Play it back in a tab of its own', run: () => void window.open(r.url, '_blank', 'noopener') },
248
+ ],
249
+ })),
250
+ ...voices.map((v, i) => ({ kind: 'audio' as const, url: v.url, title: `${voiceName(i)} · ${clock(v.duration)}` })),
251
+ ];
252
+ const showChips = () => {
253
+ // The box stands for a shot taken as the task goes, so it's away while anything else is attached.
254
+ const attached = attachedCount() > 0;
255
+ shot.hidden = attached;
256
+ chipsEl.hidden = !attached;
257
+ if (!busy) another.textContent = chips.length > 0 ? 'Add another' : 'Annotate';
258
+ if (!attached) actions.append(...more());
259
+ chipsEl.replaceChildren(
260
+ ...chips.map((c, i) => {
261
+ const thumb = element('img', { src: c.url, alt: '' });
262
+ return chip([thumb, shotName(i)], `Open ${shotName(i).toLowerCase()}`, i, `Remove ${shotName(i).toLowerCase()}`, () =>
263
+ dropShot(c),
264
+ );
265
+ }),
266
+ ...recordings.map((r, i) => {
267
+ const play = element('span', { className: 'play', innerHTML: PLAY });
268
+ return chip(
269
+ [play, `${recordingName(i)} ${clock(r.duration)}${r.voice === undefined ? '' : ' · voice'}`],
270
+ `Play ${recordingName(i).toLowerCase()} back`,
271
+ chips.length + i,
272
+ `Remove ${recordingName(i).toLowerCase()}`,
273
+ () => {
274
+ recordings = recordings.filter((x) => x !== r);
275
+ dropped();
276
+ },
277
+ );
278
+ }),
279
+ ...voices.map((v, i) => {
280
+ const play = element('span', { className: 'play', innerHTML: PLAY });
281
+ return chip(
282
+ [play, `${voiceName(i)} ${clock(v.duration)}`],
283
+ `Play ${voiceName(i).toLowerCase()}`,
284
+ chips.length + recordings.length + i,
285
+ `Remove ${voiceName(i).toLowerCase()}`,
286
+ () => {
287
+ voices = voices.filter((x) => x !== v);
288
+ dropped();
289
+ },
290
+ );
291
+ }),
292
+ ...(attached && shots !== undefined && notes !== undefined ? [another] : []),
293
+ ...(attached ? more() : []),
294
+ );
295
+ able();
296
+ mark();
297
+ };
298
+ const dropShot = (c: Chip) => {
299
+ URL.revokeObjectURL(c.url);
300
+ chips = chips.filter((x) => x !== c);
301
+ dropped();
302
+ };
303
+ // Back to the box, unticked, once the last is gone: what was attached is, so nothing is until it's asked for again.
304
+ const dropped = () => {
305
+ if (attachedCount() === 0) attach.checked = false;
306
+ showChips();
307
+ };
308
+ const toChip = (drawn: Drawn): Chip => ({ ...drawn, url: URL.createObjectURL(drawn.png) });
309
+ const reset = () => {
310
+ for (const c of chips) URL.revokeObjectURL(c.url);
311
+ chips = [];
312
+ recordings = [];
313
+ voices = [];
314
+ attach.checked = true;
315
+ showChips();
316
+ };
317
+
318
+ /** Takes a shot and opens it to draw on, or opens a chip's again: saved, it's a chip. */
319
+ const draw = async (old?: Chip) => {
320
+ if (shots === undefined || notes === undefined || busy) return;
321
+ busy = true;
322
+ const was = { annotate: annotate.textContent, another: another.textContent };
323
+ annotate.textContent = another.textContent = old === undefined ? 'Capturing…' : was.another;
324
+ problem.textContent = '';
325
+ try {
326
+ const taken = old?.shot ?? (await shots.capture(context.element));
327
+ annotate.textContent = was.annotate;
328
+ another.textContent = was.another;
329
+ const picture: Picture = { url: taken.url, ...taken.size };
330
+ const marks = await notes.edit(picture, old?.marks ?? [], { signal: aborts.signal });
331
+ if (marks === undefined) return;
332
+ const next = toChip({ shot: taken, marks, png: await notes.flatten(picture, marks) });
333
+ if (old === undefined) {
334
+ chips = [...chips, next];
335
+ } else {
336
+ URL.revokeObjectURL(old.url);
337
+ chips = chips.map((c) => (c === old ? next : c));
338
+ }
339
+ showChips();
340
+ } catch (e) {
341
+ problem.textContent = messageOf(e);
342
+ } finally {
343
+ annotate.textContent = was.annotate;
344
+ another.textContent = was.another;
345
+ busy = false;
346
+ showChips();
347
+ }
348
+ };
349
+ annotate.addEventListener('click', () => void draw());
350
+ another.addEventListener('click', () => void draw());
351
+ record.addEventListener('click', () => {
352
+ if (recorder === undefined || busy) return;
353
+ // A take going on is kept first, as a chip, to come back to with the rest.
354
+ void speakOff().then(() => {
355
+ const drawn = chips.map(({ url: _, ...d }) => d);
356
+ const kept: Kept = { text: text.value, attach: attach.checked, drawn, recordings, voices };
357
+ context.away(kept, () => recorder.record());
358
+ });
359
+ });
360
+
361
+ // The mic, here: Voice starts a take, the same button (now its time) stops it, and it's a chip.
362
+ let take: Take | undefined;
363
+ let ticking: ReturnType<typeof setInterval> | undefined;
364
+ let stopWave: (() => void) | undefined;
365
+ const speakOn = async () => {
366
+ if (voice === undefined) return;
367
+ problem.textContent = '';
368
+ speak.disabled = true;
369
+ try {
370
+ take = await voice.start();
371
+ } catch (e) {
372
+ problem.textContent = messageOf(e);
373
+ return;
374
+ } finally {
375
+ speak.disabled = false;
376
+ }
377
+ const started = take.started;
378
+ // The time, the mic's live waveform, so it's plain it hears, and what a click does.
379
+ const time = element('span', { className: 'time' });
380
+ const wave = element('canvas', { className: 'wave' });
381
+ speak.replaceChildren('● ', time, wave, 'Stop');
382
+ stopWave = liveWaveform(wave, take.stream, { played: '#fff', rest: 'rgb(255 255 255 / 0.4)' });
383
+ const tick = () => (time.textContent = clock(Date.now() - started));
384
+ speak.classList.add('voice-on');
385
+ speak.title = 'Stop, and attach it';
386
+ tick();
387
+ ticking = setInterval(tick, 250);
388
+ mark();
389
+ };
390
+ const speakOff = async () => {
391
+ const ending = take;
392
+ if (ending === undefined) return;
393
+ take = undefined;
394
+ clearInterval(ticking);
395
+ stopWave?.();
396
+ stopWave = undefined;
397
+ speak.classList.remove('voice-on');
398
+ speak.textContent = 'Saving…';
399
+ try {
400
+ voices = [...voices, await ending.stop()];
401
+ } catch (e) {
402
+ problem.textContent = messageOf(e);
403
+ } finally {
404
+ speak.textContent = 'Voice';
405
+ speak.title = 'Record your voice, here';
406
+ showChips();
407
+ }
408
+ };
409
+ speak.addEventListener('click', () => void (take === undefined ? speakOn() : speakOff()));
410
+
411
+ // The task whose words are open to change, by ID, and the tasks as last read.
412
+ let editing: string | undefined;
413
+ // The Save of a listed task whose words were changed and not kept yet.
414
+ let edited: HTMLElement | undefined;
415
+ /** Tells the popup what leaving now would lose: a listed task's changed words, or a new one's, typed, attached or being spoken. */
416
+ const mark = () => context.unsaved(edited ?? (text.value.trim() !== '' || attachedCount() > 0 || take !== undefined ? add : undefined));
417
+ let shown = context.model;
418
+ /** Asks for a change to a listed task and shows the tasks as they are after; the panel's status says why not. */
419
+ const change = async (op: Change) => {
420
+ try {
421
+ editing = undefined;
422
+ render(await context.apply(op));
423
+ } catch {
424
+ render(shown);
425
+ }
426
+ };
427
+ /** A task's words in a field of their own: Save or Ctrl+Enter keeps them, Cancel or Esc leaves them. */
428
+ const editor = (t: Task) => {
429
+ // Marked a modal while it's open, so Esc is its and not the popup's.
430
+ const item = element('li', { className: 'editing' });
431
+ item.dataset['deicticModal'] = '';
432
+ const field = element('textarea', { value: t.text });
433
+ const save = element('button', { type: 'button', className: 'primary', textContent: 'Save' });
434
+ const leave = element('button', { type: 'button', textContent: 'Cancel' });
435
+ const buttons = element('div', { className: 'row' });
436
+ const right = element('span', { className: 'actions' });
437
+ right.append(leave, save);
438
+ buttons.append(element('code', { textContent: `#${t.id}` }), right);
439
+ item.append(field, buttons);
440
+ const done = () => {
441
+ editing = undefined;
442
+ render(shown);
443
+ };
444
+ const keep = () => {
445
+ if (field.value.trim() === '') return;
446
+ if (field.value.trim() === t.text) return done();
447
+ save.disabled = true;
448
+ void change({ edit: t.id, text: field.value });
449
+ };
450
+ field.addEventListener('input', () => {
451
+ save.disabled = field.value.trim() === '';
452
+ edited = field.value.trim() === t.text ? undefined : save;
453
+ mark();
454
+ });
455
+ field.addEventListener('keydown', (e) => {
456
+ if (e.key === 'Escape') {
457
+ e.stopPropagation();
458
+ done();
459
+ }
460
+ if (e.key !== 'Enter' || !(e.ctrlKey || e.metaKey)) return;
461
+ e.preventDefault();
462
+ keep();
463
+ });
464
+ save.addEventListener('click', keep);
465
+ leave.addEventListener('click', done);
466
+ queueMicrotask(() => field.focus());
467
+ return item;
468
+ };
469
+ const render = (model: Model) => {
470
+ shown = model;
471
+ // A task being changed is drawn again with its words as they were.
472
+ edited = undefined;
473
+ const { channel, link, tasks } = model;
474
+ const name =
475
+ link === undefined
476
+ ? element('span')
477
+ : element('a', { href: link, target: '_blank', rel: 'noreferrer', title: 'Open the channel on the board' });
478
+ name.textContent = `#${channel}`;
479
+ hint.replaceChildren('to ', name);
480
+ list.replaceChildren(
481
+ ...tasks.map((t) => {
482
+ if (t.id === editing && t.open) return editor(t);
483
+ const item = element('li');
484
+ const id = element('code', { textContent: `#${t.id}` });
485
+ const title = element('span', { className: 'title', textContent: t.title, title: t.title });
486
+ const state = element('span', { className: `state ${t.state}`, textContent: t.state });
487
+ if (t.link === undefined) {
488
+ item.append(id, title, state);
489
+ } else {
490
+ const to = element('a', { href: t.link, target: '_blank', rel: 'noreferrer', title: 'Open the task on the board' });
491
+ to.append(id);
492
+ item.append(to, title, state);
493
+ }
494
+ if (!t.open) return item;
495
+ const edit = element('button', { type: 'button', className: 'act', innerHTML: EDIT, title: 'Edit the task' });
496
+ edit.addEventListener('click', () => {
497
+ editing = t.id;
498
+ render(shown);
499
+ });
500
+ const remove = element('button', { type: 'button', className: 'act', innerHTML: DELETE, title: 'Delete the task' });
501
+ remove.addEventListener('click', () => {
502
+ remove.disabled = true;
503
+ void change({ remove: t.id });
504
+ });
505
+ item.append(edit, remove);
506
+ return item;
507
+ }),
508
+ );
509
+ able();
510
+ mark();
511
+ };
512
+
513
+ /** The shots to attach: each chip's PNG, kept as a shot, or a shot taken now where the box is ticked. */
514
+ const screenshotsOf = async (): Promise<Op['screenshots']> => {
515
+ if (shots === undefined) return undefined;
516
+ if (chips.length > 0) {
517
+ add.textContent = 'Uploading…';
518
+ return Promise.all(
519
+ chips.map(async ({ shot: taken, marks, png }) => ({
520
+ id: (await shots.upload(png, taken)).id,
521
+ box: taken.box,
522
+ ...(marks.length === 0 ? {} : { marks: marksOf(marks) }),
523
+ })),
524
+ );
525
+ }
526
+ // With a recording attached the box is away, so no shot is asked for.
527
+ if (!attach.checked || recordings.length > 0) return undefined;
528
+ add.textContent = 'Capturing…';
529
+ const { id, box } = await shots.capture(context.element);
530
+ return [{ id, box }];
531
+ };
532
+
533
+ const send = async () => {
534
+ if (busy || sending) return;
535
+ // A take still going is kept first: it may be all there is to send.
536
+ await speakOff();
537
+ if (able()) return;
538
+ sending = true;
539
+ able();
540
+ problem.textContent = '';
541
+ let screenshots: Op['screenshots'];
542
+ try {
543
+ screenshots = await screenshotsOf();
544
+ } catch (e) {
545
+ // Said under the shots, which stay as they are: untick the box, or remove the chips, to send without.
546
+ problem.textContent = messageOf(e);
547
+ add.textContent = 'Add task';
548
+ sending = false;
549
+ able();
550
+ return;
551
+ }
552
+ add.textContent = 'Add task';
553
+ const { element: el, elements } = context;
554
+ try {
555
+ const op: Op = {
556
+ text: text.value,
557
+ page: location.href,
558
+ viewport: { width: innerWidth, height: innerHeight },
559
+ ...(context.pointed === undefined
560
+ ? {}
561
+ : { pointed: { path: pathTo(context.pointed, el), html: context.pointed.outerHTML.slice(0, 4000) } }),
562
+ count: elements.length,
563
+ ...(screenshots === undefined ? {} : { screenshots }),
564
+ ...(recordings.length === 0
565
+ ? {}
566
+ : {
567
+ recordings: recordings.map(({ id, duration, url, voice: clips }) => ({
568
+ id,
569
+ duration,
570
+ replay: new URL(url, location.href).href,
571
+ ...(clips === undefined ? {} : { voice: clips.map((c) => c.id) }),
572
+ })),
573
+ }),
574
+ ...(voices.length === 0 ? {} : { voice: voices.map(({ id, duration }) => ({ id, duration })) }),
575
+ ...annotationsOf(notes),
576
+ };
577
+ const model = await context.apply(op);
578
+ render(model);
579
+ announce(model, op, context.target);
580
+ text.value = '';
581
+ reset();
582
+ } catch {
583
+ // The panel's status says why; what was typed and drawn stays, to send again.
584
+ }
585
+ sending = false;
586
+ able();
587
+ };
588
+
589
+ // Back from recording: what was kept, and the recording, or why there's none.
590
+ const back = context.resumed;
591
+ if (back !== undefined) {
592
+ const kept = back.state as Kept;
593
+ text.value = kept.text;
594
+ attach.checked = kept.attach;
595
+ chips = kept.drawn.map(toChip);
596
+ recordings = [...kept.recordings, ...(back.result === undefined ? [] : [back.result as Recording])];
597
+ voices = kept.voices;
598
+ if (back.problem !== undefined) problem.textContent = back.problem;
599
+ }
600
+
601
+ text.addEventListener('input', () => {
602
+ able();
603
+ mark();
604
+ });
605
+ text.addEventListener('keydown', (e) => {
606
+ if (e.key !== 'Enter' || !(e.ctrlKey || e.metaKey)) return;
607
+ e.preventDefault();
608
+ void send();
609
+ });
610
+ add.addEventListener('click', () => void send());
611
+ showChips();
612
+ render(context.model);
613
+ text.focus();
614
+ return () => {
615
+ aborts.abort();
616
+ take?.cancel();
617
+ clearInterval(ticking);
618
+ stopWave?.();
619
+ for (const c of chips) URL.revokeObjectURL(c.url);
620
+ };
621
+ },
622
+ });