@webstudio-is/react-sdk 0.99.1-939d933.0 → 0.99.1-9c576a1.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 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
- dataSourceValuesStore: atom(/* @__PURE__ */ new Map()),
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
- dataSourceValuesStore,
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
- dataSourceValuesStore,
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
- dataSourceValuesStore,
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, dataSourceValuesStore],
202
- (propsByInstanceId, dataSourceValues) => {
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 = dataSourceValues.get(dataSourceId);
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
- instancePropsObject2[prop.name] = (...args) => {
225
- for (const value of prop.value) {
226
- if (value.type === "execute") {
227
- const argsMap = /* @__PURE__ */ new Map();
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 dataSourceValuesStoreRef = useRef(void 0);
429
- if (dataSourceValuesStoreRef.current === void 0) {
430
- dataSourceValuesStoreRef.current = computed2(
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 result = executeComputingExpressions2(dataSourceValues);
442
- for (const [id, value] of result) {
443
- dataSourceValues.set(id, value);
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 dataSourceValues;
410
+ return /* @__PURE__ */ new Map();
449
411
  }
450
412
  );
451
413
  }
452
- const dataSourceValuesStore = dataSourceValuesStoreRef.current;
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
- executeEffectfulExpression: executeEffectfulExpression2,
476
- dataSourceValuesStore,
477
- onDataSourceUpdate,
427
+ dataSourcesLogicStore,
478
428
  Component: Component ?? WebstudioComponent,
479
429
  components,
480
430
  scripts
@@ -937,117 +887,6 @@ var sortTopologically = (list, depsById, explored = /* @__PURE__ */ new Set(), s
937
887
  }
938
888
  return sorted;
939
889
  };
940
- var generateComputingExpressions = (expressions, allowedVariables) => {
941
- const depsById = /* @__PURE__ */ new Map();
942
- const inputVariables = /* @__PURE__ */ new Set();
943
- for (const [id, code] of expressions) {
944
- const deps = /* @__PURE__ */ new Set();
945
- validateExpression(code, {
946
- transformIdentifier: (identifier) => {
947
- if (allowedVariables.has(identifier)) {
948
- inputVariables.add(identifier);
949
- return identifier;
950
- }
951
- if (expressions.has(identifier)) {
952
- deps.add(identifier);
953
- return identifier;
954
- }
955
- throw Error(`Unknown dependency "${identifier}"`);
956
- }
957
- });
958
- depsById.set(id, deps);
959
- }
960
- const sortedExpressions = sortTopologically(
961
- new Set(expressions.keys()),
962
- depsById
963
- );
964
- let generatedCode = "";
965
- for (const id of inputVariables) {
966
- generatedCode += `const ${id} = _variables.get('${id}');
967
- `;
968
- }
969
- for (const id of sortedExpressions) {
970
- const code = expressions.get(id);
971
- if (code === void 0) {
972
- continue;
973
- }
974
- generatedCode += `const ${id} = (${code});
975
- `;
976
- }
977
- generatedCode += `return new Map([
978
- `;
979
- for (const id of sortedExpressions) {
980
- generatedCode += ` ['${id}', ${id}],
981
- `;
982
- }
983
- generatedCode += `]);`;
984
- return generatedCode;
985
- };
986
- var executeComputingExpressions = (expressions, variables) => {
987
- const generatedCode = generateComputingExpressions(
988
- expressions,
989
- new Set(variables.keys())
990
- );
991
- const executeFn = new Function("_variables", generatedCode);
992
- const values = executeFn(variables);
993
- return values;
994
- };
995
- var generateEffectfulExpression = (code, args, allowedVariables) => {
996
- const inputVariables = /* @__PURE__ */ new Set();
997
- const outputVariables = /* @__PURE__ */ new Set();
998
- validateExpression(code, {
999
- effectful: true,
1000
- transformIdentifier: (identifier, assignee) => {
1001
- if (args.has(identifier)) {
1002
- return identifier;
1003
- }
1004
- if (allowedVariables.has(identifier)) {
1005
- if (assignee) {
1006
- outputVariables.add(identifier);
1007
- } else {
1008
- inputVariables.add(identifier);
1009
- }
1010
- return identifier;
1011
- }
1012
- throw Error(`Unknown dependency "${identifier}"`);
1013
- }
1014
- });
1015
- let generatedCode = "";
1016
- for (const id of args) {
1017
- generatedCode += `let ${id} = _args.get('${id}');
1018
- `;
1019
- }
1020
- for (const id of inputVariables) {
1021
- generatedCode += `let ${id} = _variables.get('${id}');
1022
- `;
1023
- }
1024
- for (const id of outputVariables) {
1025
- if (inputVariables.has(id) === false) {
1026
- generatedCode += `let ${id};
1027
- `;
1028
- }
1029
- }
1030
- generatedCode += `${code};
1031
- `;
1032
- generatedCode += `return new Map([
1033
- `;
1034
- for (const id of outputVariables) {
1035
- generatedCode += ` ['${id}', ${id}],
1036
- `;
1037
- }
1038
- generatedCode += `]);`;
1039
- return generatedCode;
1040
- };
1041
- var executeEffectfulExpression = (code, args, variables) => {
1042
- const generatedCode = generateEffectfulExpression(
1043
- code,
1044
- new Set(args.keys()),
1045
- new Set(variables.keys())
1046
- );
1047
- const executeFn = new Function("_variables", "_args", generatedCode);
1048
- const values = executeFn(variables, args);
1049
- return values;
1050
- };
1051
890
  var computeExpressionDependencies = (expressions, expressionId, dependencies) => {
1052
891
  const depsById = dependencies.get(expressionId);
1053
892
  if (depsById) {
@@ -1087,13 +926,6 @@ var encodeDataSourceVariable = (id) => {
1087
926
  const encoded = id.replaceAll("-", "__DASH__");
1088
927
  return `${dataSourceVariablePrefix}${encoded}`;
1089
928
  };
1090
- var encodeVariablesMap = (values) => {
1091
- const encodedValues = /* @__PURE__ */ new Map();
1092
- for (const [id, value] of values) {
1093
- encodedValues.set(encodeDataSourceVariable(id), value);
1094
- }
1095
- return encodedValues;
1096
- };
1097
929
  var decodeDataSourceVariable = (name) => {
1098
930
  if (name.startsWith(dataSourceVariablePrefix)) {
1099
931
  const encoded = name.slice(dataSourceVariablePrefix.length);
@@ -1101,15 +933,117 @@ var decodeDataSourceVariable = (name) => {
1101
933
  }
1102
934
  return;
1103
935
  };
1104
- var decodeVariablesMap = (values) => {
1105
- const decodedValues = /* @__PURE__ */ new Map();
1106
- for (const [name, value] of values) {
1107
- const id = decodeDataSourceVariable(name);
1108
- if (id !== void 0) {
1109
- decodedValues.set(id, value);
936
+ var generateDataSources = ({
937
+ scope,
938
+ typed = false,
939
+ dataSources,
940
+ props
941
+ }) => {
942
+ const variables = /* @__PURE__ */ new Map();
943
+ let body = "";
944
+ const output = /* @__PURE__ */ new Map();
945
+ const depsById = /* @__PURE__ */ new Map();
946
+ const codeById = /* @__PURE__ */ new Map();
947
+ for (const dataSource of dataSources.values()) {
948
+ if (dataSource.type === "expression") {
949
+ const deps = /* @__PURE__ */ new Set();
950
+ const newCode = validateExpression(dataSource.code, {
951
+ transformIdentifier: (identifier) => {
952
+ const depId = decodeDataSourceVariable(identifier);
953
+ const dep = depId ? dataSources.get(depId) : void 0;
954
+ if (dep) {
955
+ deps.add(dep.id);
956
+ return scope.getName(dep.id, dep.name);
957
+ }
958
+ console.error(`Unknown dependency "${identifier}"`);
959
+ return identifier;
960
+ }
961
+ });
962
+ depsById.set(dataSource.id, deps);
963
+ codeById.set(dataSource.id, newCode);
964
+ }
965
+ }
966
+ const sortedDataSources = sortTopologically(
967
+ new Set(dataSources.keys()),
968
+ depsById
969
+ );
970
+ for (const dataSourceId of sortedDataSources) {
971
+ const dataSource = dataSources.get(dataSourceId);
972
+ if (dataSource?.type === "variable") {
973
+ const valueName = scope.getName(dataSource.id, dataSource.name);
974
+ const setterName = scope.getName(
975
+ `set$${dataSource.id}`,
976
+ `set$${dataSource.name}`
977
+ );
978
+ const initialValue = dataSource.value.value;
979
+ output.set(dataSource.id, valueName);
980
+ variables.set(dataSource.id, { valueName, setterName, initialValue });
981
+ }
982
+ if (dataSource?.type === "expression") {
983
+ const name = scope.getName(dataSource.id, dataSource.name);
984
+ const code = codeById.get(dataSourceId);
985
+ output.set(dataSource.id, name);
986
+ body += `let ${name} = (${code});
987
+ `;
1110
988
  }
1111
989
  }
1112
- return decodedValues;
990
+ for (const prop of props.values()) {
991
+ if (prop.type !== "action") {
992
+ continue;
993
+ }
994
+ const name = scope.getName(prop.id, prop.name);
995
+ output.set(prop.id, name);
996
+ const setters = /* @__PURE__ */ new Set();
997
+ let args = void 0;
998
+ let newCode = "";
999
+ for (const value of prop.value) {
1000
+ args = value.args;
1001
+ newCode += validateExpression(value.code, {
1002
+ effectful: true,
1003
+ transformIdentifier: (identifier, assignee) => {
1004
+ if (args?.includes(identifier)) {
1005
+ return identifier;
1006
+ }
1007
+ const depId = decodeDataSourceVariable(identifier);
1008
+ const dep = depId ? dataSources.get(depId) : void 0;
1009
+ if (dep) {
1010
+ const name2 = scope.getName(dep.id, dep.name);
1011
+ if (assignee) {
1012
+ setters.add(dep.id);
1013
+ }
1014
+ return name2;
1015
+ }
1016
+ console.error(`Unknown dependency "${identifier}"`);
1017
+ return identifier;
1018
+ }
1019
+ });
1020
+ newCode += `
1021
+ `;
1022
+ }
1023
+ if (args === void 0) {
1024
+ continue;
1025
+ }
1026
+ if (typed) {
1027
+ args = args.map((arg) => `${arg}: any`);
1028
+ }
1029
+ body += `let ${name} = (${args.join(", ")}) => {
1030
+ `;
1031
+ body += newCode;
1032
+ for (const dataSourceId of setters.values()) {
1033
+ const variable = variables.get(dataSourceId);
1034
+ if (variable) {
1035
+ body += `${variable.setterName}(${variable.valueName})
1036
+ `;
1037
+ }
1038
+ }
1039
+ body += `}
1040
+ `;
1041
+ }
1042
+ return {
1043
+ variables,
1044
+ body,
1045
+ output
1046
+ };
1113
1047
  };
1114
1048
 
1115
1049
  // src/embed-template.ts
@@ -1509,7 +1443,7 @@ var WsComponentMeta = z3.object({
1509
1443
  });
1510
1444
 
1511
1445
  // src/component-renderer.tsx
1512
- import { getStyleDeclKey } from "@webstudio-is/sdk";
1446
+ import { createScope, getStyleDeclKey } from "@webstudio-is/sdk";
1513
1447
 
1514
1448
  // src/instance-utils.ts
1515
1449
  var getIndexesWithinAncestors = (metas, instances, rootIds) => {
@@ -1644,25 +1578,46 @@ var renderComponentTemplate = ({
1644
1578
  new Map(instances),
1645
1579
  ["root"]
1646
1580
  ),
1647
- executeComputingExpressions: (values) => {
1648
- const expressions = /* @__PURE__ */ new Map();
1649
- for (const dataSource of data.dataSources) {
1650
- const name2 = encodeDataSourceVariable(dataSource.id);
1651
- if (dataSource.type === "expression") {
1652
- expressions.set(name2, dataSource.code);
1653
- }
1654
- }
1655
- return decodeVariablesMap(
1656
- executeComputingExpressions(
1657
- expressions,
1658
- encodeVariablesMap(values)
1581
+ getDataSourcesLogic(getVariable, setVariable) {
1582
+ const { variables, body, output } = generateDataSources({
1583
+ scope: createScope(["_getVariable", "_setVariable", "_output"]),
1584
+ props: new Map(data.props.map((prop) => [prop.id, prop])),
1585
+ dataSources: new Map(
1586
+ data.dataSources.map((dataSource) => [
1587
+ dataSource.id,
1588
+ dataSource
1589
+ ])
1659
1590
  )
1660
- );
1661
- },
1662
- executeEffectfulExpression: (code, args, values) => {
1663
- return decodeVariablesMap(
1664
- executeEffectfulExpression(code, args, encodeVariablesMap(values))
1665
- );
1591
+ });
1592
+ let generatedCode = "";
1593
+ for (const [dataSourceId, variable] of variables) {
1594
+ const { valueName, setterName } = variable;
1595
+ const initialValue = JSON.stringify(variable.initialValue);
1596
+ generatedCode += `let ${valueName} = _getVariable("${dataSourceId}") ?? ${initialValue};
1597
+ `;
1598
+ generatedCode += `let ${setterName} = (value) => _setVariable("${dataSourceId}", value);
1599
+ `;
1600
+ }
1601
+ generatedCode += body;
1602
+ generatedCode += `let _output = new Map();
1603
+ `;
1604
+ for (const [dataSourceId, variableName] of output) {
1605
+ generatedCode += `_output.set('${dataSourceId}', ${variableName})
1606
+ `;
1607
+ }
1608
+ generatedCode += `return _output
1609
+ `;
1610
+ try {
1611
+ const executeFn = new Function(
1612
+ "_getVariable",
1613
+ "_setVariable",
1614
+ generatedCode
1615
+ );
1616
+ return executeFn(getVariable, setVariable);
1617
+ } catch (error) {
1618
+ console.error(error);
1619
+ }
1620
+ return /* @__PURE__ */ new Map();
1666
1621
  }
1667
1622
  },
1668
1623
  Component: WebstudioComponent,
@@ -1687,6 +1642,9 @@ var getClosestInstance = (instancePath, currentInstance, closestComponent) => {
1687
1642
  };
1688
1643
 
1689
1644
  // src/generator.ts
1645
+ import {
1646
+ createScope as createScope2
1647
+ } from "@webstudio-is/sdk";
1690
1648
  var generateUtilsExport = (siteData) => {
1691
1649
  const indexesWithinAncestors = getIndexesWithinAncestors(
1692
1650
  siteData.metas,
@@ -1705,91 +1663,256 @@ var generateUtilsExport = (siteData) => {
1705
1663
  ${indexesWithinAncestorsEntries}
1706
1664
  ]);
1707
1665
  `;
1708
- const variables = /* @__PURE__ */ new Set();
1709
- const expressions = /* @__PURE__ */ new Map();
1710
- for (const dataSource of siteData.dataSources.values()) {
1711
- if (dataSource.type === "variable") {
1712
- variables.add(encodeDataSourceVariable(dataSource.id));
1713
- }
1714
- if (dataSource.type === "expression") {
1715
- expressions.set(encodeDataSourceVariable(dataSource.id), dataSource.code);
1716
- }
1666
+ const { variables, body, output } = generateDataSources({
1667
+ scope: createScope2(["_getVariable", "_setVariable", "_output"]),
1668
+ typed: true,
1669
+ dataSources: siteData.dataSources,
1670
+ props: siteData.props
1671
+ });
1672
+ let generatedDataSources = "";
1673
+ generatedDataSources += `const getDataSourcesLogic = (
1674
+ `;
1675
+ generatedDataSources += ` _getVariable: (id: string) => unknown,
1676
+ `;
1677
+ generatedDataSources += ` _setVariable: (id: string, value: unknown) => void
1678
+ `;
1679
+ generatedDataSources += `) => {
1680
+ `;
1681
+ for (const [dataSourceId, variable] of variables) {
1682
+ const { valueName, setterName } = variable;
1683
+ const initialValue = JSON.stringify(variable.initialValue);
1684
+ generatedDataSources += `let ${valueName} = _getVariable("${dataSourceId}") ?? ${initialValue};
1685
+ `;
1686
+ generatedDataSources += `let ${setterName} = (value: unknown) => _setVariable("${dataSourceId}", value);
1687
+ `;
1717
1688
  }
1718
- const generatedExecuteComputingExpressions = `
1719
- const rawExecuteComputingExpressions = (
1720
- _variables: Map<string, unknown>
1721
- ): Map<string, unknown> => {
1722
- ${generateComputingExpressions(expressions, variables)}
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}],
1689
+ generatedDataSources += body;
1690
+ generatedDataSources += `let _output = new Map();
1691
+ `;
1692
+ for (const [dataSourceId, variableName] of output) {
1693
+ generatedDataSources += `_output.set('${dataSourceId}', ${variableName})
1694
+ `;
1695
+ }
1696
+ generatedDataSources += `return _output
1697
+ `;
1698
+ generatedDataSources += `}
1742
1699
  `;
1700
+ const formsProperties = /* @__PURE__ */ new Map();
1701
+ for (const prop of siteData.props.values()) {
1702
+ if (prop.type === "string") {
1703
+ if (prop.name === "action" || prop.name === "method") {
1704
+ let properties = formsProperties.get(prop.instanceId);
1705
+ if (properties === void 0) {
1706
+ properties = {};
1707
+ }
1708
+ properties[prop.name] = prop.value;
1709
+ formsProperties.set(prop.instanceId, properties);
1743
1710
  }
1744
1711
  }
1745
1712
  }
1746
- const generatedExecuteEffectfulExpression = `const generatedEffectfulExpressions = new Map<
1747
- string,
1748
- (args: Map<string, any>, variables: Map<string, any>) => Map<string, unknown>
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
- `;
1713
+ const generatedFormsProperties = `export const formsProperties = new Map<string, { method?: string, action?: string }>(${JSON.stringify(
1714
+ Array.from(formsProperties.entries())
1715
+ )})`;
1775
1716
  return `
1776
1717
  /* eslint-disable */
1777
1718
 
1778
1719
  ${generatedIndexesWithinAncestors.trim()}
1779
1720
 
1780
- ${generatedExecuteComputingExpressions.trim()}
1721
+ ${generatedDataSources}
1781
1722
 
1782
- ${generatedExecuteEffectfulExpression.trim()}
1723
+ ${generatedFormsProperties}
1783
1724
 
1784
1725
  export const utils = {
1785
1726
  indexesWithinAncestors,
1786
- executeComputingExpressions,
1787
- executeEffectfulExpression,
1727
+ getDataSourcesLogic,
1788
1728
  };
1789
1729
 
1790
1730
  /* eslint-enable */
1791
1731
  `;
1792
1732
  };
1733
+
1734
+ // src/component-generator.ts
1735
+ import { parseComponentName } from "@webstudio-is/sdk";
1736
+ var generateJsxElement = ({
1737
+ scope,
1738
+ instance,
1739
+ props,
1740
+ dataSources,
1741
+ indexesWithinAncestors,
1742
+ children
1743
+ }) => {
1744
+ let conditionVariableName;
1745
+ let generatedProps = "";
1746
+ generatedProps += `
1747
+ ${idAttribute}=${JSON.stringify(instance.id)}`;
1748
+ generatedProps += `
1749
+ ${componentAttribute}=${JSON.stringify(
1750
+ instance.component
1751
+ )}`;
1752
+ const index = indexesWithinAncestors.get(instance.id);
1753
+ if (index !== void 0) {
1754
+ generatedProps += `
1755
+ ${indexAttribute}="${index}"`;
1756
+ }
1757
+ for (const prop of props.values()) {
1758
+ if (prop.instanceId !== instance.id) {
1759
+ continue;
1760
+ }
1761
+ if (prop.name === showAttribute) {
1762
+ if (prop.type === "boolean" && prop.value === false) {
1763
+ return "";
1764
+ }
1765
+ if (prop.type === "dataSource") {
1766
+ const dataSourceId = prop.value;
1767
+ const dataSource = dataSources.get(dataSourceId);
1768
+ if (dataSource === void 0) {
1769
+ continue;
1770
+ }
1771
+ conditionVariableName = scope.getName(dataSource.id, dataSource.name);
1772
+ }
1773
+ continue;
1774
+ }
1775
+ if (prop.type === "string" || prop.type === "number" || prop.type === "boolean" || prop.type === "string[]") {
1776
+ generatedProps += `
1777
+ ${prop.name}={${JSON.stringify(prop.value)}}`;
1778
+ continue;
1779
+ }
1780
+ if (prop.type === "asset" || prop.type === "page") {
1781
+ continue;
1782
+ }
1783
+ if (prop.type === "dataSource") {
1784
+ const dataSourceId = prop.value;
1785
+ const dataSource = dataSources.get(dataSourceId);
1786
+ if (dataSource === void 0) {
1787
+ continue;
1788
+ }
1789
+ const dataSourceVariable = scope.getName(dataSource.id, dataSource.name);
1790
+ generatedProps += `
1791
+ ${prop.name}={${dataSourceVariable}}`;
1792
+ continue;
1793
+ }
1794
+ if (prop.type === "action") {
1795
+ const propVariable = scope.getName(prop.id, prop.name);
1796
+ generatedProps += `
1797
+ ${prop.name}={${propVariable}}`;
1798
+ continue;
1799
+ }
1800
+ prop;
1801
+ }
1802
+ let generatedElement = "";
1803
+ if (conditionVariableName) {
1804
+ generatedElement += `{${conditionVariableName} &&
1805
+ `;
1806
+ }
1807
+ const [_namespace, shortName] = parseComponentName(instance.component);
1808
+ const componentVariable = scope.getName(instance.component, shortName);
1809
+ if (instance.children.length === 0) {
1810
+ generatedElement += `<${componentVariable}${generatedProps} />
1811
+ `;
1812
+ } else {
1813
+ generatedElement += `<${componentVariable}${generatedProps}>
1814
+ `;
1815
+ generatedElement += children;
1816
+ generatedElement += `</${componentVariable}>
1817
+ `;
1818
+ }
1819
+ if (conditionVariableName) {
1820
+ generatedElement += `}
1821
+ `;
1822
+ }
1823
+ return generatedElement;
1824
+ };
1825
+ var generateJsxChildren = ({
1826
+ scope,
1827
+ children,
1828
+ instances,
1829
+ props,
1830
+ dataSources,
1831
+ indexesWithinAncestors
1832
+ }) => {
1833
+ let generatedChildren = "";
1834
+ for (const child of children) {
1835
+ if (child.type === "text") {
1836
+ generatedChildren += child.value.split("\n").map((line) => `{${JSON.stringify(line)}}
1837
+ `).join(`<br />
1838
+ `);
1839
+ continue;
1840
+ }
1841
+ if (child.type === "id") {
1842
+ const instanceId = child.value;
1843
+ const instance = instances.get(instanceId);
1844
+ if (instance === void 0) {
1845
+ continue;
1846
+ }
1847
+ generatedChildren += generateJsxElement({
1848
+ scope,
1849
+ instance,
1850
+ props,
1851
+ dataSources,
1852
+ indexesWithinAncestors,
1853
+ children: generateJsxChildren({
1854
+ scope,
1855
+ children: instance.children,
1856
+ instances,
1857
+ props,
1858
+ dataSources,
1859
+ indexesWithinAncestors
1860
+ })
1861
+ });
1862
+ continue;
1863
+ }
1864
+ child;
1865
+ }
1866
+ return generatedChildren;
1867
+ };
1868
+ var generatePageComponent = ({
1869
+ scope,
1870
+ rootInstanceId,
1871
+ instances,
1872
+ props,
1873
+ dataSources,
1874
+ indexesWithinAncestors
1875
+ }) => {
1876
+ const instance = instances.get(rootInstanceId);
1877
+ if (instance === void 0) {
1878
+ return "";
1879
+ }
1880
+ const { variables, body: dataSourcesBody } = generateDataSources({
1881
+ scope,
1882
+ dataSources,
1883
+ props
1884
+ });
1885
+ let generatedDataSources = "";
1886
+ for (const { valueName, setterName, initialValue } of variables.values()) {
1887
+ const initialValueString = JSON.stringify(initialValue);
1888
+ generatedDataSources += `let [${valueName}, ${setterName}] = useState(${initialValueString})
1889
+ `;
1890
+ }
1891
+ generatedDataSources += dataSourcesBody;
1892
+ const generatedJsx = generateJsxElement({
1893
+ scope,
1894
+ instance,
1895
+ props,
1896
+ dataSources,
1897
+ indexesWithinAncestors,
1898
+ children: generateJsxChildren({
1899
+ scope,
1900
+ children: instance.children,
1901
+ instances,
1902
+ props,
1903
+ dataSources,
1904
+ indexesWithinAncestors
1905
+ }) + "{props.scripts}\n"
1906
+ });
1907
+ let generatedComponent = "";
1908
+ generatedComponent += `export const Page = (props: { scripts: ReactNode }) => {
1909
+ `;
1910
+ generatedComponent += `${generatedDataSources}`;
1911
+ generatedComponent += `return ${generatedJsx}`;
1912
+ generatedComponent += `}
1913
+ `;
1914
+ return generatedComponent;
1915
+ };
1793
1916
  export {
1794
1917
  EmbedTemplateInstance,
1795
1918
  EmbedTemplateStyleDecl,
@@ -1807,16 +1930,12 @@ export {
1807
1930
  createElementsTree,
1808
1931
  createImageValueTransformer,
1809
1932
  decodeDataSourceVariable,
1810
- decodeVariablesMap,
1811
1933
  defaultStates,
1812
1934
  encodeDataSourceVariable,
1813
- encodeVariablesMap,
1814
- executeComputingExpressions,
1815
- executeEffectfulExpression,
1816
- generateComputingExpressions,
1817
1935
  generateCssText,
1818
1936
  generateDataFromEmbedTemplate,
1819
- generateEffectfulExpression,
1937
+ generateDataSources,
1938
+ generatePageComponent,
1820
1939
  generateUtilsExport,
1821
1940
  getClosestInstance,
1822
1941
  getIndexWithinAncestorFromComponentProps,