@cube-drone/marquee-react-renderer 0.9.0 → 0.9.2
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/render.js +109 -122
- package/package.json +7 -7
- package/src/render.ts +130 -164
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) {
|
|
@@ -241,17 +245,20 @@ function embed(node, ctx, key) {
|
|
|
241
245
|
return h("span", nodeProps(node, ctx, { key, className: "mq-placeholder" }), node.alt === "" ? node.target : node.alt);
|
|
242
246
|
}
|
|
243
247
|
const common = { key, className: "mq-embed" };
|
|
248
|
+
// Audio and video have no alt attribute; the description labels them for
|
|
249
|
+
// assistive tech instead, as the static renderer does.
|
|
250
|
+
const label = { "aria-label": node.alt === "" ? undefined : node.alt };
|
|
244
251
|
if (media.kind === "image") {
|
|
245
252
|
return h("img", nodeProps(node, ctx, { ...common, src: media.url, alt: node.alt, loading: "lazy" }));
|
|
246
253
|
}
|
|
247
254
|
if (media.kind === "audio") {
|
|
248
|
-
return h("audio", nodeProps(node, ctx, { ...common, src: media.url, controls: true }));
|
|
255
|
+
return h("audio", nodeProps(node, ctx, { ...common, ...label, src: media.url, controls: true }));
|
|
249
256
|
}
|
|
250
257
|
if (media.loop === true) {
|
|
251
258
|
// A silent animation: the gif's own manners - looping, muted, autoplaying, no controls.
|
|
252
|
-
return h("video", nodeProps(node, ctx, { ...common, src: media.url, autoPlay: true, loop: true, muted: true, playsInline: true }));
|
|
259
|
+
return h("video", nodeProps(node, ctx, { ...common, ...label, src: media.url, autoPlay: true, loop: true, muted: true, playsInline: true }));
|
|
253
260
|
}
|
|
254
|
-
return h("video", nodeProps(node, ctx, { ...common, src: media.url, controls: true }));
|
|
261
|
+
return h("video", nodeProps(node, ctx, { ...common, ...label, src: media.url, controls: true }));
|
|
255
262
|
}
|
|
256
263
|
const TURBOLINK_LEVELS = new Set(["full", "title", "bare"]);
|
|
257
264
|
function turbolink(node, target, levelAttr, ctx, key) {
|
|
@@ -274,6 +281,14 @@ function turbolink(node, target, levelAttr, ctx, key) {
|
|
|
274
281
|
}
|
|
275
282
|
function directive(node, ctx, key) {
|
|
276
283
|
const { name, attrs } = node;
|
|
284
|
+
// The hooks see the children rendered; built-ins that lay their children
|
|
285
|
+
// out themselves (table, stack) render them again, so rewind the aside
|
|
286
|
+
// numbering first - or a note inside counts twice.
|
|
287
|
+
const saved = { n: ctx.note.n, pending: [...ctx.note.pending] };
|
|
288
|
+
const rewind = () => {
|
|
289
|
+
ctx.note.n = saved.n;
|
|
290
|
+
ctx.note.pending = [...saved.pending];
|
|
291
|
+
};
|
|
277
292
|
const kids = renderChildren(node.children, ctx);
|
|
278
293
|
const custom = ctx.hooks.directive?.(name, attrs, kids);
|
|
279
294
|
if (custom !== undefined && custom !== null) {
|
|
@@ -321,7 +336,19 @@ function directive(node, ctx, key) {
|
|
|
321
336
|
return h("div", nodeProps(node, ctx, { key, className: "mq-media", style }), ...kids);
|
|
322
337
|
}
|
|
323
338
|
case "table":
|
|
339
|
+
rewind();
|
|
324
340
|
return renderTable(node, ctx, key);
|
|
341
|
+
case "stack":
|
|
342
|
+
rewind();
|
|
343
|
+
return renderStack(node, ctx, key);
|
|
344
|
+
case "layer":
|
|
345
|
+
// Outside a stack a layer is just a section-like container: its style
|
|
346
|
+
// knobs apply, place/backing mean nothing.
|
|
347
|
+
return h("div", nodeProps(node, ctx, {
|
|
348
|
+
key,
|
|
349
|
+
className: `mq-layer${schemeClass(attrs)}${fontClass(attrs)}`,
|
|
350
|
+
style: styleVars(attrs, ctx.profile),
|
|
351
|
+
}), ...kids);
|
|
325
352
|
case "center":
|
|
326
353
|
case "right":
|
|
327
354
|
case "left":
|
|
@@ -393,6 +420,64 @@ function renderTable(node, ctx, key) {
|
|
|
393
420
|
const table = h("table", nodeProps(node, ctx, { key, className: "mq-table" }), h("tbody", null, rows));
|
|
394
421
|
return withNotes(ctx, table, key);
|
|
395
422
|
}
|
|
423
|
+
/** A stack paints at most this many layers; the rest flow below the box. */
|
|
424
|
+
const MAX_LAYERS = 8;
|
|
425
|
+
const PLACES = new Set([
|
|
426
|
+
"fill", "top-left", "top", "top-right", "left", "center", "right", "bottom-left", "bottom", "bottom-right",
|
|
427
|
+
]);
|
|
428
|
+
/** The `aspect` vocabulary as CSS aspect-ratio values. */
|
|
429
|
+
const STACK_ASPECTS = {
|
|
430
|
+
"16:9": "16/9",
|
|
431
|
+
"4:3": "4/3",
|
|
432
|
+
"3:2": "3/2",
|
|
433
|
+
"1:1": "1/1",
|
|
434
|
+
card: "5/7",
|
|
435
|
+
};
|
|
436
|
+
/** :::stack - every direct child is a layer, source order is z-order; the
|
|
437
|
+
* stylesheet puts every layer in one grid cell (SPEC.md, "Stacks"). Layers
|
|
438
|
+
* past the cap render in flow below the box. */
|
|
439
|
+
function renderStack(node, ctx, key) {
|
|
440
|
+
const style = {};
|
|
441
|
+
const aspectAttr = node.attrs["aspect"] ?? "";
|
|
442
|
+
if (Object.hasOwn(STACK_ASPECTS, aspectAttr)) {
|
|
443
|
+
style["--mq-stack-aspect"] = STACK_ASPECTS[aspectAttr];
|
|
444
|
+
}
|
|
445
|
+
const w = mediaSize(node.attrs["width"]);
|
|
446
|
+
if (w !== null) {
|
|
447
|
+
style["--mq-stack-w"] = w;
|
|
448
|
+
}
|
|
449
|
+
const layers = node.children.slice(0, MAX_LAYERS).map((child, i) => renderLayer(child, ctx, String(i)));
|
|
450
|
+
// The class tells the stylesheet a cover base fills the box rather than
|
|
451
|
+
// sizing it.
|
|
452
|
+
const className = style["--mq-stack-aspect"] === undefined ? "mq-stack" : "mq-stack mq-stack-aspect";
|
|
453
|
+
const stack = h("div", nodeProps(node, ctx, { key, className, style }), ...layers);
|
|
454
|
+
const rest = node.children.slice(MAX_LAYERS);
|
|
455
|
+
if (rest.length === 0) {
|
|
456
|
+
return stack;
|
|
457
|
+
}
|
|
458
|
+
return h("div", { key, style: { display: "contents" } }, stack, h("div", { key: "rest", className: "mq-stack-rest" }, ...renderChildren(rest, ctx)));
|
|
459
|
+
}
|
|
460
|
+
/** One layer of a stack: a `:::layer` child carries knobs; any other child
|
|
461
|
+
* block is a layer with the defaults (`fill`, no backing). */
|
|
462
|
+
function renderLayer(child, ctx, key) {
|
|
463
|
+
const isLayer = child.type === "directive" && child.name === "layer";
|
|
464
|
+
const attrs = isLayer ? child.attrs : {};
|
|
465
|
+
const content = isLayer ? child.children : [child];
|
|
466
|
+
const place = PLACES.has(attrs["place"] ?? "") ? attrs["place"] : "fill";
|
|
467
|
+
const backing = attrs["backing"] === "box" || attrs["backing"] === "outline" ? ` mq-backing-${attrs["backing"]}` : "";
|
|
468
|
+
// A fill layer that is exactly one embed is that media covering the box -
|
|
469
|
+
// the one crop the spec permits, of pictures, never of words.
|
|
470
|
+
const only = content.length === 1 ? content[0] : undefined;
|
|
471
|
+
const cover = place === "fill" && only?.type === "paragraph" && only.children.length === 1 && only.children[0].type === "embed"
|
|
472
|
+
? " mq-layer-cover"
|
|
473
|
+
: "";
|
|
474
|
+
const props = {
|
|
475
|
+
key,
|
|
476
|
+
className: `mq-layer mq-place-${place}${backing}${cover}${schemeClass(attrs)}${fontClass(attrs)}`,
|
|
477
|
+
style: styleVars(attrs, ctx.profile),
|
|
478
|
+
};
|
|
479
|
+
return h("div", isLayer ? nodeProps(child, ctx, props) : props, ...renderChildren(content, ctx));
|
|
480
|
+
}
|
|
396
481
|
function sizeRung(value, inner, node, ctx, key) {
|
|
397
482
|
// <font> is deliberate: presentational attributes are outside CSP's
|
|
398
483
|
// jurisdiction, so size survives with no stylesheet at all.
|
|
@@ -492,99 +577,28 @@ function span(node, ctx, key) {
|
|
|
492
577
|
// Unknown span: pure shrug, children as plain content.
|
|
493
578
|
return h("span", nodeProps(node, ctx, { key, style: { display: "contents" } }), ...kids);
|
|
494
579
|
}
|
|
495
|
-
// -- effects,
|
|
496
|
-
//
|
|
497
|
-
// same in both - deterministic, never random.
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
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) {
|
|
580
|
+
// -- effects: the look (classes, knobs, the per-unit split and its word
|
|
581
|
+
// groups) comes from the static renderer's effects.ts, so a document
|
|
582
|
+
// animates the same in both - deterministic, never random.
|
|
583
|
+
function splitRender(nodes, ctx, look) {
|
|
584
|
+
const pieces = (list, prefix) => list.map((p, i) => p.kind === "text"
|
|
585
|
+
? p.text // spaces and punctuation ride along
|
|
586
|
+
: p.kind === "unit"
|
|
587
|
+
? h("span", { key: `${prefix}${i}`, className: "mq-l", style: { "--mq-o": p.offset } }, p.text)
|
|
588
|
+
: h("span", { key: `${prefix}${i}`, className: "mq-w" }, ...pieces(p.pieces, `${prefix}${i}-`)));
|
|
567
589
|
const out = [];
|
|
568
590
|
nodes.forEach((node, ni) => {
|
|
569
591
|
if (node.type === "text") {
|
|
570
|
-
|
|
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
|
-
}
|
|
592
|
+
out.push(...pieces(splitPieces(node, look), `${ni}-`));
|
|
579
593
|
}
|
|
580
594
|
else if (node.type === "emphasis") {
|
|
581
|
-
out.push(h("em", { key: ni }, ...splitRender(node.children, ctx,
|
|
595
|
+
out.push(h("em", { key: ni }, ...splitRender(node.children, ctx, look)));
|
|
582
596
|
}
|
|
583
597
|
else if (node.type === "strong") {
|
|
584
|
-
out.push(h("strong", { key: ni }, ...splitRender(node.children, ctx,
|
|
598
|
+
out.push(h("strong", { key: ni }, ...splitRender(node.children, ctx, look)));
|
|
585
599
|
}
|
|
586
600
|
else if (node.type === "strikethrough") {
|
|
587
|
-
out.push(h("del", { key: ni }, ...splitRender(node.children, ctx,
|
|
601
|
+
out.push(h("del", { key: ni }, ...splitRender(node.children, ctx, look)));
|
|
588
602
|
}
|
|
589
603
|
else {
|
|
590
604
|
out.push(renderNode(node, ctx, String(ni))); // anything else renders whole
|
|
@@ -593,39 +607,12 @@ function splitRender(nodes, ctx, state) {
|
|
|
593
607
|
return out;
|
|
594
608
|
}
|
|
595
609
|
function effect(node, ctx, key) {
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
const
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
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));
|
|
610
|
+
// The look - classes, knobs, the per-unit split and its word groups - is
|
|
611
|
+
// the HTML renderer's own (effects.ts), so the two can't disagree.
|
|
612
|
+
const look = effectLook(node.name, node.attrs, node.children);
|
|
613
|
+
const style = Object.fromEntries(look.vars);
|
|
614
|
+
const className = `mq-${node.name} ${ANIM_CLASS}${look.units === null ? "" : " mq-split"}`;
|
|
615
|
+
const kids = look.units === null ? renderChildren(node.children, ctx) : splitRender(node.children, ctx, look);
|
|
616
|
+
return h("span", nodeProps(node, ctx, { key, className, style }), ...kids);
|
|
630
617
|
}
|
|
631
618
|
export { ANIMATED };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cube-drone/marquee-react-renderer",
|
|
3
|
-
"version": "0.9.
|
|
3
|
+
"version": "0.9.2",
|
|
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.
|
|
38
|
-
"@cube-drone/marquee-parser": "^0.9.
|
|
37
|
+
"@cube-drone/marquee-html-renderer": "^0.9.2",
|
|
38
|
+
"@cube-drone/marquee-parser": "^0.9.2"
|
|
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.
|
|
52
|
-
"@cube-drone/marquee-fonts": "^0.9.
|
|
53
|
-
"@cube-drone/marquee-emoji": "^0.9.
|
|
54
|
-
"@cube-drone/marquee-turbolink": "^0.9.
|
|
51
|
+
"@cube-drone/marquee-css": "^0.9.2",
|
|
52
|
+
"@cube-drone/marquee-fonts": "^0.9.2",
|
|
53
|
+
"@cube-drone/marquee-emoji": "^0.9.2",
|
|
54
|
+
"@cube-drone/marquee-turbolink": "^0.9.2"
|
|
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
|
|
|
@@ -324,20 +332,23 @@ function embed(node: Node & { type: "embed" }, ctx: Ctx, key: string): ReactNode
|
|
|
324
332
|
);
|
|
325
333
|
}
|
|
326
334
|
const common = { key, className: "mq-embed" };
|
|
335
|
+
// Audio and video have no alt attribute; the description labels them for
|
|
336
|
+
// assistive tech instead, as the static renderer does.
|
|
337
|
+
const label = { "aria-label": node.alt === "" ? undefined : node.alt };
|
|
327
338
|
if (media.kind === "image") {
|
|
328
339
|
return h("img", nodeProps(node, ctx, { ...common, src: media.url, alt: node.alt, loading: "lazy" }));
|
|
329
340
|
}
|
|
330
341
|
if (media.kind === "audio") {
|
|
331
|
-
return h("audio", nodeProps(node, ctx, { ...common, src: media.url, controls: true }));
|
|
342
|
+
return h("audio", nodeProps(node, ctx, { ...common, ...label, src: media.url, controls: true }));
|
|
332
343
|
}
|
|
333
344
|
if (media.loop === true) {
|
|
334
345
|
// A silent animation: the gif's own manners - looping, muted, autoplaying, no controls.
|
|
335
346
|
return h(
|
|
336
347
|
"video",
|
|
337
|
-
nodeProps(node, ctx, { ...common, src: media.url, autoPlay: true, loop: true, muted: true, playsInline: true }),
|
|
348
|
+
nodeProps(node, ctx, { ...common, ...label, src: media.url, autoPlay: true, loop: true, muted: true, playsInline: true }),
|
|
338
349
|
);
|
|
339
350
|
}
|
|
340
|
-
return h("video", nodeProps(node, ctx, { ...common, src: media.url, controls: true }));
|
|
351
|
+
return h("video", nodeProps(node, ctx, { ...common, ...label, src: media.url, controls: true }));
|
|
341
352
|
}
|
|
342
353
|
|
|
343
354
|
const TURBOLINK_LEVELS = new Set<TurbolinkLevel>(["full", "title", "bare"]);
|
|
@@ -379,6 +390,14 @@ function turbolink(
|
|
|
379
390
|
|
|
380
391
|
function directive(node: Node & { type: "directive" }, ctx: Ctx, key: string): ReactNode {
|
|
381
392
|
const { name, attrs } = node;
|
|
393
|
+
// The hooks see the children rendered; built-ins that lay their children
|
|
394
|
+
// out themselves (table, stack) render them again, so rewind the aside
|
|
395
|
+
// numbering first - or a note inside counts twice.
|
|
396
|
+
const saved = { n: ctx.note.n, pending: [...ctx.note.pending] };
|
|
397
|
+
const rewind = (): void => {
|
|
398
|
+
ctx.note.n = saved.n;
|
|
399
|
+
ctx.note.pending = [...saved.pending];
|
|
400
|
+
};
|
|
382
401
|
const kids = renderChildren(node.children, ctx);
|
|
383
402
|
|
|
384
403
|
const custom = ctx.hooks.directive?.(name, attrs, kids);
|
|
@@ -440,7 +459,23 @@ function directive(node: Node & { type: "directive" }, ctx: Ctx, key: string): R
|
|
|
440
459
|
return h("div", nodeProps(node, ctx, { key, className: "mq-media", style }), ...kids);
|
|
441
460
|
}
|
|
442
461
|
case "table":
|
|
462
|
+
rewind();
|
|
443
463
|
return renderTable(node, ctx, key);
|
|
464
|
+
case "stack":
|
|
465
|
+
rewind();
|
|
466
|
+
return renderStack(node, ctx, key);
|
|
467
|
+
case "layer":
|
|
468
|
+
// Outside a stack a layer is just a section-like container: its style
|
|
469
|
+
// knobs apply, place/backing mean nothing.
|
|
470
|
+
return h(
|
|
471
|
+
"div",
|
|
472
|
+
nodeProps(node, ctx, {
|
|
473
|
+
key,
|
|
474
|
+
className: `mq-layer${schemeClass(attrs)}${fontClass(attrs)}`,
|
|
475
|
+
style: styleVars(attrs, ctx.profile),
|
|
476
|
+
}),
|
|
477
|
+
...kids,
|
|
478
|
+
);
|
|
444
479
|
case "center":
|
|
445
480
|
case "right":
|
|
446
481
|
case "left":
|
|
@@ -530,6 +565,75 @@ function renderTable(node: Node & { type: "directive" }, ctx: Ctx, key: string):
|
|
|
530
565
|
return withNotes(ctx, table, key);
|
|
531
566
|
}
|
|
532
567
|
|
|
568
|
+
/** A stack paints at most this many layers; the rest flow below the box. */
|
|
569
|
+
const MAX_LAYERS = 8;
|
|
570
|
+
|
|
571
|
+
const PLACES = new Set([
|
|
572
|
+
"fill", "top-left", "top", "top-right", "left", "center", "right", "bottom-left", "bottom", "bottom-right",
|
|
573
|
+
]);
|
|
574
|
+
|
|
575
|
+
/** The `aspect` vocabulary as CSS aspect-ratio values. */
|
|
576
|
+
const STACK_ASPECTS: Record<string, string> = {
|
|
577
|
+
"16:9": "16/9",
|
|
578
|
+
"4:3": "4/3",
|
|
579
|
+
"3:2": "3/2",
|
|
580
|
+
"1:1": "1/1",
|
|
581
|
+
card: "5/7",
|
|
582
|
+
};
|
|
583
|
+
|
|
584
|
+
/** :::stack - every direct child is a layer, source order is z-order; the
|
|
585
|
+
* stylesheet puts every layer in one grid cell (SPEC.md, "Stacks"). Layers
|
|
586
|
+
* past the cap render in flow below the box. */
|
|
587
|
+
function renderStack(node: Node & { type: "directive" }, ctx: Ctx, key: string): ReactNode {
|
|
588
|
+
const style: Record<string, string> = {};
|
|
589
|
+
const aspectAttr = node.attrs["aspect"] ?? "";
|
|
590
|
+
if (Object.hasOwn(STACK_ASPECTS, aspectAttr)) {
|
|
591
|
+
style["--mq-stack-aspect"] = STACK_ASPECTS[aspectAttr]!;
|
|
592
|
+
}
|
|
593
|
+
const w = mediaSize(node.attrs["width"]);
|
|
594
|
+
if (w !== null) {
|
|
595
|
+
style["--mq-stack-w"] = w;
|
|
596
|
+
}
|
|
597
|
+
const layers = node.children.slice(0, MAX_LAYERS).map((child, i) => renderLayer(child, ctx, String(i)));
|
|
598
|
+
// The class tells the stylesheet a cover base fills the box rather than
|
|
599
|
+
// sizing it.
|
|
600
|
+
const className = style["--mq-stack-aspect"] === undefined ? "mq-stack" : "mq-stack mq-stack-aspect";
|
|
601
|
+
const stack = h("div", nodeProps(node, ctx, { key, className, style }), ...layers);
|
|
602
|
+
const rest = node.children.slice(MAX_LAYERS);
|
|
603
|
+
if (rest.length === 0) {
|
|
604
|
+
return stack;
|
|
605
|
+
}
|
|
606
|
+
return h(
|
|
607
|
+
"div",
|
|
608
|
+
{ key, style: { display: "contents" } },
|
|
609
|
+
stack,
|
|
610
|
+
h("div", { key: "rest", className: "mq-stack-rest" }, ...renderChildren(rest, ctx)),
|
|
611
|
+
);
|
|
612
|
+
}
|
|
613
|
+
|
|
614
|
+
/** One layer of a stack: a `:::layer` child carries knobs; any other child
|
|
615
|
+
* block is a layer with the defaults (`fill`, no backing). */
|
|
616
|
+
function renderLayer(child: Node, ctx: Ctx, key: string): ReactNode {
|
|
617
|
+
const isLayer = child.type === "directive" && child.name === "layer";
|
|
618
|
+
const attrs: Attrs = isLayer ? child.attrs : {};
|
|
619
|
+
const content: Node[] = isLayer ? child.children : [child];
|
|
620
|
+
const place = PLACES.has(attrs["place"] ?? "") ? attrs["place"]! : "fill";
|
|
621
|
+
const backing = attrs["backing"] === "box" || attrs["backing"] === "outline" ? ` mq-backing-${attrs["backing"]}` : "";
|
|
622
|
+
// A fill layer that is exactly one embed is that media covering the box -
|
|
623
|
+
// the one crop the spec permits, of pictures, never of words.
|
|
624
|
+
const only = content.length === 1 ? content[0]! : undefined;
|
|
625
|
+
const cover =
|
|
626
|
+
place === "fill" && only?.type === "paragraph" && only.children.length === 1 && only.children[0]!.type === "embed"
|
|
627
|
+
? " mq-layer-cover"
|
|
628
|
+
: "";
|
|
629
|
+
const props = {
|
|
630
|
+
key,
|
|
631
|
+
className: `mq-layer mq-place-${place}${backing}${cover}${schemeClass(attrs)}${fontClass(attrs)}`,
|
|
632
|
+
style: styleVars(attrs, ctx.profile),
|
|
633
|
+
};
|
|
634
|
+
return h("div", isLayer ? nodeProps(child, ctx, props) : props, ...renderChildren(content, ctx));
|
|
635
|
+
}
|
|
636
|
+
|
|
533
637
|
function sizeRung(value: string, inner: ReactNode[], node: Node, ctx: Ctx, key: string): ReactNode {
|
|
534
638
|
// <font> is deliberate: presentational attributes are outside CSP's
|
|
535
639
|
// jurisdiction, so size survives with no stylesheet at all.
|
|
@@ -651,122 +755,29 @@ function span(node: Node & { type: "span" }, ctx: Ctx, key: string): ReactNode {
|
|
|
651
755
|
return h("span", nodeProps(node, ctx, { key, style: { display: "contents" } }), ...kids);
|
|
652
756
|
}
|
|
653
757
|
|
|
654
|
-
// -- effects,
|
|
655
|
-
//
|
|
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
|
-
}
|
|
758
|
+
// -- effects: the look (classes, knobs, the per-unit split and its word
|
|
759
|
+
// groups) comes from the static renderer's effects.ts, so a document
|
|
760
|
+
// animates the same in both - deterministic, never random.
|
|
691
761
|
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
}
|
|
699
|
-
|
|
700
|
-
|
|
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[] {
|
|
762
|
+
function splitRender(nodes: Node[], ctx: Ctx, look: EffectLook): ReactNode[] {
|
|
763
|
+
const pieces = (list: SplitPiece[], prefix: string): ReactNode[] =>
|
|
764
|
+
list.map((p, i) =>
|
|
765
|
+
p.kind === "text"
|
|
766
|
+
? p.text // spaces and punctuation ride along
|
|
767
|
+
: p.kind === "unit"
|
|
768
|
+
? h("span", { key: `${prefix}${i}`, className: "mq-l", style: { "--mq-o": p.offset } as Record<string, string> }, p.text)
|
|
769
|
+
: h("span", { key: `${prefix}${i}`, className: "mq-w" }, ...pieces(p.pieces, `${prefix}${i}-`)),
|
|
770
|
+
);
|
|
746
771
|
const out: ReactNode[] = [];
|
|
747
772
|
nodes.forEach((node, ni) => {
|
|
748
773
|
if (node.type === "text") {
|
|
749
|
-
|
|
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
|
-
}
|
|
774
|
+
out.push(...pieces(splitPieces(node, look), `${ni}-`));
|
|
764
775
|
} else if (node.type === "emphasis") {
|
|
765
|
-
out.push(h("em", { key: ni }, ...splitRender(node.children, ctx,
|
|
776
|
+
out.push(h("em", { key: ni }, ...splitRender(node.children, ctx, look)));
|
|
766
777
|
} else if (node.type === "strong") {
|
|
767
|
-
out.push(h("strong", { key: ni }, ...splitRender(node.children, ctx,
|
|
778
|
+
out.push(h("strong", { key: ni }, ...splitRender(node.children, ctx, look)));
|
|
768
779
|
} else if (node.type === "strikethrough") {
|
|
769
|
-
out.push(h("del", { key: ni }, ...splitRender(node.children, ctx,
|
|
780
|
+
out.push(h("del", { key: ni }, ...splitRender(node.children, ctx, look)));
|
|
770
781
|
} else {
|
|
771
782
|
out.push(renderNode(node, ctx, String(ni))); // anything else renders whole
|
|
772
783
|
}
|
|
@@ -775,58 +786,13 @@ function splitRender(nodes: Node[], ctx: Ctx, state: SplitState): ReactNode[] {
|
|
|
775
786
|
}
|
|
776
787
|
|
|
777
788
|
function effect(node: Node & { type: "span" }, ctx: Ctx, key: string): ReactNode {
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
const
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
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
|
-
);
|
|
789
|
+
// The look - classes, knobs, the per-unit split and its word groups - is
|
|
790
|
+
// the HTML renderer's own (effects.ts), so the two can't disagree.
|
|
791
|
+
const look = effectLook(node.name, node.attrs, node.children)!;
|
|
792
|
+
const style: Record<string, string> = Object.fromEntries(look.vars);
|
|
793
|
+
const className = `mq-${node.name} ${ANIM_CLASS}${look.units === null ? "" : " mq-split"}`;
|
|
794
|
+
const kids = look.units === null ? renderChildren(node.children, ctx) : splitRender(node.children, ctx, look);
|
|
795
|
+
return h("span", nodeProps(node, ctx, { key, className, style }), ...kids);
|
|
830
796
|
}
|
|
831
797
|
|
|
832
798
|
export { ANIMATED };
|