@webstudio-is/react-sdk 0.26.0 → 0.28.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.
@@ -0,0 +1,29 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+ var context_exports = {};
20
+ __export(context_exports, {
21
+ ReactSdkContext: () => ReactSdkContext
22
+ });
23
+ module.exports = __toCommonJS(context_exports);
24
+ var import_nanostores = require("nanostores");
25
+ var import_react = require("react");
26
+ const ReactSdkContext = (0, import_react.createContext)({
27
+ propsByInstanceIdStore: (0, import_nanostores.atom)(/* @__PURE__ */ new Map()),
28
+ assetsStore: (0, import_nanostores.atom)(/* @__PURE__ */ new Map())
29
+ });
package/lib/cjs/index.cjs CHANGED
@@ -20,8 +20,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
20
20
  var src_exports = {};
21
21
  __export(src_exports, {
22
22
  customComponents: () => import_custom_components.customComponents,
23
- customComponentsMeta: () => import_custom_components.customComponentsMeta,
24
- setPropsByInstanceIdStore: () => import_props.setPropsByInstanceIdStore
23
+ customComponentsMeta: () => import_custom_components.customComponentsMeta
25
24
  });
26
25
  module.exports = __toCommonJS(src_exports);
27
26
  __reExport(src_exports, require("./css"), module.exports);
@@ -30,4 +29,3 @@ __reExport(src_exports, require("./components"), module.exports);
30
29
  __reExport(src_exports, require("./pubsub"), module.exports);
31
30
  __reExport(src_exports, require("./app"), module.exports);
32
31
  var import_custom_components = require("./app/custom-components");
33
- var import_props = require("./props");
package/lib/cjs/props.cjs CHANGED
@@ -19,8 +19,6 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
19
19
  var props_exports = {};
20
20
  __export(props_exports, {
21
21
  getPropsByInstanceId: () => getPropsByInstanceId,
22
- getPropsByInstanceIdStore: () => getPropsByInstanceIdStore,
23
- setPropsByInstanceIdStore: () => setPropsByInstanceIdStore,
24
22
  useInstanceProps: () => useInstanceProps,
25
23
  usePropAsset: () => usePropAsset
26
24
  });
@@ -28,13 +26,7 @@ module.exports = __toCommonJS(props_exports);
28
26
  var import_react = require("react");
29
27
  var import_nanostores = require("nanostores");
30
28
  var import_react2 = require("@nanostores/react");
31
- let propsByInstanceIdStore = (0, import_nanostores.atom)(/* @__PURE__ */ new Map());
32
- const setPropsByInstanceIdStore = (store) => {
33
- propsByInstanceIdStore = store;
34
- };
35
- const getPropsByInstanceIdStore = () => {
36
- return propsByInstanceIdStore;
37
- };
29
+ var import_context = require("./context");
38
30
  const getPropsByInstanceId = (props) => {
39
31
  const propsByInstanceId = /* @__PURE__ */ new Map();
40
32
  for (const prop of props.values()) {
@@ -48,6 +40,7 @@ const getPropsByInstanceId = (props) => {
48
40
  return propsByInstanceId;
49
41
  };
50
42
  const useInstanceProps = (instanceId) => {
43
+ const { propsByInstanceIdStore } = (0, import_react.useContext)(import_context.ReactSdkContext);
51
44
  const propsByInstanceId = (0, import_react2.useStore)(propsByInstanceIdStore);
52
45
  const instanceProps = propsByInstanceId.get(instanceId);
53
46
  const instancePropsObject = {};
@@ -61,20 +54,24 @@ const useInstanceProps = (instanceId) => {
61
54
  return instancePropsObject;
62
55
  };
63
56
  const usePropAsset = (instanceId, name) => {
57
+ const { propsByInstanceIdStore, assetsStore } = (0, import_react.useContext)(import_context.ReactSdkContext);
64
58
  const assetStore = (0, import_react.useMemo)(() => {
65
- return (0, import_nanostores.computed)(propsByInstanceIdStore, (propsByInstanceId) => {
66
- const instanceProps = propsByInstanceId.get(instanceId);
67
- let asset2 = void 0;
68
- if (instanceProps) {
59
+ return (0, import_nanostores.computed)(
60
+ [propsByInstanceIdStore, assetsStore],
61
+ (propsByInstanceId, assets) => {
62
+ const instanceProps = propsByInstanceId.get(instanceId);
63
+ if (instanceProps === void 0) {
64
+ return void 0;
65
+ }
69
66
  for (const prop of instanceProps) {
70
67
  if (prop.type === "asset" && prop.name === name) {
71
- asset2 = prop.value;
68
+ const assetId = prop.value;
69
+ return assets.get(assetId);
72
70
  }
73
71
  }
74
72
  }
75
- return asset2;
76
- });
77
- }, [instanceId, name]);
73
+ );
74
+ }, [propsByInstanceIdStore, assetsStore, instanceId, name]);
78
75
  const asset = (0, import_react2.useStore)(assetStore);
79
76
  return asset;
80
77
  };
@@ -24,10 +24,13 @@ module.exports = __toCommonJS(create_elements_tree_exports);
24
24
  var import_jsx_runtime = require("react/jsx-runtime");
25
25
  var import_react = require("react");
26
26
  var import_react2 = require("@remix-run/react");
27
+ var import_context = require("../context");
27
28
  var import_session_storage_polyfill = require("./session-storage-polyfill");
28
29
  const createElementsTree = ({
29
30
  sandbox,
30
31
  instance,
32
+ propsByInstanceIdStore,
33
+ assetsStore,
31
34
  Component,
32
35
  onChangeChildren
33
36
  }) => {
@@ -50,7 +53,10 @@ const createElementsTree = ({
50
53
  }, "children")
51
54
  ]
52
55
  });
53
- return body;
56
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_context.ReactSdkContext.Provider, {
57
+ value: { propsByInstanceIdStore, assetsStore },
58
+ children: body
59
+ });
54
60
  };
55
61
  const createInstanceChildrenElements = ({
56
62
  children,
@@ -36,13 +36,14 @@ const InstanceRoot = ({
36
36
  if (data.tree === null) {
37
37
  throw new Error("Tree is null");
38
38
  }
39
- (0, import_props.setPropsByInstanceIdStore)(
40
- (0, import_nanostores.atom)((0, import_props.getPropsByInstanceId)(new Map(data.tree.props)))
41
- );
42
39
  (0, import_params.setParams)(data.params ?? null);
43
40
  (0, import_components.registerComponents)(customComponents);
44
41
  return (0, import_create_elements_tree.createElementsTree)({
45
42
  instance: data.tree.root,
43
+ propsByInstanceIdStore: (0, import_nanostores.atom)(
44
+ (0, import_props.getPropsByInstanceId)(new Map(data.tree.props))
45
+ ),
46
+ assetsStore: (0, import_nanostores.atom)(new Map(data.assets.map((asset) => [asset.id, asset]))),
46
47
  Component: Component ?? import_wrapper_component.WrapperComponent
47
48
  });
48
49
  };
package/lib/context.js ADDED
@@ -0,0 +1,9 @@
1
+ import { atom } from "nanostores";
2
+ import { createContext } from "react";
3
+ const ReactSdkContext = createContext({
4
+ propsByInstanceIdStore: atom(/* @__PURE__ */ new Map()),
5
+ assetsStore: atom(/* @__PURE__ */ new Map())
6
+ });
7
+ export {
8
+ ReactSdkContext
9
+ };
package/lib/index.js CHANGED
@@ -7,9 +7,7 @@ import {
7
7
  customComponents,
8
8
  customComponentsMeta
9
9
  } from "./app/custom-components";
10
- import { setPropsByInstanceIdStore } from "./props";
11
10
  export {
12
11
  customComponents,
13
- customComponentsMeta,
14
- setPropsByInstanceIdStore
12
+ customComponentsMeta
15
13
  };
package/lib/props.js CHANGED
@@ -1,13 +1,7 @@
1
- import { useMemo } from "react";
2
- import { atom, computed } from "nanostores";
1
+ import { useContext, useMemo } from "react";
2
+ import { computed } from "nanostores";
3
3
  import { useStore } from "@nanostores/react";
4
- let propsByInstanceIdStore = atom(/* @__PURE__ */ new Map());
5
- const setPropsByInstanceIdStore = (store) => {
6
- propsByInstanceIdStore = store;
7
- };
8
- const getPropsByInstanceIdStore = () => {
9
- return propsByInstanceIdStore;
10
- };
4
+ import { ReactSdkContext } from "./context";
11
5
  const getPropsByInstanceId = (props) => {
12
6
  const propsByInstanceId = /* @__PURE__ */ new Map();
13
7
  for (const prop of props.values()) {
@@ -21,6 +15,7 @@ const getPropsByInstanceId = (props) => {
21
15
  return propsByInstanceId;
22
16
  };
23
17
  const useInstanceProps = (instanceId) => {
18
+ const { propsByInstanceIdStore } = useContext(ReactSdkContext);
24
19
  const propsByInstanceId = useStore(propsByInstanceIdStore);
25
20
  const instanceProps = propsByInstanceId.get(instanceId);
26
21
  const instancePropsObject = {};
@@ -34,27 +29,29 @@ const useInstanceProps = (instanceId) => {
34
29
  return instancePropsObject;
35
30
  };
36
31
  const usePropAsset = (instanceId, name) => {
32
+ const { propsByInstanceIdStore, assetsStore } = useContext(ReactSdkContext);
37
33
  const assetStore = useMemo(() => {
38
- return computed(propsByInstanceIdStore, (propsByInstanceId) => {
39
- const instanceProps = propsByInstanceId.get(instanceId);
40
- let asset2 = void 0;
41
- if (instanceProps) {
34
+ return computed(
35
+ [propsByInstanceIdStore, assetsStore],
36
+ (propsByInstanceId, assets) => {
37
+ const instanceProps = propsByInstanceId.get(instanceId);
38
+ if (instanceProps === void 0) {
39
+ return void 0;
40
+ }
42
41
  for (const prop of instanceProps) {
43
42
  if (prop.type === "asset" && prop.name === name) {
44
- asset2 = prop.value;
43
+ const assetId = prop.value;
44
+ return assets.get(assetId);
45
45
  }
46
46
  }
47
47
  }
48
- return asset2;
49
- });
50
- }, [instanceId, name]);
48
+ );
49
+ }, [propsByInstanceIdStore, assetsStore, instanceId, name]);
51
50
  const asset = useStore(assetStore);
52
51
  return asset;
53
52
  };
54
53
  export {
55
54
  getPropsByInstanceId,
56
- getPropsByInstanceIdStore,
57
- setPropsByInstanceIdStore,
58
55
  useInstanceProps,
59
56
  usePropAsset
60
57
  };
@@ -1,10 +1,13 @@
1
1
  import { jsx, jsxs } from "react/jsx-runtime";
2
2
  import { Fragment } from "react";
3
3
  import { Scripts, ScrollRestoration } from "@remix-run/react";
4
+ import { ReactSdkContext } from "../context";
4
5
  import { SessionStoragePolyfill } from "./session-storage-polyfill";
5
6
  const createElementsTree = ({
6
7
  sandbox,
7
8
  instance,
9
+ propsByInstanceIdStore,
10
+ assetsStore,
8
11
  Component,
9
12
  onChangeChildren
10
13
  }) => {
@@ -27,7 +30,10 @@ const createElementsTree = ({
27
30
  }, "children")
28
31
  ]
29
32
  });
30
- return body;
33
+ return /* @__PURE__ */ jsx(ReactSdkContext.Provider, {
34
+ value: { propsByInstanceIdStore, assetsStore },
35
+ children: body
36
+ });
31
37
  };
32
38
  const createInstanceChildrenElements = ({
33
39
  children,
package/lib/tree/root.js CHANGED
@@ -4,7 +4,7 @@ import { WrapperComponent } from "./wrapper-component";
4
4
  import { registerComponents } from "../components";
5
5
  import { customComponents as defaultCustomComponents } from "../app/custom-components";
6
6
  import { setParams } from "../app/params";
7
- import { getPropsByInstanceId, setPropsByInstanceIdStore } from "../props";
7
+ import { getPropsByInstanceId } from "../props";
8
8
  const InstanceRoot = ({
9
9
  data,
10
10
  Component,
@@ -13,13 +13,14 @@ const InstanceRoot = ({
13
13
  if (data.tree === null) {
14
14
  throw new Error("Tree is null");
15
15
  }
16
- setPropsByInstanceIdStore(
17
- atom(getPropsByInstanceId(new Map(data.tree.props)))
18
- );
19
16
  setParams(data.params ?? null);
20
17
  registerComponents(customComponents);
21
18
  return createElementsTree({
22
19
  instance: data.tree.root,
20
+ propsByInstanceIdStore: atom(
21
+ getPropsByInstanceId(new Map(data.tree.props))
22
+ ),
23
+ assetsStore: atom(new Map(data.assets.map((asset) => [asset.id, asset]))),
23
24
  Component: Component ?? WrapperComponent
24
25
  });
25
26
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@webstudio-is/react-sdk",
3
- "version": "0.26.0",
3
+ "version": "0.28.0",
4
4
  "description": "Webstudio JavaScript / TypeScript API",
5
5
  "author": "Webstudio <github@webstudio.is>",
6
6
  "homepage": "https://webstudio.is",
@@ -47,12 +47,12 @@
47
47
  "mitt": "^3.0.0",
48
48
  "nanostores": "^0.7.1",
49
49
  "warn-once": "^0.1.1",
50
- "@webstudio-is/asset-uploader": "^0.25.0",
51
- "@webstudio-is/css-data": "^0.26.0",
52
- "@webstudio-is/icons": "^0.26.0",
53
- "@webstudio-is/image": "^0.26.0",
54
- "@webstudio-is/prisma-client": "^0.26.0",
55
- "@webstudio-is/project-build": "^0.26.0"
50
+ "@webstudio-is/asset-uploader": "^0.28.0",
51
+ "@webstudio-is/css-data": "^0.28.0",
52
+ "@webstudio-is/icons": "^0.28.0",
53
+ "@webstudio-is/image": "^0.28.0",
54
+ "@webstudio-is/prisma-client": "^0.28.0",
55
+ "@webstudio-is/project-build": "^0.28.0"
56
56
  },
57
57
  "exports": {
58
58
  "import": "./lib/index.js",
@@ -0,0 +1,11 @@
1
+ import { type ReadableAtom, atom } from "nanostores";
2
+ import { createContext } from "react";
3
+ import type { Assets, PropsByInstanceId } from "./props";
4
+
5
+ export const ReactSdkContext = createContext<{
6
+ propsByInstanceIdStore: ReadableAtom<PropsByInstanceId>;
7
+ assetsStore: ReadableAtom<Assets>;
8
+ }>({
9
+ propsByInstanceIdStore: atom(new Map()),
10
+ assetsStore: atom(new Map()),
11
+ });
package/src/index.ts CHANGED
@@ -8,4 +8,3 @@ export {
8
8
  customComponentsMeta,
9
9
  } from "./app/custom-components";
10
10
  export type { MetaProps, WsComponentMeta } from "./components/component-type";
11
- export { setPropsByInstanceIdStore } from "./props";
package/src/props.ts CHANGED
@@ -1,22 +1,13 @@
1
- import { useMemo } from "react";
2
- import { atom, computed, type ReadableAtom } from "nanostores";
1
+ import { useContext, useMemo } from "react";
2
+ import { computed } from "nanostores";
3
3
  import { useStore } from "@nanostores/react";
4
4
  import type { Instance, Prop, Props } from "@webstudio-is/project-build";
5
5
  import type { Asset } from "@webstudio-is/asset-uploader";
6
+ import { ReactSdkContext } from "./context";
6
7
 
7
- type PropsByInstanceId = Map<Instance["id"], Prop[]>;
8
+ export type PropsByInstanceId = Map<Instance["id"], Prop[]>;
8
9
 
9
- type PropsByInstanceIdStore = ReadableAtom<PropsByInstanceId>;
10
-
11
- let propsByInstanceIdStore: PropsByInstanceIdStore = atom(new Map());
12
-
13
- export const setPropsByInstanceIdStore = (store: PropsByInstanceIdStore) => {
14
- propsByInstanceIdStore = store;
15
- };
16
-
17
- export const getPropsByInstanceIdStore = () => {
18
- return propsByInstanceIdStore;
19
- };
10
+ export type Assets = Map<Asset["id"], Asset>;
20
11
 
21
12
  export const getPropsByInstanceId = (props: Props) => {
22
13
  const propsByInstanceId: PropsByInstanceId = new Map();
@@ -34,6 +25,7 @@ export const getPropsByInstanceId = (props: Props) => {
34
25
  // this utility is be used only for preview with static props
35
26
  // so there is no need to use computed to optimize rerenders
36
27
  export const useInstanceProps = (instanceId: Instance["id"]) => {
28
+ const { propsByInstanceIdStore } = useContext(ReactSdkContext);
37
29
  const propsByInstanceId = useStore(propsByInstanceIdStore);
38
30
  const instanceProps = propsByInstanceId.get(instanceId);
39
31
  const instancePropsObject: Record<string, number | string | boolean> = {};
@@ -50,20 +42,24 @@ export const useInstanceProps = (instanceId: Instance["id"]) => {
50
42
  // this utility is be used for image component in both designer and preview
51
43
  // so need to optimize rerenders with computed
52
44
  export const usePropAsset = (instanceId: Instance["id"], name: string) => {
45
+ const { propsByInstanceIdStore, assetsStore } = useContext(ReactSdkContext);
53
46
  const assetStore = useMemo(() => {
54
- return computed(propsByInstanceIdStore, (propsByInstanceId) => {
55
- const instanceProps = propsByInstanceId.get(instanceId);
56
- let asset: undefined | Asset = undefined;
57
- if (instanceProps) {
47
+ return computed(
48
+ [propsByInstanceIdStore, assetsStore],
49
+ (propsByInstanceId, assets) => {
50
+ const instanceProps = propsByInstanceId.get(instanceId);
51
+ if (instanceProps === undefined) {
52
+ return undefined;
53
+ }
58
54
  for (const prop of instanceProps) {
59
55
  if (prop.type === "asset" && prop.name === name) {
60
- asset = prop.value;
56
+ const assetId = prop.value;
57
+ return assets.get(assetId);
61
58
  }
62
59
  }
63
60
  }
64
- return asset;
65
- });
66
- }, [instanceId, name]);
61
+ );
62
+ }, [propsByInstanceIdStore, assetsStore, instanceId, name]);
67
63
  const asset = useStore(assetStore);
68
64
  return asset;
69
65
  };
@@ -1,6 +1,9 @@
1
1
  import { type ComponentProps, Fragment } from "react";
2
+ import { ReadableAtom } from "nanostores";
2
3
  import { Scripts, ScrollRestoration } from "@remix-run/react";
3
4
  import type { Instance } from "@webstudio-is/project-build";
5
+ import { ReactSdkContext } from "../context";
6
+ import { Assets, PropsByInstanceId } from "../props";
4
7
  import { WrapperComponent } from "./wrapper-component";
5
8
  import { SessionStoragePolyfill } from "./session-storage-polyfill";
6
9
 
@@ -25,11 +28,15 @@ export type OnChangeChildren = (change: {
25
28
  export const createElementsTree = ({
26
29
  sandbox,
27
30
  instance,
31
+ propsByInstanceIdStore,
32
+ assetsStore,
28
33
  Component,
29
34
  onChangeChildren,
30
35
  }: {
31
36
  sandbox?: boolean;
32
37
  instance: Instance;
38
+ propsByInstanceIdStore: ReadableAtom<PropsByInstanceId>;
39
+ assetsStore: ReadableAtom<Assets>;
33
40
  Component: (props: ComponentProps<typeof WrapperComponent>) => JSX.Element;
34
41
  onChangeChildren?: OnChangeChildren;
35
42
  }) => {
@@ -50,7 +57,11 @@ export const createElementsTree = ({
50
57
  </Fragment>,
51
58
  ],
52
59
  });
53
- return body;
60
+ return (
61
+ <ReactSdkContext.Provider value={{ propsByInstanceIdStore, assetsStore }}>
62
+ {body}
63
+ </ReactSdkContext.Provider>
64
+ );
54
65
  };
55
66
 
56
67
  const createInstanceChildrenElements = ({
package/src/tree/root.ts CHANGED
@@ -7,7 +7,7 @@ import { WrapperComponent } from "./wrapper-component";
7
7
  import { registerComponents } from "../components";
8
8
  import { customComponents as defaultCustomComponents } from "../app/custom-components";
9
9
  import { setParams, type Params } from "../app/params";
10
- import { getPropsByInstanceId, setPropsByInstanceIdStore } from "../props";
10
+ import { getPropsByInstanceId } from "../props";
11
11
 
12
12
  export type Data = {
13
13
  tree: Tree | null;
@@ -30,16 +30,16 @@ export const InstanceRoot = ({
30
30
  throw new Error("Tree is null");
31
31
  }
32
32
 
33
- setPropsByInstanceIdStore(
34
- atom(getPropsByInstanceId(new Map(data.tree.props)))
35
- );
36
-
37
33
  setParams(data.params ?? null);
38
34
 
39
35
  registerComponents(customComponents);
40
36
 
41
37
  return createElementsTree({
42
38
  instance: data.tree.root,
39
+ propsByInstanceIdStore: atom(
40
+ getPropsByInstanceId(new Map(data.tree.props))
41
+ ),
42
+ assetsStore: atom(new Map(data.assets.map((asset) => [asset.id, asset]))),
43
43
  Component: Component ?? WrapperComponent,
44
44
  });
45
45
  };