@gemboss/ui 0.61.0 → 0.63.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/CHANGELOG.md +103 -0
- package/README.md +107 -2
- package/catalog/editor.html +1 -1
- package/catalog/index.html +21 -24
- package/gemboss-ui.css +3 -3
- package/package.json +9 -1
- package/parts/components.css +3 -3
- package/parts/core.css +1 -1
- package/parts/shell.css +1 -1
- package/src/app-nav.css +1 -0
- package/src/app-nav.tsx +236 -0
- package/src/editor/frame-context.ts +9 -0
- package/src/editor/index.ts +8 -0
- package/src/editor/inspector-frame.tsx +285 -0
- package/src/editor/label-side.ts +79 -0
- package/src/editor/panel.tsx +23 -6
- package/src/editor/repeater.tsx +9 -3
- package/src/editor-inspector-frame.css +1 -0
- package/src/editor.css +1 -1
- package/src/gem-at-work.css +1 -0
- package/src/gem-at-work.ts +1119 -0
- package/src/index.ts +16 -1
- package/src/kit-plain.css +1 -1
- package/src/rail-mini.css +1 -0
- package/src/report-charts.css +1 -0
- package/src/shell.tsx +36 -8
- package/src/skill-feedback.css +1 -0
- package/src/skill-feedback.tsx +63 -0
- package/src/skill-history.css +1 -0
- package/src/skill-history.tsx +115 -0
- package/src/skill-pages.css +1 -0
- package/src/skill-pages.tsx +204 -0
- package/src/skill-tabs-group.tsx +45 -0
- package/src/skill-view.tsx +7 -1
|
@@ -0,0 +1,1119 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* GEM AT WORK (unreleased, 05/11/2026; ADR 0013): what Gem is doing, drawn on the page it is doing it to. Chris, 04/11, after
|
|
3
|
+
* the "many cursors build the page" video: bring it into GemBoss, "một mặt câu giờ, một mặt tạo sự sinh động", without it
|
|
4
|
+
* breaking at the next upgrade.
|
|
5
|
+
*
|
|
6
|
+
* It reads ONE thing, the activity contract v1 (the admin turns the agent's stream into it: apps/admin/src/lib/gem-activity.ts),
|
|
7
|
+
* never the stream itself, so a renamed tool or a reworded `applied` sentence changes a line in the admin's map and nothing here.
|
|
8
|
+
* What each activity draws (the grammar of the boards Chris approved, artifacts 7HCVjjq2… and CYpPYpT57…; the many-cursor
|
|
9
|
+
* crew kept on his word, 05/11: "giữ lại làm cho sinh động và dễ hiểu… chuyên gia, tự động, thông minh"):
|
|
10
|
+
* think / plan Gem's crew arrives where the person is looking: Gem leading, a copy, a layout and an image expert, QA, styling
|
|
11
|
+
* focus the layout expert goes to the block and a ring names it; a new section's place is a slot below the one
|
|
12
|
+
* it follows
|
|
13
|
+
* draft the expert for that kind of work drags the box from its corner at its real size, guides across the page, and
|
|
14
|
+
* the words arrive IN the block itself (a section on its way: in its slot, in the page's own font)
|
|
15
|
+
* apply the ring takes a tick
|
|
16
|
+
* check QA sweeps one scan line down the screen
|
|
17
|
+
* fail Gem and the ring turn red, the words go back, and they go
|
|
18
|
+
* land what changed lights up once; the crew leaves
|
|
19
|
+
* Each of the crew has its trade and moves only for it (Chris 05/11: "các con chuột nó có vai trò của nó thật"): the layout
|
|
20
|
+
* expert places sections, the copy expert writes words, the image expert pictures, the CRO expert buttons, prices and offers, QA
|
|
21
|
+
* checks, smart styling colours; Gem says the step. As people in one file (Chris 05/11: standing in a tidy row "nhìn nó lại bị
|
|
22
|
+
* buồn"; "có mấy con trỏ xuất hiện mà ko rối"): at most four on the page at once, Gem, the one at work, and two trades beside it
|
|
23
|
+
* walking slowly along the block's edges, off its words; the others are gone until their trade comes up. The walking lasts
|
|
24
|
+
* as long as the turn is alive and stops with it (STROLL_MS after Gem last said anything). Gem's tag counts a quiet step's seconds.
|
|
25
|
+
*
|
|
26
|
+
* The CALM CLOCK (Chris 05/11: "các bước cũng ko cần phải quá nhanh… để mắt người nhìn kịp"): what Gem did is drawn in order, each
|
|
27
|
+
* step held long enough to be seen, words written at reading pace, the saved page built block by block. The drawing may trail
|
|
28
|
+
* Gem, never lead it (it only ever draws what has arrived). A real build comes in bursts with long quiet between (Gem thinking,
|
|
29
|
+
* a picture being made, a save): the clock may trail further when Gem's quiet spells are long (up to LAG_MAX_MS), so a burst is
|
|
30
|
+
* spread over the quiet after it, and past that it hurries in proportion. `clock: "live"` draws each activity as it arrives.
|
|
31
|
+
*
|
|
32
|
+
* SKIP (ADR 0013 rule 3): a small button in the corner while anything is drawn; pressed, the page shows as it is and nothing more
|
|
33
|
+
* is drawn until the turn lands.
|
|
34
|
+
*
|
|
35
|
+
* Rules the browser test holds (gem-at-work-browser.test.ts):
|
|
36
|
+
* - one layer, no hooks in the page: words written into a block are its own text node's value only, put back by `skip()`,
|
|
37
|
+
* `destroy()` and a refusal, and left for the saved page to replace when a turn lands a change; the one block put in the
|
|
38
|
+
* page is the empty room a page being built makes for its sections on their way (so its footer moves down), taken out with
|
|
39
|
+
* them; a reveal's opacity and clip-path are put back as they were;
|
|
40
|
+
* - only transform, opacity and clip-path move, and nothing loops forever;
|
|
41
|
+
* - the LADDER: an element it cannot find is drawn on its section, a section it cannot find at page level, and an error
|
|
42
|
+
* anywhere skips to the page as it is; `onFallback` reports each step down, so a release that breaks the anchors shows up
|
|
43
|
+
* in the numbers.
|
|
44
|
+
*
|
|
45
|
+
* Plain DOM, no React: the same file runs in the preview frame (gemsite) and on any surface that holds the page itself.
|
|
46
|
+
*/
|
|
47
|
+
import "./gem-at-work.css";
|
|
48
|
+
|
|
49
|
+
export const GEM_ACTIVITY_VERSION = 1 as const;
|
|
50
|
+
export type GemActivityKind = "think" | "plan" | "focus" | "draft" | "apply" | "check" | "land" | "fail";
|
|
51
|
+
export type GemActivityScope = "page" | "section" | "element";
|
|
52
|
+
export interface GemActivityTarget { sectionId?: string; elId?: string; after?: string; at?: "top" | "bottom" }
|
|
53
|
+
/** One thing Gem did, as the contract says it (ADR 0013). The admin's reader makes these; nothing else should. */
|
|
54
|
+
export interface GemActivity {
|
|
55
|
+
v: typeof GEM_ACTIVITY_VERSION;
|
|
56
|
+
kind: GemActivityKind;
|
|
57
|
+
scope: GemActivityScope;
|
|
58
|
+
label: string;
|
|
59
|
+
target?: GemActivityTarget;
|
|
60
|
+
text?: string;
|
|
61
|
+
partial?: boolean;
|
|
62
|
+
progress?: { done: number; total: number };
|
|
63
|
+
callId?: string;
|
|
64
|
+
tool?: string;
|
|
65
|
+
changed?: boolean;
|
|
66
|
+
generic?: true;
|
|
67
|
+
/** draft: a section this turn is adding (an `add` op), not on the page yet. Optional, so a v1 reader that never says it still
|
|
68
|
+
* reads as v1; a page built from scratch stacks these one below the other. */
|
|
69
|
+
created?: true;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** Where a target was drawn: the block it names, its section, the place after a section (a section on its way), or the page. */
|
|
73
|
+
export type GemAtWorkLevel = "element" | "section" | "after" | "page";
|
|
74
|
+
|
|
75
|
+
export interface GemAtWorkOptions {
|
|
76
|
+
/** Holds the layer; the page's body by default. Nothing in it is restyled. */
|
|
77
|
+
root?: HTMLElement;
|
|
78
|
+
/** How a target is found; by default `[data-gem-el-id]`, then `[data-gem-section-id]`. */
|
|
79
|
+
find?: (target: GemActivityTarget) => { el: Element; level: Exclude<GemAtWorkLevel, "page"> } | null;
|
|
80
|
+
/** full: everything. subtle: rings, words and the light at the end, no cursor. off: nothing is drawn. */
|
|
81
|
+
mode?: "full" | "subtle" | "off";
|
|
82
|
+
/** Defaults to the person's prefers-reduced-motion: then nothing travels and nothing animates. */
|
|
83
|
+
reducedMotion?: boolean;
|
|
84
|
+
/** A step down the ladder, with the activity that took it. */
|
|
85
|
+
onFallback?: (level: GemAtWorkLevel, activity: unknown) => void;
|
|
86
|
+
/** What Gem is doing, in a few words, for the surface's own status line. */
|
|
87
|
+
onStatus?: (text: string) => void;
|
|
88
|
+
/** How many of the crew draw in `full` mode: Gem and up to six experts (default all seven; 1 is Gem alone). */
|
|
89
|
+
crew?: number;
|
|
90
|
+
/** calm (default): the calm clock above. live: each activity drawn the moment it arrives. */
|
|
91
|
+
clock?: "calm" | "live";
|
|
92
|
+
/** The Skip button's words (`full` mode); false draws none. */
|
|
93
|
+
skipLabel?: string | false;
|
|
94
|
+
/** The person pressed Skip. */
|
|
95
|
+
onSkip?: () => void;
|
|
96
|
+
/** Where Gem is working, in the page's own pixels (its top and height), when this document cannot scroll to it itself: a page
|
|
97
|
+
* drawn at its full height on a board that pans (the Studio's Canvas), whose host moves the board there. */
|
|
98
|
+
onLook?: (top: number, height: number) => void;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export interface GemAtWork {
|
|
102
|
+
push(activity: unknown): void;
|
|
103
|
+
/** Straight to the page as it is: every piece goes at once. */
|
|
104
|
+
skip(): void;
|
|
105
|
+
/** skip(), and the layer leaves the page. */
|
|
106
|
+
destroy(): void;
|
|
107
|
+
/** Sections of the saved page have arrived (a build, a clone, a section added): after what is still being drawn, they appear top
|
|
108
|
+
* to bottom, each built block by block, the crew at work on them. All the page's sections, or only the ids given. Each one's
|
|
109
|
+
* own style is put back as it was; `skip()` shows them all at once. */
|
|
110
|
+
reveal(sections?: readonly string[]): Promise<void>;
|
|
111
|
+
/** Resolves once everything that arrived has been drawn (the calm clock has caught up). */
|
|
112
|
+
idle(): Promise<void>;
|
|
113
|
+
/** Skip pressed on the host's own button (the Canvas): the page as it is, and nothing more is drawn until the turn lands. */
|
|
114
|
+
skipTurn(): void;
|
|
115
|
+
/** Joining a turn already under way (the person switched between the page and the canvas, or a frame loaded mid-turn): what
|
|
116
|
+
* Gem did so far, drawn at once with nothing moving; what arrives next is drawn as usual, words carrying on from these. */
|
|
117
|
+
catchUp(activities: readonly unknown[]): void;
|
|
118
|
+
readonly busy: boolean;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
const ARROW = '<svg viewBox="0 0 14 18" aria-hidden="true"><path d="M1 1 L1 14.5 L4.6 11.2 L7.2 17 L9.6 16 L7 10.3 L12.2 10.3 Z"/></svg>';
|
|
122
|
+
const EASE = "cubic-bezier(.3,.75,.2,1)";
|
|
123
|
+
/** A failed ring stays this long, so it is read, then goes. */
|
|
124
|
+
const FAIL_MS = 2400;
|
|
125
|
+
/** Two checks closer than this draw one scan. */
|
|
126
|
+
const SCAN_GAP_MS = 1500;
|
|
127
|
+
/** QA's sweep during a turn: this often while words are being written, this often while Gem is quiet. */
|
|
128
|
+
const SWEEP_MS = 12000;
|
|
129
|
+
const SWEEP_QUIET_MS = 8000;
|
|
130
|
+
/** The calm clock: how long each kind of step stays before the next is drawn (a finished line of words is read for HOLD_MS.draft),
|
|
131
|
+
* the pace words are written at, and how far it may trail Gem before it hurries. */
|
|
132
|
+
const HOLD_MS: Record<GemActivityKind, number> = { think: 1200, plan: 1400, focus: 1100, draft: 700, apply: 900, check: 1800, land: 0, fail: 600 };
|
|
133
|
+
const TYPE_CPS = 28;
|
|
134
|
+
const TICK_MS = 70;
|
|
135
|
+
const LAG_MS = 12000;
|
|
136
|
+
/** How far the clock may trail when Gem's quiet spells are long: a burst spread over the quiet that follows it. */
|
|
137
|
+
const LAG_MAX_MS = 25000;
|
|
138
|
+
/** Who comes along when one of the crew is at work (the trade of the one at work: the two beside it), and what they are doing. */
|
|
139
|
+
const ALONGSIDE: Record<Role, Role[]> = {
|
|
140
|
+
lead: ["copy", "layout"], copy: ["layout", "check"], layout: ["style", "check"], image: ["style", "layout"], cro: ["copy", "check"], check: ["layout", "style"], style: ["layout", "image"],
|
|
141
|
+
};
|
|
142
|
+
const BESIDE: Record<Role, string> = { lead: "working", copy: "reading", layout: "arranging", image: "looking", cro: "optimizing", check: "auto-checking", style: "tuning" };
|
|
143
|
+
/** The crew walks this long after Gem last said anything; past it a turn has gone silent (a tab left open), and it stands. */
|
|
144
|
+
const STROLL_MS = 360_000;
|
|
145
|
+
/** Quiet this long and Gem's tag counts the step's seconds. */
|
|
146
|
+
const QUIET_MS = 3000;
|
|
147
|
+
/** The person moved the page themselves this recently: the work is not followed down the page. */
|
|
148
|
+
const HANDS_MS = 4000;
|
|
149
|
+
|
|
150
|
+
type Box = { x: number; y: number; w: number; h: number };
|
|
151
|
+
type Mark = {
|
|
152
|
+
key: string; el: Element; level: Exclude<GemAtWorkLevel, "page">; target: GemActivityTarget;
|
|
153
|
+
ring?: HTMLElement; slot?: HTMLElement; sel?: HTMLElement; veil?: HTMLElement; caret?: HTMLElement;
|
|
154
|
+
/** Words written into the block: its own text node and what it said before, to put back. */
|
|
155
|
+
text?: { node: Text; was: string };
|
|
156
|
+
/** A section on its way: the lines written into its slot so far. */
|
|
157
|
+
lines?: string[];
|
|
158
|
+
/** A section this turn adds that the page does not have yet: its slot stacks below the last one. */
|
|
159
|
+
stacked?: boolean;
|
|
160
|
+
/** A section on its way below one the page has: the room made for it there, so what follows moves down instead of being drawn
|
|
161
|
+
* over (the stack shares one room, `space`). */
|
|
162
|
+
room?: HTMLElement;
|
|
163
|
+
};
|
|
164
|
+
|
|
165
|
+
type Role = "lead" | "copy" | "layout" | "image" | "cro" | "check" | "style";
|
|
166
|
+
/** The crew, in the order they arrive. Colours from the boards; each expert's name says what kind of work it does. */
|
|
167
|
+
const CREW: ReadonlyArray<{ role: Role; name: string; doing: string; c: string; ink: string }> = [
|
|
168
|
+
{ role: "lead", name: "Gem", doing: "working", c: "#5661f5", ink: "#fff" },
|
|
169
|
+
{ role: "copy", name: "Copy expert", doing: "writing", c: "#FF6FD8", ink: "#16171a" },
|
|
170
|
+
{ role: "layout", name: "Layout expert", doing: "arranging", c: "#FFE45E", ink: "#16171a" },
|
|
171
|
+
{ role: "image", name: "Image expert", doing: "placing", c: "#A78BFA", ink: "#16171a" },
|
|
172
|
+
{ role: "cro", name: "CRO expert", doing: "optimizing", c: "#FB923C", ink: "#16171a" },
|
|
173
|
+
{ role: "check", name: "QA", doing: "auto-checking", c: "#5EEAD4", ink: "#16171a" },
|
|
174
|
+
{ role: "style", name: "Smart styling", doing: "tuning", c: "#A3E635", ink: "#16171a" },
|
|
175
|
+
];
|
|
176
|
+
type Member = { role: Role; name: string; doing: string; el: HTMLElement; tag: HTMLElement; x: number; y: number; anim: Animation | null; fade: Animation | null; resting: boolean };
|
|
177
|
+
/** Buttons, prices and offers are conversion work; pictures the image expert's; colours and fonts smart styling's; words the copy
|
|
178
|
+
* expert's; the rest is layout. */
|
|
179
|
+
const CRO = /^(Button|Cta|Call to action|Price|Offer|Discount|Bundle|Upsell|Countdown)/i;
|
|
180
|
+
/** Colours, fonts and the theme's look are smart styling's. */
|
|
181
|
+
const STYLE = /^(Palette|Colou?rs?|Fonts?|Typography|Theme|Style)/i;
|
|
182
|
+
const roleOf = (a: GemActivity): Role =>
|
|
183
|
+
a.label === "Picture" ? "image" : CRO.test(a.label) ? "cro" : STYLE.test(a.label) ? "style" : a.text !== undefined ? "copy" : "layout";
|
|
184
|
+
|
|
185
|
+
/** THE ONE ORDER every section is built in, overview to detail (Chris 05/11: "trái phải trên dưới, tổng quan đến chi tiết… có tính
|
|
186
|
+
* hệ thống"): its structure, its pictures, its headlines, its words, its buttons, then QA's sweep. Each step is one trade's,
|
|
187
|
+
* and within a step blocks go as a reader meets them. Gem's tag says the step. */
|
|
188
|
+
type BlockKind = "frame" | "picture" | "heading" | "words" | "action";
|
|
189
|
+
const BUILD_ORDER: ReadonlyArray<{ kind: BlockKind; step: string }> = [
|
|
190
|
+
{ kind: "frame", step: "laying out the structure" },
|
|
191
|
+
{ kind: "picture", step: "placing the pictures" },
|
|
192
|
+
{ kind: "heading", step: "writing the headlines" },
|
|
193
|
+
{ kind: "words", step: "writing the words" },
|
|
194
|
+
{ kind: "action", step: "finishing the buttons" },
|
|
195
|
+
];
|
|
196
|
+
/** Who may take a block of each kind, first choice first; a busy expert's block goes to the next one along, never to one whose
|
|
197
|
+
* name says another trade (no "Image expert · writing"). */
|
|
198
|
+
const TAKERS: Record<BlockKind, Role[]> = {
|
|
199
|
+
frame: ["layout", "style", "cro"], picture: ["image", "layout"], heading: ["copy", "layout", "style"], words: ["copy", "layout", "style"], action: ["cro", "style", "layout"],
|
|
200
|
+
};
|
|
201
|
+
const VERB: Record<BlockKind, string> = { frame: "arranging", picture: "placing", heading: "writing", words: "writing", action: "optimizing" };
|
|
202
|
+
/** What a block with no blocks inside it is. */
|
|
203
|
+
function kindOfBlock(b: HTMLElement): BlockKind {
|
|
204
|
+
if (b.matches("img, picture, video") || b.querySelector("img, picture, video")) return "picture";
|
|
205
|
+
const text = (b.textContent ?? "").trim();
|
|
206
|
+
// A button is what looks like one: a product's name that links is words (seen on a real page, 05/11: names came after their prices).
|
|
207
|
+
const control = b.matches("a, button, [role=button]") ? b : b.querySelector<HTMLElement>("a, button, [role=button]");
|
|
208
|
+
if (control && text.length < 40 && (control.tagName === "BUTTON" || paints(control))) return "action";
|
|
209
|
+
if (!text) return "frame";
|
|
210
|
+
if (b.matches("h1, h2, h3, h4") || b.querySelector("h1, h2, h3, h4")) return "heading";
|
|
211
|
+
return parseFloat(getComputedStyle(b).fontSize) >= 24 ? "heading" : "words";
|
|
212
|
+
}
|
|
213
|
+
/** Whether a block holding blocks shows anything of its own (a card's fill, a border): one that does not has nothing to build. */
|
|
214
|
+
function paints(f: HTMLElement): boolean {
|
|
215
|
+
const c = getComputedStyle(f);
|
|
216
|
+
if (c.backgroundImage !== "none" || c.boxShadow !== "none" || !/^(transparent|rgba\(0, 0, 0, 0\))$/.test(c.backgroundColor)) return true;
|
|
217
|
+
return ["Top", "Right", "Bottom", "Left"].some((side) => parseFloat(c.getPropertyValue(`border-${side.toLowerCase()}-width`)) > 0 && c.getPropertyValue(`border-${side.toLowerCase()}-style`) !== "none");
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
const KINDS = new Set<GemActivityKind>(["think", "plan", "focus", "draft", "apply", "check", "land", "fail"]);
|
|
221
|
+
const keyOf = (t: GemActivityTarget) => `${t.sectionId ?? ""}|${t.elId ?? ""}|${t.after ?? ""}|${t.at ?? ""}`;
|
|
222
|
+
/** The field a draft writes: its name, whether it is a section on its way, and where. */
|
|
223
|
+
const fieldOf = (a: GemActivity) => `${a.label}|${a.created ? "new" : ""}|${keyOf(a.target ?? {})}`;
|
|
224
|
+
/** Two drafts of one field: the same words being written. */
|
|
225
|
+
const sameField = (x: GemActivity, y: GemActivity) => x.kind === "draft" && y.kind === "draft" && fieldOf(x) === fieldOf(y);
|
|
226
|
+
|
|
227
|
+
function defaultFind(doc: Document, t: GemActivityTarget): { el: Element; level: Exclude<GemAtWorkLevel, "page"> } | null {
|
|
228
|
+
// The first match that has a box: a custom-liquid section is an outer `display: contents` wrapper around an inner section with
|
|
229
|
+
// the SAME id, and the outer one has none (gemsite ScrollBridge `boxed`, measured 11/10/2026: 4 of 7 sections had no box).
|
|
230
|
+
const q = (attr: string, v: string) => {
|
|
231
|
+
const all = doc.querySelectorAll(`[${attr}="${typeof CSS !== "undefined" && CSS.escape ? CSS.escape(v) : v.replace(/["\\]/g, "\\$&")}"]`);
|
|
232
|
+
for (const el of all) if (el.getClientRects().length > 0) return el;
|
|
233
|
+
return null;
|
|
234
|
+
};
|
|
235
|
+
const el = t.elId ? q("data-gem-el-id", t.elId) : null;
|
|
236
|
+
if (el) return { el, level: "element" };
|
|
237
|
+
const section = t.sectionId ? q("data-gem-section-id", t.sectionId) : null;
|
|
238
|
+
if (section) return { el: section, level: "section" };
|
|
239
|
+
const before = t.after ? q("data-gem-section-id", t.after) : null;
|
|
240
|
+
return before ? { el: before, level: "after" } : null;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
function isActivity(a: unknown): a is GemActivity {
|
|
244
|
+
if (!a || typeof a !== "object") return false;
|
|
245
|
+
const o = a as Partial<GemActivity>;
|
|
246
|
+
return o.v === GEM_ACTIVITY_VERSION && typeof o.kind === "string" && KINDS.has(o.kind) && typeof o.label === "string";
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
|
|
250
|
+
/** The text node that holds a block's words: the longest one not inside another block, as gemsite's live text finds it. A
|
|
251
|
+
* section named alone gives its heading. */
|
|
252
|
+
function ownText(el: Element): Text | null {
|
|
253
|
+
const scope = el.matches("[data-gem-el-id]") ? el : el.querySelector("h1, h2, h3") ?? el;
|
|
254
|
+
const own = (n: Node) => { const host = n.parentElement?.closest("[data-gem-el-id]"); return !host || host === scope || !scope.contains(host); };
|
|
255
|
+
const walker = el.ownerDocument.createTreeWalker(scope, NodeFilter.SHOW_TEXT, {
|
|
256
|
+
acceptNode: (n) => (own(n) && n.nodeValue?.trim() ? NodeFilter.FILTER_ACCEPT : NodeFilter.FILTER_SKIP),
|
|
257
|
+
});
|
|
258
|
+
let best: Text | null = null;
|
|
259
|
+
for (let n = walker.nextNode(); n; n = walker.nextNode()) if (!best || (n.nodeValue ?? "").length > (best.nodeValue ?? "").length) best = n as Text;
|
|
260
|
+
return best;
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
export function createGemAtWork(options: GemAtWorkOptions = {}): GemAtWork {
|
|
264
|
+
const root = options.root ?? document.body;
|
|
265
|
+
const doc = root.ownerDocument;
|
|
266
|
+
const win = doc.defaultView ?? window;
|
|
267
|
+
const mode = options.mode ?? "full";
|
|
268
|
+
const still = options.reducedMotion ?? !!win.matchMedia?.("(prefers-reduced-motion: reduce)").matches;
|
|
269
|
+
const pace = still ? 0 : mode === "subtle" ? 0.6 : 1;
|
|
270
|
+
const size = Math.max(1, Math.min(CREW.length, options.crew ?? CREW.length));
|
|
271
|
+
const find = options.find ?? ((t: GemActivityTarget) => defaultFind(doc, t));
|
|
272
|
+
let layer: HTMLElement | null = null;
|
|
273
|
+
let crew: Member[] = [];
|
|
274
|
+
/** The crew members building a block of the saved page right now. */
|
|
275
|
+
const onBlock = new Set<Member>();
|
|
276
|
+
const marks = new Map<string, Mark>();
|
|
277
|
+
const timers = new Set<number>();
|
|
278
|
+
let lastScan = -Infinity;
|
|
279
|
+
/** Gem's quiet spells, measured between arrivals: how far the calm clock may trail. */
|
|
280
|
+
let lastArrival = 0;
|
|
281
|
+
let quietAvg = 0;
|
|
282
|
+
/** What Gem's tag says the step is, and the ticker that counts a quiet step's seconds. */
|
|
283
|
+
let step = "";
|
|
284
|
+
let ticker = 0;
|
|
285
|
+
let tickerFrom = 0;
|
|
286
|
+
/** Where the work is now, and who walks beside it (in turn, a beat a second). */
|
|
287
|
+
let stage: Box | null = null;
|
|
288
|
+
let stageKey = "";
|
|
289
|
+
let beside: Member[] = [];
|
|
290
|
+
let beat = 0;
|
|
291
|
+
const walked = new Map<Member, number>();
|
|
292
|
+
/** The one at work on the stage now, and QA while it rides a sweep to its end. */
|
|
293
|
+
let atWork: Member | undefined;
|
|
294
|
+
let riding: Member | null = null;
|
|
295
|
+
/** Skip pressed: nothing more is drawn until the turn lands. */
|
|
296
|
+
let skipped = false;
|
|
297
|
+
let skipButton: HTMLButtonElement | null = null;
|
|
298
|
+
/** The last slot of a section this turn added that the page does not have yet: the next one stacks below it. */
|
|
299
|
+
let lastStack: HTMLElement | null = null;
|
|
300
|
+
/** Sections a build adds come one after another: a new name starts the next one (two in a row of one kind stay apart by count). */
|
|
301
|
+
let created = { label: "", seq: 0 };
|
|
302
|
+
/** The one block put in the page itself: room for the sections on their way, so the footer moves down as they stack. */
|
|
303
|
+
let space: HTMLElement | null = null;
|
|
304
|
+
/** Sections hidden while the saved page is revealed, with the style each had, to put back. */
|
|
305
|
+
let revealing: Array<{ e: HTMLElement; opacity: string; clip: string }> | null = null;
|
|
306
|
+
/** The calm clock: what has arrived and is not drawn yet, in order; `epoch` moves on skip() so a drain in flight stops. */
|
|
307
|
+
const calm = (options.clock ?? "calm") === "calm";
|
|
308
|
+
let queue: Array<{ at: number; a?: GemActivity; reveal?: readonly string[] | undefined; done?: () => void }> = [];
|
|
309
|
+
let draining = false;
|
|
310
|
+
let epoch = 0;
|
|
311
|
+
let waiting: Array<() => void> = [];
|
|
312
|
+
/** The words written so far in each field: its next draft carries on from them. */
|
|
313
|
+
const typed = new Map<string, string>();
|
|
314
|
+
let handsOn = -Infinity;
|
|
315
|
+
/** Drawing a turn's past at once (catchUp): every move ends where it was going, now. */
|
|
316
|
+
let instant = false;
|
|
317
|
+
let frame = 0;
|
|
318
|
+
|
|
319
|
+
const now = () => win.performance.now();
|
|
320
|
+
const later = (fn: () => void, ms: number) => { const id = win.setTimeout(() => { timers.delete(id); fn(); }, ms); timers.add(id); };
|
|
321
|
+
const wait = (ms: number) => new Promise<void>((done) => later(done, ms));
|
|
322
|
+
const onHands = () => { handsOn = now(); };
|
|
323
|
+
const onScroll = () => { if (!frame) frame = win.requestAnimationFrame(() => { frame = 0; cut(); }); };
|
|
324
|
+
function ensureLayer(): HTMLElement {
|
|
325
|
+
if (!layer) {
|
|
326
|
+
layer = doc.createElement("div");
|
|
327
|
+
layer.className = "gb-gaw";
|
|
328
|
+
layer.setAttribute("aria-hidden", "true");
|
|
329
|
+
root.append(layer);
|
|
330
|
+
for (const type of ["wheel", "touchmove", "keydown"]) win.addEventListener(type, onHands, { passive: true });
|
|
331
|
+
win.addEventListener("scroll", onScroll, { passive: true });
|
|
332
|
+
cut();
|
|
333
|
+
}
|
|
334
|
+
return layer;
|
|
335
|
+
}
|
|
336
|
+
/** A bar that stays at the top of the screen (a sticky header): its bottom edge, or 0. */
|
|
337
|
+
function topBar(): number {
|
|
338
|
+
for (let e = doc.elementFromPoint(win.innerWidth / 2, 2); e && e !== doc.body; e = e.parentElement) {
|
|
339
|
+
const p = getComputedStyle(e).position;
|
|
340
|
+
if (p !== "fixed" && p !== "sticky") continue;
|
|
341
|
+
const r = e.getBoundingClientRect();
|
|
342
|
+
return r.top <= 2 && r.height < win.innerHeight * 0.4 ? r.bottom : 0;
|
|
343
|
+
}
|
|
344
|
+
return 0;
|
|
345
|
+
}
|
|
346
|
+
/** Everything drawn goes under that bar as the page's own blocks do when it scrolls, not over it (seen on a real page, 05/11). */
|
|
347
|
+
function cut() {
|
|
348
|
+
if (!layer) return;
|
|
349
|
+
const bar = topBar(), y = bar - layer.getBoundingClientRect().top;
|
|
350
|
+
layer.style.clipPath = bar > 0 ? `polygon(-100000px ${y}px, 100000px ${y}px, 100000px 10000000px, -100000px 10000000px)` : "";
|
|
351
|
+
}
|
|
352
|
+
function box(el: Element | Range | DOMRect): Box {
|
|
353
|
+
const r = "getBoundingClientRect" in el ? el.getBoundingClientRect() : el, b = ensureLayer().getBoundingClientRect();
|
|
354
|
+
return { x: r.left - b.left, y: r.top - b.top, w: r.width, h: r.height };
|
|
355
|
+
}
|
|
356
|
+
function place(d: HTMLElement, r: Box) { Object.assign(d.style, { left: `${r.x}px`, top: `${r.y}px`, width: `${r.w}px`, height: `${r.h}px` }); }
|
|
357
|
+
function piece(cls: string, r?: Box): HTMLElement {
|
|
358
|
+
const d = doc.createElement("div");
|
|
359
|
+
d.className = cls;
|
|
360
|
+
if (r) place(d, r);
|
|
361
|
+
ensureLayer().append(d);
|
|
362
|
+
return d;
|
|
363
|
+
}
|
|
364
|
+
/** Moves only what the compositor moves; ends where it was going, and with no motion it is simply there. */
|
|
365
|
+
function move(el: HTMLElement, frames: Keyframe[], ms: number, delay = 0): Animation | null {
|
|
366
|
+
const end = frames[frames.length - 1] as Record<string, string>;
|
|
367
|
+
if (!pace || instant || typeof el.animate !== "function") { Object.assign(el.style, end); return null; }
|
|
368
|
+
const a = el.animate(frames, { duration: ms * pace, delay: delay * pace, easing: EASE, fill: "forwards" });
|
|
369
|
+
a.finished.then(() => { Object.assign(el.style, end); a.cancel(); }).catch(() => {});
|
|
370
|
+
return a;
|
|
371
|
+
}
|
|
372
|
+
function gone(el: HTMLElement | undefined, ms = 220, delay = 0) {
|
|
373
|
+
if (!el) return;
|
|
374
|
+
const a = move(el, [{ opacity: getComputedStyle(el).opacity }, { opacity: "0" }], ms, delay);
|
|
375
|
+
if (a) a.finished.then(() => el.remove(), () => el.remove()); else el.remove();
|
|
376
|
+
}
|
|
377
|
+
/** Where the person is looking, in the layer's terms. */
|
|
378
|
+
function view(): Box { const b = ensureLayer().getBoundingClientRect(); return { x: -b.left, y: -b.top, w: win.innerWidth, h: win.innerHeight }; }
|
|
379
|
+
/** Keeps the work in view as it goes down the page, unless the person moved the page themselves a moment ago. */
|
|
380
|
+
function follow(r: Box) {
|
|
381
|
+
if (!pace || now() - handsOn < HANDS_MS) return;
|
|
382
|
+
// Drawn at its full height (a board pans it, not this document): the host is told where to look.
|
|
383
|
+
if (doc.documentElement.scrollHeight <= win.innerHeight + 2) { options.onLook?.(Math.round(r.y - view().y), Math.round(r.h)); return; }
|
|
384
|
+
const y = r.y - view().y;
|
|
385
|
+
if (y < topBar() || y > win.innerHeight * 0.55) win.scrollTo({ top: Math.max(0, win.scrollY + y - topBar() - 96), behavior: "smooth" });
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
/** The crew, arriving the first time anything happens; none in subtle mode or with reduced motion. */
|
|
389
|
+
function arrive(): Member[] {
|
|
390
|
+
if (mode !== "full" || still) return [];
|
|
391
|
+
if (!crew.length) {
|
|
392
|
+
const v = view(), under = Math.max(60, topBar() + 28);
|
|
393
|
+
crew = CREW.slice(0, size).map((m, i) => {
|
|
394
|
+
const el = doc.createElement("div");
|
|
395
|
+
el.className = "gb-gaw__cursor";
|
|
396
|
+
el.style.setProperty("--c", m.c);
|
|
397
|
+
el.style.setProperty("--ink", m.ink);
|
|
398
|
+
el.innerHTML = `${ARROW}<span class="gb-gaw__tag"></span>`;
|
|
399
|
+
ensureLayer().append(el);
|
|
400
|
+
// Gem where the person is looking; the experts come on when their trade does.
|
|
401
|
+
const x = v.x + v.w * 0.45, y = v.y + under;
|
|
402
|
+
el.style.transform = `translate(${x}px,${y}px)`;
|
|
403
|
+
const member: Member = { ...m, el, tag: el.querySelector(".gb-gaw__tag") as HTMLElement, x, y, anim: null, fade: null, resting: i > 0 };
|
|
404
|
+
if (i > 0) { el.classList.add("is-resting"); el.style.opacity = "0"; } else move(el, [{ opacity: "0" }, { opacity: "1" }], 220);
|
|
405
|
+
say(member);
|
|
406
|
+
return member;
|
|
407
|
+
});
|
|
408
|
+
}
|
|
409
|
+
return crew;
|
|
410
|
+
}
|
|
411
|
+
function say(m: Member, doing?: string) { m.tag.textContent = m.role === "lead" && !doing ? m.name : `${m.name} · ${doing ?? m.doing}`; }
|
|
412
|
+
/** The one whose trade this is, now at work (named, at full strength); Gem with a crew of one. */
|
|
413
|
+
function who(role: Role): Member | undefined {
|
|
414
|
+
const c = arrive(), m = c.find((x) => x.role === role) ?? c[0];
|
|
415
|
+
if (m) wake(m);
|
|
416
|
+
return m;
|
|
417
|
+
}
|
|
418
|
+
function wake(m: Member) {
|
|
419
|
+
if (!m.resting) return;
|
|
420
|
+
m.resting = false;
|
|
421
|
+
m.el.classList.remove("is-resting");
|
|
422
|
+
m.fade?.cancel();
|
|
423
|
+
m.fade = move(m.el, [{ opacity: "0" }, { opacity: "1" }], 220);
|
|
424
|
+
}
|
|
425
|
+
function rest(m: Member) {
|
|
426
|
+
if (m.resting) return;
|
|
427
|
+
m.resting = true;
|
|
428
|
+
m.el.classList.add("is-resting");
|
|
429
|
+
say(m);
|
|
430
|
+
m.fade?.cancel();
|
|
431
|
+
m.fade = move(m.el, [{ opacity: "1" }, { opacity: "0" }], 260);
|
|
432
|
+
}
|
|
433
|
+
function travel(m: Member | undefined, x: number, y: number, ms?: number) {
|
|
434
|
+
if (!m) return;
|
|
435
|
+
const from = getComputedStyle(m.el).transform;
|
|
436
|
+
m.anim?.cancel();
|
|
437
|
+
const d = ms ?? Math.max(200, Math.min(560, 180 + Math.hypot(x - m.x, y - m.y) * 0.35));
|
|
438
|
+
m.x = x; m.y = y;
|
|
439
|
+
const to = `translate(${x}px,${y}px)`;
|
|
440
|
+
m.anim = move(m.el, [{ transform: from === "none" ? to : from }, { transform: to }], d);
|
|
441
|
+
}
|
|
442
|
+
/** The work is here, this one at it: the two trades beside it come on (or `alongside: false`, a block being built has its own
|
|
443
|
+
* hands), everyone else goes. Said again and again while words are written: only a new place or a new hand moves anyone. */
|
|
444
|
+
function onStage(r: Box, doer: Member | undefined, alongside = true) {
|
|
445
|
+
stage = r;
|
|
446
|
+
atWork = doer;
|
|
447
|
+
const key = `${Math.round(r.x)}|${Math.round(r.y)}|${Math.round(r.w)}|${doer?.role ?? ""}|${alongside}`;
|
|
448
|
+
if (key === stageKey || !crew.length) return;
|
|
449
|
+
stageKey = key;
|
|
450
|
+
beside = alongside ? ALONGSIDE[doer?.role ?? "lead"].map((role) => crew.find((m) => m.role === role)).filter((m): m is Member => !!m && m !== doer && m.role !== "lead" && !onBlock.has(m)) : [];
|
|
451
|
+
for (const m of crew) if (m.role !== "lead" && m !== doer && m !== riding && !onBlock.has(m) && !beside.includes(m)) rest(m);
|
|
452
|
+
beside.forEach((m, i) => {
|
|
453
|
+
const p = waypoint(i, walked.get(m) ?? 0);
|
|
454
|
+
say(m, BESIDE[m.role]);
|
|
455
|
+
if (m.resting) { m.anim?.cancel(); m.x = p.x; m.y = p.y; m.el.style.transform = `translate(${p.x}px,${p.y}px)`; wake(m); } else travel(m, p.x, p.y, 600);
|
|
456
|
+
});
|
|
457
|
+
const lead = crew.find((m) => m.role === "lead");
|
|
458
|
+
if (lead) { const p = waypoint(2, walked.get(lead) ?? 0); travel(lead, p.x, p.y, 500); }
|
|
459
|
+
startTicker();
|
|
460
|
+
}
|
|
461
|
+
/** Where one beside the work walks: along the block's edges, off its words. The first below it, the second down its right-hand
|
|
462
|
+
* side, Gem above it; there and back in eight beats. */
|
|
463
|
+
function waypoint(i: number, at: number): { x: number; y: number } {
|
|
464
|
+
const r = stage!, v = view(), bar = topBar();
|
|
465
|
+
const along = Math.abs((at % 8) / 4 - 1);
|
|
466
|
+
const top = Math.max(r.y, v.y + bar + 24), bottom = Math.max(top, Math.min(r.y + r.h, v.y + v.h - 60));
|
|
467
|
+
const roomRight = v.x + v.w - 170;
|
|
468
|
+
if (i === 0) return { x: Math.min(roomRight, r.x + r.w * (0.08 + 0.55 * along)), y: Math.min(r.y + r.h + 10, v.y + v.h - 44) };
|
|
469
|
+
if (i === 1) return { x: Math.min(roomRight, r.x + r.w + 12), y: top + (bottom - top) * along };
|
|
470
|
+
return { x: Math.min(roomRight, r.x + r.w * (0.42 + 0.4 * (1 - along))), y: Math.max(r.y - 40, v.y + bar + 6) };
|
|
471
|
+
}
|
|
472
|
+
/** A beat: the next of them takes its next steps along its edge, an easy walk then a pause, never a jump. */
|
|
473
|
+
function strollNext() {
|
|
474
|
+
if (!stage) return;
|
|
475
|
+
const lead = crew.find((m) => m.role === "lead");
|
|
476
|
+
const walkers = [...beside.filter((m) => !m.resting && !onBlock.has(m)), ...(lead ? [lead] : [])];
|
|
477
|
+
beat += 1;
|
|
478
|
+
const m = walkers[beat % Math.max(1, walkers.length)];
|
|
479
|
+
if (!m || onBlock.has(m)) return;
|
|
480
|
+
const steps = (walked.get(m) ?? 0) + 1;
|
|
481
|
+
walked.set(m, steps);
|
|
482
|
+
const p = waypoint(m === lead ? 2 : beside.indexOf(m), steps);
|
|
483
|
+
travel(m, p.x, p.y, 1500);
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
function markFor(a: GemActivity): Mark | null {
|
|
487
|
+
const t = a.target ?? (a.created ? {} : undefined);
|
|
488
|
+
if (!t) return null;
|
|
489
|
+
if (a.created && a.label !== created.label) created = { label: a.label, seq: created.seq + 1 };
|
|
490
|
+
const key = a.created ? `new|${keyOf(t)}|${created.seq}` : keyOf(t);
|
|
491
|
+
const known = marks.get(key);
|
|
492
|
+
if (known?.el.isConnected) return known;
|
|
493
|
+
const hit = t.elId || t.sectionId || t.after ? find(t) : null;
|
|
494
|
+
if (!hit) {
|
|
495
|
+
// A section this turn adds, on a page that does not have it yet (a page built from scratch): its slot stacks.
|
|
496
|
+
if (a.created) { const mark: Mark = { key, el: doc.body, level: "after", target: t, stacked: true }; marks.set(key, mark); return mark; }
|
|
497
|
+
options.onFallback?.("page", a);
|
|
498
|
+
return null;
|
|
499
|
+
}
|
|
500
|
+
const asked: GemAtWorkLevel = t.elId ? "element" : t.sectionId ? "section" : t.after ? "after" : "page";
|
|
501
|
+
if (hit.level !== asked) options.onFallback?.(hit.level, a);
|
|
502
|
+
const mark: Mark = { key, el: hit.el, level: hit.level, target: t };
|
|
503
|
+
marks.set(key, mark);
|
|
504
|
+
return mark;
|
|
505
|
+
}
|
|
506
|
+
/** The page's content column (where its blocks start and end), so a section on its way lines up with the page, not the screen. */
|
|
507
|
+
function column(): { x: number; w: number } {
|
|
508
|
+
const v = view();
|
|
509
|
+
const blocks = [...doc.querySelectorAll("[data-gem-section-id] [data-gem-el-id]")].filter((b) => b.getClientRects().length > 0).slice(0, 40).map((b) => box(b));
|
|
510
|
+
const narrow = blocks.filter((b) => b.w < v.w * 0.9);
|
|
511
|
+
if (narrow.length) { const x = Math.min(...narrow.map((b) => b.x)), r = Math.max(...narrow.map((b) => b.x + b.w)); return { x, w: r - x }; }
|
|
512
|
+
const w = Math.min(1040, v.w * 0.84);
|
|
513
|
+
return { x: v.x + (v.w - w) / 2, w };
|
|
514
|
+
}
|
|
515
|
+
/** The page's own sections, outermost, with a box (a custom-liquid section's outer wrapper has none; its inner one counts). */
|
|
516
|
+
function topSections(): HTMLElement[] {
|
|
517
|
+
const boxed = [...doc.querySelectorAll<HTMLElement>("[data-gem-section-id]")].filter((e) => e.getClientRects().length > 0);
|
|
518
|
+
return boxed.filter((e) => !boxed.some((o) => o !== e && o.contains(e)));
|
|
519
|
+
}
|
|
520
|
+
/** Where the next section of a page being built goes: below the last one on its way, else where the room for them starts (after
|
|
521
|
+
* the page's last saved section), else below the header. */
|
|
522
|
+
function stackBox(): Box {
|
|
523
|
+
const c = column();
|
|
524
|
+
if (lastStack?.isConnected) { const b = box(lastStack); return { x: c.x, y: b.y + b.h + 16, w: c.w, h: 120 }; }
|
|
525
|
+
const at = room();
|
|
526
|
+
if (at) { const b = box(at); return { x: c.x, y: b.y + 24, w: c.w, h: 120 }; }
|
|
527
|
+
const head = doc.querySelector("header");
|
|
528
|
+
return { x: c.x, y: head ? box(head).y + box(head).h + 24 : view().y + 80, w: c.w, h: 120 };
|
|
529
|
+
}
|
|
530
|
+
/** The room a page being built makes for its sections on their way (Chris 05/11: the footer must move down, not be drawn over):
|
|
531
|
+
* an empty block after the page's last saved section, or at the end of its main, as tall as the stack. */
|
|
532
|
+
function room(): HTMLElement | null {
|
|
533
|
+
if (!space?.isConnected) {
|
|
534
|
+
const saved = topSections(), end = saved[saved.length - 1], main = doc.querySelector("main");
|
|
535
|
+
if (!end && !main) return null;
|
|
536
|
+
space = doc.createElement("div");
|
|
537
|
+
space.setAttribute("aria-hidden", "true");
|
|
538
|
+
if (end) end.after(space); else main!.append(space);
|
|
539
|
+
}
|
|
540
|
+
if (lastStack?.isConnected) space.style.height = `${Math.max(0, lastStack.getBoundingClientRect().bottom - space.getBoundingClientRect().top + 24)}px`;
|
|
541
|
+
return space;
|
|
542
|
+
}
|
|
543
|
+
function unroom() { space?.remove(); space = null; }
|
|
544
|
+
/** A section on its way's own room: as tall as its slot and the gap round it. */
|
|
545
|
+
function fit(m: Mark) { if (m.room && m.slot) m.room.style.height = `${m.slot.getBoundingClientRect().height + 16}px`; }
|
|
546
|
+
/** The box a mark is drawn round: the block, or for a section on its way, the place below the one it follows. */
|
|
547
|
+
const placeOf = (m: Mark): Box => {
|
|
548
|
+
if (m.slot) return box(m.slot);
|
|
549
|
+
const r = box(m.el);
|
|
550
|
+
return m.level === "after" ? { x: r.x, y: r.y + r.h + 8, w: r.w, h: 120 } : r;
|
|
551
|
+
};
|
|
552
|
+
|
|
553
|
+
function focus(a: GemActivity) {
|
|
554
|
+
const m = markFor(a);
|
|
555
|
+
const lead = who("lead");
|
|
556
|
+
// A step on the whole page (a clone's "Cloning that page into your store"): Gem's tag says it, and counts its seconds.
|
|
557
|
+
if (!m) { if (lead) tellStep(a.label); return; }
|
|
558
|
+
const r = placeOf(m);
|
|
559
|
+
if (m.level === "after" && !m.slot) {
|
|
560
|
+
m.slot = piece(m.stacked ? "gb-gaw__slot gb-gaw__slot--new" : "gb-gaw__slot", m.stacked ? stackBox() : r);
|
|
561
|
+
if (m.stacked) { lastStack = m.slot; room(); follow(box(m.slot)); }
|
|
562
|
+
m.slot.style.height = "";
|
|
563
|
+
if (!m.stacked) { m.room = doc.createElement("div"); m.room.setAttribute("aria-hidden", "true"); m.el.after(m.room); fit(m); }
|
|
564
|
+
move(m.slot, [{ clipPath: "inset(0 100% 100% 0)" }, { clipPath: "inset(-2px -2px -2px -2px)" }], 260);
|
|
565
|
+
}
|
|
566
|
+
if (!m.ring) {
|
|
567
|
+
m.ring = piece(m.level === "after" ? "gb-gaw__ring gb-gaw__ring--slot" : "gb-gaw__ring", m.level === "after" ? placeOf(m) : box(m.el));
|
|
568
|
+
const label = doc.createElement("span");
|
|
569
|
+
label.className = "gb-gaw__label";
|
|
570
|
+
label.textContent = m.level === "after" ? `New · ${a.label}` : a.label;
|
|
571
|
+
m.ring.append(label);
|
|
572
|
+
move(m.ring, [{ opacity: "0" }, { opacity: "1" }], 160);
|
|
573
|
+
}
|
|
574
|
+
const at = placeOf(m);
|
|
575
|
+
follow(at);
|
|
576
|
+
const layout = who("layout");
|
|
577
|
+
travel(layout, at.x + 2, at.y + 2);
|
|
578
|
+
onStage(at, layout);
|
|
579
|
+
}
|
|
580
|
+
|
|
581
|
+
function writeIn(m: Mark, a: GemActivity) {
|
|
582
|
+
if (a.text === undefined) return;
|
|
583
|
+
if (m.level === "after") {
|
|
584
|
+
// A section on its way: its words in its slot, in the page's own font, a new line when a new field starts.
|
|
585
|
+
const lines = (m.lines ??= []);
|
|
586
|
+
const last = lines[lines.length - 1];
|
|
587
|
+
if (last !== undefined && (a.text.startsWith(last) || last.startsWith(a.text))) lines[lines.length - 1] = a.text; else lines.push(a.text);
|
|
588
|
+
const slot = m.slot;
|
|
589
|
+
if (!slot) return;
|
|
590
|
+
const font = getComputedStyle(m.el.querySelector("h1,h2,h3,p") ?? m.el).fontFamily;
|
|
591
|
+
const fresh = slot.children.length !== lines.length;
|
|
592
|
+
slot.replaceChildren(...lines.map((line, i) => {
|
|
593
|
+
const p = doc.createElement("div");
|
|
594
|
+
p.className = i === 0 ? "gb-gaw__ghost-h" : "gb-gaw__ghost-p";
|
|
595
|
+
p.style.fontFamily = font;
|
|
596
|
+
p.textContent = line;
|
|
597
|
+
if (i === lines.length - 1 && a.partial) { const c = doc.createElement("span"); c.className = "gb-gaw__caret gb-gaw__caret--inline"; p.append(c); }
|
|
598
|
+
return p;
|
|
599
|
+
}));
|
|
600
|
+
if (m.ring) place(m.ring, box(slot));
|
|
601
|
+
if (m.stacked) room(); else fit(m);
|
|
602
|
+
// The line being written gets the board's box: dragged from its corner when the line starts, then held round it, snug.
|
|
603
|
+
const line = slot.lastElementChild as HTMLElement | null;
|
|
604
|
+
if (line && mode === "full") {
|
|
605
|
+
const r = box(line);
|
|
606
|
+
if (fresh || !m.sel) {
|
|
607
|
+
if (m.sel) gone(m.sel, 160);
|
|
608
|
+
m.sel = piece("gb-gaw__sel", r);
|
|
609
|
+
m.sel.innerHTML = "<i></i><i></i><i></i><i></i>";
|
|
610
|
+
move(m.sel, [{ clipPath: "inset(-5px 100% 100% -5px)" }, { clipPath: "inset(-5px -5px -5px -5px)" }], 220);
|
|
611
|
+
const writer = who(roleOf(a));
|
|
612
|
+
travel(writer, r.x + 2, r.y + 2, 120);
|
|
613
|
+
} else place(m.sel, r);
|
|
614
|
+
const writer = who(roleOf(a));
|
|
615
|
+
if (writer && a.partial) travel(writer, r.x + r.w + 6, r.y + r.h * 0.6, 120);
|
|
616
|
+
}
|
|
617
|
+
return;
|
|
618
|
+
}
|
|
619
|
+
// A block on the page: its own words, written where they are read. A section named alone takes only a heading's words, into
|
|
620
|
+
// its heading; any other field's words wait for the saved page rather than land in the wrong place.
|
|
621
|
+
if (m.level === "section" && !/^(Heading|Title)$/.test(a.label)) return;
|
|
622
|
+
const node = m.text?.node.isConnected ? m.text.node : ownText(m.el);
|
|
623
|
+
if (!node) return;
|
|
624
|
+
if (!m.text || m.text.node !== node) m.text = { node, was: node.nodeValue ?? "" };
|
|
625
|
+
node.nodeValue = a.text;
|
|
626
|
+
const r = box(m.el);
|
|
627
|
+
if (m.sel) { place(m.sel, r); const dim = m.sel.querySelector(".gb-gaw__dim"); if (dim) dim.textContent = `${Math.round(r.w)} × ${Math.round(r.h)}`; }
|
|
628
|
+
if (m.ring && m.level === "element") place(m.ring, r);
|
|
629
|
+
if (a.partial) {
|
|
630
|
+
const range = doc.createRange();
|
|
631
|
+
range.setStart(node, node.length);
|
|
632
|
+
range.setEnd(node, node.length);
|
|
633
|
+
const rects = range.getClientRects();
|
|
634
|
+
const end = rects.length ? box(rects[rects.length - 1]!) : null;
|
|
635
|
+
m.caret ??= piece("gb-gaw__caret");
|
|
636
|
+
if (end) place(m.caret, { x: end.x + 1, y: end.y, w: 2, h: end.h });
|
|
637
|
+
const writer = who(roleOf(a));
|
|
638
|
+
if (end && writer) travel(writer, end.x + 6, end.y + end.h * 0.6, 140);
|
|
639
|
+
} else if (m.caret) { m.caret.remove(); m.caret = undefined; }
|
|
640
|
+
}
|
|
641
|
+
function putBack(m: Mark) {
|
|
642
|
+
if (m.text?.node.isConnected) m.text.node.nodeValue = m.text.was;
|
|
643
|
+
m.text = undefined;
|
|
644
|
+
}
|
|
645
|
+
|
|
646
|
+
function draft(a: GemActivity) {
|
|
647
|
+
const m = markFor(a);
|
|
648
|
+
if (!m) return;
|
|
649
|
+
if (!m.ring) focus(a);
|
|
650
|
+
const writer = who(roleOf(a));
|
|
651
|
+
if (writer) say(writer);
|
|
652
|
+
const r = placeOf(m);
|
|
653
|
+
if (!m.sel && mode === "full" && !m.stacked) {
|
|
654
|
+
m.sel = piece("gb-gaw__sel", r);
|
|
655
|
+
m.sel.innerHTML = "<i></i><i></i><i></i><i></i>";
|
|
656
|
+
const ms = Math.max(160, Math.min(380, 150 + (r.w + r.h) * 0.18));
|
|
657
|
+
move(m.sel, [{ clipPath: "inset(-5px 100% 100% -5px)" }, { clipPath: "inset(-5px -5px -5px -5px)" }], ms);
|
|
658
|
+
travel(writer, r.x + 2, r.y + 2, 120);
|
|
659
|
+
later(() => travel(writer, r.x + r.w - 3, r.y + r.h - 3, ms), 120 * pace);
|
|
660
|
+
// Two guides from its edges, across its section only, gone once the box is drawn.
|
|
661
|
+
const across = box(m.el.closest("[data-gem-section-id]") ?? m.el);
|
|
662
|
+
for (const y of [r.y, r.y + r.h]) {
|
|
663
|
+
const g = piece("gb-gaw__guide", { x: across.x, y, w: across.w, h: 1 });
|
|
664
|
+
move(g, [{ opacity: "0" }, { opacity: "0.55" }], 100);
|
|
665
|
+
gone(g, 200, 120 + ms);
|
|
666
|
+
}
|
|
667
|
+
const dim = doc.createElement("span");
|
|
668
|
+
dim.className = "gb-gaw__dim";
|
|
669
|
+
dim.textContent = `${Math.round(r.w)} × ${Math.round(r.h)}`;
|
|
670
|
+
m.sel.append(dim);
|
|
671
|
+
}
|
|
672
|
+
onStage(r, writer);
|
|
673
|
+
if (a.label === "Picture" && !m.veil && m.level !== "after") m.veil = piece("gb-gaw__veil", box(m.el));
|
|
674
|
+
writeIn(m, a);
|
|
675
|
+
}
|
|
676
|
+
|
|
677
|
+
function settle(a: GemActivity, cls: "is-applied" | "is-failed") {
|
|
678
|
+
const m = a.target ? marks.get(keyOf(a.target)) : undefined;
|
|
679
|
+
for (const part of [m?.ring, m?.sel, m?.slot]) part?.classList.add(cls);
|
|
680
|
+
const lead = who("lead");
|
|
681
|
+
if (cls === "is-applied") { tellStep("applied ✓"); return; }
|
|
682
|
+
if (m) putBack(m);
|
|
683
|
+
if (lead) {
|
|
684
|
+
lead.el.classList.add("is-failed");
|
|
685
|
+
say(lead, a.label);
|
|
686
|
+
lead.tag.textContent = a.label;
|
|
687
|
+
move(lead.tag, [{ transform: "translateX(0)" }, { transform: "translateX(-5px)" }, { transform: "translateX(5px)" }, { transform: "translateX(0)" }], 320);
|
|
688
|
+
}
|
|
689
|
+
later(() => { if (m) clearMark(m); if (lead) { lead.el.classList.remove("is-failed"); say(lead); } }, FAIL_MS);
|
|
690
|
+
}
|
|
691
|
+
|
|
692
|
+
/** The part of a place on screen now (a section longer than the view is swept where it shows). */
|
|
693
|
+
function inView(r: Box): Box {
|
|
694
|
+
const v = view(), top = Math.max(r.y, v.y + topBar()), bottom = Math.min(r.y + r.h, v.y + v.h);
|
|
695
|
+
return bottom - top > 40 ? { x: r.x, y: top, w: r.w, h: bottom - top } : { x: v.x, y: v.y + topBar(), w: v.w, h: v.h - topBar() };
|
|
696
|
+
}
|
|
697
|
+
/** QA's sweep (Chris 05/11: the line "nhìn cũng nguy hiểm nên thường xuyên xuất hiện"): down a place and back, QA riding its end,
|
|
698
|
+
* named, gone again after unless its trade is on. At every check, each section just built, and every few seconds of a turn. */
|
|
699
|
+
function sweep(r: Box) {
|
|
700
|
+
const t = now();
|
|
701
|
+
if (still || !pace || instant || mode === "off" || t - lastScan < SCAN_GAP_MS || typeof doc.body.animate !== "function") return;
|
|
702
|
+
lastScan = t;
|
|
703
|
+
const ms = Math.max(1600, Math.min(2800, r.h * 2.2));
|
|
704
|
+
const line = piece("gb-gaw__scan", { x: r.x, y: r.y, w: r.w, h: 2 });
|
|
705
|
+
const run = line.animate([{ transform: "translateY(0)", easing: "ease-in-out" }, { transform: `translateY(${r.h}px)`, easing: "ease-in-out" }, { transform: "translateY(0)" }], { duration: ms });
|
|
706
|
+
run.finished.then(() => line.remove(), () => line.remove());
|
|
707
|
+
const qa = crew.find((m) => m.role === "check");
|
|
708
|
+
if (!qa || onBlock.has(qa)) return;
|
|
709
|
+
wake(qa);
|
|
710
|
+
say(qa);
|
|
711
|
+
riding = qa;
|
|
712
|
+
const x = Math.min(view().x + view().w - 170, r.x + r.w * 0.72);
|
|
713
|
+
travel(qa, x, r.y - 4, 200);
|
|
714
|
+
later(() => travel(qa, x, r.y + r.h - 6, ms / 2 - 200), 200);
|
|
715
|
+
later(() => travel(qa, x, r.y - 4, ms / 2), ms / 2);
|
|
716
|
+
later(() => { riding = null; if (qa !== atWork && !beside.includes(qa) && !onBlock.has(qa)) rest(qa); }, ms + 120);
|
|
717
|
+
}
|
|
718
|
+
function scan() {
|
|
719
|
+
const v = view(), qa = who("check");
|
|
720
|
+
onStage(marks.size && stage ? stage : { x: v.x + v.w * 0.2, y: v.y + 60, w: v.w * 0.6, h: v.h - 160 }, qa);
|
|
721
|
+
sweep(stage && doc.documentElement.scrollHeight <= win.innerHeight + 2 ? inView(stage) : inView({ x: v.x, y: v.y, w: v.w, h: v.h }));
|
|
722
|
+
}
|
|
723
|
+
|
|
724
|
+
function clearMark(m: Mark) {
|
|
725
|
+
for (const part of [m.ring, m.slot, m.sel, m.veil, m.caret]) gone(part);
|
|
726
|
+
m.room?.remove();
|
|
727
|
+
marks.delete(m.key);
|
|
728
|
+
}
|
|
729
|
+
|
|
730
|
+
function land(a: GemActivity) {
|
|
731
|
+
const lit: Element[] = [];
|
|
732
|
+
if (a.changed) for (const m of marks.values()) if (m.el.isConnected && m.level !== "after") lit.push(m.el);
|
|
733
|
+
// No change landed: every word written goes back. A change landed: the saved page draws its own words over these.
|
|
734
|
+
if (!a.changed) for (const m of marks.values()) putBack(m);
|
|
735
|
+
for (const m of [...marks.values()]) clearMark(m);
|
|
736
|
+
for (const el of lit) {
|
|
737
|
+
const g = piece("gb-gaw__glow", box(el));
|
|
738
|
+
// With no motion the light is simply on for a second, then off.
|
|
739
|
+
if (!pace) { g.style.opacity = "1"; later(() => g.remove(), 1000); continue; }
|
|
740
|
+
const anim = move(g, [{ opacity: "0" }, { opacity: "1", offset: 0.15 }, { opacity: "1", offset: 0.65 }, { opacity: "0" }], 1600);
|
|
741
|
+
if (anim) anim.finished.then(() => g.remove(), () => g.remove()); else g.remove();
|
|
742
|
+
}
|
|
743
|
+
lastStack = null;
|
|
744
|
+
created = { label: "", seq: created.seq };
|
|
745
|
+
typed.clear();
|
|
746
|
+
unroom();
|
|
747
|
+
stopTicker();
|
|
748
|
+
step = "";
|
|
749
|
+
if (!queue.length) hideSkip();
|
|
750
|
+
leave();
|
|
751
|
+
}
|
|
752
|
+
function leave() {
|
|
753
|
+
const leaving = crew;
|
|
754
|
+
crew = [];
|
|
755
|
+
stage = null; stageKey = ""; beside = []; riding = null;
|
|
756
|
+
leaving.forEach((m, i) => gone(m.el, 300, 400 + i * 60));
|
|
757
|
+
}
|
|
758
|
+
|
|
759
|
+
/** Blocks as a reader meets them: rows from the top (a block that starts above the middle of the row's first one is in its
|
|
760
|
+
* row), each row from the left. */
|
|
761
|
+
function readingOrder(blocks: HTMLElement[]): HTMLElement[] {
|
|
762
|
+
const rows: Array<Array<{ b: HTMLElement; r: DOMRect }>> = [];
|
|
763
|
+
for (const x of blocks.map((b) => ({ b, r: b.getBoundingClientRect() })).sort((p, q) => p.r.top - q.r.top)) {
|
|
764
|
+
const row = rows[rows.length - 1];
|
|
765
|
+
if (row && x.r.top < row[0]!.r.top + Math.max(12, Math.min(row[0]!.r.height, x.r.height) / 2)) row.push(x); else rows.push([x]);
|
|
766
|
+
}
|
|
767
|
+
return rows.flatMap((row) => row.sort((p, q) => p.r.left - q.r.left).map((x) => x.b));
|
|
768
|
+
}
|
|
769
|
+
/** Saved sections, as the approved board builds them (artifact 7HCVjjq2…, the video's grammar): in turn, each one through the one
|
|
770
|
+
* order (BUILD_ORDER), a screenful at a time; the crew holding the mouse down at a block's corner and dragging its box out to size.
|
|
771
|
+
* Slow enough to follow (Chris 05/11), quicker when the clock trails. The sections on their way that stood for them go first, with
|
|
772
|
+
* their room. Every style it touches is put back as it was. */
|
|
773
|
+
async function showSaved(only: readonly string[] | undefined, hurry: number): Promise<void> {
|
|
774
|
+
const sections = topSections().filter((e) => !only || only.includes(e.getAttribute("data-gem-section-id") ?? ""));
|
|
775
|
+
if (!sections.length) return;
|
|
776
|
+
for (const m of [...marks.values()]) if (m.stacked) clearMark(m);
|
|
777
|
+
lastStack = null;
|
|
778
|
+
created = { label: "", seq: created.seq };
|
|
779
|
+
unroom();
|
|
780
|
+
const was = sections.map((e) => ({ e, opacity: e.style.opacity, clip: e.style.clipPath }));
|
|
781
|
+
revealing = was;
|
|
782
|
+
for (const e of sections) e.style.opacity = "0";
|
|
783
|
+
who("lead");
|
|
784
|
+
tellStep("building the page");
|
|
785
|
+
showSkip();
|
|
786
|
+
cut();
|
|
787
|
+
for (const [i, e] of sections.entries()) {
|
|
788
|
+
if (revealing !== was) return;
|
|
789
|
+
const r = box(e);
|
|
790
|
+
follow(r);
|
|
791
|
+
// Every block of it hidden, none shown ahead of its turn (Chris 05/11: "có mấy cái hiện sẵn rồi xong vào vẽ tiếp"). The section's
|
|
792
|
+
// own ground comes first; a block holding blocks that paints nothing is simply there (what it holds has its own turn).
|
|
793
|
+
const all = [...e.querySelectorAll<HTMLElement>("[data-gem-el-id]")].filter((b) => b.getClientRects().length > 0);
|
|
794
|
+
const leaves = all.filter((b) => !b.querySelector("[data-gem-el-id]"));
|
|
795
|
+
const holders = all.filter((b) => b.querySelector("[data-gem-el-id]"));
|
|
796
|
+
const kinds = new Map<HTMLElement, BlockKind>(leaves.map((b) => [b, kindOfBlock(b)]));
|
|
797
|
+
for (const f of holders) if (paints(f)) kinds.set(f, "frame");
|
|
798
|
+
const own = new Map<HTMLElement, string>();
|
|
799
|
+
for (const b of all) { own.set(b, b.style.opacity); was.push({ e: b, opacity: b.style.opacity, clip: b.style.clipPath }); b.style.opacity = "0"; }
|
|
800
|
+
for (const f of holders) if (!kinds.has(f)) f.style.opacity = own.get(f) ?? "";
|
|
801
|
+
e.style.opacity = was[i]!.opacity;
|
|
802
|
+
if (!leaves.length || !pace) {
|
|
803
|
+
for (const b of all) b.style.opacity = own.get(b) ?? "";
|
|
804
|
+
if (pace && typeof e.animate === "function") e.animate([{ clipPath: "inset(0 0 100% 0)", opacity: 0 }, { clipPath: "inset(0 0 0 0)", opacity: 1 }], { duration: 1200 / hurry, easing: EASE });
|
|
805
|
+
await wait(pace ? 1400 / hurry : 0);
|
|
806
|
+
continue;
|
|
807
|
+
}
|
|
808
|
+
if (typeof e.animate === "function") e.animate([{ opacity: 0 }, { opacity: 1 }], { duration: 300 / hurry, easing: EASE });
|
|
809
|
+
await wait(360 / hurry);
|
|
810
|
+
// A section of many blocks goes quicker per block (eight blocks' time at most at full pace).
|
|
811
|
+
const quick = hurry * Math.max(1, kinds.size / 8);
|
|
812
|
+
// A screenful at a time, each through the whole order, so the page only ever moves down.
|
|
813
|
+
for (const band of bandsOf([...kinds.keys()])) {
|
|
814
|
+
const area = bandBox(band);
|
|
815
|
+
follow(area);
|
|
816
|
+
for (const { kind, step } of BUILD_ORDER) {
|
|
817
|
+
const group = readingOrder(band.filter((b) => kinds.get(b) === kind));
|
|
818
|
+
if (!group.length) continue;
|
|
819
|
+
if (revealing !== was) return;
|
|
820
|
+
tellStep(step);
|
|
821
|
+
// No outline ahead of the boxes (Chris 05/11: a box dragged over a box already there makes no sense): each one comes into
|
|
822
|
+
// being as it is dragged out. The expert for this step at it, two trades beside it; the next block starts once this
|
|
823
|
+
// one's box is out, one to three hands by how many there are.
|
|
824
|
+
onStage(area, who(TAKERS[kind][0]));
|
|
825
|
+
const lanes = Math.min(TAKERS[kind].length, group.length >= 6 ? 3 : group.length >= 3 ? 2 : 1);
|
|
826
|
+
const going = new Set<Promise<void>>();
|
|
827
|
+
for (const b of group) {
|
|
828
|
+
if (revealing !== was) return;
|
|
829
|
+
while (going.size >= lanes) await Promise.race(going);
|
|
830
|
+
let out = () => {};
|
|
831
|
+
const boxOut = new Promise<void>((done) => { out = done; });
|
|
832
|
+
const job: Promise<void> = buildBlock(b, kind, own.get(b) ?? "", quick, () => out()).finally(() => { going.delete(job); out(); });
|
|
833
|
+
going.add(job);
|
|
834
|
+
await boxOut;
|
|
835
|
+
}
|
|
836
|
+
await Promise.all(going);
|
|
837
|
+
}
|
|
838
|
+
}
|
|
839
|
+
if (revealing !== was) return;
|
|
840
|
+
// Built: QA sweeps it.
|
|
841
|
+
sweep(inView(box(e)));
|
|
842
|
+
await wait(360 / hurry);
|
|
843
|
+
}
|
|
844
|
+
if (revealing !== was) return;
|
|
845
|
+
for (const { e, opacity, clip } of was) { e.style.opacity = opacity; e.style.clipPath = clip; }
|
|
846
|
+
revealing = null;
|
|
847
|
+
// More of the turn to draw: the crew stays on it.
|
|
848
|
+
if (queue.length) return;
|
|
849
|
+
stopTicker();
|
|
850
|
+
hideSkip();
|
|
851
|
+
leave();
|
|
852
|
+
}
|
|
853
|
+
/** A section's blocks by the screenful, from the top: a section taller than the screen is built a part at a time. */
|
|
854
|
+
function bandsOf(blocks: HTMLElement[]): HTMLElement[][] {
|
|
855
|
+
const tall = Math.min(win.innerHeight, 1000) * 0.85, bands: HTMLElement[][] = [];
|
|
856
|
+
let from = -Infinity;
|
|
857
|
+
for (const { b, top } of blocks.map((x) => ({ b: x, top: x.getBoundingClientRect().top })).sort((p, q) => p.top - q.top)) {
|
|
858
|
+
if (top - from > tall) { bands.push([]); from = top; }
|
|
859
|
+
bands[bands.length - 1]!.push(b);
|
|
860
|
+
}
|
|
861
|
+
return bands;
|
|
862
|
+
}
|
|
863
|
+
function bandBox(band: HTMLElement[]): Box {
|
|
864
|
+
const boxes = band.map((b) => box(b)), x = Math.min(...boxes.map((r) => r.x)), y = Math.min(...boxes.map((r) => r.y));
|
|
865
|
+
return { x, y, w: Math.max(...boxes.map((r) => r.x + r.w)) - x, h: Math.max(...boxes.map((r) => r.y + r.h)) - y };
|
|
866
|
+
}
|
|
867
|
+
function hand(kind: BlockKind): Member | undefined {
|
|
868
|
+
const free = arrive().filter((x) => !onBlock.has(x));
|
|
869
|
+
for (const role of TAKERS[kind]) {
|
|
870
|
+
const m = free.find((x) => x.role === role);
|
|
871
|
+
if (m) { wake(m); return m; }
|
|
872
|
+
}
|
|
873
|
+
return undefined;
|
|
874
|
+
}
|
|
875
|
+
/** One block of the saved page, as the board draws it: the expert goes to its top-left corner, holds the mouse down and drags its
|
|
876
|
+
* box out to the bottom-right corner, its size shown once it is out; then its words wipe in from the left behind a caret, or,
|
|
877
|
+
* for a picture, the box fills with the board's tint as it is dragged and the picture comes up through it (Chris 05/11: "giữ
|
|
878
|
+
* kéo chuột thành hình vuông rồi hiện ảnh"). */
|
|
879
|
+
async function buildBlock(b: HTMLElement, kind: BlockKind, own: string, hurry: number, boxOut: () => void) {
|
|
880
|
+
const bx = box(b);
|
|
881
|
+
const picture = kind === "picture", words = (b.textContent ?? "").trim().length;
|
|
882
|
+
const worker = hand(kind);
|
|
883
|
+
if (worker) { onBlock.add(worker); say(worker, worker.role === TAKERS[kind][0] ? VERB[kind] : undefined); }
|
|
884
|
+
try {
|
|
885
|
+
// To the corner.
|
|
886
|
+
if (worker) travel(worker, bx.x, bx.y, 320 / hurry);
|
|
887
|
+
await wait(340 / hurry);
|
|
888
|
+
// Mouse down, and drag the box out with it.
|
|
889
|
+
const drag = Math.max(500, Math.min(900, 300 + (bx.w + bx.h) * 0.35)) / hurry;
|
|
890
|
+
if (worker) {
|
|
891
|
+
// Pressed for the whole drag, let go at the end.
|
|
892
|
+
worker.el.querySelector("svg")?.animate([{ transform: "scale(1)" }, { transform: "scale(.82)", offset: 0.12 }, { transform: "scale(.82)", offset: 0.9 }, { transform: "scale(1)" }], { duration: (drag + 160) * pace, easing: "linear" });
|
|
893
|
+
travel(worker, bx.x + bx.w - 2, bx.y + bx.h - 2, drag);
|
|
894
|
+
}
|
|
895
|
+
const sel = piece("gb-gaw__sel", bx);
|
|
896
|
+
sel.innerHTML = "<i></i><i></i><i></i><i></i>";
|
|
897
|
+
const grow = [{ clipPath: "inset(-5px 100% 100% -5px)" }, { clipPath: "inset(-5px -5px -5px -5px)" }];
|
|
898
|
+
const tint = picture ? piece("gb-gaw__veil", bx) : null;
|
|
899
|
+
move(sel, grow, drag);
|
|
900
|
+
if (tint) move(tint, [{ clipPath: "inset(0 100% 100% 0)" }, { clipPath: "inset(0 0 0 0)" }], drag);
|
|
901
|
+
await wait(drag);
|
|
902
|
+
boxOut();
|
|
903
|
+
// Mouse up: its size.
|
|
904
|
+
const dim = doc.createElement("span");
|
|
905
|
+
dim.className = "gb-gaw__dim";
|
|
906
|
+
dim.textContent = `${Math.round(bx.w)} × ${Math.round(bx.h)}`;
|
|
907
|
+
sel.append(dim);
|
|
908
|
+
move(dim, [{ opacity: "0" }, { opacity: "1" }], 140);
|
|
909
|
+
b.style.opacity = own;
|
|
910
|
+
if (picture) {
|
|
911
|
+
// The tint held a moment, then the picture comes up through it.
|
|
912
|
+
if (typeof b.animate === "function") b.animate([{ opacity: 0 }, { opacity: 0, offset: 0.45 }, { opacity: 1 }], { duration: 1100 / hurry, easing: EASE });
|
|
913
|
+
await wait(500 / hurry);
|
|
914
|
+
gone(tint ?? undefined, 600 / hurry);
|
|
915
|
+
await wait(700 / hurry);
|
|
916
|
+
} else if (kind === "heading" || kind === "words") {
|
|
917
|
+
const wipe = Math.max(500, Math.min(1300, words * 14)) / hurry;
|
|
918
|
+
if (typeof b.animate === "function") b.animate([{ clipPath: "inset(-4px 100% -4px -4px)" }, { clipPath: "inset(-4px -4px -4px -4px)" }], { duration: wipe, easing: "linear" });
|
|
919
|
+
const caret = piece("gb-gaw__caret", { x: bx.x, y: bx.y, w: 2, h: Math.min(bx.h, 60) });
|
|
920
|
+
move(caret, [{ transform: "translateX(0)" }, { transform: `translateX(${bx.w}px)` }], wipe);
|
|
921
|
+
if (worker) { worker.anim?.cancel(); travel(worker, bx.x + bx.w + 4, bx.y + Math.min(bx.h, 60) * 0.6, wipe); }
|
|
922
|
+
await wait(wipe);
|
|
923
|
+
caret.remove();
|
|
924
|
+
} else if (kind === "action") {
|
|
925
|
+
// A button: it arrives whole.
|
|
926
|
+
if (typeof b.animate === "function") b.animate([{ opacity: 0, transform: "scale(.94)" }, { opacity: 1, transform: "scale(1)" }], { duration: 420 / hurry, easing: EASE });
|
|
927
|
+
await wait(420 / hurry);
|
|
928
|
+
} else {
|
|
929
|
+
// The structure: its fill comes up in the box.
|
|
930
|
+
if (typeof b.animate === "function") b.animate([{ opacity: 0 }, { opacity: 1 }], { duration: 420 / hurry, easing: EASE });
|
|
931
|
+
await wait(420 / hurry);
|
|
932
|
+
}
|
|
933
|
+
gone(sel, 260);
|
|
934
|
+
await wait(160 / hurry);
|
|
935
|
+
} finally {
|
|
936
|
+
// Its block done: it stays a moment for the next one, then goes.
|
|
937
|
+
if (worker) { onBlock.delete(worker); later(() => { if (!onBlock.has(worker) && worker !== atWork && !beside.includes(worker)) rest(worker); }, 1200); }
|
|
938
|
+
}
|
|
939
|
+
}
|
|
940
|
+
|
|
941
|
+
function push(raw: unknown) {
|
|
942
|
+
if (mode === "off" || !isActivity(raw)) return;
|
|
943
|
+
// Skipped: the rest of this turn is not drawn; its landing ends the skip.
|
|
944
|
+
if (skipped) { if (raw.kind === "land") skipped = false; return; }
|
|
945
|
+
const t = now();
|
|
946
|
+
if (lastArrival && t - lastArrival > 1500) quietAvg = quietAvg ? quietAvg * 0.7 + (t - lastArrival) * 0.3 : t - lastArrival;
|
|
947
|
+
lastArrival = t;
|
|
948
|
+
if (!calm) { play(raw); return; }
|
|
949
|
+
queue.push({ at: now(), a: raw });
|
|
950
|
+
void drain();
|
|
951
|
+
}
|
|
952
|
+
/** The calm clock: one thing at a time, in the order it arrived, each held to be seen; hurried in proportion when it trails. */
|
|
953
|
+
async function drain() {
|
|
954
|
+
if (draining) return;
|
|
955
|
+
draining = true;
|
|
956
|
+
const run = epoch;
|
|
957
|
+
while (queue.length && run === epoch) {
|
|
958
|
+
const item = queue.shift()!;
|
|
959
|
+
// Long quiet spells let it trail further (a burst spread over the quiet after it), never past LAG_MAX_MS.
|
|
960
|
+
const hurry = Math.max(1, (now() - item.at) / Math.min(LAG_MAX_MS, Math.max(LAG_MS, quietAvg * 1.5)));
|
|
961
|
+
if (!item.a) { await showSaved(item.reveal, hurry); item.done?.(); continue; }
|
|
962
|
+
let last = item.a;
|
|
963
|
+
if (last.kind === "draft" && last.text !== undefined) last = await typeOut(last, hurry, run);
|
|
964
|
+
else play(last);
|
|
965
|
+
const hold = last.kind === "draft" && last.partial ? 0 : HOLD_MS[last.kind];
|
|
966
|
+
if (hold && pace && run === epoch) await wait(hold / hurry);
|
|
967
|
+
}
|
|
968
|
+
if (run !== epoch) return;
|
|
969
|
+
draining = false;
|
|
970
|
+
for (const done of waiting.splice(0)) done();
|
|
971
|
+
}
|
|
972
|
+
/** Words at reading pace: from what the field shows to the newest words Gem has sent for it, later drafts of the same field
|
|
973
|
+
* folded in as they arrive. Never past what Gem sent. */
|
|
974
|
+
async function typeOut(first: GemActivity, hurry: number, run: number): Promise<GemActivity> {
|
|
975
|
+
let cur = first;
|
|
976
|
+
const fold = () => { for (let next = queue[0]?.a; next && sameField(next, cur) && next.text?.startsWith(cur.text ?? ""); next = queue[0]?.a) { cur = next; queue.shift(); } };
|
|
977
|
+
fold();
|
|
978
|
+
const was = typed.get(fieldOf(cur)) ?? "";
|
|
979
|
+
let n = cur.text!.startsWith(was) ? was.length : 0;
|
|
980
|
+
const step = pace ? Math.max(1, Math.round((TYPE_CPS * hurry * TICK_MS) / 1000)) : Infinity;
|
|
981
|
+
for (;;) {
|
|
982
|
+
const text = cur.text!;
|
|
983
|
+
n = Math.min(text.length, n + step);
|
|
984
|
+
typed.set(fieldOf(cur), text.slice(0, n));
|
|
985
|
+
play({ ...cur, text: text.slice(0, n), partial: n < text.length || cur.partial });
|
|
986
|
+
if (n >= text.length) { fold(); if (n >= cur.text!.length) return cur; }
|
|
987
|
+
await wait(TICK_MS);
|
|
988
|
+
if (run !== epoch) return cur;
|
|
989
|
+
fold();
|
|
990
|
+
}
|
|
991
|
+
}
|
|
992
|
+
/** Gem's tag: the step it is on; quiet for a while, the step's real seconds ("Generating a picture · 12s"). */
|
|
993
|
+
function tellStep(text: string) {
|
|
994
|
+
step = text;
|
|
995
|
+
const lead = crew.find((m) => m.role === "lead");
|
|
996
|
+
if (lead) say(lead, text);
|
|
997
|
+
startTicker();
|
|
998
|
+
}
|
|
999
|
+
/** A beat a second while the turn is alive: the crew's walk, and a quiet step's seconds on Gem's tag. */
|
|
1000
|
+
function startTicker() {
|
|
1001
|
+
if (ticker || !crew.length) return;
|
|
1002
|
+
tickerFrom = now();
|
|
1003
|
+
ticker = win.setInterval(() => {
|
|
1004
|
+
const lead = crew.find((m) => m.role === "lead"), quiet = now() - lastArrival;
|
|
1005
|
+
if (!lead) { win.clearInterval(ticker); ticker = 0; return; }
|
|
1006
|
+
if (!queue.length && !draining && quiet > QUIET_MS && step) say(lead, `${step} · ${Math.round(quiet / 1000)}s`);
|
|
1007
|
+
if (draining || revealing || quiet < STROLL_MS) {
|
|
1008
|
+
strollNext();
|
|
1009
|
+
// A sweep every few seconds of a turn: sooner while Gem is quiet (a reveal sweeps each section as it is built).
|
|
1010
|
+
if (!revealing && now() - Math.max(lastScan, tickerFrom) > (quiet > QUIET_MS ? SWEEP_QUIET_MS : SWEEP_MS)) { const v = view(); sweep(stage && doc.documentElement.scrollHeight <= win.innerHeight + 2 ? inView(stage) : inView({ x: v.x, y: v.y, w: v.w, h: v.h })); }
|
|
1011
|
+
}
|
|
1012
|
+
}, 1000);
|
|
1013
|
+
}
|
|
1014
|
+
function stopTicker() { if (ticker) win.clearInterval(ticker); ticker = 0; }
|
|
1015
|
+
/** The corner button while anything is drawn (`full` mode): the page as it is, and nothing more this turn. */
|
|
1016
|
+
function showSkip() {
|
|
1017
|
+
if (skipButton || mode !== "full" || options.skipLabel === false) return;
|
|
1018
|
+
// Drawn at its full height on a board (the Canvas): its corner is the page's foot, out of sight; the host has its own button.
|
|
1019
|
+
if (options.onLook && doc.documentElement.scrollHeight <= win.innerHeight + 2) return;
|
|
1020
|
+
const b = doc.createElement("button");
|
|
1021
|
+
b.type = "button";
|
|
1022
|
+
b.className = "gb-gaw-skip";
|
|
1023
|
+
b.textContent = options.skipLabel ?? "Skip";
|
|
1024
|
+
b.addEventListener("click", skipTurn);
|
|
1025
|
+
root.append(b);
|
|
1026
|
+
skipButton = b;
|
|
1027
|
+
}
|
|
1028
|
+
function hideSkip() { skipButton?.remove(); skipButton = null; }
|
|
1029
|
+
function skipTurn() { skipped = true; skip(); options.onSkip?.(); }
|
|
1030
|
+
|
|
1031
|
+
function play(a: GemActivity) {
|
|
1032
|
+
try {
|
|
1033
|
+
cut();
|
|
1034
|
+
if (a.kind !== "land") showSkip();
|
|
1035
|
+
switch (a.kind) {
|
|
1036
|
+
case "think": case "plan": {
|
|
1037
|
+
const lead = who("lead"), v = view();
|
|
1038
|
+
// Thinking with nothing on the page yet: the crew where the person is looking; with work under way, it stays by the work.
|
|
1039
|
+
onStage(marks.size && stage ? stage : { x: v.x + v.w * 0.2, y: v.y + Math.max(60, topBar() + 28) + 70, w: v.w * 0.6, h: Math.min(240, v.h * 0.35) }, lead);
|
|
1040
|
+
tellStep(a.label); options.onStatus?.(a.label); break;
|
|
1041
|
+
}
|
|
1042
|
+
case "focus": focus(a); options.onStatus?.(a.label); break;
|
|
1043
|
+
case "draft": draft(a); tellStep(a.text !== undefined ? "writing" : step || "working"); break;
|
|
1044
|
+
case "apply": settle(a, "is-applied"); break;
|
|
1045
|
+
case "check": scan(); tellStep(a.label); options.onStatus?.(a.label); break;
|
|
1046
|
+
case "fail": settle(a, "is-failed"); options.onStatus?.(a.text ?? a.label); break;
|
|
1047
|
+
case "land": land(a); break;
|
|
1048
|
+
}
|
|
1049
|
+
} catch {
|
|
1050
|
+
// The bottom of the ladder: the page as it is.
|
|
1051
|
+
options.onFallback?.("page", a);
|
|
1052
|
+
skip();
|
|
1053
|
+
}
|
|
1054
|
+
}
|
|
1055
|
+
|
|
1056
|
+
function skip() {
|
|
1057
|
+
for (const id of timers) win.clearTimeout(id);
|
|
1058
|
+
timers.clear();
|
|
1059
|
+
stopTicker();
|
|
1060
|
+
hideSkip();
|
|
1061
|
+
// The calm clock stops where it is: nothing queued is drawn, and whoever waits on it is let go.
|
|
1062
|
+
epoch += 1;
|
|
1063
|
+
draining = false;
|
|
1064
|
+
typed.clear();
|
|
1065
|
+
const left = queue;
|
|
1066
|
+
queue = [];
|
|
1067
|
+
for (const item of left) item.done?.();
|
|
1068
|
+
for (const done of waiting.splice(0)) done();
|
|
1069
|
+
for (const m of marks.values()) putBack(m);
|
|
1070
|
+
if (revealing) { for (const { e, opacity, clip } of revealing) { for (const an of e.getAnimations()) an.cancel(); e.style.opacity = opacity; e.style.clipPath = clip; } revealing = null; }
|
|
1071
|
+
lastStack = null;
|
|
1072
|
+
created = { label: "", seq: created.seq };
|
|
1073
|
+
unroom();
|
|
1074
|
+
if (layer) { for (const el of [layer, ...layer.querySelectorAll<HTMLElement>("*")]) for (const anim of el.getAnimations?.() ?? []) anim.cancel(); layer.replaceChildren(); }
|
|
1075
|
+
for (const m of marks.values()) m.room?.remove();
|
|
1076
|
+
marks.clear();
|
|
1077
|
+
crew = [];
|
|
1078
|
+
stage = null; stageKey = ""; beside = []; riding = null;
|
|
1079
|
+
}
|
|
1080
|
+
|
|
1081
|
+
function catchUp(list: readonly unknown[]) {
|
|
1082
|
+
if (mode === "off") return;
|
|
1083
|
+
// The turn so far replaces whatever this frame drew of it: told twice, it is drawn once.
|
|
1084
|
+
skip();
|
|
1085
|
+
instant = true;
|
|
1086
|
+
try {
|
|
1087
|
+
for (const raw of list) {
|
|
1088
|
+
if (!isActivity(raw) || raw.kind === "land") continue;
|
|
1089
|
+
play(raw);
|
|
1090
|
+
if (raw.kind === "draft" && raw.text !== undefined) typed.set(fieldOf(raw), raw.text);
|
|
1091
|
+
}
|
|
1092
|
+
} finally { instant = false; }
|
|
1093
|
+
}
|
|
1094
|
+
|
|
1095
|
+
function reveal(sections?: readonly string[]): Promise<void> {
|
|
1096
|
+
if (mode === "off" || skipped) return Promise.resolve();
|
|
1097
|
+
if (!calm) return showSaved(sections, 1);
|
|
1098
|
+
return new Promise<void>((done) => { queue.push({ at: now(), reveal: sections, done }); void drain(); });
|
|
1099
|
+
}
|
|
1100
|
+
|
|
1101
|
+
return {
|
|
1102
|
+
push,
|
|
1103
|
+
skip,
|
|
1104
|
+
reveal,
|
|
1105
|
+
catchUp,
|
|
1106
|
+
skipTurn,
|
|
1107
|
+
idle: () => (draining || queue.length ? new Promise<void>((done) => waiting.push(done)) : Promise.resolve()),
|
|
1108
|
+
destroy() {
|
|
1109
|
+
skip();
|
|
1110
|
+
for (const type of ["wheel", "touchmove", "keydown"]) win.removeEventListener(type, onHands);
|
|
1111
|
+
win.removeEventListener("scroll", onScroll);
|
|
1112
|
+
if (frame) win.cancelAnimationFrame(frame);
|
|
1113
|
+
frame = 0;
|
|
1114
|
+
layer?.remove();
|
|
1115
|
+
layer = null;
|
|
1116
|
+
},
|
|
1117
|
+
get busy() { return marks.size > 0 || crew.length > 0 || draining || queue.length > 0; },
|
|
1118
|
+
};
|
|
1119
|
+
}
|