@webstudio-is/react-sdk 0.191.4 → 0.192.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.
- package/lib/index.js +1723 -0
- package/lib/runtime.js +61 -0
- package/lib/types/component-generator.d.ts +37 -0
- package/lib/types/component-generator.test.d.ts +1 -0
- package/lib/types/components/component-meta.d.ts +3792 -0
- package/lib/types/components/components-utils.d.ts +8 -0
- package/lib/types/context.d.ts +37 -0
- package/lib/types/core-components.d.ts +748 -0
- package/lib/types/css/css.d.ts +21 -0
- package/lib/types/css/css.test.d.ts +1 -0
- package/lib/types/css/global-rules.d.ts +6 -0
- package/lib/types/css/index.d.ts +2 -0
- package/lib/types/embed-template.d.ts +2587 -0
- package/lib/types/embed-template.test.d.ts +1 -0
- package/lib/types/hook.d.ts +34 -0
- package/lib/types/hook.test.d.ts +1 -0
- package/lib/types/index.d.ts +12 -0
- package/lib/types/instance-utils.d.ts +4 -0
- package/lib/types/instance-utils.test.d.ts +1 -0
- package/lib/types/prop-meta.d.ts +434 -0
- package/lib/types/props.d.ts +104 -0
- package/lib/types/props.test.d.ts +1 -0
- package/lib/types/remix.d.ts +20 -0
- package/lib/types/remix.test.d.ts +1 -0
- package/lib/types/runtime.d.ts +4 -0
- package/lib/types/variable-state.d.ts +2 -0
- package/package.json +6 -6
package/lib/index.js
ADDED
|
@@ -0,0 +1,1723 @@
|
|
|
1
|
+
// src/remix.ts
|
|
2
|
+
var getRemixSegment = (segment) => {
|
|
3
|
+
if (segment === "*") {
|
|
4
|
+
return "$";
|
|
5
|
+
}
|
|
6
|
+
const match = segment.match(/^:(?<name>\w+)(?<modifier>\*|\?)?$/);
|
|
7
|
+
const name = match?.groups?.name;
|
|
8
|
+
const modifier = match?.groups?.modifier;
|
|
9
|
+
if (name) {
|
|
10
|
+
if (modifier === "*") {
|
|
11
|
+
return "$";
|
|
12
|
+
}
|
|
13
|
+
if (modifier === "?") {
|
|
14
|
+
return `($${name})`;
|
|
15
|
+
}
|
|
16
|
+
return `$${name}`;
|
|
17
|
+
}
|
|
18
|
+
return `[${segment}]`;
|
|
19
|
+
};
|
|
20
|
+
var generateRemixRoute = (pathname) => {
|
|
21
|
+
if (pathname.startsWith("/")) {
|
|
22
|
+
pathname = pathname.slice(1);
|
|
23
|
+
}
|
|
24
|
+
if (pathname === "") {
|
|
25
|
+
return `_index`;
|
|
26
|
+
}
|
|
27
|
+
const base = pathname.split("/").map(getRemixSegment).join(".");
|
|
28
|
+
const tail = pathname.endsWith("*") ? "" : "._index";
|
|
29
|
+
return `${base}${tail}`;
|
|
30
|
+
};
|
|
31
|
+
var generateRemixParams = (pathname) => {
|
|
32
|
+
const name = pathname.match(/:(?<name>\w+)\*$/)?.groups?.name;
|
|
33
|
+
let generated = "";
|
|
34
|
+
generated += `type Params = Record<string, string | undefined>;
|
|
35
|
+
`;
|
|
36
|
+
generated += `export const getRemixParams = ({ ...params }: Params): Params => {
|
|
37
|
+
`;
|
|
38
|
+
if (name) {
|
|
39
|
+
generated += ` params["${name}"] = params["*"]
|
|
40
|
+
`;
|
|
41
|
+
generated += ` delete params["*"]
|
|
42
|
+
`;
|
|
43
|
+
}
|
|
44
|
+
if (pathname.endsWith("/*")) {
|
|
45
|
+
generated += ` params[0] = params["*"]
|
|
46
|
+
`;
|
|
47
|
+
generated += ` delete params["*"]
|
|
48
|
+
`;
|
|
49
|
+
}
|
|
50
|
+
generated += ` return params
|
|
51
|
+
`;
|
|
52
|
+
generated += `}
|
|
53
|
+
`;
|
|
54
|
+
return generated;
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
// src/css/global-rules.ts
|
|
58
|
+
import { getFontFaces } from "@webstudio-is/fonts";
|
|
59
|
+
var addGlobalRules = (sheet, { assets, assetBaseUrl }) => {
|
|
60
|
+
const fontAssets = [];
|
|
61
|
+
for (const asset of assets.values()) {
|
|
62
|
+
if (asset.type === "font") {
|
|
63
|
+
fontAssets.push(asset);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
const fontFaces = getFontFaces(fontAssets, { assetBaseUrl });
|
|
67
|
+
for (const fontFace of fontFaces) {
|
|
68
|
+
sheet.addFontFaceRule(fontFace);
|
|
69
|
+
}
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
// src/css/css.ts
|
|
73
|
+
import {
|
|
74
|
+
createRegularStyleSheet,
|
|
75
|
+
generateAtomic
|
|
76
|
+
} from "@webstudio-is/css-engine";
|
|
77
|
+
import {
|
|
78
|
+
ROOT_INSTANCE_ID,
|
|
79
|
+
createScope,
|
|
80
|
+
parseComponentName
|
|
81
|
+
} from "@webstudio-is/sdk";
|
|
82
|
+
|
|
83
|
+
// src/core-components.ts
|
|
84
|
+
import {
|
|
85
|
+
EditIcon,
|
|
86
|
+
ListViewIcon,
|
|
87
|
+
PaintBrushIcon,
|
|
88
|
+
SettingsIcon,
|
|
89
|
+
AddTemplateInstanceIcon
|
|
90
|
+
} from "@webstudio-is/icons/svg";
|
|
91
|
+
import { html } from "@webstudio-is/sdk/normalize.css";
|
|
92
|
+
var rootComponent = "ws:root";
|
|
93
|
+
var rootMeta = {
|
|
94
|
+
category: "hidden",
|
|
95
|
+
type: "container",
|
|
96
|
+
label: "Global Root",
|
|
97
|
+
icon: SettingsIcon,
|
|
98
|
+
presetStyle: {
|
|
99
|
+
html
|
|
100
|
+
}
|
|
101
|
+
};
|
|
102
|
+
var rootPropsMeta = {
|
|
103
|
+
props: {}
|
|
104
|
+
};
|
|
105
|
+
var portalComponent = "Slot";
|
|
106
|
+
var collectionComponent = "ws:collection";
|
|
107
|
+
var collectionMeta = {
|
|
108
|
+
category: "data",
|
|
109
|
+
order: 2,
|
|
110
|
+
type: "container",
|
|
111
|
+
label: "Collection",
|
|
112
|
+
icon: ListViewIcon,
|
|
113
|
+
stylable: false,
|
|
114
|
+
template: [
|
|
115
|
+
{
|
|
116
|
+
type: "instance",
|
|
117
|
+
component: collectionComponent,
|
|
118
|
+
props: [
|
|
119
|
+
{
|
|
120
|
+
name: "data",
|
|
121
|
+
type: "json",
|
|
122
|
+
value: [
|
|
123
|
+
"Collection Item 1",
|
|
124
|
+
"Collection Item 2",
|
|
125
|
+
"Collection Item 3"
|
|
126
|
+
]
|
|
127
|
+
},
|
|
128
|
+
{
|
|
129
|
+
name: "item",
|
|
130
|
+
type: "parameter",
|
|
131
|
+
variableName: "collectionItem",
|
|
132
|
+
variableAlias: "Collection Item"
|
|
133
|
+
}
|
|
134
|
+
],
|
|
135
|
+
children: [
|
|
136
|
+
{
|
|
137
|
+
type: "instance",
|
|
138
|
+
component: "Box",
|
|
139
|
+
children: [
|
|
140
|
+
{
|
|
141
|
+
type: "instance",
|
|
142
|
+
component: "Text",
|
|
143
|
+
children: [{ type: "expression", value: "collectionItem" }]
|
|
144
|
+
}
|
|
145
|
+
]
|
|
146
|
+
}
|
|
147
|
+
]
|
|
148
|
+
}
|
|
149
|
+
]
|
|
150
|
+
};
|
|
151
|
+
var collectionPropsMeta = {
|
|
152
|
+
props: {
|
|
153
|
+
data: {
|
|
154
|
+
required: true,
|
|
155
|
+
control: "json",
|
|
156
|
+
type: "json"
|
|
157
|
+
}
|
|
158
|
+
},
|
|
159
|
+
initialProps: ["data"]
|
|
160
|
+
};
|
|
161
|
+
var descendantComponent = "ws:descendant";
|
|
162
|
+
var descendantMeta = {
|
|
163
|
+
category: "internal",
|
|
164
|
+
type: "control",
|
|
165
|
+
label: "Descendant",
|
|
166
|
+
icon: PaintBrushIcon,
|
|
167
|
+
detachable: false
|
|
168
|
+
};
|
|
169
|
+
var descendantPropsMeta = {
|
|
170
|
+
props: {
|
|
171
|
+
selector: {
|
|
172
|
+
required: true,
|
|
173
|
+
type: "string",
|
|
174
|
+
control: "select",
|
|
175
|
+
options: [
|
|
176
|
+
" p",
|
|
177
|
+
" h1",
|
|
178
|
+
" h2",
|
|
179
|
+
" h3",
|
|
180
|
+
" h4",
|
|
181
|
+
" h5",
|
|
182
|
+
" h6",
|
|
183
|
+
" :where(strong, b)",
|
|
184
|
+
" :where(em, i)",
|
|
185
|
+
" a",
|
|
186
|
+
" img",
|
|
187
|
+
" blockquote",
|
|
188
|
+
" code",
|
|
189
|
+
" :where(ul, ol)",
|
|
190
|
+
" li",
|
|
191
|
+
" hr"
|
|
192
|
+
]
|
|
193
|
+
}
|
|
194
|
+
},
|
|
195
|
+
initialProps: ["selector"]
|
|
196
|
+
};
|
|
197
|
+
var blockTemplateComponent = "ws:block-template";
|
|
198
|
+
var blockTemplateMeta = {
|
|
199
|
+
category: "hidden",
|
|
200
|
+
detachable: false,
|
|
201
|
+
type: "container",
|
|
202
|
+
icon: AddTemplateInstanceIcon,
|
|
203
|
+
stylable: false
|
|
204
|
+
};
|
|
205
|
+
var blockTemplatePropsMeta = {
|
|
206
|
+
props: {},
|
|
207
|
+
initialProps: []
|
|
208
|
+
};
|
|
209
|
+
var blockComponent = "ws:block";
|
|
210
|
+
var blockMeta = {
|
|
211
|
+
category: "data",
|
|
212
|
+
order: 2,
|
|
213
|
+
type: "container",
|
|
214
|
+
label: "Content Block",
|
|
215
|
+
icon: EditIcon,
|
|
216
|
+
constraints: {
|
|
217
|
+
relation: "ancestor",
|
|
218
|
+
component: { $neq: collectionComponent }
|
|
219
|
+
},
|
|
220
|
+
stylable: false,
|
|
221
|
+
template: [
|
|
222
|
+
{
|
|
223
|
+
type: "instance",
|
|
224
|
+
component: blockComponent,
|
|
225
|
+
props: [],
|
|
226
|
+
children: [
|
|
227
|
+
{
|
|
228
|
+
type: "instance",
|
|
229
|
+
label: "Templates",
|
|
230
|
+
component: blockTemplateComponent,
|
|
231
|
+
children: [
|
|
232
|
+
{
|
|
233
|
+
type: "instance",
|
|
234
|
+
component: "Paragraph",
|
|
235
|
+
children: [
|
|
236
|
+
{
|
|
237
|
+
type: "text",
|
|
238
|
+
value: "Paragraph text you can edit",
|
|
239
|
+
placeholder: true
|
|
240
|
+
}
|
|
241
|
+
]
|
|
242
|
+
},
|
|
243
|
+
{
|
|
244
|
+
type: "instance",
|
|
245
|
+
component: "List",
|
|
246
|
+
children: [
|
|
247
|
+
{
|
|
248
|
+
type: "instance",
|
|
249
|
+
component: "ListItem",
|
|
250
|
+
children: [
|
|
251
|
+
{
|
|
252
|
+
type: "text",
|
|
253
|
+
value: "List Item text you can edit",
|
|
254
|
+
placeholder: true
|
|
255
|
+
}
|
|
256
|
+
]
|
|
257
|
+
},
|
|
258
|
+
{
|
|
259
|
+
type: "instance",
|
|
260
|
+
component: "ListItem",
|
|
261
|
+
children: [
|
|
262
|
+
{
|
|
263
|
+
type: "text",
|
|
264
|
+
value: "List Item text you can edit",
|
|
265
|
+
placeholder: true
|
|
266
|
+
}
|
|
267
|
+
]
|
|
268
|
+
},
|
|
269
|
+
{
|
|
270
|
+
type: "instance",
|
|
271
|
+
component: "ListItem",
|
|
272
|
+
children: [
|
|
273
|
+
{
|
|
274
|
+
type: "text",
|
|
275
|
+
value: "List Item text you can edit",
|
|
276
|
+
placeholder: true
|
|
277
|
+
}
|
|
278
|
+
]
|
|
279
|
+
}
|
|
280
|
+
]
|
|
281
|
+
}
|
|
282
|
+
]
|
|
283
|
+
},
|
|
284
|
+
{
|
|
285
|
+
type: "instance",
|
|
286
|
+
component: "Paragraph",
|
|
287
|
+
children: [
|
|
288
|
+
{
|
|
289
|
+
type: "text",
|
|
290
|
+
value: "The Content Block component designates regions on the page where pre-styled instances can be inserted in "
|
|
291
|
+
},
|
|
292
|
+
{
|
|
293
|
+
type: "instance",
|
|
294
|
+
component: "RichTextLink",
|
|
295
|
+
children: [
|
|
296
|
+
{
|
|
297
|
+
type: "text",
|
|
298
|
+
value: "Content mode"
|
|
299
|
+
}
|
|
300
|
+
],
|
|
301
|
+
props: [
|
|
302
|
+
{
|
|
303
|
+
type: "string",
|
|
304
|
+
name: "href",
|
|
305
|
+
value: "https://wstd.us/content-block"
|
|
306
|
+
}
|
|
307
|
+
]
|
|
308
|
+
},
|
|
309
|
+
{
|
|
310
|
+
type: "text",
|
|
311
|
+
value: "."
|
|
312
|
+
}
|
|
313
|
+
]
|
|
314
|
+
},
|
|
315
|
+
{
|
|
316
|
+
type: "instance",
|
|
317
|
+
component: "List",
|
|
318
|
+
children: [
|
|
319
|
+
{
|
|
320
|
+
type: "instance",
|
|
321
|
+
component: "ListItem",
|
|
322
|
+
children: [
|
|
323
|
+
{
|
|
324
|
+
type: "text",
|
|
325
|
+
value: "In Content mode, you can edit any direct child instances that were pre-added to the Content Block, as well as add new instances predefined in Templates."
|
|
326
|
+
}
|
|
327
|
+
]
|
|
328
|
+
},
|
|
329
|
+
{
|
|
330
|
+
type: "instance",
|
|
331
|
+
component: "ListItem",
|
|
332
|
+
children: [
|
|
333
|
+
{
|
|
334
|
+
type: "text",
|
|
335
|
+
value: "To predefine instances for insertion in Content mode, switch to Design mode and add them to the Templates container."
|
|
336
|
+
}
|
|
337
|
+
]
|
|
338
|
+
},
|
|
339
|
+
{
|
|
340
|
+
type: "instance",
|
|
341
|
+
component: "ListItem",
|
|
342
|
+
children: [
|
|
343
|
+
{
|
|
344
|
+
type: "text",
|
|
345
|
+
value: "To insert predefined instances in Content mode, click the + button while hovering over the Content Block on the canvas and choose an instance from the list."
|
|
346
|
+
}
|
|
347
|
+
]
|
|
348
|
+
}
|
|
349
|
+
]
|
|
350
|
+
}
|
|
351
|
+
]
|
|
352
|
+
}
|
|
353
|
+
]
|
|
354
|
+
};
|
|
355
|
+
var blockPropsMeta = {
|
|
356
|
+
props: {},
|
|
357
|
+
initialProps: []
|
|
358
|
+
};
|
|
359
|
+
var coreMetas = {
|
|
360
|
+
[rootComponent]: rootMeta,
|
|
361
|
+
[collectionComponent]: collectionMeta,
|
|
362
|
+
[descendantComponent]: descendantMeta,
|
|
363
|
+
[blockComponent]: blockMeta,
|
|
364
|
+
[blockTemplateComponent]: blockTemplateMeta
|
|
365
|
+
};
|
|
366
|
+
var corePropsMetas = {
|
|
367
|
+
[rootComponent]: rootPropsMeta,
|
|
368
|
+
[collectionComponent]: collectionPropsMeta,
|
|
369
|
+
[descendantComponent]: descendantPropsMeta,
|
|
370
|
+
[blockComponent]: blockPropsMeta,
|
|
371
|
+
[blockTemplateComponent]: blockTemplatePropsMeta
|
|
372
|
+
};
|
|
373
|
+
var isCoreComponent = (component) => component === rootComponent || component === collectionComponent || component === descendantComponent || component === blockComponent || component === blockTemplateComponent;
|
|
374
|
+
|
|
375
|
+
// src/css/css.ts
|
|
376
|
+
import { kebabCase } from "change-case";
|
|
377
|
+
var createImageValueTransformer = (assets, { assetBaseUrl }) => (styleValue) => {
|
|
378
|
+
if (styleValue.type === "image" && styleValue.value.type === "asset") {
|
|
379
|
+
const asset = assets.get(styleValue.value.value);
|
|
380
|
+
if (asset === void 0) {
|
|
381
|
+
return { type: "keyword", value: "none" };
|
|
382
|
+
}
|
|
383
|
+
const url = `${assetBaseUrl}${asset.name}`;
|
|
384
|
+
return {
|
|
385
|
+
type: "image",
|
|
386
|
+
value: {
|
|
387
|
+
type: "url",
|
|
388
|
+
url
|
|
389
|
+
},
|
|
390
|
+
hidden: styleValue.hidden
|
|
391
|
+
};
|
|
392
|
+
}
|
|
393
|
+
};
|
|
394
|
+
var normalizeClassName = (name) => kebabCase(name);
|
|
395
|
+
var generateCss = ({
|
|
396
|
+
assets,
|
|
397
|
+
instances,
|
|
398
|
+
props,
|
|
399
|
+
breakpoints,
|
|
400
|
+
styles,
|
|
401
|
+
styleSourceSelections,
|
|
402
|
+
componentMetas,
|
|
403
|
+
assetBaseUrl,
|
|
404
|
+
atomic
|
|
405
|
+
}) => {
|
|
406
|
+
const globalSheet = createRegularStyleSheet({ name: "ssr" });
|
|
407
|
+
const sheet = createRegularStyleSheet({ name: "ssr" });
|
|
408
|
+
addGlobalRules(globalSheet, { assets, assetBaseUrl });
|
|
409
|
+
globalSheet.addMediaRule("presets");
|
|
410
|
+
const presetClasses = /* @__PURE__ */ new Map();
|
|
411
|
+
const scope = createScope([], normalizeClassName, "-");
|
|
412
|
+
for (const [component, meta] of componentMetas) {
|
|
413
|
+
const [_namespace, componentName] = parseComponentName(component);
|
|
414
|
+
const className = `w-${scope.getName(component, meta.label ?? componentName)}`;
|
|
415
|
+
const presetStyle = Object.entries(meta.presetStyle ?? {});
|
|
416
|
+
if (presetStyle.length > 0) {
|
|
417
|
+
presetClasses.set(component, className);
|
|
418
|
+
}
|
|
419
|
+
for (const [tag, styles2] of presetStyle) {
|
|
420
|
+
const selector = component === rootComponent ? ":root" : `:where(${tag}.${className})`;
|
|
421
|
+
const rule = globalSheet.addNestingRule(selector);
|
|
422
|
+
for (const declaration of styles2) {
|
|
423
|
+
rule.setDeclaration({
|
|
424
|
+
breakpoint: "presets",
|
|
425
|
+
selector: declaration.state ?? "",
|
|
426
|
+
property: declaration.property,
|
|
427
|
+
value: declaration.value
|
|
428
|
+
});
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
for (const breakpoint of breakpoints.values()) {
|
|
433
|
+
sheet.addMediaRule(breakpoint.id, breakpoint);
|
|
434
|
+
}
|
|
435
|
+
const imageValueTransformer = createImageValueTransformer(assets, {
|
|
436
|
+
assetBaseUrl
|
|
437
|
+
});
|
|
438
|
+
sheet.setTransformer(imageValueTransformer);
|
|
439
|
+
for (const styleDecl of styles.values()) {
|
|
440
|
+
const rule = sheet.addMixinRule(styleDecl.styleSourceId);
|
|
441
|
+
rule.setDeclaration({
|
|
442
|
+
breakpoint: styleDecl.breakpointId,
|
|
443
|
+
selector: styleDecl.state ?? "",
|
|
444
|
+
property: styleDecl.property,
|
|
445
|
+
value: styleDecl.value
|
|
446
|
+
});
|
|
447
|
+
}
|
|
448
|
+
const classes = /* @__PURE__ */ new Map();
|
|
449
|
+
const parentIdByInstanceId = /* @__PURE__ */ new Map();
|
|
450
|
+
for (const instance of instances.values()) {
|
|
451
|
+
const presetClass = presetClasses.get(instance.component);
|
|
452
|
+
if (presetClass) {
|
|
453
|
+
classes.set(instance.id, [presetClass]);
|
|
454
|
+
}
|
|
455
|
+
for (const child of instance.children) {
|
|
456
|
+
if (child.type === "id") {
|
|
457
|
+
parentIdByInstanceId.set(child.value, instance.id);
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
const descendantSelectorByInstanceId = /* @__PURE__ */ new Map();
|
|
462
|
+
for (const prop of props.values()) {
|
|
463
|
+
if (prop.name === "selector" && prop.type === "string") {
|
|
464
|
+
descendantSelectorByInstanceId.set(prop.instanceId, prop.value);
|
|
465
|
+
}
|
|
466
|
+
}
|
|
467
|
+
const instanceByRule = /* @__PURE__ */ new Map();
|
|
468
|
+
for (const selection of styleSourceSelections.values()) {
|
|
469
|
+
let { instanceId } = selection;
|
|
470
|
+
const { values } = selection;
|
|
471
|
+
if (instanceId === ROOT_INSTANCE_ID) {
|
|
472
|
+
const rule2 = sheet.addNestingRule(`:root`);
|
|
473
|
+
rule2.applyMixins(values);
|
|
474
|
+
continue;
|
|
475
|
+
}
|
|
476
|
+
let descendantSuffix = "";
|
|
477
|
+
const instance = instances.get(instanceId);
|
|
478
|
+
if (instance?.component === descendantComponent) {
|
|
479
|
+
const parentId = parentIdByInstanceId.get(instanceId);
|
|
480
|
+
const descendantSelector = descendantSelectorByInstanceId.get(instanceId);
|
|
481
|
+
if (parentId && descendantSelector) {
|
|
482
|
+
descendantSuffix = descendantSelector;
|
|
483
|
+
instanceId = parentId;
|
|
484
|
+
}
|
|
485
|
+
}
|
|
486
|
+
const meta = instance ? componentMetas.get(instance.component) : void 0;
|
|
487
|
+
const baseName = instance?.label ?? meta?.label ?? instance?.component ?? instanceId;
|
|
488
|
+
const className = `w-${scope.getName(instanceId, baseName)}`;
|
|
489
|
+
if (atomic === false) {
|
|
490
|
+
let classList = classes.get(instanceId);
|
|
491
|
+
if (classList === void 0) {
|
|
492
|
+
classList = [];
|
|
493
|
+
classes.set(instanceId, classList);
|
|
494
|
+
}
|
|
495
|
+
classList.push(className);
|
|
496
|
+
}
|
|
497
|
+
const rule = sheet.addNestingRule(`.${className}`, descendantSuffix);
|
|
498
|
+
rule.applyMixins(values);
|
|
499
|
+
instanceByRule.set(rule, instanceId);
|
|
500
|
+
}
|
|
501
|
+
if (atomic) {
|
|
502
|
+
const { cssText } = generateAtomic(sheet, {
|
|
503
|
+
getKey: (rule) => instanceByRule.get(rule),
|
|
504
|
+
transformValue: imageValueTransformer,
|
|
505
|
+
classes
|
|
506
|
+
});
|
|
507
|
+
return { cssText: `${globalSheet.cssText}
|
|
508
|
+
${cssText}`, classes };
|
|
509
|
+
}
|
|
510
|
+
return {
|
|
511
|
+
cssText: `${globalSheet.cssText}
|
|
512
|
+
${sheet.cssText}`,
|
|
513
|
+
classes
|
|
514
|
+
};
|
|
515
|
+
};
|
|
516
|
+
|
|
517
|
+
// src/props.ts
|
|
518
|
+
import {
|
|
519
|
+
getPagePath,
|
|
520
|
+
findPageByIdOrPath
|
|
521
|
+
} from "@webstudio-is/sdk";
|
|
522
|
+
var normalizeProps = ({
|
|
523
|
+
props,
|
|
524
|
+
assetBaseUrl,
|
|
525
|
+
assets,
|
|
526
|
+
uploadingImageAssets,
|
|
527
|
+
pages,
|
|
528
|
+
source
|
|
529
|
+
}) => {
|
|
530
|
+
const newProps = [];
|
|
531
|
+
for (const prop of props) {
|
|
532
|
+
if (prop.type === "asset") {
|
|
533
|
+
const assetId = prop.value;
|
|
534
|
+
const asset = assets.get(assetId) ?? uploadingImageAssets.find((asset2) => asset2.id === assetId);
|
|
535
|
+
if (asset === void 0) {
|
|
536
|
+
continue;
|
|
537
|
+
}
|
|
538
|
+
const propBase = {
|
|
539
|
+
id: prop.id,
|
|
540
|
+
name: prop.name,
|
|
541
|
+
required: prop.required,
|
|
542
|
+
instanceId: prop.instanceId
|
|
543
|
+
};
|
|
544
|
+
if (prop.name === "width" && asset.type === "image") {
|
|
545
|
+
newProps.push({
|
|
546
|
+
...propBase,
|
|
547
|
+
type: "number",
|
|
548
|
+
value: asset.meta.width
|
|
549
|
+
});
|
|
550
|
+
continue;
|
|
551
|
+
}
|
|
552
|
+
if (prop.name === "height" && asset.type === "image") {
|
|
553
|
+
newProps.push({
|
|
554
|
+
...propBase,
|
|
555
|
+
type: "number",
|
|
556
|
+
value: asset.meta.height
|
|
557
|
+
});
|
|
558
|
+
continue;
|
|
559
|
+
}
|
|
560
|
+
newProps.push({
|
|
561
|
+
...propBase,
|
|
562
|
+
type: "string",
|
|
563
|
+
value: `${assetBaseUrl}${asset.name}`
|
|
564
|
+
});
|
|
565
|
+
if (source === "canvas") {
|
|
566
|
+
newProps.push({
|
|
567
|
+
id: `${prop.instanceId}-${asset.id}-assetId`,
|
|
568
|
+
name: "$webstudio$canvasOnly$assetId",
|
|
569
|
+
required: false,
|
|
570
|
+
instanceId: prop.instanceId,
|
|
571
|
+
type: "string",
|
|
572
|
+
value: asset.id
|
|
573
|
+
});
|
|
574
|
+
}
|
|
575
|
+
continue;
|
|
576
|
+
}
|
|
577
|
+
if (prop.type === "page") {
|
|
578
|
+
let idProp;
|
|
579
|
+
const pageId = typeof prop.value === "string" ? prop.value : prop.value.pageId;
|
|
580
|
+
const page = findPageByIdOrPath(pageId, pages);
|
|
581
|
+
if (page === void 0) {
|
|
582
|
+
continue;
|
|
583
|
+
}
|
|
584
|
+
if (typeof prop.value !== "string") {
|
|
585
|
+
const { instanceId } = prop.value;
|
|
586
|
+
idProp = props.find(
|
|
587
|
+
(prop2) => prop2.instanceId === instanceId && prop2.name === "id"
|
|
588
|
+
);
|
|
589
|
+
}
|
|
590
|
+
const path = getPagePath(page.id, pages);
|
|
591
|
+
const url = new URL(path, "https://any-valid.url");
|
|
592
|
+
let value = url.pathname;
|
|
593
|
+
if (idProp?.type === "string") {
|
|
594
|
+
const hash = idProp.value;
|
|
595
|
+
url.hash = encodeURIComponent(hash);
|
|
596
|
+
value = `${url.pathname}${url.hash}`;
|
|
597
|
+
}
|
|
598
|
+
newProps.push({
|
|
599
|
+
id: prop.id,
|
|
600
|
+
name: prop.name,
|
|
601
|
+
required: prop.required,
|
|
602
|
+
instanceId: prop.instanceId,
|
|
603
|
+
type: "string",
|
|
604
|
+
value
|
|
605
|
+
});
|
|
606
|
+
continue;
|
|
607
|
+
}
|
|
608
|
+
newProps.push(prop);
|
|
609
|
+
}
|
|
610
|
+
return newProps;
|
|
611
|
+
};
|
|
612
|
+
var idAttribute = "data-ws-id";
|
|
613
|
+
var selectorIdAttribute = "data-ws-selector";
|
|
614
|
+
var componentAttribute = "data-ws-component";
|
|
615
|
+
var showAttribute = "data-ws-show";
|
|
616
|
+
var indexAttribute = "data-ws-index";
|
|
617
|
+
var collapsedAttribute = "data-ws-collapsed";
|
|
618
|
+
var textContentAttribute = "data-ws-text-content";
|
|
619
|
+
var attributeNameStartChar = "A-Z_a-z\\u00C0-\\u00D6\\u00D8-\\u00F6\\u00F8-\\u02FF\\u0370-\\u037D\\u037F-\\u1FFF\\u200C-\\u200D\\u2070-\\u218F\\u2C00-\\u2FEF\\u3001-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFFD";
|
|
620
|
+
var attributeNameChar = attributeNameStartChar + ":\\-0-9\\u00B7\\u0300-\\u036F\\u203F-\\u2040";
|
|
621
|
+
var validAttributeNameRegex = new RegExp(
|
|
622
|
+
// eslint-disable-next-line no-misleading-character-class
|
|
623
|
+
"^[" + attributeNameStartChar + "][" + attributeNameChar + "]*$"
|
|
624
|
+
);
|
|
625
|
+
var illegalAttributeNameCache = /* @__PURE__ */ new Map();
|
|
626
|
+
var validatedAttributeNameCache = /* @__PURE__ */ new Map();
|
|
627
|
+
var isAttributeNameSafe = (attributeName) => {
|
|
628
|
+
if (validatedAttributeNameCache.has(attributeName)) {
|
|
629
|
+
return true;
|
|
630
|
+
}
|
|
631
|
+
if (illegalAttributeNameCache.has(attributeName)) {
|
|
632
|
+
return false;
|
|
633
|
+
}
|
|
634
|
+
if (validAttributeNameRegex.test(attributeName)) {
|
|
635
|
+
validatedAttributeNameCache.set(attributeName, true);
|
|
636
|
+
return true;
|
|
637
|
+
}
|
|
638
|
+
illegalAttributeNameCache.set(attributeName, true);
|
|
639
|
+
return false;
|
|
640
|
+
};
|
|
641
|
+
|
|
642
|
+
// src/prop-meta.ts
|
|
643
|
+
import { z } from "zod";
|
|
644
|
+
var common = {
|
|
645
|
+
label: z.string().optional(),
|
|
646
|
+
description: z.string().optional(),
|
|
647
|
+
required: z.boolean()
|
|
648
|
+
};
|
|
649
|
+
var Number = z.object({
|
|
650
|
+
...common,
|
|
651
|
+
control: z.literal("number"),
|
|
652
|
+
type: z.literal("number"),
|
|
653
|
+
defaultValue: z.number().optional()
|
|
654
|
+
});
|
|
655
|
+
var Range = z.object({
|
|
656
|
+
...common,
|
|
657
|
+
control: z.literal("range"),
|
|
658
|
+
type: z.literal("number"),
|
|
659
|
+
defaultValue: z.number().optional()
|
|
660
|
+
});
|
|
661
|
+
var Text = z.object({
|
|
662
|
+
...common,
|
|
663
|
+
control: z.literal("text"),
|
|
664
|
+
type: z.literal("string"),
|
|
665
|
+
defaultValue: z.string().optional(),
|
|
666
|
+
/**
|
|
667
|
+
* The number of rows in <textarea>. If set to 0 an <input> will be used instead.
|
|
668
|
+
* In line with Storybook team's plan: https://github.com/storybookjs/storybook/issues/21100
|
|
669
|
+
*/
|
|
670
|
+
rows: z.number().optional()
|
|
671
|
+
});
|
|
672
|
+
var Code = z.object({
|
|
673
|
+
...common,
|
|
674
|
+
control: z.literal("code"),
|
|
675
|
+
type: z.literal("string"),
|
|
676
|
+
language: z.union([z.literal("html"), z.literal("markdown")]),
|
|
677
|
+
defaultValue: z.string().optional()
|
|
678
|
+
});
|
|
679
|
+
var CodeText = z.object({
|
|
680
|
+
...common,
|
|
681
|
+
control: z.literal("codetext"),
|
|
682
|
+
type: z.literal("string"),
|
|
683
|
+
defaultValue: z.string().optional()
|
|
684
|
+
});
|
|
685
|
+
var Color = z.object({
|
|
686
|
+
...common,
|
|
687
|
+
control: z.literal("color"),
|
|
688
|
+
type: z.literal("string"),
|
|
689
|
+
defaultValue: z.string().optional()
|
|
690
|
+
});
|
|
691
|
+
var Boolean = z.object({
|
|
692
|
+
...common,
|
|
693
|
+
control: z.literal("boolean"),
|
|
694
|
+
type: z.literal("boolean"),
|
|
695
|
+
defaultValue: z.boolean().optional()
|
|
696
|
+
});
|
|
697
|
+
var Radio = z.object({
|
|
698
|
+
...common,
|
|
699
|
+
control: z.literal("radio"),
|
|
700
|
+
type: z.literal("string"),
|
|
701
|
+
defaultValue: z.string().optional(),
|
|
702
|
+
options: z.array(z.string())
|
|
703
|
+
});
|
|
704
|
+
var InlineRadio = z.object({
|
|
705
|
+
...common,
|
|
706
|
+
control: z.literal("inline-radio"),
|
|
707
|
+
type: z.literal("string"),
|
|
708
|
+
defaultValue: z.string().optional(),
|
|
709
|
+
options: z.array(z.string())
|
|
710
|
+
});
|
|
711
|
+
var Select = z.object({
|
|
712
|
+
...common,
|
|
713
|
+
control: z.literal("select"),
|
|
714
|
+
type: z.literal("string"),
|
|
715
|
+
defaultValue: z.string().optional(),
|
|
716
|
+
options: z.array(z.string())
|
|
717
|
+
});
|
|
718
|
+
var Check = z.object({
|
|
719
|
+
...common,
|
|
720
|
+
control: z.literal("check"),
|
|
721
|
+
type: z.literal("string[]"),
|
|
722
|
+
defaultValue: z.array(z.string()).optional(),
|
|
723
|
+
options: z.array(z.string())
|
|
724
|
+
});
|
|
725
|
+
var InlineCheck = z.object({
|
|
726
|
+
...common,
|
|
727
|
+
control: z.literal("inline-check"),
|
|
728
|
+
type: z.literal("string[]"),
|
|
729
|
+
defaultValue: z.array(z.string()).optional(),
|
|
730
|
+
options: z.array(z.string())
|
|
731
|
+
});
|
|
732
|
+
var MultiSelect = z.object({
|
|
733
|
+
...common,
|
|
734
|
+
control: z.literal("multi-select"),
|
|
735
|
+
type: z.literal("string[]"),
|
|
736
|
+
defaultValue: z.array(z.string()).optional(),
|
|
737
|
+
options: z.array(z.string())
|
|
738
|
+
});
|
|
739
|
+
var File = z.object({
|
|
740
|
+
...common,
|
|
741
|
+
control: z.literal("file"),
|
|
742
|
+
type: z.literal("string"),
|
|
743
|
+
defaultValue: z.string().optional(),
|
|
744
|
+
/** https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/file#accept */
|
|
745
|
+
accept: z.string().optional()
|
|
746
|
+
});
|
|
747
|
+
var Url = z.object({
|
|
748
|
+
...common,
|
|
749
|
+
control: z.literal("url"),
|
|
750
|
+
type: z.literal("string"),
|
|
751
|
+
defaultValue: z.string().optional()
|
|
752
|
+
});
|
|
753
|
+
var Json = z.object({
|
|
754
|
+
...common,
|
|
755
|
+
control: z.literal("json"),
|
|
756
|
+
type: z.literal("json"),
|
|
757
|
+
defaultValue: z.unknown().optional()
|
|
758
|
+
});
|
|
759
|
+
var Date = z.object({
|
|
760
|
+
...common,
|
|
761
|
+
control: z.literal("date"),
|
|
762
|
+
// @todo not sure what type should be here
|
|
763
|
+
// (we don't support Date yet, added for completeness)
|
|
764
|
+
type: z.literal("string"),
|
|
765
|
+
defaultValue: z.string().optional()
|
|
766
|
+
});
|
|
767
|
+
var Action = z.object({
|
|
768
|
+
...common,
|
|
769
|
+
control: z.literal("action"),
|
|
770
|
+
type: z.literal("action"),
|
|
771
|
+
defaultValue: z.undefined().optional()
|
|
772
|
+
});
|
|
773
|
+
var TextContent = z.object({
|
|
774
|
+
...common,
|
|
775
|
+
control: z.literal("textContent"),
|
|
776
|
+
type: z.literal("string"),
|
|
777
|
+
defaultValue: z.string().optional()
|
|
778
|
+
});
|
|
779
|
+
var PropMeta = z.union([
|
|
780
|
+
Number,
|
|
781
|
+
Range,
|
|
782
|
+
Text,
|
|
783
|
+
Code,
|
|
784
|
+
CodeText,
|
|
785
|
+
Color,
|
|
786
|
+
Boolean,
|
|
787
|
+
Radio,
|
|
788
|
+
InlineRadio,
|
|
789
|
+
Select,
|
|
790
|
+
MultiSelect,
|
|
791
|
+
Check,
|
|
792
|
+
InlineCheck,
|
|
793
|
+
File,
|
|
794
|
+
Url,
|
|
795
|
+
Json,
|
|
796
|
+
Date,
|
|
797
|
+
Action,
|
|
798
|
+
TextContent
|
|
799
|
+
]);
|
|
800
|
+
|
|
801
|
+
// src/components/component-meta.ts
|
|
802
|
+
import { z as z3 } from "zod";
|
|
803
|
+
import { Matchers } from "@webstudio-is/sdk";
|
|
804
|
+
|
|
805
|
+
// src/embed-template.ts
|
|
806
|
+
import { z as z2 } from "zod";
|
|
807
|
+
import { nanoid } from "nanoid";
|
|
808
|
+
import { titleCase } from "title-case";
|
|
809
|
+
import { noCase } from "change-case";
|
|
810
|
+
import {
|
|
811
|
+
encodeDataSourceVariable,
|
|
812
|
+
transpileExpression
|
|
813
|
+
} from "@webstudio-is/sdk";
|
|
814
|
+
import { StyleValue } from "@webstudio-is/css-engine";
|
|
815
|
+
var EmbedTemplateText = z2.object({
|
|
816
|
+
type: z2.literal("text"),
|
|
817
|
+
value: z2.string(),
|
|
818
|
+
placeholder: z2.boolean().optional()
|
|
819
|
+
});
|
|
820
|
+
var EmbedTemplateExpression = z2.object({
|
|
821
|
+
type: z2.literal("expression"),
|
|
822
|
+
value: z2.string()
|
|
823
|
+
});
|
|
824
|
+
var EmbedTemplateVariable = z2.object({
|
|
825
|
+
alias: z2.optional(z2.string()),
|
|
826
|
+
initialValue: z2.unknown()
|
|
827
|
+
});
|
|
828
|
+
var EmbedTemplateProp = z2.union([
|
|
829
|
+
z2.object({
|
|
830
|
+
type: z2.literal("number"),
|
|
831
|
+
name: z2.string(),
|
|
832
|
+
value: z2.number()
|
|
833
|
+
}),
|
|
834
|
+
z2.object({
|
|
835
|
+
type: z2.literal("string"),
|
|
836
|
+
name: z2.string(),
|
|
837
|
+
value: z2.string()
|
|
838
|
+
}),
|
|
839
|
+
z2.object({
|
|
840
|
+
type: z2.literal("boolean"),
|
|
841
|
+
name: z2.string(),
|
|
842
|
+
value: z2.boolean()
|
|
843
|
+
}),
|
|
844
|
+
z2.object({
|
|
845
|
+
type: z2.literal("string[]"),
|
|
846
|
+
name: z2.string(),
|
|
847
|
+
value: z2.array(z2.string())
|
|
848
|
+
}),
|
|
849
|
+
z2.object({
|
|
850
|
+
type: z2.literal("json"),
|
|
851
|
+
name: z2.string(),
|
|
852
|
+
value: z2.unknown()
|
|
853
|
+
}),
|
|
854
|
+
z2.object({
|
|
855
|
+
type: z2.literal("expression"),
|
|
856
|
+
name: z2.string(),
|
|
857
|
+
code: z2.string()
|
|
858
|
+
}),
|
|
859
|
+
z2.object({
|
|
860
|
+
type: z2.literal("parameter"),
|
|
861
|
+
name: z2.string(),
|
|
862
|
+
variableName: z2.string(),
|
|
863
|
+
variableAlias: z2.optional(z2.string())
|
|
864
|
+
}),
|
|
865
|
+
z2.object({
|
|
866
|
+
type: z2.literal("action"),
|
|
867
|
+
name: z2.string(),
|
|
868
|
+
value: z2.array(
|
|
869
|
+
z2.object({
|
|
870
|
+
type: z2.literal("execute"),
|
|
871
|
+
args: z2.optional(z2.array(z2.string())),
|
|
872
|
+
code: z2.string()
|
|
873
|
+
})
|
|
874
|
+
)
|
|
875
|
+
})
|
|
876
|
+
]);
|
|
877
|
+
var EmbedTemplateStyleDeclRaw = z2.object({
|
|
878
|
+
// State selector, e.g. :hover
|
|
879
|
+
state: z2.optional(z2.string()),
|
|
880
|
+
property: z2.string(),
|
|
881
|
+
value: StyleValue
|
|
882
|
+
});
|
|
883
|
+
var EmbedTemplateStyleDecl = EmbedTemplateStyleDeclRaw;
|
|
884
|
+
var EmbedTemplateInstance = z2.lazy(
|
|
885
|
+
() => z2.object({
|
|
886
|
+
type: z2.literal("instance"),
|
|
887
|
+
component: z2.string(),
|
|
888
|
+
label: z2.optional(z2.string()),
|
|
889
|
+
variables: z2.optional(z2.record(z2.string(), EmbedTemplateVariable)),
|
|
890
|
+
props: z2.optional(z2.array(EmbedTemplateProp)),
|
|
891
|
+
tokens: z2.optional(z2.array(z2.string())),
|
|
892
|
+
styles: z2.optional(z2.array(EmbedTemplateStyleDecl)),
|
|
893
|
+
children: WsEmbedTemplate
|
|
894
|
+
})
|
|
895
|
+
);
|
|
896
|
+
var WsEmbedTemplate = z2.lazy(
|
|
897
|
+
() => z2.array(
|
|
898
|
+
z2.union([EmbedTemplateInstance, EmbedTemplateText, EmbedTemplateExpression])
|
|
899
|
+
)
|
|
900
|
+
);
|
|
901
|
+
var getVariablValue = (value) => {
|
|
902
|
+
if (typeof value === "string") {
|
|
903
|
+
return { type: "string", value };
|
|
904
|
+
}
|
|
905
|
+
if (typeof value === "number") {
|
|
906
|
+
return { type: "number", value };
|
|
907
|
+
}
|
|
908
|
+
if (typeof value === "boolean") {
|
|
909
|
+
return { type: "boolean", value };
|
|
910
|
+
}
|
|
911
|
+
if (Array.isArray(value)) {
|
|
912
|
+
return { type: "string[]", value };
|
|
913
|
+
}
|
|
914
|
+
return { type: "json", value };
|
|
915
|
+
};
|
|
916
|
+
var createInstancesFromTemplate = (treeTemplate, instances, props, dataSourceByRef, styleSourceSelections, styleSources, styles, metas, defaultBreakpointId, generateId) => {
|
|
917
|
+
const parentChildren = [];
|
|
918
|
+
for (const item of treeTemplate) {
|
|
919
|
+
if (item.type === "instance") {
|
|
920
|
+
const instanceId = generateId();
|
|
921
|
+
if (item.variables) {
|
|
922
|
+
for (const [name, variable] of Object.entries(item.variables)) {
|
|
923
|
+
if (dataSourceByRef.has(name)) {
|
|
924
|
+
throw Error(`${name} data source already defined`);
|
|
925
|
+
}
|
|
926
|
+
dataSourceByRef.set(name, {
|
|
927
|
+
type: "variable",
|
|
928
|
+
id: generateId(),
|
|
929
|
+
scopeInstanceId: instanceId,
|
|
930
|
+
name: variable.alias ?? name,
|
|
931
|
+
value: getVariablValue(variable.initialValue)
|
|
932
|
+
});
|
|
933
|
+
}
|
|
934
|
+
}
|
|
935
|
+
if (item.props) {
|
|
936
|
+
for (const prop of item.props) {
|
|
937
|
+
const propId = generateId();
|
|
938
|
+
if (prop.type === "expression") {
|
|
939
|
+
props.push({
|
|
940
|
+
id: propId,
|
|
941
|
+
instanceId,
|
|
942
|
+
name: prop.name,
|
|
943
|
+
type: "expression",
|
|
944
|
+
// replace all references with variable names
|
|
945
|
+
value: transpileExpression({
|
|
946
|
+
expression: prop.code,
|
|
947
|
+
replaceVariable: (ref) => {
|
|
948
|
+
const id = dataSourceByRef.get(ref)?.id ?? ref;
|
|
949
|
+
return encodeDataSourceVariable(id);
|
|
950
|
+
}
|
|
951
|
+
})
|
|
952
|
+
});
|
|
953
|
+
continue;
|
|
954
|
+
}
|
|
955
|
+
if (prop.type === "action") {
|
|
956
|
+
props.push({
|
|
957
|
+
id: propId,
|
|
958
|
+
instanceId,
|
|
959
|
+
type: "action",
|
|
960
|
+
name: prop.name,
|
|
961
|
+
value: prop.value.map((value) => {
|
|
962
|
+
const args = value.args ?? [];
|
|
963
|
+
return {
|
|
964
|
+
type: "execute",
|
|
965
|
+
args,
|
|
966
|
+
// replace all references with variable names
|
|
967
|
+
code: transpileExpression({
|
|
968
|
+
expression: value.code,
|
|
969
|
+
replaceVariable: (ref) => {
|
|
970
|
+
if (args.includes(ref)) {
|
|
971
|
+
return;
|
|
972
|
+
}
|
|
973
|
+
const id = dataSourceByRef.get(ref)?.id ?? ref;
|
|
974
|
+
return encodeDataSourceVariable(id);
|
|
975
|
+
}
|
|
976
|
+
})
|
|
977
|
+
};
|
|
978
|
+
})
|
|
979
|
+
});
|
|
980
|
+
continue;
|
|
981
|
+
}
|
|
982
|
+
if (prop.type === "parameter") {
|
|
983
|
+
const dataSourceId = generateId();
|
|
984
|
+
dataSourceByRef.set(prop.variableName, {
|
|
985
|
+
type: "parameter",
|
|
986
|
+
id: dataSourceId,
|
|
987
|
+
scopeInstanceId: instanceId,
|
|
988
|
+
name: prop.variableAlias ?? prop.variableName
|
|
989
|
+
});
|
|
990
|
+
props.push({
|
|
991
|
+
id: propId,
|
|
992
|
+
instanceId,
|
|
993
|
+
name: prop.name,
|
|
994
|
+
type: "parameter",
|
|
995
|
+
// replace variable reference with variable id
|
|
996
|
+
value: dataSourceId
|
|
997
|
+
});
|
|
998
|
+
continue;
|
|
999
|
+
}
|
|
1000
|
+
props.push({ id: propId, instanceId, ...prop });
|
|
1001
|
+
}
|
|
1002
|
+
}
|
|
1003
|
+
const styleSourceIds = [];
|
|
1004
|
+
if (item.tokens) {
|
|
1005
|
+
const meta = metas.get(item.component);
|
|
1006
|
+
if (meta?.presetTokens) {
|
|
1007
|
+
for (const name of item.tokens) {
|
|
1008
|
+
const tokenValue = meta.presetTokens[name];
|
|
1009
|
+
if (tokenValue) {
|
|
1010
|
+
const styleSourceId = `${item.component}:${name}`;
|
|
1011
|
+
styleSourceIds.push(styleSourceId);
|
|
1012
|
+
styleSources.push({
|
|
1013
|
+
type: "token",
|
|
1014
|
+
id: styleSourceId,
|
|
1015
|
+
name: titleCase(noCase(name))
|
|
1016
|
+
});
|
|
1017
|
+
for (const styleDecl of tokenValue.styles) {
|
|
1018
|
+
styles.push({
|
|
1019
|
+
breakpointId: defaultBreakpointId,
|
|
1020
|
+
styleSourceId,
|
|
1021
|
+
state: styleDecl.state,
|
|
1022
|
+
property: styleDecl.property,
|
|
1023
|
+
value: styleDecl.value
|
|
1024
|
+
});
|
|
1025
|
+
}
|
|
1026
|
+
}
|
|
1027
|
+
}
|
|
1028
|
+
}
|
|
1029
|
+
}
|
|
1030
|
+
if (item.styles) {
|
|
1031
|
+
const styleSourceId = generateId();
|
|
1032
|
+
styleSources.push({
|
|
1033
|
+
type: "local",
|
|
1034
|
+
id: styleSourceId
|
|
1035
|
+
});
|
|
1036
|
+
styleSourceIds.push(styleSourceId);
|
|
1037
|
+
for (const styleDecl of item.styles) {
|
|
1038
|
+
styles.push({
|
|
1039
|
+
breakpointId: defaultBreakpointId,
|
|
1040
|
+
styleSourceId,
|
|
1041
|
+
state: styleDecl.state,
|
|
1042
|
+
property: styleDecl.property,
|
|
1043
|
+
value: styleDecl.value
|
|
1044
|
+
});
|
|
1045
|
+
}
|
|
1046
|
+
}
|
|
1047
|
+
if (styleSourceIds.length > 0) {
|
|
1048
|
+
styleSourceSelections.push({
|
|
1049
|
+
instanceId,
|
|
1050
|
+
values: styleSourceIds
|
|
1051
|
+
});
|
|
1052
|
+
}
|
|
1053
|
+
const instance = {
|
|
1054
|
+
type: "instance",
|
|
1055
|
+
id: instanceId,
|
|
1056
|
+
label: item.label,
|
|
1057
|
+
component: item.component,
|
|
1058
|
+
children: []
|
|
1059
|
+
};
|
|
1060
|
+
instances.push(instance);
|
|
1061
|
+
instance.children = createInstancesFromTemplate(
|
|
1062
|
+
item.children,
|
|
1063
|
+
instances,
|
|
1064
|
+
props,
|
|
1065
|
+
dataSourceByRef,
|
|
1066
|
+
styleSourceSelections,
|
|
1067
|
+
styleSources,
|
|
1068
|
+
styles,
|
|
1069
|
+
metas,
|
|
1070
|
+
defaultBreakpointId,
|
|
1071
|
+
generateId
|
|
1072
|
+
);
|
|
1073
|
+
parentChildren.push({
|
|
1074
|
+
type: "id",
|
|
1075
|
+
value: instanceId
|
|
1076
|
+
});
|
|
1077
|
+
}
|
|
1078
|
+
if (item.type === "text") {
|
|
1079
|
+
parentChildren.push({
|
|
1080
|
+
type: "text",
|
|
1081
|
+
value: item.value,
|
|
1082
|
+
placeholder: item.placeholder
|
|
1083
|
+
});
|
|
1084
|
+
}
|
|
1085
|
+
if (item.type === "expression") {
|
|
1086
|
+
parentChildren.push({
|
|
1087
|
+
type: "expression",
|
|
1088
|
+
// replace all references with variable names
|
|
1089
|
+
value: transpileExpression({
|
|
1090
|
+
expression: item.value,
|
|
1091
|
+
replaceVariable: (ref) => {
|
|
1092
|
+
const id = dataSourceByRef.get(ref)?.id ?? ref;
|
|
1093
|
+
return encodeDataSourceVariable(id);
|
|
1094
|
+
}
|
|
1095
|
+
})
|
|
1096
|
+
});
|
|
1097
|
+
}
|
|
1098
|
+
}
|
|
1099
|
+
return parentChildren;
|
|
1100
|
+
};
|
|
1101
|
+
var generateDataFromEmbedTemplate = (treeTemplate, metas, defaultBreakpointId, generateId = nanoid) => {
|
|
1102
|
+
const instances = [];
|
|
1103
|
+
const props = [];
|
|
1104
|
+
const dataSourceByRef = /* @__PURE__ */ new Map();
|
|
1105
|
+
const styleSourceSelections = [];
|
|
1106
|
+
const styleSources = [];
|
|
1107
|
+
const styles = [];
|
|
1108
|
+
const children = createInstancesFromTemplate(
|
|
1109
|
+
treeTemplate,
|
|
1110
|
+
instances,
|
|
1111
|
+
props,
|
|
1112
|
+
dataSourceByRef,
|
|
1113
|
+
styleSourceSelections,
|
|
1114
|
+
styleSources,
|
|
1115
|
+
styles,
|
|
1116
|
+
metas,
|
|
1117
|
+
defaultBreakpointId,
|
|
1118
|
+
generateId
|
|
1119
|
+
);
|
|
1120
|
+
return {
|
|
1121
|
+
children,
|
|
1122
|
+
instances,
|
|
1123
|
+
props,
|
|
1124
|
+
dataSources: Array.from(dataSourceByRef.values()),
|
|
1125
|
+
styleSourceSelections,
|
|
1126
|
+
styleSources,
|
|
1127
|
+
styles,
|
|
1128
|
+
assets: [],
|
|
1129
|
+
breakpoints: [],
|
|
1130
|
+
resources: []
|
|
1131
|
+
};
|
|
1132
|
+
};
|
|
1133
|
+
var namespaceEmbedTemplateComponents = (template, namespace, components) => {
|
|
1134
|
+
return template.map((item) => {
|
|
1135
|
+
if (item.type === "text") {
|
|
1136
|
+
return item;
|
|
1137
|
+
}
|
|
1138
|
+
if (item.type === "expression") {
|
|
1139
|
+
return item;
|
|
1140
|
+
}
|
|
1141
|
+
if (item.type === "instance") {
|
|
1142
|
+
const prefix = components.has(item.component) ? `${namespace}:` : "";
|
|
1143
|
+
return {
|
|
1144
|
+
...item,
|
|
1145
|
+
component: `${prefix}${item.component}`,
|
|
1146
|
+
children: namespaceEmbedTemplateComponents(
|
|
1147
|
+
item.children,
|
|
1148
|
+
namespace,
|
|
1149
|
+
components
|
|
1150
|
+
)
|
|
1151
|
+
};
|
|
1152
|
+
}
|
|
1153
|
+
item;
|
|
1154
|
+
throw Error("Impossible case");
|
|
1155
|
+
});
|
|
1156
|
+
};
|
|
1157
|
+
var namespaceMatcher = (namespace, matcher) => {
|
|
1158
|
+
const newMatcher = structuredClone(matcher);
|
|
1159
|
+
if (newMatcher.component?.$eq) {
|
|
1160
|
+
newMatcher.component.$eq = `${namespace}:${newMatcher.component.$eq}`;
|
|
1161
|
+
}
|
|
1162
|
+
if (newMatcher.component?.$neq) {
|
|
1163
|
+
newMatcher.component.$neq = `${namespace}:${newMatcher.component.$neq}`;
|
|
1164
|
+
}
|
|
1165
|
+
if (newMatcher.component?.$in) {
|
|
1166
|
+
newMatcher.component.$in = newMatcher.component.$in.map(
|
|
1167
|
+
(component) => `${namespace}:${component}`
|
|
1168
|
+
);
|
|
1169
|
+
}
|
|
1170
|
+
if (newMatcher.component?.$nin) {
|
|
1171
|
+
newMatcher.component.$nin = newMatcher.component.$nin.map(
|
|
1172
|
+
(component) => `${namespace}:${component}`
|
|
1173
|
+
);
|
|
1174
|
+
}
|
|
1175
|
+
return newMatcher;
|
|
1176
|
+
};
|
|
1177
|
+
var namespaceMeta = (meta, namespace, components) => {
|
|
1178
|
+
const newMeta = { ...meta };
|
|
1179
|
+
if (newMeta.constraints) {
|
|
1180
|
+
if (Array.isArray(newMeta.constraints)) {
|
|
1181
|
+
newMeta.constraints = newMeta.constraints.map(
|
|
1182
|
+
(matcher) => namespaceMatcher(namespace, matcher)
|
|
1183
|
+
);
|
|
1184
|
+
} else {
|
|
1185
|
+
newMeta.constraints = namespaceMatcher(namespace, newMeta.constraints);
|
|
1186
|
+
}
|
|
1187
|
+
}
|
|
1188
|
+
if (newMeta.indexWithinAncestor) {
|
|
1189
|
+
newMeta.indexWithinAncestor = components.has(newMeta.indexWithinAncestor) ? `${namespace}:${newMeta.indexWithinAncestor}` : newMeta.indexWithinAncestor;
|
|
1190
|
+
}
|
|
1191
|
+
if (newMeta.template) {
|
|
1192
|
+
newMeta.template = namespaceEmbedTemplateComponents(
|
|
1193
|
+
newMeta.template,
|
|
1194
|
+
namespace,
|
|
1195
|
+
components
|
|
1196
|
+
);
|
|
1197
|
+
}
|
|
1198
|
+
return newMeta;
|
|
1199
|
+
};
|
|
1200
|
+
|
|
1201
|
+
// src/components/component-meta.ts
|
|
1202
|
+
var WsComponentPropsMeta = z3.object({
|
|
1203
|
+
props: z3.record(PropMeta),
|
|
1204
|
+
// Props that will be always visible in properties panel.
|
|
1205
|
+
initialProps: z3.array(z3.string()).optional()
|
|
1206
|
+
});
|
|
1207
|
+
var componentCategories = [
|
|
1208
|
+
"general",
|
|
1209
|
+
"text",
|
|
1210
|
+
"data",
|
|
1211
|
+
"media",
|
|
1212
|
+
"forms",
|
|
1213
|
+
"radix",
|
|
1214
|
+
"xml",
|
|
1215
|
+
"hidden",
|
|
1216
|
+
"internal"
|
|
1217
|
+
];
|
|
1218
|
+
var stateCategories = ["states", "component-states"];
|
|
1219
|
+
var ComponentState = z3.object({
|
|
1220
|
+
category: z3.enum(stateCategories).optional(),
|
|
1221
|
+
selector: z3.string(),
|
|
1222
|
+
label: z3.string()
|
|
1223
|
+
});
|
|
1224
|
+
var ComponentToken = z3.object({
|
|
1225
|
+
variant: z3.optional(z3.string()),
|
|
1226
|
+
styles: z3.array(EmbedTemplateStyleDecl)
|
|
1227
|
+
});
|
|
1228
|
+
var defaultStates = [
|
|
1229
|
+
{ selector: ":hover", label: "Hover" },
|
|
1230
|
+
{ selector: ":active", label: "Active" },
|
|
1231
|
+
{ selector: ":focus", label: "Focus" },
|
|
1232
|
+
{ selector: ":focus-visible", label: "Focus Visible" },
|
|
1233
|
+
{ selector: ":focus-within", label: "Focus Within" }
|
|
1234
|
+
];
|
|
1235
|
+
var WsComponentMeta = z3.object({
|
|
1236
|
+
category: z3.enum(componentCategories).optional(),
|
|
1237
|
+
// container - can accept other components with dnd or be edited as text
|
|
1238
|
+
// control - usually form controls like inputs, without children
|
|
1239
|
+
// embed - images, videos or other embeddable components, without children
|
|
1240
|
+
// rich-text-child - formatted text fragment, not listed in components list
|
|
1241
|
+
type: z3.enum(["container", "control", "embed", "rich-text-child"]),
|
|
1242
|
+
constraints: Matchers.optional(),
|
|
1243
|
+
// when this field is specified component receives
|
|
1244
|
+
// prop with index of same components withiin specified ancestor
|
|
1245
|
+
// important to automatically enumerate collections without
|
|
1246
|
+
// naming every item manually
|
|
1247
|
+
indexWithinAncestor: z3.optional(z3.string()),
|
|
1248
|
+
stylable: z3.optional(z3.boolean()),
|
|
1249
|
+
// specifies whether the instance can be deleted,
|
|
1250
|
+
// copied or dragged out of its parent instance
|
|
1251
|
+
// true by default
|
|
1252
|
+
detachable: z3.optional(z3.boolean()),
|
|
1253
|
+
label: z3.optional(z3.string()),
|
|
1254
|
+
description: z3.string().optional(),
|
|
1255
|
+
icon: z3.string(),
|
|
1256
|
+
presetStyle: z3.optional(
|
|
1257
|
+
z3.record(z3.string(), z3.array(EmbedTemplateStyleDecl))
|
|
1258
|
+
),
|
|
1259
|
+
presetTokens: z3.optional(z3.record(z3.string(), ComponentToken)),
|
|
1260
|
+
states: z3.optional(z3.array(ComponentState)),
|
|
1261
|
+
template: z3.optional(WsEmbedTemplate),
|
|
1262
|
+
order: z3.number().optional()
|
|
1263
|
+
});
|
|
1264
|
+
|
|
1265
|
+
// src/instance-utils.ts
|
|
1266
|
+
var getIndexesWithinAncestors = (metas, instances, rootIds) => {
|
|
1267
|
+
const ancestors = /* @__PURE__ */ new Set();
|
|
1268
|
+
for (const meta of metas.values()) {
|
|
1269
|
+
if (meta.indexWithinAncestor !== void 0) {
|
|
1270
|
+
ancestors.add(meta.indexWithinAncestor);
|
|
1271
|
+
}
|
|
1272
|
+
}
|
|
1273
|
+
const indexes = /* @__PURE__ */ new Map();
|
|
1274
|
+
const traverseInstances = (instances2, instanceId, latestIndexes2 = /* @__PURE__ */ new Map()) => {
|
|
1275
|
+
const instance = instances2.get(instanceId);
|
|
1276
|
+
if (instance === void 0) {
|
|
1277
|
+
return;
|
|
1278
|
+
}
|
|
1279
|
+
const meta = metas.get(instance.component);
|
|
1280
|
+
if (meta === void 0) {
|
|
1281
|
+
return;
|
|
1282
|
+
}
|
|
1283
|
+
if (ancestors.has(instance.component)) {
|
|
1284
|
+
latestIndexes2 = new Map(latestIndexes2);
|
|
1285
|
+
latestIndexes2.set(instance.component, /* @__PURE__ */ new Map());
|
|
1286
|
+
}
|
|
1287
|
+
if (meta.indexWithinAncestor !== void 0) {
|
|
1288
|
+
const ancestorIndexes = latestIndexes2.get(meta.indexWithinAncestor);
|
|
1289
|
+
if (ancestorIndexes !== void 0) {
|
|
1290
|
+
let index = ancestorIndexes.get(instance.component) ?? -1;
|
|
1291
|
+
index += 1;
|
|
1292
|
+
ancestorIndexes.set(instance.component, index);
|
|
1293
|
+
indexes.set(instance.id, index);
|
|
1294
|
+
}
|
|
1295
|
+
}
|
|
1296
|
+
for (const child of instance.children) {
|
|
1297
|
+
if (child.type === "id") {
|
|
1298
|
+
traverseInstances(instances2, child.value, latestIndexes2);
|
|
1299
|
+
}
|
|
1300
|
+
}
|
|
1301
|
+
};
|
|
1302
|
+
const latestIndexes = /* @__PURE__ */ new Map();
|
|
1303
|
+
for (const instanceId of rootIds) {
|
|
1304
|
+
traverseInstances(instances, instanceId, latestIndexes);
|
|
1305
|
+
}
|
|
1306
|
+
return indexes;
|
|
1307
|
+
};
|
|
1308
|
+
|
|
1309
|
+
// src/component-generator.ts
|
|
1310
|
+
import {
|
|
1311
|
+
parseComponentName as parseComponentName2,
|
|
1312
|
+
generateExpression,
|
|
1313
|
+
decodeDataSourceVariable,
|
|
1314
|
+
transpileExpression as transpileExpression2
|
|
1315
|
+
} from "@webstudio-is/sdk";
|
|
1316
|
+
var generateAction = ({
|
|
1317
|
+
scope,
|
|
1318
|
+
prop,
|
|
1319
|
+
dataSources,
|
|
1320
|
+
usedDataSources
|
|
1321
|
+
}) => {
|
|
1322
|
+
const setters = /* @__PURE__ */ new Set();
|
|
1323
|
+
let args = [];
|
|
1324
|
+
let assignersCode = "";
|
|
1325
|
+
for (const value of prop.value) {
|
|
1326
|
+
args = value.args;
|
|
1327
|
+
assignersCode += transpileExpression2({
|
|
1328
|
+
expression: value.code,
|
|
1329
|
+
executable: true,
|
|
1330
|
+
replaceVariable: (identifier, assignee) => {
|
|
1331
|
+
if (args?.includes(identifier)) {
|
|
1332
|
+
return;
|
|
1333
|
+
}
|
|
1334
|
+
const depId = decodeDataSourceVariable(identifier);
|
|
1335
|
+
const dep = depId ? dataSources.get(depId) : void 0;
|
|
1336
|
+
if (dep) {
|
|
1337
|
+
usedDataSources.set(dep.id, dep);
|
|
1338
|
+
if (assignee) {
|
|
1339
|
+
setters.add(dep);
|
|
1340
|
+
}
|
|
1341
|
+
const valueName = scope.getName(dep.id, dep.name);
|
|
1342
|
+
return valueName;
|
|
1343
|
+
}
|
|
1344
|
+
console.error(`Unknown dependency "${identifier}"`);
|
|
1345
|
+
}
|
|
1346
|
+
});
|
|
1347
|
+
assignersCode += `
|
|
1348
|
+
`;
|
|
1349
|
+
}
|
|
1350
|
+
let settersCode = "";
|
|
1351
|
+
for (const dataSource of setters) {
|
|
1352
|
+
const valueName = scope.getName(dataSource.id, dataSource.name);
|
|
1353
|
+
const setterName = scope.getName(
|
|
1354
|
+
`set$${dataSource.id}`,
|
|
1355
|
+
`set$${dataSource.name}`
|
|
1356
|
+
);
|
|
1357
|
+
settersCode += `${setterName}(${valueName})
|
|
1358
|
+
`;
|
|
1359
|
+
}
|
|
1360
|
+
const argsList = args.map((arg) => `${arg}: any`).join(", ");
|
|
1361
|
+
let generated = "";
|
|
1362
|
+
generated += `(${argsList}) => {
|
|
1363
|
+
`;
|
|
1364
|
+
generated += assignersCode;
|
|
1365
|
+
generated += settersCode;
|
|
1366
|
+
generated += `}`;
|
|
1367
|
+
return generated;
|
|
1368
|
+
};
|
|
1369
|
+
var generatePropValue = ({
|
|
1370
|
+
scope,
|
|
1371
|
+
prop,
|
|
1372
|
+
dataSources,
|
|
1373
|
+
usedDataSources
|
|
1374
|
+
}) => {
|
|
1375
|
+
if (prop.type === "asset" || prop.type === "page") {
|
|
1376
|
+
return;
|
|
1377
|
+
}
|
|
1378
|
+
if (prop.type === "string" || prop.type === "number" || prop.type === "boolean" || prop.type === "string[]" || prop.type === "json") {
|
|
1379
|
+
return JSON.stringify(prop.value);
|
|
1380
|
+
}
|
|
1381
|
+
if (prop.type === "parameter") {
|
|
1382
|
+
const dataSource = dataSources.get(prop.value);
|
|
1383
|
+
if (dataSource === void 0) {
|
|
1384
|
+
return;
|
|
1385
|
+
}
|
|
1386
|
+
usedDataSources.set(dataSource.id, dataSource);
|
|
1387
|
+
return scope.getName(dataSource.id, dataSource.name);
|
|
1388
|
+
}
|
|
1389
|
+
if (prop.type === "expression") {
|
|
1390
|
+
return generateExpression({
|
|
1391
|
+
expression: prop.value,
|
|
1392
|
+
dataSources,
|
|
1393
|
+
usedDataSources,
|
|
1394
|
+
scope
|
|
1395
|
+
});
|
|
1396
|
+
}
|
|
1397
|
+
if (prop.type === "action") {
|
|
1398
|
+
return generateAction({ scope, prop, dataSources, usedDataSources });
|
|
1399
|
+
}
|
|
1400
|
+
if (prop.type === "resource") {
|
|
1401
|
+
return JSON.stringify(scope.getName(prop.value, prop.name));
|
|
1402
|
+
}
|
|
1403
|
+
prop;
|
|
1404
|
+
};
|
|
1405
|
+
var generateJsxElement = ({
|
|
1406
|
+
context = "jsx",
|
|
1407
|
+
scope,
|
|
1408
|
+
instance,
|
|
1409
|
+
props,
|
|
1410
|
+
dataSources,
|
|
1411
|
+
usedDataSources,
|
|
1412
|
+
indexesWithinAncestors,
|
|
1413
|
+
children,
|
|
1414
|
+
classesMap
|
|
1415
|
+
}) => {
|
|
1416
|
+
if (instance.component === descendantComponent) {
|
|
1417
|
+
return "";
|
|
1418
|
+
}
|
|
1419
|
+
let generatedProps = "";
|
|
1420
|
+
const index = indexesWithinAncestors.get(instance.id);
|
|
1421
|
+
if (index !== void 0) {
|
|
1422
|
+
generatedProps += `
|
|
1423
|
+
${indexAttribute}="${index}"`;
|
|
1424
|
+
}
|
|
1425
|
+
let conditionValue;
|
|
1426
|
+
let collectionDataValue;
|
|
1427
|
+
let collectionItemValue;
|
|
1428
|
+
const classMapArray = classesMap?.get(instance.id);
|
|
1429
|
+
const classes = classMapArray !== void 0 ? [JSON.stringify(classMapArray.join(" "))] : [];
|
|
1430
|
+
for (const prop of props.values()) {
|
|
1431
|
+
if (prop.instanceId !== instance.id) {
|
|
1432
|
+
continue;
|
|
1433
|
+
}
|
|
1434
|
+
const propValue = generatePropValue({
|
|
1435
|
+
scope,
|
|
1436
|
+
prop,
|
|
1437
|
+
dataSources,
|
|
1438
|
+
usedDataSources
|
|
1439
|
+
});
|
|
1440
|
+
if (isAttributeNameSafe(prop.name) === false) {
|
|
1441
|
+
continue;
|
|
1442
|
+
}
|
|
1443
|
+
if (prop.name === showAttribute) {
|
|
1444
|
+
if (propValue === "true") {
|
|
1445
|
+
continue;
|
|
1446
|
+
}
|
|
1447
|
+
if (propValue === "false") {
|
|
1448
|
+
return "";
|
|
1449
|
+
}
|
|
1450
|
+
conditionValue = propValue;
|
|
1451
|
+
continue;
|
|
1452
|
+
}
|
|
1453
|
+
if (instance.component === collectionComponent) {
|
|
1454
|
+
if (prop.name === "data") {
|
|
1455
|
+
collectionDataValue = propValue;
|
|
1456
|
+
}
|
|
1457
|
+
if (prop.name === "item") {
|
|
1458
|
+
collectionItemValue = propValue;
|
|
1459
|
+
}
|
|
1460
|
+
continue;
|
|
1461
|
+
}
|
|
1462
|
+
if (prop.name === "className" && propValue !== void 0) {
|
|
1463
|
+
classes.push(propValue);
|
|
1464
|
+
continue;
|
|
1465
|
+
}
|
|
1466
|
+
if (propValue !== void 0) {
|
|
1467
|
+
generatedProps += `
|
|
1468
|
+
${prop.name}={${propValue}}`;
|
|
1469
|
+
}
|
|
1470
|
+
}
|
|
1471
|
+
if (classes.length !== 0) {
|
|
1472
|
+
generatedProps += `
|
|
1473
|
+
className={${classes.join(` + " " + `)}}`;
|
|
1474
|
+
}
|
|
1475
|
+
let generatedElement = "";
|
|
1476
|
+
if (instance.component === blockTemplateComponent) {
|
|
1477
|
+
return "";
|
|
1478
|
+
}
|
|
1479
|
+
if (instance.component === collectionComponent) {
|
|
1480
|
+
if (collectionDataValue === void 0 || collectionItemValue === void 0) {
|
|
1481
|
+
return "";
|
|
1482
|
+
}
|
|
1483
|
+
const indexVariable = scope.getName(`${instance.id}-index`, "index");
|
|
1484
|
+
generatedElement += `{${collectionDataValue}?.map((${collectionItemValue}: any, ${indexVariable}: number) =>
|
|
1485
|
+
`;
|
|
1486
|
+
generatedElement += `<Fragment key={${indexVariable}}>
|
|
1487
|
+
`;
|
|
1488
|
+
generatedElement += children;
|
|
1489
|
+
generatedElement += `</Fragment>
|
|
1490
|
+
`;
|
|
1491
|
+
generatedElement += `)}
|
|
1492
|
+
`;
|
|
1493
|
+
} else if (instance.component === blockComponent) {
|
|
1494
|
+
generatedElement += children;
|
|
1495
|
+
} else {
|
|
1496
|
+
const [_namespace, shortName] = parseComponentName2(instance.component);
|
|
1497
|
+
const componentVariable = scope.getName(instance.component, shortName);
|
|
1498
|
+
if (instance.children.length === 0) {
|
|
1499
|
+
generatedElement += `<${componentVariable}${generatedProps} />
|
|
1500
|
+
`;
|
|
1501
|
+
} else {
|
|
1502
|
+
generatedElement += `<${componentVariable}${generatedProps}>
|
|
1503
|
+
`;
|
|
1504
|
+
generatedElement += children;
|
|
1505
|
+
generatedElement += `</${componentVariable}>
|
|
1506
|
+
`;
|
|
1507
|
+
}
|
|
1508
|
+
}
|
|
1509
|
+
if (conditionValue) {
|
|
1510
|
+
let conditionalElement = "";
|
|
1511
|
+
let before = "";
|
|
1512
|
+
let after = "";
|
|
1513
|
+
if (context === "jsx") {
|
|
1514
|
+
before = "{";
|
|
1515
|
+
after = "}";
|
|
1516
|
+
}
|
|
1517
|
+
conditionalElement += `${before}(${conditionValue}) &&
|
|
1518
|
+
`;
|
|
1519
|
+
if (instance.component === collectionComponent) {
|
|
1520
|
+
conditionalElement += "<>\n";
|
|
1521
|
+
conditionalElement += generatedElement;
|
|
1522
|
+
conditionalElement += "</>\n";
|
|
1523
|
+
} else {
|
|
1524
|
+
conditionalElement += generatedElement;
|
|
1525
|
+
}
|
|
1526
|
+
conditionalElement += `${after}
|
|
1527
|
+
`;
|
|
1528
|
+
return conditionalElement;
|
|
1529
|
+
}
|
|
1530
|
+
return generatedElement;
|
|
1531
|
+
};
|
|
1532
|
+
var generateJsxChildren = ({
|
|
1533
|
+
scope,
|
|
1534
|
+
children,
|
|
1535
|
+
instances,
|
|
1536
|
+
props,
|
|
1537
|
+
dataSources,
|
|
1538
|
+
usedDataSources,
|
|
1539
|
+
indexesWithinAncestors,
|
|
1540
|
+
classesMap,
|
|
1541
|
+
excludePlaceholders
|
|
1542
|
+
}) => {
|
|
1543
|
+
let generatedChildren = "";
|
|
1544
|
+
for (const child of children) {
|
|
1545
|
+
if (child.type === "text") {
|
|
1546
|
+
if (excludePlaceholders && child.placeholder === true) {
|
|
1547
|
+
continue;
|
|
1548
|
+
}
|
|
1549
|
+
generatedChildren += child.value.split("\n").map((line) => `{${JSON.stringify(line)}}
|
|
1550
|
+
`).join(`<br />
|
|
1551
|
+
`);
|
|
1552
|
+
continue;
|
|
1553
|
+
}
|
|
1554
|
+
if (child.type === "expression") {
|
|
1555
|
+
const expression = generateExpression({
|
|
1556
|
+
expression: child.value,
|
|
1557
|
+
dataSources,
|
|
1558
|
+
usedDataSources,
|
|
1559
|
+
scope
|
|
1560
|
+
});
|
|
1561
|
+
generatedChildren = `{${expression}}
|
|
1562
|
+
`;
|
|
1563
|
+
continue;
|
|
1564
|
+
}
|
|
1565
|
+
if (child.type === "id") {
|
|
1566
|
+
const instanceId = child.value;
|
|
1567
|
+
const instance = instances.get(instanceId);
|
|
1568
|
+
if (instance === void 0) {
|
|
1569
|
+
continue;
|
|
1570
|
+
}
|
|
1571
|
+
generatedChildren += generateJsxElement({
|
|
1572
|
+
context: "jsx",
|
|
1573
|
+
scope,
|
|
1574
|
+
instance,
|
|
1575
|
+
props,
|
|
1576
|
+
dataSources,
|
|
1577
|
+
usedDataSources,
|
|
1578
|
+
indexesWithinAncestors,
|
|
1579
|
+
classesMap,
|
|
1580
|
+
children: generateJsxChildren({
|
|
1581
|
+
classesMap,
|
|
1582
|
+
scope,
|
|
1583
|
+
children: instance.children,
|
|
1584
|
+
instances,
|
|
1585
|
+
props,
|
|
1586
|
+
dataSources,
|
|
1587
|
+
usedDataSources,
|
|
1588
|
+
indexesWithinAncestors,
|
|
1589
|
+
excludePlaceholders
|
|
1590
|
+
})
|
|
1591
|
+
});
|
|
1592
|
+
continue;
|
|
1593
|
+
}
|
|
1594
|
+
child;
|
|
1595
|
+
}
|
|
1596
|
+
return generatedChildren;
|
|
1597
|
+
};
|
|
1598
|
+
var generateWebstudioComponent = ({
|
|
1599
|
+
scope,
|
|
1600
|
+
name,
|
|
1601
|
+
rootInstanceId,
|
|
1602
|
+
parameters,
|
|
1603
|
+
instances,
|
|
1604
|
+
props,
|
|
1605
|
+
dataSources,
|
|
1606
|
+
indexesWithinAncestors,
|
|
1607
|
+
classesMap
|
|
1608
|
+
}) => {
|
|
1609
|
+
const instance = instances.get(rootInstanceId);
|
|
1610
|
+
if (instance === void 0) {
|
|
1611
|
+
return "";
|
|
1612
|
+
}
|
|
1613
|
+
const usedDataSources = /* @__PURE__ */ new Map();
|
|
1614
|
+
const generatedJsx = generateJsxElement({
|
|
1615
|
+
context: "expression",
|
|
1616
|
+
scope,
|
|
1617
|
+
instance,
|
|
1618
|
+
props,
|
|
1619
|
+
dataSources,
|
|
1620
|
+
usedDataSources,
|
|
1621
|
+
indexesWithinAncestors,
|
|
1622
|
+
classesMap,
|
|
1623
|
+
children: generateJsxChildren({
|
|
1624
|
+
scope,
|
|
1625
|
+
children: instance.children,
|
|
1626
|
+
instances,
|
|
1627
|
+
props,
|
|
1628
|
+
dataSources,
|
|
1629
|
+
usedDataSources,
|
|
1630
|
+
indexesWithinAncestors,
|
|
1631
|
+
classesMap
|
|
1632
|
+
})
|
|
1633
|
+
});
|
|
1634
|
+
let generatedProps = "";
|
|
1635
|
+
if (parameters.length > 0) {
|
|
1636
|
+
let generatedPropsValue = "{ ";
|
|
1637
|
+
let generatedPropsType = "{ ";
|
|
1638
|
+
for (const parameter of parameters) {
|
|
1639
|
+
const dataSource = usedDataSources.get(parameter.value);
|
|
1640
|
+
if (dataSource) {
|
|
1641
|
+
const valueName = scope.getName(dataSource.id, dataSource.name);
|
|
1642
|
+
generatedPropsValue += `${parameter.name}: ${valueName}, `;
|
|
1643
|
+
}
|
|
1644
|
+
generatedPropsType += `${parameter.name}: any; `;
|
|
1645
|
+
}
|
|
1646
|
+
generatedPropsValue += `}`;
|
|
1647
|
+
generatedPropsType += `}`;
|
|
1648
|
+
generatedProps = `${generatedPropsValue}: ${generatedPropsType}`;
|
|
1649
|
+
}
|
|
1650
|
+
let generatedDataSources = "";
|
|
1651
|
+
for (const dataSource of usedDataSources.values()) {
|
|
1652
|
+
if (dataSource.type === "variable") {
|
|
1653
|
+
const valueName = scope.getName(dataSource.id, dataSource.name);
|
|
1654
|
+
const setterName = scope.getName(
|
|
1655
|
+
`set$${dataSource.id}`,
|
|
1656
|
+
`set$${dataSource.name}`
|
|
1657
|
+
);
|
|
1658
|
+
const initialValue = dataSource.value.value;
|
|
1659
|
+
const initialValueString = JSON.stringify(initialValue);
|
|
1660
|
+
generatedDataSources += `let [${valueName}, ${setterName}] = useVariableState<any>(${initialValueString})
|
|
1661
|
+
`;
|
|
1662
|
+
}
|
|
1663
|
+
if (dataSource.type === "resource") {
|
|
1664
|
+
const valueName = scope.getName(dataSource.id, dataSource.name);
|
|
1665
|
+
const resourceName = scope.getName(
|
|
1666
|
+
dataSource.resourceId,
|
|
1667
|
+
dataSource.name
|
|
1668
|
+
);
|
|
1669
|
+
const resourceNameString = JSON.stringify(resourceName);
|
|
1670
|
+
generatedDataSources += `let ${valueName} = useResource(${resourceNameString})
|
|
1671
|
+
`;
|
|
1672
|
+
}
|
|
1673
|
+
}
|
|
1674
|
+
let generatedComponent = "";
|
|
1675
|
+
generatedComponent += `const ${name} = (${generatedProps}) => {
|
|
1676
|
+
`;
|
|
1677
|
+
generatedComponent += `${generatedDataSources}`;
|
|
1678
|
+
generatedComponent += `return ${generatedJsx}`;
|
|
1679
|
+
generatedComponent += `}
|
|
1680
|
+
`;
|
|
1681
|
+
return generatedComponent;
|
|
1682
|
+
};
|
|
1683
|
+
export {
|
|
1684
|
+
EmbedTemplateInstance,
|
|
1685
|
+
EmbedTemplateProp,
|
|
1686
|
+
EmbedTemplateStyleDecl,
|
|
1687
|
+
PropMeta,
|
|
1688
|
+
WsComponentMeta,
|
|
1689
|
+
WsEmbedTemplate,
|
|
1690
|
+
addGlobalRules,
|
|
1691
|
+
blockComponent,
|
|
1692
|
+
blockTemplateComponent,
|
|
1693
|
+
blockTemplateMeta,
|
|
1694
|
+
collapsedAttribute,
|
|
1695
|
+
collectionComponent,
|
|
1696
|
+
componentAttribute,
|
|
1697
|
+
componentCategories,
|
|
1698
|
+
coreMetas,
|
|
1699
|
+
corePropsMetas,
|
|
1700
|
+
createImageValueTransformer,
|
|
1701
|
+
defaultStates,
|
|
1702
|
+
descendantComponent,
|
|
1703
|
+
generateCss,
|
|
1704
|
+
generateDataFromEmbedTemplate,
|
|
1705
|
+
generateJsxChildren,
|
|
1706
|
+
generateJsxElement,
|
|
1707
|
+
generateRemixParams,
|
|
1708
|
+
generateRemixRoute,
|
|
1709
|
+
generateWebstudioComponent,
|
|
1710
|
+
getIndexesWithinAncestors,
|
|
1711
|
+
idAttribute,
|
|
1712
|
+
indexAttribute,
|
|
1713
|
+
isAttributeNameSafe,
|
|
1714
|
+
isCoreComponent,
|
|
1715
|
+
namespaceMeta,
|
|
1716
|
+
normalizeProps,
|
|
1717
|
+
portalComponent,
|
|
1718
|
+
rootComponent,
|
|
1719
|
+
selectorIdAttribute,
|
|
1720
|
+
showAttribute,
|
|
1721
|
+
stateCategories,
|
|
1722
|
+
textContentAttribute
|
|
1723
|
+
};
|