@webstudio-is/react-sdk 0.20.0 → 0.21.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.
@@ -18,33 +18,42 @@ var __copyProps = (to, from, except, desc) => {
18
18
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
19
  var instance_exports = {};
20
20
  __export(instance_exports, {
21
+ Id: () => Id,
21
22
  Instance: () => Instance,
23
+ Instances: () => Instances,
24
+ InstancesItem: () => InstancesItem,
22
25
  Text: () => Text,
23
26
  toBaseInstance: () => toBaseInstance
24
27
  });
25
28
  module.exports = __toCommonJS(instance_exports);
26
- var import_css_data = require("@webstudio-is/css-data");
27
29
  var import_zod = require("zod");
28
30
  var import_components = require("../components");
29
31
  const toBaseInstance = (instance) => {
30
32
  return {
31
33
  id: instance.id,
32
- component: instance.component,
33
- cssRules: instance.cssRules
34
+ component: instance.component
34
35
  };
35
36
  };
36
- const Text = import_zod.z.lazy(
37
- () => import_zod.z.object({
38
- type: import_zod.z.literal("text"),
39
- value: import_zod.z.string()
40
- })
41
- );
37
+ const Text = import_zod.z.object({
38
+ type: import_zod.z.literal("text"),
39
+ value: import_zod.z.string()
40
+ });
41
+ const Id = import_zod.z.object({
42
+ type: import_zod.z.literal("id"),
43
+ value: import_zod.z.string()
44
+ });
45
+ const InstancesItem = import_zod.z.object({
46
+ type: import_zod.z.literal("instance"),
47
+ id: import_zod.z.string(),
48
+ component: import_zod.z.enum((0, import_components.getComponentNames)()),
49
+ children: import_zod.z.array(import_zod.z.union([Id, Text]))
50
+ });
51
+ const Instances = import_zod.z.array(InstancesItem);
42
52
  const Instance = import_zod.z.lazy(
43
53
  () => import_zod.z.object({
44
54
  type: import_zod.z.literal("instance"),
45
55
  id: import_zod.z.string(),
46
56
  component: import_zod.z.enum((0, import_components.getComponentNames)()),
47
- children: import_zod.z.array(import_zod.z.union([Instance, Text])),
48
- cssRules: import_zod.z.array(import_css_data.CssRule)
57
+ children: import_zod.z.array(import_zod.z.union([Instance, Text]))
49
58
  })
50
59
  );
@@ -23,17 +23,18 @@ __export(all_user_props_exports, {
23
23
  });
24
24
  module.exports = __toCommonJS(all_user_props_exports);
25
25
  var import_react = require("react");
26
- var import_react_nano_state = require("react-nano-state");
27
- const allUserPropsContainer = (0, import_react_nano_state.createValueContainer)({});
26
+ var import_nanostores = require("nanostores");
27
+ var import_react2 = require("@nanostores/react");
28
+ const allUserPropsContainer = (0, import_nanostores.atom)({});
28
29
  const useAllUserProps = (initialUserProps) => {
29
30
  const ref = (0, import_react.useRef)(false);
30
31
  if (ref.current === false && initialUserProps !== void 0) {
31
32
  const propsMap = {};
32
- for (const props of initialUserProps) {
33
- propsMap[props.instanceId] = props;
33
+ for (const item of initialUserProps) {
34
+ propsMap[item.instanceId] = item.props;
34
35
  }
35
- allUserPropsContainer.value = propsMap;
36
+ allUserPropsContainer.set(propsMap);
36
37
  ref.current = true;
37
38
  }
38
- return (0, import_react_nano_state.useValue)(allUserPropsContainer);
39
+ return (0, import_react2.useStore)(allUserPropsContainer);
39
40
  };
@@ -17,5 +17,4 @@ var user_props_exports = {};
17
17
  module.exports = __toCommonJS(user_props_exports);
18
18
  __reExport(user_props_exports, require("./all-user-props"), module.exports);
19
19
  __reExport(user_props_exports, require("./use-user-props"), module.exports);
20
- __reExport(user_props_exports, require("./types"), module.exports);
21
20
  __reExport(user_props_exports, require("./schema"), module.exports);
@@ -24,13 +24,13 @@ module.exports = __toCommonJS(use_user_props_asset_exports);
24
24
  var import_react = require("react");
25
25
  var import_all_user_props = require("./all-user-props");
26
26
  const useUserPropsAsset = (instanceId, propName) => {
27
- const [allUserProps] = (0, import_all_user_props.useAllUserProps)();
27
+ const allUserProps = (0, import_all_user_props.useAllUserProps)();
28
28
  const propsData = allUserProps[instanceId];
29
29
  const asset = (0, import_react.useMemo)(() => {
30
30
  if (propsData == null) {
31
31
  return void 0;
32
32
  }
33
- const prop = propsData.props.find((prop2) => prop2.prop === propName);
33
+ const prop = propsData.find((prop2) => prop2.prop === propName);
34
34
  if (prop == null) {
35
35
  return void 0;
36
36
  }
@@ -24,14 +24,14 @@ module.exports = __toCommonJS(use_user_props_exports);
24
24
  var import_react = require("react");
25
25
  var import_all_user_props = require("./all-user-props");
26
26
  const useUserProps = (instanceId) => {
27
- const [allUserProps] = (0, import_all_user_props.useAllUserProps)();
27
+ const allUserProps = (0, import_all_user_props.useAllUserProps)();
28
28
  const propsData = allUserProps[instanceId];
29
29
  const props = (0, import_react.useMemo)(() => {
30
30
  if (propsData == null) {
31
31
  return {};
32
32
  }
33
33
  const result = {};
34
- for (const userProp of propsData.props) {
34
+ for (const userProp of propsData) {
35
35
  if (userProp.type !== "asset") {
36
36
  result[userProp.prop] = userProp.value;
37
37
  }
@@ -1,30 +1,39 @@
1
- import { CssRule } from "@webstudio-is/css-data";
2
1
  import { z } from "zod";
3
2
  import { getComponentNames } from "../components";
4
3
  const toBaseInstance = (instance) => {
5
4
  return {
6
5
  id: instance.id,
7
- component: instance.component,
8
- cssRules: instance.cssRules
6
+ component: instance.component
9
7
  };
10
8
  };
11
- const Text = z.lazy(
12
- () => z.object({
13
- type: z.literal("text"),
14
- value: z.string()
15
- })
16
- );
9
+ const Text = z.object({
10
+ type: z.literal("text"),
11
+ value: z.string()
12
+ });
13
+ const Id = z.object({
14
+ type: z.literal("id"),
15
+ value: z.string()
16
+ });
17
+ const InstancesItem = z.object({
18
+ type: z.literal("instance"),
19
+ id: z.string(),
20
+ component: z.enum(getComponentNames()),
21
+ children: z.array(z.union([Id, Text]))
22
+ });
23
+ const Instances = z.array(InstancesItem);
17
24
  const Instance = z.lazy(
18
25
  () => z.object({
19
26
  type: z.literal("instance"),
20
27
  id: z.string(),
21
28
  component: z.enum(getComponentNames()),
22
- children: z.array(z.union([Instance, Text])),
23
- cssRules: z.array(CssRule)
29
+ children: z.array(z.union([Instance, Text]))
24
30
  })
25
31
  );
26
32
  export {
33
+ Id,
27
34
  Instance,
35
+ Instances,
36
+ InstancesItem,
28
37
  Text,
29
38
  toBaseInstance
30
39
  };
@@ -1,17 +1,18 @@
1
1
  import { useRef } from "react";
2
- import { createValueContainer, useValue } from "react-nano-state";
3
- const allUserPropsContainer = createValueContainer({});
2
+ import { atom } from "nanostores";
3
+ import { useStore } from "@nanostores/react";
4
+ const allUserPropsContainer = atom({});
4
5
  const useAllUserProps = (initialUserProps) => {
5
6
  const ref = useRef(false);
6
7
  if (ref.current === false && initialUserProps !== void 0) {
7
8
  const propsMap = {};
8
- for (const props of initialUserProps) {
9
- propsMap[props.instanceId] = props;
9
+ for (const item of initialUserProps) {
10
+ propsMap[item.instanceId] = item.props;
10
11
  }
11
- allUserPropsContainer.value = propsMap;
12
+ allUserPropsContainer.set(propsMap);
12
13
  ref.current = true;
13
14
  }
14
- return useValue(allUserPropsContainer);
15
+ return useStore(allUserPropsContainer);
15
16
  };
16
17
  export {
17
18
  allUserPropsContainer,
@@ -1,4 +1,3 @@
1
1
  export * from "./all-user-props";
2
2
  export * from "./use-user-props";
3
- export * from "./types";
4
3
  export * from "./schema";
@@ -1,13 +1,13 @@
1
1
  import { useMemo } from "react";
2
2
  import { useAllUserProps } from "./all-user-props";
3
3
  const useUserPropsAsset = (instanceId, propName) => {
4
- const [allUserProps] = useAllUserProps();
4
+ const allUserProps = useAllUserProps();
5
5
  const propsData = allUserProps[instanceId];
6
6
  const asset = useMemo(() => {
7
7
  if (propsData == null) {
8
8
  return void 0;
9
9
  }
10
- const prop = propsData.props.find((prop2) => prop2.prop === propName);
10
+ const prop = propsData.find((prop2) => prop2.prop === propName);
11
11
  if (prop == null) {
12
12
  return void 0;
13
13
  }
@@ -1,14 +1,14 @@
1
1
  import { useMemo } from "react";
2
2
  import { useAllUserProps } from "./all-user-props";
3
3
  const useUserProps = (instanceId) => {
4
- const [allUserProps] = useAllUserProps();
4
+ const allUserProps = useAllUserProps();
5
5
  const propsData = allUserProps[instanceId];
6
6
  const props = useMemo(() => {
7
7
  if (propsData == null) {
8
8
  return {};
9
9
  }
10
10
  const result = {};
11
- for (const userProp of propsData.props) {
11
+ for (const userProp of propsData) {
12
12
  if (userProp.type !== "asset") {
13
13
  result[userProp.prop] = userProp.value;
14
14
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@webstudio-is/react-sdk",
3
- "version": "0.20.0",
3
+ "version": "0.21.0",
4
4
  "description": "Webstudio JavaScript / TypeScript API",
5
5
  "author": "Webstudio <github@webstudio.is>",
6
6
  "homepage": "https://webstudio.is",
@@ -42,17 +42,17 @@
42
42
  "zod": "^3.19.1"
43
43
  },
44
44
  "dependencies": {
45
+ "@nanostores/react": "^0.4.1",
45
46
  "detect-font": "^0.1.5",
46
- "immer": "^9.0.12",
47
47
  "mitt": "^3.0.0",
48
- "react-nano-state": "^0.4.0",
48
+ "nanostores": "^0.7.1",
49
49
  "warn-once": "^0.1.1",
50
- "@webstudio-is/asset-uploader": "^0.20.0",
51
- "@webstudio-is/css-data": "^0.20.0",
52
- "@webstudio-is/design-tokens": "^0.20.0",
53
- "@webstudio-is/icons": "^0.20.0",
54
- "@webstudio-is/image": "^0.20.0",
55
- "@webstudio-is/prisma-client": "^0.20.0"
50
+ "@webstudio-is/asset-uploader": "^0.21.0",
51
+ "@webstudio-is/css-data": "^0.21.0",
52
+ "@webstudio-is/design-tokens": "^0.21.0",
53
+ "@webstudio-is/icons": "^0.21.0",
54
+ "@webstudio-is/image": "^0.21.0",
55
+ "@webstudio-is/prisma-client": "^0.21.0"
56
56
  },
57
57
  "exports": {
58
58
  "import": "./lib/index.js",
@@ -1,18 +1,11 @@
1
- import { CssRule } from "@webstudio-is/css-data";
2
1
  import { z } from "zod";
3
- import { ComponentName, getComponentNames } from "../components";
2
+ import { type ComponentName, getComponentNames } from "../components";
4
3
 
5
4
  // This should be used when passing a lot of data is potentially costly.
6
5
  // For example, when passing data from an iframe.
7
6
  export type BaseInstance = {
8
7
  id: string;
9
8
  component: ComponentName;
10
- cssRules: Array<CssRule>;
11
- };
12
-
13
- export type Text = {
14
- type: "text";
15
- value: string;
16
9
  };
17
10
 
18
11
  export type Instance = BaseInstance & {
@@ -24,16 +17,34 @@ export const toBaseInstance = (instance: Instance): BaseInstance => {
24
17
  return {
25
18
  id: instance.id,
26
19
  component: instance.component,
27
- cssRules: instance.cssRules,
28
20
  };
29
21
  };
30
22
 
31
- export const Text = z.lazy(() =>
32
- z.object({
33
- type: z.literal("text"),
34
- value: z.string(),
35
- })
36
- );
23
+ export const Text = z.object({
24
+ type: z.literal("text"),
25
+ value: z.string(),
26
+ });
27
+
28
+ export type Text = z.infer<typeof Text>;
29
+
30
+ export const Id = z.object({
31
+ type: z.literal("id"),
32
+ value: z.string(),
33
+ });
34
+ export type Id = z.infer<typeof Id>;
35
+
36
+ export const InstancesItem = z.object({
37
+ type: z.literal("instance"),
38
+ id: z.string(),
39
+ component: z.enum(getComponentNames() as [ComponentName]),
40
+ children: z.array(z.union([Id, Text])),
41
+ });
42
+
43
+ export type InstancesItem = z.infer<typeof InstancesItem>;
44
+
45
+ export const Instances = z.array(InstancesItem);
46
+
47
+ export type Instances = z.infer<typeof Instances>;
37
48
 
38
49
  export const Instance: z.ZodType<Instance> = z.lazy(() =>
39
50
  z.object({
@@ -41,6 +52,5 @@ export const Instance: z.ZodType<Instance> = z.lazy(() =>
41
52
  id: z.string(),
42
53
  component: z.enum(getComponentNames() as [ComponentName]),
43
54
  children: z.array(z.union([Instance, Text])),
44
- cssRules: z.array(CssRule),
45
55
  })
46
56
  );
package/src/db/style.ts CHANGED
@@ -7,18 +7,14 @@ import {
7
7
  getComponentNames,
8
8
  } from "../components";
9
9
 
10
- export type PresetStylesItem = {
11
- component: ComponentName;
12
- property: StyleProperty;
13
- value: StyleValue;
14
- };
15
-
16
- // @todo can't figure out how to make property to be enum
17
10
  export const PresetStylesItem = z.object({
18
11
  component: z.enum(getComponentNames() as [ComponentName]),
19
- property: z.string(),
20
- value: SharedStyleValue,
21
- }) as z.ZodType<PresetStylesItem>;
12
+ // @todo can't figure out how to make property to be enum
13
+ property: z.string() as z.ZodType<StyleProperty>,
14
+ value: SharedStyleValue as z.ZodType<StyleValue>,
15
+ });
16
+
17
+ export type PresetStylesItem = z.infer<typeof PresetStylesItem>;
22
18
 
23
19
  export const PresetStyles = z.array(PresetStylesItem);
24
20
 
@@ -52,20 +48,15 @@ export const findMissingPresetStyles = (
52
48
  return missingPresetStyles;
53
49
  };
54
50
 
55
- export type StylesItem = {
56
- breakpointId: string;
57
- instanceId: string;
58
- property: StyleProperty;
59
- value: StyleValue;
60
- };
61
-
62
- // @todo can't figure out how to make property to be enum
63
51
  export const StylesItem = z.object({
64
52
  breakpointId: z.string(),
65
53
  instanceId: z.string(),
66
- property: z.string(),
67
- value: SharedStyleValue,
68
- }) as z.ZodType<StylesItem>;
54
+ // @todo can't figure out how to make property to be enum
55
+ property: z.string() as z.ZodType<StyleProperty>,
56
+ value: SharedStyleValue as z.ZodType<StyleValue>,
57
+ });
58
+
59
+ export type StylesItem = z.infer<typeof StylesItem>;
69
60
 
70
61
  export const Styles = z.array(StylesItem);
71
62
 
@@ -1,22 +1,26 @@
1
1
  import { useRef } from "react";
2
- import { createValueContainer, useValue } from "react-nano-state";
2
+ import { atom } from "nanostores";
3
+ import { useStore } from "@nanostores/react";
3
4
  import type { InstanceProps, Instance } from "../db";
5
+ import { UserProp } from "./schema";
4
6
 
5
- export type AllUserProps = { [id: Instance["id"]]: InstanceProps };
7
+ export type AllUserProps = {
8
+ [id: Instance["id"]]: UserProp[];
9
+ };
6
10
 
7
- export const allUserPropsContainer = createValueContainer<AllUserProps>({});
11
+ export const allUserPropsContainer = atom<AllUserProps>({});
8
12
 
9
13
  export const useAllUserProps = (initialUserProps?: Array<InstanceProps>) => {
10
14
  // @todo ssr workaround for https://github.com/webstudio-is/webstudio-designer/issues/213
11
15
  const ref = useRef(false);
12
16
  if (ref.current === false && initialUserProps !== undefined) {
13
17
  const propsMap: AllUserProps = {};
14
- for (const props of initialUserProps) {
15
- propsMap[props.instanceId] = props;
18
+ for (const item of initialUserProps) {
19
+ propsMap[item.instanceId] = item.props;
16
20
  }
17
21
  //We don't need to trigger rerender when setting the initial value
18
- allUserPropsContainer.value = propsMap;
22
+ allUserPropsContainer.set(propsMap);
19
23
  ref.current = true;
20
24
  }
21
- return useValue(allUserPropsContainer);
25
+ return useStore(allUserPropsContainer);
22
26
  };
@@ -1,4 +1,3 @@
1
1
  export * from "./all-user-props";
2
2
  export * from "./use-user-props";
3
- export * from "./types";
4
3
  export * from "./schema";
@@ -11,14 +11,14 @@ export const useUserPropsAsset = (
11
11
  instanceId: Instance["id"],
12
12
  propName: UserProp["prop"]
13
13
  ): Asset | undefined => {
14
- const [allUserProps] = useAllUserProps();
14
+ const allUserProps = useAllUserProps();
15
15
 
16
16
  const propsData = allUserProps[instanceId];
17
17
  const asset = useMemo(() => {
18
18
  if (propsData == null) {
19
19
  return undefined;
20
20
  }
21
- const prop = propsData.props.find((prop) => prop.prop === propName);
21
+ const prop = propsData.find((prop) => prop.prop === propName);
22
22
 
23
23
  if (prop == null) {
24
24
  return undefined;
@@ -10,7 +10,7 @@ type UserProps = { [prop: UserProp["prop"]]: string | number | boolean };
10
10
  * up to date with props panel.
11
11
  */
12
12
  export const useUserProps = (instanceId: Instance["id"]) => {
13
- const [allUserProps] = useAllUserProps();
13
+ const allUserProps = useAllUserProps();
14
14
 
15
15
  const propsData = allUserProps[instanceId];
16
16
  const props = useMemo(() => {
@@ -19,7 +19,7 @@ export const useUserProps = (instanceId: Instance["id"]) => {
19
19
  }
20
20
  const result: UserProps = {};
21
21
 
22
- for (const userProp of propsData.props) {
22
+ for (const userProp of propsData) {
23
23
  if (userProp.type !== "asset") {
24
24
  result[userProp.prop] = userProp.value;
25
25
  }
@@ -1,16 +0,0 @@
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 __copyProps = (to, from, except, desc) => {
7
- if (from && typeof from === "object" || typeof from === "function") {
8
- for (let key of __getOwnPropNames(from))
9
- if (!__hasOwnProp.call(to, key) && key !== except)
10
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
11
- }
12
- return to;
13
- };
14
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
15
- var types_exports = {};
16
- module.exports = __toCommonJS(types_exports);
File without changes
@@ -1,13 +0,0 @@
1
- import type { InstanceProps, Instance } from "../db";
2
- import { UserProp } from "./schema";
3
-
4
- export type UserPropsUpdates = {
5
- instanceId: Instance["id"];
6
- propsId?: InstanceProps["id"];
7
- updates: Array<UserProp>;
8
- };
9
-
10
- export type DeleteProp = {
11
- instanceId: Instance["id"];
12
- propId: UserProp["id"];
13
- };