@zeyos/zx 5.5.0 → 5.6.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 +56 -0
- package/dist/types/components/agent-thread/agent-thread.d.ts +583 -0
- package/dist/types/components/approval-card/approval-card.d.ts +596 -0
- package/dist/types/components/avatar/avatar.d.ts +91 -4
- package/dist/types/components/block-view/block-view.d.ts +205 -0
- package/dist/types/components/composer/composer.d.ts +736 -0
- package/dist/types/components/run-status/run-status.d.ts +393 -0
- package/dist/types/components/run-trace/run-trace.d.ts +624 -0
- package/dist/types/components/source-list/source-list.d.ts +177 -0
- package/dist/types/core/messages.d.ts +135 -0
- package/dist/types/index.d.ts +8 -0
- package/dist/types/internal/agent-model.d.ts +587 -0
- package/dist/types/internal/highlight.d.ts +24 -0
- package/dist/types/internal/presence-loop.d.ts +148 -0
- package/dist/zx-zeyos.esm.js +4648 -4513
- package/dist/zx-zeyos.esm.min.js +2 -2
- package/dist/zx.css +1927 -1
- package/dist/zx.esm.js +15769 -9194
- package/dist/zx.esm.min.js +27 -8
- package/dist/zx.global.js +15769 -9194
- package/dist/zx.global.min.js +27 -8
- package/dist/zx.min.css +1 -1
- package/docs/api.json +1166 -5
- package/docs/llms.md +455 -2
- package/docs/llms.txt +10 -1
- package/package.json +1 -1
- package/src/components/agent-thread/agent-thread.css +417 -0
- package/src/components/agent-thread/agent-thread.js +1365 -0
- package/src/components/approval-card/approval-card.css +289 -0
- package/src/components/approval-card/approval-card.js +1377 -0
- package/src/components/avatar/avatar.css +56 -1
- package/src/components/avatar/avatar.js +148 -26
- package/src/components/block-view/block-view.css +300 -0
- package/src/components/block-view/block-view.js +523 -0
- package/src/components/composer/composer.css +403 -0
- package/src/components/composer/composer.js +1441 -0
- package/src/components/run-status/run-status.css +208 -0
- package/src/components/run-status/run-status.js +782 -0
- package/src/components/run-trace/run-trace.css +443 -0
- package/src/components/run-trace/run-trace.js +1209 -0
- package/src/components/source-list/source-list.css +208 -0
- package/src/components/source-list/source-list.js +355 -0
- package/src/core/messages.js +143 -0
- package/src/index.js +8 -0
- package/src/internal/agent-model.js +488 -0
- package/src/internal/highlight.js +104 -0
- package/src/internal/presence-loop.js +343 -0
- package/styles/zx.css +7 -0
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
/**
|
|
3
|
+
* A small syntax tokenizer for code an agent writes into an answer. It recognises comments,
|
|
4
|
+
* strings, numbers, and keywords — enough to make code readable, never enough to be an editor —
|
|
5
|
+
* and returns segments, never markup, so the caller builds text nodes and spans from them.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
/** @typedef {'comment'|'string'|'number'|'keyword'} TokenKind */
|
|
9
|
+
/** @typedef {{text: string, token: TokenKind|null}} CodeSegment */
|
|
10
|
+
|
|
11
|
+
/** A string in single, double, or back quotes; unterminated while it is still being streamed. */
|
|
12
|
+
const STRING = /'(?:[^'\\\n]|\\.)*'?|"(?:[^"\\\n]|\\.)*"?|`(?:[^`\\]|\\.)*`?/;
|
|
13
|
+
/** A number, digit separators included: `30_000`, `0.5`, `1e3`. */
|
|
14
|
+
const NUMBER = /\b\d[\d_]*(?:\.\d+)?(?:e[+-]?\d+)?\b/;
|
|
15
|
+
/** A name or a keyword. */
|
|
16
|
+
const WORD = /[A-Za-z_$][\w$]*/;
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* What the tokenizer recognises in one language. Comments, strings, numbers, and words are
|
|
20
|
+
* matched in that order from left to right, so a quote inside a comment or a digit inside a name
|
|
21
|
+
* never starts a token of its own.
|
|
22
|
+
* @param {RegExp} comment Line or block comment.
|
|
23
|
+
* @param {string} keywords Space-separated, lower case for caseless languages.
|
|
24
|
+
* @param {{caseless?: boolean, string?: RegExp}} [options]
|
|
25
|
+
* @returns {{pattern: RegExp, keywords: Set<string>, caseless: boolean}}
|
|
26
|
+
*/
|
|
27
|
+
function grammar(comment, keywords, options = {}) {
|
|
28
|
+
const parts = [['comment', comment], ['string', options.string ?? STRING], ['number', NUMBER], ['word', WORD]];
|
|
29
|
+
return {
|
|
30
|
+
pattern: new RegExp(parts.map(([name, part]) => `(?<${name}>${/** @type {RegExp} */ (part).source})`).join('|'), 'g'),
|
|
31
|
+
keywords: new Set(keywords.split(' ')),
|
|
32
|
+
caseless: Boolean(options.caseless)
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const SCRIPT_KEYWORDS = 'async await break case catch class const continue debugger default delete do else export '
|
|
37
|
+
+ 'extends false finally for from function if import in instanceof let new null of return static super switch '
|
|
38
|
+
+ 'this throw true try typeof undefined var void while yield';
|
|
39
|
+
|
|
40
|
+
const GRAMMARS = Object.freeze({
|
|
41
|
+
javascript: grammar(/\/\/[^\n]*|\/\*[\s\S]*?(?:\*\/|$)/, SCRIPT_KEYWORDS),
|
|
42
|
+
typescript: grammar(/\/\/[^\n]*|\/\*[\s\S]*?(?:\*\/|$)/, `${SCRIPT_KEYWORDS} abstract as declare enum implements interface keyof `
|
|
43
|
+
+ 'namespace private protected public readonly type'),
|
|
44
|
+
json: grammar(/(?!)/, 'true false null', { string: /"(?:[^"\\\n]|\\.)*"?/ }),
|
|
45
|
+
sql: grammar(/--[^\n]*|\/\*[\s\S]*?(?:\*\/|$)/, 'and as asc between by case count delete desc distinct else end exists from '
|
|
46
|
+
+ 'group having in inner insert interval into is join left limit not null on or order outer right select set sum '
|
|
47
|
+
+ 'then union update values when where with', { caseless: true, string: /'(?:[^'\n]|'')*'?/ }),
|
|
48
|
+
python: grammar(/#[^\n]*/, 'and as assert async await break class continue def del elif else except false finally for '
|
|
49
|
+
+ 'from global if import in is lambda none nonlocal not or pass raise return true try while with yield', { caseless: false }),
|
|
50
|
+
shell: grammar(/(?<!\S)#[^\n]*/, 'case do done elif else esac export fi for function if in local read return then until while'),
|
|
51
|
+
css: grammar(/\/\*[\s\S]*?(?:\*\/|$)/, 'important inherit initial none unset auto')
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
/** Language names an answer may use, mapped to the grammar that reads them. */
|
|
55
|
+
const ALIASES = Object.freeze({
|
|
56
|
+
js: 'javascript', javascript: 'javascript', mjs: 'javascript', jsx: 'javascript',
|
|
57
|
+
ts: 'typescript', typescript: 'typescript', tsx: 'typescript',
|
|
58
|
+
json: 'json',
|
|
59
|
+
sql: 'sql',
|
|
60
|
+
py: 'python', python: 'python',
|
|
61
|
+
sh: 'shell', bash: 'shell', shell: 'shell', zsh: 'shell',
|
|
62
|
+
css: 'css'
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Whether code in this language is coloured.
|
|
67
|
+
* @param {unknown} language
|
|
68
|
+
* @returns {boolean}
|
|
69
|
+
*/
|
|
70
|
+
export function isHighlighted(language) {
|
|
71
|
+
return Object.hasOwn(ALIASES, String(language ?? '').toLowerCase());
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Splits code into plain and coloured segments. An unknown language is one plain segment.
|
|
76
|
+
* Python's `True`/`None` and SQL's upper-case keywords are matched without regard to case.
|
|
77
|
+
* @param {unknown} language
|
|
78
|
+
* @param {unknown} source
|
|
79
|
+
* @returns {CodeSegment[]}
|
|
80
|
+
*/
|
|
81
|
+
export function highlight(language, source) {
|
|
82
|
+
const code = source == null ? '' : String(source);
|
|
83
|
+
const name = ALIASES[/** @type {keyof typeof ALIASES} */ (String(language ?? '').toLowerCase())];
|
|
84
|
+
const syntax = name ? GRAMMARS[/** @type {keyof typeof GRAMMARS} */ (name)] : null;
|
|
85
|
+
if (!syntax) return code ? [{ text: code, token: null }] : [];
|
|
86
|
+
const caseless = syntax.caseless || name === 'python';
|
|
87
|
+
/** @type {CodeSegment[]} */
|
|
88
|
+
const out = [];
|
|
89
|
+
let plain = 0;
|
|
90
|
+
for (const match of code.matchAll(syntax.pattern)) {
|
|
91
|
+
if (match[0] === '') continue;
|
|
92
|
+
const groups = /** @type {Record<string, string|undefined>} */ (match.groups);
|
|
93
|
+
/** @type {TokenKind|null} */
|
|
94
|
+
const token = groups.comment ? 'comment' : groups.string ? 'string' : groups.number ? 'number'
|
|
95
|
+
: groups.word && syntax.keywords.has(caseless ? groups.word.toLowerCase() : groups.word) ? 'keyword' : null;
|
|
96
|
+
if (!token) continue;
|
|
97
|
+
const start = Number(match.index);
|
|
98
|
+
if (start > plain) out.push({ text: code.slice(plain, start), token: null });
|
|
99
|
+
out.push({ text: match[0], token });
|
|
100
|
+
plain = start + match[0].length;
|
|
101
|
+
}
|
|
102
|
+
if (plain < code.length) out.push({ text: code.slice(plain), token: null });
|
|
103
|
+
return out;
|
|
104
|
+
}
|
|
@@ -0,0 +1,343 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
/**
|
|
3
|
+
* The one animation loop every moving agent avatar on a page shares.
|
|
4
|
+
*
|
|
5
|
+
* An avatar joins with `trackPresence(element, activity)` and leaves with `untrack()`. The loop
|
|
6
|
+
* lives on the avatar's document under a symbol-keyed property — never in module scope — and is
|
|
7
|
+
* created with the first tracked avatar and torn down with the last. It moves only avatars on
|
|
8
|
+
* screen (an `IntersectionObserver`), eases from one pose to the next when an activity changes,
|
|
9
|
+
* clamps its frame step so a long frame never makes an avatar jump, and sleeps while the page is
|
|
10
|
+
* hidden or nothing is moving. Under reduced motion — the system's, or a `data-zx-motion="reduced"`
|
|
11
|
+
* region around an avatar, followed through `watchMotion()` — every avatar returns to rest and the
|
|
12
|
+
* loop stops. It writes `transform` and nothing else.
|
|
13
|
+
*
|
|
14
|
+
* Motion is never the only signal: an avatar's activity is also its dot and its word.
|
|
15
|
+
*/
|
|
16
|
+
import { prefersReducedMotion, watchMotion } from '../core/motion.js';
|
|
17
|
+
|
|
18
|
+
/** @typedef {import('./agent-model.js').AgentActivity} AgentActivity */
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Where an avatar wants to be at one moment: a vertical offset in pixels, a rotation in degrees, a
|
|
22
|
+
* scale, how much a blink may squash it (`w`, eased like the rest), and the blink's depth now.
|
|
23
|
+
* @typedef {{y: number, r: number, s: number, w: number, blink: number}} PresencePose
|
|
24
|
+
*/
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* @typedef {Object} PresenceHandle
|
|
28
|
+
* @property {(activity: AgentActivity|string|null, options?: TrackOptions) => void} set Changes
|
|
29
|
+
* what the avatar is doing, and its phase when `options.phase` is given; the loop eases it into
|
|
30
|
+
* the new pose.
|
|
31
|
+
* @property {() => void} untrack Leaves the loop and puts the avatar back at rest. Safe to call
|
|
32
|
+
* more than once.
|
|
33
|
+
*/
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* @typedef {Object} TrackOptions
|
|
37
|
+
* @property {number} [phase=0] Where in its cycle this avatar moves, as a fraction of the cycle
|
|
38
|
+
* from 0 up to 1 (whole turns wrap; anything that is not a finite number is 0). Avatars in the same
|
|
39
|
+
* activity at different phases never move in step: 0.5 is half a cycle apart, whatever the
|
|
40
|
+
* activity. `presencePhase(key)` derives one from a stable key.
|
|
41
|
+
*/
|
|
42
|
+
|
|
43
|
+
/** The longest step one frame may take, so a frame after a stall eases instead of jumping. */
|
|
44
|
+
export const MAX_FRAME_MS = 50;
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* How long one cycle of each moving activity takes, in seconds. Needs-you counts its blink beat;
|
|
48
|
+
* its lean runs on a shorter period of its own.
|
|
49
|
+
*/
|
|
50
|
+
const PERIODS = Object.freeze({ idle: 4.8, thinking: 3.4, working: 1.1, 'needs-you': 3.6 });
|
|
51
|
+
|
|
52
|
+
/** How quickly a pose follows its target: the fraction left after one second is e^-8. */
|
|
53
|
+
const EASE_RATE = 8;
|
|
54
|
+
|
|
55
|
+
/** The pose at rest. */
|
|
56
|
+
const REST = Object.freeze({ y: 0, r: 0, s: 1, w: 0, blink: 0 });
|
|
57
|
+
|
|
58
|
+
/** Where the shared loop is kept on its document. */
|
|
59
|
+
const LOOP = Symbol('zx.presenceLoop');
|
|
60
|
+
|
|
61
|
+
/** A handle that does nothing, for an environment the loop cannot run in. */
|
|
62
|
+
const INERT = Object.freeze({ set() {}, untrack() {} });
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* The pose of an avatar doing `activity` at time `t`. Pure: the same input always gives the same
|
|
66
|
+
* pose. Idle breathes, thinking sways, working bobs, and needs-you leans and blinks; blocked,
|
|
67
|
+
* offline, and anything unknown stay at rest.
|
|
68
|
+
* @param {AgentActivity|string|null|undefined} activity
|
|
69
|
+
* @param {number} t Seconds.
|
|
70
|
+
* @returns {PresencePose}
|
|
71
|
+
*/
|
|
72
|
+
export function presencePose(activity, t) {
|
|
73
|
+
const time = Number.isFinite(t) ? t : 0;
|
|
74
|
+
/** @param {number} period @returns {number} 0 → 1 → 0 over one period. */
|
|
75
|
+
const cycle = (period) => 0.5 - 0.5 * Math.cos((2 * Math.PI * time) / period);
|
|
76
|
+
switch (activity) {
|
|
77
|
+
case 'idle': return { y: 0, r: 0, s: 1 + 0.035 * cycle(4.8), w: 0, blink: 0 };
|
|
78
|
+
case 'thinking': return { y: 0, r: 4 * Math.sin((2 * Math.PI * time) / 3.4), s: 1, w: 0, blink: 0 };
|
|
79
|
+
case 'working': return { y: -1.6 * cycle(1.1), r: 0, s: 1, w: 0, blink: 0 };
|
|
80
|
+
case 'needs-you': {
|
|
81
|
+
const beat = ((time % 3.6) + 3.6) % 3.6;
|
|
82
|
+
const blink = beat < 0.18 ? Math.sin((Math.PI * beat) / 0.18) : 0;
|
|
83
|
+
return { y: 0, r: -8 + Math.sin((2 * Math.PI * time) / 2.6), s: 1, w: 1, blink: 0.28 * blink };
|
|
84
|
+
}
|
|
85
|
+
default: return { ...REST };
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* A stable phase for a key, as a fraction of the cycle from 0 up to 1, so two avatars never move in
|
|
91
|
+
* step and the same avatar always moves the same way.
|
|
92
|
+
* @param {unknown} key
|
|
93
|
+
* @returns {number}
|
|
94
|
+
*/
|
|
95
|
+
export function presencePhase(key) {
|
|
96
|
+
let hash = 0;
|
|
97
|
+
for (const char of String(key ?? '')) hash = (hash * 31 + (char.codePointAt(0) ?? 0)) % 997;
|
|
98
|
+
return hash / 997;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Reads a phase: a finite number wrapped into a fraction from 0 up to 1 (`1.25` is `0.25`, `-0.25`
|
|
103
|
+
* is `0.75`), or null for anything else.
|
|
104
|
+
* @param {unknown} phase
|
|
105
|
+
* @returns {number|null}
|
|
106
|
+
*/
|
|
107
|
+
export function normalizePhase(phase) {
|
|
108
|
+
if (typeof phase !== 'number' || !Number.isFinite(phase)) return null;
|
|
109
|
+
const turn = phase % 1;
|
|
110
|
+
return turn < 0 ? turn + 1 : turn;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* How long one cycle of an activity takes, in seconds; 0 for one that stays at rest.
|
|
115
|
+
* @param {AgentActivity|string|null|undefined} activity
|
|
116
|
+
* @returns {number}
|
|
117
|
+
*/
|
|
118
|
+
export function presencePeriod(activity) {
|
|
119
|
+
return typeof activity === 'string' && Object.hasOwn(PERIODS, activity) ? PERIODS[/** @type {keyof typeof PERIODS} */ (activity)] : 0;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* The seconds a phase moves an avatar along its activity's cycle.
|
|
124
|
+
* @param {AgentActivity|string|null|undefined} activity
|
|
125
|
+
* @param {unknown} phase A fraction of the cycle; see `normalizePhase()`.
|
|
126
|
+
* @returns {number}
|
|
127
|
+
*/
|
|
128
|
+
export function presenceOffset(activity, phase) {
|
|
129
|
+
return (normalizePhase(phase) ?? 0) * presencePeriod(activity);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Whether a pose is at rest, to within what a screen can show.
|
|
134
|
+
* @param {{y: number, r: number, s: number, w: number}} pose
|
|
135
|
+
* @returns {boolean}
|
|
136
|
+
*/
|
|
137
|
+
function settled(pose) {
|
|
138
|
+
return Math.abs(pose.y) < 0.01 && Math.abs(pose.r) < 0.01 && Math.abs(pose.s - 1) < 0.0005 && Math.abs(pose.w) < 0.01;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* @typedef {Object} Entry
|
|
143
|
+
* @property {string|null} activity
|
|
144
|
+
* @property {number} phase A fraction of the cycle.
|
|
145
|
+
* @property {{y: number, r: number, s: number, w: number}} pose
|
|
146
|
+
* @property {boolean} visible
|
|
147
|
+
* @property {boolean} reduced
|
|
148
|
+
* @property {boolean} moved Whether the loop has written a transform it has not cleared.
|
|
149
|
+
* @property {() => void} unwatch
|
|
150
|
+
*/
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* @typedef {Object} Loop
|
|
154
|
+
* @property {Document} document
|
|
155
|
+
* @property {Map<HTMLElement, Entry>} entries
|
|
156
|
+
* @property {IntersectionObserver} observer
|
|
157
|
+
* @property {AbortController} abort
|
|
158
|
+
* @property {number} frame
|
|
159
|
+
* @property {number} last
|
|
160
|
+
* @property {number} time
|
|
161
|
+
*/
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* Adds an element to its document's presence loop. The element's `transform` belongs to the loop
|
|
165
|
+
* while it is tracked; `untrack()` clears it. Where `requestAnimationFrame` or
|
|
166
|
+
* `IntersectionObserver` is missing the handle does nothing.
|
|
167
|
+
* @param {HTMLElement} element What moves: the avatar's root.
|
|
168
|
+
* @param {AgentActivity|string|null} activity What the avatar is doing.
|
|
169
|
+
* @param {TrackOptions} [options={}]
|
|
170
|
+
* @returns {PresenceHandle}
|
|
171
|
+
*/
|
|
172
|
+
export function trackPresence(element, activity, options = {}) {
|
|
173
|
+
const doc = element?.ownerDocument;
|
|
174
|
+
if (!doc || typeof globalThis.requestAnimationFrame !== 'function'
|
|
175
|
+
|| typeof globalThis.IntersectionObserver !== 'function') return INERT;
|
|
176
|
+
const loop = loopOf(doc);
|
|
177
|
+
const existing = loop.entries.get(element);
|
|
178
|
+
if (existing) {
|
|
179
|
+
existing.activity = activity ?? null;
|
|
180
|
+
if (options && Object.hasOwn(options, 'phase')) existing.phase = normalizePhase(options.phase) ?? 0;
|
|
181
|
+
schedule(loop);
|
|
182
|
+
return handleFor(loop, element);
|
|
183
|
+
}
|
|
184
|
+
/** @type {Entry} */
|
|
185
|
+
const entry = {
|
|
186
|
+
activity: activity ?? null,
|
|
187
|
+
phase: normalizePhase(options?.phase) ?? 0,
|
|
188
|
+
pose: { y: 0, r: 0, s: 1, w: 0 },
|
|
189
|
+
visible: false,
|
|
190
|
+
reduced: prefersReducedMotion(element),
|
|
191
|
+
moved: false,
|
|
192
|
+
unwatch: () => {}
|
|
193
|
+
};
|
|
194
|
+
loop.entries.set(element, entry);
|
|
195
|
+
entry.unwatch = watchMotion(element, (reduced) => {
|
|
196
|
+
entry.reduced = reduced;
|
|
197
|
+
if (reduced) rest(element, entry);
|
|
198
|
+
else schedule(loop);
|
|
199
|
+
});
|
|
200
|
+
loop.observer.observe(element);
|
|
201
|
+
return handleFor(loop, element);
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* @param {Loop} loop
|
|
206
|
+
* @param {HTMLElement} element
|
|
207
|
+
* @returns {PresenceHandle}
|
|
208
|
+
*/
|
|
209
|
+
function handleFor(loop, element) {
|
|
210
|
+
return {
|
|
211
|
+
set(activity, options) {
|
|
212
|
+
const entry = loop.entries.get(element);
|
|
213
|
+
if (!entry) return;
|
|
214
|
+
entry.activity = activity ?? null;
|
|
215
|
+
if (options && Object.hasOwn(options, 'phase')) entry.phase = normalizePhase(options.phase) ?? 0;
|
|
216
|
+
schedule(loop);
|
|
217
|
+
},
|
|
218
|
+
untrack() {
|
|
219
|
+
const entry = loop.entries.get(element);
|
|
220
|
+
if (!entry) return;
|
|
221
|
+
loop.entries.delete(element);
|
|
222
|
+
entry.unwatch();
|
|
223
|
+
loop.observer.unobserve(element);
|
|
224
|
+
rest(element, entry);
|
|
225
|
+
if (loop.entries.size === 0) teardown(loop);
|
|
226
|
+
}
|
|
227
|
+
};
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/**
|
|
231
|
+
* The document's loop, created on first use.
|
|
232
|
+
* @param {Document} doc
|
|
233
|
+
* @returns {Loop}
|
|
234
|
+
*/
|
|
235
|
+
function loopOf(doc) {
|
|
236
|
+
const host = /** @type {Document & {[LOOP]?: Loop}} */ (doc);
|
|
237
|
+
const current = host[LOOP];
|
|
238
|
+
if (current) return current;
|
|
239
|
+
/** @type {Loop} */
|
|
240
|
+
const loop = {
|
|
241
|
+
document: doc,
|
|
242
|
+
entries: new Map(),
|
|
243
|
+
observer: new IntersectionObserver((records) => {
|
|
244
|
+
for (const record of records) {
|
|
245
|
+
const entry = loop.entries.get(/** @type {HTMLElement} */ (record.target));
|
|
246
|
+
if (!entry) continue;
|
|
247
|
+
entry.visible = record.isIntersecting;
|
|
248
|
+
// An avatar built before it was attached could not see the region it landed in.
|
|
249
|
+
if (entry.visible) entry.reduced = prefersReducedMotion(record.target);
|
|
250
|
+
if (entry.reduced) rest(/** @type {HTMLElement} */ (record.target), entry);
|
|
251
|
+
}
|
|
252
|
+
schedule(loop);
|
|
253
|
+
}),
|
|
254
|
+
abort: new AbortController(),
|
|
255
|
+
frame: 0,
|
|
256
|
+
last: 0,
|
|
257
|
+
time: 0
|
|
258
|
+
};
|
|
259
|
+
doc.addEventListener('visibilitychange', () => {
|
|
260
|
+
if (doc.hidden) cancel(loop);
|
|
261
|
+
else schedule(loop);
|
|
262
|
+
}, { signal: loop.abort.signal });
|
|
263
|
+
host[LOOP] = loop;
|
|
264
|
+
return loop;
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
/** @param {Loop} loop @returns {void} */
|
|
268
|
+
function teardown(loop) {
|
|
269
|
+
cancel(loop);
|
|
270
|
+
loop.observer.disconnect();
|
|
271
|
+
loop.abort.abort();
|
|
272
|
+
const host = /** @type {Document & {[LOOP]?: Loop}} */ (loop.document);
|
|
273
|
+
if (host[LOOP] === loop) delete host[LOOP];
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
/** @param {Loop} loop @returns {boolean} Whether any avatar could move now. */
|
|
277
|
+
function runnable(loop) {
|
|
278
|
+
if (loop.document.hidden) return false;
|
|
279
|
+
for (const entry of loop.entries.values()) {
|
|
280
|
+
if (entry.visible && !entry.reduced) return true;
|
|
281
|
+
}
|
|
282
|
+
return false;
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
/** Starts the loop unless it is running or has nothing to move. @param {Loop} loop @returns {void} */
|
|
286
|
+
function schedule(loop) {
|
|
287
|
+
if (loop.frame || !runnable(loop)) return;
|
|
288
|
+
loop.last = performance.now();
|
|
289
|
+
loop.frame = requestAnimationFrame((now) => step(loop, now));
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
/** @param {Loop} loop @returns {void} */
|
|
293
|
+
function cancel(loop) {
|
|
294
|
+
if (loop.frame) cancelAnimationFrame(loop.frame);
|
|
295
|
+
loop.frame = 0;
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
/**
|
|
299
|
+
* Puts one avatar back at rest at once.
|
|
300
|
+
* @param {HTMLElement} element
|
|
301
|
+
* @param {Entry} entry
|
|
302
|
+
* @returns {void}
|
|
303
|
+
*/
|
|
304
|
+
function rest(element, entry) {
|
|
305
|
+
entry.pose = { y: 0, r: 0, s: 1, w: 0 };
|
|
306
|
+
if (entry.moved) element.style.removeProperty('transform');
|
|
307
|
+
entry.moved = false;
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
/**
|
|
311
|
+
* One frame: every visible avatar eases toward its pose; the loop goes on only while one moves.
|
|
312
|
+
* @param {Loop} loop
|
|
313
|
+
* @param {number} now
|
|
314
|
+
* @returns {void}
|
|
315
|
+
*/
|
|
316
|
+
function step(loop, now) {
|
|
317
|
+
loop.frame = 0;
|
|
318
|
+
if (loop.document.hidden) return;
|
|
319
|
+
const dt = Math.min(Math.max(now - loop.last, 0), MAX_FRAME_MS) / 1000;
|
|
320
|
+
loop.last = now;
|
|
321
|
+
loop.time += dt;
|
|
322
|
+
const ease = 1 - Math.exp(-dt * EASE_RATE);
|
|
323
|
+
let moving = false;
|
|
324
|
+
for (const [element, entry] of loop.entries) {
|
|
325
|
+
if (!entry.visible || entry.reduced) continue;
|
|
326
|
+
const target = presencePose(entry.activity, loop.time + presenceOffset(entry.activity, entry.phase));
|
|
327
|
+
const pose = entry.pose;
|
|
328
|
+
pose.y += (target.y - pose.y) * ease;
|
|
329
|
+
pose.r += (target.r - pose.r) * ease;
|
|
330
|
+
pose.s += (target.s - pose.s) * ease;
|
|
331
|
+
pose.w += (target.w - pose.w) * ease;
|
|
332
|
+
if (settled(target) && settled(pose)) {
|
|
333
|
+
rest(element, entry);
|
|
334
|
+
continue;
|
|
335
|
+
}
|
|
336
|
+
moving = true;
|
|
337
|
+
entry.moved = true;
|
|
338
|
+
const squash = 1 - pose.w * target.blink;
|
|
339
|
+
element.style.transform = `translateY(${pose.y.toFixed(2)}px) rotate(${pose.r.toFixed(2)}deg) `
|
|
340
|
+
+ `scale(${pose.s.toFixed(4)}, ${(pose.s * squash).toFixed(4)})`;
|
|
341
|
+
}
|
|
342
|
+
if (moving) loop.frame = requestAnimationFrame((next) => step(loop, next));
|
|
343
|
+
}
|
package/styles/zx.css
CHANGED
|
@@ -93,4 +93,11 @@
|
|
|
93
93
|
@import url("../src/components/multi-value-editor/multi-value-editor.css");
|
|
94
94
|
@import url("../src/components/field-upload/field-upload.css");
|
|
95
95
|
@import url("../src/components/questionnaire/questionnaire.css");
|
|
96
|
+
@import url("../src/components/block-view/block-view.css");
|
|
97
|
+
@import url("../src/components/run-status/run-status.css");
|
|
98
|
+
@import url("../src/components/composer/composer.css");
|
|
99
|
+
@import url("../src/components/approval-card/approval-card.css");
|
|
100
|
+
@import url("../src/components/run-trace/run-trace.css");
|
|
101
|
+
@import url("../src/components/source-list/source-list.css");
|
|
102
|
+
@import url("../src/components/agent-thread/agent-thread.css");
|
|
96
103
|
@import url("./print.css");
|