@webstudio-is/react-sdk 0.76.0 → 0.78.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.
@@ -22,7 +22,117 @@ const outline = [
22
22
  value: { type: "unit", value: 1, unit: "px" }
23
23
  }
24
24
  ];
25
+ const margins = [
26
+ {
27
+ property: "marginTop",
28
+ value: { type: "unit", value: 0, unit: "px" }
29
+ },
30
+ {
31
+ property: "marginRight",
32
+ value: { type: "unit", value: 0, unit: "px" }
33
+ },
34
+ {
35
+ property: "marginBottom",
36
+ value: { type: "unit", value: 0, unit: "px" }
37
+ },
38
+ {
39
+ property: "marginLeft",
40
+ value: { type: "unit", value: 0, unit: "px" }
41
+ }
42
+ ];
43
+ const verticalMargins = [
44
+ {
45
+ property: "marginTop",
46
+ value: { type: "unit", value: 0, unit: "px" }
47
+ },
48
+ {
49
+ property: "marginBottom",
50
+ value: { type: "unit", value: 0, unit: "px" }
51
+ }
52
+ ];
53
+ const blockquote = [
54
+ ...margins,
55
+ {
56
+ property: "paddingTop",
57
+ value: { type: "unit", value: 10, unit: "px" }
58
+ },
59
+ {
60
+ property: "paddingBottom",
61
+ value: { type: "unit", value: 10, unit: "px" }
62
+ },
63
+ {
64
+ property: "paddingLeft",
65
+ value: { type: "unit", value: 20, unit: "px" }
66
+ },
67
+ {
68
+ property: "paddingRight",
69
+ value: { type: "unit", value: 20, unit: "px" }
70
+ },
71
+ {
72
+ property: "borderLeftWidth",
73
+ value: { type: "unit", value: 5, unit: "px" }
74
+ },
75
+ {
76
+ property: "borderLeftStyle",
77
+ value: { type: "keyword", value: "solid" }
78
+ },
79
+ {
80
+ property: "borderLeftColor",
81
+ value: { type: "rgb", r: 226, g: 226, b: 226, alpha: 1 }
82
+ }
83
+ ];
84
+ const h1 = [
85
+ ...verticalMargins,
86
+ {
87
+ property: "fontSize",
88
+ value: { type: "unit", value: 38, unit: "px" }
89
+ }
90
+ ];
91
+ const h2 = [
92
+ ...verticalMargins,
93
+ {
94
+ property: "fontSize",
95
+ value: { type: "unit", value: 32, unit: "px" }
96
+ }
97
+ ];
98
+ const h3 = [
99
+ ...verticalMargins,
100
+ {
101
+ property: "fontSize",
102
+ value: { type: "unit", value: 24, unit: "px" }
103
+ }
104
+ ];
105
+ const h4 = [
106
+ ...verticalMargins,
107
+ {
108
+ property: "fontSize",
109
+ value: { type: "unit", value: 18, unit: "px" }
110
+ }
111
+ ];
112
+ const h5 = [
113
+ ...verticalMargins,
114
+ {
115
+ property: "fontSize",
116
+ value: { type: "unit", value: 14, unit: "px" }
117
+ }
118
+ ];
119
+ const h6 = [
120
+ ...verticalMargins,
121
+ {
122
+ property: "fontSize",
123
+ value: { type: "unit", value: 12, unit: "px" }
124
+ }
125
+ ];
25
126
  export {
127
+ blockquote,
26
128
  borders,
27
- outline
129
+ h1,
130
+ h2,
131
+ h3,
132
+ h4,
133
+ h5,
134
+ h6,
135
+ margins,
136
+ outline,
137
+ verticalMargins
28
138
  };
package/lib/expression.js CHANGED
@@ -97,12 +97,29 @@ const validateExpression = (code, transformIdentifier = (id) => id) => {
97
97
  const expression = jsep(code);
98
98
  return generateCode(expression, true, transformIdentifier);
99
99
  };
100
- const executeExpressions = (variables, expressions) => {
100
+ const sortTopologically = (list, depsById, explored = /* @__PURE__ */ new Set(), sorted = []) => {
101
+ for (const id of list) {
102
+ if (explored.has(id)) {
103
+ continue;
104
+ }
105
+ explored.add(id);
106
+ const deps = depsById.get(id);
107
+ if (deps) {
108
+ sortTopologically(deps, depsById, explored, sorted);
109
+ }
110
+ sorted.push(id);
111
+ }
112
+ return sorted;
113
+ };
114
+ const generateExpressionsComputation = (variables, expressions) => {
101
115
  const depsById = /* @__PURE__ */ new Map();
102
116
  for (const [id, code] of expressions) {
103
117
  const deps = /* @__PURE__ */ new Set();
104
118
  validateExpression(code, (identifier) => {
105
- if (variables.has(identifier) || expressions.has(identifier)) {
119
+ if (variables.has(identifier)) {
120
+ return identifier;
121
+ }
122
+ if (expressions.has(identifier)) {
106
123
  deps.add(identifier);
107
124
  return identifier;
108
125
  }
@@ -110,35 +127,39 @@ const executeExpressions = (variables, expressions) => {
110
127
  });
111
128
  depsById.set(id, deps);
112
129
  }
113
- const sortedExpressions = Array.from(expressions.keys()).sort(
114
- (left, right) => {
115
- if (depsById.get(left)?.has(right)) {
116
- return 1;
117
- }
118
- if (depsById.get(right)?.has(left)) {
119
- return -1;
120
- }
121
- return 0;
122
- }
130
+ const sortedExpressions = sortTopologically(
131
+ new Set(expressions.keys()),
132
+ depsById
123
133
  );
124
- let header = "";
125
- for (const [id, value] of variables) {
126
- header += `const ${id} = ${JSON.stringify(value)};
134
+ let generatedCode = "";
135
+ for (const id of variables) {
136
+ generatedCode += `const ${id} = _variables.get('${id}');
127
137
  `;
128
138
  }
129
- const values = /* @__PURE__ */ new Map();
130
139
  for (const id of sortedExpressions) {
131
140
  const code = expressions.get(id);
132
141
  if (code === void 0) {
133
142
  continue;
134
143
  }
135
- const executeFn = new Function(`${header}
136
- return (${code});`);
137
- const value = executeFn();
138
- header += `const ${id} = ${JSON.stringify(value)};
144
+ generatedCode += `const ${id} = (${code});
139
145
  `;
140
- values.set(id, value);
141
146
  }
147
+ generatedCode += `return new Map([
148
+ `;
149
+ for (const id of sortedExpressions) {
150
+ generatedCode += ` ['${id}', ${id}],
151
+ `;
152
+ }
153
+ generatedCode += `]);`;
154
+ return generatedCode;
155
+ };
156
+ const executeExpressions = (variables, expressions) => {
157
+ const generatedCode = generateExpressionsComputation(
158
+ new Set(variables.keys()),
159
+ expressions
160
+ );
161
+ const executeFn = new Function("_variables", generatedCode);
162
+ const values = executeFn(variables);
142
163
  return values;
143
164
  };
144
165
  const dataSourceVariablePrefix = "$ws$dataSource$";
@@ -157,5 +178,6 @@ export {
157
178
  decodeDataSourceVariable,
158
179
  encodeDataSourceVariable,
159
180
  executeExpressions,
181
+ generateExpressionsComputation,
160
182
  validateExpression
161
183
  };
package/lib/index.js CHANGED
@@ -18,6 +18,7 @@ import {
18
18
  import { ReactSdkContext } from "./context";
19
19
  import {
20
20
  validateExpression,
21
+ generateExpressionsComputation,
21
22
  executeExpressions,
22
23
  encodeDataSourceVariable,
23
24
  decodeDataSourceVariable
@@ -29,6 +30,7 @@ export {
29
30
  defaultStates,
30
31
  encodeDataSourceVariable,
31
32
  executeExpressions,
33
+ generateExpressionsComputation,
32
34
  getInstanceIdFromComponentProps,
33
35
  stateCategories,
34
36
  useInstanceProps,
package/lib/tree/root.js CHANGED
@@ -6,51 +6,15 @@ import {
6
6
  import { createElementsTree } from "./create-elements-tree";
7
7
  import { WebstudioComponent } from "./webstudio-component";
8
8
  import { getPropsByInstanceId } from "../props";
9
- import {
10
- executeExpressions,
11
- encodeDataSourceVariable,
12
- decodeDataSourceVariable
13
- } from "../expression";
14
- const computeExpressions = (dataSources, dataSourceValues) => {
15
- const outputValues = /* @__PURE__ */ new Map();
16
- const variables = /* @__PURE__ */ new Map();
17
- const expressions = /* @__PURE__ */ new Map();
18
- for (const [dataSourceId, dataSource] of dataSources) {
19
- const name = encodeDataSourceVariable(dataSourceId);
20
- if (dataSource.type === "variable") {
21
- const value = dataSourceValues.get(dataSourceId) ?? dataSource.value.value;
22
- variables.set(name, value);
23
- outputValues.set(dataSourceId, value);
24
- }
25
- if (dataSource.type === "expression") {
26
- expressions.set(name, dataSource.code);
27
- }
28
- }
29
- try {
30
- const outputVariables = executeExpressions(variables, expressions);
31
- for (const [name, value] of outputVariables) {
32
- const id = decodeDataSourceVariable(name);
33
- if (id !== void 0) {
34
- outputValues.set(id, value);
35
- }
36
- }
37
- } catch (error) {
38
- console.error(error);
39
- }
40
- return outputValues;
41
- };
42
9
  const InstanceRoot = ({
43
10
  data,
11
+ computeExpressions,
44
12
  Component,
45
13
  components
46
14
  }) => {
47
15
  const dataSourceVariablesStoreRef = useRef(void 0);
48
16
  if (dataSourceVariablesStoreRef.current === void 0) {
49
- const dataSourceVariables = /* @__PURE__ */ new Map();
50
- for (const [dataSourceId, dataSource] of data.build.dataSources) {
51
- dataSourceVariables.set(dataSourceId, dataSource);
52
- }
53
- dataSourceVariablesStoreRef.current = atom(dataSourceVariables);
17
+ dataSourceVariablesStoreRef.current = atom(/* @__PURE__ */ new Map());
54
18
  }
55
19
  const dataSourceVariablesStore = dataSourceVariablesStoreRef.current;
56
20
  const dataSourceValuesStoreRef = useRef(void 0);
@@ -58,7 +22,22 @@ const InstanceRoot = ({
58
22
  dataSourceValuesStoreRef.current = computed(
59
23
  dataSourceVariablesStore,
60
24
  (dataSourceVariables) => {
61
- return computeExpressions(data.build.dataSources, dataSourceVariables);
25
+ const dataSourceValues = /* @__PURE__ */ new Map();
26
+ for (const [dataSourceId, dataSource] of data.build.dataSources) {
27
+ if (dataSource.type === "variable") {
28
+ const value = dataSourceVariables.get(dataSourceId) ?? dataSource.value.value;
29
+ dataSourceValues.set(dataSourceId, value);
30
+ }
31
+ }
32
+ try {
33
+ const result = computeExpressions(dataSourceValues);
34
+ for (const [id, value] of result) {
35
+ dataSourceValues.set(id, value);
36
+ }
37
+ } catch (error) {
38
+ console.error(error);
39
+ }
40
+ return dataSourceValues;
62
41
  }
63
42
  );
64
43
  }