@webstudio-is/react-sdk 0.40.0 → 0.42.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.
@@ -28,7 +28,7 @@ var import_components = require("../components");
28
28
  var import_custom_components = require("../app/custom-components");
29
29
  var import_params = require("../app/params");
30
30
  var import_props = require("../props");
31
- const denormalizeTree = (instances) => {
31
+ const denormalizeTree = (instances, rootInstanceId) => {
32
32
  const convertTree = (instance) => {
33
33
  const legacyInstance = {
34
34
  type: "instance",
@@ -48,7 +48,11 @@ const denormalizeTree = (instances) => {
48
48
  }
49
49
  return legacyInstance;
50
50
  };
51
- return convertTree(Array.from(instances.values())[0]);
51
+ const rootInstance = instances.get(rootInstanceId);
52
+ if (rootInstance === void 0) {
53
+ return void 0;
54
+ }
55
+ return convertTree(rootInstance);
52
56
  };
53
57
  const InstanceRoot = ({
54
58
  data,
@@ -56,15 +60,19 @@ const InstanceRoot = ({
56
60
  customComponents = import_custom_components.customComponents,
57
61
  getComponent
58
62
  }) => {
59
- if (data.tree === null) {
60
- throw new Error("Tree is null");
61
- }
62
63
  (0, import_params.setParams)(data.params ?? null);
63
64
  (0, import_components.registerComponents)(customComponents);
65
+ const instance = denormalizeTree(
66
+ new Map(data.build.instances),
67
+ data.page.rootInstanceId
68
+ );
69
+ if (instance === void 0) {
70
+ return null;
71
+ }
64
72
  return (0, import_create_elements_tree.createElementsTree)({
65
- instance: denormalizeTree(new Map(data.tree.instances)),
73
+ instance,
66
74
  propsByInstanceIdStore: (0, import_nanostores.atom)(
67
- (0, import_props.getPropsByInstanceId)(new Map(data.tree.props))
75
+ (0, import_props.getPropsByInstanceId)(new Map(data.build.props))
68
76
  ),
69
77
  assetsStore: (0, import_nanostores.atom)(new Map(data.assets.map((asset) => [asset.id, asset]))),
70
78
  Component: Component ?? import_webstudio_component.WebstudioComponent,
package/lib/tree/root.js CHANGED
@@ -5,7 +5,7 @@ import { registerComponents } from "../components";
5
5
  import { customComponents as defaultCustomComponents } from "../app/custom-components";
6
6
  import { setParams } from "../app/params";
7
7
  import { getPropsByInstanceId } from "../props";
8
- const denormalizeTree = (instances) => {
8
+ const denormalizeTree = (instances, rootInstanceId) => {
9
9
  const convertTree = (instance) => {
10
10
  const legacyInstance = {
11
11
  type: "instance",
@@ -25,7 +25,11 @@ const denormalizeTree = (instances) => {
25
25
  }
26
26
  return legacyInstance;
27
27
  };
28
- return convertTree(Array.from(instances.values())[0]);
28
+ const rootInstance = instances.get(rootInstanceId);
29
+ if (rootInstance === void 0) {
30
+ return void 0;
31
+ }
32
+ return convertTree(rootInstance);
29
33
  };
30
34
  const InstanceRoot = ({
31
35
  data,
@@ -33,15 +37,19 @@ const InstanceRoot = ({
33
37
  customComponents = defaultCustomComponents,
34
38
  getComponent
35
39
  }) => {
36
- if (data.tree === null) {
37
- throw new Error("Tree is null");
38
- }
39
40
  setParams(data.params ?? null);
40
41
  registerComponents(customComponents);
42
+ const instance = denormalizeTree(
43
+ new Map(data.build.instances),
44
+ data.page.rootInstanceId
45
+ );
46
+ if (instance === void 0) {
47
+ return null;
48
+ }
41
49
  return createElementsTree({
42
- instance: denormalizeTree(new Map(data.tree.instances)),
50
+ instance,
43
51
  propsByInstanceIdStore: atom(
44
- getPropsByInstanceId(new Map(data.tree.props))
52
+ getPropsByInstanceId(new Map(data.build.props))
45
53
  ),
46
54
  assetsStore: atom(new Map(data.assets.map((asset) => [asset.id, asset]))),
47
55
  Component: Component ?? WebstudioComponent,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@webstudio-is/react-sdk",
3
- "version": "0.40.0",
3
+ "version": "0.42.0",
4
4
  "description": "Webstudio JavaScript / TypeScript API",
5
5
  "author": "Webstudio <github@webstudio.is>",
6
6
  "homepage": "https://webstudio.is",
@@ -46,14 +46,14 @@
46
46
  "mitt": "^3.0.0",
47
47
  "nanostores": "^0.7.1",
48
48
  "warn-once": "^0.1.1",
49
- "@webstudio-is/asset-uploader": "^0.40.0",
50
- "@webstudio-is/css-data": "^0.40.0",
51
- "@webstudio-is/css-vars": "^0.40.0",
52
- "@webstudio-is/generate-arg-types": "^0.40.0",
53
- "@webstudio-is/icons": "^0.40.0",
54
- "@webstudio-is/image": "^0.40.0",
55
- "@webstudio-is/prisma-client": "^0.40.0",
56
- "@webstudio-is/project-build": "^0.40.0"
49
+ "@webstudio-is/asset-uploader": "^0.42.0",
50
+ "@webstudio-is/css-data": "^0.42.0",
51
+ "@webstudio-is/css-vars": "^0.42.0",
52
+ "@webstudio-is/generate-arg-types": "^0.42.0",
53
+ "@webstudio-is/icons": "^0.42.0",
54
+ "@webstudio-is/image": "^0.42.0",
55
+ "@webstudio-is/prisma-client": "^0.42.0",
56
+ "@webstudio-is/project-build": "^0.42.0"
57
57
  },
58
58
  "exports": {
59
59
  ".": {
package/src/tree/root.ts CHANGED
@@ -1,10 +1,11 @@
1
1
  import type { ComponentProps } from "react";
2
2
  import { atom } from "nanostores";
3
3
  import type {
4
+ Build,
4
5
  Instance,
5
6
  Instances,
6
7
  InstancesItem,
7
- Tree,
8
+ Page,
8
9
  } from "@webstudio-is/project-build";
9
10
  import type { Asset } from "@webstudio-is/asset-uploader";
10
11
  import { createElementsTree } from "./create-elements-tree";
@@ -16,7 +17,8 @@ import { getPropsByInstanceId } from "../props";
16
17
  import type { GetComponent } from "../components/components-utils";
17
18
 
18
19
  export type Data = {
19
- tree: Tree | null;
20
+ page: Page;
21
+ build: Build;
20
22
  assets: Array<Asset>;
21
23
  params?: Params;
22
24
  };
@@ -28,7 +30,10 @@ type RootProps = {
28
30
  getComponent: GetComponent;
29
31
  };
30
32
 
31
- const denormalizeTree = (instances: Instances) => {
33
+ const denormalizeTree = (
34
+ instances: Instances,
35
+ rootInstanceId: Instance["id"]
36
+ ) => {
32
37
  const convertTree = (instance: InstancesItem) => {
33
38
  const legacyInstance: Instance = {
34
39
  type: "instance",
@@ -48,7 +53,11 @@ const denormalizeTree = (instances: Instances) => {
48
53
  }
49
54
  return legacyInstance;
50
55
  };
51
- return convertTree(Array.from(instances.values())[0]);
56
+ const rootInstance = instances.get(rootInstanceId);
57
+ if (rootInstance === undefined) {
58
+ return undefined;
59
+ }
60
+ return convertTree(rootInstance);
52
61
  };
53
62
 
54
63
  export const InstanceRoot = ({
@@ -57,18 +66,20 @@ export const InstanceRoot = ({
57
66
  customComponents = defaultCustomComponents,
58
67
  getComponent,
59
68
  }: RootProps): JSX.Element | null => {
60
- if (data.tree === null) {
61
- throw new Error("Tree is null");
62
- }
63
-
64
69
  setParams(data.params ?? null);
65
70
 
66
71
  registerComponents(customComponents);
67
-
72
+ const instance = denormalizeTree(
73
+ new Map(data.build.instances),
74
+ data.page.rootInstanceId
75
+ );
76
+ if (instance === undefined) {
77
+ return null;
78
+ }
68
79
  return createElementsTree({
69
- instance: denormalizeTree(new Map(data.tree.instances)),
80
+ instance,
70
81
  propsByInstanceIdStore: atom(
71
- getPropsByInstanceId(new Map(data.tree.props))
82
+ getPropsByInstanceId(new Map(data.build.props))
72
83
  ),
73
84
  assetsStore: atom(new Map(data.assets.map((asset) => [asset.id, asset]))),
74
85
  Component: Component ?? WebstudioComponent,