@cratis/scene.engine 4.11.0 → 4.13.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 (218) hide show
  1. package/dist/cjs/forms/CommandFormLayoutDiagnosticCode.js +20 -0
  2. package/dist/cjs/forms/CommandFormLayoutDiagnosticCode.js.map +1 -0
  3. package/dist/cjs/forms/commitCommandFormLayoutDraft.js +12 -0
  4. package/dist/cjs/forms/commitCommandFormLayoutDraft.js.map +1 -0
  5. package/dist/cjs/forms/defaultCommandFormLayout.js +23 -0
  6. package/dist/cjs/forms/defaultCommandFormLayout.js.map +1 -0
  7. package/dist/cjs/forms/formWidthToCss.js +24 -0
  8. package/dist/cjs/forms/formWidthToCss.js.map +1 -0
  9. package/dist/cjs/forms/normalizeCommandFormLayout.js +42 -0
  10. package/dist/cjs/forms/normalizeCommandFormLayout.js.map +1 -0
  11. package/dist/cjs/forms/parseFormWidth.js +34 -0
  12. package/dist/cjs/forms/parseFormWidth.js.map +1 -0
  13. package/dist/cjs/forms/preserveDirtyCommandValues.js +15 -0
  14. package/dist/cjs/forms/preserveDirtyCommandValues.js.map +1 -0
  15. package/dist/cjs/forms/resizeCommandFormColumn.js +27 -0
  16. package/dist/cjs/forms/resizeCommandFormColumn.js.map +1 -0
  17. package/dist/cjs/forms/updateCommandFormFieldPlacement.js +27 -0
  18. package/dist/cjs/forms/updateCommandFormFieldPlacement.js.map +1 -0
  19. package/dist/cjs/forms/validateCommandFormLayout.js +72 -0
  20. package/dist/cjs/forms/validateCommandFormLayout.js.map +1 -0
  21. package/dist/cjs/index.js +40 -0
  22. package/dist/cjs/index.js.map +1 -1
  23. package/dist/cjs/navigation/NavigationDiagnosticCode.js +19 -0
  24. package/dist/cjs/navigation/NavigationDiagnosticCode.js.map +1 -0
  25. package/dist/cjs/navigation/collectOutlets.js +45 -0
  26. package/dist/cjs/navigation/collectOutlets.js.map +1 -0
  27. package/dist/cjs/navigation/diagnoseNavigation.js +116 -0
  28. package/dist/cjs/navigation/diagnoseNavigation.js.map +1 -0
  29. package/dist/cjs/navigation/findHostingCycles.js +43 -0
  30. package/dist/cjs/navigation/findHostingCycles.js.map +1 -0
  31. package/dist/cjs/navigation/navigationEntriesFrom.js +19 -0
  32. package/dist/cjs/navigation/navigationEntriesFrom.js.map +1 -0
  33. package/dist/cjs/navigation/navigationRoute.js +25 -0
  34. package/dist/cjs/navigation/navigationRoute.js.map +1 -0
  35. package/dist/cjs/packageVersionRange.js +16 -0
  36. package/dist/cjs/packageVersionRange.js.map +1 -1
  37. package/dist/cjs/templates/TemplateCatalogKind.js +15 -0
  38. package/dist/cjs/templates/TemplateCatalogKind.js.map +1 -0
  39. package/dist/cjs/templates/describeTemplateCatalog.js +89 -0
  40. package/dist/cjs/templates/describeTemplateCatalog.js.map +1 -0
  41. package/dist/cjs/templates/templateMetadataRules.js +32 -0
  42. package/dist/cjs/templates/templateMetadataRules.js.map +1 -0
  43. package/dist/cjs/templates/validateTemplateMetadata.js +50 -0
  44. package/dist/cjs/templates/validateTemplateMetadata.js.map +1 -0
  45. package/dist/cjs/validateNavigationDestinations.js +12 -38
  46. package/dist/cjs/validateNavigationDestinations.js.map +1 -1
  47. package/dist/esm/forms/CommandFormLayoutDiagnostic.d.ts +7 -0
  48. package/dist/esm/forms/CommandFormLayoutDiagnostic.d.ts.map +1 -0
  49. package/dist/esm/forms/CommandFormLayoutDiagnostic.js +2 -0
  50. package/dist/esm/forms/CommandFormLayoutDiagnostic.js.map +1 -0
  51. package/dist/esm/forms/CommandFormLayoutDiagnosticCode.d.ts +11 -0
  52. package/dist/esm/forms/CommandFormLayoutDiagnosticCode.d.ts.map +1 -0
  53. package/dist/esm/forms/CommandFormLayoutDiagnosticCode.js +18 -0
  54. package/dist/esm/forms/CommandFormLayoutDiagnosticCode.js.map +1 -0
  55. package/dist/esm/forms/CommandFormLayoutDraft.d.ts +9 -0
  56. package/dist/esm/forms/CommandFormLayoutDraft.d.ts.map +1 -0
  57. package/dist/esm/forms/CommandFormLayoutDraft.js +2 -0
  58. package/dist/esm/forms/CommandFormLayoutDraft.js.map +1 -0
  59. package/dist/esm/forms/commitCommandFormLayoutDraft.d.ts +4 -0
  60. package/dist/esm/forms/commitCommandFormLayoutDraft.d.ts.map +1 -0
  61. package/dist/esm/forms/commitCommandFormLayoutDraft.js +10 -0
  62. package/dist/esm/forms/commitCommandFormLayoutDraft.js.map +1 -0
  63. package/dist/esm/forms/defaultCommandFormLayout.d.ts +3 -0
  64. package/dist/esm/forms/defaultCommandFormLayout.d.ts.map +1 -0
  65. package/dist/esm/forms/defaultCommandFormLayout.js +21 -0
  66. package/dist/esm/forms/defaultCommandFormLayout.js.map +1 -0
  67. package/dist/esm/forms/formWidthToCss.d.ts +3 -0
  68. package/dist/esm/forms/formWidthToCss.d.ts.map +1 -0
  69. package/dist/esm/forms/formWidthToCss.js +22 -0
  70. package/dist/esm/forms/formWidthToCss.js.map +1 -0
  71. package/dist/esm/forms/index.d.ts +13 -0
  72. package/dist/esm/forms/index.d.ts.map +1 -0
  73. package/dist/esm/forms/index.js +13 -0
  74. package/dist/esm/forms/index.js.map +1 -0
  75. package/dist/esm/forms/normalizeCommandFormLayout.d.ts +8 -0
  76. package/dist/esm/forms/normalizeCommandFormLayout.d.ts.map +1 -0
  77. package/dist/esm/forms/normalizeCommandFormLayout.js +40 -0
  78. package/dist/esm/forms/normalizeCommandFormLayout.js.map +1 -0
  79. package/dist/esm/forms/parseFormWidth.d.ts +3 -0
  80. package/dist/esm/forms/parseFormWidth.d.ts.map +1 -0
  81. package/dist/esm/forms/parseFormWidth.js +32 -0
  82. package/dist/esm/forms/parseFormWidth.js.map +1 -0
  83. package/dist/esm/forms/preserveDirtyCommandValues.d.ts +2 -0
  84. package/dist/esm/forms/preserveDirtyCommandValues.d.ts.map +1 -0
  85. package/dist/esm/forms/preserveDirtyCommandValues.js +13 -0
  86. package/dist/esm/forms/preserveDirtyCommandValues.js.map +1 -0
  87. package/dist/esm/forms/resizeCommandFormColumn.d.ts +4 -0
  88. package/dist/esm/forms/resizeCommandFormColumn.d.ts.map +1 -0
  89. package/dist/esm/forms/resizeCommandFormColumn.js +25 -0
  90. package/dist/esm/forms/resizeCommandFormColumn.js.map +1 -0
  91. package/dist/esm/forms/updateCommandFormFieldPlacement.d.ts +4 -0
  92. package/dist/esm/forms/updateCommandFormFieldPlacement.d.ts.map +1 -0
  93. package/dist/esm/forms/updateCommandFormFieldPlacement.js +25 -0
  94. package/dist/esm/forms/updateCommandFormFieldPlacement.js.map +1 -0
  95. package/dist/esm/forms/validateCommandFormLayout.d.ts +4 -0
  96. package/dist/esm/forms/validateCommandFormLayout.d.ts.map +1 -0
  97. package/dist/esm/forms/validateCommandFormLayout.js +70 -0
  98. package/dist/esm/forms/validateCommandFormLayout.js.map +1 -0
  99. package/dist/esm/index.d.ts +3 -0
  100. package/dist/esm/index.d.ts.map +1 -1
  101. package/dist/esm/index.js +19 -1
  102. package/dist/esm/index.js.map +1 -1
  103. package/dist/esm/navigation/NavigationDiagnostic.d.ts +7 -0
  104. package/dist/esm/navigation/NavigationDiagnostic.d.ts.map +1 -0
  105. package/dist/esm/navigation/NavigationDiagnostic.js +2 -0
  106. package/dist/esm/navigation/NavigationDiagnostic.js.map +1 -0
  107. package/dist/esm/navigation/NavigationDiagnosticCode.d.ts +9 -0
  108. package/dist/esm/navigation/NavigationDiagnosticCode.d.ts.map +1 -0
  109. package/dist/esm/navigation/NavigationDiagnosticCode.js +17 -0
  110. package/dist/esm/navigation/NavigationDiagnosticCode.js.map +1 -0
  111. package/dist/esm/navigation/NavigationEntry.d.ts +6 -0
  112. package/dist/esm/navigation/NavigationEntry.d.ts.map +1 -0
  113. package/dist/esm/navigation/NavigationEntry.js +2 -0
  114. package/dist/esm/navigation/NavigationEntry.js.map +1 -0
  115. package/dist/esm/navigation/NavigationGraph.d.ts +10 -0
  116. package/dist/esm/navigation/NavigationGraph.d.ts.map +1 -0
  117. package/dist/esm/navigation/NavigationGraph.js +2 -0
  118. package/dist/esm/navigation/NavigationGraph.js.map +1 -0
  119. package/dist/esm/navigation/OutletOwner.d.ts +7 -0
  120. package/dist/esm/navigation/OutletOwner.d.ts.map +1 -0
  121. package/dist/esm/navigation/OutletOwner.js +2 -0
  122. package/dist/esm/navigation/OutletOwner.js.map +1 -0
  123. package/dist/esm/navigation/collectOutlets.d.ts +5 -0
  124. package/dist/esm/navigation/collectOutlets.d.ts.map +1 -0
  125. package/dist/esm/navigation/collectOutlets.js +43 -0
  126. package/dist/esm/navigation/collectOutlets.js.map +1 -0
  127. package/dist/esm/navigation/diagnoseNavigation.d.ts +5 -0
  128. package/dist/esm/navigation/diagnoseNavigation.d.ts.map +1 -0
  129. package/dist/esm/navigation/diagnoseNavigation.js +114 -0
  130. package/dist/esm/navigation/diagnoseNavigation.js.map +1 -0
  131. package/dist/esm/navigation/findHostingCycles.d.ts +2 -0
  132. package/dist/esm/navigation/findHostingCycles.d.ts.map +1 -0
  133. package/dist/esm/navigation/findHostingCycles.js +41 -0
  134. package/dist/esm/navigation/findHostingCycles.js.map +1 -0
  135. package/dist/esm/navigation/index.d.ts +8 -0
  136. package/dist/esm/navigation/index.d.ts.map +1 -0
  137. package/dist/esm/navigation/index.js +8 -0
  138. package/dist/esm/navigation/index.js.map +1 -0
  139. package/dist/esm/navigation/navigationEntriesFrom.d.ts +4 -0
  140. package/dist/esm/navigation/navigationEntriesFrom.d.ts.map +1 -0
  141. package/dist/esm/navigation/navigationEntriesFrom.js +17 -0
  142. package/dist/esm/navigation/navigationEntriesFrom.js.map +1 -0
  143. package/dist/esm/navigation/navigationRoute.d.ts +5 -0
  144. package/dist/esm/navigation/navigationRoute.d.ts.map +1 -0
  145. package/dist/esm/navigation/navigationRoute.js +22 -0
  146. package/dist/esm/navigation/navigationRoute.js.map +1 -0
  147. package/dist/esm/packageVersionRange.d.ts +2 -0
  148. package/dist/esm/packageVersionRange.d.ts.map +1 -1
  149. package/dist/esm/packageVersionRange.js +15 -1
  150. package/dist/esm/packageVersionRange.js.map +1 -1
  151. package/dist/esm/templates/TemplateCatalogEntry.d.ts +20 -0
  152. package/dist/esm/templates/TemplateCatalogEntry.d.ts.map +1 -0
  153. package/dist/esm/templates/TemplateCatalogEntry.js +2 -0
  154. package/dist/esm/templates/TemplateCatalogEntry.js.map +1 -0
  155. package/dist/esm/templates/TemplateCatalogKind.d.ts +6 -0
  156. package/dist/esm/templates/TemplateCatalogKind.d.ts.map +1 -0
  157. package/dist/esm/templates/TemplateCatalogKind.js +13 -0
  158. package/dist/esm/templates/TemplateCatalogKind.js.map +1 -0
  159. package/dist/esm/templates/TemplateSource.d.ts +8 -0
  160. package/dist/esm/templates/TemplateSource.d.ts.map +1 -0
  161. package/dist/esm/templates/TemplateSource.js +2 -0
  162. package/dist/esm/templates/TemplateSource.js.map +1 -0
  163. package/dist/esm/templates/describeTemplateCatalog.d.ts +4 -0
  164. package/dist/esm/templates/describeTemplateCatalog.d.ts.map +1 -0
  165. package/dist/esm/templates/describeTemplateCatalog.js +87 -0
  166. package/dist/esm/templates/describeTemplateCatalog.js.map +1 -0
  167. package/dist/esm/templates/index.d.ts +7 -0
  168. package/dist/esm/templates/index.d.ts.map +1 -0
  169. package/dist/esm/templates/index.js +7 -0
  170. package/dist/esm/templates/index.js.map +1 -0
  171. package/dist/esm/templates/templateMetadataRules.d.ts +3 -0
  172. package/dist/esm/templates/templateMetadataRules.d.ts.map +1 -0
  173. package/dist/esm/templates/templateMetadataRules.js +29 -0
  174. package/dist/esm/templates/templateMetadataRules.js.map +1 -0
  175. package/dist/esm/templates/validateTemplateMetadata.d.ts +3 -0
  176. package/dist/esm/templates/validateTemplateMetadata.d.ts.map +1 -0
  177. package/dist/esm/templates/validateTemplateMetadata.js +48 -0
  178. package/dist/esm/templates/validateTemplateMetadata.js.map +1 -0
  179. package/dist/esm/tsconfig.tsbuildinfo +1 -1
  180. package/dist/esm/validateNavigationDestinations.d.ts +5 -5
  181. package/dist/esm/validateNavigationDestinations.d.ts.map +1 -1
  182. package/dist/esm/validateNavigationDestinations.js +12 -38
  183. package/dist/esm/validateNavigationDestinations.js.map +1 -1
  184. package/forms/CommandFormLayoutDiagnostic.ts +13 -0
  185. package/forms/CommandFormLayoutDiagnosticCode.ts +16 -0
  186. package/forms/CommandFormLayoutDraft.ts +15 -0
  187. package/forms/commitCommandFormLayoutDraft.ts +12 -0
  188. package/forms/defaultCommandFormLayout.ts +12 -0
  189. package/forms/formWidthToCss.ts +21 -0
  190. package/forms/index.ts +15 -0
  191. package/forms/normalizeCommandFormLayout.ts +39 -0
  192. package/forms/parseFormWidth.ts +20 -0
  193. package/forms/preserveDirtyCommandValues.ts +13 -0
  194. package/forms/resizeCommandFormColumn.ts +23 -0
  195. package/forms/updateCommandFormFieldPlacement.ts +22 -0
  196. package/forms/validateCommandFormLayout.ts +74 -0
  197. package/index.ts +3 -0
  198. package/navigation/NavigationDiagnostic.ts +18 -0
  199. package/navigation/NavigationDiagnosticCode.ts +26 -0
  200. package/navigation/NavigationEntry.ts +16 -0
  201. package/navigation/NavigationGraph.ts +26 -0
  202. package/navigation/OutletOwner.ts +13 -0
  203. package/navigation/collectOutlets.ts +36 -0
  204. package/navigation/diagnoseNavigation.ts +136 -0
  205. package/navigation/findHostingCycles.ts +41 -0
  206. package/navigation/index.ts +10 -0
  207. package/navigation/navigationEntriesFrom.ts +16 -0
  208. package/navigation/navigationRoute.ts +25 -0
  209. package/package.json +3 -3
  210. package/packageVersionRange.ts +18 -0
  211. package/templates/TemplateCatalogEntry.ts +35 -0
  212. package/templates/TemplateCatalogKind.ts +11 -0
  213. package/templates/TemplateSource.ts +15 -0
  214. package/templates/describeTemplateCatalog.ts +92 -0
  215. package/templates/index.ts +9 -0
  216. package/templates/templateMetadataRules.ts +25 -0
  217. package/templates/validateTemplateMetadata.ts +55 -0
  218. package/validateNavigationDestinations.ts +12 -32
@@ -0,0 +1,136 @@
1
+ // Copyright (c) Cratis. All rights reserved.
2
+ // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3
+
4
+ import { DestinationKind, DestinationReference, Screen } from '@cratis/scene.model';
5
+ import { collectOutlets } from './collectOutlets';
6
+ import { findHostingCycles } from './findHostingCycles';
7
+ import { NavigationDiagnostic } from './NavigationDiagnostic';
8
+ import { NavigationDiagnosticCode } from './NavigationDiagnosticCode';
9
+ import { NavigationGraph } from './NavigationGraph';
10
+ import { NavigationRouteKey, authoredRouteKey, navigationTarget } from './navigationRoute';
11
+ import { OutletOwner } from './OutletOwner';
12
+
13
+ /**
14
+ * Diagnoses how destinations, outlets, routes and targets fit together: duplicate routes, duplicate,
15
+ * missing or incompatible outlets, hosting cycles and unavailable targets.
16
+ *
17
+ * Routes are compared by `routeKey` - by default the authored route override, since the engine constructs no
18
+ * URLs. Nothing is rewritten. The C# engine (`NavigationDiagnostics.Diagnose`) reports the same codes and
19
+ * messages in the same order for the same model; both are asserted against `navigation-diagnostic-fixtures.json`.
20
+ */
21
+ export function diagnoseNavigation(graph: NavigationGraph, routeKey: NavigationRouteKey = authoredRouteKey): NavigationDiagnostic[] {
22
+ const diagnostics: NavigationDiagnostic[] = [];
23
+ const outlets = collectOutlets(graph, diagnostics);
24
+ const screens = graph.screens ? new Map(graph.screens.map(screen => [screen.name, screen])) : undefined;
25
+ const routes = new Map<string, { entry: string; target: string }>();
26
+ const hostedIn = new Map<string, string[]>();
27
+
28
+ graph.entries.forEach((navigationEntry, index) => {
29
+ const entry = navigationEntry.id ?? `#${index + 1}`;
30
+ const destination = navigationEntry.destination;
31
+ const kind = destination.kind ?? (destination.dialog ? DestinationKind.Dialog : DestinationKind.Outlet);
32
+ const report = (code: NavigationDiagnosticCode, message: string) => diagnostics.push({ code, message, entry });
33
+
34
+ if (!destination.screen && !destination.module && !destination.route && !destination.dialog) {
35
+ report(NavigationDiagnosticCode.UnavailableTarget, `Destination '${entry}' names no screen, stable identity, route or dialog.`);
36
+ return;
37
+ }
38
+
39
+ diagnoseTarget(graph, screens, destination, kind, entry, report);
40
+ const owner = diagnoseOutlet(graph, outlets, screens, destination, kind, entry, report);
41
+ if (owner?.ownerKind === 'ScreenTemplate' && destination.screen && screens?.has(destination.screen)) {
42
+ const hosts = graph.screens!.filter(screen => screen.screenTemplate === owner.owner).map(screen => screen.name);
43
+ hostedIn.set(destination.screen, [...new Set([...(hostedIn.get(destination.screen) ?? []), ...hosts])]);
44
+ }
45
+
46
+ const route = kind === DestinationKind.External ? undefined : routeKey(destination);
47
+ if (route) {
48
+ const target = navigationTarget(destination, kind);
49
+ const existing = routes.get(route);
50
+ if (!existing) {
51
+ routes.set(route, { entry, target });
52
+ } else if (existing.target !== target) {
53
+ report(NavigationDiagnosticCode.DuplicateRoute, `Route '${destination.route ?? route}' is used by destinations '${existing.entry}' and '${entry}', which open different targets.`);
54
+ }
55
+ }
56
+ });
57
+
58
+ for (const cycle of findHostingCycles(graph.screens?.map(screen => screen.name) ?? [], hostedIn)) {
59
+ diagnostics.push({
60
+ code: NavigationDiagnosticCode.NavigationCycle,
61
+ message: `Navigation cycle ${[...cycle, cycle[0]].join(' -> ')}: each screen is placed in an outlet owned by the next, so composition never terminates.`,
62
+ });
63
+ }
64
+
65
+ return diagnostics;
66
+ }
67
+
68
+ type Report = (code: NavigationDiagnosticCode, message: string) => void;
69
+
70
+ function diagnoseTarget(
71
+ graph: NavigationGraph,
72
+ screens: Map<string, Screen> | undefined,
73
+ destination: DestinationReference,
74
+ kind: DestinationKind,
75
+ entry: string,
76
+ report: Report,
77
+ ) {
78
+ if (kind === DestinationKind.Dialog) {
79
+ if (!destination.dialog) {
80
+ report(NavigationDiagnosticCode.UnavailableTarget, `Dialog destination '${entry}' names no dialog.`);
81
+ } else if (graph.dialogTemplates && !graph.dialogTemplates.some(template => template.name === destination.dialog)) {
82
+ report(NavigationDiagnosticCode.UnavailableTarget, `Destination '${entry}' opens dialog '${destination.dialog}', which no dialog template provides.`);
83
+ }
84
+ return;
85
+ }
86
+
87
+ if (kind === DestinationKind.Outlet && destination.screen && screens && !screens.has(destination.screen)) {
88
+ report(NavigationDiagnosticCode.UnavailableTarget, `Destination '${entry}' opens screen '${destination.screen}', which does not exist.`);
89
+ }
90
+ }
91
+
92
+ function diagnoseOutlet(
93
+ graph: NavigationGraph,
94
+ outlets: Map<string, OutletOwner>,
95
+ screens: Map<string, Screen> | undefined,
96
+ destination: DestinationReference,
97
+ kind: DestinationKind,
98
+ entry: string,
99
+ report: Report,
100
+ ): OutletOwner | undefined {
101
+ if (!destination.outlet) return undefined;
102
+
103
+ if (kind !== DestinationKind.Outlet) {
104
+ const opens = kind === DestinationKind.Dialog ? 'a dialog' : 'an external target';
105
+ report(NavigationDiagnosticCode.IncompatibleOutlet, `Destination '${entry}' opens ${opens} but names outlet '${destination.outlet}'; only outlet destinations render in outlets.`);
106
+ return undefined;
107
+ }
108
+
109
+ const owner = outlets.get(destination.outlet);
110
+ if (!owner) {
111
+ report(NavigationDiagnosticCode.MissingOutlet, `Destination '${entry}' targets missing outlet '${destination.outlet}'.`);
112
+ return undefined;
113
+ }
114
+
115
+ const screen = destination.screen ? screens?.get(destination.screen) : undefined;
116
+ if (!screen) return owner;
117
+
118
+ if (owner.ownerKind === 'Layout' && screen.layout !== owner.owner) {
119
+ report(
120
+ NavigationDiagnosticCode.IncompatibleOutlet,
121
+ `Outlet '${owner.outlet.name}' belongs to layout '${owner.owner}', but destination '${entry}' places '${screen.name}', which renders in layout '${screen.layout}'.`,
122
+ );
123
+ }
124
+
125
+ if (owner.outlet.accepts) {
126
+ const type = graph.screenTemplates?.find(template => template.name === screen.screenTemplate)?.metadata?.type;
127
+ if (!type || !owner.outlet.accepts.includes(type)) {
128
+ report(
129
+ NavigationDiagnosticCode.IncompatibleOutlet,
130
+ `Outlet '${owner.outlet.name}' accepts ${owner.outlet.accepts.join(', ')} screens, but destination '${entry}' places '${screen.name}' of type '${type ?? 'untyped'}'.`,
131
+ );
132
+ }
133
+ }
134
+
135
+ return owner;
136
+ }
@@ -0,0 +1,41 @@
1
+ // Copyright (c) Cratis. All rights reserved.
2
+ // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3
+
4
+ /**
5
+ * Finds cycles in a "renders inside" graph, where an edge from a screen to another means the first is
6
+ * placed in an outlet the second one's template owns. Screens are visited in the given order and edges in
7
+ * insertion order, so the same graph always yields the same cycles in the same order. Each set of screens
8
+ * is reported once, starting from the screen the walk reached it through.
9
+ */
10
+ export function findHostingCycles(screens: string[], edges: Map<string, string[]>): string[][] {
11
+ const state = new Map<string, 'visiting' | 'done'>();
12
+ const stack: string[] = [];
13
+ const reported = new Set<string>();
14
+ const cycles: string[][] = [];
15
+
16
+ const visit = (screen: string) => {
17
+ state.set(screen, 'visiting');
18
+ stack.push(screen);
19
+ for (const host of edges.get(screen) ?? []) {
20
+ const hostState = state.get(host);
21
+ if (hostState === 'visiting') {
22
+ const cycle = stack.slice(stack.indexOf(host));
23
+ const key = [...cycle].sort().join('\u0000');
24
+ if (!reported.has(key)) {
25
+ reported.add(key);
26
+ cycles.push(cycle);
27
+ }
28
+ } else if (hostState === undefined) {
29
+ visit(host);
30
+ }
31
+ }
32
+ stack.pop();
33
+ state.set(screen, 'done');
34
+ };
35
+
36
+ for (const screen of screens) {
37
+ if (!state.has(screen)) visit(screen);
38
+ }
39
+
40
+ return cycles;
41
+ }
@@ -0,0 +1,10 @@
1
+ // Copyright (c) Cratis. All rights reserved.
2
+ // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3
+
4
+ export * from './NavigationDiagnostic';
5
+ export * from './NavigationDiagnosticCode';
6
+ export * from './NavigationEntry';
7
+ export * from './NavigationGraph';
8
+ export * from './diagnoseNavigation';
9
+ export * from './navigationEntriesFrom';
10
+ export * from './navigationRoute';
@@ -0,0 +1,16 @@
1
+ // Copyright (c) Cratis. All rights reserved.
2
+ // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3
+
4
+ import { NavigationItem } from '@cratis/scene.model';
5
+ import { NavigationEntry } from './NavigationEntry';
6
+
7
+ /**
8
+ * Turns navigation contributions into entries for {@link diagnoseNavigation}. A legacy item carrying only
9
+ * `targetScreen` becomes a destination for that screen, so old and new content are checked the same way.
10
+ */
11
+ export function navigationEntriesFrom(items: NavigationItem[]): NavigationEntry[] {
12
+ return items.map(item => ({
13
+ id: item.id,
14
+ destination: item.destination ?? { screen: item.targetScreen, routeParameterBindings: item.routeParameterBindings },
15
+ }));
16
+ }
@@ -0,0 +1,25 @@
1
+ // Copyright (c) Cratis. All rights reserved.
2
+ // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3
+
4
+ import { DestinationReference } from '@cratis/scene.model';
5
+
6
+ /**
7
+ * Decides which route a destination occupies, so destinations occupying the same one can be compared.
8
+ *
9
+ * The engine constructs no URLs (ADR-004), so it only compares what was authored. A renderer that derives
10
+ * routes from stable identities, or treats parameter spellings as equivalent, supplies its own key - the same
11
+ * derivation it uses when it builds the URL - and gets duplicate routes diagnosed by that rule instead.
12
+ */
13
+ export type NavigationRouteKey = (destination: DestinationReference) => string | undefined;
14
+
15
+ /** The default route key: the authored route override, trimmed, or none. */
16
+ export const authoredRouteKey: NavigationRouteKey = destination => destination.route?.trim() || undefined;
17
+
18
+ /**
19
+ * What a destination opens, so two destinations sharing a route are only a conflict when they open
20
+ * different things.
21
+ */
22
+ export function navigationTarget(destination: DestinationReference, kind: string): string {
23
+ const identity = destination.screen ?? [destination.module, destination.feature, destination.slice].filter(part => part).join('.');
24
+ return [kind, identity, destination.dialog ?? '', destination.outlet ?? ''].join('|');
25
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cratis/scene.engine",
3
- "version": "4.11.0",
3
+ "version": "4.13.0",
4
4
  "description": "The Scene runtime: walks a Scene element tree and drives whichever renderer is plugged in. Does not know React, or any other rendering platform, exists.",
5
5
  "keywords": [
6
6
  "cratis",
@@ -48,9 +48,9 @@
48
48
  "up": "yarn g:up"
49
49
  },
50
50
  "dependencies": {
51
- "@cratis/scene.model": "4.11.0"
51
+ "@cratis/scene.model": "4.13.0"
52
52
  },
53
53
  "devDependencies": {
54
- "@cratis/scene.model": "4.11.0"
54
+ "@cratis/scene.model": "4.13.0"
55
55
  }
56
56
  }
@@ -42,6 +42,24 @@ export function isVersionSatisfiedBy(version: string, range: string | undefined)
42
42
  }
43
43
  }
44
44
 
45
+ /**
46
+ * Whether a range is one {@link isVersionSatisfiedBy} understands. An empty range or `*` is valid and means
47
+ * "any version"; anything the parser cannot read is invalid, so declarations can be rejected up front
48
+ * instead of surfacing later as a conflict nobody can satisfy.
49
+ */
50
+ export function isValidVersionRange(range: string | undefined): boolean {
51
+ const trimmed = range?.trim();
52
+ if (!trimmed || trimmed === '*') return true;
53
+ return parseVersion(splitRange(trimmed)[1]) !== undefined;
54
+ }
55
+
56
+ /**
57
+ * Whether a value is a `major.minor.patch` version, optionally with a pre-release or build suffix.
58
+ */
59
+ export function isValidVersion(version: string | undefined): boolean {
60
+ return version !== undefined && version.trim().length > 0 && parseVersion(version) !== undefined;
61
+ }
62
+
45
63
  type Version = [number, number, number];
46
64
 
47
65
  function splitRange(range: string): [string, string] {
@@ -0,0 +1,35 @@
1
+ // Copyright (c) Cratis. All rights reserved.
2
+ // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3
+
4
+ import { TemplateAttribution, TemplateCompatibility, TemplateScope } from '@cratis/scene.model';
5
+ import { TemplateCatalogKind } from './TemplateCatalogKind';
6
+
7
+ /**
8
+ * One template as a template browser reads it: what it is, where it comes from, what it needs and whether the
9
+ * active package set can use it.
10
+ */
11
+ export interface TemplateCatalogEntry {
12
+ /** The package that provides the template. */
13
+ package: string;
14
+
15
+ /** The version of that package. */
16
+ packageVersion: string;
17
+
18
+ kind: TemplateCatalogKind;
19
+ name: string;
20
+ displayName?: string;
21
+ description?: string;
22
+ type?: string;
23
+ category?: string;
24
+ scopes?: TemplateScope[];
25
+ compatibility?: TemplateCompatibility;
26
+ attribution?: TemplateAttribution;
27
+ license?: string;
28
+ licenseUrl?: string;
29
+
30
+ /** Whether the template's metadata is complete and every requirement is met. */
31
+ compatible: boolean;
32
+
33
+ /** Why it is not - missing or invalid metadata, or an unmet Scene or package requirement. */
34
+ problems: string[];
35
+ }
@@ -0,0 +1,11 @@
1
+ // Copyright (c) Cratis. All rights reserved.
2
+ // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3
+
4
+ /**
5
+ * Which kind of template a catalog entry describes.
6
+ */
7
+ export enum TemplateCatalogKind {
8
+ Layout = 'Layout',
9
+ ScreenTemplate = 'ScreenTemplate',
10
+ DialogTemplate = 'DialogTemplate',
11
+ }
@@ -0,0 +1,15 @@
1
+ // Copyright (c) Cratis. All rights reserved.
2
+ // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3
+
4
+ import { DialogTemplate, Layout, ScenePackage, ScreenTemplate } from '@cratis/scene.model';
5
+
6
+ /**
7
+ * A package and the templates it provides. A React `ScenePackageBundle` already has this shape, so hosts pass
8
+ * their approved bundles straight in.
9
+ */
10
+ export interface TemplateSource {
11
+ manifest: ScenePackage;
12
+ layouts?: Layout[];
13
+ screenTemplates?: ScreenTemplate[];
14
+ dialogTemplates?: DialogTemplate[];
15
+ }
@@ -0,0 +1,92 @@
1
+ // Copyright (c) Cratis. All rights reserved.
2
+ // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3
+
4
+ import { ScenePackage, TemplateMetadata } from '@cratis/scene.model';
5
+ import { isVersionSatisfiedBy } from '../packageVersionRange';
6
+ import { TemplateCatalogEntry } from './TemplateCatalogEntry';
7
+ import { TemplateCatalogKind } from './TemplateCatalogKind';
8
+ import { TemplateSource } from './TemplateSource';
9
+ import { validateTemplateMetadata } from './validateTemplateMetadata';
10
+
11
+ /**
12
+ * Describes every template the given packages provide, in package then layout, screen template, dialog
13
+ * template order, with its compatibility, attribution and license metadata and whether it can be used.
14
+ *
15
+ * A template is compatible when its metadata is complete and valid, the Scene version (when given) satisfies
16
+ * its Scene range, and every package it requires is among the given packages at a satisfying version and is
17
+ * either its own package or one that package depends on. Nothing is filtered out: a browser shows incompatible
18
+ * templates with their problems rather than hiding why they are unavailable.
19
+ */
20
+ export function describeTemplateCatalog(sources: TemplateSource[], sceneVersion?: string): TemplateCatalogEntry[] {
21
+ const versions = new Map(sources.map(source => [source.manifest.name, source.manifest.version]));
22
+ const entries: TemplateCatalogEntry[] = [];
23
+
24
+ for (const source of sources) {
25
+ const templates = [
26
+ ...(source.layouts ?? []).map(template => ({ kind: TemplateCatalogKind.Layout, template })),
27
+ ...(source.screenTemplates ?? []).map(template => ({ kind: TemplateCatalogKind.ScreenTemplate, template })),
28
+ ...(source.dialogTemplates ?? []).map(template => ({ kind: TemplateCatalogKind.DialogTemplate, template })),
29
+ ];
30
+
31
+ for (const { kind, template } of templates) {
32
+ const metadata = template.metadata;
33
+ const describable = template as { displayName?: string; description?: string };
34
+ const problems = [
35
+ ...validateTemplateMetadata(template.name, metadata),
36
+ ...requirementProblems(template.name, metadata, source.manifest, versions, sceneVersion),
37
+ ];
38
+
39
+ entries.push({
40
+ package: source.manifest.name,
41
+ packageVersion: source.manifest.version,
42
+ kind,
43
+ name: template.name,
44
+ displayName: describable.displayName,
45
+ description: describable.description,
46
+ type: metadata?.type,
47
+ category: metadata?.category,
48
+ scopes: metadata?.scopes,
49
+ compatibility: metadata?.compatibility,
50
+ attribution: metadata?.attribution,
51
+ license: metadata?.license,
52
+ licenseUrl: metadata?.licenseUrl,
53
+ compatible: problems.length === 0,
54
+ problems,
55
+ });
56
+ }
57
+ }
58
+
59
+ return entries;
60
+ }
61
+
62
+ function requirementProblems(
63
+ name: string,
64
+ metadata: TemplateMetadata | undefined,
65
+ owner: ScenePackage,
66
+ versions: Map<string, string>,
67
+ sceneVersion: string | undefined,
68
+ ): string[] {
69
+ const problems: string[] = [];
70
+ const compatibility = metadata?.compatibility;
71
+ if (!compatibility) return problems;
72
+
73
+ if (sceneVersion !== undefined && compatibility.scene?.trim() && !isVersionSatisfiedBy(sceneVersion, compatibility.scene)) {
74
+ problems.push(`Template '${name}' requires Scene ${compatibility.scene} but the host runs ${sceneVersion}`);
75
+ }
76
+
77
+ for (const requirement of compatibility.packages ?? []) {
78
+ if (!requirement.name?.trim()) continue;
79
+ if (requirement.name !== owner.name && !owner.dependencies.some(dependency => dependency.name === requirement.name)) {
80
+ problems.push(`Template '${name}' requires package '${requirement.name}', which its package '${owner.name}' does not depend on`);
81
+ }
82
+
83
+ const version = versions.get(requirement.name);
84
+ if (version === undefined) {
85
+ problems.push(`Template '${name}' requires package '${requirement.name}', which is not available`);
86
+ } else if (!isVersionSatisfiedBy(version, requirement.versionRange)) {
87
+ problems.push(`Template '${name}' requires package '${requirement.name}' ${requirement.versionRange} but version ${version} is available`);
88
+ }
89
+ }
90
+
91
+ return problems;
92
+ }
@@ -0,0 +1,9 @@
1
+ // Copyright (c) Cratis. All rights reserved.
2
+ // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3
+
4
+ export * from './TemplateCatalogEntry';
5
+ export * from './TemplateCatalogKind';
6
+ export * from './TemplateSource';
7
+ export * from './describeTemplateCatalog';
8
+ export * from './templateMetadataRules';
9
+ export * from './validateTemplateMetadata';
@@ -0,0 +1,25 @@
1
+ // Copyright (c) Cratis. All rights reserved.
2
+ // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3
+
4
+ /**
5
+ * Whether a value is an absolute `https` URL with a host - the only kind of link a template browser
6
+ * should follow for attribution or license terms. Kept to plain string rules so the C# twin matches exactly.
7
+ */
8
+ export function isAbsoluteHttpsUrl(value: string): boolean {
9
+ const prefix = 'https://';
10
+ return value.startsWith(prefix) && value.length > prefix.length && value[prefix.length] !== '/' && !/\s/.test(value);
11
+ }
12
+
13
+ /**
14
+ * Whether a value is a simple SPDX license expression: identifiers such as `MIT`, `Apache-2.0` or
15
+ * `LicenseRef-Acme`, optionally joined by `AND`, `OR` or `WITH`. Parentheses are not supported.
16
+ */
17
+ export function isSpdxExpression(value: string): boolean {
18
+ const tokens = value.split(' ');
19
+ if (tokens.length % 2 === 0) return false;
20
+ return tokens.every((token, index) => (index % 2 === 1 ? ['AND', 'OR', 'WITH'].includes(token) : isSpdxIdentifier(token)));
21
+ }
22
+
23
+ function isSpdxIdentifier(token: string): boolean {
24
+ return token.length > 0 && [...token].every(character => /[A-Za-z0-9.+-]/.test(character));
25
+ }
@@ -0,0 +1,55 @@
1
+ // Copyright (c) Cratis. All rights reserved.
2
+ // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3
+
4
+ import { TemplateMetadata } from '@cratis/scene.model';
5
+ import { isValidVersionRange } from '../packageVersionRange';
6
+ import { isAbsoluteHttpsUrl, isSpdxExpression } from './templateMetadataRules';
7
+
8
+ /**
9
+ * Checks that a template carries the compatibility, attribution and license metadata every shipped template
10
+ * needs, and that each value is well formed. The C# engine (`TemplateMetadataValidation.Validate`) reports the
11
+ * same problems in the same order; both are asserted against `template-metadata-fixtures.json`.
12
+ */
13
+ export function validateTemplateMetadata(name: string, metadata: TemplateMetadata | undefined): string[] {
14
+ const problems: string[] = [];
15
+ const compatibility = metadata?.compatibility;
16
+
17
+ if (!compatibility) {
18
+ problems.push(`Template '${name}' declares no compatibility`);
19
+ } else {
20
+ if (!compatibility.scene?.trim()) {
21
+ problems.push(`Template '${name}' declares no Scene version range`);
22
+ } else if (!isValidVersionRange(compatibility.scene)) {
23
+ problems.push(`Template '${name}' declares an invalid Scene version range '${compatibility.scene}'`);
24
+ }
25
+
26
+ for (const requirement of compatibility.packages ?? []) {
27
+ if (!requirement.name?.trim()) {
28
+ problems.push(`Template '${name}' declares a package requirement without a name`);
29
+ } else if (!isValidVersionRange(requirement.versionRange)) {
30
+ problems.push(`Template '${name}' declares an invalid version range '${requirement.versionRange}' for package '${requirement.name}'`);
31
+ }
32
+ }
33
+ }
34
+
35
+ const attribution = metadata?.attribution;
36
+ if (!attribution?.author?.trim()) {
37
+ problems.push(`Template '${name}' declares no author`);
38
+ }
39
+
40
+ if (attribution?.url !== undefined && !isAbsoluteHttpsUrl(attribution.url)) {
41
+ problems.push(`Template '${name}' declares an attribution URL '${attribution.url}' that is not an absolute https URL`);
42
+ }
43
+
44
+ if (!metadata?.license?.trim()) {
45
+ problems.push(`Template '${name}' declares no license`);
46
+ } else if (!isSpdxExpression(metadata.license)) {
47
+ problems.push(`Template '${name}' declares '${metadata.license}', which is not an SPDX license expression`);
48
+ }
49
+
50
+ if (metadata?.licenseUrl !== undefined && !isAbsoluteHttpsUrl(metadata.licenseUrl)) {
51
+ problems.push(`Template '${name}' declares a license URL '${metadata.licenseUrl}' that is not an absolute https URL`);
52
+ }
53
+
54
+ return problems;
55
+ }
@@ -1,43 +1,23 @@
1
1
  // Copyright (c) Cratis. All rights reserved.
2
2
  // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3
3
 
4
- import { DestinationReference, Layout, ScreenTemplate } from '@cratis/scene.model';
4
+ import { DestinationReference, DialogTemplate, Layout, Screen, ScreenTemplate } from '@cratis/scene.model';
5
+ import { NavigationDiagnostic, diagnoseNavigation } from './navigation';
5
6
 
6
- export interface NavigationDestinationDiagnostic {
7
- code: string;
8
- message: string;
9
- }
7
+ /**
8
+ * A navigation problem as {@link validateNavigationDestinations} reports it.
9
+ */
10
+ export type NavigationDestinationDiagnostic = NavigationDiagnostic;
10
11
 
11
12
  /**
12
- * Validates destinations against the outlet/route declarations owned by layouts and templates.
13
+ * Validates destinations against the outlet/route declarations owned by layouts and templates, and - when
14
+ * the screens and dialog templates are given - against the targets that actually exist.
15
+ *
16
+ * A shorthand for {@link diagnoseNavigation} over destinations without entry identities.
13
17
  */
14
18
  export function validateNavigationDestinations(
15
19
  destinations: DestinationReference[],
16
- surfaces: { layouts?: Layout[]; screenTemplates?: ScreenTemplate[] } = {},
20
+ surfaces: { layouts?: Layout[]; screenTemplates?: ScreenTemplate[]; screens?: Screen[]; dialogTemplates?: DialogTemplate[] } = {},
17
21
  ): NavigationDestinationDiagnostic[] {
18
- const diagnostics: NavigationDestinationDiagnostic[] = [];
19
- const outlets = new Set<string>();
20
- const routes = new Set<string>();
21
-
22
- for (const surface of [...(surfaces.layouts ?? []), ...(surfaces.screenTemplates ?? [])]) {
23
- for (const outlet of surface.outlets ?? []) {
24
- if (outlets.has(outlet.name)) diagnostics.push({ code: 'duplicateOutlet', message: `Outlet '${outlet.name}' is declared more than once.` });
25
- outlets.add(outlet.name);
26
- }
27
- }
28
-
29
- for (const destination of destinations) {
30
- if (destination.outlet && !outlets.has(destination.outlet)) {
31
- diagnostics.push({ code: 'missingOutlet', message: `Destination targets missing outlet '${destination.outlet}'.` });
32
- }
33
- if (destination.route) {
34
- if (routes.has(destination.route)) diagnostics.push({ code: 'duplicateRoute', message: `Route '${destination.route}' is used by more than one destination.` });
35
- routes.add(destination.route);
36
- }
37
- if (!destination.screen && !destination.module && !destination.route && !destination.dialog) {
38
- diagnostics.push({ code: 'unavailableTarget', message: 'Destination must name a screen, stable identity, route or dialog.' });
39
- }
40
- }
41
-
42
- return diagnostics;
22
+ return diagnoseNavigation({ entries: destinations.map(destination => ({ destination })), ...surfaces });
43
23
  }