@webstudio-is/react-sdk 0.99.1-87c0eb4.0 → 0.99.1-926a4f6.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
@@ -887,117 +887,6 @@ var sortTopologically = (list, depsById, explored = /* @__PURE__ */ new Set(), s
887
887
  }
888
888
  return sorted;
889
889
  };
890
- var generateComputingExpressions = (expressions, allowedVariables) => {
891
- const depsById = /* @__PURE__ */ new Map();
892
- const inputVariables = /* @__PURE__ */ new Set();
893
- for (const [id, code] of expressions) {
894
- const deps = /* @__PURE__ */ new Set();
895
- validateExpression(code, {
896
- transformIdentifier: (identifier) => {
897
- if (allowedVariables.has(identifier)) {
898
- inputVariables.add(identifier);
899
- return identifier;
900
- }
901
- if (expressions.has(identifier)) {
902
- deps.add(identifier);
903
- return identifier;
904
- }
905
- throw Error(`Unknown dependency "${identifier}"`);
906
- }
907
- });
908
- depsById.set(id, deps);
909
- }
910
- const sortedExpressions = sortTopologically(
911
- new Set(expressions.keys()),
912
- depsById
913
- );
914
- let generatedCode = "";
915
- for (const id of inputVariables) {
916
- generatedCode += `const ${id} = _variables.get('${id}');
917
- `;
918
- }
919
- for (const id of sortedExpressions) {
920
- const code = expressions.get(id);
921
- if (code === void 0) {
922
- continue;
923
- }
924
- generatedCode += `const ${id} = (${code});
925
- `;
926
- }
927
- generatedCode += `return new Map([
928
- `;
929
- for (const id of sortedExpressions) {
930
- generatedCode += ` ['${id}', ${id}],
931
- `;
932
- }
933
- generatedCode += `]);`;
934
- return generatedCode;
935
- };
936
- var executeComputingExpressions = (expressions, variables) => {
937
- const generatedCode = generateComputingExpressions(
938
- expressions,
939
- new Set(variables.keys())
940
- );
941
- const executeFn = new Function("_variables", generatedCode);
942
- const values = executeFn(variables);
943
- return values;
944
- };
945
- var generateEffectfulExpression = (code, args, allowedVariables) => {
946
- const inputVariables = /* @__PURE__ */ new Set();
947
- const outputVariables = /* @__PURE__ */ new Set();
948
- validateExpression(code, {
949
- effectful: true,
950
- transformIdentifier: (identifier, assignee) => {
951
- if (args.has(identifier)) {
952
- return identifier;
953
- }
954
- if (allowedVariables.has(identifier)) {
955
- if (assignee) {
956
- outputVariables.add(identifier);
957
- } else {
958
- inputVariables.add(identifier);
959
- }
960
- return identifier;
961
- }
962
- throw Error(`Unknown dependency "${identifier}"`);
963
- }
964
- });
965
- let generatedCode = "";
966
- for (const id of args) {
967
- generatedCode += `let ${id} = _args.get('${id}');
968
- `;
969
- }
970
- for (const id of inputVariables) {
971
- generatedCode += `let ${id} = _variables.get('${id}');
972
- `;
973
- }
974
- for (const id of outputVariables) {
975
- if (inputVariables.has(id) === false) {
976
- generatedCode += `let ${id};
977
- `;
978
- }
979
- }
980
- generatedCode += `${code};
981
- `;
982
- generatedCode += `return new Map([
983
- `;
984
- for (const id of outputVariables) {
985
- generatedCode += ` ['${id}', ${id}],
986
- `;
987
- }
988
- generatedCode += `]);`;
989
- return generatedCode;
990
- };
991
- var executeEffectfulExpression = (code, args, variables) => {
992
- const generatedCode = generateEffectfulExpression(
993
- code,
994
- new Set(args.keys()),
995
- new Set(variables.keys())
996
- );
997
- const executeFn = new Function("_variables", "_args", generatedCode);
998
- const values = executeFn(variables, args);
999
- return values;
1000
- };
1001
890
  var computeExpressionDependencies = (expressions, expressionId, dependencies) => {
1002
891
  const depsById = dependencies.get(expressionId);
1003
892
  if (depsById) {
@@ -1037,13 +926,6 @@ var encodeDataSourceVariable = (id) => {
1037
926
  const encoded = id.replaceAll("-", "__DASH__");
1038
927
  return `${dataSourceVariablePrefix}${encoded}`;
1039
928
  };
1040
- var encodeVariablesMap = (values) => {
1041
- const encodedValues = /* @__PURE__ */ new Map();
1042
- for (const [id, value] of values) {
1043
- encodedValues.set(encodeDataSourceVariable(id), value);
1044
- }
1045
- return encodedValues;
1046
- };
1047
929
  var decodeDataSourceVariable = (name) => {
1048
930
  if (name.startsWith(dataSourceVariablePrefix)) {
1049
931
  const encoded = name.slice(dataSourceVariablePrefix.length);
@@ -1051,16 +933,6 @@ var decodeDataSourceVariable = (name) => {
1051
933
  }
1052
934
  return;
1053
935
  };
1054
- var decodeVariablesMap = (values) => {
1055
- const decodedValues = /* @__PURE__ */ new Map();
1056
- for (const [name, value] of values) {
1057
- const id = decodeDataSourceVariable(name);
1058
- if (id !== void 0) {
1059
- decodedValues.set(id, value);
1060
- }
1061
- }
1062
- return decodedValues;
1063
- };
1064
936
  var generateDataSources = ({
1065
937
  scope,
1066
938
  typed = false,
@@ -1270,11 +1142,11 @@ var getDataSourceValue = (value) => {
1270
1142
  value;
1271
1143
  throw Error("Impossible case");
1272
1144
  };
1273
- var createInstancesFromTemplate = (treeTemplate, instances, props, dataSourceByRef, styleSourceSelections, styleSources, styles, metas, defaultBreakpointId) => {
1145
+ var createInstancesFromTemplate = (treeTemplate, instances, props, dataSourceByRef, styleSourceSelections, styleSources, styles, metas, defaultBreakpointId, generateId) => {
1274
1146
  const parentChildren = [];
1275
1147
  for (const item of treeTemplate) {
1276
1148
  if (item.type === "instance") {
1277
- const instanceId = nanoid();
1149
+ const instanceId = generateId();
1278
1150
  if (item.dataSources) {
1279
1151
  for (const [name, dataSource] of Object.entries(item.dataSources)) {
1280
1152
  if (dataSourceByRef.has(name)) {
@@ -1283,7 +1155,7 @@ var createInstancesFromTemplate = (treeTemplate, instances, props, dataSourceByR
1283
1155
  if (dataSource.type === "variable") {
1284
1156
  dataSourceByRef.set(name, {
1285
1157
  type: "variable",
1286
- id: nanoid(),
1158
+ id: generateId(),
1287
1159
  scopeInstanceId: instanceId,
1288
1160
  name,
1289
1161
  value: getDataSourceValue(dataSource.initialValue)
@@ -1292,7 +1164,7 @@ var createInstancesFromTemplate = (treeTemplate, instances, props, dataSourceByR
1292
1164
  if (dataSource.type === "expression") {
1293
1165
  dataSourceByRef.set(name, {
1294
1166
  type: "expression",
1295
- id: nanoid(),
1167
+ id: generateId(),
1296
1168
  scopeInstanceId: instanceId,
1297
1169
  name,
1298
1170
  // replace all references with variable names
@@ -1308,7 +1180,7 @@ var createInstancesFromTemplate = (treeTemplate, instances, props, dataSourceByR
1308
1180
  }
1309
1181
  if (item.props) {
1310
1182
  for (const prop of item.props) {
1311
- const propId = nanoid();
1183
+ const propId = generateId();
1312
1184
  if (prop.type === "action") {
1313
1185
  props.push({
1314
1186
  id: propId,
@@ -1381,7 +1253,7 @@ var createInstancesFromTemplate = (treeTemplate, instances, props, dataSourceByR
1381
1253
  }
1382
1254
  }
1383
1255
  if (item.styles) {
1384
- const styleSourceId = nanoid();
1256
+ const styleSourceId = generateId();
1385
1257
  styleSources.push({
1386
1258
  type: "local",
1387
1259
  id: styleSourceId
@@ -1420,7 +1292,8 @@ var createInstancesFromTemplate = (treeTemplate, instances, props, dataSourceByR
1420
1292
  styleSources,
1421
1293
  styles,
1422
1294
  metas,
1423
- defaultBreakpointId
1295
+ defaultBreakpointId,
1296
+ generateId
1424
1297
  );
1425
1298
  parentChildren.push({
1426
1299
  type: "id",
@@ -1436,7 +1309,7 @@ var createInstancesFromTemplate = (treeTemplate, instances, props, dataSourceByR
1436
1309
  }
1437
1310
  return parentChildren;
1438
1311
  };
1439
- var generateDataFromEmbedTemplate = (treeTemplate, metas, defaultBreakpointId) => {
1312
+ var generateDataFromEmbedTemplate = (treeTemplate, metas, defaultBreakpointId, generateId = nanoid) => {
1440
1313
  const instances = [];
1441
1314
  const props = [];
1442
1315
  const dataSourceByRef = /* @__PURE__ */ new Map();
@@ -1452,7 +1325,8 @@ var generateDataFromEmbedTemplate = (treeTemplate, metas, defaultBreakpointId) =
1452
1325
  styleSources,
1453
1326
  styles,
1454
1327
  metas,
1455
- defaultBreakpointId
1328
+ defaultBreakpointId,
1329
+ generateId
1456
1330
  );
1457
1331
  return {
1458
1332
  children,
@@ -1563,16 +1437,15 @@ var WsComponentMeta = z3.object({
1563
1437
  label: z3.optional(z3.string()),
1564
1438
  description: z3.string().optional(),
1565
1439
  icon: z3.string(),
1566
- presetStyle: z3.optional(z3.record(z3.string(), EmbedTemplateStyleDecl)),
1440
+ presetStyle: z3.optional(
1441
+ z3.record(z3.string(), z3.array(EmbedTemplateStyleDecl))
1442
+ ),
1567
1443
  presetTokens: z3.optional(z3.record(z3.string(), ComponentToken)),
1568
1444
  states: z3.optional(z3.array(ComponentState)),
1569
1445
  template: z3.optional(WsEmbedTemplate),
1570
1446
  order: z3.number().optional()
1571
1447
  });
1572
1448
 
1573
- // src/component-renderer.tsx
1574
- import { createScope, getStyleDeclKey } from "@webstudio-is/sdk";
1575
-
1576
1449
  // src/instance-utils.ts
1577
1450
  var getIndexesWithinAncestors = (metas, instances, rootIds) => {
1578
1451
  const ancestors = /* @__PURE__ */ new Set();
@@ -1617,145 +1490,6 @@ var getIndexesWithinAncestors = (metas, instances, rootIds) => {
1617
1490
  return indexes;
1618
1491
  };
1619
1492
 
1620
- // src/component-renderer.tsx
1621
- import { Fragment as Fragment4, jsx as jsx4, jsxs as jsxs4 } from "react/jsx-runtime";
1622
- var renderComponentTemplate = ({
1623
- name,
1624
- metas: metasRecord,
1625
- components,
1626
- props,
1627
- imageLoader
1628
- }) => {
1629
- const metas = new Map(Object.entries(metasRecord));
1630
- const template = metas.get(name)?.template ?? [
1631
- {
1632
- type: "instance",
1633
- component: name,
1634
- children: []
1635
- }
1636
- ];
1637
- if (template[0].type === "instance" && props !== void 0) {
1638
- template[0].props = Object.entries(props).map(([prop, value]) => {
1639
- if (typeof value === "string") {
1640
- return { type: "string", name: prop, value };
1641
- }
1642
- if (typeof value === "number") {
1643
- return { type: "number", name: prop, value };
1644
- }
1645
- if (typeof value === "boolean") {
1646
- return { type: "boolean", name: prop, value };
1647
- }
1648
- throw new Error(`Unsupported prop ${props} with value ${value}`);
1649
- });
1650
- }
1651
- const data = generateDataFromEmbedTemplate(template, metas, "base");
1652
- const instances = [
1653
- [
1654
- "root",
1655
- {
1656
- type: "instance",
1657
- id: "root",
1658
- component: "Box",
1659
- children: data.children
1660
- }
1661
- ],
1662
- ...data.instances.map(
1663
- (instance) => [instance.id, instance]
1664
- )
1665
- ];
1666
- return /* @__PURE__ */ jsxs4(Fragment4, { children: [
1667
- /* @__PURE__ */ jsx4("style", { children: generateCssText(
1668
- {
1669
- assets: [],
1670
- breakpoints: [["base", { id: "base", label: "base" }]],
1671
- styles: data.styles.map((item) => [getStyleDeclKey(item), item]),
1672
- styleSourceSelections: data.styleSourceSelections.map((item) => [
1673
- item.instanceId,
1674
- item
1675
- ]),
1676
- componentMetas: metas
1677
- },
1678
- { assetBaseUrl: "/" }
1679
- ) }),
1680
- /* @__PURE__ */ jsx4(
1681
- InstanceRoot,
1682
- {
1683
- data: {
1684
- page: {
1685
- path: "",
1686
- id: "",
1687
- name: "",
1688
- title: "",
1689
- meta: {},
1690
- rootInstanceId: "root"
1691
- },
1692
- pages: [],
1693
- assets: [],
1694
- build: {
1695
- instances,
1696
- props: data.props.map((prop) => [prop.id, prop]),
1697
- dataSources: data.dataSources.map((dataSource) => [
1698
- dataSource.id,
1699
- dataSource
1700
- ])
1701
- }
1702
- },
1703
- utils: {
1704
- indexesWithinAncestors: getIndexesWithinAncestors(
1705
- metas,
1706
- new Map(instances),
1707
- ["root"]
1708
- ),
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
- ])
1718
- )
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();
1749
- }
1750
- },
1751
- Component: WebstudioComponent,
1752
- components: new Map(Object.entries(components)),
1753
- imageLoader: imageLoader ?? (({ src }) => src)
1754
- }
1755
- )
1756
- ] });
1757
- };
1758
-
1759
1493
  // src/hook.ts
1760
1494
  var getClosestInstance = (instancePath, currentInstance, closestComponent) => {
1761
1495
  let matched = false;
@@ -1771,7 +1505,7 @@ var getClosestInstance = (instancePath, currentInstance, closestComponent) => {
1771
1505
 
1772
1506
  // src/generator.ts
1773
1507
  import {
1774
- createScope as createScope2
1508
+ createScope
1775
1509
  } from "@webstudio-is/sdk";
1776
1510
  var generateUtilsExport = (siteData) => {
1777
1511
  const indexesWithinAncestors = getIndexesWithinAncestors(
@@ -1792,7 +1526,7 @@ var generateUtilsExport = (siteData) => {
1792
1526
  ]);
1793
1527
  `;
1794
1528
  const { variables, body, output } = generateDataSources({
1795
- scope: createScope2(["_getVariable", "_setVariable", "_output"]),
1529
+ scope: createScope(["_getVariable", "_setVariable", "_output"]),
1796
1530
  typed: true,
1797
1531
  dataSources: siteData.dataSources,
1798
1532
  props: siteData.props
@@ -1825,6 +1559,22 @@ var generateUtilsExport = (siteData) => {
1825
1559
  `;
1826
1560
  generatedDataSources += `}
1827
1561
  `;
1562
+ const formsProperties = /* @__PURE__ */ new Map();
1563
+ for (const prop of siteData.props.values()) {
1564
+ if (prop.type === "string") {
1565
+ if (prop.name === "action" || prop.name === "method") {
1566
+ let properties = formsProperties.get(prop.instanceId);
1567
+ if (properties === void 0) {
1568
+ properties = {};
1569
+ }
1570
+ properties[prop.name] = prop.value;
1571
+ formsProperties.set(prop.instanceId, properties);
1572
+ }
1573
+ }
1574
+ }
1575
+ const generatedFormsProperties = `export const formsProperties = new Map<string, { method?: string, action?: string }>(${JSON.stringify(
1576
+ Array.from(formsProperties.entries())
1577
+ )})`;
1828
1578
  return `
1829
1579
  /* eslint-disable */
1830
1580
 
@@ -1832,6 +1582,8 @@ var generateUtilsExport = (siteData) => {
1832
1582
 
1833
1583
  ${generatedDataSources}
1834
1584
 
1585
+ ${generatedFormsProperties}
1586
+
1835
1587
  export const utils = {
1836
1588
  indexesWithinAncestors,
1837
1589
  getDataSourcesLogic,
@@ -1842,11 +1594,12 @@ var generateUtilsExport = (siteData) => {
1842
1594
  };
1843
1595
 
1844
1596
  // src/component-generator.ts
1845
- import { findTreeInstanceIds, parseComponentName } from "@webstudio-is/sdk";
1597
+ import { parseComponentName } from "@webstudio-is/sdk";
1846
1598
  var generateJsxElement = ({
1847
1599
  scope,
1848
1600
  instance,
1849
1601
  props,
1602
+ dataSources,
1850
1603
  indexesWithinAncestors,
1851
1604
  children
1852
1605
  }) => {
@@ -1873,7 +1626,11 @@ ${indexAttribute}="${index}"`;
1873
1626
  }
1874
1627
  if (prop.type === "dataSource") {
1875
1628
  const dataSourceId = prop.value;
1876
- conditionVariableName = encodeDataSourceVariable(dataSourceId);
1629
+ const dataSource = dataSources.get(dataSourceId);
1630
+ if (dataSource === void 0) {
1631
+ continue;
1632
+ }
1633
+ conditionVariableName = scope.getName(dataSource.id, dataSource.name);
1877
1634
  }
1878
1635
  continue;
1879
1636
  }
@@ -1887,7 +1644,11 @@ ${prop.name}={${JSON.stringify(prop.value)}}`;
1887
1644
  }
1888
1645
  if (prop.type === "dataSource") {
1889
1646
  const dataSourceId = prop.value;
1890
- const dataSourceVariable = encodeDataSourceVariable(dataSourceId);
1647
+ const dataSource = dataSources.get(dataSourceId);
1648
+ if (dataSource === void 0) {
1649
+ continue;
1650
+ }
1651
+ const dataSourceVariable = scope.getName(dataSource.id, dataSource.name);
1891
1652
  generatedProps += `
1892
1653
  ${prop.name}={${dataSourceVariable}}`;
1893
1654
  continue;
@@ -1928,6 +1689,7 @@ var generateJsxChildren = ({
1928
1689
  children,
1929
1690
  instances,
1930
1691
  props,
1692
+ dataSources,
1931
1693
  indexesWithinAncestors
1932
1694
  }) => {
1933
1695
  let generatedChildren = "";
@@ -1948,12 +1710,14 @@ var generateJsxChildren = ({
1948
1710
  scope,
1949
1711
  instance,
1950
1712
  props,
1713
+ dataSources,
1951
1714
  indexesWithinAncestors,
1952
1715
  children: generateJsxChildren({
1953
1716
  scope,
1954
1717
  children: instance.children,
1955
1718
  instances,
1956
1719
  props,
1720
+ dataSources,
1957
1721
  indexesWithinAncestors
1958
1722
  })
1959
1723
  });
@@ -1963,97 +1727,52 @@ var generateJsxChildren = ({
1963
1727
  }
1964
1728
  return generatedChildren;
1965
1729
  };
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
1730
  var generatePageComponent = ({
2022
1731
  scope,
2023
1732
  rootInstanceId,
2024
1733
  instances,
2025
1734
  props,
1735
+ dataSources,
2026
1736
  indexesWithinAncestors
2027
1737
  }) => {
2028
1738
  const instance = instances.get(rootInstanceId);
2029
1739
  if (instance === void 0) {
2030
1740
  return "";
2031
1741
  }
2032
- const generatedDataSources = generateDataSources2({
1742
+ const { variables, body: dataSourcesBody } = generateDataSources({
1743
+ typed: true,
2033
1744
  scope,
2034
- rootInstanceId,
2035
- instances,
1745
+ dataSources,
2036
1746
  props
2037
1747
  });
1748
+ let generatedDataSources = "";
1749
+ for (const { valueName, setterName, initialValue } of variables.values()) {
1750
+ const initialValueString = JSON.stringify(initialValue);
1751
+ generatedDataSources += `let [${valueName}, ${setterName}] = useState(${initialValueString})
1752
+ `;
1753
+ }
1754
+ generatedDataSources += dataSourcesBody;
2038
1755
  const generatedJsx = generateJsxElement({
2039
1756
  scope,
2040
1757
  instance,
2041
1758
  props,
1759
+ dataSources,
2042
1760
  indexesWithinAncestors,
2043
1761
  children: generateJsxChildren({
2044
1762
  scope,
2045
1763
  children: instance.children,
2046
1764
  instances,
2047
1765
  props,
1766
+ dataSources,
2048
1767
  indexesWithinAncestors
2049
1768
  }) + "{props.scripts}\n"
2050
1769
  });
2051
1770
  let generatedComponent = "";
2052
- generatedComponent += `export const Page = (props: { scripts: ReactNode }) => {
1771
+ generatedComponent += `const Page = (props: { scripts?: ReactNode }) => {
2053
1772
  `;
2054
1773
  generatedComponent += `${generatedDataSources}`;
2055
1774
  generatedComponent += `return ${generatedJsx}`;
2056
- generatedComponent += `};
1775
+ generatedComponent += `}
2057
1776
  `;
2058
1777
  return generatedComponent;
2059
1778
  };
@@ -2065,6 +1784,7 @@ export {
2065
1784
  ReactSdkContext,
2066
1785
  Root,
2067
1786
  WebstudioComponent,
1787
+ WsComponentMeta,
2068
1788
  WsEmbedTemplate,
2069
1789
  addGlobalRules,
2070
1790
  collapsedAttribute,
@@ -2074,17 +1794,11 @@ export {
2074
1794
  createElementsTree,
2075
1795
  createImageValueTransformer,
2076
1796
  decodeDataSourceVariable,
2077
- decodeVariablesMap,
2078
1797
  defaultStates,
2079
1798
  encodeDataSourceVariable,
2080
- encodeVariablesMap,
2081
- executeComputingExpressions,
2082
- executeEffectfulExpression,
2083
- generateComputingExpressions,
2084
1799
  generateCssText,
2085
1800
  generateDataFromEmbedTemplate,
2086
1801
  generateDataSources,
2087
- generateEffectfulExpression,
2088
1802
  generatePageComponent,
2089
1803
  generateUtilsExport,
2090
1804
  getClosestInstance,
@@ -2096,7 +1810,6 @@ export {
2096
1810
  idAttribute,
2097
1811
  indexAttribute,
2098
1812
  namespaceMeta,
2099
- renderComponentTemplate,
2100
1813
  renderWebstudioComponentChildren,
2101
1814
  selectorIdAttribute,
2102
1815
  showAttribute,
@@ -1,6 +1,6 @@
1
- import type { Instances, Instance, Props, Scope } from "@webstudio-is/sdk";
1
+ import type { Instances, Instance, Props, Scope, DataSources } from "@webstudio-is/sdk";
2
2
  import type { IndexesWithinAncestors } from "./instance-utils";
3
- export declare const generateJsxElement: ({ scope, instance, props, indexesWithinAncestors, children, }: {
3
+ export declare const generateJsxElement: ({ scope, instance, props, dataSources, indexesWithinAncestors, children, }: {
4
4
  scope: Scope;
5
5
  instance: Instance;
6
6
  props: Map<string, {
@@ -70,6 +70,31 @@ export declare const generateJsxElement: ({ scope, instance, props, indexesWithi
70
70
  instanceId: string;
71
71
  required?: boolean | undefined;
72
72
  }>;
73
+ dataSources: Map<string, {
74
+ value: {
75
+ value: number;
76
+ type: "number";
77
+ } | {
78
+ value: string;
79
+ type: "string";
80
+ } | {
81
+ value: boolean;
82
+ type: "boolean";
83
+ } | {
84
+ value: string[];
85
+ type: "string[]";
86
+ };
87
+ type: "variable";
88
+ name: string;
89
+ id: string;
90
+ scopeInstanceId?: string | undefined;
91
+ } | {
92
+ code: string;
93
+ type: "expression";
94
+ name: string;
95
+ id: string;
96
+ scopeInstanceId?: string | undefined;
97
+ }>;
73
98
  indexesWithinAncestors: IndexesWithinAncestors;
74
99
  children: string;
75
100
  }) => string;
@@ -77,7 +102,7 @@ export declare const generateJsxElement: ({ scope, instance, props, indexesWithi
77
102
  * Jsx element and children are generated separately to be able
78
103
  * to inject some scripts into Body if necessary
79
104
  */
80
- export declare const generateJsxChildren: ({ scope, children, instances, props, indexesWithinAncestors, }: {
105
+ export declare const generateJsxChildren: ({ scope, children, instances, props, dataSources, indexesWithinAncestors, }: {
81
106
  scope: Scope;
82
107
  children: Instance["children"];
83
108
  instances: Map<string, {
@@ -160,9 +185,34 @@ export declare const generateJsxChildren: ({ scope, children, instances, props,
160
185
  instanceId: string;
161
186
  required?: boolean | undefined;
162
187
  }>;
188
+ dataSources: Map<string, {
189
+ value: {
190
+ value: number;
191
+ type: "number";
192
+ } | {
193
+ value: string;
194
+ type: "string";
195
+ } | {
196
+ value: boolean;
197
+ type: "boolean";
198
+ } | {
199
+ value: string[];
200
+ type: "string[]";
201
+ };
202
+ type: "variable";
203
+ name: string;
204
+ id: string;
205
+ scopeInstanceId?: string | undefined;
206
+ } | {
207
+ code: string;
208
+ type: "expression";
209
+ name: string;
210
+ id: string;
211
+ scopeInstanceId?: string | undefined;
212
+ }>;
163
213
  indexesWithinAncestors: IndexesWithinAncestors;
164
214
  }) => string;
165
- export declare const generatePageComponent: ({ scope, rootInstanceId, instances, props, indexesWithinAncestors, }: {
215
+ export declare const generatePageComponent: ({ scope, rootInstanceId, instances, props, dataSources, indexesWithinAncestors, }: {
166
216
  scope: Scope;
167
217
  rootInstanceId: Instance["id"];
168
218
  instances: Map<string, {
@@ -245,5 +295,30 @@ export declare const generatePageComponent: ({ scope, rootInstanceId, instances,
245
295
  instanceId: string;
246
296
  required?: boolean | undefined;
247
297
  }>;
298
+ dataSources: Map<string, {
299
+ value: {
300
+ value: number;
301
+ type: "number";
302
+ } | {
303
+ value: string;
304
+ type: "string";
305
+ } | {
306
+ value: boolean;
307
+ type: "boolean";
308
+ } | {
309
+ value: string[];
310
+ type: "string[]";
311
+ };
312
+ type: "variable";
313
+ name: string;
314
+ id: string;
315
+ scopeInstanceId?: string | undefined;
316
+ } | {
317
+ code: string;
318
+ type: "expression";
319
+ name: string;
320
+ id: string;
321
+ scopeInstanceId?: string | undefined;
322
+ }>;
248
323
  indexesWithinAncestors: IndexesWithinAncestors;
249
324
  }) => string;
@@ -670,7 +670,7 @@ export declare const ComponentState: z.ZodObject<{
670
670
  }>;
671
671
  export type ComponentState = z.infer<typeof ComponentState>;
672
672
  export declare const defaultStates: ComponentState[];
673
- declare const WsComponentMeta: z.ZodObject<{
673
+ export declare const WsComponentMeta: z.ZodObject<{
674
674
  category: z.ZodOptional<z.ZodEnum<["general", "text", "media", "forms", "radix", "hidden"]>>;
675
675
  type: z.ZodEnum<["container", "control", "embed", "rich-text-child"]>;
676
676
  requiredAncestors: z.ZodOptional<z.ZodArray<z.ZodString, "many">>;
@@ -681,7 +681,7 @@ declare const WsComponentMeta: z.ZodObject<{
681
681
  label: z.ZodOptional<z.ZodString>;
682
682
  description: z.ZodOptional<z.ZodString>;
683
683
  icon: z.ZodString;
684
- presetStyle: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodType<{
684
+ presetStyle: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodArray<z.ZodType<{
685
685
  value: {
686
686
  value: number;
687
687
  type: "unit";
@@ -1105,7 +1105,7 @@ declare const WsComponentMeta: z.ZodObject<{
1105
1105
  };
1106
1106
  state?: string | undefined;
1107
1107
  property: import("@webstudio-is/css-engine").StyleProperty;
1108
- }>>>;
1108
+ }>, "many">>>;
1109
1109
  presetTokens: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodObject<{
1110
1110
  variant: z.ZodOptional<z.ZodString>;
1111
1111
  styles: z.ZodArray<z.ZodType<{
@@ -2211,7 +2211,7 @@ declare const WsComponentMeta: z.ZodObject<{
2211
2211
  };
2212
2212
  state?: string | undefined;
2213
2213
  property: import("@webstudio-is/css-engine").StyleProperty;
2214
- }> | undefined;
2214
+ }[]> | undefined;
2215
2215
  presetTokens?: Record<string, {
2216
2216
  styles: {
2217
2217
  value: {
@@ -2661,7 +2661,7 @@ declare const WsComponentMeta: z.ZodObject<{
2661
2661
  };
2662
2662
  state?: string | undefined;
2663
2663
  property: import("@webstudio-is/css-engine").StyleProperty;
2664
- }> | undefined;
2664
+ }[]> | undefined;
2665
2665
  presetTokens?: Record<string, {
2666
2666
  styles: {
2667
2667
  value: {
@@ -2196,7 +2196,7 @@ export type WsEmbedTemplate = z.infer<typeof WsEmbedTemplate>;
2196
2196
  export declare const generateDataFromEmbedTemplate: (treeTemplate: ({
2197
2197
  value: string;
2198
2198
  type: "text";
2199
- } | EmbedTemplateInstance)[], metas: Map<Instance["component"], WsComponentMeta>, defaultBreakpointId: Breakpoint["id"]) => {
2199
+ } | EmbedTemplateInstance)[], metas: Map<Instance["component"], WsComponentMeta>, defaultBreakpointId: Breakpoint["id"], generateId?: () => string) => {
2200
2200
  children: ({
2201
2201
  value: string;
2202
2202
  type: "text";
@@ -4,20 +4,9 @@ export declare const validateExpression: (code: string, options?: {
4
4
  effectful?: boolean;
5
5
  transformIdentifier?: TransformIdentifier;
6
6
  }) => string;
7
- /**
8
- * Generates a function body expecting map as _variables argument
9
- * and outputing map of results
10
- */
11
- export declare const generateComputingExpressions: (expressions: Map<string, string>, allowedVariables: Set<string>) => string;
12
- export declare const executeComputingExpressions: (expressions: Map<string, string>, variables: Map<string, unknown>) => Map<string, unknown>;
13
- export declare const generateEffectfulExpression: (code: string, args: Set<string>, allowedVariables: Set<string>) => string;
14
- export declare const executeEffectfulExpression: (code: string, args: Map<string, unknown>, variables: Map<string, unknown>) => Map<string, unknown>;
15
7
  export declare const computeExpressionsDependencies: (expressions: Map<string, string>) => Map<string, Set<string>>;
16
- type Values = Map<string, unknown>;
17
8
  export declare const encodeDataSourceVariable: (id: string) => string;
18
- export declare const encodeVariablesMap: (values: Values) => Values;
19
9
  export declare const decodeDataSourceVariable: (name: string) => string | undefined;
20
- export declare const decodeVariablesMap: (values: Values) => Values;
21
10
  type VariableName = string;
22
11
  export declare const generateDataSources: ({ scope, typed, dataSources, props, }: {
23
12
  scope: Scope;
@@ -1,14 +1,13 @@
1
- export * from "./css";
2
- export * from "./tree";
3
- export * from "./app";
1
+ export * from "./css/index";
2
+ export * from "./tree/index";
3
+ export * from "./app/index";
4
4
  export * from "./components/components-utils";
5
5
  export { PropMeta } from "./prop-meta";
6
- export { type WsComponentPropsMeta, type WsComponentMeta, type ComponentState, type PresetStyle, componentCategories, stateCategories, defaultStates, } from "./components/component-meta";
6
+ export { type WsComponentPropsMeta, type ComponentState, type PresetStyle, WsComponentMeta, componentCategories, stateCategories, defaultStates, } from "./components/component-meta";
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, generateDataSources, } from "./expression";
11
- export { renderComponentTemplate } from "./component-renderer";
10
+ export { validateExpression, computeExpressionsDependencies, encodeDataSourceVariable, decodeDataSourceVariable, generateDataSources, } from "./expression";
12
11
  export { getIndexesWithinAncestors } from "./instance-utils";
13
12
  export * from "./hook";
14
13
  export { generateUtilsExport } from "./generator";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@webstudio-is/react-sdk",
3
- "version": "0.99.1-87c0eb4.0",
3
+ "version": "0.99.1-926a4f6.0",
4
4
  "description": "Webstudio JavaScript / TypeScript API",
5
5
  "author": "Webstudio <github@webstudio.is>",
6
6
  "homepage": "https://webstudio.is",
@@ -17,8 +17,8 @@
17
17
  "type-fest": "^4.3.1",
18
18
  "typescript": "5.2.2",
19
19
  "zod": "^3.21.4",
20
- "@webstudio-is/jest-config": "^1.0.8-87c0eb4.0",
21
- "@webstudio-is/tsconfig": "^1.0.8-87c0eb4.0"
20
+ "@webstudio-is/jest-config": "1.0.8-926a4f6.0",
21
+ "@webstudio-is/tsconfig": "1.0.8-926a4f6.0"
22
22
  },
23
23
  "peerDependencies": {
24
24
  "@remix-run/react": "^1.19.1",
@@ -35,10 +35,10 @@
35
35
  "nanostores": "^0.9.3",
36
36
  "no-case": "^3.0.4",
37
37
  "title-case": "^3.0.3",
38
- "@webstudio-is/css-engine": "^0.99.1-87c0eb4.0",
39
- "@webstudio-is/fonts": "^0.99.1-87c0eb4.0",
40
- "@webstudio-is/image": "^0.99.1-87c0eb4.0",
41
- "@webstudio-is/sdk": "^0.99.1-87c0eb4.0"
38
+ "@webstudio-is/css-engine": "0.99.1-926a4f6.0",
39
+ "@webstudio-is/fonts": "0.99.1-926a4f6.0",
40
+ "@webstudio-is/image": "0.99.1-926a4f6.0",
41
+ "@webstudio-is/sdk": "0.99.1-926a4f6.0"
42
42
  },
43
43
  "exports": {
44
44
  ".": {
@@ -1,11 +0,0 @@
1
- import type { ExoticComponent } from "react";
2
- import type { Instance } from "@webstudio-is/sdk";
3
- import type { WsComponentMeta } from "./components/component-meta";
4
- import type { ImageLoader } from "@webstudio-is/image";
5
- export declare const renderComponentTemplate: ({ name, metas: metasRecord, components, props, imageLoader, }: {
6
- name: Instance["component"];
7
- metas: Record<string, WsComponentMeta>;
8
- props?: Record<string, unknown> | undefined;
9
- components: Record<string, ExoticComponent<any>>;
10
- imageLoader?: ImageLoader | undefined;
11
- }) => import("react/jsx-runtime").JSX.Element;