deictic 0.0.1 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (173) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +144 -0
  3. package/dist/annotations/client.d.ts +4 -0
  4. package/dist/annotations/client.d.ts.map +1 -0
  5. package/dist/annotations/client.js +497 -0
  6. package/dist/annotations/client.js.map +1 -0
  7. package/dist/annotations/index.d.ts +110 -0
  8. package/dist/annotations/index.d.ts.map +1 -0
  9. package/dist/annotations/index.js +22 -0
  10. package/dist/annotations/index.js.map +1 -0
  11. package/dist/astro.d.ts +29 -0
  12. package/dist/astro.d.ts.map +1 -0
  13. package/dist/astro.js +24 -0
  14. package/dist/astro.js.map +1 -0
  15. package/dist/bun.d.ts +12 -0
  16. package/dist/bun.d.ts.map +1 -0
  17. package/dist/bun.js +67 -0
  18. package/dist/bun.js.map +1 -0
  19. package/dist/client/index.d.ts +93 -0
  20. package/dist/client/index.d.ts.map +1 -0
  21. package/dist/client/index.js +1329 -0
  22. package/dist/client/index.js.map +1 -0
  23. package/dist/endpoints.js +354 -0
  24. package/dist/endpoints.js.map +1 -0
  25. package/dist/index.d.ts +220 -0
  26. package/dist/index.d.ts.map +1 -0
  27. package/dist/index.js +10 -0
  28. package/dist/index.js.map +1 -0
  29. package/dist/index2.d.ts +23 -0
  30. package/dist/index3.d.ts +98 -0
  31. package/dist/index3.d.ts.map +1 -0
  32. package/dist/index4.d.ts +74 -0
  33. package/dist/index4.d.ts.map +1 -0
  34. package/dist/index5.d.ts +70 -0
  35. package/dist/index5.d.ts.map +1 -0
  36. package/dist/integration.d.ts +19 -0
  37. package/dist/integration.d.ts.map +1 -0
  38. package/dist/integration.js +277 -0
  39. package/dist/integration.js.map +1 -0
  40. package/dist/kept.js +23 -0
  41. package/dist/kept.js.map +1 -0
  42. package/dist/loader.d.ts +15 -0
  43. package/dist/loader.d.ts.map +1 -0
  44. package/dist/loader.js +17 -0
  45. package/dist/loader.js.map +1 -0
  46. package/dist/marks.js +14 -0
  47. package/dist/marks.js.map +1 -0
  48. package/dist/next.d.ts +17 -0
  49. package/dist/next.d.ts.map +1 -0
  50. package/dist/next.js +150 -0
  51. package/dist/next.js.map +1 -0
  52. package/dist/playwright.js +105 -0
  53. package/dist/playwright.js.map +1 -0
  54. package/dist/recommended/index.d.ts +25 -0
  55. package/dist/recommended/index.d.ts.map +1 -0
  56. package/dist/recommended/index.js +19 -0
  57. package/dist/recommended/index.js.map +1 -0
  58. package/dist/recordings/client.d.ts +4 -0
  59. package/dist/recordings/client.d.ts.map +1 -0
  60. package/dist/recordings/client.js +376 -0
  61. package/dist/recordings/client.js.map +1 -0
  62. package/dist/recordings/index.d.ts +2 -0
  63. package/dist/recordings/index.js +3 -0
  64. package/dist/recordings.js +235 -0
  65. package/dist/recordings.js.map +1 -0
  66. package/dist/screenshots/client.d.ts +23 -0
  67. package/dist/screenshots/client.d.ts.map +1 -0
  68. package/dist/screenshots/client.js +76 -0
  69. package/dist/screenshots/client.js.map +1 -0
  70. package/dist/screenshots/dom.d.ts +4 -0
  71. package/dist/screenshots/dom.d.ts.map +1 -0
  72. package/dist/screenshots/dom.js +92 -0
  73. package/dist/screenshots/dom.js.map +1 -0
  74. package/dist/screenshots/index.d.ts +2 -0
  75. package/dist/screenshots/index.js +98 -0
  76. package/dist/screenshots/index.js.map +1 -0
  77. package/dist/screenshots/rrweb.d.ts +9 -0
  78. package/dist/screenshots/rrweb.d.ts.map +1 -0
  79. package/dist/screenshots/rrweb.js +60 -0
  80. package/dist/screenshots/rrweb.js.map +1 -0
  81. package/dist/shortcut.js +63 -0
  82. package/dist/shortcut.js.map +1 -0
  83. package/dist/tailwind/client.d.ts +4 -0
  84. package/dist/tailwind/client.d.ts.map +1 -0
  85. package/dist/tailwind/client.js +613 -0
  86. package/dist/tailwind/client.js.map +1 -0
  87. package/dist/tailwind/index.d.ts +2 -0
  88. package/dist/tailwind/index.js +557 -0
  89. package/dist/tailwind/index.js.map +1 -0
  90. package/dist/tasks/client.d.ts +6 -0
  91. package/dist/tasks/client.d.ts.map +1 -0
  92. package/dist/tasks/client.js +642 -0
  93. package/dist/tasks/client.js.map +1 -0
  94. package/dist/tasks/index.d.ts +159 -0
  95. package/dist/tasks/index.d.ts.map +1 -0
  96. package/dist/tasks/index.js +348 -0
  97. package/dist/tasks/index.js.map +1 -0
  98. package/dist/vite.d.ts +17 -0
  99. package/dist/vite.d.ts.map +1 -0
  100. package/dist/vite.js +44 -0
  101. package/dist/vite.js.map +1 -0
  102. package/dist/voice/client.d.ts +4 -0
  103. package/dist/voice/client.d.ts.map +1 -0
  104. package/dist/voice/client.js +66 -0
  105. package/dist/voice/client.js.map +1 -0
  106. package/dist/voice/index.d.ts +47 -0
  107. package/dist/voice/index.d.ts.map +1 -0
  108. package/dist/voice/index.js +74 -0
  109. package/dist/voice/index.js.map +1 -0
  110. package/dist/waveform.js +93 -0
  111. package/dist/waveform.js.map +1 -0
  112. package/dist/webpack.d.ts +50 -0
  113. package/dist/webpack.d.ts.map +1 -0
  114. package/dist/webpack.js +78 -0
  115. package/dist/webpack.js.map +1 -0
  116. package/package.json +307 -7
  117. package/skills/deictic/SKILL.md +47 -0
  118. package/skills/deictic/references/install.md +81 -0
  119. package/skills/deictic/references/recordings.md +83 -0
  120. package/skills/deictic/references/tasks.md +79 -0
  121. package/skills/deictic/references/watch.md +43 -0
  122. package/skills/deictic/scripts/frame.mjs +62 -0
  123. package/skills/deictic/scripts/lib.mjs +193 -0
  124. package/skills/deictic/scripts/recording.mjs +186 -0
  125. package/skills/deictic/scripts/task.mjs +84 -0
  126. package/skills/deictic/scripts/transcribe.mjs +93 -0
  127. package/skills/deictic/scripts/voice.mjs +50 -0
  128. package/skills/deictic/scripts/watch.mjs +51 -0
  129. package/src/astro.ts +34 -0
  130. package/src/bun.ts +79 -0
  131. package/src/client/index.ts +833 -0
  132. package/src/client/lightbox.ts +256 -0
  133. package/src/client/style.ts +243 -0
  134. package/src/client/tooltip.ts +78 -0
  135. package/src/client/waveform.ts +98 -0
  136. package/src/index.ts +240 -0
  137. package/src/loader.ts +28 -0
  138. package/src/next.ts +133 -0
  139. package/src/plugins/annotations/client.ts +301 -0
  140. package/src/plugins/annotations/draw.ts +169 -0
  141. package/src/plugins/annotations/index.ts +105 -0
  142. package/src/plugins/annotations/svg.ts +61 -0
  143. package/src/plugins/recommended/index.ts +35 -0
  144. package/src/plugins/recordings/client.ts +315 -0
  145. package/src/plugins/recordings/index.ts +174 -0
  146. package/src/plugins/recordings/marks.ts +11 -0
  147. package/src/plugins/recordings/ours.ts +70 -0
  148. package/src/plugins/recordings/replay.ts +139 -0
  149. package/src/plugins/screenshots/client.ts +63 -0
  150. package/src/plugins/screenshots/dom.ts +79 -0
  151. package/src/plugins/screenshots/index.ts +194 -0
  152. package/src/plugins/screenshots/playwright.ts +119 -0
  153. package/src/plugins/screenshots/rrweb.ts +45 -0
  154. package/src/plugins/tailwind/catalog.ts +396 -0
  155. package/src/plugins/tailwind/client.ts +669 -0
  156. package/src/plugins/tailwind/index.ts +267 -0
  157. package/src/plugins/tasks/client.ts +622 -0
  158. package/src/plugins/tasks/index.ts +513 -0
  159. package/src/plugins/voice/client.ts +57 -0
  160. package/src/plugins/voice/index.ts +114 -0
  161. package/src/shortcut.ts +83 -0
  162. package/src/tools/astro.ts +81 -0
  163. package/src/tools/editor.ts +22 -0
  164. package/src/tools/endpoints.ts +163 -0
  165. package/src/tools/history.ts +203 -0
  166. package/src/tools/integration.ts +106 -0
  167. package/src/tools/kept.ts +21 -0
  168. package/src/tools/locate.ts +94 -0
  169. package/src/tools/plugins.ts +41 -0
  170. package/src/tools/source.ts +72 -0
  171. package/src/vite.ts +62 -0
  172. package/src/webpack.ts +107 -0
  173. package/index.js +0 -1
@@ -0,0 +1,1329 @@
1
+ import { r as gestureOf } from "../shortcut.js";
2
+ import { n as liveWaveform, r as peaksOf, t as drawBars } from "../waveform.js";
3
+ // src/client/lightbox.ts
4
+ const EASE = "cubic-bezier(0.2, 0.8, 0.2, 1)";
5
+ const PLAY = "<svg viewBox=\"0 0 24 24\" aria-hidden=\"true\"><path d=\"M7 4v16l13-8z\" fill=\"currentColor\"/></svg>";
6
+ const PAUSE = "<svg viewBox=\"0 0 24 24\" aria-hidden=\"true\"><path d=\"M6 4h4v16H6zM14 4h4v16h-4z\" fill=\"currentColor\"/></svg>";
7
+ const reduced = () => matchMedia("(prefers-reduced-motion: reduce)").matches;
8
+ const clock = (s) => `${Math.floor(s / 60)}:${String(Math.floor(s % 60)).padStart(2, "0")}`;
9
+ /**
10
+ * Opens it in `root`. `pinned` draws it where `root` is in the page instead, with no backdrop, motion or keys, and
11
+ * actions that don't close it: for a page of deictic's own states (`lightboxIn`).
12
+ */
13
+ function openLightbox(root, items, { at = 0, from, pinned = false } = {}) {
14
+ const box = div$1(pinned ? "lightbox pinned" : "lightbox");
15
+ if (!pinned) {
16
+ box.dataset.deicticModal = "";
17
+ box.setAttribute("role", "dialog");
18
+ box.setAttribute("aria-modal", "true");
19
+ }
20
+ const backdrop = div$1("lb-backdrop");
21
+ const stage = div$1("lb-stage");
22
+ const head = div$1("lb-head");
23
+ const title = div$1("lb-title");
24
+ const count = div$1("lb-count");
25
+ const actions = div$1("lb-actions");
26
+ const close = button$1("×", "Close (Esc)", "lb-close");
27
+ head.append(title, count, actions, close);
28
+ const body = div$1("lb-body");
29
+ stage.append(head, body);
30
+ const prev = button$1("‹", "Previous (←)", "lb-prev");
31
+ const next = button$1("›", "Next (→)", "lb-next");
32
+ box.append(backdrop, stage, ...items.length > 1 ? [prev, next] : []);
33
+ root.append(box);
34
+ let index = Math.min(Math.max(0, at), items.length - 1);
35
+ let leave;
36
+ let closing = false;
37
+ const show = (i, animate) => {
38
+ leave?.();
39
+ leave = void 0;
40
+ index = (i + items.length) % items.length;
41
+ const item = items[index];
42
+ box.setAttribute("aria-label", item.title);
43
+ title.textContent = item.title;
44
+ count.textContent = items.length > 1 ? `${index + 1} / ${items.length}` : "";
45
+ actions.replaceChildren(...(item.actions ?? []).map((a) => {
46
+ const b = button$1(a.label, a.title ?? "", "");
47
+ b.addEventListener("click", () => {
48
+ if (!pinned) done();
49
+ a.run();
50
+ });
51
+ return b;
52
+ }));
53
+ const content = contentOf(item, !pinned);
54
+ body.replaceChildren(content.element);
55
+ leave = content.leave;
56
+ if (animate && !reduced()) body.animate({ opacity: [0, 1] }, {
57
+ duration: 160,
58
+ easing: "ease-out"
59
+ });
60
+ };
61
+ /** The transform that puts the stage where `from` is: what it grows from and shrinks back to. */
62
+ const fromRect = () => {
63
+ if (from === void 0 || !from.isConnected) return void 0;
64
+ const f = from.getBoundingClientRect();
65
+ const s = stage.getBoundingClientRect();
66
+ if (f.width === 0 || s.width === 0) return void 0;
67
+ return `translate(${f.left - s.left}px, ${f.top - s.top}px) scale(${f.width / s.width}, ${f.height / s.height})`;
68
+ };
69
+ const onKey = (e) => {
70
+ if (e.key === "Escape") done();
71
+ else if (e.key === "ArrowLeft" && items.length > 1) show(index - 1, true);
72
+ else if (e.key === "ArrowRight" && items.length > 1) show(index + 1, true);
73
+ else return;
74
+ e.preventDefault();
75
+ e.stopImmediatePropagation();
76
+ };
77
+ if (!pinned) addEventListener("keydown", onKey, true);
78
+ function done() {
79
+ if (closing) return;
80
+ closing = true;
81
+ removeEventListener("keydown", onKey, true);
82
+ leave?.();
83
+ if (pinned) return box.remove();
84
+ const gone = () => {
85
+ box.remove();
86
+ if (from instanceof HTMLElement && from.isConnected) from.focus({ preventScroll: true });
87
+ };
88
+ if (reduced()) return gone();
89
+ const to = fromRect();
90
+ backdrop.animate({ opacity: [1, 0] }, {
91
+ duration: 200,
92
+ easing: "ease-in",
93
+ fill: "forwards"
94
+ });
95
+ stage.animate(to === void 0 ? {
96
+ opacity: [1, 0],
97
+ transform: ["none", "scale(0.96)"]
98
+ } : {
99
+ transform: ["none", to],
100
+ opacity: [
101
+ 1,
102
+ 1,
103
+ 0
104
+ ],
105
+ offset: [
106
+ 0,
107
+ .8,
108
+ 1
109
+ ]
110
+ }, {
111
+ duration: 220,
112
+ easing: EASE,
113
+ fill: "forwards"
114
+ }).finished.then(gone, gone);
115
+ }
116
+ backdrop.addEventListener("click", done);
117
+ close.addEventListener("click", done);
118
+ prev.addEventListener("click", () => show(index - 1, true));
119
+ next.addEventListener("click", () => show(index + 1, true));
120
+ show(index, false);
121
+ if (pinned) {
122
+ close.hidden = true;
123
+ return { close: done };
124
+ }
125
+ close.focus({ preventScroll: true });
126
+ if (reduced()) box.animate({ opacity: [0, 1] }, { duration: 150 });
127
+ else {
128
+ backdrop.animate({ opacity: [0, 1] }, {
129
+ duration: 240,
130
+ easing: "ease-out"
131
+ });
132
+ const grow = fromRect();
133
+ stage.animate(grow === void 0 ? {
134
+ opacity: [0, 1],
135
+ transform: ["scale(0.96)", "none"]
136
+ } : {
137
+ transform: [grow, "none"],
138
+ opacity: [
139
+ 0,
140
+ 1,
141
+ 1
142
+ ],
143
+ offset: [
144
+ 0,
145
+ .2,
146
+ 1
147
+ ]
148
+ }, {
149
+ duration: 280,
150
+ easing: EASE
151
+ });
152
+ }
153
+ return { close: done };
154
+ }
155
+ /** An item, drawn: what goes in the body, and what to stop when it's left (a sound playing). */
156
+ function contentOf(item, autoplay) {
157
+ switch (item.kind) {
158
+ case "image": return { element: Object.assign(document.createElement("img"), {
159
+ src: item.url,
160
+ alt: item.title,
161
+ className: "lb-image",
162
+ draggable: false
163
+ }) };
164
+ case "page": {
165
+ const frame = Object.assign(document.createElement("iframe"), {
166
+ src: item.url,
167
+ title: item.title,
168
+ className: "lb-page"
169
+ });
170
+ frame.allow = "autoplay; fullscreen";
171
+ return { element: frame };
172
+ }
173
+ case "audio": return player(item.url, autoplay);
174
+ }
175
+ }
176
+ /** A sound: play and pause, its waveform with what's played lit, and the time. With `autoplay`, it starts playing. */
177
+ function player(url, autoplay) {
178
+ const el = div$1("lb-audio");
179
+ const play = button$1("", "Play (Space)", "lb-play");
180
+ const wave = Object.assign(document.createElement("canvas"), { className: "lb-wave" });
181
+ wave.setAttribute("role", "slider");
182
+ wave.setAttribute("aria-label", "Where in the clip");
183
+ wave.tabIndex = 0;
184
+ const time = div$1("lb-time");
185
+ el.append(play, wave, time);
186
+ const audio = new Audio(url);
187
+ const colors = {
188
+ played: "#3ddc7c",
189
+ rest: "#52525b"
190
+ };
191
+ let peaks = Array(120).fill(.05);
192
+ let frame = 0;
193
+ let length = 0;
194
+ const lengthOf = () => Number.isFinite(audio.duration) && audio.duration > 0 ? audio.duration : length;
195
+ const draw = () => {
196
+ const total = lengthOf();
197
+ drawBars(wave, peaks, total > 0 ? audio.currentTime / total : 0, colors);
198
+ time.textContent = `${clock(audio.currentTime)} / ${total > 0 ? clock(total) : "–:––"}`;
199
+ wave.setAttribute("aria-valuenow", String(Math.round(audio.currentTime)));
200
+ };
201
+ const loop = () => {
202
+ draw();
203
+ frame = audio.paused ? 0 : requestAnimationFrame(loop);
204
+ };
205
+ const state = () => {
206
+ play.innerHTML = audio.paused ? PLAY : PAUSE;
207
+ play.title = audio.paused ? "Play (Space)" : "Pause (Space)";
208
+ if (!audio.paused && frame === 0) frame = requestAnimationFrame(loop);
209
+ draw();
210
+ };
211
+ audio.addEventListener("play", state);
212
+ audio.addEventListener("pause", state);
213
+ audio.addEventListener("ended", state);
214
+ audio.addEventListener("loadedmetadata", draw);
215
+ const toggle = () => void (audio.paused ? audio.play().catch(() => {}) : audio.pause());
216
+ play.addEventListener("click", toggle);
217
+ const seek = (e) => {
218
+ const r = wave.getBoundingClientRect();
219
+ const total = lengthOf();
220
+ if (total > 0) audio.currentTime = Math.min(1, Math.max(0, (e.clientX - r.left) / r.width)) * total;
221
+ draw();
222
+ };
223
+ wave.addEventListener("pointerdown", (e) => {
224
+ wave.setPointerCapture(e.pointerId);
225
+ seek(e);
226
+ });
227
+ wave.addEventListener("pointermove", (e) => {
228
+ if (wave.hasPointerCapture(e.pointerId)) seek(e);
229
+ });
230
+ el.addEventListener("keydown", (e) => {
231
+ if (e.key !== " ") return;
232
+ e.preventDefault();
233
+ toggle();
234
+ });
235
+ peaksOf(url, 120).then((read) => {
236
+ peaks = read.peaks;
237
+ length = read.duration;
238
+ draw();
239
+ }, () => {});
240
+ state();
241
+ if (autoplay) audio.play().catch(() => state());
242
+ requestAnimationFrame(draw);
243
+ return {
244
+ element: el,
245
+ leave: () => {
246
+ audio.pause();
247
+ cancelAnimationFrame(frame);
248
+ }
249
+ };
250
+ }
251
+ function div$1(className) {
252
+ const el = document.createElement("div");
253
+ el.className = className;
254
+ return el;
255
+ }
256
+ function button$1(text, title, className) {
257
+ const el = document.createElement("button");
258
+ el.type = "button";
259
+ el.textContent = text;
260
+ el.title = title;
261
+ el.className = className;
262
+ return el;
263
+ }
264
+ // src/client/style.ts
265
+ /**
266
+ * The overlay's styles, in its shadow root. Panels style their bodies with the same custom
267
+ * properties, so a plugin's panel looks like the rest without styles of its own for the basics.
268
+ */
269
+ const STYLE = `
270
+ :host {
271
+ --deictic-accent: #12b358;
272
+ --deictic-on-accent: #04130b;
273
+ --deictic-bg: #18181b;
274
+ --deictic-fg: #f4f4f5;
275
+ --deictic-muted: #a1a1aa;
276
+ --deictic-line: #3f3f46;
277
+ --deictic-problem: #f87171;
278
+ all: initial;
279
+ /* Inherited by everything in the root: a thumb and no track, on whatever the panel's background is. */
280
+ scrollbar-color: var(--deictic-line) transparent;
281
+ }
282
+ /* The host takes no room in the page: everything in it is fixed, so nothing of deictic's moves or scrolls it. */
283
+ :host(deictic-overlay) { display: contents; }
284
+ .box {
285
+ position: fixed;
286
+ display: none;
287
+ z-index: 2147483646;
288
+ pointer-events: none;
289
+ box-sizing: border-box;
290
+ outline: 2px solid var(--deictic-accent);
291
+ background: rgb(18 179 88 / 0.1);
292
+ }
293
+ .box.shown { display: block; }
294
+ .box.inner { outline: 1px dashed var(--deictic-accent); background: none; }
295
+ /* The element the popup is open on: dashed and off it a little, with no fill, so what's tried on it shows as it is. */
296
+ .box.on { outline: 1px dashed var(--deictic-accent); border-radius: 4px; background: none; }
297
+ .label {
298
+ position: absolute;
299
+ left: -2px;
300
+ bottom: 100%;
301
+ padding: 1px 6px;
302
+ font: 12px/1.5 ui-monospace, monospace;
303
+ white-space: nowrap;
304
+ color: var(--deictic-on-accent);
305
+ background: var(--deictic-accent);
306
+ }
307
+ .label.below { top: 100%; bottom: auto; }
308
+ .panel {
309
+ position: fixed;
310
+ z-index: 2147483647;
311
+ width: min(360px, calc(100vw - 16px));
312
+ max-height: 60vh;
313
+ display: flex;
314
+ flex-direction: column;
315
+ box-sizing: border-box;
316
+ border: 1px solid var(--deictic-line);
317
+ border-radius: 8px;
318
+ box-shadow: 0 12px 32px rgb(0 0 0 / 0.35);
319
+ font: 13px/1.4 system-ui, sans-serif;
320
+ color: var(--deictic-fg);
321
+ background: var(--deictic-bg);
322
+ }
323
+ .panel.pinned { position: relative; z-index: auto; max-height: none; }
324
+ @media (prefers-reduced-motion: no-preference) {
325
+ .panel:not(.pinned) { animation: deictic-in 0.12s ease-out; }
326
+ }
327
+ @keyframes deictic-in {
328
+ from { opacity: 0; translate: 0 4px; }
329
+ }
330
+ .head {
331
+ display: flex;
332
+ align-items: flex-end;
333
+ gap: 2px;
334
+ padding: 4px 8px 0;
335
+ }
336
+ /* Over the page the head is what the popup is dragged by; no touch scrolling starts there. */
337
+ .panel:not(.pinned) .head { cursor: grab; touch-action: none; user-select: none; }
338
+ .panel.dragging .head, .panel.dragging .head button { cursor: grabbing; }
339
+ .head .tab {
340
+ border: 0;
341
+ border-bottom: 2px solid transparent;
342
+ border-radius: 0;
343
+ padding: 4px 8px;
344
+ font-weight: 600;
345
+ color: var(--deictic-muted);
346
+ }
347
+ .head .tab:hover { color: var(--deictic-fg); }
348
+ .head .tab[aria-selected='true'] { color: var(--deictic-fg); border-bottom-color: var(--deictic-accent); }
349
+ .head .close { margin: 0 0 3px auto; }
350
+ .bar {
351
+ display: flex;
352
+ align-items: center;
353
+ gap: 2px;
354
+ padding: 4px 8px;
355
+ border-top: 1px solid var(--deictic-line);
356
+ border-bottom: 1px solid var(--deictic-line);
357
+ }
358
+ .bar .where {
359
+ display: inline-flex;
360
+ align-items: center;
361
+ gap: 4px;
362
+ min-width: 0;
363
+ margin-right: auto;
364
+ font: 12px ui-monospace, monospace;
365
+ color: var(--deictic-muted);
366
+ text-decoration: none;
367
+ }
368
+ .bar .where span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
369
+ .bar .where svg { flex: none; }
370
+ .bar .where:hover, .bar .where:focus-visible { color: var(--deictic-fg); }
371
+ .bar .where:hover span { text-decoration: underline; }
372
+ .lightbox {
373
+ position: fixed;
374
+ inset: 0;
375
+ z-index: 2147483647;
376
+ display: flex;
377
+ align-items: center;
378
+ justify-content: center;
379
+ font: 13px/1.4 system-ui, sans-serif;
380
+ color: var(--deictic-fg);
381
+ }
382
+ .lb-backdrop { position: absolute; inset: 0; background: rgb(9 9 11 / 0.72); backdrop-filter: blur(2px); }
383
+ .lb-stage {
384
+ position: relative;
385
+ display: flex;
386
+ flex-direction: column;
387
+ max-width: 92vw;
388
+ max-height: 90vh;
389
+ border: 1px solid var(--deictic-line);
390
+ border-radius: 10px;
391
+ background: var(--deictic-bg);
392
+ box-shadow: 0 24px 64px rgb(0 0 0 / 0.5);
393
+ transform-origin: 0 0;
394
+ overflow: hidden;
395
+ }
396
+ .lb-head { display: flex; align-items: center; gap: 8px; padding: 8px 8px 8px 12px; border-bottom: 1px solid var(--deictic-line); }
397
+ .lb-title { font-weight: 600; }
398
+ .lb-count { color: var(--deictic-muted); font-size: 12px; font-variant-numeric: tabular-nums; }
399
+ .lb-actions { display: flex; gap: 6px; margin-left: auto; }
400
+ .lb-body { display: flex; justify-content: center; min-height: 0; background: #09090b; }
401
+ .lb-image { display: block; max-width: 92vw; max-height: calc(90vh - 48px); object-fit: contain; }
402
+ .lb-page { display: block; width: min(1280px, 92vw); height: min(860px, calc(90vh - 48px)); border: 0; background: #18181b; }
403
+ .lb-audio { display: flex; align-items: center; gap: 12px; width: min(640px, 86vw); padding: 20px 16px; }
404
+ .lb-play {
405
+ flex: none;
406
+ display: inline-flex;
407
+ align-items: center;
408
+ justify-content: center;
409
+ width: 40px;
410
+ height: 40px;
411
+ padding: 0;
412
+ border: 0;
413
+ border-radius: 50%;
414
+ color: var(--deictic-on-accent);
415
+ background: var(--deictic-accent);
416
+ }
417
+ .lb-play svg { width: 16px; height: 16px; }
418
+ .lb-wave { flex: 1; min-width: 0; height: 56px; cursor: pointer; border-radius: 4px; }
419
+ .lb-wave:focus-visible { outline: 1px solid var(--deictic-accent); outline-offset: 2px; }
420
+ .lb-time { flex: none; min-width: 9ch; text-align: right; font: 12px ui-monospace, monospace; color: var(--deictic-muted); }
421
+ .lb-prev, .lb-next {
422
+ position: absolute;
423
+ top: 50%;
424
+ translate: 0 -50%;
425
+ width: 40px;
426
+ height: 40px;
427
+ padding: 0;
428
+ border: 1px solid var(--deictic-line);
429
+ border-radius: 50%;
430
+ font-size: 22px;
431
+ line-height: 1;
432
+ background: var(--deictic-bg);
433
+ }
434
+ .lb-prev { left: 16px; }
435
+ .lb-next { right: 16px; }
436
+ .lightbox.pinned { position: relative; inset: auto; z-index: auto; display: block; padding: 0 64px; }
437
+ .lightbox.pinned .lb-stage { max-width: 100%; max-height: none; }
438
+ .lightbox.pinned .lb-page { width: 100%; height: 420px; }
439
+ .lightbox.pinned .lb-image { max-width: 100%; max-height: 420px; }
440
+ .lightbox.pinned .lb-backdrop { display: none; }
441
+ .lightbox.pinned .lb-audio { width: 100%; box-sizing: border-box; }
442
+ .lightbox.pinned .lb-close { display: none; }
443
+ .tip {
444
+ position: fixed;
445
+ left: 0;
446
+ top: 0;
447
+ z-index: 2147483647;
448
+ max-width: 280px;
449
+ padding: 3px 6px;
450
+ border: 1px solid var(--deictic-line);
451
+ border-radius: 4px;
452
+ font: 12px/1.4 system-ui, sans-serif;
453
+ color: var(--deictic-fg);
454
+ background: #09090b;
455
+ box-shadow: 0 4px 12px rgb(0 0 0 / 0.3);
456
+ pointer-events: none;
457
+ visibility: hidden;
458
+ opacity: 0;
459
+ transition: opacity 0.12s;
460
+ }
461
+ .tip.shown { visibility: visible; opacity: 1; }
462
+ button {
463
+ font: inherit;
464
+ color: inherit;
465
+ background: transparent;
466
+ border: 1px solid var(--deictic-line);
467
+ border-radius: 4px;
468
+ padding: 1px 7px;
469
+ cursor: pointer;
470
+ }
471
+ button:hover { border-color: var(--deictic-muted); }
472
+ button:disabled { opacity: 0.5; cursor: default; }
473
+ button:disabled:hover { border-color: var(--deictic-line); }
474
+ /* One ring for whatever the keyboard lands on, in place of each browser's own. */
475
+ button:focus-visible, a:focus-visible { outline: 1px solid var(--deictic-accent); outline-offset: 1px; }
476
+ button.icon {
477
+ flex: none;
478
+ display: inline-flex;
479
+ align-items: center;
480
+ justify-content: center;
481
+ width: 22px;
482
+ height: 22px;
483
+ padding: 0;
484
+ border: 0;
485
+ color: var(--deictic-muted);
486
+ }
487
+ button.icon svg { width: 14px; height: 14px; }
488
+ button.icon:hover:not(:disabled) { color: var(--deictic-fg); background: rgb(255 255 255 / 0.08); }
489
+ button.primary { background: var(--deictic-accent); border-color: var(--deictic-accent); color: var(--deictic-on-accent); }
490
+ input, textarea {
491
+ box-sizing: border-box;
492
+ width: 100%;
493
+ font: 12px ui-monospace, monospace;
494
+ color: inherit;
495
+ background: #09090b;
496
+ border: 1px solid var(--deictic-line);
497
+ border-radius: 4px;
498
+ padding: 4px 6px;
499
+ }
500
+ input:focus, textarea:focus { outline: 1px solid var(--deictic-accent); }
501
+ code { font: 12px ui-monospace, monospace; }
502
+ /* Border-box, as the popup holds the body's height by its whole box while a tab reads. */
503
+ .body { box-sizing: border-box; padding: 8px; overflow: auto; }
504
+ .status { padding: 0 8px 6px; font-size: 12px; color: var(--deictic-muted); }
505
+ .status:empty { display: none; }
506
+ .status.problem { color: var(--deictic-problem); }
507
+ `;
508
+ // src/client/tooltip.ts
509
+ /**
510
+ * One tooltip for everything deictic draws in a shadow root: whatever has a `title` (or `data-tip`) shows it
511
+ * in a box of ours, soon after the pointer rests or focus lands, instead of the browser's, which comes late
512
+ * and looks like nothing else here. Plugins only set `title`, as they would anywhere; the first hover moves
513
+ * it to `data-tip`, so the browser's own stays away, and gives a textless control the title as its name.
514
+ * The box is fixed in the root, so no panel's scrolling clips it.
515
+ */
516
+ const DELAY = 300;
517
+ function tooltips(root) {
518
+ const tip = document.createElement("div");
519
+ tip.className = "tip";
520
+ tip.setAttribute("role", "tooltip");
521
+ root.append(tip);
522
+ let timer = 0;
523
+ let over;
524
+ const hide = () => {
525
+ clearTimeout(timer);
526
+ over = void 0;
527
+ tip.classList.remove("shown");
528
+ };
529
+ const show = (el) => {
530
+ const text = el.getAttribute("data-tip");
531
+ if (text === null || text === "" || !el.isConnected) return hide();
532
+ tip.textContent = text;
533
+ if (tip !== root.lastElementChild) root.append(tip);
534
+ tip.classList.add("shown");
535
+ const r = el.getBoundingClientRect();
536
+ const t = tip.getBoundingClientRect();
537
+ const left = Math.max(8, Math.min(r.left + r.width / 2 - t.width / 2, innerWidth - t.width - 8));
538
+ const top = r.bottom + 6 + t.height > innerHeight - 8 ? r.top - 6 - t.height : r.bottom + 6;
539
+ Object.assign(tip.style, {
540
+ left: `${left}px`,
541
+ top: `${top}px`
542
+ });
543
+ };
544
+ /** The element with a tip around where the event happened, its `title` taken over the first time. */
545
+ const tipped = (e) => {
546
+ for (const node of e.composedPath()) {
547
+ if (!(node instanceof Element)) continue;
548
+ if (node === tip) return void 0;
549
+ if (node.localName === "iframe") return void 0;
550
+ const title = node.getAttribute("title");
551
+ if (title !== null) {
552
+ node.removeAttribute("title");
553
+ if (title === "") continue;
554
+ node.setAttribute("data-tip", title);
555
+ if (!node.hasAttribute("aria-label") && (node.textContent ?? "").trim().length <= 1) node.setAttribute("aria-label", title);
556
+ else node.setAttribute("aria-description", title);
557
+ }
558
+ if (node.hasAttribute("data-tip")) return node;
559
+ }
560
+ };
561
+ const enter = (e) => {
562
+ const el = tipped(e);
563
+ if (el === over) return;
564
+ hide();
565
+ if (el === void 0) return;
566
+ over = el;
567
+ timer = window.setTimeout(() => show(el), e.type === "focusin" ? 0 : DELAY);
568
+ };
569
+ root.addEventListener("pointerover", enter);
570
+ root.addEventListener("focusin", (e) => {
571
+ if (e.target.matches(":focus-visible")) enter(e);
572
+ });
573
+ root.addEventListener("pointerout", (e) => {
574
+ const to = e.relatedTarget;
575
+ if (over !== void 0 && !(to instanceof Node && over.contains(to))) hide();
576
+ });
577
+ root.addEventListener("focusout", hide);
578
+ root.addEventListener("pointerdown", hide);
579
+ addEventListener("scroll", hide, {
580
+ capture: true,
581
+ passive: true
582
+ });
583
+ }
584
+ // src/client/index.ts
585
+ const ATTRIBUTE = "data-deictic";
586
+ /** An arrow pointing up and out: the link leaves the page for the editor. */
587
+ const OUT = icon("M7 17 17 7M8 7h9v9", 2.25);
588
+ const UNDO = icon("M9 14 4 9l5-5M4 9h10.5a5.5 5.5 0 0 1 0 11H11");
589
+ const REDO = icon("m15 14 5-5-5-5M20 9H9.5a5.5 5.5 0 0 0 0 11H13");
590
+ const CLOSE = icon("M18 6 6 18M6 6l12 12");
591
+ /** Where the tab used last is kept, for deictic's own shortcut to open it again. */
592
+ const LAST = "deictic:tab";
593
+ /** How long a read may take, in milliseconds, before the popup says it's reading. */
594
+ const SLOW = 200;
595
+ const STARTED = Symbol.for("deictic");
596
+ function targetOf(el) {
597
+ const at = el.getAttribute(ATTRIBUTE);
598
+ const match = at === null ? null : /^(.*):(\d+):(\d+)$/.exec(at);
599
+ if (match === null) return void 0;
600
+ return {
601
+ loc: {
602
+ file: match[1],
603
+ line: Number(match[2]),
604
+ column: Number(match[3])
605
+ },
606
+ tag: el.localName
607
+ };
608
+ }
609
+ const sameAs = (el) => [...document.querySelectorAll(`[${ATTRIBUTE}="${CSS.escape(el.getAttribute(ATTRIBUTE) ?? "")}"]`)];
610
+ /** The nearest marked element around `el`, out through shadow roots to their hosts. */
611
+ function markedAround(el) {
612
+ for (let at = el; at !== void 0;) {
613
+ const marked = at.closest(`[${ATTRIBUTE}]`);
614
+ if (marked !== null) return marked;
615
+ const root = at.getRootNode();
616
+ at = root instanceof ShadowRoot ? root.host : void 0;
617
+ }
618
+ }
619
+ /** `div.panel > button.tab`: the element as a selector, the deepest classes only, for a person to tell it by. */
620
+ function icon(path, width = 2) {
621
+ 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>`;
622
+ }
623
+ const nameOf = (el) => `${el.localName}${[...el.classList].map((c) => `.${c}`).join("")}`;
624
+ let started;
625
+ /**
626
+ * What the page hears on `window` as the popup opens over it on an element (its `detail` a `PopupOpened`) and as
627
+ * it closes: for a page that moves on its own to hold still meanwhile, as deictic's site does. Not for one pinned.
628
+ */
629
+ const POPUP_OPENED = "deictic:open";
630
+ const POPUP_CLOSED = "deictic:close";
631
+ /**
632
+ * Opens the popup over the page on `element`, as deictic's own shortcut does: at `tab` (a plugin's name), or at the
633
+ * tab used last. For a page that opens it from a gesture of its own, as deictic's site does on a plain click.
634
+ * Answers whether it opened: not where no plugin has a tab, or `element` has no mark. The overlay must have started.
635
+ */
636
+ function openPopup(element, { tab } = {}) {
637
+ if (started === void 0) throw new Error("deictic: the overlay has not started, so there is no popup to open");
638
+ return started.open(element, tab);
639
+ }
640
+ /**
641
+ * Shows the popup in `container`, open at a tab (`tab`, a plugin's name, or the first) on `element`, where
642
+ * the page puts it rather than over the page: a page of the popup's states, to point at and fix, as deictic's
643
+ * own site does. It's the real popup, its tabs and plugins working. Closing it, or a tab's `away`, opens it
644
+ * again in place. It draws in `container`'s shadow root, made if there isn't one. Answers what removes it.
645
+ * The overlay must have started (the dev server's client module starts it), and `element` be marked.
646
+ */
647
+ function popupIn(container, { tab: name, element }) {
648
+ if (started === void 0) throw new Error("deictic: the overlay has not started, so there is no popup to show");
649
+ const target = targetOf(element);
650
+ if (target === void 0) throw new Error(`deictic: ${nameOf(element)} has no ${ATTRIBUTE} mark to open the popup on`);
651
+ const { base, contextOf, tabs } = started;
652
+ const remember = () => {};
653
+ const tab = tabs.find((t) => t.plugin.name === name) ?? tabs[0];
654
+ if (tab === void 0) throw new Error("deictic: no plugin has a tab to show");
655
+ const root = container.shadowRoot ?? container.attachShadow({ mode: "open" });
656
+ const style = document.createElement("style");
657
+ style.textContent = `${STYLE}:host { display: block; }`;
658
+ root.replaceChildren(style);
659
+ tooltips(root);
660
+ let panel;
661
+ let gone = false;
662
+ const open = (at, resumed) => {
663
+ panel = openPanel({
664
+ base,
665
+ root,
666
+ contextOf,
667
+ tabs,
668
+ tab: at,
669
+ remember,
670
+ element,
671
+ target,
672
+ pinned: true,
673
+ onClose: (shown, leaving) => {
674
+ panel = void 0;
675
+ if (!gone && !leaving) open(shown);
676
+ },
677
+ ...resumed === void 0 ? {} : { resumed },
678
+ reopen: (again, _, kept) => open(again, kept)
679
+ });
680
+ };
681
+ open(tab);
682
+ return () => {
683
+ gone = true;
684
+ panel?.close();
685
+ style.remove();
686
+ };
687
+ }
688
+ /**
689
+ * Shows the lightbox in `container`, at `at`, where the page puts it rather than over everything: as `popupIn`
690
+ * does the popup, for a page of deictic's own states. Its items step with its arrows; there's no backdrop, close
691
+ * or keys, and an item's actions run without closing it. Answers what removes it.
692
+ */
693
+ function lightboxIn(container, items, { at } = {}) {
694
+ const root = container.shadowRoot ?? container.attachShadow({ mode: "open" });
695
+ const style = document.createElement("style");
696
+ style.textContent = `${STYLE}:host { display: block; }`;
697
+ root.replaceChildren(style);
698
+ tooltips(root);
699
+ const { close } = openLightbox(root, items, {
700
+ ...at === void 0 ? {} : { at },
701
+ pinned: true
702
+ });
703
+ return () => {
704
+ close();
705
+ style.remove();
706
+ };
707
+ }
708
+ /** Starts the overlay, once per page: the client module runs again when a module importing it is replaced. */
709
+ function start({ base, shortcut: own, plugins: given }) {
710
+ const page = globalThis;
711
+ if (page[STARTED] === true) return;
712
+ page[STARTED] = true;
713
+ const host = document.createElement("deictic-overlay");
714
+ const root = host.attachShadow({ mode: "open" });
715
+ const style = document.createElement("style");
716
+ style.textContent = STYLE;
717
+ const box = div("box");
718
+ const label = div("label");
719
+ box.append(label);
720
+ const inner = div("box inner");
721
+ root.append(style, box, inner);
722
+ tooltips(root);
723
+ for (const type of [
724
+ "keydown",
725
+ "keypress",
726
+ "keyup"
727
+ ]) host.addEventListener(type, (e) => e.stopPropagation());
728
+ document.documentElement.append(host);
729
+ const apis = /* @__PURE__ */ new Map();
730
+ const optionsOf = new Map(given.map(({ plugin, options }) => [plugin.name, options]));
731
+ const contextOf = (name) => ({
732
+ base,
733
+ root,
734
+ options: optionsOf.get(name),
735
+ use: (other) => apis.get(other),
736
+ request: (route, init) => fetch(`${base}__deictic/plugins/${name}/${route}`, init),
737
+ open: (plugin, element) => openAt(plugin, element),
738
+ lightbox: (items, options) => openLightbox(root, items, options)
739
+ });
740
+ for (const { plugin } of given) {
741
+ if (plugin.setup === void 0) continue;
742
+ try {
743
+ apis.set(plugin.name, plugin.setup(contextOf(plugin.name)));
744
+ } catch (e) {
745
+ console.error(`deictic: ${plugin.name} failed to set up`, e);
746
+ }
747
+ }
748
+ const tabs = given.flatMap(({ plugin }) => plugin.mount === void 0 ? [] : [{
749
+ plugin,
750
+ mount: plugin.mount
751
+ }]);
752
+ started = {
753
+ base,
754
+ contextOf,
755
+ tabs,
756
+ open: (element, tab) => tabs.length > 0 && panel?.unsaved() !== true && openAt(tab ?? last().plugin.name, element)
757
+ };
758
+ const last = () => {
759
+ let name = null;
760
+ try {
761
+ name = localStorage.getItem(LAST);
762
+ } catch {}
763
+ return tabs.find((t) => t.plugin.name === name) ?? tabs[0];
764
+ };
765
+ const remember = (tab) => {
766
+ try {
767
+ localStorage.setItem(LAST, tab.plugin.name);
768
+ } catch {}
769
+ };
770
+ const plugins = [...own === void 0 || tabs.length === 0 ? [] : [{
771
+ shortcut: own,
772
+ gesture: gestureOf(own),
773
+ tab: last
774
+ }], ...given.flatMap(({ plugin, shortcut }) => {
775
+ if (shortcut === void 0) return [];
776
+ const tab = tabs.find((t) => t.plugin === plugin);
777
+ if (tab !== void 0) return [{
778
+ shortcut,
779
+ gesture: gestureOf(shortcut),
780
+ tab: () => tab
781
+ }];
782
+ console.error(`deictic: ${plugin.name} has a shortcut, but its page half has no \`mount\` to draw a tab with`);
783
+ return [];
784
+ })];
785
+ let pointer;
786
+ let frame = 0;
787
+ let panel;
788
+ /** The plugins whose keys are held: several, where they share keys and differ by button. */
789
+ const heldBy = (e) => plugins.filter((p) => p.gesture.held(e));
790
+ /** The plugin a press opens. */
791
+ const pressedBy = (e) => plugins.find((p) => p.gesture.held(e) && e.button === p.gesture.button);
792
+ /**
793
+ * The marked element at a point, and what's under the pointer inside it where that differs: down through
794
+ * open shadow roots but the overlay's own, as a page showing the popup (`popupIn`) has it in one.
795
+ */
796
+ const hitAt = (x, y) => {
797
+ let under = document.elementFromPoint(x, y) ?? void 0;
798
+ while (under !== void 0 && under !== host && under.shadowRoot !== null) {
799
+ const inside = under.shadowRoot.elementFromPoint(x, y);
800
+ if (inside === null || inside === under) break;
801
+ under = inside;
802
+ }
803
+ const el = under === void 0 || under === host ? void 0 : markedAround(under);
804
+ return el === void 0 ? void 0 : {
805
+ el,
806
+ pointed: under === el ? void 0 : under
807
+ };
808
+ };
809
+ const hide = () => {
810
+ box.classList.remove("shown");
811
+ inner.classList.remove("shown");
812
+ };
813
+ const show = (held) => {
814
+ frame = 0;
815
+ const hit = pointer === void 0 ? void 0 : hitAt(pointer.x, pointer.y);
816
+ const el = hit?.el;
817
+ const target = el === void 0 ? void 0 : targetOf(el);
818
+ if (el === void 0 || target === void 0) return hide();
819
+ place(box, el.getBoundingClientRect());
820
+ if (hit?.pointed === void 0) inner.classList.remove("shown");
821
+ else {
822
+ place(inner, hit.pointed.getBoundingClientRect());
823
+ inner.classList.add("shown");
824
+ }
825
+ const count = sameAs(el).length;
826
+ const names = held.map(({ tab, shortcut }) => {
827
+ const { plugin } = tab();
828
+ const name = plugin.label ?? plugin.name;
829
+ return shortcut.button === "left" ? name : `${name} (${shortcut.button})`;
830
+ });
831
+ const within = hit?.pointed === void 0 ? "" : ` › ${nameOf(hit.pointed)}`;
832
+ label.textContent = `${names.join(" / ")} · <${target.tag}> ${target.loc.file}:${target.loc.line}${count > 1 ? ` · ×${count}` : ""}${within}`;
833
+ label.classList.toggle("below", el.getBoundingClientRect().top < 24);
834
+ box.classList.add("shown");
835
+ };
836
+ const update = (e) => {
837
+ const held = heldBy(e);
838
+ if (held.length === 0) return hide();
839
+ if (frame === 0) frame = requestAnimationFrame(() => show(held));
840
+ };
841
+ window.addEventListener("mousemove", (e) => {
842
+ pointer = {
843
+ x: e.clientX,
844
+ y: e.clientY
845
+ };
846
+ update(e);
847
+ }, {
848
+ capture: true,
849
+ passive: true
850
+ });
851
+ window.addEventListener("keydown", update, true);
852
+ window.addEventListener("keyup", update, true);
853
+ window.addEventListener("blur", hide);
854
+ let press = {};
855
+ let opened;
856
+ window.addEventListener("mousedown", (e) => {
857
+ press = {};
858
+ if (e.composedPath().includes(host)) return;
859
+ if (pressedBy(e) !== void 0 && hitAt(e.clientX, e.clientY) !== void 0) e.preventDefault();
860
+ }, true);
861
+ const pressed = (e) => {
862
+ if (e.composedPath().includes(host)) return;
863
+ const found = pressedBy(e);
864
+ const hit = found === void 0 ? void 0 : hitAt(e.clientX, e.clientY);
865
+ const target = hit === void 0 ? void 0 : targetOf(hit.el);
866
+ if (found === void 0 || hit === void 0 || target === void 0) return;
867
+ e.preventDefault();
868
+ e.stopPropagation();
869
+ if (opened === press) return;
870
+ opened = press;
871
+ if (panel?.unsaved() === true) return;
872
+ openTab(found.tab(), hit.el, target, void 0, hit.pointed);
873
+ };
874
+ const openTab = (tab, el, target, resumed, pointed) => {
875
+ hide();
876
+ panel?.close();
877
+ panel = openPanel({
878
+ base,
879
+ root,
880
+ contextOf,
881
+ tabs,
882
+ tab,
883
+ remember,
884
+ element: el,
885
+ ...pointed === void 0 ? {} : { pointed },
886
+ target,
887
+ onClose: () => panel = void 0,
888
+ ...resumed === void 0 ? {} : { resumed },
889
+ reopen: (again, element, kept) => {
890
+ const found = element.isConnected ? element : sameAs(element)[0];
891
+ const at = found === void 0 ? void 0 : targetOf(found);
892
+ if (found === void 0 || at === void 0) return console.warn("deictic: the element is no longer on the page, so the popup stays closed");
893
+ openTab(again, found, at, kept, pointed?.isConnected === true && found === element ? pointed : void 0);
894
+ }
895
+ });
896
+ };
897
+ function openAt(name, given) {
898
+ const tab = tabs.find((t) => t.plugin.name === name);
899
+ const el = given.isConnected ? given : sameAs(given)[0] ?? void 0;
900
+ const target = el === void 0 ? void 0 : targetOf(el);
901
+ if (tab === void 0 || el === void 0 || target === void 0) return false;
902
+ openTab(tab, el, target);
903
+ return true;
904
+ }
905
+ for (const type of [
906
+ "click",
907
+ "auxclick",
908
+ "contextmenu"
909
+ ]) window.addEventListener(type, pressed, true);
910
+ }
911
+ function openPanel({ base, root, contextOf, tabs, tab: first, remember, element, pointed, target, pinned = false, onClose, resumed: coming, reopen }) {
912
+ let resumed = coming;
913
+ const panel = div("panel");
914
+ const head = div("head");
915
+ head.setAttribute("role", "tablist");
916
+ const bar = div("bar");
917
+ const where = document.createElement("a");
918
+ where.className = "where";
919
+ const file = document.createElement("span");
920
+ file.textContent = `${target.loc.file}:${target.loc.line}`;
921
+ where.append(file);
922
+ where.insertAdjacentHTML("beforeend", OUT);
923
+ where.title = "Open in editor";
924
+ where.setAttribute("aria-label", `Open ${file.textContent} in the editor`);
925
+ const at = {
926
+ file: target.loc.file,
927
+ line: String(target.loc.line),
928
+ column: String(target.loc.column)
929
+ };
930
+ where.href = `${base}__deictic/open?${new URLSearchParams(at)}`;
931
+ where.addEventListener("click", (e) => {
932
+ e.preventDefault();
933
+ fetch(where.href);
934
+ });
935
+ const undo = iconButton(UNDO, "Undo", "Nothing to undo");
936
+ const redo = iconButton(REDO, "Redo", "Nothing to redo");
937
+ undo.disabled = redo.disabled = true;
938
+ const close = iconButton(CLOSE, "Close", pinned ? "Close" : "Close (Esc)");
939
+ const buttons = tabs.map((tab) => {
940
+ const el = button(tab.plugin.label ?? tab.plugin.name, "");
941
+ el.className = "tab";
942
+ el.setAttribute("role", "tab");
943
+ el.addEventListener("click", () => show(tab));
944
+ return {
945
+ tab,
946
+ el
947
+ };
948
+ });
949
+ close.classList.add("close");
950
+ head.append(...buttons.map((b) => b.el), close);
951
+ bar.append(where, undo, redo);
952
+ const body = div("body");
953
+ body.setAttribute("role", "tabpanel");
954
+ const status = div("status");
955
+ panel.append(head, bar, body, status);
956
+ root.append(panel);
957
+ if (pinned) panel.classList.add("pinned");
958
+ const release = pinned ? void 0 : keepInView(panel, body, head, element);
959
+ const unmark = pinned ? void 0 : outline(root, element);
960
+ if (!pinned) window.dispatchEvent(new CustomEvent(POPUP_OPENED, { detail: {
961
+ element,
962
+ tab: first.plugin.name
963
+ } }));
964
+ let cleanup;
965
+ let holding;
966
+ const say = (text, problem = false) => {
967
+ status.textContent = text;
968
+ status.classList.toggle("problem", problem);
969
+ };
970
+ const post = async (route, body) => {
971
+ const res = await fetch(`${base}__deictic/${route}`, {
972
+ method: "POST",
973
+ headers: { "content-type": "application/json" },
974
+ body: JSON.stringify(body ?? {})
975
+ });
976
+ const json = await res.json();
977
+ if (!res.ok) throw new Error(json.problem ?? res.statusText);
978
+ return json;
979
+ };
980
+ const unmount = () => {
981
+ cleanup?.();
982
+ cleanup = void 0;
983
+ holding = void 0;
984
+ body.replaceChildren();
985
+ };
986
+ const step = (route) => post(route, {
987
+ plugin: current.plugin.name,
988
+ target
989
+ }).then(async ({ change }) => {
990
+ await load();
991
+ say(`${route === "undo" ? "Undid" : "Redid"} ${change.label}`);
992
+ }, (e) => say(messageOf(e), true)).finally(() => void steps());
993
+ let asked = 0;
994
+ const steps = async () => {
995
+ const ask = ++asked;
996
+ let history;
997
+ try {
998
+ const res = await fetch(`${base}__deictic/history?${new URLSearchParams({
999
+ plugin: current.plugin.name,
1000
+ ...at
1001
+ })}`);
1002
+ if (!res.ok || ask !== asked) return;
1003
+ history = await res.json();
1004
+ } catch {
1005
+ return;
1006
+ }
1007
+ const last = history.done.at(-1);
1008
+ const next = history.undone.at(-1);
1009
+ undo.disabled = last === void 0;
1010
+ undo.title = last === void 0 ? "Nothing to undo" : `Undo ${last.label}`;
1011
+ redo.disabled = next === void 0;
1012
+ redo.title = next === void 0 ? "Nothing to redo" : `Redo ${next.label}`;
1013
+ };
1014
+ panel.addEventListener("pointerenter", () => void steps());
1015
+ const done = (leaving = false) => {
1016
+ unmount();
1017
+ release?.();
1018
+ unmark?.();
1019
+ panel.remove();
1020
+ window.removeEventListener("keydown", onKey, true);
1021
+ window.removeEventListener("pointerdown", onOutside, true);
1022
+ if (!pinned) window.dispatchEvent(new CustomEvent(POPUP_CLOSED));
1023
+ onClose(current, leaving);
1024
+ };
1025
+ const onKey = (e) => {
1026
+ if (e.key !== "Escape" || root.querySelector("[data-deictic-modal]") !== null) return;
1027
+ e.preventDefault();
1028
+ e.stopPropagation();
1029
+ done();
1030
+ };
1031
+ const onOutside = (e) => {
1032
+ if (e.composedPath().includes(root.host)) return;
1033
+ if (holding === void 0) return done();
1034
+ shake(holding);
1035
+ };
1036
+ if (!pinned) {
1037
+ window.addEventListener("keydown", onKey, true);
1038
+ window.addEventListener("pointerdown", onOutside, true);
1039
+ }
1040
+ undo.addEventListener("click", () => void step("undo"));
1041
+ redo.addEventListener("click", () => void step("redo"));
1042
+ close.addEventListener("click", () => done());
1043
+ let current = first;
1044
+ let reads = 0;
1045
+ const load = async () => {
1046
+ const { plugin, mount } = current;
1047
+ const read = ++reads;
1048
+ body.style.minHeight = `${body.offsetHeight}px`;
1049
+ unmount();
1050
+ say("");
1051
+ const slow = setTimeout(() => read === reads && say("Reading…"), SLOW);
1052
+ let model;
1053
+ try {
1054
+ ({model} = await post("read", {
1055
+ plugin: plugin.name,
1056
+ target
1057
+ }));
1058
+ } catch (e) {
1059
+ if (read !== reads) return;
1060
+ body.style.minHeight = "";
1061
+ say(messageOf(e), true);
1062
+ return;
1063
+ } finally {
1064
+ clearTimeout(slow);
1065
+ }
1066
+ if (read !== reads) return;
1067
+ body.style.minHeight = "";
1068
+ say("");
1069
+ const back = current === first ? resumed : void 0;
1070
+ resumed = void 0;
1071
+ cleanup = mount(body, {
1072
+ ...contextOf(plugin.name),
1073
+ ...back === void 0 ? {} : { resumed: back },
1074
+ away: (state, task) => {
1075
+ const tab = current;
1076
+ done(true);
1077
+ task().then((result) => reopen(tab, element, {
1078
+ state,
1079
+ result
1080
+ }), (e) => reopen(tab, element, {
1081
+ state,
1082
+ problem: messageOf(e)
1083
+ }));
1084
+ },
1085
+ element,
1086
+ ...pointed === void 0 ? {} : { pointed },
1087
+ elements: sameAs(element),
1088
+ target,
1089
+ model,
1090
+ apply: async (op) => {
1091
+ say("Writing…");
1092
+ try {
1093
+ const result = await post("apply", {
1094
+ plugin: plugin.name,
1095
+ target,
1096
+ op
1097
+ });
1098
+ say("");
1099
+ steps();
1100
+ return result.model;
1101
+ } catch (e) {
1102
+ say(messageOf(e), true);
1103
+ throw e;
1104
+ }
1105
+ },
1106
+ close: () => done(),
1107
+ unsaved: (action) => {
1108
+ if (read === reads) holding = action;
1109
+ }
1110
+ });
1111
+ };
1112
+ const show = (tab) => {
1113
+ current = tab;
1114
+ for (const b of buttons) b.el.setAttribute("aria-selected", String(b.tab === tab));
1115
+ remember(tab);
1116
+ steps();
1117
+ load();
1118
+ };
1119
+ show(first);
1120
+ return {
1121
+ close: done,
1122
+ unsaved: () => holding !== void 0
1123
+ };
1124
+ }
1125
+ /** Shakes a panel's button, in view: what a press outside does where closing would lose something. A blink where motion is reduced. */
1126
+ function shake(el) {
1127
+ el.scrollIntoView({ block: "nearest" });
1128
+ if (matchMedia("(prefers-reduced-motion: reduce)").matches) return void el.animate({ opacity: [
1129
+ 1,
1130
+ .3,
1131
+ 1,
1132
+ .3,
1133
+ 1
1134
+ ] }, 400);
1135
+ el.animate({ translate: [
1136
+ "0",
1137
+ "-5px",
1138
+ "5px",
1139
+ "-4px",
1140
+ "4px",
1141
+ "-2px",
1142
+ "0"
1143
+ ] }, {
1144
+ duration: 360,
1145
+ easing: "ease-out"
1146
+ });
1147
+ }
1148
+ /** Puts an outline over `r`. */
1149
+ function place(el, r) {
1150
+ Object.assign(el.style, {
1151
+ left: `${r.left}px`,
1152
+ top: `${r.top}px`,
1153
+ width: `${r.width}px`,
1154
+ height: `${r.height}px`
1155
+ });
1156
+ }
1157
+ /** How far from the element the popup's outline is drawn, in pixels. */
1158
+ const ROOM = 4;
1159
+ /**
1160
+ * Outlines the element the popup is open on, for as long as it is: which one it's about, where several look alike
1161
+ * or the popup covers it. Dashed, with no fill and a little room around the element, so nothing of it is drawn on:
1162
+ * 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
1163
+ * scrolling, its layout and what a panel previews on it. Answers what removes it.
1164
+ */
1165
+ function outline(root, element) {
1166
+ const box = div("box on");
1167
+ root.append(box);
1168
+ let frame = 0;
1169
+ let was = "";
1170
+ const follow = () => {
1171
+ frame = requestAnimationFrame(follow);
1172
+ const r = element.getBoundingClientRect();
1173
+ const now = element.isConnected ? `${r.left},${r.top},${r.width},${r.height}` : "";
1174
+ if (now === was) return;
1175
+ was = now;
1176
+ box.classList.toggle("shown", now !== "");
1177
+ place(box, new DOMRect(r.left - ROOM, r.top - ROOM, r.width + 8, r.height + 8));
1178
+ };
1179
+ follow();
1180
+ return () => {
1181
+ cancelAnimationFrame(frame);
1182
+ box.remove();
1183
+ };
1184
+ }
1185
+ const MARGIN = 8;
1186
+ const LEAST = 160;
1187
+ /** How far a press on the head moves, in pixels, before it's a drag: a click there leaves the popup following. */
1188
+ const SLACK = 3;
1189
+ /**
1190
+ * Keeps a panel beside `element` and inside the viewport, as its tabs fill it, the window resizes and the element
1191
+ * moves with the page: below, or above where it fits better, its height capped to the room there, so the body
1192
+ * scrolls rather than the panel leaving the window. Dragged by `handle`, it stays where it's put, in the window,
1193
+ * and follows the element no more. Fixed, so it never changes the page's own scrolling. Answers a release.
1194
+ */
1195
+ function keepInView(panel, body, handle, element) {
1196
+ let frame = 0;
1197
+ let timer;
1198
+ let r = element.getBoundingClientRect();
1199
+ let moved;
1200
+ const fit = () => {
1201
+ cancelAnimationFrame(frame);
1202
+ clearTimeout(timer);
1203
+ frame = 0;
1204
+ timer = void 0;
1205
+ const width = document.documentElement.clientWidth;
1206
+ const height = document.documentElement.clientHeight;
1207
+ const wanted = panel.offsetHeight - body.clientHeight + body.scrollHeight;
1208
+ if (moved !== void 0) {
1209
+ const room = Math.max(0, height - 16);
1210
+ Object.assign(panel.style, {
1211
+ left: `${Math.max(MARGIN, Math.min(moved.left, width - MARGIN - panel.offsetWidth))}px`,
1212
+ top: `${Math.max(MARGIN, Math.min(moved.top, height - MARGIN - Math.min(wanted, room)))}px`,
1213
+ maxHeight: `${room}px`
1214
+ });
1215
+ return;
1216
+ }
1217
+ const top = Math.min(Math.max(r.top, 0), height);
1218
+ const bottom = Math.min(Math.max(r.bottom, 0), height);
1219
+ const below = height - bottom - 16;
1220
+ const above = top - 16;
1221
+ const cap = Math.max(0, Math.min(height * .6, height - 16));
1222
+ let at;
1223
+ let room;
1224
+ if (Math.max(below, above) < LEAST) {
1225
+ room = height - 16;
1226
+ at = Math.min(Math.max(bottom + MARGIN, MARGIN), height - MARGIN - Math.min(wanted, room));
1227
+ } else if (wanted <= below || below >= above) {
1228
+ room = Math.min(below, cap);
1229
+ at = bottom + MARGIN;
1230
+ } else {
1231
+ room = Math.min(above, cap);
1232
+ at = top - MARGIN - Math.min(wanted, room);
1233
+ }
1234
+ Object.assign(panel.style, {
1235
+ left: `${Math.max(MARGIN, Math.min(r.left, width - MARGIN - panel.offsetWidth))}px`,
1236
+ top: `${at}px`,
1237
+ bottom: "",
1238
+ maxHeight: `${room}px`
1239
+ });
1240
+ };
1241
+ const later = () => {
1242
+ frame ||= requestAnimationFrame(fit);
1243
+ timer ??= setTimeout(fit, 50);
1244
+ };
1245
+ fit();
1246
+ let watching = requestAnimationFrame(function watch() {
1247
+ if (moved !== void 0) return;
1248
+ watching = requestAnimationFrame(watch);
1249
+ if (!element.isConnected) return;
1250
+ const now = element.getBoundingClientRect();
1251
+ if (now.left === r.left && now.top === r.top && now.width === r.width && now.height === r.height) return;
1252
+ r = now;
1253
+ fit();
1254
+ });
1255
+ let from;
1256
+ const down = (e) => {
1257
+ if (e.button !== 0 || e.target.closest("button") !== null) return;
1258
+ e.preventDefault();
1259
+ const at = panel.getBoundingClientRect();
1260
+ from = {
1261
+ x: e.clientX,
1262
+ y: e.clientY,
1263
+ left: at.left,
1264
+ top: at.top
1265
+ };
1266
+ handle.setPointerCapture(e.pointerId);
1267
+ };
1268
+ const move = (e) => {
1269
+ if (from === void 0) return;
1270
+ const x = e.clientX - from.x;
1271
+ const y = e.clientY - from.y;
1272
+ if (moved === void 0 && Math.hypot(x, y) < SLACK) return;
1273
+ moved = {
1274
+ left: from.left + x,
1275
+ top: from.top + y
1276
+ };
1277
+ panel.classList.add("dragging");
1278
+ fit();
1279
+ };
1280
+ const up = () => {
1281
+ from = void 0;
1282
+ panel.classList.remove("dragging");
1283
+ };
1284
+ handle.addEventListener("pointerdown", down);
1285
+ handle.addEventListener("pointermove", move);
1286
+ handle.addEventListener("pointerup", up);
1287
+ handle.addEventListener("pointercancel", up);
1288
+ const resized = new ResizeObserver(later);
1289
+ resized.observe(panel);
1290
+ const changed = new MutationObserver(later);
1291
+ changed.observe(panel, {
1292
+ childList: true,
1293
+ subtree: true,
1294
+ characterData: true
1295
+ });
1296
+ window.addEventListener("resize", later);
1297
+ return () => {
1298
+ cancelAnimationFrame(frame);
1299
+ cancelAnimationFrame(watching);
1300
+ clearTimeout(timer);
1301
+ resized.disconnect();
1302
+ changed.disconnect();
1303
+ window.removeEventListener("resize", later);
1304
+ };
1305
+ }
1306
+ function div(className) {
1307
+ const el = document.createElement("div");
1308
+ el.className = className;
1309
+ return el;
1310
+ }
1311
+ /** A button that is a drawing: named for what it does, as its title, the tooltip, changes with what it would do. */
1312
+ function iconButton(svg, name, title) {
1313
+ const el = button("", title);
1314
+ el.className = "icon";
1315
+ el.innerHTML = svg;
1316
+ el.setAttribute("aria-label", name);
1317
+ return el;
1318
+ }
1319
+ function button(text, title) {
1320
+ const el = document.createElement("button");
1321
+ el.type = "button";
1322
+ el.textContent = text;
1323
+ el.title = title;
1324
+ return el;
1325
+ }
1326
+ const messageOf = (e) => e instanceof Error ? e.message : String(e);
1327
+ export { POPUP_CLOSED, POPUP_OPENED, drawBars, lightboxIn, liveWaveform, openPopup, peaksOf, popupIn, start, targetOf };
1328
+
1329
+ //# sourceMappingURL=index.js.map