@webstudio-is/react-sdk 0.22.0 → 0.23.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.
- package/lib/app/custom-components/image.js +3 -3
- package/lib/cjs/app/custom-components/image.cjs +3 -3
- package/lib/cjs/components/index.cjs +15 -9
- package/lib/cjs/index.cjs +3 -3
- package/lib/cjs/props.cjs +80 -0
- package/lib/cjs/tree/root.cjs +3 -2
- package/lib/cjs/tree/wrapper-component.cjs +7 -4
- package/lib/components/index.js +15 -9
- package/lib/index.js +3 -3
- package/lib/props.js +60 -0
- package/lib/tree/root.js +3 -2
- package/lib/tree/wrapper-component.js +10 -7
- package/package.json +11 -11
- package/src/app/custom-components/image.tsx +3 -3
- package/src/components/index.ts +26 -17
- package/src/index.ts +2 -3
- package/src/props.ts +69 -0
- package/src/tree/create-elements-tree.tsx +2 -2
- package/src/tree/root.ts +9 -9
- package/src/tree/wrapper-component.tsx +8 -5
- package/lib/cjs/db/index.cjs +0 -20
- package/lib/cjs/db/instance.cjs +0 -59
- package/lib/cjs/db/style.cjs +0 -67
- package/lib/cjs/db/types.cjs +0 -16
- package/lib/cjs/user-props/all-user-props.cjs +0 -40
- package/lib/cjs/user-props/index.cjs +0 -20
- package/lib/cjs/user-props/schema.cjs +0 -54
- package/lib/cjs/user-props/use-user-props-asset.cjs +0 -43
- package/lib/cjs/user-props/use-user-props.cjs +0 -42
- package/lib/db/index.js +0 -3
- package/lib/db/instance.js +0 -39
- package/lib/db/style.js +0 -50
- package/lib/db/types.js +0 -0
- package/lib/user-props/all-user-props.js +0 -20
- package/lib/user-props/index.js +0 -3
- package/lib/user-props/schema.js +0 -34
- package/lib/user-props/use-user-props-asset.js +0 -23
- package/lib/user-props/use-user-props.js +0 -22
- package/src/db/index.ts +0 -3
- package/src/db/instance.ts +0 -56
- package/src/db/style.ts +0 -63
- package/src/db/types.ts +0 -22
- package/src/user-props/all-user-props.ts +0 -26
- package/src/user-props/index.ts +0 -3
- package/src/user-props/schema.ts +0 -35
- package/src/user-props/use-user-props-asset.ts +0 -35
- package/src/user-props/use-user-props.ts +0 -32
package/src/db/instance.ts
DELETED
|
@@ -1,56 +0,0 @@
|
|
|
1
|
-
import { z } from "zod";
|
|
2
|
-
import { type ComponentName, getComponentNames } from "../components";
|
|
3
|
-
|
|
4
|
-
// This should be used when passing a lot of data is potentially costly.
|
|
5
|
-
// For example, when passing data from an iframe.
|
|
6
|
-
export type BaseInstance = {
|
|
7
|
-
id: string;
|
|
8
|
-
component: ComponentName;
|
|
9
|
-
};
|
|
10
|
-
|
|
11
|
-
export type Instance = BaseInstance & {
|
|
12
|
-
type: "instance";
|
|
13
|
-
children: Array<Instance | Text>;
|
|
14
|
-
};
|
|
15
|
-
|
|
16
|
-
export const toBaseInstance = (instance: Instance): BaseInstance => {
|
|
17
|
-
return {
|
|
18
|
-
id: instance.id,
|
|
19
|
-
component: instance.component,
|
|
20
|
-
};
|
|
21
|
-
};
|
|
22
|
-
|
|
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>;
|
|
48
|
-
|
|
49
|
-
export const Instance: z.ZodType<Instance> = z.lazy(() =>
|
|
50
|
-
z.object({
|
|
51
|
-
type: z.literal("instance"),
|
|
52
|
-
id: z.string(),
|
|
53
|
-
component: z.enum(getComponentNames() as [ComponentName]),
|
|
54
|
-
children: z.array(z.union([Instance, Text])),
|
|
55
|
-
})
|
|
56
|
-
);
|
package/src/db/style.ts
DELETED
|
@@ -1,63 +0,0 @@
|
|
|
1
|
-
import { z } from "zod";
|
|
2
|
-
import type { StyleProperty, StyleValue } from "@webstudio-is/css-data";
|
|
3
|
-
import { SharedStyleValue } from "@webstudio-is/css-data";
|
|
4
|
-
import {
|
|
5
|
-
type ComponentName,
|
|
6
|
-
getComponentMeta,
|
|
7
|
-
getComponentNames,
|
|
8
|
-
} from "../components";
|
|
9
|
-
|
|
10
|
-
export const PresetStylesItem = z.object({
|
|
11
|
-
component: z.enum(getComponentNames() as [ComponentName]),
|
|
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>;
|
|
18
|
-
|
|
19
|
-
export const PresetStyles = z.array(PresetStylesItem);
|
|
20
|
-
|
|
21
|
-
export type PresetStyles = z.infer<typeof PresetStyles>;
|
|
22
|
-
|
|
23
|
-
export const findMissingPresetStyles = (
|
|
24
|
-
presetStyles: PresetStyles,
|
|
25
|
-
components: ComponentName[]
|
|
26
|
-
) => {
|
|
27
|
-
const populatedComponents = new Set();
|
|
28
|
-
for (const style of presetStyles) {
|
|
29
|
-
populatedComponents.add(style.component);
|
|
30
|
-
}
|
|
31
|
-
const missingPresetStyles: PresetStyles = [];
|
|
32
|
-
for (const component of components) {
|
|
33
|
-
if (populatedComponents.has(component)) {
|
|
34
|
-
continue;
|
|
35
|
-
}
|
|
36
|
-
const meta = getComponentMeta(component);
|
|
37
|
-
if (meta.presetStyle === undefined) {
|
|
38
|
-
continue;
|
|
39
|
-
}
|
|
40
|
-
for (const [property, value] of Object.entries(meta.presetStyle)) {
|
|
41
|
-
missingPresetStyles.push({
|
|
42
|
-
component,
|
|
43
|
-
property: property as StyleProperty,
|
|
44
|
-
value,
|
|
45
|
-
});
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
return missingPresetStyles;
|
|
49
|
-
};
|
|
50
|
-
|
|
51
|
-
export const StylesItem = z.object({
|
|
52
|
-
breakpointId: z.string(),
|
|
53
|
-
instanceId: z.string(),
|
|
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>;
|
|
60
|
-
|
|
61
|
-
export const Styles = z.array(StylesItem);
|
|
62
|
-
|
|
63
|
-
export type Styles = z.infer<typeof Styles>;
|
package/src/db/types.ts
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import type { InstanceProps as DbInstanceProps } from "@webstudio-is/prisma-client";
|
|
2
|
-
import type { UserProp } from "../user-props";
|
|
3
|
-
import type { Instance } from "./instance";
|
|
4
|
-
import type { PresetStyles, Styles } from "./style";
|
|
5
|
-
|
|
6
|
-
export type Tree = {
|
|
7
|
-
id: string;
|
|
8
|
-
root: Instance;
|
|
9
|
-
presetStyles: PresetStyles;
|
|
10
|
-
styles: Styles;
|
|
11
|
-
};
|
|
12
|
-
|
|
13
|
-
export type Props = {
|
|
14
|
-
props: string;
|
|
15
|
-
id: string;
|
|
16
|
-
instanceId: string;
|
|
17
|
-
treeId: string;
|
|
18
|
-
};
|
|
19
|
-
|
|
20
|
-
export type InstanceProps = Omit<DbInstanceProps, "props"> & {
|
|
21
|
-
props: Array<UserProp>;
|
|
22
|
-
};
|
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
import { useRef } from "react";
|
|
2
|
-
import { atom } from "nanostores";
|
|
3
|
-
import { useStore } from "@nanostores/react";
|
|
4
|
-
import type { InstanceProps, Instance } from "../db";
|
|
5
|
-
import { UserProp } from "./schema";
|
|
6
|
-
|
|
7
|
-
export type AllUserProps = {
|
|
8
|
-
[id: Instance["id"]]: UserProp[];
|
|
9
|
-
};
|
|
10
|
-
|
|
11
|
-
export const allUserPropsContainer = atom<AllUserProps>({});
|
|
12
|
-
|
|
13
|
-
export const useAllUserProps = (initialUserProps?: Array<InstanceProps>) => {
|
|
14
|
-
// @todo ssr workaround for https://github.com/webstudio-is/webstudio-designer/issues/213
|
|
15
|
-
const ref = useRef(false);
|
|
16
|
-
if (ref.current === false && initialUserProps !== undefined) {
|
|
17
|
-
const propsMap: AllUserProps = {};
|
|
18
|
-
for (const item of initialUserProps) {
|
|
19
|
-
propsMap[item.instanceId] = item.props;
|
|
20
|
-
}
|
|
21
|
-
//We don't need to trigger rerender when setting the initial value
|
|
22
|
-
allUserPropsContainer.set(propsMap);
|
|
23
|
-
ref.current = true;
|
|
24
|
-
}
|
|
25
|
-
return useStore(allUserPropsContainer);
|
|
26
|
-
};
|
package/src/user-props/index.ts
DELETED
package/src/user-props/schema.ts
DELETED
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
import { z } from "zod";
|
|
2
|
-
import { Asset } from "@webstudio-is/asset-uploader";
|
|
3
|
-
|
|
4
|
-
const baseUserProps = {
|
|
5
|
-
id: z.string(),
|
|
6
|
-
prop: z.string(),
|
|
7
|
-
required: z.optional(z.boolean()),
|
|
8
|
-
};
|
|
9
|
-
|
|
10
|
-
export const UserProp = z.discriminatedUnion("type", [
|
|
11
|
-
z.object({
|
|
12
|
-
...baseUserProps,
|
|
13
|
-
type: z.literal("number"),
|
|
14
|
-
value: z.number(),
|
|
15
|
-
}),
|
|
16
|
-
z.object({
|
|
17
|
-
...baseUserProps,
|
|
18
|
-
type: z.literal("string"),
|
|
19
|
-
value: z.string(),
|
|
20
|
-
}),
|
|
21
|
-
z.object({
|
|
22
|
-
...baseUserProps,
|
|
23
|
-
type: z.literal("boolean"),
|
|
24
|
-
value: z.boolean(),
|
|
25
|
-
}),
|
|
26
|
-
z.object({
|
|
27
|
-
...baseUserProps,
|
|
28
|
-
type: z.literal("asset"),
|
|
29
|
-
value: Asset,
|
|
30
|
-
}),
|
|
31
|
-
]);
|
|
32
|
-
|
|
33
|
-
export const UserProps = z.array(UserProp);
|
|
34
|
-
|
|
35
|
-
export type UserProp = z.infer<typeof UserProp>;
|
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
import { useMemo } from "react";
|
|
2
|
-
import { type Instance } from "../db";
|
|
3
|
-
import { type UserProp } from "./schema";
|
|
4
|
-
import { useAllUserProps } from "./all-user-props";
|
|
5
|
-
import type { Asset } from "@webstudio-is/asset-uploader";
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* Get asset for prop, like src on the Image component
|
|
9
|
-
*/
|
|
10
|
-
export const useUserPropsAsset = (
|
|
11
|
-
instanceId: Instance["id"],
|
|
12
|
-
propName: UserProp["prop"]
|
|
13
|
-
): Asset | undefined => {
|
|
14
|
-
const allUserProps = useAllUserProps();
|
|
15
|
-
|
|
16
|
-
const propsData = allUserProps[instanceId];
|
|
17
|
-
const asset = useMemo(() => {
|
|
18
|
-
if (propsData == null) {
|
|
19
|
-
return undefined;
|
|
20
|
-
}
|
|
21
|
-
const prop = propsData.find((prop) => prop.prop === propName);
|
|
22
|
-
|
|
23
|
-
if (prop == null) {
|
|
24
|
-
return undefined;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
if (prop.type === "asset") {
|
|
28
|
-
return prop.value;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
return undefined;
|
|
32
|
-
}, [propName, propsData]);
|
|
33
|
-
|
|
34
|
-
return asset;
|
|
35
|
-
};
|
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
import { useMemo } from "react";
|
|
2
|
-
import { type Instance } from "../db";
|
|
3
|
-
import { type UserProp } from "./schema";
|
|
4
|
-
import { useAllUserProps } from "./all-user-props";
|
|
5
|
-
|
|
6
|
-
type UserProps = { [prop: UserProp["prop"]]: string | number | boolean };
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* User props mapped in prop:value format,
|
|
10
|
-
* up to date with props panel.
|
|
11
|
-
*/
|
|
12
|
-
export const useUserProps = (instanceId: Instance["id"]) => {
|
|
13
|
-
const allUserProps = useAllUserProps();
|
|
14
|
-
|
|
15
|
-
const propsData = allUserProps[instanceId];
|
|
16
|
-
const props = useMemo(() => {
|
|
17
|
-
if (propsData == null) {
|
|
18
|
-
return {};
|
|
19
|
-
}
|
|
20
|
-
const result: UserProps = {};
|
|
21
|
-
|
|
22
|
-
for (const userProp of propsData) {
|
|
23
|
-
if (userProp.type !== "asset") {
|
|
24
|
-
result[userProp.prop] = userProp.value;
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
return result;
|
|
29
|
-
}, [propsData]);
|
|
30
|
-
|
|
31
|
-
return props;
|
|
32
|
-
};
|