@metronforge/concept-map 0.0.0-stage → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +109 -2
- package/css/concept-map.css +903 -0
- package/dist/dom/animator.d.ts +25 -0
- package/dist/dom/animator.d.ts.map +1 -0
- package/dist/dom/animator.js +124 -0
- package/dist/dom/animator.js.map +1 -0
- package/dist/dom/geometry.d.ts +15 -0
- package/dist/dom/geometry.d.ts.map +1 -0
- package/dist/dom/geometry.js +52 -0
- package/dist/dom/geometry.js.map +1 -0
- package/dist/dom/panelDom.d.ts +15 -0
- package/dist/dom/panelDom.d.ts.map +1 -0
- package/dist/dom/panelDom.js +61 -0
- package/dist/dom/panelDom.js.map +1 -0
- package/dist/dom/player.d.ts +40 -0
- package/dist/dom/player.d.ts.map +1 -0
- package/dist/dom/player.js +273 -0
- package/dist/dom/player.js.map +1 -0
- package/dist/dom/svgDom.d.ts +19 -0
- package/dist/dom/svgDom.d.ts.map +1 -0
- package/dist/dom/svgDom.js +70 -0
- package/dist/dom/svgDom.js.map +1 -0
- package/dist/index.d.ts +18 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +12 -0
- package/dist/index.js.map +1 -0
- package/dist/model/flow.d.ts +43 -0
- package/dist/model/flow.d.ts.map +1 -0
- package/dist/model/flow.js +87 -0
- package/dist/model/flow.js.map +1 -0
- package/dist/model/frame.d.ts +61 -0
- package/dist/model/frame.d.ts.map +1 -0
- package/dist/model/frame.js +124 -0
- package/dist/model/frame.js.map +1 -0
- package/dist/model/labels.d.ts +18 -0
- package/dist/model/labels.d.ts.map +1 -0
- package/dist/model/labels.js +25 -0
- package/dist/model/labels.js.map +1 -0
- package/dist/model/path.d.ts +4 -0
- package/dist/model/path.d.ts.map +1 -0
- package/dist/model/path.js +51 -0
- package/dist/model/path.js.map +1 -0
- package/dist/model/state.d.ts +30 -0
- package/dist/model/state.d.ts.map +1 -0
- package/dist/model/state.js +30 -0
- package/dist/model/state.js.map +1 -0
- package/dist/render/concepts.d.ts +4 -0
- package/dist/render/concepts.d.ts.map +1 -0
- package/dist/render/concepts.js +16 -0
- package/dist/render/concepts.js.map +1 -0
- package/dist/render/inline.d.ts +6 -0
- package/dist/render/inline.d.ts.map +1 -0
- package/dist/render/inline.js +70 -0
- package/dist/render/inline.js.map +1 -0
- package/dist/render/panel.d.ts +27 -0
- package/dist/render/panel.d.ts.map +1 -0
- package/dist/render/panel.js +57 -0
- package/dist/render/panel.js.map +1 -0
- package/dist/render/svg.d.ts +8 -0
- package/dist/render/svg.d.ts.map +1 -0
- package/dist/render/svg.js +43 -0
- package/dist/render/svg.js.map +1 -0
- package/dist/schema.d.ts +352 -0
- package/dist/schema.d.ts.map +1 -0
- package/dist/schema.js +138 -0
- package/dist/schema.js.map +1 -0
- package/package.json +74 -4
- package/resolved-map.schema.json +769 -0
- package/src/dom/animator.ts +129 -0
- package/src/dom/geometry.ts +81 -0
- package/src/dom/panelDom.ts +72 -0
- package/src/dom/player.ts +318 -0
- package/src/dom/svgDom.ts +79 -0
- package/src/index.ts +18 -0
- package/src/model/flow.ts +122 -0
- package/src/model/frame.ts +180 -0
- package/src/model/labels.ts +37 -0
- package/src/model/path.ts +56 -0
- package/src/model/state.ts +51 -0
- package/src/render/concepts.ts +21 -0
- package/src/render/inline.ts +71 -0
- package/src/render/panel.ts +69 -0
- package/src/render/svg.ts +52 -0
- package/src/schema.ts +162 -0
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import type { FlowPlan, Segment } from "../model/flow.js";
|
|
2
|
+
import type { ResolvedMap } from "../schema.js";
|
|
3
|
+
export interface Playback {
|
|
4
|
+
stop(): void;
|
|
5
|
+
readonly finished: Promise<void>;
|
|
6
|
+
}
|
|
7
|
+
export interface FlowHooks {
|
|
8
|
+
onHop(i: number): void;
|
|
9
|
+
onDone(): void;
|
|
10
|
+
onHopStart?(i: number): void;
|
|
11
|
+
onSegment?(segment: Segment): void;
|
|
12
|
+
onHopEnd?(i: number): void;
|
|
13
|
+
}
|
|
14
|
+
export interface Animator {
|
|
15
|
+
play(plan: FlowPlan, hooks: FlowHooks): Playback;
|
|
16
|
+
}
|
|
17
|
+
export class WebAnimator implements Animator {
|
|
18
|
+
private generation = 0;
|
|
19
|
+
private current?: Playback;
|
|
20
|
+
constructor(
|
|
21
|
+
readonly tokens: SVGCircleElement[],
|
|
22
|
+
readonly edges: ResolvedMap["edges"],
|
|
23
|
+
) {}
|
|
24
|
+
play(plan: FlowPlan, hooks: FlowHooks): Playback {
|
|
25
|
+
this.current?.stop();
|
|
26
|
+
const generation = ++this.generation;
|
|
27
|
+
let stopped = false;
|
|
28
|
+
const active = new Set<Animation>();
|
|
29
|
+
const clearStyles = () => {
|
|
30
|
+
if (generation !== this.generation) return;
|
|
31
|
+
this.tokens.forEach((t) => {
|
|
32
|
+
t.style.offsetPath = "";
|
|
33
|
+
t.style.offsetDistance = "";
|
|
34
|
+
t.style.offsetRotate = "";
|
|
35
|
+
t.style.offsetAnchor = "";
|
|
36
|
+
t.style.offsetPosition = "";
|
|
37
|
+
t.style.transform = "";
|
|
38
|
+
});
|
|
39
|
+
};
|
|
40
|
+
const place = ([x, y]: [number, number]) =>
|
|
41
|
+
this.tokens.forEach((t) => {
|
|
42
|
+
t.setAttribute("cx", String(x));
|
|
43
|
+
t.setAttribute("cy", String(y));
|
|
44
|
+
t.setAttribute("visibility", "visible");
|
|
45
|
+
});
|
|
46
|
+
const finished = (async () => {
|
|
47
|
+
try {
|
|
48
|
+
for (let index = 0; index < plan.segments.length; index++) {
|
|
49
|
+
if (stopped) return;
|
|
50
|
+
const segment = plan.segments[index];
|
|
51
|
+
plan.hops.forEach((hop, i) => {
|
|
52
|
+
if (hop.startSegment === index) hooks.onHopStart?.(i);
|
|
53
|
+
});
|
|
54
|
+
hooks.onSegment?.(segment);
|
|
55
|
+
plan.hops.forEach((hop, i) => {
|
|
56
|
+
if (hop.segment === index) hooks.onHop(i);
|
|
57
|
+
});
|
|
58
|
+
clearStyles();
|
|
59
|
+
let frames: Keyframe[];
|
|
60
|
+
if (segment.type === "pause") {
|
|
61
|
+
place(segment.at);
|
|
62
|
+
frames = [
|
|
63
|
+
{ transform: "translate(0px, 0px)" },
|
|
64
|
+
{ transform: "translate(0px, 0px)" },
|
|
65
|
+
];
|
|
66
|
+
} else {
|
|
67
|
+
place([0, 0]);
|
|
68
|
+
if (segment.type === "jump")
|
|
69
|
+
frames = [
|
|
70
|
+
{
|
|
71
|
+
transform: `translate(${segment.from[0]}px, ${segment.from[1]}px)`,
|
|
72
|
+
},
|
|
73
|
+
{
|
|
74
|
+
transform: `translate(${segment.to[0]}px, ${segment.to[1]}px)`,
|
|
75
|
+
},
|
|
76
|
+
];
|
|
77
|
+
else {
|
|
78
|
+
for (const t of this.tokens) {
|
|
79
|
+
t.style.offsetPath = `path("${this.edges[segment.edge].path}")`;
|
|
80
|
+
t.style.offsetRotate = "0deg";
|
|
81
|
+
t.style.offsetAnchor = "0px 0px";
|
|
82
|
+
t.style.offsetPosition = "0px 0px";
|
|
83
|
+
}
|
|
84
|
+
frames = [
|
|
85
|
+
{ offsetDistance: segment.reverse ? "100%" : "0%" },
|
|
86
|
+
{ offsetDistance: segment.reverse ? "0%" : "100%" },
|
|
87
|
+
];
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
const animations = this.tokens.map((t) =>
|
|
91
|
+
t.animate(frames, {
|
|
92
|
+
duration: segment.duration,
|
|
93
|
+
fill: "forwards",
|
|
94
|
+
easing: "linear",
|
|
95
|
+
}),
|
|
96
|
+
);
|
|
97
|
+
animations.forEach((a) => active.add(a));
|
|
98
|
+
await Promise.all(animations.map((a) => a.finished));
|
|
99
|
+
if (stopped) return;
|
|
100
|
+
clearStyles();
|
|
101
|
+
place(segment.type === "pause" ? segment.at : segment.to);
|
|
102
|
+
animations.forEach((a) => {
|
|
103
|
+
a.cancel();
|
|
104
|
+
active.delete(a);
|
|
105
|
+
});
|
|
106
|
+
plan.hops.forEach((hop, i) => {
|
|
107
|
+
if (hop.segment === index) hooks.onHopEnd?.(i);
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
if (!stopped) hooks.onDone();
|
|
111
|
+
} catch (error) {
|
|
112
|
+
if (!stopped) throw error;
|
|
113
|
+
} finally {
|
|
114
|
+
active.forEach((a) => a.cancel());
|
|
115
|
+
active.clear();
|
|
116
|
+
clearStyles();
|
|
117
|
+
}
|
|
118
|
+
})();
|
|
119
|
+
return (this.current = {
|
|
120
|
+
finished,
|
|
121
|
+
stop() {
|
|
122
|
+
stopped = true;
|
|
123
|
+
active.forEach((a) => a.cancel());
|
|
124
|
+
active.clear();
|
|
125
|
+
clearStyles();
|
|
126
|
+
},
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import type { ResolvedMap } from "../schema.js";
|
|
2
|
+
import { labelMinimum, fitNodeLabels, fitEdgeLabel } from "../model/labels.js";
|
|
3
|
+
export interface Geometry {
|
|
4
|
+
pathLength(edge: string): number;
|
|
5
|
+
pointAt(edge: string, t: number): [number, number];
|
|
6
|
+
textBox(el: SVGGraphicsElement): {
|
|
7
|
+
x: number;
|
|
8
|
+
y: number;
|
|
9
|
+
width: number;
|
|
10
|
+
height: number;
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
export function geometry(svg: SVGSVGElement): Geometry {
|
|
14
|
+
const path = (edge: string) =>
|
|
15
|
+
svg.querySelector<SVGPathElement>(`g.edge[data-edge="${edge}"] path`)!;
|
|
16
|
+
return {
|
|
17
|
+
pathLength: (edge) => path(edge).getTotalLength(),
|
|
18
|
+
pointAt(edge, t) {
|
|
19
|
+
const p = path(edge),
|
|
20
|
+
at = p.getPointAtLength(p.getTotalLength() * t);
|
|
21
|
+
return [at.x, at.y];
|
|
22
|
+
},
|
|
23
|
+
textBox: (el) => el.getBBox(),
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
/** Measurements are browser-owned; all fitting decisions are pure model math. */
|
|
27
|
+
export function sizeLabels(root: HTMLElement, map: ResolvedMap, geo: Geometry) {
|
|
28
|
+
const svg = root.querySelector<SVGSVGElement>(".map svg")!;
|
|
29
|
+
const width = svg.getBoundingClientRect().width;
|
|
30
|
+
if (width)
|
|
31
|
+
root.style.setProperty(
|
|
32
|
+
"--map-label-min",
|
|
33
|
+
`${labelMinimum(map.viewBox[0], width)}px`,
|
|
34
|
+
);
|
|
35
|
+
svg.querySelectorAll<SVGTextElement>(".node .tag").forEach((tag) => {
|
|
36
|
+
tag.removeAttribute("textLength");
|
|
37
|
+
const node =
|
|
38
|
+
map.nodes[(tag.parentElement as unknown as SVGGElement).dataset.node!];
|
|
39
|
+
if (!node) return;
|
|
40
|
+
if (tag.getComputedTextLength() > node.box[2] - 16) {
|
|
41
|
+
tag.setAttribute("textLength", String(node.box[2] - 16));
|
|
42
|
+
tag.setAttribute("lengthAdjust", "spacing");
|
|
43
|
+
}
|
|
44
|
+
const label =
|
|
45
|
+
tag.parentElement!.querySelector<SVGTextElement>("text:not(.tag)")!;
|
|
46
|
+
label.setAttribute("y", String(node.box[1] + 17));
|
|
47
|
+
tag.setAttribute("y", String(node.box[1] + 32));
|
|
48
|
+
const fitted = fitNodeLabels(
|
|
49
|
+
node.box,
|
|
50
|
+
geo.textBox(label),
|
|
51
|
+
geo.textBox(tag),
|
|
52
|
+
);
|
|
53
|
+
label.setAttribute("y", String(fitted.main));
|
|
54
|
+
tag.setAttribute("y", String(fitted.tag));
|
|
55
|
+
});
|
|
56
|
+
svg.querySelectorAll<SVGTextElement>(".etext").forEach((label) => {
|
|
57
|
+
const edge = map.edges[label.dataset.edge!];
|
|
58
|
+
if (!edge.label) return;
|
|
59
|
+
label.textContent = edge.label.text;
|
|
60
|
+
const lines = fitEdgeLabel(
|
|
61
|
+
edge.label.text,
|
|
62
|
+
edge.label.at,
|
|
63
|
+
map.viewBox[0],
|
|
64
|
+
label.getComputedTextLength(),
|
|
65
|
+
parseFloat(getComputedStyle(label).fontSize),
|
|
66
|
+
);
|
|
67
|
+
if (!lines) return;
|
|
68
|
+
label.replaceChildren(
|
|
69
|
+
...lines.map((line) => {
|
|
70
|
+
const span = root.ownerDocument.createElementNS(
|
|
71
|
+
"http://www.w3.org/2000/svg",
|
|
72
|
+
"tspan",
|
|
73
|
+
);
|
|
74
|
+
span.setAttribute("x", String(line.x));
|
|
75
|
+
span.setAttribute("y", String(line.y));
|
|
76
|
+
span.textContent = line.text;
|
|
77
|
+
return span;
|
|
78
|
+
}),
|
|
79
|
+
);
|
|
80
|
+
});
|
|
81
|
+
}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import type { ResolvedMap } from "../schema.js";
|
|
2
|
+
import type { Frame, LogLine } from "../model/frame.js";
|
|
3
|
+
import {
|
|
4
|
+
renderPanelParts,
|
|
5
|
+
statusText,
|
|
6
|
+
defaultStrings,
|
|
7
|
+
type UiStrings,
|
|
8
|
+
} from "../render/panel.js";
|
|
9
|
+
export class PanelDom {
|
|
10
|
+
readonly head: HTMLElement;
|
|
11
|
+
readonly more: HTMLElement;
|
|
12
|
+
readonly status: HTMLElement | null;
|
|
13
|
+
constructor(
|
|
14
|
+
readonly root: HTMLElement,
|
|
15
|
+
readonly strings: Partial<UiStrings> = {},
|
|
16
|
+
) {
|
|
17
|
+
this.head = root.querySelector('[data-role="panel"]')!;
|
|
18
|
+
this.more = root.querySelector('[data-role="more"]')!;
|
|
19
|
+
this.status = root.querySelector("[data-map-status]");
|
|
20
|
+
}
|
|
21
|
+
applyFrame(map: ResolvedMap, frame: Frame) {
|
|
22
|
+
const focused = this.root.ownerDocument.activeElement;
|
|
23
|
+
const owned = focused instanceof HTMLElement && this.root.contains(focused);
|
|
24
|
+
const role = owned ? focused.dataset.role : undefined;
|
|
25
|
+
const segment = owned ? focused.dataset.seg : undefined;
|
|
26
|
+
const parts = renderPanelParts(map, frame, this.strings);
|
|
27
|
+
this.head.innerHTML = parts.head;
|
|
28
|
+
this.more.innerHTML = parts.more;
|
|
29
|
+
// The server renders the initial text, so the first frame changes nothing
|
|
30
|
+
// and the page load stays silent.
|
|
31
|
+
const status = statusText(frame);
|
|
32
|
+
if (this.status && this.status.textContent !== status)
|
|
33
|
+
this.status.textContent = status;
|
|
34
|
+
if (focused instanceof HTMLElement && !focused.isConnected) {
|
|
35
|
+
const replacement = role
|
|
36
|
+
? this.root.querySelector<HTMLElement>(`[data-role="${role}"]`)
|
|
37
|
+
: segment
|
|
38
|
+
? this.root.querySelector<HTMLElement>(`[data-seg="${segment}"]`)
|
|
39
|
+
: this.root;
|
|
40
|
+
const target =
|
|
41
|
+
replacement && !replacement.matches(":disabled")
|
|
42
|
+
? replacement
|
|
43
|
+
: (this.root.querySelector<HTMLElement>('[aria-current="step"]') ??
|
|
44
|
+
this.root);
|
|
45
|
+
target.focus({ preventScroll: true });
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
markStep(index: number) {
|
|
49
|
+
this.more
|
|
50
|
+
.querySelectorAll<HTMLElement>("[data-li]")
|
|
51
|
+
.forEach((e) =>
|
|
52
|
+
e.classList.toggle("now", Number(e.dataset.li) === index),
|
|
53
|
+
);
|
|
54
|
+
}
|
|
55
|
+
setLog(lines: LogLine[]) {
|
|
56
|
+
const log = this.root.querySelector<HTMLElement>('[data-role="flowlog"]');
|
|
57
|
+
if (!log) return;
|
|
58
|
+
const doc = this.root.ownerDocument,
|
|
59
|
+
title = doc.createElement("h3");
|
|
60
|
+
title.textContent = this.strings.callLog ?? defaultStrings.callLog;
|
|
61
|
+
log.replaceChildren(
|
|
62
|
+
title,
|
|
63
|
+
...lines.map((l) => {
|
|
64
|
+
const div = doc.createElement("div");
|
|
65
|
+
div.className = `line ${l.kind}`;
|
|
66
|
+
div.textContent = l.text;
|
|
67
|
+
return div;
|
|
68
|
+
}),
|
|
69
|
+
);
|
|
70
|
+
log.scrollTop = log.scrollHeight;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
@@ -0,0 +1,318 @@
|
|
|
1
|
+
import type { ResolvedMap } from "../schema.js";
|
|
2
|
+
import {
|
|
3
|
+
initialState,
|
|
4
|
+
reduce,
|
|
5
|
+
type PlayerState,
|
|
6
|
+
type Action,
|
|
7
|
+
} from "../model/state.js";
|
|
8
|
+
import { derive, type LogLine } from "../model/frame.js";
|
|
9
|
+
import { planFlow, reducedFlow, appendLog } from "../model/flow.js";
|
|
10
|
+
import type { UiStrings } from "../render/panel.js";
|
|
11
|
+
import { SvgDom } from "./svgDom.js";
|
|
12
|
+
import { PanelDom } from "./panelDom.js";
|
|
13
|
+
import { geometry, sizeLabels, type Geometry } from "./geometry.js";
|
|
14
|
+
import { WebAnimator, type Animator, type Playback } from "./animator.js";
|
|
15
|
+
const bindings = new WeakMap<MapPlayer, Set<() => void>>();
|
|
16
|
+
export class MapPlayer {
|
|
17
|
+
private current: PlayerState = { ...initialState };
|
|
18
|
+
private readonly abort = new AbortController();
|
|
19
|
+
private readonly svg: SvgDom;
|
|
20
|
+
private readonly panel: PanelDom;
|
|
21
|
+
private readonly geo: Geometry;
|
|
22
|
+
private readonly animator: Animator;
|
|
23
|
+
readonly reducedMotion: boolean;
|
|
24
|
+
private readonly resize: ResizeObserver;
|
|
25
|
+
private playback?: Playback;
|
|
26
|
+
private flowId = 0;
|
|
27
|
+
private log: LogLine[] = [];
|
|
28
|
+
private destroyed = false;
|
|
29
|
+
constructor(
|
|
30
|
+
readonly root: HTMLElement,
|
|
31
|
+
readonly map: ResolvedMap,
|
|
32
|
+
options: {
|
|
33
|
+
animator?: Animator;
|
|
34
|
+
geometry?: Geometry;
|
|
35
|
+
reducedMotion?: boolean;
|
|
36
|
+
strings?: Partial<UiStrings>;
|
|
37
|
+
} = {},
|
|
38
|
+
) {
|
|
39
|
+
if (map.format !== 2)
|
|
40
|
+
throw new Error(
|
|
41
|
+
`Unsupported concept map format ${map.format}; expected 2`,
|
|
42
|
+
);
|
|
43
|
+
const view = root.ownerDocument.defaultView!;
|
|
44
|
+
this.svg = new SvgDom(root.querySelector<SVGSVGElement>(".map svg")!);
|
|
45
|
+
this.panel = new PanelDom(root, options.strings);
|
|
46
|
+
this.geo = options.geometry ?? geometry(this.svg.svg);
|
|
47
|
+
this.animator =
|
|
48
|
+
options.animator ?? new WebAnimator(this.svg.tokens, map.edges);
|
|
49
|
+
this.reducedMotion =
|
|
50
|
+
options.reducedMotion ??
|
|
51
|
+
view.matchMedia("(prefers-reduced-motion: reduce)").matches;
|
|
52
|
+
bindings.set(this, new Set());
|
|
53
|
+
root.addEventListener("click", this.click, { signal: this.abort.signal });
|
|
54
|
+
root.addEventListener("keydown", this.key, { signal: this.abort.signal });
|
|
55
|
+
view.addEventListener(
|
|
56
|
+
"pagehide",
|
|
57
|
+
() => {
|
|
58
|
+
const before = this.current;
|
|
59
|
+
this.stopFlow();
|
|
60
|
+
this.current = { ...this.current, scenario: 0, step: 0 };
|
|
61
|
+
this.emitChange(before);
|
|
62
|
+
},
|
|
63
|
+
{ signal: this.abort.signal },
|
|
64
|
+
);
|
|
65
|
+
view.addEventListener(
|
|
66
|
+
"pageshow",
|
|
67
|
+
(event) => {
|
|
68
|
+
if (event.persisted) this.dispatch({ type: "reset" });
|
|
69
|
+
},
|
|
70
|
+
{ signal: this.abort.signal },
|
|
71
|
+
);
|
|
72
|
+
const fit = () => {
|
|
73
|
+
if (!this.destroyed) sizeLabels(root, map, this.geo);
|
|
74
|
+
};
|
|
75
|
+
this.resize = new ResizeObserver(fit);
|
|
76
|
+
this.resize.observe(this.svg.svg);
|
|
77
|
+
this.render();
|
|
78
|
+
fit();
|
|
79
|
+
void root.ownerDocument.fonts.ready.then(fit);
|
|
80
|
+
}
|
|
81
|
+
get state(): PlayerState {
|
|
82
|
+
return { ...this.current };
|
|
83
|
+
}
|
|
84
|
+
dispatch(action: Action) {
|
|
85
|
+
if (
|
|
86
|
+
this.destroyed ||
|
|
87
|
+
(action.type === "mode" && action.mode === this.current.mode)
|
|
88
|
+
)
|
|
89
|
+
return;
|
|
90
|
+
const before = this.current;
|
|
91
|
+
this.current = reduce(this.map, this.current, action);
|
|
92
|
+
this.render();
|
|
93
|
+
const generation = this.flowId;
|
|
94
|
+
this.emitChange(before);
|
|
95
|
+
// Listeners may destroy the player or supersede this dispatch.
|
|
96
|
+
if (
|
|
97
|
+
!this.destroyed &&
|
|
98
|
+
generation === this.flowId &&
|
|
99
|
+
this.current.mode === "all"
|
|
100
|
+
)
|
|
101
|
+
this.startFlow();
|
|
102
|
+
}
|
|
103
|
+
highlight(nodes: string[], on: boolean) {
|
|
104
|
+
if (!this.destroyed) this.svg.highlight(nodes, on);
|
|
105
|
+
}
|
|
106
|
+
replay() {
|
|
107
|
+
if (this.destroyed || this.current.mode !== "all") return;
|
|
108
|
+
this.stopFlow();
|
|
109
|
+
this.startFlow();
|
|
110
|
+
}
|
|
111
|
+
destroy() {
|
|
112
|
+
if (this.destroyed) return;
|
|
113
|
+
this.destroyed = true;
|
|
114
|
+
this.stopFlow();
|
|
115
|
+
this.abort.abort();
|
|
116
|
+
this.resize.disconnect();
|
|
117
|
+
for (const cleanup of bindings.get(this) ?? []) cleanup();
|
|
118
|
+
bindings.delete(this);
|
|
119
|
+
}
|
|
120
|
+
private emitChange(before: PlayerState) {
|
|
121
|
+
if (
|
|
122
|
+
before.scenario !== this.current.scenario ||
|
|
123
|
+
before.step !== this.current.step ||
|
|
124
|
+
before.mode !== this.current.mode
|
|
125
|
+
)
|
|
126
|
+
this.root.dispatchEvent(
|
|
127
|
+
new CustomEvent("mapchange", { detail: this.state, bubbles: true }),
|
|
128
|
+
);
|
|
129
|
+
}
|
|
130
|
+
private readonly click = (event: MouseEvent) => {
|
|
131
|
+
if (!(event.target instanceof Element)) return;
|
|
132
|
+
const button = event.target.closest<HTMLElement>(
|
|
133
|
+
"[data-scenario], [data-mode], [data-seg], [data-go], [data-role]",
|
|
134
|
+
);
|
|
135
|
+
if (!button || !this.root.contains(button) || button.matches(":disabled"))
|
|
136
|
+
return;
|
|
137
|
+
if (button.dataset.scenario !== undefined)
|
|
138
|
+
this.dispatch({
|
|
139
|
+
type: "scenario",
|
|
140
|
+
index: this.map.scenarios.findIndex(
|
|
141
|
+
(s) => s.id === button.dataset.scenario,
|
|
142
|
+
),
|
|
143
|
+
});
|
|
144
|
+
else if (button.dataset.mode === "step" || button.dataset.mode === "all")
|
|
145
|
+
this.dispatch({ type: "mode", mode: button.dataset.mode });
|
|
146
|
+
else if (button.dataset.seg !== undefined)
|
|
147
|
+
this.dispatch({ type: "go", step: Number(button.dataset.seg) });
|
|
148
|
+
else if (button.dataset.go !== undefined)
|
|
149
|
+
this.dispatch({
|
|
150
|
+
type: "concept",
|
|
151
|
+
scenario: this.current.scenario,
|
|
152
|
+
step: Number(button.dataset.go),
|
|
153
|
+
});
|
|
154
|
+
else if (button.dataset.role === "back") this.dispatch({ type: "back" });
|
|
155
|
+
else if (button.dataset.role === "next") this.dispatch({ type: "next" });
|
|
156
|
+
else if (button.dataset.role === "replay") this.replay();
|
|
157
|
+
else if (button.dataset.role === "nextscenario") {
|
|
158
|
+
event.preventDefault();
|
|
159
|
+
this.dispatch({ type: "scenario", index: this.current.scenario + 1 });
|
|
160
|
+
}
|
|
161
|
+
};
|
|
162
|
+
private readonly key = (event: KeyboardEvent) => {
|
|
163
|
+
if (
|
|
164
|
+
this.current.mode !== "step" ||
|
|
165
|
+
(event.target instanceof Element &&
|
|
166
|
+
event.target.closest("input, textarea, select"))
|
|
167
|
+
)
|
|
168
|
+
return;
|
|
169
|
+
if (event.key === "ArrowRight" || event.key === "ArrowLeft") {
|
|
170
|
+
event.preventDefault();
|
|
171
|
+
this.dispatch({ type: event.key === "ArrowRight" ? "next" : "back" });
|
|
172
|
+
}
|
|
173
|
+
};
|
|
174
|
+
private render() {
|
|
175
|
+
this.stopFlow();
|
|
176
|
+
const frame = derive(this.map, this.current);
|
|
177
|
+
this.svg.applyFrame(frame);
|
|
178
|
+
this.panel.applyFrame(this.map, frame);
|
|
179
|
+
this.root
|
|
180
|
+
.querySelectorAll<HTMLElement>("[data-scenario]")
|
|
181
|
+
.forEach((b) =>
|
|
182
|
+
b.setAttribute(
|
|
183
|
+
"aria-pressed",
|
|
184
|
+
String(
|
|
185
|
+
b.dataset.scenario === this.map.scenarios[this.current.scenario].id,
|
|
186
|
+
),
|
|
187
|
+
),
|
|
188
|
+
);
|
|
189
|
+
this.root
|
|
190
|
+
.querySelectorAll<HTMLElement>("[data-mode]")
|
|
191
|
+
.forEach((b) =>
|
|
192
|
+
b.setAttribute(
|
|
193
|
+
"aria-pressed",
|
|
194
|
+
String(b.dataset.mode === this.current.mode),
|
|
195
|
+
),
|
|
196
|
+
);
|
|
197
|
+
}
|
|
198
|
+
private stopFlow() {
|
|
199
|
+
this.flowId++;
|
|
200
|
+
this.playback?.stop();
|
|
201
|
+
this.playback = undefined;
|
|
202
|
+
this.svg.clearFlow();
|
|
203
|
+
}
|
|
204
|
+
private startFlow() {
|
|
205
|
+
const scenario = this.current.scenario,
|
|
206
|
+
story = this.map.scenarios[scenario];
|
|
207
|
+
this.log = [];
|
|
208
|
+
this.panel.setLog([]);
|
|
209
|
+
if (!story.flow?.length) return;
|
|
210
|
+
this.svg.clearFlow();
|
|
211
|
+
if (this.reducedMotion) {
|
|
212
|
+
const flow = reducedFlow(this.map, scenario);
|
|
213
|
+
this.svg.applyReduced(flow);
|
|
214
|
+
this.panel.setLog(flow.log);
|
|
215
|
+
this.panel.markStep(flow.lastStep);
|
|
216
|
+
return;
|
|
217
|
+
}
|
|
218
|
+
const generation = ++this.flowId,
|
|
219
|
+
active = () => !this.destroyed && generation === this.flowId;
|
|
220
|
+
this.svg.tokenFailure(false);
|
|
221
|
+
const plan = planFlow(this.map, scenario, {
|
|
222
|
+
length: (id) => this.geo.pathLength(id),
|
|
223
|
+
});
|
|
224
|
+
let pendingHop: number | undefined;
|
|
225
|
+
const finishHop = (index: number) => {
|
|
226
|
+
const h = plan.hops[index],
|
|
227
|
+
node = this.svg.nodes[h.node];
|
|
228
|
+
node.classList.remove("oknow");
|
|
229
|
+
if (!h.fail) node.classList.remove("failnow");
|
|
230
|
+
this.svg.tokenFailure(false);
|
|
231
|
+
if (pendingHop === index) pendingHop = undefined;
|
|
232
|
+
};
|
|
233
|
+
this.playback = this.animator.play(plan, {
|
|
234
|
+
onHopStart: (i) => {
|
|
235
|
+
if (active()) this.panel.markStep(plan.hops[i].step);
|
|
236
|
+
},
|
|
237
|
+
onSegment: (segment) => {
|
|
238
|
+
if (active() && segment.type === "travel")
|
|
239
|
+
this.svg.edges[segment.edge].classList.add("trail");
|
|
240
|
+
},
|
|
241
|
+
onHop: (i) => {
|
|
242
|
+
if (!active()) return;
|
|
243
|
+
// Required hooks alone must leave a complete route, even when an
|
|
244
|
+
// injected animator does not report optional intermediate phases.
|
|
245
|
+
if (pendingHop !== undefined) finishHop(pendingHop);
|
|
246
|
+
pendingHop = i;
|
|
247
|
+
this.panel.markStep(plan.hops[i].step);
|
|
248
|
+
const edge = story.flow![i].edge;
|
|
249
|
+
if (edge) this.svg.edges[edge].classList.add("trail");
|
|
250
|
+
const h = plan.hops[i],
|
|
251
|
+
node = this.svg.nodes[h.node];
|
|
252
|
+
node.classList.add("visit", h.fail ? "failnow" : "oknow");
|
|
253
|
+
this.svg.tokenFailure(h.fail);
|
|
254
|
+
this.log = appendLog(this.log, h.log);
|
|
255
|
+
this.panel.setLog(this.log);
|
|
256
|
+
},
|
|
257
|
+
onHopEnd: (i) => {
|
|
258
|
+
if (active()) finishHop(i);
|
|
259
|
+
},
|
|
260
|
+
onDone: () => {
|
|
261
|
+
if (active()) {
|
|
262
|
+
if (pendingHop !== undefined) finishHop(pendingHop);
|
|
263
|
+
this.svg.hideToken();
|
|
264
|
+
this.playback = undefined;
|
|
265
|
+
}
|
|
266
|
+
},
|
|
267
|
+
});
|
|
268
|
+
void this.playback.finished.catch((error) => {
|
|
269
|
+
if (active()) throw error;
|
|
270
|
+
});
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
export function connectConcepts(
|
|
274
|
+
player: MapPlayer,
|
|
275
|
+
container: HTMLElement,
|
|
276
|
+
): () => void {
|
|
277
|
+
if (!bindings.has(player)) return () => {};
|
|
278
|
+
const abort = new AbortController(),
|
|
279
|
+
signal = abort.signal;
|
|
280
|
+
for (const row of container.querySelectorAll<HTMLElement>("li[data-nodes]")) {
|
|
281
|
+
const highlight = (on: boolean) =>
|
|
282
|
+
player.highlight(row.dataset.nodes!.split(" "), on);
|
|
283
|
+
row.addEventListener("mouseenter", () => highlight(true), { signal });
|
|
284
|
+
row.addEventListener("mouseleave", () => highlight(false), { signal });
|
|
285
|
+
row.addEventListener("focusin", () => highlight(true), { signal });
|
|
286
|
+
row.addEventListener("focusout", () => highlight(false), { signal });
|
|
287
|
+
row.querySelector<HTMLElement>("[data-step]")?.addEventListener(
|
|
288
|
+
"click",
|
|
289
|
+
(event) => {
|
|
290
|
+
const button = event.currentTarget as HTMLElement;
|
|
291
|
+
const scenario = Math.max(
|
|
292
|
+
0,
|
|
293
|
+
player.map.scenarios.findIndex(
|
|
294
|
+
(s) =>
|
|
295
|
+
s.id ===
|
|
296
|
+
(button.dataset.conceptScenario ?? player.map.conceptScenario),
|
|
297
|
+
),
|
|
298
|
+
);
|
|
299
|
+
const step = Number(button.dataset.step);
|
|
300
|
+
player.dispatch({ type: "concept", scenario, step });
|
|
301
|
+
player.root
|
|
302
|
+
.querySelector<HTMLElement>(`[data-seg="${player.state.step}"]`)
|
|
303
|
+
?.focus({ preventScroll: true });
|
|
304
|
+
player.root.scrollIntoView({
|
|
305
|
+
behavior: player.reducedMotion ? "auto" : "smooth",
|
|
306
|
+
block: "start",
|
|
307
|
+
});
|
|
308
|
+
},
|
|
309
|
+
{ signal },
|
|
310
|
+
);
|
|
311
|
+
}
|
|
312
|
+
const cleanup = () => {
|
|
313
|
+
abort.abort();
|
|
314
|
+
bindings.get(player)?.delete(cleanup);
|
|
315
|
+
};
|
|
316
|
+
bindings.get(player)!.add(cleanup);
|
|
317
|
+
return cleanup;
|
|
318
|
+
}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import type { Frame } from "../model/frame.js";
|
|
2
|
+
import type { reducedFlow } from "../model/flow.js";
|
|
3
|
+
import { renderBadges } from "../render/svg.js";
|
|
4
|
+
let markerSequence = 0;
|
|
5
|
+
export class SvgDom {
|
|
6
|
+
readonly nodes: Record<string, SVGGElement>;
|
|
7
|
+
readonly edges: Record<string, SVGGElement>;
|
|
8
|
+
readonly lanes: Record<string, SVGGElement>;
|
|
9
|
+
readonly frames: Record<string, SVGGElement>;
|
|
10
|
+
readonly labels: Record<string, SVGTextElement>;
|
|
11
|
+
readonly tokens: SVGCircleElement[];
|
|
12
|
+
constructor(readonly svg: SVGSVGElement) {
|
|
13
|
+
const markerId = `map-arrow-${++markerSequence}`;
|
|
14
|
+
svg.querySelector("marker")!.id = markerId;
|
|
15
|
+
svg
|
|
16
|
+
.querySelectorAll("[marker-end]")
|
|
17
|
+
.forEach((path) => path.setAttribute("marker-end", `url(#${markerId})`));
|
|
18
|
+
const groups = (selector: string, key: string) =>
|
|
19
|
+
Object.fromEntries(
|
|
20
|
+
[...svg.querySelectorAll<SVGGElement>(selector)].map((e) => [
|
|
21
|
+
e.getAttribute(key)!,
|
|
22
|
+
e,
|
|
23
|
+
]),
|
|
24
|
+
);
|
|
25
|
+
this.nodes = groups(".node[data-node]", "data-node");
|
|
26
|
+
this.edges = groups("g.edge[data-edge]", "data-edge");
|
|
27
|
+
this.lanes = groups(".lane[data-lane]", "data-lane");
|
|
28
|
+
this.frames = groups(".frame[data-frame]", "data-frame");
|
|
29
|
+
this.labels = Object.fromEntries(
|
|
30
|
+
[...svg.querySelectorAll<SVGTextElement>(".etext[data-edge]")].map(
|
|
31
|
+
(e) => [e.dataset.edge!, e],
|
|
32
|
+
),
|
|
33
|
+
);
|
|
34
|
+
this.tokens = [
|
|
35
|
+
...svg.querySelectorAll<SVGCircleElement>(".token, .token-glow"),
|
|
36
|
+
];
|
|
37
|
+
}
|
|
38
|
+
applyFrame(frame: Frame) {
|
|
39
|
+
for (const [id, n] of Object.entries(frame.nodes)) {
|
|
40
|
+
this.nodes[id].classList.toggle("ghost", !n.revealed);
|
|
41
|
+
this.nodes[id].classList.toggle("cur", n.current);
|
|
42
|
+
this.nodes[id].querySelector("text")!.textContent = n.label;
|
|
43
|
+
}
|
|
44
|
+
for (const [id, e] of Object.entries(frame.edges)) {
|
|
45
|
+
this.edges[id].classList.toggle("ghost", !e.revealed);
|
|
46
|
+
this.edges[id].classList.toggle("cur", e.current);
|
|
47
|
+
}
|
|
48
|
+
for (const [id, l] of Object.entries(frame.lanes))
|
|
49
|
+
this.lanes[id].classList.toggle("ghost", !l.revealed);
|
|
50
|
+
for (const [id, f] of Object.entries(frame.frames))
|
|
51
|
+
this.frames[id].classList.toggle("ghost", !f.revealed);
|
|
52
|
+
for (const [id, l] of Object.entries(frame.edgeLabels))
|
|
53
|
+
this.labels[id].classList.toggle("ghost", !l.revealed);
|
|
54
|
+
this.svg.querySelector("[data-map-badges]")!.innerHTML = renderBadges(
|
|
55
|
+
frame.badges,
|
|
56
|
+
);
|
|
57
|
+
}
|
|
58
|
+
clearFlow() {
|
|
59
|
+
Object.values(this.nodes).forEach((e) =>
|
|
60
|
+
e.classList.remove("visit", "failnow", "oknow"),
|
|
61
|
+
);
|
|
62
|
+
Object.values(this.edges).forEach((e) => e.classList.remove("trail"));
|
|
63
|
+
this.hideToken();
|
|
64
|
+
}
|
|
65
|
+
hideToken() {
|
|
66
|
+
this.tokens.forEach((e) => e.setAttribute("visibility", "hidden"));
|
|
67
|
+
}
|
|
68
|
+
tokenFailure(on: boolean) {
|
|
69
|
+
this.tokens.forEach((e) => e.classList.toggle("fail", on));
|
|
70
|
+
}
|
|
71
|
+
highlight(nodes: string[], on: boolean) {
|
|
72
|
+
for (const id of nodes) this.nodes[id]?.classList.toggle("hl", on);
|
|
73
|
+
}
|
|
74
|
+
applyReduced(flow: ReturnType<typeof reducedFlow>) {
|
|
75
|
+
flow.trail.forEach((id) => this.edges[id].classList.add("trail"));
|
|
76
|
+
flow.visited.forEach((id) => this.nodes[id].classList.add("visit"));
|
|
77
|
+
flow.failed.forEach((id) => this.nodes[id].classList.add("failnow"));
|
|
78
|
+
}
|
|
79
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export { MapPlayer, connectConcepts } from "./dom/player.js";
|
|
2
|
+
export type { Animator, Playback, FlowHooks } from "./dom/animator.js";
|
|
3
|
+
export type { Geometry } from "./dom/geometry.js";
|
|
4
|
+
export { derive } from "./model/frame.js";
|
|
5
|
+
export { reduce, initialState } from "./model/state.js";
|
|
6
|
+
export { planFlow, reducedFlow } from "./model/flow.js";
|
|
7
|
+
export { renderSvg } from "./render/svg.js";
|
|
8
|
+
export { renderPanel, renderPanelParts } from "./render/panel.js";
|
|
9
|
+
export { renderConcepts, renderDeeper } from "./render/concepts.js";
|
|
10
|
+
export { renderInline } from "./render/inline.js";
|
|
11
|
+
export type { ResolvedMap } from "./schema.js";
|
|
12
|
+
export type { PlayerState, Action, Mode } from "./model/state.js";
|
|
13
|
+
export type { Frame } from "./model/frame.js";
|
|
14
|
+
export type { UiStrings } from "./render/panel.js";
|
|
15
|
+
|
|
16
|
+
export { mapSchema, ResolvedMapSchema } from "./schema.js";
|
|
17
|
+
export { statusText } from "./render/panel.js";
|
|
18
|
+
export { escapeHtml } from "./render/inline.js";
|