@metronforge/concept-map 0.1.1 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (199) hide show
  1. package/README.md +142 -91
  2. package/css/concept-map.css +31 -21
  3. package/dist/dom/badgeDom.d.ts +3 -0
  4. package/dist/dom/badgeDom.d.ts.map +1 -0
  5. package/dist/dom/badgeDom.js +23 -0
  6. package/dist/dom/badgeDom.js.map +1 -0
  7. package/dist/dom/geometry.d.ts.map +1 -1
  8. package/dist/dom/geometry.js +12 -2
  9. package/dist/dom/geometry.js.map +1 -1
  10. package/dist/dom/index.d.ts +6 -0
  11. package/dist/dom/index.d.ts.map +1 -0
  12. package/dist/dom/index.js +3 -0
  13. package/dist/dom/index.js.map +1 -0
  14. package/dist/dom/measureLabel.d.ts +4 -0
  15. package/dist/dom/measureLabel.d.ts.map +1 -0
  16. package/dist/dom/measureLabel.js +29 -0
  17. package/dist/dom/measureLabel.js.map +1 -0
  18. package/dist/dom/mount.d.ts +7 -0
  19. package/dist/dom/mount.d.ts.map +1 -0
  20. package/dist/dom/mount.js +37 -0
  21. package/dist/dom/mount.js.map +1 -0
  22. package/dist/dom/player.d.ts.map +1 -1
  23. package/dist/dom/player.js +3 -3
  24. package/dist/dom/player.js.map +1 -1
  25. package/dist/element/element.d.ts +26 -0
  26. package/dist/element/element.d.ts.map +1 -0
  27. package/dist/element/element.js +115 -0
  28. package/dist/element/element.js.map +1 -0
  29. package/dist/element/input.d.ts +7 -0
  30. package/dist/element/input.d.ts.map +1 -0
  31. package/dist/element/input.js +40 -0
  32. package/dist/element/input.js.map +1 -0
  33. package/dist/element/layout.d.ts +4 -0
  34. package/dist/element/layout.d.ts.map +1 -0
  35. package/dist/element/layout.js +14 -0
  36. package/dist/element/layout.js.map +1 -0
  37. package/dist/element.d.ts +9 -0
  38. package/dist/element.d.ts.map +1 -0
  39. package/dist/element.js +5 -0
  40. package/dist/element.js.map +1 -0
  41. package/dist/index.d.ts +4 -5
  42. package/dist/index.d.ts.map +1 -1
  43. package/dist/index.js +2 -2
  44. package/dist/index.js.map +1 -1
  45. package/dist/layout/badges.d.ts +4 -0
  46. package/dist/layout/badges.d.ts.map +1 -0
  47. package/dist/layout/badges.js +45 -0
  48. package/dist/layout/badges.js.map +1 -0
  49. package/dist/layout/check.d.ts +5 -0
  50. package/dist/layout/check.d.ts.map +1 -0
  51. package/dist/layout/check.js +61 -0
  52. package/dist/layout/check.js.map +1 -0
  53. package/dist/layout/dagre.d.ts +4 -0
  54. package/dist/layout/dagre.d.ts.map +1 -0
  55. package/dist/layout/dagre.js +62 -0
  56. package/dist/layout/dagre.js.map +1 -0
  57. package/dist/layout/elk.d.ts +4 -0
  58. package/dist/layout/elk.d.ts.map +1 -0
  59. package/dist/layout/elk.js +98 -0
  60. package/dist/layout/elk.js.map +1 -0
  61. package/dist/layout/font-metrics.d.ts +3 -0
  62. package/dist/layout/font-metrics.d.ts.map +1 -0
  63. package/dist/layout/font-metrics.js +128 -0
  64. package/dist/layout/font-metrics.js.map +1 -0
  65. package/dist/layout/geometry.d.ts +7 -0
  66. package/dist/layout/geometry.d.ts.map +1 -0
  67. package/dist/layout/geometry.js +56 -0
  68. package/dist/layout/geometry.js.map +1 -0
  69. package/dist/layout/graph.d.ts +6 -0
  70. package/dist/layout/graph.d.ts.map +1 -0
  71. package/dist/layout/graph.js +57 -0
  72. package/dist/layout/graph.js.map +1 -0
  73. package/dist/layout/index.d.ts +8 -0
  74. package/dist/layout/index.d.ts.map +1 -0
  75. package/dist/layout/index.js +21 -0
  76. package/dist/layout/index.js.map +1 -0
  77. package/dist/layout/lanes.d.ts +8 -0
  78. package/dist/layout/lanes.d.ts.map +1 -0
  79. package/dist/layout/lanes.js +78 -0
  80. package/dist/layout/lanes.js.map +1 -0
  81. package/dist/layout/measure.d.ts +4 -0
  82. package/dist/layout/measure.d.ts.map +1 -0
  83. package/dist/layout/measure.js +24 -0
  84. package/dist/layout/measure.js.map +1 -0
  85. package/dist/layout/resolve.d.ts +4 -0
  86. package/dist/layout/resolve.d.ts.map +1 -0
  87. package/dist/layout/resolve.js +114 -0
  88. package/dist/layout/resolve.js.map +1 -0
  89. package/dist/layout/self-route.d.ts +6 -0
  90. package/dist/layout/self-route.d.ts.map +1 -0
  91. package/dist/layout/self-route.js +110 -0
  92. package/dist/layout/self-route.js.map +1 -0
  93. package/dist/layout/types.d.ts +53 -0
  94. package/dist/layout/types.d.ts.map +1 -0
  95. package/dist/layout/types.js +2 -0
  96. package/dist/layout/types.js.map +1 -0
  97. package/dist/model/badge-placement.d.ts +10 -0
  98. package/dist/model/badge-placement.d.ts.map +1 -0
  99. package/dist/model/badge-placement.js +27 -0
  100. package/dist/model/badge-placement.js.map +1 -0
  101. package/dist/model/frame.d.ts +1 -1
  102. package/dist/model/frame.js +1 -1
  103. package/dist/model/input.d.ts +20 -0
  104. package/dist/model/input.d.ts.map +1 -0
  105. package/dist/model/input.js +15 -0
  106. package/dist/model/input.js.map +1 -0
  107. package/dist/model/labels.d.ts +1 -0
  108. package/dist/model/labels.d.ts.map +1 -1
  109. package/dist/model/labels.js +2 -0
  110. package/dist/model/labels.js.map +1 -1
  111. package/dist/render/concepts.d.ts.map +1 -1
  112. package/dist/render/concepts.js +6 -5
  113. package/dist/render/concepts.js.map +1 -1
  114. package/dist/render/data.d.ts +3 -0
  115. package/dist/render/data.d.ts.map +1 -0
  116. package/dist/render/data.js +7 -0
  117. package/dist/render/data.js.map +1 -0
  118. package/dist/render/figure.d.ts +10 -0
  119. package/dist/render/figure.d.ts.map +1 -0
  120. package/dist/render/figure.js +22 -0
  121. package/dist/render/figure.js.map +1 -0
  122. package/dist/render/hydration.d.ts +3 -0
  123. package/dist/render/hydration.d.ts.map +1 -0
  124. package/dist/render/hydration.js +13 -0
  125. package/dist/render/hydration.js.map +1 -0
  126. package/dist/render/panel.d.ts.map +1 -1
  127. package/dist/render/panel.js +7 -6
  128. package/dist/render/panel.js.map +1 -1
  129. package/dist/render/svg.d.ts.map +1 -1
  130. package/dist/render/svg.js +5 -4
  131. package/dist/render/svg.js.map +1 -1
  132. package/dist/schema/content.d.ts +90 -0
  133. package/dist/schema/content.d.ts.map +1 -0
  134. package/dist/schema/content.js +60 -0
  135. package/dist/schema/content.js.map +1 -0
  136. package/dist/schema/layout.d.ts +10 -0
  137. package/dist/schema/layout.d.ts.map +1 -0
  138. package/dist/schema/layout.js +8 -0
  139. package/dist/schema/layout.js.map +1 -0
  140. package/dist/schema/level.d.ts +3 -0
  141. package/dist/schema/level.d.ts.map +1 -0
  142. package/dist/schema/level.js +7 -0
  143. package/dist/schema/level.js.map +1 -0
  144. package/dist/schema/primitives.d.ts +61 -0
  145. package/dist/schema/primitives.d.ts.map +1 -0
  146. package/dist/schema/primitives.js +46 -0
  147. package/dist/schema/primitives.js.map +1 -0
  148. package/dist/schema/references.d.ts +6 -0
  149. package/dist/schema/references.d.ts.map +1 -0
  150. package/dist/schema/references.js +81 -0
  151. package/dist/schema/references.js.map +1 -0
  152. package/dist/schema.d.ts +357 -192
  153. package/dist/schema.d.ts.map +1 -1
  154. package/dist/schema.js +51 -119
  155. package/dist/schema.js.map +1 -1
  156. package/package.json +53 -9
  157. package/resolved-map.schema.json +184 -212
  158. package/source-map.schema.json +606 -0
  159. package/src/dom/badgeDom.ts +32 -0
  160. package/src/dom/geometry.ts +24 -2
  161. package/src/dom/index.ts +5 -0
  162. package/src/dom/measureLabel.ts +28 -0
  163. package/src/dom/mount.ts +46 -0
  164. package/src/dom/player.ts +3 -3
  165. package/src/element/element.ts +128 -0
  166. package/src/element/input.ts +60 -0
  167. package/src/element/layout.ts +18 -0
  168. package/src/element.ts +11 -0
  169. package/src/index.ts +10 -5
  170. package/src/layout/FONT-LICENSE.txt +78 -0
  171. package/src/layout/badges.ts +71 -0
  172. package/src/layout/check.ts +81 -0
  173. package/src/layout/dagre.ts +81 -0
  174. package/src/layout/elk.ts +113 -0
  175. package/src/layout/font-metrics.ts +127 -0
  176. package/src/layout/geometry.ts +72 -0
  177. package/src/layout/graph.ts +88 -0
  178. package/src/layout/index.ts +29 -0
  179. package/src/layout/lanes.ts +114 -0
  180. package/src/layout/measure.ts +25 -0
  181. package/src/layout/resolve.ts +155 -0
  182. package/src/layout/self-route.ts +168 -0
  183. package/src/layout/types.ts +50 -0
  184. package/src/model/badge-placement.ts +43 -0
  185. package/src/model/frame.ts +2 -2
  186. package/src/model/input.ts +26 -0
  187. package/src/model/labels.ts +3 -0
  188. package/src/render/concepts.ts +7 -5
  189. package/src/render/data.ts +10 -0
  190. package/src/render/figure.ts +35 -0
  191. package/src/render/hydration.ts +11 -0
  192. package/src/render/panel.ts +7 -6
  193. package/src/render/svg.ts +5 -4
  194. package/src/schema/content.ts +91 -0
  195. package/src/schema/layout.ts +10 -0
  196. package/src/schema/level.ts +6 -0
  197. package/src/schema/primitives.ts +55 -0
  198. package/src/schema/references.ts +106 -0
  199. package/src/schema.ts +73 -134
@@ -0,0 +1,28 @@
1
+ import type { LabelMeasurer } from "../layout/types.js";
2
+ /** Call after host.ownerDocument.fonts.ready; uses the host's --cmap font inputs. */
3
+ export function createDomMeasurer(host: HTMLElement): LabelMeasurer {
4
+ return (text, style) => {
5
+ const span = host.ownerDocument.createElement("span");
6
+ Object.assign(span.style, {
7
+ position: "absolute",
8
+ visibility: "hidden",
9
+ pointerEvents: "none",
10
+ whiteSpace: "pre",
11
+ fontWeight: "500",
12
+ lineHeight: "1.2",
13
+ fontSize: `${style.fontSize}px`,
14
+ letterSpacing: `${style.letterSpacing ?? 0}px`,
15
+ fontFamily: style.mono
16
+ ? "var(--cmap-font-mono, monospace)"
17
+ : "var(--cmap-font-sans, system-ui)",
18
+ });
19
+ span.textContent = text || " ";
20
+ host.append(span);
21
+ try {
22
+ const rect = span.getBoundingClientRect();
23
+ return { width: text ? rect.width : 0, height: rect.height };
24
+ } finally {
25
+ span.remove();
26
+ }
27
+ };
28
+ }
@@ -0,0 +1,46 @@
1
+ import { canHydrate } from "../render/hydration.js";
2
+ import type { ResolvedMap } from "../schema.js";
3
+ import { renderFigure, type FigureOptions } from "../render/figure.js";
4
+ import { MapPlayer, connectConcepts } from "./player.js";
5
+
6
+ export type MountOptions = FigureOptions &
7
+ ConstructorParameters<typeof MapPlayer>[2];
8
+ const mounted = new WeakMap<HTMLElement, MapPlayer>();
9
+
10
+ /** Populate a host or hydrate its existing figure. Repeated mounts replace bindings. */
11
+ export function mount(
12
+ element: HTMLElement,
13
+ map: ResolvedMap,
14
+ options: MountOptions = {},
15
+ ): MapPlayer {
16
+ const existing = element.matches('figure[data-figure="map"]')
17
+ ? element
18
+ : element.querySelector<HTMLElement>(':scope > figure[data-figure="map"]');
19
+ mounted.get(element)?.destroy();
20
+ if (existing) mounted.get(existing)?.destroy();
21
+ const markup = renderFigure(map, options);
22
+ let root = existing;
23
+ const data = root?.querySelector(":scope > script[data-map-data]");
24
+ if (!root || !canHydrate(data ? (data.textContent ?? "") : undefined, map)) {
25
+ if (existing === element) {
26
+ const template = element.ownerDocument.createElement("template");
27
+ template.innerHTML = markup;
28
+ const figure = template.content.firstElementChild!;
29
+ for (const attr of [...figure.attributes])
30
+ element.setAttribute(attr.name, attr.value);
31
+ element.innerHTML = figure.innerHTML;
32
+ root = element;
33
+ } else {
34
+ element.innerHTML = markup;
35
+ root = element.firstElementChild as HTMLElement;
36
+ }
37
+ }
38
+ const player = new MapPlayer(root, map, options);
39
+ connectConcepts(
40
+ player,
41
+ root.querySelector<HTMLElement>("[data-map-concepts]") ?? root,
42
+ );
43
+ mounted.set(element, player);
44
+ mounted.set(root, player);
45
+ return player;
46
+ }
package/src/dom/player.ts CHANGED
@@ -36,9 +36,9 @@ export class MapPlayer {
36
36
  strings?: Partial<UiStrings>;
37
37
  } = {},
38
38
  ) {
39
- if (map.format !== 2)
39
+ if (map.format !== 3)
40
40
  throw new Error(
41
- `Unsupported concept map format ${map.format}; expected 2`,
41
+ `Unsupported concept map format ${map.format}; expected 3`,
42
42
  );
43
43
  const view = root.ownerDocument.defaultView!;
44
44
  this.svg = new SvgDom(root.querySelector<SVGSVGElement>(".map svg")!);
@@ -75,7 +75,6 @@ export class MapPlayer {
75
75
  this.resize = new ResizeObserver(fit);
76
76
  this.resize.observe(this.svg.svg);
77
77
  this.render();
78
- fit();
79
78
  void root.ownerDocument.fonts.ready.then(fit);
80
79
  }
81
80
  get state(): PlayerState {
@@ -176,6 +175,7 @@ export class MapPlayer {
176
175
  const frame = derive(this.map, this.current);
177
176
  this.svg.applyFrame(frame);
178
177
  this.panel.applyFrame(this.map, frame);
178
+ sizeLabels(this.root, this.map, this.geo);
179
179
  this.root
180
180
  .querySelectorAll<HTMLElement>("[data-scenario]")
181
181
  .forEach((b) =>
@@ -0,0 +1,128 @@
1
+ import { mount } from "../dom/mount.js";
2
+ import type { MapPlayer } from "../dom/player.js";
3
+ import {
4
+ readInput,
5
+ resolveInput,
6
+ type MapInput,
7
+ type MapLayout,
8
+ } from "./input.js";
9
+
10
+ /** A light-DOM player. Import the element entry point to register it. */
11
+ export class ConceptMapElement extends HTMLElement {
12
+ static readonly observedAttributes = ["src", "layout"];
13
+ private assigned?: MapInput;
14
+ private inline?: unknown;
15
+ private resolver?: MapLayout;
16
+ private active?: MapPlayer;
17
+ private request?: AbortController;
18
+ private readonly observer = new MutationObserver((records) => {
19
+ if (
20
+ records.some((record) => {
21
+ const target =
22
+ record.target instanceof Text
23
+ ? record.target.parentElement
24
+ : record.target;
25
+ return (
26
+ (target instanceof HTMLScriptElement &&
27
+ target.parentElement === this) ||
28
+ (target === this &&
29
+ [...record.addedNodes, ...record.removedNodes].some(
30
+ (node) => node instanceof HTMLScriptElement,
31
+ ))
32
+ );
33
+ })
34
+ )
35
+ this.reload();
36
+ });
37
+ /** Settles after the latest input has loaded and mounted; rejects on invalid input. */
38
+ ready: Promise<MapPlayer | undefined> = Promise.resolve(undefined);
39
+
40
+ get map(): MapInput | undefined {
41
+ return this.assigned;
42
+ }
43
+ set map(value: MapInput | undefined) {
44
+ this.assigned = value;
45
+ this.reload();
46
+ }
47
+ get layout(): MapLayout | undefined {
48
+ return this.resolver;
49
+ }
50
+ set layout(value: MapLayout | undefined) {
51
+ this.resolver = value;
52
+ this.reload();
53
+ }
54
+ get player(): MapPlayer | undefined {
55
+ return this.active;
56
+ }
57
+
58
+ connectedCallback() {
59
+ for (const key of ["map", "layout"] as const) {
60
+ if (!Object.prototype.hasOwnProperty.call(this, key)) continue;
61
+ const value = this[key];
62
+ delete (this as Partial<ConceptMapElement>)[key];
63
+ if (key === "map") this.map = value as MapInput | undefined;
64
+ else this.layout = value as MapLayout | undefined;
65
+ }
66
+ this.observe();
67
+ this.reload();
68
+ }
69
+ disconnectedCallback() {
70
+ this.observer.disconnect();
71
+ this.request?.abort();
72
+ this.active?.destroy();
73
+ this.active = undefined;
74
+ this.removeAttribute("aria-busy");
75
+ }
76
+ attributeChangedCallback() {
77
+ this.reload();
78
+ }
79
+
80
+ private observe() {
81
+ this.observer.observe(this, {
82
+ childList: true,
83
+ subtree: true,
84
+ characterData: true,
85
+ });
86
+ }
87
+ private reload() {
88
+ if (!this.isConnected) return;
89
+ this.request?.abort();
90
+ const request = (this.request = new AbortController());
91
+ this.active?.destroy();
92
+ this.active = undefined;
93
+ this.setAttribute("aria-busy", "true");
94
+ this.ready = Promise.resolve().then(() => this.load(request.signal));
95
+ // Consumers can await ready or listen to maperror, without unhandled rejections.
96
+ void this.ready.catch(() => {});
97
+ }
98
+ private async load(signal: AbortSignal): Promise<MapPlayer | undefined> {
99
+ try {
100
+ if (signal.aborted) return;
101
+ const input = await readInput(this, this.assigned, signal, this.inline);
102
+ if (input === undefined || signal.aborted) return;
103
+ if (this.assigned === undefined && !this.hasAttribute("src"))
104
+ this.inline = input;
105
+ const map = await resolveInput(
106
+ input,
107
+ this.resolver,
108
+ this.getAttribute("layout"),
109
+ );
110
+ if (signal.aborted || !this.isConnected) return;
111
+ this.observer.disconnect();
112
+ this.active = mount(this, map);
113
+ this.observe();
114
+ this.dispatchEvent(
115
+ new CustomEvent("mapready", { detail: this.active, bubbles: true }),
116
+ );
117
+ return this.active;
118
+ } catch (error) {
119
+ if (signal.aborted) return;
120
+ this.dispatchEvent(
121
+ new CustomEvent("maperror", { detail: error, bubbles: true }),
122
+ );
123
+ throw error;
124
+ } finally {
125
+ if (!signal.aborted) this.removeAttribute("aria-busy");
126
+ }
127
+ }
128
+ }
@@ -0,0 +1,60 @@
1
+ import { layoutLevel } from "../schema/level.js";
2
+ import { selectMapInput } from "../model/input.js";
3
+ import { layoutWithEngine } from "./layout.js";
4
+ import {
5
+ ResolvedMapSchema,
6
+ SourceMapSchema,
7
+ type ResolvedMap,
8
+ type SourceMap,
9
+ } from "../schema.js";
10
+
11
+ export type MapInput = ResolvedMap | SourceMap;
12
+ /** A host-supplied layout function takes priority over the layout attribute. */
13
+ export type MapLayout = (map: SourceMap) => Promise<ResolvedMap>;
14
+
15
+ export async function readInput(
16
+ element: HTMLElement,
17
+ assigned: MapInput | undefined,
18
+ signal: AbortSignal,
19
+ inline?: unknown,
20
+ ): Promise<unknown> {
21
+ const selected = selectMapInput({
22
+ assigned,
23
+ src: element.getAttribute("src"),
24
+ direct: scriptText(element, ':scope > script[type="application/json"]'),
25
+ inline,
26
+ server: scriptText(element, ":scope > figure > script[data-map-data]"),
27
+ });
28
+ if (!selected) return undefined;
29
+ if (selected.kind === "value") return selected.value;
30
+ if (selected.kind === "json") return JSON.parse(selected.text);
31
+ const response = await fetch(new URL(selected.url, element.baseURI), {
32
+ signal,
33
+ });
34
+ if (!response.ok)
35
+ throw new Error(`Unable to load concept map: HTTP ${response.status}`);
36
+ return response.json();
37
+ }
38
+
39
+ export async function resolveInput(
40
+ input: unknown,
41
+ layout?: MapLayout,
42
+ engine: string | null = null,
43
+ ): Promise<ResolvedMap> {
44
+ if (layoutLevel(input) === "resolved") return ResolvedMapSchema.parse(input);
45
+ const source = SourceMapSchema.parse(input);
46
+ if (!layout && engine !== null) return layoutWithEngine(source, engine);
47
+ if (!layout)
48
+ throw new Error(
49
+ "Source map needs layout: assign element.layout or provide a resolved map",
50
+ );
51
+ return ResolvedMapSchema.parse(await layout(source));
52
+ }
53
+
54
+ function scriptText(
55
+ element: HTMLElement,
56
+ selector: string,
57
+ ): string | undefined {
58
+ const script = element.querySelector(selector);
59
+ return script ? (script.textContent ?? "") : undefined;
60
+ }
@@ -0,0 +1,18 @@
1
+ import type { SourceMap, ResolvedMap } from "../schema.js";
2
+
3
+ /** Keep engine imports behind source-map resolution, never registration or hydration. */
4
+ export async function layoutWithEngine(
5
+ source: SourceMap,
6
+ name: string,
7
+ ): Promise<ResolvedMap> {
8
+ if (name !== "dagre" && name !== "elk")
9
+ throw new Error(`Unknown layout "${name}"; expected "dagre" or "elk"`);
10
+ const { layout } = await import("../layout/index.js");
11
+ // Separate branches also keep bundler-generated preload lists independent.
12
+ if (name === "dagre") {
13
+ const { dagreEngine } = await import("../layout/dagre.js");
14
+ return layout(source, dagreEngine);
15
+ }
16
+ const { elkEngine } = await import("../layout/elk.js");
17
+ return layout(source, elkEngine);
18
+ }
package/src/element.ts ADDED
@@ -0,0 +1,11 @@
1
+ export { ConceptMapElement } from "./element/element.js";
2
+ export type { MapInput, MapLayout } from "./element/input.js";
3
+ import { ConceptMapElement } from "./element/element.js";
4
+ if (!customElements.get("concept-map"))
5
+ customElements.define("concept-map", ConceptMapElement);
6
+
7
+ declare global {
8
+ interface HTMLElementTagNameMap {
9
+ "concept-map": ConceptMapElement;
10
+ }
11
+ }
package/src/index.ts CHANGED
@@ -1,6 +1,3 @@
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
1
  export { derive } from "./model/frame.js";
5
2
  export { reduce, initialState } from "./model/state.js";
6
3
  export { planFlow, reducedFlow } from "./model/flow.js";
@@ -8,11 +5,19 @@ export { renderSvg } from "./render/svg.js";
8
5
  export { renderPanel, renderPanelParts } from "./render/panel.js";
9
6
  export { renderConcepts, renderDeeper } from "./render/concepts.js";
10
7
  export { renderInline } from "./render/inline.js";
11
- export type { ResolvedMap } from "./schema.js";
8
+ export type { ResolvedMap, SourceMap } from "./schema.js";
12
9
  export type { PlayerState, Action, Mode } from "./model/state.js";
13
10
  export type { Frame } from "./model/frame.js";
14
11
  export type { UiStrings } from "./render/panel.js";
15
12
 
16
- export { mapSchema, ResolvedMapSchema } from "./schema.js";
13
+ export {
14
+ mapSchema,
15
+ sourceMapSchema,
16
+ ResolvedMapSchema,
17
+ SourceMapSchema,
18
+ } from "./schema.js";
17
19
  export { statusText } from "./render/panel.js";
18
20
  export { escapeHtml } from "./render/inline.js";
21
+
22
+ export { renderFigure } from "./render/figure.js";
23
+ export type { FigureOptions } from "./render/figure.js";
@@ -0,0 +1,78 @@
1
+ Format: https://www.debian.org/doc/packaging-manuals/copyright-format/1.0/
2
+ Upstream-Name: DejaVu fonts
3
+ Upstream-Author: Stepan Roh <src@users.sourceforge.net> (original author),
4
+ see /usr/share/doc/fonts-dejavu-core/AUTHORS for full list
5
+ Source: https://dejavu-fonts.github.io/
6
+
7
+ Files: *
8
+ Copyright: Copyright (c) 2003 by Bitstream, Inc. All Rights Reserved.
9
+ Bitstream Vera is a trademark of Bitstream, Inc.
10
+ DejaVu changes are in public domain.
11
+ License: bitstream-vera
12
+ Permission is hereby granted, free of charge, to any person obtaining a copy
13
+ of the fonts accompanying this license ("Fonts") and associated
14
+ documentation files (the "Font Software"), to reproduce and distribute the
15
+ Font Software, including without limitation the rights to use, copy, merge,
16
+ publish, distribute, and/or sell copies of the Font Software, and to permit
17
+ persons to whom the Font Software is furnished to do so, subject to the
18
+ following conditions:
19
+ .
20
+ The above copyright and trademark notices and this permission notice shall
21
+ be included in all copies of one or more of the Font Software typefaces.
22
+ .
23
+ The Font Software may be modified, altered, or added to, and in particular
24
+ the designs of glyphs or characters in the Fonts may be modified and
25
+ additional glyphs or characters may be added to the Fonts, only if the fonts
26
+ are renamed to names not containing either the words "Bitstream" or the word
27
+ "Vera".
28
+ .
29
+ This License becomes null and void to the extent applicable to Fonts or Font
30
+ Software that has been modified and is distributed under the "Bitstream
31
+ Vera" names.
32
+ .
33
+ The Font Software may be sold as part of a larger software package but no
34
+ copy of one or more of the Font Software typefaces may be sold by itself.
35
+ .
36
+ THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
37
+ OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF MERCHANTABILITY,
38
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF COPYRIGHT, PATENT,
39
+ TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL BITSTREAM OR THE GNOME
40
+ FOUNDATION BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, INCLUDING
41
+ ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES,
42
+ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
43
+ THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM OTHER DEALINGS IN THE
44
+ FONT SOFTWARE.
45
+ .
46
+ Except as contained in this notice, the names of Gnome, the Gnome
47
+ Foundation, and Bitstream Inc., shall not be used in advertising or
48
+ otherwise to promote the sale, use or other dealings in this Font Software
49
+ without prior written authorization from the Gnome Foundation or Bitstream
50
+ Inc., respectively. For further information, contact: fonts at gnome dot
51
+ org.
52
+
53
+ Files: debian/*
54
+ Copyright: (C) 2005-2006 Peter Cernak <pce@users.sourceforge.net>
55
+ (C) 2006-2011 Davide Viti <zinosat@tiscali.it>
56
+ (C) 2011-2013 Christian Perrier <bubulle@debian.org>
57
+ (C) 2013 Fabian Greffrath <fabian+debian@greffrath.com>
58
+ License: GPL-2+
59
+ This program is free software; you can redistribute it
60
+ and/or modify it under the terms of the GNU General Public
61
+ License as published by the Free Software Foundation; either
62
+ version 2 of the License, or (at your option) any later
63
+ version.
64
+ .
65
+ This program is distributed in the hope that it will be
66
+ useful, but WITHOUT ANY WARRANTY; without even the implied
67
+ warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
68
+ PURPOSE. See the GNU General Public License for more
69
+ details.
70
+ .
71
+ You should have received a copy of the GNU General Public
72
+ License along with this package; if not, write to the Free
73
+ Software Foundation, Inc., 51 Franklin St, Fifth Floor,
74
+ Boston, MA 02110-1301 USA
75
+ .
76
+ On Debian systems, the full text of the GNU General Public
77
+ License version 2 can be found in the file
78
+ /usr/share/common-licenses/GPL-2'.
@@ -0,0 +1,71 @@
1
+ import type {
2
+ LayoutGraph,
3
+ LayoutResult,
4
+ LayoutPoint,
5
+ LayoutBox,
6
+ } from "./types.js";
7
+ import { midpoint } from "./geometry.js";
8
+ /** Keep the badge radius and a small gap clear of every label and node. */
9
+ export function badgePositions(
10
+ graph: LayoutGraph,
11
+ result: LayoutResult,
12
+ ): Record<string, LayoutPoint> {
13
+ const obstacles: LayoutBox[] = [...Object.values(result.nodes)];
14
+ for (const edge of graph.edges) {
15
+ if (!edge.label) continue;
16
+ const route = result.edges[edge.id];
17
+ const [x, y] = route.label ?? midpoint(route.points);
18
+ obstacles.push([
19
+ x - edge.label.width / 2,
20
+ y - (edge.label.height * 2) / 3,
21
+ edge.label.width,
22
+ edge.label.height,
23
+ ]);
24
+ }
25
+ const clear = ([x, y]: LayoutPoint) =>
26
+ obstacles.every(
27
+ ([left, top, w, h]) =>
28
+ Math.hypot(
29
+ Math.max(left - x, 0, x - left - w),
30
+ Math.max(top - y, 0, y - top - h),
31
+ ) >= 12,
32
+ );
33
+ return Object.fromEntries(
34
+ graph.edges.map((edge) => {
35
+ const points = result.edges[edge.id].points;
36
+ return [edge.id, freeBadge(points, clear)];
37
+ }),
38
+ );
39
+ }
40
+
41
+ function freeBadge(
42
+ points: LayoutPoint[],
43
+ clear: (p: LayoutPoint) => boolean,
44
+ ): LayoutPoint {
45
+ const middle = midpoint(points);
46
+ const candidates: LayoutPoint[] = [middle];
47
+ for (let i = 1; i < points.length; i++) {
48
+ const a = points[i - 1],
49
+ b = points[i];
50
+ for (let t = 0; t <= 1; t += 1 / 32)
51
+ candidates.push([a[0] + (b[0] - a[0]) * t, a[1] + (b[1] - a[1]) * t]);
52
+ }
53
+ candidates.sort(
54
+ (a, b) =>
55
+ Math.hypot(a[0] - middle[0], a[1] - middle[1]) -
56
+ Math.hypot(b[0] - middle[0], b[1] - middle[1]),
57
+ );
58
+ let badge = candidates.find(clear);
59
+ // Very short routes can have no free point. Move beside the route instead.
60
+ for (let offset = 16; !badge; offset += 16) {
61
+ badge = (
62
+ [
63
+ [middle[0] + offset, middle[1]],
64
+ [middle[0] - offset, middle[1]],
65
+ [middle[0], middle[1] + offset],
66
+ [middle[0], middle[1] - offset],
67
+ ] as LayoutPoint[]
68
+ ).find(clear);
69
+ }
70
+ return badge;
71
+ }
@@ -0,0 +1,81 @@
1
+ import { layoutLevel } from "../schema/level.js";
2
+ import { SourceMapSchema, ResolvedMapSchema } from "../schema.js";
3
+ import { LayoutResultSchema } from "../schema/layout.js";
4
+ import type { SourceMap, ResolvedMap } from "../schema.js";
5
+ import type { LayoutGraph, LayoutResult } from "./types.js";
6
+ function path(parts: PropertyKey[]): string {
7
+ return parts
8
+ .map((p, i) =>
9
+ typeof p === "number" ? `[${p}]` : `${i ? "." : ""}${String(p)}`,
10
+ )
11
+ .join("");
12
+ }
13
+ export function parseMap(input: unknown): SourceMap | ResolvedMap {
14
+ const result =
15
+ layoutLevel(input) === "resolved"
16
+ ? ResolvedMapSchema.safeParse(input)
17
+ : SourceMapSchema.safeParse(input);
18
+ if (!result.success)
19
+ throw new Error(
20
+ result.error.issues
21
+ .map((i) => `${path(i.path) || "map"}: ${i.message}`)
22
+ .join("\n"),
23
+ );
24
+ return result.data;
25
+ }
26
+ function keys(actual: object, ids: string[], field: string) {
27
+ const expected = new Set(ids);
28
+ for (const id of ids)
29
+ if (!Object.hasOwn(actual, id))
30
+ throw new Error(`layout.${field}: missing id “${id}”`);
31
+ for (const id of Object.keys(actual))
32
+ if (!expected.has(id))
33
+ throw new Error(`layout.${field}: unknown id “${id}”`);
34
+ }
35
+ export function checkedResult(
36
+ input: unknown,
37
+ graph: LayoutGraph,
38
+ ): LayoutResult {
39
+ const parsed = LayoutResultSchema.safeParse(input);
40
+ if (!parsed.success)
41
+ throw new Error(
42
+ parsed.error.issues
43
+ .map((i) => `layout.${path(i.path)}: ${i.message}`)
44
+ .join("\n"),
45
+ );
46
+ const result = parsed.data;
47
+ for (const field of ["nodes", "edges", "lanes"] as const)
48
+ keys(
49
+ result[field],
50
+ graph[field].map((item) => item.id),
51
+ field,
52
+ );
53
+ for (const n of graph.nodes) {
54
+ const b = result.nodes[n.id];
55
+ if (b[2] + 1e-6 < n.width || b[3] + 1e-6 < n.height)
56
+ throw new Error(`layout.nodes.${n.id}: box smaller than measured label`);
57
+ if (n.lane) {
58
+ const l = result.lanes[n.lane];
59
+ if (
60
+ b[0] < l[0] ||
61
+ b[1] < l[1] ||
62
+ b[0] + b[2] > l[0] + l[2] + 1e-6 ||
63
+ b[1] + b[3] > l[1] + l[3] + 1e-6
64
+ )
65
+ throw new Error(`layout.nodes.${n.id}: outside lane “${n.lane}”`);
66
+ }
67
+ }
68
+ checkLanes(graph, result);
69
+ return result;
70
+ }
71
+
72
+ function checkLanes(graph: LayoutGraph, result: LayoutResult): void {
73
+ const axis = graph.direction === "right" ? 0 : 1;
74
+ graph.lanes.slice(1).forEach((l, i) => {
75
+ const prev = result.lanes[graph.lanes[i].id];
76
+ if (result.lanes[l.id][axis] < prev[axis] + prev[axis + 2])
77
+ throw new Error(
78
+ `layout.lanes.${l.id}: lanes must be ordered and disjoint`,
79
+ );
80
+ });
81
+ }
@@ -0,0 +1,81 @@
1
+ import dagre from "@dagrejs/dagre";
2
+ import type { GraphLabel, NodeLabel, EdgeLabel, Point } from "@dagrejs/dagre";
3
+ import type {
4
+ LayoutEngine,
5
+ LayoutResult,
6
+ LayoutPoint,
7
+ LayoutGraph,
8
+ LayoutNode,
9
+ } from "./types.js";
10
+ import { routeSelfEdges, reservedSelfRoute } from "./self-route.js";
11
+ import { engineNodes, partitionDagre } from "./lanes.js";
12
+ function dagreGraph(graph: LayoutGraph, nodes: LayoutNode[]) {
13
+ const g = new dagre.graphlib.Graph<GraphLabel, NodeLabel, EdgeLabel, Point>({
14
+ multigraph: true,
15
+ });
16
+ g.setGraph({
17
+ rankdir: graph.direction === "right" ? "LR" : "TB",
18
+ ranksep: 64,
19
+ nodesep: 32,
20
+ marginx: 16,
21
+ marginy: 32,
22
+ });
23
+ for (const n of nodes)
24
+ g.setNode(`n:${n.id}`, { width: n.width, height: n.height });
25
+ for (const e of graph.edges)
26
+ g.setEdge(
27
+ `n:${e.from}`,
28
+ `n:${e.to}`,
29
+ {
30
+ width: e.label?.width ?? 0,
31
+ height: e.label?.height ?? 0,
32
+ // Self-edges already have a separate dummy label lane.
33
+ labelpos: e.from === e.to ? "c" : "r",
34
+ labeloffset: 12,
35
+ },
36
+ `e:${e.id}`,
37
+ );
38
+ return g;
39
+ }
40
+ /** Explicit Dagre adapter; never imported by the player or layout entry point. */
41
+ export const dagreEngine: LayoutEngine = {
42
+ async layout(graph) {
43
+ const nodes = engineNodes(graph);
44
+ const g = dagreGraph(graph, nodes);
45
+ dagre.layout(g);
46
+ const result: LayoutResult = { nodes: {}, edges: {}, lanes: {} };
47
+ for (const n of nodes) {
48
+ const b = g.node(`n:${n.id}`);
49
+ result.nodes[n.id] = [
50
+ (b.x ?? NaN) - b.width / 2,
51
+ (b.y ?? NaN) - b.height / 2,
52
+ b.width,
53
+ b.height,
54
+ ];
55
+ }
56
+ for (const e of graph.edges) {
57
+ const route = g.edge({
58
+ v: `n:${e.from}`,
59
+ w: `n:${e.to}`,
60
+ name: `e:${e.id}`,
61
+ });
62
+ result.edges[e.id] =
63
+ e.from === e.to
64
+ ? reservedSelfRoute(
65
+ result.nodes[e.from],
66
+ [route.x ?? 0, route.y ?? 0],
67
+ e.label,
68
+ graph.direction === "right",
69
+ )
70
+ : {
71
+ points: (route.points ?? []).map(
72
+ (p: Point) => [p.x, p.y] as LayoutPoint,
73
+ ),
74
+ ...(e.label && route.x !== undefined && route.y !== undefined
75
+ ? { label: [route.x ?? NaN, route.y ?? NaN] as LayoutPoint }
76
+ : {}),
77
+ };
78
+ }
79
+ return routeSelfEdges(graph, partitionDagre(graph, nodes, result));
80
+ },
81
+ };