@johnoscott/isometric-engine 0.15.0 → 0.16.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/README.md +1 -1
- package/iso-editor.js +230 -193
- package/iso-render.js +184 -147
- package/iso-systems.js +183 -146
- package/isometric-diagram.js +389 -0
- package/js/iso-core/index.js +5 -0
- package/js/iso-core/mono.js +242 -0
- package/js/iso-editor/ui/canvas.js +23 -1
- package/js/iso-objects/ants.js +1 -1
- package/js/iso-objects/index.js +10 -2
- package/js/iso-objects/mono-paint.js +96 -0
- package/js/iso-objects/motion-doc.js +0 -79
- package/js/iso-objects/motion-entry.js +4 -2
- package/js/iso-objects/motion-schema.js +83 -0
- package/js/iso-objects/motion.js +9 -4
- package/js/iso-objects/timeline.js +51 -11
- package/js/iso-systems/compile.js +25 -1
- package/js/iso-systems/render.js +1 -1
- package/js/iso-systems/schema.json +1 -0
- package/js/iso-viewer/index.js +6 -0
- package/js/iso-viewer/page.js +40 -17
- package/js/iso-viewer/play.js +340 -0
- package/js/iso-viewer/player.js +147 -162
- package/js/iso-viewer/transport.js +289 -0
- package/package.json +39 -2
- package/src/iso-core/index.ts +1 -0
- package/src/iso-core/mono.ts +306 -0
- package/src/iso-core/theme.ts +2 -1
- package/src/iso-editor/ui/canvas.ts +14 -1
- package/src/iso-objects/ants.ts +2 -2
- package/src/iso-objects/index.ts +5 -3
- package/src/iso-objects/mono-paint.ts +97 -0
- package/src/iso-objects/motion-doc.ts +0 -52
- package/src/iso-objects/motion-entry.ts +4 -3
- package/src/iso-objects/motion-schema.ts +55 -0
- package/src/iso-objects/motion.ts +6 -5
- package/src/iso-objects/scene.ts +3 -0
- package/src/iso-objects/timeline.ts +63 -13
- package/src/iso-objects/types.ts +2 -0
- package/src/iso-systems/compile.ts +29 -2
- package/src/iso-systems/render.ts +5 -1
- package/src/iso-systems/schema.json +1 -0
- package/src/iso-viewer/index.ts +4 -0
- package/src/iso-viewer/page.ts +28 -12
- package/src/iso-viewer/play.ts +280 -0
- package/src/iso-viewer/player.ts +133 -134
- package/src/iso-viewer/transport.ts +272 -0
- package/types/iso-core/index.d.ts +1 -0
- package/types/iso-core/mono.d.ts +61 -0
- package/types/iso-core/theme.d.ts +2 -0
- package/types/iso-objects/ants.d.ts +1 -0
- package/types/iso-objects/index.d.ts +5 -3
- package/types/iso-objects/mono-paint.d.ts +20 -0
- package/types/iso-objects/motion-doc.d.ts +0 -594
- package/types/iso-objects/motion-entry.d.ts +4 -3
- package/types/iso-objects/motion-schema.d.ts +594 -0
- package/types/iso-objects/motion.d.ts +4 -2
- package/types/iso-objects/scene.d.ts +3 -0
- package/types/iso-objects/timeline.d.ts +22 -2
- package/types/iso-objects/types.d.ts +2 -0
- package/types/iso-systems/compile.d.ts +5 -0
- package/types/iso-systems/render.d.ts +4 -0
- package/types/iso-viewer/index.d.ts +4 -0
- package/types/iso-viewer/play.d.ts +37 -0
- package/types/iso-viewer/transport.d.ts +73 -0
- package/types/player/isometric-diagram.d.ts +24 -0
|
@@ -4,10 +4,13 @@ import { parse, type Parsed } from '../iso-core/parse.ts';
|
|
|
4
4
|
import { validateSchema } from './validate.ts';
|
|
5
5
|
import type { Diag } from '../iso-core/errors.ts';
|
|
6
6
|
import { place } from '../iso-objects/place.ts';
|
|
7
|
-
import { extendTheme, loadTheme } from '../iso-core/theme.ts';
|
|
7
|
+
import { extendTheme, loadTheme, type Theme } from '../iso-core/theme.ts';
|
|
8
8
|
import type { RawDiagram } from '../iso-objects/types.ts';
|
|
9
9
|
import { compileRaw, type Compiled } from '../iso-objects/compile.ts';
|
|
10
10
|
import { resolveSvg, type ThemeMode } from '../iso-objects/resolve.ts';
|
|
11
|
+
import { monoOfDrawing, paintMono } from '../iso-objects/mono-paint.ts';
|
|
12
|
+
import type { MonoTable } from '../iso-core/mono.ts';
|
|
13
|
+
import type { CompiledScene } from '../iso-objects/compile.ts';
|
|
11
14
|
import { bindMotion } from '../iso-objects/timeline.ts';
|
|
12
15
|
import type { MotionDoc } from '../iso-objects/motion-doc.ts';
|
|
13
16
|
import { antsOf } from '../iso-objects/ants.ts';
|
|
@@ -24,6 +27,10 @@ export interface CompileOptions {
|
|
|
24
27
|
/** (0.15.0) self-contained drawings: each scene's SVG has its colours and text styles on its elements, in `mode`: no
|
|
25
28
|
* stylesheet, no custom properties, no hsl() or color-mix() (svgAt's frames too) */
|
|
26
29
|
resolveVars?: boolean;
|
|
30
|
+
/** (0.16.0) a self-contained drawing in black and white, in either mode: greys, patterns and line rhythms, and no hue */
|
|
31
|
+
treatment?: 'mono' | 'none';
|
|
32
|
+
/** a host's own table, over the treatment's */
|
|
33
|
+
mono?: MonoTable;
|
|
27
34
|
/** a file with an error draws nothing: `scenes` is empty (true), or it throws ('throw'). Default: what could be laid
|
|
28
35
|
* out is in `scenes` all the same, with `ok` false */
|
|
29
36
|
strict?: boolean | 'throw';
|
|
@@ -40,16 +47,36 @@ export function compile(source: string, themeText: string, opts: CompileOptions
|
|
|
40
47
|
if (!c.ok && opts.strict === 'throw') throw new CompileError(c.diags);
|
|
41
48
|
if (!c.ok && opts.strict) c.scenes = [];
|
|
42
49
|
if (opts.stepMs !== undefined) for (const s of c.scenes) for (const st of s.steps.steps) st.duration = opts.stepMs;
|
|
50
|
+
if (opts.treatment !== undefined && opts.treatment !== 'mono' && opts.treatment !== 'none') throw new Error(`treatment: there is "mono" (and "none"), and no ${JSON.stringify(opts.treatment)}`);
|
|
51
|
+
const treatment = (opts.treatment ?? c.doc?.treatment) === 'mono';
|
|
52
|
+
if (opts.treatment === 'mono' && !opts.resolveVars) throw new Error('treatment: a self-contained drawing\'s (resolveVars)');
|
|
53
|
+
const theme = c.theme;
|
|
43
54
|
if (opts.resolveVars) for (const s of c.scenes) {
|
|
44
|
-
const
|
|
55
|
+
const mode = opts.mode ?? 'light', css = drawingCss(systems) + MARKS_CSS + GRID_CSS;
|
|
56
|
+
if (treatment) { toMono(s, theme!, mode, css, opts.mono); continue; }
|
|
57
|
+
const o = { mode, theme: c.theme!, css } as const;
|
|
45
58
|
s.rendered.svg = resolveSvg(s.rendered.svg, o);
|
|
46
59
|
if (s.rendered.variants) s.rendered.variants = Object.fromEntries(Object.entries(s.rendered.variants).map(([k, v]) => [k, resolveSvg(v, o)]));
|
|
47
60
|
s.rendered.inks = { surface: c.theme![o.mode].surface ?? c.theme!.light.surface, control: c.theme![o.mode].control ?? c.theme!.light.control,
|
|
48
61
|
kinds: Object.fromEntries(Object.entries(c.theme!.kinds).map(([k, v]) => [k, c.theme![o.mode][v.color] ?? v.color])) };
|
|
49
62
|
}
|
|
63
|
+
// its connectors march in their own rhythms
|
|
64
|
+
if (opts.resolveVars && treatment && theme) c.theme = { ...theme, kinds: Object.fromEntries(Object.entries(theme.kinds).map(([k, v]) => { const d = c.scenes.map(s => s.rendered.mono?.kinds[k]?.dash).find(x => x && x.length > 1); return [k, d ? { ...v, ants: [d[0], d[1]] as [number, number] } : v]; })) };
|
|
50
65
|
return c;
|
|
51
66
|
}
|
|
52
67
|
|
|
68
|
+
/** a scene in black and white: its drawing and its looks alike, by one table */
|
|
69
|
+
function toMono(s: CompiledScene, theme: Theme, mode: ThemeMode, css: string, own?: MonoTable) {
|
|
70
|
+
const all = [s.rendered.svg, ...Object.values(s.rendered.variants ?? {})];
|
|
71
|
+
const m = monoOfDrawing(all.join(''), theme, mode, [...s.routes.map(r => r.kind), ...s.steps.kindsUsed], own);
|
|
72
|
+
const draw = (svg: string) => paintMono(resolveSvg(svg, { mode, theme: m.theme, css: css + m.css }), m.look, s.id);
|
|
73
|
+
s.rendered.svg = draw(s.rendered.svg);
|
|
74
|
+
if (s.rendered.variants) s.rendered.variants = Object.fromEntries(Object.entries(s.rendered.variants).map(([k, v]) => [k, draw(v)]));
|
|
75
|
+
s.rendered.inks = { surface: m.look.paper, control: m.look.ink, kinds: Object.fromEntries(Object.keys(theme.kinds).map(k => [k, m.look.ink])) };
|
|
76
|
+
s.rendered.mono = m.look;
|
|
77
|
+
for (const w of m.look.warnings) s.rendered.warnings.push({ path: '', message: `black and white: ${w}` });
|
|
78
|
+
}
|
|
79
|
+
|
|
53
80
|
function compileAll(source: string, themeText: string, opts: { snap?: number }): Compiled {
|
|
54
81
|
const parsed = parse(source);
|
|
55
82
|
const diags: Diag[] = [...parsed.diags];
|
|
@@ -6,12 +6,16 @@ import { svgAt } from '../iso-objects/motion.ts';
|
|
|
6
6
|
import { bindMotion } from '../iso-objects/timeline.ts';
|
|
7
7
|
import type { MotionDoc } from '../iso-objects/motion-doc.ts';
|
|
8
8
|
import { compile } from './compile.ts';
|
|
9
|
+
import type { MonoTable } from '../iso-core/mono.ts';
|
|
9
10
|
import type { ThemeMode } from '../iso-objects/resolve.ts';
|
|
10
11
|
|
|
11
12
|
export interface RenderSvgOptions {
|
|
12
13
|
/** the theme's text */
|
|
13
14
|
theme: string;
|
|
14
15
|
mode?: ThemeMode;
|
|
16
|
+
/** black and white, in either mode, and a host's own table for it */
|
|
17
|
+
treatment?: 'mono' | 'none';
|
|
18
|
+
mono?: MonoTable;
|
|
15
19
|
/** which scene: its id or its place (default: the first) */
|
|
16
20
|
scene?: string | number;
|
|
17
21
|
/** a moment of its motion, ms: the still without it */
|
|
@@ -27,7 +31,7 @@ export interface RenderSvgOptions {
|
|
|
27
31
|
|
|
28
32
|
/** Throws, with every fault and its line, when the file or its motion has one. */
|
|
29
33
|
export function renderSvg(source: string, o: RenderSvgOptions): string {
|
|
30
|
-
const c = compile(source, o.theme, { mode: o.mode ?? 'light', resolveVars: true, strict: 'throw' }), kinds = c.theme!.kinds;
|
|
34
|
+
const c = compile(source, o.theme, { mode: o.mode ?? 'light', resolveVars: true, strict: 'throw', treatment: o.treatment, mono: o.mono }), kinds = c.theme!.kinds;
|
|
31
35
|
const s = o.scene === undefined ? c.scenes[0] : typeof o.scene === 'number' ? c.scenes[o.scene] : c.scenes.find(x => x.id === o.scene);
|
|
32
36
|
if (!s) throw new Error(`no scene ${JSON.stringify(o.scene)} (there ${c.scenes.length === 1 ? 'is' : 'are'} ${c.scenes.map(x => x.id).join(', ')})`);
|
|
33
37
|
const ants = o.ants === undefined ? antsOf(s.raw) : o.ants === true ? antsOf(s.raw) ?? {} : o.ants || undefined;
|
|
@@ -26,6 +26,7 @@
|
|
|
26
26
|
"grid": { "$ref": "#/$defs/grid" },
|
|
27
27
|
"assemble": { "type": "boolean", "description": "draw each scene by flying its parts in from the nearest edge, then play" },
|
|
28
28
|
"footer": { "type": "string" },
|
|
29
|
+
"treatment": { "enum": ["mono"], "description": "How a self-contained drawing (renderSvg, <isometric-diagram>) is painted: mono, black and white. A host may say otherwise. The page isodiag build writes is in colour." },
|
|
29
30
|
"scenes": { "type": "array", "minItems": 1, "items": { "$ref": "#/$defs/scene" } }
|
|
30
31
|
},
|
|
31
32
|
"$defs": {
|
package/src/iso-viewer/index.ts
CHANGED
|
@@ -17,6 +17,10 @@ export { isoVars, scopeSelector, scope } from './view-css.ts';
|
|
|
17
17
|
export { md, stepData, legend, renderPage } from './page.ts';
|
|
18
18
|
export type { PageOpts } from './page.ts';
|
|
19
19
|
export { playerScript } from './bundle.ts';
|
|
20
|
+
export { transport, pageClock } from './transport.ts';
|
|
21
|
+
export type { Transport, TransportClock, TransportEvents, TransportOptions } from './transport.ts';
|
|
22
|
+
export { mountPlayer, PLAY_CSS } from './play.ts';
|
|
23
|
+
export type { Player, PlayerOptions } from './play.ts';
|
|
20
24
|
export { h } from './dom.ts';
|
|
21
25
|
export { TIMELINE_VERSION, SEGMENT_NAME, SEVERITIES, BINS, BIN_MS, scopePattern } from './timeline-contract.ts';
|
|
22
26
|
export type { TimelineTypes, AnyTimeline, Scope, Severity, AttrValue, TimelineEvent, Bin, MetricPoint, MetricRollup, EventBin, Match, AstTerm, SearchAst, Days, Retention, SeriesInfo, KindInfo, SourceInfo, EffectiveRetention, ScopeName, Catalog, Filters, Query, Row, Result, Batch, View as TimelineView, Recording, TimelineSource, TimelineCatalog } from './timeline-contract.ts';
|
package/src/iso-viewer/page.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
// The HTML page around the scenes: header, one section per scene, theme toggle. One file, no network.
|
|
2
2
|
|
|
3
|
-
import {
|
|
3
|
+
import { bindMotion, motionOfSteps, motionSource } from '../iso-objects/timeline.ts';
|
|
4
|
+
import type { StepData } from '../iso-objects/steps.ts';
|
|
4
5
|
import { esc } from '../iso-core/kernel.ts';
|
|
5
6
|
import type { Compiled } from '../iso-objects/compile.ts';
|
|
6
7
|
import { themeCss } from '../iso-core/theme.ts';
|
|
@@ -26,14 +27,29 @@ const paras = (s: string | undefined) => (s ?? '').split(/\n\s*\n/).filter(Boole
|
|
|
26
27
|
|
|
27
28
|
export interface PageOpts { runtime?: string; extraHead?: string; extraBody?: string }
|
|
28
29
|
|
|
30
|
+
/** a scene's own motion, where it has any that binds: what its page plays, in place of its steps */
|
|
31
|
+
const ownMotion = (c: Compiled, s: CompiledScene) => !!s.motion && bindMotion(s, s.motion, { kinds: c.theme!.kinds }).ok;
|
|
32
|
+
/** what the page steps through: the scene's steps, or the beats of its own motion */
|
|
33
|
+
function stepsOf(c: Compiled, s: CompiledScene): StepData[] {
|
|
34
|
+
if (!ownMotion(c, s)) return s.steps.steps;
|
|
35
|
+
return bindMotion(s, s.motion!, { kinds: c.theme!.kinds }).beats.map(b => ({
|
|
36
|
+
n: b.index + 1, num: b.index + 1, kind: 'control', color: b.say?.color ?? colorRef(c.theme!, c.theme!.kinds.control?.color ?? 'control'),
|
|
37
|
+
heading: b.say?.title ?? b.name, caption: b.say?.text ?? '', command: b.say?.code ?? '', phases: [], pulses: [], lit: [], duration: b.end - b.start,
|
|
38
|
+
}));
|
|
39
|
+
}
|
|
40
|
+
|
|
29
41
|
/** The data the player runs on, for every scene with steps. */
|
|
30
42
|
export function stepData(c: Compiled): string {
|
|
31
43
|
return JSON.stringify({
|
|
32
|
-
scenes: c.scenes.map(s =>
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
44
|
+
scenes: c.scenes.map(s => {
|
|
45
|
+
const own = ownMotion(c, s), steps = stepsOf(c, s), doc = own ? s.motion : s.steps.steps.length ? motionOfSteps(s) : undefined;
|
|
46
|
+
return {
|
|
47
|
+
id: s.id, loop: s.raw.loop ?? true, dim: s.raw.dim ?? 'flows', vis: s.steps.vis,
|
|
48
|
+
// (where a step's view goes is its document's camera)
|
|
49
|
+
steps: steps.map(({ focus: _, ...st }) => ({ ...st, caption: md(st.caption) })),
|
|
50
|
+
assemble: s.assemble, grid: !!s.grid.show, source: motionSource(s), ...(doc ? { doc } : {}), ...(own ? { own } : {}),
|
|
51
|
+
};
|
|
52
|
+
}),
|
|
37
53
|
});
|
|
38
54
|
}
|
|
39
55
|
|
|
@@ -58,8 +74,8 @@ export function legend(c: Compiled, s: CompiledScene): string {
|
|
|
58
74
|
return items.join('');
|
|
59
75
|
}
|
|
60
76
|
|
|
61
|
-
function player(s: CompiledScene): string {
|
|
62
|
-
const st = s
|
|
77
|
+
function player(c: Compiled, s: CompiledScene): string {
|
|
78
|
+
const st = stepsOf(c, s);
|
|
63
79
|
const tools = `<div class="tools">
|
|
64
80
|
<button class="pbtn small" type="button" data-a="grid" aria-pressed="${s.grid.show ? 'true' : 'false'}" title="Show the floor grid">Grid</button>
|
|
65
81
|
<button class="pbtn small" type="button" data-a="assemble" title="Draw the diagram again, piece by piece">Assemble</button>
|
|
@@ -83,8 +99,8 @@ function player(s: CompiledScene): string {
|
|
|
83
99
|
</div>`;
|
|
84
100
|
}
|
|
85
101
|
|
|
86
|
-
function stepList(s: CompiledScene): string {
|
|
87
|
-
const st = s
|
|
102
|
+
function stepList(c: Compiled, s: CompiledScene): string {
|
|
103
|
+
const st = stepsOf(c, s);
|
|
88
104
|
if (!st.length) return '';
|
|
89
105
|
return `<details class="steps"><summary>All ${st.length} steps as text</summary><ol>${st.map(x =>
|
|
90
106
|
`<li value="${x.num}"><b>${esc(x.heading)}.</b> ${md(x.caption)}${x.command ? `<br><code>${esc(x.command)}</code>` : ''}</li>`).join('')}</ol></details>`;
|
|
@@ -99,10 +115,10 @@ export function renderPage(c: Compiled, opts: PageOpts = {}): string {
|
|
|
99
115
|
${paras(s.raw.caption)}
|
|
100
116
|
<div class="figure" tabindex="-1">
|
|
101
117
|
<div class="scroller">${s.grid.show ? s.rendered.svg.replace('<svg ', '<svg class="show-grid" ') : s.rendered.svg}</div>
|
|
102
|
-
${player(s)}
|
|
118
|
+
${player(c, s)}
|
|
103
119
|
<div class="legend">${legend(c, s)}</div>
|
|
104
120
|
</div>
|
|
105
|
-
${stepList(s)}
|
|
121
|
+
${stepList(c, s)}
|
|
106
122
|
${s.raw.after ? `<div class="after">${paras(s.raw.after)}</div>` : ''}
|
|
107
123
|
</section>`).join('\n');
|
|
108
124
|
return `<!doctype html>
|
|
@@ -0,0 +1,280 @@
|
|
|
1
|
+
// The player (docs/design/MOTION.md, 6.2): a skin over a transport. The drawing, play and pause, back and next, the beats as dots by
|
|
2
|
+
// chapter, a scrubber, the commentary with its facts, the ambient switch, a text list of the beats. It owns no clock (transport.ts does)
|
|
3
|
+
// and draws nothing of its own: what shows is paint(svg, timeline.at(…)). Built as DOM (no HTML text but the commentary's own small
|
|
4
|
+
// Markdown, escaped), no style attributes, no storage, nothing fetched, so it runs under a strict CSP; its stylesheet is the host's to add
|
|
5
|
+
// (PLAY_CSS, in a page or a shadow root). The drawing is a self-contained one (compile's resolveVars), so no theme's CSS is needed.
|
|
6
|
+
|
|
7
|
+
import { paint, type SceneState } from '../iso-objects/motion.ts';
|
|
8
|
+
import type { CompiledScene } from '../iso-objects/compile.ts';
|
|
9
|
+
import type { BeatInfo, Timeline } from '../iso-objects/timeline.ts';
|
|
10
|
+
import { h } from './dom.ts';
|
|
11
|
+
import { md } from './page.ts';
|
|
12
|
+
import { transport, type Transport, type TransportClock } from './transport.ts';
|
|
13
|
+
|
|
14
|
+
export interface PlayerOptions {
|
|
15
|
+
scene: Pick<CompiledScene, 'rendered' | 'id'>;
|
|
16
|
+
/** none: the drawing alone, with nothing to play */
|
|
17
|
+
timeline?: Timeline;
|
|
18
|
+
/** what a screen reader calls it */
|
|
19
|
+
label?: string;
|
|
20
|
+
/** the controls and the commentary (default true): without them, the drawing alone */
|
|
21
|
+
controls?: boolean;
|
|
22
|
+
/** start at once, or each time most of it is in view (default: neither) */
|
|
23
|
+
autoplay?: boolean | 'visible';
|
|
24
|
+
loop?: boolean;
|
|
25
|
+
rate?: number;
|
|
26
|
+
ambient?: 'on' | 'off';
|
|
27
|
+
/** a reader who asked for less motion (default 'ignore': motion plays). 'respect': the last state, everything lit, the ambient off, until they press Play */
|
|
28
|
+
reducedMotion?: 'ignore' | 'respect';
|
|
29
|
+
mode?: 'light' | 'dark';
|
|
30
|
+
/** the timeline is the drawing's own steps (motionOfSteps): its callouts and marks show at their step, and the other steps' numbers dim */
|
|
31
|
+
steps?: boolean;
|
|
32
|
+
clock?: TransportClock;
|
|
33
|
+
}
|
|
34
|
+
export interface Player {
|
|
35
|
+
readonly root: HTMLElement;
|
|
36
|
+
readonly transport: Transport;
|
|
37
|
+
/** another drawing, or another document: the place is kept (transport's rebind). Faults are refused, and what plays goes on */
|
|
38
|
+
set(scene: Pick<CompiledScene, 'rendered' | 'id'>, timeline?: Timeline, mode?: 'light' | 'dark'): boolean;
|
|
39
|
+
/** show one moment, still: no clock, no controls (a screenshot, a link) */
|
|
40
|
+
freeze(ms: number): void;
|
|
41
|
+
/** to a beat and through it, stopping before the next */
|
|
42
|
+
step(beat: number): void;
|
|
43
|
+
dispose(): void;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** which of the page's players plays: the one most in view (an autoplaying one that is not stops) */
|
|
47
|
+
const mounted = new Map<HTMLElement, { seen: number; auto(on: boolean): void; wants: boolean }>();
|
|
48
|
+
function pick() {
|
|
49
|
+
let best: HTMLElement | undefined, most = 0.35;
|
|
50
|
+
for (const [el, m] of mounted) if (m.wants && m.seen >= most && (m.seen > most || !best)) { best = el; most = m.seen; }
|
|
51
|
+
for (const [el, m] of mounted) if (m.wants) m.auto(el === best);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const svgOf = (text: string): SVGSVGElement => {
|
|
55
|
+
const doc = new DOMParser().parseFromString(text, 'image/svg+xml');
|
|
56
|
+
if (doc.documentElement.localName !== 'svg') throw new Error('the drawing is not SVG');
|
|
57
|
+
return document.importNode(doc.documentElement, true) as unknown as SVGSVGElement;
|
|
58
|
+
};
|
|
59
|
+
const clockText = (ms: number) => { const s = Math.round(ms / 1000); return `${Math.floor(s / 60)}:${String(s % 60).padStart(2, '0')}`; };
|
|
60
|
+
/** a drawing with nothing to play */
|
|
61
|
+
const NOTHING: Timeline = { ok: true, diags: [], duration: 0, beats: [], markers: {}, ambient: 'on', loops: false, at: () => ({}), cues: () => undefined, beatAt: () => undefined, startOf: () => undefined };
|
|
62
|
+
/** everything still hidden stays hidden; what is only dimmed is lit */
|
|
63
|
+
function lit(S: SceneState): SceneState {
|
|
64
|
+
const out: SceneState = { ...(S.camera ? { camera: S.camera } : {}), ...(S.variants ? { variants: S.variants } : {}) };
|
|
65
|
+
for (const k of ['items', 'labels', 'routes'] as const) {
|
|
66
|
+
const gone = Object.entries(S[k] ?? {}).filter(([, v]) => v.opacity === 0).map(([id]) => [id, { opacity: 0 }]);
|
|
67
|
+
if (gone.length) out[k] = Object.fromEntries(gone);
|
|
68
|
+
}
|
|
69
|
+
return out;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export function mountPlayer(host: HTMLElement | ShadowRoot, o: PlayerOptions): Player {
|
|
73
|
+
if (o.timeline && !o.timeline.ok) throw new Error(`the motion has faults: ${o.timeline.diags.map(d => `${d.path}: ${d.message}`).join('; ')}`);
|
|
74
|
+
let scene = o.scene, svg = svgOf(scene.rendered.svg), frozen: number | undefined, stepTo: number | undefined, userPaused = false, gone = false;
|
|
75
|
+
const tr = transport(o.timeline ?? NOTHING, { loop: o.loop, rate: o.rate, ambient: o.ambient, clock: o.clock });
|
|
76
|
+
const controls = o.controls ?? true;
|
|
77
|
+
const calm = o.reducedMotion === 'respect' && typeof matchMedia === 'function' && matchMedia('(prefers-reduced-motion: reduce)').matches;
|
|
78
|
+
let resting = calm;
|
|
79
|
+
|
|
80
|
+
// ---- the parts ----
|
|
81
|
+
const btn = (cls: string, label: string, text: string, f: () => void) => h('button', { type: 'button', class: `iso-play-btn ${cls}`, 'aria-label': label, title: label, on: { click: f as EventListener } }, text);
|
|
82
|
+
const stage = h('div', { class: 'iso-play-stage' }, svg as unknown as Node);
|
|
83
|
+
const back = btn('back', 'Back', '‹', () => byHand(() => step(Math.max(0, at() - 1))));
|
|
84
|
+
const play = btn('play', 'Play', '▶', () => toggle());
|
|
85
|
+
const next = btn('next', 'Next', '›', () => byHand(() => step(Math.min(tr.timeline.beats.length - 1, at() + 1))));
|
|
86
|
+
const again = btn('again', 'Reset', '↺', () => byHand(() => { stop(); tr.restart(); }));
|
|
87
|
+
const amb = btn('ambient', 'Ambient motion', '∿', () => { wake(); tr.ambient = !tr.ambient; show(); });
|
|
88
|
+
const bar = h('input', { type: 'range', class: 'iso-play-bar', min: 0, step: 1, 'aria-label': 'Time', on: { input: (() => byHand(() => { stop(); tr.seek(Number(bar.value)); })) as EventListener } });
|
|
89
|
+
const time = h('span', { class: 'iso-play-time' });
|
|
90
|
+
const dots = h('div', { class: 'iso-play-dots' });
|
|
91
|
+
const num = h('span', { class: 'iso-play-num' }), title = h('span', { class: 'iso-play-title' }), chapter = h('span', { class: 'iso-play-chapter' }), count = h('span', { class: 'iso-play-count' });
|
|
92
|
+
const text = h('div', { class: 'iso-play-text' }), code = h('pre', { class: 'iso-play-code' }), facts = h('dl', { class: 'iso-play-facts' });
|
|
93
|
+
const say = h('div', { class: 'iso-play-say', 'aria-live': 'polite' }, h('div', { class: 'iso-play-head' }, num, title, chapter, count), text, code, facts);
|
|
94
|
+
const list = h('details', { class: 'iso-play-list' });
|
|
95
|
+
const row = h('div', { class: 'iso-play-row' }, back, play, next, again, amb, bar, time);
|
|
96
|
+
const root = h('div', { class: 'iso-play', role: 'group', 'aria-roledescription': 'animated diagram', 'aria-label': o.label ?? 'Diagram', tabindex: controls ? 0 : undefined, 'data-mode': o.mode ?? 'light' },
|
|
97
|
+
stage, controls && row, controls && dots, controls && say, controls && list);
|
|
98
|
+
host.append(root);
|
|
99
|
+
|
|
100
|
+
// ---- what there is to show: the beats ----
|
|
101
|
+
let dotOf: HTMLButtonElement[] = [];
|
|
102
|
+
function beats() {
|
|
103
|
+
const all = tr.timeline.beats, some = all.length > 0, said = all.some(b => b.say);
|
|
104
|
+
dotOf = [];
|
|
105
|
+
dots.replaceChildren();
|
|
106
|
+
let group: HTMLElement | undefined, last: string | undefined;
|
|
107
|
+
for (const b of all) {
|
|
108
|
+
if (!group || b.say?.chapter !== last) { group = h('span', { class: 'iso-play-group', role: 'group', 'aria-label': b.say?.chapter, title: b.say?.chapter }); dots.append(group); last = b.say?.chapter; }
|
|
109
|
+
const d = h('button', { type: 'button', class: 'iso-play-dot', 'aria-label': `Step ${b.index + 1}: ${b.say?.title ?? b.name}`, title: b.say?.title ?? b.name, on: { click: (() => byHand(() => step(b.index))) as EventListener } });
|
|
110
|
+
group.append(d); dotOf.push(d);
|
|
111
|
+
}
|
|
112
|
+
list.replaceChildren(h('summary', null, `All ${all.length} steps as text`), h('ol', null, ...all.map(b => h('li', null, ...words(b)))));
|
|
113
|
+
for (const el of [back, next, dots]) el.hidden = !some;
|
|
114
|
+
say.hidden = !said; list.hidden = !said;
|
|
115
|
+
amb.hidden = !tr.timeline.loops;
|
|
116
|
+
bar.max = String(Math.round(tr.duration));
|
|
117
|
+
for (const el of [play, again, bar, time]) el.hidden = tr.duration <= 0;
|
|
118
|
+
told = undefined;
|
|
119
|
+
}
|
|
120
|
+
function words(b: BeatInfo): Node[] {
|
|
121
|
+
const s = b.say, out: Node[] = [h('b', null, s?.title ?? b.name)];
|
|
122
|
+
if (s?.text) { const p = h('div', null); p.innerHTML = md(s.text); out.push(p); }
|
|
123
|
+
if (s?.code) out.push(h('code', null, s.code));
|
|
124
|
+
if (s?.facts?.length) out.push(h('span', null, s.facts.map(([k, v]) => `${k}: ${v}`).join(' · ')));
|
|
125
|
+
return out;
|
|
126
|
+
}
|
|
127
|
+
let told: BeatInfo | undefined;
|
|
128
|
+
/** what is drawn for one step alone: the rest of it is the Timeline's */
|
|
129
|
+
function marks(b: BeatInfo | undefined) {
|
|
130
|
+
const n = String((b?.index ?? -1) + 1);
|
|
131
|
+
for (const e of svg.querySelectorAll('[data-badge]')) {
|
|
132
|
+
e.classList.toggle('on', e.getAttribute('data-badge') === n);
|
|
133
|
+
if (o.steps) { if (e.getAttribute('data-badge') === n || resting) e.removeAttribute('opacity'); else e.setAttribute('opacity', '0.14'); }
|
|
134
|
+
}
|
|
135
|
+
if (o.steps) for (const e of svg.querySelectorAll('[data-step]')) e.setAttribute('visibility', e.getAttribute('data-step') === n && !resting ? 'visible' : 'hidden');
|
|
136
|
+
}
|
|
137
|
+
function caption() {
|
|
138
|
+
const b = tr.beat;
|
|
139
|
+
if (b === told) return;
|
|
140
|
+
told = b;
|
|
141
|
+
marks(b);
|
|
142
|
+
if (!controls) return;
|
|
143
|
+
const s = b?.say;
|
|
144
|
+
num.textContent = b ? String(b.index + 1) : '';
|
|
145
|
+
num.style.backgroundColor = s?.color ?? '';
|
|
146
|
+
title.textContent = s?.title ?? b?.name ?? '';
|
|
147
|
+
chapter.textContent = s?.chapter ?? '';
|
|
148
|
+
count.textContent = b ? `${b.index + 1} / ${tr.timeline.beats.length}` : '';
|
|
149
|
+
text.innerHTML = md(s?.text);
|
|
150
|
+
code.textContent = s?.code ?? '';
|
|
151
|
+
facts.replaceChildren(...(s?.facts ?? []).flatMap(([k, v]) => [h('dt', null, k), h('dd', null, v)]));
|
|
152
|
+
code.hidden = !s?.code; facts.hidden = !s?.facts?.length;
|
|
153
|
+
dotOf.forEach((d, i) => { d.classList.toggle('on', i === b?.index); d.classList.toggle('done', b !== undefined && i < b.index); if (i === b?.index) d.setAttribute('aria-current', 'step'); else d.removeAttribute('aria-current'); });
|
|
154
|
+
}
|
|
155
|
+
function show() {
|
|
156
|
+
const S = frozen !== undefined ? tr.timeline.at(frozen, { ambient: frozen }) : resting ? lit(tr.timeline.at(tr.duration, { ambient: 'off' })) : tr.state();
|
|
157
|
+
paint(svg, S, scene.rendered, { styles: 'cssom' });
|
|
158
|
+
root.dataset.time = String(Math.round(frozen ?? tr.time));
|
|
159
|
+
if (frozen !== undefined) marks(tr.timeline.beatAt(frozen)); else caption();
|
|
160
|
+
if (!controls) return;
|
|
161
|
+
bar.value = String(Math.round(tr.time));
|
|
162
|
+
bar.setAttribute('aria-valuetext', `${clockText(tr.time)} of ${clockText(tr.duration)}${tr.beat ? `, ${tr.beat.say?.title ?? tr.beat.name}` : ''}`);
|
|
163
|
+
time.textContent = `${clockText(tr.time)} / ${clockText(tr.duration)}`;
|
|
164
|
+
}
|
|
165
|
+
function buttons() {
|
|
166
|
+
const on = going();
|
|
167
|
+
play.textContent = on ? '❚❚' : '▶';
|
|
168
|
+
play.setAttribute('aria-label', on ? 'Pause' : 'Play'); play.title = on ? 'Pause' : 'Play';
|
|
169
|
+
amb.setAttribute('aria-pressed', String(tr.ambient));
|
|
170
|
+
say.setAttribute('aria-live', on ? 'off' : 'polite');
|
|
171
|
+
root.dataset.playing = String(on);
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
// ---- playing ----
|
|
175
|
+
/** playing on and on, not only through one step */
|
|
176
|
+
const going = () => tr.playing && stepTo === undefined;
|
|
177
|
+
const at = () => tr.beat?.index ?? 0;
|
|
178
|
+
/** a reader who asked for less motion has asked for some */
|
|
179
|
+
function wake() { if (resting) { resting = false; told = undefined; } }
|
|
180
|
+
/** the reader did it: an autoplay doesn't start again by itself */
|
|
181
|
+
function byHand(f: () => void) { wake(); userPaused = true; f(); buttons(); show(); }
|
|
182
|
+
function stop() { stepTo = undefined; tr.pause(); }
|
|
183
|
+
function step(i: number) {
|
|
184
|
+
const b = tr.timeline.beats[i];
|
|
185
|
+
if (!b || gone) return;
|
|
186
|
+
tr.pause();
|
|
187
|
+
tr.seek(b.start, { join: true });
|
|
188
|
+
const nextStart = tr.timeline.beats[i + 1]?.start ?? Infinity;
|
|
189
|
+
// through the beat, and to a stop within it (its end is the next one's start)
|
|
190
|
+
stepTo = Math.max(b.start, Math.min(b.end, nextStart, tr.duration) - 1);
|
|
191
|
+
tr.play({ until: stepTo });
|
|
192
|
+
}
|
|
193
|
+
function toggle() {
|
|
194
|
+
wake();
|
|
195
|
+
if (going()) { userPaused = true; stop(); } else { userPaused = false; stepTo = undefined; tr.pause(); tr.play(); }
|
|
196
|
+
buttons(); show();
|
|
197
|
+
}
|
|
198
|
+
tr.on('time', show);
|
|
199
|
+
tr.on('play', buttons); tr.on('ambient', buttons);
|
|
200
|
+
tr.on('pause', () => { stepTo = undefined; buttons(); });
|
|
201
|
+
root.addEventListener('keydown', e => {
|
|
202
|
+
const k = e.key, t = e.target as Element;
|
|
203
|
+
if (e.altKey || e.ctrlKey || e.metaKey || t === bar) return;
|
|
204
|
+
if (k === 'ArrowRight' || k === 'ArrowLeft') { e.preventDefault(); (k === 'ArrowRight' ? next : back).click(); }
|
|
205
|
+
else if (k === 'Home') { e.preventDefault(); again.click(); }
|
|
206
|
+
else if ((k === ' ' || k === 'k') && t === root) { e.preventDefault(); toggle(); }
|
|
207
|
+
});
|
|
208
|
+
const onBadge = (e: Event) => {
|
|
209
|
+
const b = (e.target as Element).closest?.('[data-go]'), key = (e as KeyboardEvent).key;
|
|
210
|
+
if (!b || (e.type === 'keydown' && key !== 'Enter' && key !== ' ')) return;
|
|
211
|
+
e.preventDefault(); e.stopPropagation();
|
|
212
|
+
byHand(() => step(Number(b.getAttribute('data-go')) - 1));
|
|
213
|
+
};
|
|
214
|
+
stage.addEventListener('click', onBadge); stage.addEventListener('keydown', onBadge);
|
|
215
|
+
|
|
216
|
+
// ---- in view: neither clock moves for a player nobody can see; one that autoplays in view is one at a time ----
|
|
217
|
+
const me = {
|
|
218
|
+
seen: 0, wants: o.autoplay === 'visible' && !calm,
|
|
219
|
+
auto(on: boolean) { if (on) { if (!userPaused && !going()) { stepTo = undefined; tr.play(); } } else if (going()) { stepTo = undefined; tr.pause(); } },
|
|
220
|
+
};
|
|
221
|
+
mounted.set(root, me);
|
|
222
|
+
const io = typeof IntersectionObserver === 'function' ? new IntersectionObserver(es => { for (const e of es) { me.seen = e.isIntersecting ? e.intersectionRatio : 0; if (frozen === undefined) tr.active = e.isIntersecting; } pick(); }, { threshold: [0, 0.2, 0.35, 0.5, 0.7, 0.9, 1] }) : undefined;
|
|
223
|
+
io?.observe(root);
|
|
224
|
+
|
|
225
|
+
beats(); buttons(); show();
|
|
226
|
+
if (o.autoplay === true && !calm) tr.play();
|
|
227
|
+
return {
|
|
228
|
+
root, transport: tr, step: i => byHand(() => step(i)),
|
|
229
|
+
set(s, tl = NOTHING, mode) {
|
|
230
|
+
if (gone || !tl.ok) return false;
|
|
231
|
+
const fresh = svgOf(s.rendered.svg);
|
|
232
|
+
scene = s; svg.replaceWith(fresh); svg = fresh;
|
|
233
|
+
if (mode) root.dataset.mode = mode;
|
|
234
|
+
if (stepTo !== undefined) stop();
|
|
235
|
+
tr.rebind(tl);
|
|
236
|
+
beats(); buttons(); show();
|
|
237
|
+
return true;
|
|
238
|
+
},
|
|
239
|
+
freeze(ms) { frozen = ms; tr.pause(); tr.active = false; root.classList.add('frozen'); show(); },
|
|
240
|
+
dispose() { gone = true; io?.disconnect(); mounted.delete(root); tr.dispose(); root.remove(); pick(); },
|
|
241
|
+
};
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
/** The player's stylesheet: its own, under one class; the colours are custom properties a host may set. */
|
|
245
|
+
export const PLAY_CSS = `
|
|
246
|
+
.iso-play{--ip-ink:#1c2026;--ip-muted:#68707c;--ip-line:#dfe3e8;--ip-bg:#fff;--ip-code:#f2f4f7;--ip-focus:#2a64d6;container:iso-play/inline-size;display:block;color:var(--ip-ink);background:var(--ip-bg);font:14px/1.5 system-ui,-apple-system,"Segoe UI",sans-serif;border-radius:10px}
|
|
247
|
+
.iso-play[data-mode="dark"]{--ip-ink:#e6e9ee;--ip-muted:#98a1ae;--ip-line:#333a44;--ip-bg:#15181d;--ip-code:#1f242b;--ip-focus:#7aa7ff;color-scheme:dark}
|
|
248
|
+
.iso-play:focus-visible,.iso-play button:focus-visible,.iso-play input:focus-visible,.iso-play summary:focus-visible{outline:2px solid var(--ip-focus);outline-offset:2px}
|
|
249
|
+
.iso-play [hidden]{display:none}
|
|
250
|
+
.iso-play-stage svg{display:block;width:100%;height:auto}
|
|
251
|
+
.iso-play-stage [data-go]{cursor:pointer}
|
|
252
|
+
.iso-play-row{display:flex;align-items:center;gap:6px;padding:8px 12px 4px}
|
|
253
|
+
.iso-play-btn{font:inherit;min-width:32px;height:32px;padding:0 8px;border:1px solid var(--ip-line);border-radius:8px;background:none;color:inherit;cursor:pointer}
|
|
254
|
+
.iso-play-btn:hover{border-color:var(--ip-muted)}
|
|
255
|
+
.iso-play-btn.play{background:var(--ip-ink);color:var(--ip-bg);border-color:var(--ip-ink);min-width:44px}
|
|
256
|
+
.iso-play-btn.ambient[aria-pressed="false"]{color:var(--ip-muted);text-decoration:line-through}
|
|
257
|
+
.iso-play-bar{flex:1 1 60px;min-width:0;accent-color:var(--ip-ink)}
|
|
258
|
+
.iso-play-time{color:var(--ip-muted);font-size:12px;font-variant-numeric:tabular-nums;white-space:nowrap}
|
|
259
|
+
.iso-play-dots{display:flex;flex-wrap:wrap;gap:4px 12px;padding:2px 12px}
|
|
260
|
+
.iso-play-group{display:inline-flex;gap:0}
|
|
261
|
+
.iso-play-dot{width:24px;height:24px;padding:0;border:0;background:none;cursor:pointer;display:grid;place-items:center}
|
|
262
|
+
.iso-play-dot::before{content:"";width:9px;height:9px;border-radius:50%;background:var(--ip-line)}
|
|
263
|
+
.iso-play-dot.done::before{background:var(--ip-muted)}
|
|
264
|
+
.iso-play-dot.on::before{background:var(--ip-ink);width:11px;height:11px}
|
|
265
|
+
.iso-play-say{padding:4px 12px 10px;min-height:5.2em}
|
|
266
|
+
.iso-play-head{display:flex;align-items:baseline;gap:8px;font-weight:700}
|
|
267
|
+
.iso-play-num{flex:none;display:inline-grid;place-items:center;min-width:22px;height:22px;border-radius:11px;background:var(--ip-ink);color:var(--ip-bg);font-size:11px;align-self:center}
|
|
268
|
+
.iso-play-chapter,.iso-play-count{color:var(--ip-muted);font-size:12px;font-weight:500}
|
|
269
|
+
.iso-play-count{margin-left:auto;white-space:nowrap}
|
|
270
|
+
.iso-play-text{max-width:80ch}
|
|
271
|
+
.iso-play-text a{color:inherit}
|
|
272
|
+
.iso-play-code{margin:6px 0 0;padding:6px 10px;border-radius:8px;background:var(--ip-code);font:12px/1.5 ui-monospace,Menlo,monospace;white-space:pre-wrap;overflow-wrap:anywhere}
|
|
273
|
+
.iso-play-facts{display:flex;flex-wrap:wrap;gap:2px 16px;margin:6px 0 0;font-size:12px}
|
|
274
|
+
.iso-play-facts dt{color:var(--ip-muted)}
|
|
275
|
+
.iso-play-facts dd{margin:0 8px 0 -10px;font-weight:600}
|
|
276
|
+
.iso-play-list{padding:0 12px 10px;font-size:13px}
|
|
277
|
+
.iso-play-list summary{color:var(--ip-muted);cursor:pointer}
|
|
278
|
+
.iso-play.frozen>:not(.iso-play-stage){display:none}
|
|
279
|
+
@container iso-play (max-width:420px){.iso-play-time{display:none}.iso-play-row{flex-wrap:wrap}.iso-play-bar{flex-basis:100%;order:9}}
|
|
280
|
+
`;
|