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