@cratis/scene.engine 4.27.0 → 4.28.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 (78) hide show
  1. package/dist/cjs/index.js +15 -0
  2. package/dist/cjs/index.js.map +1 -1
  3. package/dist/cjs/structure/ApplicationStructureIndex.js +10 -0
  4. package/dist/cjs/structure/ApplicationStructureIndex.js.map +1 -1
  5. package/dist/cjs/toolbar/SliceDialogSource.js +14 -0
  6. package/dist/cjs/toolbar/SliceDialogSource.js.map +1 -0
  7. package/dist/cjs/toolbar/ToolbarDiagnosticCode.js +16 -0
  8. package/dist/cjs/toolbar/ToolbarDiagnosticCode.js.map +1 -0
  9. package/dist/cjs/toolbar/ToolbarDiagnosticSeverity.js +14 -0
  10. package/dist/cjs/toolbar/ToolbarDiagnosticSeverity.js.map +1 -0
  11. package/dist/cjs/toolbar/sliceDialogFor.js +38 -0
  12. package/dist/cjs/toolbar/sliceDialogFor.js.map +1 -0
  13. package/dist/cjs/toolbar/toolbarButtons.js +93 -0
  14. package/dist/cjs/toolbar/toolbarButtons.js.map +1 -0
  15. package/dist/cjs/toolbar/validateToolbarButtons.js +59 -0
  16. package/dist/cjs/toolbar/validateToolbarButtons.js.map +1 -0
  17. package/dist/esm/index.d.ts +1 -0
  18. package/dist/esm/index.d.ts.map +1 -1
  19. package/dist/esm/index.js +6 -0
  20. package/dist/esm/index.js.map +1 -1
  21. package/dist/esm/structure/ApplicationStructureIndex.d.ts +1 -0
  22. package/dist/esm/structure/ApplicationStructureIndex.d.ts.map +1 -1
  23. package/dist/esm/structure/ApplicationStructureIndex.js +10 -0
  24. package/dist/esm/structure/ApplicationStructureIndex.js.map +1 -1
  25. package/dist/esm/toolbar/ResolvedToolbarButton.d.ts +13 -0
  26. package/dist/esm/toolbar/ResolvedToolbarButton.d.ts.map +1 -0
  27. package/dist/esm/toolbar/ResolvedToolbarButton.js +2 -0
  28. package/dist/esm/toolbar/ResolvedToolbarButton.js.map +1 -0
  29. package/dist/esm/toolbar/SliceDialog.d.ts +8 -0
  30. package/dist/esm/toolbar/SliceDialog.d.ts.map +1 -0
  31. package/dist/esm/toolbar/SliceDialog.js +2 -0
  32. package/dist/esm/toolbar/SliceDialog.js.map +1 -0
  33. package/dist/esm/toolbar/SliceDialogSource.d.ts +5 -0
  34. package/dist/esm/toolbar/SliceDialogSource.d.ts.map +1 -0
  35. package/dist/esm/toolbar/SliceDialogSource.js +12 -0
  36. package/dist/esm/toolbar/SliceDialogSource.js.map +1 -0
  37. package/dist/esm/toolbar/ToolbarDiagnostic.d.ts +9 -0
  38. package/dist/esm/toolbar/ToolbarDiagnostic.d.ts.map +1 -0
  39. package/dist/esm/toolbar/ToolbarDiagnostic.js +2 -0
  40. package/dist/esm/toolbar/ToolbarDiagnostic.js.map +1 -0
  41. package/dist/esm/toolbar/ToolbarDiagnosticCode.d.ts +6 -0
  42. package/dist/esm/toolbar/ToolbarDiagnosticCode.d.ts.map +1 -0
  43. package/dist/esm/toolbar/ToolbarDiagnosticCode.js +14 -0
  44. package/dist/esm/toolbar/ToolbarDiagnosticCode.js.map +1 -0
  45. package/dist/esm/toolbar/ToolbarDiagnosticSeverity.d.ts +5 -0
  46. package/dist/esm/toolbar/ToolbarDiagnosticSeverity.d.ts.map +1 -0
  47. package/dist/esm/toolbar/ToolbarDiagnosticSeverity.js +12 -0
  48. package/dist/esm/toolbar/ToolbarDiagnosticSeverity.js.map +1 -0
  49. package/dist/esm/toolbar/index.d.ts +10 -0
  50. package/dist/esm/toolbar/index.d.ts.map +1 -0
  51. package/dist/esm/toolbar/index.js +10 -0
  52. package/dist/esm/toolbar/index.js.map +1 -0
  53. package/dist/esm/toolbar/sliceDialogFor.d.ts +5 -0
  54. package/dist/esm/toolbar/sliceDialogFor.d.ts.map +1 -0
  55. package/dist/esm/toolbar/sliceDialogFor.js +35 -0
  56. package/dist/esm/toolbar/sliceDialogFor.js.map +1 -0
  57. package/dist/esm/toolbar/toolbarButtons.d.ts +10 -0
  58. package/dist/esm/toolbar/toolbarButtons.d.ts.map +1 -0
  59. package/dist/esm/toolbar/toolbarButtons.js +89 -0
  60. package/dist/esm/toolbar/toolbarButtons.js.map +1 -0
  61. package/dist/esm/toolbar/validateToolbarButtons.d.ts +5 -0
  62. package/dist/esm/toolbar/validateToolbarButtons.d.ts.map +1 -0
  63. package/dist/esm/toolbar/validateToolbarButtons.js +57 -0
  64. package/dist/esm/toolbar/validateToolbarButtons.js.map +1 -0
  65. package/dist/esm/tsconfig.tsbuildinfo +1 -1
  66. package/index.ts +1 -0
  67. package/package.json +3 -3
  68. package/structure/ApplicationStructureIndex.ts +11 -0
  69. package/toolbar/ResolvedToolbarButton.ts +34 -0
  70. package/toolbar/SliceDialog.ts +21 -0
  71. package/toolbar/SliceDialogSource.ts +13 -0
  72. package/toolbar/ToolbarDiagnostic.ts +22 -0
  73. package/toolbar/ToolbarDiagnosticCode.ts +17 -0
  74. package/toolbar/ToolbarDiagnosticSeverity.ts +13 -0
  75. package/toolbar/index.ts +12 -0
  76. package/toolbar/sliceDialogFor.ts +31 -0
  77. package/toolbar/toolbarButtons.ts +83 -0
  78. package/toolbar/validateToolbarButtons.ts +61 -0
package/index.ts CHANGED
@@ -45,3 +45,4 @@ export * from './validateNavigationDestinations';
45
45
  export * from './editing';
46
46
  export * from './sampleData';
47
47
  export * from './structure';
48
+ export * from './toolbar';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cratis/scene.engine",
3
- "version": "4.27.0",
3
+ "version": "4.28.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.27.0"
51
+ "@cratis/scene.model": "4.28.0"
52
52
  },
53
53
  "devDependencies": {
54
- "@cratis/scene.model": "4.27.0"
54
+ "@cratis/scene.model": "4.28.0"
55
55
  }
56
56
  }
@@ -104,6 +104,17 @@ export class ApplicationStructureIndex {
104
104
  return [...(this.featuresByName.get(name.toLowerCase()) ?? [])];
105
105
  }
106
106
 
107
+ /**
108
+ * Finds the state change slices that accept a command, so a command named by hand can be turned into the slice that owns it.
109
+ * @param command A command name - the slice's `command`, or its `id` when it names none - or a slice's qualified id.
110
+ * @returns The qualified ids of the matching state change slices, in declaration order.
111
+ */
112
+ findSlicesByCommand(command: string): string[] {
113
+ return [...this.slices.entries()]
114
+ .filter(([qualified, slice]) => slice.kind === SliceKind.StateChange && (qualified === command || (slice.command ?? slice.id) === command))
115
+ .map(([qualified]) => qualified);
116
+ }
117
+
107
118
  private indexContainer(path: string, features: FeatureNode[], slices: SliceNode[]): void {
108
119
  const seen = new Map<string, string>();
109
120
  const featureNames = new Map<string, string>();
@@ -0,0 +1,34 @@
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 { ToolbarEmphasis } from '@cratis/scene.model';
5
+ import { SliceDialog } from './SliceDialog';
6
+
7
+ /**
8
+ * A toolbar button resolved against the application structure: what a renderer needs to draw it and to open its dialog.
9
+ */
10
+ export interface ResolvedToolbarButton {
11
+ /** The button identity. */
12
+ id: string;
13
+
14
+ /** The text shown on the button. */
15
+ title: string;
16
+
17
+ /** How prominent the button is. */
18
+ emphasis: ToolbarEmphasis;
19
+
20
+ /** Qualified icon reference, when the button has one. */
21
+ icon?: string;
22
+
23
+ /** The qualified id of the slice the button invokes, when it names or resolves to one. */
24
+ slice?: string;
25
+
26
+ /** Whether the slice was found from a hand-authored command rather than named directly. */
27
+ converted: boolean;
28
+
29
+ /** The command the slice accepts; absent unless the slice is a known state change. */
30
+ command?: string;
31
+
32
+ /** The task dialog the button opens; absent unless the slice is a known state change. */
33
+ dialog?: SliceDialog;
34
+ }
@@ -0,0 +1,21 @@
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 { SliceDialogSource } from './SliceDialogSource';
5
+
6
+ /**
7
+ * The task dialog a state change slice opens: its command and the dialog template that shows the command's fields as a form.
8
+ */
9
+ export interface SliceDialog {
10
+ /** The qualified id of the slice. */
11
+ slice: string;
12
+
13
+ /** The command the dialog submits. */
14
+ command: string;
15
+
16
+ /** The dialog template. */
17
+ template: string;
18
+
19
+ /** Whether the slice named the template or the default was used. */
20
+ source: SliceDialogSource;
21
+ }
@@ -0,0 +1,13 @@
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
+ * Where a slice's dialog template comes from.
6
+ */
7
+ export enum SliceDialogSource {
8
+ /** The slice names its own dialog template. */
9
+ Slice = 'slice',
10
+
11
+ /** The slice names none, so the default form dialog is used. */
12
+ Default = 'default',
13
+ }
@@ -0,0 +1,22 @@
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 { ToolbarDiagnosticCode } from './ToolbarDiagnosticCode';
5
+ import { ToolbarDiagnosticSeverity } from './ToolbarDiagnosticSeverity';
6
+
7
+ /**
8
+ * One problem with a toolbar button.
9
+ */
10
+ export interface ToolbarDiagnostic {
11
+ /** What kind of problem this is. */
12
+ code: ToolbarDiagnosticCode;
13
+
14
+ /** Whether the button cannot work or is only incomplete. */
15
+ severity: ToolbarDiagnosticSeverity;
16
+
17
+ /** A sentence naming the button and the slice. */
18
+ message: string;
19
+
20
+ /** The id of the button the problem was found on. */
21
+ button: string;
22
+ }
@@ -0,0 +1,17 @@
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
+ * What is wrong with a toolbar button. The values are shared with the C# engine so both report the same code for
6
+ * the same toolbar.
7
+ */
8
+ export enum ToolbarDiagnosticCode {
9
+ /** A button invokes a slice the application structure does not have. */
10
+ ToolbarButtonSliceNotFound = 'toolbarButtonSliceNotFound',
11
+
12
+ /** A button invokes a slice that is not a state change; state views and automations never take user input. */
13
+ ToolbarButtonSliceNotStateChange = 'toolbarButtonSliceNotStateChange',
14
+
15
+ /** A button has no slice yet. It is allowed while designing, and flagged. */
16
+ ToolbarButtonSliceEmpty = 'toolbarButtonSliceEmpty',
17
+ }
@@ -0,0 +1,13 @@
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
+ * How serious a toolbar diagnostic is.
6
+ */
7
+ export enum ToolbarDiagnosticSeverity {
8
+ /** The button cannot work. */
9
+ Error = 'error',
10
+
11
+ /** The button is allowed but incomplete. */
12
+ Warning = 'warning',
13
+ }
@@ -0,0 +1,12 @@
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 './ToolbarDiagnosticCode';
5
+ export * from './ToolbarDiagnosticSeverity';
6
+ export * from './ToolbarDiagnostic';
7
+ export * from './SliceDialogSource';
8
+ export * from './SliceDialog';
9
+ export * from './ResolvedToolbarButton';
10
+ export * from './sliceDialogFor';
11
+ export * from './toolbarButtons';
12
+ export * from './validateToolbarButtons';
@@ -0,0 +1,31 @@
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 { SliceKind } from '@cratis/scene.model';
5
+ import { ApplicationStructureIndex } from '../structure';
6
+ import { SliceDialog } from './SliceDialog';
7
+ import { SliceDialogSource } from './SliceDialogSource';
8
+
9
+ /** The dialog template used when a slice names none. */
10
+ export const defaultSliceDialogTemplate = 'FormDialog';
11
+
12
+ /**
13
+ * Works out the task dialog a state change slice opens, once, so no host decides it again (TB4): the slice's own
14
+ * dialog template when it names one, else the default form dialog.
15
+ *
16
+ * The C# engine (`SliceDialogs.For`) resolves the same dialog; both are asserted against `toolbar-slice-fixtures.json`.
17
+ *
18
+ * @param index The modules, features and slices of the application.
19
+ * @param sliceId The slice's qualified id.
20
+ * @param defaultTemplate The template used when the slice names none; {@link defaultSliceDialogTemplate} when absent.
21
+ * @returns The dialog, or undefined when there is no such slice or it is not a state change.
22
+ */
23
+ export function sliceDialogFor(index: ApplicationStructureIndex, sliceId: string, defaultTemplate?: string): SliceDialog | undefined {
24
+ const slice = index.findSlice(sliceId);
25
+ if (slice?.kind !== SliceKind.StateChange) return undefined;
26
+
27
+ const command = slice.command ?? slice.id;
28
+ return slice.dialogTemplate
29
+ ? { slice: sliceId, command, template: slice.dialogTemplate, source: SliceDialogSource.Slice }
30
+ : { slice: sliceId, command, template: defaultTemplate ?? defaultSliceDialogTemplate, source: SliceDialogSource.Default };
31
+ }
@@ -0,0 +1,83 @@
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 { ToolbarButton, ToolbarEmphasis } from '@cratis/scene.model';
5
+ import { ApplicationStructureIndex } from '../structure';
6
+ import { ResolvedToolbarButton } from './ResolvedToolbarButton';
7
+ import { sliceDialogFor } from './sliceDialogFor';
8
+
9
+ function text(properties: Record<string, unknown>, name: string): string | undefined {
10
+ const value = properties[name];
11
+ return typeof value === 'string' && value.length > 0 ? value : undefined;
12
+ }
13
+
14
+ /**
15
+ * Reads a button from the properties of a `core:toolbarItem` element: `label` (or `name`), `kind`, `slice`,
16
+ * `emphasis`, `icon` and `target`.
17
+ *
18
+ * The C# engine (`ToolbarButtons.FromItem`) reads the same; both are asserted against `toolbar-slice-fixtures.json`.
19
+ *
20
+ * @param id The element id.
21
+ * @param properties The element properties.
22
+ * @returns The button, or undefined when the item does not invoke a command or slice - a navigate or dialog item.
23
+ */
24
+ export function toolbarButtonFromItem(id: string, properties: Record<string, unknown>): ToolbarButton | undefined {
25
+ const slice = text(properties, 'slice');
26
+ const action = text(properties, 'kind')?.toLowerCase() === 'action';
27
+ if (slice === undefined && !action) return undefined;
28
+
29
+ const button: ToolbarButton = { id, title: text(properties, 'label') ?? text(properties, 'name') ?? id };
30
+ if (slice !== undefined) button.slice = slice;
31
+
32
+ const emphasis = text(properties, 'emphasis')?.toLowerCase();
33
+ if (emphasis === ToolbarEmphasis.Primary || emphasis === ToolbarEmphasis.Secondary || emphasis === ToolbarEmphasis.Quiet) button.emphasis = emphasis;
34
+
35
+ const icon = text(properties, 'icon');
36
+ if (icon !== undefined) button.icon = icon;
37
+
38
+ const target = text(properties, 'target');
39
+ if (target !== undefined) button.target = target;
40
+ return button;
41
+ }
42
+
43
+ /**
44
+ * Converts a hand-authored button to a slice-backed one: a button with no slice whose command is accepted by exactly
45
+ * one state change slice names that slice instead.
46
+ * @param index The modules, features and slices of the application.
47
+ * @param button The button.
48
+ * @returns The button to use, and whether it was converted. A button that cannot be converted is returned as it is.
49
+ */
50
+ export function normalizeToolbarButton(index: ApplicationStructureIndex, button: ToolbarButton): { button: ToolbarButton; converted: boolean } {
51
+ if (button.slice || !button.target) return { button, converted: false };
52
+
53
+ const matches = index.findSlicesByCommand(button.target);
54
+ if (matches.length !== 1) return { button, converted: false };
55
+
56
+ const { target, ...rest } = button;
57
+ void target;
58
+ return { button: { ...rest, slice: matches[0] }, converted: true };
59
+ }
60
+
61
+ /**
62
+ * Resolves a button: its slice, command and task dialog, without any rendering. This is what a renderer, Stage and the
63
+ * Studio preview call.
64
+ *
65
+ * The C# engine (`ToolbarButtons.Resolve`) resolves the same; both are asserted against `toolbar-slice-fixtures.json`.
66
+ *
67
+ * @param index The modules, features and slices of the application.
68
+ * @param button The button.
69
+ * @param defaultDialogTemplate The dialog template used when a slice names none.
70
+ */
71
+ export function resolveToolbarButton(index: ApplicationStructureIndex, button: ToolbarButton, defaultDialogTemplate?: string): ResolvedToolbarButton {
72
+ const { button: normalized, converted } = normalizeToolbarButton(index, button);
73
+ const dialog = normalized.slice ? sliceDialogFor(index, normalized.slice, defaultDialogTemplate) : undefined;
74
+ const resolved: ResolvedToolbarButton = { id: normalized.id, title: normalized.title, emphasis: normalized.emphasis ?? ToolbarEmphasis.Secondary, converted };
75
+ if (normalized.icon !== undefined) resolved.icon = normalized.icon;
76
+ if (normalized.slice) resolved.slice = normalized.slice;
77
+ if (dialog) {
78
+ resolved.command = dialog.command;
79
+ resolved.dialog = dialog;
80
+ }
81
+
82
+ return resolved;
83
+ }
@@ -0,0 +1,61 @@
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 { ApplicationStructure, SliceKind, Toolbar } from '@cratis/scene.model';
5
+ import { ApplicationStructureIndex } from '../structure';
6
+ import { ToolbarDiagnostic } from './ToolbarDiagnostic';
7
+ import { ToolbarDiagnosticCode } from './ToolbarDiagnosticCode';
8
+ import { ToolbarDiagnosticSeverity } from './ToolbarDiagnosticSeverity';
9
+ import { normalizeToolbarButton } from './toolbarButtons';
10
+
11
+ /**
12
+ * Validates the buttons of a toolbar against the application structure: a button must invoke a state change slice
13
+ * (TB3), and a button without a slice is allowed but flagged (TB5). A hand-authored button that names a command is
14
+ * converted first, and a button whose command no single slice accepts is a legacy button that still validates.
15
+ *
16
+ * The C# engine (`ToolbarButtonValidation.Validate`) reports the same codes, severities and messages in the same
17
+ * order; both are asserted against `toolbar-slice-fixtures.json`.
18
+ *
19
+ * @param structure The modules, features and slices of the application.
20
+ * @param toolbar The toolbar.
21
+ * @returns The problems found, in button order.
22
+ */
23
+ export function validateToolbarButtons(structure: ApplicationStructure | ApplicationStructureIndex, toolbar: Toolbar): ToolbarDiagnostic[] {
24
+ const index = structure instanceof ApplicationStructureIndex ? structure : new ApplicationStructureIndex(structure);
25
+ const diagnostics: ToolbarDiagnostic[] = [];
26
+
27
+ for (const authored of toolbar.buttons) {
28
+ const { button } = normalizeToolbarButton(index, authored);
29
+ if (!button.slice) {
30
+ if (!button.target) {
31
+ diagnostics.push({
32
+ code: ToolbarDiagnosticCode.ToolbarButtonSliceEmpty,
33
+ severity: ToolbarDiagnosticSeverity.Warning,
34
+ message: `Toolbar button '${button.id}' has no slice to invoke.`,
35
+ button: button.id,
36
+ });
37
+ }
38
+
39
+ continue;
40
+ }
41
+
42
+ const slice = index.findSlice(button.slice);
43
+ if (!slice) {
44
+ diagnostics.push({
45
+ code: ToolbarDiagnosticCode.ToolbarButtonSliceNotFound,
46
+ severity: ToolbarDiagnosticSeverity.Error,
47
+ message: `Toolbar button '${button.id}' invokes slice '${button.slice}', which does not exist.`,
48
+ button: button.id,
49
+ });
50
+ } else if (slice.kind !== SliceKind.StateChange) {
51
+ diagnostics.push({
52
+ code: ToolbarDiagnosticCode.ToolbarButtonSliceNotStateChange,
53
+ severity: ToolbarDiagnosticSeverity.Error,
54
+ message: `Toolbar button '${button.id}' invokes slice '${button.slice}', which is ${slice.kind === SliceKind.StateView ? 'a state view' : 'an automation'}; only a state change can be invoked from a toolbar.`,
55
+ button: button.id,
56
+ });
57
+ }
58
+ }
59
+
60
+ return diagnostics;
61
+ }