@cratis/scene.blueprint.default 4.33.0 → 4.35.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 (134) hide show
  1. package/dist/cjs/index.js +53 -4
  2. package/dist/cjs/index.js.map +1 -1
  3. package/dist/cjs/preview-host/ApplicationDocument.js +17 -0
  4. package/dist/cjs/preview-host/ApplicationDocument.js.map +1 -0
  5. package/dist/cjs/preview-host/ApplicationHost.js +134 -0
  6. package/dist/cjs/preview-host/ApplicationHost.js.map +1 -0
  7. package/dist/cjs/preview-host/FlowArrangementView.js +75 -0
  8. package/dist/cjs/preview-host/FlowArrangementView.js.map +1 -0
  9. package/dist/cjs/preview-host/PreviewProblem.js +32 -0
  10. package/dist/cjs/preview-host/PreviewProblem.js.map +1 -0
  11. package/dist/cjs/preview-host/PreviewProblemsView.js +51 -0
  12. package/dist/cjs/preview-host/PreviewProblemsView.js.map +1 -0
  13. package/dist/cjs/preview-host/PreviewSliceFragment.js +55 -0
  14. package/dist/cjs/preview-host/PreviewSliceFragment.js.map +1 -0
  15. package/dist/cjs/preview-host/PreviewTarget.js +17 -0
  16. package/dist/cjs/preview-host/PreviewTarget.js.map +1 -0
  17. package/dist/cjs/preview-host/PreviewTemplate.js +49 -0
  18. package/dist/cjs/preview-host/PreviewTemplate.js.map +1 -0
  19. package/dist/cjs/preview-host/PreviewToast.js +45 -0
  20. package/dist/cjs/preview-host/PreviewToast.js.map +1 -0
  21. package/dist/cjs/preview-host/ScenePreviewHost.js +290 -0
  22. package/dist/cjs/preview-host/ScenePreviewHost.js.map +1 -0
  23. package/dist/cjs/preview-host/composeApplication.js +249 -0
  24. package/dist/cjs/preview-host/composeApplication.js.map +1 -0
  25. package/dist/cjs/preview-host/defaultApplicationChrome.js +43 -0
  26. package/dist/cjs/preview-host/defaultApplicationChrome.js.map +1 -0
  27. package/dist/cjs/preview-host/diagnosePreview.js +100 -0
  28. package/dist/cjs/preview-host/diagnosePreview.js.map +1 -0
  29. package/dist/cjs/preview-host/previewProfile.js +53 -0
  30. package/dist/cjs/preview-host/previewProfile.js.map +1 -0
  31. package/dist/cjs/preview-host/previewSampleData.js +116 -0
  32. package/dist/cjs/preview-host/previewSampleData.js.map +1 -0
  33. package/dist/cjs/preview-host/resolvePreviewPlan.js +126 -0
  34. package/dist/cjs/preview-host/resolvePreviewPlan.js.map +1 -0
  35. package/dist/cjs/preview-host/useSizeClass.js +23 -0
  36. package/dist/cjs/preview-host/useSizeClass.js.map +1 -0
  37. package/dist/esm/index.d.ts +1 -0
  38. package/dist/esm/index.d.ts.map +1 -1
  39. package/dist/esm/index.js +17 -0
  40. package/dist/esm/index.js.map +1 -1
  41. package/dist/esm/preview-host/ApplicationDocument.d.ts +7 -0
  42. package/dist/esm/preview-host/ApplicationDocument.d.ts.map +1 -0
  43. package/dist/esm/preview-host/ApplicationDocument.js +15 -0
  44. package/dist/esm/preview-host/ApplicationDocument.js.map +1 -0
  45. package/dist/esm/preview-host/ApplicationHost.d.ts +24 -0
  46. package/dist/esm/preview-host/ApplicationHost.d.ts.map +1 -0
  47. package/dist/esm/preview-host/ApplicationHost.js +132 -0
  48. package/dist/esm/preview-host/ApplicationHost.js.map +1 -0
  49. package/dist/esm/preview-host/FlowArrangementView.d.ts +7 -0
  50. package/dist/esm/preview-host/FlowArrangementView.d.ts.map +1 -0
  51. package/dist/esm/preview-host/FlowArrangementView.js +73 -0
  52. package/dist/esm/preview-host/FlowArrangementView.js.map +1 -0
  53. package/dist/esm/preview-host/PreviewProblem.d.ts +26 -0
  54. package/dist/esm/preview-host/PreviewProblem.d.ts.map +1 -0
  55. package/dist/esm/preview-host/PreviewProblem.js +28 -0
  56. package/dist/esm/preview-host/PreviewProblem.js.map +1 -0
  57. package/dist/esm/preview-host/PreviewProblemsView.d.ts +7 -0
  58. package/dist/esm/preview-host/PreviewProblemsView.d.ts.map +1 -0
  59. package/dist/esm/preview-host/PreviewProblemsView.js +49 -0
  60. package/dist/esm/preview-host/PreviewProblemsView.js.map +1 -0
  61. package/dist/esm/preview-host/PreviewSliceFragment.d.ts +3 -0
  62. package/dist/esm/preview-host/PreviewSliceFragment.d.ts.map +1 -0
  63. package/dist/esm/preview-host/PreviewSliceFragment.js +53 -0
  64. package/dist/esm/preview-host/PreviewSliceFragment.js.map +1 -0
  65. package/dist/esm/preview-host/PreviewTarget.d.ts +10 -0
  66. package/dist/esm/preview-host/PreviewTarget.d.ts.map +1 -0
  67. package/dist/esm/preview-host/PreviewTarget.js +15 -0
  68. package/dist/esm/preview-host/PreviewTarget.js.map +1 -0
  69. package/dist/esm/preview-host/PreviewTemplate.d.ts +5 -0
  70. package/dist/esm/preview-host/PreviewTemplate.d.ts.map +1 -0
  71. package/dist/esm/preview-host/PreviewTemplate.js +45 -0
  72. package/dist/esm/preview-host/PreviewTemplate.js.map +1 -0
  73. package/dist/esm/preview-host/PreviewToast.d.ts +10 -0
  74. package/dist/esm/preview-host/PreviewToast.d.ts.map +1 -0
  75. package/dist/esm/preview-host/PreviewToast.js +43 -0
  76. package/dist/esm/preview-host/PreviewToast.js.map +1 -0
  77. package/dist/esm/preview-host/ScenePreviewHost.d.ts +32 -0
  78. package/dist/esm/preview-host/ScenePreviewHost.d.ts.map +1 -0
  79. package/dist/esm/preview-host/ScenePreviewHost.js +288 -0
  80. package/dist/esm/preview-host/ScenePreviewHost.js.map +1 -0
  81. package/dist/esm/preview-host/composeApplication.d.ts +34 -0
  82. package/dist/esm/preview-host/composeApplication.d.ts.map +1 -0
  83. package/dist/esm/preview-host/composeApplication.js +239 -0
  84. package/dist/esm/preview-host/composeApplication.js.map +1 -0
  85. package/dist/esm/preview-host/defaultApplicationChrome.d.ts +4 -0
  86. package/dist/esm/preview-host/defaultApplicationChrome.d.ts.map +1 -0
  87. package/dist/esm/preview-host/defaultApplicationChrome.js +41 -0
  88. package/dist/esm/preview-host/defaultApplicationChrome.js.map +1 -0
  89. package/dist/esm/preview-host/diagnosePreview.d.ts +22 -0
  90. package/dist/esm/preview-host/diagnosePreview.d.ts.map +1 -0
  91. package/dist/esm/preview-host/diagnosePreview.js +98 -0
  92. package/dist/esm/preview-host/diagnosePreview.js.map +1 -0
  93. package/dist/esm/preview-host/index.d.ts +18 -0
  94. package/dist/esm/preview-host/index.d.ts.map +1 -0
  95. package/dist/esm/preview-host/index.js +18 -0
  96. package/dist/esm/preview-host/index.js.map +1 -0
  97. package/dist/esm/preview-host/previewProfile.d.ts +6 -0
  98. package/dist/esm/preview-host/previewProfile.d.ts.map +1 -0
  99. package/dist/esm/preview-host/previewProfile.js +49 -0
  100. package/dist/esm/preview-host/previewProfile.js.map +1 -0
  101. package/dist/esm/preview-host/previewSampleData.d.ts +22 -0
  102. package/dist/esm/preview-host/previewSampleData.d.ts.map +1 -0
  103. package/dist/esm/preview-host/previewSampleData.js +113 -0
  104. package/dist/esm/preview-host/previewSampleData.js.map +1 -0
  105. package/dist/esm/preview-host/resolvePreviewPlan.d.ts +18 -0
  106. package/dist/esm/preview-host/resolvePreviewPlan.d.ts.map +1 -0
  107. package/dist/esm/preview-host/resolvePreviewPlan.js +124 -0
  108. package/dist/esm/preview-host/resolvePreviewPlan.js.map +1 -0
  109. package/dist/esm/preview-host/useSizeClass.d.ts +3 -0
  110. package/dist/esm/preview-host/useSizeClass.d.ts.map +1 -0
  111. package/dist/esm/preview-host/useSizeClass.js +21 -0
  112. package/dist/esm/preview-host/useSizeClass.js.map +1 -0
  113. package/dist/esm/tsconfig.tsbuildinfo +1 -1
  114. package/index.ts +1 -0
  115. package/package.json +8 -8
  116. package/preview-host/ApplicationDocument.ts +31 -0
  117. package/preview-host/ApplicationHost.tsx +155 -0
  118. package/preview-host/FlowArrangementView.tsx +59 -0
  119. package/preview-host/PreviewProblem.ts +68 -0
  120. package/preview-host/PreviewProblemsView.tsx +31 -0
  121. package/preview-host/PreviewSliceFragment.ts +43 -0
  122. package/preview-host/PreviewTarget.ts +19 -0
  123. package/preview-host/PreviewTemplate.tsx +45 -0
  124. package/preview-host/PreviewToast.tsx +32 -0
  125. package/preview-host/ScenePreviewHost.tsx +250 -0
  126. package/preview-host/composeApplication.ts +273 -0
  127. package/preview-host/defaultApplicationChrome.ts +33 -0
  128. package/preview-host/diagnosePreview.ts +132 -0
  129. package/preview-host/index.ts +20 -0
  130. package/preview-host/previewProfile.ts +47 -0
  131. package/preview-host/previewSampleData.ts +144 -0
  132. package/preview-host/resolvePreviewPlan.ts +100 -0
  133. package/preview-host/useSizeClass.ts +23 -0
  134. package/shell/layout.css +22 -0
package/index.ts CHANGED
@@ -12,3 +12,4 @@ export * from './themes';
12
12
  export * from './defaultBlueprintComponents';
13
13
  export * from './defaultBlueprint';
14
14
  export * from './templateProvenance';
15
+ export * from './preview-host';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cratis/scene.blueprint.default",
3
- "version": "4.33.0",
3
+ "version": "4.35.0",
4
4
  "description": "The default blueprint: application shell layouts, the screen and dialog templates that go inside them, the components that fill their slots, and two themes.",
5
5
  "keywords": [
6
6
  "cratis",
@@ -55,15 +55,15 @@
55
55
  "build-storybook": "storybook build"
56
56
  },
57
57
  "dependencies": {
58
- "@cratis/scene.engine": "4.33.0",
59
- "@cratis/scene.model": "4.33.0",
60
- "@cratis/scene.react": "4.33.0"
58
+ "@cratis/scene.engine": "4.35.0",
59
+ "@cratis/scene.model": "4.35.0",
60
+ "@cratis/scene.react": "4.35.0"
61
61
  },
62
62
  "devDependencies": {
63
- "@cratis/scene.engine": "4.33.0",
64
- "@cratis/scene.model": "4.33.0",
65
- "@cratis/scene.primereact": "4.33.0",
66
- "@cratis/scene.react": "4.33.0",
63
+ "@cratis/scene.engine": "4.35.0",
64
+ "@cratis/scene.model": "4.35.0",
65
+ "@cratis/scene.primereact": "4.35.0",
66
+ "@cratis/scene.react": "4.35.0",
67
67
  "@primereact/core": "11.2.0",
68
68
  "@primereact/headless": "11.2.0",
69
69
  "@primeuix/themes": "^3.0.1",
@@ -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 type { SceneDocument, Theme, UiProfile } from '@cratis/scene.model';
5
+
6
+ /**
7
+ * What an application is made of: the document a designer edits - layouts, templates, screens, and what a template exposes and
8
+ * each instance contributes - and the profile and theme the application renders with. Stage assembles it from a Screenplay
9
+ * model, Studio from the model in memory (unsaved edits included); the composition and the hosts take nothing else.
10
+ */
11
+ export type ApplicationDocument =
12
+ & Pick<SceneDocument, 'layouts' | 'screenTemplates' | 'screens'>
13
+ & Partial<Pick<SceneDocument, 'dialogTemplates' | 'exposures' | 'instanceContributions'>>
14
+ & {
15
+ /** The profiles the application declares; the first for the web is the one rendered with. */
16
+ uiProfiles?: UiProfile[];
17
+
18
+ themes?: Theme[];
19
+ };
20
+
21
+ /** The document with everything a template chain needs filled in. */
22
+ export function completeDocument(application: ApplicationDocument): SceneDocument {
23
+ return {
24
+ layouts: application.layouts,
25
+ screenTemplates: application.screenTemplates,
26
+ dialogTemplates: application.dialogTemplates ?? [],
27
+ screens: application.screens,
28
+ exposures: application.exposures ?? [],
29
+ instanceContributions: application.instanceContributions ?? [],
30
+ };
31
+ }
@@ -0,0 +1,155 @@
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 { ReactNode, useMemo, useState } from 'react';
5
+ import { DialogTemplate, SceneElement, type EffectiveConfiguration } from '@cratis/scene.model';
6
+ import { BindingResolver, BindingScope } from '@cratis/scene.engine';
7
+ import {
8
+ ComponentRegistry, SceneElementView, SceneHistory, SceneNavigationContextValue, SceneNavigationModel, SceneNavigationModelProvider,
9
+ SceneNavigationProvider, SceneOutletContext, SliceDialogView, collectOutletOwners, createMemorySceneHistory, navigationDestinations,
10
+ sceneRoutesFrom, sliceDialogParameters, useSceneHistoryNavigation,
11
+ } from '@cratis/scene.react';
12
+ import { LayoutConfigProvider } from '../configuration';
13
+ import { ApplicationDocument } from './ApplicationDocument';
14
+ import { previewComponents } from './PreviewTemplate';
15
+ import { CompositionOptions, collectDestinations, composeApplicationScreen, composeDialog, composeOutletScreen } from './composeApplication';
16
+
17
+ export interface ApplicationHostProps {
18
+ /** The document the application is composed from. */
19
+ application: ApplicationDocument;
20
+
21
+ /** What the host decides about composition; Stage and the preview host differ only here. */
22
+ composition?: CompositionOptions;
23
+
24
+ /** The structure and navigation the shell's regions show and the items' destinations resolve against. */
25
+ navigationModel: SceneNavigationModel;
26
+
27
+ registry: ComponentRegistry;
28
+
29
+ /** The screen shown first. */
30
+ initialScreen: string;
31
+
32
+ /** Where entries are recorded: the address bar for Stage, an in-memory history for a preview, which never touches the host's URL (PRV4). */
33
+ history?: SceneHistory;
34
+
35
+ /** What the screens' bindings resolve against. */
36
+ resolveBinding?: BindingResolver;
37
+ queryResults?: Record<string, unknown>;
38
+
39
+ /**
40
+ * Draws the composed screen. The default renders it with `SceneElementView`; Stage wraps it with its data and command scope.
41
+ * @param element The composed screen.
42
+ * @param configuration What the screen's templates were configured with.
43
+ * @param navigation Where the user is.
44
+ */
45
+ renderScreen?(element: SceneElement, configuration: EffectiveConfiguration | undefined, navigation: SceneNavigationContextValue): ReactNode;
46
+
47
+ /**
48
+ * Draws what the open dialog shows when the document has no dialog template of that name - a toolbar slice button's task
49
+ * dialog by default.
50
+ */
51
+ renderDialog?(navigation: SceneNavigationContextValue): ReactNode;
52
+
53
+ /**
54
+ * Draws a dialog the document declares, in place of the host's plain one - a host that wants its own backdrop, a close button
55
+ * and keyboard handling around the template's content. Called inside {@link scope}.
56
+ * @param template The dialog template that is open.
57
+ * @param content The template's composed content.
58
+ * @param navigation Where the user is.
59
+ */
60
+ renderDialogTemplate?(template: DialogTemplate, content: SceneElement, navigation: SceneNavigationContextValue): ReactNode;
61
+
62
+ /**
63
+ * What a host provides around everything that renders - the screen on the page and the dialog over it: its data, its command
64
+ * dispatcher, its chrome state. Inside the navigation providers, so it can read where the user is. Stage puts its data and
65
+ * interaction scope here, so the screen, a dialog template and a slice dialog all share them.
66
+ * @param navigation Where the user is.
67
+ * @param children The screen and the dialog.
68
+ */
69
+ scope?(navigation: SceneNavigationContextValue, children: ReactNode): ReactNode;
70
+
71
+ /**
72
+ * Where the shell keeps its layout configuration - the sidebar's state, the chosen theme. A preview keeps none (null, the default);
73
+ * a running application passes its storage so what a user chose is still chosen on the next visit.
74
+ */
75
+ layoutConfigStorage?: Storage | null;
76
+
77
+ /** Rendered inside the host's providers, after the application. */
78
+ children?: ReactNode;
79
+ }
80
+
81
+ /**
82
+ * The application as both Stage and the preview host run it: the shell composed for the screen on the page
83
+ * (`composeApplicationScreen`), nested outlets holding the screens placed in them, the navigation model in scope for the shell's
84
+ * topbar and sidebar regions, every destination the document and the navigation lead to routed, and the dialog a toolbar slice
85
+ * button or a dialog destination opened shown over it - one place for all of it (spec PRV9), so a behaviour that differs between
86
+ * the two hosts is a bug in the host, not a second implementation.
87
+ */
88
+ export function ApplicationHost({ application, composition = {}, navigationModel, registry: hostRegistry, initialScreen, history, resolveBinding, queryResults, renderScreen, renderDialog, renderDialogTemplate, scope, layoutConfigStorage = null, children }: ApplicationHostProps) {
89
+ const [memoryHistory] = useState(() => createMemorySceneHistory());
90
+ const registry = useMemo(() => ({ ...previewComponents, ...hostRegistry }), [hostRegistry]);
91
+ const hostHistory = history ?? memoryHistory;
92
+ const screens = application.screens;
93
+
94
+ const outletScreens = useMemo(
95
+ () => Object.fromEntries(screens.map(candidate => [candidate.name, composeOutletScreen(application, candidate, composition)])),
96
+ [application, composition, screens]);
97
+ const owners = useMemo(() => collectOutletOwners(outletScreens), [outletScreens]);
98
+ const destinations = useMemo(
99
+ () => [...collectDestinations(application), ...navigationDestinations(navigationModel.structure, navigationModel.navigation, navigationModel.screenFor)],
100
+ [application, navigationModel]);
101
+ const routes = useMemo(() => sceneRoutesFrom(screens.map(candidate => candidate.name), destinations), [destinations, screens]);
102
+ const bindingScope = useMemo<BindingScope>(() => ({ queryResults }), [queryResults]);
103
+ const navigation = useSceneHistoryNavigation(hostHistory, routes, initialScreen, bindingScope, owners, destinations);
104
+
105
+ const screen = screens.find(candidate => candidate.name === navigation.primaryScreen) ?? screens.find(candidate => candidate.name === initialScreen) ?? screens[0];
106
+ const composed = useMemo(() => screen ? composeApplicationScreen(application, screen, composition) : undefined, [application, composition, screen]);
107
+ const outletContext = useMemo(() => ({ screens: outletScreens, registry }), [outletScreens, registry]);
108
+
109
+ const dialogTemplate = navigation.currentDialog ? application.dialogTemplates?.find(candidate => candidate.name === navigation.currentDialog) : undefined;
110
+ const sliceDialog = !dialogTemplate && navigation.currentDialog && sliceDialogParameters(navigation.currentDialogParameters) !== undefined;
111
+ const dialogName = sliceDialogParameters(navigation.currentDialogParameters)?.title ?? navigation.currentDialog;
112
+
113
+ const dialogContent = dialogTemplate ? composeDialog(application, dialogTemplate, composition) : undefined;
114
+
115
+ const body = (
116
+ <>
117
+ <div data-scene-outlet='primary' data-scene-url={navigation.currentUrl} data-scene-screen={navigation.primaryScreen}>
118
+ {composed && <Drawn>{renderScreen
119
+ ? () => renderScreen(composed.element, composed.configuration, navigation)
120
+ : () => <SceneElementView element={composed.element} registry={registry} resolveBinding={resolveBinding} queryResults={queryResults} configuration={composed.configuration} />}</Drawn>}
121
+ </div>
122
+ {dialogTemplate && dialogContent && (renderDialogTemplate
123
+ ? <Drawn>{() => renderDialogTemplate(dialogTemplate, dialogContent, navigation)}</Drawn>
124
+ : (
125
+ <div role='dialog' aria-modal='true' aria-label={dialogName} data-scene-dialog={navigation.currentDialog}>
126
+ <SceneElementView element={dialogContent} registry={registry} resolveBinding={resolveBinding} queryResults={queryResults} />
127
+ </div>
128
+ ))}
129
+ {sliceDialog && (
130
+ <div role='dialog' aria-modal='true' aria-label={dialogName} data-scene-dialog={navigation.currentDialog}>
131
+ {renderDialog?.(navigation) ?? <SliceDialogView />}
132
+ </div>
133
+ )}
134
+ </>
135
+ );
136
+
137
+ return (
138
+ <SceneNavigationProvider value={navigation}>
139
+ <SceneOutletContext.Provider value={outletContext}>
140
+ <SceneNavigationModelProvider model={navigationModel}>
141
+ {/* The shell reads its layout configuration from a provider; nothing is saved unless the host gives it somewhere to. */}
142
+ <LayoutConfigProvider storage={layoutConfigStorage}>
143
+ {scope ? scope(navigation, body) : body}
144
+ {children}
145
+ </LayoutConfigProvider>
146
+ </SceneNavigationModelProvider>
147
+ </SceneOutletContext.Provider>
148
+ </SceneNavigationProvider>
149
+ );
150
+ }
151
+
152
+ /** Calls what a host draws as part of this component, so a hook it uses runs under the providers the host put in scope. */
153
+ function Drawn({ children }: { children(): ReactNode }) {
154
+ return <>{children()}</>;
155
+ }
@@ -0,0 +1,59 @@
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 type { CSSProperties, ReactNode } from 'react';
5
+ import { FlowContainerKind, type FlowNode } from '@cratis/scene.model';
6
+ import { isFlowContainer, isFlowGrid, isFlowSlotLeaf } from '@cratis/scene.engine';
7
+
8
+ /**
9
+ * Renders a {@link FlowNode} tree - the same one `evaluateFlowArrangement` picks for the current size
10
+ * class - by walking it into nested flex/grid containers and dropping each named slot's real content into
11
+ * its {@link FlowSlotLeaf}. This is what turns a modeled `arrangement flow` (a layout's topbar/sidebar/
12
+ * content/footer, or a screen template's header/body/aside) into something that actually looks like more
13
+ * than one column, rather than every slot's content stacked flat regardless of what the document said.
14
+ *
15
+ * A fixed `width`/`height` on a slot is a known, documented gap in the arrangement it is fed (see
16
+ * the Screenplay converter) - only `grow` and `span` survive conversion. A slot that does not grow
17
+ * therefore sizes to its own content here, which is a reasonable rendering of "not growing" even though
18
+ * the exact pixel the author wrote is not carried this far yet.
19
+ */
20
+ export function FlowArrangementView({ node, slots }: { node: FlowNode; slots: Record<string, ReactNode> }) {
21
+ if (isFlowSlotLeaf(node)) {
22
+ return <>{slots[node.slotName] ?? null}</>;
23
+ }
24
+
25
+ if (isFlowGrid(node)) {
26
+ const style: CSSProperties = {
27
+ gap: `${node.gap}px`,
28
+ gridTemplateColumns: node.columns ? `repeat(${node.columns}, 1fr)` : undefined,
29
+ gridTemplateRows: node.rows ? `repeat(${node.rows}, 1fr)` : undefined,
30
+ };
31
+ return (
32
+ <div className='flow-container flow-container--grid' style={style}>
33
+ {node.children.map((child, index) => <FlowChild key={index} node={child} slots={slots} />)}
34
+ </div>
35
+ );
36
+ }
37
+
38
+ if (isFlowContainer(node)) {
39
+ const direction = node.kind === FlowContainerKind.Row ? 'row' : 'column';
40
+ return (
41
+ <div className={`flow-container flow-container--${direction}`} style={{ gap: `${node.gap}px` }}>
42
+ {node.children.map((child, index) => <FlowChild key={index} node={child} slots={slots} />)}
43
+ </div>
44
+ );
45
+ }
46
+
47
+ return null;
48
+ }
49
+
50
+ function FlowChild({ node, slots }: { node: FlowNode; slots: Record<string, ReactNode> }) {
51
+ const style: CSSProperties = node.grow
52
+ ? { flex: `${node.grow} 1 0%`, minWidth: 0, minHeight: 0 }
53
+ : { flex: '0 0 auto' };
54
+ return (
55
+ <div className='flow-child' style={style}>
56
+ <FlowArrangementView node={node} slots={slots} />
57
+ </div>
58
+ );
59
+ }
@@ -0,0 +1,68 @@
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
+ /** Why a preview could not be rendered, or rendered with a caveat. */
5
+ export enum PreviewProblemCode {
6
+ /** The module, feature or slice the preview was asked for is not in the application structure. */
7
+ TargetNotFound = 'targetNotFound',
8
+
9
+ /** The module has no feature to show. */
10
+ ModuleWithoutFeatures = 'moduleWithoutFeatures',
11
+
12
+ /** The feature the preview shows has no screen in the document. */
13
+ ScreenMissing = 'screenMissing',
14
+
15
+ /** The slice is neither a State Change nor a State View, so there is nothing to preview. */
16
+ SliceNotPreviewable = 'sliceNotPreviewable',
17
+
18
+ /** A State View slice's data fragment was not found, so its feature's page is shown. */
19
+ SliceFragmentMissing = 'sliceFragmentMissing',
20
+
21
+ /** The layout a screen names is neither in the document nor one the blueprint provides. */
22
+ LayoutMissing = 'layoutMissing',
23
+
24
+ /** The screen template a screen names is neither in the document nor one the blueprint provides. */
25
+ TemplateMissing = 'templateMissing',
26
+
27
+ /** A template found no single home in the layout, or nests inside itself. */
28
+ TemplateUnplaced = 'templateUnplaced',
29
+
30
+ /** The application structure has a duplicate or ambiguous id. */
31
+ StructureInvalid = 'structureInvalid',
32
+
33
+ /** A navigation item leads nowhere or collides with another (the engine's navigation diagnostics). */
34
+ NavigationInvalid = 'navigationInvalid',
35
+
36
+ /** A toolbar button's slice does not exist, is not a State Change or is empty (the engine's toolbar diagnostics). */
37
+ ToolbarInvalid = 'toolbarInvalid',
38
+
39
+ /** The profile's sample data selection names a pack or set that does not exist. */
40
+ SampleDataInvalid = 'sampleDataInvalid',
41
+ }
42
+
43
+ /** How a problem affects the preview. */
44
+ export enum PreviewProblemSeverity {
45
+ /** The preview cannot show what was asked for; the problems are shown instead of a blank screen. */
46
+ Error = 'error',
47
+
48
+ /** The preview renders, but something in the model will not behave as authored. */
49
+ Warning = 'warning',
50
+ }
51
+
52
+ /**
53
+ * One problem the preview found (PRV8): what it is, where, and a sentence an author can act on. The node id is what a host
54
+ * links to - a screen, a template, a navigation item, a toolbar button, a module, feature or slice.
55
+ */
56
+ export interface PreviewProblem {
57
+ code: PreviewProblemCode;
58
+ severity: PreviewProblemSeverity;
59
+ message: string;
60
+
61
+ /** The model node the problem is about, when it has one. */
62
+ nodeId?: string;
63
+ }
64
+
65
+ /** Whether any of the problems stops the preview from rendering. */
66
+ export function hasBlockingProblem(problems: PreviewProblem[]): boolean {
67
+ return problems.some(problem => problem.severity === PreviewProblemSeverity.Error);
68
+ }
@@ -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 { PreviewProblem } from './PreviewProblem';
5
+
6
+ export interface PreviewProblemsViewProps {
7
+ problems: PreviewProblem[];
8
+
9
+ /** Called when the author follows a problem to the node it is about. */
10
+ onSelectProblem?(problem: PreviewProblem): void;
11
+ }
12
+
13
+ /**
14
+ * What a preview shows instead of a blank screen when the model has errors that would break rendering (PRV8): each problem
15
+ * with its code, its message and a link to the offending node.
16
+ */
17
+ export function PreviewProblemsView({ problems, onSelectProblem }: PreviewProblemsViewProps) {
18
+ return (
19
+ <section className='scene-preview-problems' role='alert' data-scene-preview-problems>
20
+ <h2>This preview cannot be shown</h2>
21
+ <ul>
22
+ {problems.map((problem, index) => (
23
+ <li key={`${problem.code}-${problem.nodeId ?? index}-${index}`} data-severity={problem.severity} data-code={problem.code} data-node-id={problem.nodeId}>
24
+ <code>{problem.code}</code> {problem.message}
25
+ {problem.nodeId !== undefined && onSelectProblem && <> <button type='button' onClick={() => onSelectProblem(problem)}>Show {problem.nodeId}</button></>}
26
+ </li>
27
+ ))}
28
+ </ul>
29
+ </section>
30
+ );
31
+ }
@@ -0,0 +1,43 @@
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 { ExternalComponent, SceneElement, Screen, SliceNode } from '@cratis/scene.model';
5
+
6
+ function matches(element: ExternalComponent, names: Set<string>): boolean {
7
+ const properties = element.properties as Record<string, unknown> | undefined;
8
+ return ['query', 'readModel', 'slice'].some(key => {
9
+ const value = properties?.[key];
10
+ return typeof value === 'string' && (names.has(value) || names.has(value.slice(value.lastIndexOf('.') + 1)));
11
+ });
12
+ }
13
+
14
+ function childrenOf(element: SceneElement): SceneElement[] {
15
+ const node = element as { slots?: Record<string, SceneElement[]>; children?: SceneElement[]; content?: SceneElement; itemTemplate?: SceneElement };
16
+ return [
17
+ ...Object.values(node.slots ?? {}).flat(),
18
+ ...(Array.isArray(node.children) ? node.children : []),
19
+ ...(node.content ? [node.content] : []),
20
+ ...(node.itemTemplate ? [node.itemTemplate] : []),
21
+ ];
22
+ }
23
+
24
+ /**
25
+ * The data fragment of a State View slice (PRV3): the outermost components of the slice's screen bound to the slice's read model
26
+ * or query - by the slice's qualified id, its id, its name or its command - each with everything inside it. Empty when the screen
27
+ * has nothing bound to it.
28
+ */
29
+ export function findSliceFragment(screen: Screen, sliceId: string, slice: SliceNode): SceneElement[] {
30
+ const names = new Set([sliceId, slice.id, slice.name, ...(slice.command ? [slice.command] : [])]);
31
+ const found: SceneElement[] = [];
32
+ const visit = (element: SceneElement) => {
33
+ if ((element as ExternalComponent).componentName !== undefined && matches(element as ExternalComponent, names)) {
34
+ found.push(element);
35
+ return;
36
+ }
37
+
38
+ childrenOf(element).forEach(visit);
39
+ };
40
+
41
+ Object.values(screen.slotContent).flat().forEach(visit);
42
+ return found;
43
+ }
@@ -0,0 +1,19 @@
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 a preview shows (spec PRV1/PRV3): a module - the application shell with the module's effective navigation and its
6
+ * first feature -, one of its features - the same shell with that feature - or a slice. Ids are the qualified ids of the
7
+ * application structure: `sales`, `sales.orders`, `sales.orders.PlaceOrder`. A feature may also be given relative to its module
8
+ * (`orders`).
9
+ *
10
+ * A State Change slice opens its dialog over a minimal host page; a State View slice renders its data fragment.
11
+ */
12
+ export type PreviewTarget =
13
+ | { module: string; feature?: string }
14
+ | { slice: string };
15
+
16
+ /** Whether a target names a slice. */
17
+ export function isSliceTarget(target: PreviewTarget): target is { slice: string } {
18
+ return 'slice' in target;
19
+ }
@@ -0,0 +1,45 @@
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 type { ReactNode } from 'react';
5
+ import type { FlowArrangement } from '@cratis/scene.model';
6
+ import { evaluateFlowArrangement } from '@cratis/scene.engine';
7
+ import type { ComponentRegistry, RegisteredComponentProps } from '@cratis/scene.react';
8
+ import { FlowArrangementView } from './FlowArrangementView';
9
+ import { useSizeClass } from './useSizeClass';
10
+
11
+ /** The name the template wrapper is registered under. */
12
+ export const previewTemplateComponent = 'Scene:template';
13
+
14
+ function isFlowArrangement(arrangement: unknown): arrangement is FlowArrangement {
15
+ return !!arrangement && typeof arrangement === 'object' && 'root' in arrangement;
16
+ }
17
+
18
+ /**
19
+ * A screen's content inside the screen template it names.
20
+ *
21
+ * The template's own `arrangement flow` positions its slots - header, body, aside - exactly as the document wrote it, evaluated
22
+ * for the window's current size class. A template with no flow arrangement stacks them. It sits inside the shell's content
23
+ * slot, so the shell, not this, owns the chrome around it. Stage and the preview host render templates with this one component.
24
+ */
25
+ export function PreviewTemplate({ element, slots }: RegisteredComponentProps) {
26
+ const sizeClass = useSizeClass();
27
+ const arrangement = (element.properties as Record<string, unknown> | undefined)?.arrangement;
28
+ const nodes: Record<string, ReactNode> = {};
29
+ for (const [name, content] of Object.entries(slots)) {
30
+ nodes[name] = <section className='scene-template-slot' data-slot={name}>{content}</section>;
31
+ }
32
+
33
+ return (
34
+ <div className='scene-template' data-scene-id={element.id}>
35
+ {isFlowArrangement(arrangement)
36
+ ? <FlowArrangementView node={evaluateFlowArrangement(arrangement, sizeClass)} slots={nodes} />
37
+ : Object.entries(nodes).map(([name, node]) => <div key={name}>{node}</div>)}
38
+ </div>
39
+ );
40
+ }
41
+
42
+ /** The components the preview host and the Stage add to the registry for composed screens. */
43
+ export const previewComponents: ComponentRegistry = {
44
+ [previewTemplateComponent]: PreviewTemplate,
45
+ };
@@ -0,0 +1,32 @@
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
+ /** What a dry run reports (PRV5). */
5
+ export interface PreviewInvocation {
6
+ /** The qualified id of the slice. */
7
+ sliceId: string;
8
+
9
+ /** The command the slice accepts, when known. */
10
+ command?: string;
11
+
12
+ /** The payload that would have been sent. */
13
+ payload: Record<string, unknown>;
14
+ }
15
+
16
+ /**
17
+ * The toast that confirms a dry run: "Command would be sent", with the payload in an expandable detail. Nothing was sent to
18
+ * Chronicle or Arc.
19
+ */
20
+ export function PreviewToast({ invocation, onDismiss }: { invocation: PreviewInvocation; onDismiss(): void }) {
21
+ return (
22
+ <div className='scene-preview-toast' role='status' data-scene-preview-toast>
23
+ <strong>Command would be sent</strong>
24
+ <span> · {invocation.command ?? invocation.sliceId}</span>
25
+ <details>
26
+ <summary>Payload</summary>
27
+ <pre>{JSON.stringify(invocation.payload, null, 2)}</pre>
28
+ </details>
29
+ <button type='button' onClick={onDismiss}>Dismiss</button>
30
+ </div>
31
+ );
32
+ }