@openartifactsai/editor-kit 0.0.0-stage → 0.23.0-rc.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/LICENSE +202 -0
- package/NOTICE +5 -0
- package/README.md +143 -2
- package/package.json +67 -4
- package/src/Announcer.d.ts +2 -0
- package/src/Announcer.js +8 -0
- package/src/FindBar.d.ts +31 -0
- package/src/FindBar.js +37 -0
- package/src/FoldToolbar.d.ts +58 -0
- package/src/FoldToolbar.js +438 -0
- package/src/PanelEdge.d.ts +20 -0
- package/src/PanelEdge.js +74 -0
- package/src/VideoShrinkNotice.d.ts +10 -0
- package/src/VideoShrinkNotice.js +81 -0
- package/src/WheelZoomItem.d.ts +12 -0
- package/src/WheelZoomItem.js +18 -0
- package/src/agent.d.ts +68 -0
- package/src/agent.js +113 -0
- package/src/announce.d.ts +21 -0
- package/src/announce.js +31 -0
- package/src/chrome.d.ts +208 -0
- package/src/chrome.js +148 -0
- package/src/clipboard.d.ts +19 -0
- package/src/clipboard.js +48 -0
- package/src/collab/CommentList.d.ts +32 -0
- package/src/collab/CommentList.js +165 -0
- package/src/collab/CommentViews.d.ts +131 -0
- package/src/collab/CommentViews.js +619 -0
- package/src/collab/HistoryViews.d.ts +38 -0
- package/src/collab/HistoryViews.js +103 -0
- package/src/collab/PresenceViews.d.ts +53 -0
- package/src/collab/PresenceViews.js +48 -0
- package/src/collab/cds/cds.d.ts +151 -0
- package/src/collab/cds/cds.js +231 -0
- package/src/collab/cds/comment-card.d.ts +113 -0
- package/src/collab/cds/comment-card.js +230 -0
- package/src/collab/cds/comment-composer.d.ts +35 -0
- package/src/collab/cds/comment-composer.js +125 -0
- package/src/collab/cds/comments-css.d.ts +2 -0
- package/src/collab/cds/comments-css.js +3 -0
- package/src/collab/cds/comments.css +504 -0
- package/src/collab/cds/edge-piles.d.ts +69 -0
- package/src/collab/cds/edge-piles.js +178 -0
- package/src/collab/cds/layer.d.ts +41 -0
- package/src/collab/cds/layer.js +144 -0
- package/src/collab/cds/markdown.d.ts +20 -0
- package/src/collab/cds/markdown.js +534 -0
- package/src/collab/cds/shield.d.ts +28 -0
- package/src/collab/cds/shield.js +114 -0
- package/src/collab/cds/time.d.ts +2 -0
- package/src/collab/cds/time.js +41 -0
- package/src/collab/collab.d.ts +102 -0
- package/src/collab/collab.js +121 -0
- package/src/collab/comments.d.ts +272 -0
- package/src/collab/comments.js +596 -0
- package/src/collab/history-wiring.d.ts +32 -0
- package/src/collab/history-wiring.js +53 -0
- package/src/collab/history.d.ts +59 -0
- package/src/collab/history.js +96 -0
- package/src/collab/mentions/mention-list.d.ts +63 -0
- package/src/collab/mentions/mention-list.js +257 -0
- package/src/collab/mentions.d.ts +112 -0
- package/src/collab/mentions.js +164 -0
- package/src/collab/peer-colors.d.ts +123 -0
- package/src/collab/peer-colors.js +273 -0
- package/src/collab/pins.d.ts +70 -0
- package/src/collab/pins.js +44 -0
- package/src/collab/presence.d.ts +92 -0
- package/src/collab/presence.js +158 -0
- package/src/design-systems.d.ts +46 -0
- package/src/design-systems.js +61 -0
- package/src/downloads.d.ts +31 -0
- package/src/downloads.js +46 -0
- package/src/file-drop.d.ts +33 -0
- package/src/file-drop.js +36 -0
- package/src/find.d.ts +64 -0
- package/src/find.js +147 -0
- package/src/follow.d.ts +141 -0
- package/src/follow.js +389 -0
- package/src/index.d.ts +32 -0
- package/src/index.js +32 -0
- package/src/keys.d.ts +18 -0
- package/src/keys.js +42 -0
- package/src/menu-fit.d.ts +12 -0
- package/src/menu-fit.js +9 -0
- package/src/merge.d.ts +23 -0
- package/src/merge.js +116 -0
- package/src/page-link.d.ts +33 -0
- package/src/page-link.js +75 -0
- package/src/project-sync.d.ts +161 -0
- package/src/project-sync.js +319 -0
- package/src/react.d.ts +20 -0
- package/src/react.js +20 -0
- package/src/read-retries.d.ts +55 -0
- package/src/read-retries.js +125 -0
- package/src/right-drag-pan.d.ts +128 -0
- package/src/right-drag-pan.js +253 -0
- package/src/roles.d.ts +39 -0
- package/src/roles.js +27 -0
- package/src/session.d.ts +56 -0
- package/src/session.js +57 -0
- package/src/shell.d.ts +60 -0
- package/src/shell.js +111 -0
- package/src/stash.d.ts +26 -0
- package/src/stash.js +69 -0
- package/src/styles.css +17 -0
- package/src/toolbar-fold.d.ts +106 -0
- package/src/toolbar-fold.js +130 -0
- package/src/video-shrink/index.d.ts +8 -0
- package/src/video-shrink/index.js +7 -0
- package/src/video-shrink/plan.d.ts +92 -0
- package/src/video-shrink/plan.js +133 -0
- package/src/video-shrink/protocol.d.ts +29 -0
- package/src/video-shrink/protocol.js +2 -0
- package/src/video-shrink/queue.d.ts +69 -0
- package/src/video-shrink/queue.js +287 -0
- package/src/video-shrink/worker.d.ts +8 -0
- package/src/video-shrink/worker.js +223 -0
- package/src/wheel-zoom.d.ts +14 -0
- package/src/wheel-zoom.js +48 -0
package/src/find.js
ADDED
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Find (⌘F) as every editor has it: a query, the matches in reading order,
|
|
3
|
+
* the one shown, Enter / ⇧Enter to step (wrapping), Escape to close, and
|
|
4
|
+
* the matched words highlighted with the CSS Custom Highlight API where the
|
|
5
|
+
* browser has it. There is no replace.
|
|
6
|
+
*
|
|
7
|
+
* The editor supplies what differs: how it searches its document, where it
|
|
8
|
+
* starts, how it shows a match, and which element owns a run of rendered
|
|
9
|
+
* text. Pure (no DOM until a highlight is drawn).
|
|
10
|
+
*/
|
|
11
|
+
import { create } from 'zustand';
|
|
12
|
+
/* -------------------------------------------------------------------------- */
|
|
13
|
+
/* Matching */
|
|
14
|
+
/* -------------------------------------------------------------------------- */
|
|
15
|
+
/** Start offsets of each case-insensitive occurrence of `needle` in `text` (no overlaps). */
|
|
16
|
+
export function occurrences(text, needle) {
|
|
17
|
+
const hay = text.toLowerCase();
|
|
18
|
+
const n = needle.toLowerCase();
|
|
19
|
+
if (!n)
|
|
20
|
+
return [];
|
|
21
|
+
const out = [];
|
|
22
|
+
for (let at = hay.indexOf(n); at >= 0; at = hay.indexOf(n, at + n.length))
|
|
23
|
+
out.push(at);
|
|
24
|
+
return out;
|
|
25
|
+
}
|
|
26
|
+
/** A short excerpt around an occurrence, for the find bar and screen readers. */
|
|
27
|
+
export function excerpt(text, at, length, radius = 24) {
|
|
28
|
+
const start = Math.max(0, at - radius);
|
|
29
|
+
const end = Math.min(text.length, at + length + radius);
|
|
30
|
+
return `${start > 0 ? '…' : ''}${text.slice(start, end)}${end < text.length ? '…' : ''}`;
|
|
31
|
+
}
|
|
32
|
+
/** Steps through `count` matches from `index`, wrapping around; -1 when there are none. */
|
|
33
|
+
export function stepIndex(index, count, delta) {
|
|
34
|
+
if (count <= 0)
|
|
35
|
+
return -1;
|
|
36
|
+
if (index < 0)
|
|
37
|
+
return delta > 0 ? 0 : count - 1;
|
|
38
|
+
return (((index + delta) % count) + count) % count;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* The find bar's store. Stepping searches again first: the document may
|
|
42
|
+
* have changed since the search.
|
|
43
|
+
*/
|
|
44
|
+
export function createFind(adapter) {
|
|
45
|
+
return create()((set, get) => ({
|
|
46
|
+
query: '',
|
|
47
|
+
matches: [],
|
|
48
|
+
index: -1,
|
|
49
|
+
setQuery(query) {
|
|
50
|
+
const matches = adapter.search(query);
|
|
51
|
+
const index = matches.length ? (adapter.start?.(matches) ?? 0) : -1;
|
|
52
|
+
set({ query, matches, index });
|
|
53
|
+
if (!query.trim())
|
|
54
|
+
return;
|
|
55
|
+
if (matches.length)
|
|
56
|
+
adapter.reveal(matches[index], index, matches.length);
|
|
57
|
+
else
|
|
58
|
+
adapter.noResults?.();
|
|
59
|
+
},
|
|
60
|
+
step(delta) {
|
|
61
|
+
const { index, query } = get();
|
|
62
|
+
const fresh = adapter.search(query);
|
|
63
|
+
const next = stepIndex(Math.min(index, fresh.length - 1), fresh.length, delta);
|
|
64
|
+
set({ matches: fresh, index: next });
|
|
65
|
+
if (next >= 0)
|
|
66
|
+
adapter.reveal(fresh[next], next, fresh.length);
|
|
67
|
+
},
|
|
68
|
+
close() {
|
|
69
|
+
set({ query: '', matches: [], index: -1 });
|
|
70
|
+
adapter.onClose();
|
|
71
|
+
},
|
|
72
|
+
}));
|
|
73
|
+
}
|
|
74
|
+
/* -------------------------------------------------------------------------- */
|
|
75
|
+
/* Highlights */
|
|
76
|
+
/* -------------------------------------------------------------------------- */
|
|
77
|
+
/** Highlight names: every match, and the one shown. */
|
|
78
|
+
export const FIND_HIGHLIGHT = 'open-artifacts-find';
|
|
79
|
+
export const FIND_HIGHLIGHT_CURRENT = 'open-artifacts-find-current';
|
|
80
|
+
/** A window's highlight registry, when the browser has one. */
|
|
81
|
+
function registry(win) {
|
|
82
|
+
const css = win?.CSS;
|
|
83
|
+
const Highlight = win?.Highlight;
|
|
84
|
+
return css?.highlights && Highlight ? { highlights: css.highlights, Highlight } : null;
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* The highlight colors, injected at runtime: `::highlight()` is newer than
|
|
88
|
+
* the build's CSS minifier, which warns about it in a stylesheet.
|
|
89
|
+
*/
|
|
90
|
+
const HIGHLIGHT_CSS = `::highlight(${FIND_HIGHLIGHT}){background-color:rgb(250 204 21 / .45);color:inherit}::highlight(${FIND_HIGHLIGHT_CURRENT}){background-color:rgb(249 115 22 / .75);color:inherit}`;
|
|
91
|
+
function installStyles(doc) {
|
|
92
|
+
if (doc.getElementById('open-artifacts-find-styles'))
|
|
93
|
+
return;
|
|
94
|
+
const style = doc.createElement('style');
|
|
95
|
+
style.id = 'open-artifacts-find-styles';
|
|
96
|
+
style.textContent = HIGHLIGHT_CSS;
|
|
97
|
+
(doc.head ?? doc.documentElement).appendChild(style);
|
|
98
|
+
}
|
|
99
|
+
/** Removes the find highlights of a document. */
|
|
100
|
+
export function clearFindHighlights(doc) {
|
|
101
|
+
const reg = registry(doc.defaultView);
|
|
102
|
+
reg?.highlights.delete(FIND_HIGHLIGHT);
|
|
103
|
+
reg?.highlights.delete(FIND_HIGHLIGHT_CURRENT);
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Highlights `query` in the text under `root` (replacing the document's
|
|
107
|
+
* find highlights). `ownerOf` names the element each text node counts
|
|
108
|
+
* under (`null`: skip the text); the occurrence `currentOccurrence` of the
|
|
109
|
+
* current owner gets the stronger color.
|
|
110
|
+
*/
|
|
111
|
+
export function highlightText(doc, root, query, ownerOf, currentOccurrence) {
|
|
112
|
+
const reg = registry(doc.defaultView);
|
|
113
|
+
if (!reg)
|
|
114
|
+
return;
|
|
115
|
+
reg.highlights.delete(FIND_HIGHLIGHT);
|
|
116
|
+
reg.highlights.delete(FIND_HIGHLIGHT_CURRENT);
|
|
117
|
+
const needle = query.trim().toLowerCase();
|
|
118
|
+
if (!root || !needle)
|
|
119
|
+
return;
|
|
120
|
+
installStyles(doc);
|
|
121
|
+
const all = [];
|
|
122
|
+
const strong = [];
|
|
123
|
+
const counts = new Map();
|
|
124
|
+
const walker = doc.createTreeWalker(root, NodeFilter.SHOW_TEXT);
|
|
125
|
+
for (let node = walker.nextNode(); node; node = walker.nextNode()) {
|
|
126
|
+
const parent = node.parentElement;
|
|
127
|
+
const owner = parent ? ownerOf(parent) : null;
|
|
128
|
+
if (!owner)
|
|
129
|
+
continue;
|
|
130
|
+
const text = node.textContent?.toLowerCase() ?? '';
|
|
131
|
+
for (let at = text.indexOf(needle); at >= 0; at = text.indexOf(needle, at + needle.length)) {
|
|
132
|
+
const range = doc.createRange();
|
|
133
|
+
range.setStart(node, at);
|
|
134
|
+
range.setEnd(node, at + needle.length);
|
|
135
|
+
const n = counts.get(owner.key) ?? 0;
|
|
136
|
+
counts.set(owner.key, n + 1);
|
|
137
|
+
if (owner.current && n === currentOccurrence)
|
|
138
|
+
strong.push(range);
|
|
139
|
+
else
|
|
140
|
+
all.push(range);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
if (all.length)
|
|
144
|
+
reg.highlights.set(FIND_HIGHLIGHT, new reg.Highlight(...all));
|
|
145
|
+
if (strong.length)
|
|
146
|
+
reg.highlights.set(FIND_HIGHLIGHT_CURRENT, new reg.Highlight(...strong));
|
|
147
|
+
}
|
package/src/follow.d.ts
ADDED
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Following the agent's work on a canvas.
|
|
3
|
+
*
|
|
4
|
+
* While the agent works, the items it adds are "fresh". Once its changes
|
|
5
|
+
* have been quiet for a moment, the camera glides to frame them: all of them
|
|
6
|
+
* when that fits at no less than half the zoom the viewer had, otherwise the
|
|
7
|
+
* newest one not yet seen. Following stops as soon as the viewer touches the
|
|
8
|
+
* canvas (pointer, wheel, keys), switches page or presses Stop; nothing moves
|
|
9
|
+
* the camera again until they ask (`show`). Fresh work that stays out of view
|
|
10
|
+
* is counted, with the side it lies on, for the "New while the agent works"
|
|
11
|
+
* chip.
|
|
12
|
+
*
|
|
13
|
+
* Some items are *minor* (notes beside artboards, arrows between shapes):
|
|
14
|
+
* they are framed with the rest but only counted when nothing else is new.
|
|
15
|
+
*
|
|
16
|
+
* The agent can also ask to show a place (`showing`). While the viewer has
|
|
17
|
+
* not taken over, the camera goes there (`goShown`) and the framing of fresh
|
|
18
|
+
* work stands aside; after they have, it stays put until the agent's next run.
|
|
19
|
+
*
|
|
20
|
+
* Pure: time, timers, the camera and the canvas come from a
|
|
21
|
+
* {@link FollowHost}, so it runs under a fake clock in tests.
|
|
22
|
+
*/
|
|
23
|
+
/** How long after the agent stops its work still counts as this ask. */
|
|
24
|
+
export declare const WORK_END_MS = 120000;
|
|
25
|
+
/** The agent's changes must be quiet this long before the camera moves. */
|
|
26
|
+
export declare const QUIET_MS = 1500;
|
|
27
|
+
/** A frame waits at most this long for quiet; then it gives up. */
|
|
28
|
+
export declare const MAX_WAIT_MS = 60000;
|
|
29
|
+
/** Following stays on this long after the agent stops. */
|
|
30
|
+
export declare const LINGER_MS = 30000;
|
|
31
|
+
/** A pause shorter than this is the same piece of work resuming. */
|
|
32
|
+
export declare const RESUME_MS = 3000;
|
|
33
|
+
/** Items the camera is gliding to are not counted as unseen for this long. */
|
|
34
|
+
export declare const GLIDE_HOLD_MS = 2000;
|
|
35
|
+
/** Following zooms out to at most this fraction of the viewer's zoom. */
|
|
36
|
+
export declare const MIN_ZOOM_FACTOR = 0.5;
|
|
37
|
+
/** Screen padding around a framed area, px. */
|
|
38
|
+
export declare const FRAME_PADDING = 56;
|
|
39
|
+
/** A world-space box. */
|
|
40
|
+
export interface FollowRect {
|
|
41
|
+
x: number;
|
|
42
|
+
y: number;
|
|
43
|
+
w: number;
|
|
44
|
+
h: number;
|
|
45
|
+
}
|
|
46
|
+
/** The side of the view something lies on. */
|
|
47
|
+
export type Heading = 'n' | 'e' | 's' | 'w';
|
|
48
|
+
/** A camera: screen = world × zoom + (x, y). */
|
|
49
|
+
export interface FollowCamera {
|
|
50
|
+
x: number;
|
|
51
|
+
y: number;
|
|
52
|
+
zoom: number;
|
|
53
|
+
}
|
|
54
|
+
/** What the follower needs from the editor. */
|
|
55
|
+
export interface FollowHost {
|
|
56
|
+
now(): number;
|
|
57
|
+
/** Runs `run` after `ms`; returns a cancel function. */
|
|
58
|
+
after(ms: number, run: () => void): () => void;
|
|
59
|
+
/** The boxes that can be followed, keyed by item, in creation order (newest last). */
|
|
60
|
+
boxes(): Map<string, FollowRect>;
|
|
61
|
+
/** True for a minor item's key. */
|
|
62
|
+
minor?(key: string): boolean;
|
|
63
|
+
/** Something else drives the camera (the viewer follows a person): nothing is framed or counted meanwhile. */
|
|
64
|
+
held?(): boolean;
|
|
65
|
+
/** The viewport size in screen px. */
|
|
66
|
+
screen(): {
|
|
67
|
+
w: number;
|
|
68
|
+
h: number;
|
|
69
|
+
};
|
|
70
|
+
/** The current camera. */
|
|
71
|
+
camera(): FollowCamera;
|
|
72
|
+
/** Moves the camera (the editor glides there). */
|
|
73
|
+
go(camera: FollowCamera): void;
|
|
74
|
+
/** Stops a running glide. */
|
|
75
|
+
halt(): void;
|
|
76
|
+
/** Smallest zoom the editor allows. */
|
|
77
|
+
minZoom: number;
|
|
78
|
+
}
|
|
79
|
+
/** What the chip shows. */
|
|
80
|
+
export interface FollowState {
|
|
81
|
+
/** The camera follows the agent. */
|
|
82
|
+
following: boolean;
|
|
83
|
+
/** Fresh items out of view (the main ones when there are any, else the minor ones). */
|
|
84
|
+
unseen: number;
|
|
85
|
+
of: 'main' | 'minor';
|
|
86
|
+
/** The side the unseen items lie on. */
|
|
87
|
+
heading: Heading;
|
|
88
|
+
/** The side the chip sits on: it stays put while the count lasts. */
|
|
89
|
+
edge: Heading;
|
|
90
|
+
}
|
|
91
|
+
export declare const IDLE_FOLLOW: FollowState;
|
|
92
|
+
/** The visible world rect of a camera. */
|
|
93
|
+
export declare function viewOf(camera: FollowCamera, screen: {
|
|
94
|
+
w: number;
|
|
95
|
+
h: number;
|
|
96
|
+
}): FollowRect;
|
|
97
|
+
/** A box counts as shown when it fits and is fully inside, or is bigger and has its centre inside. */
|
|
98
|
+
export declare function shownIn(box: FollowRect, view: FollowRect): boolean;
|
|
99
|
+
/** Which side of the view a box lies on, judged against the view's own shape. */
|
|
100
|
+
export declare function directionOf(box: FollowRect, view: FollowRect): Heading;
|
|
101
|
+
/** The camera that centres `area` at the largest zoom in range that shows it whole. */
|
|
102
|
+
export declare function cameraFor(area: FollowRect, zoom: {
|
|
103
|
+
min: number;
|
|
104
|
+
max: number;
|
|
105
|
+
}, screen: {
|
|
106
|
+
w: number;
|
|
107
|
+
h: number;
|
|
108
|
+
}): FollowCamera;
|
|
109
|
+
/** The follower's controls. */
|
|
110
|
+
export interface Follower {
|
|
111
|
+
/** The agent started or stopped working. */
|
|
112
|
+
working(busy: boolean): void;
|
|
113
|
+
/** The canvas changed from elsewhere (the agent). */
|
|
114
|
+
remoteChanged(): void;
|
|
115
|
+
/** The viewer touched the canvas: stop following. */
|
|
116
|
+
touched(): void;
|
|
117
|
+
/** The camera moved (by anyone): recount what is in view. */
|
|
118
|
+
viewChanged(): void;
|
|
119
|
+
/** The viewer switched page: start over. */
|
|
120
|
+
paged(): void;
|
|
121
|
+
/** The page changed to reach a place the agent shows: start over on it, still following. */
|
|
122
|
+
turned(): void;
|
|
123
|
+
/** The agent asks to show a place (`true`), or no longer does. */
|
|
124
|
+
showing(on: boolean): void;
|
|
125
|
+
/** Moves the camera to the shown place; false once the viewer took over (nothing moves then). */
|
|
126
|
+
goShown(camera: FollowCamera): boolean;
|
|
127
|
+
/** A shown place may still change the view: it is asked for and the viewer has not taken over. */
|
|
128
|
+
mayShow(): boolean;
|
|
129
|
+
/** Go to the new work and follow again (the chip's button). */
|
|
130
|
+
show(): void;
|
|
131
|
+
/** Stop following; returns whether it was following. */
|
|
132
|
+
stop(): boolean;
|
|
133
|
+
/** Forget the unseen count (the chip's Hide). */
|
|
134
|
+
dismiss(): void;
|
|
135
|
+
/** The host's `held` changed: recount. */
|
|
136
|
+
heldChanged(): void;
|
|
137
|
+
state(): FollowState;
|
|
138
|
+
subscribe(listener: () => void): () => void;
|
|
139
|
+
dispose(): void;
|
|
140
|
+
}
|
|
141
|
+
export declare function createFollower(host: FollowHost): Follower;
|
package/src/follow.js
ADDED
|
@@ -0,0 +1,389 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Following the agent's work on a canvas.
|
|
3
|
+
*
|
|
4
|
+
* While the agent works, the items it adds are "fresh". Once its changes
|
|
5
|
+
* have been quiet for a moment, the camera glides to frame them: all of them
|
|
6
|
+
* when that fits at no less than half the zoom the viewer had, otherwise the
|
|
7
|
+
* newest one not yet seen. Following stops as soon as the viewer touches the
|
|
8
|
+
* canvas (pointer, wheel, keys), switches page or presses Stop; nothing moves
|
|
9
|
+
* the camera again until they ask (`show`). Fresh work that stays out of view
|
|
10
|
+
* is counted, with the side it lies on, for the "New while the agent works"
|
|
11
|
+
* chip.
|
|
12
|
+
*
|
|
13
|
+
* Some items are *minor* (notes beside artboards, arrows between shapes):
|
|
14
|
+
* they are framed with the rest but only counted when nothing else is new.
|
|
15
|
+
*
|
|
16
|
+
* The agent can also ask to show a place (`showing`). While the viewer has
|
|
17
|
+
* not taken over, the camera goes there (`goShown`) and the framing of fresh
|
|
18
|
+
* work stands aside; after they have, it stays put until the agent's next run.
|
|
19
|
+
*
|
|
20
|
+
* Pure: time, timers, the camera and the canvas come from a
|
|
21
|
+
* {@link FollowHost}, so it runs under a fake clock in tests.
|
|
22
|
+
*/
|
|
23
|
+
/** How long after the agent stops its work still counts as this ask. */
|
|
24
|
+
export const WORK_END_MS = 120_000;
|
|
25
|
+
/** The agent's changes must be quiet this long before the camera moves. */
|
|
26
|
+
export const QUIET_MS = 1500;
|
|
27
|
+
/** A frame waits at most this long for quiet; then it gives up. */
|
|
28
|
+
export const MAX_WAIT_MS = 60_000;
|
|
29
|
+
/** Following stays on this long after the agent stops. */
|
|
30
|
+
export const LINGER_MS = 30_000;
|
|
31
|
+
/** A pause shorter than this is the same piece of work resuming. */
|
|
32
|
+
export const RESUME_MS = 3000;
|
|
33
|
+
/** Items the camera is gliding to are not counted as unseen for this long. */
|
|
34
|
+
export const GLIDE_HOLD_MS = 2000;
|
|
35
|
+
/** Following zooms out to at most this fraction of the viewer's zoom. */
|
|
36
|
+
export const MIN_ZOOM_FACTOR = 0.5;
|
|
37
|
+
/** Screen padding around a framed area, px. */
|
|
38
|
+
export const FRAME_PADDING = 56;
|
|
39
|
+
/** Retry interval while waiting for quiet. */
|
|
40
|
+
const RETRY_MS = 100;
|
|
41
|
+
export const IDLE_FOLLOW = { following: false, unseen: 0, of: 'main', heading: 's', edge: 's' };
|
|
42
|
+
/** The visible world rect of a camera. */
|
|
43
|
+
export function viewOf(camera, screen) {
|
|
44
|
+
return { x: -camera.x / camera.zoom, y: -camera.y / camera.zoom, w: screen.w / camera.zoom, h: screen.h / camera.zoom };
|
|
45
|
+
}
|
|
46
|
+
/** A box counts as shown when it fits and is fully inside, or is bigger and has its centre inside. */
|
|
47
|
+
export function shownIn(box, view) {
|
|
48
|
+
if (box.w <= view.w && box.h <= view.h)
|
|
49
|
+
return contains(view, box);
|
|
50
|
+
const cx = box.x + box.w / 2;
|
|
51
|
+
const cy = box.y + box.h / 2;
|
|
52
|
+
return cx >= view.x && cx <= view.x + view.w && cy >= view.y && cy <= view.y + view.h;
|
|
53
|
+
}
|
|
54
|
+
/** Which side of the view a box lies on, judged against the view's own shape. */
|
|
55
|
+
export function directionOf(box, view) {
|
|
56
|
+
const dx = box.x + box.w / 2 - (view.x + view.w / 2);
|
|
57
|
+
const dy = box.y + box.h / 2 - (view.y + view.h / 2);
|
|
58
|
+
if (Math.abs(dx) * view.h > Math.abs(dy) * view.w)
|
|
59
|
+
return dx > 0 ? 'e' : 'w';
|
|
60
|
+
return dy > 0 ? 's' : 'n';
|
|
61
|
+
}
|
|
62
|
+
function union(rects) {
|
|
63
|
+
if (!rects.length)
|
|
64
|
+
return undefined;
|
|
65
|
+
const x = Math.min(...rects.map((r) => r.x));
|
|
66
|
+
const y = Math.min(...rects.map((r) => r.y));
|
|
67
|
+
return { x, y, w: Math.max(...rects.map((r) => r.x + r.w)) - x, h: Math.max(...rects.map((r) => r.y + r.h)) - y };
|
|
68
|
+
}
|
|
69
|
+
function contains(area, box) {
|
|
70
|
+
const right = area.x + area.w;
|
|
71
|
+
const bottom = area.y + area.h;
|
|
72
|
+
return area.x <= box.x && area.y <= box.y && right >= box.x + box.w && bottom >= box.y + box.h;
|
|
73
|
+
}
|
|
74
|
+
/** The camera that centres `area` at the largest zoom in range that shows it whole. */
|
|
75
|
+
export function cameraFor(area, zoom, screen) {
|
|
76
|
+
const fits = Math.min(screen.w / Math.max(1, area.w), screen.h / Math.max(1, area.h));
|
|
77
|
+
const scale = fits < zoom.min ? zoom.min : fits > zoom.max ? zoom.max : fits;
|
|
78
|
+
const [midX, midY] = [area.x + area.w / 2, area.y + area.h / 2];
|
|
79
|
+
return { zoom: scale, x: screen.w / 2 - midX * scale, y: screen.h / 2 - midY * scale };
|
|
80
|
+
}
|
|
81
|
+
export function createFollower(host) {
|
|
82
|
+
const isMinor = (key) => host.minor?.(key) ?? false;
|
|
83
|
+
const held = () => host.held?.() ?? false;
|
|
84
|
+
let working = false;
|
|
85
|
+
let allowed = true;
|
|
86
|
+
let ended = false;
|
|
87
|
+
let gaveUp = false;
|
|
88
|
+
/** A place the agent asked to show is in view or on its way. */
|
|
89
|
+
let viewing = false;
|
|
90
|
+
let stoppedAt;
|
|
91
|
+
let lastRemoteAt = -Infinity;
|
|
92
|
+
let lingerUntil = -Infinity;
|
|
93
|
+
let boxes = host.boxes();
|
|
94
|
+
let fresh = [];
|
|
95
|
+
const seen = new Set();
|
|
96
|
+
const considered = new Set();
|
|
97
|
+
const holding = new Set();
|
|
98
|
+
let pending;
|
|
99
|
+
let baseZoom;
|
|
100
|
+
let cancelRetry;
|
|
101
|
+
let cancelHold;
|
|
102
|
+
let cancelEnd;
|
|
103
|
+
let cancelLinger;
|
|
104
|
+
let state = IDLE_FOLLOW;
|
|
105
|
+
const listeners = new Set();
|
|
106
|
+
const view = () => viewOf(host.camera(), host.screen());
|
|
107
|
+
/** Marks fresh items now in view as seen; returns the others. */
|
|
108
|
+
const unseenItems = () => {
|
|
109
|
+
const rect = view();
|
|
110
|
+
const out = [];
|
|
111
|
+
for (const key of fresh) {
|
|
112
|
+
const box = boxes.get(key);
|
|
113
|
+
if (!box || seen.has(key))
|
|
114
|
+
continue;
|
|
115
|
+
if (shownIn(box, rect)) {
|
|
116
|
+
seen.add(key);
|
|
117
|
+
holding.delete(key);
|
|
118
|
+
}
|
|
119
|
+
else
|
|
120
|
+
out.push(key);
|
|
121
|
+
}
|
|
122
|
+
return out;
|
|
123
|
+
};
|
|
124
|
+
const refresh = () => {
|
|
125
|
+
const away = held();
|
|
126
|
+
const counted = away ? [] : unseenItems().filter((key) => !holding.has(key) && pending === undefined);
|
|
127
|
+
const main = counted.filter((key) => !isMinor(key));
|
|
128
|
+
const area = union(counted.map((key) => boxes.get(key)));
|
|
129
|
+
const heading = area ? directionOf(area, view()) : state.heading;
|
|
130
|
+
const unseen = main.length || counted.length;
|
|
131
|
+
const next = {
|
|
132
|
+
following: !away && allowed && ((viewing && !ended) || (!ended && !gaveUp && fresh.length > 0 && (working || pending !== undefined || holding.size > 0 || host.now() < lingerUntil))),
|
|
133
|
+
unseen,
|
|
134
|
+
of: main.length > 0 || counted.length === 0 ? 'main' : 'minor',
|
|
135
|
+
heading,
|
|
136
|
+
edge: state.unseen > 0 && unseen > 0 ? state.edge : heading,
|
|
137
|
+
};
|
|
138
|
+
const settled = next.following || next.unseen > 0 ? next : IDLE_FOLLOW;
|
|
139
|
+
if (settled.following === state.following && settled.unseen === state.unseen && settled.of === state.of && settled.heading === state.heading && settled.edge === state.edge)
|
|
140
|
+
return;
|
|
141
|
+
state = settled;
|
|
142
|
+
for (const listener of listeners)
|
|
143
|
+
listener();
|
|
144
|
+
};
|
|
145
|
+
const cancelPending = () => {
|
|
146
|
+
cancelRetry?.();
|
|
147
|
+
cancelRetry = undefined;
|
|
148
|
+
pending = undefined;
|
|
149
|
+
};
|
|
150
|
+
const releaseHolding = () => {
|
|
151
|
+
cancelHold?.();
|
|
152
|
+
cancelHold = undefined;
|
|
153
|
+
holding.clear();
|
|
154
|
+
};
|
|
155
|
+
const reset = () => {
|
|
156
|
+
cancelPending();
|
|
157
|
+
releaseHolding();
|
|
158
|
+
fresh = [];
|
|
159
|
+
seen.clear();
|
|
160
|
+
considered.clear();
|
|
161
|
+
gaveUp = false;
|
|
162
|
+
baseZoom = undefined;
|
|
163
|
+
};
|
|
164
|
+
/** All fresh items when they fit at the lowest zoom, else the newest unseen main item. */
|
|
165
|
+
const frameTarget = () => {
|
|
166
|
+
const rect = view();
|
|
167
|
+
const candidates = fresh.filter((key) => boxes.has(key) && (!seen.has(key) || shownIn(boxes.get(key), rect)));
|
|
168
|
+
const area = union(candidates.map((key) => boxes.get(key)));
|
|
169
|
+
if (!area)
|
|
170
|
+
return undefined;
|
|
171
|
+
baseZoom ??= host.camera().zoom;
|
|
172
|
+
const zoom = { min: Math.max(host.minZoom, baseZoom * MIN_ZOOM_FACTOR), max: baseZoom };
|
|
173
|
+
const screen = host.screen();
|
|
174
|
+
const padded = (r, scale) => ({ x: r.x - FRAME_PADDING / scale, y: r.y - FRAME_PADDING / scale, w: r.w + (2 * FRAME_PADDING) / scale, h: r.h + (2 * FRAME_PADDING) / scale });
|
|
175
|
+
const fits = (r) => Math.min(screen.w / padded(r, zoom.min).w, screen.h / padded(r, zoom.min).h) >= zoom.min;
|
|
176
|
+
const newest = (keys) => [...keys].reverse().find((key) => !isMinor(key)) ?? keys[keys.length - 1];
|
|
177
|
+
const focus = newest(candidates.filter((key) => !seen.has(key))) ?? newest(candidates);
|
|
178
|
+
const target = padded(fits(area) ? area : boxes.get(focus), zoom.max);
|
|
179
|
+
for (const key of fresh) {
|
|
180
|
+
considered.add(key);
|
|
181
|
+
const box = boxes.get(key);
|
|
182
|
+
if (box && !seen.has(key) && contains(target, box))
|
|
183
|
+
holding.add(key);
|
|
184
|
+
}
|
|
185
|
+
return cameraFor(target, zoom, screen);
|
|
186
|
+
};
|
|
187
|
+
const tryFrame = () => {
|
|
188
|
+
cancelRetry = undefined;
|
|
189
|
+
if (!pending)
|
|
190
|
+
return;
|
|
191
|
+
if (!allowed || (ended && !pending.atOnce)) {
|
|
192
|
+
cancelPending();
|
|
193
|
+
refresh();
|
|
194
|
+
return;
|
|
195
|
+
}
|
|
196
|
+
const now = host.now();
|
|
197
|
+
if (now - pending.since > MAX_WAIT_MS) {
|
|
198
|
+
for (const key of fresh)
|
|
199
|
+
considered.add(key);
|
|
200
|
+
gaveUp = true;
|
|
201
|
+
cancelPending();
|
|
202
|
+
refresh();
|
|
203
|
+
return;
|
|
204
|
+
}
|
|
205
|
+
const quietFor = now - lastRemoteAt;
|
|
206
|
+
if (held() || (!pending.atOnce && quietFor < QUIET_MS)) {
|
|
207
|
+
cancelRetry = host.after(Math.max(RETRY_MS, QUIET_MS - quietFor), tryFrame);
|
|
208
|
+
refresh();
|
|
209
|
+
return;
|
|
210
|
+
}
|
|
211
|
+
cancelPending();
|
|
212
|
+
releaseHolding();
|
|
213
|
+
gaveUp = false;
|
|
214
|
+
const camera = frameTarget();
|
|
215
|
+
if (camera) {
|
|
216
|
+
cancelHold = host.after(GLIDE_HOLD_MS, () => {
|
|
217
|
+
cancelHold = undefined;
|
|
218
|
+
holding.clear();
|
|
219
|
+
refresh();
|
|
220
|
+
});
|
|
221
|
+
host.go(camera);
|
|
222
|
+
}
|
|
223
|
+
refresh();
|
|
224
|
+
};
|
|
225
|
+
const request = (atOnce = false) => {
|
|
226
|
+
const hasNew = () => unseenItems().some((key) => !considered.has(key));
|
|
227
|
+
if (allowed && (atOnce || (!viewing && working && !ended && hasNew()))) {
|
|
228
|
+
pending ??= { since: host.now(), atOnce };
|
|
229
|
+
if (atOnce)
|
|
230
|
+
pending.atOnce = true;
|
|
231
|
+
if (!cancelRetry)
|
|
232
|
+
tryFrame();
|
|
233
|
+
}
|
|
234
|
+
refresh();
|
|
235
|
+
};
|
|
236
|
+
const stopFollowing = () => {
|
|
237
|
+
if (!allowed)
|
|
238
|
+
return false;
|
|
239
|
+
const was = state.following;
|
|
240
|
+
allowed = false;
|
|
241
|
+
cancelPending();
|
|
242
|
+
releaseHolding();
|
|
243
|
+
host.halt();
|
|
244
|
+
return was;
|
|
245
|
+
};
|
|
246
|
+
const endLater = () => {
|
|
247
|
+
cancelEnd?.();
|
|
248
|
+
cancelEnd = host.after(WORK_END_MS, () => {
|
|
249
|
+
ended = true;
|
|
250
|
+
cancelPending();
|
|
251
|
+
refresh();
|
|
252
|
+
});
|
|
253
|
+
};
|
|
254
|
+
return {
|
|
255
|
+
working(busy) {
|
|
256
|
+
const now = host.now();
|
|
257
|
+
if (!busy) {
|
|
258
|
+
if (!working)
|
|
259
|
+
return;
|
|
260
|
+
working = false;
|
|
261
|
+
stoppedAt = now;
|
|
262
|
+
lingerUntil = now + LINGER_MS;
|
|
263
|
+
cancelLinger?.();
|
|
264
|
+
cancelLinger = host.after(LINGER_MS, refresh);
|
|
265
|
+
endLater();
|
|
266
|
+
refresh();
|
|
267
|
+
return;
|
|
268
|
+
}
|
|
269
|
+
if (working)
|
|
270
|
+
return;
|
|
271
|
+
const resumed = stoppedAt !== undefined && now - stoppedAt < RESUME_MS;
|
|
272
|
+
working = true;
|
|
273
|
+
ended = false;
|
|
274
|
+
cancelEnd?.();
|
|
275
|
+
cancelEnd = undefined;
|
|
276
|
+
stoppedAt = undefined;
|
|
277
|
+
if (resumed) {
|
|
278
|
+
refresh();
|
|
279
|
+
return;
|
|
280
|
+
}
|
|
281
|
+
reset();
|
|
282
|
+
boxes = host.boxes();
|
|
283
|
+
allowed = true;
|
|
284
|
+
refresh();
|
|
285
|
+
},
|
|
286
|
+
remoteChanged() {
|
|
287
|
+
const previous = boxes;
|
|
288
|
+
boxes = host.boxes();
|
|
289
|
+
if (fresh.some((key) => !boxes.has(key))) {
|
|
290
|
+
fresh = fresh.filter((key) => boxes.has(key));
|
|
291
|
+
for (const set of [seen, considered, holding])
|
|
292
|
+
for (const key of set)
|
|
293
|
+
if (!boxes.has(key))
|
|
294
|
+
set.delete(key);
|
|
295
|
+
}
|
|
296
|
+
if (!working) {
|
|
297
|
+
refresh();
|
|
298
|
+
return;
|
|
299
|
+
}
|
|
300
|
+
lastRemoteAt = host.now();
|
|
301
|
+
for (const key of boxes.keys())
|
|
302
|
+
if (!previous.has(key) && !fresh.includes(key))
|
|
303
|
+
fresh.push(key);
|
|
304
|
+
request();
|
|
305
|
+
},
|
|
306
|
+
touched() {
|
|
307
|
+
// Touching while the agent works (or while following) declines this
|
|
308
|
+
// run; touching an idle canvas changes nothing.
|
|
309
|
+
if (working || state.following || holding.size > 0)
|
|
310
|
+
stopFollowing();
|
|
311
|
+
refresh();
|
|
312
|
+
},
|
|
313
|
+
viewChanged() {
|
|
314
|
+
if (fresh.length)
|
|
315
|
+
refresh();
|
|
316
|
+
},
|
|
317
|
+
paged() {
|
|
318
|
+
reset();
|
|
319
|
+
host.halt();
|
|
320
|
+
boxes = host.boxes();
|
|
321
|
+
viewing = false;
|
|
322
|
+
stopFollowing();
|
|
323
|
+
refresh();
|
|
324
|
+
},
|
|
325
|
+
turned() {
|
|
326
|
+
reset();
|
|
327
|
+
boxes = host.boxes();
|
|
328
|
+
refresh();
|
|
329
|
+
},
|
|
330
|
+
showing(on) {
|
|
331
|
+
if (!on) {
|
|
332
|
+
if (!viewing)
|
|
333
|
+
return;
|
|
334
|
+
viewing = false;
|
|
335
|
+
request();
|
|
336
|
+
return;
|
|
337
|
+
}
|
|
338
|
+
viewing = true;
|
|
339
|
+
ended = false;
|
|
340
|
+
gaveUp = false;
|
|
341
|
+
cancelPending();
|
|
342
|
+
releaseHolding();
|
|
343
|
+
refresh();
|
|
344
|
+
},
|
|
345
|
+
goShown(camera) {
|
|
346
|
+
if (!allowed || !viewing)
|
|
347
|
+
return false;
|
|
348
|
+
host.go(camera);
|
|
349
|
+
return true;
|
|
350
|
+
},
|
|
351
|
+
mayShow: () => allowed && viewing,
|
|
352
|
+
show() {
|
|
353
|
+
viewing = false;
|
|
354
|
+
allowed = true;
|
|
355
|
+
ended = false;
|
|
356
|
+
gaveUp = false;
|
|
357
|
+
baseZoom = undefined;
|
|
358
|
+
if (!working && stoppedAt !== undefined) {
|
|
359
|
+
endLater();
|
|
360
|
+
lingerUntil = host.now() + LINGER_MS;
|
|
361
|
+
}
|
|
362
|
+
request(true);
|
|
363
|
+
},
|
|
364
|
+
stop() {
|
|
365
|
+
const was = stopFollowing();
|
|
366
|
+
refresh();
|
|
367
|
+
return was;
|
|
368
|
+
},
|
|
369
|
+
dismiss() {
|
|
370
|
+
for (const key of fresh)
|
|
371
|
+
seen.add(key);
|
|
372
|
+
releaseHolding();
|
|
373
|
+
refresh();
|
|
374
|
+
},
|
|
375
|
+
heldChanged: refresh,
|
|
376
|
+
state: () => state,
|
|
377
|
+
subscribe(listener) {
|
|
378
|
+
listeners.add(listener);
|
|
379
|
+
return () => listeners.delete(listener);
|
|
380
|
+
},
|
|
381
|
+
dispose() {
|
|
382
|
+
cancelPending();
|
|
383
|
+
releaseHolding();
|
|
384
|
+
cancelEnd?.();
|
|
385
|
+
cancelLinger?.();
|
|
386
|
+
listeners.clear();
|
|
387
|
+
},
|
|
388
|
+
};
|
|
389
|
+
}
|