@builder.io/sdk-svelte 0.0.1-23 → 0.0.1-26

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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@builder.io/sdk-svelte",
3
3
  "description": "Builder.io SDK for Svelte",
4
- "version": "0.0.1-23",
4
+ "version": "0.0.1-26",
5
5
  "type": "module",
6
6
  "files": [
7
7
  "src"
@@ -42,15 +42,20 @@
42
42
 
43
43
  return getSrcSet(url);
44
44
  };
45
+
46
+ $: webpSrcSet = () => {
47
+ if (srcSetToUse?.()?.match(/builder\.io/) && !noWebp) {
48
+ return srcSetToUse().replace(/\?/g, "?format=webp&");
49
+ } else {
50
+ return "";
51
+ }
52
+ };
45
53
  </script>
46
54
 
47
55
  <div class="div">
48
56
  <picture>
49
- {#if srcSetToUse?.()?.match(/builder\.io/) && !noWebp}
50
- <source
51
- type="image/webp"
52
- srcset={srcSetToUse?.()?.replace(/\?/g, "?format=webp&")}
53
- />
57
+ {#if webpSrcSet()}
58
+ <source type="image/webp" srcset={webpSrcSet()} />
54
59
  {/if}
55
60
 
56
61
  <img
@@ -22,13 +22,22 @@
22
22
 
23
23
  function onUpdateFn_0() {
24
24
  const symbolToUse = symbol;
25
+ /**
26
+ * If:
27
+ * - we have a symbol prop
28
+ * - yet it does not have any content
29
+ * - and we have not already stored content from before
30
+ * - and it has a model name
31
+ *
32
+ * then we want to re-fetch the symbol content.
33
+ */
25
34
 
26
35
  if (symbolToUse && !symbolToUse.content && !content && symbolToUse.model) {
27
36
  getContent({
28
37
  model: symbolToUse.model,
29
38
  apiKey: builderContext.apiKey,
30
- options: {
31
- entry: symbolToUse.entry,
39
+ query: {
40
+ id: symbolToUse.entry,
32
41
  },
33
42
  }).then((response) => {
34
43
  content = response;
@@ -48,6 +57,7 @@
48
57
  <RenderContent
49
58
  apiKey={builderContext.apiKey}
50
59
  context={builderContext.context}
60
+ customComponents={Object.values(builderContext.registeredComponents)}
51
61
  data={{
52
62
  ...symbol?.data,
53
63
  ...builderContext.state,
@@ -139,34 +139,33 @@
139
139
  return undefined;
140
140
  }
141
141
 
142
- const { collection, itemName } = repeat;
143
142
  const itemsArray = evaluate({
144
- code: collection,
143
+ code: repeat.collection,
145
144
  state: builderContext.state,
146
145
  context: builderContext.context,
147
146
  });
148
147
 
149
- if (Array.isArray(itemsArray)) {
150
- const collectionName = collection.split(".").pop();
151
- const itemNameToUse =
152
- itemName || (collectionName ? collectionName + "Item" : "item");
153
- const repeatArray = itemsArray.map((item, index) => ({
154
- context: {
155
- ...builderContext,
156
- state: {
157
- ...builderContext.state,
158
- $index: index,
159
- $item: item,
160
- [itemNameToUse]: item,
161
- [`$${itemNameToUse}Index`]: index,
162
- },
163
- },
164
- block: blockWithoutRepeat,
165
- }));
166
- return repeatArray;
167
- } else {
148
+ if (!Array.isArray(itemsArray)) {
168
149
  return undefined;
169
150
  }
151
+
152
+ const collectionName = repeat.collection.split(".").pop();
153
+ const itemNameToUse =
154
+ repeat.itemName || (collectionName ? collectionName + "Item" : "item");
155
+ const repeatArray = itemsArray.map((item, index) => ({
156
+ context: {
157
+ ...builderContext,
158
+ state: {
159
+ ...builderContext.state,
160
+ $index: index,
161
+ $item: item,
162
+ [itemNameToUse]: item,
163
+ [`$${itemNameToUse}Index`]: index,
164
+ },
165
+ },
166
+ block: blockWithoutRepeat,
167
+ }));
168
+ return repeatArray;
170
169
  };
171
170
 
172
171
  let builderContext = getContext(BuilderContext.key);
@@ -182,7 +181,9 @@
182
181
  block={data.block}
183
182
  />
184
183
  {/each}
185
- {:else}
184
+ {/if}
185
+
186
+ {#if !repeatItemData()}
186
187
  <RenderComponent {...renderComponentProps()} />
187
188
  {/if}
188
189
 
File without changes
@@ -7,16 +7,11 @@
7
7
  import { TARGET } from "../../constants/target.js";
8
8
  import BuilderContext from "../../context/builder.context";
9
9
  import { evaluate } from "../../functions/evaluate.js";
10
- import {
11
- convertSearchParamsToQueryObject,
12
- getBuilderSearchParams,
13
- } from "../../functions/get-builder-search-params/index.js";
14
10
  import { getContent } from "../../functions/get-content/index.js";
15
11
  import { getFetch } from "../../functions/get-fetch.js";
16
12
  import { isBrowser } from "../../functions/is-browser.js";
17
13
  import { isEditing } from "../../functions/is-editing.js";
18
14
  import { isPreviewing } from "../../functions/is-previewing.js";
19
- import { previewingModelName } from "../../functions/previewing-model-name.js";
20
15
  import {
21
16
  components,
22
17
  createRegisterComponentMessage,
@@ -163,6 +158,14 @@
163
158
  return {};
164
159
  };
165
160
 
161
+ $: shouldRenderContentStyles = () => {
162
+ return Boolean(
163
+ (useContent?.()?.data?.cssCode ||
164
+ useContent?.()?.data?.customFonts?.length) &&
165
+ TARGET !== "reactNative"
166
+ );
167
+ };
168
+
166
169
  setContext(BuilderContext.key, {
167
170
  get content() {
168
171
  return useContent();
@@ -207,17 +210,16 @@
207
210
  } // override normal content in preview mode
208
211
 
209
212
  if (isPreviewing()) {
210
- if (model && previewingModelName() === model) {
211
- const currentUrl = new URL(location.href);
212
- const previewApiKey = currentUrl.searchParams.get("apiKey");
213
+ const searchParams = new URL(location.href).searchParams;
214
+
215
+ if (model && searchParams.get("builder.preview") === model) {
216
+ const previewApiKey =
217
+ searchParams.get("apiKey") || searchParams.get("builder.space");
213
218
 
214
219
  if (previewApiKey) {
215
220
  getContent({
216
221
  model: model,
217
222
  apiKey: previewApiKey,
218
- options: getBuilderSearchParams(
219
- convertSearchParamsToQueryObject(currentUrl.searchParams)
220
- ),
221
223
  }).then((content) => {
222
224
  if (content) {
223
225
  overrideContent = content;
@@ -266,10 +268,10 @@
266
268
  }}
267
269
  data-builder-content-id={useContent?.()?.id}
268
270
  >
269
- {#if (useContent?.()?.data?.cssCode || useContent?.()?.data?.customFonts?.length) && TARGET !== "reactNative"}
271
+ {#if shouldRenderContentStyles()}
270
272
  <RenderContentStyles
271
- cssCode={useContent().data.cssCode}
272
- customFonts={useContent().data.customFonts}
273
+ cssCode={useContent?.()?.data?.cssCode}
274
+ customFonts={useContent?.()?.data?.customFonts}
273
275
  />
274
276
  {/if}
275
277
 
@@ -1,3 +1,4 @@
1
+ import { isBrowser } from "../is-browser";
1
2
  const BUILDER_SEARCHPARAMS_PREFIX = "builder.";
2
3
  const convertSearchParamsToQueryObject = (searchParams) => {
3
4
  const options = {};
@@ -16,7 +17,17 @@ const getBuilderSearchParams = (options) => {
16
17
  });
17
18
  return newOptions;
18
19
  };
20
+ const getBuilderSearchParamsFromWindow = () => {
21
+ if (!isBrowser()) {
22
+ return {};
23
+ }
24
+ const searchParams = new URLSearchParams(window.location.search);
25
+ return getBuilderSearchParams(convertSearchParamsToQueryObject(searchParams));
26
+ };
27
+ const normalizeSearchParams = (searchParams) => searchParams instanceof URLSearchParams ? convertSearchParamsToQueryObject(searchParams) : searchParams;
19
28
  export {
20
29
  convertSearchParamsToQueryObject,
21
- getBuilderSearchParams
30
+ getBuilderSearchParams,
31
+ getBuilderSearchParamsFromWindow,
32
+ normalizeSearchParams
22
33
  };
@@ -0,0 +1,38 @@
1
+ const handleABTesting = (item, testGroups) => {
2
+ if (item.variations && Object.keys(item.variations).length) {
3
+ const testGroup = item.id ? testGroups[item.id] : void 0;
4
+ const variationValue = testGroup ? item.variations[testGroup] : void 0;
5
+ if (testGroup && variationValue) {
6
+ item.data = variationValue.data;
7
+ item.testVariationId = variationValue.id;
8
+ item.testVariationName = variationValue.name;
9
+ } else {
10
+ let n = 0;
11
+ const random = Math.random();
12
+ let set = false;
13
+ for (const id in item.variations) {
14
+ const variation = item.variations[id];
15
+ const testRatio = variation.testRatio;
16
+ n += testRatio;
17
+ if (random < n) {
18
+ const variationName = variation.name || (variation.id === item.id ? "Default variation" : "");
19
+ set = true;
20
+ Object.assign(item, {
21
+ data: variation.data,
22
+ testVariationId: variation.id,
23
+ testVariationName: variationName
24
+ });
25
+ }
26
+ }
27
+ if (!set) {
28
+ Object.assign(item, {
29
+ testVariationId: item.id,
30
+ testVariationName: "Default"
31
+ });
32
+ }
33
+ }
34
+ }
35
+ };
36
+ export {
37
+ handleABTesting
38
+ };
@@ -37,20 +37,14 @@ var __async = (__this, __arguments, generator) => {
37
37
  step((generator = generator.apply(__this, __arguments)).next());
38
38
  });
39
39
  };
40
+ import { flatten } from "../../helpers/flatten.js";
41
+ import {
42
+ getBuilderSearchParamsFromWindow,
43
+ normalizeSearchParams
44
+ } from "../get-builder-search-params/index.js";
40
45
  import { getFetch } from "../get-fetch.js";
46
+ import { handleABTesting } from "./ab-testing.js";
41
47
  const fetch$ = getFetch();
42
- function flatten(object, path = null, separator = ".") {
43
- return Object.keys(object).reduce((acc, key) => {
44
- const value = object[key];
45
- const newPath = [path, key].filter(Boolean).join(separator);
46
- const isObject = [
47
- typeof value === "object",
48
- value !== null,
49
- !(Array.isArray(value) && value.length === 0)
50
- ].every(Boolean);
51
- return isObject ? __spreadValues(__spreadValues({}, acc), flatten(value, newPath, separator)) : __spreadProps(__spreadValues({}, acc), { [newPath]: value });
52
- }, {});
53
- }
54
48
  function getContent(options) {
55
49
  return __async(this, null, function* () {
56
50
  return (yield getAllContent(__spreadProps(__spreadValues({}, options), { limit: 1 }))).results[0] || null;
@@ -66,67 +60,31 @@ const generateContentUrl = (options) => {
66
60
  apiKey
67
61
  } = options;
68
62
  const url = new URL(`https://cdn.builder.io/api/v2/content/${model}?apiKey=${apiKey}&limit=${limit}&noTraverse=${noTraverse}`);
69
- if (options.options) {
70
- const flattened = flatten(options.options);
71
- for (const key in flattened) {
72
- url.searchParams.set(key, String(flattened[key]));
73
- }
63
+ const queryOptions = __spreadValues(__spreadValues({}, getBuilderSearchParamsFromWindow()), normalizeSearchParams(options.options || {}));
64
+ const flattened = flatten(queryOptions);
65
+ for (const key in flattened) {
66
+ url.searchParams.set(key, String(flattened[key]));
74
67
  }
75
68
  if (userAttributes) {
76
69
  url.searchParams.set("userAttributes", JSON.stringify(userAttributes));
77
70
  }
78
71
  if (query) {
79
- const flattened = flatten({ query });
80
- for (const key in flattened) {
81
- url.searchParams.set(key, JSON.stringify(flattened[key]));
72
+ const flattened2 = flatten({ query });
73
+ for (const key in flattened2) {
74
+ url.searchParams.set(key, JSON.stringify(flattened2[key]));
82
75
  }
83
76
  }
84
77
  return url;
85
78
  };
86
- const handleABTesting = (content, testGroups) => {
87
- for (const item of content.results) {
88
- if (item.variations && Object.keys(item.variations).length) {
89
- const testGroup = testGroups[item.id];
90
- const variationValue = item.variations[testGroup];
91
- if (testGroup && variationValue) {
92
- item.data = variationValue.data;
93
- item.testVariationId = variationValue.id;
94
- item.testVariationName = variationValue.name;
95
- } else {
96
- let n = 0;
97
- const random = Math.random();
98
- let set = false;
99
- for (const id in item.variations) {
100
- const variation = item.variations[id];
101
- const testRatio = variation.testRatio;
102
- n += testRatio;
103
- if (random < n) {
104
- const variationName = variation.name || (variation.id === item.id ? "Default variation" : "");
105
- set = true;
106
- Object.assign(item, {
107
- data: variation.data,
108
- testVariationId: variation.id,
109
- testVariationName: variationName
110
- });
111
- }
112
- }
113
- if (!set) {
114
- Object.assign(item, {
115
- testVariationId: item.id,
116
- testVariationName: "Default"
117
- });
118
- }
119
- }
120
- }
121
- }
122
- };
123
79
  function getAllContent(options) {
124
80
  return __async(this, null, function* () {
125
81
  const url = generateContentUrl(options);
126
82
  const fetch = yield fetch$;
127
83
  const content = yield fetch(url.href).then((res) => res.json());
128
84
  if (options.testGroups) {
129
- handleABTesting(content, options.testGroups);
85
+ for (const item of content.results) {
86
+ handleABTesting(item, options.testGroups);
87
+ }
130
88
  }
131
89
  return content;
132
90
  });
File without changes
@@ -46,12 +46,22 @@ const createRegisterComponentMessage = (_a) => {
46
46
  data: prepareComponentInfoToSend(info)
47
47
  };
48
48
  };
49
- const serializeFn = (fn) => `return (${fn.toString()}).apply(this, arguments)`;
50
- const prepareComponentInfoToSend = (info) => {
51
- var _a;
52
- return __spreadProps(__spreadValues({}, info), {
53
- inputs: (_a = info.inputs) == null ? void 0 : _a.map((input) => Object.entries(input).reduce((acc, [key, value]) => __spreadProps(__spreadValues({}, acc), {
54
- [key]: typeof value === "function" ? serializeFn(value) : value
49
+ const fastClone = (obj) => JSON.parse(JSON.stringify(obj));
50
+ const serializeValue = (value) => typeof value === "function" ? serializeFn(value) : fastClone(value);
51
+ const serializeFn = (fnValue) => {
52
+ const fnStr = fnValue.toString().trim();
53
+ const appendFunction = !fnStr.startsWith("function") && !fnStr.startsWith("(");
54
+ return `return (${appendFunction ? "function " : ""}${fnStr}).apply(this, arguments)`;
55
+ };
56
+ const prepareComponentInfoToSend = (_c) => {
57
+ var _d = _c, {
58
+ inputs
59
+ } = _d, info = __objRest(_d, [
60
+ "inputs"
61
+ ]);
62
+ return __spreadProps(__spreadValues({}, fastClone(info)), {
63
+ inputs: inputs == null ? void 0 : inputs.map((input) => Object.entries(input).reduce((acc, [key, value]) => __spreadProps(__spreadValues({}, acc), {
64
+ [key]: serializeValue(value)
55
65
  }), {}))
56
66
  });
57
67
  };
@@ -0,0 +1,34 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __defProps = Object.defineProperties;
3
+ var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
4
+ var __getOwnPropSymbols = Object.getOwnPropertySymbols;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __propIsEnum = Object.prototype.propertyIsEnumerable;
7
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
8
+ var __spreadValues = (a, b) => {
9
+ for (var prop in b || (b = {}))
10
+ if (__hasOwnProp.call(b, prop))
11
+ __defNormalProp(a, prop, b[prop]);
12
+ if (__getOwnPropSymbols)
13
+ for (var prop of __getOwnPropSymbols(b)) {
14
+ if (__propIsEnum.call(b, prop))
15
+ __defNormalProp(a, prop, b[prop]);
16
+ }
17
+ return a;
18
+ };
19
+ var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
20
+ function flatten(object, path = null, separator = ".") {
21
+ return Object.keys(object).reduce((acc, key) => {
22
+ const value = object[key];
23
+ const newPath = [path, key].filter(Boolean).join(separator);
24
+ const isObject = [
25
+ typeof value === "object",
26
+ value !== null,
27
+ !(Array.isArray(value) && value.length === 0)
28
+ ].every(Boolean);
29
+ return isObject ? __spreadValues(__spreadValues({}, acc), flatten(value, newPath, separator)) : __spreadProps(__spreadValues({}, acc), { [newPath]: value });
30
+ }, {});
31
+ }
32
+ export {
33
+ flatten
34
+ };
@@ -1,11 +0,0 @@
1
- import { isPreviewing } from "./is-previewing.js";
2
- function previewingModelName() {
3
- if (!isPreviewing()) {
4
- return null;
5
- }
6
- const url = new URL(location.href);
7
- return url.searchParams.get("builder.preview");
8
- }
9
- export {
10
- previewingModelName
11
- };