@webstudio-is/react-sdk 0.238.0 → 0.252.1

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
@@ -186,6 +186,33 @@ var isAttributeNameSafe = (attributeName) => {
186
186
  return false;
187
187
  };
188
188
 
189
+ // src/collection-utils.ts
190
+ var getCollectionEntries = (data) => {
191
+ if (data === null || data === void 0) {
192
+ return [];
193
+ }
194
+ if (typeof data !== "object") {
195
+ return [];
196
+ }
197
+ const entries = Object.entries(data);
198
+ if (Array.isArray(data)) {
199
+ return entries.map(([key, value]) => [Number(key), value]);
200
+ }
201
+ return entries;
202
+ };
203
+ var generateCollectionIterationCode = ({
204
+ dataExpression,
205
+ keyVariable,
206
+ itemVariable
207
+ }) => {
208
+ return `Object.entries(
209
+ // @ts-ignore
210
+ ${dataExpression} ?? {}
211
+ ).map(([_key, ${itemVariable}]: any) => {
212
+ const ${keyVariable} = Array.isArray(${dataExpression}) ? Number(_key) : _key;
213
+ return`;
214
+ };
215
+
189
216
  // src/component-generator.ts
190
217
  import {
191
218
  parseComponentName,
@@ -540,6 +567,7 @@ ${tagProperty}=${JSON.stringify(instance.tag)}`;
540
567
  let conditionValue;
541
568
  let collectionDataValue;
542
569
  let collectionItemValue;
570
+ let collectionItemKeyValue;
543
571
  let classNameValue;
544
572
  for (const prop of props.values()) {
545
573
  if (prop.instanceId !== instance.id) {
@@ -575,6 +603,9 @@ ${tagProperty}=${JSON.stringify(instance.tag)}`;
575
603
  if (prop.name === "item") {
576
604
  collectionItemValue = propValue;
577
605
  }
606
+ if (prop.name === "itemKey") {
607
+ collectionItemKeyValue = propValue;
608
+ }
578
609
  continue;
579
610
  }
580
611
  if (name === "className" && propValue !== void 0) {
@@ -606,14 +637,23 @@ ${name}={${propValue}}`;
606
637
  return "";
607
638
  }
608
639
  const indexVariable = scope.getName(`${instance.id}-index`, "index");
609
- generatedElement += `{${collectionDataValue}?.map?.((${collectionItemValue}: any, ${indexVariable}: number) =>
640
+ const keyVariable = collectionItemKeyValue ?? indexVariable;
641
+ generatedElement += `{${generateCollectionIterationCode({
642
+ dataExpression: collectionDataValue,
643
+ keyVariable,
644
+ itemVariable: collectionItemValue
645
+ })} (
610
646
  `;
611
- generatedElement += `<Fragment key={${indexVariable}}>
647
+ generatedElement += `<Fragment key={${keyVariable}}>
612
648
  `;
613
649
  generatedElement += children;
614
650
  generatedElement += `</Fragment>
615
651
  `;
616
- generatedElement += `)}
652
+ generatedElement += `)
653
+ `;
654
+ generatedElement += `})
655
+ `;
656
+ generatedElement += `}
617
657
  `;
618
658
  } else if (instance.component === blockComponent) {
619
659
  generatedElement += children;
@@ -837,11 +877,13 @@ var generateWebstudioComponent = ({
837
877
  export {
838
878
  collapsedAttribute,
839
879
  componentAttribute,
880
+ generateCollectionIterationCode,
840
881
  generateJsxChildren,
841
882
  generateJsxElement,
842
883
  generateRemixParams,
843
884
  generateRemixRoute,
844
885
  generateWebstudioComponent,
886
+ getCollectionEntries,
845
887
  idAttribute,
846
888
  isAttributeNameSafe,
847
889
  normalizeProps,
package/lib/runtime.js CHANGED
@@ -127,9 +127,10 @@ var PageSettingsMeta = ({
127
127
  });
128
128
  }
129
129
  if (pageMeta.socialImageAssetName) {
130
+ const origin = host ? `https://${host}` : "";
130
131
  metas.push({
131
132
  property: "og:image",
132
- content: `https://${host}${imageLoader({
133
+ content: `${origin}${imageLoader({
133
134
  src: `${assetBaseUrl}${pageMeta.socialImageAssetName}`,
134
135
  // Do not transform social image (not enough information do we need to do this)
135
136
  format: "raw"
@@ -0,0 +1,24 @@
1
+ /**
2
+ * Shared collection iteration logic for both builder and published sites.
3
+ *
4
+ * Collections support both arrays and objects:
5
+ * - Arrays: [item1, item2] -> entries are [["0", item1], ["1", item2]]
6
+ * - Objects: {key1: val1, key2: val2} -> entries are [["key1", val1], ["key2", val2]]
7
+ *
8
+ * Using Object.entries() unifies both cases since arrays are objects with numeric keys.
9
+ */
10
+ /**
11
+ * Normalize collection data to entries format [key, value][].
12
+ * Returns empty array if data is not iterable.
13
+ * For arrays, keys are converted to numbers.
14
+ */
15
+ export declare const getCollectionEntries: (data: unknown) => Array<[string | number, unknown]>;
16
+ /**
17
+ * Template for generated code that iterates over collections.
18
+ * Used by component-generator.ts to ensure consistency.
19
+ */
20
+ export declare const generateCollectionIterationCode: ({ dataExpression, keyVariable, itemVariable, }: {
21
+ dataExpression: string;
22
+ keyVariable: string;
23
+ itemVariable: string;
24
+ }) => string;
@@ -1,6 +1,7 @@
1
1
  export * from "./remix";
2
2
  export * from "./components/components-utils";
3
3
  export * from "./props";
4
+ export * from "./collection-utils";
4
5
  export type * from "./context";
5
6
  export type * from "./hook";
6
7
  export { generateWebstudioComponent, generateJsxElement, generateJsxChildren, } from "./component-generator";
@@ -3,8 +3,8 @@ import type { PageMeta } from "@webstudio-is/sdk";
3
3
  export declare const isElementRenderedWithReact: (element: Element) => boolean;
4
4
  export declare const PageSettingsMeta: ({ url, host, siteName, pageMeta, imageLoader, assetBaseUrl, }: {
5
5
  url?: string;
6
- host: string;
7
- siteName: string;
6
+ host?: string;
7
+ siteName?: string;
8
8
  pageMeta: PageMeta;
9
9
  imageLoader: ImageLoader;
10
10
  assetBaseUrl: string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@webstudio-is/react-sdk",
3
- "version": "0.238.0",
3
+ "version": "0.252.1",
4
4
  "description": "Webstudio JavaScript / TypeScript API",
5
5
  "author": "Webstudio <github@webstudio.is>",
6
6
  "homepage": "https://webstudio.is",
@@ -14,7 +14,7 @@
14
14
  "type-fest": "^4.37.0",
15
15
  "vitest": "^3.1.2",
16
16
  "zod": "^3.24.2",
17
- "@webstudio-is/template": "0.238.0",
17
+ "@webstudio-is/template": "0.252.1",
18
18
  "@webstudio-is/tsconfig": "1.0.7"
19
19
  },
20
20
  "peerDependencies": {
@@ -26,11 +26,11 @@
26
26
  "change-case": "^5.4.4",
27
27
  "html-tags": "^4.0.0",
28
28
  "nanoid": "^5.1.5",
29
- "@webstudio-is/css-engine": "0.238.0",
30
- "@webstudio-is/fonts": "0.238.0",
31
- "@webstudio-is/icons": "^0.238.0",
32
- "@webstudio-is/image": "0.238.0",
33
- "@webstudio-is/sdk": "0.238.0"
29
+ "@webstudio-is/fonts": "0.252.1",
30
+ "@webstudio-is/image": "0.252.1",
31
+ "@webstudio-is/css-engine": "0.252.1",
32
+ "@webstudio-is/icons": "^0.252.1",
33
+ "@webstudio-is/sdk": "0.252.1"
34
34
  },
35
35
  "exports": {
36
36
  ".": {
@@ -58,7 +58,7 @@
58
58
  "scripts": {
59
59
  "build": "rm -rf lib && esbuild src/index.ts src/runtime.ts --outdir=lib --bundle --format=esm --packages=external",
60
60
  "dts": "tsc --project tsconfig.dts.json",
61
- "typecheck": "tsc",
61
+ "typecheck": "tsgo --noEmit",
62
62
  "test": "vitest run"
63
63
  }
64
64
  }
package/placeholder.d.ts CHANGED
@@ -58,6 +58,11 @@ declare module "__SITEMAP__" {
58
58
  }>;
59
59
  }
60
60
 
61
+ declare module "__ASSETS__" {
62
+ import type { RuntimeAsset } from "@webstudio-is/sdk";
63
+ export const assets: Record<string, RuntimeAsset>;
64
+ }
65
+
61
66
  declare module "__REDIRECT__" {
62
67
  export const url: string;
63
68
  export const status: number;