@liqweed/motion-kit 0.2.0 → 0.4.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/.claude-plugin/marketplace.json +1 -1
- package/.claude-plugin/plugin.json +1 -1
- package/README.md +74 -7
- package/adapters/astro/AnimatedDiagram.astro +150 -24
- package/adapters/element/README.md +49 -0
- package/adapters/element/motion-diagram.mjs +124 -0
- package/adapters/page/README.md +3 -2
- package/adapters/slidev/AnimatedDiagram.vue +24 -0
- package/cli/motion-kit.mjs +77 -79
- package/docs/spec/technique.md +102 -44
- package/lib/build.mjs +26 -12
- package/lib/bundle.mjs +9 -2
- package/lib/check.mjs +15 -2
- package/lib/export.mjs +33 -11
- package/lib/gate.mjs +88 -0
- package/lib/load.mjs +49 -19
- package/package.json +1 -1
- package/runtime/bake.mjs +18 -4
- package/runtime/kit.mjs +59 -0
- package/runtime/page.mjs +52 -0
- package/runtime/player.mjs +375 -127
- package/runtime/scene.mjs +80 -5
- package/runtime/style.mjs +2 -0
- package/runtime/types/hub-spoke.mjs +5 -5
- package/runtime/types/sequence.mjs +3 -3
- package/skills/animated-diagram/SKILL.md +3 -1
- package/skills/animated-diagram/references/custom-scenes.md +58 -8
- package/skills/animated-diagram/references/interactivity.md +50 -7
- package/skills/animated-diagram/references/qc.md +2 -2
- package/skills/animated-diagram/references/targets.md +7 -5
- package/skills/animated-diagram/references/technique.md +5 -0
- package/skills/motion-render/SKILL.md +18 -2
package/cli/motion-kit.mjs
CHANGED
|
@@ -2,16 +2,19 @@
|
|
|
2
2
|
/**
|
|
3
3
|
* motion-kit CLI.
|
|
4
4
|
*
|
|
5
|
-
* motion-kit check <scene...> [--format F] [--locale L] gate (all formats/locales by default)
|
|
5
|
+
* motion-kit check <scene...> [--format F]… [--locale L]… [--style S] gate (all formats/locales by default)
|
|
6
6
|
* motion-kit build <scene> --out x.html [--format F] [--locale L] [--style S] [--no-scrubber] [--no-heading]
|
|
7
7
|
* motion-kit export <scene> --out dir [--format F] [--locale L] [--style S] [--theme light|dark] [--png] [--scale 2] [--qc] [--background]
|
|
8
8
|
* motion-kit bake <scene> --out x.svg [--fps 15] [--theme ...] [--style S]
|
|
9
9
|
* motion-kit video <scene> --out x.mp4|.gif|.webm [--fps 30] [--scale 2] [--hold 1.5] [--sheet sheet.png]
|
|
10
10
|
* motion-kit icon <scene> --out <repo> [--size 512] [--style S] [--no-png] project icon set (assets/, .idea/)
|
|
11
|
-
* motion-kit vendor <dir> [--adapter page|slidev|astro]
|
|
11
|
+
* motion-kit vendor <dir> [--adapter page|slidev|astro|element]
|
|
12
12
|
* motion-kit formats | types
|
|
13
13
|
*
|
|
14
14
|
* <scene> is a spec (.json, any registered type) or a custom module (.mjs).
|
|
15
|
+
* --options O passes scene options to a custom module's factory, unchanged:
|
|
16
|
+
* inline JSON ('{"view":"short"}'), a .json file, or an .mjs module whose
|
|
17
|
+
* default export is the options or ({ format, locale }) => options.
|
|
15
18
|
*/
|
|
16
19
|
import { cpSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
17
20
|
import { dirname, join, resolve } from "node:path";
|
|
@@ -19,13 +22,13 @@ import { fileURLToPath } from "node:url";
|
|
|
19
22
|
import { parseArgs } from "node:util";
|
|
20
23
|
import { bake, bakeStats } from "../runtime/bake.mjs";
|
|
21
24
|
import { FORMATS } from "../runtime/scene.mjs";
|
|
22
|
-
import { checkContrast } from "../runtime/style.mjs";
|
|
23
25
|
import { SCENE_TYPES } from "../runtime/types/index.mjs";
|
|
24
26
|
import { checkScene } from "../lib/check.mjs";
|
|
27
|
+
import { checkFiles } from "../lib/gate.mjs";
|
|
25
28
|
import { buildHtml } from "../lib/build.mjs";
|
|
26
29
|
import { writeIconSet } from "../lib/icon.mjs";
|
|
27
30
|
import { exportSteps } from "../lib/export.mjs";
|
|
28
|
-
import { loadScene, loadStyle } from "../lib/load.mjs";
|
|
31
|
+
import { isModule, loadOptions, loadScene, loadStyle } from "../lib/load.mjs";
|
|
29
32
|
import { rasterizer } from "../lib/raster.mjs";
|
|
30
33
|
import { probe, renderVideo, videoContactSheet } from "../lib/video.mjs";
|
|
31
34
|
|
|
@@ -36,9 +39,10 @@ const { values: a, positionals } = parseArgs({
|
|
|
36
39
|
allowPositionals: true,
|
|
37
40
|
options: {
|
|
38
41
|
out: { type: "string", short: "o" },
|
|
39
|
-
format: { type: "string", short: "f" },
|
|
40
|
-
locale: { type: "string", short: "l" },
|
|
42
|
+
format: { type: "string", short: "f", multiple: true },
|
|
43
|
+
locale: { type: "string", short: "l", multiple: true },
|
|
41
44
|
style: { type: "string", short: "s" },
|
|
45
|
+
options: { type: "string" },
|
|
42
46
|
theme: { type: "string", default: "light" },
|
|
43
47
|
png: { type: "boolean", default: false },
|
|
44
48
|
scale: { type: "string", default: "2" },
|
|
@@ -73,7 +77,15 @@ async function main() {
|
|
|
73
77
|
return;
|
|
74
78
|
}
|
|
75
79
|
const style = loadStyle(a.style);
|
|
76
|
-
|
|
80
|
+
// Commands other than check build one format and locale: the last one given.
|
|
81
|
+
const format = a.format?.at(-1);
|
|
82
|
+
const where = { format: format ?? "16:9", locale: a.locale?.at(-1) ?? "en" };
|
|
83
|
+
const optionsFor = await loadOptions(a.options);
|
|
84
|
+
/** Scene options for one scene file at `at`; specs take none. @param {string} file @param {{format: string, locale: string}} at */
|
|
85
|
+
const sceneOpts = async (file, at = where) => {
|
|
86
|
+
if (optionsFor && !isModule(file)) fail(`${cmd}: --options apply to custom scene modules, not to the spec ${file}`);
|
|
87
|
+
return { ...at, options: optionsFor ? await optionsFor(at) : undefined };
|
|
88
|
+
};
|
|
77
89
|
|
|
78
90
|
switch (cmd) {
|
|
79
91
|
case "formats":
|
|
@@ -84,38 +96,17 @@ async function main() {
|
|
|
84
96
|
return;
|
|
85
97
|
case "check": {
|
|
86
98
|
if (!args.length) fail("check: give at least one scene file");
|
|
87
|
-
const
|
|
88
|
-
for (const p of styleProblems) console.error(`✗ style: ${p}`);
|
|
89
|
-
let bad = styleProblems.length;
|
|
90
|
-
for (const file of args) {
|
|
91
|
-
const formats = a.format ? [a.format] : Object.keys(FORMATS);
|
|
92
|
-
const raw = file.endsWith(".json") ? JSON.parse(readFileSync(file, "utf8")) : null;
|
|
93
|
-
const locales = a.locale ? [a.locale] : raw ? detectLocales(raw) : ["en"];
|
|
94
|
-
for (const format of formats) {
|
|
95
|
-
for (const locale of locales) {
|
|
96
|
-
try {
|
|
97
|
-
const { scene } = await loadScene(file, { format, locale });
|
|
98
|
-
const r = checkScene(scene);
|
|
99
|
-
if (r.ok) console.log(`✓ ${file} · ${format} · ${locale} · ${scene.duration.toFixed(2)} s · ${scene.steps.length} steps`);
|
|
100
|
-
else {
|
|
101
|
-
bad++;
|
|
102
|
-
console.error(`✗ ${file} · ${format} · ${locale}\n - ${r.problems.join("\n - ")}`);
|
|
103
|
-
}
|
|
104
|
-
} catch (e) {
|
|
105
|
-
bad++;
|
|
106
|
-
console.error(`✗ ${file} · ${format} · ${locale}: ${e instanceof Error ? e.message : e}`);
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
}
|
|
99
|
+
const bad = await checkFiles(args, { formats: a.format, locales: a.locale, style, options: optionsFor });
|
|
111
100
|
if (bad) process.exit(1);
|
|
112
101
|
return;
|
|
113
102
|
}
|
|
114
103
|
case "build": {
|
|
115
|
-
const
|
|
104
|
+
const file = need(args[0], "<scene>");
|
|
105
|
+
const sopts = await sceneOpts(file);
|
|
106
|
+
const loaded = await loadScene(file, sopts);
|
|
116
107
|
const r = checkScene(loaded.scene);
|
|
117
108
|
if (!r.ok) fail(`scene fails the gate:\n - ${r.problems.join("\n - ")}`);
|
|
118
|
-
const html = buildHtml(loaded, { style, ...
|
|
109
|
+
const html = buildHtml(loaded, { style, ...where, options: sopts.options, scrubber: !a["no-scrubber"], heading: !a["no-heading"] });
|
|
119
110
|
if (/<\/script/i.test(html.slice(html.indexOf("<script>") + 8, html.lastIndexOf("</script>")))) fail("bundled code contains </script>");
|
|
120
111
|
const out = need(a.out, "--out");
|
|
121
112
|
mkdirSync(dirname(resolve(out)), { recursive: true });
|
|
@@ -125,16 +116,29 @@ async function main() {
|
|
|
125
116
|
}
|
|
126
117
|
case "export": {
|
|
127
118
|
const file = need(args[0], "<scene>");
|
|
128
|
-
const
|
|
119
|
+
const sopts = await sceneOpts(file);
|
|
120
|
+
const { scene } = await loadScene(file, sopts);
|
|
129
121
|
const r = checkScene(scene);
|
|
130
122
|
if (!r.ok) fail(`scene fails the gate:\n - ${r.problems.join("\n - ")}`);
|
|
131
123
|
if (a.png && !(await rasterizer())) fail("--png needs @resvg/resvg-js or Chrome");
|
|
132
|
-
|
|
124
|
+
// The contact sheet's phone row shows the tall layout, as a phone does.
|
|
125
|
+
/** @type {{ scene?: any, error?: string }} */
|
|
126
|
+
const phone = {};
|
|
127
|
+
if (where.format === "tall") phone.scene = scene;
|
|
128
|
+
else {
|
|
129
|
+
try {
|
|
130
|
+
phone.scene = (await loadScene(file, await sceneOpts(file, { ...where, format: "tall" }))).scene;
|
|
131
|
+
} catch (e) {
|
|
132
|
+
phone.error = e instanceof Error ? e.message : String(e);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
const m = await exportSteps(scene, { out: need(a.out, "--out"), style, theme: a.theme, png: a.png, scale: Number(a.scale), qc: a.qc, background: a.background, sourceFile: resolve(file), version: VERSION, phone, ...sopts });
|
|
133
136
|
console.log(`wrote ${m.files.length} frames + index.html + manifest.json to ${a.out}`);
|
|
134
137
|
return;
|
|
135
138
|
}
|
|
136
139
|
case "bake": {
|
|
137
|
-
const
|
|
140
|
+
const file = need(args[0], "<scene>");
|
|
141
|
+
const { scene } = await loadScene(file, await sceneOpts(file));
|
|
138
142
|
const svg = bake(scene, { fps: Number(a.fps ?? 15), style, theme: a.theme, background: a.background });
|
|
139
143
|
const out = need(a.out, "--out");
|
|
140
144
|
writeFileSync(out, svg);
|
|
@@ -143,7 +147,8 @@ async function main() {
|
|
|
143
147
|
return;
|
|
144
148
|
}
|
|
145
149
|
case "video": {
|
|
146
|
-
const
|
|
150
|
+
const file = need(args[0], "<scene>");
|
|
151
|
+
const { scene } = await loadScene(file, await sceneOpts(file));
|
|
147
152
|
const out = need(a.out, "--out");
|
|
148
153
|
const r = await renderVideo(scene, {
|
|
149
154
|
out,
|
|
@@ -161,7 +166,8 @@ async function main() {
|
|
|
161
166
|
return;
|
|
162
167
|
}
|
|
163
168
|
case "icon": {
|
|
164
|
-
const
|
|
169
|
+
const file = need(args[0], "<scene>");
|
|
170
|
+
const { scene } = await loadScene(file, await sceneOpts(file, { ...where, format: format ?? "1:1" }));
|
|
165
171
|
const r = checkScene(scene);
|
|
166
172
|
if (!r.ok) fail(`scene fails the gate:\n - ${r.problems.join("\n - ")}`);
|
|
167
173
|
const png = !a["no-png"];
|
|
@@ -180,7 +186,7 @@ async function main() {
|
|
|
180
186
|
const adapter = join(ROOT, "adapters", a.adapter);
|
|
181
187
|
cpSync(adapter, join(dir, "adapter"), { recursive: true });
|
|
182
188
|
mkdirSync(join(dir, "lib"), { recursive: true });
|
|
183
|
-
cpSync(join(ROOT, "lib",
|
|
189
|
+
for (const f of ["check.mjs", "gate.mjs", "load.mjs"]) cpSync(join(ROOT, "lib", f), join(dir, "lib", f));
|
|
184
190
|
writeFileSync(join(dir, "check.mjs"), VENDOR_CHECK);
|
|
185
191
|
writeFileSync(join(dir, "MOTION_KIT_VERSION"), `motion-kit ${VERSION}\nadapter ${a.adapter}\nvendored from liqweed/motion-kit; do not edit here, re-vendor instead.\n`);
|
|
186
192
|
console.log(`vendored runtime + ${a.adapter} adapter (motion-kit ${VERSION}) into ${dir}`);
|
|
@@ -192,48 +198,40 @@ async function main() {
|
|
|
192
198
|
}
|
|
193
199
|
|
|
194
200
|
const VENDOR_CHECK = `#!/usr/bin/env node
|
|
195
|
-
// motion-kit gate for vendored copies:
|
|
196
|
-
//
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
+
// motion-kit gate for vendored copies: the same checks as \`motion-kit check\` (lib/gate.mjs).
|
|
202
|
+
//
|
|
203
|
+
// node check.mjs <scene.json|scene.mjs …> [--format F]… [--locale L]… [--style style.json] [--options O]
|
|
204
|
+
//
|
|
205
|
+
// Every format unless --format; for a spec every locale its strings carry, for
|
|
206
|
+
// a custom module "en", unless --locale. The style (neutral by default) must
|
|
207
|
+
// pass the contrast check. --options passes scene options to custom modules:
|
|
208
|
+
// a .json file, inline JSON, or an .mjs module exporting ({ format, locale }) => options.
|
|
209
|
+
// Exits 1 if anything fails; a file that cannot be loaded fails on its own.
|
|
210
|
+
import { parseArgs } from "node:util";
|
|
211
|
+
import { checkFiles } from "./lib/gate.mjs";
|
|
212
|
+
import { loadOptions, loadStyle } from "./lib/load.mjs";
|
|
201
213
|
|
|
202
|
-
const
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
try {
|
|
216
|
-
const r = checkScene(createScene(spec, { format, locale }));
|
|
217
|
-
if (r.ok) console.log(\`✓ \${file} · \${format} · \${locale}\`);
|
|
218
|
-
else { bad++; console.error(\`✗ \${file} · \${format} · \${locale}\\n - \${r.problems.join("\\n - ")}\`); }
|
|
219
|
-
} catch (e) { bad++; console.error(\`✗ \${file} · \${format} · \${locale}: \${e.message}\`); }
|
|
220
|
-
}
|
|
221
|
-
}
|
|
214
|
+
const { values, positionals } = parseArgs({
|
|
215
|
+
allowPositionals: true,
|
|
216
|
+
options: {
|
|
217
|
+
format: { type: "string", multiple: true },
|
|
218
|
+
locale: { type: "string", multiple: true },
|
|
219
|
+
style: { type: "string" },
|
|
220
|
+
options: { type: "string" },
|
|
221
|
+
help: { type: "boolean", short: "h" },
|
|
222
|
+
},
|
|
223
|
+
});
|
|
224
|
+
if (values.help || !positionals.length) {
|
|
225
|
+
console.log("usage: node check.mjs <scene.json|scene.mjs …> [--format F]… [--locale L]… [--style style.json] [--options O]");
|
|
226
|
+
process.exit(values.help ? 0 : 1);
|
|
222
227
|
}
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
if (v && typeof v === "object" && !Array.isArray(v)) {
|
|
230
|
-
const keys = Object.keys(v);
|
|
231
|
-
if (keys.length && keys.every((k) => /^[a-z]{2}(-[A-Z]{2})?$/.test(k) && typeof v[k] === "string")) keys.forEach((k) => found.add(k));
|
|
232
|
-
else Object.values(v).forEach(walk);
|
|
233
|
-
} else if (Array.isArray(v)) v.forEach(walk);
|
|
234
|
-
};
|
|
235
|
-
walk(spec);
|
|
236
|
-
return found.size ? [...found] : ["en"];
|
|
228
|
+
try {
|
|
229
|
+
const bad = await checkFiles(positionals, { formats: values.format, locales: values.locale, style: loadStyle(values.style), options: await loadOptions(values.options) });
|
|
230
|
+
process.exit(bad ? 1 : 0);
|
|
231
|
+
} catch (e) {
|
|
232
|
+
console.error(\`✗ \${e instanceof Error ? e.message : e}\`);
|
|
233
|
+
process.exit(1);
|
|
237
234
|
}
|
|
235
|
+
`;
|
|
238
236
|
|
|
239
237
|
main().catch((e) => fail(e.stack ?? e.message));
|
package/docs/spec/technique.md
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
title: motion-kit technique — normative specification
|
|
3
3
|
status: draft
|
|
4
|
-
last_updated: 2026-11-
|
|
4
|
+
last_updated: 2026-11-16
|
|
5
5
|
---
|
|
6
6
|
|
|
7
7
|
# motion-kit technique — normative specification
|
|
@@ -35,18 +35,27 @@ A scene is a plain object (`runtime/scene.mjs:1-18`).
|
|
|
35
35
|
| `duration` | number, s | MUST | Time of the final state. |
|
|
36
36
|
| `title` | string | MUST | Accessible name (`<title>`) [throws if empty]. |
|
|
37
37
|
| `description` | string | MUST | Accessible description (`<desc>`) [throws if empty]. |
|
|
38
|
-
| `steps` | `{at, label, detail?}[]` | MUST | ≥ 1 step; `at` strictly increasing; `at ≤ duration` (+1e-9); every `label` non-empty [throws] (`
|
|
39
|
-
| `frame(t)` | `(number) → string` | MUST | Inner SVG markup at `t` (§2). |
|
|
38
|
+
| `steps` | `{at, label, detail?, id?}[]` | MUST | ≥ 1 step; `at` strictly increasing; `at ≤ duration` (+1e-9); every `label` non-empty; an `id`, if present, is a non-empty string unique within the scene [throws] (`validateScene`). The last step is the finished diagram. Page scripts address steps by `id` (§10.2); scene types copy a spec node's, hub's or spoke's `id`. |
|
|
39
|
+
| `frame(t, ctx)` | `(number, {idPrefix}) → string` | MUST | Inner SVG markup at `t` (§2). `ctx.idPrefix` is unique to the rendered instance; every id the frame defines (clip paths, gradients, masks, markers, filters) MUST start with it, as `${idPrefix}-name` [gate] G14. A frame that defines no ids MAY ignore `ctx`. |
|
|
40
40
|
| `stepLabelsInFrame` | boolean | MAY | `false` declares narrative steps whose labels are not drawn; exempts the scene from gate rule G10 (`lib/check.mjs:74`; used by `examples/custom-pulse.scene.mjs:137`, `brand/logo.scene.mjs:108`). |
|
|
41
41
|
| `type`, `format`, `locale` | string | MAY | Set by scene types; recorded in the export manifest (`lib/export.mjs:75-77`). |
|
|
42
42
|
|
|
43
43
|
- A scene MUST be produced by either a registered scene type —
|
|
44
44
|
`createScene(spec, {format, locale})` (`runtime/types/index.mjs:19-23`) — or a
|
|
45
|
-
custom module whose default export is `(opts) → scene
|
|
46
|
-
(`
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
45
|
+
custom module whose default export is `(opts) → scene`, called by
|
|
46
|
+
`sceneFromModule` (`runtime/kit.mjs`) with
|
|
47
|
+
`opts = { ...options, format, locale, kit }`. `options` are the caller's
|
|
48
|
+
serializable scene options, passed unchanged; option names `format`,
|
|
49
|
+
`locale` and `kit` throw. `kit` is the flat exports of the `motion`, `svg`, `style`, `scene` and `wrap` runtime modules (`KIT`); it has no `createScene`, scene types or player. Both paths
|
|
50
|
+
run `validateScene`. The CLI (`--options`), the vendored gate and the Astro
|
|
51
|
+
adapter call `sceneFromModule`, so one module with the same options, format
|
|
52
|
+
and locale gives the same scene everywhere. A `build` page calls the factory
|
|
53
|
+
with the same object, its `kit` being the bundled runtime, after the CLI has
|
|
54
|
+
built the scene once through `sceneFromModule`.
|
|
55
|
+
- A custom module MUST take the runtime from `kit`, or import only from
|
|
56
|
+
`runtime/` with single-line relative `import { … } from "./x.mjs";`
|
|
57
|
+
statements and use only the export forms the inliner supports, or `build`
|
|
58
|
+
throws (`lib/bundle.mjs:8-11,33,59`).
|
|
50
59
|
- `detail` SHOULD hold sentences; labels stay short (§7). `detail` feeds the
|
|
51
60
|
player caption and screen-reader list (§10.2).
|
|
52
61
|
|
|
@@ -299,34 +308,64 @@ Every target goes through this one function. It MUST emit `<svg>` with
|
|
|
299
308
|
| `theme` | `light`/`dark` for resolution or inline vars. |
|
|
300
309
|
| `live` | Omits inline vars; the container supplies them. |
|
|
301
310
|
| `background` | Paints a full-size `bg` rect behind the diagram. |
|
|
302
|
-
| `idPrefix` | MUST be unique per page when several diagrams share it. |
|
|
311
|
+
| `idPrefix` | Prefix for every id in the output: `<title>`/`<desc>`, and the scene's own through `frame(t, { idPrefix })`. Made a safe id token (`safeId`: letters, digits, `_`, `-`). MUST be unique per page when several diagrams share it. |
|
|
303
312
|
|
|
304
313
|
### 10.2 Live player (`runtime/player.mjs`)
|
|
305
314
|
|
|
306
315
|
| Requirement | Source |
|
|
307
316
|
|---|---|
|
|
308
|
-
| MUST touch the DOM only when mounted; scenes stay pure, the player owns the clock (`requestAnimationFrame`, wall time). | `runtime/player.mjs
|
|
309
|
-
| Initial state MUST be the final frame
|
|
310
|
-
| Autoplay `visible` (default): plays once from 0 when ≥ 40 % in view, then holds; `none` disables. MUST NOT loop. |
|
|
311
|
-
|
|
|
312
|
-
|
|
|
313
|
-
|
|
|
314
|
-
|
|
|
315
|
-
|
|
|
316
|
-
|
|
|
317
|
-
| `
|
|
318
|
-
|
|
|
319
|
-
|
|
|
320
|
-
|
|
321
|
-
|
|
317
|
+
| MUST touch the DOM only when mounted; scenes stay pure, the player owns the clock (`requestAnimationFrame`, wall time). | `runtime/player.mjs` (`mountPlayer`, `tick`), `AGENTS.md` |
|
|
318
|
+
| Initial state MUST be the final frame. A server/no-JS fallback SHOULD be `playerMarkup(scene, { id, … })` inside a `.mk-player` container, with `PLAYER_CSS` in a `style[data-mk-player-css]` and the tokens from `themeCss(selector, style, theme)`: the markup the player builds, with the controls `inert`. The step list and the colours then exist without JavaScript, and mounting moves nothing. A plain final-frame SVG MAY be used instead. | `runtime/player.mjs` (`view`, `playerMarkup`, `themeCss`), `lib/build.mjs`, `adapters/astro/AnimatedDiagram.astro`, `test/player.test.mjs` |
|
|
319
|
+
| Autoplay `visible` (default): plays once from 0 when ≥ 40 % of the diagram, or ≥ 40 % of the viewport's height, is in view (decided from the intersection, not `isIntersecting` alone), then holds; `none` disables. MUST NOT loop. | `runtime/player.mjs` (`shown`, `THRESHOLDS`) |
|
|
320
|
+
| While a playing diagram does not intersect the viewport (scrolled away, `display: none`), its clock MUST stop and no frame be drawn; when it intersects again, playback resumes from the same time. Visibility is observed until `destroy()`. | `runtime/player.mjs` (`onVisibility`, `run`) |
|
|
321
|
+
| Play/pause MUST always be present when motion can play (WCAG 2.2.2); label shows Play/Pause/Replay. | `runtime/player.mjs` (`playLabel`, `draw`) |
|
|
322
|
+
| Every control (buttons, scrubber) MUST be at least 44 × 44 px. The caption and the play button MUST keep the size of their longest content, so playback does not shift the layout. | `PLAYER_CSS` (`.mk-btn`, `.mk-scrub`, `.mk-caption`, `.mk-play`) |
|
|
323
|
+
| One `<button>` per step, labelled `N. label`; activating it jumps by default. `animateSteps: true` opts buttons and keyboard navigation into `playToStep`. `aria-current="step"` marks the active step. | `runtime/player.mjs` |
|
|
324
|
+
| Optional range scrubber (`aria-label`, default "Timeline"); input pauses and seeks. | `runtime/player.mjs` (`options.scrubber`) |
|
|
325
|
+
| Caption shows the active step's `detail` and is `aria-hidden`; a visually hidden ordered list of all steps (`label: detail`) MUST be present for screen readers. | `runtime/player.mjs` (`caption`, `sr`) |
|
|
326
|
+
| Keyboard on the focusable stage, a `role="group"` named by the scene title and described by a visually hidden hint (`labels.keys`): ←/→ step, Home/End first/last, Space play/pause. Every element with `tabindex="0"` has a role. | `runtime/player.mjs` (`view`, `keydown` handler) |
|
|
327
|
+
| Visible focus style for the stage and every control. | `PLAYER_CSS` (`:focus-visible` rule) |
|
|
328
|
+
| `prefers-reduced-motion: reduce`: final frame, no autoplay, no play button, Space ignored. | `runtime/player.mjs` (`reduced`) |
|
|
329
|
+
| Theme: `options.theme`, else follows `prefers-color-scheme` live. Tokens MUST be set one custom property at a time, keeping the container's other inline CSS. | `runtime/player.mjs` (`applyTheme`, `themeQuery`) |
|
|
330
|
+
| The player stylesheet MUST be in the document whenever a player mounts: it is added to `<head>` unless a `style[data-mk-player-css]` is present, so a page swap that removed it cannot leave players unstyled. `injectCss: false` opts out. | `runtime/player.mjs` (`ensureStyle`), `test/player.test.mjs` |
|
|
331
|
+
| Ids: each player renders with its own prefix, `options.id`, else `scene.id` plus a per-page counter, so two players of one scene share no ids. | `runtime/player.mjs` (`prefix`) |
|
|
332
|
+
| `playToStep(ref)` MUST play forward from the current time, clamp to the target step's exact `at` and hold. Backward, same-time and reduced-motion calls MUST jump. `seekStep(ref)` jumps. `ref` is a zero-based index or a step `id`; an invalid index or unknown id throws `RangeError` (`stepIndex`). Retargeting replaces the target; `play()` continues to duration; `seek(t)` cancels playback. | `runtime/player.mjs`, `runtime/scene.mjs`, `test/player.test.mjs` |
|
|
333
|
+
| `destroy()` MUST cancel the animation frame, disconnect visibility observation, remove theme listening and remove this controller's capture entry. | `runtime/player.mjs`, `test/player.test.mjs` |
|
|
334
|
+
| **Capture hook**: the controller `{seek, play, pause, seekStep, playToStep, destroy, time, scene}` MUST be exposed at `globalThis.__motionKit[options.id ?? scene.id]`. | `runtime/player.mjs` |
|
|
335
|
+
|
|
336
|
+
UI strings MAY be localized via `options.labels`: a flat `{ play, pause, replay, steps, scrubber, keys }` object (`steps` names the controls group, `keys` is the stage's keyboard hint), or a dictionary keyed by locale (`{ en: {…}, nl: {…} }`) resolved with `options.locale`, then its language subtag, then `en`, then the first entry. Missing keys keep the English defaults (`resolveLabels`, `runtime/player.mjs`).
|
|
337
|
+
|
|
338
|
+
Keyboard step changes (←/→/Home/End) MUST be announced through a visually hidden `role="status"` polite live region (`N. label: detail`); autoplay, pointer and scrubber use stay silent.
|
|
339
|
+
|
|
340
|
+
`mountPlayer` MUST release any player already mounted on the same container (`destroy()`), so remounting never leaks clocks, observers or capture entries. The build output passes `locale` through, so label dictionaries resolve there too; the Slidev adapter announces each click's step through the same kind of live region.
|
|
341
|
+
|
|
342
|
+
The Astro adapter mounts each diagram once, tears the players down on `astro:before-swap` and mounts the new page's diagrams on `astro:page-load`. Its root element exposes the controller as `.controller` from mount, and dispatches `motion-ready` (bubbling, `detail.controller`) or `motion-error` (`detail.error`) once the page's own scripts and their `DOMContentLoaded` and `astro:page-load` handlers have run, so a script placed after the diagram can still listen. Its `id` prop sets the element id, the capture-hook key and the id prefix; it MUST be a safe id token, used once per page, or the build fails.
|
|
343
|
+
|
|
344
|
+
### 10.2.1 Custom element (`adapters/element/motion-diagram.mjs`)
|
|
345
|
+
|
|
346
|
+
- Vendoring with `--adapter element` MUST copy an unbuilt module importing the
|
|
347
|
+
adjacent vendored runtime. Importing it in a browser defines `<motion-diagram>`.
|
|
348
|
+
- The spec MUST come from the `spec` JSON attribute, a child
|
|
349
|
+
`script[type="application/json"]`, or a `src` URL, in that order. URLs resolve
|
|
350
|
+
against the document base URI. `format` and `locale` select the scene;
|
|
351
|
+
`theme` and JSON `style-spec` configure the player; HTML `style` remains CSS.
|
|
352
|
+
- Supplied final-frame child markup MUST remain visible without JavaScript,
|
|
353
|
+
while loading and on errors. Only a successful mount hides the fallback.
|
|
354
|
+
- `controller` MUST expose the mounted player, or be null when unmounted.
|
|
355
|
+
`motion-ready` carries `detail.controller`; `motion-error` carries `detail.error`.
|
|
356
|
+
- Observed attribute changes MUST remount. Disconnection MUST abort loading,
|
|
357
|
+
destroy the player and restore the fallback; reconnection mounts afresh.
|
|
358
|
+
Superseded asynchronous loads MUST NOT mount or publish errors.
|
|
322
359
|
|
|
323
360
|
### 10.3 Self-contained HTML (`motion-kit build`, `lib/build.mjs`)
|
|
324
361
|
|
|
325
362
|
- MUST be one document with the runtime inlined by `lib/bundle.mjs`, no
|
|
326
363
|
external scripts, stylesheets or fetches (`test/cli.test.mjs:31-37`), so it
|
|
327
364
|
works under `connect-src 'none'` and from `file://`.
|
|
328
|
-
- MUST pre-render the
|
|
329
|
-
to the scene locale (
|
|
365
|
+
- MUST pre-render the player's first state (`playerMarkup`, `PLAYER_CSS`,
|
|
366
|
+
`themeCss`) and set `<html lang>` to the scene locale (`lib/build.mjs`).
|
|
367
|
+
- A custom module is built with the caller's `--options` and `kit`: every
|
|
368
|
+
runtime module is a bundle entry, so the `MotionKit` global is the kit.
|
|
330
369
|
- The CLI MUST refuse a scene that fails the gate (`cli/motion-kit.mjs:112-113`)
|
|
331
370
|
and bundled code containing `</script>` (`:115`).
|
|
332
371
|
- The inlined runtime MUST render the same bytes as Node
|
|
@@ -343,6 +382,8 @@ UI strings MAY be localized via `options.labels`.
|
|
|
343
382
|
animation delayed `0.3 s + start`; the last slice holds (`mk-hold`, fill
|
|
344
383
|
forwards) (`:35-47`). Plays once; MUST hold the final frame.
|
|
345
384
|
- Under `prefers-reduced-motion: reduce` only the final slice is visible (`:39`).
|
|
385
|
+
- Slices share one document: a slice whose markup defines ids is rendered with
|
|
386
|
+
its own prefix (`<idPrefix>-<n>`, base `mk-bake` or `options.idPrefix`).
|
|
346
387
|
- One theme per file; for theme-following embeds publish a light and dark file
|
|
347
388
|
(`README.md:21-29`, `scripts/render-media.sh`).
|
|
348
389
|
- Size grows with fps × duration; README use SHOULD lower fps to 10–12.
|
|
@@ -359,11 +400,15 @@ UI strings MAY be localized via `options.labels`.
|
|
|
359
400
|
makes the canvas opaque (transparent by default).
|
|
360
401
|
- `--qc`: frames at `max(0, at − 0.2)` and `at + 0.2` (not after the last step)
|
|
361
402
|
under `qc/` (`lib/export.mjs:58-65`).
|
|
362
|
-
- `index.html` contact sheet: steps, QC row, a
|
|
363
|
-
|
|
403
|
+
- `index.html` contact sheet: steps, QC row, a phone row and the human review
|
|
404
|
+
checklist (`lib/export.mjs`, `contactSheet`). The phone row MUST show the
|
|
405
|
+
scene built for the `tall` format, in the same locale, options, style and
|
|
406
|
+
theme, at 360 px (`qc/phone.svg`, manifest `kind: "phone"`); when the tall
|
|
407
|
+
layout cannot be built, it shows the exported frame and says why.
|
|
364
408
|
- `manifest.json` MUST contain no timestamps: tool, version, scene, type,
|
|
365
|
-
format, locale, size, duration, style, theme, a 16-hex input
|
|
366
|
-
(source file + style), source commit, render settings,
|
|
409
|
+
format, locale, scene options, size, duration, style, theme, a 16-hex input
|
|
410
|
+
hash (source file + style + scene options), source commit, render settings,
|
|
411
|
+
file list
|
|
367
412
|
(`lib/export.mjs:66-88`). Two exports of the same inputs MUST be equal
|
|
368
413
|
(`test/cli.test.mjs:20-30`).
|
|
369
414
|
|
|
@@ -387,21 +432,34 @@ UI strings MAY be localized via `options.labels`.
|
|
|
387
432
|
itself (`:67-72`); forward by one click animates from the previous step's
|
|
388
433
|
`at` to the next, any other move seeks (`:93-100`); print/export, overview
|
|
389
434
|
and reduced motion show the settled step frame (`:65`).
|
|
390
|
-
- **Astro** (`adapters/astro/AnimatedDiagram.astro`): server-renders the
|
|
391
|
-
|
|
435
|
+
- **Astro** (`adapters/astro/AnimatedDiagram.astro`): server-renders the
|
|
436
|
+
player's first state (§10.2) and mounts the player. It takes a `spec`, or a
|
|
437
|
+
custom module by path (`module`, a `/src/…/*.scene.mjs|js` file found with
|
|
438
|
+
`import.meta.glob`, loaded on demand in the browser) with its `options`;
|
|
439
|
+
`theme` (`light`/`dark`, else the system), `style` (token data), `id`,
|
|
440
|
+
`scrubber`, `labels`, `class`. Props MUST be serializable (a spec or a module
|
|
441
|
+
path, not a scene). The player stylesheet is emitted once per page; each
|
|
442
|
+
instance carries its tokens and a page-unique id (`mk-<scene.id>-<n>` unless
|
|
443
|
+
`id` is given). It fails the build for a missing module, options with a spec,
|
|
444
|
+
or an invalid theme.
|
|
392
445
|
- **Vendor** (`cli/motion-kit.mjs`, `vendor` command): copies `runtime/`, one
|
|
393
|
-
adapter, `lib/check.mjs` and the full MIT
|
|
394
|
-
|
|
446
|
+
adapter, `lib/check.mjs`, `lib/gate.mjs`, `lib/load.mjs` and the full MIT
|
|
447
|
+
`LICENSE`; writes `check.mjs`, which runs the same gate as `motion-kit
|
|
448
|
+
check` (§11) with `--format`, `--locale`, `--style` and `--options`, and
|
|
449
|
+
`MOTION_KIT_VERSION`. Consumers MUST retain `LICENSE` and the
|
|
395
450
|
SPDX headers when redistributing vendored files. Vendored files MUST NOT
|
|
396
451
|
be edited in the consumer.
|
|
397
452
|
|
|
398
453
|
## 11. The gate
|
|
399
454
|
|
|
400
|
-
`checkScene(scene)` (`lib/check.mjs
|
|
401
|
-
(`SAMPLES = 120
|
|
402
|
-
`motion-kit check`
|
|
403
|
-
|
|
404
|
-
|
|
455
|
+
`checkScene(scene)` (`lib/check.mjs`) samples 121 evenly spaced times
|
|
456
|
+
(`SAMPLES = 120`) and stops collecting after 13 problems.
|
|
457
|
+
`motion-kit check` and the vendored `check.mjs` both run `checkFiles`
|
|
458
|
+
(`lib/gate.mjs`): every file × format (all six unless `--format`, repeatable)
|
|
459
|
+
× locale (`--locale`, repeatable; else every locale a spec's strings carry, and
|
|
460
|
+
`en` for a module, §7), with `--options` for modules, plus the style contrast
|
|
461
|
+
check. A file that cannot be read, parsed or built fails on its own and the
|
|
462
|
+
remaining files are still checked; the exit code is 1 on any failure. `npm run gate` = unit tests +
|
|
405
463
|
`check` over `examples/` and `brand/` (`package.json:15-18`).
|
|
406
464
|
|
|
407
465
|
| ID | Rule | Code | Rationale |
|
|
@@ -409,7 +467,7 @@ construction error (`cli/motion-kit.mjs:81-108`). `npm run gate` = unit tests +
|
|
|
409
467
|
| G1 | Rendering the same `t` twice MUST yield identical bytes. | `lib/check.mjs:41-42` | Determinism (T1/T2). Catches clocks/randomness and call-to-call state; cannot catch state that is consistent between two calls. |
|
|
410
468
|
| G2 | Each sampled frame MUST be well-formed (balanced tags, no stray `<`, entities only `amp lt gt quot #39 #n`). | `:12-31,43-44` | Escaping failures and broken markup break every target. |
|
|
411
469
|
| G3 | No number with ≥ 5 decimals in the full SVG. | `:45-46` | Rounding (T4). |
|
|
412
|
-
| G4 | No `#hex`, `rgb(a)(`, `hsl(a)(` in `frame()` output. | `:47-49` | Token-only colours: themes and brand styles must work everywhere. |
|
|
470
|
+
| G4 | No `#hex`, `rgb(a)(`, `hsl(a)(` in `frame()` output; id references (`url(#…)`, `href="#…"`) are not colours. | `:47-49` | Token-only colours: themes and brand styles must work everywhere. |
|
|
413
471
|
| G5 | No `NaN`, `undefined`, `Infinity` in `frame()` output. | `:50` | Silent geometry bugs. |
|
|
414
472
|
| G6 | `render(at − 0.001) === render(at)` for every step. | `:55-58` | Settled step frames (§8). |
|
|
415
473
|
| G7 | First frame ≠ last frame. | `:61-63` | Something must animate. |
|
|
@@ -417,8 +475,9 @@ construction error (`cli/motion-kit.mjs:81-108`). `npm run gate` = unit tests +
|
|
|
417
475
|
| G9 | `duration ≤ 12` s. | `:68` | Explainer budget (see §12 D1). |
|
|
418
476
|
| G10 | Every word of every step label (escaped, trailing `/` stripped) appears in the final frame's text, unless `stepLabelsInFrame === false`. | `:72-77` | Step buttons and slides refer to things the viewer can see. Word-level, so it does not prove the label is contiguous. |
|
|
419
477
|
| G11 | `frame.toString()` MUST NOT contain `Date.now`, `new Date`, `Math.random`, `performance.now`. | `:80-81` | T1. Textual heuristic: helpers defined outside `frame` are not scanned. |
|
|
420
|
-
| G12 | Style passes WCAG contrast in both themes. | `
|
|
421
|
-
| G13 | Scene construction succeeds for the format × locale (validation, fit, collisions, wrapping). | `
|
|
478
|
+
| G12 | Style passes WCAG contrast in both themes. | `lib/gate.mjs`, `runtime/style.mjs` (`checkContrast`) | Legibility in light and dark. |
|
|
479
|
+
| G13 | Scene construction succeeds for the format × locale (validation, fit, collisions, wrapping). | `lib/gate.mjs` | Reject, don't invent: a broken picture never renders. |
|
|
480
|
+
| G14 | Rendered with two different `idPrefix` values, sampled frames share no `id`. | `lib/check.mjs` (rule 7) | Two instances on one page (wide and tall, or one diagram twice) must not share ids: `url(#x)` resolves to the first element with that id, and WCAG 2.1 SC 4.1.1 forbids duplicates. |
|
|
422
481
|
|
|
423
482
|
The gate checks structure, not looks. Visual review (before/after frames,
|
|
424
483
|
phone size, real-time playback, dark mode) is required by
|
|
@@ -440,8 +499,7 @@ Recorded, not resolved. Each needs an owner decision.
|
|
|
440
499
|
| D7 | Style `radius` / `stroke` | Normalized (`runtime/style.mjs:63-64`) but never read; parts default to radius 10, stroke 2 (`runtime/parts.mjs:52,75`). | Documented as style fields (`styles.md:28-29`). |
|
|
441
500
|
| D8 | Dark fallback | A style with no `dark` applies its **light** overrides to dark (`runtime/style.mjs:66`). | "Missing tokens fall back to neutral" (`styles.md:35`). |
|
|
442
501
|
| D9 | Video frame reuse | PNGs are reused only when `t` repeats (lead and hold) (`lib/video.mjs:46`). | "Identical consecutive frames are rasterized once" (`skills/motion-render/references/video.md:9`). |
|
|
443
|
-
| D10 | Capture-hook key | Key is `options.id ?? scene.id`, i.e. format-suffixed (`order-flow-16x9`); the Astro adapter
|
|
444
|
-
| D11 | Vendored gate | `check.mjs` written by `vendor` handles `.json` only, skips the contrast check, and detects locales with `^[a-z]{2}$` (no region, no string-value test) (`cli/motion-kit.mjs:177-207`). | Implied parity with `motion-kit check` (`README.md:90-92`). |
|
|
502
|
+
| D10 | Capture-hook key | Key is `options.id ?? scene.id`, i.e. format-suffixed (`order-flow-16x9`); the Astro adapter uses its `id` prop or `mk-<scene.id>-<n>`; the Slidev adapter exposes none. | `window.__motionKit[sceneId]` / `[id]` (`adapters/page/README.md:22`, `interactivity.md`). |
|
|
445
503
|
| D12 | Auto-fit maximum | Sequence 22 px, hub-spoke 20 px (`runtime/types/hub-spoke.mjs:97`). | "Fixed formats ≤ 22 px" without type distinction (`technique.md:90`); `formats.md:12` is correct. |
|
|
446
504
|
| D13 | Timing budgets | `ENTRANCE_MAX` is exported but unused; stagger and entrance limits are not checked. | Listed as "Hard rules" (`skills/animated-diagram/SKILL.md:50`). |
|
|
447
|
-
| D14 | Manifest reproducibility | `inputHash` covers the source file and
|
|
505
|
+
| D14 | Manifest reproducibility | `inputHash` covers the source file, style and scene options, not the runtime; a runtime change keeps the hash while frames change (only `version` hints at it) (`lib/export.mjs:66-70`). | "A stale export is easy to spot" (`skills/motion-render/SKILL.md:66-68`). |
|
package/lib/build.mjs
CHANGED
|
@@ -2,32 +2,44 @@
|
|
|
2
2
|
* motion-kit · build a self-contained interactive HTML file.
|
|
3
3
|
*
|
|
4
4
|
* One document, runtime inlined, no network requests: safe for Craft Pages
|
|
5
|
-
* (connect-src 'none'), file:// and any static host. The
|
|
6
|
-
* pre-rendered into the markup, so it
|
|
5
|
+
* (connect-src 'none'), file:// and any static host. The player's first state
|
|
6
|
+
* (final frame, step list, colours) is pre-rendered into the markup, so it
|
|
7
|
+
* reads correctly before (or without) JS, and mounting moves nothing.
|
|
8
|
+
*
|
|
9
|
+
* A custom module gets its scene options and `kit` (every runtime export),
|
|
10
|
+
* as it does from the CLI.
|
|
7
11
|
*/
|
|
8
12
|
import { fileURLToPath } from "node:url";
|
|
9
13
|
import { bundle } from "./bundle.mjs";
|
|
10
14
|
import { escapeHtml } from "./export.mjs";
|
|
11
|
-
import {
|
|
15
|
+
import { PLAYER_CSS, playerMarkup, themeCss } from "../runtime/player.mjs";
|
|
12
16
|
import { cssVars } from "../runtime/style.mjs";
|
|
13
17
|
|
|
14
18
|
const RUNTIME = fileURLToPath(new URL("../runtime/", import.meta.url));
|
|
15
19
|
|
|
20
|
+
/** JSON for an inline script: `<` escaped, so no value can close the script element. */
|
|
21
|
+
const scriptJson = (/** @type {unknown} */ value) => JSON.stringify(value).replace(/</g, "\\u003c");
|
|
22
|
+
|
|
16
23
|
/**
|
|
17
24
|
* @param {import("./load.mjs").LoadedScene} loaded
|
|
18
|
-
* @param {{style:import("../runtime/style.mjs").Style, format:string, locale:string, scrubber?:boolean, heading?:boolean, theme?:string, labels?:Record<string,string>}} o
|
|
25
|
+
* @param {{style:import("../runtime/style.mjs").Style, format:string, locale:string, scrubber?:boolean, heading?:boolean, theme?:string, labels?:Record<string,string|Record<string,string>>, options?:Record<string,unknown>}} o
|
|
26
|
+
* options: scene options for a custom module's factory
|
|
19
27
|
*/
|
|
20
28
|
export function buildHtml(loaded, o) {
|
|
21
|
-
|
|
29
|
+
// Every runtime module is an entry, so the MotionKit global is the whole kit.
|
|
30
|
+
const entries = ["motion", "svg", "style", "wrap", "scene", "types/index", "player"].map((m) => `${RUNTIME}${m}.mjs`);
|
|
31
|
+
const runtimeCount = entries.length;
|
|
22
32
|
if (loaded.kind === "module") entries.push(loaded.file);
|
|
23
|
-
|
|
33
|
+
// MotionKitRuntime is the runtime alone: a custom module's exports, spread
|
|
34
|
+
// after it into MotionKit, may reuse a runtime name without breaking `kit`.
|
|
35
|
+
const code = bundle(entries, { runtimeName: "MotionKitRuntime", runtimeCount });
|
|
24
36
|
const { scene } = loaded;
|
|
25
|
-
const
|
|
37
|
+
const options = { style: o.style, scrubber: o.scrubber !== false, theme: o.theme, labels: o.labels, locale: o.locale };
|
|
38
|
+
const fallback = playerMarkup(scene, { ...options, id: "mk-fallback" });
|
|
26
39
|
const make =
|
|
27
40
|
loaded.kind === "spec"
|
|
28
|
-
? `MotionKit.createScene(${
|
|
29
|
-
: `MotionKit.default(${
|
|
30
|
-
const options = { style: o.style, scrubber: o.scrubber !== false, theme: o.theme, labels: o.labels };
|
|
41
|
+
? `MotionKit.createScene(${scriptJson(loaded.spec)}, ${scriptJson({ format: o.format, locale: o.locale })})`
|
|
42
|
+
: `MotionKitRuntime.validateScene(MotionKit.default(Object.assign(${scriptJson(o.options ?? {})}, ${scriptJson({ format: o.format, locale: o.locale })}, { kit: MotionKitRuntime })))`;
|
|
31
43
|
const lang = scene.locale ?? o.locale ?? "en";
|
|
32
44
|
return `<!doctype html>
|
|
33
45
|
<html lang="${escapeHtml(lang)}">
|
|
@@ -42,18 +54,20 @@ body{margin:0;padding:24px;background:var(--mk-bg);color:var(--mk-ink);font-fami
|
|
|
42
54
|
main{max-width:${Math.max(scene.width, 360)}px;margin:0 auto}
|
|
43
55
|
h1{font-size:20px;line-height:1.3;margin:0 0 4px}
|
|
44
56
|
.lede{margin:0 0 16px;color:var(--mk-muted);font-size:15px;line-height:1.45}
|
|
57
|
+
${themeCss("#mk", o.style, o.theme)}
|
|
45
58
|
</style>
|
|
59
|
+
<style data-mk-player-css>${PLAYER_CSS}</style>
|
|
46
60
|
</head>
|
|
47
61
|
<body>
|
|
48
62
|
<main>
|
|
49
63
|
${o.heading === false ? "" : `<h1>${escapeHtml(scene.title)}</h1>\n<p class="lede">${escapeHtml(scene.description)}</p>`}
|
|
50
|
-
<div id="mk">${fallback}</div>
|
|
64
|
+
<div id="mk" class="mk-player">${fallback}</div>
|
|
51
65
|
</main>
|
|
52
66
|
<script>
|
|
53
67
|
${code}
|
|
54
68
|
(function () {
|
|
55
69
|
var scene = ${make};
|
|
56
|
-
|
|
70
|
+
MotionKitRuntime.mountPlayer(document.getElementById("mk"), scene, ${scriptJson(options)});
|
|
57
71
|
})();
|
|
58
72
|
</script>
|
|
59
73
|
</body>
|
package/lib/bundle.mjs
CHANGED
|
@@ -15,7 +15,11 @@ import { dirname, resolve } from "node:path";
|
|
|
15
15
|
|
|
16
16
|
const IMPORT_RE = /^import\s*\{([^}]*)\}\s*from\s*["'](\.{1,2}\/[^"']+)["'];?\s*$/;
|
|
17
17
|
|
|
18
|
-
|
|
18
|
+
/**
|
|
19
|
+
* @param {string[]} entries
|
|
20
|
+
* @param {{ globalName?: string, runtimeName?: string, runtimeCount?: number }} [o]
|
|
21
|
+
*/
|
|
22
|
+
export function bundle(entries, { globalName = "MotionKit", runtimeName = "", runtimeCount = entries.length } = {}) {
|
|
19
23
|
const order = [];
|
|
20
24
|
const seen = new Map();
|
|
21
25
|
function visit(file) {
|
|
@@ -64,5 +68,8 @@ export function bundle(entries, { globalName = "MotionKit" } = {}) {
|
|
|
64
68
|
}
|
|
65
69
|
const ids = entries.map((e) => visit(e));
|
|
66
70
|
const merged = ids.map((id) => `...${id}`).join(", ");
|
|
67
|
-
|
|
71
|
+
// runtimeName: a second global of the first `runtimeCount` entries only, so a
|
|
72
|
+
// later entry's exports cannot shadow theirs.
|
|
73
|
+
const runtime = runtimeName ? `\nglobalThis.${runtimeName} = Object.freeze({ ${ids.slice(0, runtimeCount).map((id) => `...${id}`).join(", ")} });` : "";
|
|
74
|
+
return `(() => {\n"use strict";\n${order.join("\n")}\nglobalThis.${globalName} = Object.freeze({ ${merged} });${runtime}\n})();\n`;
|
|
68
75
|
}
|