@cratis/scene.react 4.14.0 → 4.15.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 (43) hide show
  1. package/dist/cjs/index.js +4 -0
  2. package/dist/cjs/index.js.map +1 -1
  3. package/dist/cjs/packages/PackageHostConfiguration.js +18 -2
  4. package/dist/cjs/packages/PackageHostConfiguration.js.map +1 -1
  5. package/dist/cjs/packages/isNetworkAsset.js +18 -0
  6. package/dist/cjs/packages/isNetworkAsset.js.map +1 -0
  7. package/dist/cjs/renderer/RenderBindingScopeContext.js +15 -5
  8. package/dist/cjs/renderer/RenderBindingScopeContext.js.map +1 -1
  9. package/dist/cjs/renderer/useBoundQuery.js +50 -0
  10. package/dist/cjs/renderer/useBoundQuery.js.map +1 -0
  11. package/dist/esm/index.js +2 -0
  12. package/dist/esm/index.js.map +1 -1
  13. package/dist/esm/packages/PackageHostConfiguration.d.ts +2 -0
  14. package/dist/esm/packages/PackageHostConfiguration.d.ts.map +1 -1
  15. package/dist/esm/packages/PackageHostConfiguration.js +19 -3
  16. package/dist/esm/packages/PackageHostConfiguration.js.map +1 -1
  17. package/dist/esm/packages/index.d.ts +1 -0
  18. package/dist/esm/packages/index.d.ts.map +1 -1
  19. package/dist/esm/packages/index.js +1 -0
  20. package/dist/esm/packages/index.js.map +1 -1
  21. package/dist/esm/packages/isNetworkAsset.d.ts +2 -0
  22. package/dist/esm/packages/isNetworkAsset.d.ts.map +1 -0
  23. package/dist/esm/packages/isNetworkAsset.js +16 -0
  24. package/dist/esm/packages/isNetworkAsset.js.map +1 -0
  25. package/dist/esm/renderer/RenderBindingScopeContext.d.ts.map +1 -1
  26. package/dist/esm/renderer/RenderBindingScopeContext.js +17 -7
  27. package/dist/esm/renderer/RenderBindingScopeContext.js.map +1 -1
  28. package/dist/esm/renderer/index.d.ts +1 -0
  29. package/dist/esm/renderer/index.d.ts.map +1 -1
  30. package/dist/esm/renderer/index.js +1 -0
  31. package/dist/esm/renderer/index.js.map +1 -1
  32. package/dist/esm/renderer/useBoundQuery.d.ts +3 -0
  33. package/dist/esm/renderer/useBoundQuery.d.ts.map +1 -0
  34. package/dist/esm/renderer/useBoundQuery.js +48 -0
  35. package/dist/esm/renderer/useBoundQuery.js.map +1 -0
  36. package/dist/esm/tsconfig.tsbuildinfo +1 -1
  37. package/package.json +5 -5
  38. package/packages/PackageHostConfiguration.ts +26 -3
  39. package/packages/index.ts +1 -0
  40. package/packages/isNetworkAsset.ts +15 -0
  41. package/renderer/RenderBindingScopeContext.tsx +12 -8
  42. package/renderer/index.ts +1 -0
  43. package/renderer/useBoundQuery.ts +50 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cratis/scene.react",
3
- "version": "4.14.0",
3
+ "version": "4.15.0",
4
4
  "description": "The React renderer implementing Scene.Engine's Renderer contract against real React/DOM.",
5
5
  "keywords": [
6
6
  "cratis",
@@ -51,12 +51,12 @@
51
51
  "build-storybook": "storybook build"
52
52
  },
53
53
  "dependencies": {
54
- "@cratis/scene.engine": "4.14.0",
55
- "@cratis/scene.model": "4.14.0"
54
+ "@cratis/scene.engine": "4.15.0",
55
+ "@cratis/scene.model": "4.15.0"
56
56
  },
57
57
  "devDependencies": {
58
- "@cratis/scene.engine": "4.14.0",
59
- "@cratis/scene.model": "4.14.0",
58
+ "@cratis/scene.engine": "4.15.0",
59
+ "@cratis/scene.model": "4.15.0",
60
60
  "@storybook/addon-links": "^10.6.1",
61
61
  "@storybook/react": "^10.6.1",
62
62
  "@storybook/react-vite": "^10.6.1",
@@ -2,9 +2,10 @@
2
2
  // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3
3
 
4
4
  import { DialogTemplate, Layout, ScreenTemplate, Theme, UiProfile } from '@cratis/scene.model';
5
- import { TemplateCatalogEntry, describeTemplateCatalog, resolvePackageDependencies } from '@cratis/scene.engine';
5
+ import { TemplateCatalogEntry, describeTemplateCatalog, incompatiblePackages, resolvePackageDependencies } from '@cratis/scene.engine';
6
6
  import { IconAdapterRegistry, createIconAdapterRegistry } from '../icons/IconAdapterRegistry';
7
7
  import { ScenePackageBundle, mergePackageRegistries, validatePackageBundle } from './ScenePackageBundle';
8
+ import { isNetworkAsset } from './isNetworkAsset';
8
9
 
9
10
  export interface PackageHostPolicy {
10
11
  allowExecutableImports: boolean;
@@ -31,6 +32,12 @@ export interface ResolvedPackageHost {
31
32
  dialogTemplates: DialogTemplate[];
32
33
  themes: Theme[];
33
34
 
35
+ /** The layout the UI profile names, from the approved packages. Absent when the profile names none. */
36
+ layout?: Layout;
37
+
38
+ /** The theme the UI profile names, from the approved packages. Absent when the profile names none. */
39
+ theme?: Theme;
40
+
34
41
  /**
35
42
  * Every template the approved packages provide, with compatibility, attribution and license metadata and
36
43
  * whether it can be used with this package set. Template browsers read this rather than the raw lists.
@@ -82,8 +89,9 @@ export function resolvePackageHost(configuration: PackageHostConfiguration): Res
82
89
  }
83
90
 
84
91
  const packageAssets = bundle.manifest.assets ?? [];
85
- if (!configuration.policy.allowNetworkAssets && packageAssets.some(asset => /^https?:\/\//.test(asset))) {
86
- diagnostics.push(`Package '${packageName}' declares network assets, but host policy forbids them`);
92
+ const networkAssets = packageAssets.filter(isNetworkAsset);
93
+ if (!configuration.policy.allowNetworkAssets && networkAssets.length > 0) {
94
+ diagnostics.push(`Package '${packageName}' declares network assets (${networkAssets.join(', ')}), but host policy forbids them`);
87
95
  }
88
96
  assets.push(...packageAssets);
89
97
 
@@ -98,6 +106,19 @@ export function resolvePackageHost(configuration: PackageHostConfiguration): Res
98
106
  bundles.push(bundle);
99
107
  }
100
108
 
109
+ const layouts = bundles.flatMap(bundle => bundle.layouts ?? []);
110
+ const themes = bundles.flatMap(bundle => bundle.themes ?? []);
111
+ const layout = configuration.profile.layout ? layouts.find(candidate => candidate.name === configuration.profile.layout) : undefined;
112
+ const theme = configuration.profile.theme ? themes.find(candidate => candidate.name === configuration.profile.theme) : undefined;
113
+ if (configuration.profile.layout && !layout) {
114
+ diagnostics.push(`UI profile '${configuration.profile.name}' uses layout '${configuration.profile.layout}', which no approved package provides`);
115
+ }
116
+ if (configuration.profile.theme && !theme) {
117
+ diagnostics.push(`UI profile '${configuration.profile.name}' uses theme '${configuration.profile.theme}', which no approved package provides`);
118
+ } else if (theme && incompatiblePackages(theme, configuration.profile).length > 0) {
119
+ diagnostics.push(`Theme '${theme.name}' is not compatible with ${incompatiblePackages(theme, configuration.profile).join(', ')} in UI profile '${configuration.profile.name}'`);
120
+ }
121
+
101
122
  const blocked = diagnostics.length > 0;
102
123
  const approved = blocked ? [] : bundles;
103
124
  const approvedAssets = blocked ? [] : assets;
@@ -109,6 +130,8 @@ export function resolvePackageHost(configuration: PackageHostConfiguration): Res
109
130
  screenTemplates: approved.flatMap(bundle => bundle.screenTemplates ?? []),
110
131
  dialogTemplates: approved.flatMap(bundle => bundle.dialogTemplates ?? []),
111
132
  themes: approved.flatMap(bundle => bundle.themes ?? []),
133
+ layout: blocked ? undefined : layout,
134
+ theme: blocked ? undefined : theme,
112
135
  templates: describeTemplateCatalog(approved, configuration.sceneVersion),
113
136
  icons: createIconAdapterRegistry(approved.map(bundle => bundle.iconLibrary?.adapter).filter(adapter => adapter !== undefined)),
114
137
  assets: approvedAssets,
package/packages/index.ts CHANGED
@@ -16,6 +16,7 @@ export * from './DesignTimeResolution';
16
16
  export * from '../navigation';
17
17
  export * from './EmbeddedPackageHost';
18
18
  export * from './loadDesignTimeContributions';
19
+ export * from './isNetworkAsset';
19
20
  export * from './PackageHostAssets';
20
21
  export * from './PackageHostConfiguration';
21
22
  export * from './PackageHostRenderMode';
@@ -0,0 +1,15 @@
1
+ // Copyright (c) Cratis. All rights reserved.
2
+ // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3
+
4
+ /**
5
+ * Whether loading an asset reaches beyond the host: an absolute URL with any scheme other than `data:`
6
+ * or `blob:` (`https://`, `http://`, `ws://`, `ftp://`), or a protocol-relative `//host/...` reference. A
7
+ * package-relative path or a bare module specifier such as `@cratis/components/styles` is resolved by the
8
+ * host from what it bundled, so it is not a network asset.
9
+ */
10
+ export function isNetworkAsset(asset: string): boolean {
11
+ const trimmed = asset.trim();
12
+ if (trimmed.startsWith('//')) return true;
13
+ const scheme = /^([a-zA-Z][a-zA-Z0-9+.-]*):/.exec(trimmed)?.[1]?.toLowerCase();
14
+ return scheme !== undefined && scheme !== 'data' && scheme !== 'blob';
15
+ }
@@ -2,7 +2,7 @@
2
2
  // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3
3
 
4
4
  import { ReactNode, createContext, useCallback, useContext, useMemo, useRef } from 'react';
5
- import { BindingResolver, BindingScope, resolveBindingExpression } from '@cratis/scene.engine';
5
+ import { BindingResolver, BindingScope, nestBindingScope, resolveBindingExpression } from '@cratis/scene.engine';
6
6
  import { BindingExpression, BindingNullBehavior } from '@cratis/scene.model';
7
7
  import { BindingOutputProvider, useBindingOutputScope } from './BindingOutputContext';
8
8
 
@@ -24,10 +24,15 @@ export interface RenderBindingScopeProviderProps {
24
24
 
25
25
  /**
26
26
  * Provides the inherited reactive binding scope for a rendered Scene tree.
27
+ *
28
+ * The outermost provider owns the component output scope. A provider rendered inside another - a nested
29
+ * template or region - nests its own data context, query results and component outputs over the inherited
30
+ * scope with `nestBindingScope`, so a detail region bound to the selected row reads that row while still
31
+ * seeing the screen's queries and every component's outputs.
27
32
  */
28
33
  export function RenderBindingScopeProvider(props: RenderBindingScopeProviderProps) {
29
34
  const current = useContext(Context);
30
- if (current) return <>{props.children}</>;
35
+ if (current) return <RenderBindingScopeInner {...props} parent={current.scope} />;
31
36
 
32
37
  return (
33
38
  <BindingOutputProvider onOutputsChanged={props.onComponentOutputsChanged}>
@@ -36,14 +41,13 @@ export function RenderBindingScopeProvider(props: RenderBindingScopeProviderProp
36
41
  );
37
42
  }
38
43
 
39
- function RenderBindingScopeInner({ children, dataContext, queryResults, componentOutputs, resolveBinding }: RenderBindingScopeProviderProps) {
44
+ function RenderBindingScopeInner({ children, dataContext, queryResults, componentOutputs, resolveBinding, parent }: RenderBindingScopeProviderProps & { parent?: BindingScope }) {
40
45
  const outputs = useBindingOutputScope();
41
46
  const previousValues = useRef(new Map<string, unknown>());
42
- const scope = useMemo<BindingScope>(() => ({
43
- dataContext,
44
- queryResults,
45
- componentOutputs: { ...(componentOutputs ?? {}), ...outputs },
46
- }), [componentOutputs, dataContext, outputs, queryResults]);
47
+ const scope = useMemo<BindingScope>(() => {
48
+ const own = { dataContext, queryResults, componentOutputs: { ...(componentOutputs ?? {}), ...outputs } };
49
+ return parent ? nestBindingScope(parent, own) : own;
50
+ }, [componentOutputs, dataContext, outputs, parent, queryResults]);
47
51
 
48
52
  const resolver = useCallback<BindingResolver>((binding: BindingExpression) => {
49
53
  if (!binding.kind && resolveBinding) return resolveBinding(binding);
package/renderer/index.ts CHANGED
@@ -6,5 +6,6 @@ export * from './BindingOutputContext';
6
6
  export * from './ComponentRegistry';
7
7
  export * from './RenderBindingScopeContext';
8
8
  export * from './UnresolvedComponent';
9
+ export * from './useBoundQuery';
9
10
  export * from './createReactRenderer';
10
11
  export * from './InteractiveComponent';
@@ -0,0 +1,50 @@
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 { useEffect, useMemo, useRef, useState } from 'react';
5
+ import { QueryBindingState } from '@cratis/scene.engine';
6
+ import { QueryPerformer } from './AbortableQueryRunner';
7
+
8
+ /**
9
+ * Runs a bound query whenever its arguments change and returns the accepted query results.
10
+ *
11
+ * Each change of arguments rebinds the query: the previous run is aborted and, should its answer still
12
+ * arrive, discarded by `QueryBindingState` - a slow result for a previous selection never replaces the
13
+ * current one. Arguments of `undefined` mean the binding is cleared, as when a selection is cleared, so the
14
+ * result is removed, and so is it when the current run fails - a failed rebind must not leave the previous
15
+ * selection's data on screen. The results are keyed by `query`, ready to pass as a scope's `queryResults`.
16
+ */
17
+ export function useBoundQuery(query: string, argumentsValue: unknown, perform: QueryPerformer): Record<string, unknown> {
18
+ const state = useMemo(() => new QueryBindingState(), []);
19
+ const [results, setResults] = useState<Record<string, unknown>>({});
20
+ const key = argumentsValue === undefined ? undefined : JSON.stringify(argumentsValue);
21
+ const latestArguments = useRef(argumentsValue);
22
+ latestArguments.current = argumentsValue;
23
+
24
+ // The arguments are compared by value through `key`; the effect reads them from `latestArguments`.
25
+ useEffect(() => {
26
+ const argumentsForRun = latestArguments.current;
27
+ if (argumentsForRun === undefined) {
28
+ state.clear(query);
29
+ setResults(state.queryResults);
30
+ return undefined;
31
+ }
32
+
33
+ const controller = new AbortController();
34
+ const ticket = state.begin(query);
35
+ perform(query, argumentsForRun, controller.signal).then(
36
+ result => {
37
+ if (state.complete(ticket, result)) setResults(state.queryResults);
38
+ },
39
+ () => {
40
+ if (controller.signal.aborted || !state.complete(ticket, undefined)) return;
41
+ state.clear(query);
42
+ setResults(state.queryResults);
43
+ },
44
+ );
45
+
46
+ return () => controller.abort();
47
+ }, [key, perform, query, state]);
48
+
49
+ return results;
50
+ }