@cratis/scene.react 4.15.0 → 4.16.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 (47) hide show
  1. package/core/ScreenplayCoreComponents.tsx +24 -3
  2. package/dist/cjs/SceneElementView.js +1 -0
  3. package/dist/cjs/SceneElementView.js.map +1 -1
  4. package/dist/cjs/core/ScreenplayCoreComponents.js +25 -5
  5. package/dist/cjs/core/ScreenplayCoreComponents.js.map +1 -1
  6. package/dist/cjs/index.js +7 -0
  7. package/dist/cjs/index.js.map +1 -1
  8. package/dist/cjs/navigation/createHashSceneHistory.js +43 -0
  9. package/dist/cjs/navigation/createHashSceneHistory.js.map +1 -0
  10. package/dist/cjs/renderer/useValueOutput.js +31 -0
  11. package/dist/cjs/renderer/useValueOutput.js.map +1 -0
  12. package/dist/cjs/renderer/valueOutputProperty.js +28 -0
  13. package/dist/cjs/renderer/valueOutputProperty.js.map +1 -0
  14. package/dist/esm/SceneElementView.js +1 -0
  15. package/dist/esm/SceneElementView.js.map +1 -1
  16. package/dist/esm/core/ScreenplayCoreComponents.d.ts.map +1 -1
  17. package/dist/esm/core/ScreenplayCoreComponents.js +25 -5
  18. package/dist/esm/core/ScreenplayCoreComponents.js.map +1 -1
  19. package/dist/esm/index.js +3 -0
  20. package/dist/esm/index.js.map +1 -1
  21. package/dist/esm/navigation/createHashSceneHistory.d.ts +4 -0
  22. package/dist/esm/navigation/createHashSceneHistory.d.ts.map +1 -0
  23. package/dist/esm/navigation/createHashSceneHistory.js +41 -0
  24. package/dist/esm/navigation/createHashSceneHistory.js.map +1 -0
  25. package/dist/esm/navigation/index.d.ts +1 -0
  26. package/dist/esm/navigation/index.d.ts.map +1 -1
  27. package/dist/esm/navigation/index.js +1 -0
  28. package/dist/esm/navigation/index.js.map +1 -1
  29. package/dist/esm/renderer/index.d.ts +2 -0
  30. package/dist/esm/renderer/index.d.ts.map +1 -1
  31. package/dist/esm/renderer/index.js +2 -0
  32. package/dist/esm/renderer/index.js.map +1 -1
  33. package/dist/esm/renderer/useValueOutput.d.ts +4 -0
  34. package/dist/esm/renderer/useValueOutput.d.ts.map +1 -0
  35. package/dist/esm/renderer/useValueOutput.js +28 -0
  36. package/dist/esm/renderer/useValueOutput.js.map +1 -0
  37. package/dist/esm/renderer/valueOutputProperty.d.ts +3 -0
  38. package/dist/esm/renderer/valueOutputProperty.d.ts.map +1 -0
  39. package/dist/esm/renderer/valueOutputProperty.js +26 -0
  40. package/dist/esm/renderer/valueOutputProperty.js.map +1 -0
  41. package/dist/esm/tsconfig.tsbuildinfo +1 -1
  42. package/navigation/createHashSceneHistory.ts +42 -0
  43. package/navigation/index.ts +1 -0
  44. package/package.json +5 -5
  45. package/renderer/index.ts +2 -0
  46. package/renderer/useValueOutput.ts +28 -0
  47. package/renderer/valueOutputProperty.ts +25 -0
@@ -0,0 +1,42 @@
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 { SceneBrowserWindow } from './createBrowserSceneHistory';
5
+ import { SceneHistory } from './SceneHistory';
6
+ import { SceneNavigationState } from './SceneNavigationState';
7
+
8
+ const stateKey = 'sceneNavigation';
9
+
10
+ /**
11
+ * A history kept in the URL fragment - `index.html#/WorkItemDetails?workItemId=B` - for hosts that are served
12
+ * as a single file or from a path they do not control, such as a generated runtime or a webview. It behaves
13
+ * like `createBrowserSceneHistory`: entries are pushed with their navigation state, back and forward restore
14
+ * them, and a typed or bookmarked fragment is matched by URL alone. A fragment changed by hand, which fires
15
+ * `hashchange` rather than `popstate`, is reported the same way.
16
+ */
17
+ export function createHashSceneHistory(browser: SceneBrowserWindow = window): SceneHistory {
18
+ const location = () => {
19
+ const fragment = browser.location.hash.replace(/^#\/?/, '');
20
+ return fragment.length ? fragment : undefined;
21
+ };
22
+ const href = (url: string | undefined) => `${browser.location.pathname}${browser.location.search}#/${url ?? ''}`;
23
+ const stored = (value: unknown) => (value as Record<string, SceneNavigationState> | null)?.[stateKey];
24
+
25
+ return {
26
+ location,
27
+ state: () => stored(browser.history.state),
28
+ push: state => browser.history.pushState({ [stateKey]: state }, '', href(state.url)),
29
+ replace: state => browser.history.replaceState({ [stateKey]: state }, '', href(state.url)),
30
+ back: () => browser.history.back(),
31
+ listen: listener => {
32
+ const onPopState = (event: PopStateEvent) => listener(location(), stored(event.state));
33
+ const onHashChange = () => listener(location(), stored(browser.history.state));
34
+ browser.addEventListener('popstate', onPopState);
35
+ browser.addEventListener('hashchange', onHashChange);
36
+ return () => {
37
+ browser.removeEventListener('popstate', onPopState);
38
+ browser.removeEventListener('hashchange', onHashChange);
39
+ };
40
+ },
41
+ };
42
+ }
@@ -9,6 +9,7 @@ export * from './SceneNavigationHost';
9
9
  export * from './SceneNavigationState';
10
10
  export * from './SceneRoute';
11
11
  export * from './createBrowserSceneHistory';
12
+ export * from './createHashSceneHistory';
12
13
  export * from './createMemorySceneHistory';
13
14
  export * from './diagnoseRenderedNavigation';
14
15
  export * from './identityRoute';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cratis/scene.react",
3
- "version": "4.15.0",
3
+ "version": "4.16.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.15.0",
55
- "@cratis/scene.model": "4.15.0"
54
+ "@cratis/scene.engine": "4.16.0",
55
+ "@cratis/scene.model": "4.16.0"
56
56
  },
57
57
  "devDependencies": {
58
- "@cratis/scene.engine": "4.15.0",
59
- "@cratis/scene.model": "4.15.0",
58
+ "@cratis/scene.engine": "4.16.0",
59
+ "@cratis/scene.model": "4.16.0",
60
60
  "@storybook/addon-links": "^10.6.1",
61
61
  "@storybook/react": "^10.6.1",
62
62
  "@storybook/react-vite": "^10.6.1",
package/renderer/index.ts CHANGED
@@ -7,5 +7,7 @@ export * from './ComponentRegistry';
7
7
  export * from './RenderBindingScopeContext';
8
8
  export * from './UnresolvedComponent';
9
9
  export * from './useBoundQuery';
10
+ export * from './useValueOutput';
11
+ export * from './valueOutputProperty';
10
12
  export * from './createReactRenderer';
11
13
  export * from './InteractiveComponent';
@@ -0,0 +1,28 @@
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, useRef } from 'react';
5
+ import { ComponentBindingOutputs } from './BindingOutputContext';
6
+
7
+ /** The output property an input widget publishes its current value under. */
8
+ export const valueOutputName = 'value';
9
+
10
+ /**
11
+ * Publishes an input widget's current value as its `value` output: once when it mounts, with the initial
12
+ * value, and again on every change. Other elements bind to it with a `componentProperty` binding, and a
13
+ * command action maps it as `component.<id>.value`. The renderer clears the output when the widget unmounts.
14
+ *
15
+ * Values are published as JSON-compatible data so both engines read the same thing: a `Date` is published
16
+ * as its ISO 8601 string, and `undefined` as `null`.
17
+ */
18
+ export function useValueOutput(bindingOutputs: ComponentBindingOutputs | undefined, value: unknown): void {
19
+ const published = value instanceof Date ? value.toISOString() : value ?? null;
20
+ const key = JSON.stringify(published);
21
+ const latest = useRef(published);
22
+ latest.current = published;
23
+
24
+ // Published by value through `key`: a new array or object with the same content is not a change.
25
+ useEffect(() => {
26
+ bindingOutputs?.setOutput(valueOutputName, latest.current);
27
+ }, [bindingOutputs, key]);
28
+ }
@@ -0,0 +1,25 @@
1
+ // Copyright (c) Cratis. All rights reserved.
2
+ // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3
+
4
+ import { BindingMode, PropertyDescriptor, PropertyValueType } from '@cratis/scene.model';
5
+ import { valueOutputName } from './useValueOutput';
6
+
7
+ /**
8
+ * The descriptor entry an input widget declares for the value it publishes with `useValueOutput`, so a
9
+ * designer can offer it as a binding source - for a detail component, a query argument or a command input.
10
+ *
11
+ * @param valueType The type of value the widget publishes.
12
+ * @param description What the value is, when the default sentence does not say enough.
13
+ */
14
+ export function valueOutputProperty(valueType: PropertyValueType, description = 'What the user has entered, published as the input changes.'): PropertyDescriptor {
15
+ return {
16
+ path: valueOutputName,
17
+ label: 'Value',
18
+ group: 'Outputs',
19
+ valueType,
20
+ readOnly: true,
21
+ output: true,
22
+ bindingMode: BindingMode.OneWay,
23
+ description,
24
+ };
25
+ }