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
@@ -4,8 +4,20 @@
4
4
  * and looks like nothing else here. Plugins only set `title`, as they would anywhere; the first hover moves
5
5
  * it to `data-tip`, so the browser's own stays away, and gives a textless control the title as its name.
6
6
  * The box is fixed in the root, so no panel's scrolling clips it.
7
+ *
8
+ * A control's keys (`aria-keyshortcuts`, as `Control+Z`) are in its tip too, each a keycap after the words, and a
9
+ * tip of their own for a control whose text already names it.
7
10
  */
8
11
  const DELAY = 300;
12
+ const KEYS = 'aria-keyshortcuts';
13
+ /** A key as its cap reads, where that isn't its name. */
14
+ const CAPS: Record<string, string> = { Control: 'Ctrl', Meta: '⌘', Escape: 'Esc', Backspace: '⌫', Enter: '↵' };
15
+
16
+ /** The first of a control's shortcuts, a cap for each key held together. */
17
+ function capsOf(el: Element): HTMLElement[] {
18
+ const [first = ''] = (el.getAttribute(KEYS) ?? '').split(' ');
19
+ return first === '' ? [] : first.split('+').map((key) => Object.assign(document.createElement('kbd'), { textContent: CAPS[key] ?? key }));
20
+ }
9
21
 
10
22
  export function tooltips(root: ShadowRoot): void {
11
23
  const tip = document.createElement('div');
@@ -21,15 +33,23 @@ export function tooltips(root: ShadowRoot): void {
21
33
  tip.classList.remove('shown');
22
34
  };
23
35
  const show = (el: Element) => {
24
- const text = el.getAttribute('data-tip');
25
- if (text === null || text === '' || !el.isConnected) return hide();
26
- tip.textContent = text;
36
+ const text = el.getAttribute('data-tip') ?? '';
37
+ const caps = capsOf(el);
38
+ if ((text === '' && caps.length === 0) || !el.isConnected) return hide();
39
+ tip.replaceChildren(text, ...caps);
27
40
  // Last in the root, over a modal or toolbar added since at the same z-index.
28
41
  if (tip !== root.lastElementChild) root.append(tip);
29
42
  tip.classList.add('shown');
30
- // Centred below it, kept on screen; above it where there's no room below.
31
43
  const r = el.getBoundingClientRect();
32
44
  const t = tip.getBoundingClientRect();
45
+ // Beside it in a toolbar that runs down the window's side: below would be over the next tool.
46
+ if (el.closest('[aria-orientation="vertical"]') !== null) {
47
+ const left = r.right + 6 + t.width > innerWidth - 8 ? r.left - 6 - t.width : r.right + 6;
48
+ const top = Math.max(8, Math.min(r.top + r.height / 2 - t.height / 2, innerHeight - t.height - 8));
49
+ Object.assign(tip.style, { left: `${left}px`, top: `${top}px` });
50
+ return;
51
+ }
52
+ // Centred below it, kept on screen; above it where there's no room below.
33
53
  const left = Math.max(8, Math.min(r.left + r.width / 2 - t.width / 2, innerWidth - t.width - 8));
34
54
  const top = r.bottom + 6 + t.height > innerHeight - 8 ? r.top - 6 - t.height : r.bottom + 6;
35
55
  Object.assign(tip.style, { left: `${left}px`, top: `${top}px` });
@@ -50,7 +70,7 @@ export function tooltips(root: ShadowRoot): void {
50
70
  if (!node.hasAttribute('aria-label') && (node.textContent ?? '').trim().length <= 1) node.setAttribute('aria-label', title);
51
71
  else node.setAttribute('aria-description', title);
52
72
  }
53
- if (node.hasAttribute('data-tip')) return node;
73
+ if (node.hasAttribute('data-tip') || node.hasAttribute(KEYS)) return node;
54
74
  }
55
75
  return undefined;
56
76
  };
@@ -0,0 +1,48 @@
1
+ /**
2
+ * The tray: what plugins made on the page that nothing has taken yet (a shot, a recording, a voice clip), for
3
+ * whatever takes it next, as a task does. Kept in the tab's session storage, as JSON, so it's still there on the
4
+ * tab's next page: a recording that carried on to it ends there, and what was taken before it is with it.
5
+ */
6
+ import type { Tray, TrayItem } from '#src/index.ts';
7
+
8
+ const KEY = 'deictic:tray';
9
+
10
+ function stored(): TrayItem[] {
11
+ try {
12
+ const kept: unknown = JSON.parse(sessionStorage.getItem(KEY) ?? '[]');
13
+ return Array.isArray(kept) ? (kept as TrayItem[]) : [];
14
+ } catch {
15
+ // Storage can be off, or hold something else: an empty tray, then.
16
+ return [];
17
+ }
18
+ }
19
+
20
+ export function tray(): Tray {
21
+ let items = stored();
22
+ const listeners = new Set<() => void>();
23
+ const changed = () => {
24
+ try {
25
+ sessionStorage.setItem(KEY, JSON.stringify(items));
26
+ } catch {
27
+ // Not kept past this page, then.
28
+ }
29
+ for (const listener of listeners) listener();
30
+ };
31
+ return {
32
+ list: (plugin) => items.filter((item) => plugin === undefined || item.plugin === plugin),
33
+ add: (plugin, id, value) => {
34
+ items = [...items.filter((item) => item.plugin !== plugin || item.id !== id), { plugin, id, value }];
35
+ changed();
36
+ },
37
+ remove: (plugin, id) => {
38
+ const left = items.filter((item) => item.plugin !== plugin || (id !== undefined && item.id !== id));
39
+ if (left.length === items.length) return;
40
+ items = left;
41
+ changed();
42
+ },
43
+ subscribe: (listener) => {
44
+ listeners.add(listener);
45
+ return () => listeners.delete(listener);
46
+ },
47
+ };
48
+ }
package/src/index.ts CHANGED
@@ -81,6 +81,30 @@ export interface Done<Record = unknown> {
81
81
 
82
82
  type Maybe<T> = T | Promise<T>;
83
83
 
84
+ /**
85
+ * Something of deictic's that went wrong, which nothing a person did in the page caused: a package the project
86
+ * hasn't installed, a plugin that failed. The dev server says it, and hands it to each plugin with a `problem`
87
+ * (tasks posts it to the board, for an agent to fix).
88
+ */
89
+ export interface Problem {
90
+ /**
91
+ * What tells it apart, and the same problem again from a new one: `packages:rrweb`, `setup:tailwind`. Never a
92
+ * time or an ID, so a problem that comes back is known.
93
+ */
94
+ key: string;
95
+ /**
96
+ * `setup`: the project's to fix, as `message` says (install a package, configure a plugin). `bug`: deictic's own, or
97
+ * a plugin's, to report where it's made, not to patch where it's installed.
98
+ */
99
+ kind: 'setup' | 'bug';
100
+ /** What went wrong, and the fix where there is one, as any problem message says it. */
101
+ message: string;
102
+ /** What more there is to look into: a stack. */
103
+ details?: string;
104
+ /** The plugin it's about, where it's one's: none for deictic's own. */
105
+ plugin?: string;
106
+ }
107
+
84
108
  /**
85
109
  * Each plugin's API to the others on the server, by name. A plugin adds its own, so `use` knows its type:
86
110
  * `declare module 'deictic' { interface ServerApis { screenshots: Screenshots } }`.
@@ -99,10 +123,12 @@ export interface ServerContext {
99
123
  /** The project's root, as an absolute path. */
100
124
  root: string;
101
125
  /**
102
- * Another plugin's API, as its `setup` answered it, or nothing when it isn't loaded. One the plugin
103
- * `needs` always is; any other may not be, so check.
126
+ * Another plugin's API, as its `setup` answered it, or nothing when it isn't loaded (or its `setup` failed,
127
+ * which is reported). One the plugin `needs` always is loaded; any other may not be, so check.
104
128
  */
105
129
  use: <Name extends string>(name: Name) => ApiOf<ServerApis, Name> | undefined;
130
+ /** Says a problem of the plugin's own, as deictic says its own: in the dev server's output, and to each plugin's `problem`. */
131
+ report: (problem: Problem) => void;
106
132
  }
107
133
 
108
134
  /**
@@ -148,12 +174,101 @@ export interface ServerPlugin<Model = unknown, Op = unknown, Record = unknown, A
148
174
  redo?(record: Record): Maybe<Record>;
149
175
  /** Says what an op does, for the history: `className: px-2 → px-4`. */
150
176
  describe?(op: Op, before: Model): string;
177
+ /**
178
+ * Hears each problem deictic or a plugin reports, on the server or in the page, after every `setup` has run: once
179
+ * per `key` while the dev server runs. Its own failure is only said, never reported again.
180
+ */
181
+ problem?(problem: Problem): Maybe<void>;
151
182
  }
152
183
 
153
184
  export const definePlugin = <Model, Op, Record = unknown, Api = unknown>(
154
185
  plugin: ServerPlugin<Model, Op, Record, Api>,
155
186
  ): ServerPlugin<Model, Op, Record, Api> => plugin;
156
187
 
188
+ /** Where something is in an element, in CSS pixels from its top left. */
189
+ export interface Point {
190
+ x: number;
191
+ y: number;
192
+ }
193
+
194
+ /** A spot on the page: a marked element, and a point in it (or beside it, for the nearest to a point no marked element is under). */
195
+ export interface Spot {
196
+ element: Element;
197
+ at: Point;
198
+ }
199
+
200
+ /**
201
+ * The tool panel: a bar docked to an edge of the window, which shows while a shortcut's keys are held and stays
202
+ * while it's in use. A plugin adds its tools to it from its page half's `setup`.
203
+ */
204
+ export interface ToolPanel {
205
+ /**
206
+ * Adds a plugin's controls, as one group, in the order plugins are loaded. Groups of one `section` sit together,
207
+ * with no line between them, after the groups of none: the screenshots', recordings', voice's and tasks' are
208
+ * `capture`. `last` puts a group at the end of its section, or after all the others for one of none. Each control is a button named by its `title`; its `aria-keyshortcuts`
209
+ * (`B`, `Control+Z`) is the key the panel hears for it, alone or with the keys held to point, and what its tooltip
210
+ * shows.
211
+ */
212
+ add: (group: HTMLElement, options?: { last?: boolean; section?: string }) => void;
213
+ /**
214
+ * Takes the page for a tool that works on it (drawing, picking a point): the tool that had it lets go first, and
215
+ * the panel stays shown. `left` is called when another takes it, or Esc or the pointer tool gives the page back.
216
+ * Answers what lets go of it, for the tool to call when it's done on its own.
217
+ */
218
+ hold: (left: () => void) => () => void;
219
+ /** Keeps the panel shown while something goes on that its controls end (a recording). Answers what ends that. */
220
+ keep: () => () => void;
221
+ /**
222
+ * Takes the page until a point of it is pressed, lighting what's under the pointer as pointing does: answers the
223
+ * spot, the marked element there or the nearest to it, or nothing where the page was given back first.
224
+ */
225
+ pick: () => Promise<Spot | undefined>;
226
+ }
227
+
228
+ /**
229
+ * The drawer: on the window's side, where what the plugins have for the page lands, a section each. It
230
+ * shows while a shortcut's keys are held and a section has something, and stays while it's in use.
231
+ */
232
+ export interface Drawer {
233
+ /**
234
+ * Adds a section, in the order added, or before the others with `first`. A section that has nothing to show sets
235
+ * its own `hidden`: the drawer stays away while every one is. Folded away, the drawer counts what the sections
236
+ * have: each says how many things it lists in `data-count`, and how many are unread in `data-unread`.
237
+ */
238
+ add: (section: HTMLElement, options?: { first?: boolean }) => void;
239
+ /**
240
+ * Opens one item in the drawer, in the sections' place, under `title` and a way back. The drawer stays while it's
241
+ * open. `closed` is called when it's left, by the way back, Esc, or another item opening. Answers what closes it.
242
+ */
243
+ show: (view: HTMLElement, options?: { title?: string; closed?: () => void }) => () => void;
244
+ /** Keeps the drawer up while something goes on in it; answers what lets it go. */
245
+ keep: () => () => void;
246
+ }
247
+
248
+ /** Something a plugin made on the page that nothing has taken yet: a shot, a recording, a voice clip. */
249
+ export interface TrayItem {
250
+ /** The plugin that made it. */
251
+ plugin: string;
252
+ /** Its ID, as that plugin has it. */
253
+ id: string;
254
+ /** What that plugin says of it, as JSON: the screenshots plugin's `Shot`, a `Recording`, a `Clip`. */
255
+ value: unknown;
256
+ }
257
+
258
+ /**
259
+ * What plugins made on the page from the tool panel, until something takes it: a task attaches what's there and
260
+ * removes it. Kept for the tab's session, so it's there on the tab's next page too.
261
+ */
262
+ export interface Tray {
263
+ /** What's kept, oldest first: all of it, or one plugin's. */
264
+ list: (plugin?: string) => TrayItem[];
265
+ add: (plugin: string, id: string, value: unknown) => void;
266
+ /** Takes one out, or all of a plugin's. */
267
+ remove: (plugin: string, id?: string) => void;
268
+ /** Hears every change from now on; answers what stops it. */
269
+ subscribe: (listener: () => void) => () => void;
270
+ }
271
+
157
272
  export interface ClientContext {
158
273
  /** The dev server's base URL, ending in `/`. */
159
274
  base: string;
@@ -165,12 +280,27 @@ export interface ClientContext {
165
280
  use: <Name extends string>(name: Name) => ApiOf<ClientApis, Name> | undefined;
166
281
  /** Asks one of this plugin's own routes, from the page: `request('shots', { method: 'POST', body: blob })`. */
167
282
  request: (route: string, init?: RequestInit) => Promise<Response>;
283
+ /** Says a problem of the plugin's own to the dev server, as the server half's `report` does: its `plugin` is this one. */
284
+ report: (problem: Problem) => void;
168
285
  /**
169
- * Opens the popup on an element at a plugin's tab, as its shortcut would: after a recording, say. An
170
- * element rendered again since stands in for one no longer on the page. False when there's no such tab
171
- * or no such element any more. Not from `setup`, which runs before the popup can open.
286
+ * Opens the popup on an element at a plugin's tab, as its shortcut would, or with `at` on a spot in it: the
287
+ * popup is then beside the spot, which is what's lit, and the tab's context has it. An element rendered again
288
+ * since stands in for one no longer on the page. False when there's no such tab or no such element any more, and
289
+ * while the open popup holds something unsaved, whose button then shakes. Not as `setup` runs, which is before the
290
+ * popup can open.
172
291
  */
173
- open: (plugin: string, element: Element) => boolean;
292
+ open: (plugin: string, element: Element, at?: Point) => boolean;
293
+ /** The tool panel, to add this plugin's tools to. */
294
+ tools: ToolPanel;
295
+ /** What was made from the tool panel and not taken yet. */
296
+ tray: Tray;
297
+ /** The drawer, to add this plugin's section to. */
298
+ drawer: Drawer;
299
+ /**
300
+ * Lights an element on the page as pointing at it does, for something of the overlay's own that stands for it (a
301
+ * task's row in the drawer, while the pointer is on it); none puts the light back as it was.
302
+ */
303
+ light: (element?: Element) => void;
174
304
  /**
175
305
  * Shows `items` in the lightbox over everything, at `at` (the first by default), stepping between them with
176
306
  * ← and →. With `from`, it grows out of that element (the chip clicked) and shrinks back into it on close.
@@ -211,12 +341,17 @@ export interface PanelContext<Model, Op> extends ClientContext {
211
341
  * dependency's, or in an open shadow root, as the popup's own on a page that shows it (`popupIn`).
212
342
  */
213
343
  pointed?: Element;
344
+ /** The spot in `element` the popup was opened on, where it was (the tool panel's task tool), not the element as a whole. */
345
+ at?: Point;
214
346
  /** Every element on the page from the same place in the source, the clicked one among them: the rows of a list. */
215
347
  elements: Element[];
216
348
  target: Target;
217
349
  model: Model;
218
- /** Applies an op on disk, and reads the model again. Rejects with the server's reason, which the panel also shows. */
219
- apply(op: Op): Promise<Model>;
350
+ /**
351
+ * Applies an op on disk, and reads the model again. Rejects with the server's reason, which the panel also shows.
352
+ * On another element with `on`, whose model is then the one answered: for an op that's about that one.
353
+ */
354
+ apply(op: Op, on?: Target): Promise<Model>;
220
355
  close: () => void;
221
356
  /**
222
357
  * Says there's something here that leaving would lose (words typed, a change tried on the page), and the button
@@ -261,6 +396,11 @@ export interface ClientPlugin<Model = unknown, Op = unknown, Api = unknown> {
261
396
  * done the popup opens at this tab with `resumed`, on the element where this page has it and without where not.
262
397
  */
263
398
  resume?(context: ClientContext): Promise<unknown> | undefined;
399
+ /**
400
+ * What it shows on the page while a shortcut's keys are held, and the page is dimmed to point on: the tasks
401
+ * plugin pins each task where its element is. Answers what takes it away.
402
+ */
403
+ pointing?(context: ClientContext): () => void;
264
404
  /** Draws into `panel`, inside deictic's shadow root. Returns what to undo on the page when the panel closes. */
265
405
  mount?: (panel: HTMLElement, context: PanelContext<Model, Op>) => (() => void) | void;
266
406
  }
@@ -1,20 +1,20 @@
1
1
  /**
2
2
  * Annotations in the page. `setup` keeps them (in the tab's session storage, per page) and shows them in a
3
- * layer of deictic's overlay that lets the pointer through; it answers the API other plugins `use`. The
4
- * panel is annotate mode: a toolbar, and while it's open a surface over the page that takes the pointer, so
5
- * drawing never clicks the page. Esc or Done leaves it; the marks stay.
3
+ * layer of deictic's overlay that lets the pointer through; it adds the tools that draw them to the tool panel,
4
+ * and answers the API other plugins `use`.
6
5
  *
7
6
  * The layer is deictic's, so a screenshot leaves it out with the rest of the overlay and draws the marks
8
7
  * itself from `svg(area)`: one way to draw them, wherever a shot is rendered. `edit` draws on a picture
9
- * instead, a shot most often, in the same toolbar over everything, and `flatten` makes the PNG of it.
8
+ * instead, a shot most often, with the same tools in a bar over everything, and `flatten` makes the PNG of it.
10
9
  */
10
+ import { controlFor, MOD } from '#src/client/dom.ts';
11
11
  import { defineClientPlugin } from '#src/index.ts';
12
12
 
13
- import { button, drawing, measurer, TOOLS_STYLE } from './draw.ts';
14
- import type { Annotation, AnnotationEvent, Annotations, Area, MarkSet, Picture, Point, Shape } from './index.ts';
13
+ import { drawing, measurer } from './draw.ts';
14
+ import type { Annotation, AnnotationEvent, Annotations, Area, Picture, Point, Shape } from './index.ts';
15
15
  import { type Measure, toSvg } from './svg.ts';
16
16
 
17
- /** Under deictic's outline and panels, over the page. */
17
+ /** Under deictic's light and its popup, over the page. */
18
18
  const LAYER = 2147483645;
19
19
  /** Over everything of deictic's, the popup too. */
20
20
  const TOP = 2147483647;
@@ -41,11 +41,19 @@ function fixed(z: number): HTMLDivElement {
41
41
  }
42
42
 
43
43
  const EDITOR_STYLE = `
44
+ .annotations-editor:focus { outline: none; }
44
45
  .annotations-editor { position: fixed; inset: 0; z-index: ${TOP}; box-sizing: border-box; padding: 16px; display: flex;
45
46
  flex-direction: column; align-items: center; justify-content: center; gap: 8px; background: rgb(0 0 0 / 0.75);
46
47
  font: 13px/1.4 system-ui, sans-serif; color: var(--deictic-fg); }
47
- .annotations-editor .bar { box-sizing: border-box; width: min(100%, 720px); padding: 6px 8px; border: 1px solid var(--deictic-line);
48
- border-radius: 8px; background: var(--deictic-bg); }
48
+ /* The tools as the tool panel has them, then Cancel and Save at its end. */
49
+ .annotations-editor .edit-bar { display: flex; align-items: center; gap: 6px; box-sizing: border-box; width: min(100%, 720px);
50
+ padding: 6px 8px; border: 1px solid var(--deictic-line); border-radius: 12px; background: var(--deictic-bg); }
51
+ .annotations-editor .tool-group { display: flex; align-items: center; gap: 4px; margin-right: auto; }
52
+ .annotations-editor button.icon { width: 28px; height: 26px; border-radius: 6px; color: var(--deictic-fg); }
53
+ .annotations-editor button.icon svg { width: 16px; height: 16px; }
54
+ .annotations-editor button[aria-pressed='true'], .annotations-editor button[aria-pressed='true']:hover {
55
+ color: var(--deictic-on-accent); background: var(--deictic-accent); }
56
+ .annotations-editor .edit-bar > button { padding: 3px 12px; border-radius: 6px; }
49
57
  .annotations-editor .stage { position: relative; line-height: 0; box-shadow: 0 12px 32px rgb(0 0 0 / 0.5); touch-action: none; }
50
58
  .annotations-editor img { display: block; max-width: calc(100vw - 32px); max-height: calc(100vh - 120px); user-select: none; }
51
59
  .annotations-editor .marks { position: absolute; inset: 0; pointer-events: none; }
@@ -64,8 +72,9 @@ const over = (picture: Picture, marks: readonly Annotation[], measure?: Measure)
64
72
  toSvg(marks, { x: 0, y: 0, width: picture.width, height: picture.height }, { x: 0, y: 0 }, measure);
65
73
 
66
74
  /**
67
- * The picture over everything, to draw on: the marks on Save, nothing on Cancel, Esc or `signal`. It holds
68
- * Esc while it's open (`data-deictic-modal`), so Esc leaves it, not the popup under it.
75
+ * The picture over everything, to draw on: the marks on Save, nothing on Cancel, Esc or `signal`. It's a modal
76
+ * (`data-deictic-modal`) while it's open, so Esc leaves it, not the popup under it, and the tool panel leaves the
77
+ * keys to it: its own bar hears them, as the panel hears its own.
69
78
  */
70
79
  function edit(
71
80
  root: ShadowRoot,
@@ -77,11 +86,10 @@ function edit(
77
86
  let marks = [...start];
78
87
  const backdrop = document.createElement('div');
79
88
  backdrop.className = 'annotations-editor';
80
- backdrop.dataset.deicticModal = '';
81
- const style = document.createElement('style');
82
- style.textContent = TOOLS_STYLE + EDITOR_STYLE;
89
+ backdrop.dataset['deicticModal'] = '';
83
90
  const bar = document.createElement('div');
84
- bar.className = 'bar';
91
+ bar.className = 'edit-bar';
92
+ bar.setAttribute('role', 'toolbar');
85
93
  const stage = document.createElement('div');
86
94
  stage.className = 'stage';
87
95
  const img = Object.assign(document.createElement('img'), { src: picture.url, alt: 'The screenshot', draggable: false });
@@ -96,7 +104,7 @@ function edit(
96
104
  svg?.setAttribute('width', '100%');
97
105
  svg?.setAttribute('height', '100%');
98
106
  };
99
- const { tools, hint } = drawing({
107
+ const { group, draw } = drawing({
100
108
  surface: stage,
101
109
  at: (e) => {
102
110
  const r = stage.getBoundingClientRect();
@@ -115,39 +123,71 @@ function edit(
115
123
  marks = [];
116
124
  show();
117
125
  },
126
+ // The picture is the tools' for as long as it's open: nothing takes it from them.
127
+ hold: () => () => {},
118
128
  });
129
+ draw();
130
+ // The focus is the editor's while it's open: a field under it (the task's words) would take what's typed.
131
+ const focused = root.activeElement;
132
+ backdrop.tabIndex = -1;
119
133
  const end = (result: Annotation[] | undefined) => {
120
134
  backdrop.remove();
135
+ if (focused instanceof HTMLElement) focused.focus();
121
136
  window.removeEventListener('keydown', onKey, true);
122
137
  signal?.removeEventListener('abort', cancel);
123
138
  resolve(result);
124
139
  };
125
140
  const cancel = () => end(undefined);
126
141
  const onKey = (e: KeyboardEvent) => {
127
- if (e.key !== 'Escape') return;
128
- e.stopPropagation();
129
- cancel();
142
+ // A note being typed has its keys to itself.
143
+ if (e.repeat || e.composedPath()[0] instanceof HTMLInputElement) return;
144
+ const control = controlFor(bar, e);
145
+ if (control === undefined) return;
146
+ e.preventDefault();
147
+ e.stopImmediatePropagation();
148
+ control.click();
130
149
  };
131
- const save = button('Save', 'Keep these marks', () => end(marks));
132
- save.classList.add('primary');
133
- const back = button('Cancel', 'Leave it as it was (Esc)', cancel);
134
- back.classList.add('end');
135
- tools.append(back, save);
136
- bar.append(tools, hint);
137
- backdrop.append(style, bar, stage);
150
+ const back = Object.assign(document.createElement('button'), { type: 'button', textContent: 'Cancel' });
151
+ back.setAttribute('aria-keyshortcuts', 'Escape');
152
+ back.addEventListener('click', cancel);
153
+ const save = Object.assign(document.createElement('button'), { type: 'button', className: 'primary', textContent: 'Save' });
154
+ save.setAttribute('aria-keyshortcuts', `${MOD}+Enter`);
155
+ save.addEventListener('click', () => end(marks));
156
+ bar.append(group, back, save);
157
+ backdrop.append(Object.assign(document.createElement('style'), { textContent: EDITOR_STYLE }), bar, stage);
138
158
  root.append(backdrop);
159
+ backdrop.focus();
139
160
  window.addEventListener('keydown', onKey, true);
140
161
  signal?.addEventListener('abort', cancel);
141
162
  show();
142
163
  });
143
164
  }
144
165
 
166
+ /** The picture with the marks drawn over it, as a PNG at the picture's own resolution. */
167
+ async function flatten(picture: Picture, marks: readonly Annotation[], measure: Measure): Promise<Blob> {
168
+ const base = await image(picture.url);
169
+ const canvas = document.createElement('canvas');
170
+ canvas.width = base.naturalWidth;
171
+ canvas.height = base.naturalHeight;
172
+ const ctx = canvas.getContext('2d');
173
+ if (ctx === null) throw new Error("annotations: this browser won't draw to a canvas");
174
+ ctx.drawImage(base, 0, 0);
175
+ if (marks.length > 0) {
176
+ // Shapes only, no `foreignObject`, so drawing the SVG leaves the canvas readable.
177
+ const svg = await image(`data:image/svg+xml;charset=utf-8,${encodeURIComponent(over(picture, marks, measure))}`);
178
+ ctx.drawImage(svg, 0, 0, canvas.width, canvas.height);
179
+ }
180
+ return new Promise((done, fail) =>
181
+ canvas.toBlob((blob) => (blob === null ? fail(new Error('annotations: the picture drew nothing')) : done(blob)), 'image/png'),
182
+ );
183
+ }
184
+
145
185
  /**
146
- * Marks shown on the page, in a layer of the overlay that lets the pointer through, redrawn as it
147
- * scrolls. With a `key`, kept in the tab's session storage under it, per page; without, kept by nothing.
186
+ * The page's marks, shown in a layer of the overlay that lets the pointer through, redrawn as it scrolls, and kept
187
+ * in the tab's session storage under `key`.
148
188
  */
149
- function marksOnPage(root: ShadowRoot, color: string, measure: Measure, key?: string): { marks: MarkSet; remove: () => void } {
150
- let list = key === undefined ? [] : stored(key);
189
+ function marksOnPage(root: ShadowRoot, color: string, measure: Measure, key: string): Omit<Annotations, 'draw' | 'edit' | 'flatten'> {
190
+ let list = stored(key);
151
191
  const listeners = new Set<(event: AnnotationEvent) => void>();
152
192
  const layer = fixed(LAYER);
153
193
  root.append(layer);
@@ -163,19 +203,17 @@ function marksOnPage(root: ShadowRoot, color: string, measure: Measure, key?: st
163
203
  window.addEventListener('scroll', redraw, { capture: true, passive: true });
164
204
  window.addEventListener('resize', redraw, { passive: true });
165
205
  const changed = (event: AnnotationEvent) => {
166
- if (key !== undefined) {
167
- try {
168
- sessionStorage.setItem(key, JSON.stringify(list));
169
- } catch {
170
- // Not kept past a reload, then; the page still shows them.
171
- }
206
+ try {
207
+ sessionStorage.setItem(key, JSON.stringify(list));
208
+ } catch {
209
+ // Not kept past a reload, then; the page still shows them.
172
210
  }
173
211
  draw();
174
212
  for (const listener of listeners) listener(event);
175
213
  };
176
214
  draw();
177
215
 
178
- const marks: MarkSet = {
216
+ return {
179
217
  list: () => [...list],
180
218
  add: (shape, given = color) => {
181
219
  const annotation = mark(shape, given);
@@ -188,6 +226,7 @@ function marksOnPage(root: ShadowRoot, color: string, measure: Measure, key?: st
188
226
  changed({ type: 'remove', id });
189
227
  },
190
228
  clear: () => {
229
+ if (list.length === 0) return;
191
230
  list = [];
192
231
  changed({ type: 'clear' });
193
232
  },
@@ -197,105 +236,40 @@ function marksOnPage(root: ShadowRoot, color: string, measure: Measure, key?: st
197
236
  },
198
237
  svg: (area) => (list.length === 0 ? undefined : toSvg(list, area, scroll(), measure)),
199
238
  };
200
- const remove = () => {
201
- window.removeEventListener('scroll', redraw, { capture: true });
202
- window.removeEventListener('resize', redraw);
203
- cancelAnimationFrame(frame);
204
- layer.remove();
205
- listeners.clear();
206
- };
207
- return { marks, remove };
208
- }
209
-
210
- /**
211
- * Drawing on the page: a surface over it, under the popup, and the toolbar. Where the page stays usable
212
- * (`usable`), the surface takes the pointer only while a drawing tool is picked.
213
- */
214
- function onPage(root: ShadowRoot, marks: MarkSet, color: string, usable: boolean) {
215
- const surface = fixed(LAYER + 1);
216
- surface.style.pointerEvents = usable ? 'none' : 'auto';
217
- const preview = fixed(0);
218
- surface.append(preview);
219
- root.append(surface);
220
- const { tools, hint } = drawing({
221
- surface,
222
- at: (e) => ({ x: e.clientX + scrollX, y: e.clientY + scrollY }),
223
- preview: (shape) => {
224
- preview.innerHTML = shape === undefined ? '' : toSvg([mark(shape, color)], viewport(), scroll());
225
- },
226
- add: (shape) => marks.add(shape),
227
- removeLast: () => {
228
- const last = marks.list().at(-1);
229
- if (last !== undefined) marks.remove(last.id);
230
- },
231
- clear: () => marks.clear(),
232
- ...(usable ? { drawing: (on: boolean) => (surface.style.pointerEvents = on ? 'auto' : 'none') } : {}),
233
- });
234
- return { tools, hint, surface };
235
- }
236
-
237
- /** The picture with the marks drawn over it, as a PNG at the picture's own resolution. */
238
- async function flatten(picture: Picture, marks: readonly Annotation[], measure: Measure): Promise<Blob> {
239
- const base = await image(picture.url);
240
- const canvas = document.createElement('canvas');
241
- canvas.width = base.naturalWidth;
242
- canvas.height = base.naturalHeight;
243
- const ctx = canvas.getContext('2d');
244
- if (ctx === null) throw new Error("annotations: this browser won't draw to a canvas");
245
- ctx.drawImage(base, 0, 0);
246
- if (marks.length > 0) {
247
- // Shapes only, no `foreignObject`, so drawing the SVG leaves the canvas readable.
248
- const svg = await image(`data:image/svg+xml;charset=utf-8,${encodeURIComponent(over(picture, marks, measure))}`);
249
- ctx.drawImage(svg, 0, 0, canvas.width, canvas.height);
250
- }
251
- return new Promise((done, fail) =>
252
- canvas.toBlob((blob) => (blob === null ? fail(new Error('annotations: the picture drew nothing')) : done(blob)), 'image/png'),
253
- );
254
239
  }
255
240
 
256
- export default defineClientPlugin<null, never, Annotations>({
241
+ export default defineClientPlugin<never, never, Annotations>({
257
242
  name: 'annotations',
258
- label: 'Annotate',
259
- setup: ({ root, options }) => {
243
+ setup: ({ root, options, tools }) => {
260
244
  const color = (options as { color: string }).color;
261
245
  const measure = measurer();
262
- const page = marksOnPage(root, color, measure, `deictic:annotations:${location.pathname}`);
246
+ const marks = marksOnPage(root, color, measure, `deictic:annotations:${location.pathname}`);
263
247
 
264
- const api: Annotations = {
265
- ...page.marks,
266
- edit: (picture, marks = [], { signal } = {}) => edit(root, picture, marks, { color, measure, ...(signal ? { signal } : {}) }),
267
- flatten: (picture, marks) => flatten(picture, marks, measure),
268
- tools: () => {
269
- // Its own marks, kept by nothing: they show while it's open, and go with it.
270
- const own = marksOnPage(root, color, measure);
271
- const { tools, surface } = onPage(root, own.marks, color, true);
272
- const element = document.createElement('div');
273
- element.append(Object.assign(document.createElement('style'), { textContent: TOOLS_STYLE }), tools);
274
- return {
275
- element,
276
- marks: own.marks,
277
- close: () => {
278
- surface.remove();
279
- own.remove();
280
- },
281
- };
248
+ // Over the page, under the popup: it takes the pointer only while a tool is picked.
249
+ const surface = fixed(LAYER + 1);
250
+ const preview = fixed(0);
251
+ surface.append(preview);
252
+ root.append(surface);
253
+ const { group, draw } = drawing({
254
+ surface,
255
+ at: (e) => ({ x: e.clientX + scrollX, y: e.clientY + scrollY }),
256
+ preview: (shape) => {
257
+ preview.innerHTML = shape === undefined ? '' : toSvg([mark(shape, color)], viewport(), scroll());
258
+ },
259
+ add: (shape) => marks.add(shape),
260
+ removeLast: () => {
261
+ const last = marks.list().at(-1);
262
+ if (last !== undefined) marks.remove(last.id);
282
263
  },
264
+ clear: () => marks.clear(),
265
+ hold: tools.hold,
266
+ });
267
+ tools.add(group);
268
+ return {
269
+ ...marks,
270
+ draw,
271
+ edit: (picture, start = [], { signal } = {}) => edit(root, picture, start, { color, measure, ...(signal ? { signal } : {}) }),
272
+ flatten: (picture, drawn) => flatten(picture, drawn, measure),
283
273
  };
284
- return api;
285
- },
286
-
287
- mount: (panel, { root, use, close, options }) => {
288
- const { color } = options as { color: string };
289
- const marks = use('annotations');
290
- if (marks === undefined) return;
291
- const style = document.createElement('style');
292
- style.textContent = TOOLS_STYLE;
293
- // Over the page while annotate mode is open: it takes the pointer, so drawing never clicks the page.
294
- const { tools, hint, surface } = onPage(root, marks, color, false);
295
- const done = button('Done', 'Leave annotate mode (Esc)', close);
296
- done.classList.add('primary', 'end');
297
- tools.append(done);
298
- panel.append(style, tools, hint);
299
- return () => surface.remove();
300
274
  },
301
275
  });