@webstudio-is/react-sdk 0.78.0 → 0.80.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 +8 -2
- package/lib/cjs/css/normalize.js +23 -43
- package/lib/cjs/css/presets.js +1 -111
- package/lib/cjs/embed-template.js +45 -16
- package/lib/cjs/expression.js +137 -22
- package/lib/cjs/index.js +6 -2
- package/lib/cjs/props.js +32 -2
- package/lib/cjs/tree/create-elements-tree.js +7 -2
- package/lib/cjs/tree/root.js +15 -7
- package/lib/context.js +8 -2
- package/lib/css/normalize.js +23 -33
- package/lib/css/presets.js +1 -111
- package/lib/embed-template.js +45 -16
- package/lib/expression.js +137 -22
- package/lib/index.js +13 -5
- package/lib/props.js +32 -2
- package/lib/tree/create-elements-tree.js +10 -3
- package/lib/tree/root.js +16 -8
- package/lib/types/components/component-meta.d.ts +30 -0
- package/lib/types/context.d.ts +5 -2
- package/lib/types/css/normalize.d.ts +0 -520
- package/lib/types/css/presets.d.ts +0 -282
- package/lib/types/embed-template.d.ts +38 -0
- package/lib/types/expression.d.ts +12 -4
- package/lib/types/index.d.ts +1 -1
- package/lib/types/props.d.ts +10 -0
- package/lib/types/tree/create-elements-tree.d.ts +6 -5
- package/lib/types/tree/root.d.ts +5 -5
- package/package.json +17 -16
- package/src/context.tsx +17 -4
- package/src/css/normalize.ts +23 -32
- package/src/css/presets.ts +0 -110
- package/src/embed-template.test.ts +84 -4
- package/src/embed-template.ts +51 -18
- package/src/expression.test.ts +106 -12
- package/src/expression.ts +157 -26
- package/src/index.ts +6 -2
- package/src/props.ts +33 -3
- package/src/tree/create-elements-tree.tsx +19 -10
- package/src/tree/root.ts +24 -13
package/lib/css/presets.js
CHANGED
|
@@ -22,117 +22,7 @@ const outline = [
|
|
|
22
22
|
value: { type: "unit", value: 1, unit: "px" }
|
|
23
23
|
}
|
|
24
24
|
];
|
|
25
|
-
const margins = [
|
|
26
|
-
{
|
|
27
|
-
property: "marginTop",
|
|
28
|
-
value: { type: "unit", value: 0, unit: "px" }
|
|
29
|
-
},
|
|
30
|
-
{
|
|
31
|
-
property: "marginRight",
|
|
32
|
-
value: { type: "unit", value: 0, unit: "px" }
|
|
33
|
-
},
|
|
34
|
-
{
|
|
35
|
-
property: "marginBottom",
|
|
36
|
-
value: { type: "unit", value: 0, unit: "px" }
|
|
37
|
-
},
|
|
38
|
-
{
|
|
39
|
-
property: "marginLeft",
|
|
40
|
-
value: { type: "unit", value: 0, unit: "px" }
|
|
41
|
-
}
|
|
42
|
-
];
|
|
43
|
-
const verticalMargins = [
|
|
44
|
-
{
|
|
45
|
-
property: "marginTop",
|
|
46
|
-
value: { type: "unit", value: 0, unit: "px" }
|
|
47
|
-
},
|
|
48
|
-
{
|
|
49
|
-
property: "marginBottom",
|
|
50
|
-
value: { type: "unit", value: 0, unit: "px" }
|
|
51
|
-
}
|
|
52
|
-
];
|
|
53
|
-
const blockquote = [
|
|
54
|
-
...margins,
|
|
55
|
-
{
|
|
56
|
-
property: "paddingTop",
|
|
57
|
-
value: { type: "unit", value: 10, unit: "px" }
|
|
58
|
-
},
|
|
59
|
-
{
|
|
60
|
-
property: "paddingBottom",
|
|
61
|
-
value: { type: "unit", value: 10, unit: "px" }
|
|
62
|
-
},
|
|
63
|
-
{
|
|
64
|
-
property: "paddingLeft",
|
|
65
|
-
value: { type: "unit", value: 20, unit: "px" }
|
|
66
|
-
},
|
|
67
|
-
{
|
|
68
|
-
property: "paddingRight",
|
|
69
|
-
value: { type: "unit", value: 20, unit: "px" }
|
|
70
|
-
},
|
|
71
|
-
{
|
|
72
|
-
property: "borderLeftWidth",
|
|
73
|
-
value: { type: "unit", value: 5, unit: "px" }
|
|
74
|
-
},
|
|
75
|
-
{
|
|
76
|
-
property: "borderLeftStyle",
|
|
77
|
-
value: { type: "keyword", value: "solid" }
|
|
78
|
-
},
|
|
79
|
-
{
|
|
80
|
-
property: "borderLeftColor",
|
|
81
|
-
value: { type: "rgb", r: 226, g: 226, b: 226, alpha: 1 }
|
|
82
|
-
}
|
|
83
|
-
];
|
|
84
|
-
const h1 = [
|
|
85
|
-
...verticalMargins,
|
|
86
|
-
{
|
|
87
|
-
property: "fontSize",
|
|
88
|
-
value: { type: "unit", value: 38, unit: "px" }
|
|
89
|
-
}
|
|
90
|
-
];
|
|
91
|
-
const h2 = [
|
|
92
|
-
...verticalMargins,
|
|
93
|
-
{
|
|
94
|
-
property: "fontSize",
|
|
95
|
-
value: { type: "unit", value: 32, unit: "px" }
|
|
96
|
-
}
|
|
97
|
-
];
|
|
98
|
-
const h3 = [
|
|
99
|
-
...verticalMargins,
|
|
100
|
-
{
|
|
101
|
-
property: "fontSize",
|
|
102
|
-
value: { type: "unit", value: 24, unit: "px" }
|
|
103
|
-
}
|
|
104
|
-
];
|
|
105
|
-
const h4 = [
|
|
106
|
-
...verticalMargins,
|
|
107
|
-
{
|
|
108
|
-
property: "fontSize",
|
|
109
|
-
value: { type: "unit", value: 18, unit: "px" }
|
|
110
|
-
}
|
|
111
|
-
];
|
|
112
|
-
const h5 = [
|
|
113
|
-
...verticalMargins,
|
|
114
|
-
{
|
|
115
|
-
property: "fontSize",
|
|
116
|
-
value: { type: "unit", value: 14, unit: "px" }
|
|
117
|
-
}
|
|
118
|
-
];
|
|
119
|
-
const h6 = [
|
|
120
|
-
...verticalMargins,
|
|
121
|
-
{
|
|
122
|
-
property: "fontSize",
|
|
123
|
-
value: { type: "unit", value: 12, unit: "px" }
|
|
124
|
-
}
|
|
125
|
-
];
|
|
126
25
|
export {
|
|
127
|
-
blockquote,
|
|
128
26
|
borders,
|
|
129
|
-
|
|
130
|
-
h2,
|
|
131
|
-
h3,
|
|
132
|
-
h4,
|
|
133
|
-
h5,
|
|
134
|
-
h6,
|
|
135
|
-
margins,
|
|
136
|
-
outline,
|
|
137
|
-
verticalMargins
|
|
27
|
+
outline
|
|
138
28
|
};
|
package/lib/embed-template.js
CHANGED
|
@@ -6,11 +6,12 @@ const EmbedTemplateText = z.object({
|
|
|
6
6
|
type: z.literal("text"),
|
|
7
7
|
value: z.string()
|
|
8
8
|
});
|
|
9
|
+
const DataSourceVariableRef = z.object({
|
|
10
|
+
type: z.literal("variable"),
|
|
11
|
+
name: z.string()
|
|
12
|
+
});
|
|
9
13
|
const DataSourceRef = z.union([
|
|
10
|
-
|
|
11
|
-
type: z.literal("variable"),
|
|
12
|
-
name: z.string()
|
|
13
|
-
}),
|
|
14
|
+
DataSourceVariableRef,
|
|
14
15
|
z.object({
|
|
15
16
|
type: z.literal("expression"),
|
|
16
17
|
name: z.string(),
|
|
@@ -41,6 +42,16 @@ const EmbedTemplateProp = z.union([
|
|
|
41
42
|
name: z.string(),
|
|
42
43
|
dataSourceRef: z.optional(DataSourceRef),
|
|
43
44
|
value: z.array(z.string())
|
|
45
|
+
}),
|
|
46
|
+
z.object({
|
|
47
|
+
type: z.literal("action"),
|
|
48
|
+
name: z.string(),
|
|
49
|
+
value: z.array(
|
|
50
|
+
z.object({
|
|
51
|
+
type: z.literal("execute"),
|
|
52
|
+
code: z.string()
|
|
53
|
+
})
|
|
54
|
+
)
|
|
44
55
|
})
|
|
45
56
|
]);
|
|
46
57
|
const EmbedTemplateStyleDeclRaw = z.object({
|
|
@@ -71,6 +82,28 @@ const createInstancesFromTemplate = (treeTemplate, instances, props, dataSourceB
|
|
|
71
82
|
if (item.props) {
|
|
72
83
|
for (const prop of item.props) {
|
|
73
84
|
const propId = nanoid();
|
|
85
|
+
if (prop.type === "action") {
|
|
86
|
+
props.push({
|
|
87
|
+
id: propId,
|
|
88
|
+
instanceId,
|
|
89
|
+
type: "action",
|
|
90
|
+
name: prop.name,
|
|
91
|
+
value: prop.value.map((value) => {
|
|
92
|
+
return {
|
|
93
|
+
type: "execute",
|
|
94
|
+
// replace all references with variable names
|
|
95
|
+
code: validateExpression(value.code, {
|
|
96
|
+
effectful: true,
|
|
97
|
+
transformIdentifier: (ref) => {
|
|
98
|
+
const id = dataSourceByRef.get(ref)?.id ?? ref;
|
|
99
|
+
return encodeDataSourceVariable(id);
|
|
100
|
+
}
|
|
101
|
+
})
|
|
102
|
+
};
|
|
103
|
+
})
|
|
104
|
+
});
|
|
105
|
+
continue;
|
|
106
|
+
}
|
|
74
107
|
if (prop.dataSourceRef === void 0) {
|
|
75
108
|
props.push({ id: propId, instanceId, ...prop });
|
|
76
109
|
continue;
|
|
@@ -95,7 +128,13 @@ const createInstancesFromTemplate = (treeTemplate, instances, props, dataSourceB
|
|
|
95
128
|
id,
|
|
96
129
|
scopeInstanceId: instanceId,
|
|
97
130
|
name: dataSourceRef.name,
|
|
98
|
-
|
|
131
|
+
// replace all references with variable names
|
|
132
|
+
code: validateExpression(dataSourceRef.code, {
|
|
133
|
+
transformIdentifier: (ref) => {
|
|
134
|
+
const id2 = dataSourceByRef.get(ref)?.id ?? ref;
|
|
135
|
+
return encodeDataSourceVariable(id2);
|
|
136
|
+
}
|
|
137
|
+
})
|
|
99
138
|
};
|
|
100
139
|
dataSourceByRef.set(dataSourceRef.name, dataSource);
|
|
101
140
|
} else {
|
|
@@ -181,21 +220,11 @@ const generateDataFromEmbedTemplate = (treeTemplate, defaultBreakpointId) => {
|
|
|
181
220
|
styles,
|
|
182
221
|
defaultBreakpointId
|
|
183
222
|
);
|
|
184
|
-
const dataSources = [];
|
|
185
|
-
for (const dataSource of dataSourceByRef.values()) {
|
|
186
|
-
if (dataSource.type === "expression") {
|
|
187
|
-
dataSource.code = validateExpression(dataSource.code, (ref) => {
|
|
188
|
-
const id = dataSourceByRef.get(ref)?.id ?? ref;
|
|
189
|
-
return encodeDataSourceVariable(id);
|
|
190
|
-
});
|
|
191
|
-
}
|
|
192
|
-
dataSources.push(dataSource);
|
|
193
|
-
}
|
|
194
223
|
return {
|
|
195
224
|
children,
|
|
196
225
|
instances,
|
|
197
226
|
props,
|
|
198
|
-
dataSources,
|
|
227
|
+
dataSources: Array.from(dataSourceByRef.values()),
|
|
199
228
|
styleSourceSelections,
|
|
200
229
|
styleSources,
|
|
201
230
|
styles
|
package/lib/expression.js
CHANGED
|
@@ -1,18 +1,22 @@
|
|
|
1
1
|
import jsep from "jsep";
|
|
2
|
-
|
|
2
|
+
import jsepAssignment from "@jsep-plugin/assignment";
|
|
3
|
+
jsep.plugins.register(jsepAssignment);
|
|
4
|
+
const generateCode = (node, failOnForbidden, effectful, transformIdentifier) => {
|
|
3
5
|
if (node.type === "Identifier") {
|
|
4
|
-
return transformIdentifier(node.name);
|
|
6
|
+
return transformIdentifier(node.name, false);
|
|
5
7
|
}
|
|
6
8
|
if (node.type === "MemberExpression") {
|
|
7
9
|
if (failOnForbidden) {
|
|
8
10
|
const object2 = generateCode(
|
|
9
11
|
node.object,
|
|
10
12
|
false,
|
|
13
|
+
effectful,
|
|
11
14
|
transformIdentifier
|
|
12
15
|
);
|
|
13
16
|
const property2 = generateCode(
|
|
14
17
|
node.property,
|
|
15
18
|
false,
|
|
19
|
+
effectful,
|
|
16
20
|
transformIdentifier
|
|
17
21
|
);
|
|
18
22
|
throw Error(`Cannot access "${property2}" of "${object2}"`);
|
|
@@ -20,11 +24,13 @@ const generateCode = (node, failOnForbidden, transformIdentifier) => {
|
|
|
20
24
|
const object = generateCode(
|
|
21
25
|
node.object,
|
|
22
26
|
failOnForbidden,
|
|
27
|
+
effectful,
|
|
23
28
|
transformIdentifier
|
|
24
29
|
);
|
|
25
30
|
const property = generateCode(
|
|
26
31
|
node.property,
|
|
27
32
|
failOnForbidden,
|
|
33
|
+
effectful,
|
|
28
34
|
transformIdentifier
|
|
29
35
|
);
|
|
30
36
|
return `${object}.${property}`;
|
|
@@ -36,6 +42,7 @@ const generateCode = (node, failOnForbidden, transformIdentifier) => {
|
|
|
36
42
|
const arg = generateCode(
|
|
37
43
|
node.argument,
|
|
38
44
|
failOnForbidden,
|
|
45
|
+
effectful,
|
|
39
46
|
transformIdentifier
|
|
40
47
|
);
|
|
41
48
|
return `${node.operator}${arg}`;
|
|
@@ -44,18 +51,25 @@ const generateCode = (node, failOnForbidden, transformIdentifier) => {
|
|
|
44
51
|
const left = generateCode(
|
|
45
52
|
node.left,
|
|
46
53
|
failOnForbidden,
|
|
54
|
+
effectful,
|
|
47
55
|
transformIdentifier
|
|
48
56
|
);
|
|
49
57
|
const right = generateCode(
|
|
50
58
|
node.right,
|
|
51
59
|
failOnForbidden,
|
|
60
|
+
effectful,
|
|
52
61
|
transformIdentifier
|
|
53
62
|
);
|
|
54
63
|
return `${left} ${node.operator} ${right}`;
|
|
55
64
|
}
|
|
56
65
|
if (node.type === "ArrayExpression") {
|
|
57
66
|
const elements = node.elements.map(
|
|
58
|
-
(element) => generateCode(
|
|
67
|
+
(element) => generateCode(
|
|
68
|
+
element,
|
|
69
|
+
failOnForbidden,
|
|
70
|
+
effectful,
|
|
71
|
+
transformIdentifier
|
|
72
|
+
)
|
|
59
73
|
);
|
|
60
74
|
return `[${elements.join(", ")}]`;
|
|
61
75
|
}
|
|
@@ -64,6 +78,7 @@ const generateCode = (node, failOnForbidden, transformIdentifier) => {
|
|
|
64
78
|
const callee2 = generateCode(
|
|
65
79
|
node.callee,
|
|
66
80
|
false,
|
|
81
|
+
effectful,
|
|
67
82
|
transformIdentifier
|
|
68
83
|
);
|
|
69
84
|
throw Error(`Cannot call "${callee2}"`);
|
|
@@ -71,10 +86,11 @@ const generateCode = (node, failOnForbidden, transformIdentifier) => {
|
|
|
71
86
|
const callee = generateCode(
|
|
72
87
|
node.callee,
|
|
73
88
|
failOnForbidden,
|
|
89
|
+
effectful,
|
|
74
90
|
transformIdentifier
|
|
75
91
|
);
|
|
76
92
|
const args = node.arguments.map(
|
|
77
|
-
(arg) => generateCode(arg, failOnForbidden, transformIdentifier)
|
|
93
|
+
(arg) => generateCode(arg, failOnForbidden, effectful, transformIdentifier)
|
|
78
94
|
);
|
|
79
95
|
return `${callee}(${args.join(", ")})`;
|
|
80
96
|
}
|
|
@@ -90,12 +106,38 @@ const generateCode = (node, failOnForbidden, transformIdentifier) => {
|
|
|
90
106
|
if (node.type === "Compound") {
|
|
91
107
|
throw Error("Cannot use multiple expressions");
|
|
92
108
|
}
|
|
109
|
+
if (node.type === "AssignmentExpression") {
|
|
110
|
+
if (node.operator !== "=") {
|
|
111
|
+
throw Error(`Only "=" assignment operator is supported`);
|
|
112
|
+
}
|
|
113
|
+
if (effectful === false) {
|
|
114
|
+
throw Error(`Cannot use assignment in this expression`);
|
|
115
|
+
}
|
|
116
|
+
const left = generateCode(
|
|
117
|
+
node.left,
|
|
118
|
+
failOnForbidden,
|
|
119
|
+
effectful,
|
|
120
|
+
// override and mark all identifiers inside of left expression as assignee
|
|
121
|
+
(id) => transformIdentifier(id, true)
|
|
122
|
+
);
|
|
123
|
+
const right = generateCode(
|
|
124
|
+
node.right,
|
|
125
|
+
failOnForbidden,
|
|
126
|
+
effectful,
|
|
127
|
+
transformIdentifier
|
|
128
|
+
);
|
|
129
|
+
return `${left} ${node.operator} ${right}`;
|
|
130
|
+
}
|
|
131
|
+
if (node.type === "UpdateExpression") {
|
|
132
|
+
throw Error(`"${node.operator}" operator is not supported`);
|
|
133
|
+
}
|
|
93
134
|
node;
|
|
94
135
|
return "";
|
|
95
136
|
};
|
|
96
|
-
const validateExpression = (code,
|
|
137
|
+
const validateExpression = (code, options) => {
|
|
138
|
+
const { effectful = false, transformIdentifier = (id) => id } = options ?? {};
|
|
97
139
|
const expression = jsep(code);
|
|
98
|
-
return generateCode(expression, true, transformIdentifier);
|
|
140
|
+
return generateCode(expression, true, effectful, transformIdentifier);
|
|
99
141
|
};
|
|
100
142
|
const sortTopologically = (list, depsById, explored = /* @__PURE__ */ new Set(), sorted = []) => {
|
|
101
143
|
for (const id of list) {
|
|
@@ -111,19 +153,23 @@ const sortTopologically = (list, depsById, explored = /* @__PURE__ */ new Set(),
|
|
|
111
153
|
}
|
|
112
154
|
return sorted;
|
|
113
155
|
};
|
|
114
|
-
const
|
|
156
|
+
const generateComputingExpressions = (expressions, allowedVariables) => {
|
|
115
157
|
const depsById = /* @__PURE__ */ new Map();
|
|
158
|
+
const inputVariables = /* @__PURE__ */ new Set();
|
|
116
159
|
for (const [id, code] of expressions) {
|
|
117
160
|
const deps = /* @__PURE__ */ new Set();
|
|
118
|
-
validateExpression(code,
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
161
|
+
validateExpression(code, {
|
|
162
|
+
transformIdentifier: (identifier) => {
|
|
163
|
+
if (allowedVariables.has(identifier)) {
|
|
164
|
+
inputVariables.add(identifier);
|
|
165
|
+
return identifier;
|
|
166
|
+
}
|
|
167
|
+
if (expressions.has(identifier)) {
|
|
168
|
+
deps.add(identifier);
|
|
169
|
+
return identifier;
|
|
170
|
+
}
|
|
171
|
+
throw Error(`Unknown dependency "${identifier}"`);
|
|
125
172
|
}
|
|
126
|
-
throw Error(`Unknown dependency "${identifier}"`);
|
|
127
173
|
});
|
|
128
174
|
depsById.set(id, deps);
|
|
129
175
|
}
|
|
@@ -132,7 +178,7 @@ const generateExpressionsComputation = (variables, expressions) => {
|
|
|
132
178
|
depsById
|
|
133
179
|
);
|
|
134
180
|
let generatedCode = "";
|
|
135
|
-
for (const id of
|
|
181
|
+
for (const id of inputVariables) {
|
|
136
182
|
generatedCode += `const ${id} = _variables.get('${id}');
|
|
137
183
|
`;
|
|
138
184
|
}
|
|
@@ -153,10 +199,58 @@ const generateExpressionsComputation = (variables, expressions) => {
|
|
|
153
199
|
generatedCode += `]);`;
|
|
154
200
|
return generatedCode;
|
|
155
201
|
};
|
|
156
|
-
const
|
|
157
|
-
const generatedCode =
|
|
158
|
-
|
|
159
|
-
|
|
202
|
+
const executeComputingExpressions = (expressions, variables) => {
|
|
203
|
+
const generatedCode = generateComputingExpressions(
|
|
204
|
+
expressions,
|
|
205
|
+
new Set(variables.keys())
|
|
206
|
+
);
|
|
207
|
+
const executeFn = new Function("_variables", generatedCode);
|
|
208
|
+
const values = executeFn(variables);
|
|
209
|
+
return values;
|
|
210
|
+
};
|
|
211
|
+
const generateEffectfulExpression = (code, allowedVariables) => {
|
|
212
|
+
const inputVariables = /* @__PURE__ */ new Set();
|
|
213
|
+
const outputVariables = /* @__PURE__ */ new Set();
|
|
214
|
+
validateExpression(code, {
|
|
215
|
+
effectful: true,
|
|
216
|
+
transformIdentifier: (identifier, assignee) => {
|
|
217
|
+
if (allowedVariables.has(identifier)) {
|
|
218
|
+
if (assignee) {
|
|
219
|
+
outputVariables.add(identifier);
|
|
220
|
+
} else {
|
|
221
|
+
inputVariables.add(identifier);
|
|
222
|
+
}
|
|
223
|
+
return identifier;
|
|
224
|
+
}
|
|
225
|
+
throw Error(`Unknown dependency "${identifier}"`);
|
|
226
|
+
}
|
|
227
|
+
});
|
|
228
|
+
let generatedCode = "";
|
|
229
|
+
for (const id of inputVariables) {
|
|
230
|
+
generatedCode += `let ${id} = _variables.get('${id}');
|
|
231
|
+
`;
|
|
232
|
+
}
|
|
233
|
+
for (const id of outputVariables) {
|
|
234
|
+
if (inputVariables.has(id) === false) {
|
|
235
|
+
generatedCode += `let ${id};
|
|
236
|
+
`;
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
generatedCode += `${code};
|
|
240
|
+
`;
|
|
241
|
+
generatedCode += `return new Map([
|
|
242
|
+
`;
|
|
243
|
+
for (const id of outputVariables) {
|
|
244
|
+
generatedCode += ` ['${id}', ${id}],
|
|
245
|
+
`;
|
|
246
|
+
}
|
|
247
|
+
generatedCode += `]);`;
|
|
248
|
+
return generatedCode;
|
|
249
|
+
};
|
|
250
|
+
const executeEffectfulExpression = (code, variables) => {
|
|
251
|
+
const generatedCode = generateEffectfulExpression(
|
|
252
|
+
code,
|
|
253
|
+
new Set(variables.keys())
|
|
160
254
|
);
|
|
161
255
|
const executeFn = new Function("_variables", generatedCode);
|
|
162
256
|
const values = executeFn(variables);
|
|
@@ -167,6 +261,13 @@ const encodeDataSourceVariable = (id) => {
|
|
|
167
261
|
const encoded = id.replaceAll("-", "__DASH__");
|
|
168
262
|
return `${dataSourceVariablePrefix}${encoded}`;
|
|
169
263
|
};
|
|
264
|
+
const encodeVariablesMap = (values) => {
|
|
265
|
+
const encodedValues = /* @__PURE__ */ new Map();
|
|
266
|
+
for (const [id, value] of values) {
|
|
267
|
+
encodedValues.set(encodeDataSourceVariable(id), value);
|
|
268
|
+
}
|
|
269
|
+
return encodedValues;
|
|
270
|
+
};
|
|
170
271
|
const decodeDataSourceVariable = (name) => {
|
|
171
272
|
if (name.startsWith(dataSourceVariablePrefix)) {
|
|
172
273
|
const encoded = name.slice(dataSourceVariablePrefix.length);
|
|
@@ -174,10 +275,24 @@ const decodeDataSourceVariable = (name) => {
|
|
|
174
275
|
}
|
|
175
276
|
return;
|
|
176
277
|
};
|
|
278
|
+
const decodeVariablesMap = (values) => {
|
|
279
|
+
const decodedValues = /* @__PURE__ */ new Map();
|
|
280
|
+
for (const [name, value] of values) {
|
|
281
|
+
const id = decodeDataSourceVariable(name);
|
|
282
|
+
if (id !== void 0) {
|
|
283
|
+
decodedValues.set(id, value);
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
return decodedValues;
|
|
287
|
+
};
|
|
177
288
|
export {
|
|
178
289
|
decodeDataSourceVariable,
|
|
290
|
+
decodeVariablesMap,
|
|
179
291
|
encodeDataSourceVariable,
|
|
180
|
-
|
|
181
|
-
|
|
292
|
+
encodeVariablesMap,
|
|
293
|
+
executeComputingExpressions,
|
|
294
|
+
executeEffectfulExpression,
|
|
295
|
+
generateComputingExpressions,
|
|
296
|
+
generateEffectfulExpression,
|
|
182
297
|
validateExpression
|
|
183
298
|
};
|
package/lib/index.js
CHANGED
|
@@ -18,19 +18,27 @@ import {
|
|
|
18
18
|
import { ReactSdkContext } from "./context";
|
|
19
19
|
import {
|
|
20
20
|
validateExpression,
|
|
21
|
-
|
|
22
|
-
|
|
21
|
+
generateComputingExpressions,
|
|
22
|
+
executeComputingExpressions,
|
|
23
|
+
generateEffectfulExpression,
|
|
24
|
+
executeEffectfulExpression,
|
|
23
25
|
encodeDataSourceVariable,
|
|
24
|
-
|
|
26
|
+
encodeVariablesMap,
|
|
27
|
+
decodeDataSourceVariable,
|
|
28
|
+
decodeVariablesMap
|
|
25
29
|
} from "./expression";
|
|
26
30
|
export {
|
|
27
31
|
ReactSdkContext,
|
|
28
32
|
componentCategories,
|
|
29
33
|
decodeDataSourceVariable,
|
|
34
|
+
decodeVariablesMap,
|
|
30
35
|
defaultStates,
|
|
31
36
|
encodeDataSourceVariable,
|
|
32
|
-
|
|
33
|
-
|
|
37
|
+
encodeVariablesMap,
|
|
38
|
+
executeComputingExpressions,
|
|
39
|
+
executeEffectfulExpression,
|
|
40
|
+
generateComputingExpressions,
|
|
41
|
+
generateEffectfulExpression,
|
|
34
42
|
getInstanceIdFromComponentProps,
|
|
35
43
|
stateCategories,
|
|
36
44
|
useInstanceProps,
|
package/lib/props.js
CHANGED
|
@@ -16,7 +16,13 @@ const getPropsByInstanceId = (props) => {
|
|
|
16
16
|
return propsByInstanceId;
|
|
17
17
|
};
|
|
18
18
|
const useInstanceProps = (instanceId) => {
|
|
19
|
-
const {
|
|
19
|
+
const {
|
|
20
|
+
propsByInstanceIdStore,
|
|
21
|
+
dataSourceValuesStore,
|
|
22
|
+
executeEffectfulExpression,
|
|
23
|
+
setDataSourceValues,
|
|
24
|
+
renderer
|
|
25
|
+
} = useContext(ReactSdkContext);
|
|
20
26
|
const instancePropsObjectStore = useMemo(() => {
|
|
21
27
|
return computed(
|
|
22
28
|
[propsByInstanceIdStore, dataSourceValuesStore],
|
|
@@ -38,12 +44,36 @@ const useInstanceProps = (instanceId) => {
|
|
|
38
44
|
}
|
|
39
45
|
continue;
|
|
40
46
|
}
|
|
47
|
+
if (prop.type === "action") {
|
|
48
|
+
instancePropsObject2[prop.name] = () => {
|
|
49
|
+
if (renderer === "canvas") {
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
for (const value of prop.value) {
|
|
53
|
+
if (value.type === "execute") {
|
|
54
|
+
const newValues = executeEffectfulExpression(
|
|
55
|
+
value.code,
|
|
56
|
+
dataSourceValues
|
|
57
|
+
);
|
|
58
|
+
setDataSourceValues(newValues);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
continue;
|
|
63
|
+
}
|
|
41
64
|
instancePropsObject2[prop.name] = prop.value;
|
|
42
65
|
}
|
|
43
66
|
return instancePropsObject2;
|
|
44
67
|
}
|
|
45
68
|
);
|
|
46
|
-
}, [
|
|
69
|
+
}, [
|
|
70
|
+
propsByInstanceIdStore,
|
|
71
|
+
dataSourceValuesStore,
|
|
72
|
+
instanceId,
|
|
73
|
+
renderer,
|
|
74
|
+
executeEffectfulExpression,
|
|
75
|
+
setDataSourceValues
|
|
76
|
+
]);
|
|
47
77
|
const instancePropsObject = useStore(instancePropsObjectStore);
|
|
48
78
|
return instancePropsObject;
|
|
49
79
|
};
|
|
@@ -1,7 +1,9 @@
|
|
|
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 {
|
|
4
|
+
import {
|
|
5
|
+
ReactSdkContext
|
|
6
|
+
} from "../context";
|
|
5
7
|
const createElementsTree = ({
|
|
6
8
|
renderer,
|
|
7
9
|
imageBaseUrl,
|
|
@@ -12,6 +14,7 @@ const createElementsTree = ({
|
|
|
12
14
|
assetsStore,
|
|
13
15
|
pagesStore,
|
|
14
16
|
dataSourceValuesStore,
|
|
17
|
+
executeEffectfulExpression,
|
|
15
18
|
onDataSourceUpdate,
|
|
16
19
|
Component,
|
|
17
20
|
components
|
|
@@ -52,7 +55,9 @@ const createElementsTree = ({
|
|
|
52
55
|
renderer,
|
|
53
56
|
imageBaseUrl,
|
|
54
57
|
assetBaseUrl,
|
|
55
|
-
|
|
58
|
+
executeEffectfulExpression,
|
|
59
|
+
setDataSourceValues: onDataSourceUpdate,
|
|
60
|
+
setBoundDataSourceValue: (instanceId, propName, value) => {
|
|
56
61
|
const propsByInstanceId = propsByInstanceIdStore.get();
|
|
57
62
|
const props = propsByInstanceId.get(instanceId);
|
|
58
63
|
const prop = props?.find((prop2) => prop2.name === propName);
|
|
@@ -60,7 +65,9 @@ const createElementsTree = ({
|
|
|
60
65
|
throw Error(`${propName} is not data source`);
|
|
61
66
|
}
|
|
62
67
|
const dataSourceId = prop.value;
|
|
63
|
-
|
|
68
|
+
const newValues = /* @__PURE__ */ new Map();
|
|
69
|
+
newValues.set(dataSourceId, value);
|
|
70
|
+
onDataSourceUpdate(newValues);
|
|
64
71
|
}
|
|
65
72
|
},
|
|
66
73
|
children: root
|
package/lib/tree/root.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { useRef } from "react";
|
|
1
|
+
import { useRef, useCallback } from "react";
|
|
2
2
|
import {
|
|
3
3
|
atom,
|
|
4
4
|
computed
|
|
@@ -8,7 +8,8 @@ import { WebstudioComponent } from "./webstudio-component";
|
|
|
8
8
|
import { getPropsByInstanceId } from "../props";
|
|
9
9
|
const InstanceRoot = ({
|
|
10
10
|
data,
|
|
11
|
-
|
|
11
|
+
executeComputingExpressions,
|
|
12
|
+
executeEffectfulExpression,
|
|
12
13
|
Component,
|
|
13
14
|
components
|
|
14
15
|
}) => {
|
|
@@ -30,7 +31,7 @@ const InstanceRoot = ({
|
|
|
30
31
|
}
|
|
31
32
|
}
|
|
32
33
|
try {
|
|
33
|
-
const result =
|
|
34
|
+
const result = executeComputingExpressions(dataSourceValues);
|
|
34
35
|
for (const [id, value] of result) {
|
|
35
36
|
dataSourceValues.set(id, value);
|
|
36
37
|
}
|
|
@@ -42,6 +43,16 @@ const InstanceRoot = ({
|
|
|
42
43
|
);
|
|
43
44
|
}
|
|
44
45
|
const dataSourceValuesStore = dataSourceValuesStoreRef.current;
|
|
46
|
+
const onDataSourceUpdate = useCallback(
|
|
47
|
+
(newValues) => {
|
|
48
|
+
const dataSourceVariables = new Map(dataSourceVariablesStore.get());
|
|
49
|
+
for (const [dataSourceId, value] of newValues) {
|
|
50
|
+
dataSourceVariables.set(dataSourceId, value);
|
|
51
|
+
}
|
|
52
|
+
dataSourceVariablesStore.set(dataSourceVariables);
|
|
53
|
+
},
|
|
54
|
+
[dataSourceVariablesStore]
|
|
55
|
+
);
|
|
45
56
|
return createElementsTree({
|
|
46
57
|
imageBaseUrl: data.params?.imageBaseUrl ?? "/",
|
|
47
58
|
assetBaseUrl: data.params?.assetBaseUrl ?? "/",
|
|
@@ -52,12 +63,9 @@ const InstanceRoot = ({
|
|
|
52
63
|
),
|
|
53
64
|
assetsStore: atom(new Map(data.assets.map((asset) => [asset.id, asset]))),
|
|
54
65
|
pagesStore: atom(new Map(data.pages.map((page) => [page.id, page]))),
|
|
66
|
+
executeEffectfulExpression,
|
|
55
67
|
dataSourceValuesStore,
|
|
56
|
-
onDataSourceUpdate
|
|
57
|
-
const dataSourceVariables = new Map(dataSourceVariablesStore.get());
|
|
58
|
-
dataSourceVariables.set(dataSourceId, value);
|
|
59
|
-
dataSourceVariablesStore.set(dataSourceVariables);
|
|
60
|
-
},
|
|
68
|
+
onDataSourceUpdate,
|
|
61
69
|
Component: Component ?? WebstudioComponent,
|
|
62
70
|
components
|
|
63
71
|
});
|