deictic 0.0.1 → 0.2.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 (176) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +167 -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 +95 -0
  20. package/dist/client/index.d.ts.map +1 -0
  21. package/dist/client/index.js +1333 -0
  22. package/dist/client/index.js.map +1 -0
  23. package/dist/index.d.ts +23 -0
  24. package/dist/index.js +10 -0
  25. package/dist/index.js.map +1 -0
  26. package/dist/loader.d.ts +15 -0
  27. package/dist/loader.d.ts.map +1 -0
  28. package/dist/loader.js +17 -0
  29. package/dist/loader.js.map +1 -0
  30. package/dist/next.d.ts +17 -0
  31. package/dist/next.d.ts.map +1 -0
  32. package/dist/next.js +150 -0
  33. package/dist/next.js.map +1 -0
  34. package/dist/recommended/index.d.ts +25 -0
  35. package/dist/recommended/index.d.ts.map +1 -0
  36. package/dist/recommended/index.js +19 -0
  37. package/dist/recommended/index.js.map +1 -0
  38. package/dist/recordings/client.d.ts +4 -0
  39. package/dist/recordings/client.d.ts.map +1 -0
  40. package/dist/recordings/client.js +376 -0
  41. package/dist/recordings/client.js.map +1 -0
  42. package/dist/recordings/index.d.ts +2 -0
  43. package/dist/recordings/index.js +3 -0
  44. package/dist/screenshots/client.d.ts +23 -0
  45. package/dist/screenshots/client.d.ts.map +1 -0
  46. package/dist/screenshots/client.js +76 -0
  47. package/dist/screenshots/client.js.map +1 -0
  48. package/dist/screenshots/dom.d.ts +4 -0
  49. package/dist/screenshots/dom.d.ts.map +1 -0
  50. package/dist/screenshots/dom.js +92 -0
  51. package/dist/screenshots/dom.js.map +1 -0
  52. package/dist/screenshots/index.d.ts +2 -0
  53. package/dist/screenshots/index.js +98 -0
  54. package/dist/screenshots/index.js.map +1 -0
  55. package/dist/screenshots/rrweb.d.ts +9 -0
  56. package/dist/screenshots/rrweb.d.ts.map +1 -0
  57. package/dist/screenshots/rrweb.js +60 -0
  58. package/dist/screenshots/rrweb.js.map +1 -0
  59. package/dist/shared/endpoints.js +354 -0
  60. package/dist/shared/endpoints.js.map +1 -0
  61. package/dist/shared/index.d.ts +226 -0
  62. package/dist/shared/index.d.ts.map +1 -0
  63. package/dist/shared/index2.d.ts +98 -0
  64. package/dist/shared/index2.d.ts.map +1 -0
  65. package/dist/shared/index3.d.ts +86 -0
  66. package/dist/shared/index3.d.ts.map +1 -0
  67. package/dist/shared/index4.d.ts +184 -0
  68. package/dist/shared/index4.d.ts.map +1 -0
  69. package/dist/shared/index5.d.ts +70 -0
  70. package/dist/shared/index5.d.ts.map +1 -0
  71. package/dist/shared/integration.d.ts +20 -0
  72. package/dist/shared/integration.d.ts.map +1 -0
  73. package/dist/shared/integration.js +279 -0
  74. package/dist/shared/integration.js.map +1 -0
  75. package/dist/shared/kept.js +23 -0
  76. package/dist/shared/kept.js.map +1 -0
  77. package/dist/shared/marks.js +14 -0
  78. package/dist/shared/marks.js.map +1 -0
  79. package/dist/shared/playwright.js +105 -0
  80. package/dist/shared/playwright.js.map +1 -0
  81. package/dist/shared/recordings.js +246 -0
  82. package/dist/shared/recordings.js.map +1 -0
  83. package/dist/shared/shortcut.js +63 -0
  84. package/dist/shared/shortcut.js.map +1 -0
  85. package/dist/shared/tasks.js +521 -0
  86. package/dist/shared/tasks.js.map +1 -0
  87. package/dist/shared/waveform.js +93 -0
  88. package/dist/shared/waveform.js.map +1 -0
  89. package/dist/tailwind/client.d.ts +4 -0
  90. package/dist/tailwind/client.d.ts.map +1 -0
  91. package/dist/tailwind/client.js +613 -0
  92. package/dist/tailwind/client.js.map +1 -0
  93. package/dist/tailwind/index.d.ts +2 -0
  94. package/dist/tailwind/index.js +557 -0
  95. package/dist/tailwind/index.js.map +1 -0
  96. package/dist/tasks/client.d.ts +6 -0
  97. package/dist/tasks/client.d.ts.map +1 -0
  98. package/dist/tasks/client.js +642 -0
  99. package/dist/tasks/client.js.map +1 -0
  100. package/dist/tasks/index.d.ts +2 -0
  101. package/dist/tasks/index.js +1 -0
  102. package/dist/vite.d.ts +16 -0
  103. package/dist/vite.d.ts.map +1 -0
  104. package/dist/vite.js +61 -0
  105. package/dist/vite.js.map +1 -0
  106. package/dist/voice/client.d.ts +4 -0
  107. package/dist/voice/client.d.ts.map +1 -0
  108. package/dist/voice/client.js +66 -0
  109. package/dist/voice/client.js.map +1 -0
  110. package/dist/voice/index.d.ts +47 -0
  111. package/dist/voice/index.d.ts.map +1 -0
  112. package/dist/voice/index.js +74 -0
  113. package/dist/voice/index.js.map +1 -0
  114. package/dist/webpack.d.ts +50 -0
  115. package/dist/webpack.d.ts.map +1 -0
  116. package/dist/webpack.js +78 -0
  117. package/dist/webpack.js.map +1 -0
  118. package/package.json +307 -7
  119. package/skills/deictic/SKILL.md +47 -0
  120. package/skills/deictic/references/install.md +96 -0
  121. package/skills/deictic/references/recordings.md +83 -0
  122. package/skills/deictic/references/tasks.md +79 -0
  123. package/skills/deictic/references/watch.md +43 -0
  124. package/skills/deictic/scripts/frame.mjs +62 -0
  125. package/skills/deictic/scripts/lib.mjs +211 -0
  126. package/skills/deictic/scripts/recording.mjs +186 -0
  127. package/skills/deictic/scripts/task.mjs +84 -0
  128. package/skills/deictic/scripts/transcribe.mjs +93 -0
  129. package/skills/deictic/scripts/voice.mjs +50 -0
  130. package/skills/deictic/scripts/watch.mjs +51 -0
  131. package/src/astro.ts +34 -0
  132. package/src/bun.ts +79 -0
  133. package/src/client/index.ts +846 -0
  134. package/src/client/lightbox.ts +256 -0
  135. package/src/client/style.ts +243 -0
  136. package/src/client/tooltip.ts +78 -0
  137. package/src/client/waveform.ts +98 -0
  138. package/src/index.ts +247 -0
  139. package/src/loader.ts +28 -0
  140. package/src/next.ts +133 -0
  141. package/src/plugins/annotations/client.ts +301 -0
  142. package/src/plugins/annotations/draw.ts +169 -0
  143. package/src/plugins/annotations/index.ts +105 -0
  144. package/src/plugins/annotations/svg.ts +61 -0
  145. package/src/plugins/recommended/index.ts +35 -0
  146. package/src/plugins/recordings/client.ts +316 -0
  147. package/src/plugins/recordings/index.ts +175 -0
  148. package/src/plugins/recordings/marks.ts +11 -0
  149. package/src/plugins/recordings/ours.ts +70 -0
  150. package/src/plugins/recordings/replay.ts +165 -0
  151. package/src/plugins/screenshots/client.ts +64 -0
  152. package/src/plugins/screenshots/dom.ts +79 -0
  153. package/src/plugins/screenshots/index.ts +194 -0
  154. package/src/plugins/screenshots/playwright.ts +119 -0
  155. package/src/plugins/screenshots/rrweb.ts +45 -0
  156. package/src/plugins/tailwind/catalog.ts +396 -0
  157. package/src/plugins/tailwind/client.ts +669 -0
  158. package/src/plugins/tailwind/index.ts +267 -0
  159. package/src/plugins/tasks/client.ts +622 -0
  160. package/src/plugins/tasks/index.ts +557 -0
  161. package/src/plugins/tasks/local.ts +170 -0
  162. package/src/plugins/voice/client.ts +58 -0
  163. package/src/plugins/voice/index.ts +114 -0
  164. package/src/shortcut.ts +83 -0
  165. package/src/tools/astro.ts +81 -0
  166. package/src/tools/editor.ts +22 -0
  167. package/src/tools/endpoints.ts +163 -0
  168. package/src/tools/history.ts +203 -0
  169. package/src/tools/integration.ts +108 -0
  170. package/src/tools/kept.ts +21 -0
  171. package/src/tools/locate.ts +94 -0
  172. package/src/tools/plugins.ts +41 -0
  173. package/src/tools/source.ts +72 -0
  174. package/src/vite.ts +81 -0
  175. package/src/webpack.ts +107 -0
  176. package/index.js +0 -1
@@ -0,0 +1,301 @@
1
+ /**
2
+ * Annotations in the page. `setup` keeps them (in the tab's session storage, per page) and shows them in a
3
+ * layer of deictic's overlay that lets the pointer through; it answers the API other plugins `use`. The
4
+ * panel is annotate mode: a toolbar, and while it's open a surface over the page that takes the pointer, so
5
+ * drawing never clicks the page. Esc or Done leaves it; the marks stay.
6
+ *
7
+ * The layer is deictic's, so a screenshot leaves it out with the rest of the overlay and draws the marks
8
+ * itself from `svg(area)`: one way to draw them, wherever a shot is rendered. `edit` draws on a picture
9
+ * instead, a shot most often, in the same toolbar over everything, and `flatten` makes the PNG of it.
10
+ */
11
+ import { defineClientPlugin } from '#src/index.ts';
12
+
13
+ import { button, drawing, measurer, TOOLS_STYLE } from './draw.ts';
14
+ import type { Annotation, AnnotationEvent, Annotations, Area, MarkSet, Picture, Point, Shape } from './index.ts';
15
+ import { type Measure, toSvg } from './svg.ts';
16
+
17
+ /** Under deictic's outline and panels, over the page. */
18
+ const LAYER = 2147483645;
19
+ /** Over everything of deictic's, the popup too. */
20
+ const TOP = 2147483647;
21
+
22
+ const scroll = (): Point => ({ x: scrollX, y: scrollY });
23
+ const viewport = (): Area => ({ x: 0, y: 0, width: document.documentElement.clientWidth, height: document.documentElement.clientHeight });
24
+ const mark = (shape: Shape, color: string): Annotation => ({ id: crypto.randomUUID(), color, shape, at: Date.now() });
25
+
26
+ /** What's kept for this page in the tab's session, read as it can be. */
27
+ function stored(key: string): Annotation[] {
28
+ try {
29
+ const kept: unknown = JSON.parse(sessionStorage.getItem(key) ?? '[]');
30
+ return Array.isArray(kept) ? (kept as Annotation[]) : [];
31
+ } catch {
32
+ // Storage can be off, or hold something else: no marks is the page as it was.
33
+ return [];
34
+ }
35
+ }
36
+
37
+ function fixed(z: number): HTMLDivElement {
38
+ const el = document.createElement('div');
39
+ el.style.cssText = `position:fixed;inset:0;z-index:${z};pointer-events:none`;
40
+ return el;
41
+ }
42
+
43
+ const EDITOR_STYLE = `
44
+ .annotations-editor { position: fixed; inset: 0; z-index: ${TOP}; box-sizing: border-box; padding: 16px; display: flex;
45
+ flex-direction: column; align-items: center; justify-content: center; gap: 8px; background: rgb(0 0 0 / 0.75);
46
+ font: 13px/1.4 system-ui, sans-serif; color: var(--deictic-fg); }
47
+ .annotations-editor .bar { box-sizing: border-box; width: min(100%, 720px); padding: 6px 8px; border: 1px solid var(--deictic-line);
48
+ border-radius: 8px; background: var(--deictic-bg); }
49
+ .annotations-editor .stage { position: relative; line-height: 0; box-shadow: 0 12px 32px rgb(0 0 0 / 0.5); touch-action: none; }
50
+ .annotations-editor img { display: block; max-width: calc(100vw - 32px); max-height: calc(100vh - 120px); user-select: none; }
51
+ .annotations-editor .marks { position: absolute; inset: 0; pointer-events: none; }
52
+ `;
53
+
54
+ /** Loads an image, decoded. */
55
+ async function image(src: string): Promise<HTMLImageElement> {
56
+ const el = new Image();
57
+ el.src = src;
58
+ await el.decode();
59
+ return el;
60
+ }
61
+
62
+ /** The marks over a whole picture, as an SVG whose view is the picture's CSS pixels. */
63
+ const over = (picture: Picture, marks: readonly Annotation[], measure?: Measure) =>
64
+ toSvg(marks, { x: 0, y: 0, width: picture.width, height: picture.height }, { x: 0, y: 0 }, measure);
65
+
66
+ /**
67
+ * The picture over everything, to draw on: the marks on Save, nothing on Cancel, Esc or `signal`. It holds
68
+ * Esc while it's open (`data-deictic-modal`), so Esc leaves it, not the popup under it.
69
+ */
70
+ function edit(
71
+ root: ShadowRoot,
72
+ picture: Picture,
73
+ start: readonly Annotation[],
74
+ { color, measure, signal }: { color: string; measure: Measure; signal?: AbortSignal },
75
+ ): Promise<Annotation[] | undefined> {
76
+ return new Promise((resolve) => {
77
+ let marks = [...start];
78
+ const backdrop = document.createElement('div');
79
+ backdrop.className = 'annotations-editor';
80
+ backdrop.dataset.deicticModal = '';
81
+ const style = document.createElement('style');
82
+ style.textContent = TOOLS_STYLE + EDITOR_STYLE;
83
+ const bar = document.createElement('div');
84
+ bar.className = 'bar';
85
+ const stage = document.createElement('div');
86
+ stage.className = 'stage';
87
+ const img = Object.assign(document.createElement('img'), { src: picture.url, alt: 'The screenshot', draggable: false });
88
+ const layer = document.createElement('div');
89
+ layer.className = 'marks';
90
+ stage.append(img, layer);
91
+
92
+ const show = (preview?: Shape) => {
93
+ layer.innerHTML = over(picture, preview === undefined ? marks : [...marks, mark(preview, color)], measure);
94
+ // Drawn at the size the picture shows at, whatever that is: the view is the picture's own pixels.
95
+ const svg = layer.firstElementChild;
96
+ svg?.setAttribute('width', '100%');
97
+ svg?.setAttribute('height', '100%');
98
+ };
99
+ const { tools, hint } = drawing({
100
+ surface: stage,
101
+ at: (e) => {
102
+ const r = stage.getBoundingClientRect();
103
+ return { x: ((e.clientX - r.left) * picture.width) / r.width, y: ((e.clientY - r.top) * picture.height) / r.height };
104
+ },
105
+ preview: (shape) => show(shape),
106
+ add: (shape) => {
107
+ marks = [...marks, mark(shape, color)];
108
+ show();
109
+ },
110
+ removeLast: () => {
111
+ marks = marks.slice(0, -1);
112
+ show();
113
+ },
114
+ clear: () => {
115
+ marks = [];
116
+ show();
117
+ },
118
+ });
119
+ const end = (result: Annotation[] | undefined) => {
120
+ backdrop.remove();
121
+ window.removeEventListener('keydown', onKey, true);
122
+ signal?.removeEventListener('abort', cancel);
123
+ resolve(result);
124
+ };
125
+ const cancel = () => end(undefined);
126
+ const onKey = (e: KeyboardEvent) => {
127
+ if (e.key !== 'Escape') return;
128
+ e.stopPropagation();
129
+ cancel();
130
+ };
131
+ const save = button('Save', 'Keep these marks', () => end(marks));
132
+ save.classList.add('primary');
133
+ const back = button('Cancel', 'Leave it as it was (Esc)', cancel);
134
+ back.classList.add('end');
135
+ tools.append(back, save);
136
+ bar.append(tools, hint);
137
+ backdrop.append(style, bar, stage);
138
+ root.append(backdrop);
139
+ window.addEventListener('keydown', onKey, true);
140
+ signal?.addEventListener('abort', cancel);
141
+ show();
142
+ });
143
+ }
144
+
145
+ /**
146
+ * Marks shown on the page, in a layer of the overlay that lets the pointer through, redrawn as it
147
+ * scrolls. With a `key`, kept in the tab's session storage under it, per page; without, kept by nothing.
148
+ */
149
+ function marksOnPage(root: ShadowRoot, color: string, measure: Measure, key?: string): { marks: MarkSet; remove: () => void } {
150
+ let list = key === undefined ? [] : stored(key);
151
+ const listeners = new Set<(event: AnnotationEvent) => void>();
152
+ const layer = fixed(LAYER);
153
+ root.append(layer);
154
+
155
+ let frame = 0;
156
+ const draw = () => {
157
+ frame = 0;
158
+ layer.innerHTML = list.length === 0 ? '' : toSvg(list, viewport(), scroll(), measure);
159
+ };
160
+ const redraw = () => {
161
+ if (frame === 0) frame = requestAnimationFrame(draw);
162
+ };
163
+ window.addEventListener('scroll', redraw, { capture: true, passive: true });
164
+ window.addEventListener('resize', redraw, { passive: true });
165
+ const changed = (event: AnnotationEvent) => {
166
+ if (key !== undefined) {
167
+ try {
168
+ sessionStorage.setItem(key, JSON.stringify(list));
169
+ } catch {
170
+ // Not kept past a reload, then; the page still shows them.
171
+ }
172
+ }
173
+ draw();
174
+ for (const listener of listeners) listener(event);
175
+ };
176
+ draw();
177
+
178
+ const marks: MarkSet = {
179
+ list: () => [...list],
180
+ add: (shape, given = color) => {
181
+ const annotation = mark(shape, given);
182
+ list = [...list, annotation];
183
+ changed({ type: 'add', annotation });
184
+ return annotation;
185
+ },
186
+ remove: (id) => {
187
+ list = list.filter((a) => a.id !== id);
188
+ changed({ type: 'remove', id });
189
+ },
190
+ clear: () => {
191
+ list = [];
192
+ changed({ type: 'clear' });
193
+ },
194
+ subscribe: (listener) => {
195
+ listeners.add(listener);
196
+ return () => listeners.delete(listener);
197
+ },
198
+ svg: (area) => (list.length === 0 ? undefined : toSvg(list, area, scroll(), measure)),
199
+ };
200
+ const remove = () => {
201
+ window.removeEventListener('scroll', redraw, { capture: true });
202
+ window.removeEventListener('resize', redraw);
203
+ cancelAnimationFrame(frame);
204
+ layer.remove();
205
+ listeners.clear();
206
+ };
207
+ return { marks, remove };
208
+ }
209
+
210
+ /**
211
+ * Drawing on the page: a surface over it, under the popup, and the toolbar. Where the page stays usable
212
+ * (`usable`), the surface takes the pointer only while a drawing tool is picked.
213
+ */
214
+ function onPage(root: ShadowRoot, marks: MarkSet, color: string, usable: boolean) {
215
+ const surface = fixed(LAYER + 1);
216
+ surface.style.pointerEvents = usable ? 'none' : 'auto';
217
+ const preview = fixed(0);
218
+ surface.append(preview);
219
+ root.append(surface);
220
+ const { tools, hint } = drawing({
221
+ surface,
222
+ at: (e) => ({ x: e.clientX + scrollX, y: e.clientY + scrollY }),
223
+ preview: (shape) => {
224
+ preview.innerHTML = shape === undefined ? '' : toSvg([mark(shape, color)], viewport(), scroll());
225
+ },
226
+ add: (shape) => marks.add(shape),
227
+ removeLast: () => {
228
+ const last = marks.list().at(-1);
229
+ if (last !== undefined) marks.remove(last.id);
230
+ },
231
+ clear: () => marks.clear(),
232
+ ...(usable ? { drawing: (on: boolean) => (surface.style.pointerEvents = on ? 'auto' : 'none') } : {}),
233
+ });
234
+ return { tools, hint, surface };
235
+ }
236
+
237
+ /** The picture with the marks drawn over it, as a PNG at the picture's own resolution. */
238
+ async function flatten(picture: Picture, marks: readonly Annotation[], measure: Measure): Promise<Blob> {
239
+ const base = await image(picture.url);
240
+ const canvas = document.createElement('canvas');
241
+ canvas.width = base.naturalWidth;
242
+ canvas.height = base.naturalHeight;
243
+ const ctx = canvas.getContext('2d');
244
+ if (ctx === null) throw new Error("annotations: this browser won't draw to a canvas");
245
+ ctx.drawImage(base, 0, 0);
246
+ if (marks.length > 0) {
247
+ // Shapes only, no `foreignObject`, so drawing the SVG leaves the canvas readable.
248
+ const svg = await image(`data:image/svg+xml;charset=utf-8,${encodeURIComponent(over(picture, marks, measure))}`);
249
+ ctx.drawImage(svg, 0, 0, canvas.width, canvas.height);
250
+ }
251
+ return new Promise((done, fail) =>
252
+ canvas.toBlob((blob) => (blob === null ? fail(new Error('annotations: the picture drew nothing')) : done(blob)), 'image/png'),
253
+ );
254
+ }
255
+
256
+ export default defineClientPlugin<null, never, Annotations>({
257
+ name: 'annotations',
258
+ label: 'Annotate',
259
+ setup: ({ root, options }) => {
260
+ const color = (options as { color: string }).color;
261
+ const measure = measurer();
262
+ const page = marksOnPage(root, color, measure, `deictic:annotations:${location.pathname}`);
263
+
264
+ const api: Annotations = {
265
+ ...page.marks,
266
+ edit: (picture, marks = [], { signal } = {}) => edit(root, picture, marks, { color, measure, ...(signal ? { signal } : {}) }),
267
+ flatten: (picture, marks) => flatten(picture, marks, measure),
268
+ tools: () => {
269
+ // Its own marks, kept by nothing: they show while it's open, and go with it.
270
+ const own = marksOnPage(root, color, measure);
271
+ const { tools, surface } = onPage(root, own.marks, color, true);
272
+ const element = document.createElement('div');
273
+ element.append(Object.assign(document.createElement('style'), { textContent: TOOLS_STYLE }), tools);
274
+ return {
275
+ element,
276
+ marks: own.marks,
277
+ close: () => {
278
+ surface.remove();
279
+ own.remove();
280
+ },
281
+ };
282
+ },
283
+ };
284
+ return api;
285
+ },
286
+
287
+ mount: (panel, { root, use, close, options }) => {
288
+ const { color } = options as { color: string };
289
+ const marks = use('annotations');
290
+ if (marks === undefined) return;
291
+ const style = document.createElement('style');
292
+ style.textContent = TOOLS_STYLE;
293
+ // Over the page while annotate mode is open: it takes the pointer, so drawing never clicks the page.
294
+ const { tools, hint, surface } = onPage(root, marks, color, false);
295
+ const done = button('Done', 'Leave annotate mode (Esc)', close);
296
+ done.classList.add('primary', 'end');
297
+ tools.append(done);
298
+ panel.append(style, tools, hint);
299
+ return () => surface.remove();
300
+ },
301
+ });
@@ -0,0 +1,169 @@
1
+ /**
2
+ * Drawing with the pointer, wherever marks go: the page in annotate mode, or a picture in the editor.
3
+ * Each says where an event is in its marks' coordinates; this is the toolbar and the gestures.
4
+ */
5
+ import type { Point, Shape } from './index.ts';
6
+ import { FONT, guess, type Measure } from './svg.ts';
7
+
8
+ export const TOOLS_STYLE = `
9
+ .annotations-tools { display: flex; flex-wrap: wrap; gap: 4px; }
10
+ .annotations-tools .icon { display: inline-flex; align-items: center; justify-content: center; padding: 3px 5px; }
11
+ .annotations-tools .icon svg { width: 16px; height: 16px; }
12
+ .annotations-tools .gap { width: 1px; align-self: stretch; margin: 2px 4px; background: var(--deictic-line); }
13
+ .annotations-tools button[aria-pressed='true'] { background: var(--deictic-accent); border-color: var(--deictic-accent); color: var(--deictic-on-accent); }
14
+ .annotations-tools .end { margin-left: auto; }
15
+ .annotations-hint { margin: 6px 0 0; font-size: 12px; color: var(--deictic-muted); }
16
+ `;
17
+
18
+ type Tool = 'pointer' | 'box' | 'arrow' | 'pen' | 'note';
19
+ const TOOLS: Tool[] = ['box', 'arrow', 'pen', 'note'];
20
+ const NAMES: Record<Tool, string> = {
21
+ pointer: 'Use the page',
22
+ box: 'Draw a box',
23
+ arrow: 'Draw an arrow',
24
+ pen: 'Draw freehand',
25
+ note: 'Add a note',
26
+ };
27
+
28
+ /** Each icon's strokes, on a 24-pixel grid: drawn here, so the page half needs nothing to show them. */
29
+ const ICONS = {
30
+ pointer: '<path d="M5 3l14 7-6 2-2 6z"/>',
31
+ box: '<rect x="4" y="4" width="16" height="16" rx="2"/>',
32
+ arrow: '<path d="M6 18 18 6M9 6h9v9"/>',
33
+ pen: '<path d="M4 20l1-4L16 5a2.1 2.1 0 0 1 3 3L8 19z"/>',
34
+ note: '<path d="M4 5h16v11H9l-5 4zM8 9h8M8 12h5"/>',
35
+ undo: '<path d="M9 14 4 9l5-5M4 9h10a6 6 0 0 1 0 12h-3"/>',
36
+ clear: '<path d="M4 7h16M10 11v6M14 11v6M6 7l1 13h10l1-13M9 7V4h6v3"/>',
37
+ } as const;
38
+
39
+ /** A button that shows an icon, with its name as its tooltip and for a screen reader. */
40
+ export function iconButton(icon: keyof typeof ICONS, name: string, run: () => void): HTMLButtonElement {
41
+ const el = button('', name, run);
42
+ el.className = 'icon';
43
+ el.setAttribute('aria-label', name);
44
+ 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>`;
45
+ return el;
46
+ }
47
+
48
+ /** Text's width as the marks draw it, by a canvas's measure, which the guess stands in for without one. */
49
+ export function measurer(): Measure {
50
+ const ctx = document.createElement('canvas').getContext('2d');
51
+ if (ctx === null) return guess;
52
+ ctx.font = `${FONT}px system-ui, sans-serif`;
53
+ return (text) => ctx.measureText(text).width;
54
+ }
55
+
56
+ export function button(label: string, title: string, run: () => void): HTMLButtonElement {
57
+ const el = Object.assign(document.createElement('button'), { type: 'button', textContent: label, title });
58
+ el.addEventListener('click', run);
59
+ return el;
60
+ }
61
+
62
+ export interface DrawingOptions {
63
+ /** What takes the pointer to draw on. */
64
+ surface: HTMLElement;
65
+ /** Where an event is, in the coordinates the marks are kept in. */
66
+ at: (e: PointerEvent) => Point;
67
+ /** Shows the shape being drawn, or nothing once it's done. */
68
+ preview: (shape: Shape | undefined) => void;
69
+ add: (shape: Shape) => void;
70
+ removeLast: () => void;
71
+ clear: () => void;
72
+ /**
73
+ * Where the page stays usable between marks (a recording's toolbar): a pointer tool comes first and is
74
+ * picked to start with, and this hears whether a drawing tool is picked, to let the surface take the
75
+ * pointer only then.
76
+ */
77
+ drawing?: (on: boolean) => void;
78
+ }
79
+
80
+ /**
81
+ * Draws on `surface` with the tool picked: answers the toolbar (the tools, then removing the last mark or all), for the
82
+ * caller to add its own buttons to, and a hint for the tool.
83
+ */
84
+ export function drawing({ surface, at, preview, add, removeLast, clear, drawing: on }: DrawingOptions) {
85
+ const tools = document.createElement('div');
86
+ tools.className = 'annotations-tools';
87
+ const hint = document.createElement('p');
88
+ hint.className = 'annotations-hint';
89
+
90
+ let tool: Tool = on === undefined ? 'box' : 'pointer';
91
+ const picks = (on === undefined ? TOOLS : (['pointer', ...TOOLS] as Tool[])).map((t) => ({
92
+ t,
93
+ el: iconButton(t, NAMES[t], () => pick(t)),
94
+ }));
95
+ const pick = (t: Tool) => {
96
+ tool = t;
97
+ for (const p of picks) p.el.setAttribute('aria-pressed', String(p.t === t));
98
+ on?.(t !== 'pointer');
99
+ hint.textContent =
100
+ t === 'pointer'
101
+ ? 'Use the page; pick a tool to draw.'
102
+ : t === 'note'
103
+ ? 'Click where the note goes, type it, Enter to place it.'
104
+ : 'Drag to draw.';
105
+ };
106
+ const gap = document.createElement('span');
107
+ gap.className = 'gap';
108
+ tools.append(
109
+ ...picks.map((p) => p.el),
110
+ gap,
111
+ iconButton('undo', 'Remove the last mark', removeLast),
112
+ iconButton('clear', 'Remove every mark', clear),
113
+ );
114
+ pick(tool);
115
+
116
+ surface.style.cursor = 'crosshair';
117
+ const shapeFrom = (from: Point, to: Point, points: Point[]): Shape | undefined => {
118
+ if (tool === 'pen') return points.length > 1 ? { kind: 'pen', points } : undefined;
119
+ if (Math.hypot(to.x - from.x, to.y - from.y) < 4) return undefined;
120
+ if (tool === 'arrow') return { kind: 'arrow', from, to };
121
+ return {
122
+ kind: 'box',
123
+ x: Math.min(from.x, to.x),
124
+ y: Math.min(from.y, to.y),
125
+ width: Math.abs(to.x - from.x),
126
+ height: Math.abs(to.y - from.y),
127
+ };
128
+ };
129
+
130
+ surface.addEventListener('pointerdown', (e) => {
131
+ if (e.button !== 0 || e.target instanceof HTMLInputElement) return;
132
+ e.preventDefault();
133
+ if (tool === 'note') return note(at(e), e);
134
+ const from = at(e);
135
+ const points = [from];
136
+ surface.setPointerCapture(e.pointerId);
137
+ const move = (m: PointerEvent) => {
138
+ points.push(at(m));
139
+ preview(shapeFrom(from, at(m), points));
140
+ };
141
+ const up = (u: PointerEvent) => {
142
+ surface.removeEventListener('pointermove', move);
143
+ preview(undefined);
144
+ const shape = shapeFrom(from, at(u), points);
145
+ if (shape !== undefined) add(shape);
146
+ };
147
+ surface.addEventListener('pointermove', move);
148
+ surface.addEventListener('pointerup', up, { once: true });
149
+ });
150
+
151
+ /** A field where the note goes: Enter places what's typed, leaving it empty or blurring it places nothing. */
152
+ const note = (point: Point, e: PointerEvent) => {
153
+ const field = Object.assign(document.createElement('input'), { placeholder: 'Note, then Enter' });
154
+ // Its own colours: on the page, outside the popup, it would take the page's text colour on deictic's dark field.
155
+ 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)`;
156
+ surface.append(field);
157
+ const place = () => {
158
+ const text = field.value.trim();
159
+ field.remove();
160
+ if (text !== '') add({ kind: 'note', at: point, text });
161
+ };
162
+ field.addEventListener('keydown', (k) => k.key === 'Enter' && place());
163
+ field.addEventListener('blur', () => field.isConnected && place());
164
+ // Now, not a frame later: the pointerdown's default is prevented, so no mousedown takes focus back.
165
+ field.focus();
166
+ };
167
+
168
+ return { tools, hint };
169
+ }
@@ -0,0 +1,105 @@
1
+ /**
2
+ * `deictic/annotations`: mark up the page itself. Its tab is annotate
3
+ * mode over the page: boxes, arrows, a pen and notes, drawn where the page is, so they stay with
4
+ * it as it scrolls. They're kept for the tab's session, per page, until cleared.
5
+ *
6
+ * They're data other plugins build on, through `use('annotations')` in the page: screenshots draw them
7
+ * over the page (`svg(area)`), tasks repeat their notes, and a recording can keep each change as it
8
+ * happens (`subscribe`). Nothing of it runs on the server.
9
+ */
10
+ import { definePlugin, type Shortcut } from '#src/index.ts';
11
+
12
+ /** A point in page coordinates: CSS pixels from the document's top left, so it stays put as the page scrolls. */
13
+ export interface Point {
14
+ x: number;
15
+ y: number;
16
+ }
17
+
18
+ /** A rectangle: in page coordinates for a shape, in viewport coordinates for an area to draw. */
19
+ export interface Area {
20
+ x: number;
21
+ y: number;
22
+ width: number;
23
+ height: number;
24
+ }
25
+
26
+ export type Shape =
27
+ | ({ kind: 'box' } & Area)
28
+ | { kind: 'arrow'; from: Point; to: Point }
29
+ | { kind: 'pen'; points: Point[] }
30
+ | { kind: 'note'; at: Point; text: string };
31
+
32
+ export interface Annotation {
33
+ id: string;
34
+ color: string;
35
+ shape: Shape;
36
+ /** When it was made, as `Date.now()`. */
37
+ at: number;
38
+ }
39
+
40
+ /** A change, as `subscribe` hears it: what a recording keeps, to show the marks as they were made. */
41
+ export type AnnotationEvent = { type: 'add'; annotation: Annotation } | { type: 'remove'; id: string } | { type: 'clear' };
42
+
43
+ /** The page's half, for other plugins' page halves. */
44
+ export interface Annotations {
45
+ /** Every annotation on this page, oldest first. */
46
+ list(): Annotation[];
47
+ add(shape: Shape, color?: string): Annotation;
48
+ remove(id: string): void;
49
+ clear(): void;
50
+ /** Hears every change from now on; answers what stops it. */
51
+ subscribe(listener: (event: AnnotationEvent) => void): () => void;
52
+ /**
53
+ * The annotations over a part of the viewport, in viewport CSS pixels, as an SVG that size: what a
54
+ * screenshot of that area draws over it. Nothing when there are none.
55
+ */
56
+ svg(area: Area): string | undefined;
57
+ /**
58
+ * Opens a picture (a screenshot, most often) over everything, with the toolbar, to draw on: answers the
59
+ * marks, in the picture's CSS pixels, on Save, and nothing on Cancel, Esc or `signal`. It starts from
60
+ * `marks`, to edit them again. They're the picture's alone, not the page's.
61
+ */
62
+ edit(picture: Picture, marks?: readonly Annotation[], options?: { signal?: AbortSignal }): Promise<Annotation[] | undefined>;
63
+ /** The picture with the marks drawn over it, as a PNG at the picture's own resolution. */
64
+ flatten(picture: Picture, marks: readonly Annotation[]): Promise<Blob>;
65
+ /**
66
+ * Annotate mode's tools, for another plugin's UI to show (a recording's toolbar): a pointer tool, picked
67
+ * to start with, leaves the page usable; while a drawing tool is picked, the page is drawn on. What's
68
+ * drawn is its own `marks`, not the page's: shown while it's open, kept by nothing, gone on `close`.
69
+ */
70
+ tools(): { element: HTMLElement; marks: MarkSet; close: () => void };
71
+ }
72
+
73
+ /** A set of marks on the page: the page's own, kept for the session, or a toolbar's, kept by nothing. */
74
+ export type MarkSet = Pick<Annotations, 'list' | 'add' | 'remove' | 'clear' | 'subscribe' | 'svg'>;
75
+
76
+ /** An image to draw on: where it loads from (the page's origin, so a canvas may read it), and its size in CSS pixels, which marks on it are in. */
77
+ export interface Picture {
78
+ url: string;
79
+ width: number;
80
+ height: number;
81
+ }
82
+
83
+ declare module 'deictic' {
84
+ interface ClientApis {
85
+ annotations: Annotations;
86
+ }
87
+ }
88
+
89
+ export interface AnnotationsOptions {
90
+ /** What opens its tab directly, beside deictic's own shortcut, which opens any: none by default. */
91
+ shortcut?: Shortcut;
92
+ /** The colour marks are drawn in: a red that deictic's own green isn't. */
93
+ color?: string;
94
+ }
95
+
96
+ export function annotations(options: AnnotationsOptions = {}) {
97
+ return definePlugin({
98
+ name: 'annotations',
99
+ client: 'deictic/annotations/client',
100
+ shortcut: options.shortcut,
101
+ clientOptions: { color: options.color ?? '#ef4444' },
102
+ });
103
+ }
104
+
105
+ export default annotations;
@@ -0,0 +1,61 @@
1
+ /**
2
+ * Annotations as SVG: the layer the page shows them in, and what a screenshot draws over itself. Shapes
3
+ * only, no `foreignObject`, so a canvas that draws the SVG isn't tainted, and a browser rendering it
4
+ * elsewhere needs nothing of the page.
5
+ */
6
+ import type { Annotation, Area, Point } from './index.ts';
7
+
8
+ export const STROKE = 3;
9
+ export const FONT = 14;
10
+ const PAD = 6;
11
+
12
+ /** How wide text is, in CSS pixels: measured where there's a canvas, guessed where there isn't. */
13
+ export type Measure = (text: string) => number;
14
+
15
+ export const guess: Measure = (text) => text.length * FONT * 0.6;
16
+
17
+ const escape = (text: string) => text.replaceAll('&', '&amp;').replaceAll('<', '&lt;').replaceAll('>', '&gt;').replaceAll('"', '&quot;');
18
+ const n = (value: number) => Math.round(value * 10) / 10;
19
+
20
+ /** An arrow's path: its line, then its head, two strokes back from the tip either side of the line. */
21
+ function arrow(from: Point, to: Point): string {
22
+ const angle = Math.atan2(to.y - from.y, to.x - from.x);
23
+ const at = (p: Point) => `${n(p.x)},${n(p.y)}`;
24
+ const side = (turn: number) => at({ x: to.x - 14 * Math.cos(angle + turn), y: to.y - 14 * Math.sin(angle + turn) });
25
+ return `M${at(from)} L${at(to)} M${side(0.45)} L${at(to)} L${side(-0.45)}`;
26
+ }
27
+
28
+ function shapeOf({ shape, color }: Annotation, measure: Measure): string {
29
+ const line = `fill="none" stroke="${escape(color)}" stroke-width="${STROKE}" stroke-linecap="round" stroke-linejoin="round"`;
30
+ switch (shape.kind) {
31
+ case 'box':
32
+ return `<rect x="${n(shape.x)}" y="${n(shape.y)}" width="${n(shape.width)}" height="${n(shape.height)}" rx="4" ${line}/>`;
33
+ case 'arrow':
34
+ return `<path d="${arrow(shape.from, shape.to)}" ${line}/>`;
35
+ case 'pen':
36
+ return `<polyline points="${shape.points.map((p) => `${n(p.x)},${n(p.y)}`).join(' ')}" ${line}/>`;
37
+ case 'note': {
38
+ const lines = shape.text.split('\n');
39
+ const width = Math.max(...lines.map(measure)) + PAD * 2;
40
+ const height = lines.length * FONT * 1.3 + PAD * 2;
41
+ const text = lines
42
+ .map(
43
+ (l, i) => `<tspan x="${n(shape.at.x + PAD)}" y="${n(shape.at.y + PAD + FONT * (i + 1) * 1.3 - FONT * 0.3)}">${escape(l)}</tspan>`,
44
+ )
45
+ .join('');
46
+ return (
47
+ `<rect x="${n(shape.at.x)}" y="${n(shape.at.y)}" width="${n(width)}" height="${n(height)}" rx="4" fill="${escape(color)}"/>` +
48
+ `<text font-family="system-ui, sans-serif" font-size="${FONT}" fill="#fff">${text}</text>`
49
+ );
50
+ }
51
+ }
52
+ }
53
+
54
+ /**
55
+ * The annotations over `area` of the viewport, the page scrolled by `scroll`: an SVG `area`'s size,
56
+ * whose view is that part of the page.
57
+ */
58
+ export function toSvg(annotations: readonly Annotation[], area: Area, scroll: Point, measure: Measure = guess): string {
59
+ const view = `${n(area.x + scroll.x)} ${n(area.y + scroll.y)} ${n(area.width)} ${n(area.height)}`;
60
+ 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>`;
61
+ }
@@ -0,0 +1,35 @@
1
+ /**
2
+ * `deictic/recommended`: the plugins for describing a change rather than making it. Alt-click an element, say what
3
+ * should change, attach a screenshot to draw on, a screen recording or your voice, and it goes to a Vercel Board.
4
+ *
5
+ * Only the server halves, as every factory is: each names its page half, which the integration imports. A plugin
6
+ * outside the preset (Tailwind, your own) goes beside it: `plugins: [...recommended(), tailwind()]`.
7
+ */
8
+ import type { ServerPlugin } from '#src/index.ts';
9
+ import { annotations, type AnnotationsOptions } from '#src/plugins/annotations/index.ts';
10
+ import { recordings, type RecordingsOptions } from '#src/plugins/recordings/index.ts';
11
+ import { screenshots, type ScreenshotsOptions } from '#src/plugins/screenshots/index.ts';
12
+ import { tasks, type TasksOptions } from '#src/plugins/tasks/index.ts';
13
+ import { voice, type VoiceOptions } from '#src/plugins/voice/index.ts';
14
+
15
+ /** Each plugin's options, by its name, or `false` to leave it out. */
16
+ export interface RecommendedOptions {
17
+ tasks?: TasksOptions | false;
18
+ screenshots?: ScreenshotsOptions | false;
19
+ annotations?: AnnotationsOptions | false;
20
+ recordings?: RecordingsOptions | false;
21
+ voice?: VoiceOptions | false;
22
+ }
23
+
24
+ /** Tasks, screenshots, annotations, recordings and voice, in that order: the order of their tabs. */
25
+ export function recommended(options: RecommendedOptions = {}): ServerPlugin[] {
26
+ const plugins: ServerPlugin[] = [];
27
+ if (options.tasks !== false) plugins.push(tasks(options.tasks));
28
+ if (options.screenshots !== false) plugins.push(screenshots(options.screenshots));
29
+ if (options.annotations !== false) plugins.push(annotations(options.annotations));
30
+ if (options.recordings !== false) plugins.push(recordings(options.recordings));
31
+ if (options.voice !== false) plugins.push(voice(options.voice));
32
+ return plugins;
33
+ }
34
+
35
+ export default recommended;