deictic 0.6.0 → 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/API.md +136 -30
- 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/index.d.ts +116 -7
- 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 +103 -26
- package/dist/shared/index7.d.ts.map +1 -1
- package/dist/shared/index9.d.ts +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 +44 -29
- package/dist/shared/task.js.map +1 -1
- package/dist/shared/tasks.js +232 -62
- 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/tasks/client.d.ts +2 -2
- package/dist/tasks/client.d.ts.map +1 -1
- package/dist/tasks/client.js +1829 -741
- 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/recording.ts +34 -3
- package/src/cli/task.ts +59 -30
- 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 +113 -6
- 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/tasks/client.ts +219 -748
- package/src/plugins/tasks/composer.ts +797 -0
- package/src/plugins/tasks/drawer.ts +516 -0
- package/src/plugins/tasks/index.ts +360 -82
- 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/skills/deictic/references/recordings.md +0 -13
- package/skills/deictic/references/tasks.md +0 -22
- package/skills/deictic/references/watch.md +0 -17
package/src/cli/watch.ts
CHANGED
|
@@ -16,7 +16,7 @@ import { createInterface } from 'node:readline';
|
|
|
16
16
|
|
|
17
17
|
import { watcherAt } from '#src/plugins/tasks/watchers.ts';
|
|
18
18
|
|
|
19
|
-
import { answers, boardIn, boardUrl, deicticDirs, duration, ME,
|
|
19
|
+
import { answers, boardIn, boardUrl, deicticDirs, duration, ME, type Message, vbCommand, vbJson, whoami } from './board.ts';
|
|
20
20
|
|
|
21
21
|
/** What `vb tail --json` prints, one per line. */
|
|
22
22
|
interface Tailed {
|
|
@@ -98,6 +98,14 @@ export function run(channel: string, values: { idle: string; once?: boolean; lab
|
|
|
98
98
|
const idle = duration(values.idle);
|
|
99
99
|
const say = (line: string) => console.log(line.replace(/\s+/g, ' ').trim());
|
|
100
100
|
const first = (text: string) => (text.split('\n---')[0] ?? '').trim().slice(0, 160);
|
|
101
|
+
// A task in a line: its words and where, not its labels, which `task` reads out.
|
|
102
|
+
const about = (text: string, labels: Record<string, string> | undefined) => {
|
|
103
|
+
const label = (key: string) => labels?.[key];
|
|
104
|
+
const at = label('deictic.at');
|
|
105
|
+
const page = label('deictic.page');
|
|
106
|
+
const project = label('deictic.project');
|
|
107
|
+
return `"${first(text)}"${at ? ` at ${at}` : ''}${page ? ` on ${page}` : ''}${project ? ` in ${project}` : ''}`;
|
|
108
|
+
};
|
|
101
109
|
|
|
102
110
|
// A board nothing has posted to yet has no channel, and won't list or follow one it hasn't: made here, as the first
|
|
103
111
|
// task would make it. One that's there stays as it is; where the board won't let this make one, `vb tasks` says so.
|
|
@@ -109,7 +117,7 @@ export function run(channel: string, values: { idle: string; once?: boolean; lab
|
|
|
109
117
|
// Where the channel is now, taken before what's ready is: followed from here, nothing posted between is missed, and
|
|
110
118
|
// a tail started again after the board went away follows on from what this one heard, never from a list taken earlier.
|
|
111
119
|
let cursor = vbJson<{ cursor: string }>(['read', channel, '--limit', '1']).cursor;
|
|
112
|
-
const ready =
|
|
120
|
+
const ready = vbJson<{ tasks: { message: Message }[] }>(['tasks', channel, '--state', 'ready', ...labels]).tasks.map((t) => t.message);
|
|
113
121
|
const me = whoami();
|
|
114
122
|
// In the `.deictic/` whose board this is, or in each one near here where the board is the environment's.
|
|
115
123
|
const url = boardUrl();
|
|
@@ -143,11 +151,10 @@ export function run(channel: string, values: { idle: string; once?: boolean; lab
|
|
|
143
151
|
`you write as ${me.handle ?? me.actor}, the person's own identity, as nothing here says which agent you are: set BOARD_PROFILE=<your name> (claude, codex) on every command, this watch too, and start it again`,
|
|
144
152
|
);
|
|
145
153
|
}
|
|
146
|
-
const waiting = (ready ? ready.split('\n') : []).filter((line) => /^#\d+/.test(line));
|
|
147
154
|
// The tasks said, by ID: one made after the cursor above and listed as ready is said once.
|
|
148
|
-
const told = new Set(
|
|
149
|
-
for (const
|
|
150
|
-
if (values.once &&
|
|
155
|
+
const told = new Set(ready.map((m) => String(m.id)));
|
|
156
|
+
for (const m of ready) say(`ready: #${m.id} ${about(m.text, m.labels)}. Read it: ${ME} task ${m.id}`);
|
|
157
|
+
if (values.once && ready.length > 0) end(0);
|
|
151
158
|
|
|
152
159
|
/** One thing to act on: all `--once` waits for. */
|
|
153
160
|
const event = (line: string) => {
|
|
@@ -181,13 +188,7 @@ export function run(channel: string, values: { idle: string; once?: boolean; lab
|
|
|
181
188
|
if (e.cursor !== undefined) cursor = e.cursor;
|
|
182
189
|
heard();
|
|
183
190
|
const p = e.payload ?? {};
|
|
184
|
-
|
|
185
|
-
if (e.type === 'message.posted' && p.re === null && p.thread !== undefined) {
|
|
186
|
-
const at = label('deictic.at');
|
|
187
|
-
const page = label('deictic.page');
|
|
188
|
-
const project = label('deictic.project');
|
|
189
|
-
roots.set(p.thread, `"${first(p.text ?? '')}"${at ? ` at ${at}` : ''}${page ? ` on ${page}` : ''}${project ? ` in ${project}` : ''}`);
|
|
190
|
-
}
|
|
191
|
+
if (e.type === 'message.posted' && p.re === null && p.thread !== undefined) roots.set(p.thread, about(p.text ?? '', p.labels));
|
|
191
192
|
if (e.type === 'task.created' && !told.has(String(p.id)))
|
|
192
193
|
event(`new task #${p.id}: ${roots.get(p.thread ?? -1) ?? ''}. Read it: ${ME} task ${p.id}`);
|
|
193
194
|
if (e.type === 'message.posted' && p.re !== null && p.re !== undefined) {
|
package/src/client/away.ts
CHANGED
|
@@ -5,12 +5,14 @@
|
|
|
5
5
|
* The state may hold what JSON can't (a drawn screenshot is a `Blob`), so it's in IndexedDB, which is the
|
|
6
6
|
* origin's: the tab's session storage holds the key, so each tab comes back to its own.
|
|
7
7
|
*/
|
|
8
|
-
import type { Target } from '#src/index.ts';
|
|
8
|
+
import type { Point, Target } from '#src/index.ts';
|
|
9
9
|
|
|
10
10
|
/** What the next page needs to come back: which tab, on what, with what. */
|
|
11
11
|
export interface Away {
|
|
12
12
|
tab: string;
|
|
13
13
|
target: Target;
|
|
14
|
+
/** The spot in the element the popup was on, where it was on one. */
|
|
15
|
+
at?: Point;
|
|
14
16
|
state: unknown;
|
|
15
17
|
}
|
|
16
18
|
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The component that wrote an element, where the page can say: with React in development, which keeps who wrote
|
|
3
|
+
* each element on its fiber. Anything else (another framework, a production build) says nothing, and an element is
|
|
4
|
+
* then known by its tag alone.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* What React keeps, in development, of who wrote an element: a fiber (its `type` the component), or for a server
|
|
9
|
+
* component its `name`. Each has the one that wrote it in turn. A fiber has what it rendered under it, too.
|
|
10
|
+
*/
|
|
11
|
+
export interface Owner {
|
|
12
|
+
name?: unknown;
|
|
13
|
+
type?: unknown;
|
|
14
|
+
alternate?: Owner | null;
|
|
15
|
+
owner?: Owner | null;
|
|
16
|
+
_debugOwner?: Owner | null;
|
|
17
|
+
child?: Owner | null;
|
|
18
|
+
sibling?: Owner | null;
|
|
19
|
+
return?: Owner | null;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** React's fiber of an element it rendered, which it keeps on the element under a key of its own. */
|
|
23
|
+
export const fiberOf = (el: Element): Owner | undefined => {
|
|
24
|
+
const key = Object.keys(el).find((k) => k.startsWith('__reactFiber$'));
|
|
25
|
+
return key === undefined ? undefined : (el as unknown as Record<string, Owner>)[key];
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
export const ownerOf = (of: Owner | undefined): Owner | undefined => of?._debugOwner ?? of?.owner ?? undefined;
|
|
29
|
+
|
|
30
|
+
/** A component's name, through `memo` and `forwardRef`, or nothing for one written without. */
|
|
31
|
+
export function componentOf(owner: Owner): string | undefined {
|
|
32
|
+
if (typeof owner.name === 'string') return owner.name === '' ? undefined : owner.name;
|
|
33
|
+
let type = owner.type as { displayName?: unknown; name?: unknown; render?: unknown; type?: unknown } | null | undefined;
|
|
34
|
+
for (let depth = 0; depth < 4 && type !== null && type !== undefined; depth++) {
|
|
35
|
+
// A function's own name, where it has no `displayName`; a `memo` or `forwardRef` object has neither.
|
|
36
|
+
const name = type.displayName ?? type.name;
|
|
37
|
+
if (typeof name === 'string' && name !== '') return name;
|
|
38
|
+
type = (type.render ?? type.type) as typeof type;
|
|
39
|
+
}
|
|
40
|
+
return undefined;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** How an element is told to a person: its tag, and the component that wrote it where the page says. */
|
|
44
|
+
export interface Named {
|
|
45
|
+
tag: string;
|
|
46
|
+
/** The component whose JSX has the element: `PlanCard`. */
|
|
47
|
+
component?: string;
|
|
48
|
+
/** Whether the element is the only one that component wrote: then the component's name says it all. */
|
|
49
|
+
sole: boolean;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** How far under a component its elements are counted: past that it's taken to have several. */
|
|
53
|
+
const LOOKED = 400;
|
|
54
|
+
|
|
55
|
+
/** Whether `owner` wrote no element but the one: read off what it rendered, which holds what it passed down as children too. */
|
|
56
|
+
function wroteOne(owner: Owner): boolean {
|
|
57
|
+
// A component's fiber is one of two, by turns: an element may name either.
|
|
58
|
+
const its = (fiber: Owner | null | undefined) => fiber !== null && fiber !== undefined && (fiber === owner || fiber === owner.alternate);
|
|
59
|
+
let wrote = 0;
|
|
60
|
+
let looked = 0;
|
|
61
|
+
// Down, then across, then back up to the next across: every fiber under it, without a stack.
|
|
62
|
+
for (let at = owner.child; at !== null && at !== undefined;) {
|
|
63
|
+
if (++looked > LOOKED) return false;
|
|
64
|
+
if (typeof at.type === 'string' && its(ownerOf(at)) && ++wrote > 1) return false;
|
|
65
|
+
if (at.child !== null && at.child !== undefined) {
|
|
66
|
+
at = at.child;
|
|
67
|
+
continue;
|
|
68
|
+
}
|
|
69
|
+
while (at.sibling === null || at.sibling === undefined) {
|
|
70
|
+
const up: Owner | null | undefined = at.return;
|
|
71
|
+
// Back at the component: that was all of it.
|
|
72
|
+
if (up === null || up === undefined || its(up)) return wrote === 1;
|
|
73
|
+
at = up;
|
|
74
|
+
}
|
|
75
|
+
at = at.sibling;
|
|
76
|
+
}
|
|
77
|
+
return wrote === 1;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// Asked each frame while it's pointed at: an element's answer is kept while the element is.
|
|
81
|
+
const known = new WeakMap<Element, Named>();
|
|
82
|
+
|
|
83
|
+
export function named(el: Element): Named {
|
|
84
|
+
const kept = known.get(el);
|
|
85
|
+
if (kept !== undefined) return kept;
|
|
86
|
+
const owner = ownerOf(fiberOf(el));
|
|
87
|
+
const component = owner === undefined ? undefined : componentOf(owner);
|
|
88
|
+
const answer: Named =
|
|
89
|
+
owner === undefined || component === undefined
|
|
90
|
+
? { tag: el.localName, sole: false }
|
|
91
|
+
: { tag: el.localName, component, sole: wroteOne(owner) };
|
|
92
|
+
known.set(el, answer);
|
|
93
|
+
return answer;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** `<PlanCard>` for the only element of one, `<PlanCard> <li>` for one of several, `<li>` where no component is known. */
|
|
97
|
+
export const tell = ({ tag, component, sole }: Named) =>
|
|
98
|
+
component === undefined ? `<${tag}>` : sole ? `<${component}>` : `<${component}> <${tag}>`;
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
/** The little the overlay's parts all draw with. */
|
|
2
|
+
import type { Target } from '#src/index.ts';
|
|
3
|
+
|
|
4
|
+
/** The attribute the transform marks an element with: `file:line:column`. */
|
|
5
|
+
export const ATTRIBUTE = 'data-deictic';
|
|
6
|
+
|
|
7
|
+
/** An element's place in the source, as its mark has it, or nothing for one with none. */
|
|
8
|
+
export function targetOf(el: Element): Target | undefined {
|
|
9
|
+
const at = el.getAttribute(ATTRIBUTE);
|
|
10
|
+
const match = at === null ? null : /^(.*):(\d+):(\d+)$/.exec(at);
|
|
11
|
+
if (match === null) return undefined;
|
|
12
|
+
return { loc: { file: match[1]!, line: Number(match[2]), column: Number(match[3]) }, tag: el.localName };
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/** Every element on the page from the same place in the source as `el`. */
|
|
16
|
+
export const sameAs = (el: Element) => [...document.querySelectorAll(`[${ATTRIBUTE}="${CSS.escape(el.getAttribute(ATTRIBUTE) ?? '')}"]`)];
|
|
17
|
+
|
|
18
|
+
export function div(className: string): HTMLDivElement {
|
|
19
|
+
const el = document.createElement('div');
|
|
20
|
+
el.className = className;
|
|
21
|
+
return el;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** Puts a fixed box over `r`. */
|
|
25
|
+
export function place(el: HTMLElement, r: DOMRect): void {
|
|
26
|
+
Object.assign(el.style, { left: `${r.left}px`, top: `${r.top}px`, width: `${r.width}px`, height: `${r.height}px` });
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** `button.tab`: the element as a selector, its own classes only, for a person to tell it by. */
|
|
30
|
+
export const nameOf = (el: Element) => `${el.localName}${[...el.classList].map((c) => `.${c}`).join('')}`;
|
|
31
|
+
|
|
32
|
+
export const reduced = () => matchMedia('(prefers-reduced-motion: reduce)').matches;
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* A button for the tool panel: an icon (its strokes, on a 24-pixel grid), named by its tooltip and for a screen
|
|
36
|
+
* reader, with the keys that press it (`B`, `Control+Z`), which the tooltip shows and the panel hears.
|
|
37
|
+
*/
|
|
38
|
+
export function toolButton(strokes: string, name: string, keys: string | undefined, run: () => void): HTMLButtonElement {
|
|
39
|
+
const el = Object.assign(document.createElement('button'), { type: 'button', className: 'icon', title: name });
|
|
40
|
+
el.innerHTML = `<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">${strokes}</svg>`;
|
|
41
|
+
el.setAttribute('aria-label', name);
|
|
42
|
+
if (keys !== undefined) el.setAttribute('aria-keyshortcuts', keys);
|
|
43
|
+
el.addEventListener('click', run);
|
|
44
|
+
return el;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* The control in `within` that a key is for, by its `aria-keyshortcuts`: a key alone, or with the keys it names.
|
|
49
|
+
* `loose` takes a key alone whatever is held with it, for while a shortcut's keys are held to point.
|
|
50
|
+
*/
|
|
51
|
+
export function controlFor(within: ParentNode, e: KeyboardEvent, loose = false): HTMLElement | undefined {
|
|
52
|
+
// By the letter typed, and by where the key is where it types no Latin letter (another layout, or Alt held).
|
|
53
|
+
const key = /^[a-z]$/i.test(e.key) ? e.key.toUpperCase() : e.code.startsWith('Key') ? e.code.slice(3) : e.key;
|
|
54
|
+
const held = [e.ctrlKey && 'Control', e.metaKey && 'Meta', e.shiftKey && 'Shift'].filter((m) => m !== false);
|
|
55
|
+
for (const el of within.querySelectorAll<HTMLElement>('[aria-keyshortcuts]')) {
|
|
56
|
+
if (el instanceof HTMLButtonElement && el.disabled) continue;
|
|
57
|
+
// One that isn't shown now: a recording's Stop while none is going.
|
|
58
|
+
if (el.offsetParent === null) continue;
|
|
59
|
+
for (const keys of (el.getAttribute('aria-keyshortcuts') ?? '').split(' ')) {
|
|
60
|
+
const named = keys.split('+');
|
|
61
|
+
if (named.at(-1) !== key) continue;
|
|
62
|
+
const mods = named.slice(0, -1);
|
|
63
|
+
if (mods.length === 0 ? loose || held.length === 0 : mods.length === held.length && mods.every((m) => held.includes(m))) return el;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
return undefined;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** The key held with a letter for a command, as the platform has it, by the name `aria-keyshortcuts` gives it. */
|
|
70
|
+
export const MOD = typeof navigator !== 'undefined' && /Mac|iP(?:hone|ad)/.test(navigator.platform) ? 'Meta' : 'Control';
|
|
@@ -0,0 +1,258 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The drawer: on the window's side, where what the plugins have for the page lands, a section each (the
|
|
3
|
+
* tasks still to do, the shots and recordings no task has taken). It shows while a shortcut's keys are held, as the
|
|
4
|
+
* tool panel does, and only while a section has something in it; it stays while the pointer is on it, while something
|
|
5
|
+
* goes on in it (`keep`), and while one of its items is open.
|
|
6
|
+
*
|
|
7
|
+
* An item opens in the drawer itself (`show`): its view takes the sections' place, with a way back. Esc is the view's
|
|
8
|
+
* while the focus is in the drawer, or no popup is open to have it first.
|
|
9
|
+
*
|
|
10
|
+
* It folds away to a pill, by the button in its corner, and stays so (`localStorage`) until the pill is pressed or an
|
|
11
|
+
* item opens in it. The pill counts what the sections have (`data-count`), or what's unread there (`data-unread`).
|
|
12
|
+
*/
|
|
13
|
+
import type { ClientContext, Drawer, TrayItem } from '#src/index.ts';
|
|
14
|
+
|
|
15
|
+
import { div } from './dom.ts';
|
|
16
|
+
|
|
17
|
+
/** How long the drawer outlasts the keys, in milliseconds: time to get the pointer to it. */
|
|
18
|
+
const LINGER = 600;
|
|
19
|
+
/** Where whether it's folded is kept. */
|
|
20
|
+
const FOLDED = 'deictic:drawer';
|
|
21
|
+
const FOLD =
|
|
22
|
+
'<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M5 12h14"/></svg>';
|
|
23
|
+
const TRAY =
|
|
24
|
+
'<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M4 13l2.5-8h11l2.5 8v6H4zM4 13h5l1 2h4l1-2h5"/></svg>';
|
|
25
|
+
const BACK =
|
|
26
|
+
'<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M15 6l-6 6 6 6"/></svg>';
|
|
27
|
+
|
|
28
|
+
/** A play mark, for a row that plays something: drawn, as a system's own differs from one to the next. */
|
|
29
|
+
export function playMark(): HTMLElement {
|
|
30
|
+
const mark = document.createElement('span');
|
|
31
|
+
mark.innerHTML = '<svg viewBox="0 0 24 24" aria-hidden="true"><path d="M7 4v16l13-8z" fill="currentColor"/></svg>';
|
|
32
|
+
return mark;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export interface OwnDrawer extends Drawer {
|
|
36
|
+
/** Whether a shortcut's keys are held: the drawer shows while they are. */
|
|
37
|
+
point: (on: boolean) => void;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export interface DrawerOptions {
|
|
41
|
+
/** Whether the tool panel is on the window's right: the drawer is then on its left, out from under it. */
|
|
42
|
+
crowded: () => boolean;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export function drawerIn(root: ShadowRoot, { crowded }: DrawerOptions): OwnDrawer {
|
|
46
|
+
const drawer = div('drawer');
|
|
47
|
+
drawer.setAttribute('role', 'complementary');
|
|
48
|
+
drawer.setAttribute('aria-label', 'deictic drawer');
|
|
49
|
+
const sections = div('drawer-sections');
|
|
50
|
+
const viewing = div('drawer-view');
|
|
51
|
+
viewing.hidden = true;
|
|
52
|
+
const fold = Object.assign(document.createElement('button'), {
|
|
53
|
+
type: 'button',
|
|
54
|
+
className: 'icon drawer-fold',
|
|
55
|
+
title: 'Fold the drawer away',
|
|
56
|
+
innerHTML: FOLD,
|
|
57
|
+
});
|
|
58
|
+
const pill = Object.assign(document.createElement('button'), {
|
|
59
|
+
type: 'button',
|
|
60
|
+
className: 'drawer-pill',
|
|
61
|
+
title: 'Open the drawer',
|
|
62
|
+
innerHTML: TRAY,
|
|
63
|
+
});
|
|
64
|
+
const count = document.createElement('span');
|
|
65
|
+
pill.append(count);
|
|
66
|
+
drawer.append(fold, pill, sections, viewing);
|
|
67
|
+
root.append(drawer);
|
|
68
|
+
|
|
69
|
+
let folded = false;
|
|
70
|
+
try {
|
|
71
|
+
folded = localStorage.getItem(FOLDED) === 'folded';
|
|
72
|
+
} catch {
|
|
73
|
+
// Storage can be off: open, then.
|
|
74
|
+
}
|
|
75
|
+
const foldTo = (on: boolean) => {
|
|
76
|
+
folded = on;
|
|
77
|
+
drawer.classList.toggle('folded', on);
|
|
78
|
+
try {
|
|
79
|
+
localStorage.setItem(FOLDED, on ? 'folded' : 'open');
|
|
80
|
+
} catch {
|
|
81
|
+
// Not kept past the page, then.
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
drawer.classList.toggle('folded', folded);
|
|
85
|
+
fold.addEventListener('click', () => foldTo(true));
|
|
86
|
+
pill.addEventListener('click', () => foldTo(false));
|
|
87
|
+
/** What the pill says: how much the sections shown have, or how much of it is unread, in the overlay's green. */
|
|
88
|
+
const counted = () => {
|
|
89
|
+
const shown = [...sections.children].filter((section) => !section.hasAttribute('hidden')) as HTMLElement[];
|
|
90
|
+
// A section that says nothing, or something that isn't a number, counts none.
|
|
91
|
+
const sum = (key: 'count' | 'unread') =>
|
|
92
|
+
shown.reduce((n, section) => {
|
|
93
|
+
const said = Number(section.dataset[key] ?? 0);
|
|
94
|
+
return n + (Number.isFinite(said) ? said : 0);
|
|
95
|
+
}, 0);
|
|
96
|
+
const unread = sum('unread');
|
|
97
|
+
count.textContent = String(unread > 0 ? unread : sum('count'));
|
|
98
|
+
pill.classList.toggle('news', unread > 0);
|
|
99
|
+
pill.title = unread > 0 ? `Open the drawer: ${unread} unread` : 'Open the drawer';
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
let pointing = false;
|
|
103
|
+
let over = false;
|
|
104
|
+
let keeps = 0;
|
|
105
|
+
// What closes the item that's open, where one is.
|
|
106
|
+
let leave: (() => void) | undefined;
|
|
107
|
+
let leaving: ReturnType<typeof setTimeout> | undefined;
|
|
108
|
+
const shown = () => drawer.classList.contains('shown');
|
|
109
|
+
// A section with nothing to show hides itself: the drawer has nothing then, and stays away.
|
|
110
|
+
const empty = () => leave === undefined && ![...sections.children].some((section) => !section.hasAttribute('hidden'));
|
|
111
|
+
const show = () => {
|
|
112
|
+
clearTimeout(leaving);
|
|
113
|
+
leaving = undefined;
|
|
114
|
+
const on = !empty() && (pointing || over || keeps > 0 || leave !== undefined || drawer.contains(root.activeElement));
|
|
115
|
+
if (on === shown()) return;
|
|
116
|
+
// Emptied, it goes at once, not shrunk to its corner's button for a while: nothing is left to get the pointer to.
|
|
117
|
+
if (!on && empty()) {
|
|
118
|
+
drawer.classList.add('emptied');
|
|
119
|
+
drawer.classList.remove('shown');
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
drawer.classList.remove('emptied');
|
|
123
|
+
if (!on) {
|
|
124
|
+
// Not at once: the keys are let go on the way to it.
|
|
125
|
+
leaving = setTimeout(() => drawer.classList.remove('shown'), LINGER);
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
drawer.classList.toggle('left', crowded());
|
|
129
|
+
drawer.classList.add('shown');
|
|
130
|
+
};
|
|
131
|
+
drawer.addEventListener('pointerenter', () => {
|
|
132
|
+
over = true;
|
|
133
|
+
show();
|
|
134
|
+
});
|
|
135
|
+
drawer.addEventListener('pointerleave', () => {
|
|
136
|
+
over = false;
|
|
137
|
+
show();
|
|
138
|
+
});
|
|
139
|
+
drawer.addEventListener('focusout', () => queueMicrotask(show));
|
|
140
|
+
// A section fills as its plugin reads what it has: the drawer is there once one does.
|
|
141
|
+
new MutationObserver(() => {
|
|
142
|
+
counted();
|
|
143
|
+
show();
|
|
144
|
+
}).observe(sections, { subtree: true, attributes: true, attributeFilter: ['hidden', 'data-count', 'data-unread'] });
|
|
145
|
+
|
|
146
|
+
const close = () => {
|
|
147
|
+
const closed = leave;
|
|
148
|
+
if (closed === undefined) return;
|
|
149
|
+
leave = undefined;
|
|
150
|
+
viewing.replaceChildren();
|
|
151
|
+
viewing.hidden = true;
|
|
152
|
+
sections.hidden = false;
|
|
153
|
+
closed();
|
|
154
|
+
show();
|
|
155
|
+
};
|
|
156
|
+
// Before the popup's own Esc, which is added as a popup opens: the item's while it's typed in, or with no popup.
|
|
157
|
+
window.addEventListener(
|
|
158
|
+
'keydown',
|
|
159
|
+
(e) => {
|
|
160
|
+
if (e.key !== 'Escape' || leave === undefined || root.querySelector('[data-deictic-modal]') !== null) return;
|
|
161
|
+
if (!drawer.contains(root.activeElement) && root.querySelector('.panel') !== null) return;
|
|
162
|
+
e.preventDefault();
|
|
163
|
+
e.stopImmediatePropagation();
|
|
164
|
+
close();
|
|
165
|
+
},
|
|
166
|
+
true,
|
|
167
|
+
);
|
|
168
|
+
|
|
169
|
+
return {
|
|
170
|
+
add: (section, options) => {
|
|
171
|
+
section.classList.add('drawer-section');
|
|
172
|
+
if (options?.first === true) sections.prepend(section);
|
|
173
|
+
else sections.append(section);
|
|
174
|
+
counted();
|
|
175
|
+
show();
|
|
176
|
+
},
|
|
177
|
+
show: (view, { title = '', closed = () => {} } = {}) => {
|
|
178
|
+
close();
|
|
179
|
+
const back = Object.assign(document.createElement('button'), {
|
|
180
|
+
type: 'button',
|
|
181
|
+
className: 'icon',
|
|
182
|
+
title: 'Back to the list',
|
|
183
|
+
innerHTML: BACK,
|
|
184
|
+
});
|
|
185
|
+
back.setAttribute('aria-keyshortcuts', 'Escape');
|
|
186
|
+
back.addEventListener('click', close);
|
|
187
|
+
const head = div('drawer-head');
|
|
188
|
+
head.append(back, Object.assign(document.createElement('span'), { className: 'drawer-title', textContent: title }));
|
|
189
|
+
viewing.replaceChildren(head, view);
|
|
190
|
+
sections.hidden = true;
|
|
191
|
+
viewing.hidden = false;
|
|
192
|
+
leave = closed;
|
|
193
|
+
// Something asked to be seen: folded away, it couldn't be.
|
|
194
|
+
if (folded) foldTo(false);
|
|
195
|
+
show();
|
|
196
|
+
return () => {
|
|
197
|
+
if (leave === closed) close();
|
|
198
|
+
};
|
|
199
|
+
},
|
|
200
|
+
keep: () => {
|
|
201
|
+
keeps++;
|
|
202
|
+
show();
|
|
203
|
+
let kept = true;
|
|
204
|
+
return () => {
|
|
205
|
+
if (!kept) return;
|
|
206
|
+
kept = false;
|
|
207
|
+
keeps--;
|
|
208
|
+
show();
|
|
209
|
+
};
|
|
210
|
+
},
|
|
211
|
+
point: (on) => {
|
|
212
|
+
pointing = on;
|
|
213
|
+
show();
|
|
214
|
+
},
|
|
215
|
+
};
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/**
|
|
219
|
+
* A section of what a plugin's tools made and no task has taken, from the tray: a heading and a row each, which
|
|
220
|
+
* opens it; its × takes it out of the tray. Away while there are none.
|
|
221
|
+
*/
|
|
222
|
+
export function trayed(
|
|
223
|
+
{ tray }: Pick<ClientContext, 'tray'>,
|
|
224
|
+
plugin: string,
|
|
225
|
+
title: string,
|
|
226
|
+
row: (item: TrayItem, index: number, all: TrayItem[]) => { label: (string | Node)[]; open: (from: HTMLElement) => void },
|
|
227
|
+
): HTMLElement {
|
|
228
|
+
const section = document.createElement('section');
|
|
229
|
+
const heading = Object.assign(document.createElement('h3'), { textContent: title });
|
|
230
|
+
const list = div('drawer-rows');
|
|
231
|
+
section.append(heading, list);
|
|
232
|
+
const draw = () => {
|
|
233
|
+
const items = tray.list(plugin);
|
|
234
|
+
section.hidden = items.length === 0;
|
|
235
|
+
section.dataset.count = String(items.length);
|
|
236
|
+
list.replaceChildren(
|
|
237
|
+
...items.map((item, i) => {
|
|
238
|
+
const { label, open } = row(item, i, items);
|
|
239
|
+
const opener = Object.assign(document.createElement('button'), { type: 'button', className: 'drawer-open' });
|
|
240
|
+
opener.append(...label);
|
|
241
|
+
opener.addEventListener('click', () => open(el));
|
|
242
|
+
const remover = Object.assign(document.createElement('button'), {
|
|
243
|
+
type: 'button',
|
|
244
|
+
className: 'icon',
|
|
245
|
+
textContent: '×',
|
|
246
|
+
title: 'Remove',
|
|
247
|
+
});
|
|
248
|
+
remover.addEventListener('click', () => tray.remove(plugin, item.id));
|
|
249
|
+
const el = div('drawer-row');
|
|
250
|
+
el.append(opener, remover);
|
|
251
|
+
return el;
|
|
252
|
+
}),
|
|
253
|
+
);
|
|
254
|
+
};
|
|
255
|
+
tray.subscribe(draw);
|
|
256
|
+
draw();
|
|
257
|
+
return section;
|
|
258
|
+
}
|