@link-foundation/ui-animation 0.0.0-stage → 0.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +24 -0
- package/README.md +454 -2
- package/package.json +77 -4
- package/src/backgrounds/README.md +131 -0
- package/src/backgrounds/beams.d.ts +21 -0
- package/src/backgrounds/beams.js +140 -0
- package/src/backgrounds/common.d.ts +23 -0
- package/src/backgrounds/common.js +322 -0
- package/src/backgrounds/grid.d.ts +16 -0
- package/src/backgrounds/grid.js +122 -0
- package/src/backgrounds/nebula.d.ts +9 -0
- package/src/backgrounds/nebula.js +57 -0
- package/src/backgrounds/rain-cleanup.js +77 -0
- package/src/backgrounds/rain.d.ts +33 -0
- package/src/backgrounds/rain.js +238 -0
- package/src/backgrounds/shader.d.ts +14 -0
- package/src/backgrounds/shader.js +151 -0
- package/src/backgrounds/sprites.js +72 -0
- package/src/backgrounds/starfield.d.ts +38 -0
- package/src/backgrounds/starfield.js +168 -0
- package/src/clock.js +98 -0
- package/src/compile-waapi.js +147 -0
- package/src/diagnostics.js +51 -0
- package/src/easing.js +327 -0
- package/src/effects/decipher.js +83 -0
- package/src/effects.js +44 -0
- package/src/index.d.ts +581 -0
- package/src/index.js +74 -0
- package/src/layers.js +88 -0
- package/src/model.js +636 -0
- package/src/player.js +267 -0
- package/src/random.js +62 -0
- package/src/recorder/buffer.js +40 -0
- package/src/recorder/clock.js +2 -0
- package/src/recorder/format.js +125 -0
- package/src/recorder/input.js +53 -0
- package/src/recorder/recorder.js +183 -0
- package/src/recorder/redact.js +43 -0
- package/src/recorder/replay.js +115 -0
- package/src/recorder/state-at.js +62 -0
- package/src/recorder/store.js +92 -0
- package/src/recorder/to-playwright.js +113 -0
- package/src/recorder/verify.js +49 -0
- package/src/run.js +283 -0
- package/src/sample.js +160 -0
- package/src/timeline.js +115 -0
- package/src/timing.js +172 -0
- package/src/tokens.js +32 -0
- package/src/types.js +130 -0
- package/src/validate.js +218 -0
- package/src/values.js +331 -0
package/src/model.js
ADDED
|
@@ -0,0 +1,636 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `loadAnimations(text)`: the animation model of Links Notation text.
|
|
3
|
+
*
|
|
4
|
+
* Text is read only by ui-core `parse` (the official `links-notation`
|
|
5
|
+
* parser); this module reads the projected links of every `animation` and
|
|
6
|
+
* `timeline` element, and the `(on STATE (play ID))` triggers of any element.
|
|
7
|
+
*
|
|
8
|
+
* ```
|
|
9
|
+
* (NAME: animation (target ID) (duration 300ms) (delay 0ms)
|
|
10
|
+
* (iterations 1|infinite) (direction normal|reverse|alternate|alternate-reverse)
|
|
11
|
+
* (fill none|forwards|backwards|both) (composite replace|add|accumulate)
|
|
12
|
+
* (easing NAME|(cubic-bezier a b c d)|(steps n)|(spring (stiffness k) (damping c) (mass m)))
|
|
13
|
+
* (track PROPERTY (key OFFSET VALUE [EASING])...)
|
|
14
|
+
* (effect NAME (seed 42) (PARAM VALUE)...) (persist true) (envelope bell|sustain)
|
|
15
|
+
* (on hover|focus|pressed|disabled [(play ID [(target ID)])])
|
|
16
|
+
* (reduced-motion PART...))
|
|
17
|
+
* (NAME: timeline (at TIME ITEM...) ITEM...)
|
|
18
|
+
* ITEM = (play ID [(target ID)] [(targets ID...)]) | (sequence ITEM...)
|
|
19
|
+
* | (parallel ITEM...) | (stagger STEP ITEM... [(targets ID...)])
|
|
20
|
+
* ```
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
import { parse, walk, values as coreValues } from '@link-foundation/ui-core';
|
|
24
|
+
import { animationDiagnostic } from './diagnostics.js';
|
|
25
|
+
import { isEasingName } from './easing.js';
|
|
26
|
+
import { readValue } from './values.js';
|
|
27
|
+
import { getEffect } from './effects.js';
|
|
28
|
+
|
|
29
|
+
const { parseDuration, parseNumber } = coreValues;
|
|
30
|
+
|
|
31
|
+
/** The element states that can trigger an animation. */
|
|
32
|
+
export const ANIMATION_STATES = Object.freeze([
|
|
33
|
+
'hover',
|
|
34
|
+
'focus',
|
|
35
|
+
'pressed',
|
|
36
|
+
'disabled',
|
|
37
|
+
]);
|
|
38
|
+
|
|
39
|
+
const DIRECTIONS = ['normal', 'reverse', 'alternate', 'alternate-reverse'];
|
|
40
|
+
const FILLS = ['none', 'forwards', 'backwards', 'both', 'auto'];
|
|
41
|
+
const COMPOSITES = ['replace', 'add', 'accumulate'];
|
|
42
|
+
const ENVELOPES = ['bell', 'sustain'];
|
|
43
|
+
const STEP_POSITIONS = {
|
|
44
|
+
start: 'start',
|
|
45
|
+
end: 'end',
|
|
46
|
+
'jump-start': 'start',
|
|
47
|
+
'jump-end': 'end',
|
|
48
|
+
'jump-both': 'both',
|
|
49
|
+
'jump-none': 'none',
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
/** Defaults of every animation field (WAAPI defaults, `fill none`). */
|
|
53
|
+
export const ANIMATION_DEFAULTS = Object.freeze({
|
|
54
|
+
target: null,
|
|
55
|
+
duration: null,
|
|
56
|
+
delay: 0,
|
|
57
|
+
iterations: 1,
|
|
58
|
+
direction: 'normal',
|
|
59
|
+
fill: 'none',
|
|
60
|
+
composite: 'replace',
|
|
61
|
+
easing: null,
|
|
62
|
+
tracks: [],
|
|
63
|
+
effect: null,
|
|
64
|
+
persist: false,
|
|
65
|
+
envelope: null,
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
/** The ui-core diagnostics this reader restates for its own elements. */
|
|
69
|
+
const RESTATED = new Set(['unknown-sublink', 'unknown-kind']);
|
|
70
|
+
|
|
71
|
+
const isAtom = (raw) => typeof raw === 'string';
|
|
72
|
+
const isLink = (raw) => Array.isArray(raw);
|
|
73
|
+
const show = (raw) =>
|
|
74
|
+
isLink(raw) ? `(${raw.map(show).join(' ')})` : String(raw);
|
|
75
|
+
|
|
76
|
+
/** Every sublink of an element as `[name, ...values]`, declared or not. */
|
|
77
|
+
function sublinks(node) {
|
|
78
|
+
return [
|
|
79
|
+
...node.properties.map((p) => [p.name, ...p.values]),
|
|
80
|
+
...node.unknown.filter(isLink),
|
|
81
|
+
];
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* A reader of one element: collects diagnostics that point at it.
|
|
86
|
+
*/
|
|
87
|
+
class Reader {
|
|
88
|
+
constructor(element, kind, diagnostics) {
|
|
89
|
+
this.element = element;
|
|
90
|
+
this.kind = kind;
|
|
91
|
+
this.diagnostics = diagnostics;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
report(code, severity, at = {}) {
|
|
95
|
+
this.diagnostics.push(
|
|
96
|
+
animationDiagnostic(code, severity, { element: this.element, ...at })
|
|
97
|
+
);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
invalid(property, raw, expected) {
|
|
101
|
+
this.report('invalid-value', 'error', {
|
|
102
|
+
property,
|
|
103
|
+
text: show(raw),
|
|
104
|
+
expected,
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/** A time in ms: `300ms`, `0.3s`, or a `(token PATH)` reference. */
|
|
109
|
+
time(property, raw, { allowNegative = false } = {}) {
|
|
110
|
+
if (isLink(raw) && raw[0] === 'token' && isAtom(raw[1])) {
|
|
111
|
+
return { type: 'token', path: raw[1] };
|
|
112
|
+
}
|
|
113
|
+
const negative = allowNegative && isAtom(raw) && raw.startsWith('-');
|
|
114
|
+
const text = negative ? raw.slice(1) : raw;
|
|
115
|
+
const quantity = isAtom(text) ? parseDuration(text) : null;
|
|
116
|
+
if (quantity === null) {
|
|
117
|
+
this.invalid(property, raw, 'a duration such as 300ms or 0.3s');
|
|
118
|
+
return undefined;
|
|
119
|
+
}
|
|
120
|
+
const ms = quantity.unit === 's' ? quantity.value * 1000 : quantity.value;
|
|
121
|
+
return negative ? -ms : ms;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
oneOf(property, raw, choices) {
|
|
125
|
+
if (isAtom(raw) && choices.includes(raw)) {
|
|
126
|
+
return raw;
|
|
127
|
+
}
|
|
128
|
+
this.invalid(property, raw, `one of ${choices.join(', ')}`);
|
|
129
|
+
return undefined;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
number(property, raw, { min = -Infinity } = {}) {
|
|
133
|
+
const n = isAtom(raw) ? parseNumber(raw) : null;
|
|
134
|
+
if (n === null || n < min) {
|
|
135
|
+
this.invalid(property, raw, `a number >= ${min}`);
|
|
136
|
+
return undefined;
|
|
137
|
+
}
|
|
138
|
+
return n;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/** An easing: a name or a `cubic-bezier`, `steps` or `spring` link. */
|
|
142
|
+
easing(raw) {
|
|
143
|
+
if (isAtom(raw)) {
|
|
144
|
+
if (!isEasingName(raw)) {
|
|
145
|
+
this.report('unknown-easing', 'warning', { text: raw });
|
|
146
|
+
}
|
|
147
|
+
return raw === 'linear'
|
|
148
|
+
? { type: 'linear' }
|
|
149
|
+
: { type: 'named', name: raw };
|
|
150
|
+
}
|
|
151
|
+
const [head, ...args] = isLink(raw) ? raw : [];
|
|
152
|
+
if (head === 'cubic-bezier') {
|
|
153
|
+
return this.bezier(raw, args);
|
|
154
|
+
}
|
|
155
|
+
if (head === 'steps') {
|
|
156
|
+
return this.steps(raw, args);
|
|
157
|
+
}
|
|
158
|
+
if (head === 'spring') {
|
|
159
|
+
return this.spring(raw, args);
|
|
160
|
+
}
|
|
161
|
+
this.report('unknown-easing', 'error', { text: show(raw) });
|
|
162
|
+
return undefined;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
bezier(raw, args) {
|
|
166
|
+
const points = args.map((a) => (isAtom(a) ? parseNumber(a) : null));
|
|
167
|
+
const xOk = (x) => x !== null && x >= 0 && x <= 1;
|
|
168
|
+
if (
|
|
169
|
+
points.length !== 4 ||
|
|
170
|
+
points.includes(null) ||
|
|
171
|
+
!xOk(points[0]) ||
|
|
172
|
+
!xOk(points[2])
|
|
173
|
+
) {
|
|
174
|
+
this.invalid('easing', raw, '(cubic-bezier x1 y1 x2 y2), x in 0..1');
|
|
175
|
+
return undefined;
|
|
176
|
+
}
|
|
177
|
+
return { type: 'cubic-bezier', points };
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
steps(raw, [count, position = 'end', ...rest]) {
|
|
181
|
+
const n = isAtom(count) ? parseNumber(count) : null;
|
|
182
|
+
const where = isAtom(position) ? STEP_POSITIONS[position] : undefined;
|
|
183
|
+
if (
|
|
184
|
+
n === null ||
|
|
185
|
+
!Number.isInteger(n) ||
|
|
186
|
+
n < 1 ||
|
|
187
|
+
where === undefined ||
|
|
188
|
+
rest.length > 0 ||
|
|
189
|
+
(where === 'none' && n < 2)
|
|
190
|
+
) {
|
|
191
|
+
this.invalid('easing', raw, '(steps N [start|end|jump-both|jump-none])');
|
|
192
|
+
return undefined;
|
|
193
|
+
}
|
|
194
|
+
return { type: 'steps', count: n, position: where };
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
spring(raw, args) {
|
|
198
|
+
const spring = { type: 'spring', stiffness: 100, damping: 10, mass: 1 };
|
|
199
|
+
for (const arg of args) {
|
|
200
|
+
const [name, value] = isLink(arg) ? arg : [];
|
|
201
|
+
if (!['stiffness', 'damping', 'mass'].includes(name)) {
|
|
202
|
+
this.invalid(
|
|
203
|
+
'easing',
|
|
204
|
+
raw,
|
|
205
|
+
'(spring (stiffness k) (damping c) (mass m))'
|
|
206
|
+
);
|
|
207
|
+
return undefined;
|
|
208
|
+
}
|
|
209
|
+
const n = this.number(`spring ${name}`, value, { min: 0 });
|
|
210
|
+
if (n === undefined) {
|
|
211
|
+
return undefined;
|
|
212
|
+
}
|
|
213
|
+
spring[name] = n;
|
|
214
|
+
}
|
|
215
|
+
if (!(spring.stiffness > 0 && spring.mass > 0 && spring.damping > 0)) {
|
|
216
|
+
this.invalid(
|
|
217
|
+
'easing',
|
|
218
|
+
raw,
|
|
219
|
+
'a spring with positive stiffness, damping and mass'
|
|
220
|
+
);
|
|
221
|
+
return undefined;
|
|
222
|
+
}
|
|
223
|
+
return spring;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/** `(track PROPERTY (key OFFSET VALUE [EASING])...)` */
|
|
227
|
+
track(raw) {
|
|
228
|
+
const [property, ...keys] = raw;
|
|
229
|
+
if (!isAtom(property)) {
|
|
230
|
+
this.invalid('track', raw, '(track PROPERTY (key OFFSET VALUE)...)');
|
|
231
|
+
return undefined;
|
|
232
|
+
}
|
|
233
|
+
const track = { property, keys: [] };
|
|
234
|
+
let last = -Infinity;
|
|
235
|
+
for (const key of keys) {
|
|
236
|
+
const parsed = this.key(property, key);
|
|
237
|
+
if (parsed === undefined) {
|
|
238
|
+
continue;
|
|
239
|
+
}
|
|
240
|
+
if (parsed.offset < last) {
|
|
241
|
+
this.report('key-offset-order', 'error', {
|
|
242
|
+
track: property,
|
|
243
|
+
text: show(key[1]),
|
|
244
|
+
});
|
|
245
|
+
}
|
|
246
|
+
last = Math.max(last, parsed.offset);
|
|
247
|
+
track.keys.push(parsed);
|
|
248
|
+
}
|
|
249
|
+
if (keys.length === 0) {
|
|
250
|
+
this.report('empty-track', 'error', { track: property });
|
|
251
|
+
}
|
|
252
|
+
return track;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
key(property, raw) {
|
|
256
|
+
if (!isLink(raw) || raw[0] !== 'key' || raw.length < 3 || raw.length > 4) {
|
|
257
|
+
this.invalid(`track ${property}`, raw, '(key OFFSET VALUE [EASING])');
|
|
258
|
+
return undefined;
|
|
259
|
+
}
|
|
260
|
+
const [, offsetRaw, valueRaw, easingRaw] = raw;
|
|
261
|
+
const offset = isAtom(offsetRaw) ? parseNumber(offsetRaw) : null;
|
|
262
|
+
if (offset === null || offset < 0 || offset > 1) {
|
|
263
|
+
this.report('key-offset-range', 'error', {
|
|
264
|
+
track: property,
|
|
265
|
+
text: show(offsetRaw),
|
|
266
|
+
});
|
|
267
|
+
return undefined;
|
|
268
|
+
}
|
|
269
|
+
const value = readValue(valueRaw);
|
|
270
|
+
if (value === null) {
|
|
271
|
+
this.invalid(
|
|
272
|
+
`track ${property}`,
|
|
273
|
+
valueRaw,
|
|
274
|
+
'a number, colour, text or (token PATH)'
|
|
275
|
+
);
|
|
276
|
+
return undefined;
|
|
277
|
+
}
|
|
278
|
+
const key = { offset, value, easing: null };
|
|
279
|
+
if (easingRaw !== undefined) {
|
|
280
|
+
key.easing = this.easing(easingRaw) ?? null;
|
|
281
|
+
}
|
|
282
|
+
return key;
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
/** `(effect NAME (PARAM VALUE)...)` */
|
|
286
|
+
effect(raw) {
|
|
287
|
+
const [name, ...params] = raw;
|
|
288
|
+
if (!isAtom(name)) {
|
|
289
|
+
this.invalid('effect', raw, '(effect NAME (PARAM VALUE)...)');
|
|
290
|
+
return undefined;
|
|
291
|
+
}
|
|
292
|
+
if (getEffect(name) === undefined) {
|
|
293
|
+
this.report('unknown-effect', 'warning', { text: name });
|
|
294
|
+
}
|
|
295
|
+
const effect = { name, params: {} };
|
|
296
|
+
for (const param of params) {
|
|
297
|
+
const [key, value] = isLink(param) ? param : [];
|
|
298
|
+
if (!isAtom(key) || !isAtom(value) || param.length !== 2) {
|
|
299
|
+
this.invalid('effect', param, '(PARAM VALUE)');
|
|
300
|
+
continue;
|
|
301
|
+
}
|
|
302
|
+
effect.params[key] = parseNumber(value) ?? value;
|
|
303
|
+
}
|
|
304
|
+
return effect;
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
/** `(envelope bell|sustain [(attack 100ms)] [(release 100ms)])` */
|
|
308
|
+
envelope(raw) {
|
|
309
|
+
const [type, ...parts] = raw;
|
|
310
|
+
if (this.oneOf('envelope', type, ENVELOPES) === undefined) {
|
|
311
|
+
return undefined;
|
|
312
|
+
}
|
|
313
|
+
const envelope = { type };
|
|
314
|
+
for (const part of parts) {
|
|
315
|
+
const [name, value] = isLink(part) ? part : [];
|
|
316
|
+
if (type !== 'sustain' || !['attack', 'release'].includes(name)) {
|
|
317
|
+
this.invalid(
|
|
318
|
+
'envelope',
|
|
319
|
+
part,
|
|
320
|
+
'(attack TIME) or (release TIME) of a sustain envelope'
|
|
321
|
+
);
|
|
322
|
+
continue;
|
|
323
|
+
}
|
|
324
|
+
const ms = this.time(`envelope ${name}`, value);
|
|
325
|
+
if (typeof ms === 'number') {
|
|
326
|
+
envelope[name] = ms;
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
return envelope;
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
/** `(on STATE [(play ID [(target ID)])])`; `null` when STATE is not a state. */
|
|
333
|
+
trigger(raw, { strict }) {
|
|
334
|
+
const [state, play] = raw;
|
|
335
|
+
if (!ANIMATION_STATES.includes(state)) {
|
|
336
|
+
if (strict) {
|
|
337
|
+
this.report('unknown-state', 'warning', { text: show(state) });
|
|
338
|
+
}
|
|
339
|
+
return null;
|
|
340
|
+
}
|
|
341
|
+
if (play === undefined) {
|
|
342
|
+
return { state, animation: null, target: null };
|
|
343
|
+
}
|
|
344
|
+
const [head, id, ...rest] = isLink(play) ? play : [];
|
|
345
|
+
if (head !== 'play' || !isAtom(id)) {
|
|
346
|
+
if (strict) {
|
|
347
|
+
this.invalid('on', play, '(play ID [(target ID)])');
|
|
348
|
+
}
|
|
349
|
+
return null;
|
|
350
|
+
}
|
|
351
|
+
const target = rest.find((r) => isLink(r) && r[0] === 'target')?.[1];
|
|
352
|
+
return { state, animation: id, target: isAtom(target) ? target : null };
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
const FIELD_READERS = {
|
|
357
|
+
target: (r, [v]) => (isAtom(v) ? v : r.invalid('target', v, 'an element id')),
|
|
358
|
+
duration: (r, [v]) => r.time('duration', v),
|
|
359
|
+
delay: (r, [v]) => r.time('delay', v, { allowNegative: true }),
|
|
360
|
+
iterations: (r, [v]) =>
|
|
361
|
+
v === 'infinite' ? Infinity : r.number('iterations', v, { min: 0 }),
|
|
362
|
+
direction: (r, [v]) => r.oneOf('direction', v, DIRECTIONS),
|
|
363
|
+
fill: (r, [v]) => r.oneOf('fill', v, FILLS),
|
|
364
|
+
composite: (r, [v]) => r.oneOf('composite', v, COMPOSITES),
|
|
365
|
+
easing: (r, [v]) => r.easing(v),
|
|
366
|
+
effect: (r, values) => r.effect(values),
|
|
367
|
+
persist: (r, [v]) =>
|
|
368
|
+
v === 'true'
|
|
369
|
+
? true
|
|
370
|
+
: v === 'false'
|
|
371
|
+
? false
|
|
372
|
+
: r.invalid('persist', v, 'true or false'),
|
|
373
|
+
envelope: (r, values) => r.envelope(values),
|
|
374
|
+
};
|
|
375
|
+
|
|
376
|
+
/**
|
|
377
|
+
* The fields of an animation (or of its reduced-motion block): only the
|
|
378
|
+
* fields that are written are set.
|
|
379
|
+
*/
|
|
380
|
+
function readFields(reader, links, { nested }) {
|
|
381
|
+
const fields = {};
|
|
382
|
+
const triggers = [];
|
|
383
|
+
let reduced = null;
|
|
384
|
+
for (const [name, ...values] of links) {
|
|
385
|
+
if (name === 'track') {
|
|
386
|
+
const track = reader.track(values);
|
|
387
|
+
if (track) {
|
|
388
|
+
(fields.tracks ??= []).push(track);
|
|
389
|
+
}
|
|
390
|
+
} else if (Object.hasOwn(FIELD_READERS, name)) {
|
|
391
|
+
const value = FIELD_READERS[name](reader, values);
|
|
392
|
+
if (value !== undefined) {
|
|
393
|
+
fields[name] = value;
|
|
394
|
+
}
|
|
395
|
+
} else if (name === 'on' && !nested) {
|
|
396
|
+
const trigger = reader.trigger(values, { strict: true });
|
|
397
|
+
if (trigger) {
|
|
398
|
+
triggers.push(trigger);
|
|
399
|
+
}
|
|
400
|
+
} else if (name === 'reduced-motion' && !nested) {
|
|
401
|
+
reduced = readFields(reader, values.filter(isLink), {
|
|
402
|
+
nested: true,
|
|
403
|
+
}).fields;
|
|
404
|
+
} else {
|
|
405
|
+
reader.report('unknown-sublink', 'warning', {
|
|
406
|
+
kind: nested ? 'reduced-motion block' : 'animation',
|
|
407
|
+
text: show(name),
|
|
408
|
+
});
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
return { fields, triggers, reduced };
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
function readAnimation(node, diagnostics) {
|
|
415
|
+
const reader = new Reader(node.id, 'animation', diagnostics);
|
|
416
|
+
const { fields, triggers, reduced } = readFields(reader, sublinks(node), {
|
|
417
|
+
nested: false,
|
|
418
|
+
});
|
|
419
|
+
const animation = {
|
|
420
|
+
id: node.id,
|
|
421
|
+
kind: 'animation',
|
|
422
|
+
...ANIMATION_DEFAULTS,
|
|
423
|
+
tracks: [],
|
|
424
|
+
...fields,
|
|
425
|
+
triggers,
|
|
426
|
+
reducedMotion: reduced,
|
|
427
|
+
};
|
|
428
|
+
return animation;
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
/** One timeline item, or `undefined` (reported) when it is invalid. */
|
|
432
|
+
function readItem(reader, raw) {
|
|
433
|
+
const [head, ...args] = isLink(raw) ? raw : [];
|
|
434
|
+
const items = () =>
|
|
435
|
+
args
|
|
436
|
+
.filter((a) => !(isLink(a) && a[0] === 'targets'))
|
|
437
|
+
.map((a) => readItem(reader, a))
|
|
438
|
+
.filter(Boolean);
|
|
439
|
+
const targets = () => {
|
|
440
|
+
const link = args.find((a) => isLink(a) && a[0] === 'targets');
|
|
441
|
+
return link ? link.slice(1).filter(isAtom) : null;
|
|
442
|
+
};
|
|
443
|
+
switch (head) {
|
|
444
|
+
case 'play': {
|
|
445
|
+
const [id, ...rest] = args;
|
|
446
|
+
if (!isAtom(id)) {
|
|
447
|
+
break;
|
|
448
|
+
}
|
|
449
|
+
const target = rest.find((r) => isLink(r) && r[0] === 'target')?.[1];
|
|
450
|
+
return {
|
|
451
|
+
type: 'play',
|
|
452
|
+
id,
|
|
453
|
+
target: isAtom(target) ? target : null,
|
|
454
|
+
targets: targets(),
|
|
455
|
+
};
|
|
456
|
+
}
|
|
457
|
+
case 'sequence':
|
|
458
|
+
case 'parallel':
|
|
459
|
+
return { type: head, items: items() };
|
|
460
|
+
case 'at':
|
|
461
|
+
case 'stagger': {
|
|
462
|
+
const time = reader.time(head, args.shift(), {
|
|
463
|
+
allowNegative: head === 'at',
|
|
464
|
+
});
|
|
465
|
+
if (time === undefined) {
|
|
466
|
+
return undefined;
|
|
467
|
+
}
|
|
468
|
+
const item = { type: head, items: items() };
|
|
469
|
+
item[head === 'at' ? 'time' : 'step'] = time;
|
|
470
|
+
if (head === 'stagger') {
|
|
471
|
+
item.targets = targets();
|
|
472
|
+
}
|
|
473
|
+
return item;
|
|
474
|
+
}
|
|
475
|
+
default:
|
|
476
|
+
break;
|
|
477
|
+
}
|
|
478
|
+
reader.invalid(
|
|
479
|
+
'timeline item',
|
|
480
|
+
raw,
|
|
481
|
+
'(at TIME ITEM...), (play ID), (sequence ...), (parallel ...) or (stagger STEP ...)'
|
|
482
|
+
);
|
|
483
|
+
return undefined;
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
function readTimeline(node, diagnostics) {
|
|
487
|
+
const reader = new Reader(node.id, 'timeline', diagnostics);
|
|
488
|
+
const items = sublinks(node)
|
|
489
|
+
.map((link) => readItem(reader, link))
|
|
490
|
+
.filter(Boolean);
|
|
491
|
+
return { id: node.id, kind: 'timeline', items };
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
/** Every `(play ID)` of a timeline item tree. */
|
|
495
|
+
function playsOf(items, out = []) {
|
|
496
|
+
for (const item of items) {
|
|
497
|
+
if (item.type === 'play') {
|
|
498
|
+
out.push(item.id);
|
|
499
|
+
} else {
|
|
500
|
+
playsOf(item.items, out);
|
|
501
|
+
}
|
|
502
|
+
}
|
|
503
|
+
return out;
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
/** Report unknown `play` targets and cycles between timelines. */
|
|
507
|
+
function checkReferences(model, diagnostics) {
|
|
508
|
+
const known = (id) => model.animations.has(id) || model.timelines.has(id);
|
|
509
|
+
for (const timeline of model.timelines.values()) {
|
|
510
|
+
for (const id of playsOf(timeline.items)) {
|
|
511
|
+
if (!known(id)) {
|
|
512
|
+
diagnostics.push(
|
|
513
|
+
animationDiagnostic('unknown-animation', 'error', {
|
|
514
|
+
element: timeline.id,
|
|
515
|
+
property: 'timeline',
|
|
516
|
+
text: id,
|
|
517
|
+
})
|
|
518
|
+
);
|
|
519
|
+
}
|
|
520
|
+
}
|
|
521
|
+
const cycle = findCycle(model, timeline.id);
|
|
522
|
+
if (cycle) {
|
|
523
|
+
diagnostics.push(
|
|
524
|
+
animationDiagnostic('timeline-cycle', 'error', {
|
|
525
|
+
element: timeline.id,
|
|
526
|
+
text: cycle.join(' -> '),
|
|
527
|
+
})
|
|
528
|
+
);
|
|
529
|
+
}
|
|
530
|
+
}
|
|
531
|
+
for (const trigger of model.triggers) {
|
|
532
|
+
if (!known(trigger.animation)) {
|
|
533
|
+
diagnostics.push(
|
|
534
|
+
animationDiagnostic('unknown-animation', 'error', {
|
|
535
|
+
element: trigger.element,
|
|
536
|
+
property: `trigger ${trigger.state}`,
|
|
537
|
+
text: trigger.animation,
|
|
538
|
+
})
|
|
539
|
+
);
|
|
540
|
+
}
|
|
541
|
+
}
|
|
542
|
+
}
|
|
543
|
+
|
|
544
|
+
/** The path back to `start` through nested timelines, or `null`. */
|
|
545
|
+
function findCycle(model, start) {
|
|
546
|
+
const visit = (id, path) => {
|
|
547
|
+
const timeline = model.timelines.get(id);
|
|
548
|
+
if (!timeline) {
|
|
549
|
+
return null;
|
|
550
|
+
}
|
|
551
|
+
for (const next of playsOf(timeline.items)) {
|
|
552
|
+
if (next === start) {
|
|
553
|
+
return [...path, next];
|
|
554
|
+
}
|
|
555
|
+
if (!path.includes(next)) {
|
|
556
|
+
const found = visit(next, [...path, next]);
|
|
557
|
+
if (found) {
|
|
558
|
+
return found;
|
|
559
|
+
}
|
|
560
|
+
}
|
|
561
|
+
}
|
|
562
|
+
return null;
|
|
563
|
+
};
|
|
564
|
+
return visit(start, [start]);
|
|
565
|
+
}
|
|
566
|
+
|
|
567
|
+
/**
|
|
568
|
+
* Read the animations, timelines and state triggers of Links Notation text.
|
|
569
|
+
* Never throws: every problem is a diagnostic.
|
|
570
|
+
* @param {string} text
|
|
571
|
+
* @returns {import('./types.js').AnimationModel}
|
|
572
|
+
*/
|
|
573
|
+
export function loadAnimations(text) {
|
|
574
|
+
const document = parse(text, { checks: false });
|
|
575
|
+
const own = new Set();
|
|
576
|
+
const diagnostics = [];
|
|
577
|
+
const model = {
|
|
578
|
+
animations: new Map(),
|
|
579
|
+
timelines: new Map(),
|
|
580
|
+
triggers: [],
|
|
581
|
+
diagnostics,
|
|
582
|
+
};
|
|
583
|
+
walk(document, (node) => {
|
|
584
|
+
if (node.kind === 'animation' || node.kind === 'timeline') {
|
|
585
|
+
own.add(node.id);
|
|
586
|
+
if (node.id === null) {
|
|
587
|
+
diagnostics.push(
|
|
588
|
+
animationDiagnostic('anonymous-animation', 'warning', {})
|
|
589
|
+
);
|
|
590
|
+
return false;
|
|
591
|
+
}
|
|
592
|
+
if (node.kind === 'animation') {
|
|
593
|
+
const animation = readAnimation(node, diagnostics);
|
|
594
|
+
model.animations.set(node.id, animation);
|
|
595
|
+
for (const t of animation.triggers) {
|
|
596
|
+
// `(on STATE ...)` of an animation fires on the state of its
|
|
597
|
+
// target and plays there unless the play names another target.
|
|
598
|
+
model.triggers.push({
|
|
599
|
+
state: t.state,
|
|
600
|
+
element: animation.target ?? node.id,
|
|
601
|
+
animation: t.animation ?? node.id,
|
|
602
|
+
target: t.target ?? animation.target,
|
|
603
|
+
});
|
|
604
|
+
}
|
|
605
|
+
} else {
|
|
606
|
+
model.timelines.set(node.id, readTimeline(node, diagnostics));
|
|
607
|
+
}
|
|
608
|
+
return false;
|
|
609
|
+
}
|
|
610
|
+
readElementTriggers(node, model, diagnostics);
|
|
611
|
+
return undefined;
|
|
612
|
+
});
|
|
613
|
+
const restated = (d) => RESTATED.has(d.code) && own.has(d.at?.element);
|
|
614
|
+
diagnostics.unshift(...document.diagnostics.filter((d) => !restated(d)));
|
|
615
|
+
checkReferences(model, diagnostics);
|
|
616
|
+
return model;
|
|
617
|
+
}
|
|
618
|
+
|
|
619
|
+
/** `(on STATE (play ID [(target ID)]))` on any other element. */
|
|
620
|
+
function readElementTriggers(node, model, diagnostics) {
|
|
621
|
+
const reader = new Reader(node.id, node.kind, diagnostics);
|
|
622
|
+
for (const property of node.properties) {
|
|
623
|
+
if (property.name !== 'on') {
|
|
624
|
+
continue;
|
|
625
|
+
}
|
|
626
|
+
const trigger = reader.trigger(property.values, { strict: false });
|
|
627
|
+
if (trigger && trigger.animation) {
|
|
628
|
+
model.triggers.push({
|
|
629
|
+
state: trigger.state,
|
|
630
|
+
element: node.id,
|
|
631
|
+
animation: trigger.animation,
|
|
632
|
+
target: trigger.target ?? node.id,
|
|
633
|
+
});
|
|
634
|
+
}
|
|
635
|
+
}
|
|
636
|
+
}
|