@webstudio-is/react-sdk 0.0.0-c575b54 → 0.0.0-ca00e2a
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 +185 -553
- package/lib/runtime.js +5 -5
- package/lib/types/__generated__/standard-attributes.d.ts +2 -0
- package/lib/types/component-generator.d.ts +7 -6
- package/lib/types/context.d.ts +6 -0
- package/lib/types/hook.d.ts +1 -2
- package/lib/types/index.d.ts +1 -3
- package/lib/types/props.d.ts +1337 -8
- package/lib/types/runtime.d.ts +0 -1
- package/package.json +12 -12
- package/placeholder.d.ts +14 -11
- package/lib/types/css/css.d.ts +0 -20
- package/lib/types/css/css.test.d.ts +0 -1
- package/lib/types/css/global-rules.d.ts +0 -6
- package/lib/types/css/index.d.ts +0 -2
- package/lib/types/embed-template.d.ts +0 -54
- package/lib/types/embed-template.test.d.ts +0 -1
- package/lib/types/instance-utils.d.ts +0 -3
- package/lib/types/instance-utils.test.d.ts +0 -1
package/lib/index.js
CHANGED
|
@@ -54,178 +54,6 @@ var generateRemixParams = (pathname) => {
|
|
|
54
54
|
return generated;
|
|
55
55
|
};
|
|
56
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
|
-
descendantComponent,
|
|
82
|
-
rootComponent
|
|
83
|
-
} from "@webstudio-is/sdk";
|
|
84
|
-
import { kebabCase } from "change-case";
|
|
85
|
-
var createImageValueTransformer = (assets, { assetBaseUrl }) => (styleValue) => {
|
|
86
|
-
if (styleValue.type === "image" && styleValue.value.type === "asset") {
|
|
87
|
-
const asset = assets.get(styleValue.value.value);
|
|
88
|
-
if (asset === void 0) {
|
|
89
|
-
return { type: "keyword", value: "none" };
|
|
90
|
-
}
|
|
91
|
-
const url = `${assetBaseUrl}${asset.name}`;
|
|
92
|
-
return {
|
|
93
|
-
type: "image",
|
|
94
|
-
value: {
|
|
95
|
-
type: "url",
|
|
96
|
-
url
|
|
97
|
-
},
|
|
98
|
-
hidden: styleValue.hidden
|
|
99
|
-
};
|
|
100
|
-
}
|
|
101
|
-
};
|
|
102
|
-
var normalizeClassName = (name) => kebabCase(name);
|
|
103
|
-
var generateCss = ({
|
|
104
|
-
assets,
|
|
105
|
-
instances,
|
|
106
|
-
props,
|
|
107
|
-
breakpoints,
|
|
108
|
-
styles,
|
|
109
|
-
styleSourceSelections,
|
|
110
|
-
componentMetas,
|
|
111
|
-
assetBaseUrl,
|
|
112
|
-
atomic
|
|
113
|
-
}) => {
|
|
114
|
-
const globalSheet = createRegularStyleSheet({ name: "ssr" });
|
|
115
|
-
const sheet = createRegularStyleSheet({ name: "ssr" });
|
|
116
|
-
addGlobalRules(globalSheet, { assets, assetBaseUrl });
|
|
117
|
-
globalSheet.addMediaRule("presets");
|
|
118
|
-
const presetClasses = /* @__PURE__ */ new Map();
|
|
119
|
-
const scope = createScope([], normalizeClassName, "-");
|
|
120
|
-
for (const [component, meta] of componentMetas) {
|
|
121
|
-
const [_namespace, componentName] = parseComponentName(component);
|
|
122
|
-
const className = `w-${scope.getName(component, meta.label ?? componentName)}`;
|
|
123
|
-
const presetStyle = Object.entries(meta.presetStyle ?? {});
|
|
124
|
-
if (presetStyle.length > 0) {
|
|
125
|
-
presetClasses.set(component, className);
|
|
126
|
-
}
|
|
127
|
-
for (const [tag, styles2] of presetStyle) {
|
|
128
|
-
const selector = component === rootComponent ? ":root" : `:where(${tag}.${className})`;
|
|
129
|
-
const rule = globalSheet.addNestingRule(selector);
|
|
130
|
-
for (const declaration of styles2) {
|
|
131
|
-
rule.setDeclaration({
|
|
132
|
-
breakpoint: "presets",
|
|
133
|
-
selector: declaration.state ?? "",
|
|
134
|
-
property: declaration.property,
|
|
135
|
-
value: declaration.value
|
|
136
|
-
});
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
for (const breakpoint of breakpoints.values()) {
|
|
141
|
-
sheet.addMediaRule(breakpoint.id, breakpoint);
|
|
142
|
-
}
|
|
143
|
-
const imageValueTransformer = createImageValueTransformer(assets, {
|
|
144
|
-
assetBaseUrl
|
|
145
|
-
});
|
|
146
|
-
sheet.setTransformer(imageValueTransformer);
|
|
147
|
-
for (const styleDecl of styles.values()) {
|
|
148
|
-
const rule = sheet.addMixinRule(styleDecl.styleSourceId);
|
|
149
|
-
rule.setDeclaration({
|
|
150
|
-
breakpoint: styleDecl.breakpointId,
|
|
151
|
-
selector: styleDecl.state ?? "",
|
|
152
|
-
property: styleDecl.property,
|
|
153
|
-
value: styleDecl.value
|
|
154
|
-
});
|
|
155
|
-
}
|
|
156
|
-
const classes = /* @__PURE__ */ new Map();
|
|
157
|
-
const parentIdByInstanceId = /* @__PURE__ */ new Map();
|
|
158
|
-
for (const instance of instances.values()) {
|
|
159
|
-
const presetClass = presetClasses.get(instance.component);
|
|
160
|
-
if (presetClass) {
|
|
161
|
-
classes.set(instance.id, [presetClass]);
|
|
162
|
-
}
|
|
163
|
-
for (const child of instance.children) {
|
|
164
|
-
if (child.type === "id") {
|
|
165
|
-
parentIdByInstanceId.set(child.value, instance.id);
|
|
166
|
-
}
|
|
167
|
-
}
|
|
168
|
-
}
|
|
169
|
-
const descendantSelectorByInstanceId = /* @__PURE__ */ new Map();
|
|
170
|
-
for (const prop of props.values()) {
|
|
171
|
-
if (prop.name === "selector" && prop.type === "string") {
|
|
172
|
-
descendantSelectorByInstanceId.set(prop.instanceId, prop.value);
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
const instanceByRule = /* @__PURE__ */ new Map();
|
|
176
|
-
for (const selection of styleSourceSelections.values()) {
|
|
177
|
-
let { instanceId } = selection;
|
|
178
|
-
const { values } = selection;
|
|
179
|
-
if (instanceId === ROOT_INSTANCE_ID) {
|
|
180
|
-
const rule2 = sheet.addNestingRule(`:root`);
|
|
181
|
-
rule2.applyMixins(values);
|
|
182
|
-
continue;
|
|
183
|
-
}
|
|
184
|
-
let descendantSuffix = "";
|
|
185
|
-
const instance = instances.get(instanceId);
|
|
186
|
-
if (instance === void 0) {
|
|
187
|
-
continue;
|
|
188
|
-
}
|
|
189
|
-
if (instance.component === descendantComponent) {
|
|
190
|
-
const parentId = parentIdByInstanceId.get(instanceId);
|
|
191
|
-
const descendantSelector = descendantSelectorByInstanceId.get(instanceId);
|
|
192
|
-
if (parentId && descendantSelector) {
|
|
193
|
-
descendantSuffix = descendantSelector;
|
|
194
|
-
instanceId = parentId;
|
|
195
|
-
}
|
|
196
|
-
}
|
|
197
|
-
const meta = componentMetas.get(instance.component);
|
|
198
|
-
const [_namespace, shortName] = parseComponentName(instance.component);
|
|
199
|
-
const baseName = instance.label ?? meta?.label ?? shortName;
|
|
200
|
-
const className = `w-${scope.getName(instanceId, baseName)}`;
|
|
201
|
-
if (atomic === false) {
|
|
202
|
-
let classList = classes.get(instanceId);
|
|
203
|
-
if (classList === void 0) {
|
|
204
|
-
classList = [];
|
|
205
|
-
classes.set(instanceId, classList);
|
|
206
|
-
}
|
|
207
|
-
classList.push(className);
|
|
208
|
-
}
|
|
209
|
-
const rule = sheet.addNestingRule(`.${className}`, descendantSuffix);
|
|
210
|
-
rule.applyMixins(values);
|
|
211
|
-
instanceByRule.set(rule, instanceId);
|
|
212
|
-
}
|
|
213
|
-
if (atomic) {
|
|
214
|
-
const { cssText } = generateAtomic(sheet, {
|
|
215
|
-
getKey: (rule) => instanceByRule.get(rule),
|
|
216
|
-
transformValue: imageValueTransformer,
|
|
217
|
-
classes
|
|
218
|
-
});
|
|
219
|
-
return { cssText: `${globalSheet.cssText}
|
|
220
|
-
${cssText}`, classes };
|
|
221
|
-
}
|
|
222
|
-
return {
|
|
223
|
-
cssText: `${globalSheet.cssText}
|
|
224
|
-
${sheet.cssText}`,
|
|
225
|
-
classes
|
|
226
|
-
};
|
|
227
|
-
};
|
|
228
|
-
|
|
229
57
|
// src/props.ts
|
|
230
58
|
import {
|
|
231
59
|
getPagePath,
|
|
@@ -325,11 +153,8 @@ var idAttribute = "data-ws-id";
|
|
|
325
153
|
var selectorIdAttribute = "data-ws-selector";
|
|
326
154
|
var componentAttribute = "data-ws-component";
|
|
327
155
|
var showAttribute = "data-ws-show";
|
|
328
|
-
var indexAttribute = "data-ws-index";
|
|
329
156
|
var collapsedAttribute = "data-ws-collapsed";
|
|
330
157
|
var textContentAttribute = "data-ws-text-content";
|
|
331
|
-
var editablePlaceholderVariable = "--data-ws-editable-placeholder";
|
|
332
|
-
var editingPlaceholderVariable = "--data-ws-editing-placeholder";
|
|
333
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";
|
|
334
159
|
var attributeNameChar = attributeNameStartChar + ":\\-0-9\\u00B7\\u0300-\\u036F\\u203F-\\u2040";
|
|
335
160
|
var validAttributeNameRegex = new RegExp(
|
|
@@ -353,349 +178,128 @@ var isAttributeNameSafe = (attributeName) => {
|
|
|
353
178
|
return false;
|
|
354
179
|
};
|
|
355
180
|
|
|
356
|
-
// src/embed-template.ts
|
|
357
|
-
import { nanoid } from "nanoid";
|
|
358
|
-
import {
|
|
359
|
-
encodeDataSourceVariable,
|
|
360
|
-
transpileExpression
|
|
361
|
-
} from "@webstudio-is/sdk";
|
|
362
|
-
var getVariablValue = (value) => {
|
|
363
|
-
if (typeof value === "string") {
|
|
364
|
-
return { type: "string", value };
|
|
365
|
-
}
|
|
366
|
-
if (typeof value === "number") {
|
|
367
|
-
return { type: "number", value };
|
|
368
|
-
}
|
|
369
|
-
if (typeof value === "boolean") {
|
|
370
|
-
return { type: "boolean", value };
|
|
371
|
-
}
|
|
372
|
-
if (Array.isArray(value)) {
|
|
373
|
-
return { type: "string[]", value };
|
|
374
|
-
}
|
|
375
|
-
return { type: "json", value };
|
|
376
|
-
};
|
|
377
|
-
var createInstancesFromTemplate = (treeTemplate, instances, props, dataSourceByRef, styleSourceSelections, styleSources, styles, metas, defaultBreakpointId, generateId) => {
|
|
378
|
-
const parentChildren = [];
|
|
379
|
-
for (const item of treeTemplate) {
|
|
380
|
-
if (item.type === "instance") {
|
|
381
|
-
const instanceId = generateId();
|
|
382
|
-
if (item.variables) {
|
|
383
|
-
for (const [name, variable] of Object.entries(item.variables)) {
|
|
384
|
-
if (dataSourceByRef.has(name)) {
|
|
385
|
-
throw Error(`${name} data source already defined`);
|
|
386
|
-
}
|
|
387
|
-
dataSourceByRef.set(name, {
|
|
388
|
-
type: "variable",
|
|
389
|
-
id: generateId(),
|
|
390
|
-
scopeInstanceId: instanceId,
|
|
391
|
-
name: variable.alias ?? name,
|
|
392
|
-
value: getVariablValue(variable.initialValue)
|
|
393
|
-
});
|
|
394
|
-
}
|
|
395
|
-
}
|
|
396
|
-
if (item.props) {
|
|
397
|
-
for (const prop of item.props) {
|
|
398
|
-
const propId = generateId();
|
|
399
|
-
if (prop.type === "expression") {
|
|
400
|
-
props.push({
|
|
401
|
-
id: propId,
|
|
402
|
-
instanceId,
|
|
403
|
-
name: prop.name,
|
|
404
|
-
type: "expression",
|
|
405
|
-
// replace all references with variable names
|
|
406
|
-
value: transpileExpression({
|
|
407
|
-
expression: prop.code,
|
|
408
|
-
replaceVariable: (ref) => {
|
|
409
|
-
const id = dataSourceByRef.get(ref)?.id ?? ref;
|
|
410
|
-
return encodeDataSourceVariable(id);
|
|
411
|
-
}
|
|
412
|
-
})
|
|
413
|
-
});
|
|
414
|
-
continue;
|
|
415
|
-
}
|
|
416
|
-
if (prop.type === "action") {
|
|
417
|
-
props.push({
|
|
418
|
-
id: propId,
|
|
419
|
-
instanceId,
|
|
420
|
-
type: "action",
|
|
421
|
-
name: prop.name,
|
|
422
|
-
value: prop.value.map((value) => {
|
|
423
|
-
const args = value.args ?? [];
|
|
424
|
-
return {
|
|
425
|
-
type: "execute",
|
|
426
|
-
args,
|
|
427
|
-
// replace all references with variable names
|
|
428
|
-
code: transpileExpression({
|
|
429
|
-
expression: value.code,
|
|
430
|
-
replaceVariable: (ref) => {
|
|
431
|
-
if (args.includes(ref)) {
|
|
432
|
-
return;
|
|
433
|
-
}
|
|
434
|
-
const id = dataSourceByRef.get(ref)?.id ?? ref;
|
|
435
|
-
return encodeDataSourceVariable(id);
|
|
436
|
-
}
|
|
437
|
-
})
|
|
438
|
-
};
|
|
439
|
-
})
|
|
440
|
-
});
|
|
441
|
-
continue;
|
|
442
|
-
}
|
|
443
|
-
if (prop.type === "parameter") {
|
|
444
|
-
const dataSourceId = generateId();
|
|
445
|
-
dataSourceByRef.set(prop.variableName, {
|
|
446
|
-
type: "parameter",
|
|
447
|
-
id: dataSourceId,
|
|
448
|
-
scopeInstanceId: instanceId,
|
|
449
|
-
name: prop.variableAlias ?? prop.variableName
|
|
450
|
-
});
|
|
451
|
-
props.push({
|
|
452
|
-
id: propId,
|
|
453
|
-
instanceId,
|
|
454
|
-
name: prop.name,
|
|
455
|
-
type: "parameter",
|
|
456
|
-
// replace variable reference with variable id
|
|
457
|
-
value: dataSourceId
|
|
458
|
-
});
|
|
459
|
-
continue;
|
|
460
|
-
}
|
|
461
|
-
props.push({ id: propId, instanceId, ...prop });
|
|
462
|
-
}
|
|
463
|
-
}
|
|
464
|
-
const styleSourceIds = [];
|
|
465
|
-
if (item.styles) {
|
|
466
|
-
const styleSourceId = generateId();
|
|
467
|
-
styleSources.push({
|
|
468
|
-
type: "local",
|
|
469
|
-
id: styleSourceId
|
|
470
|
-
});
|
|
471
|
-
styleSourceIds.push(styleSourceId);
|
|
472
|
-
for (const styleDecl of item.styles) {
|
|
473
|
-
styles.push({
|
|
474
|
-
breakpointId: defaultBreakpointId,
|
|
475
|
-
styleSourceId,
|
|
476
|
-
state: styleDecl.state,
|
|
477
|
-
property: styleDecl.property,
|
|
478
|
-
value: styleDecl.value
|
|
479
|
-
});
|
|
480
|
-
}
|
|
481
|
-
}
|
|
482
|
-
if (styleSourceIds.length > 0) {
|
|
483
|
-
styleSourceSelections.push({
|
|
484
|
-
instanceId,
|
|
485
|
-
values: styleSourceIds
|
|
486
|
-
});
|
|
487
|
-
}
|
|
488
|
-
const instance = {
|
|
489
|
-
type: "instance",
|
|
490
|
-
id: instanceId,
|
|
491
|
-
label: item.label,
|
|
492
|
-
component: item.component,
|
|
493
|
-
children: []
|
|
494
|
-
};
|
|
495
|
-
instances.push(instance);
|
|
496
|
-
instance.children = createInstancesFromTemplate(
|
|
497
|
-
item.children,
|
|
498
|
-
instances,
|
|
499
|
-
props,
|
|
500
|
-
dataSourceByRef,
|
|
501
|
-
styleSourceSelections,
|
|
502
|
-
styleSources,
|
|
503
|
-
styles,
|
|
504
|
-
metas,
|
|
505
|
-
defaultBreakpointId,
|
|
506
|
-
generateId
|
|
507
|
-
);
|
|
508
|
-
parentChildren.push({
|
|
509
|
-
type: "id",
|
|
510
|
-
value: instanceId
|
|
511
|
-
});
|
|
512
|
-
}
|
|
513
|
-
if (item.type === "text") {
|
|
514
|
-
parentChildren.push({
|
|
515
|
-
type: "text",
|
|
516
|
-
value: item.value,
|
|
517
|
-
placeholder: item.placeholder
|
|
518
|
-
});
|
|
519
|
-
}
|
|
520
|
-
if (item.type === "expression") {
|
|
521
|
-
parentChildren.push({
|
|
522
|
-
type: "expression",
|
|
523
|
-
// replace all references with variable names
|
|
524
|
-
value: transpileExpression({
|
|
525
|
-
expression: item.value,
|
|
526
|
-
replaceVariable: (ref) => {
|
|
527
|
-
const id = dataSourceByRef.get(ref)?.id ?? ref;
|
|
528
|
-
return encodeDataSourceVariable(id);
|
|
529
|
-
}
|
|
530
|
-
})
|
|
531
|
-
});
|
|
532
|
-
}
|
|
533
|
-
}
|
|
534
|
-
return parentChildren;
|
|
535
|
-
};
|
|
536
|
-
var generateDataFromEmbedTemplate = (treeTemplate, metas, generateId = nanoid) => {
|
|
537
|
-
const instances = [];
|
|
538
|
-
const props = [];
|
|
539
|
-
const dataSourceByRef = /* @__PURE__ */ new Map();
|
|
540
|
-
const styleSourceSelections = [];
|
|
541
|
-
const styleSources = [];
|
|
542
|
-
const styles = [];
|
|
543
|
-
const baseBreakpointId = generateId();
|
|
544
|
-
const children = createInstancesFromTemplate(
|
|
545
|
-
treeTemplate,
|
|
546
|
-
instances,
|
|
547
|
-
props,
|
|
548
|
-
dataSourceByRef,
|
|
549
|
-
styleSourceSelections,
|
|
550
|
-
styleSources,
|
|
551
|
-
styles,
|
|
552
|
-
metas,
|
|
553
|
-
baseBreakpointId,
|
|
554
|
-
generateId
|
|
555
|
-
);
|
|
556
|
-
const breakpoints = [];
|
|
557
|
-
if (styles.length > 0) {
|
|
558
|
-
breakpoints.push({
|
|
559
|
-
id: baseBreakpointId,
|
|
560
|
-
label: ""
|
|
561
|
-
});
|
|
562
|
-
}
|
|
563
|
-
return {
|
|
564
|
-
children,
|
|
565
|
-
instances,
|
|
566
|
-
props,
|
|
567
|
-
dataSources: Array.from(dataSourceByRef.values()),
|
|
568
|
-
styleSourceSelections,
|
|
569
|
-
styleSources,
|
|
570
|
-
styles,
|
|
571
|
-
breakpoints,
|
|
572
|
-
assets: [],
|
|
573
|
-
resources: []
|
|
574
|
-
};
|
|
575
|
-
};
|
|
576
|
-
var namespaceEmbedTemplateComponents = (template, namespace, components) => {
|
|
577
|
-
return template.map((item) => {
|
|
578
|
-
if (item.type === "text") {
|
|
579
|
-
return item;
|
|
580
|
-
}
|
|
581
|
-
if (item.type === "expression") {
|
|
582
|
-
return item;
|
|
583
|
-
}
|
|
584
|
-
if (item.type === "instance") {
|
|
585
|
-
const prefix = components.has(item.component) ? `${namespace}:` : "";
|
|
586
|
-
return {
|
|
587
|
-
...item,
|
|
588
|
-
component: `${prefix}${item.component}`,
|
|
589
|
-
children: namespaceEmbedTemplateComponents(
|
|
590
|
-
item.children,
|
|
591
|
-
namespace,
|
|
592
|
-
components
|
|
593
|
-
)
|
|
594
|
-
};
|
|
595
|
-
}
|
|
596
|
-
item;
|
|
597
|
-
throw Error("Impossible case");
|
|
598
|
-
});
|
|
599
|
-
};
|
|
600
|
-
var namespaceMatcher = (namespace, matcher) => {
|
|
601
|
-
const newMatcher = structuredClone(matcher);
|
|
602
|
-
if (newMatcher.component?.$eq) {
|
|
603
|
-
newMatcher.component.$eq = `${namespace}:${newMatcher.component.$eq}`;
|
|
604
|
-
}
|
|
605
|
-
if (newMatcher.component?.$neq) {
|
|
606
|
-
newMatcher.component.$neq = `${namespace}:${newMatcher.component.$neq}`;
|
|
607
|
-
}
|
|
608
|
-
if (newMatcher.component?.$in) {
|
|
609
|
-
newMatcher.component.$in = newMatcher.component.$in.map(
|
|
610
|
-
(component) => `${namespace}:${component}`
|
|
611
|
-
);
|
|
612
|
-
}
|
|
613
|
-
if (newMatcher.component?.$nin) {
|
|
614
|
-
newMatcher.component.$nin = newMatcher.component.$nin.map(
|
|
615
|
-
(component) => `${namespace}:${component}`
|
|
616
|
-
);
|
|
617
|
-
}
|
|
618
|
-
return newMatcher;
|
|
619
|
-
};
|
|
620
|
-
var namespaceMeta = (meta, namespace, components) => {
|
|
621
|
-
const newMeta = { ...meta };
|
|
622
|
-
if (newMeta.constraints) {
|
|
623
|
-
if (Array.isArray(newMeta.constraints)) {
|
|
624
|
-
newMeta.constraints = newMeta.constraints.map(
|
|
625
|
-
(matcher) => namespaceMatcher(namespace, matcher)
|
|
626
|
-
);
|
|
627
|
-
} else {
|
|
628
|
-
newMeta.constraints = namespaceMatcher(namespace, newMeta.constraints);
|
|
629
|
-
}
|
|
630
|
-
}
|
|
631
|
-
if (newMeta.indexWithinAncestor) {
|
|
632
|
-
newMeta.indexWithinAncestor = components.has(newMeta.indexWithinAncestor) ? `${namespace}:${newMeta.indexWithinAncestor}` : newMeta.indexWithinAncestor;
|
|
633
|
-
}
|
|
634
|
-
if (newMeta.template) {
|
|
635
|
-
newMeta.template = namespaceEmbedTemplateComponents(
|
|
636
|
-
newMeta.template,
|
|
637
|
-
namespace,
|
|
638
|
-
components
|
|
639
|
-
);
|
|
640
|
-
}
|
|
641
|
-
return newMeta;
|
|
642
|
-
};
|
|
643
|
-
|
|
644
|
-
// src/instance-utils.ts
|
|
645
|
-
var getIndexesWithinAncestors = (metas, instances, rootIds) => {
|
|
646
|
-
const ancestors = /* @__PURE__ */ new Set();
|
|
647
|
-
for (const meta of metas.values()) {
|
|
648
|
-
if (meta.indexWithinAncestor !== void 0) {
|
|
649
|
-
ancestors.add(meta.indexWithinAncestor);
|
|
650
|
-
}
|
|
651
|
-
}
|
|
652
|
-
const indexes = /* @__PURE__ */ new Map();
|
|
653
|
-
const traverseInstances = (instances2, instanceId, latestIndexes2 = /* @__PURE__ */ new Map()) => {
|
|
654
|
-
const instance = instances2.get(instanceId);
|
|
655
|
-
if (instance === void 0) {
|
|
656
|
-
return;
|
|
657
|
-
}
|
|
658
|
-
const meta = metas.get(instance.component);
|
|
659
|
-
if (meta === void 0) {
|
|
660
|
-
return;
|
|
661
|
-
}
|
|
662
|
-
if (ancestors.has(instance.component)) {
|
|
663
|
-
latestIndexes2 = new Map(latestIndexes2);
|
|
664
|
-
latestIndexes2.set(instance.component, /* @__PURE__ */ new Map());
|
|
665
|
-
}
|
|
666
|
-
if (meta.indexWithinAncestor !== void 0) {
|
|
667
|
-
const ancestorIndexes = latestIndexes2.get(meta.indexWithinAncestor);
|
|
668
|
-
if (ancestorIndexes !== void 0) {
|
|
669
|
-
let index = ancestorIndexes.get(instance.component) ?? -1;
|
|
670
|
-
index += 1;
|
|
671
|
-
ancestorIndexes.set(instance.component, index);
|
|
672
|
-
indexes.set(instance.id, index);
|
|
673
|
-
}
|
|
674
|
-
}
|
|
675
|
-
for (const child of instance.children) {
|
|
676
|
-
if (child.type === "id") {
|
|
677
|
-
traverseInstances(instances2, child.value, latestIndexes2);
|
|
678
|
-
}
|
|
679
|
-
}
|
|
680
|
-
};
|
|
681
|
-
const latestIndexes = /* @__PURE__ */ new Map();
|
|
682
|
-
for (const instanceId of rootIds) {
|
|
683
|
-
traverseInstances(instances, instanceId, latestIndexes);
|
|
684
|
-
}
|
|
685
|
-
return indexes;
|
|
686
|
-
};
|
|
687
|
-
|
|
688
181
|
// src/component-generator.ts
|
|
689
182
|
import {
|
|
690
|
-
parseComponentName
|
|
183
|
+
parseComponentName,
|
|
691
184
|
generateExpression,
|
|
692
185
|
decodeDataSourceVariable,
|
|
693
|
-
transpileExpression
|
|
186
|
+
transpileExpression,
|
|
694
187
|
blockComponent,
|
|
695
188
|
blockTemplateComponent,
|
|
696
189
|
collectionComponent,
|
|
697
|
-
descendantComponent
|
|
190
|
+
descendantComponent,
|
|
191
|
+
getIndexesWithinAncestors,
|
|
192
|
+
elementComponent
|
|
698
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
|
+
popovertarget: "popoverTarget",
|
|
246
|
+
popovertargetaction: "popoverTargetAction",
|
|
247
|
+
dirname: "dirName"
|
|
248
|
+
};
|
|
249
|
+
var reactPropsToStandardAttributes = {
|
|
250
|
+
acceptCharset: "accept-charset",
|
|
251
|
+
accessKey: "accesskey",
|
|
252
|
+
allowFullScreen: "allowfullscreen",
|
|
253
|
+
autoCapitalize: "autocapitalize",
|
|
254
|
+
autoComplete: "autocomplete",
|
|
255
|
+
autoCorrect: "autocorrect",
|
|
256
|
+
autoFocus: "autofocus",
|
|
257
|
+
autoPlay: "autoplay",
|
|
258
|
+
charSet: "charset",
|
|
259
|
+
className: "class",
|
|
260
|
+
colSpan: "colspan",
|
|
261
|
+
contentEditable: "contenteditable",
|
|
262
|
+
crossOrigin: "crossorigin",
|
|
263
|
+
dateTime: "datetime",
|
|
264
|
+
encType: "enctype",
|
|
265
|
+
enterKeyHint: "enterkeyhint",
|
|
266
|
+
fetchPriority: "fetchpriority",
|
|
267
|
+
htmlFor: "for",
|
|
268
|
+
formMethod: "formmethod",
|
|
269
|
+
formAction: "formaction",
|
|
270
|
+
formEncType: "formenctype",
|
|
271
|
+
formNoValidate: "formnovalidate",
|
|
272
|
+
formTarget: "formtarget",
|
|
273
|
+
hrefLang: "hreflang",
|
|
274
|
+
httpEquiv: "http-equiv",
|
|
275
|
+
imageSizes: "imagesizes",
|
|
276
|
+
imageSrcSet: "imagesrcset",
|
|
277
|
+
inputMode: "inputmode",
|
|
278
|
+
itemID: "itemid",
|
|
279
|
+
itemProp: "itemprop",
|
|
280
|
+
itemRef: "itemref",
|
|
281
|
+
itemScope: "itemscope",
|
|
282
|
+
itemType: "itemtype",
|
|
283
|
+
maxLength: "maxlength",
|
|
284
|
+
minLength: "minlength",
|
|
285
|
+
noModule: "nomodule",
|
|
286
|
+
noValidate: "novalidate",
|
|
287
|
+
playsInline: "playsinline",
|
|
288
|
+
readOnly: "readonly",
|
|
289
|
+
referrerPolicy: "referrerpolicy",
|
|
290
|
+
rowSpan: "rowspan",
|
|
291
|
+
spellCheck: "spellcheck",
|
|
292
|
+
srcDoc: "srcdoc",
|
|
293
|
+
srcLang: "srclang",
|
|
294
|
+
srcSet: "srcset",
|
|
295
|
+
tabIndex: "tabindex",
|
|
296
|
+
useMap: "usemap",
|
|
297
|
+
popoverTarget: "popovertarget",
|
|
298
|
+
popoverTargetAction: "popovertargetaction",
|
|
299
|
+
dirName: "dirname"
|
|
300
|
+
};
|
|
301
|
+
|
|
302
|
+
// src/component-generator.ts
|
|
699
303
|
var generateAction = ({
|
|
700
304
|
scope,
|
|
701
305
|
prop,
|
|
@@ -707,7 +311,7 @@ var generateAction = ({
|
|
|
707
311
|
let assignersCode = "";
|
|
708
312
|
for (const value of prop.value) {
|
|
709
313
|
args = value.args;
|
|
710
|
-
assignersCode +=
|
|
314
|
+
assignersCode += transpileExpression({
|
|
711
315
|
expression: value.code,
|
|
712
316
|
executable: true,
|
|
713
317
|
replaceVariable: (identifier, assignee) => {
|
|
@@ -758,7 +362,7 @@ var generatePropValue = ({
|
|
|
758
362
|
if (prop.type === "asset" || prop.type === "page") {
|
|
759
363
|
return;
|
|
760
364
|
}
|
|
761
|
-
if (prop.type === "string" || prop.type === "number" || prop.type === "boolean" || prop.type === "string[]" || prop.type === "json") {
|
|
365
|
+
if (prop.type === "string" || prop.type === "number" || prop.type === "boolean" || prop.type === "string[]" || prop.type === "json" || prop.type === "animationAction") {
|
|
762
366
|
return JSON.stringify(prop.value);
|
|
763
367
|
}
|
|
764
368
|
if (prop.type === "parameter") {
|
|
@@ -788,6 +392,7 @@ var generatePropValue = ({
|
|
|
788
392
|
var generateJsxElement = ({
|
|
789
393
|
context = "jsx",
|
|
790
394
|
scope,
|
|
395
|
+
metas,
|
|
791
396
|
instance,
|
|
792
397
|
props,
|
|
793
398
|
dataSources,
|
|
@@ -796,14 +401,19 @@ var generateJsxElement = ({
|
|
|
796
401
|
children,
|
|
797
402
|
classesMap
|
|
798
403
|
}) => {
|
|
799
|
-
if (instance.component ===
|
|
404
|
+
if (instance.component === descendantComponent) {
|
|
800
405
|
return "";
|
|
801
406
|
}
|
|
407
|
+
const hasTags = Object.keys(metas.get(instance.component)?.presetStyle ?? {}).length > 0;
|
|
802
408
|
let generatedProps = "";
|
|
803
409
|
const index = indexesWithinAncestors.get(instance.id);
|
|
804
410
|
if (index !== void 0) {
|
|
805
411
|
generatedProps += `
|
|
806
|
-
${
|
|
412
|
+
${indexProperty}="${index}"`;
|
|
413
|
+
}
|
|
414
|
+
if (instance.tag !== void 0 && instance.component !== elementComponent) {
|
|
415
|
+
generatedProps += `
|
|
416
|
+
${tagProperty}=${JSON.stringify(instance.tag)}`;
|
|
807
417
|
}
|
|
808
418
|
let conditionValue;
|
|
809
419
|
let collectionDataValue;
|
|
@@ -822,6 +432,10 @@ ${indexAttribute}="${index}"`;
|
|
|
822
432
|
if (isAttributeNameSafe(prop.name) === false) {
|
|
823
433
|
continue;
|
|
824
434
|
}
|
|
435
|
+
let name = prop.name;
|
|
436
|
+
if (instance.component === elementComponent || hasTags) {
|
|
437
|
+
name = standardAttributesToReactProps[prop.name] ?? prop.name;
|
|
438
|
+
}
|
|
825
439
|
if (prop.name === showAttribute) {
|
|
826
440
|
if (propValue === "true") {
|
|
827
441
|
continue;
|
|
@@ -841,13 +455,13 @@ ${indexAttribute}="${index}"`;
|
|
|
841
455
|
}
|
|
842
456
|
continue;
|
|
843
457
|
}
|
|
844
|
-
if (
|
|
458
|
+
if (name === "className" && propValue !== void 0) {
|
|
845
459
|
classNameValue = propValue;
|
|
846
460
|
continue;
|
|
847
461
|
}
|
|
848
462
|
if (propValue !== void 0) {
|
|
849
463
|
generatedProps += `
|
|
850
|
-
${
|
|
464
|
+
${name}={${propValue}}`;
|
|
851
465
|
}
|
|
852
466
|
}
|
|
853
467
|
const classMapArray = classesMap?.get(instance.id);
|
|
@@ -870,7 +484,7 @@ ${prop.name}={${propValue}}`;
|
|
|
870
484
|
return "";
|
|
871
485
|
}
|
|
872
486
|
const indexVariable = scope.getName(`${instance.id}-index`, "index");
|
|
873
|
-
generatedElement += `{${collectionDataValue}?.map((${collectionItemValue}: any, ${indexVariable}: number) =>
|
|
487
|
+
generatedElement += `{${collectionDataValue}?.map?.((${collectionItemValue}: any, ${indexVariable}: number) =>
|
|
874
488
|
`;
|
|
875
489
|
generatedElement += `<Fragment key={${indexVariable}}>
|
|
876
490
|
`;
|
|
@@ -881,8 +495,20 @@ ${prop.name}={${propValue}}`;
|
|
|
881
495
|
`;
|
|
882
496
|
} else if (instance.component === blockComponent) {
|
|
883
497
|
generatedElement += children;
|
|
498
|
+
} else if (instance.component === elementComponent) {
|
|
499
|
+
const tagName = instance.tag ?? "div";
|
|
500
|
+
if (instance.children.length === 0) {
|
|
501
|
+
generatedElement += `<${tagName}${generatedProps} />
|
|
502
|
+
`;
|
|
503
|
+
} else {
|
|
504
|
+
generatedElement += `<${tagName}${generatedProps}>
|
|
505
|
+
`;
|
|
506
|
+
generatedElement += children;
|
|
507
|
+
generatedElement += `</${tagName}>
|
|
508
|
+
`;
|
|
509
|
+
}
|
|
884
510
|
} else {
|
|
885
|
-
const [_namespace, shortName] =
|
|
511
|
+
const [_namespace, shortName] = parseComponentName(instance.component);
|
|
886
512
|
const componentVariable = scope.getName(instance.component, shortName);
|
|
887
513
|
if (instance.children.length === 0) {
|
|
888
514
|
generatedElement += `<${componentVariable}${generatedProps} />
|
|
@@ -920,6 +546,7 @@ ${prop.name}={${propValue}}`;
|
|
|
920
546
|
};
|
|
921
547
|
var generateJsxChildren = ({
|
|
922
548
|
scope,
|
|
549
|
+
metas,
|
|
923
550
|
children,
|
|
924
551
|
instances,
|
|
925
552
|
props,
|
|
@@ -960,6 +587,7 @@ var generateJsxChildren = ({
|
|
|
960
587
|
generatedChildren += generateJsxElement({
|
|
961
588
|
context: "jsx",
|
|
962
589
|
scope,
|
|
590
|
+
metas,
|
|
963
591
|
instance,
|
|
964
592
|
props,
|
|
965
593
|
dataSources,
|
|
@@ -969,6 +597,7 @@ var generateJsxChildren = ({
|
|
|
969
597
|
children: generateJsxChildren({
|
|
970
598
|
classesMap,
|
|
971
599
|
scope,
|
|
600
|
+
metas,
|
|
972
601
|
children: instance.children,
|
|
973
602
|
instances,
|
|
974
603
|
props,
|
|
@@ -992,49 +621,58 @@ var generateWebstudioComponent = ({
|
|
|
992
621
|
instances,
|
|
993
622
|
props,
|
|
994
623
|
dataSources,
|
|
995
|
-
|
|
624
|
+
metas,
|
|
996
625
|
classesMap
|
|
997
626
|
}) => {
|
|
998
627
|
const instance = instances.get(rootInstanceId);
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
628
|
+
const indexesWithinAncestors = getIndexesWithinAncestors(metas, instances, [
|
|
629
|
+
rootInstanceId
|
|
630
|
+
]);
|
|
1002
631
|
const usedDataSources = /* @__PURE__ */ new Map();
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
props,
|
|
1008
|
-
dataSources,
|
|
1009
|
-
usedDataSources,
|
|
1010
|
-
indexesWithinAncestors,
|
|
1011
|
-
classesMap,
|
|
1012
|
-
children: generateJsxChildren({
|
|
632
|
+
let generatedJsx = "<></>\n";
|
|
633
|
+
if (instance) {
|
|
634
|
+
generatedJsx = generateJsxElement({
|
|
635
|
+
context: "expression",
|
|
1013
636
|
scope,
|
|
1014
|
-
|
|
1015
|
-
|
|
637
|
+
metas,
|
|
638
|
+
instance,
|
|
1016
639
|
props,
|
|
1017
640
|
dataSources,
|
|
1018
641
|
usedDataSources,
|
|
1019
642
|
indexesWithinAncestors,
|
|
1020
|
-
classesMap
|
|
1021
|
-
|
|
1022
|
-
|
|
643
|
+
classesMap,
|
|
644
|
+
children: generateJsxChildren({
|
|
645
|
+
scope,
|
|
646
|
+
metas,
|
|
647
|
+
children: instance.children,
|
|
648
|
+
instances,
|
|
649
|
+
props,
|
|
650
|
+
dataSources,
|
|
651
|
+
usedDataSources,
|
|
652
|
+
indexesWithinAncestors,
|
|
653
|
+
classesMap
|
|
654
|
+
})
|
|
655
|
+
});
|
|
656
|
+
}
|
|
1023
657
|
let generatedProps = "";
|
|
658
|
+
let generatedParameters = "";
|
|
659
|
+
const uniqueParameters = new Set(
|
|
660
|
+
parameters.map((parameter) => parameter.name)
|
|
661
|
+
);
|
|
1024
662
|
if (parameters.length > 0) {
|
|
1025
|
-
let
|
|
1026
|
-
|
|
663
|
+
let generatedPropsType = "";
|
|
664
|
+
for (const parameterName of uniqueParameters) {
|
|
665
|
+
generatedPropsType += `${parameterName}: any; `;
|
|
666
|
+
}
|
|
667
|
+
generatedProps = `_props: { ${generatedPropsType}}`;
|
|
1027
668
|
for (const parameter of parameters) {
|
|
1028
669
|
const dataSource = usedDataSources.get(parameter.value);
|
|
1029
670
|
if (dataSource) {
|
|
1030
671
|
const valueName = scope.getName(dataSource.id, dataSource.name);
|
|
1031
|
-
|
|
672
|
+
generatedParameters += `const ${valueName} = _props.${parameter.name};
|
|
673
|
+
`;
|
|
1032
674
|
}
|
|
1033
|
-
generatedPropsType += `${parameter.name}: any; `;
|
|
1034
675
|
}
|
|
1035
|
-
generatedPropsValue += `}`;
|
|
1036
|
-
generatedPropsType += `}`;
|
|
1037
|
-
generatedProps = `${generatedPropsValue}: ${generatedPropsType}`;
|
|
1038
676
|
}
|
|
1039
677
|
let generatedDataSources = "";
|
|
1040
678
|
for (const dataSource of usedDataSources.values()) {
|
|
@@ -1063,6 +701,7 @@ var generateWebstudioComponent = ({
|
|
|
1063
701
|
let generatedComponent = "";
|
|
1064
702
|
generatedComponent += `const ${name} = (${generatedProps}) => {
|
|
1065
703
|
`;
|
|
704
|
+
generatedComponent += `${generatedParameters}`;
|
|
1066
705
|
generatedComponent += `${generatedDataSources}`;
|
|
1067
706
|
generatedComponent += `return ${generatedJsx}`;
|
|
1068
707
|
generatedComponent += `}
|
|
@@ -1070,26 +709,19 @@ var generateWebstudioComponent = ({
|
|
|
1070
709
|
return generatedComponent;
|
|
1071
710
|
};
|
|
1072
711
|
export {
|
|
1073
|
-
addGlobalRules,
|
|
1074
712
|
collapsedAttribute,
|
|
1075
713
|
componentAttribute,
|
|
1076
|
-
createImageValueTransformer,
|
|
1077
|
-
editablePlaceholderVariable,
|
|
1078
|
-
editingPlaceholderVariable,
|
|
1079
|
-
generateCss,
|
|
1080
|
-
generateDataFromEmbedTemplate,
|
|
1081
714
|
generateJsxChildren,
|
|
1082
715
|
generateJsxElement,
|
|
1083
716
|
generateRemixParams,
|
|
1084
717
|
generateRemixRoute,
|
|
1085
718
|
generateWebstudioComponent,
|
|
1086
|
-
getIndexesWithinAncestors,
|
|
1087
719
|
idAttribute,
|
|
1088
|
-
indexAttribute,
|
|
1089
720
|
isAttributeNameSafe,
|
|
1090
|
-
namespaceMeta,
|
|
1091
721
|
normalizeProps,
|
|
722
|
+
reactPropsToStandardAttributes,
|
|
1092
723
|
selectorIdAttribute,
|
|
1093
724
|
showAttribute,
|
|
725
|
+
standardAttributesToReactProps,
|
|
1094
726
|
textContentAttribute
|
|
1095
727
|
};
|