@webstudio-is/react-sdk 0.0.0-017f1bd

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 ADDED
@@ -0,0 +1,845 @@
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/props.ts
58
+ import {
59
+ getPagePath,
60
+ findPageByIdOrPath
61
+ } from "@webstudio-is/sdk";
62
+ var normalizeProps = ({
63
+ props,
64
+ assetBaseUrl,
65
+ assets,
66
+ uploadingImageAssets,
67
+ pages,
68
+ source
69
+ }) => {
70
+ const newProps = [];
71
+ for (const prop of props) {
72
+ if (prop.type === "asset") {
73
+ const assetId = prop.value;
74
+ const asset = assets.get(assetId) ?? uploadingImageAssets.find((asset2) => asset2.id === assetId);
75
+ if (asset === void 0) {
76
+ continue;
77
+ }
78
+ const propBase = {
79
+ id: prop.id,
80
+ name: prop.name,
81
+ required: prop.required,
82
+ instanceId: prop.instanceId
83
+ };
84
+ if (prop.name === "width" && asset.type === "image") {
85
+ newProps.push({
86
+ ...propBase,
87
+ type: "number",
88
+ value: asset.meta.width
89
+ });
90
+ continue;
91
+ }
92
+ if (prop.name === "height" && asset.type === "image") {
93
+ newProps.push({
94
+ ...propBase,
95
+ type: "number",
96
+ value: asset.meta.height
97
+ });
98
+ continue;
99
+ }
100
+ newProps.push({
101
+ ...propBase,
102
+ type: "string",
103
+ value: `${assetBaseUrl}${asset.name}`
104
+ });
105
+ if (source === "canvas") {
106
+ newProps.push({
107
+ id: `${prop.instanceId}-${asset.id}-assetId`,
108
+ name: "$webstudio$canvasOnly$assetId",
109
+ required: false,
110
+ instanceId: prop.instanceId,
111
+ type: "string",
112
+ value: asset.id
113
+ });
114
+ }
115
+ continue;
116
+ }
117
+ if (prop.type === "page") {
118
+ let idProp;
119
+ const pageId = typeof prop.value === "string" ? prop.value : prop.value.pageId;
120
+ const page = findPageByIdOrPath(pageId, pages);
121
+ if (page === void 0) {
122
+ continue;
123
+ }
124
+ if (typeof prop.value !== "string") {
125
+ const { instanceId } = prop.value;
126
+ idProp = props.find(
127
+ (prop2) => prop2.instanceId === instanceId && prop2.name === "id"
128
+ );
129
+ }
130
+ const path = getPagePath(page.id, pages);
131
+ const url = new URL(path, "https://any-valid.url");
132
+ let value = url.pathname;
133
+ if (idProp?.type === "string") {
134
+ const hash = idProp.value;
135
+ url.hash = encodeURIComponent(hash);
136
+ value = `${url.pathname}${url.hash}`;
137
+ }
138
+ newProps.push({
139
+ id: prop.id,
140
+ name: prop.name,
141
+ required: prop.required,
142
+ instanceId: prop.instanceId,
143
+ type: "string",
144
+ value
145
+ });
146
+ continue;
147
+ }
148
+ newProps.push(prop);
149
+ }
150
+ return newProps;
151
+ };
152
+ var idAttribute = "data-ws-id";
153
+ var selectorIdAttribute = "data-ws-selector";
154
+ var componentAttribute = "data-ws-component";
155
+ var showAttribute = "data-ws-show";
156
+ var collapsedAttribute = "data-ws-collapsed";
157
+ var textContentAttribute = "data-ws-text-content";
158
+ 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";
159
+ var attributeNameChar = attributeNameStartChar + ":\\-0-9\\u00B7\\u0300-\\u036F\\u203F-\\u2040";
160
+ var validAttributeNameRegex = new RegExp(
161
+ // eslint-disable-next-line no-misleading-character-class
162
+ "^[" + attributeNameStartChar + "][" + attributeNameChar + "]*$"
163
+ );
164
+ var illegalAttributeNameCache = /* @__PURE__ */ new Map();
165
+ var validatedAttributeNameCache = /* @__PURE__ */ new Map();
166
+ var isAttributeNameSafe = (attributeName) => {
167
+ if (validatedAttributeNameCache.has(attributeName)) {
168
+ return true;
169
+ }
170
+ if (illegalAttributeNameCache.has(attributeName)) {
171
+ return false;
172
+ }
173
+ if (validAttributeNameRegex.test(attributeName)) {
174
+ validatedAttributeNameCache.set(attributeName, true);
175
+ return true;
176
+ }
177
+ illegalAttributeNameCache.set(attributeName, true);
178
+ return false;
179
+ };
180
+
181
+ // src/component-generator.ts
182
+ import {
183
+ parseComponentName,
184
+ generateExpression,
185
+ decodeDataSourceVariable,
186
+ transpileExpression,
187
+ blockComponent,
188
+ blockTemplateComponent,
189
+ collectionComponent,
190
+ descendantComponent,
191
+ getIndexesWithinAncestors,
192
+ elementComponent
193
+ } from "@webstudio-is/sdk";
194
+ import { indexProperty, tagProperty } from "@webstudio-is/sdk/runtime";
195
+
196
+ // src/__generated__/standard-attributes.ts
197
+ var standardAttributesToReactProps = {
198
+ "accept-charset": "acceptCharset",
199
+ accesskey: "accessKey",
200
+ allowfullscreen: "allowFullScreen",
201
+ autocapitalize: "autoCapitalize",
202
+ autocomplete: "autoComplete",
203
+ autocorrect: "autoCorrect",
204
+ autofocus: "autoFocus",
205
+ autoplay: "autoPlay",
206
+ charset: "charSet",
207
+ class: "className",
208
+ colspan: "colSpan",
209
+ contenteditable: "contentEditable",
210
+ crossorigin: "crossOrigin",
211
+ datetime: "dateTime",
212
+ enctype: "encType",
213
+ enterkeyhint: "enterKeyHint",
214
+ fetchpriority: "fetchPriority",
215
+ for: "htmlFor",
216
+ formmethod: "formMethod",
217
+ formaction: "formAction",
218
+ formenctype: "formEncType",
219
+ formnovalidate: "formNoValidate",
220
+ formtarget: "formTarget",
221
+ hreflang: "hrefLang",
222
+ "http-equiv": "httpEquiv",
223
+ imagesizes: "imageSizes",
224
+ imagesrcset: "imageSrcSet",
225
+ inputmode: "inputMode",
226
+ itemid: "itemID",
227
+ itemprop: "itemProp",
228
+ itemref: "itemRef",
229
+ itemscope: "itemScope",
230
+ itemtype: "itemType",
231
+ maxlength: "maxLength",
232
+ minlength: "minLength",
233
+ nomodule: "noModule",
234
+ novalidate: "noValidate",
235
+ playsinline: "playsInline",
236
+ readonly: "readOnly",
237
+ referrerpolicy: "referrerPolicy",
238
+ rowspan: "rowSpan",
239
+ spellcheck: "spellCheck",
240
+ srcdoc: "srcDoc",
241
+ srclang: "srcLang",
242
+ srcset: "srcSet",
243
+ tabindex: "tabIndex",
244
+ usemap: "useMap",
245
+ "alignment-baseline": "alignmentBaseline",
246
+ "baseline-shift": "baselineShift",
247
+ "clip-path": "clipPath",
248
+ "clip-rule": "clipRule",
249
+ "color-interpolation": "colorInterpolation",
250
+ "color-interpolation-filters": "colorInterpolationFilters",
251
+ "color-profile": "colorProfile",
252
+ "color-rendering": "colorRendering",
253
+ "dominant-baseline": "dominantBaseline",
254
+ "enable-background": "enableBackground",
255
+ "fill-opacity": "fillOpacity",
256
+ "fill-rule": "fillRule",
257
+ "flood-opacity": "floodOpacity",
258
+ "flood-color": "floodColor",
259
+ "font-family": "fontFamily",
260
+ "font-size": "fontSize",
261
+ "font-size-adjust": "fontSizeAdjust",
262
+ "font-stretch": "fontStretch",
263
+ "font-style": "fontStyle",
264
+ "font-variant": "fontVariant",
265
+ "font-weight": "fontWeight",
266
+ "glyph-orientation-horizontal": "glyphOrientationHorizontal",
267
+ "glyph-orientation-vertical": "glyphOrientationVertical",
268
+ "image-rendering": "imageRendering",
269
+ "letter-spacing": "letterSpacing",
270
+ "lighting-color": "lightingColor",
271
+ "marker-end": "markerEnd",
272
+ "marker-mid": "markerMid",
273
+ "marker-start": "markerStart",
274
+ "pointer-events": "pointerEvents",
275
+ popovertarget: "popoverTarget",
276
+ popovertargetaction: "popoverTargetAction",
277
+ "shape-rendering": "shapeRendering",
278
+ "stop-color": "stopColor",
279
+ "stop-opacity": "stopOpacity",
280
+ "stroke-dasharray": "strokeDasharray",
281
+ "stroke-dashoffset": "strokeDashoffset",
282
+ "stroke-linecap": "strokeLinecap",
283
+ "stroke-linejoin": "strokeLinejoin",
284
+ "stroke-miterlimit": "strokeMiterlimit",
285
+ "stroke-width": "strokeWidth",
286
+ "stroke-opacity": "strokeOpacity",
287
+ "text-anchor": "textAnchor",
288
+ "text-decoration": "textDecoration",
289
+ "text-rendering": "textRendering",
290
+ "unicode-bidi": "unicodeBidi",
291
+ "word-spacing": "wordSpacing",
292
+ "writing-mode": "writingMode",
293
+ "xlink:actuate": "xlinkActuate",
294
+ "xlink:arcrole": "xlinkArcrole",
295
+ "xlink:href": "xlinkHref",
296
+ "xlink:role": "xlinkRole",
297
+ "xlink:show": "xlinkShow",
298
+ "xlink:title": "xlinkTitle",
299
+ "xlink:type": "xlinkType",
300
+ "xml:base": "xmlBase",
301
+ "xml:lang": "xmlLang",
302
+ "xml:space": "xmlSpace",
303
+ dirname: "dirName"
304
+ };
305
+ var reactPropsToStandardAttributes = {
306
+ acceptCharset: "accept-charset",
307
+ accessKey: "accesskey",
308
+ allowFullScreen: "allowfullscreen",
309
+ autoCapitalize: "autocapitalize",
310
+ autoComplete: "autocomplete",
311
+ autoCorrect: "autocorrect",
312
+ autoFocus: "autofocus",
313
+ autoPlay: "autoplay",
314
+ charSet: "charset",
315
+ className: "class",
316
+ colSpan: "colspan",
317
+ contentEditable: "contenteditable",
318
+ crossOrigin: "crossorigin",
319
+ dateTime: "datetime",
320
+ encType: "enctype",
321
+ enterKeyHint: "enterkeyhint",
322
+ fetchPriority: "fetchpriority",
323
+ htmlFor: "for",
324
+ formMethod: "formmethod",
325
+ formAction: "formaction",
326
+ formEncType: "formenctype",
327
+ formNoValidate: "formnovalidate",
328
+ formTarget: "formtarget",
329
+ hrefLang: "hreflang",
330
+ httpEquiv: "http-equiv",
331
+ imageSizes: "imagesizes",
332
+ imageSrcSet: "imagesrcset",
333
+ inputMode: "inputmode",
334
+ itemID: "itemid",
335
+ itemProp: "itemprop",
336
+ itemRef: "itemref",
337
+ itemScope: "itemscope",
338
+ itemType: "itemtype",
339
+ maxLength: "maxlength",
340
+ minLength: "minlength",
341
+ noModule: "nomodule",
342
+ noValidate: "novalidate",
343
+ playsInline: "playsinline",
344
+ readOnly: "readonly",
345
+ referrerPolicy: "referrerpolicy",
346
+ rowSpan: "rowspan",
347
+ spellCheck: "spellcheck",
348
+ srcDoc: "srcdoc",
349
+ srcLang: "srclang",
350
+ srcSet: "srcset",
351
+ tabIndex: "tabindex",
352
+ useMap: "usemap",
353
+ alignmentBaseline: "alignment-baseline",
354
+ baselineShift: "baseline-shift",
355
+ clipPath: "clip-path",
356
+ clipRule: "clip-rule",
357
+ colorInterpolation: "color-interpolation",
358
+ colorInterpolationFilters: "color-interpolation-filters",
359
+ colorProfile: "color-profile",
360
+ colorRendering: "color-rendering",
361
+ dominantBaseline: "dominant-baseline",
362
+ enableBackground: "enable-background",
363
+ fillOpacity: "fill-opacity",
364
+ fillRule: "fill-rule",
365
+ floodOpacity: "flood-opacity",
366
+ floodColor: "flood-color",
367
+ fontFamily: "font-family",
368
+ fontSize: "font-size",
369
+ fontSizeAdjust: "font-size-adjust",
370
+ fontStretch: "font-stretch",
371
+ fontStyle: "font-style",
372
+ fontVariant: "font-variant",
373
+ fontWeight: "font-weight",
374
+ glyphOrientationHorizontal: "glyph-orientation-horizontal",
375
+ glyphOrientationVertical: "glyph-orientation-vertical",
376
+ imageRendering: "image-rendering",
377
+ letterSpacing: "letter-spacing",
378
+ lightingColor: "lighting-color",
379
+ markerEnd: "marker-end",
380
+ markerMid: "marker-mid",
381
+ markerStart: "marker-start",
382
+ pointerEvents: "pointer-events",
383
+ popoverTarget: "popovertarget",
384
+ popoverTargetAction: "popovertargetaction",
385
+ shapeRendering: "shape-rendering",
386
+ stopColor: "stop-color",
387
+ stopOpacity: "stop-opacity",
388
+ strokeDasharray: "stroke-dasharray",
389
+ strokeDashoffset: "stroke-dashoffset",
390
+ strokeLinecap: "stroke-linecap",
391
+ strokeLinejoin: "stroke-linejoin",
392
+ strokeMiterlimit: "stroke-miterlimit",
393
+ strokeWidth: "stroke-width",
394
+ strokeOpacity: "stroke-opacity",
395
+ textAnchor: "text-anchor",
396
+ textDecoration: "text-decoration",
397
+ textRendering: "text-rendering",
398
+ unicodeBidi: "unicode-bidi",
399
+ wordSpacing: "word-spacing",
400
+ writingMode: "writing-mode",
401
+ xlinkActuate: "xlink:actuate",
402
+ xlinkArcrole: "xlink:arcrole",
403
+ xlinkHref: "xlink:href",
404
+ xlinkRole: "xlink:role",
405
+ xlinkShow: "xlink:show",
406
+ xlinkTitle: "xlink:title",
407
+ xlinkType: "xlink:type",
408
+ xmlBase: "xml:base",
409
+ xmlLang: "xml:lang",
410
+ xmlSpace: "xml:space",
411
+ dirName: "dirname"
412
+ };
413
+
414
+ // src/component-generator.ts
415
+ var generateAction = ({
416
+ scope,
417
+ prop,
418
+ dataSources,
419
+ usedDataSources
420
+ }) => {
421
+ const setters = /* @__PURE__ */ new Set();
422
+ let args = [];
423
+ let assignersCode = "";
424
+ for (const value of prop.value) {
425
+ args = value.args;
426
+ assignersCode += transpileExpression({
427
+ expression: value.code,
428
+ executable: true,
429
+ replaceVariable: (identifier, assignee) => {
430
+ if (args?.includes(identifier)) {
431
+ return;
432
+ }
433
+ const depId = decodeDataSourceVariable(identifier);
434
+ const dep = depId ? dataSources.get(depId) : void 0;
435
+ if (dep) {
436
+ usedDataSources.set(dep.id, dep);
437
+ if (assignee) {
438
+ setters.add(dep);
439
+ }
440
+ const valueName = scope.getName(dep.id, dep.name);
441
+ return valueName;
442
+ }
443
+ console.error(`Unknown dependency "${identifier}"`);
444
+ }
445
+ });
446
+ assignersCode += `
447
+ `;
448
+ }
449
+ let settersCode = "";
450
+ for (const dataSource of setters) {
451
+ const valueName = scope.getName(dataSource.id, dataSource.name);
452
+ const setterName = scope.getName(
453
+ `set$${dataSource.id}`,
454
+ `set$${dataSource.name}`
455
+ );
456
+ settersCode += `${setterName}(${valueName})
457
+ `;
458
+ }
459
+ const argsList = args.map((arg) => `${arg}: any`).join(", ");
460
+ let generated = "";
461
+ generated += `(${argsList}) => {
462
+ `;
463
+ generated += assignersCode;
464
+ generated += settersCode;
465
+ generated += `}`;
466
+ return generated;
467
+ };
468
+ var generatePropValue = ({
469
+ scope,
470
+ prop,
471
+ dataSources,
472
+ usedDataSources
473
+ }) => {
474
+ if (prop.type === "asset" || prop.type === "page") {
475
+ return;
476
+ }
477
+ if (prop.type === "string" || prop.type === "number" || prop.type === "boolean" || prop.type === "string[]" || prop.type === "json" || prop.type === "animationAction") {
478
+ return JSON.stringify(prop.value);
479
+ }
480
+ if (prop.type === "parameter") {
481
+ const dataSource = dataSources.get(prop.value);
482
+ if (dataSource === void 0) {
483
+ return;
484
+ }
485
+ usedDataSources.set(dataSource.id, dataSource);
486
+ return scope.getName(dataSource.id, dataSource.name);
487
+ }
488
+ if (prop.type === "expression") {
489
+ return generateExpression({
490
+ expression: prop.value,
491
+ dataSources,
492
+ usedDataSources,
493
+ scope
494
+ });
495
+ }
496
+ if (prop.type === "action") {
497
+ return generateAction({ scope, prop, dataSources, usedDataSources });
498
+ }
499
+ if (prop.type === "resource") {
500
+ return JSON.stringify(scope.getName(prop.value, prop.name));
501
+ }
502
+ prop;
503
+ };
504
+ var generateJsxElement = ({
505
+ context = "jsx",
506
+ scope,
507
+ metas,
508
+ tagsOverrides,
509
+ instance,
510
+ props,
511
+ dataSources,
512
+ usedDataSources,
513
+ indexesWithinAncestors,
514
+ children,
515
+ classesMap
516
+ }) => {
517
+ if (instance.component === descendantComponent) {
518
+ return "";
519
+ }
520
+ const meta = metas.get(instance.component);
521
+ const hasTags = Object.keys(meta?.presetStyle ?? {}).length > 0;
522
+ let generatedProps = "";
523
+ const index = indexesWithinAncestors.get(instance.id);
524
+ if (index !== void 0) {
525
+ generatedProps += `
526
+ ${indexProperty}="${index}"`;
527
+ }
528
+ if (instance.tag !== void 0 && instance.component !== elementComponent) {
529
+ generatedProps += `
530
+ ${tagProperty}=${JSON.stringify(instance.tag)}`;
531
+ }
532
+ let conditionValue;
533
+ let collectionDataValue;
534
+ let collectionItemValue;
535
+ let classNameValue;
536
+ for (const prop of props.values()) {
537
+ if (prop.instanceId !== instance.id) {
538
+ continue;
539
+ }
540
+ const propValue = generatePropValue({
541
+ scope,
542
+ prop,
543
+ dataSources,
544
+ usedDataSources
545
+ });
546
+ if (isAttributeNameSafe(prop.name) === false) {
547
+ continue;
548
+ }
549
+ let name = prop.name;
550
+ if (hasTags && !meta?.props?.[prop.name]) {
551
+ name = standardAttributesToReactProps[prop.name] ?? prop.name;
552
+ }
553
+ if (prop.name === showAttribute) {
554
+ if (propValue === "true") {
555
+ continue;
556
+ }
557
+ if (propValue === "false") {
558
+ return "";
559
+ }
560
+ conditionValue = propValue;
561
+ continue;
562
+ }
563
+ if (instance.component === collectionComponent) {
564
+ if (prop.name === "data") {
565
+ collectionDataValue = propValue;
566
+ }
567
+ if (prop.name === "item") {
568
+ collectionItemValue = propValue;
569
+ }
570
+ continue;
571
+ }
572
+ if (name === "className" && propValue !== void 0) {
573
+ classNameValue = propValue;
574
+ continue;
575
+ }
576
+ if (propValue !== void 0) {
577
+ generatedProps += `
578
+ ${name}={${propValue}}`;
579
+ }
580
+ }
581
+ const classMapArray = classesMap?.get(instance.id);
582
+ if (classMapArray || classNameValue) {
583
+ let classNameTemplate = classMapArray ? classMapArray.join(" ") : "";
584
+ if (classNameValue) {
585
+ if (classNameTemplate) {
586
+ classNameTemplate += " ";
587
+ }
588
+ classNameTemplate += "${" + classNameValue + "}";
589
+ }
590
+ generatedProps += "\nclassName={`" + classNameTemplate + "`}";
591
+ }
592
+ let generatedElement = "";
593
+ if (instance.component === blockTemplateComponent) {
594
+ return "";
595
+ }
596
+ if (instance.component === collectionComponent) {
597
+ if (collectionDataValue === void 0 || collectionItemValue === void 0) {
598
+ return "";
599
+ }
600
+ const indexVariable = scope.getName(`${instance.id}-index`, "index");
601
+ generatedElement += `{${collectionDataValue}?.map?.((${collectionItemValue}: any, ${indexVariable}: number) =>
602
+ `;
603
+ generatedElement += `<Fragment key={${indexVariable}}>
604
+ `;
605
+ generatedElement += children;
606
+ generatedElement += `</Fragment>
607
+ `;
608
+ generatedElement += `)}
609
+ `;
610
+ } else if (instance.component === blockComponent) {
611
+ generatedElement += children;
612
+ } else {
613
+ let componentVariable;
614
+ if (instance.component === elementComponent) {
615
+ componentVariable = instance.tag ?? "div";
616
+ const componentDescriptor = tagsOverrides?.[componentVariable];
617
+ if (componentDescriptor !== void 0) {
618
+ const [_importSource, importSpecifier] = componentDescriptor.split(":");
619
+ componentVariable = scope.getName(componentDescriptor, importSpecifier);
620
+ }
621
+ } else {
622
+ const [_namespace, shortName] = parseComponentName(instance.component);
623
+ componentVariable = scope.getName(instance.component, shortName);
624
+ }
625
+ if (instance.children.length === 0) {
626
+ generatedElement += `<${componentVariable}${generatedProps} />
627
+ `;
628
+ } else {
629
+ generatedElement += `<${componentVariable}${generatedProps}>
630
+ `;
631
+ generatedElement += children;
632
+ generatedElement += `</${componentVariable}>
633
+ `;
634
+ }
635
+ }
636
+ if (conditionValue) {
637
+ let conditionalElement = "";
638
+ let before = "";
639
+ let after = "";
640
+ if (context === "jsx") {
641
+ before = "{";
642
+ after = "}";
643
+ }
644
+ conditionalElement += `${before}(${conditionValue}) &&
645
+ `;
646
+ if (instance.component === collectionComponent) {
647
+ conditionalElement += "<>\n";
648
+ conditionalElement += generatedElement;
649
+ conditionalElement += "</>\n";
650
+ } else {
651
+ conditionalElement += generatedElement;
652
+ }
653
+ conditionalElement += `${after}
654
+ `;
655
+ return conditionalElement;
656
+ }
657
+ return generatedElement;
658
+ };
659
+ var generateJsxChildren = ({
660
+ scope,
661
+ metas,
662
+ tagsOverrides,
663
+ children,
664
+ instances,
665
+ props,
666
+ dataSources,
667
+ usedDataSources,
668
+ indexesWithinAncestors,
669
+ classesMap,
670
+ excludePlaceholders
671
+ }) => {
672
+ let generatedChildren = "";
673
+ for (const child of children) {
674
+ if (child.type === "text") {
675
+ if (excludePlaceholders && child.placeholder === true) {
676
+ continue;
677
+ }
678
+ generatedChildren += child.value.split("\n").map((line) => `{${JSON.stringify(line)}}
679
+ `).join(`<br />
680
+ `);
681
+ continue;
682
+ }
683
+ if (child.type === "expression") {
684
+ const expression = generateExpression({
685
+ expression: child.value,
686
+ dataSources,
687
+ usedDataSources,
688
+ scope
689
+ });
690
+ generatedChildren = `{${expression}}
691
+ `;
692
+ continue;
693
+ }
694
+ if (child.type === "id") {
695
+ const instanceId = child.value;
696
+ const instance = instances.get(instanceId);
697
+ if (instance === void 0) {
698
+ continue;
699
+ }
700
+ generatedChildren += generateJsxElement({
701
+ context: "jsx",
702
+ scope,
703
+ metas,
704
+ tagsOverrides,
705
+ instance,
706
+ props,
707
+ dataSources,
708
+ usedDataSources,
709
+ indexesWithinAncestors,
710
+ classesMap,
711
+ children: generateJsxChildren({
712
+ classesMap,
713
+ scope,
714
+ metas,
715
+ tagsOverrides,
716
+ children: instance.children,
717
+ instances,
718
+ props,
719
+ dataSources,
720
+ usedDataSources,
721
+ indexesWithinAncestors,
722
+ excludePlaceholders
723
+ })
724
+ });
725
+ continue;
726
+ }
727
+ child;
728
+ }
729
+ return generatedChildren;
730
+ };
731
+ var generateWebstudioComponent = ({
732
+ scope,
733
+ name,
734
+ rootInstanceId,
735
+ parameters,
736
+ instances,
737
+ props,
738
+ dataSources,
739
+ metas,
740
+ tagsOverrides,
741
+ classesMap
742
+ }) => {
743
+ const instance = instances.get(rootInstanceId);
744
+ const indexesWithinAncestors = getIndexesWithinAncestors(metas, instances, [
745
+ rootInstanceId
746
+ ]);
747
+ const usedDataSources = /* @__PURE__ */ new Map();
748
+ let generatedJsx = "<></>\n";
749
+ if (instance) {
750
+ generatedJsx = generateJsxElement({
751
+ context: "expression",
752
+ scope,
753
+ metas,
754
+ tagsOverrides,
755
+ instance,
756
+ props,
757
+ dataSources,
758
+ usedDataSources,
759
+ indexesWithinAncestors,
760
+ classesMap,
761
+ children: generateJsxChildren({
762
+ scope,
763
+ metas,
764
+ tagsOverrides,
765
+ children: instance.children,
766
+ instances,
767
+ props,
768
+ dataSources,
769
+ usedDataSources,
770
+ indexesWithinAncestors,
771
+ classesMap
772
+ })
773
+ });
774
+ }
775
+ let generatedProps = "";
776
+ let generatedParameters = "";
777
+ const uniqueParameters = new Set(
778
+ parameters.map((parameter) => parameter.name)
779
+ );
780
+ if (parameters.length > 0) {
781
+ let generatedPropsType = "";
782
+ for (const parameterName of uniqueParameters) {
783
+ generatedPropsType += `${parameterName}: any; `;
784
+ }
785
+ generatedProps = `_props: { ${generatedPropsType}}`;
786
+ for (const parameter of parameters) {
787
+ const dataSource = usedDataSources.get(parameter.value);
788
+ if (dataSource) {
789
+ const valueName = scope.getName(dataSource.id, dataSource.name);
790
+ generatedParameters += `const ${valueName} = _props.${parameter.name};
791
+ `;
792
+ }
793
+ }
794
+ }
795
+ let generatedDataSources = "";
796
+ for (const dataSource of usedDataSources.values()) {
797
+ if (dataSource.type === "variable") {
798
+ const valueName = scope.getName(dataSource.id, dataSource.name);
799
+ const setterName = scope.getName(
800
+ `set$${dataSource.id}`,
801
+ `set$${dataSource.name}`
802
+ );
803
+ const initialValue = dataSource.value.value;
804
+ const initialValueString = JSON.stringify(initialValue);
805
+ generatedDataSources += `let [${valueName}, ${setterName}] = useVariableState<any>(${initialValueString})
806
+ `;
807
+ }
808
+ if (dataSource.type === "resource") {
809
+ const valueName = scope.getName(dataSource.id, dataSource.name);
810
+ const resourceName = scope.getName(
811
+ dataSource.resourceId,
812
+ dataSource.name
813
+ );
814
+ const resourceNameString = JSON.stringify(resourceName);
815
+ generatedDataSources += `let ${valueName} = useResource(${resourceNameString})
816
+ `;
817
+ }
818
+ }
819
+ let generatedComponent = "";
820
+ generatedComponent += `const ${name} = (${generatedProps}) => {
821
+ `;
822
+ generatedComponent += `${generatedParameters}`;
823
+ generatedComponent += `${generatedDataSources}`;
824
+ generatedComponent += `return ${generatedJsx}`;
825
+ generatedComponent += `}
826
+ `;
827
+ return generatedComponent;
828
+ };
829
+ export {
830
+ collapsedAttribute,
831
+ componentAttribute,
832
+ generateJsxChildren,
833
+ generateJsxElement,
834
+ generateRemixParams,
835
+ generateRemixRoute,
836
+ generateWebstudioComponent,
837
+ idAttribute,
838
+ isAttributeNameSafe,
839
+ normalizeProps,
840
+ reactPropsToStandardAttributes,
841
+ selectorIdAttribute,
842
+ showAttribute,
843
+ standardAttributesToReactProps,
844
+ textContentAttribute
845
+ };