@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.
- package/dist/cjs/index.js +53 -4
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/preview-host/ApplicationDocument.js +17 -0
- package/dist/cjs/preview-host/ApplicationDocument.js.map +1 -0
- package/dist/cjs/preview-host/ApplicationHost.js +134 -0
- package/dist/cjs/preview-host/ApplicationHost.js.map +1 -0
- package/dist/cjs/preview-host/FlowArrangementView.js +75 -0
- package/dist/cjs/preview-host/FlowArrangementView.js.map +1 -0
- package/dist/cjs/preview-host/PreviewProblem.js +32 -0
- package/dist/cjs/preview-host/PreviewProblem.js.map +1 -0
- package/dist/cjs/preview-host/PreviewProblemsView.js +51 -0
- package/dist/cjs/preview-host/PreviewProblemsView.js.map +1 -0
- package/dist/cjs/preview-host/PreviewSliceFragment.js +55 -0
- package/dist/cjs/preview-host/PreviewSliceFragment.js.map +1 -0
- package/dist/cjs/preview-host/PreviewTarget.js +17 -0
- package/dist/cjs/preview-host/PreviewTarget.js.map +1 -0
- package/dist/cjs/preview-host/PreviewTemplate.js +49 -0
- package/dist/cjs/preview-host/PreviewTemplate.js.map +1 -0
- package/dist/cjs/preview-host/PreviewToast.js +45 -0
- package/dist/cjs/preview-host/PreviewToast.js.map +1 -0
- package/dist/cjs/preview-host/ScenePreviewHost.js +290 -0
- package/dist/cjs/preview-host/ScenePreviewHost.js.map +1 -0
- package/dist/cjs/preview-host/composeApplication.js +249 -0
- package/dist/cjs/preview-host/composeApplication.js.map +1 -0
- package/dist/cjs/preview-host/defaultApplicationChrome.js +43 -0
- package/dist/cjs/preview-host/defaultApplicationChrome.js.map +1 -0
- package/dist/cjs/preview-host/diagnosePreview.js +100 -0
- package/dist/cjs/preview-host/diagnosePreview.js.map +1 -0
- package/dist/cjs/preview-host/previewProfile.js +53 -0
- package/dist/cjs/preview-host/previewProfile.js.map +1 -0
- package/dist/cjs/preview-host/previewSampleData.js +116 -0
- package/dist/cjs/preview-host/previewSampleData.js.map +1 -0
- package/dist/cjs/preview-host/resolvePreviewPlan.js +126 -0
- package/dist/cjs/preview-host/resolvePreviewPlan.js.map +1 -0
- package/dist/cjs/preview-host/useSizeClass.js +23 -0
- package/dist/cjs/preview-host/useSizeClass.js.map +1 -0
- package/dist/esm/index.d.ts +1 -0
- package/dist/esm/index.d.ts.map +1 -1
- package/dist/esm/index.js +17 -0
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/preview-host/ApplicationDocument.d.ts +7 -0
- package/dist/esm/preview-host/ApplicationDocument.d.ts.map +1 -0
- package/dist/esm/preview-host/ApplicationDocument.js +15 -0
- package/dist/esm/preview-host/ApplicationDocument.js.map +1 -0
- package/dist/esm/preview-host/ApplicationHost.d.ts +24 -0
- package/dist/esm/preview-host/ApplicationHost.d.ts.map +1 -0
- package/dist/esm/preview-host/ApplicationHost.js +132 -0
- package/dist/esm/preview-host/ApplicationHost.js.map +1 -0
- package/dist/esm/preview-host/FlowArrangementView.d.ts +7 -0
- package/dist/esm/preview-host/FlowArrangementView.d.ts.map +1 -0
- package/dist/esm/preview-host/FlowArrangementView.js +73 -0
- package/dist/esm/preview-host/FlowArrangementView.js.map +1 -0
- package/dist/esm/preview-host/PreviewProblem.d.ts +26 -0
- package/dist/esm/preview-host/PreviewProblem.d.ts.map +1 -0
- package/dist/esm/preview-host/PreviewProblem.js +28 -0
- package/dist/esm/preview-host/PreviewProblem.js.map +1 -0
- package/dist/esm/preview-host/PreviewProblemsView.d.ts +7 -0
- package/dist/esm/preview-host/PreviewProblemsView.d.ts.map +1 -0
- package/dist/esm/preview-host/PreviewProblemsView.js +49 -0
- package/dist/esm/preview-host/PreviewProblemsView.js.map +1 -0
- package/dist/esm/preview-host/PreviewSliceFragment.d.ts +3 -0
- package/dist/esm/preview-host/PreviewSliceFragment.d.ts.map +1 -0
- package/dist/esm/preview-host/PreviewSliceFragment.js +53 -0
- package/dist/esm/preview-host/PreviewSliceFragment.js.map +1 -0
- package/dist/esm/preview-host/PreviewTarget.d.ts +10 -0
- package/dist/esm/preview-host/PreviewTarget.d.ts.map +1 -0
- package/dist/esm/preview-host/PreviewTarget.js +15 -0
- package/dist/esm/preview-host/PreviewTarget.js.map +1 -0
- package/dist/esm/preview-host/PreviewTemplate.d.ts +5 -0
- package/dist/esm/preview-host/PreviewTemplate.d.ts.map +1 -0
- package/dist/esm/preview-host/PreviewTemplate.js +45 -0
- package/dist/esm/preview-host/PreviewTemplate.js.map +1 -0
- package/dist/esm/preview-host/PreviewToast.d.ts +10 -0
- package/dist/esm/preview-host/PreviewToast.d.ts.map +1 -0
- package/dist/esm/preview-host/PreviewToast.js +43 -0
- package/dist/esm/preview-host/PreviewToast.js.map +1 -0
- package/dist/esm/preview-host/ScenePreviewHost.d.ts +32 -0
- package/dist/esm/preview-host/ScenePreviewHost.d.ts.map +1 -0
- package/dist/esm/preview-host/ScenePreviewHost.js +288 -0
- package/dist/esm/preview-host/ScenePreviewHost.js.map +1 -0
- package/dist/esm/preview-host/composeApplication.d.ts +34 -0
- package/dist/esm/preview-host/composeApplication.d.ts.map +1 -0
- package/dist/esm/preview-host/composeApplication.js +239 -0
- package/dist/esm/preview-host/composeApplication.js.map +1 -0
- package/dist/esm/preview-host/defaultApplicationChrome.d.ts +4 -0
- package/dist/esm/preview-host/defaultApplicationChrome.d.ts.map +1 -0
- package/dist/esm/preview-host/defaultApplicationChrome.js +41 -0
- package/dist/esm/preview-host/defaultApplicationChrome.js.map +1 -0
- package/dist/esm/preview-host/diagnosePreview.d.ts +22 -0
- package/dist/esm/preview-host/diagnosePreview.d.ts.map +1 -0
- package/dist/esm/preview-host/diagnosePreview.js +98 -0
- package/dist/esm/preview-host/diagnosePreview.js.map +1 -0
- package/dist/esm/preview-host/index.d.ts +18 -0
- package/dist/esm/preview-host/index.d.ts.map +1 -0
- package/dist/esm/preview-host/index.js +18 -0
- package/dist/esm/preview-host/index.js.map +1 -0
- package/dist/esm/preview-host/previewProfile.d.ts +6 -0
- package/dist/esm/preview-host/previewProfile.d.ts.map +1 -0
- package/dist/esm/preview-host/previewProfile.js +49 -0
- package/dist/esm/preview-host/previewProfile.js.map +1 -0
- package/dist/esm/preview-host/previewSampleData.d.ts +22 -0
- package/dist/esm/preview-host/previewSampleData.d.ts.map +1 -0
- package/dist/esm/preview-host/previewSampleData.js +113 -0
- package/dist/esm/preview-host/previewSampleData.js.map +1 -0
- package/dist/esm/preview-host/resolvePreviewPlan.d.ts +18 -0
- package/dist/esm/preview-host/resolvePreviewPlan.d.ts.map +1 -0
- package/dist/esm/preview-host/resolvePreviewPlan.js +124 -0
- package/dist/esm/preview-host/resolvePreviewPlan.js.map +1 -0
- package/dist/esm/preview-host/useSizeClass.d.ts +3 -0
- package/dist/esm/preview-host/useSizeClass.d.ts.map +1 -0
- package/dist/esm/preview-host/useSizeClass.js +21 -0
- package/dist/esm/preview-host/useSizeClass.js.map +1 -0
- package/dist/esm/tsconfig.tsbuildinfo +1 -1
- package/index.ts +1 -0
- package/package.json +8 -8
- package/preview-host/ApplicationDocument.ts +31 -0
- package/preview-host/ApplicationHost.tsx +155 -0
- package/preview-host/FlowArrangementView.tsx +59 -0
- package/preview-host/PreviewProblem.ts +68 -0
- package/preview-host/PreviewProblemsView.tsx +31 -0
- package/preview-host/PreviewSliceFragment.ts +43 -0
- package/preview-host/PreviewTarget.ts +19 -0
- package/preview-host/PreviewTemplate.tsx +45 -0
- package/preview-host/PreviewToast.tsx +32 -0
- package/preview-host/ScenePreviewHost.tsx +250 -0
- package/preview-host/composeApplication.ts +273 -0
- package/preview-host/defaultApplicationChrome.ts +33 -0
- package/preview-host/diagnosePreview.ts +132 -0
- package/preview-host/index.ts +20 -0
- package/preview-host/previewProfile.ts +47 -0
- package/preview-host/previewSampleData.ts +144 -0
- package/preview-host/resolvePreviewPlan.ts +100 -0
- package/preview-host/useSizeClass.ts +23 -0
- package/shell/layout.css +22 -0
|
@@ -0,0 +1,250 @@
|
|
|
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, useCallback, useEffect, useMemo, useRef, useState } from 'react';
|
|
5
|
+
import { ApplicationStructure, NavigationModel, Screen, SliceKind, Theme } from '@cratis/scene.model';
|
|
6
|
+
import { ApplicationStructureIndex, EffectiveNavItem, resolveToolbarButton } from '@cratis/scene.engine';
|
|
7
|
+
import {
|
|
8
|
+
ComponentRegistry, SceneHistory, SceneThemeProvider, SliceCommandFieldsProvider, SliceCommandFieldsSource, SliceInvoker,
|
|
9
|
+
SliceInvokerProvider, createDryRunInvoker, createMemorySceneHistory, sliceDialogDestination, useSceneNavigation,
|
|
10
|
+
} from '@cratis/scene.react';
|
|
11
|
+
import { ComponentName } from '../ComponentName';
|
|
12
|
+
import { externalComponent } from '../gallery/elements';
|
|
13
|
+
import { LayoutName, SlotName } from '../layouts';
|
|
14
|
+
import { previewProfile } from './previewProfile';
|
|
15
|
+
import { ApplicationDocument } from './ApplicationDocument';
|
|
16
|
+
import { ApplicationHost } from './ApplicationHost';
|
|
17
|
+
import { CompositionOptions } from './composeApplication';
|
|
18
|
+
import { diagnosePreview } from './diagnosePreview';
|
|
19
|
+
import { PreviewProblem, PreviewProblemCode, PreviewProblemSeverity, hasBlockingProblem } from './PreviewProblem';
|
|
20
|
+
import { PreviewProblemsView } from './PreviewProblemsView';
|
|
21
|
+
import { PreviewSampleData, createPreviewSampleData } from './previewSampleData';
|
|
22
|
+
import { findSliceFragment } from './PreviewSliceFragment';
|
|
23
|
+
import { PreviewTarget } from './PreviewTarget';
|
|
24
|
+
import { PreviewInvocation, PreviewToast } from './PreviewToast';
|
|
25
|
+
import { PreviewPlan } from './resolvePreviewPlan';
|
|
26
|
+
|
|
27
|
+
export interface ScenePreviewHostProps {
|
|
28
|
+
/** The document to preview - the in-memory one, unsaved edits included. */
|
|
29
|
+
document: ApplicationDocument;
|
|
30
|
+
|
|
31
|
+
/** The modules, features and slices of the application. */
|
|
32
|
+
structure: ApplicationStructure | ApplicationStructureIndex;
|
|
33
|
+
|
|
34
|
+
/** The application and module navigation regions; none when absent. */
|
|
35
|
+
navigation?: NavigationModel;
|
|
36
|
+
|
|
37
|
+
/** What to preview: a module, a feature or a slice. */
|
|
38
|
+
target: PreviewTarget;
|
|
39
|
+
|
|
40
|
+
/** The sample data to preview with; none when absent. */
|
|
41
|
+
sampleData?: PreviewSampleData;
|
|
42
|
+
|
|
43
|
+
/** The components to draw with. */
|
|
44
|
+
registry: ComponentRegistry;
|
|
45
|
+
|
|
46
|
+
/** What a submitted slice dialog goes through; a dry run, which only validates and reports, when absent (PRV5). */
|
|
47
|
+
invoker?: SliceInvoker;
|
|
48
|
+
|
|
49
|
+
/** Called for every dry run that passes validation, with what would have been sent. */
|
|
50
|
+
onInvoked?(invocation: PreviewInvocation): void;
|
|
51
|
+
|
|
52
|
+
/** The fields of a command, for the slice dialogs; the preview's sample rows fill their starting values. */
|
|
53
|
+
fieldsFor?: SliceCommandFieldsSource;
|
|
54
|
+
|
|
55
|
+
/** What the host decides about composition, such as the packages names resolve against. */
|
|
56
|
+
composition?: CompositionOptions;
|
|
57
|
+
|
|
58
|
+
/** The theme to apply; Studio passes the application's selected one (PRV9). */
|
|
59
|
+
theme?: Theme;
|
|
60
|
+
|
|
61
|
+
/** Hides a navigation item the preview user is not allowed to see. */
|
|
62
|
+
isVisible?(item: EffectiveNavItem): boolean;
|
|
63
|
+
|
|
64
|
+
/** Keeps the preview's navigation; an in-memory history by default, so a preview never touches the host's address (PRV4). */
|
|
65
|
+
history?: SceneHistory;
|
|
66
|
+
|
|
67
|
+
/** Called with the problems found - blocking or not - whenever they change (PRV8). */
|
|
68
|
+
onProblems?(problems: PreviewProblem[]): void;
|
|
69
|
+
|
|
70
|
+
/** Called when the author follows a problem to its node. */
|
|
71
|
+
onSelectProblem?(problem: PreviewProblem): void;
|
|
72
|
+
|
|
73
|
+
/** Called with what is being previewed, `Sales`, `Orders`, ... - the strip's breadcrumb (PRV7). */
|
|
74
|
+
onBreadcrumbChange?(breadcrumb: string[]): void;
|
|
75
|
+
|
|
76
|
+
/** Called when Escape is pressed with no dialog open: the host exits the preview (PRV7). */
|
|
77
|
+
onExit?(): void;
|
|
78
|
+
|
|
79
|
+
/** Extra content inside the host, such as a design layer's strip. */
|
|
80
|
+
children?: ReactNode;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
const previewPageScreen = '#preview-page';
|
|
84
|
+
const noNavigation: NavigationModel = { application: { regions: [] }, modules: [] };
|
|
85
|
+
|
|
86
|
+
/** The page a slice preview happens over when its feature has no screen of its own, or a State View's fragment is shown. */
|
|
87
|
+
function minimalPage(layout: string, content: Screen['slotContent'][string]): Screen {
|
|
88
|
+
return {
|
|
89
|
+
name: previewPageScreen,
|
|
90
|
+
layout,
|
|
91
|
+
slotContent: { [SlotName.Content]: content },
|
|
92
|
+
forms: [],
|
|
93
|
+
contributions: [],
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Renders the effective application (spec 10, PRV1-PRV9) from an in-memory document: the real shell, its navigation regions, the
|
|
99
|
+
* first feature of a module, a feature, or a slice - a State Change slice as its task dialog over a minimal host page, a State View
|
|
100
|
+
* slice as its data fragment.
|
|
101
|
+
*
|
|
102
|
+
* - **One code path**: the composition and the host are the Stage's own (`composeApplicationScreen`, `ApplicationHost`); nothing
|
|
103
|
+
* here draws a shell, a menu or a dialog (PRV9).
|
|
104
|
+
* - **Read-only**: no design affordances, no edits (PRV8) - a toolbar button without a slice shows no warning badge here; it is
|
|
105
|
+
* reported as a problem instead. A model that cannot render - an unknown target, a feature without a screen,
|
|
106
|
+
* a layout or template nobody provides - gives a {@link PreviewProblemsView} and `onProblems`, not a blank screen.
|
|
107
|
+
* - **Navigation** keeps to an in-memory history; a different module swaps the shell's effective navigation (PRV4).
|
|
108
|
+
* - **Dialogs** submit through the dry-run invoker unless one is given: validation runs, nothing is sent, a toast says so (PRV5).
|
|
109
|
+
* - **Data** is the active sample data pack's (PRV6); the same seed shows the same rows.
|
|
110
|
+
* - The strip (`Previewing`, sample data, `Exit preview · Esc`) is the host application's: this reports the breadcrumb and calls
|
|
111
|
+
* `onExit` on Escape.
|
|
112
|
+
*/
|
|
113
|
+
export function ScenePreviewHost({
|
|
114
|
+
document: application, structure, navigation = noNavigation, target, sampleData, registry, invoker, onInvoked, fieldsFor, composition = {},
|
|
115
|
+
theme, isVisible, history, onProblems, onSelectProblem, onBreadcrumbChange, onExit, children,
|
|
116
|
+
}: ScenePreviewHostProps) {
|
|
117
|
+
const index = useMemo(() => structure instanceof ApplicationStructureIndex ? structure : new ApplicationStructureIndex(structure), [structure]);
|
|
118
|
+
const compose = useMemo<CompositionOptions>(() => ({ logo: navigation.application.logo, ...composition }), [composition, navigation.application.logo]);
|
|
119
|
+
const diagnosis = useMemo(
|
|
120
|
+
() => diagnosePreview({ document: application, structure: index, navigation, target, packs: sampleData?.packs, composition: compose }),
|
|
121
|
+
[application, compose, index, navigation, sampleData?.packs, target]);
|
|
122
|
+
|
|
123
|
+
const problems = useMemo(() => {
|
|
124
|
+
const found = [...diagnosis.problems];
|
|
125
|
+
const { plan, screen } = diagnosis;
|
|
126
|
+
if (plan?.kind === 'stateView' && screen && plan.slice) {
|
|
127
|
+
const slice = index.findSlice(plan.slice);
|
|
128
|
+
if (slice && findSliceFragment(screen, plan.slice, slice).length === 0) {
|
|
129
|
+
found.push({ code: PreviewProblemCode.SliceFragmentMissing, severity: PreviewProblemSeverity.Warning, message: `Nothing on the screen '${screen.name}' is bound to the State View '${plan.slice}', so its page is shown.`, nodeId: plan.slice });
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
return found;
|
|
134
|
+
}, [diagnosis, index]);
|
|
135
|
+
|
|
136
|
+
const key = JSON.stringify(problems);
|
|
137
|
+
const reported = useRef<string | undefined>(undefined);
|
|
138
|
+
useEffect(() => {
|
|
139
|
+
if (reported.current === key) return;
|
|
140
|
+
reported.current = key;
|
|
141
|
+
onProblems?.(problems);
|
|
142
|
+
}, [key, onProblems, problems]);
|
|
143
|
+
|
|
144
|
+
const breadcrumb = diagnosis.plan?.breadcrumb;
|
|
145
|
+
const breadcrumbKey = breadcrumb?.join('\u0000');
|
|
146
|
+
useEffect(() => {
|
|
147
|
+
if (breadcrumb) onBreadcrumbChange?.(breadcrumb);
|
|
148
|
+
// `breadcrumbKey` is the value of `breadcrumb`.
|
|
149
|
+
}, [breadcrumbKey, onBreadcrumbChange]);
|
|
150
|
+
|
|
151
|
+
const [memoryHistory] = useState(() => createMemorySceneHistory());
|
|
152
|
+
const [toast, setToast] = useState<PreviewInvocation>();
|
|
153
|
+
const dryRun = useMemo(() => createDryRunInvoker({
|
|
154
|
+
onInvoked: invocation => {
|
|
155
|
+
setToast(invocation);
|
|
156
|
+
onInvoked?.(invocation);
|
|
157
|
+
},
|
|
158
|
+
}), [onInvoked]);
|
|
159
|
+
|
|
160
|
+
const samples = useMemo(() => createPreviewSampleData(application, sampleData), [application, sampleData]);
|
|
161
|
+
const fields = useMemo<SliceCommandFieldsSource>(() => (command, slice) => samples.commandFields(command, fieldsFor?.(command, slice) ?? []), [fieldsFor, samples]);
|
|
162
|
+
|
|
163
|
+
const page = useMemo(() => {
|
|
164
|
+
const { plan, screen } = diagnosis;
|
|
165
|
+
if (!plan) return undefined;
|
|
166
|
+
|
|
167
|
+
if (!screen) {
|
|
168
|
+
// A slice whose feature has no screen of its own: a minimal host page, headed with where it is.
|
|
169
|
+
if (plan.slice === undefined) return undefined;
|
|
170
|
+
const layout = previewProfile(application.uiProfiles).layout ?? LayoutName.AppShell;
|
|
171
|
+
const header = externalComponent('preview-page-header', ComponentName.PageHeader, { title: plan.breadcrumb.slice(0, -1).join(' › ') || plan.breadcrumb[0] });
|
|
172
|
+
return { screens: [...application.screens, minimalPage(layout, [header])], initial: previewPageScreen };
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
if (plan.kind === 'stateView' && plan.slice) {
|
|
176
|
+
const slice = index.findSlice(plan.slice);
|
|
177
|
+
const fragment = slice ? findSliceFragment(screen, plan.slice, slice) : [];
|
|
178
|
+
if (fragment.length > 0) return { screens: [...application.screens, minimalPage(screen.layout, fragment)], initial: previewPageScreen };
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
return { screens: application.screens, initial: screen.name };
|
|
182
|
+
}, [application.screens, application.uiProfiles, diagnosis, index]);
|
|
183
|
+
|
|
184
|
+
const hostApplication = useMemo(() => page ? { ...application, screens: page.screens } : application, [application, page]);
|
|
185
|
+
const navigationModel = useMemo(() => ({ structure: index, navigation, isVisible }), [index, isVisible, navigation]);
|
|
186
|
+
|
|
187
|
+
const onKeyDown = useCallback((event: KeyboardEvent) => {
|
|
188
|
+
if (event.key === 'Escape' && !event.defaultPrevented) onExit?.();
|
|
189
|
+
}, [onExit]);
|
|
190
|
+
|
|
191
|
+
if (hasBlockingProblem(problems) || !page) {
|
|
192
|
+
return <PreviewProblemsView problems={problems.filter(problem => problem.severity === PreviewProblemSeverity.Error)} onSelectProblem={onSelectProblem} />;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
const body = (
|
|
196
|
+
<SliceInvokerProvider invoker={invoker ?? dryRun} warnEmptySlices={false}>
|
|
197
|
+
<SliceCommandFieldsProvider fieldsFor={fields}>
|
|
198
|
+
<div className='scene-preview-host' data-scene-preview={diagnosis.plan?.kind} data-preview-breadcrumb={breadcrumb?.join(' › ')}>
|
|
199
|
+
<ApplicationHost
|
|
200
|
+
application={hostApplication}
|
|
201
|
+
composition={compose}
|
|
202
|
+
navigationModel={navigationModel}
|
|
203
|
+
registry={registry}
|
|
204
|
+
initialScreen={page.initial}
|
|
205
|
+
history={history ?? memoryHistory}
|
|
206
|
+
resolveBinding={samples.resolveBinding}
|
|
207
|
+
queryResults={samples.queryResults}>
|
|
208
|
+
{diagnosis.plan?.kind === 'stateChange' && <OpenSliceDialog index={index} plan={diagnosis.plan} />}
|
|
209
|
+
<EscapeToExit onKeyDown={onKeyDown} enabled={onExit !== undefined} />
|
|
210
|
+
{toast && invoker === undefined && <PreviewToast invocation={toast} onDismiss={() => setToast(undefined)} />}
|
|
211
|
+
{children}
|
|
212
|
+
</ApplicationHost>
|
|
213
|
+
</div>
|
|
214
|
+
</SliceCommandFieldsProvider>
|
|
215
|
+
</SliceInvokerProvider>
|
|
216
|
+
);
|
|
217
|
+
|
|
218
|
+
return theme ? <SceneThemeProvider theme={theme}>{body}</SceneThemeProvider> : body;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/** Opens the task dialog of a State Change slice once the page is up - the slice preview (PRV3). */
|
|
222
|
+
function OpenSliceDialog({ index, plan }: { index: ApplicationStructureIndex; plan: PreviewPlan }) {
|
|
223
|
+
const navigation = useSceneNavigation();
|
|
224
|
+
const opened = useRef(false);
|
|
225
|
+
useEffect(() => {
|
|
226
|
+
if (opened.current || plan.slice === undefined) return;
|
|
227
|
+
const slice = index.findSlice(plan.slice);
|
|
228
|
+
if (!slice || slice.kind !== SliceKind.StateChange) return;
|
|
229
|
+
|
|
230
|
+
const destination = sliceDialogDestination(resolveToolbarButton(index, { id: plan.slice, title: slice.name, slice: plan.slice }));
|
|
231
|
+
if (destination === undefined) return;
|
|
232
|
+
opened.current = true;
|
|
233
|
+
navigation.navigate(destination);
|
|
234
|
+
}, [index, navigation, plan.slice]);
|
|
235
|
+
|
|
236
|
+
return null;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
/** Exits the preview on Escape, unless something - an open dialog - already used the key. */
|
|
240
|
+
function EscapeToExit({ onKeyDown, enabled }: { onKeyDown(event: KeyboardEvent): void; enabled: boolean }) {
|
|
241
|
+
const navigation = useSceneNavigation();
|
|
242
|
+
const dialogOpen = navigation.currentDialog !== undefined;
|
|
243
|
+
useEffect(() => {
|
|
244
|
+
if (!enabled || dialogOpen) return undefined;
|
|
245
|
+
globalThis.addEventListener('keydown', onKeyDown);
|
|
246
|
+
return () => globalThis.removeEventListener('keydown', onKeyDown);
|
|
247
|
+
}, [dialogOpen, enabled, onKeyDown]);
|
|
248
|
+
|
|
249
|
+
return null;
|
|
250
|
+
}
|
|
@@ -0,0 +1,273 @@
|
|
|
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 {
|
|
5
|
+
DestinationReference, DialogTemplate, EditingScopeKind, Form, Layout, SceneElement, Screen, ScreenTemplate, UiProfile,
|
|
6
|
+
type ApplicationLogo, type EffectiveConfiguration,
|
|
7
|
+
} from '@cratis/scene.model';
|
|
8
|
+
import {
|
|
9
|
+
DescriptorCatalog, PackageCatalog, resolveEffectiveConfiguration, resolveScreenTemplates, resolveTemplateChain,
|
|
10
|
+
} from '@cratis/scene.engine';
|
|
11
|
+
import { ComponentName } from '../ComponentName';
|
|
12
|
+
import { externalComponent } from '../gallery/elements';
|
|
13
|
+
import { resolveElementComponentNames } from '../gallery/resolveElementNames';
|
|
14
|
+
import { galleryScreenTemplates } from '../gallery/screens';
|
|
15
|
+
import { SlotName, defaultLayouts, shellComponentForLayout } from '../layouts';
|
|
16
|
+
import { ApplicationDocument, completeDocument } from './ApplicationDocument';
|
|
17
|
+
import { defaultApplicationChrome } from './defaultApplicationChrome';
|
|
18
|
+
import { previewTemplateComponent } from './PreviewTemplate';
|
|
19
|
+
import { blueprintPackageCatalog, previewProfile } from './previewProfile';
|
|
20
|
+
|
|
21
|
+
/** What a host decides about how a screen is composed; everything else is the document's. */
|
|
22
|
+
export interface CompositionOptions {
|
|
23
|
+
/** The profile names are resolved with; {@link previewProfile} of the document's profiles when absent. */
|
|
24
|
+
profile?: UiProfile;
|
|
25
|
+
|
|
26
|
+
/** What each package declares; the core and default blueprint packages when absent. */
|
|
27
|
+
catalog?: PackageCatalog;
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* The chrome the shell is given in its slots - topbar, sidebar, menu, breadcrumb, footer. Absent, the shell shows the
|
|
31
|
+
* application's navigation regions (`defaultApplicationChrome`). A full-page shell gets none of it unless the host says so.
|
|
32
|
+
*/
|
|
33
|
+
chrome?(screen: Screen, shell: string): Record<string, SceneElement[]>;
|
|
34
|
+
|
|
35
|
+
/** The element a screen's form becomes; the Cratis `commandForm` component when absent. */
|
|
36
|
+
form?(screenName: string, form: Form): SceneElement;
|
|
37
|
+
|
|
38
|
+
/** Elements placed after the screen's content in the shell's content slot - Stage's status line. */
|
|
39
|
+
trailing?: SceneElement[];
|
|
40
|
+
|
|
41
|
+
/** A last pass over the composed tree before names are resolved - Stage binds data sources here. */
|
|
42
|
+
decorate?(element: SceneElement): SceneElement;
|
|
43
|
+
|
|
44
|
+
/** The templates the blueprint provides, for a screen that names one the document does not declare; the gallery's when absent. */
|
|
45
|
+
blueprintTemplates?: ScreenTemplate[];
|
|
46
|
+
|
|
47
|
+
/** The layouts the blueprint provides; the default blueprint's when absent. */
|
|
48
|
+
blueprintLayouts?: Layout[];
|
|
49
|
+
|
|
50
|
+
/** What each component exposes, so the templates' instance contributions can be resolved; without it no configuration is resolved. */
|
|
51
|
+
descriptors?: DescriptorCatalog;
|
|
52
|
+
|
|
53
|
+
/** The application's logo, shown in the default chrome's topbar. */
|
|
54
|
+
logo?: ApplicationLogo;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** One screen composed for the application shell. */
|
|
58
|
+
export interface ComposedScreen {
|
|
59
|
+
/** The shell with its chrome, the screen's template chain and the screen, names resolved. */
|
|
60
|
+
element: SceneElement;
|
|
61
|
+
|
|
62
|
+
/** What each scope configured on the components the templates contribute; absent when nothing was configured. */
|
|
63
|
+
configuration?: EffectiveConfiguration;
|
|
64
|
+
|
|
65
|
+
/** The templates around the screen, outermost first. */
|
|
66
|
+
templates: string[];
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function previewCatalog(options: CompositionOptions): PackageCatalog {
|
|
70
|
+
return options.catalog ?? blueprintPackageCatalog;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function profileOf(application: ApplicationDocument, options: CompositionOptions): UiProfile {
|
|
74
|
+
return options.profile ?? previewProfile(application.uiProfiles);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function defaultForm(screenName: string, form: Form): SceneElement {
|
|
78
|
+
return externalComponent(`form-${screenName}-${form.name}`, 'commandForm', {
|
|
79
|
+
command: form.forCommand,
|
|
80
|
+
label: form.name,
|
|
81
|
+
generationMode: form.generationMode,
|
|
82
|
+
layout: form.layout,
|
|
83
|
+
fields: form.fields,
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function mergeSlots(...sources: (Record<string, SceneElement[]> | undefined)[]): Record<string, SceneElement[]> {
|
|
88
|
+
const merged: Record<string, SceneElement[]> = {};
|
|
89
|
+
for (const source of sources) {
|
|
90
|
+
for (const [slot, elements] of Object.entries(source ?? {})) merged[slot] = [...(merged[slot] ?? []), ...elements];
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
return merged;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** The layout a screen names: the document's own, or one the blueprint provides. */
|
|
97
|
+
export function findLayout(application: ApplicationDocument, name: string, options: CompositionOptions = {}): Layout | undefined {
|
|
98
|
+
return application.layouts.find(candidate => candidate.name === name)
|
|
99
|
+
?? (options.blueprintLayouts ?? defaultLayouts).find(candidate => candidate.name === name);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* The templates a screen is composed with: the document's own, then the blueprint's for any name the document does not declare.
|
|
104
|
+
* A model that writes `template CrudList` without declaring it is naming one of the blueprint's templates; the document's
|
|
105
|
+
* own declaration always wins the name.
|
|
106
|
+
*/
|
|
107
|
+
export function templatesOf(application: ApplicationDocument, options: CompositionOptions = {}): ScreenTemplate[] {
|
|
108
|
+
const own = application.screenTemplates;
|
|
109
|
+
return [...own, ...(options.blueprintTemplates ?? galleryScreenTemplates).filter(template => !own.some(candidate => candidate.name === template.name))];
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* The templates a screen's template chain is worked out among: the document's own when the screen's template is one of them,
|
|
114
|
+
* otherwise the blueprint's as well. The blueprint's templates are never mixed into a document's own chain - several of them fit
|
|
115
|
+
* the same slot name, which would make every slot ambiguous.
|
|
116
|
+
*/
|
|
117
|
+
export function templatePoolFor(application: ApplicationDocument, screen: Screen, options: CompositionOptions = {}): ScreenTemplate[] {
|
|
118
|
+
return application.screenTemplates.some(candidate => candidate.name === screen.screenTemplate)
|
|
119
|
+
? application.screenTemplates
|
|
120
|
+
: templatesOf(application, options);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/** The slot of the layout a template chain's outermost template fits: its declared slot, or `content`. */
|
|
124
|
+
function layoutSlotOf(layout: Layout | undefined, outermost: ScreenTemplate): string {
|
|
125
|
+
const fits = outermost.fitsSlot;
|
|
126
|
+
if (fits === undefined) return SlotName.Content;
|
|
127
|
+
const slot = fits.includes('.') ? fits.slice(fits.lastIndexOf('.') + 1) : fits;
|
|
128
|
+
return layout === undefined || layout.slots.some(candidate => candidate.name === slot) ? slot : SlotName.Content;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/** The templates a screen's own template sits inside, outermost first, ending with the screen's own template. */
|
|
132
|
+
export function templateChainOf(application: ApplicationDocument, screen: Screen, options: CompositionOptions = {}): ScreenTemplate[] {
|
|
133
|
+
if (!screen.screenTemplate) return [];
|
|
134
|
+
const templates = templatePoolFor(application, screen, options);
|
|
135
|
+
const own = templates.find(candidate => candidate.name === screen.screenTemplate);
|
|
136
|
+
if (!own) return [];
|
|
137
|
+
|
|
138
|
+
const layout = findLayout(application, screen.layout, options);
|
|
139
|
+
const placements = layout ? resolveScreenTemplates(layout, templates).placements : [];
|
|
140
|
+
const chain: ScreenTemplate[] = [own];
|
|
141
|
+
for (let guard = 0; guard <= templates.length; guard++) {
|
|
142
|
+
const placement = placements.find(candidate => candidate.template === chain[0].name);
|
|
143
|
+
if (!placement || placement.container === layout?.name) break;
|
|
144
|
+
const parent = templates.find(candidate => candidate.name === placement.container);
|
|
145
|
+
if (!parent || chain.includes(parent)) break;
|
|
146
|
+
chain.unshift(parent);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
return chain;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* What a screen puts in the shell's content slot: the screen - inside the template chain it names, when it names one - and the
|
|
154
|
+
* forms it hosts. The same content fills the shell on the primary region and a nested outlet.
|
|
155
|
+
*
|
|
156
|
+
* The innermost template takes the screen's own slot content; each template outside it holds the one inside in the slot it
|
|
157
|
+
* declared (`fitsSlot`), so the chain's arrangements all apply.
|
|
158
|
+
*/
|
|
159
|
+
export function screenContent(application: ApplicationDocument, screen: Screen, options: CompositionOptions = {}): { content: SceneElement[]; templates: string[]; slot: string } {
|
|
160
|
+
const form = options.form ?? defaultForm;
|
|
161
|
+
const forms = screen.forms.map(candidate => form(screen.name, candidate));
|
|
162
|
+
const chain = templateChainOf(application, screen, options);
|
|
163
|
+
if (chain.length === 0) {
|
|
164
|
+
return { content: [...Object.values(screen.slotContent).flat(), ...forms], templates: [], slot: SlotName.Content };
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
let inner: SceneElement | undefined;
|
|
168
|
+
for (let index = chain.length - 1; index >= 0; index--) {
|
|
169
|
+
const template = chain[index];
|
|
170
|
+
const slots = index === chain.length - 1
|
|
171
|
+
? mergeSlots(template.content, screen.slotContent, { forms })
|
|
172
|
+
: mergeSlots(template.content, { [slotName(chain[index + 1])]: [inner!] });
|
|
173
|
+
inner = externalComponent(`template-${screen.name}-${template.name}`, previewTemplateComponent, { arrangement: template.arrangement }, slots);
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
const layout = findLayout(application, screen.layout, options);
|
|
177
|
+
return { content: [inner!], templates: chain.map(template => template.name), slot: layoutSlotOf(layout, chain[0]) };
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
function slotName(template: ScreenTemplate): string {
|
|
181
|
+
const fits = template.fitsSlot ?? SlotName.Content;
|
|
182
|
+
return fits.includes('.') ? fits.slice(fits.lastIndexOf('.') + 1) : fits;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
function finish(element: SceneElement, profile: UiProfile, catalog: PackageCatalog, options: CompositionOptions): SceneElement {
|
|
186
|
+
const decorated = options.decorate ? options.decorate(element) : element;
|
|
187
|
+
return resolveElementComponentNames(decorated, profile, catalog);
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* Composes one screen the way the application shows it: the shell for the screen's layout, the chrome every screen shares in the
|
|
192
|
+
* shell's slots, and the screen - inside the template chain it names - in the slot the chain fits. Stage and the preview host
|
|
193
|
+
* both compose through this, so a screen is the same screen in both (spec PRV9, REN2).
|
|
194
|
+
*
|
|
195
|
+
* A layout the selected package set does not provide stays unresolved in the rendered tree: showing that missing component is
|
|
196
|
+
* deliberate, because silently substituting a default shell would hide a package/profile problem.
|
|
197
|
+
*
|
|
198
|
+
* @param application The document and profiles.
|
|
199
|
+
* @param screen The screen to compose.
|
|
200
|
+
* @param options What the host decides.
|
|
201
|
+
* @returns The element to render, the configuration its templates resolved, and the templates around the screen.
|
|
202
|
+
*/
|
|
203
|
+
export function composeApplicationScreen(application: ApplicationDocument, screen: Screen, options: CompositionOptions = {}): ComposedScreen {
|
|
204
|
+
const profile = profileOf(application, options);
|
|
205
|
+
const { content, templates, slot } = screenContent(application, screen, options);
|
|
206
|
+
|
|
207
|
+
const shell = shellComponentForLayout(screen.layout) ?? screen.layout;
|
|
208
|
+
const chrome = options.chrome
|
|
209
|
+
? options.chrome(screen, shell)
|
|
210
|
+
: shell === ComponentName.FullPageShell ? {} : defaultApplicationChrome(options.logo);
|
|
211
|
+
const composed = externalComponent(`screen-${screen.name}`, shell, { screenName: screen.name }, {
|
|
212
|
+
...chrome,
|
|
213
|
+
[slot]: [...(chrome[slot] ?? []), ...content, ...(slot === SlotName.Content ? options.trailing ?? [] : [])],
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
return { element: finish(composed, profile, previewCatalog(options), options), configuration: configurationOf(application, screen, options), templates };
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/**
|
|
220
|
+
* A screen as it appears inside an outlet of another screen: its content alone, without the shell the primary region wraps
|
|
221
|
+
* every screen in, with the same name resolution.
|
|
222
|
+
*/
|
|
223
|
+
export function composeOutletScreen(application: ApplicationDocument, screen: Screen, options: CompositionOptions = {}): SceneElement {
|
|
224
|
+
const { content } = screenContent(application, screen, options);
|
|
225
|
+
const element = content.length === 1 ? content[0] : { id: `outlet-${screen.name}`, children: content } as unknown as SceneElement;
|
|
226
|
+
return finish(element, profileOf(application, options), previewCatalog(options), options);
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* A dialog template as it is shown over the application: its slot content inside the template wrapper, with the arrangement the
|
|
231
|
+
* template wrote, names resolved - the same wrapper a screen's template gets.
|
|
232
|
+
*/
|
|
233
|
+
export function composeDialog(application: ApplicationDocument, template: DialogTemplate, options: CompositionOptions = {}): SceneElement {
|
|
234
|
+
const element = externalComponent(`dialog-${template.name}`, previewTemplateComponent, { arrangement: template.arrangement }, template.content ?? {});
|
|
235
|
+
return finish(element, profileOf(application, options), previewCatalog(options), options);
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
function configurationOf(application: ApplicationDocument, screen: Screen, options: CompositionOptions): EffectiveConfiguration | undefined {
|
|
239
|
+
if (!options.descriptors || (application.instanceContributions ?? []).length === 0) return undefined;
|
|
240
|
+
|
|
241
|
+
const templates = templatesOf(application, options);
|
|
242
|
+
const document = { ...completeDocument(application), screenTemplates: templates, layouts: [...application.layouts, ...(options.blueprintLayouts ?? defaultLayouts).filter(layout => !application.layouts.some(own => own.name === layout.name))] };
|
|
243
|
+
const chain = resolveTemplateChain(document, { kind: EditingScopeKind.Screen, name: screen.name });
|
|
244
|
+
return resolveEffectiveConfiguration(chain, document.instanceContributions, options.descriptors);
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
/**
|
|
248
|
+
* Every destination the document authors - on actions, navigations, contributions and toolbar items - wherever it sits, so a
|
|
249
|
+
* route override on one of them can be deep linked.
|
|
250
|
+
*/
|
|
251
|
+
export function collectDestinations(application: ApplicationDocument): DestinationReference[] {
|
|
252
|
+
const found: DestinationReference[] = [];
|
|
253
|
+
const visit = (value: unknown) => {
|
|
254
|
+
if (value === null || typeof value !== 'object') return;
|
|
255
|
+
if (Array.isArray(value)) {
|
|
256
|
+
value.forEach(visit);
|
|
257
|
+
return;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
for (const [key, entry] of Object.entries(value as Record<string, unknown>)) {
|
|
261
|
+
if (key === 'destination' && entry !== null && typeof entry === 'object' && !Array.isArray(entry)) found.push(entry as DestinationReference);
|
|
262
|
+
visit(entry);
|
|
263
|
+
}
|
|
264
|
+
};
|
|
265
|
+
|
|
266
|
+
for (const screen of application.screens) {
|
|
267
|
+
visit(screen.slotContent);
|
|
268
|
+
visit(screen.contributions);
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
for (const template of [...application.screenTemplates, ...(application.dialogTemplates ?? [])]) visit(template.content);
|
|
272
|
+
return found;
|
|
273
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
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 { ApplicationLogo } from '@cratis/scene.model';
|
|
5
|
+
import type { SceneElement } from '@cratis/scene.model';
|
|
6
|
+
import { ComponentName } from '../ComponentName';
|
|
7
|
+
import { externalComponent } from '../gallery/elements';
|
|
8
|
+
import { SlotName } from '../layouts';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* The chrome the application shell wraps every screen in when the model supplies navigation: a topbar and a sidebar. Neither
|
|
12
|
+
* carries items - each shows the effective navigation of its region (`NavigationRegionView`), so inherit/extend/override
|
|
13
|
+
* applies and nothing here knows the items. The logo is the application's (`navigation.application.logo`).
|
|
14
|
+
*
|
|
15
|
+
* @param logo The application's logo, when it has one.
|
|
16
|
+
* @returns The slot content to give the shell.
|
|
17
|
+
*/
|
|
18
|
+
export function defaultApplicationChrome(logo?: ApplicationLogo): Record<string, SceneElement[]> {
|
|
19
|
+
const slots: Record<string, SceneElement[]> = {};
|
|
20
|
+
if (logo) {
|
|
21
|
+
slots.logo = [externalComponent('topbar-logo', ComponentName.Logo, {
|
|
22
|
+
label: logo.title,
|
|
23
|
+
initials: logo.title.slice(0, 1).toUpperCase(),
|
|
24
|
+
lightImageUrl: logo.image || undefined,
|
|
25
|
+
targetScreen: '',
|
|
26
|
+
})];
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
return {
|
|
30
|
+
[SlotName.Topbar]: [externalComponent('topbar', ComponentName.Topbar, {}, slots)],
|
|
31
|
+
[SlotName.Sidebar]: [externalComponent('sidebar', ComponentName.Sidebar, {})],
|
|
32
|
+
};
|
|
33
|
+
}
|