@webstudio-is/react-sdk 0.99.1-90cd039.0 → 0.99.1-de264e0.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/index.js +437 -168
- package/lib/types/component-generator.d.ts +249 -0
- package/lib/types/component-generator.test.d.ts +1 -0
- package/lib/types/context.d.ts +2 -5
- package/lib/types/expression.d.ts +106 -0
- package/lib/types/generator.d.ts +2 -4
- package/lib/types/index.d.ts +2 -1
- package/lib/types/tree/create-elements-tree.d.ts +3 -5
- package/package.json +8 -7
package/lib/index.js
CHANGED
|
@@ -29,13 +29,7 @@ var ReactSdkContext = createContext({
|
|
|
29
29
|
propsByInstanceIdStore: atom(/* @__PURE__ */ new Map()),
|
|
30
30
|
assetsStore: atom(/* @__PURE__ */ new Map()),
|
|
31
31
|
pagesStore: atom(/* @__PURE__ */ new Map()),
|
|
32
|
-
|
|
33
|
-
executeEffectfulExpression: () => {
|
|
34
|
-
throw Error("React SDK executeEffectfulExpression is not implemented");
|
|
35
|
-
},
|
|
36
|
-
setDataSourceValues: () => {
|
|
37
|
-
throw Error("React SDK setDataSourceValues is not implemented");
|
|
38
|
-
},
|
|
32
|
+
dataSourcesLogicStore: atom(/* @__PURE__ */ new Map()),
|
|
39
33
|
indexesWithinAncestors: /* @__PURE__ */ new Map()
|
|
40
34
|
});
|
|
41
35
|
|
|
@@ -51,9 +45,7 @@ var createElementsTree = ({
|
|
|
51
45
|
propsByInstanceIdStore,
|
|
52
46
|
assetsStore,
|
|
53
47
|
pagesStore,
|
|
54
|
-
|
|
55
|
-
executeEffectfulExpression: executeEffectfulExpression2,
|
|
56
|
-
onDataSourceUpdate,
|
|
48
|
+
dataSourcesLogicStore,
|
|
57
49
|
indexesWithinAncestors,
|
|
58
50
|
Component,
|
|
59
51
|
components,
|
|
@@ -90,14 +82,12 @@ var createElementsTree = ({
|
|
|
90
82
|
propsByInstanceIdStore,
|
|
91
83
|
assetsStore,
|
|
92
84
|
pagesStore,
|
|
93
|
-
|
|
85
|
+
dataSourcesLogicStore,
|
|
94
86
|
renderer,
|
|
95
87
|
imageLoader,
|
|
96
88
|
assetBaseUrl,
|
|
97
89
|
imageBaseUrl,
|
|
98
|
-
indexesWithinAncestors
|
|
99
|
-
executeEffectfulExpression: executeEffectfulExpression2,
|
|
100
|
-
setDataSourceValues: onDataSourceUpdate
|
|
90
|
+
indexesWithinAncestors
|
|
101
91
|
},
|
|
102
92
|
children: root
|
|
103
93
|
}
|
|
@@ -160,8 +150,7 @@ var createInstanceElement = ({
|
|
|
160
150
|
|
|
161
151
|
// src/tree/root.ts
|
|
162
152
|
import {
|
|
163
|
-
useRef
|
|
164
|
-
useCallback
|
|
153
|
+
useRef
|
|
165
154
|
} from "react";
|
|
166
155
|
import {
|
|
167
156
|
atom as atom2,
|
|
@@ -190,16 +179,14 @@ var getPropsByInstanceId = (props) => {
|
|
|
190
179
|
var useInstanceProps = (instanceId) => {
|
|
191
180
|
const {
|
|
192
181
|
propsByInstanceIdStore,
|
|
193
|
-
|
|
194
|
-
executeEffectfulExpression: executeEffectfulExpression2,
|
|
195
|
-
setDataSourceValues,
|
|
182
|
+
dataSourcesLogicStore,
|
|
196
183
|
indexesWithinAncestors
|
|
197
184
|
} = useContext(ReactSdkContext);
|
|
198
185
|
const index = indexesWithinAncestors.get(instanceId);
|
|
199
186
|
const instancePropsObjectStore = useMemo(() => {
|
|
200
187
|
return computed(
|
|
201
|
-
[propsByInstanceIdStore,
|
|
202
|
-
(propsByInstanceId,
|
|
188
|
+
[propsByInstanceIdStore, dataSourcesLogicStore],
|
|
189
|
+
(propsByInstanceId, dataSourcesLogic) => {
|
|
203
190
|
const instancePropsObject2 = {};
|
|
204
191
|
if (index !== void 0) {
|
|
205
192
|
instancePropsObject2[indexAttribute] = index.toString();
|
|
@@ -214,29 +201,17 @@ var useInstanceProps = (instanceId) => {
|
|
|
214
201
|
}
|
|
215
202
|
if (prop.type === "dataSource") {
|
|
216
203
|
const dataSourceId = prop.value;
|
|
217
|
-
const value =
|
|
204
|
+
const value = dataSourcesLogic.get(dataSourceId);
|
|
218
205
|
if (value !== void 0) {
|
|
219
206
|
instancePropsObject2[prop.name] = value;
|
|
220
207
|
}
|
|
221
208
|
continue;
|
|
222
209
|
}
|
|
223
210
|
if (prop.type === "action") {
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
for (const [i, name] of value.args.entries()) {
|
|
229
|
-
argsMap.set(name, args[i]);
|
|
230
|
-
}
|
|
231
|
-
const newValues = executeEffectfulExpression2(
|
|
232
|
-
value.code,
|
|
233
|
-
argsMap,
|
|
234
|
-
dataSourceValues
|
|
235
|
-
);
|
|
236
|
-
setDataSourceValues(newValues);
|
|
237
|
-
}
|
|
238
|
-
}
|
|
239
|
-
};
|
|
211
|
+
const action = dataSourcesLogic.get(prop.id);
|
|
212
|
+
if (typeof action === "function") {
|
|
213
|
+
instancePropsObject2[prop.name] = action;
|
|
214
|
+
}
|
|
240
215
|
continue;
|
|
241
216
|
}
|
|
242
217
|
instancePropsObject2[prop.name] = prop.value;
|
|
@@ -244,14 +219,7 @@ var useInstanceProps = (instanceId) => {
|
|
|
244
219
|
return instancePropsObject2;
|
|
245
220
|
}
|
|
246
221
|
);
|
|
247
|
-
}, [
|
|
248
|
-
propsByInstanceIdStore,
|
|
249
|
-
dataSourceValuesStore,
|
|
250
|
-
instanceId,
|
|
251
|
-
executeEffectfulExpression2,
|
|
252
|
-
setDataSourceValues,
|
|
253
|
-
index
|
|
254
|
-
]);
|
|
222
|
+
}, [propsByInstanceIdStore, dataSourcesLogicStore, instanceId, index]);
|
|
255
223
|
const instancePropsObject = useStore(instancePropsObjectStore);
|
|
256
224
|
return instancePropsObject;
|
|
257
225
|
};
|
|
@@ -415,51 +383,35 @@ var InstanceRoot = ({
|
|
|
415
383
|
scripts,
|
|
416
384
|
imageLoader
|
|
417
385
|
}) => {
|
|
418
|
-
const {
|
|
419
|
-
indexesWithinAncestors,
|
|
420
|
-
executeComputingExpressions: executeComputingExpressions2,
|
|
421
|
-
executeEffectfulExpression: executeEffectfulExpression2
|
|
422
|
-
} = utils;
|
|
386
|
+
const { indexesWithinAncestors, getDataSourcesLogic } = utils;
|
|
423
387
|
const dataSourceVariablesStoreRef = useRef(void 0);
|
|
424
388
|
if (dataSourceVariablesStoreRef.current === void 0) {
|
|
425
389
|
dataSourceVariablesStoreRef.current = atom2(/* @__PURE__ */ new Map());
|
|
426
390
|
}
|
|
427
391
|
const dataSourceVariablesStore = dataSourceVariablesStoreRef.current;
|
|
428
|
-
const
|
|
429
|
-
if (
|
|
430
|
-
|
|
392
|
+
const dataSourcesLogicStoreRef = useRef(void 0);
|
|
393
|
+
if (dataSourcesLogicStoreRef.current === void 0) {
|
|
394
|
+
dataSourcesLogicStoreRef.current = computed2(
|
|
431
395
|
dataSourceVariablesStore,
|
|
432
396
|
(dataSourceVariables) => {
|
|
433
|
-
const dataSourceValues = /* @__PURE__ */ new Map();
|
|
434
|
-
for (const [dataSourceId, dataSource] of data.build.dataSources) {
|
|
435
|
-
if (dataSource.type === "variable") {
|
|
436
|
-
const value = dataSourceVariables.get(dataSourceId) ?? dataSource.value.value;
|
|
437
|
-
dataSourceValues.set(dataSourceId, value);
|
|
438
|
-
}
|
|
439
|
-
}
|
|
440
397
|
try {
|
|
441
|
-
const
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
398
|
+
const getVariable = (id) => {
|
|
399
|
+
return dataSourceVariables.get(id);
|
|
400
|
+
};
|
|
401
|
+
const setVariable = (id, value) => {
|
|
402
|
+
const dataSourceVariables2 = new Map(dataSourceVariablesStore.get());
|
|
403
|
+
dataSourceVariables2.set(id, value);
|
|
404
|
+
dataSourceVariablesStore.set(dataSourceVariables2);
|
|
405
|
+
};
|
|
406
|
+
return getDataSourcesLogic(getVariable, setVariable);
|
|
445
407
|
} catch (error) {
|
|
446
408
|
console.error(error);
|
|
447
409
|
}
|
|
448
|
-
return
|
|
410
|
+
return /* @__PURE__ */ new Map();
|
|
449
411
|
}
|
|
450
412
|
);
|
|
451
413
|
}
|
|
452
|
-
const
|
|
453
|
-
const onDataSourceUpdate = useCallback(
|
|
454
|
-
(newValues) => {
|
|
455
|
-
const dataSourceVariables = new Map(dataSourceVariablesStore.get());
|
|
456
|
-
for (const [dataSourceId, value] of newValues) {
|
|
457
|
-
dataSourceVariables.set(dataSourceId, value);
|
|
458
|
-
}
|
|
459
|
-
dataSourceVariablesStore.set(dataSourceVariables);
|
|
460
|
-
},
|
|
461
|
-
[dataSourceVariablesStore]
|
|
462
|
-
);
|
|
414
|
+
const dataSourcesLogicStore = dataSourcesLogicStoreRef.current;
|
|
463
415
|
return createElementsTree({
|
|
464
416
|
imageLoader,
|
|
465
417
|
imageBaseUrl: data.params?.imageBaseUrl ?? "/",
|
|
@@ -472,9 +424,7 @@ var InstanceRoot = ({
|
|
|
472
424
|
assetsStore: atom2(new Map(data.assets.map((asset) => [asset.id, asset]))),
|
|
473
425
|
pagesStore: atom2(new Map(data.pages.map((page) => [page.id, page]))),
|
|
474
426
|
indexesWithinAncestors,
|
|
475
|
-
|
|
476
|
-
dataSourceValuesStore,
|
|
477
|
-
onDataSourceUpdate,
|
|
427
|
+
dataSourcesLogicStore,
|
|
478
428
|
Component: Component ?? WebstudioComponent,
|
|
479
429
|
components,
|
|
480
430
|
scripts
|
|
@@ -1111,6 +1061,118 @@ var decodeVariablesMap = (values) => {
|
|
|
1111
1061
|
}
|
|
1112
1062
|
return decodedValues;
|
|
1113
1063
|
};
|
|
1064
|
+
var generateDataSources = ({
|
|
1065
|
+
scope,
|
|
1066
|
+
typed = false,
|
|
1067
|
+
dataSources,
|
|
1068
|
+
props
|
|
1069
|
+
}) => {
|
|
1070
|
+
const variables = /* @__PURE__ */ new Map();
|
|
1071
|
+
let body = "";
|
|
1072
|
+
const output = /* @__PURE__ */ new Map();
|
|
1073
|
+
const depsById = /* @__PURE__ */ new Map();
|
|
1074
|
+
const codeById = /* @__PURE__ */ new Map();
|
|
1075
|
+
for (const dataSource of dataSources.values()) {
|
|
1076
|
+
if (dataSource.type === "expression") {
|
|
1077
|
+
const deps = /* @__PURE__ */ new Set();
|
|
1078
|
+
const newCode = validateExpression(dataSource.code, {
|
|
1079
|
+
transformIdentifier: (identifier) => {
|
|
1080
|
+
const depId = decodeDataSourceVariable(identifier);
|
|
1081
|
+
const dep = depId ? dataSources.get(depId) : void 0;
|
|
1082
|
+
if (dep) {
|
|
1083
|
+
deps.add(dep.id);
|
|
1084
|
+
return scope.getName(dep.id, dep.name);
|
|
1085
|
+
}
|
|
1086
|
+
console.error(`Unknown dependency "${identifier}"`);
|
|
1087
|
+
return identifier;
|
|
1088
|
+
}
|
|
1089
|
+
});
|
|
1090
|
+
depsById.set(dataSource.id, deps);
|
|
1091
|
+
codeById.set(dataSource.id, newCode);
|
|
1092
|
+
}
|
|
1093
|
+
}
|
|
1094
|
+
const sortedDataSources = sortTopologically(
|
|
1095
|
+
new Set(dataSources.keys()),
|
|
1096
|
+
depsById
|
|
1097
|
+
);
|
|
1098
|
+
for (const dataSourceId of sortedDataSources) {
|
|
1099
|
+
const dataSource = dataSources.get(dataSourceId);
|
|
1100
|
+
if (dataSource?.type === "variable") {
|
|
1101
|
+
const valueName = scope.getName(dataSource.id, dataSource.name);
|
|
1102
|
+
const setterName = scope.getName(
|
|
1103
|
+
`set$${dataSource.id}`,
|
|
1104
|
+
`set$${dataSource.name}`
|
|
1105
|
+
);
|
|
1106
|
+
const initialValue = dataSource.value.value;
|
|
1107
|
+
output.set(dataSource.id, valueName);
|
|
1108
|
+
variables.set(dataSource.id, { valueName, setterName, initialValue });
|
|
1109
|
+
}
|
|
1110
|
+
if (dataSource?.type === "expression") {
|
|
1111
|
+
const name = scope.getName(dataSource.id, dataSource.name);
|
|
1112
|
+
const code = codeById.get(dataSourceId);
|
|
1113
|
+
output.set(dataSource.id, name);
|
|
1114
|
+
body += `let ${name} = (${code});
|
|
1115
|
+
`;
|
|
1116
|
+
}
|
|
1117
|
+
}
|
|
1118
|
+
for (const prop of props.values()) {
|
|
1119
|
+
if (prop.type !== "action") {
|
|
1120
|
+
continue;
|
|
1121
|
+
}
|
|
1122
|
+
const name = scope.getName(prop.id, prop.name);
|
|
1123
|
+
output.set(prop.id, name);
|
|
1124
|
+
const setters = /* @__PURE__ */ new Set();
|
|
1125
|
+
let args = void 0;
|
|
1126
|
+
let newCode = "";
|
|
1127
|
+
for (const value of prop.value) {
|
|
1128
|
+
args = value.args;
|
|
1129
|
+
newCode += validateExpression(value.code, {
|
|
1130
|
+
effectful: true,
|
|
1131
|
+
transformIdentifier: (identifier, assignee) => {
|
|
1132
|
+
if (args?.includes(identifier)) {
|
|
1133
|
+
return identifier;
|
|
1134
|
+
}
|
|
1135
|
+
const depId = decodeDataSourceVariable(identifier);
|
|
1136
|
+
const dep = depId ? dataSources.get(depId) : void 0;
|
|
1137
|
+
if (dep) {
|
|
1138
|
+
const name2 = scope.getName(dep.id, dep.name);
|
|
1139
|
+
if (assignee) {
|
|
1140
|
+
setters.add(dep.id);
|
|
1141
|
+
}
|
|
1142
|
+
return name2;
|
|
1143
|
+
}
|
|
1144
|
+
console.error(`Unknown dependency "${identifier}"`);
|
|
1145
|
+
return identifier;
|
|
1146
|
+
}
|
|
1147
|
+
});
|
|
1148
|
+
newCode += `
|
|
1149
|
+
`;
|
|
1150
|
+
}
|
|
1151
|
+
if (args === void 0) {
|
|
1152
|
+
continue;
|
|
1153
|
+
}
|
|
1154
|
+
if (typed) {
|
|
1155
|
+
args = args.map((arg) => `${arg}: any`);
|
|
1156
|
+
}
|
|
1157
|
+
body += `let ${name} = (${args.join(", ")}) => {
|
|
1158
|
+
`;
|
|
1159
|
+
body += newCode;
|
|
1160
|
+
for (const dataSourceId of setters.values()) {
|
|
1161
|
+
const variable = variables.get(dataSourceId);
|
|
1162
|
+
if (variable) {
|
|
1163
|
+
body += `${variable.setterName}(${variable.valueName})
|
|
1164
|
+
`;
|
|
1165
|
+
}
|
|
1166
|
+
}
|
|
1167
|
+
body += `}
|
|
1168
|
+
`;
|
|
1169
|
+
}
|
|
1170
|
+
return {
|
|
1171
|
+
variables,
|
|
1172
|
+
body,
|
|
1173
|
+
output
|
|
1174
|
+
};
|
|
1175
|
+
};
|
|
1114
1176
|
|
|
1115
1177
|
// src/embed-template.ts
|
|
1116
1178
|
var EmbedTemplateText = z2.object({
|
|
@@ -1509,7 +1571,7 @@ var WsComponentMeta = z3.object({
|
|
|
1509
1571
|
});
|
|
1510
1572
|
|
|
1511
1573
|
// src/component-renderer.tsx
|
|
1512
|
-
import { getStyleDeclKey } from "@webstudio-is/sdk";
|
|
1574
|
+
import { createScope, getStyleDeclKey } from "@webstudio-is/sdk";
|
|
1513
1575
|
|
|
1514
1576
|
// src/instance-utils.ts
|
|
1515
1577
|
var getIndexesWithinAncestors = (metas, instances, rootIds) => {
|
|
@@ -1644,25 +1706,46 @@ var renderComponentTemplate = ({
|
|
|
1644
1706
|
new Map(instances),
|
|
1645
1707
|
["root"]
|
|
1646
1708
|
),
|
|
1647
|
-
|
|
1648
|
-
const
|
|
1649
|
-
|
|
1650
|
-
|
|
1651
|
-
|
|
1652
|
-
|
|
1653
|
-
|
|
1654
|
-
|
|
1655
|
-
|
|
1656
|
-
executeComputingExpressions(
|
|
1657
|
-
expressions,
|
|
1658
|
-
encodeVariablesMap(values)
|
|
1709
|
+
getDataSourcesLogic(getVariable, setVariable) {
|
|
1710
|
+
const { variables, body, output } = generateDataSources({
|
|
1711
|
+
scope: createScope(["_getVariable", "_setVariable", "_output"]),
|
|
1712
|
+
props: new Map(data.props.map((prop) => [prop.id, prop])),
|
|
1713
|
+
dataSources: new Map(
|
|
1714
|
+
data.dataSources.map((dataSource) => [
|
|
1715
|
+
dataSource.id,
|
|
1716
|
+
dataSource
|
|
1717
|
+
])
|
|
1659
1718
|
)
|
|
1660
|
-
);
|
|
1661
|
-
|
|
1662
|
-
|
|
1663
|
-
|
|
1664
|
-
|
|
1665
|
-
|
|
1719
|
+
});
|
|
1720
|
+
let generatedCode = "";
|
|
1721
|
+
for (const [dataSourceId, variable] of variables) {
|
|
1722
|
+
const { valueName, setterName } = variable;
|
|
1723
|
+
const initialValue = JSON.stringify(variable.initialValue);
|
|
1724
|
+
generatedCode += `let ${valueName} = _getVariable("${dataSourceId}") ?? ${initialValue};
|
|
1725
|
+
`;
|
|
1726
|
+
generatedCode += `let ${setterName} = (value) => _setVariable("${dataSourceId}", value);
|
|
1727
|
+
`;
|
|
1728
|
+
}
|
|
1729
|
+
generatedCode += body;
|
|
1730
|
+
generatedCode += `let _output = new Map();
|
|
1731
|
+
`;
|
|
1732
|
+
for (const [dataSourceId, variableName] of output) {
|
|
1733
|
+
generatedCode += `_output.set('${dataSourceId}', ${variableName})
|
|
1734
|
+
`;
|
|
1735
|
+
}
|
|
1736
|
+
generatedCode += `return _output
|
|
1737
|
+
`;
|
|
1738
|
+
try {
|
|
1739
|
+
const executeFn = new Function(
|
|
1740
|
+
"_getVariable",
|
|
1741
|
+
"_setVariable",
|
|
1742
|
+
generatedCode
|
|
1743
|
+
);
|
|
1744
|
+
return executeFn(getVariable, setVariable);
|
|
1745
|
+
} catch (error) {
|
|
1746
|
+
console.error(error);
|
|
1747
|
+
}
|
|
1748
|
+
return /* @__PURE__ */ new Map();
|
|
1666
1749
|
}
|
|
1667
1750
|
},
|
|
1668
1751
|
Component: WebstudioComponent,
|
|
@@ -1687,6 +1770,9 @@ var getClosestInstance = (instancePath, currentInstance, closestComponent) => {
|
|
|
1687
1770
|
};
|
|
1688
1771
|
|
|
1689
1772
|
// src/generator.ts
|
|
1773
|
+
import {
|
|
1774
|
+
createScope as createScope2
|
|
1775
|
+
} from "@webstudio-is/sdk";
|
|
1690
1776
|
var generateUtilsExport = (siteData) => {
|
|
1691
1777
|
const indexesWithinAncestors = getIndexesWithinAncestors(
|
|
1692
1778
|
siteData.metas,
|
|
@@ -1705,91 +1791,272 @@ var generateUtilsExport = (siteData) => {
|
|
|
1705
1791
|
${indexesWithinAncestorsEntries}
|
|
1706
1792
|
]);
|
|
1707
1793
|
`;
|
|
1708
|
-
const variables
|
|
1709
|
-
|
|
1710
|
-
|
|
1711
|
-
|
|
1712
|
-
|
|
1713
|
-
|
|
1714
|
-
|
|
1715
|
-
|
|
1716
|
-
|
|
1794
|
+
const { variables, body, output } = generateDataSources({
|
|
1795
|
+
scope: createScope2(["_getVariable", "_setVariable", "_output"]),
|
|
1796
|
+
typed: true,
|
|
1797
|
+
dataSources: siteData.dataSources,
|
|
1798
|
+
props: siteData.props
|
|
1799
|
+
});
|
|
1800
|
+
let generatedDataSources = "";
|
|
1801
|
+
generatedDataSources += `const getDataSourcesLogic = (
|
|
1802
|
+
`;
|
|
1803
|
+
generatedDataSources += ` _getVariable: (id: string) => unknown,
|
|
1804
|
+
`;
|
|
1805
|
+
generatedDataSources += ` _setVariable: (id: string, value: unknown) => void
|
|
1806
|
+
`;
|
|
1807
|
+
generatedDataSources += `) => {
|
|
1808
|
+
`;
|
|
1809
|
+
for (const [dataSourceId, variable] of variables) {
|
|
1810
|
+
const { valueName, setterName } = variable;
|
|
1811
|
+
const initialValue = JSON.stringify(variable.initialValue);
|
|
1812
|
+
generatedDataSources += `let ${valueName} = _getVariable("${dataSourceId}") ?? ${initialValue};
|
|
1813
|
+
`;
|
|
1814
|
+
generatedDataSources += `let ${setterName} = (value: unknown) => _setVariable("${dataSourceId}", value);
|
|
1815
|
+
`;
|
|
1717
1816
|
}
|
|
1718
|
-
|
|
1719
|
-
|
|
1720
|
-
|
|
1721
|
-
|
|
1722
|
-
${
|
|
1723
|
-
};
|
|
1724
|
-
const executeComputingExpressions = (variables: Map<string, unknown>) => {
|
|
1725
|
-
const encodedvariables = sdk.encodeVariablesMap(variables);
|
|
1726
|
-
const encodedResult = rawExecuteComputingExpressions(encodedvariables);
|
|
1727
|
-
return sdk.decodeVariablesMap(encodedResult);
|
|
1728
|
-
};
|
|
1729
|
-
`;
|
|
1730
|
-
let effectfulExpressionsEntries = "";
|
|
1731
|
-
for (const prop of siteData.props.values()) {
|
|
1732
|
-
if (prop.type === "action") {
|
|
1733
|
-
for (const executableValue of prop.value) {
|
|
1734
|
-
const codeString = JSON.stringify(executableValue.code);
|
|
1735
|
-
const generatedCode = generateEffectfulExpression(
|
|
1736
|
-
executableValue.code,
|
|
1737
|
-
new Set(executableValue.args),
|
|
1738
|
-
variables
|
|
1739
|
-
);
|
|
1740
|
-
const generatedFunction = `(_args: Map<string, any>, _variables: Map<string, any>) => { ${generatedCode} }`;
|
|
1741
|
-
effectfulExpressionsEntries += `[${codeString}, ${generatedFunction}],
|
|
1817
|
+
generatedDataSources += body;
|
|
1818
|
+
generatedDataSources += `let _output = new Map();
|
|
1819
|
+
`;
|
|
1820
|
+
for (const [dataSourceId, variableName] of output) {
|
|
1821
|
+
generatedDataSources += `_output.set('${dataSourceId}', ${variableName})
|
|
1742
1822
|
`;
|
|
1743
|
-
}
|
|
1744
|
-
}
|
|
1745
1823
|
}
|
|
1746
|
-
|
|
1747
|
-
|
|
1748
|
-
|
|
1749
|
-
|
|
1750
|
-
${effectfulExpressionsEntries}
|
|
1751
|
-
]);
|
|
1752
|
-
|
|
1753
|
-
const rawExecuteEffectfulExpression = (
|
|
1754
|
-
code: string,
|
|
1755
|
-
args: Map<string, unknown>,
|
|
1756
|
-
variables: Map<string, unknown>
|
|
1757
|
-
): Map<string, unknown> => {
|
|
1758
|
-
if(generatedEffectfulExpressions.has(code)) {
|
|
1759
|
-
return generatedEffectfulExpressions.get(code)!(args, variables);
|
|
1760
|
-
}
|
|
1761
|
-
console.error("Effectful expression not found", code);
|
|
1762
|
-
throw new Error("Effectful expression not found");
|
|
1763
|
-
};
|
|
1764
|
-
|
|
1765
|
-
const executeEffectfulExpression = (
|
|
1766
|
-
code: string,
|
|
1767
|
-
args: Map<string, unknown>,
|
|
1768
|
-
variables: Map<string, unknown>
|
|
1769
|
-
) => {
|
|
1770
|
-
const encodedvariables = sdk.encodeVariablesMap(variables);
|
|
1771
|
-
const encodedResult = rawExecuteEffectfulExpression(code, args, encodedvariables);
|
|
1772
|
-
return sdk.decodeVariablesMap(encodedResult);
|
|
1773
|
-
};
|
|
1774
|
-
`;
|
|
1824
|
+
generatedDataSources += `return _output
|
|
1825
|
+
`;
|
|
1826
|
+
generatedDataSources += `}
|
|
1827
|
+
`;
|
|
1775
1828
|
return `
|
|
1776
1829
|
/* eslint-disable */
|
|
1777
1830
|
|
|
1778
1831
|
${generatedIndexesWithinAncestors.trim()}
|
|
1779
1832
|
|
|
1780
|
-
${
|
|
1781
|
-
|
|
1782
|
-
${generatedExecuteEffectfulExpression.trim()}
|
|
1833
|
+
${generatedDataSources}
|
|
1783
1834
|
|
|
1784
1835
|
export const utils = {
|
|
1785
1836
|
indexesWithinAncestors,
|
|
1786
|
-
|
|
1787
|
-
executeEffectfulExpression,
|
|
1837
|
+
getDataSourcesLogic,
|
|
1788
1838
|
};
|
|
1789
1839
|
|
|
1790
1840
|
/* eslint-enable */
|
|
1791
1841
|
`;
|
|
1792
1842
|
};
|
|
1843
|
+
|
|
1844
|
+
// src/component-generator.ts
|
|
1845
|
+
import { findTreeInstanceIds, parseComponentName } from "@webstudio-is/sdk";
|
|
1846
|
+
var generateJsxElement = ({
|
|
1847
|
+
scope,
|
|
1848
|
+
instance,
|
|
1849
|
+
props,
|
|
1850
|
+
indexesWithinAncestors,
|
|
1851
|
+
children
|
|
1852
|
+
}) => {
|
|
1853
|
+
let conditionVariableName;
|
|
1854
|
+
let generatedProps = "";
|
|
1855
|
+
generatedProps += `
|
|
1856
|
+
${idAttribute}=${JSON.stringify(instance.id)}`;
|
|
1857
|
+
generatedProps += `
|
|
1858
|
+
${componentAttribute}=${JSON.stringify(
|
|
1859
|
+
instance.component
|
|
1860
|
+
)}`;
|
|
1861
|
+
const index = indexesWithinAncestors.get(instance.id);
|
|
1862
|
+
if (index !== void 0) {
|
|
1863
|
+
generatedProps += `
|
|
1864
|
+
${indexAttribute}="${index}"`;
|
|
1865
|
+
}
|
|
1866
|
+
for (const prop of props.values()) {
|
|
1867
|
+
if (prop.instanceId !== instance.id) {
|
|
1868
|
+
continue;
|
|
1869
|
+
}
|
|
1870
|
+
if (prop.name === showAttribute) {
|
|
1871
|
+
if (prop.type === "boolean" && prop.value === false) {
|
|
1872
|
+
return "";
|
|
1873
|
+
}
|
|
1874
|
+
if (prop.type === "dataSource") {
|
|
1875
|
+
const dataSourceId = prop.value;
|
|
1876
|
+
conditionVariableName = encodeDataSourceVariable(dataSourceId);
|
|
1877
|
+
}
|
|
1878
|
+
continue;
|
|
1879
|
+
}
|
|
1880
|
+
if (prop.type === "string" || prop.type === "number" || prop.type === "boolean" || prop.type === "string[]") {
|
|
1881
|
+
generatedProps += `
|
|
1882
|
+
${prop.name}={${JSON.stringify(prop.value)}}`;
|
|
1883
|
+
continue;
|
|
1884
|
+
}
|
|
1885
|
+
if (prop.type === "asset" || prop.type === "page") {
|
|
1886
|
+
continue;
|
|
1887
|
+
}
|
|
1888
|
+
if (prop.type === "dataSource") {
|
|
1889
|
+
const dataSourceId = prop.value;
|
|
1890
|
+
const dataSourceVariable = encodeDataSourceVariable(dataSourceId);
|
|
1891
|
+
generatedProps += `
|
|
1892
|
+
${prop.name}={${dataSourceVariable}}`;
|
|
1893
|
+
continue;
|
|
1894
|
+
}
|
|
1895
|
+
if (prop.type === "action") {
|
|
1896
|
+
const propVariable = scope.getName(prop.id, prop.name);
|
|
1897
|
+
generatedProps += `
|
|
1898
|
+
${prop.name}={${propVariable}}`;
|
|
1899
|
+
continue;
|
|
1900
|
+
}
|
|
1901
|
+
prop;
|
|
1902
|
+
}
|
|
1903
|
+
let generatedElement = "";
|
|
1904
|
+
if (conditionVariableName) {
|
|
1905
|
+
generatedElement += `{${conditionVariableName} &&
|
|
1906
|
+
`;
|
|
1907
|
+
}
|
|
1908
|
+
const [_namespace, shortName] = parseComponentName(instance.component);
|
|
1909
|
+
const componentVariable = scope.getName(instance.component, shortName);
|
|
1910
|
+
if (instance.children.length === 0) {
|
|
1911
|
+
generatedElement += `<${componentVariable}${generatedProps} />
|
|
1912
|
+
`;
|
|
1913
|
+
} else {
|
|
1914
|
+
generatedElement += `<${componentVariable}${generatedProps}>
|
|
1915
|
+
`;
|
|
1916
|
+
generatedElement += children;
|
|
1917
|
+
generatedElement += `</${componentVariable}>
|
|
1918
|
+
`;
|
|
1919
|
+
}
|
|
1920
|
+
if (conditionVariableName) {
|
|
1921
|
+
generatedElement += `}
|
|
1922
|
+
`;
|
|
1923
|
+
}
|
|
1924
|
+
return generatedElement;
|
|
1925
|
+
};
|
|
1926
|
+
var generateJsxChildren = ({
|
|
1927
|
+
scope,
|
|
1928
|
+
children,
|
|
1929
|
+
instances,
|
|
1930
|
+
props,
|
|
1931
|
+
indexesWithinAncestors
|
|
1932
|
+
}) => {
|
|
1933
|
+
let generatedChildren = "";
|
|
1934
|
+
for (const child of children) {
|
|
1935
|
+
if (child.type === "text") {
|
|
1936
|
+
generatedChildren += child.value.split("\n").map((line) => `{${JSON.stringify(line)}}
|
|
1937
|
+
`).join(`<br />
|
|
1938
|
+
`);
|
|
1939
|
+
continue;
|
|
1940
|
+
}
|
|
1941
|
+
if (child.type === "id") {
|
|
1942
|
+
const instanceId = child.value;
|
|
1943
|
+
const instance = instances.get(instanceId);
|
|
1944
|
+
if (instance === void 0) {
|
|
1945
|
+
continue;
|
|
1946
|
+
}
|
|
1947
|
+
generatedChildren += generateJsxElement({
|
|
1948
|
+
scope,
|
|
1949
|
+
instance,
|
|
1950
|
+
props,
|
|
1951
|
+
indexesWithinAncestors,
|
|
1952
|
+
children: generateJsxChildren({
|
|
1953
|
+
scope,
|
|
1954
|
+
children: instance.children,
|
|
1955
|
+
instances,
|
|
1956
|
+
props,
|
|
1957
|
+
indexesWithinAncestors
|
|
1958
|
+
})
|
|
1959
|
+
});
|
|
1960
|
+
continue;
|
|
1961
|
+
}
|
|
1962
|
+
child;
|
|
1963
|
+
}
|
|
1964
|
+
return generatedChildren;
|
|
1965
|
+
};
|
|
1966
|
+
var generateDataSources2 = ({
|
|
1967
|
+
scope,
|
|
1968
|
+
rootInstanceId,
|
|
1969
|
+
instances,
|
|
1970
|
+
props
|
|
1971
|
+
}) => {
|
|
1972
|
+
let generatedDataSources = "";
|
|
1973
|
+
generatedDataSources += `const { dataSourceValuesStore, setDataSourceValues, executeEffectfulExpression } = useContext(ReactSdkContext);
|
|
1974
|
+
`;
|
|
1975
|
+
generatedDataSources += "const dataSourceValues = useStore(dataSourceValuesStore);\n";
|
|
1976
|
+
const usedInstanceIds = findTreeInstanceIds(instances, rootInstanceId);
|
|
1977
|
+
for (const prop of props.values()) {
|
|
1978
|
+
if (prop.type === "dataSource" && usedInstanceIds.has(prop.instanceId)) {
|
|
1979
|
+
const dataSourceId = prop.value;
|
|
1980
|
+
const variableName = encodeDataSourceVariable(dataSourceId);
|
|
1981
|
+
const key = JSON.stringify(dataSourceId);
|
|
1982
|
+
generatedDataSources += `const ${variableName} = dataSourceValues.get(${key});
|
|
1983
|
+
`;
|
|
1984
|
+
}
|
|
1985
|
+
if (prop.type === "action") {
|
|
1986
|
+
const propVariable = scope.getName(prop.id, prop.name);
|
|
1987
|
+
let args = "";
|
|
1988
|
+
for (const value of prop.value) {
|
|
1989
|
+
const newArgs = value.args.map((arg) => `${arg}: unknown`).join(", ");
|
|
1990
|
+
if (args !== "" && newArgs !== args) {
|
|
1991
|
+
continue;
|
|
1992
|
+
}
|
|
1993
|
+
args = newArgs;
|
|
1994
|
+
}
|
|
1995
|
+
generatedDataSources += `const ${propVariable} = (${args}) => {
|
|
1996
|
+
`;
|
|
1997
|
+
for (const value of prop.value) {
|
|
1998
|
+
if (value.type === "execute") {
|
|
1999
|
+
generatedDataSources += `const newValues = executeEffectfulExpression(
|
|
2000
|
+
`;
|
|
2001
|
+
generatedDataSources += `value.code,
|
|
2002
|
+
`;
|
|
2003
|
+
generatedDataSources += `new Map([`;
|
|
2004
|
+
generatedDataSources += value.args.map((arg) => `[${JSON.stringify(arg)}, ${arg}]`).join(", ");
|
|
2005
|
+
generatedDataSources += `]),
|
|
2006
|
+
`;
|
|
2007
|
+
generatedDataSources += `dataSourceValues
|
|
2008
|
+
`;
|
|
2009
|
+
generatedDataSources += `);
|
|
2010
|
+
`;
|
|
2011
|
+
generatedDataSources += `setDataSourceValues(newValues);
|
|
2012
|
+
`;
|
|
2013
|
+
}
|
|
2014
|
+
}
|
|
2015
|
+
generatedDataSources += `};
|
|
2016
|
+
`;
|
|
2017
|
+
}
|
|
2018
|
+
}
|
|
2019
|
+
return generatedDataSources;
|
|
2020
|
+
};
|
|
2021
|
+
var generatePageComponent = ({
|
|
2022
|
+
scope,
|
|
2023
|
+
rootInstanceId,
|
|
2024
|
+
instances,
|
|
2025
|
+
props,
|
|
2026
|
+
indexesWithinAncestors
|
|
2027
|
+
}) => {
|
|
2028
|
+
const instance = instances.get(rootInstanceId);
|
|
2029
|
+
if (instance === void 0) {
|
|
2030
|
+
return "";
|
|
2031
|
+
}
|
|
2032
|
+
const generatedDataSources = generateDataSources2({
|
|
2033
|
+
scope,
|
|
2034
|
+
rootInstanceId,
|
|
2035
|
+
instances,
|
|
2036
|
+
props
|
|
2037
|
+
});
|
|
2038
|
+
const generatedJsx = generateJsxElement({
|
|
2039
|
+
scope,
|
|
2040
|
+
instance,
|
|
2041
|
+
props,
|
|
2042
|
+
indexesWithinAncestors,
|
|
2043
|
+
children: generateJsxChildren({
|
|
2044
|
+
scope,
|
|
2045
|
+
children: instance.children,
|
|
2046
|
+
instances,
|
|
2047
|
+
props,
|
|
2048
|
+
indexesWithinAncestors
|
|
2049
|
+
}) + "{props.scripts}\n"
|
|
2050
|
+
});
|
|
2051
|
+
let generatedComponent = "";
|
|
2052
|
+
generatedComponent += `export const Page = (props: { scripts: ReactNode }) => {
|
|
2053
|
+
`;
|
|
2054
|
+
generatedComponent += `${generatedDataSources}`;
|
|
2055
|
+
generatedComponent += `return ${generatedJsx}`;
|
|
2056
|
+
generatedComponent += `};
|
|
2057
|
+
`;
|
|
2058
|
+
return generatedComponent;
|
|
2059
|
+
};
|
|
1793
2060
|
export {
|
|
1794
2061
|
EmbedTemplateInstance,
|
|
1795
2062
|
EmbedTemplateStyleDecl,
|
|
@@ -1816,7 +2083,9 @@ export {
|
|
|
1816
2083
|
generateComputingExpressions,
|
|
1817
2084
|
generateCssText,
|
|
1818
2085
|
generateDataFromEmbedTemplate,
|
|
2086
|
+
generateDataSources,
|
|
1819
2087
|
generateEffectfulExpression,
|
|
2088
|
+
generatePageComponent,
|
|
1820
2089
|
generateUtilsExport,
|
|
1821
2090
|
getClosestInstance,
|
|
1822
2091
|
getIndexWithinAncestorFromComponentProps,
|
|
@@ -0,0 +1,249 @@
|
|
|
1
|
+
import type { Instances, Instance, Props, Scope } from "@webstudio-is/sdk";
|
|
2
|
+
import type { IndexesWithinAncestors } from "./instance-utils";
|
|
3
|
+
export declare const generateJsxElement: ({ scope, instance, props, indexesWithinAncestors, children, }: {
|
|
4
|
+
scope: Scope;
|
|
5
|
+
instance: Instance;
|
|
6
|
+
props: Map<string, {
|
|
7
|
+
value: number;
|
|
8
|
+
type: "number";
|
|
9
|
+
name: string;
|
|
10
|
+
id: string;
|
|
11
|
+
instanceId: string;
|
|
12
|
+
required?: boolean | undefined;
|
|
13
|
+
} | {
|
|
14
|
+
value: string;
|
|
15
|
+
type: "string";
|
|
16
|
+
name: string;
|
|
17
|
+
id: string;
|
|
18
|
+
instanceId: string;
|
|
19
|
+
required?: boolean | undefined;
|
|
20
|
+
} | {
|
|
21
|
+
value: boolean;
|
|
22
|
+
type: "boolean";
|
|
23
|
+
name: string;
|
|
24
|
+
id: string;
|
|
25
|
+
instanceId: string;
|
|
26
|
+
required?: boolean | undefined;
|
|
27
|
+
} | {
|
|
28
|
+
value: string;
|
|
29
|
+
type: "asset";
|
|
30
|
+
name: string;
|
|
31
|
+
id: string;
|
|
32
|
+
instanceId: string;
|
|
33
|
+
required?: boolean | undefined;
|
|
34
|
+
} | {
|
|
35
|
+
value: (string | {
|
|
36
|
+
instanceId: string;
|
|
37
|
+
pageId: string;
|
|
38
|
+
}) & (string | {
|
|
39
|
+
instanceId: string;
|
|
40
|
+
pageId: string;
|
|
41
|
+
} | undefined);
|
|
42
|
+
type: "page";
|
|
43
|
+
name: string;
|
|
44
|
+
id: string;
|
|
45
|
+
instanceId: string;
|
|
46
|
+
required?: boolean | undefined;
|
|
47
|
+
} | {
|
|
48
|
+
value: string[];
|
|
49
|
+
type: "string[]";
|
|
50
|
+
name: string;
|
|
51
|
+
id: string;
|
|
52
|
+
instanceId: string;
|
|
53
|
+
required?: boolean | undefined;
|
|
54
|
+
} | {
|
|
55
|
+
value: string;
|
|
56
|
+
type: "dataSource";
|
|
57
|
+
name: string;
|
|
58
|
+
id: string;
|
|
59
|
+
instanceId: string;
|
|
60
|
+
required?: boolean | undefined;
|
|
61
|
+
} | {
|
|
62
|
+
value: {
|
|
63
|
+
code: string;
|
|
64
|
+
type: "execute";
|
|
65
|
+
args: string[];
|
|
66
|
+
}[];
|
|
67
|
+
type: "action";
|
|
68
|
+
name: string;
|
|
69
|
+
id: string;
|
|
70
|
+
instanceId: string;
|
|
71
|
+
required?: boolean | undefined;
|
|
72
|
+
}>;
|
|
73
|
+
indexesWithinAncestors: IndexesWithinAncestors;
|
|
74
|
+
children: string;
|
|
75
|
+
}) => string;
|
|
76
|
+
/**
|
|
77
|
+
* Jsx element and children are generated separately to be able
|
|
78
|
+
* to inject some scripts into Body if necessary
|
|
79
|
+
*/
|
|
80
|
+
export declare const generateJsxChildren: ({ scope, children, instances, props, indexesWithinAncestors, }: {
|
|
81
|
+
scope: Scope;
|
|
82
|
+
children: Instance["children"];
|
|
83
|
+
instances: Map<string, {
|
|
84
|
+
type: "instance";
|
|
85
|
+
id: string;
|
|
86
|
+
component: string;
|
|
87
|
+
children: ({
|
|
88
|
+
value: string;
|
|
89
|
+
type: "text";
|
|
90
|
+
} | {
|
|
91
|
+
value: string;
|
|
92
|
+
type: "id";
|
|
93
|
+
})[];
|
|
94
|
+
label?: string | undefined;
|
|
95
|
+
}>;
|
|
96
|
+
props: Map<string, {
|
|
97
|
+
value: number;
|
|
98
|
+
type: "number";
|
|
99
|
+
name: string;
|
|
100
|
+
id: string;
|
|
101
|
+
instanceId: string;
|
|
102
|
+
required?: boolean | undefined;
|
|
103
|
+
} | {
|
|
104
|
+
value: string;
|
|
105
|
+
type: "string";
|
|
106
|
+
name: string;
|
|
107
|
+
id: string;
|
|
108
|
+
instanceId: string;
|
|
109
|
+
required?: boolean | undefined;
|
|
110
|
+
} | {
|
|
111
|
+
value: boolean;
|
|
112
|
+
type: "boolean";
|
|
113
|
+
name: string;
|
|
114
|
+
id: string;
|
|
115
|
+
instanceId: string;
|
|
116
|
+
required?: boolean | undefined;
|
|
117
|
+
} | {
|
|
118
|
+
value: string;
|
|
119
|
+
type: "asset";
|
|
120
|
+
name: string;
|
|
121
|
+
id: string;
|
|
122
|
+
instanceId: string;
|
|
123
|
+
required?: boolean | undefined;
|
|
124
|
+
} | {
|
|
125
|
+
value: (string | {
|
|
126
|
+
instanceId: string;
|
|
127
|
+
pageId: string;
|
|
128
|
+
}) & (string | {
|
|
129
|
+
instanceId: string;
|
|
130
|
+
pageId: string;
|
|
131
|
+
} | undefined);
|
|
132
|
+
type: "page";
|
|
133
|
+
name: string;
|
|
134
|
+
id: string;
|
|
135
|
+
instanceId: string;
|
|
136
|
+
required?: boolean | undefined;
|
|
137
|
+
} | {
|
|
138
|
+
value: string[];
|
|
139
|
+
type: "string[]";
|
|
140
|
+
name: string;
|
|
141
|
+
id: string;
|
|
142
|
+
instanceId: string;
|
|
143
|
+
required?: boolean | undefined;
|
|
144
|
+
} | {
|
|
145
|
+
value: string;
|
|
146
|
+
type: "dataSource";
|
|
147
|
+
name: string;
|
|
148
|
+
id: string;
|
|
149
|
+
instanceId: string;
|
|
150
|
+
required?: boolean | undefined;
|
|
151
|
+
} | {
|
|
152
|
+
value: {
|
|
153
|
+
code: string;
|
|
154
|
+
type: "execute";
|
|
155
|
+
args: string[];
|
|
156
|
+
}[];
|
|
157
|
+
type: "action";
|
|
158
|
+
name: string;
|
|
159
|
+
id: string;
|
|
160
|
+
instanceId: string;
|
|
161
|
+
required?: boolean | undefined;
|
|
162
|
+
}>;
|
|
163
|
+
indexesWithinAncestors: IndexesWithinAncestors;
|
|
164
|
+
}) => string;
|
|
165
|
+
export declare const generatePageComponent: ({ scope, rootInstanceId, instances, props, indexesWithinAncestors, }: {
|
|
166
|
+
scope: Scope;
|
|
167
|
+
rootInstanceId: Instance["id"];
|
|
168
|
+
instances: Map<string, {
|
|
169
|
+
type: "instance";
|
|
170
|
+
id: string;
|
|
171
|
+
component: string;
|
|
172
|
+
children: ({
|
|
173
|
+
value: string;
|
|
174
|
+
type: "text";
|
|
175
|
+
} | {
|
|
176
|
+
value: string;
|
|
177
|
+
type: "id";
|
|
178
|
+
})[];
|
|
179
|
+
label?: string | undefined;
|
|
180
|
+
}>;
|
|
181
|
+
props: Map<string, {
|
|
182
|
+
value: number;
|
|
183
|
+
type: "number";
|
|
184
|
+
name: string;
|
|
185
|
+
id: string;
|
|
186
|
+
instanceId: string;
|
|
187
|
+
required?: boolean | undefined;
|
|
188
|
+
} | {
|
|
189
|
+
value: string;
|
|
190
|
+
type: "string";
|
|
191
|
+
name: string;
|
|
192
|
+
id: string;
|
|
193
|
+
instanceId: string;
|
|
194
|
+
required?: boolean | undefined;
|
|
195
|
+
} | {
|
|
196
|
+
value: boolean;
|
|
197
|
+
type: "boolean";
|
|
198
|
+
name: string;
|
|
199
|
+
id: string;
|
|
200
|
+
instanceId: string;
|
|
201
|
+
required?: boolean | undefined;
|
|
202
|
+
} | {
|
|
203
|
+
value: string;
|
|
204
|
+
type: "asset";
|
|
205
|
+
name: string;
|
|
206
|
+
id: string;
|
|
207
|
+
instanceId: string;
|
|
208
|
+
required?: boolean | undefined;
|
|
209
|
+
} | {
|
|
210
|
+
value: (string | {
|
|
211
|
+
instanceId: string;
|
|
212
|
+
pageId: string;
|
|
213
|
+
}) & (string | {
|
|
214
|
+
instanceId: string;
|
|
215
|
+
pageId: string;
|
|
216
|
+
} | undefined);
|
|
217
|
+
type: "page";
|
|
218
|
+
name: string;
|
|
219
|
+
id: string;
|
|
220
|
+
instanceId: string;
|
|
221
|
+
required?: boolean | undefined;
|
|
222
|
+
} | {
|
|
223
|
+
value: string[];
|
|
224
|
+
type: "string[]";
|
|
225
|
+
name: string;
|
|
226
|
+
id: string;
|
|
227
|
+
instanceId: string;
|
|
228
|
+
required?: boolean | undefined;
|
|
229
|
+
} | {
|
|
230
|
+
value: string;
|
|
231
|
+
type: "dataSource";
|
|
232
|
+
name: string;
|
|
233
|
+
id: string;
|
|
234
|
+
instanceId: string;
|
|
235
|
+
required?: boolean | undefined;
|
|
236
|
+
} | {
|
|
237
|
+
value: {
|
|
238
|
+
code: string;
|
|
239
|
+
type: "execute";
|
|
240
|
+
args: string[];
|
|
241
|
+
}[];
|
|
242
|
+
type: "action";
|
|
243
|
+
name: string;
|
|
244
|
+
id: string;
|
|
245
|
+
instanceId: string;
|
|
246
|
+
required?: boolean | undefined;
|
|
247
|
+
}>;
|
|
248
|
+
indexesWithinAncestors: IndexesWithinAncestors;
|
|
249
|
+
}) => string;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/lib/types/context.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/// <reference types="react" />
|
|
2
2
|
import { type ReadableAtom } from "nanostores";
|
|
3
|
-
import type {
|
|
3
|
+
import type { Assets } from "@webstudio-is/sdk";
|
|
4
4
|
import type { Pages, PropsByInstanceId } from "./props";
|
|
5
5
|
import type { IndexesWithinAncestors } from "./instance-utils";
|
|
6
6
|
import type { ImageLoader } from "@webstudio-is/image";
|
|
@@ -30,14 +30,11 @@ export type Params = {
|
|
|
30
30
|
*/
|
|
31
31
|
assetBaseUrl: string;
|
|
32
32
|
};
|
|
33
|
-
export type DataSourceValues = Map<DataSource["id"], unknown>;
|
|
34
33
|
export declare const ReactSdkContext: import("react").Context<Params & {
|
|
35
34
|
imageLoader: ImageLoader;
|
|
36
35
|
propsByInstanceIdStore: ReadableAtom<PropsByInstanceId>;
|
|
37
36
|
assetsStore: ReadableAtom<Assets>;
|
|
38
37
|
pagesStore: ReadableAtom<Pages>;
|
|
39
|
-
|
|
40
|
-
executeEffectfulExpression: (expression: string, args: DataSourceValues, values: DataSourceValues) => DataSourceValues;
|
|
41
|
-
setDataSourceValues: (newValues: DataSourceValues) => void;
|
|
38
|
+
dataSourcesLogicStore: ReadableAtom<Map<string, unknown>>;
|
|
42
39
|
indexesWithinAncestors: IndexesWithinAncestors;
|
|
43
40
|
}>;
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { DataSources, Props, Scope } from "@webstudio-is/sdk";
|
|
1
2
|
type TransformIdentifier = (id: string, assignee: boolean) => string;
|
|
2
3
|
export declare const validateExpression: (code: string, options?: {
|
|
3
4
|
effectful?: boolean;
|
|
@@ -17,4 +18,109 @@ export declare const encodeDataSourceVariable: (id: string) => string;
|
|
|
17
18
|
export declare const encodeVariablesMap: (values: Values) => Values;
|
|
18
19
|
export declare const decodeDataSourceVariable: (name: string) => string | undefined;
|
|
19
20
|
export declare const decodeVariablesMap: (values: Values) => Values;
|
|
21
|
+
type VariableName = string;
|
|
22
|
+
export declare const generateDataSources: ({ scope, typed, dataSources, props, }: {
|
|
23
|
+
scope: Scope;
|
|
24
|
+
typed?: boolean | undefined;
|
|
25
|
+
dataSources: Map<string, {
|
|
26
|
+
value: {
|
|
27
|
+
value: number;
|
|
28
|
+
type: "number";
|
|
29
|
+
} | {
|
|
30
|
+
value: string;
|
|
31
|
+
type: "string";
|
|
32
|
+
} | {
|
|
33
|
+
value: boolean;
|
|
34
|
+
type: "boolean";
|
|
35
|
+
} | {
|
|
36
|
+
value: string[];
|
|
37
|
+
type: "string[]";
|
|
38
|
+
};
|
|
39
|
+
type: "variable";
|
|
40
|
+
name: string;
|
|
41
|
+
id: string;
|
|
42
|
+
scopeInstanceId?: string | undefined;
|
|
43
|
+
} | {
|
|
44
|
+
code: string;
|
|
45
|
+
type: "expression";
|
|
46
|
+
name: string;
|
|
47
|
+
id: string;
|
|
48
|
+
scopeInstanceId?: string | undefined;
|
|
49
|
+
}>;
|
|
50
|
+
props: Map<string, {
|
|
51
|
+
value: number;
|
|
52
|
+
type: "number";
|
|
53
|
+
name: string;
|
|
54
|
+
id: string;
|
|
55
|
+
instanceId: string;
|
|
56
|
+
required?: boolean | undefined;
|
|
57
|
+
} | {
|
|
58
|
+
value: string;
|
|
59
|
+
type: "string";
|
|
60
|
+
name: string;
|
|
61
|
+
id: string;
|
|
62
|
+
instanceId: string;
|
|
63
|
+
required?: boolean | undefined;
|
|
64
|
+
} | {
|
|
65
|
+
value: boolean;
|
|
66
|
+
type: "boolean";
|
|
67
|
+
name: string;
|
|
68
|
+
id: string;
|
|
69
|
+
instanceId: string;
|
|
70
|
+
required?: boolean | undefined;
|
|
71
|
+
} | {
|
|
72
|
+
value: string;
|
|
73
|
+
type: "asset";
|
|
74
|
+
name: string;
|
|
75
|
+
id: string;
|
|
76
|
+
instanceId: string;
|
|
77
|
+
required?: boolean | undefined;
|
|
78
|
+
} | {
|
|
79
|
+
value: (string | {
|
|
80
|
+
instanceId: string;
|
|
81
|
+
pageId: string;
|
|
82
|
+
}) & (string | {
|
|
83
|
+
instanceId: string;
|
|
84
|
+
pageId: string;
|
|
85
|
+
} | undefined);
|
|
86
|
+
type: "page";
|
|
87
|
+
name: string;
|
|
88
|
+
id: string;
|
|
89
|
+
instanceId: string;
|
|
90
|
+
required?: boolean | undefined;
|
|
91
|
+
} | {
|
|
92
|
+
value: string[];
|
|
93
|
+
type: "string[]";
|
|
94
|
+
name: string;
|
|
95
|
+
id: string;
|
|
96
|
+
instanceId: string;
|
|
97
|
+
required?: boolean | undefined;
|
|
98
|
+
} | {
|
|
99
|
+
value: string;
|
|
100
|
+
type: "dataSource";
|
|
101
|
+
name: string;
|
|
102
|
+
id: string;
|
|
103
|
+
instanceId: string;
|
|
104
|
+
required?: boolean | undefined;
|
|
105
|
+
} | {
|
|
106
|
+
value: {
|
|
107
|
+
code: string;
|
|
108
|
+
type: "execute";
|
|
109
|
+
args: string[];
|
|
110
|
+
}[];
|
|
111
|
+
type: "action";
|
|
112
|
+
name: string;
|
|
113
|
+
id: string;
|
|
114
|
+
instanceId: string;
|
|
115
|
+
required?: boolean | undefined;
|
|
116
|
+
}>;
|
|
117
|
+
}) => {
|
|
118
|
+
variables: Map<string, {
|
|
119
|
+
valueName: VariableName;
|
|
120
|
+
setterName: VariableName;
|
|
121
|
+
initialValue: unknown;
|
|
122
|
+
}>;
|
|
123
|
+
body: string;
|
|
124
|
+
output: Map<string, string>;
|
|
125
|
+
};
|
|
20
126
|
export {};
|
package/lib/types/generator.d.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
import type
|
|
1
|
+
import { type DataSources, type Instance, type Instances, type Page, type Props } from "@webstudio-is/sdk";
|
|
2
2
|
import type { WsComponentMeta } from "./components/component-meta";
|
|
3
3
|
import { type IndexesWithinAncestors } from "./instance-utils";
|
|
4
|
-
import type { DataSourceValues } from "./context";
|
|
5
4
|
type PageData = {
|
|
6
5
|
page: Page;
|
|
7
6
|
metas: Map<Instance["component"], WsComponentMeta>;
|
|
@@ -11,8 +10,7 @@ type PageData = {
|
|
|
11
10
|
};
|
|
12
11
|
export type GeneratedUtils = {
|
|
13
12
|
indexesWithinAncestors: IndexesWithinAncestors;
|
|
14
|
-
|
|
15
|
-
executeEffectfulExpression: (expression: string, args: DataSourceValues, values: DataSourceValues) => DataSourceValues;
|
|
13
|
+
getDataSourcesLogic: (getVariable: (id: string) => unknown, setVariable: (id: string, value: unknown) => void) => Map<string, unknown>;
|
|
16
14
|
};
|
|
17
15
|
/**
|
|
18
16
|
* Generates data based utilities at build time
|
package/lib/types/index.d.ts
CHANGED
|
@@ -7,8 +7,9 @@ export { type WsComponentPropsMeta, type WsComponentMeta, type ComponentState, t
|
|
|
7
7
|
export * from "./embed-template";
|
|
8
8
|
export { useInstanceProps, usePropUrl, usePropAsset, getInstanceIdFromComponentProps, getIndexWithinAncestorFromComponentProps, } from "./props";
|
|
9
9
|
export { type Params, ReactSdkContext } from "./context";
|
|
10
|
-
export { validateExpression, generateComputingExpressions, executeComputingExpressions, generateEffectfulExpression, executeEffectfulExpression, computeExpressionsDependencies, encodeDataSourceVariable, encodeVariablesMap, decodeDataSourceVariable, decodeVariablesMap, } from "./expression";
|
|
10
|
+
export { validateExpression, generateComputingExpressions, executeComputingExpressions, generateEffectfulExpression, executeEffectfulExpression, computeExpressionsDependencies, encodeDataSourceVariable, encodeVariablesMap, decodeDataSourceVariable, decodeVariablesMap, generateDataSources, } from "./expression";
|
|
11
11
|
export { renderComponentTemplate } from "./component-renderer";
|
|
12
12
|
export { getIndexesWithinAncestors } from "./instance-utils";
|
|
13
13
|
export * from "./hook";
|
|
14
14
|
export { generateUtilsExport } from "./generator";
|
|
15
|
+
export { generatePageComponent } from "./component-generator";
|
|
@@ -2,12 +2,12 @@ import { type ForwardRefExoticComponent, type RefAttributes, type ReactNode } fr
|
|
|
2
2
|
import type { ReadableAtom } from "nanostores";
|
|
3
3
|
import type { Instance, Instances, Assets } from "@webstudio-is/sdk";
|
|
4
4
|
import type { Components } from "../components/components-utils";
|
|
5
|
-
import { type Params
|
|
5
|
+
import { type Params } from "../context";
|
|
6
6
|
import type { Pages, PropsByInstanceId } from "../props";
|
|
7
7
|
import type { WebstudioComponentProps } from "./webstudio-component";
|
|
8
8
|
import type { IndexesWithinAncestors } from "../instance-utils";
|
|
9
9
|
import type { ImageLoader } from "@webstudio-is/image";
|
|
10
|
-
export declare const createElementsTree: ({ renderer, assetBaseUrl, imageBaseUrl, imageLoader, instances, rootInstanceId, propsByInstanceIdStore, assetsStore, pagesStore,
|
|
10
|
+
export declare const createElementsTree: ({ renderer, assetBaseUrl, imageBaseUrl, imageLoader, instances, rootInstanceId, propsByInstanceIdStore, assetsStore, pagesStore, dataSourcesLogicStore, indexesWithinAncestors, Component, components, scripts, }: Params & {
|
|
11
11
|
instances: Map<string, {
|
|
12
12
|
type: "instance";
|
|
13
13
|
id: string;
|
|
@@ -26,9 +26,7 @@ export declare const createElementsTree: ({ renderer, assetBaseUrl, imageBaseUrl
|
|
|
26
26
|
propsByInstanceIdStore: ReadableAtom<PropsByInstanceId>;
|
|
27
27
|
assetsStore: ReadableAtom<Assets>;
|
|
28
28
|
pagesStore: ReadableAtom<Pages>;
|
|
29
|
-
|
|
30
|
-
dataSourceValuesStore: ReadableAtom<DataSourceValues>;
|
|
31
|
-
onDataSourceUpdate: (newValues: DataSourceValues) => void;
|
|
29
|
+
dataSourcesLogicStore: ReadableAtom<Map<string, unknown>>;
|
|
32
30
|
indexesWithinAncestors: IndexesWithinAncestors;
|
|
33
31
|
Component: ForwardRefExoticComponent<WebstudioComponentProps & RefAttributes<HTMLElement>>;
|
|
34
32
|
components: Components;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@webstudio-is/react-sdk",
|
|
3
|
-
"version": "0.99.1-
|
|
3
|
+
"version": "0.99.1-de264e0.0",
|
|
4
4
|
"description": "Webstudio JavaScript / TypeScript API",
|
|
5
5
|
"author": "Webstudio <github@webstudio.is>",
|
|
6
6
|
"homepage": "https://webstudio.is",
|
|
@@ -13,11 +13,12 @@
|
|
|
13
13
|
"jest": "^29.6.4",
|
|
14
14
|
"react": "^18.2.0",
|
|
15
15
|
"react-dom": "^18.2.0",
|
|
16
|
+
"strip-indent": "^4.0.0",
|
|
16
17
|
"type-fest": "^4.3.1",
|
|
17
18
|
"typescript": "5.2.2",
|
|
18
19
|
"zod": "^3.21.4",
|
|
19
|
-
"@webstudio-is/jest-config": "^1.0.8-
|
|
20
|
-
"@webstudio-is/tsconfig": "^1.0.8-
|
|
20
|
+
"@webstudio-is/jest-config": "^1.0.8-de264e0.0",
|
|
21
|
+
"@webstudio-is/tsconfig": "^1.0.8-de264e0.0"
|
|
21
22
|
},
|
|
22
23
|
"peerDependencies": {
|
|
23
24
|
"@remix-run/react": "^1.19.1",
|
|
@@ -34,10 +35,10 @@
|
|
|
34
35
|
"nanostores": "^0.9.3",
|
|
35
36
|
"no-case": "^3.0.4",
|
|
36
37
|
"title-case": "^3.0.3",
|
|
37
|
-
"@webstudio-is/css-engine": "^0.99.1-
|
|
38
|
-
"@webstudio-is/fonts": "^0.99.1-
|
|
39
|
-
"@webstudio-is/image": "^0.99.1-
|
|
40
|
-
"@webstudio-is/sdk": "^0.99.1-
|
|
38
|
+
"@webstudio-is/css-engine": "^0.99.1-de264e0.0",
|
|
39
|
+
"@webstudio-is/fonts": "^0.99.1-de264e0.0",
|
|
40
|
+
"@webstudio-is/image": "^0.99.1-de264e0.0",
|
|
41
|
+
"@webstudio-is/sdk": "^0.99.1-de264e0.0"
|
|
41
42
|
},
|
|
42
43
|
"exports": {
|
|
43
44
|
".": {
|