@cube-drone/marquee-react-renderer 0.9.0 → 0.9.1

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.
Files changed (3) hide show
  1. package/dist/render.js +103 -119
  2. package/package.json +7 -7
  3. package/src/render.ts +124 -161
package/dist/render.js CHANGED
@@ -10,7 +10,7 @@
10
10
  // author bytes only ever reach it as a target string, which the hook escapes.
11
11
  // Pass React-returning `hooks` instead to avoid the string path entirely.
12
12
  import { createElement as h, useState } from "react";
13
- import { FONTS, render as renderHtml, } from "@cube-drone/marquee-html-renderer";
13
+ import { effectLook, FONTS, render as renderHtml, splitPieces, } from "@cube-drone/marquee-html-renderer";
14
14
  /** The string-hook path for embedder vocabulary (no React hook supplied):
15
15
  * probe with empty children - the contract says a claim never depends on
16
16
  * them - and only for a claim pay for the children as HTML, through the
@@ -88,6 +88,10 @@ function styleVars(attrs, profile) {
88
88
  else if (isColorValue(bg)) {
89
89
  style["--mq-bg"] = bg;
90
90
  }
91
+ // A scheme's signature color (an EarthBound frame, an FF6 window).
92
+ if (isColorValue(attrs["accent"])) {
93
+ style["--mq-accent"] = attrs["accent"];
94
+ }
91
95
  return style;
92
96
  }
93
97
  function schemeClass(attrs) {
@@ -274,6 +278,14 @@ function turbolink(node, target, levelAttr, ctx, key) {
274
278
  }
275
279
  function directive(node, ctx, key) {
276
280
  const { name, attrs } = node;
281
+ // The hooks see the children rendered; built-ins that lay their children
282
+ // out themselves (table, stack) render them again, so rewind the aside
283
+ // numbering first - or a note inside counts twice.
284
+ const saved = { n: ctx.note.n, pending: [...ctx.note.pending] };
285
+ const rewind = () => {
286
+ ctx.note.n = saved.n;
287
+ ctx.note.pending = [...saved.pending];
288
+ };
277
289
  const kids = renderChildren(node.children, ctx);
278
290
  const custom = ctx.hooks.directive?.(name, attrs, kids);
279
291
  if (custom !== undefined && custom !== null) {
@@ -321,7 +333,19 @@ function directive(node, ctx, key) {
321
333
  return h("div", nodeProps(node, ctx, { key, className: "mq-media", style }), ...kids);
322
334
  }
323
335
  case "table":
336
+ rewind();
324
337
  return renderTable(node, ctx, key);
338
+ case "stack":
339
+ rewind();
340
+ return renderStack(node, ctx, key);
341
+ case "layer":
342
+ // Outside a stack a layer is just a section-like container: its style
343
+ // knobs apply, place/backing mean nothing.
344
+ return h("div", nodeProps(node, ctx, {
345
+ key,
346
+ className: `mq-layer${schemeClass(attrs)}${fontClass(attrs)}`,
347
+ style: styleVars(attrs, ctx.profile),
348
+ }), ...kids);
325
349
  case "center":
326
350
  case "right":
327
351
  case "left":
@@ -393,6 +417,64 @@ function renderTable(node, ctx, key) {
393
417
  const table = h("table", nodeProps(node, ctx, { key, className: "mq-table" }), h("tbody", null, rows));
394
418
  return withNotes(ctx, table, key);
395
419
  }
420
+ /** A stack paints at most this many layers; the rest flow below the box. */
421
+ const MAX_LAYERS = 8;
422
+ const PLACES = new Set([
423
+ "fill", "top-left", "top", "top-right", "left", "center", "right", "bottom-left", "bottom", "bottom-right",
424
+ ]);
425
+ /** The `aspect` vocabulary as CSS aspect-ratio values. */
426
+ const STACK_ASPECTS = {
427
+ "16:9": "16/9",
428
+ "4:3": "4/3",
429
+ "3:2": "3/2",
430
+ "1:1": "1/1",
431
+ card: "5/7",
432
+ };
433
+ /** :::stack - every direct child is a layer, source order is z-order; the
434
+ * stylesheet puts every layer in one grid cell (SPEC.md, "Stacks"). Layers
435
+ * past the cap render in flow below the box. */
436
+ function renderStack(node, ctx, key) {
437
+ const style = {};
438
+ const aspectAttr = node.attrs["aspect"] ?? "";
439
+ if (Object.hasOwn(STACK_ASPECTS, aspectAttr)) {
440
+ style["--mq-stack-aspect"] = STACK_ASPECTS[aspectAttr];
441
+ }
442
+ const w = mediaSize(node.attrs["width"]);
443
+ if (w !== null) {
444
+ style["--mq-stack-w"] = w;
445
+ }
446
+ const layers = node.children.slice(0, MAX_LAYERS).map((child, i) => renderLayer(child, ctx, String(i)));
447
+ // The class tells the stylesheet a cover base fills the box rather than
448
+ // sizing it.
449
+ const className = style["--mq-stack-aspect"] === undefined ? "mq-stack" : "mq-stack mq-stack-aspect";
450
+ const stack = h("div", nodeProps(node, ctx, { key, className, style }), ...layers);
451
+ const rest = node.children.slice(MAX_LAYERS);
452
+ if (rest.length === 0) {
453
+ return stack;
454
+ }
455
+ return h("div", { key, style: { display: "contents" } }, stack, h("div", { key: "rest", className: "mq-stack-rest" }, ...renderChildren(rest, ctx)));
456
+ }
457
+ /** One layer of a stack: a `:::layer` child carries knobs; any other child
458
+ * block is a layer with the defaults (`fill`, no backing). */
459
+ function renderLayer(child, ctx, key) {
460
+ const isLayer = child.type === "directive" && child.name === "layer";
461
+ const attrs = isLayer ? child.attrs : {};
462
+ const content = isLayer ? child.children : [child];
463
+ const place = PLACES.has(attrs["place"] ?? "") ? attrs["place"] : "fill";
464
+ const backing = attrs["backing"] === "box" || attrs["backing"] === "outline" ? ` mq-backing-${attrs["backing"]}` : "";
465
+ // A fill layer that is exactly one embed is that media covering the box -
466
+ // the one crop the spec permits, of pictures, never of words.
467
+ const only = content.length === 1 ? content[0] : undefined;
468
+ const cover = place === "fill" && only?.type === "paragraph" && only.children.length === 1 && only.children[0].type === "embed"
469
+ ? " mq-layer-cover"
470
+ : "";
471
+ const props = {
472
+ key,
473
+ className: `mq-layer mq-place-${place}${backing}${cover}${schemeClass(attrs)}${fontClass(attrs)}`,
474
+ style: styleVars(attrs, ctx.profile),
475
+ };
476
+ return h("div", isLayer ? nodeProps(child, ctx, props) : props, ...renderChildren(content, ctx));
477
+ }
396
478
  function sizeRung(value, inner, node, ctx, key) {
397
479
  // <font> is deliberate: presentational attributes are outside CSP's
398
480
  // jurisdiction, so size survives with no stylesheet at all.
@@ -492,99 +574,28 @@ function span(node, ctx, key) {
492
574
  // Unknown span: pure shrug, children as plain content.
493
575
  return h("span", nodeProps(node, ctx, { key, style: { display: "contents" } }), ...kids);
494
576
  }
495
- // -- effects, including the per-unit (by=letter / by=word) machinery. The
496
- // offsets mirror the static renderer's exactly, so a document animates the
497
- // same in both - deterministic, never random.
498
- const SEGMENTERS = {
499
- letter: new Intl.Segmenter("en", { granularity: "grapheme" }),
500
- word: new Intl.Segmenter("en", { granularity: "word" }),
501
- };
502
- const MAX_SPLIT_UNITS = 400;
503
- const MAX_REVEAL_UNITS = 2000;
504
- function isReveal(effectName) {
505
- return effectName === "typewriter" || effectName === "fadein";
506
- }
507
- function revealStep(speedAttr, dflt) {
508
- const speed = speedAttr !== undefined && COUNT.test(speedAttr) && Number(speedAttr) > 0
509
- ? Number(speedAttr)
510
- : dflt;
511
- return Math.round(1000 / speed) / 1000;
512
- }
513
- function gcd(a, b) {
514
- return b === 0 ? a : gcd(b, a % b);
515
- }
516
- function scatterStride(total) {
517
- let stride = Math.max(1, Math.round(total * 0.618));
518
- while (stride < total && gcd(stride, total) !== 1) {
519
- stride += 1;
520
- }
521
- return gcd(stride, total) === 1 ? stride : 1;
522
- }
523
- function unitOffset(state) {
524
- if (isReveal(state.effect)) {
525
- const o = state.phase === "scatter" ? (state.i * scatterStride(state.total)) % state.total : state.i;
526
- return String(o);
527
- }
528
- let o;
529
- if (state.phase === "scatter") {
530
- o = ((state.i * 7919) % 101) / 101;
531
- }
532
- else {
533
- switch (state.effect) {
534
- case "rainbow":
535
- o = state.i / state.total;
536
- break;
537
- case "bounce":
538
- o = (state.i % 6) / 6;
539
- break;
540
- default:
541
- o = (state.i % 8) / 8;
542
- break;
543
- }
544
- }
545
- return String(Math.round(o * 1000) / 1000);
546
- }
547
- function isUnit(seg, by) {
548
- return by === "word" ? seg.isWordLike === true : !/^\s+$/.test(seg.segment);
549
- }
550
- function countUnits(nodes, by) {
551
- let n = 0;
552
- for (const node of nodes) {
553
- if (node.type === "text") {
554
- for (const seg of SEGMENTERS[by].segment(node.value)) {
555
- if (isUnit(seg, by)) {
556
- n += 1;
557
- }
558
- }
559
- }
560
- else if (node.type === "emphasis" || node.type === "strong" || node.type === "strikethrough") {
561
- n += countUnits(node.children, by);
562
- }
563
- }
564
- return n;
565
- }
566
- function splitRender(nodes, ctx, state) {
577
+ // -- effects: the look (classes, knobs, the per-unit split and its word
578
+ // groups) comes from the static renderer's effects.ts, so a document
579
+ // animates the same in both - deterministic, never random.
580
+ function splitRender(nodes, ctx, look) {
581
+ const pieces = (list, prefix) => list.map((p, i) => p.kind === "text"
582
+ ? p.text // spaces and punctuation ride along
583
+ : p.kind === "unit"
584
+ ? h("span", { key: `${prefix}${i}`, className: "mq-l", style: { "--mq-o": p.offset } }, p.text)
585
+ : h("span", { key: `${prefix}${i}`, className: "mq-w" }, ...pieces(p.pieces, `${prefix}${i}-`)));
567
586
  const out = [];
568
587
  nodes.forEach((node, ni) => {
569
588
  if (node.type === "text") {
570
- for (const seg of SEGMENTERS[state.by].segment(node.value)) {
571
- if (!isUnit(seg, state.by)) {
572
- out.push(seg.segment); // spaces and punctuation ride along
573
- continue;
574
- }
575
- const o = unitOffset(state);
576
- state.i += 1;
577
- out.push(h("span", { key: `${ni}-${state.i}`, className: "mq-l", style: { "--mq-o": o } }, seg.segment));
578
- }
589
+ out.push(...pieces(splitPieces(node, look), `${ni}-`));
579
590
  }
580
591
  else if (node.type === "emphasis") {
581
- out.push(h("em", { key: ni }, ...splitRender(node.children, ctx, state)));
592
+ out.push(h("em", { key: ni }, ...splitRender(node.children, ctx, look)));
582
593
  }
583
594
  else if (node.type === "strong") {
584
- out.push(h("strong", { key: ni }, ...splitRender(node.children, ctx, state)));
595
+ out.push(h("strong", { key: ni }, ...splitRender(node.children, ctx, look)));
585
596
  }
586
597
  else if (node.type === "strikethrough") {
587
- out.push(h("del", { key: ni }, ...splitRender(node.children, ctx, state)));
598
+ out.push(h("del", { key: ni }, ...splitRender(node.children, ctx, look)));
588
599
  }
589
600
  else {
590
601
  out.push(renderNode(node, ctx, String(ni))); // anything else renders whole
@@ -593,39 +604,12 @@ function splitRender(nodes, ctx, state) {
593
604
  return out;
594
605
  }
595
606
  function effect(node, ctx, key) {
596
- const { name, attrs } = node;
597
- const byAttr = attrs["by"];
598
- const splitBy = byAttr === "letter" || byAttr === "word"
599
- ? byAttr
600
- : name === "typewriter"
601
- ? "letter" // typewriter is per-unit by nature
602
- : null;
603
- const style = {};
604
- if (name === "blink" && attrs["rate"] !== undefined && COUNT.test(attrs["rate"])) {
605
- style["--mq-rate"] = attrs["rate"];
606
- }
607
- if (name === "typewriter") {
608
- style["--mq-tw-step"] = `${revealStep(attrs["speed"], 14)}s`;
609
- }
610
- if (name === "fadein" && splitBy !== null) {
611
- style["--mq-fi-step"] = `${revealStep(attrs["speed"], 16)}s`;
612
- }
613
- const className = `mq-${name} ${ANIM_CLASS}`;
614
- if (splitBy === null) {
615
- return h("span", nodeProps(node, ctx, { key, className, style }), ...renderChildren(node.children, ctx));
616
- }
617
- const phase = attrs["phase"] === "scatter" || attrs["phase"] === "ramp"
618
- ? attrs["phase"]
619
- : name === "jitter"
620
- ? "scatter"
621
- : "ramp";
622
- const total = countUnits(node.children, splitBy);
623
- const cap = isReveal(name) ? MAX_REVEAL_UNITS : MAX_SPLIT_UNITS;
624
- if (total === 0 || total > cap) {
625
- // Past the cap: the run animates whole. DOM weight discipline.
626
- return h("span", nodeProps(node, ctx, { key, className, style }), ...renderChildren(node.children, ctx));
627
- }
628
- const state = { effect: name, by: splitBy, phase, i: 0, total };
629
- return h("span", nodeProps(node, ctx, { key, className: `${className} mq-split`, style }), ...splitRender(node.children, ctx, state));
607
+ // The look - classes, knobs, the per-unit split and its word groups - is
608
+ // the HTML renderer's own (effects.ts), so the two can't disagree.
609
+ const look = effectLook(node.name, node.attrs, node.children);
610
+ const style = Object.fromEntries(look.vars);
611
+ const className = `mq-${node.name} ${ANIM_CLASS}${look.units === null ? "" : " mq-split"}`;
612
+ const kids = look.units === null ? renderChildren(node.children, ctx) : splitRender(node.children, ctx, look);
613
+ return h("span", nodeProps(node, ctx, { key, className, style }), ...kids);
630
614
  }
631
615
  export { ANIMATED };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cube-drone/marquee-react-renderer",
3
- "version": "0.9.0",
3
+ "version": "0.9.1",
4
4
  "description": "Interactive React renderer for the Marquee markup language: the JS halves of the animation contract (start-on-visible, tap-to-skip) plus source-position scroll sync for side-by-side editors",
5
5
  "license": "MPL-2.0",
6
6
  "repository": {
@@ -34,8 +34,8 @@
34
34
  "demo": "node --conditions=marquee-src scripts/demo.ts"
35
35
  },
36
36
  "dependencies": {
37
- "@cube-drone/marquee-html-renderer": "^0.9.0",
38
- "@cube-drone/marquee-parser": "^0.9.0"
37
+ "@cube-drone/marquee-html-renderer": "^0.9.1",
38
+ "@cube-drone/marquee-parser": "^0.9.1"
39
39
  },
40
40
  "peerDependencies": {
41
41
  "react": ">=18"
@@ -48,9 +48,9 @@
48
48
  "react": "^19.0.0",
49
49
  "react-dom": "^19.0.0",
50
50
  "typescript": "^5.9.0",
51
- "@cube-drone/marquee-css": "^0.9.0",
52
- "@cube-drone/marquee-fonts": "^0.9.0",
53
- "@cube-drone/marquee-emoji": "^0.9.0",
54
- "@cube-drone/marquee-turbolink": "^0.9.0"
51
+ "@cube-drone/marquee-css": "^0.9.1",
52
+ "@cube-drone/marquee-fonts": "^0.9.1",
53
+ "@cube-drone/marquee-emoji": "^0.9.1",
54
+ "@cube-drone/marquee-turbolink": "^0.9.1"
55
55
  }
56
56
  }
package/src/render.ts CHANGED
@@ -13,8 +13,12 @@
13
13
  import { createElement as h, useState, type KeyboardEvent, type ReactNode } from "react";
14
14
  import type { Attrs, Node, Span } from "@cube-drone/marquee-parser";
15
15
  import {
16
+ effectLook,
16
17
  FONTS,
17
18
  render as renderHtml,
19
+ splitPieces,
20
+ type EffectLook,
21
+ type SplitPiece,
18
22
  type EmojiResolution,
19
23
  type Profile,
20
24
  type TurbolinkLevel,
@@ -128,6 +132,10 @@ function styleVars(attrs: Attrs, profile: Profile): Record<string, string> {
128
132
  } else if (isColorValue(bg)) {
129
133
  style["--mq-bg"] = bg;
130
134
  }
135
+ // A scheme's signature color (an EarthBound frame, an FF6 window).
136
+ if (isColorValue(attrs["accent"])) {
137
+ style["--mq-accent"] = attrs["accent"];
138
+ }
131
139
  return style;
132
140
  }
133
141
 
@@ -379,6 +387,14 @@ function turbolink(
379
387
 
380
388
  function directive(node: Node & { type: "directive" }, ctx: Ctx, key: string): ReactNode {
381
389
  const { name, attrs } = node;
390
+ // The hooks see the children rendered; built-ins that lay their children
391
+ // out themselves (table, stack) render them again, so rewind the aside
392
+ // numbering first - or a note inside counts twice.
393
+ const saved = { n: ctx.note.n, pending: [...ctx.note.pending] };
394
+ const rewind = (): void => {
395
+ ctx.note.n = saved.n;
396
+ ctx.note.pending = [...saved.pending];
397
+ };
382
398
  const kids = renderChildren(node.children, ctx);
383
399
 
384
400
  const custom = ctx.hooks.directive?.(name, attrs, kids);
@@ -440,7 +456,23 @@ function directive(node: Node & { type: "directive" }, ctx: Ctx, key: string): R
440
456
  return h("div", nodeProps(node, ctx, { key, className: "mq-media", style }), ...kids);
441
457
  }
442
458
  case "table":
459
+ rewind();
443
460
  return renderTable(node, ctx, key);
461
+ case "stack":
462
+ rewind();
463
+ return renderStack(node, ctx, key);
464
+ case "layer":
465
+ // Outside a stack a layer is just a section-like container: its style
466
+ // knobs apply, place/backing mean nothing.
467
+ return h(
468
+ "div",
469
+ nodeProps(node, ctx, {
470
+ key,
471
+ className: `mq-layer${schemeClass(attrs)}${fontClass(attrs)}`,
472
+ style: styleVars(attrs, ctx.profile),
473
+ }),
474
+ ...kids,
475
+ );
444
476
  case "center":
445
477
  case "right":
446
478
  case "left":
@@ -530,6 +562,75 @@ function renderTable(node: Node & { type: "directive" }, ctx: Ctx, key: string):
530
562
  return withNotes(ctx, table, key);
531
563
  }
532
564
 
565
+ /** A stack paints at most this many layers; the rest flow below the box. */
566
+ const MAX_LAYERS = 8;
567
+
568
+ const PLACES = new Set([
569
+ "fill", "top-left", "top", "top-right", "left", "center", "right", "bottom-left", "bottom", "bottom-right",
570
+ ]);
571
+
572
+ /** The `aspect` vocabulary as CSS aspect-ratio values. */
573
+ const STACK_ASPECTS: Record<string, string> = {
574
+ "16:9": "16/9",
575
+ "4:3": "4/3",
576
+ "3:2": "3/2",
577
+ "1:1": "1/1",
578
+ card: "5/7",
579
+ };
580
+
581
+ /** :::stack - every direct child is a layer, source order is z-order; the
582
+ * stylesheet puts every layer in one grid cell (SPEC.md, "Stacks"). Layers
583
+ * past the cap render in flow below the box. */
584
+ function renderStack(node: Node & { type: "directive" }, ctx: Ctx, key: string): ReactNode {
585
+ const style: Record<string, string> = {};
586
+ const aspectAttr = node.attrs["aspect"] ?? "";
587
+ if (Object.hasOwn(STACK_ASPECTS, aspectAttr)) {
588
+ style["--mq-stack-aspect"] = STACK_ASPECTS[aspectAttr]!;
589
+ }
590
+ const w = mediaSize(node.attrs["width"]);
591
+ if (w !== null) {
592
+ style["--mq-stack-w"] = w;
593
+ }
594
+ const layers = node.children.slice(0, MAX_LAYERS).map((child, i) => renderLayer(child, ctx, String(i)));
595
+ // The class tells the stylesheet a cover base fills the box rather than
596
+ // sizing it.
597
+ const className = style["--mq-stack-aspect"] === undefined ? "mq-stack" : "mq-stack mq-stack-aspect";
598
+ const stack = h("div", nodeProps(node, ctx, { key, className, style }), ...layers);
599
+ const rest = node.children.slice(MAX_LAYERS);
600
+ if (rest.length === 0) {
601
+ return stack;
602
+ }
603
+ return h(
604
+ "div",
605
+ { key, style: { display: "contents" } },
606
+ stack,
607
+ h("div", { key: "rest", className: "mq-stack-rest" }, ...renderChildren(rest, ctx)),
608
+ );
609
+ }
610
+
611
+ /** One layer of a stack: a `:::layer` child carries knobs; any other child
612
+ * block is a layer with the defaults (`fill`, no backing). */
613
+ function renderLayer(child: Node, ctx: Ctx, key: string): ReactNode {
614
+ const isLayer = child.type === "directive" && child.name === "layer";
615
+ const attrs: Attrs = isLayer ? child.attrs : {};
616
+ const content: Node[] = isLayer ? child.children : [child];
617
+ const place = PLACES.has(attrs["place"] ?? "") ? attrs["place"]! : "fill";
618
+ const backing = attrs["backing"] === "box" || attrs["backing"] === "outline" ? ` mq-backing-${attrs["backing"]}` : "";
619
+ // A fill layer that is exactly one embed is that media covering the box -
620
+ // the one crop the spec permits, of pictures, never of words.
621
+ const only = content.length === 1 ? content[0]! : undefined;
622
+ const cover =
623
+ place === "fill" && only?.type === "paragraph" && only.children.length === 1 && only.children[0]!.type === "embed"
624
+ ? " mq-layer-cover"
625
+ : "";
626
+ const props = {
627
+ key,
628
+ className: `mq-layer mq-place-${place}${backing}${cover}${schemeClass(attrs)}${fontClass(attrs)}`,
629
+ style: styleVars(attrs, ctx.profile),
630
+ };
631
+ return h("div", isLayer ? nodeProps(child, ctx, props) : props, ...renderChildren(content, ctx));
632
+ }
633
+
533
634
  function sizeRung(value: string, inner: ReactNode[], node: Node, ctx: Ctx, key: string): ReactNode {
534
635
  // <font> is deliberate: presentational attributes are outside CSP's
535
636
  // jurisdiction, so size survives with no stylesheet at all.
@@ -651,122 +752,29 @@ function span(node: Node & { type: "span" }, ctx: Ctx, key: string): ReactNode {
651
752
  return h("span", nodeProps(node, ctx, { key, style: { display: "contents" } }), ...kids);
652
753
  }
653
754
 
654
- // -- effects, including the per-unit (by=letter / by=word) machinery. The
655
- // offsets mirror the static renderer's exactly, so a document animates the
656
- // same in both - deterministic, never random.
657
-
658
- const SEGMENTERS = {
659
- letter: new Intl.Segmenter("en", { granularity: "grapheme" }),
660
- word: new Intl.Segmenter("en", { granularity: "word" }),
661
- };
662
-
663
- type SplitBy = keyof typeof SEGMENTERS;
664
-
665
- const MAX_SPLIT_UNITS = 400;
666
- const MAX_REVEAL_UNITS = 2000;
667
-
668
- function isReveal(effectName: string): boolean {
669
- return effectName === "typewriter" || effectName === "fadein";
670
- }
671
-
672
- function revealStep(speedAttr: string | undefined, dflt: number): number {
673
- const speed =
674
- speedAttr !== undefined && COUNT.test(speedAttr) && Number(speedAttr) > 0
675
- ? Number(speedAttr)
676
- : dflt;
677
- return Math.round(1000 / speed) / 1000;
678
- }
679
-
680
- function gcd(a: number, b: number): number {
681
- return b === 0 ? a : gcd(b, a % b);
682
- }
683
-
684
- function scatterStride(total: number): number {
685
- let stride = Math.max(1, Math.round(total * 0.618));
686
- while (stride < total && gcd(stride, total) !== 1) {
687
- stride += 1;
688
- }
689
- return gcd(stride, total) === 1 ? stride : 1;
690
- }
755
+ // -- effects: the look (classes, knobs, the per-unit split and its word
756
+ // groups) comes from the static renderer's effects.ts, so a document
757
+ // animates the same in both - deterministic, never random.
691
758
 
692
- interface SplitState {
693
- effect: string;
694
- by: SplitBy;
695
- phase: "ramp" | "scatter";
696
- i: number;
697
- total: number;
698
- }
699
-
700
- function unitOffset(state: SplitState): string {
701
- if (isReveal(state.effect)) {
702
- const o =
703
- state.phase === "scatter" ? (state.i * scatterStride(state.total)) % state.total : state.i;
704
- return String(o);
705
- }
706
- let o: number;
707
- if (state.phase === "scatter") {
708
- o = ((state.i * 7919) % 101) / 101;
709
- } else {
710
- switch (state.effect) {
711
- case "rainbow":
712
- o = state.i / state.total;
713
- break;
714
- case "bounce":
715
- o = (state.i % 6) / 6;
716
- break;
717
- default:
718
- o = (state.i % 8) / 8;
719
- break;
720
- }
721
- }
722
- return String(Math.round(o * 1000) / 1000);
723
- }
724
-
725
- function isUnit(seg: Intl.SegmentData, by: SplitBy): boolean {
726
- return by === "word" ? seg.isWordLike === true : !/^\s+$/.test(seg.segment);
727
- }
728
-
729
- function countUnits(nodes: Node[], by: SplitBy): number {
730
- let n = 0;
731
- for (const node of nodes) {
732
- if (node.type === "text") {
733
- for (const seg of SEGMENTERS[by].segment(node.value)) {
734
- if (isUnit(seg, by)) {
735
- n += 1;
736
- }
737
- }
738
- } else if (node.type === "emphasis" || node.type === "strong" || node.type === "strikethrough") {
739
- n += countUnits(node.children, by);
740
- }
741
- }
742
- return n;
743
- }
744
-
745
- function splitRender(nodes: Node[], ctx: Ctx, state: SplitState): ReactNode[] {
759
+ function splitRender(nodes: Node[], ctx: Ctx, look: EffectLook): ReactNode[] {
760
+ const pieces = (list: SplitPiece[], prefix: string): ReactNode[] =>
761
+ list.map((p, i) =>
762
+ p.kind === "text"
763
+ ? p.text // spaces and punctuation ride along
764
+ : p.kind === "unit"
765
+ ? h("span", { key: `${prefix}${i}`, className: "mq-l", style: { "--mq-o": p.offset } as Record<string, string> }, p.text)
766
+ : h("span", { key: `${prefix}${i}`, className: "mq-w" }, ...pieces(p.pieces, `${prefix}${i}-`)),
767
+ );
746
768
  const out: ReactNode[] = [];
747
769
  nodes.forEach((node, ni) => {
748
770
  if (node.type === "text") {
749
- for (const seg of SEGMENTERS[state.by].segment(node.value)) {
750
- if (!isUnit(seg, state.by)) {
751
- out.push(seg.segment); // spaces and punctuation ride along
752
- continue;
753
- }
754
- const o = unitOffset(state);
755
- state.i += 1;
756
- out.push(
757
- h(
758
- "span",
759
- { key: `${ni}-${state.i}`, className: "mq-l", style: { "--mq-o": o } as Record<string, string> },
760
- seg.segment,
761
- ),
762
- );
763
- }
771
+ out.push(...pieces(splitPieces(node, look), `${ni}-`));
764
772
  } else if (node.type === "emphasis") {
765
- out.push(h("em", { key: ni }, ...splitRender(node.children, ctx, state)));
773
+ out.push(h("em", { key: ni }, ...splitRender(node.children, ctx, look)));
766
774
  } else if (node.type === "strong") {
767
- out.push(h("strong", { key: ni }, ...splitRender(node.children, ctx, state)));
775
+ out.push(h("strong", { key: ni }, ...splitRender(node.children, ctx, look)));
768
776
  } else if (node.type === "strikethrough") {
769
- out.push(h("del", { key: ni }, ...splitRender(node.children, ctx, state)));
777
+ out.push(h("del", { key: ni }, ...splitRender(node.children, ctx, look)));
770
778
  } else {
771
779
  out.push(renderNode(node, ctx, String(ni))); // anything else renders whole
772
780
  }
@@ -775,58 +783,13 @@ function splitRender(nodes: Node[], ctx: Ctx, state: SplitState): ReactNode[] {
775
783
  }
776
784
 
777
785
  function effect(node: Node & { type: "span" }, ctx: Ctx, key: string): ReactNode {
778
- const { name, attrs } = node;
779
- const byAttr = attrs["by"];
780
- const splitBy: SplitBy | null =
781
- byAttr === "letter" || byAttr === "word"
782
- ? byAttr
783
- : name === "typewriter"
784
- ? "letter" // typewriter is per-unit by nature
785
- : null;
786
-
787
- const style: Record<string, string> = {};
788
- if (name === "blink" && attrs["rate"] !== undefined && COUNT.test(attrs["rate"])) {
789
- style["--mq-rate"] = attrs["rate"];
790
- }
791
- if (name === "typewriter") {
792
- style["--mq-tw-step"] = `${revealStep(attrs["speed"], 14)}s`;
793
- }
794
- if (name === "fadein" && splitBy !== null) {
795
- style["--mq-fi-step"] = `${revealStep(attrs["speed"], 16)}s`;
796
- }
797
-
798
- const className = `mq-${name} ${ANIM_CLASS}`;
799
-
800
- if (splitBy === null) {
801
- return h(
802
- "span",
803
- nodeProps(node, ctx, { key, className, style }),
804
- ...renderChildren(node.children, ctx),
805
- );
806
- }
807
-
808
- const phase: "ramp" | "scatter" =
809
- attrs["phase"] === "scatter" || attrs["phase"] === "ramp"
810
- ? attrs["phase"]
811
- : name === "jitter"
812
- ? "scatter"
813
- : "ramp";
814
- const total = countUnits(node.children, splitBy);
815
- const cap = isReveal(name) ? MAX_REVEAL_UNITS : MAX_SPLIT_UNITS;
816
- if (total === 0 || total > cap) {
817
- // Past the cap: the run animates whole. DOM weight discipline.
818
- return h(
819
- "span",
820
- nodeProps(node, ctx, { key, className, style }),
821
- ...renderChildren(node.children, ctx),
822
- );
823
- }
824
- const state: SplitState = { effect: name, by: splitBy, phase, i: 0, total };
825
- return h(
826
- "span",
827
- nodeProps(node, ctx, { key, className: `${className} mq-split`, style }),
828
- ...splitRender(node.children, ctx, state),
829
- );
786
+ // The look - classes, knobs, the per-unit split and its word groups - is
787
+ // the HTML renderer's own (effects.ts), so the two can't disagree.
788
+ const look = effectLook(node.name, node.attrs, node.children)!;
789
+ const style: Record<string, string> = Object.fromEntries(look.vars);
790
+ const className = `mq-${node.name} ${ANIM_CLASS}${look.units === null ? "" : " mq-split"}`;
791
+ const kids = look.units === null ? renderChildren(node.children, ctx) : splitRender(node.children, ctx, look);
792
+ return h("span", nodeProps(node, ctx, { key, className, style }), ...kids);
830
793
  }
831
794
 
832
795
  export { ANIMATED };