@wardic/dom 1.0.0-rc.8 → 1.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/dist/Appear.d.ts +7 -1
- package/dist/Appear.d.ts.map +1 -1
- package/dist/Appear.js +7 -1
- package/dist/Appear.js.map +1 -1
- package/dist/DomBatch.d.ts +20 -0
- package/dist/DomBatch.d.ts.map +1 -0
- package/dist/DomBatch.js +23 -0
- package/dist/DomBatch.js.map +1 -0
- package/dist/DomInput.d.ts +2 -1
- package/dist/DomInput.d.ts.map +1 -1
- package/dist/DomInput.js +3 -1
- package/dist/DomInput.js.map +1 -1
- package/dist/KeyboardPress.d.ts +16 -0
- package/dist/KeyboardPress.d.ts.map +1 -0
- package/dist/KeyboardPress.js +17 -0
- package/dist/KeyboardPress.js.map +1 -0
- package/dist/ReorderController.d.ts +24 -4
- package/dist/ReorderController.d.ts.map +1 -1
- package/dist/ReorderController.js +38 -4
- package/dist/ReorderController.js.map +1 -1
- package/dist/index.d.ts +5 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -1
- package/dist/index.js.map +1 -1
- package/dist/internal/appear-slot.d.ts +23 -0
- package/dist/internal/appear-slot.d.ts.map +1 -0
- package/dist/internal/appear-slot.js +29 -0
- package/dist/internal/appear-slot.js.map +1 -0
- package/dist/internal/appear.d.ts +84 -16
- package/dist/internal/appear.d.ts.map +1 -1
- package/dist/internal/appear.js +302 -61
- package/dist/internal/appear.js.map +1 -1
- package/dist/internal/batch-writer.d.ts +16 -0
- package/dist/internal/batch-writer.d.ts.map +1 -0
- package/dist/internal/batch-writer.js +77 -0
- package/dist/internal/batch-writer.js.map +1 -0
- package/dist/internal/gate.d.ts +3 -2
- package/dist/internal/gate.d.ts.map +1 -1
- package/dist/internal/gate.js +5 -1
- package/dist/internal/gate.js.map +1 -1
- package/dist/internal/input.d.ts +0 -6
- package/dist/internal/input.d.ts.map +1 -1
- package/dist/internal/input.js +205 -23
- package/dist/internal/input.js.map +1 -1
- package/dist/internal/keyboard-press.d.ts +7 -0
- package/dist/internal/keyboard-press.d.ts.map +1 -0
- package/dist/internal/keyboard-press.js +45 -0
- package/dist/internal/keyboard-press.js.map +1 -0
- package/dist/internal/measure.d.ts +7 -0
- package/dist/internal/measure.d.ts.map +1 -1
- package/dist/internal/measure.js +19 -1
- package/dist/internal/measure.js.map +1 -1
- package/dist/internal/projection.d.ts +13 -5
- package/dist/internal/projection.d.ts.map +1 -1
- package/dist/internal/projection.js +271 -33
- package/dist/internal/projection.js.map +1 -1
- package/dist/internal/renderer.d.ts +18 -0
- package/dist/internal/renderer.d.ts.map +1 -1
- package/dist/internal/renderer.js +4 -2
- package/dist/internal/renderer.js.map +1 -1
- package/dist/internal/reorder.d.ts +28 -13
- package/dist/internal/reorder.d.ts.map +1 -1
- package/dist/internal/reorder.js +55 -26
- package/dist/internal/reorder.js.map +1 -1
- package/dist/internal/reveal.d.ts +7 -3
- package/dist/internal/reveal.d.ts.map +1 -1
- package/dist/internal/reveal.js +8 -3
- package/dist/internal/reveal.js.map +1 -1
- package/package.json +4 -4
- package/src/Appear.ts +12 -3
- package/src/DomBatch.ts +22 -0
- package/src/DomInput.ts +2 -1
- package/src/KeyboardPress.ts +16 -0
- package/src/ReorderController.ts +54 -4
- package/src/index.ts +5 -3
- package/src/internal/appear-slot.ts +32 -0
- package/src/internal/appear.ts +462 -67
- package/src/internal/batch-writer.ts +95 -0
- package/src/internal/gate.ts +7 -3
- package/src/internal/input.ts +209 -25
- package/src/internal/keyboard-press.ts +45 -0
- package/src/internal/measure.ts +17 -1
- package/src/internal/projection.ts +272 -38
- package/src/internal/renderer.ts +4 -2
- package/src/internal/reorder.ts +85 -39
- package/src/internal/reveal.ts +10 -4
package/src/internal/appear.ts
CHANGED
|
@@ -9,11 +9,13 @@
|
|
|
9
9
|
* - **A sheet** collects one render's rules. An element's inline style names only a custom
|
|
10
10
|
* property, `animation: var(--wardic-aN)`, which the sheet defines in `:root` once the render
|
|
11
11
|
* knows its timing (decision 5); undefined, it leaves the element at its Starting style.
|
|
12
|
-
* - **The Handoff's reads:** the element's `wardic-…` animation
|
|
13
|
-
*
|
|
12
|
+
* - **The Handoff's reads:** the element's `wardic-…` animation and how far it has got. Its removal,
|
|
13
|
+
* written in the same task as the engine's first frame (decision 6), is `appear-slot.ts`'s, with
|
|
14
|
+
* `pending`: what a page's code asks before any Handoff, kept out of this module.
|
|
14
15
|
*
|
|
15
16
|
* @module
|
|
16
17
|
*/
|
|
18
|
+
import * as Batch from "@wardic/core/Batch"
|
|
17
19
|
import * as BaseValue from "@wardic/core/BaseValue"
|
|
18
20
|
import type * as Frame from "@wardic/core/Frame"
|
|
19
21
|
import * as Machine from "@wardic/core/Machine"
|
|
@@ -21,7 +23,8 @@ import * as Projection from "@wardic/core/Projection"
|
|
|
21
23
|
import type * as Values from "@wardic/core/Values"
|
|
22
24
|
import * as Arr from "effect/Array"
|
|
23
25
|
import * as Bool from "effect/Boolean"
|
|
24
|
-
import type
|
|
26
|
+
import { identity, type LazyArg } from "effect/Function"
|
|
27
|
+
import * as Match from "effect/Match"
|
|
25
28
|
import * as Option from "effect/Option"
|
|
26
29
|
import * as Order from "effect/Order"
|
|
27
30
|
import * as Predicate from "effect/Predicate"
|
|
@@ -29,7 +32,8 @@ import * as Rec from "effect/Record"
|
|
|
29
32
|
import * as Result from "effect/Result"
|
|
30
33
|
import * as UndefinedOr from "effect/UndefinedOr"
|
|
31
34
|
import { spacesRegistered } from "./colors.ts"
|
|
32
|
-
import { drawnUnitOf, project, TEMPLATE_KEY, type TransformTemplate } from "./renderer.ts"
|
|
35
|
+
import { drawnUnitOf, project, TEMPLATE_KEY, TRANSFORM_FUNCS, type TransformTemplate, unitOf } from "./renderer.ts"
|
|
36
|
+
import { PREFIX } from "./appear-slot.ts"
|
|
33
37
|
import { dashCase, isPathKey, isSvgInnerTag, splitPatchForTag, svgAttrNameOf } from "./svg.ts"
|
|
34
38
|
|
|
35
39
|
/** An element's mount animation, as its binding describes it. */
|
|
@@ -97,13 +101,10 @@ export interface Play {
|
|
|
97
101
|
readonly delay: number
|
|
98
102
|
}
|
|
99
103
|
|
|
100
|
-
/** The prefix every Appear rule's name starts with. */
|
|
101
|
-
export const PREFIX = "wardic-"
|
|
102
|
-
|
|
103
104
|
/** The longest mount animation sampled, in seconds; a longer one keeps its Starting style. */
|
|
104
105
|
export const MAX_SECONDS = 60
|
|
105
106
|
|
|
106
|
-
const MAX_STEPS = Math.round(MAX_SECONDS / Machine.CATCH_UP_DT)
|
|
107
|
+
const MAX_STEPS = /*#__PURE__*/ Math.round(MAX_SECONDS / Machine.CATCH_UP_DT)
|
|
107
108
|
|
|
108
109
|
// --- The spec's name ---------------------------------------------------------------------------
|
|
109
110
|
|
|
@@ -120,7 +121,7 @@ const hash53 = (text: string): string => {
|
|
|
120
121
|
|
|
121
122
|
/** Each spec's name by its text, so a page's elements sharing a spec hash it once; the oldest dropped past `NAMES_SIZE`. */
|
|
122
123
|
// Module state (#460, Q7): a cache keyed by what it caches: a bounded set of names.
|
|
123
|
-
const names = new Map<string, string>()
|
|
124
|
+
const names = /*#__PURE__*/ new Map<string, string>()
|
|
124
125
|
|
|
125
126
|
const NAMES_SIZE = 4096
|
|
126
127
|
|
|
@@ -151,15 +152,28 @@ const specTextOf = (spec: Pick<Spec, "key" | "tag" | "infinite" | "projected">):
|
|
|
151
152
|
*/
|
|
152
153
|
const framesOf = (machine: Machine.Machine<Frame.Frame<Values.Values>>): Option.Option<ReadonlyArray<Values.Values>> => {
|
|
153
154
|
const step = machine.start()
|
|
154
|
-
const copy = (frame: Frame.Frame<Values.Values>) => ({ values: { ...frame.value }, done: frame.done })
|
|
155
155
|
// Copied before the next step, which may rewrite it.
|
|
156
|
-
const
|
|
156
|
+
const copy = (frame: Frame.Frame<Values.Values>): Sampled60 => ({ values: { ...frame.value }, done: frame.done })
|
|
157
|
+
return unfoldFrames(copy(step(0)), () => copy(step(Machine.CATCH_UP_DT)))
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/** One 60 Hz sample: its values, and whether its series has settled there. */
|
|
161
|
+
interface Sampled60 {
|
|
162
|
+
readonly values: Values.Values
|
|
163
|
+
readonly done: boolean
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* A series' values from its first sample and each next one, 1/60 s on, to the one that settles: `None`
|
|
168
|
+
* past `MAX_SECONDS`. An element's machine's (`framesOf`) and a Batch's (#706).
|
|
169
|
+
*/
|
|
170
|
+
const unfoldFrames = (first: Sampled60, next: () => Sampled60): Option.Option<ReadonlyArray<Values.Values>> => {
|
|
157
171
|
const rest = Arr.unfold({ frame: first, steps: 0 }, ({ frame, steps }) =>
|
|
158
|
-
Bool.match(frame.done
|
|
172
|
+
Bool.match(Bool.or(frame.done, steps >= MAX_STEPS), {
|
|
159
173
|
onTrue: () => Option.none(),
|
|
160
174
|
onFalse: () => {
|
|
161
|
-
const
|
|
162
|
-
return Option.some([
|
|
175
|
+
const sampled = next()
|
|
176
|
+
return Option.some([sampled, { frame: sampled, steps: steps + 1 }] as const)
|
|
163
177
|
},
|
|
164
178
|
}))
|
|
165
179
|
const frames = Arr.prepend(rest, first)
|
|
@@ -236,7 +250,7 @@ const bareTolerance = (key: string): number =>
|
|
|
236
250
|
})
|
|
237
251
|
|
|
238
252
|
/** The opacities an svg-inner element draws as attributes, judged as opacity is. */
|
|
239
|
-
const SVG_OPACITIES: ReadonlySet<string> = new Set(["opacity", "fillOpacity", "strokeOpacity", "stopOpacity", "floodOpacity"])
|
|
253
|
+
const SVG_OPACITIES: ReadonlySet<string> = /*#__PURE__*/ new Set(["opacity", "fillOpacity", "strokeOpacity", "stopOpacity", "floodOpacity"])
|
|
240
254
|
|
|
241
255
|
/**
|
|
242
256
|
* A bare number's tolerance on an svg-inner element (#245), by what its attribute means: a
|
|
@@ -335,6 +349,9 @@ export const thin = (
|
|
|
335
349
|
const trimNumbers = (text: string): string =>
|
|
336
350
|
text.replace(/-?(?:\d+\.\d+|\.\d+)(?:e[-+]?\d+)?|-?\d+e[-+]?\d+/gi, (token) => String(Number(Number(token).toFixed(4)) + 0))
|
|
337
351
|
|
|
352
|
+
/** A number for a rule's text: at most four decimals, as every stop's. */
|
|
353
|
+
const numberText = (value: number): string => trimNumbers(String(value))
|
|
354
|
+
|
|
338
355
|
/** Text CSS can't hold inside a declaration, or that would end the `<style>` it sits in. */
|
|
339
356
|
const UNSAFE = /[<>{};]|\/\*|\\/
|
|
340
357
|
|
|
@@ -343,7 +360,7 @@ const UNSAFE = /[<>{};]|\/\*|\\/
|
|
|
343
360
|
* presentation attributes, which a stylesheet's or an animation's value overrides, so the attribute
|
|
344
361
|
* the engine writes at the Handoff takes over value for value. Unitless, as the attribute is.
|
|
345
362
|
*/
|
|
346
|
-
const SVG_PRESENTATION: ReadonlySet<string> = new Set([
|
|
363
|
+
const SVG_PRESENTATION: ReadonlySet<string> = /*#__PURE__*/ new Set([
|
|
347
364
|
"opacity",
|
|
348
365
|
"fill",
|
|
349
366
|
"stroke",
|
|
@@ -364,10 +381,10 @@ const SVG_PRESENTATION: ReadonlySet<string> = new Set([
|
|
|
364
381
|
* `@supports (width: 0px)` holds for HTML in every browser, so it can't tell whether one draws a
|
|
365
382
|
* `<rect>`'s from CSS.
|
|
366
383
|
*/
|
|
367
|
-
const SVG_GEOMETRY: ReadonlySet<string> = new Set(["cx", "cy", "r", "rx", "ry", "x", "y"])
|
|
384
|
+
const SVG_GEOMETRY: ReadonlySet<string> = /*#__PURE__*/ new Set(["cx", "cy", "r", "rx", "ry", "x", "y"])
|
|
368
385
|
|
|
369
386
|
/** The attribute the stroke dash writes beside it, `pathLength="1"`: the Starting style writes it too, and it never changes. */
|
|
370
|
-
const SVG_FIXED: ReadonlySet<string> = new Set(["pathLength"])
|
|
387
|
+
const SVG_FIXED: ReadonlySet<string> = /*#__PURE__*/ new Set(["pathLength"])
|
|
371
388
|
|
|
372
389
|
const BARE_NUMBER = /^-?(?:\d+\.?\d*|\.\d+)(?:e[-+]?\d+)?$/i
|
|
373
390
|
|
|
@@ -424,38 +441,65 @@ const percentOf = (stop: number, last: number): string =>
|
|
|
424
441
|
const JUMP_AT = 0.5
|
|
425
442
|
const JUMP_TAKES = 0.001
|
|
426
443
|
|
|
444
|
+
/** A rule's stops, by their offsets in 60 Hz frames, and where it ends and settles. */
|
|
445
|
+
interface Stops {
|
|
446
|
+
readonly stops: ReadonlyArray<{ readonly offset: number; readonly frame: Values.Values }>
|
|
447
|
+
readonly kept: ReadonlyArray<number>
|
|
448
|
+
/** The frame it ends at: `thin`'s. */
|
|
449
|
+
readonly end: number
|
|
450
|
+
/** The frame it settles at: the series' last. */
|
|
451
|
+
readonly last: number
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
/**
|
|
455
|
+
* A sampled series' stops (decision 9): each one `thin` keeps, and at a jump, the Frame before it held to
|
|
456
|
+
* halfway, then its own; the end drawn as the settled Frame. An element's rule's and a Batch's.
|
|
457
|
+
*/
|
|
458
|
+
const stopsOf = (frames: ReadonlyArray<Values.Values>, tag: string | undefined): Stops => {
|
|
459
|
+
const last = frames.length - 1
|
|
460
|
+
const { kept, end, jumps } = thin(frames, tag)
|
|
461
|
+
/** A kept stop's Frame: the end drawn as the settled Frame. */
|
|
462
|
+
const frameAt = (stop: number): Values.Values => frames[Bool.match(stop === end, { onTrue: () => last, onFalse: () => stop })] as Values.Values
|
|
463
|
+
const stops = Arr.flatMap(kept, (stop): ReadonlyArray<{ readonly offset: number; readonly frame: Values.Values }> =>
|
|
464
|
+
Bool.match(Arr.contains(jumps, stop), {
|
|
465
|
+
onTrue: () => [
|
|
466
|
+
{ offset: stop - JUMP_AT, frame: frames[stop - 1] as Values.Values },
|
|
467
|
+
{ offset: stop - JUMP_AT + JUMP_TAKES, frame: frameAt(stop) },
|
|
468
|
+
{ offset: stop, frame: frameAt(stop) },
|
|
469
|
+
],
|
|
470
|
+
onFalse: () => [{ offset: stop, frame: frameAt(stop) }],
|
|
471
|
+
}))
|
|
472
|
+
return { stops, kept, end, last }
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
/** A rule's timing from its stops: how long it plays, when it settles, each stop's time. */
|
|
476
|
+
const timingOf = (series: Stops): Pick<Keyframes, "duration" | "settled" | "times"> => ({
|
|
477
|
+
duration: series.end * Machine.CATCH_UP_DT,
|
|
478
|
+
settled: series.last * Machine.CATCH_UP_DT,
|
|
479
|
+
times: Arr.map(series.kept, (stop) => stop * Machine.CATCH_UP_DT),
|
|
480
|
+
})
|
|
481
|
+
|
|
482
|
+
/** A rule's body: each stop's offset and its declarations, one stop of `0%,100%` for a rule that settles at once. */
|
|
483
|
+
const bodyOf = ({ stops, end }: Stops, texts: ReadonlyArray<string>): string =>
|
|
484
|
+
Arr.map(Bool.match(end === 0, { onTrue: () => ["0%,100%"], onFalse: () => Arr.map(stops, ({ offset }) => percentOf(offset, end)) }), (offset, i) =>
|
|
485
|
+
`${offset}{${texts[i] as string}}`).join("")
|
|
486
|
+
|
|
427
487
|
/** A spec sampled into its rule, or why it can't be one. Not cached: {@link keyframes} is. */
|
|
428
488
|
const sample = (spec: Spec): Result.Result<Keyframes, string> =>
|
|
429
489
|
Option.match(framesOf(spec.machine()), {
|
|
430
490
|
onNone: () => Result.fail(`longer than ${MAX_SECONDS} s`),
|
|
431
491
|
onSome: (frames) => {
|
|
432
|
-
const last = frames.length - 1
|
|
433
492
|
const projected = spec.projected === true
|
|
434
|
-
const
|
|
435
|
-
/** A kept stop's Frame: the end drawn as the settled Frame. */
|
|
436
|
-
const frameAt = (stop: number): Values.Values => frames[Bool.match(stop === end, { onTrue: () => last, onFalse: () => stop })] as Values.Values
|
|
437
|
-
// The rule's stops: each kept one, and at a jump, the Frame before it held to halfway, then its own.
|
|
438
|
-
const stops = Arr.flatMap(kept, (stop): ReadonlyArray<{ readonly offset: number; readonly frame: Values.Values }> =>
|
|
439
|
-
Bool.match(Arr.contains(jumps, stop), {
|
|
440
|
-
onTrue: () => [
|
|
441
|
-
{ offset: stop - JUMP_AT, frame: frames[stop - 1] as Values.Values },
|
|
442
|
-
{ offset: stop - JUMP_AT + JUMP_TAKES, frame: frameAt(stop) },
|
|
443
|
-
{ offset: stop, frame: frameAt(stop) },
|
|
444
|
-
],
|
|
445
|
-
onFalse: () => [{ offset: stop, frame: frameAt(stop) }],
|
|
446
|
-
}))
|
|
493
|
+
const series = stopsOf(frames, spec.tag)
|
|
447
494
|
return Result.map(
|
|
448
|
-
Result.all(Arr.map(stops, ({ frame }) => declarationsOf(spec.tag, frame, projected, spec.template))),
|
|
495
|
+
Result.all(Arr.map(series.stops, ({ frame }) => declarationsOf(spec.tag, frame, projected, spec.template))),
|
|
449
496
|
(declarations): Keyframes => {
|
|
450
|
-
const body =
|
|
451
|
-
`${offset}{${(declarations[i] as Declarations).text}}`).join("")
|
|
497
|
+
const body = bodyOf(series, Arr.map(declarations, (d) => d.text))
|
|
452
498
|
const name = ruleNameOf(spec, body)
|
|
453
499
|
return {
|
|
454
500
|
name,
|
|
455
|
-
|
|
456
|
-
settled: last * Machine.CATCH_UP_DT,
|
|
501
|
+
...timingOf(series),
|
|
457
502
|
infinite: spec.infinite === true,
|
|
458
|
-
times: Arr.map(kept, (stop) => stop * Machine.CATCH_UP_DT),
|
|
459
503
|
css: `@keyframes ${name}{${body}}`,
|
|
460
504
|
supports: supportsOf(Arr.flatMap(declarations, (d) => d.supports)),
|
|
461
505
|
}
|
|
@@ -486,7 +530,7 @@ const conditionOf = (properties: ReadonlyArray<string>): string => Arr.map(prope
|
|
|
486
530
|
* registered when it was (#511); the oldest dropped past `CACHE_SIZE`.
|
|
487
531
|
*/
|
|
488
532
|
// Module state (#460, Q7): a cache keyed by what it caches: each mount animation's sampled keyframes, by its text.
|
|
489
|
-
const cache = new Map<string, Sampled>()
|
|
533
|
+
const cache = /*#__PURE__*/ new Map<string, Sampled>()
|
|
490
534
|
|
|
491
535
|
/** A spec's rule as sampled, and whether the colour spaces were registered then. */
|
|
492
536
|
interface Sampled {
|
|
@@ -511,11 +555,17 @@ export const keyframes = (spec: Spec): Result.Result<Keyframes, string> => {
|
|
|
511
555
|
const kept = cache.get(name)
|
|
512
556
|
if (Predicate.isNotUndefined(kept) && kept.spaces === spaces) return kept.rule
|
|
513
557
|
const sampled = sample(spec)
|
|
514
|
-
|
|
515
|
-
cache.set(name, { spaces, rule: sampled })
|
|
558
|
+
remember(cache, name, { spaces, rule: sampled })
|
|
516
559
|
return sampled
|
|
517
560
|
}
|
|
518
561
|
|
|
562
|
+
/** `value` kept in `kept` by `name`, the oldest dropped past `CACHE_SIZE`: each rule cache's. */
|
|
563
|
+
const remember = <A>(kept: Map<string, A>, name: string, value: A): A => {
|
|
564
|
+
Option.map(Arr.head(Arr.take(Array.from(kept.keys()), Math.max(0, kept.size + 1 - CACHE_SIZE))), (oldest) => kept.delete(oldest))
|
|
565
|
+
kept.set(name, value)
|
|
566
|
+
return value
|
|
567
|
+
}
|
|
568
|
+
|
|
519
569
|
/**
|
|
520
570
|
* An element's `animation` value for a play of `rule`: its name, length, delay and repeat, filling
|
|
521
571
|
* forwards. Not backwards: through its delay the element shows its inline starting style, which is the
|
|
@@ -524,10 +574,293 @@ export const keyframes = (spec: Spec): Result.Result<Keyframes, string> => {
|
|
|
524
574
|
// And the start a reduced rule's snap holds until the delay ends (ADR-0029), where the first stop is
|
|
525
575
|
// already snapped.
|
|
526
576
|
export const animationOf = (rule: Keyframes, delay: number): string =>
|
|
527
|
-
`${rule.name} ${
|
|
577
|
+
`${rule.name} ${numberText(rule.duration)}s linear ${numberText(delay)}s ${
|
|
528
578
|
Bool.match(rule.infinite, { onTrue: () => "infinite", onFalse: () => "1" })
|
|
529
579
|
} forwards`
|
|
530
580
|
|
|
581
|
+
// --- A Batch's rule (#706) ---------------------------------------------------------------------
|
|
582
|
+
|
|
583
|
+
/**
|
|
584
|
+
* A Batch's entrance, as its binding describes it: its members on one set of transitions, each with its
|
|
585
|
+
* own start and target. One rule plays them all, each member's own values read from custom properties
|
|
586
|
+
* on its element (#706).
|
|
587
|
+
*/
|
|
588
|
+
export interface BatchSpec {
|
|
589
|
+
/**
|
|
590
|
+
* Its transitions, key by key, and whether motion is reduced, as one string the binding writes the
|
|
591
|
+
* same on the server and the client: the rule's name starts with its hash, which a client checks
|
|
592
|
+
* ({@link matchesBatch}) before it takes a member over.
|
|
593
|
+
*/
|
|
594
|
+
readonly key: string
|
|
595
|
+
/** The Batch's keys, in its order: the DOM's transform keys and `opacity`. */
|
|
596
|
+
readonly keys: ReadonlyArray<string>
|
|
597
|
+
/** How its keys move, as `Batch.make` takes it. */
|
|
598
|
+
readonly transition: Batch.Transitions
|
|
599
|
+
/** Each member's start, a number a key, in `keys`' order. */
|
|
600
|
+
readonly starts: ReadonlyArray<ReadonlyArray<number>>
|
|
601
|
+
/** Each member's target, as `starts`. */
|
|
602
|
+
readonly targets: ReadonlyArray<ReadonlyArray<number>>
|
|
603
|
+
}
|
|
604
|
+
|
|
605
|
+
/** A Batch's rule, and each member's own values for it. */
|
|
606
|
+
export interface BatchKeyframes extends Keyframes {
|
|
607
|
+
/**
|
|
608
|
+
* A member's own values as the custom properties the rule reads (`--wardic-x1: 120px`), by its index
|
|
609
|
+
* in the spec; `undefined` for a member none of whose keys moves, which plays nothing.
|
|
610
|
+
*/
|
|
611
|
+
readonly memberStyle: (member: number) => Readonly<Record<string, string>> | undefined
|
|
612
|
+
}
|
|
613
|
+
|
|
614
|
+
/** The prefix a Batch rule's name starts with, from its spec's `key`: what a client checks. */
|
|
615
|
+
const batchPrefixOf = (key: string): string => `${PREFIX}b${hash53(key)}-`
|
|
616
|
+
|
|
617
|
+
/** The transform functions a Batch draws, in the renderer's order: its writer's, less a path's turn. */
|
|
618
|
+
const BATCH_FUNCS = /*#__PURE__*/ Arr.filter(TRANSFORM_FUNCS, (func) => func.key !== "pathRotation")
|
|
619
|
+
|
|
620
|
+
/**
|
|
621
|
+
* How a rule writes one key: as a value every member that moves shares, or as each one's own from custom
|
|
622
|
+
* properties, its start's (`--wardic-<key>0`) where starts differ, its target's (`--wardic-<key>1`) where
|
|
623
|
+
* targets do.
|
|
624
|
+
*/
|
|
625
|
+
interface Column {
|
|
626
|
+
readonly key: string
|
|
627
|
+
readonly k: number
|
|
628
|
+
readonly unit: string
|
|
629
|
+
/** Whether any member's start and target differ: the key is sampled. */
|
|
630
|
+
readonly moves: boolean
|
|
631
|
+
/** Whether every member starts at one value, and ends at one. */
|
|
632
|
+
readonly sharedStart: boolean
|
|
633
|
+
readonly sharedTarget: boolean
|
|
634
|
+
/** That shared start and target: the first member's. */
|
|
635
|
+
readonly start: number
|
|
636
|
+
readonly target: number
|
|
637
|
+
/** What it's sampled over when its members' values differ: 0 to their longest move, a power of two up (the tolerance's tier). */
|
|
638
|
+
readonly span: number
|
|
639
|
+
}
|
|
640
|
+
|
|
641
|
+
/** Whether every member that moves starts at one value and ends at one: the rule writes the key as it is. */
|
|
642
|
+
const isShared = (column: Column): boolean => Bool.and(column.sharedStart, column.sharedTarget)
|
|
643
|
+
|
|
644
|
+
/** The custom property for a key's start (`0`) or target (`1`). */
|
|
645
|
+
const propertyOf = (key: string, end: 0 | 1): string => `--${PREFIX}${key}${end}`
|
|
646
|
+
|
|
647
|
+
/** A key's value with its unit: `120px`, `45deg`, `0.8`. */
|
|
648
|
+
const unitText = (column: Column, value: number): string => `${numberText(value)}${column.unit}`
|
|
649
|
+
|
|
650
|
+
/** A progress, 0 to 1, for a `calc()`: five decimals, a 0.03 px miss at a move of 4,096 px; `-0` as `0`, as `trimNumbers`. */
|
|
651
|
+
const progressText = (p: number): string => String(Number(p.toFixed(5)) + 0)
|
|
652
|
+
|
|
653
|
+
/** A stop's key whose members' values differ: `p` of the way from its start's text to its target's. */
|
|
654
|
+
interface OwnStop {
|
|
655
|
+
readonly column: Column
|
|
656
|
+
readonly p: number
|
|
657
|
+
readonly start: string
|
|
658
|
+
readonly target: string
|
|
659
|
+
}
|
|
660
|
+
|
|
661
|
+
/** What a stop writes for a key whose members' values differ: the shortest form for where it is. */
|
|
662
|
+
const ownText: (stop: OwnStop) => string = /*#__PURE__*/ Match.type<OwnStop>().pipe(
|
|
663
|
+
Match.when(({ column }) => Bool.not(column.moves), ({ target }) => target),
|
|
664
|
+
Match.when(({ p }) => p === 0, ({ start }) => start),
|
|
665
|
+
Match.when(({ p }) => p === 1, ({ target }) => target),
|
|
666
|
+
Match.when(({ column }) => Bool.and(column.sharedStart, column.start === 0), ({ p, target }) => `calc(${target} * ${progressText(p)})`),
|
|
667
|
+
Match.when(({ column }) => Bool.and(column.sharedTarget, column.target === 0), ({ p, start }) => `calc(${start} * ${progressText(1 - p)})`),
|
|
668
|
+
Match.orElse(({ p, start, target }) => `calc(${start} + (${target} - ${start}) * ${progressText(p)})`),
|
|
669
|
+
)
|
|
670
|
+
|
|
671
|
+
/** What a stop writes for a key, from its sampled value: a shared one as it is, an own one as its progress. */
|
|
672
|
+
const valueText = (column: Column, frame: Values.Values): string => {
|
|
673
|
+
const value = UndefinedOr.match(frame[column.key] as number | undefined, { onUndefined: () => column.target, onDefined: identity })
|
|
674
|
+
return Bool.match(isShared(column), {
|
|
675
|
+
onTrue: () => unitText(column, value),
|
|
676
|
+
onFalse: () =>
|
|
677
|
+
ownText({
|
|
678
|
+
column,
|
|
679
|
+
p: value / column.span,
|
|
680
|
+
start: Bool.match(column.sharedStart, { onTrue: () => unitText(column, column.start), onFalse: () => `var(${propertyOf(column.key, 0)})` }),
|
|
681
|
+
target: Bool.match(column.sharedTarget, { onTrue: () => unitText(column, column.target), onFalse: () => `var(${propertyOf(column.key, 1)})` }),
|
|
682
|
+
}),
|
|
683
|
+
})
|
|
684
|
+
}
|
|
685
|
+
|
|
686
|
+
/** The power of two at or above `move`: a span shared by Batches whose longest moves are near. */
|
|
687
|
+
const spanOf = (move: number): number => 2 ** Math.ceil(Math.log2(move))
|
|
688
|
+
|
|
689
|
+
/** A spec's members that move, some key's start not its target: the ones a rule plays, and all it reads. */
|
|
690
|
+
interface Movers {
|
|
691
|
+
readonly starts: ReadonlyArray<ReadonlyArray<number>>
|
|
692
|
+
readonly targets: ReadonlyArray<ReadonlyArray<number>>
|
|
693
|
+
}
|
|
694
|
+
|
|
695
|
+
/** Whether a member moves: some key's start isn't its target. */
|
|
696
|
+
const moves = (start: ReadonlyArray<number>, target: ReadonlyArray<number>): boolean => Arr.some(start, (s, k) => s !== target[k])
|
|
697
|
+
|
|
698
|
+
/** The members that move: one that plays nothing shares nothing with the rule. */
|
|
699
|
+
const moversOf = (spec: BatchSpec): Movers => {
|
|
700
|
+
const moving = Arr.filter(Arr.range(0, spec.starts.length - 1), (m) => moves(spec.starts[m] as ReadonlyArray<number>, spec.targets[m] as ReadonlyArray<number>))
|
|
701
|
+
return {
|
|
702
|
+
starts: Arr.map(moving, (m) => spec.starts[m] as ReadonlyArray<number>),
|
|
703
|
+
targets: Arr.map(moving, (m) => spec.targets[m] as ReadonlyArray<number>),
|
|
704
|
+
}
|
|
705
|
+
}
|
|
706
|
+
|
|
707
|
+
/** Each of the spec's keys as a rule writes it, from the members that move. */
|
|
708
|
+
const columnsOf = (keys: ReadonlyArray<string>, movers: Movers): ReadonlyArray<Column> =>
|
|
709
|
+
Arr.map(keys, (key, k): Column => {
|
|
710
|
+
const starts = Arr.map(movers.starts, (start) => start[k] as number)
|
|
711
|
+
const targets = Arr.map(movers.targets, (target) => target[k] as number)
|
|
712
|
+
const start = UndefinedOr.match(starts[0], { onUndefined: () => 0, onDefined: identity })
|
|
713
|
+
const target = UndefinedOr.match(targets[0], { onUndefined: () => 0, onDefined: identity })
|
|
714
|
+
const longest = Math.max(0, ...Arr.map(starts, (s, m) => Math.abs((targets[m] as number) - s)))
|
|
715
|
+
return {
|
|
716
|
+
key,
|
|
717
|
+
k,
|
|
718
|
+
unit: Bool.match(key === "opacity", { onTrue: () => "", onFalse: () => unitOf(key) }),
|
|
719
|
+
moves: longest > 0,
|
|
720
|
+
sharedStart: Arr.every(starts, (s) => s === start),
|
|
721
|
+
sharedTarget: Arr.every(targets, (t) => t === target),
|
|
722
|
+
start,
|
|
723
|
+
target,
|
|
724
|
+
span: Bool.match(longest > 0, { onTrue: () => spanOf(longest), onFalse: () => 1 }),
|
|
725
|
+
}
|
|
726
|
+
})
|
|
727
|
+
|
|
728
|
+
/** What a rule writes: its `transform`'s keys, each with its function, and its `opacity`. */
|
|
729
|
+
interface Written {
|
|
730
|
+
readonly transform: ReadonlyArray<Column & { readonly fn: string }>
|
|
731
|
+
readonly opacity: Option.Option<Column>
|
|
732
|
+
}
|
|
733
|
+
|
|
734
|
+
/**
|
|
735
|
+
* The columns a rule writes: `opacity` when it moves; and when any transform key moves, every transform
|
|
736
|
+
* key off its identity for some member, in the renderer's order, since the rule's `transform` replaces the
|
|
737
|
+
* element's whole.
|
|
738
|
+
*/
|
|
739
|
+
const writtenOf = (movers: Movers, columns: ReadonlyArray<Column>): Written => {
|
|
740
|
+
const byKey = new Map(Arr.map(columns, (column) => [column.key, column] as const))
|
|
741
|
+
const transform = Arr.filterMap(BATCH_FUNCS, (func) =>
|
|
742
|
+
Option.match(Option.fromUndefinedOr(byKey.get(func.key)), {
|
|
743
|
+
onNone: () => Result.failVoid,
|
|
744
|
+
onSome: (column) => {
|
|
745
|
+
const off = (value: ReadonlyArray<number>): boolean => value[column.k] !== func.identity
|
|
746
|
+
return Bool.match(Bool.or(Arr.some(movers.starts, off), Arr.some(movers.targets, off)), {
|
|
747
|
+
onTrue: () => Result.succeed({ ...column, fn: func.fn }),
|
|
748
|
+
onFalse: () => Result.failVoid,
|
|
749
|
+
})
|
|
750
|
+
},
|
|
751
|
+
}))
|
|
752
|
+
return {
|
|
753
|
+
transform: Bool.match(Arr.some(transform, (column) => column.moves), { onTrue: () => transform, onFalse: () => [] }),
|
|
754
|
+
opacity: Option.filter(Option.fromUndefinedOr(byKey.get("opacity")), (column) => column.moves),
|
|
755
|
+
}
|
|
756
|
+
}
|
|
757
|
+
|
|
758
|
+
/** A writer that draws nothing: the sampler reads a Batch's values, not its writes. */
|
|
759
|
+
const UNDRAWN: Batch.Writer<null> = { writes: () => true, forKeys: () => () => undefined }
|
|
760
|
+
|
|
761
|
+
/**
|
|
762
|
+
* The Batch's own frames for its moving keys, at 0, 1/60 s, 2/60 s… to rest: one member from each key's
|
|
763
|
+
* shared start to its shared target, or from 0 to its span, stepped as a client's Batch steps it. `None`
|
|
764
|
+
* past `MAX_SECONDS`.
|
|
765
|
+
*/
|
|
766
|
+
const batchFramesOf = (spec: BatchSpec, columns: ReadonlyArray<Column>): Option.Option<ReadonlyArray<Values.Values>> => {
|
|
767
|
+
const moving = Arr.filter(columns, (column) => column.moves)
|
|
768
|
+
const batch = Batch.make({ keys: spec.keys, writer: UNDRAWN, transition: spec.transition })
|
|
769
|
+
const slot = Batch.enter(batch, null, Rec.fromEntries(Arr.map(moving, (column) => [column.key, Bool.match(isShared(column), { onTrue: () => column.start, onFalse: () => 0 })] as const)))
|
|
770
|
+
const targets = Arr.map(columns, (column) =>
|
|
771
|
+
Bool.match(column.moves, {
|
|
772
|
+
onTrue: () => Bool.match(isShared(column), { onTrue: () => column.target, onFalse: () => column.span }),
|
|
773
|
+
onFalse: () => Number.NaN,
|
|
774
|
+
}))
|
|
775
|
+
Batch.retarget(batch, [slot], (_, k) => targets[k] as number)
|
|
776
|
+
// Each step's values, and whether the Batch rests after it; the first at 0 s, as `framesOf`'s.
|
|
777
|
+
const after = (done: boolean): Sampled60 => ({ values: Rec.fromEntries(Arr.map(moving, (column) => [column.key, Batch.get(batch, slot, column.key)] as const)), done })
|
|
778
|
+
return unfoldFrames(after(Batch.step(batch, 0)), () => after(Batch.step(batch, Machine.CATCH_UP_DT)))
|
|
779
|
+
}
|
|
780
|
+
|
|
781
|
+
/**
|
|
782
|
+
* A Batch's rule's name: its key's hash, then the hash of what it writes and how: each key's shared start
|
|
783
|
+
* and target, written into it, and an own key's span. Members' own values aren't in it, so Batches whose
|
|
784
|
+
* members differ but whose longest moves share a span share a rule.
|
|
785
|
+
*/
|
|
786
|
+
const batchNameOf = (spec: BatchSpec, columns: ReadonlyArray<Column>): string => {
|
|
787
|
+
const shape = Arr.map(columns, (column) => {
|
|
788
|
+
const start = Bool.match(column.sharedStart, { onTrue: () => String(column.start), onFalse: () => "*" })
|
|
789
|
+
const target = Bool.match(column.sharedTarget, { onTrue: () => String(column.target), onFalse: () => "*" })
|
|
790
|
+
return `${column.key}:${column.moves}:${start}:${target}:${column.span}`
|
|
791
|
+
}).join("|")
|
|
792
|
+
return `${batchPrefixOf(spec.key)}${hash53(`${spec.keys.join(",")}|${shape}`)}`
|
|
793
|
+
}
|
|
794
|
+
|
|
795
|
+
/** The Batch rules sampled so far, across requests, by name (as {@link keyframes}' cache): each member's own values aren't in them. */
|
|
796
|
+
// Module state (#460, Q7): a cache keyed by what it caches.
|
|
797
|
+
const batchCache = /*#__PURE__*/ new Map<string, Result.Result<Keyframes, string>>()
|
|
798
|
+
|
|
799
|
+
/** A spec's first key with a start or target that isn't a finite number: a rule can't write it. */
|
|
800
|
+
const unfiniteKeyOf = (spec: BatchSpec): Option.Option<string> =>
|
|
801
|
+
Arr.findFirst(spec.keys, (_, k) => Arr.some(Arr.appendAll(spec.starts, spec.targets), (values) => Bool.not(Number.isFinite(values[k] as number))))
|
|
802
|
+
|
|
803
|
+
/**
|
|
804
|
+
* A Batch's rule for its members' entrance, or why it has none: one `@keyframes` for every member, each
|
|
805
|
+
* key's stops sampled from the Batch's own spring or tween at 60 Hz and thinned within its tolerance at
|
|
806
|
+
* its longest move. A key whose start and target every member that moves shares is written as it is;
|
|
807
|
+
* one whose values differ, as each member's custom properties and its progress between them.
|
|
808
|
+
*/
|
|
809
|
+
// #706, Q2 A. Sampled once a shape a process, as an element's spec is.
|
|
810
|
+
export const batchKeyframes = (spec: BatchSpec): Result.Result<BatchKeyframes, string> => {
|
|
811
|
+
const unfinite = unfiniteKeyOf(spec)
|
|
812
|
+
if (Option.isSome(unfinite)) return Result.fail(`a start or target that isn't a finite number (${unfinite.value})`)
|
|
813
|
+
const movers = moversOf(spec)
|
|
814
|
+
const columns = columnsOf(spec.keys, movers)
|
|
815
|
+
const written = writtenOf(movers, columns)
|
|
816
|
+
if (Bool.and(Arr.isReadonlyArrayEmpty(written.transform), Option.isNone(written.opacity))) return Result.fail("no key moves")
|
|
817
|
+
const name = batchNameOf(spec, columns)
|
|
818
|
+
const rule = Option.getOrElse(Option.fromUndefinedOr(batchCache.get(name)), () => remember(batchCache, name, sampleBatch(spec, columns, written, name)))
|
|
819
|
+
const own = Arr.filter(Arr.appendAll(written.transform, Option.toArray(written.opacity)), (column) => Bool.not(isShared(column)))
|
|
820
|
+
return Result.map(rule, (keyframes) => ({
|
|
821
|
+
...keyframes,
|
|
822
|
+
memberStyle: (member) => {
|
|
823
|
+
const start = spec.starts[member] as ReadonlyArray<number>
|
|
824
|
+
const target = spec.targets[member] as ReadonlyArray<number>
|
|
825
|
+
return Bool.match(moves(start, target), {
|
|
826
|
+
onFalse: () => undefined,
|
|
827
|
+
onTrue: () =>
|
|
828
|
+
Rec.fromEntries(Arr.flatMap(own, (column): ReadonlyArray<readonly [string, string]> => [
|
|
829
|
+
...Bool.match(Bool.and(column.moves, Bool.not(column.sharedStart)), { onTrue: () => [[propertyOf(column.key, 0), unitText(column, start[column.k] as number)] as const], onFalse: () => [] }),
|
|
830
|
+
...Bool.match(column.sharedTarget, { onTrue: () => [], onFalse: () => [[propertyOf(column.key, 1), unitText(column, target[column.k] as number)] as const] }),
|
|
831
|
+
])),
|
|
832
|
+
})
|
|
833
|
+
},
|
|
834
|
+
}))
|
|
835
|
+
}
|
|
836
|
+
|
|
837
|
+
/** A Batch's rule sampled, or why it can't be. Not cached: {@link batchKeyframes} is. */
|
|
838
|
+
const sampleBatch = (spec: BatchSpec, columns: ReadonlyArray<Column>, written: Written, name: string): Result.Result<Keyframes, string> =>
|
|
839
|
+
Option.match(batchFramesOf(spec, columns), {
|
|
840
|
+
onNone: () => Result.fail(`longer than ${MAX_SECONDS} s`),
|
|
841
|
+
onSome: (frames) => {
|
|
842
|
+
const series = stopsOf(frames, undefined)
|
|
843
|
+
const texts = Arr.map(series.stops, ({ frame }) => {
|
|
844
|
+
const transform = Arr.map(written.transform, (column) => `${column.fn}(${valueText(column, frame)})`).join(" ")
|
|
845
|
+
const declarations = Arr.appendAll(
|
|
846
|
+
Bool.match(transform === "", { onTrue: () => [], onFalse: () => [`transform:${transform}`] }),
|
|
847
|
+
Option.toArray(Option.map(written.opacity, (column) => `opacity:${valueText(column, frame)}`)),
|
|
848
|
+
)
|
|
849
|
+
return declarations.join(";")
|
|
850
|
+
})
|
|
851
|
+
const body = bodyOf(series, texts)
|
|
852
|
+
return Result.succeed({ name, ...timingOf(series), infinite: false, css: `@keyframes ${name}{${body}}`, supports: [] })
|
|
853
|
+
},
|
|
854
|
+
})
|
|
855
|
+
|
|
856
|
+
/**
|
|
857
|
+
* A Batch slot's `animation`, less a delay: each member's inline `animation` names the slot and gives its
|
|
858
|
+
* own after it, `var(--wardic-aN) 0.035s`, the shorthand's second time. A delay here too would be a third,
|
|
859
|
+
* and the member's `animation` invalid: it would keep its start.
|
|
860
|
+
*/
|
|
861
|
+
// #706, Q2 and Q6: one `:root` line a rule group, which reduced motion's media query switches; a member's stagger its own.
|
|
862
|
+
const slotAnimationOf = (rule: Keyframes): string => `${rule.name} ${numberText(rule.duration)}s linear 1 forwards`
|
|
863
|
+
|
|
531
864
|
// --- A render's sheet --------------------------------------------------------------------------
|
|
532
865
|
|
|
533
866
|
/** One element's place in a sheet. */
|
|
@@ -547,10 +880,26 @@ export interface Slot {
|
|
|
547
880
|
readonly define: (play: Play, reduced?: Play | undefined, supports?: ReadonlyArray<string>) => Result.Result<void, string>
|
|
548
881
|
}
|
|
549
882
|
|
|
883
|
+
/** A Batch's members' place in a sheet: one slot for every member on one rule (#706). */
|
|
884
|
+
export interface BatchSlot {
|
|
885
|
+
/**
|
|
886
|
+
* A member's inline style, by its index in the rule's spec: its own values, and its `animation`, the slot
|
|
887
|
+
* and, when it waits (its stagger), its delay after it. `undefined` for a member that doesn't move.
|
|
888
|
+
*/
|
|
889
|
+
readonly styleOf: (member: number, delay: number) => Readonly<Record<string, string>> | undefined
|
|
890
|
+
}
|
|
891
|
+
|
|
550
892
|
/** One render's Appear rules: a slot per element, and the `<style>` text for the head and each late chunk. */
|
|
551
893
|
export interface Sheet {
|
|
552
894
|
/** A slot for the next element. */
|
|
553
895
|
readonly slot: () => Slot
|
|
896
|
+
/**
|
|
897
|
+
* A slot for a Batch's members on `play`'s rule, and under `prefers-reduced-motion: reduce` `reduced`'s
|
|
898
|
+
* when given (a provider's `"user"`): both rules, and the slot's `:root` lines naming each with no delay,
|
|
899
|
+
* since each member gives its own.
|
|
900
|
+
*/
|
|
901
|
+
// #706, Q2 and Q6: one `:root` line a rule group, not a member.
|
|
902
|
+
readonly batch: (play: BatchKeyframes, reduced?: BatchKeyframes | undefined) => BatchSlot
|
|
554
903
|
/**
|
|
555
904
|
* The CSS since the last call: each rule this render hasn't written yet, and the custom property of
|
|
556
905
|
* each slot defined since, the reduced ones under the media query. Called once for the head, and again
|
|
@@ -586,13 +935,37 @@ export const sheet = (): Sheet => {
|
|
|
586
935
|
const written = new Set<string>()
|
|
587
936
|
const blocks: Blocks = new Map()
|
|
588
937
|
const reducedBlocks: Blocks = new Map()
|
|
938
|
+
/** A rule added to the sheet, unless it has been written. */
|
|
939
|
+
const add = (rule: Keyframes): void => {
|
|
940
|
+
if (Bool.not(written.has(rule.name))) rules.set(rule.name, rule.css)
|
|
941
|
+
}
|
|
589
942
|
/** A play's rule added to the sheet: its `animation` value, and the geometry properties it writes. */
|
|
590
943
|
const animationFor = (play: Play): Result.Result<{ readonly animation: string; readonly supports: ReadonlyArray<string> }, string> =>
|
|
591
944
|
Result.map(keyframes(play.spec), (rule) => {
|
|
592
|
-
|
|
945
|
+
add(rule)
|
|
593
946
|
return { animation: animationOf(rule, play.delay), supports: rule.supports }
|
|
594
947
|
})
|
|
595
948
|
return {
|
|
949
|
+
batch: (play, reduced) => {
|
|
950
|
+
const property = `--${PREFIX}a${slots++}`
|
|
951
|
+
add(play)
|
|
952
|
+
addTo(blocks, "", `${property}:${slotAnimationOf(play)}`)
|
|
953
|
+
UndefinedOr.map(reduced, (rule) => {
|
|
954
|
+
add(rule)
|
|
955
|
+
addTo(reducedBlocks, "", `${property}:${slotAnimationOf(rule)}`)
|
|
956
|
+
})
|
|
957
|
+
return {
|
|
958
|
+
styleOf: (member, delay) =>
|
|
959
|
+
UndefinedOr.map(play.memberStyle(member), (own) => {
|
|
960
|
+
// Once a member a render, on a server: its own values (both rules', which read the same), and the
|
|
961
|
+
// slot with its delay after it, the shorthand's second time: a longhand after a `var()` shorthand
|
|
962
|
+
// leaves the inline `animation` unreadable, which `pending` reads.
|
|
963
|
+
const reducedOwn = UndefinedOr.match(reduced, { onUndefined: () => ({}), onDefined: (rule) => rule.memberStyle(member) })
|
|
964
|
+
const wait = Bool.match(delay === 0, { onTrue: () => "", onFalse: () => ` ${numberText(delay)}s` })
|
|
965
|
+
return { ...own, ...reducedOwn, animation: `var(${property})${wait}` }
|
|
966
|
+
}),
|
|
967
|
+
}
|
|
968
|
+
},
|
|
596
969
|
slot: () => {
|
|
597
970
|
const property = `--${PREFIX}a${slots++}`
|
|
598
971
|
return {
|
|
@@ -653,7 +1026,7 @@ const escapeAttribute = (value: string): string => value.replaceAll("&", "&"
|
|
|
653
1026
|
|
|
654
1027
|
/** The documents {@link adoptChunks} watches, or has: one observer a document. */
|
|
655
1028
|
// Module state (#460, Q7): the documents already watched, held weakly.
|
|
656
|
-
const watched = new WeakSet<Document>()
|
|
1029
|
+
const watched = /*#__PURE__*/ new WeakSet<Document>()
|
|
657
1030
|
|
|
658
1031
|
/**
|
|
659
1032
|
* Moves each late chunk's Appear rules into the head: its `<style data-wardic-appear-chunk>`'s text into a
|
|
@@ -691,18 +1064,61 @@ export const adoptChunks = (doc: Document): void => {
|
|
|
691
1064
|
const nameOfAnimation = (animation: Animation): Option.Option<string> =>
|
|
692
1065
|
Option.filter(Option.some((animation as Animation & { readonly animationName?: unknown }).animationName), Predicate.isString)
|
|
693
1066
|
|
|
694
|
-
/**
|
|
1067
|
+
/** Whether `animation` is an Appear one: a `wardic-…` CSS animation. */
|
|
1068
|
+
const isAppearAnimation = (animation: Animation): boolean => Option.exists(nameOfAnimation(animation), (name) => name.startsWith(PREFIX))
|
|
1069
|
+
|
|
1070
|
+
/**
|
|
1071
|
+
* Whether `name` is a Batch's rule's (#706): `wardic-b<key>-<shape>`, two hashes, where an element's is
|
|
1072
|
+
* `wardic-<hash>`, one, and a hash has no `-`.
|
|
1073
|
+
*/
|
|
1074
|
+
const isBatchRuleName = (name: string): boolean => name.includes("-", PREFIX.length)
|
|
1075
|
+
|
|
1076
|
+
/** Whether `animation` is an element's own Appear one: a `wardic-…` CSS animation, not a Batch's rule. */
|
|
1077
|
+
const isElementAppearAnimation = (animation: Animation): boolean =>
|
|
1078
|
+
Option.exists(nameOfAnimation(animation), (name) => Bool.and(name.startsWith(PREFIX), Bool.not(isBatchRuleName(name))))
|
|
1079
|
+
|
|
1080
|
+
/** The element an animation draws, where it has one. */
|
|
1081
|
+
const targetOf = (animation: Animation): Option.Option<Element> =>
|
|
1082
|
+
Option.flatMap(Option.fromNullishOr(animation.effect), (effect) => Option.fromNullishOr((effect as KeyframeEffect).target))
|
|
1083
|
+
|
|
1084
|
+
/**
|
|
1085
|
+
* The element's Appear animation: its `wardic-…` CSS animation, running, or finished and filling. A
|
|
1086
|
+
* Batch's rule on a member's element isn't one (#706): its Batch takes it over, not a Wardic element there.
|
|
1087
|
+
*/
|
|
695
1088
|
// ADR-0044, decision 6.
|
|
696
1089
|
export const find = (element: Element): Option.Option<Animation> =>
|
|
697
1090
|
Bool.match(Predicate.hasProperty(element, "getAnimations"), {
|
|
698
1091
|
onFalse: () => Option.none(),
|
|
699
|
-
onTrue: () => Arr.findFirst(element.getAnimations(),
|
|
1092
|
+
onTrue: () => Arr.findFirst(element.getAnimations(), isElementAppearAnimation),
|
|
700
1093
|
})
|
|
701
1094
|
|
|
1095
|
+
/**
|
|
1096
|
+
* A document's Appear animations by element, running or finished and filling: one `getAnimations()` for
|
|
1097
|
+
* the page, for a Handoff of many elements at once, a Batch's members (#706). Each element's first: a
|
|
1098
|
+
* member's Batch rule, or the one `find` gets.
|
|
1099
|
+
*/
|
|
1100
|
+
// #706, Q3: measured at a Batch's 1,000 members in one list, Chromium's `Element.getAnimations()` cost
|
|
1101
|
+
// about a whole document's each (5.6 s for every member; the document's, 5.6 ms); Firefox's, alike either way.
|
|
1102
|
+
export const findAll = (doc: Document): ReadonlyMap<Element, Animation> => {
|
|
1103
|
+
const byElement = new Map<Element, Animation>()
|
|
1104
|
+
if (Bool.not(Predicate.hasProperty(doc, "getAnimations"))) return byElement
|
|
1105
|
+
// Once a Handoff, over the page's animations: the first per element, as `find`'s.
|
|
1106
|
+
Arr.forEach(Arr.filter(doc.getAnimations(), isAppearAnimation), (animation) =>
|
|
1107
|
+
Option.map(Option.filter(targetOf(animation), (target) => Bool.not(byElement.has(target))), (target) => byElement.set(target, animation)))
|
|
1108
|
+
return byElement
|
|
1109
|
+
}
|
|
1110
|
+
|
|
702
1111
|
/** Whether `animation` is `spec`'s: the rule the server sampled from the same spec. */
|
|
703
1112
|
export const matches = (animation: Animation, spec: Pick<Spec, "key" | "tag" | "infinite" | "projected">): boolean =>
|
|
704
1113
|
Option.exists(nameOfAnimation(animation), (name) => name === nameOf(spec))
|
|
705
1114
|
|
|
1115
|
+
/**
|
|
1116
|
+
* Whether `animation` is a Batch rule for a spec's `key`: its name starts with the key's prefix, so the
|
|
1117
|
+
* server's transitions and reduced motion are the client's. Its members' values aren't checked (#706, Q9).
|
|
1118
|
+
*/
|
|
1119
|
+
export const matchesBatch = (animation: Animation, key: string): boolean =>
|
|
1120
|
+
Option.exists(nameOfAnimation(animation), (name) => name.startsWith(batchPrefixOf(key)))
|
|
1121
|
+
|
|
706
1122
|
/** How far `animation` has got, in seconds since it started (its delay included): 0 before it has. */
|
|
707
1123
|
export const elapsedOf = (animation: Animation): number =>
|
|
708
1124
|
Option.match(Option.filter(Option.fromNullishOr(animation.currentTime), Predicate.isNumber), {
|
|
@@ -716,24 +1132,3 @@ export const delayOf = (animation: Animation): number =>
|
|
|
716
1132
|
onNone: () => 0,
|
|
717
1133
|
onSome: (effect) => Option.getOrElse(Option.filter(Option.some(effect.getTiming().delay), Predicate.isNumber), () => 0) / 1000,
|
|
718
1134
|
})
|
|
719
|
-
|
|
720
|
-
/** What a slot's inline `animation` starts with: `var(--wardic-a`. */
|
|
721
|
-
const SLOT_PREFIX = `var(--${PREFIX}a`
|
|
722
|
-
|
|
723
|
-
/**
|
|
724
|
-
* Whether `element`'s Appear animation is running, not yet handed over: its inline style still names its
|
|
725
|
-
* slot, which `release` removes. A layout measure asks it of each node, so a running animation's
|
|
726
|
-
* transform, which beats the inline one, stays out of the box it reads.
|
|
727
|
-
*/
|
|
728
|
-
// #245: a Measure pass; a string read, no style resolved.
|
|
729
|
-
export const pending = (element: ElementCSSInlineStyle): boolean => element.style.getPropertyValue("animation").startsWith(SLOT_PREFIX)
|
|
730
|
-
|
|
731
|
-
/**
|
|
732
|
-
* The handover's last step: the element's inline `animation` removed, in the task that writes the
|
|
733
|
-
* engine's first frame, so no paint falls between. While it ran, its value beat the inline style the
|
|
734
|
-
* engine writes; removing it ends the CSS animation at that task's style update.
|
|
735
|
-
*/
|
|
736
|
-
// The **Handoff** (ADR-0044, decision 6). A `cancel()` too changed nothing (a mutation, #48's PR 1).
|
|
737
|
-
export const release = (element: ElementCSSInlineStyle): void => {
|
|
738
|
-
element.style.removeProperty("animation")
|
|
739
|
-
}
|