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