@webstudio-is/react-sdk 0.168.0 → 0.174.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 CHANGED
@@ -70,152 +70,15 @@ var addGlobalRules = (sheet, { assets, assetBaseUrl }) => {
70
70
  }
71
71
  };
72
72
 
73
- // src/props.ts
74
- import {
75
- getPagePath,
76
- findPageByIdOrPath
77
- } from "@webstudio-is/sdk";
78
- var normalizeProps = ({
79
- props,
80
- assetBaseUrl,
81
- assets,
82
- pages
83
- }) => {
84
- const newProps = [];
85
- for (const prop of props) {
86
- if (prop.type === "asset") {
87
- const assetId = prop.value;
88
- const asset = assets.get(assetId);
89
- if (asset === void 0) {
90
- continue;
91
- }
92
- newProps.push({
93
- id: prop.id,
94
- name: prop.name,
95
- required: prop.required,
96
- instanceId: prop.instanceId,
97
- type: "string",
98
- value: `${assetBaseUrl}${asset.name}`
99
- });
100
- continue;
101
- }
102
- if (prop.type === "page") {
103
- let idProp;
104
- const pageId = typeof prop.value === "string" ? prop.value : prop.value.pageId;
105
- const page = findPageByIdOrPath(pageId, pages);
106
- if (page === void 0) {
107
- continue;
108
- }
109
- if (typeof prop.value !== "string") {
110
- const { instanceId } = prop.value;
111
- idProp = props.find(
112
- (prop2) => prop2.instanceId === instanceId && prop2.name === "id"
113
- );
114
- }
115
- const path = getPagePath(page.id, pages);
116
- const url = new URL(path, "https://any-valid.url");
117
- let value = url.pathname;
118
- if (idProp?.type === "string") {
119
- const hash = idProp.value;
120
- url.hash = encodeURIComponent(hash);
121
- value = `${url.pathname}${url.hash}`;
122
- }
123
- newProps.push({
124
- id: prop.id,
125
- name: prop.name,
126
- required: prop.required,
127
- instanceId: prop.instanceId,
128
- type: "string",
129
- value
130
- });
131
- continue;
132
- }
133
- newProps.push(prop);
134
- }
135
- return newProps;
136
- };
137
- var idAttribute = "data-ws-id";
138
- var selectorIdAttribute = "data-ws-selector";
139
- var componentAttribute = "data-ws-component";
140
- var showAttribute = "data-ws-show";
141
- var indexAttribute = "data-ws-index";
142
- var collapsedAttribute = "data-ws-collapsed";
143
- var textContentAttribute = "data-ws-text-content";
144
- var getInstanceIdFromComponentProps = (props) => {
145
- return props[idAttribute];
146
- };
147
- var getIndexWithinAncestorFromComponentProps = (props) => {
148
- return props[indexAttribute];
149
- };
150
-
151
- // src/css/style-rules.ts
152
- var getStyleRules = (styles, styleSourceSelections) => {
153
- if (styles === void 0 || styleSourceSelections === void 0) {
154
- return [];
155
- }
156
- const stylesByStyleSourceId = /* @__PURE__ */ new Map();
157
- for (const styleDecl of styles.values()) {
158
- const { styleSourceId } = styleDecl;
159
- let styleSourceStyles = stylesByStyleSourceId.get(styleSourceId);
160
- if (styleSourceStyles === void 0) {
161
- styleSourceStyles = [];
162
- stylesByStyleSourceId.set(styleSourceId, styleSourceStyles);
163
- }
164
- styleSourceStyles.push(styleDecl);
165
- }
166
- const styleRules = [];
167
- for (const { instanceId, values } of styleSourceSelections.values()) {
168
- const styleRuleByBreakpointId = /* @__PURE__ */ new Map();
169
- for (const styleSourceId of values) {
170
- const styleSourceStyles = stylesByStyleSourceId.get(styleSourceId);
171
- if (styleSourceStyles === void 0) {
172
- continue;
173
- }
174
- for (const {
175
- breakpointId,
176
- state,
177
- property,
178
- value
179
- } of styleSourceStyles) {
180
- const key = `${breakpointId}:${state ?? ""}`;
181
- let styleRule = styleRuleByBreakpointId.get(key);
182
- if (styleRule === void 0) {
183
- styleRule = {
184
- instanceId,
185
- breakpointId,
186
- state,
187
- style: {}
188
- };
189
- styleRuleByBreakpointId.set(key, styleRule);
190
- }
191
- styleRule.style[property] = value;
192
- }
193
- }
194
- styleRules.push(...styleRuleByBreakpointId.values());
195
- }
196
- return styleRules;
197
- };
198
- var getPresetStyleRules = (component, presetStyle) => {
199
- const presetStyleRules = /* @__PURE__ */ new Map();
200
- for (const [tag, styles] of Object.entries(presetStyle)) {
201
- for (const styleDecl of styles) {
202
- const selector = `${tag}:where([${componentAttribute}="${component}"])${styleDecl.state ?? ""}`;
203
- let rule = presetStyleRules.get(selector);
204
- if (rule === void 0) {
205
- rule = {};
206
- presetStyleRules.set(selector, rule);
207
- }
208
- rule[styleDecl.property] = styleDecl.value;
209
- }
210
- }
211
- return presetStyleRules;
212
- };
213
-
214
73
  // src/css/css.ts
215
74
  import {
216
75
  createRegularStyleSheet,
217
76
  generateAtomic
218
77
  } from "@webstudio-is/css-engine";
78
+ import {
79
+ createScope,
80
+ parseComponentName
81
+ } from "@webstudio-is/sdk";
219
82
 
220
83
  // src/core-components.ts
221
84
  import { ListViewIcon, PaintBrushIcon } from "@webstudio-is/icons/svg";
@@ -253,7 +116,13 @@ var collectionMeta = {
253
116
  {
254
117
  type: "instance",
255
118
  component: "Box",
256
- children: [{ type: "expression", value: "collectionItem" }]
119
+ children: [
120
+ {
121
+ type: "instance",
122
+ component: "Text",
123
+ children: [{ type: "expression", value: "collectionItem" }]
124
+ }
125
+ ]
257
126
  }
258
127
  ]
259
128
  }
@@ -316,6 +185,7 @@ var corePropsMetas = {
316
185
  var isCoreComponent = (component) => component === collectionComponent || component === descendantComponent;
317
186
 
318
187
  // src/css/css.ts
188
+ import { kebabCase } from "change-case";
319
189
  var createImageValueTransformer = (assets, { assetBaseUrl }) => (styleValue) => {
320
190
  if (styleValue.type === "image" && styleValue.value.type === "asset") {
321
191
  const asset = assets.get(styleValue.value.value);
@@ -333,6 +203,7 @@ var createImageValueTransformer = (assets, { assetBaseUrl }) => (styleValue) =>
333
203
  };
334
204
  }
335
205
  };
206
+ var normalizeClassName = (name) => kebabCase(name);
336
207
  var generateCss = ({
337
208
  assets,
338
209
  instances,
@@ -344,35 +215,34 @@ var generateCss = ({
344
215
  assetBaseUrl,
345
216
  atomic
346
217
  }) => {
218
+ const globalSheet = createRegularStyleSheet({ name: "ssr" });
347
219
  const sheet = createRegularStyleSheet({ name: "ssr" });
348
- const parentIdByInstanceId = /* @__PURE__ */ new Map();
349
- for (const instance of instances.values()) {
350
- for (const child of instance.children) {
351
- if (child.type === "id") {
352
- parentIdByInstanceId.set(child.value, instance.id);
353
- }
220
+ addGlobalRules(globalSheet, { assets, assetBaseUrl });
221
+ globalSheet.addMediaRule("presets");
222
+ const presetClasses = /* @__PURE__ */ new Map();
223
+ const scope = createScope([], normalizeClassName, "-");
224
+ for (const [component, meta] of componentMetas) {
225
+ const [_namespace, componentName] = parseComponentName(component);
226
+ const className = `w-${scope.getName(component, meta.label ?? componentName)}`;
227
+ const presetStyle = Object.entries(meta.presetStyle ?? {});
228
+ if (presetStyle.length > 0) {
229
+ presetClasses.set(component, className);
354
230
  }
355
- }
356
- const descendantSelectorByInstanceId = /* @__PURE__ */ new Map();
357
- for (const prop of props.values()) {
358
- if (prop.name === "selector" && prop.type === "string") {
359
- descendantSelectorByInstanceId.set(prop.instanceId, prop.value);
231
+ for (const [tag, styles2] of presetStyle) {
232
+ const rule = globalSheet.addNestingRule(`:where(${tag}.${className})`);
233
+ for (const declaration of styles2) {
234
+ rule.setDeclaration({
235
+ breakpoint: "presets",
236
+ selector: declaration.state ?? "",
237
+ property: declaration.property,
238
+ value: declaration.value
239
+ });
240
+ }
360
241
  }
361
242
  }
362
- addGlobalRules(sheet, { assets, assetBaseUrl });
363
243
  for (const breakpoint of breakpoints.values()) {
364
244
  sheet.addMediaRule(breakpoint.id, breakpoint);
365
245
  }
366
- for (const [component, meta] of componentMetas) {
367
- const presetStyle = meta.presetStyle;
368
- if (presetStyle === void 0) {
369
- continue;
370
- }
371
- const rules = getPresetStyleRules(component, presetStyle);
372
- for (const [selector, style] of rules) {
373
- sheet.addStyleRule({ style }, selector);
374
- }
375
- }
376
246
  const imageValueTransformer = createImageValueTransformer(assets, {
377
247
  assetBaseUrl
378
248
  });
@@ -386,6 +256,25 @@ var generateCss = ({
386
256
  value: styleDecl.value
387
257
  });
388
258
  }
259
+ const classes = /* @__PURE__ */ new Map();
260
+ const parentIdByInstanceId = /* @__PURE__ */ new Map();
261
+ for (const instance of instances.values()) {
262
+ const presetClass = presetClasses.get(instance.component);
263
+ if (presetClass) {
264
+ classes.set(instance.id, [presetClass]);
265
+ }
266
+ for (const child of instance.children) {
267
+ if (child.type === "id") {
268
+ parentIdByInstanceId.set(child.value, instance.id);
269
+ }
270
+ }
271
+ }
272
+ const descendantSelectorByInstanceId = /* @__PURE__ */ new Map();
273
+ for (const prop of props.values()) {
274
+ if (prop.name === "selector" && prop.type === "string") {
275
+ descendantSelectorByInstanceId.set(prop.instanceId, prop.value);
276
+ }
277
+ }
389
278
  const instanceByRule = /* @__PURE__ */ new Map();
390
279
  for (const selection of styleSourceSelections.values()) {
391
280
  let { instanceId } = selection;
@@ -400,22 +289,34 @@ var generateCss = ({
400
289
  instanceId = parentId;
401
290
  }
402
291
  }
403
- const rule = sheet.addNestingRule(
404
- `[${idAttribute}="${instanceId}"]`,
405
- descendantSuffix
406
- );
292
+ const meta = instance ? componentMetas.get(instance.component) : void 0;
293
+ const baseName = instance?.label ?? meta?.label ?? instance?.component ?? instanceId;
294
+ const className = `w-${scope.getName(instanceId, baseName)}`;
295
+ if (atomic === false) {
296
+ let classList = classes.get(instanceId);
297
+ if (classList === void 0) {
298
+ classList = [];
299
+ classes.set(instanceId, classList);
300
+ }
301
+ classList.push(className);
302
+ }
303
+ const rule = sheet.addNestingRule(`.${className}`, descendantSuffix);
407
304
  rule.applyMixins(values);
408
305
  instanceByRule.set(rule, instanceId);
409
306
  }
410
307
  if (atomic) {
411
- return generateAtomic(sheet, {
308
+ const { cssText } = generateAtomic(sheet, {
412
309
  getKey: (rule) => instanceByRule.get(rule) ?? "",
413
- transformValue: imageValueTransformer
310
+ transformValue: imageValueTransformer,
311
+ classes
414
312
  });
313
+ return { cssText: `${globalSheet.cssText}
314
+ ${cssText}`, classes };
415
315
  }
416
316
  return {
417
- cssText: sheet.cssText,
418
- classesMap: /* @__PURE__ */ new Map()
317
+ cssText: `${globalSheet.cssText}
318
+ ${sheet.cssText}`,
319
+ classes
419
320
  };
420
321
  };
421
322
 
@@ -536,6 +437,112 @@ var createInstanceElement = ({
536
437
  );
537
438
  };
538
439
 
440
+ // src/props.ts
441
+ import {
442
+ getPagePath,
443
+ findPageByIdOrPath
444
+ } from "@webstudio-is/sdk";
445
+ var normalizeProps = ({
446
+ props,
447
+ assetBaseUrl,
448
+ assets,
449
+ uploadingImageAssets,
450
+ pages,
451
+ source
452
+ }) => {
453
+ const newProps = [];
454
+ for (const prop of props) {
455
+ if (prop.type === "asset") {
456
+ const assetId = prop.value;
457
+ const asset = assets.get(assetId) ?? uploadingImageAssets.find((asset2) => asset2.id === assetId);
458
+ if (asset === void 0) {
459
+ continue;
460
+ }
461
+ const propBase = {
462
+ id: prop.id,
463
+ name: prop.name,
464
+ required: prop.required,
465
+ instanceId: prop.instanceId
466
+ };
467
+ if (prop.name === "width" && asset.type === "image") {
468
+ newProps.push({
469
+ ...propBase,
470
+ type: "number",
471
+ value: asset.meta.width
472
+ });
473
+ continue;
474
+ }
475
+ if (prop.name === "height" && asset.type === "image") {
476
+ newProps.push({
477
+ ...propBase,
478
+ type: "number",
479
+ value: asset.meta.height
480
+ });
481
+ continue;
482
+ }
483
+ newProps.push({
484
+ ...propBase,
485
+ type: "string",
486
+ value: `${assetBaseUrl}${asset.name}`
487
+ });
488
+ if (source === "canvas") {
489
+ newProps.push({
490
+ id: `${prop.instanceId}-${asset.id}-assetId`,
491
+ name: "$webstudio$canvasOnly$assetId",
492
+ required: false,
493
+ instanceId: prop.instanceId,
494
+ type: "string",
495
+ value: asset.id
496
+ });
497
+ }
498
+ continue;
499
+ }
500
+ if (prop.type === "page") {
501
+ let idProp;
502
+ const pageId = typeof prop.value === "string" ? prop.value : prop.value.pageId;
503
+ const page = findPageByIdOrPath(pageId, pages);
504
+ if (page === void 0) {
505
+ continue;
506
+ }
507
+ if (typeof prop.value !== "string") {
508
+ const { instanceId } = prop.value;
509
+ idProp = props.find(
510
+ (prop2) => prop2.instanceId === instanceId && prop2.name === "id"
511
+ );
512
+ }
513
+ const path = getPagePath(page.id, pages);
514
+ const url = new URL(path, "https://any-valid.url");
515
+ let value = url.pathname;
516
+ if (idProp?.type === "string") {
517
+ const hash = idProp.value;
518
+ url.hash = encodeURIComponent(hash);
519
+ value = `${url.pathname}${url.hash}`;
520
+ }
521
+ newProps.push({
522
+ id: prop.id,
523
+ name: prop.name,
524
+ required: prop.required,
525
+ instanceId: prop.instanceId,
526
+ type: "string",
527
+ value
528
+ });
529
+ continue;
530
+ }
531
+ newProps.push(prop);
532
+ }
533
+ return newProps;
534
+ };
535
+ var idAttribute = "data-ws-id";
536
+ var selectorIdAttribute = "data-ws-selector";
537
+ var componentAttribute = "data-ws-component";
538
+ var showAttribute = "data-ws-show";
539
+ var indexAttribute = "data-ws-index";
540
+ var collapsedAttribute = "data-ws-collapsed";
541
+ var textContentAttribute = "data-ws-text-content";
542
+ var getIndexWithinAncestorFromComponentProps = (props) => {
543
+ return props[indexAttribute];
544
+ };
545
+
539
546
  // src/prop-meta.ts
540
547
  import { z } from "zod";
541
548
  var common = {
@@ -1192,10 +1199,19 @@ var getClosestInstance = (instancePath, currentInstance, closestComponent) => {
1192
1199
  }
1193
1200
  }
1194
1201
  };
1202
+ var getInstanceSelectorById = (instanceSelector, instanceId) => {
1203
+ const index = instanceSelector.findIndex(
1204
+ (selector) => selector === instanceId
1205
+ );
1206
+ if (index === -1) {
1207
+ return [];
1208
+ }
1209
+ return instanceSelector.slice(index);
1210
+ };
1195
1211
 
1196
1212
  // src/component-generator.ts
1197
1213
  import {
1198
- parseComponentName,
1214
+ parseComponentName as parseComponentName2,
1199
1215
  generateExpression,
1200
1216
  decodeDataSourceVariable,
1201
1217
  transpileExpression as transpileExpression2
@@ -1284,6 +1300,9 @@ var generatePropValue = ({
1284
1300
  if (prop.type === "action") {
1285
1301
  return generateAction({ scope, prop, dataSources, usedDataSources });
1286
1302
  }
1303
+ if (prop.type === "resource") {
1304
+ return JSON.stringify(scope.getName(prop.value, prop.name));
1305
+ }
1287
1306
  prop;
1288
1307
  };
1289
1308
  var generateJsxElement = ({
@@ -1300,12 +1319,6 @@ var generateJsxElement = ({
1300
1319
  return "";
1301
1320
  }
1302
1321
  let generatedProps = "";
1303
- generatedProps += `
1304
- ${idAttribute}=${JSON.stringify(instance.id)}`;
1305
- generatedProps += `
1306
- ${componentAttribute}=${JSON.stringify(
1307
- instance.component
1308
- )}`;
1309
1322
  const index = indexesWithinAncestors.get(instance.id);
1310
1323
  if (index !== void 0) {
1311
1324
  generatedProps += `
@@ -1375,7 +1388,7 @@ className=${JSON.stringify(classes.join(" "))}`;
1375
1388
  generatedElement += `)}
1376
1389
  `;
1377
1390
  } else {
1378
- const [_namespace, shortName] = parseComponentName(instance.component);
1391
+ const [_namespace, shortName] = parseComponentName2(instance.component);
1379
1392
  const componentVariable = scope.getName(instance.component, shortName);
1380
1393
  if (instance.children.length === 0) {
1381
1394
  generatedElement += `<${componentVariable}${generatedProps} />
@@ -1584,9 +1597,7 @@ export {
1584
1597
  getClosestInstance,
1585
1598
  getIndexWithinAncestorFromComponentProps,
1586
1599
  getIndexesWithinAncestors,
1587
- getInstanceIdFromComponentProps,
1588
- getPresetStyleRules,
1589
- getStyleRules,
1600
+ getInstanceSelectorById,
1590
1601
  idAttribute,
1591
1602
  indexAttribute,
1592
1603
  isCoreComponent,