@metronforge/concept-map 0.1.1 → 0.2.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.
Files changed (199) hide show
  1. package/README.md +124 -93
  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,91 @@
1
+ import * as z from "zod";
2
+ import {
3
+ Id,
4
+ Layer,
5
+ Display,
6
+ Hop,
7
+ ids,
8
+ record,
9
+ type Leaves,
10
+ } from "./primitives.js";
11
+
12
+ function stepSchema<
13
+ T extends z.ZodType,
14
+ S extends z.ZodType,
15
+ D extends z.ZodType,
16
+ >(leaf: Leaves<T, S, D>) {
17
+ return z.object({
18
+ id: Id,
19
+ title: z.string(),
20
+ text: leaf.text,
21
+ show: ids(),
22
+ current: ids(),
23
+ edges: ids(),
24
+ defs: ids(),
25
+ code: Id.optional(),
26
+ log: z.array(z.string()).optional(),
27
+ fix: leaf.text.optional(),
28
+ sources: z.array(leaf.source).default([]),
29
+ deeper: z.array(leaf.deeper).default([]),
30
+ });
31
+ }
32
+ export function scenarioSchema<
33
+ T extends z.ZodType,
34
+ S extends z.ZodType,
35
+ D extends z.ZodType,
36
+ B extends z.ZodType,
37
+ >(leaf: Leaves<T, S, D>, badge: B) {
38
+ return z.object({
39
+ id: Id,
40
+ label: z.string(),
41
+ display: Display.prefault({}),
42
+ nodeLabels: record(z.string()).optional(),
43
+ whole: z
44
+ .object({ title: z.string().optional(), text: leaf.text.optional() })
45
+ .optional(),
46
+ steps: z.array(stepSchema(leaf)).min(1),
47
+ flow: z.array(Hop).optional(),
48
+ repeatBadges: z.array(badge).optional(),
49
+ });
50
+ }
51
+ export function conceptSchema<
52
+ T extends z.ZodType,
53
+ S extends z.ZodType,
54
+ D extends z.ZodType,
55
+ >(leaf: Leaves<T, S, D>) {
56
+ return z.object({
57
+ label: z.string(),
58
+ layer: Layer,
59
+ items: z.array(
60
+ z.object({
61
+ definition: Id,
62
+ short: leaf.text,
63
+ nodes: ids(),
64
+ step: z.object({ scenario: Id.optional(), step: Id }),
65
+ source: leaf.source.optional(),
66
+ }),
67
+ ),
68
+ });
69
+ }
70
+ export function contentSchema<
71
+ T extends z.ZodType,
72
+ S extends z.ZodType,
73
+ D extends z.ZodType,
74
+ >(leaf: Leaves<T, S, D>) {
75
+ return {
76
+ definitions: record(
77
+ z.object({ term: z.string(), text: leaf.text }),
78
+ ).default({}),
79
+ code: record(
80
+ z.object({
81
+ title: z.string(),
82
+ source: leaf.source.optional(),
83
+ lines: z.array(z.string()),
84
+ }),
85
+ ).default({}),
86
+ conceptScenario: Id.optional(),
87
+ concepts: z.array(conceptSchema(leaf)).default([]),
88
+ callouts: z.record(z.string(), ids()).default({}),
89
+ deeper: z.array(leaf.deeper).default([]),
90
+ };
91
+ }
@@ -0,0 +1,10 @@
1
+ import * as z from "zod";
2
+ import { Box, Point } from "./primitives.js";
3
+ export const LayoutResultSchema = z.strictObject({
4
+ nodes: z.record(z.string(), Box),
5
+ lanes: z.record(z.string(), Box),
6
+ edges: z.record(
7
+ z.string(),
8
+ z.strictObject({ points: z.array(Point).min(2), label: Point.optional() }),
9
+ ),
10
+ });
@@ -0,0 +1,6 @@
1
+ /** Select a schema by geometry presence; validation still checks its contents. */
2
+ export function layoutLevel(input: unknown): "source" | "resolved" {
3
+ return input !== null && typeof input === "object" && "viewBox" in input
4
+ ? "resolved"
5
+ : "source";
6
+ }
@@ -0,0 +1,55 @@
1
+ import * as z from "zod";
2
+
3
+ export const Id = z.string().regex(/^[A-Za-z][A-Za-z0-9_-]*$/);
4
+ export const Point = z.tuple([z.number(), z.number()]);
5
+ export const Box = z.tuple([
6
+ z.number(),
7
+ z.number(),
8
+ z.number().positive(),
9
+ z.number().positive(),
10
+ ]);
11
+ export const Layer = Id;
12
+ export const ids = () => z.array(Id).default([]);
13
+ export const record = <T extends z.ZodType>(value: T) => z.record(Id, value);
14
+ export const Display = z.object({
15
+ counterLabel: z.boolean().default(true),
16
+ edges: z.enum(["progressive", "all"]).default("progressive"),
17
+ edgeBadges: z.boolean().default(true),
18
+ nodeBadges: z.boolean().default(false),
19
+ log: z.enum(["incremental", "cumulative"]).default("incremental"),
20
+ });
21
+ export const Lane = z.strictObject({ label: z.string(), layer: Layer });
22
+ export const Node = z.strictObject({
23
+ label: z.string(),
24
+ layer: Layer,
25
+ lane: Id.optional(),
26
+ tag: z.string().optional(),
27
+ mono: z.boolean().optional(),
28
+ fail: z.boolean().optional(),
29
+ custom: z.boolean().optional(),
30
+ });
31
+ export const Edge = z.strictObject({
32
+ from: Id,
33
+ to: Id,
34
+ label: z.string().optional(),
35
+ dashed: z.boolean().optional(),
36
+ });
37
+ export const Frame = z.strictObject({ label: z.string(), showWith: ids() });
38
+ export const RepeatBadge = z.strictObject({ edge: Id, step: Id });
39
+ export const Hop = z.object({
40
+ node: Id,
41
+ step: Id,
42
+ log: z.array(z.string()).default([]),
43
+ edge: Id.optional(),
44
+ reverse: z.boolean().optional(),
45
+ fail: z.boolean().optional(),
46
+ });
47
+ export interface Leaves<
48
+ T extends z.ZodType,
49
+ S extends z.ZodType,
50
+ D extends z.ZodType,
51
+ > {
52
+ text: T;
53
+ source: S;
54
+ deeper: D;
55
+ }
@@ -0,0 +1,106 @@
1
+ import type { SourceMap, ResolvedMap } from "../schema.js";
2
+ type MapData = SourceMap | ResolvedMap;
3
+ type Check = (
4
+ path: string,
5
+ kind: string,
6
+ id: string | undefined,
7
+ values: object | readonly string[],
8
+ ) => void;
9
+
10
+ function unique(ids: string[], path: string, kind: string, errors: string[]) {
11
+ const seen = new Set<string>();
12
+ ids.forEach((id, i) => {
13
+ if (seen.has(id))
14
+ errors.push(`${path}[${i}].id: duplicate ${kind} “${id}”`);
15
+ seen.add(id);
16
+ });
17
+ }
18
+ function steps(
19
+ map: MapData,
20
+ s: MapData["scenarios"][number],
21
+ path: string,
22
+ check: Check,
23
+ ) {
24
+ s.steps.forEach((step, i) => {
25
+ const p = `${path}.steps[${i}]`;
26
+ for (const field of ["show", "current"] as const)
27
+ for (const id of step[field])
28
+ check(`${p}.${field}`, "node", id, map.nodes);
29
+ for (const id of step.edges) check(`${p}.edges`, "edge", id, map.edges);
30
+ for (const id of step.defs)
31
+ check(`${p}.defs`, "definition", id, map.definitions);
32
+ check(`${p}.code`, "code", step.code, map.code);
33
+ });
34
+ }
35
+ function scenarios(map: MapData, check: Check, errors: string[]) {
36
+ map.scenarios.forEach((s, i) => {
37
+ const p = `scenarios[${i}]`,
38
+ ids = s.steps.map((t) => t.id);
39
+ unique(ids, `${p}.steps`, "step", errors);
40
+ steps(map, s, p, check);
41
+ for (const id of Object.keys(s.nodeLabels ?? {}))
42
+ check(`${p}.nodeLabels.${id}`, "node", id, map.nodes);
43
+ s.flow?.forEach((h, j) => {
44
+ check(`${p}.flow[${j}].node`, "node", h.node, map.nodes);
45
+ check(`${p}.flow[${j}].edge`, "edge", h.edge, map.edges);
46
+ check(`${p}.flow[${j}].step`, "step", h.step, ids);
47
+ });
48
+ s.repeatBadges?.forEach((b, j) => {
49
+ check(`${p}.repeatBadges[${j}].edge`, "edge", b.edge, map.edges);
50
+ check(`${p}.repeatBadges[${j}].step`, "step", b.step, ids);
51
+ });
52
+ });
53
+ }
54
+ function concepts(map: MapData, check: Check) {
55
+ map.concepts.forEach((group, i) =>
56
+ group.items.forEach((c, j) => {
57
+ const p = `concepts[${i}].items[${j}]`;
58
+ check(`${p}.definition`, "definition", c.definition, map.definitions);
59
+ for (const id of c.nodes) check(`${p}.nodes`, "node", id, map.nodes);
60
+ const scenario =
61
+ c.step.scenario ?? map.conceptScenario ?? map.scenarios[0].id;
62
+ check(
63
+ `${p}.step.scenario`,
64
+ "scenario",
65
+ scenario,
66
+ map.scenarios.map((s) => s.id),
67
+ );
68
+ const s = map.scenarios.find((s) => s.id === scenario);
69
+ if (s)
70
+ check(
71
+ `${p}.step.step`,
72
+ "step",
73
+ c.step.step,
74
+ s.steps.map((t) => t.id),
75
+ );
76
+ }),
77
+ );
78
+ }
79
+ /** Check every semantic reference before a layout engine sees a map. */
80
+ export function validateReferences(map: MapData): void {
81
+ const errors: string[] = [];
82
+ const check: Check = (path, kind, id, values) => {
83
+ if (
84
+ id !== undefined &&
85
+ !(Array.isArray(values) ? values.includes(id) : Object.hasOwn(values, id))
86
+ )
87
+ errors.push(`${path}: unknown ${kind} “${id}”`);
88
+ };
89
+ const scenarioIds = map.scenarios.map((s) => s.id);
90
+ unique(scenarioIds, "scenarios", "scenario", errors);
91
+ check("conceptScenario", "scenario", map.conceptScenario, scenarioIds);
92
+ for (const [id, n] of Object.entries(map.nodes))
93
+ check(`nodes.${id}.lane`, "lane", n.lane, map.lanes);
94
+ for (const [id, edge] of Object.entries(map.edges)) {
95
+ check(`edges.${id}.from`, "node", edge.from, map.nodes);
96
+ check(`edges.${id}.to`, "node", edge.to, map.nodes);
97
+ }
98
+ for (const [id, frame] of Object.entries(map.frames))
99
+ for (const node of frame.showWith)
100
+ check(`frames.${id}.showWith`, "node", node, map.nodes);
101
+ for (const [id, nodes] of Object.entries(map.callouts))
102
+ for (const node of nodes) check(`callouts.${id}`, "node", node, map.nodes);
103
+ scenarios(map, check, errors);
104
+ concepts(map, check);
105
+ if (errors.length) throw new Error(errors.join("\n"));
106
+ }
package/src/schema.ts CHANGED
@@ -1,162 +1,101 @@
1
- import { z } from "zod";
1
+ import * as z from "zod";
2
+ import {
3
+ Id,
4
+ Point,
5
+ Box,
6
+ Lane,
7
+ Node,
8
+ Edge,
9
+ Frame,
10
+ RepeatBadge,
11
+ record,
12
+ } from "./schema/primitives.js";
13
+ import { contentSchema, scenarioSchema } from "./schema/content.js";
14
+ export { Id, Point, Box, Layer, Display } from "./schema/primitives.js";
2
15
 
3
- export const Id = z.string().regex(/^[A-Za-z][A-Za-z0-9_-]*$/);
4
- export const Point = z.tuple([z.number(), z.number()]);
5
- export const Box = z.tuple([
6
- z.number(),
7
- z.number(),
8
- z.number().positive(),
9
- z.number().positive(),
10
- ]);
11
- export const Layer = z.enum([
12
- "runner",
13
- "client",
14
- "server",
15
- "page",
16
- "browser",
17
- "app",
18
- "fail",
19
- "test",
20
- "protocol",
21
- ]);
22
- const ids = () => z.array(Id);
23
- const record = <T extends z.ZodType>(value: T) => z.record(Id, value);
24
- export const Display = z.object({
25
- counterLabel: z.boolean().default(true),
26
- edges: z.enum(["progressive", "all"]).default("progressive"),
27
- edgeBadges: z.boolean().default(true),
28
- nodeBadges: z.boolean().default(false),
29
- log: z.enum(["incremental", "cumulative"]).default("incremental"),
30
- });
31
-
32
- /** The adapter specializes only leaves; runtime modules use inferred types only. */
33
- export function mapSchema<
16
+ /** Shared semantic structure; source maps deliberately reject geometry. */
17
+ export function sourceMapSchema<
34
18
  T extends z.ZodType,
35
19
  S extends z.ZodType,
36
20
  D extends z.ZodType,
37
21
  >(leaf: { text: T; source: S; deeper: D }) {
38
- return z.object({
39
- format: z.literal(2),
40
- spread: z.string(),
22
+ return z.strictObject({
23
+ format: z.literal(3),
24
+ id: z.string().min(1),
41
25
  title: z.string(),
26
+ direction: z.enum(["down", "right"]).optional(),
27
+ layers: record(z.object({ label: z.string() })),
28
+ lanes: record(Lane).default({}),
29
+ frames: record(Frame).default({}),
30
+ nodes: record(Node),
31
+ edges: record(Edge).default({}),
32
+ scenarios: z.array(scenarioSchema(leaf, RepeatBadge)).min(1),
33
+ ...contentSchema(leaf),
34
+ });
35
+ }
36
+ /** The adapter specializes leaves; resolved maps additionally require geometry. */
37
+ export function mapSchema<
38
+ T extends z.ZodType,
39
+ S extends z.ZodType,
40
+ D extends z.ZodType,
41
+ >(leaf: { text: T; source: S; deeper: D }) {
42
+ return sourceMapSchema(leaf).extend({
42
43
  viewBox: z.tuple([z.number().positive(), z.number().positive()]),
43
44
  lanes: record(
44
- z.object({
45
- label: z.string(),
46
- layer: Layer,
45
+ Lane.extend({
47
46
  y: z.number(),
48
47
  h: z.number().positive(),
49
48
  x: z.number().optional(),
50
49
  w: z.number().positive().optional(),
51
50
  }),
52
- ),
53
- frames: record(z.object({ label: z.string(), box: Box, showWith: ids() })),
54
- nodes: record(
55
- z.object({
56
- label: z.string(),
57
- layer: Layer,
58
- lane: Id,
59
- box: Box,
60
- tag: z.string().optional(),
61
- mono: z.boolean().optional(),
62
- fail: z.boolean().optional(),
63
- custom: z.boolean().optional(),
64
- }),
65
- ),
51
+ ).default({}),
52
+ frames: record(Frame.extend({ box: Box })).default({}),
53
+ nodes: record(Node.extend({ box: Box })),
66
54
  edges: record(
67
- z.object({
68
- from: Id,
69
- to: Id,
55
+ Edge.extend({
70
56
  path: z.string(),
71
- dashed: z.boolean().optional(),
72
57
  badge: Point.optional(),
73
58
  label: z.object({ at: Point, text: z.string() }).optional(),
74
59
  }),
75
- ),
76
- definitions: record(z.object({ term: z.string(), text: leaf.text })),
77
- code: record(
78
- z.object({
79
- title: z.string(),
80
- source: leaf.source.optional(),
81
- lines: z.array(z.string()),
82
- }),
83
- ),
60
+ ).default({}),
84
61
  scenarios: z
85
- .array(
86
- z.object({
87
- id: Id,
88
- label: z.string(),
89
- display: Display.prefault({}),
90
- nodeLabels: record(z.string()).optional(),
91
- whole: z
92
- .object({
93
- title: z.string().optional(),
94
- text: leaf.text.optional(),
95
- })
96
- .optional(),
97
- steps: z
98
- .array(
99
- z.object({
100
- id: Id,
101
- title: z.string(),
102
- text: leaf.text,
103
- show: ids(),
104
- current: ids(),
105
- edges: ids(),
106
- defs: ids(),
107
- code: Id.optional(),
108
- log: z.array(z.string()).optional(),
109
- fix: leaf.text.optional(),
110
- sources: z.array(leaf.source),
111
- deeper: z.array(leaf.deeper),
112
- }),
113
- )
114
- .min(1),
115
- flow: z
116
- .array(
117
- z.object({
118
- node: Id,
119
- step: Id,
120
- log: z.array(z.string()),
121
- edge: Id.optional(),
122
- reverse: z.boolean().optional(),
123
- fail: z.boolean().optional(),
124
- }),
125
- )
126
- .optional(),
127
- repeatBadges: z
128
- .array(z.object({ edge: Id, step: Id, at: Point }))
129
- .optional(),
130
- }),
131
- )
62
+ .array(scenarioSchema(leaf, RepeatBadge.extend({ at: Point })))
132
63
  .min(1),
133
- conceptScenario: Id,
134
- concepts: z.array(
135
- z.object({
136
- label: z.string(),
137
- layer: Layer,
138
- items: z.array(
139
- z.object({
140
- definition: Id,
141
- short: leaf.text,
142
- nodes: ids(),
143
- step: z.object({ scenario: Id.optional(), step: Id }),
144
- source: leaf.source,
145
- }),
146
- ),
147
- }),
148
- ),
149
- callouts: z.record(z.string(), ids()),
150
- deeper: z.array(z.object({ spread: leaf.deeper, why: leaf.text })),
151
64
  });
152
65
  }
153
66
  export const ResolvedSource = z.object({ label: z.string(), url: z.url() });
154
67
  export const ResolvedDeeper = z.object({ title: z.string(), href: z.string() });
155
- export const ResolvedMapSchema = mapSchema({
68
+ const leaves = {
156
69
  text: z.string(),
157
70
  source: ResolvedSource,
158
71
  deeper: ResolvedDeeper,
159
- }).extend({
160
- deeper: z.array(ResolvedDeeper.extend({ why: z.string() })),
161
- });
72
+ };
73
+ const deeper = z.array(ResolvedDeeper.extend({ why: z.string() })).default([]);
74
+
75
+ /** Layer identifiers are CSS-safe keys declared by this map, never a fixed enum. */
76
+ function checkLayers(
77
+ map: Pick<
78
+ z.infer<ReturnType<typeof sourceMapSchema>>,
79
+ "layers" | "nodes" | "lanes" | "concepts"
80
+ >,
81
+ ctx: z.RefinementCtx,
82
+ ) {
83
+ for (const field of ["nodes", "lanes", "concepts"] as const) {
84
+ for (const [key, item] of Object.entries(map[field])) {
85
+ if (!Object.hasOwn(map.layers, item.layer))
86
+ ctx.addIssue({
87
+ code: "custom",
88
+ path: [field, field === "concepts" ? Number(key) : key, "layer"],
89
+ message: `Unknown layer: ${item.layer}`,
90
+ });
91
+ }
92
+ }
93
+ }
94
+ export const SourceMapSchema = sourceMapSchema(leaves)
95
+ .extend({ deeper })
96
+ .superRefine(checkLayers);
97
+ export const ResolvedMapSchema = mapSchema(leaves)
98
+ .extend({ deeper })
99
+ .superRefine(checkLayers);
100
+ export type SourceMap = z.infer<typeof SourceMapSchema>;
162
101
  export type ResolvedMap = z.infer<typeof ResolvedMapSchema>;