@webstudio-is/react-sdk 0.95.0 → 0.97.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.
Files changed (44) hide show
  1. package/lib/css/normalize.js +127 -53
  2. package/lib/index.js +1850 -32
  3. package/lib/types/component-renderer.d.ts +3 -1
  4. package/lib/types/context.d.ts +2 -0
  5. package/lib/types/index.d.ts +0 -1
  6. package/lib/types/tree/create-elements-tree.d.ts +3 -1
  7. package/lib/types/tree/root.d.ts +3 -1
  8. package/package.json +7 -7
  9. package/lib/app/index.js +0 -2
  10. package/lib/app/root.js +0 -18
  11. package/lib/component-renderer.js +0 -130
  12. package/lib/components/component-meta.js +0 -62
  13. package/lib/components/components-utils.js +0 -2
  14. package/lib/context.js +0 -21
  15. package/lib/css/css.js +0 -59
  16. package/lib/css/global-rules.js +0 -15
  17. package/lib/css/index.js +0 -4
  18. package/lib/css/normalize-type-check.js +0 -4
  19. package/lib/css/presets.js +0 -25
  20. package/lib/css/style-rules.js +0 -63
  21. package/lib/css/style-rules.test.js +0 -149
  22. package/lib/embed-template.js +0 -341
  23. package/lib/embed-template.test.js +0 -648
  24. package/lib/expression.js +0 -330
  25. package/lib/expression.test.js +0 -281
  26. package/lib/generator.js +0 -112
  27. package/lib/generator.test.js +0 -166
  28. package/lib/hook.js +0 -12
  29. package/lib/hook.test.js +0 -15
  30. package/lib/instance-utils.js +0 -43
  31. package/lib/instance-utils.test.js +0 -65
  32. package/lib/prop-meta.js +0 -150
  33. package/lib/props.js +0 -176
  34. package/lib/props.test.js +0 -159
  35. package/lib/pubsub/create.js +0 -56
  36. package/lib/pubsub/index.js +0 -2
  37. package/lib/pubsub/raf-queue.js +0 -20
  38. package/lib/tree/create-elements-tree.js +0 -134
  39. package/lib/tree/index.js +0 -4
  40. package/lib/tree/root.js +0 -85
  41. package/lib/tree/webstudio-component.js +0 -61
  42. package/lib/types/pubsub/create.d.ts +0 -28
  43. package/lib/types/pubsub/index.d.ts +0 -1
  44. package/lib/types/pubsub/raf-queue.d.ts +0 -1
@@ -1,149 +0,0 @@
1
- "use strict";
2
- import { test, expect } from "@jest/globals";
3
- import {
4
- getStyleDeclKey
5
- } from "@webstudio-is/sdk";
6
- import { getStyleRules } from "./style-rules";
7
- const createStyleDeclPair = (styleDecl) => {
8
- return [getStyleDeclKey(styleDecl), styleDecl];
9
- };
10
- test("compute styles from different style sources", () => {
11
- const styles = new Map([
12
- createStyleDeclPair({
13
- breakpointId: "a",
14
- styleSourceId: "styleSource1",
15
- property: "width",
16
- value: { type: "unit", value: 10, unit: "px" }
17
- }),
18
- createStyleDeclPair({
19
- breakpointId: "a",
20
- styleSourceId: "styleSource2",
21
- property: "display",
22
- value: { type: "keyword", value: "block" }
23
- }),
24
- createStyleDeclPair({
25
- breakpointId: "a",
26
- styleSourceId: "styleSource4",
27
- property: "color",
28
- value: { type: "keyword", value: "green" }
29
- }),
30
- createStyleDeclPair({
31
- breakpointId: "a",
32
- styleSourceId: "styleSource4",
33
- property: "width",
34
- value: { type: "keyword", value: "min-content" }
35
- }),
36
- createStyleDeclPair({
37
- breakpointId: "a",
38
- styleSourceId: "styleSource3",
39
- property: "color",
40
- value: { type: "keyword", value: "red" }
41
- }),
42
- createStyleDeclPair({
43
- breakpointId: "b",
44
- styleSourceId: "styleSource5",
45
- property: "color",
46
- value: { type: "keyword", value: "orange" }
47
- }),
48
- createStyleDeclPair({
49
- breakpointId: "a",
50
- styleSourceId: "styleSource6",
51
- property: "color",
52
- value: { type: "keyword", value: "blue" }
53
- }),
54
- createStyleDeclPair({
55
- breakpointId: "a",
56
- styleSourceId: "styleSource6",
57
- state: ":hover",
58
- property: "color",
59
- value: { type: "keyword", value: "blue" }
60
- })
61
- ]);
62
- const styleSourceSelections = /* @__PURE__ */ new Map([
63
- [
64
- "instance1",
65
- {
66
- instanceId: "instance1",
67
- values: ["styleSource1"]
68
- }
69
- ],
70
- [
71
- "instance2",
72
- {
73
- instanceId: "instance2",
74
- values: ["styleSource4", "styleSource5", "styleSource3"]
75
- }
76
- ],
77
- [
78
- "instance3",
79
- {
80
- instanceId: "instance3",
81
- values: ["styleSource6"]
82
- }
83
- ]
84
- ]);
85
- expect(getStyleRules(styles, styleSourceSelections)).toMatchInlineSnapshot(`
86
- [
87
- {
88
- "breakpointId": "a",
89
- "instanceId": "instance1",
90
- "state": undefined,
91
- "style": {
92
- "width": {
93
- "type": "unit",
94
- "unit": "px",
95
- "value": 10,
96
- },
97
- },
98
- },
99
- {
100
- "breakpointId": "a",
101
- "instanceId": "instance2",
102
- "state": undefined,
103
- "style": {
104
- "color": {
105
- "type": "keyword",
106
- "value": "red",
107
- },
108
- "width": {
109
- "type": "keyword",
110
- "value": "min-content",
111
- },
112
- },
113
- },
114
- {
115
- "breakpointId": "b",
116
- "instanceId": "instance2",
117
- "state": undefined,
118
- "style": {
119
- "color": {
120
- "type": "keyword",
121
- "value": "orange",
122
- },
123
- },
124
- },
125
- {
126
- "breakpointId": "a",
127
- "instanceId": "instance3",
128
- "state": undefined,
129
- "style": {
130
- "color": {
131
- "type": "keyword",
132
- "value": "blue",
133
- },
134
- },
135
- },
136
- {
137
- "breakpointId": "a",
138
- "instanceId": "instance3",
139
- "state": ":hover",
140
- "style": {
141
- "color": {
142
- "type": "keyword",
143
- "value": "blue",
144
- },
145
- },
146
- },
147
- ]
148
- `);
149
- });
@@ -1,341 +0,0 @@
1
- "use strict";
2
- import { z } from "zod";
3
- import { nanoid } from "nanoid";
4
- import { titleCase } from "title-case";
5
- import { noCase } from "no-case";
6
- import { StyleValue } from "@webstudio-is/css-engine";
7
- import { encodeDataSourceVariable, validateExpression } from "./expression";
8
- const EmbedTemplateText = z.object({
9
- type: z.literal("text"),
10
- value: z.string()
11
- });
12
- const EmbedTemplateDataSource = z.union([
13
- z.object({
14
- type: z.literal("variable"),
15
- initialValue: z.union([
16
- z.string(),
17
- z.number(),
18
- z.boolean(),
19
- z.array(z.string())
20
- ])
21
- }),
22
- z.object({
23
- type: z.literal("expression"),
24
- code: z.string()
25
- })
26
- ]);
27
- const EmbedTemplateProp = z.union([
28
- z.object({
29
- type: z.literal("dataSource"),
30
- name: z.string(),
31
- dataSourceName: z.string()
32
- }),
33
- z.object({
34
- type: z.literal("number"),
35
- name: z.string(),
36
- value: z.number()
37
- }),
38
- z.object({
39
- type: z.literal("string"),
40
- name: z.string(),
41
- value: z.string()
42
- }),
43
- z.object({
44
- type: z.literal("boolean"),
45
- name: z.string(),
46
- value: z.boolean()
47
- }),
48
- z.object({
49
- type: z.literal("string[]"),
50
- name: z.string(),
51
- value: z.array(z.string())
52
- }),
53
- z.object({
54
- type: z.literal("action"),
55
- name: z.string(),
56
- value: z.array(
57
- z.object({
58
- type: z.literal("execute"),
59
- args: z.optional(z.array(z.string())),
60
- code: z.string()
61
- })
62
- )
63
- })
64
- ]);
65
- const EmbedTemplateStyleDeclRaw = z.object({
66
- // State selector, e.g. :hover
67
- state: z.optional(z.string()),
68
- property: z.string(),
69
- value: StyleValue
70
- });
71
- export const EmbedTemplateStyleDecl = EmbedTemplateStyleDeclRaw;
72
- export const EmbedTemplateInstance = z.lazy(
73
- () => z.object({
74
- type: z.literal("instance"),
75
- component: z.string(),
76
- label: z.optional(z.string()),
77
- dataSources: z.optional(z.record(z.string(), EmbedTemplateDataSource)),
78
- props: z.optional(z.array(EmbedTemplateProp)),
79
- tokens: z.optional(z.array(z.string())),
80
- styles: z.optional(z.array(EmbedTemplateStyleDecl)),
81
- children: WsEmbedTemplate
82
- })
83
- );
84
- export const WsEmbedTemplate = z.lazy(
85
- () => z.array(z.union([EmbedTemplateInstance, EmbedTemplateText]))
86
- );
87
- const getDataSourceValue = (value) => {
88
- if (typeof value === "string") {
89
- return { type: "string", value };
90
- }
91
- if (typeof value === "number") {
92
- return { type: "number", value };
93
- }
94
- if (typeof value === "boolean") {
95
- return { type: "boolean", value };
96
- }
97
- if (Array.isArray(value)) {
98
- return { type: "string[]", value };
99
- }
100
- value;
101
- throw Error("Impossible case");
102
- };
103
- const createInstancesFromTemplate = (treeTemplate, instances, props, dataSourceByRef, styleSourceSelections, styleSources, styles, metas, defaultBreakpointId) => {
104
- const parentChildren = [];
105
- for (const item of treeTemplate) {
106
- if (item.type === "instance") {
107
- const instanceId = nanoid();
108
- if (item.dataSources) {
109
- for (const [name, dataSource] of Object.entries(item.dataSources)) {
110
- if (dataSourceByRef.has(name)) {
111
- throw Error(`${name} data source already defined`);
112
- }
113
- if (dataSource.type === "variable") {
114
- dataSourceByRef.set(name, {
115
- type: "variable",
116
- id: nanoid(),
117
- scopeInstanceId: instanceId,
118
- name,
119
- value: getDataSourceValue(dataSource.initialValue)
120
- });
121
- }
122
- if (dataSource.type === "expression") {
123
- dataSourceByRef.set(name, {
124
- type: "expression",
125
- id: nanoid(),
126
- scopeInstanceId: instanceId,
127
- name,
128
- // replace all references with variable names
129
- code: validateExpression(dataSource.code, {
130
- transformIdentifier: (ref) => {
131
- const id = dataSourceByRef.get(ref)?.id ?? ref;
132
- return encodeDataSourceVariable(id);
133
- }
134
- })
135
- });
136
- }
137
- }
138
- }
139
- if (item.props) {
140
- for (const prop of item.props) {
141
- const propId = nanoid();
142
- if (prop.type === "action") {
143
- props.push({
144
- id: propId,
145
- instanceId,
146
- type: "action",
147
- name: prop.name,
148
- value: prop.value.map((value) => {
149
- const args = value.args ?? [];
150
- return {
151
- type: "execute",
152
- args,
153
- // replace all references with variable names
154
- code: validateExpression(value.code, {
155
- effectful: true,
156
- transformIdentifier: (ref) => {
157
- if (args.includes(ref)) {
158
- return ref;
159
- }
160
- const id = dataSourceByRef.get(ref)?.id ?? ref;
161
- return encodeDataSourceVariable(id);
162
- }
163
- })
164
- };
165
- })
166
- });
167
- continue;
168
- }
169
- if (prop.type === "dataSource") {
170
- const dataSource = dataSourceByRef.get(prop.dataSourceName);
171
- if (dataSource === void 0) {
172
- throw Error(`${prop.dataSourceName} data source is not defined`);
173
- }
174
- props.push({
175
- id: propId,
176
- instanceId,
177
- type: "dataSource",
178
- name: prop.name,
179
- value: dataSource.id
180
- });
181
- continue;
182
- }
183
- props.push({ id: propId, instanceId, ...prop });
184
- }
185
- }
186
- const styleSourceIds = [];
187
- if (item.tokens) {
188
- const meta = metas.get(item.component);
189
- if (meta?.presetTokens) {
190
- for (const name of item.tokens) {
191
- const tokenValue = meta.presetTokens[name];
192
- if (tokenValue) {
193
- const styleSourceId = `${item.component}:${name}`;
194
- styleSourceIds.push(styleSourceId);
195
- styleSources.push({
196
- type: "token",
197
- id: styleSourceId,
198
- name: titleCase(noCase(name))
199
- });
200
- for (const styleDecl of tokenValue.styles) {
201
- styles.push({
202
- breakpointId: defaultBreakpointId,
203
- styleSourceId,
204
- state: styleDecl.state,
205
- property: styleDecl.property,
206
- value: styleDecl.value
207
- });
208
- }
209
- }
210
- }
211
- }
212
- }
213
- if (item.styles) {
214
- const styleSourceId = nanoid();
215
- styleSources.push({
216
- type: "local",
217
- id: styleSourceId
218
- });
219
- styleSourceIds.push(styleSourceId);
220
- for (const styleDecl of item.styles) {
221
- styles.push({
222
- breakpointId: defaultBreakpointId,
223
- styleSourceId,
224
- state: styleDecl.state,
225
- property: styleDecl.property,
226
- value: styleDecl.value
227
- });
228
- }
229
- }
230
- if (styleSourceIds.length > 0) {
231
- styleSourceSelections.push({
232
- instanceId,
233
- values: styleSourceIds
234
- });
235
- }
236
- const instance = {
237
- type: "instance",
238
- id: instanceId,
239
- label: item.label,
240
- component: item.component,
241
- children: []
242
- };
243
- instances.push(instance);
244
- instance.children = createInstancesFromTemplate(
245
- item.children,
246
- instances,
247
- props,
248
- dataSourceByRef,
249
- styleSourceSelections,
250
- styleSources,
251
- styles,
252
- metas,
253
- defaultBreakpointId
254
- );
255
- parentChildren.push({
256
- type: "id",
257
- value: instanceId
258
- });
259
- }
260
- if (item.type === "text") {
261
- parentChildren.push({
262
- type: "text",
263
- value: item.value
264
- });
265
- }
266
- }
267
- return parentChildren;
268
- };
269
- export const generateDataFromEmbedTemplate = (treeTemplate, metas, defaultBreakpointId) => {
270
- const instances = [];
271
- const props = [];
272
- const dataSourceByRef = /* @__PURE__ */ new Map();
273
- const styleSourceSelections = [];
274
- const styleSources = [];
275
- const styles = [];
276
- const children = createInstancesFromTemplate(
277
- treeTemplate,
278
- instances,
279
- props,
280
- dataSourceByRef,
281
- styleSourceSelections,
282
- styleSources,
283
- styles,
284
- metas,
285
- defaultBreakpointId
286
- );
287
- return {
288
- children,
289
- instances,
290
- props,
291
- dataSources: Array.from(dataSourceByRef.values()),
292
- styleSourceSelections,
293
- styleSources,
294
- styles
295
- };
296
- };
297
- const namespaceEmbedTemplateComponents = (template, namespace, components) => {
298
- return template.map((item) => {
299
- if (item.type === "text") {
300
- return item;
301
- }
302
- if (item.type === "instance") {
303
- const prefix = components.has(item.component) ? `${namespace}:` : "";
304
- return {
305
- ...item,
306
- component: `${prefix}${item.component}`,
307
- children: namespaceEmbedTemplateComponents(
308
- item.children,
309
- namespace,
310
- components
311
- )
312
- };
313
- }
314
- item;
315
- throw Error("Impossible case");
316
- });
317
- };
318
- export const namespaceMeta = (meta, namespace, components) => {
319
- const newMeta = { ...meta };
320
- if (newMeta.requiredAncestors) {
321
- newMeta.requiredAncestors = newMeta.requiredAncestors.map(
322
- (component) => components.has(component) ? `${namespace}:${component}` : component
323
- );
324
- }
325
- if (newMeta.invalidAncestors) {
326
- newMeta.invalidAncestors = newMeta.invalidAncestors.map(
327
- (component) => components.has(component) ? `${namespace}:${component}` : component
328
- );
329
- }
330
- if (newMeta.indexWithinAncestor) {
331
- newMeta.indexWithinAncestor = components.has(newMeta.indexWithinAncestor) ? `${namespace}:${newMeta.indexWithinAncestor}` : newMeta.indexWithinAncestor;
332
- }
333
- if (newMeta.template) {
334
- newMeta.template = namespaceEmbedTemplateComponents(
335
- newMeta.template,
336
- namespace,
337
- components
338
- );
339
- }
340
- return newMeta;
341
- };