@webstudio-is/react-sdk 0.94.0 → 0.96.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 (40) hide show
  1. package/lib/css/normalize.js +127 -53
  2. package/lib/index.js +1843 -32
  3. package/lib/types/index.d.ts +0 -1
  4. package/package.json +10 -9
  5. package/lib/app/index.js +0 -2
  6. package/lib/app/root.js +0 -18
  7. package/lib/component-renderer.js +0 -130
  8. package/lib/components/component-meta.js +0 -62
  9. package/lib/components/components-utils.js +0 -2
  10. package/lib/context.js +0 -21
  11. package/lib/css/css.js +0 -59
  12. package/lib/css/global-rules.js +0 -15
  13. package/lib/css/index.js +0 -4
  14. package/lib/css/normalize-type-check.js +0 -4
  15. package/lib/css/presets.js +0 -25
  16. package/lib/css/style-rules.js +0 -63
  17. package/lib/css/style-rules.test.js +0 -149
  18. package/lib/embed-template.js +0 -341
  19. package/lib/embed-template.test.js +0 -648
  20. package/lib/expression.js +0 -330
  21. package/lib/expression.test.js +0 -281
  22. package/lib/generator.js +0 -112
  23. package/lib/generator.test.js +0 -166
  24. package/lib/hook.js +0 -12
  25. package/lib/hook.test.js +0 -15
  26. package/lib/instance-utils.js +0 -43
  27. package/lib/instance-utils.test.js +0 -65
  28. package/lib/prop-meta.js +0 -150
  29. package/lib/props.js +0 -176
  30. package/lib/props.test.js +0 -159
  31. package/lib/pubsub/create.js +0 -56
  32. package/lib/pubsub/index.js +0 -2
  33. package/lib/pubsub/raf-queue.js +0 -20
  34. package/lib/tree/create-elements-tree.js +0 -134
  35. package/lib/tree/index.js +0 -4
  36. package/lib/tree/root.js +0 -85
  37. package/lib/tree/webstudio-component.js +0 -61
  38. package/lib/types/pubsub/create.d.ts +0 -28
  39. package/lib/types/pubsub/index.d.ts +0 -1
  40. package/lib/types/pubsub/raf-queue.d.ts +0 -1
@@ -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
- };