@webstudio-is/react-sdk 0.75.0 → 0.77.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/cjs/context.js +5 -1
- package/lib/cjs/css/normalize.js +51 -23
- package/lib/cjs/css/presets.js +111 -1
- package/lib/cjs/embed-template.js +68 -3
- package/lib/cjs/expression.js +213 -0
- package/lib/cjs/index.js +8 -1
- package/lib/cjs/props.js +28 -10
- package/lib/cjs/tree/create-elements-tree.js +14 -1
- package/lib/cjs/tree/root.js +38 -0
- package/lib/cjs/tree/webstudio-component.js +9 -2
- package/lib/context.js +5 -1
- package/lib/css/normalize.js +41 -23
- package/lib/css/presets.js +111 -1
- package/lib/embed-template.js +68 -3
- package/lib/expression.js +183 -0
- package/lib/index.js +15 -1
- package/lib/props.js +28 -10
- package/lib/tree/create-elements-tree.js +14 -1
- package/lib/tree/root.js +42 -1
- package/lib/tree/webstudio-component.js +9 -2
- package/lib/types/components/component-meta.d.ts +112 -0
- package/lib/types/context.d.ts +3 -0
- package/lib/types/css/normalize.d.ts +1836 -0
- package/lib/types/css/presets.d.ts +282 -0
- package/lib/types/embed-template.d.ts +512 -0
- package/lib/types/expression.d.ts +11 -0
- package/lib/types/expression.test.d.ts +1 -0
- package/lib/types/index.d.ts +2 -1
- package/lib/types/props.d.ts +8 -7
- package/lib/types/tree/create-elements-tree.d.ts +4 -2
- package/lib/types/tree/root.d.ts +6 -4
- package/lib/types/tree/webstudio-component.d.ts +1 -0
- package/package.json +10 -10
- package/src/context.tsx +11 -0
- package/src/css/normalize.ts +40 -23
- package/src/css/presets.ts +110 -0
- package/src/embed-template.test.ts +177 -1
- package/src/embed-template.ts +73 -2
- package/src/expression.test.ts +147 -0
- package/src/expression.ts +217 -0
- package/src/index.ts +8 -0
- package/src/props.ts +29 -10
- package/src/tree/create-elements-tree.tsx +20 -1
- package/src/tree/root.ts +61 -4
- package/src/tree/webstudio-component.tsx +7 -1
package/src/tree/root.ts
CHANGED
|
@@ -1,6 +1,11 @@
|
|
|
1
|
-
import type
|
|
2
|
-
import {
|
|
3
|
-
|
|
1
|
+
import { useRef, type ComponentProps } from "react";
|
|
2
|
+
import {
|
|
3
|
+
atom,
|
|
4
|
+
computed,
|
|
5
|
+
type ReadableAtom,
|
|
6
|
+
type WritableAtom,
|
|
7
|
+
} from "nanostores";
|
|
8
|
+
import { type Build, type Page, DataSource } from "@webstudio-is/project-build";
|
|
4
9
|
import type { Asset } from "@webstudio-is/asset-uploader";
|
|
5
10
|
import { createElementsTree } from "./create-elements-tree";
|
|
6
11
|
import { WebstudioComponent } from "./webstudio-component";
|
|
@@ -17,20 +22,66 @@ export type Data = {
|
|
|
17
22
|
};
|
|
18
23
|
|
|
19
24
|
export type RootPropsData = Omit<Data, "build"> & {
|
|
20
|
-
build: Pick<Data["build"], "instances" | "props">;
|
|
25
|
+
build: Pick<Data["build"], "instances" | "props" | "dataSources">;
|
|
21
26
|
};
|
|
22
27
|
|
|
28
|
+
type DataSourceValues = Map<DataSource["id"], unknown>;
|
|
29
|
+
|
|
23
30
|
type RootProps = {
|
|
24
31
|
data: RootPropsData;
|
|
32
|
+
computeExpressions: (values: DataSourceValues) => DataSourceValues;
|
|
25
33
|
Component?: (props: ComponentProps<typeof WebstudioComponent>) => JSX.Element;
|
|
26
34
|
components: Components;
|
|
27
35
|
};
|
|
28
36
|
|
|
29
37
|
export const InstanceRoot = ({
|
|
30
38
|
data,
|
|
39
|
+
computeExpressions,
|
|
31
40
|
Component,
|
|
32
41
|
components,
|
|
33
42
|
}: RootProps): JSX.Element | null => {
|
|
43
|
+
const dataSourceVariablesStoreRef = useRef<
|
|
44
|
+
undefined | WritableAtom<DataSourceValues>
|
|
45
|
+
>(undefined);
|
|
46
|
+
if (dataSourceVariablesStoreRef.current === undefined) {
|
|
47
|
+
dataSourceVariablesStoreRef.current = atom(new Map());
|
|
48
|
+
}
|
|
49
|
+
const dataSourceVariablesStore = dataSourceVariablesStoreRef.current;
|
|
50
|
+
|
|
51
|
+
const dataSourceValuesStoreRef = useRef<
|
|
52
|
+
undefined | ReadableAtom<DataSourceValues>
|
|
53
|
+
>(undefined);
|
|
54
|
+
if (dataSourceValuesStoreRef.current === undefined) {
|
|
55
|
+
dataSourceValuesStoreRef.current = computed(
|
|
56
|
+
dataSourceVariablesStore,
|
|
57
|
+
(dataSourceVariables) => {
|
|
58
|
+
// set vriables with defaults
|
|
59
|
+
const dataSourceValues: DataSourceValues = new Map();
|
|
60
|
+
for (const [dataSourceId, dataSource] of data.build.dataSources) {
|
|
61
|
+
if (dataSource.type === "variable") {
|
|
62
|
+
const value =
|
|
63
|
+
dataSourceVariables.get(dataSourceId) ?? dataSource.value.value;
|
|
64
|
+
dataSourceValues.set(dataSourceId, value);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// set expression values
|
|
69
|
+
try {
|
|
70
|
+
const result = computeExpressions(dataSourceValues);
|
|
71
|
+
for (const [id, value] of result) {
|
|
72
|
+
dataSourceValues.set(id, value);
|
|
73
|
+
}
|
|
74
|
+
} catch (error) {
|
|
75
|
+
// eslint-disable-next-line no-console
|
|
76
|
+
console.error(error);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
return dataSourceValues;
|
|
80
|
+
}
|
|
81
|
+
);
|
|
82
|
+
}
|
|
83
|
+
const dataSourceValuesStore = dataSourceValuesStoreRef.current;
|
|
84
|
+
|
|
34
85
|
return createElementsTree({
|
|
35
86
|
imageBaseUrl: data.params?.imageBaseUrl ?? "/",
|
|
36
87
|
assetBaseUrl: data.params?.assetBaseUrl ?? "/",
|
|
@@ -41,6 +92,12 @@ export const InstanceRoot = ({
|
|
|
41
92
|
),
|
|
42
93
|
assetsStore: atom(new Map(data.assets.map((asset) => [asset.id, asset]))),
|
|
43
94
|
pagesStore: atom(new Map(data.pages.map((page) => [page.id, page]))),
|
|
95
|
+
dataSourceValuesStore,
|
|
96
|
+
onDataSourceUpdate: (dataSourceId, value) => {
|
|
97
|
+
const dataSourceVariables = new Map(dataSourceVariablesStore.get());
|
|
98
|
+
dataSourceVariables.set(dataSourceId, value);
|
|
99
|
+
dataSourceVariablesStore.set(dataSourceVariables);
|
|
100
|
+
},
|
|
44
101
|
Component: Component ?? WebstudioComponent,
|
|
45
102
|
components,
|
|
46
103
|
});
|
|
@@ -40,13 +40,18 @@ export const WebstudioComponent = ({
|
|
|
40
40
|
components,
|
|
41
41
|
...rest
|
|
42
42
|
}: WebstudioComponentProps) => {
|
|
43
|
-
const instanceProps = useInstanceProps(
|
|
43
|
+
const { [showAttribute]: show = true, ...instanceProps } = useInstanceProps(
|
|
44
|
+
instance.id
|
|
45
|
+
);
|
|
44
46
|
const props = {
|
|
45
47
|
...instanceProps,
|
|
46
48
|
...rest,
|
|
47
49
|
[idAttribute]: instance.id,
|
|
48
50
|
[componentAttribute]: instance.component,
|
|
49
51
|
};
|
|
52
|
+
if (show === false) {
|
|
53
|
+
return <></>;
|
|
54
|
+
}
|
|
50
55
|
const Component = components.get(instance.component);
|
|
51
56
|
if (Component === undefined) {
|
|
52
57
|
return <></>;
|
|
@@ -60,4 +65,5 @@ export const WebstudioComponent = ({
|
|
|
60
65
|
|
|
61
66
|
export const idAttribute = "data-ws-id";
|
|
62
67
|
export const componentAttribute = "data-ws-component";
|
|
68
|
+
export const showAttribute = "data-ws-show";
|
|
63
69
|
export const collapsedAttribute = "data-ws-collapsed";
|