@webstudio-is/react-sdk 0.76.0 → 0.77.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.
@@ -1,3 +1,285 @@
1
1
  import type { Styles } from "./normalize";
2
2
  export declare const borders: Styles;
3
3
  export declare const outline: Styles;
4
+ export declare const margins: ({
5
+ property: "marginTop";
6
+ value: {
7
+ type: "unit";
8
+ value: number;
9
+ unit: "px";
10
+ };
11
+ } | {
12
+ property: "marginRight";
13
+ value: {
14
+ type: "unit";
15
+ value: number;
16
+ unit: "px";
17
+ };
18
+ } | {
19
+ property: "marginBottom";
20
+ value: {
21
+ type: "unit";
22
+ value: number;
23
+ unit: "px";
24
+ };
25
+ } | {
26
+ property: "marginLeft";
27
+ value: {
28
+ type: "unit";
29
+ value: number;
30
+ unit: "px";
31
+ };
32
+ })[];
33
+ export declare const verticalMargins: ({
34
+ property: "marginTop";
35
+ value: {
36
+ type: "unit";
37
+ value: number;
38
+ unit: "px";
39
+ };
40
+ } | {
41
+ property: "marginBottom";
42
+ value: {
43
+ type: "unit";
44
+ value: number;
45
+ unit: "px";
46
+ };
47
+ })[];
48
+ export declare const blockquote: ({
49
+ property: "marginTop";
50
+ value: {
51
+ type: "unit";
52
+ value: number;
53
+ unit: "px";
54
+ };
55
+ } | {
56
+ property: "marginRight";
57
+ value: {
58
+ type: "unit";
59
+ value: number;
60
+ unit: "px";
61
+ };
62
+ } | {
63
+ property: "marginBottom";
64
+ value: {
65
+ type: "unit";
66
+ value: number;
67
+ unit: "px";
68
+ };
69
+ } | {
70
+ property: "marginLeft";
71
+ value: {
72
+ type: "unit";
73
+ value: number;
74
+ unit: "px";
75
+ };
76
+ } | {
77
+ property: "paddingTop";
78
+ value: {
79
+ type: "unit";
80
+ value: number;
81
+ unit: "px";
82
+ r?: undefined;
83
+ g?: undefined;
84
+ b?: undefined;
85
+ alpha?: undefined;
86
+ };
87
+ } | {
88
+ property: "paddingBottom";
89
+ value: {
90
+ type: "unit";
91
+ value: number;
92
+ unit: "px";
93
+ r?: undefined;
94
+ g?: undefined;
95
+ b?: undefined;
96
+ alpha?: undefined;
97
+ };
98
+ } | {
99
+ property: "paddingLeft";
100
+ value: {
101
+ type: "unit";
102
+ value: number;
103
+ unit: "px";
104
+ r?: undefined;
105
+ g?: undefined;
106
+ b?: undefined;
107
+ alpha?: undefined;
108
+ };
109
+ } | {
110
+ property: "paddingRight";
111
+ value: {
112
+ type: "unit";
113
+ value: number;
114
+ unit: "px";
115
+ r?: undefined;
116
+ g?: undefined;
117
+ b?: undefined;
118
+ alpha?: undefined;
119
+ };
120
+ } | {
121
+ property: "borderLeftWidth";
122
+ value: {
123
+ type: "unit";
124
+ value: number;
125
+ unit: "px";
126
+ r?: undefined;
127
+ g?: undefined;
128
+ b?: undefined;
129
+ alpha?: undefined;
130
+ };
131
+ } | {
132
+ property: "borderLeftStyle";
133
+ value: {
134
+ type: "keyword";
135
+ value: string;
136
+ unit?: undefined;
137
+ r?: undefined;
138
+ g?: undefined;
139
+ b?: undefined;
140
+ alpha?: undefined;
141
+ };
142
+ } | {
143
+ property: "borderLeftColor";
144
+ value: {
145
+ type: "rgb";
146
+ r: number;
147
+ g: number;
148
+ b: number;
149
+ alpha: number;
150
+ value?: undefined;
151
+ unit?: undefined;
152
+ };
153
+ })[];
154
+ export declare const h1: ({
155
+ property: "marginTop";
156
+ value: {
157
+ type: "unit";
158
+ value: number;
159
+ unit: "px";
160
+ };
161
+ } | {
162
+ property: "marginBottom";
163
+ value: {
164
+ type: "unit";
165
+ value: number;
166
+ unit: "px";
167
+ };
168
+ } | {
169
+ property: "fontSize";
170
+ value: {
171
+ type: "unit";
172
+ value: number;
173
+ unit: "px";
174
+ };
175
+ })[];
176
+ export declare const h2: ({
177
+ property: "marginTop";
178
+ value: {
179
+ type: "unit";
180
+ value: number;
181
+ unit: "px";
182
+ };
183
+ } | {
184
+ property: "marginBottom";
185
+ value: {
186
+ type: "unit";
187
+ value: number;
188
+ unit: "px";
189
+ };
190
+ } | {
191
+ property: "fontSize";
192
+ value: {
193
+ type: "unit";
194
+ value: number;
195
+ unit: "px";
196
+ };
197
+ })[];
198
+ export declare const h3: ({
199
+ property: "marginTop";
200
+ value: {
201
+ type: "unit";
202
+ value: number;
203
+ unit: "px";
204
+ };
205
+ } | {
206
+ property: "marginBottom";
207
+ value: {
208
+ type: "unit";
209
+ value: number;
210
+ unit: "px";
211
+ };
212
+ } | {
213
+ property: "fontSize";
214
+ value: {
215
+ type: "unit";
216
+ value: number;
217
+ unit: "px";
218
+ };
219
+ })[];
220
+ export declare const h4: ({
221
+ property: "marginTop";
222
+ value: {
223
+ type: "unit";
224
+ value: number;
225
+ unit: "px";
226
+ };
227
+ } | {
228
+ property: "marginBottom";
229
+ value: {
230
+ type: "unit";
231
+ value: number;
232
+ unit: "px";
233
+ };
234
+ } | {
235
+ property: "fontSize";
236
+ value: {
237
+ type: "unit";
238
+ value: number;
239
+ unit: "px";
240
+ };
241
+ })[];
242
+ export declare const h5: ({
243
+ property: "marginTop";
244
+ value: {
245
+ type: "unit";
246
+ value: number;
247
+ unit: "px";
248
+ };
249
+ } | {
250
+ property: "marginBottom";
251
+ value: {
252
+ type: "unit";
253
+ value: number;
254
+ unit: "px";
255
+ };
256
+ } | {
257
+ property: "fontSize";
258
+ value: {
259
+ type: "unit";
260
+ value: number;
261
+ unit: "px";
262
+ };
263
+ })[];
264
+ export declare const h6: ({
265
+ property: "marginTop";
266
+ value: {
267
+ type: "unit";
268
+ value: number;
269
+ unit: "px";
270
+ };
271
+ } | {
272
+ property: "marginBottom";
273
+ value: {
274
+ type: "unit";
275
+ value: number;
276
+ unit: "px";
277
+ };
278
+ } | {
279
+ property: "fontSize";
280
+ value: {
281
+ type: "unit";
282
+ value: number;
283
+ unit: "px";
284
+ };
285
+ })[];
@@ -1,5 +1,10 @@
1
1
  type TransformIdentifier = (id: string) => string;
2
2
  export declare const validateExpression: (code: string, transformIdentifier?: TransformIdentifier) => string;
3
+ /**
4
+ * Generates a function body expecting map as _variables argument
5
+ * and outputing map of results
6
+ */
7
+ export declare const generateExpressionsComputation: (variables: Set<string>, expressions: Map<string, string>) => string;
3
8
  export declare const executeExpressions: (variables: Map<string, unknown>, expressions: Map<string, string>) => Map<string, unknown>;
4
9
  export declare const encodeDataSourceVariable: (id: string) => string;
5
10
  export declare const decodeDataSourceVariable: (name: string) => string | undefined;
@@ -7,4 +7,4 @@ export { type WsComponentPropsMeta, type WsComponentMeta, type ComponentState, t
7
7
  export * from "./embed-template";
8
8
  export { useInstanceProps, usePropUrl, usePropAsset, getInstanceIdFromComponentProps, } from "./props";
9
9
  export { type Params, ReactSdkContext } from "./context";
10
- export { validateExpression, executeExpressions, encodeDataSourceVariable, decodeDataSourceVariable, } from "./expression";
10
+ export { validateExpression, generateExpressionsComputation, executeExpressions, encodeDataSourceVariable, decodeDataSourceVariable, } from "./expression";
@@ -1,5 +1,5 @@
1
1
  import { type ComponentProps } from "react";
2
- import { type Build, type Page } from "@webstudio-is/project-build";
2
+ import { type Build, type Page, DataSource } from "@webstudio-is/project-build";
3
3
  import type { Asset } from "@webstudio-is/asset-uploader";
4
4
  import { WebstudioComponent } from "./webstudio-component";
5
5
  import type { Components } from "../components/components-utils";
@@ -14,10 +14,12 @@ export type Data = {
14
14
  export type RootPropsData = Omit<Data, "build"> & {
15
15
  build: Pick<Data["build"], "instances" | "props" | "dataSources">;
16
16
  };
17
+ type DataSourceValues = Map<DataSource["id"], unknown>;
17
18
  type RootProps = {
18
19
  data: RootPropsData;
20
+ computeExpressions: (values: DataSourceValues) => DataSourceValues;
19
21
  Component?: (props: ComponentProps<typeof WebstudioComponent>) => JSX.Element;
20
22
  components: Components;
21
23
  };
22
- export declare const InstanceRoot: ({ data, Component, components, }: RootProps) => JSX.Element | null;
24
+ export declare const InstanceRoot: ({ data, computeExpressions, Component, components, }: RootProps) => JSX.Element | null;
23
25
  export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@webstudio-is/react-sdk",
3
- "version": "0.76.0",
3
+ "version": "0.77.0",
4
4
  "description": "Webstudio JavaScript / TypeScript API",
5
5
  "author": "Webstudio <github@webstudio.is>",
6
6
  "homepage": "https://webstudio.is",
@@ -34,12 +34,12 @@
34
34
  "nanoevents": "^7.0.1",
35
35
  "nanoid": "^3.3.6",
36
36
  "nanostores": "^0.7.1",
37
- "@webstudio-is/asset-uploader": "^0.76.0",
38
- "@webstudio-is/css-data": "^0.76.0",
39
- "@webstudio-is/css-engine": "^0.76.0",
40
- "@webstudio-is/fonts": "^0.76.0",
41
- "@webstudio-is/generate-arg-types": "^0.76.0",
42
- "@webstudio-is/project-build": "^0.76.0"
37
+ "@webstudio-is/asset-uploader": "^0.77.0",
38
+ "@webstudio-is/css-data": "^0.77.0",
39
+ "@webstudio-is/css-engine": "^0.77.0",
40
+ "@webstudio-is/fonts": "^0.77.0",
41
+ "@webstudio-is/generate-arg-types": "^0.77.0",
42
+ "@webstudio-is/project-build": "^0.77.0"
43
43
  },
44
44
  "exports": {
45
45
  ".": {
@@ -16,7 +16,7 @@
16
16
  */
17
17
 
18
18
  // webstudio custom opinionated presets
19
- import { borders, outline } from "./presets";
19
+ import * as presets from "./presets";
20
20
  import type { EmbedTemplateStyleDecl } from "../embed-template";
21
21
 
22
22
  export type Styles = EmbedTemplateStyleDecl[];
@@ -37,13 +37,21 @@ const boxSizing = {
37
37
  * box-sizing: border-box;
38
38
  }
39
39
  */
40
- const baseStyle = [boxSizing, ...borders, ...outline] satisfies Styles;
40
+ const baseStyle = [
41
+ boxSizing,
42
+ ...presets.borders,
43
+ ...presets.outline,
44
+ ] satisfies Styles;
41
45
 
42
46
  export const div = baseStyle;
43
47
  export const address = baseStyle;
44
48
  export const article = baseStyle;
45
49
  export const aside = baseStyle;
46
- export const figure = baseStyle;
50
+ export const blockquote = [
51
+ ...baseStyle,
52
+ ...presets.blockquote,
53
+ ] satisfies Styles;
54
+ export const figure = [...baseStyle, ...presets.margins] satisfies Styles;
47
55
  export const footer = baseStyle;
48
56
  export const header = baseStyle;
49
57
  export const main = baseStyle;
@@ -52,12 +60,12 @@ export const section = baseStyle;
52
60
  export const form = baseStyle;
53
61
  export const label = baseStyle;
54
62
 
55
- export const h1 = baseStyle;
56
- export const h2 = baseStyle;
57
- export const h3 = baseStyle;
58
- export const h4 = baseStyle;
59
- export const h5 = baseStyle;
60
- export const h6 = baseStyle;
63
+ export const h1 = [...baseStyle, ...presets.h1] satisfies Styles;
64
+ export const h2 = [...baseStyle, ...presets.h2] satisfies Styles;
65
+ export const h3 = [...baseStyle, ...presets.h3] satisfies Styles;
66
+ export const h4 = [...baseStyle, ...presets.h4] satisfies Styles;
67
+ export const h5 = [...baseStyle, ...presets.h5] satisfies Styles;
68
+ export const h6 = [...baseStyle, ...presets.h6] satisfies Styles;
61
69
 
62
70
  export const i = baseStyle;
63
71
 
@@ -68,7 +76,7 @@ export const li = baseStyle;
68
76
  export const ul = baseStyle;
69
77
  export const ol = baseStyle;
70
78
 
71
- export const p = baseStyle;
79
+ export const p = [...baseStyle, ...presets.verticalMargins];
72
80
  export const span = baseStyle;
73
81
 
74
82
  // @todo for now not applied to html, as we don't have html element
@@ -94,7 +102,7 @@ export const html = [
94
102
  value: { type: "unit", value: 4, unit: "number" },
95
103
  },
96
104
  boxSizing,
97
- ...borders,
105
+ ...presets.borders,
98
106
  ] satisfies Styles;
99
107
 
100
108
  /**
@@ -136,7 +144,7 @@ export const body = [
136
144
  value: { type: "unit", unit: "number", value: 1.2 },
137
145
  },
138
146
  boxSizing,
139
- ...borders,
147
+ ...presets.borders,
140
148
  ] satisfies Styles;
141
149
 
142
150
  /**
@@ -155,7 +163,8 @@ export const hr = [
155
163
  value: { type: "keyword", value: "inherit" },
156
164
  },
157
165
  boxSizing,
158
- ...borders,
166
+ ...presets.borders,
167
+ ...presets.margins,
159
168
  ] satisfies Styles;
160
169
 
161
170
  /**
@@ -177,7 +186,7 @@ export const b = [
177
186
  value: { type: "keyword", value: "700" },
178
187
  },
179
188
  boxSizing,
180
- ...borders,
189
+ ...presets.borders,
181
190
  ] satisfies Styles;
182
191
  export const strong = b;
183
192
 
@@ -200,7 +209,7 @@ export const code = [
200
209
  value: { type: "unit", value: 1, unit: "em" },
201
210
  },
202
211
  boxSizing,
203
- ...borders,
212
+ ...presets.borders,
204
213
  ] satisfies Styles;
205
214
 
206
215
  export const kbd = code;
@@ -217,7 +226,7 @@ export const small = [
217
226
  value: { type: "unit", value: 80, unit: "%" },
218
227
  },
219
228
  boxSizing,
220
- ...borders,
229
+ ...presets.borders,
221
230
  ] satisfies Styles;
222
231
 
223
232
  /**
@@ -242,7 +251,7 @@ const subSupBase = [
242
251
  value: { type: "keyword", value: "baseline" },
243
252
  },
244
253
  boxSizing,
245
- ...borders,
254
+ ...presets.borders,
246
255
  ] satisfies Styles;
247
256
 
248
257
  export const sub = [
@@ -277,7 +286,7 @@ export const table = [
277
286
  property: "textIndent",
278
287
  value: { type: "unit", value: 0, unit: "number" },
279
288
  },
280
- ...borders,
289
+ ...presets.borders,
281
290
  /* 2 */
282
291
  {
283
292
  property: "borderTopColor",
@@ -340,7 +349,7 @@ const buttonBase = [
340
349
  value: { type: "unit", value: 0, unit: "number" },
341
350
  },
342
351
  boxSizing,
343
- ...borders,
352
+ ...presets.borders,
344
353
  ] satisfies Styles;
345
354
 
346
355
  export const input = buttonBase;
@@ -427,7 +436,7 @@ export const legend = [
427
436
  value: { type: "unit", value: 0, unit: "number" },
428
437
  },
429
438
  boxSizing,
430
- ...borders,
439
+ ...presets.borders,
431
440
  ] satisfies Styles;
432
441
 
433
442
  /**
@@ -440,7 +449,7 @@ export const progress = [
440
449
  value: { type: "keyword", value: "baseline" },
441
450
  },
442
451
  boxSizing,
443
- ...borders,
452
+ ...presets.borders,
444
453
  ] satisfies Styles;
445
454
 
446
455
  /**
@@ -503,5 +512,5 @@ export const summary = [
503
512
  value: { type: "keyword", value: "list-item" },
504
513
  },
505
514
  boxSizing,
506
- ...borders,
515
+ ...presets.borders,
507
516
  ] satisfies Styles;
@@ -25,3 +25,113 @@ export const outline: Styles = [
25
25
  value: { type: "unit", value: 1, unit: "px" },
26
26
  },
27
27
  ];
28
+
29
+ export const margins = [
30
+ {
31
+ property: "marginTop",
32
+ value: { type: "unit", value: 0, unit: "px" },
33
+ },
34
+ {
35
+ property: "marginRight",
36
+ value: { type: "unit", value: 0, unit: "px" },
37
+ },
38
+ {
39
+ property: "marginBottom",
40
+ value: { type: "unit", value: 0, unit: "px" },
41
+ },
42
+ {
43
+ property: "marginLeft",
44
+ value: { type: "unit", value: 0, unit: "px" },
45
+ },
46
+ ] satisfies Styles;
47
+
48
+ export const verticalMargins = [
49
+ {
50
+ property: "marginTop",
51
+ value: { type: "unit", value: 0, unit: "px" },
52
+ },
53
+ {
54
+ property: "marginBottom",
55
+ value: { type: "unit", value: 0, unit: "px" },
56
+ },
57
+ ] satisfies Styles;
58
+
59
+ export const blockquote = [
60
+ ...margins,
61
+ {
62
+ property: "paddingTop",
63
+ value: { type: "unit", value: 10, unit: "px" },
64
+ },
65
+ {
66
+ property: "paddingBottom",
67
+ value: { type: "unit", value: 10, unit: "px" },
68
+ },
69
+ {
70
+ property: "paddingLeft",
71
+ value: { type: "unit", value: 20, unit: "px" },
72
+ },
73
+ {
74
+ property: "paddingRight",
75
+ value: { type: "unit", value: 20, unit: "px" },
76
+ },
77
+ {
78
+ property: "borderLeftWidth",
79
+ value: { type: "unit", value: 5, unit: "px" },
80
+ },
81
+ {
82
+ property: "borderLeftStyle",
83
+ value: { type: "keyword", value: "solid" },
84
+ },
85
+ {
86
+ property: "borderLeftColor",
87
+ value: { type: "rgb", r: 226, g: 226, b: 226, alpha: 1 },
88
+ },
89
+ ] satisfies Styles;
90
+
91
+ export const h1 = [
92
+ ...verticalMargins,
93
+ {
94
+ property: "fontSize",
95
+ value: { type: "unit", value: 38, unit: "px" },
96
+ },
97
+ ] satisfies Styles;
98
+
99
+ export const h2 = [
100
+ ...verticalMargins,
101
+ {
102
+ property: "fontSize",
103
+ value: { type: "unit", value: 32, unit: "px" },
104
+ },
105
+ ] satisfies Styles;
106
+
107
+ export const h3 = [
108
+ ...verticalMargins,
109
+ {
110
+ property: "fontSize",
111
+ value: { type: "unit", value: 24, unit: "px" },
112
+ },
113
+ ] satisfies Styles;
114
+
115
+ export const h4 = [
116
+ ...verticalMargins,
117
+ {
118
+ property: "fontSize",
119
+ value: { type: "unit", value: 18, unit: "px" },
120
+ },
121
+ ] satisfies Styles;
122
+
123
+ export const h5 = [
124
+ ...verticalMargins,
125
+ {
126
+ property: "fontSize",
127
+ value: { type: "unit", value: 14, unit: "px" },
128
+ },
129
+ ] satisfies Styles;
130
+
131
+ export const h6 = [
132
+ ...verticalMargins,
133
+ {
134
+ property: "fontSize",
135
+ value: { type: "unit", value: 12, unit: "px" },
136
+ },
137
+ ] satisfies Styles;
@@ -3,6 +3,7 @@ import {
3
3
  decodeDataSourceVariable,
4
4
  encodeDataSourceVariable,
5
5
  executeExpressions,
6
+ generateExpressionsComputation,
6
7
  validateExpression,
7
8
  } from "./expression";
8
9
 
@@ -61,6 +62,30 @@ test("transform identifiers", () => {
61
62
  );
62
63
  });
63
64
 
65
+ test("generate expressions computation", () => {
66
+ const variables = new Set(["var0"]);
67
+ const expressions = new Map([
68
+ ["exp3", "exp2 + exp1"],
69
+ ["exp1", "var0"],
70
+ ["exp2", "exp1"],
71
+ ["exp4", "exp2"],
72
+ ]);
73
+ expect(generateExpressionsComputation(variables, expressions))
74
+ .toMatchInlineSnapshot(`
75
+ "const var0 = _variables.get('var0');
76
+ const exp1 = (var0);
77
+ const exp2 = (exp1);
78
+ const exp3 = (exp2 + exp1);
79
+ const exp4 = (exp2);
80
+ return new Map([
81
+ ['exp1', exp1],
82
+ ['exp2', exp2],
83
+ ['exp3', exp3],
84
+ ['exp4', exp4],
85
+ ]);"
86
+ `);
87
+ });
88
+
64
89
  test("execute expression", () => {
65
90
  const variables = new Map();
66
91
  const expressions = new Map([["exp1", "1 + 1"]]);
@@ -77,7 +102,7 @@ test("execute expression dependent on variables", () => {
77
102
  );
78
103
  });
79
104
 
80
- test("execute expression dependent on other expressions", () => {
105
+ test("execute expression dependent on another expressions", () => {
81
106
  const variables = new Map([["var1", 3]]);
82
107
  const expressions = new Map([
83
108
  ["exp1", "exp0 + 1"],
@@ -100,7 +125,7 @@ test("forbid circular expressions", () => {
100
125
  ]);
101
126
  expect(() => {
102
127
  executeExpressions(variables, expressions);
103
- }).toThrowError(/exp2 is not defined/);
128
+ }).toThrowError(/Cannot access 'exp0' before initialization/);
104
129
  });
105
130
 
106
131
  test("make sure dependency exists", () => {