deictic 0.6.0 → 0.7.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 (90) hide show
  1. package/API.md +136 -30
  2. package/README.md +4 -3
  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/client/index.d.ts +7 -6
  9. package/dist/client/index.d.ts.map +1 -1
  10. package/dist/client/index.js +1007 -192
  11. package/dist/client/index.js.map +1 -1
  12. package/dist/index.d.ts +2 -2
  13. package/dist/recommended/index.d.ts +2 -2
  14. package/dist/recordings/client.d.ts.map +1 -1
  15. package/dist/recordings/client.js +82 -153
  16. package/dist/recordings/client.js.map +1 -1
  17. package/dist/screenshots/client.d.ts.map +1 -1
  18. package/dist/shared/annotations.js +3 -4
  19. package/dist/shared/annotations.js.map +1 -1
  20. package/dist/shared/board.js +1 -1
  21. package/dist/shared/client.js +45 -2
  22. package/dist/shared/client.js.map +1 -1
  23. package/dist/shared/component.js +62 -0
  24. package/dist/shared/component.js.map +1 -0
  25. package/dist/shared/dom.js +79 -0
  26. package/dist/shared/dom.js.map +1 -0
  27. package/dist/shared/drawer.js +217 -0
  28. package/dist/shared/drawer.js.map +1 -0
  29. package/dist/shared/index.d.ts +116 -7
  30. package/dist/shared/index.d.ts.map +1 -1
  31. package/dist/shared/index2.d.ts +9 -19
  32. package/dist/shared/index2.d.ts.map +1 -1
  33. package/dist/shared/index7.d.ts +103 -26
  34. package/dist/shared/index7.d.ts.map +1 -1
  35. package/dist/shared/index9.d.ts +1 -1
  36. package/dist/shared/integration.d.ts +1 -1
  37. package/dist/shared/packages.js +1 -1
  38. package/dist/shared/recording.js +31 -3
  39. package/dist/shared/recording.js.map +1 -1
  40. package/dist/shared/src.js.map +1 -1
  41. package/dist/shared/task.js +44 -29
  42. package/dist/shared/task.js.map +1 -1
  43. package/dist/shared/tasks.js +232 -62
  44. package/dist/shared/tasks.js.map +1 -1
  45. package/dist/shared/vb.js +1 -1
  46. package/dist/shared/voice2.js +6 -4
  47. package/dist/shared/voice2.js.map +1 -1
  48. package/dist/shared/watch.js +14 -14
  49. package/dist/shared/watch.js.map +1 -1
  50. package/dist/tasks/client.d.ts +2 -2
  51. package/dist/tasks/client.d.ts.map +1 -1
  52. package/dist/tasks/client.js +1829 -741
  53. package/dist/tasks/client.js.map +1 -1
  54. package/dist/tasks/index.d.ts +2 -2
  55. package/dist/voice/client.d.ts.map +1 -1
  56. package/dist/voice/client.js +154 -57
  57. package/dist/voice/client.js.map +1 -1
  58. package/package.json +1 -1
  59. package/skills/deictic/SKILL.md +22 -28
  60. package/src/cli/recording.ts +34 -3
  61. package/src/cli/task.ts +59 -30
  62. package/src/cli/voice.ts +9 -4
  63. package/src/cli/watch.ts +14 -13
  64. package/src/client/away.ts +3 -1
  65. package/src/client/component.ts +98 -0
  66. package/src/client/dom.ts +70 -0
  67. package/src/client/drawer.ts +258 -0
  68. package/src/client/index.ts +220 -128
  69. package/src/client/lightbox.ts +8 -8
  70. package/src/client/lighting.ts +279 -0
  71. package/src/client/style.ts +263 -44
  72. package/src/client/tools.ts +279 -0
  73. package/src/client/tooltip.ts +25 -5
  74. package/src/client/tray.ts +48 -0
  75. package/src/index.ts +113 -6
  76. package/src/plugins/annotations/client.ts +103 -129
  77. package/src/plugins/annotations/draw.ts +68 -72
  78. package/src/plugins/annotations/index.ts +12 -19
  79. package/src/plugins/recordings/client.ts +89 -167
  80. package/src/plugins/screenshots/client.ts +53 -2
  81. package/src/plugins/tasks/client.ts +219 -748
  82. package/src/plugins/tasks/composer.ts +797 -0
  83. package/src/plugins/tasks/drawer.ts +516 -0
  84. package/src/plugins/tasks/index.ts +360 -82
  85. package/src/plugins/tasks/pins.ts +275 -0
  86. package/src/plugins/tasks/store.ts +132 -0
  87. package/src/plugins/voice/client.ts +96 -5
  88. package/skills/deictic/references/recordings.md +0 -13
  89. package/skills/deictic/references/tasks.md +0 -22
  90. package/skills/deictic/references/watch.md +0 -17
@@ -1,3 +1,4 @@
1
+ import { n as MOD, r as controlFor, u as toolButton } from "../shared/dom.js";
1
2
  import { n as defineClientPlugin } from "../shared/src.js";
2
3
  const PAD = 6;
3
4
  const guess = (text) => text.length * 14 * .6;
@@ -37,14 +38,16 @@ function toSvg(annotations, area, scroll, measure = guess) {
37
38
  return `<svg xmlns="http://www.w3.org/2000/svg" width="${n(area.width)}" height="${n(area.height)}" viewBox="${view}">${annotations.map((a) => shapeOf(a, measure)).join("")}</svg>`;
38
39
  }
39
40
  // src/plugins/annotations/draw.ts
40
- const TOOLS_STYLE = `
41
- .annotations-tools { display: flex; flex-wrap: wrap; gap: 4px; }
42
- .annotations-tools .icon { display: inline-flex; align-items: center; justify-content: center; padding: 3px 5px; }
43
- .annotations-tools .icon svg { width: 16px; height: 16px; }
44
- .annotations-tools .gap { width: 1px; align-self: stretch; margin: 2px 4px; background: var(--deictic-line); }
45
- .annotations-tools button[aria-pressed='true'] { background: var(--deictic-accent); border-color: var(--deictic-accent); color: var(--deictic-on-accent); }
46
- .annotations-tools .end { margin-left: auto; }
47
- .annotations-hint { margin: 6px 0 0; font-size: 12px; color: var(--deictic-muted); }
41
+ /**
42
+ * Drawing on the page with the pointer: the tools, as the tool panel has them, and the gestures.
43
+ *
44
+ * The tools say nothing in words: each is an icon whose tooltip is its name and its key (`aria-keyshortcuts`, which
45
+ * the panel hears and the overlay's tooltip draws as keycaps). While one is picked it has the page (`hold`): the
46
+ * surface takes the pointer, so drawing never clicks the page, until the panel gives the page back.
47
+ */
48
+ const STYLE = `
49
+ .annotations-tools .gap { width: 1px; align-self: stretch; margin: 2px; background: rgb(255 255 255 / 0.12); }
50
+ .tool-panel.vertical .annotations-tools .gap { width: auto; height: 1px; }
48
51
  `;
49
52
  const TOOLS = [
50
53
  "box",
@@ -53,15 +56,20 @@ const TOOLS = [
53
56
  "note"
54
57
  ];
55
58
  const NAMES = {
56
- pointer: "Use the page",
57
- box: "Draw a box",
58
- arrow: "Draw an arrow",
59
- pen: "Draw freehand",
60
- note: "Add a note"
59
+ box: "Box",
60
+ arrow: "Arrow",
61
+ pen: "Pen",
62
+ note: "Note"
63
+ };
64
+ /** The key that picks each, alone. */
65
+ const KEYS = {
66
+ box: "M",
67
+ arrow: "A",
68
+ pen: "B",
69
+ note: "N"
61
70
  };
62
71
  /** Each icon's strokes, on a 24-pixel grid: drawn here, so the page half needs nothing to show them. */
63
72
  const ICONS = {
64
- pointer: "<path d=\"M5 3l14 7-6 2-2 6z\"/>",
65
73
  box: "<rect x=\"4\" y=\"4\" width=\"16\" height=\"16\" rx=\"2\"/>",
66
74
  arrow: "<path d=\"M6 18 18 6M9 6h9v9\"/>",
67
75
  pen: "<path d=\"M4 20l1-4L16 5a2.1 2.1 0 0 1 3 3L8 19z\"/>",
@@ -69,14 +77,6 @@ const ICONS = {
69
77
  undo: "<path d=\"M9 14 4 9l5-5M4 9h10a6 6 0 0 1 0 12h-3\"/>",
70
78
  clear: "<path d=\"M4 7h16M10 11v6M14 11v6M6 7l1 13h10l1-13M9 7V4h6v3\"/>"
71
79
  };
72
- /** A button that shows an icon, with its name as its tooltip and for a screen reader. */
73
- function iconButton(icon, name, run) {
74
- const el = button("", name, run);
75
- el.className = "icon";
76
- el.setAttribute("aria-label", name);
77
- el.innerHTML = `<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">${ICONS[icon]}</svg>`;
78
- return el;
79
- }
80
80
  /** Text's width as the marks draw it, by a canvas's measure, which the guess stands in for without one. */
81
81
  function measurer() {
82
82
  const ctx = document.createElement("canvas").getContext("2d");
@@ -84,39 +84,35 @@ function measurer() {
84
84
  ctx.font = `14px system-ui, sans-serif`;
85
85
  return (text) => ctx.measureText(text).width;
86
86
  }
87
- function button(label, title, run) {
88
- const el = Object.assign(document.createElement("button"), {
89
- type: "button",
90
- textContent: label,
91
- title
92
- });
93
- el.addEventListener("click", run);
94
- return el;
95
- }
96
- /**
97
- * Draws on `surface` with the tool picked: answers the toolbar (the tools, then removing the last mark or all), for the
98
- * caller to add its own buttons to, and a hint for the tool.
99
- */
100
- function drawing({ surface, at, preview, add, removeLast, clear, drawing: on }) {
87
+ /** Draws on `surface` with the tool picked. */
88
+ function drawing({ surface, at, preview, add, removeLast, clear, hold }) {
101
89
  const tools = document.createElement("div");
102
- tools.className = "annotations-tools";
103
- const hint = document.createElement("p");
104
- hint.className = "annotations-hint";
105
- let tool = on === void 0 ? "box" : "pointer";
106
- const picks = (on === void 0 ? TOOLS : ["pointer", ...TOOLS]).map((t) => ({
90
+ tools.className = "tool-group annotations-tools";
91
+ let tool;
92
+ let last = "box";
93
+ let release;
94
+ const picks = TOOLS.map((t) => ({
107
95
  t,
108
- el: iconButton(t, NAMES[t], () => pick(t))
96
+ el: toolButton(ICONS[t], NAMES[t], KEYS[t], () => pick(t))
109
97
  }));
98
+ const show = () => {
99
+ for (const p of picks) p.el.setAttribute("aria-pressed", String(p.t === tool));
100
+ surface.style.pointerEvents = tool === void 0 ? "none" : "auto";
101
+ };
110
102
  const pick = (t) => {
111
103
  tool = t;
112
- for (const p of picks) p.el.setAttribute("aria-pressed", String(p.t === t));
113
- on?.(t !== "pointer");
114
- hint.textContent = t === "pointer" ? "Use the page; pick a tool to draw." : t === "note" ? "Click where the note goes, type it, Enter to place it." : "Drag to draw.";
104
+ last = t;
105
+ release ??= hold(() => {
106
+ tool = void 0;
107
+ release = void 0;
108
+ show();
109
+ });
110
+ show();
115
111
  };
116
112
  const gap = document.createElement("span");
117
113
  gap.className = "gap";
118
- tools.append(...picks.map((p) => p.el), gap, iconButton("undo", "Remove the last mark", removeLast), iconButton("clear", "Remove every mark", clear));
119
- pick(tool);
114
+ tools.append(Object.assign(document.createElement("style"), { textContent: STYLE }), ...picks.map((p) => p.el), gap, toolButton(ICONS.undo, "Undo", `${MOD}+Z`, removeLast), toolButton(ICONS.clear, "Clear", "Shift+Backspace", clear));
115
+ show();
120
116
  surface.style.cursor = "crosshair";
121
117
  const shapeFrom = (from, to, points) => {
122
118
  if (tool === "pen") return points.length > 1 ? {
@@ -157,41 +153,52 @@ function drawing({ surface, at, preview, add, removeLast, clear, drawing: on })
157
153
  surface.addEventListener("pointermove", move);
158
154
  surface.addEventListener("pointerup", up, { once: true });
159
155
  });
160
- /** A field where the note goes: Enter places what's typed, leaving it empty or blurring it places nothing. */
156
+ /** A field where the note goes: Enter places what's typed, as does leaving it; Esc, or nothing typed, places nothing. */
161
157
  const note = (point, e) => {
162
- const field = Object.assign(document.createElement("input"), { placeholder: "Note, then Enter" });
158
+ const field = Object.assign(document.createElement("input"), {
159
+ placeholder: "Note",
160
+ ariaLabel: "Note"
161
+ });
162
+ field.dataset["deicticModal"] = "";
163
163
  field.style.cssText = `position:fixed;left:${e.clientX}px;top:${e.clientY}px;width:220px;pointer-events:auto;cursor:text;color:var(--deictic-fg);background:var(--deictic-bg);border-color:var(--deictic-accent)`;
164
164
  surface.append(field);
165
- const place = () => {
165
+ let over = false;
166
+ const end = (placed) => {
167
+ if (over) return;
168
+ over = true;
166
169
  const text = field.value.trim();
167
170
  field.remove();
168
- if (text !== "") add({
171
+ if (placed && text !== "") add({
169
172
  kind: "note",
170
173
  at: point,
171
174
  text
172
175
  });
173
176
  };
174
- field.addEventListener("keydown", (k) => k.key === "Enter" && place());
175
- field.addEventListener("blur", () => field.isConnected && place());
177
+ field.addEventListener("keydown", (k) => {
178
+ if (k.key === "Enter") end(true);
179
+ if (k.key !== "Escape") return;
180
+ k.stopPropagation();
181
+ end(false);
182
+ });
183
+ field.addEventListener("blur", () => end(true));
176
184
  field.focus();
177
185
  };
178
186
  return {
179
- tools,
180
- hint
187
+ group: tools,
188
+ draw: () => pick(last)
181
189
  };
182
190
  }
183
191
  // src/plugins/annotations/client.ts
184
192
  /**
185
193
  * Annotations in the page. `setup` keeps them (in the tab's session storage, per page) and shows them in a
186
- * layer of deictic's overlay that lets the pointer through; it answers the API other plugins `use`. The
187
- * panel is annotate mode: a toolbar, and while it's open a surface over the page that takes the pointer, so
188
- * drawing never clicks the page. Esc or Done leaves it; the marks stay.
194
+ * layer of deictic's overlay that lets the pointer through; it adds the tools that draw them to the tool panel,
195
+ * and answers the API other plugins `use`.
189
196
  *
190
197
  * The layer is deictic's, so a screenshot leaves it out with the rest of the overlay and draws the marks
191
198
  * itself from `svg(area)`: one way to draw them, wherever a shot is rendered. `edit` draws on a picture
192
- * instead, a shot most often, in the same toolbar over everything, and `flatten` makes the PNG of it.
199
+ * instead, a shot most often, with the same tools in a bar over everything, and `flatten` makes the PNG of it.
193
200
  */
194
- /** Under deictic's outline and panels, over the page. */
201
+ /** Under deictic's light and its popup, over the page. */
195
202
  const LAYER = 0x7ffffffd;
196
203
  /** Over everything of deictic's, the popup too. */
197
204
  const TOP = 0x7fffffff;
@@ -226,11 +233,19 @@ function fixed(z) {
226
233
  return el;
227
234
  }
228
235
  const EDITOR_STYLE = `
236
+ .annotations-editor:focus { outline: none; }
229
237
  .annotations-editor { position: fixed; inset: 0; z-index: ${TOP}; box-sizing: border-box; padding: 16px; display: flex;
230
238
  flex-direction: column; align-items: center; justify-content: center; gap: 8px; background: rgb(0 0 0 / 0.75);
231
239
  font: 13px/1.4 system-ui, sans-serif; color: var(--deictic-fg); }
232
- .annotations-editor .bar { box-sizing: border-box; width: min(100%, 720px); padding: 6px 8px; border: 1px solid var(--deictic-line);
233
- border-radius: 8px; background: var(--deictic-bg); }
240
+ /* The tools as the tool panel has them, then Cancel and Save at its end. */
241
+ .annotations-editor .edit-bar { display: flex; align-items: center; gap: 6px; box-sizing: border-box; width: min(100%, 720px);
242
+ padding: 6px 8px; border: 1px solid var(--deictic-line); border-radius: 12px; background: var(--deictic-bg); }
243
+ .annotations-editor .tool-group { display: flex; align-items: center; gap: 4px; margin-right: auto; }
244
+ .annotations-editor button.icon { width: 28px; height: 26px; border-radius: 6px; color: var(--deictic-fg); }
245
+ .annotations-editor button.icon svg { width: 16px; height: 16px; }
246
+ .annotations-editor button[aria-pressed='true'], .annotations-editor button[aria-pressed='true']:hover {
247
+ color: var(--deictic-on-accent); background: var(--deictic-accent); }
248
+ .annotations-editor .edit-bar > button { padding: 3px 12px; border-radius: 6px; }
234
249
  .annotations-editor .stage { position: relative; line-height: 0; box-shadow: 0 12px 32px rgb(0 0 0 / 0.5); touch-action: none; }
235
250
  .annotations-editor img { display: block; max-width: calc(100vw - 32px); max-height: calc(100vh - 120px); user-select: none; }
236
251
  .annotations-editor .marks { position: absolute; inset: 0; pointer-events: none; }
@@ -253,19 +268,19 @@ const over = (picture, marks, measure) => toSvg(marks, {
253
268
  y: 0
254
269
  }, measure);
255
270
  /**
256
- * The picture over everything, to draw on: the marks on Save, nothing on Cancel, Esc or `signal`. It holds
257
- * Esc while it's open (`data-deictic-modal`), so Esc leaves it, not the popup under it.
271
+ * The picture over everything, to draw on: the marks on Save, nothing on Cancel, Esc or `signal`. It's a modal
272
+ * (`data-deictic-modal`) while it's open, so Esc leaves it, not the popup under it, and the tool panel leaves the
273
+ * keys to it: its own bar hears them, as the panel hears its own.
258
274
  */
259
275
  function edit(root, picture, start, { color, measure, signal }) {
260
276
  return new Promise((resolve) => {
261
277
  let marks = [...start];
262
278
  const backdrop = document.createElement("div");
263
279
  backdrop.className = "annotations-editor";
264
- backdrop.dataset.deicticModal = "";
265
- const style = document.createElement("style");
266
- style.textContent = TOOLS_STYLE + EDITOR_STYLE;
280
+ backdrop.dataset["deicticModal"] = "";
267
281
  const bar = document.createElement("div");
268
- bar.className = "bar";
282
+ bar.className = "edit-bar";
283
+ bar.setAttribute("role", "toolbar");
269
284
  const stage = document.createElement("div");
270
285
  stage.className = "stage";
271
286
  const img = Object.assign(document.createElement("img"), {
@@ -282,7 +297,7 @@ function edit(root, picture, start, { color, measure, signal }) {
282
297
  svg?.setAttribute("width", "100%");
283
298
  svg?.setAttribute("height", "100%");
284
299
  };
285
- const { tools, hint } = drawing({
300
+ const { group, draw } = drawing({
286
301
  surface: stage,
287
302
  at: (e) => {
288
303
  const r = stage.getBoundingClientRect();
@@ -303,39 +318,71 @@ function edit(root, picture, start, { color, measure, signal }) {
303
318
  clear: () => {
304
319
  marks = [];
305
320
  show();
306
- }
321
+ },
322
+ hold: () => () => {}
307
323
  });
324
+ draw();
325
+ const focused = root.activeElement;
326
+ backdrop.tabIndex = -1;
308
327
  const end = (result) => {
309
328
  backdrop.remove();
329
+ if (focused instanceof HTMLElement) focused.focus();
310
330
  window.removeEventListener("keydown", onKey, true);
311
331
  signal?.removeEventListener("abort", cancel);
312
332
  resolve(result);
313
333
  };
314
334
  const cancel = () => end(void 0);
315
335
  const onKey = (e) => {
316
- if (e.key !== "Escape") return;
317
- e.stopPropagation();
318
- cancel();
336
+ if (e.repeat || e.composedPath()[0] instanceof HTMLInputElement) return;
337
+ const control = controlFor(bar, e);
338
+ if (control === void 0) return;
339
+ e.preventDefault();
340
+ e.stopImmediatePropagation();
341
+ control.click();
319
342
  };
320
- const save = button("Save", "Keep these marks", () => end(marks));
321
- save.classList.add("primary");
322
- const back = button("Cancel", "Leave it as it was (Esc)", cancel);
323
- back.classList.add("end");
324
- tools.append(back, save);
325
- bar.append(tools, hint);
326
- backdrop.append(style, bar, stage);
343
+ const back = Object.assign(document.createElement("button"), {
344
+ type: "button",
345
+ textContent: "Cancel"
346
+ });
347
+ back.setAttribute("aria-keyshortcuts", "Escape");
348
+ back.addEventListener("click", cancel);
349
+ const save = Object.assign(document.createElement("button"), {
350
+ type: "button",
351
+ className: "primary",
352
+ textContent: "Save"
353
+ });
354
+ save.setAttribute("aria-keyshortcuts", `${MOD}+Enter`);
355
+ save.addEventListener("click", () => end(marks));
356
+ bar.append(group, back, save);
357
+ backdrop.append(Object.assign(document.createElement("style"), { textContent: EDITOR_STYLE }), bar, stage);
327
358
  root.append(backdrop);
359
+ backdrop.focus();
328
360
  window.addEventListener("keydown", onKey, true);
329
361
  signal?.addEventListener("abort", cancel);
330
362
  show();
331
363
  });
332
364
  }
365
+ /** The picture with the marks drawn over it, as a PNG at the picture's own resolution. */
366
+ async function flatten(picture, marks, measure) {
367
+ const base = await image(picture.url);
368
+ const canvas = document.createElement("canvas");
369
+ canvas.width = base.naturalWidth;
370
+ canvas.height = base.naturalHeight;
371
+ const ctx = canvas.getContext("2d");
372
+ if (ctx === null) throw new Error("annotations: this browser won't draw to a canvas");
373
+ ctx.drawImage(base, 0, 0);
374
+ if (marks.length > 0) {
375
+ const svg = await image(`data:image/svg+xml;charset=utf-8,${encodeURIComponent(over(picture, marks, measure))}`);
376
+ ctx.drawImage(svg, 0, 0, canvas.width, canvas.height);
377
+ }
378
+ return new Promise((done, fail) => canvas.toBlob((blob) => blob === null ? fail(/* @__PURE__ */ new Error("annotations: the picture drew nothing")) : done(blob), "image/png"));
379
+ }
333
380
  /**
334
- * Marks shown on the page, in a layer of the overlay that lets the pointer through, redrawn as it
335
- * scrolls. With a `key`, kept in the tab's session storage under it, per page; without, kept by nothing.
381
+ * The page's marks, shown in a layer of the overlay that lets the pointer through, redrawn as it scrolls, and kept
382
+ * in the tab's session storage under `key`.
336
383
  */
337
384
  function marksOnPage(root, color, measure, key) {
338
- let list = key === void 0 ? [] : stored(key);
385
+ let list = stored(key);
339
386
  const listeners = /* @__PURE__ */ new Set();
340
387
  const layer = fixed(LAYER);
341
388
  root.append(layer);
@@ -353,14 +400,14 @@ function marksOnPage(root, color, measure, key) {
353
400
  });
354
401
  window.addEventListener("resize", redraw, { passive: true });
355
402
  const changed = (event) => {
356
- if (key !== void 0) try {
403
+ try {
357
404
  sessionStorage.setItem(key, JSON.stringify(list));
358
405
  } catch {}
359
406
  draw();
360
407
  for (const listener of listeners) listener(event);
361
408
  };
362
409
  draw();
363
- const marks = {
410
+ return {
364
411
  list: () => [...list],
365
412
  add: (shape, given = color) => {
366
413
  const annotation = mark(shape, given);
@@ -379,6 +426,7 @@ function marksOnPage(root, color, measure, key) {
379
426
  });
380
427
  },
381
428
  clear: () => {
429
+ if (list.length === 0) return;
382
430
  list = [];
383
431
  changed({ type: "clear" });
384
432
  },
@@ -388,108 +436,45 @@ function marksOnPage(root, color, measure, key) {
388
436
  },
389
437
  svg: (area) => list.length === 0 ? void 0 : toSvg(list, area, scroll(), measure)
390
438
  };
391
- const remove = () => {
392
- window.removeEventListener("scroll", redraw, { capture: true });
393
- window.removeEventListener("resize", redraw);
394
- cancelAnimationFrame(frame);
395
- layer.remove();
396
- listeners.clear();
397
- };
398
- return {
399
- marks,
400
- remove
401
- };
402
- }
403
- /**
404
- * Drawing on the page: a surface over it, under the popup, and the toolbar. Where the page stays usable
405
- * (`usable`), the surface takes the pointer only while a drawing tool is picked.
406
- */
407
- function onPage(root, marks, color, usable) {
408
- const surface = fixed(0x7ffffffe);
409
- surface.style.pointerEvents = usable ? "none" : "auto";
410
- const preview = fixed(0);
411
- surface.append(preview);
412
- root.append(surface);
413
- const { tools, hint } = drawing({
414
- surface,
415
- at: (e) => ({
416
- x: e.clientX + scrollX,
417
- y: e.clientY + scrollY
418
- }),
419
- preview: (shape) => {
420
- preview.innerHTML = shape === void 0 ? "" : toSvg([mark(shape, color)], viewport(), scroll());
421
- },
422
- add: (shape) => marks.add(shape),
423
- removeLast: () => {
424
- const last = marks.list().at(-1);
425
- if (last !== void 0) marks.remove(last.id);
426
- },
427
- clear: () => marks.clear(),
428
- ...usable ? { drawing: (on) => surface.style.pointerEvents = on ? "auto" : "none" } : {}
429
- });
430
- return {
431
- tools,
432
- hint,
433
- surface
434
- };
435
- }
436
- /** The picture with the marks drawn over it, as a PNG at the picture's own resolution. */
437
- async function flatten(picture, marks, measure) {
438
- const base = await image(picture.url);
439
- const canvas = document.createElement("canvas");
440
- canvas.width = base.naturalWidth;
441
- canvas.height = base.naturalHeight;
442
- const ctx = canvas.getContext("2d");
443
- if (ctx === null) throw new Error("annotations: this browser won't draw to a canvas");
444
- ctx.drawImage(base, 0, 0);
445
- if (marks.length > 0) {
446
- const svg = await image(`data:image/svg+xml;charset=utf-8,${encodeURIComponent(over(picture, marks, measure))}`);
447
- ctx.drawImage(svg, 0, 0, canvas.width, canvas.height);
448
- }
449
- return new Promise((done, fail) => canvas.toBlob((blob) => blob === null ? fail(/* @__PURE__ */ new Error("annotations: the picture drew nothing")) : done(blob), "image/png"));
450
439
  }
451
440
  var client_default = defineClientPlugin({
452
441
  name: "annotations",
453
- label: "Annotate",
454
- setup: ({ root, options }) => {
442
+ setup: ({ root, options, tools }) => {
455
443
  const color = options.color;
456
444
  const measure = measurer();
445
+ const marks = marksOnPage(root, color, measure, `deictic:annotations:${location.pathname}`);
446
+ const surface = fixed(0x7ffffffe);
447
+ const preview = fixed(0);
448
+ surface.append(preview);
449
+ root.append(surface);
450
+ const { group, draw } = drawing({
451
+ surface,
452
+ at: (e) => ({
453
+ x: e.clientX + scrollX,
454
+ y: e.clientY + scrollY
455
+ }),
456
+ preview: (shape) => {
457
+ preview.innerHTML = shape === void 0 ? "" : toSvg([mark(shape, color)], viewport(), scroll());
458
+ },
459
+ add: (shape) => marks.add(shape),
460
+ removeLast: () => {
461
+ const last = marks.list().at(-1);
462
+ if (last !== void 0) marks.remove(last.id);
463
+ },
464
+ clear: () => marks.clear(),
465
+ hold: tools.hold
466
+ });
467
+ tools.add(group);
457
468
  return {
458
- ...marksOnPage(root, color, measure, `deictic:annotations:${location.pathname}`).marks,
459
- edit: (picture, marks = [], { signal } = {}) => edit(root, picture, marks, {
469
+ ...marks,
470
+ draw,
471
+ edit: (picture, start = [], { signal } = {}) => edit(root, picture, start, {
460
472
  color,
461
473
  measure,
462
474
  ...signal ? { signal } : {}
463
475
  }),
464
- flatten: (picture, marks) => flatten(picture, marks, measure),
465
- tools: () => {
466
- const own = marksOnPage(root, color, measure);
467
- const { tools, surface } = onPage(root, own.marks, color, true);
468
- const element = document.createElement("div");
469
- element.append(Object.assign(document.createElement("style"), { textContent: TOOLS_STYLE }), tools);
470
- return {
471
- element,
472
- marks: own.marks,
473
- close: () => {
474
- surface.remove();
475
- own.remove();
476
- }
477
- };
478
- }
476
+ flatten: (picture, drawn) => flatten(picture, drawn, measure)
479
477
  };
480
- },
481
- mount: (panel, { root, use, close, options }) => {
482
- const { color } = options;
483
- const marks = use("annotations");
484
- if (marks === void 0) return;
485
- const style = document.createElement("style");
486
- style.textContent = TOOLS_STYLE;
487
- const { tools, hint, surface } = onPage(root, marks, color, false);
488
- const done = button("Done", "Leave annotate mode (Esc)", close);
489
- done.classList.add("primary", "end");
490
- tools.append(done);
491
- panel.append(style, tools, hint);
492
- return () => surface.remove();
493
478
  }
494
479
  });
495
480
  export { client_default as default };