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
@@ -1,23 +1,41 @@
1
1
  /**
2
2
  * `deictic/client`: the overlay the page loads in dev. Hold a shortcut's keys (Alt, deictic's own,
3
- * by default) and point: the element under the pointer that the source marked (`tools/locate.ts`) is
4
- * outlined, with its tag, its place and how many elements share it. Press the shortcut's button to open
5
- * the popup there: a tab for each plugin with a panel, at the one used last, or at a plugin's own where
6
- * its shortcut was pressed.
3
+ * by default) and point: the page dims around the element under the pointer that the source marked
4
+ * (`tools/locate.ts`), which stays lit, with its tag, its place and how many elements share it (`lighting.ts`).
5
+ * Press the shortcut's button to open the popup beside it: a tab for each plugin with a panel, at the one used
6
+ * last, or at a plugin's own where its shortcut was pressed.
7
7
  *
8
8
  * Everything drawn sits in one shadow root, so neither the page's styles nor ours cross over. A
9
9
  * panel's undo and redo are its tab's on its element: the latest change that plugin made there.
10
10
  */
11
- import type { ClientContext, ClientPlugin, LightboxItem, PanelContext, Resumed, Standalone, Target } from '#src/index.ts';
11
+ import type {
12
+ ClientContext,
13
+ ClientPlugin,
14
+ LightboxItem,
15
+ PanelContext,
16
+ Point,
17
+ Problem,
18
+ Resumed,
19
+ Spot,
20
+ Standalone,
21
+ Target,
22
+ } from '#src/index.ts';
12
23
  import { gestureOf, type Shortcut } from '#src/shortcut.ts';
13
24
 
14
25
  import { dropAway, keepAway, keptAway } from './away.ts';
26
+ import { named, tell } from './component.ts';
27
+ import { ATTRIBUTE, div, nameOf, reduced, sameAs, targetOf } from './dom.ts';
28
+ import { drawerIn } from './drawer.ts';
15
29
  import { openLightbox } from './lightbox.ts';
30
+ import { CLEAR, type Lighting, spotlight, spotOf } from './lighting.ts';
16
31
  import { STYLE } from './style.ts';
32
+ import { toolPanel } from './tools.ts';
17
33
  import { tooltips } from './tooltip.ts';
34
+ import { tray } from './tray.ts';
18
35
 
19
36
  // For a plugin's page half that records the mic, or shows a clip: the waveforms the lightbox draws.
20
37
  export { drawBars, liveWaveform, peaksOf, type Colors } from './waveform.ts';
38
+ export { targetOf } from './dom.ts';
21
39
 
22
40
  export interface StartOptions {
23
41
  /** The dev server's base URL, ending in `/`. */
@@ -41,7 +59,6 @@ interface Tab {
41
59
  mount: Mount;
42
60
  }
43
61
 
44
- const ATTRIBUTE = 'data-deictic';
45
62
  /** An arrow pointing up and out: the link leaves the page for the editor. */
46
63
  const OUT = icon('M7 17 17 7M8 7h9v9', 2.25);
47
64
  // Drawn, as the arrows and the cross of a system's font differ in size and weight from one to the next.
@@ -54,15 +71,6 @@ const LAST = 'deictic:tab';
54
71
  const SLOW = 200;
55
72
  const STARTED = Symbol.for('deictic');
56
73
 
57
- export function targetOf(el: Element): Target | undefined {
58
- const at = el.getAttribute(ATTRIBUTE);
59
- const match = at === null ? null : /^(.*):(\d+):(\d+)$/.exec(at);
60
- if (match === null) return undefined;
61
- return { loc: { file: match[1]!, line: Number(match[2]), column: Number(match[3]) }, tag: el.localName };
62
- }
63
-
64
- const sameAs = (el: Element) => [...document.querySelectorAll(`[${ATTRIBUTE}="${CSS.escape(el.getAttribute(ATTRIBUTE) ?? '')}"]`)];
65
-
66
74
  /** The nearest marked element around `el`, out through shadow roots to their hosts. */
67
75
  function markedAround(el: Element): Element | undefined {
68
76
  for (let at: Element | undefined = el; at !== undefined;) {
@@ -74,13 +82,10 @@ function markedAround(el: Element): Element | undefined {
74
82
  return undefined;
75
83
  }
76
84
 
77
- /** `div.panel > button.tab`: the element as a selector, the deepest classes only, for a person to tell it by. */
78
85
  function icon(path: string, width = 2): string {
79
86
  return `<svg viewBox="0 0 24 24" width="12" height="12" fill="none" stroke="currentColor" stroke-width="${width}" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="${path}"/></svg>`;
80
87
  }
81
88
 
82
- const nameOf = (el: Element) => `${el.localName}${[...el.classList].map((c) => `.${c}`).join('')}`;
83
-
84
89
  /** What the overlay keeps once started, for `popupIn` to show the popup with. */
85
90
  interface Started {
86
91
  base: string;
@@ -151,7 +156,6 @@ export function popupIn(container: Element, { tab: name, element }: { tab?: stri
151
156
  remember,
152
157
  element,
153
158
  target,
154
- pinned: true,
155
159
  onClose: (shown, leaving) => {
156
160
  panel = undefined;
157
161
  // Closed by its own × or Esc: open again, as the page is there to show it. Leaving for `away` comes back by `reopen`.
@@ -197,13 +201,22 @@ export function start({ base, shortcut: own, plugins: given, server }: StartOpti
197
201
  const root = host.attachShadow({ mode: 'open' });
198
202
  const style = document.createElement('style');
199
203
  style.textContent = STYLE;
200
- const box = div('box');
201
- const label = div('label');
202
- box.append(label);
203
- // What's under the pointer, outlined inside the marked element around it when they differ.
204
- const inner = div('box inner');
205
- root.append(style, box, inner);
204
+ root.append(style);
206
205
  tooltips(root);
206
+ const lighting = spotlight(root);
207
+ const kept = tray();
208
+ // A tool has the page to pick a point on (`pick`): what's under the pointer is lit as while the keys are held.
209
+ let picking = false;
210
+ const tools = toolPanel(root, {
211
+ spotAt: (x, y) => spotAt(x, y),
212
+ aim: (on) => {
213
+ picking = on;
214
+ if (on) glance(true);
215
+ else if (held.length === 0) hide();
216
+ },
217
+ });
218
+ // Out from under the tool panel, where that's on the window's right.
219
+ const drawer = drawerIn(root, { crowded: () => tools.edge() === 'right' });
207
220
  // What's typed in the popup is the popup's: the page hears a key from in here as one on the host, not in a field,
208
221
  // so its own shortcuts (a "/" to search, a "j" to step) would run on a task's words. Ours listen while capturing.
209
222
  for (const type of ['keydown', 'keypress', 'keyup'] as const) host.addEventListener(type, (e) => e.stopPropagation());
@@ -214,6 +227,13 @@ export function start({ base, shortcut: own, plugins: given, server }: StartOpti
214
227
  const apis = new Map<string, unknown>();
215
228
  const send: Send =
216
229
  server === undefined ? (url, init) => fetch(url, init) : async (url, init) => server(new Request(new URL(url, location.href), init));
230
+ // To the dev server, which says it and hands it on (to the board, with tasks). Whether it got there is no one's to
231
+ // know in the page: it's said in the console too.
232
+ const report = (problem: Problem) => {
233
+ console.error(`deictic${problem.plugin === undefined ? '' : `/${problem.plugin}`}: ${problem.message}`);
234
+ const init = { method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify(problem) };
235
+ send(`${base}__deictic/problem`, init).catch(() => {});
236
+ };
217
237
  const optionsOf = new Map(given.map(({ plugin, options }) => [plugin.name, options]));
218
238
  const contextOf = (name: string): ClientContext => ({
219
239
  base,
@@ -221,8 +241,17 @@ export function start({ base, shortcut: own, plugins: given, server }: StartOpti
221
241
  options: optionsOf.get(name),
222
242
  use: (other) => apis.get(other) as never,
223
243
  request: (route, init) => send(`${base}__deictic/plugins/${name}/${route}`, init),
224
- open: (plugin, element) => openAt(plugin, element),
244
+ report: (problem) => report({ ...problem, plugin: name }),
245
+ // Not from under something unsaved, which a press on the page wouldn't close either: its button shakes.
246
+ open: (plugin, element, at) => panel?.guard() !== true && openAt(plugin, element, at),
225
247
  lightbox: (items, options) => openLightbox(root, items, options),
248
+ drawer,
249
+ light: (element) => {
250
+ lit = element?.isConnected === true ? element : undefined;
251
+ relight();
252
+ },
253
+ tools,
254
+ tray: kept,
226
255
  });
227
256
  for (const { plugin } of given) {
228
257
  if (plugin.setup === undefined) continue;
@@ -230,7 +259,7 @@ export function start({ base, shortcut: own, plugins: given, server }: StartOpti
230
259
  apis.set(plugin.name, plugin.setup(contextOf(plugin.name)));
231
260
  } catch (e) {
232
261
  // One plugin's failure is no reason to take the others down with it.
233
- console.error(`deictic: ${plugin.name} failed to set up`, e);
262
+ report(failed(plugin.name, 'set up in the page, so it does nothing there until the page loads again', e));
234
263
  }
235
264
  }
236
265
  const tabs: Tab[] = given.flatMap(({ plugin }) => (plugin.mount === undefined ? [] : [{ plugin, mount: plugin.mount }]));
@@ -261,11 +290,11 @@ export function start({ base, shortcut: own, plugins: given, server }: StartOpti
261
290
  // What opens the popup, and at which tab: deictic's own shortcut at the one used last, each
262
291
  // plugin's at its own.
263
292
  const plugins = [
264
- ...(own === undefined || tabs.length === 0 ? [] : [{ shortcut: own, gesture: gestureOf(own), tab: last }]),
293
+ ...(own === undefined || tabs.length === 0 ? [] : [{ shortcut: own, gesture: gestureOf(own), tab: last, named: false }]),
265
294
  ...given.flatMap(({ plugin, shortcut }) => {
266
295
  if (shortcut === undefined) return [];
267
296
  const tab = tabs.find((t) => t.plugin === plugin);
268
- if (tab !== undefined) return [{ shortcut, gesture: gestureOf(shortcut), tab: () => tab }];
297
+ if (tab !== undefined) return [{ shortcut, gesture: gestureOf(shortcut), tab: () => tab, named: true }];
269
298
  console.error(`deictic: ${plugin.name} has a shortcut, but its page half has no \`mount\` to draw a tab with`);
270
299
  return [];
271
300
  }),
@@ -273,7 +302,7 @@ export function start({ base, shortcut: own, plugins: given, server }: StartOpti
273
302
 
274
303
  let pointer: { x: number; y: number } | undefined;
275
304
  let frame = 0;
276
- let panel: { close: () => void; unsaved: () => boolean } | undefined;
305
+ let panel: ReturnType<typeof openPanel> | undefined;
277
306
 
278
307
  /** The plugins whose keys are held: several, where they share keys and differ by button. */
279
308
  const heldBy = (e: MouseEvent | KeyboardEvent) => plugins.filter((p) => p.gesture.held(e));
@@ -281,10 +310,11 @@ export function start({ base, shortcut: own, plugins: given, server }: StartOpti
281
310
  const pressedBy = (e: MouseEvent) => plugins.find((p) => p.gesture.held(e) && e.button === p.gesture.button);
282
311
  /**
283
312
  * The marked element at a point, and what's under the pointer inside it where that differs: down through
284
- * open shadow roots but the overlay's own, as a page showing the popup (`popupIn`) has it in one.
313
+ * open shadow roots but the overlay's own, as a page showing the popup (`popupIn`) has it in one. `through` the
314
+ * overlay, where it's the overlay that has the pointer: a tool's surface, while a point is picked.
285
315
  */
286
- const hitAt = (x: number, y: number) => {
287
- let under = document.elementFromPoint(x, y) ?? undefined;
316
+ const hitAt = (x: number, y: number, through = false) => {
317
+ let under = (through ? document.elementsFromPoint(x, y).find((el) => el !== host) : document.elementFromPoint(x, y)) ?? undefined;
288
318
  while (under !== undefined && under !== host && under.shadowRoot !== null) {
289
319
  const inside = under.shadowRoot.elementFromPoint(x, y);
290
320
  if (inside === null || inside === under) break;
@@ -293,39 +323,90 @@ export function start({ base, shortcut: own, plugins: given, server }: StartOpti
293
323
  const el = under === undefined || under === host ? undefined : markedAround(under);
294
324
  return el === undefined ? undefined : { el, pointed: under === el ? undefined : under };
295
325
  };
326
+ // The plugins whose keys are held now, as the last event said: what's pointed at is shown while there are any.
327
+ let held: typeof plugins = [];
328
+ // What plugins show on the page while it's pointed on (`pointing`), each with what takes it away.
329
+ let glanced: (() => void)[] | undefined;
330
+ const glance = (on: boolean) => {
331
+ if (on === (glanced !== undefined)) return;
332
+ for (const end of glanced ?? []) end();
333
+ glanced = !on
334
+ ? undefined
335
+ : given.flatMap(({ plugin }) => {
336
+ try {
337
+ return plugin.pointing === undefined ? [] : [plugin.pointing(contextOf(plugin.name))];
338
+ } catch (e) {
339
+ report(failed(plugin.name, 'show what it has on the page as it was pointed at', e));
340
+ return [];
341
+ }
342
+ });
343
+ };
344
+ // An element a plugin lights from the overlay's own UI (`context.light`): lit while it says so, whatever the keys.
345
+ let lit: Element | undefined;
346
+ const relight = () => {
347
+ const target = lit === undefined ? undefined : targetOf(lit);
348
+ if (lit !== undefined && target !== undefined) return lighting.point({ el: lit, target, names: [], count: sameAs(lit).length });
349
+ if (held.length > 0 || picking) return show(false);
350
+ lighting.hide();
351
+ };
296
352
  const hide = () => {
297
- box.classList.remove('shown');
298
- inner.classList.remove('shown');
353
+ held = [];
354
+ tools.point(false);
355
+ drawer.point(false);
356
+ glance(false);
357
+ relight();
358
+ };
359
+ /** The marked element at a point of the window, or the nearest to it where none is under it, and the point in it. */
360
+ const spotAt = (x: number, y: number): Spot | undefined => {
361
+ const away = (el: Element) => {
362
+ const r = el.getBoundingClientRect();
363
+ return r.width === 0 || r.height === 0
364
+ ? Infinity
365
+ : Math.hypot(Math.max(r.left - x, 0, x - r.right), Math.max(r.top - y, 0, y - r.bottom));
366
+ };
367
+ const nearest = () => {
368
+ let found: { el: Element; far: number } | undefined;
369
+ for (const el of document.querySelectorAll(`[${ATTRIBUTE}]`)) {
370
+ const far = away(el);
371
+ // The later of two as near is the inner one, or the one drawn over.
372
+ if (far !== Infinity && (found === undefined || far <= found.far)) found = { el, far };
373
+ }
374
+ return found?.el;
375
+ };
376
+ const element = hitAt(x, y, true)?.el ?? nearest();
377
+ if (element === undefined) return undefined;
378
+ const r = element.getBoundingClientRect();
379
+ return { element, at: { x: Math.round(x - r.left), y: Math.round(y - r.top) } };
299
380
  };
300
- const show = (held: typeof plugins) => {
381
+ const show = (glide: boolean) => {
301
382
  frame = 0;
302
- const hit = pointer === undefined ? undefined : hitAt(pointer.x, pointer.y);
383
+ // On something of ours that takes the pointer (a task's pin, the tool panel): the light stays where it was, as
384
+ // the pointer is on its way to a press there, not to another element.
385
+ if (pointer !== undefined && !picking && held.length > 0 && document.elementFromPoint(pointer.x, pointer.y) === host) {
386
+ if (lit !== undefined) relight();
387
+ return;
388
+ }
389
+ const hit = pointer === undefined || (held.length === 0 && !picking) ? undefined : hitAt(pointer.x, pointer.y, picking);
303
390
  const el = hit?.el;
304
391
  const target = el === undefined ? undefined : targetOf(el);
305
- if (el === undefined || target === undefined) return hide();
306
- place(box, el.getBoundingClientRect());
307
- if (hit?.pointed === undefined) {
308
- inner.classList.remove('shown');
309
- } else {
310
- place(inner, hit.pointed.getBoundingClientRect());
311
- inner.classList.add('shown');
312
- }
313
- const count = sameAs(el).length;
314
- // The button, where it isn't the left: what the keys alone don't tell apart.
315
- const names = held.map(({ tab, shortcut }) => {
392
+ if (el === undefined || target === undefined) return lighting.hide();
393
+ // A plugin's own shortcut says its tab, and its button where it isn't the left, which the keys alone don't tell
394
+ // apart. deictic's own says nothing: it opens whichever tab was used last.
395
+ const names = held.flatMap(({ tab, shortcut, named }) => {
396
+ if (!named) return [];
316
397
  const { plugin } = tab();
317
398
  const name = plugin.label ?? plugin.name;
318
- return shortcut.button === 'left' ? name : `${name} (${shortcut.button})`;
399
+ return [shortcut.button === 'left' ? name : `${name} (${shortcut.button})`];
319
400
  });
320
- const within = hit?.pointed === undefined ? '' : ` › ${nameOf(hit.pointed)}`;
321
- label.textContent = `${names.join(' / ')} · <${target.tag}> ${target.loc.file}:${target.loc.line}${count > 1 ? ` · ×${count}` : ''}${within}`;
322
- label.classList.toggle('below', el.getBoundingClientRect().top < 24);
323
- box.classList.add('shown');
401
+ lighting.point({ el, ...(hit?.pointed === undefined ? {} : { pointed: hit.pointed }), target, names, count: sameAs(el).length }, glide);
324
402
  };
325
403
  const update = (e: MouseEvent | KeyboardEvent) => {
326
- const held = heldBy(e);
327
- if (held.length === 0) return hide();
328
- if (frame === 0) frame = requestAnimationFrame(() => show(held));
404
+ held = heldBy(e);
405
+ if (held.length === 0 && !picking) return hide();
406
+ tools.point(held.length > 0);
407
+ drawer.point(held.length > 0);
408
+ glance(true);
409
+ if (frame === 0) frame = requestAnimationFrame(() => show(true));
329
410
  };
330
411
 
331
412
  window.addEventListener(
@@ -339,6 +420,14 @@ export function start({ base, shortcut: own, plugins: given, server }: StartOpti
339
420
  window.addEventListener('keydown', update, true);
340
421
  window.addEventListener('keyup', update, true);
341
422
  window.addEventListener('blur', hide);
423
+ // The page scrolled under the pointer: another element is under it now, or the same one elsewhere.
424
+ window.addEventListener(
425
+ 'scroll',
426
+ () => {
427
+ if ((held.length > 0 || picking) && frame === 0) frame = requestAnimationFrame(() => show(false));
428
+ },
429
+ { capture: true, passive: true },
430
+ );
342
431
  let press = {};
343
432
  let opened: object | undefined;
344
433
  // Alt-click on a link would otherwise download it, and on a field focus it.
@@ -368,7 +457,7 @@ export function start({ base, shortcut: own, plugins: given, server }: StartOpti
368
457
  if (panel?.unsaved() === true) return;
369
458
  openTab(found.tab(), hit.el, target, undefined, hit.pointed);
370
459
  };
371
- const openTab = (tab: Tab, el: Element, target: Target, resumed?: Resumed, pointed?: Element) => {
460
+ const openTab = (tab: Tab, el: Element, target: Target, resumed?: Resumed, pointed?: Element, at?: Point) => {
372
461
  hide();
373
462
  panel?.close();
374
463
  panel = openPanel({
@@ -381,14 +470,23 @@ export function start({ base, shortcut: own, plugins: given, server }: StartOpti
381
470
  remember,
382
471
  element: el,
383
472
  ...(pointed === undefined ? {} : { pointed }),
473
+ ...(at === undefined ? {} : { at }),
384
474
  target,
475
+ lighting,
385
476
  onClose: () => (panel = undefined),
386
477
  ...(resumed === undefined ? {} : { resumed }),
387
478
  reopen: (again, element, kept) => {
388
479
  // Rendered again while away, or gone from the page, which moved on: the popup comes back all the same, on
389
480
  // the element as it was left, since what it kept (a recording) is about it wherever the page is now.
390
481
  const found = element.isConnected ? element : (sameAs(element)[0] ?? element);
391
- openTab(again, found, targetOf(found) ?? target, kept, pointed?.isConnected === true && found === element ? pointed : undefined);
482
+ openTab(
483
+ again,
484
+ found,
485
+ targetOf(found) ?? target,
486
+ kept,
487
+ pointed?.isConnected === true && found === element ? pointed : undefined,
488
+ at,
489
+ );
392
490
  },
393
491
  });
394
492
  };
@@ -404,19 +502,21 @@ export function start({ base, shortcut: own, plugins: given, server }: StartOpti
404
502
  const { loc, tag } = away.target;
405
503
  const absent = document.createElement(tag);
406
504
  absent.setAttribute(ATTRIBUTE, `${loc.file}:${loc.line}:${loc.column}`);
407
- openTab(tab, sameAs(absent).find((el) => el.localName === tag) ?? absent, away.target, resumed);
505
+ const found = sameAs(absent).find((el) => el.localName === tag);
506
+ // The spot only where this page has the element: there's nowhere for it otherwise.
507
+ openTab(tab, found ?? absent, away.target, resumed, undefined, found === undefined ? undefined : away.at);
408
508
  };
409
509
  task.then(
410
510
  (result) => back({ state: away.state, result }),
411
511
  (e: unknown) => back({ state: away.state, problem: messageOf(e) }),
412
512
  );
413
513
  });
414
- function openAt(name: string, given: Element): boolean {
514
+ function openAt(name: string, given: Element, at?: Point): boolean {
415
515
  const tab = tabs.find((t) => t.plugin.name === name);
416
516
  const el = given.isConnected ? given : (sameAs(given)[0] ?? undefined);
417
517
  const target = el === undefined ? undefined : targetOf(el);
418
518
  if (tab === undefined || el === undefined || target === undefined) return false;
419
- openTab(tab, el, target);
519
+ openTab(tab, el, target, undefined, undefined, at);
420
520
  return true;
421
521
  }
422
522
  for (const type of ['click', 'auxclick', 'contextmenu'] as const) window.addEventListener(type, pressed, true);
@@ -435,9 +535,14 @@ interface PanelOptions {
435
535
  element: Element;
436
536
  /** What was under the pointer inside `element`, where that differs. */
437
537
  pointed?: Element;
538
+ /** The spot in `element` it's opened on, where it's one: what's lit, and what it's beside. */
539
+ at?: Point;
438
540
  target: Target;
439
- /** Shown where the page put it (`popupIn`), not over the page beside the element; Esc leaves it alone. */
440
- pinned?: boolean;
541
+ /**
542
+ * Over the page, beside the element: what lights the element there. Without it the popup is shown where the
543
+ * page put it (`popupIn`): nothing on the page is lit, and Esc leaves it alone.
544
+ */
545
+ lighting?: Lighting;
441
546
  /** Called as it closes: the tab it showed, and whether it's leaving for a tab's `away`, to come back by `reopen`. */
442
547
  onClose: (shown: Tab, leaving: boolean) => void;
443
548
  /** What the first tab's mount gets, coming back from `away`. */
@@ -456,12 +561,19 @@ function openPanel({
456
561
  remember,
457
562
  element,
458
563
  pointed,
564
+ at: spot,
459
565
  target,
460
- pinned = false,
566
+ lighting,
461
567
  onClose,
462
568
  resumed: coming,
463
569
  reopen,
464
- }: PanelOptions): { close: () => void; unsaved: () => boolean } {
570
+ }: PanelOptions): {
571
+ close: () => void;
572
+ unsaved: () => boolean;
573
+ /** Whether it holds something unsaved, whose button it then shakes: what asks before opening another over it. */
574
+ guard: () => boolean;
575
+ } {
576
+ const pinned = lighting === undefined;
465
577
  // Given to the first tab's first mount only: another tab, or a remount after an undo, starts afresh.
466
578
  let resumed = coming;
467
579
  const panel = div('panel');
@@ -473,8 +585,10 @@ function openPanel({
473
585
  where.className = 'where';
474
586
  const file = document.createElement('span');
475
587
  file.textContent = `${target.loc.file}:${target.loc.line}`;
588
+ // What it is too, as the caption said it while pointing, which is gone once the popup is open.
589
+ const tag = Object.assign(document.createElement('span'), { className: 'tag', textContent: tell(named(element)) });
476
590
  // An arrow out, as it leaves the page for the editor; its title is the overlay's tooltip (`tooltip.ts`).
477
- where.append(file);
591
+ where.append(tag, file);
478
592
  where.insertAdjacentHTML('beforeend', OUT);
479
593
  where.title = 'Open in editor';
480
594
  where.setAttribute('aria-label', `Open ${file.textContent} in the editor`);
@@ -497,10 +611,18 @@ function openPanel({
497
611
  el.addEventListener('click', () => show(tab));
498
612
  return { tab, el };
499
613
  });
500
- close.classList.add('close');
501
- head.append(...buttons.map((b) => b.el), close);
614
+ // What marks the shown tab, behind it, and slides to the next one shown.
615
+ const thumb = div('thumb');
616
+ const slide = (to: HTMLElement) => {
617
+ Object.assign(thumb.style, { left: `${to.offsetLeft}px`, width: `${to.offsetWidth}px` });
618
+ // Laid out where it is before it may move: the first place is no slide from the head's corner.
619
+ void thumb.offsetWidth;
620
+ thumb.classList.add('moves');
621
+ };
622
+ close.classList.add('end');
623
+ head.append(thumb, ...buttons.map((b) => b.el), close);
502
624
  bar.append(where, undo, redo);
503
- // Back from `away` on a page that doesn't have the element: said, as nothing on it is outlined.
625
+ // Back from `away` on a page that doesn't have the element: said, as nothing on it is lit.
504
626
  if (!element.isConnected)
505
627
  where.after(Object.assign(document.createElement('span'), { className: 'absent', textContent: 'not on this page' }));
506
628
  const body = div('body');
@@ -509,8 +631,9 @@ function openPanel({
509
631
  panel.append(head, bar, body, status);
510
632
  root.append(panel);
511
633
  if (pinned) panel.classList.add('pinned');
512
- const release = pinned ? undefined : keepInView(panel, body, head, element);
513
- const unmark = pinned ? undefined : outline(root, element);
634
+ const release = pinned ? undefined : keepInView(panel, body, head, element, spot);
635
+ // Its element, or the spot in it, lit for as long as it's open.
636
+ const unmark = lighting?.hold(element, panel, spot);
514
637
  if (!pinned) window.dispatchEvent(new CustomEvent<PopupOpened>(POPUP_OPENED, { detail: { element, tab: first.plugin.name } }));
515
638
 
516
639
  let cleanup: (() => void) | void;
@@ -631,117 +754,101 @@ function openPanel({
631
754
  say('');
632
755
  const back = current === first ? resumed : undefined;
633
756
  resumed = undefined;
634
- cleanup = mount(body, {
635
- ...contextOf(plugin.name),
636
- ...(back === undefined ? {} : { resumed: back }),
637
- away: (state, task) => {
638
- const tab = current;
639
- done(true);
640
- // For the tab's next page too, should this one go before the task is done. Not one a page pinned: that's the page's own.
641
- if (!pinned) keepAway({ tab: tab.plugin.name, target, state });
642
- const back = (resumed: Resumed) => {
643
- if (!pinned) dropAway();
644
- reopen(tab, element, resumed);
645
- };
646
- void task().then(
647
- (result) => back({ state, result }),
648
- (e: unknown) => back({ state, problem: messageOf(e) }),
649
- );
650
- },
651
- element,
652
- ...(pointed === undefined ? {} : { pointed }),
653
- elements: sameAs(element),
654
- target,
655
- model,
656
- apply: async (op) => {
657
- say('Writing…');
658
- try {
659
- const result = await post<{ model: unknown }>('apply', { plugin: plugin.name, target, op });
660
- say('');
661
- void steps();
662
- return result.model;
663
- } catch (e) {
664
- say(messageOf(e), true);
665
- throw e;
666
- }
667
- },
668
- close: () => done(),
669
- unsaved: (action) => {
670
- if (read === reads) holding = action;
671
- },
672
- });
757
+ try {
758
+ cleanup = mount(body, {
759
+ ...contextOf(plugin.name),
760
+ ...(back === undefined ? {} : { resumed: back }),
761
+ away: (state, task) => {
762
+ const tab = current;
763
+ done(true);
764
+ // For the tab's next page too, should this one go before the task is done. Not one a page pinned: that's the page's own.
765
+ if (!pinned) keepAway({ tab: tab.plugin.name, target, state, ...(spot === undefined ? {} : { at: spot }) });
766
+ const back = (resumed: Resumed) => {
767
+ if (!pinned) dropAway();
768
+ reopen(tab, element, resumed);
769
+ };
770
+ void task().then(
771
+ (result) => back({ state, result }),
772
+ (e: unknown) => back({ state, problem: messageOf(e) }),
773
+ );
774
+ },
775
+ element,
776
+ ...(pointed === undefined ? {} : { pointed }),
777
+ ...(spot === undefined ? {} : { at: spot }),
778
+ elements: sameAs(element),
779
+ target,
780
+ model,
781
+ apply: async (op, on = target) => {
782
+ say('Writing…');
783
+ try {
784
+ const result = await post<{ model: unknown }>('apply', { plugin: plugin.name, target: on, op });
785
+ say('');
786
+ void steps();
787
+ return result.model;
788
+ } catch (e) {
789
+ say(messageOf(e), true);
790
+ throw e;
791
+ }
792
+ },
793
+ close: () => done(),
794
+ unsaved: (action) => {
795
+ if (read === reads) holding = action;
796
+ },
797
+ });
798
+ } catch (e) {
799
+ say(messageOf(e), true);
800
+ contextOf(plugin.name).report(failed(plugin.name, 'show its panel', e));
801
+ }
673
802
  };
674
803
  const show = (tab: Tab) => {
675
804
  current = tab;
676
- for (const b of buttons) b.el.setAttribute('aria-selected', String(b.tab === tab));
805
+ for (const b of buttons) {
806
+ b.el.setAttribute('aria-selected', String(b.tab === tab));
807
+ if (b.tab === tab) slide(b.el);
808
+ }
677
809
  remember(tab);
678
810
  void steps();
679
811
  void load();
680
812
  };
681
813
  show(first);
682
- return { close: done, unsaved: () => holding !== undefined };
814
+ const guard = () => {
815
+ if (holding !== undefined) shake(holding);
816
+ return holding !== undefined;
817
+ };
818
+ return { close: done, unsaved: () => holding !== undefined, guard };
683
819
  }
684
820
 
685
821
  /** Shakes a panel's button, in view: what a press outside does where closing would lose something. A blink where motion is reduced. */
686
822
  function shake(el: HTMLElement): void {
687
823
  el.scrollIntoView({ block: 'nearest' });
688
- if (matchMedia('(prefers-reduced-motion: reduce)').matches) return void el.animate({ opacity: [1, 0.3, 1, 0.3, 1] }, 400);
824
+ if (reduced()) return void el.animate({ opacity: [1, 0.3, 1, 0.3, 1] }, 400);
689
825
  el.animate({ translate: ['0', '-5px', '5px', '-4px', '4px', '-2px', '0'] }, { duration: 360, easing: 'ease-out' });
690
826
  }
691
827
 
692
- /** Puts an outline over `r`. */
693
- function place(el: HTMLElement, r: DOMRect): void {
694
- Object.assign(el.style, { left: `${r.left}px`, top: `${r.top}px`, width: `${r.width}px`, height: `${r.height}px` });
695
- }
696
-
697
- /** How far from the element the popup's outline is drawn, in pixels. */
698
- const ROOM = 4;
699
-
700
- /**
701
- * Outlines the element the popup is open on, for as long as it is: which one it's about, where several look alike
702
- * or the popup covers it. Dashed, with no fill and a little room around the element, so nothing of it is drawn on:
703
- * a border, a ring or a colour tried on it shows as it is. Read each frame, as the element moves with the page's
704
- * scrolling, its layout and what a panel previews on it. Answers what removes it.
705
- */
706
- function outline(root: ShadowRoot, element: Element): () => void {
707
- const box = div('box on');
708
- root.append(box);
709
- let frame = 0;
710
- let was = '';
711
- const follow = () => {
712
- frame = requestAnimationFrame(follow);
713
- const r = element.getBoundingClientRect();
714
- const now = element.isConnected ? `${r.left},${r.top},${r.width},${r.height}` : '';
715
- if (now === was) return;
716
- was = now;
717
- box.classList.toggle('shown', now !== '');
718
- place(box, new DOMRect(r.left - ROOM, r.top - ROOM, r.width + 2 * ROOM, r.height + 2 * ROOM));
719
- };
720
- follow();
721
- return () => {
722
- cancelAnimationFrame(frame);
723
- box.remove();
724
- };
725
- }
726
-
727
828
  const MARGIN = 8;
728
829
  // Under this much room on either side, the panel overlaps the element rather than shrink to a sliver beside it.
729
830
  const LEAST = 160;
831
+ /** How far from the element the popup is, in pixels: room for the line between them. */
832
+ const APART = 32;
730
833
 
731
834
  /** How far a press on the head moves, in pixels, before it's a drag: a click there leaves the popup following. */
732
835
  const SLACK = 3;
733
836
 
734
837
  /**
735
838
  * Keeps a panel beside `element` and inside the viewport, as its tabs fill it, the window resizes and the element
736
- * moves with the page: below, or above where it fits better, its height capped to the room there, so the body
737
- * scrolls rather than the panel leaving the window. Dragged by `handle`, it stays where it's put, in the window,
738
- * and follows the element no more. Fixed, so it never changes the page's own scrolling. Answers a release.
839
+ * moves with the page: to its right, or its left, where there's the room, level with its top and as tall as the
840
+ * window lets it be; else below, or above where it fits better, its height capped to the room there, so the body
841
+ * scrolls rather than the panel leaving the window. Off the element either way (`APART`): it's what's lit, with
842
+ * room for the line between them. Dragged by `handle`, it stays where it's put, in the window, and follows the
843
+ * element no more. Fixed, so it never changes the page's own scrolling. Answers a release.
739
844
  */
740
- function keepInView(panel: HTMLElement, body: HTMLElement, handle: HTMLElement, element: Element): () => void {
845
+ function keepInView(panel: HTMLElement, body: HTMLElement, handle: HTMLElement, element: Element, at?: Point): () => void {
741
846
  let frame = 0;
742
847
  let timer: ReturnType<typeof setTimeout> | undefined;
848
+ // What it's beside: the element, or the spot in it.
849
+ const boxOf = () => (at === undefined ? element.getBoundingClientRect() : spotOf(element, at));
743
850
  // Where the element was last seen: one gone from the page leaves the popup where it was.
744
- let r = element.getBoundingClientRect();
851
+ let r = boxOf();
745
852
  // An element that's not on the page (back from `away`, on another) has no place to be beside: as if dragged to the
746
853
  // middle, toward the top.
747
854
  let moved: { left: number; top: number } | undefined = element.isConnected
@@ -768,26 +875,41 @@ function keepInView(panel: HTMLElement, body: HTMLElement, handle: HTMLElement,
768
875
  }
769
876
  const top = Math.min(Math.max(r.top, 0), height);
770
877
  const bottom = Math.min(Math.max(r.bottom, 0), height);
771
- const below = height - bottom - 2 * MARGIN;
772
- const above = top - 2 * MARGIN;
878
+ const gap = APART;
879
+ const right = width - MARGIN - panel.offsetWidth - (r.right + gap);
880
+ const left = r.left - gap - panel.offsetWidth - MARGIN;
881
+ if (right >= 0 || left >= 0) {
882
+ const room = Math.max(0, height - 2 * MARGIN);
883
+ Object.assign(panel.style, {
884
+ left: `${right >= 0 ? r.right + gap : r.left - gap - panel.offsetWidth}px`,
885
+ top: `${Math.max(MARGIN, Math.min(r.top - CLEAR, height - MARGIN - Math.min(wanted, room)))}px`,
886
+ maxHeight: `${room}px`,
887
+ // It grows out of the side the element is on, as it opens.
888
+ transformOrigin: right >= 0 ? '0 24px' : '100% 24px',
889
+ });
890
+ return;
891
+ }
892
+ const below = height - bottom - gap - MARGIN;
893
+ const above = top - gap - MARGIN;
773
894
  const cap = Math.max(0, Math.min(height * 0.6, height - 2 * MARGIN));
774
895
  let at: number;
775
896
  let room: number;
776
897
  if (Math.max(below, above) < LEAST) {
777
898
  room = height - 2 * MARGIN;
778
- at = Math.min(Math.max(bottom + MARGIN, MARGIN), height - MARGIN - Math.min(wanted, room));
899
+ at = Math.min(Math.max(bottom + gap, MARGIN), height - MARGIN - Math.min(wanted, room));
779
900
  } else if (wanted <= below || below >= above) {
780
901
  room = Math.min(below, cap);
781
- at = bottom + MARGIN;
902
+ at = bottom + gap;
782
903
  } else {
783
904
  room = Math.min(above, cap);
784
- at = top - MARGIN - Math.min(wanted, room);
905
+ at = top - gap - Math.min(wanted, room);
785
906
  }
786
907
  Object.assign(panel.style, {
787
908
  left: `${Math.max(MARGIN, Math.min(r.left, width - MARGIN - panel.offsetWidth))}px`,
788
909
  top: `${at}px`,
789
910
  bottom: '',
790
911
  maxHeight: `${room}px`,
912
+ transformOrigin: at < top ? '24px 100%' : '24px 0',
791
913
  });
792
914
  };
793
915
  // Not in the observers' callbacks, where a change of size is a "ResizeObserver loop" error on the page. The next
@@ -802,7 +924,7 @@ function keepInView(panel: HTMLElement, body: HTMLElement, handle: HTMLElement,
802
924
  if (moved !== undefined) return;
803
925
  watching = requestAnimationFrame(watch);
804
926
  if (!element.isConnected) return;
805
- const now = element.getBoundingClientRect();
927
+ const now = boxOf();
806
928
  if (now.left === r.left && now.top === r.top && now.width === r.width && now.height === r.height) return;
807
929
  r = now;
808
930
  fit();
@@ -851,12 +973,6 @@ function keepInView(panel: HTMLElement, body: HTMLElement, handle: HTMLElement,
851
973
  };
852
974
  }
853
975
 
854
- function div(className: string): HTMLDivElement {
855
- const el = document.createElement('div');
856
- el.className = className;
857
- return el;
858
- }
859
-
860
976
  /** A button that is a drawing: named for what it does, as its title, the tooltip, changes with what it would do. */
861
977
  function iconButton(svg: string, name: string, title: string): HTMLButtonElement {
862
978
  const el = button('', title);
@@ -875,3 +991,12 @@ function button(text: string, title: string): HTMLButtonElement {
875
991
  }
876
992
 
877
993
  const messageOf = (e: unknown) => (e instanceof Error ? e.message : String(e));
994
+
995
+ /** A plugin's page half that threw: a bug of the plugin's, to report where it's made. Its key is the first line of what it threw. */
996
+ const failed = (plugin: string, what: string, e: unknown): Problem => ({
997
+ key: `page:${plugin}:${what.split(' ', 1)[0]}:${messageOf(e).split('\n', 1)[0]!.slice(0, 200)}`,
998
+ kind: 'bug',
999
+ plugin,
1000
+ message: `${plugin} failed to ${what}: ${messageOf(e)}`,
1001
+ ...(e instanceof Error && e.stack !== undefined ? { details: e.stack } : {}),
1002
+ });