@builder.io/sdk-svelte 3.0.0 → 3.0.2
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/browser/blocks/image/component-info.js +2 -1
- package/lib/browser/blocks/image/image.svelte +2 -1
- package/lib/browser/blocks/img/component-info.js +2 -1
- package/lib/browser/blocks/text/text.svelte +2 -15
- package/lib/browser/blocks/text/text.svelte.d.ts +0 -1
- package/lib/browser/blocks/video/component-info.js +3 -2
- package/lib/browser/components/block/block.helpers.d.ts +6 -3
- package/lib/browser/components/block/block.helpers.js +16 -5
- package/lib/browser/components/block/block.svelte +3 -3
- package/lib/browser/components/content/components/enable-editor.helpers.d.ts +11 -0
- package/lib/browser/components/content/components/enable-editor.helpers.js +15 -0
- package/lib/browser/components/content/components/enable-editor.svelte +37 -8
- package/lib/browser/components/content/components/enable-editor.svelte.d.ts +1 -1
- package/lib/browser/components/content/content.svelte +4 -15
- package/lib/browser/components/content/content.svelte.d.ts +1 -1
- package/lib/browser/constants/file-types.d.ts +2 -0
- package/lib/browser/constants/file-types.js +2 -0
- package/lib/browser/constants/sdk-version.d.ts +1 -1
- package/lib/browser/constants/sdk-version.js +1 -1
- package/lib/browser/context/builder.context.d.ts +1 -0
- package/lib/browser/context/builder.context.js +1 -0
- package/lib/browser/context/types.d.ts +1 -0
- package/lib/browser/functions/evaluate/edge-runtime/edge-runtime.js +1 -4
- package/lib/browser/functions/extract-localized-values.d.ts +2 -0
- package/lib/browser/functions/extract-localized-values.js +37 -0
- package/lib/browser/functions/get-block-component-options.d.ts +2 -1
- package/lib/browser/functions/get-block-component-options.js +18 -2
- package/lib/browser/functions/get-content/generate-content-url.js +28 -3
- package/lib/browser/functions/get-processed-block.d.ts +1 -6
- package/lib/browser/functions/get-processed-block.js +11 -14
- package/lib/browser/functions/register-component.js +4 -1
- package/lib/browser/functions/traverse.d.ts +34 -0
- package/lib/browser/functions/traverse.js +63 -0
- package/lib/browser/helpers/flatten.d.ts +6 -0
- package/lib/browser/helpers/flatten.js +23 -0
- package/lib/browser/server-index.d.ts +1 -1
- package/lib/browser/types/components.d.ts +3 -0
- package/lib/edge/blocks/image/component-info.js +2 -1
- package/lib/edge/blocks/image/image.svelte +2 -1
- package/lib/edge/blocks/img/component-info.js +2 -1
- package/lib/edge/blocks/text/text.svelte +2 -15
- package/lib/edge/blocks/text/text.svelte.d.ts +0 -1
- package/lib/edge/blocks/video/component-info.js +3 -2
- package/lib/edge/components/block/block.helpers.d.ts +6 -3
- package/lib/edge/components/block/block.helpers.js +16 -5
- package/lib/edge/components/block/block.svelte +3 -3
- package/lib/edge/components/content/components/enable-editor.helpers.d.ts +11 -0
- package/lib/edge/components/content/components/enable-editor.helpers.js +15 -0
- package/lib/edge/components/content/components/enable-editor.svelte +37 -8
- package/lib/edge/components/content/components/enable-editor.svelte.d.ts +1 -1
- package/lib/edge/components/content/content.svelte +4 -15
- package/lib/edge/components/content/content.svelte.d.ts +1 -1
- package/lib/edge/constants/file-types.d.ts +2 -0
- package/lib/edge/constants/file-types.js +2 -0
- package/lib/edge/constants/sdk-version.d.ts +1 -1
- package/lib/edge/constants/sdk-version.js +1 -1
- package/lib/edge/context/builder.context.d.ts +1 -0
- package/lib/edge/context/builder.context.js +1 -0
- package/lib/edge/context/types.d.ts +1 -0
- package/lib/edge/functions/evaluate/edge-runtime/edge-runtime.js +1 -4
- package/lib/edge/functions/extract-localized-values.d.ts +2 -0
- package/lib/edge/functions/extract-localized-values.js +37 -0
- package/lib/edge/functions/get-block-component-options.d.ts +2 -1
- package/lib/edge/functions/get-block-component-options.js +18 -2
- package/lib/edge/functions/get-content/generate-content-url.js +28 -3
- package/lib/edge/functions/get-processed-block.d.ts +1 -6
- package/lib/edge/functions/get-processed-block.js +11 -14
- package/lib/edge/functions/register-component.js +4 -1
- package/lib/edge/functions/traverse.d.ts +34 -0
- package/lib/edge/functions/traverse.js +63 -0
- package/lib/edge/helpers/flatten.d.ts +6 -0
- package/lib/edge/helpers/flatten.js +23 -0
- package/lib/edge/server-index.d.ts +1 -1
- package/lib/edge/types/components.d.ts +3 -0
- package/lib/node/blocks/image/component-info.js +2 -1
- package/lib/node/blocks/image/image.svelte +2 -1
- package/lib/node/blocks/img/component-info.js +2 -1
- package/lib/node/blocks/text/text.svelte +2 -15
- package/lib/node/blocks/text/text.svelte.d.ts +0 -1
- package/lib/node/blocks/video/component-info.js +3 -2
- package/lib/node/components/block/block.helpers.d.ts +6 -3
- package/lib/node/components/block/block.helpers.js +16 -5
- package/lib/node/components/block/block.svelte +3 -3
- package/lib/node/components/content/components/enable-editor.helpers.d.ts +11 -0
- package/lib/node/components/content/components/enable-editor.helpers.js +15 -0
- package/lib/node/components/content/components/enable-editor.svelte +37 -8
- package/lib/node/components/content/components/enable-editor.svelte.d.ts +1 -1
- package/lib/node/components/content/content.svelte +4 -15
- package/lib/node/components/content/content.svelte.d.ts +1 -1
- package/lib/node/constants/file-types.d.ts +2 -0
- package/lib/node/constants/file-types.js +2 -0
- package/lib/node/constants/sdk-version.d.ts +1 -1
- package/lib/node/constants/sdk-version.js +1 -1
- package/lib/node/context/builder.context.d.ts +1 -0
- package/lib/node/context/builder.context.js +1 -0
- package/lib/node/context/types.d.ts +1 -0
- package/lib/node/functions/evaluate/edge-runtime/edge-runtime.js +1 -4
- package/lib/node/functions/extract-localized-values.d.ts +2 -0
- package/lib/node/functions/extract-localized-values.js +37 -0
- package/lib/node/functions/get-block-component-options.d.ts +2 -1
- package/lib/node/functions/get-block-component-options.js +18 -2
- package/lib/node/functions/get-content/generate-content-url.js +28 -3
- package/lib/node/functions/get-processed-block.d.ts +1 -6
- package/lib/node/functions/get-processed-block.js +11 -14
- package/lib/node/functions/register-component.js +4 -1
- package/lib/node/functions/traverse.d.ts +34 -0
- package/lib/node/functions/traverse.js +63 -0
- package/lib/node/helpers/flatten.d.ts +6 -0
- package/lib/node/helpers/flatten.js +23 -0
- package/lib/node/server-index.d.ts +1 -1
- package/lib/node/types/components.d.ts +3 -0
- package/package.json +1 -1
- package/src/blocks/image/component-info.ts +2 -1
- package/src/blocks/image/image.svelte +4 -1
- package/src/blocks/img/component-info.ts +2 -1
- package/src/blocks/text/text.svelte +1 -24
- package/src/blocks/video/component-info.ts +3 -2
- package/src/components/block/block.helpers.ts +18 -6
- package/src/components/block/block.svelte +7 -3
- package/src/components/content/components/enable-editor.helpers.ts +18 -0
- package/src/components/content/components/enable-editor.svelte +40 -8
- package/src/components/content/content.svelte +4 -11
- package/src/constants/file-types.ts +2 -0
- package/src/constants/sdk-version.ts +1 -1
- package/src/context/builder.context.ts +1 -0
- package/src/context/types.ts +1 -0
- package/src/functions/evaluate/edge-runtime/edge-runtime.ts +1 -5
- package/src/functions/extract-localized-values.ts +38 -0
- package/src/functions/get-block-component-options.ts +19 -2
- package/src/functions/get-content/generate-content-url.ts +30 -3
- package/src/functions/get-processed-block.ts +10 -18
- package/src/functions/register-component.ts +4 -1
- package/src/functions/traverse.ts +64 -0
- package/src/helpers/flatten.ts +23 -0
- package/src/server-index.ts +1 -1
- package/src/types/components.ts +3 -0
|
@@ -18,6 +18,7 @@ import { createEditorListener } from "../../../helpers/subscribe-to-editor";
|
|
|
18
18
|
import { registerInsertMenu, setupBrowserForEditing, } from "../../../scripts/init-editing";
|
|
19
19
|
import { triggerAnimation } from "../../block/animator";
|
|
20
20
|
import "../../dynamic-div.svelte";
|
|
21
|
+
import { needsElementRefDivForEditing } from "./enable-editor.helpers";
|
|
21
22
|
import { getWrapperClassName } from "./styles.helpers";
|
|
22
23
|
export let isNestedRender = undefined;
|
|
23
24
|
export let builderContextSignal;
|
|
@@ -29,11 +30,21 @@ export let enrich = undefined;
|
|
|
29
30
|
export let trustedHosts = undefined;
|
|
30
31
|
export let model = undefined;
|
|
31
32
|
export let content = undefined;
|
|
33
|
+
export let context = undefined;
|
|
32
34
|
export let data = undefined;
|
|
33
35
|
export let showContent;
|
|
34
36
|
export let contentWrapper = undefined;
|
|
35
|
-
export let context = undefined;
|
|
36
37
|
export let contentWrapperProps = undefined;
|
|
38
|
+
function stringifyStyles(stylesObj) {
|
|
39
|
+
let styles = "";
|
|
40
|
+
for (let key in stylesObj) {
|
|
41
|
+
const dashedKey = key.replace(/[A-Z]/g, function (match) {
|
|
42
|
+
return "-" + match.toLowerCase();
|
|
43
|
+
});
|
|
44
|
+
styles += dashedKey + ":" + stylesObj[key] + ";";
|
|
45
|
+
}
|
|
46
|
+
return styles;
|
|
47
|
+
}
|
|
37
48
|
function mergeNewRootState(newData) {
|
|
38
49
|
const combinedState = {
|
|
39
50
|
...$builderContextSignal.rootState,
|
|
@@ -178,8 +189,12 @@ function elementRef_onIniteditingbldr(event) {
|
|
|
178
189
|
: {}),
|
|
179
190
|
});
|
|
180
191
|
Object.values($builderContextSignal.componentInfos).forEach((registeredComponent) => {
|
|
181
|
-
|
|
182
|
-
|
|
192
|
+
if (!model ||
|
|
193
|
+
!registeredComponent.models?.length ||
|
|
194
|
+
registeredComponent.models.includes(model)) {
|
|
195
|
+
const message = createRegisterComponentMessage(registeredComponent);
|
|
196
|
+
window.parent?.postMessage(message, "*");
|
|
197
|
+
}
|
|
183
198
|
});
|
|
184
199
|
window.addEventListener("builder:component:stateChangeListenerActivated", emitStateUpdate);
|
|
185
200
|
}
|
|
@@ -197,13 +212,21 @@ function elementRef_onInitpreviewingbldr(event) {
|
|
|
197
212
|
*
|
|
198
213
|
* TO-DO: should we only update the state when there is a change?
|
|
199
214
|
**/
|
|
200
|
-
if (searchParamPreviewModel ===
|
|
201
|
-
|
|
202
|
-
|
|
215
|
+
if (searchParamPreviewModel === "BUILDER_STUDIO" ||
|
|
216
|
+
(searchParamPreviewModel === model &&
|
|
217
|
+
previewApiKey === apiKey &&
|
|
218
|
+
(!content || searchParamPreviewId === content.id))) {
|
|
203
219
|
fetchOneEntry({
|
|
204
|
-
model: model,
|
|
220
|
+
model: model || "",
|
|
205
221
|
apiKey: apiKey,
|
|
206
222
|
apiVersion: $builderContextSignal.apiVersion,
|
|
223
|
+
...(searchParamPreviewModel === "BUILDER_STUDIO" && context?.symbolId
|
|
224
|
+
? {
|
|
225
|
+
query: {
|
|
226
|
+
id: context.symbolId,
|
|
227
|
+
},
|
|
228
|
+
}
|
|
229
|
+
: {}),
|
|
207
230
|
}).then((content) => {
|
|
208
231
|
if (content) {
|
|
209
232
|
mergeNewContent(content);
|
|
@@ -293,9 +316,15 @@ onDestroy(() => {
|
|
|
293
316
|
});
|
|
294
317
|
</script>
|
|
295
318
|
|
|
296
|
-
{#if $builderContextSignal.content}
|
|
319
|
+
{#if $builderContextSignal.content || needsElementRefDivForEditing()}
|
|
297
320
|
<svelte:element
|
|
298
321
|
this={ContentWrapper}
|
|
322
|
+
style={stringifyStyles({
|
|
323
|
+
display:
|
|
324
|
+
!$builderContextSignal.content && needsElementRefDivForEditing()
|
|
325
|
+
? "none"
|
|
326
|
+
: undefined,
|
|
327
|
+
})}
|
|
299
328
|
{...{}}
|
|
300
329
|
bind:this={elementRef}
|
|
301
330
|
on:click={(event) => {
|
|
@@ -19,10 +19,10 @@ declare const __propDef: {
|
|
|
19
19
|
trustedHosts?: BuilderEditorProps["trustedHosts"];
|
|
20
20
|
model?: BuilderEditorProps["model"];
|
|
21
21
|
content?: BuilderEditorProps["content"];
|
|
22
|
+
context?: BuilderEditorProps["context"];
|
|
22
23
|
data?: BuilderEditorProps["data"];
|
|
23
24
|
showContent: BuilderEditorProps["showContent"];
|
|
24
25
|
contentWrapper?: BuilderEditorProps["contentWrapper"];
|
|
25
|
-
context?: BuilderEditorProps["context"];
|
|
26
26
|
contentWrapperProps?: BuilderEditorProps["contentWrapperProps"];
|
|
27
27
|
};
|
|
28
28
|
events: {
|
|
@@ -18,7 +18,6 @@ export let apiKey;
|
|
|
18
18
|
export let context = undefined;
|
|
19
19
|
export let content = undefined;
|
|
20
20
|
export let customComponents = undefined;
|
|
21
|
-
export let model = undefined;
|
|
22
21
|
export let data = undefined;
|
|
23
22
|
export let locale = undefined;
|
|
24
23
|
export let canTrack = undefined;
|
|
@@ -26,6 +25,7 @@ export let apiVersion = undefined;
|
|
|
26
25
|
export let blocksWrapper = undefined;
|
|
27
26
|
export let blocksWrapperProps = undefined;
|
|
28
27
|
export let nonce = undefined;
|
|
28
|
+
export let model = undefined;
|
|
29
29
|
export let apiHost = undefined;
|
|
30
30
|
export let enrich = undefined;
|
|
31
31
|
export let showContent;
|
|
@@ -46,13 +46,7 @@ let scriptStr = getUpdateVariantVisibilityScript({
|
|
|
46
46
|
});
|
|
47
47
|
let registeredComponents = [
|
|
48
48
|
...getDefaultRegisteredComponents(),
|
|
49
|
-
...(customComponents
|
|
50
|
-
if (!models?.length)
|
|
51
|
-
return true;
|
|
52
|
-
if (!model)
|
|
53
|
-
return true;
|
|
54
|
-
return models.includes(model);
|
|
55
|
-
}) || []),
|
|
49
|
+
...(customComponents || []),
|
|
56
50
|
].reduce((acc, { component, ...info }) => ({
|
|
57
51
|
...acc,
|
|
58
52
|
[info.name]: {
|
|
@@ -78,13 +72,7 @@ let builderContextSignal = writable({
|
|
|
78
72
|
apiVersion: apiVersion,
|
|
79
73
|
componentInfos: [
|
|
80
74
|
...getDefaultRegisteredComponents(),
|
|
81
|
-
...(customComponents
|
|
82
|
-
if (!models?.length)
|
|
83
|
-
return true;
|
|
84
|
-
if (!model)
|
|
85
|
-
return true;
|
|
86
|
-
return models.includes(model);
|
|
87
|
-
}) || []),
|
|
75
|
+
...(customComponents || []),
|
|
88
76
|
].reduce((acc, { component: _, ...info }) => ({
|
|
89
77
|
...acc,
|
|
90
78
|
[info.name]: serializeIncludingFunctions(info),
|
|
@@ -93,6 +81,7 @@ let builderContextSignal = writable({
|
|
|
93
81
|
BlocksWrapper: blocksWrapper || "div",
|
|
94
82
|
BlocksWrapperProps: blocksWrapperProps || {},
|
|
95
83
|
nonce: nonce || "",
|
|
84
|
+
model: model || "",
|
|
96
85
|
});
|
|
97
86
|
if (!apiKey) {
|
|
98
87
|
logger.error("No API key provided to `Content` component. This can cause issues. Please provide an API key using the `apiKey` prop.");
|
|
@@ -6,7 +6,6 @@ declare const __propDef: {
|
|
|
6
6
|
context?: ContentProps["context"];
|
|
7
7
|
content?: ContentProps["content"];
|
|
8
8
|
customComponents?: ContentProps["customComponents"];
|
|
9
|
-
model?: ContentProps["model"];
|
|
10
9
|
data?: ContentProps["data"];
|
|
11
10
|
locale?: ContentProps["locale"];
|
|
12
11
|
canTrack?: ContentProps["canTrack"];
|
|
@@ -14,6 +13,7 @@ declare const __propDef: {
|
|
|
14
13
|
blocksWrapper?: ContentProps["blocksWrapper"];
|
|
15
14
|
blocksWrapperProps?: ContentProps["blocksWrapperProps"];
|
|
16
15
|
nonce?: ContentProps["nonce"];
|
|
16
|
+
model?: ContentProps["model"];
|
|
17
17
|
apiHost?: ContentProps["apiHost"];
|
|
18
18
|
enrich?: ContentProps["enrich"];
|
|
19
19
|
showContent: ContentProps["showContent"];
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
export const IMAGE_FILE_TYPES = ['jpeg', 'jpg', 'png', 'svg', 'webp', 'gif', 'jfif', 'pjpeg', 'pjp', 'apng', 'avif', 'tif', 'tiff', 'heif', 'bmp', 'eps', 'raw', 'cr2', 'nef', 'orf', 'sr2', 'psd', 'heic', 'dib', 'ai'];
|
|
2
|
+
export const VIDEO_FILE_TYPES = ['mp4', 'webm', 'mkv', 'flv', 'vob', 'ogv', 'ogg', 'drc', 'gif', 'gifv', 'mng', 'avi', 'mov', 'qt', 'mts', 'm2ts', 'ts', 'wmv', 'yuv', 'rm', 'rmvb', 'viv', 'asf', 'amv', 'm4p', 'mpeg', 'mpe', 'm2v', 'm4v', 'svi', '3gp', '3g2', 'mxf', 'roq', 'nsv', 'f4v', 'f4p', 'f4a', 'f4b'];
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare const SDK_VERSION = "3.0.
|
|
1
|
+
export declare const SDK_VERSION = "3.0.2";
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export const SDK_VERSION = "3.0.
|
|
1
|
+
export const SDK_VERSION = "3.0.2";
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { logger } from '../../../helpers/logger';
|
|
2
1
|
import { flattenState, getFunctionArguments } from '../helpers';
|
|
3
2
|
import Interpreter from './acorn-interpreter';
|
|
4
3
|
/**
|
|
@@ -84,10 +83,8 @@ export const runInEdge = ({ builder, context, event, rootState, localState, root
|
|
|
84
83
|
return `var ${key} = ${jsonValName} === undefined ? undefined : JSON.parse(${jsonValName});`;
|
|
85
84
|
}).join('\n');
|
|
86
85
|
const cleanedCode = processCode(code);
|
|
87
|
-
if (cleanedCode === '')
|
|
88
|
-
logger.warn('Skipping evaluation of empty code block.');
|
|
86
|
+
if (cleanedCode === '')
|
|
89
87
|
return;
|
|
90
|
-
}
|
|
91
88
|
const transformed = `
|
|
92
89
|
function theFunction() {
|
|
93
90
|
${prependedCode}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { traverse } from './traverse';
|
|
2
|
+
function isLocalizedField(value) {
|
|
3
|
+
return value && typeof value === 'object' && value['@type'] === '@builder.io/core:LocalizedValue';
|
|
4
|
+
}
|
|
5
|
+
function containsLocalizedValues(data) {
|
|
6
|
+
if (!data || !Object.getOwnPropertyNames(data).length) {
|
|
7
|
+
return false;
|
|
8
|
+
}
|
|
9
|
+
let hasLocalizedValues = false;
|
|
10
|
+
traverse(data, value => {
|
|
11
|
+
if (isLocalizedField(value)) {
|
|
12
|
+
hasLocalizedValues = true;
|
|
13
|
+
return;
|
|
14
|
+
}
|
|
15
|
+
});
|
|
16
|
+
return hasLocalizedValues;
|
|
17
|
+
}
|
|
18
|
+
function extractLocalizedValues(data, locale) {
|
|
19
|
+
if (!data || !Object.getOwnPropertyNames(data).length) {
|
|
20
|
+
return {};
|
|
21
|
+
}
|
|
22
|
+
traverse(data, (value, update) => {
|
|
23
|
+
if (isLocalizedField(value)) {
|
|
24
|
+
update(value[locale] ?? undefined);
|
|
25
|
+
}
|
|
26
|
+
});
|
|
27
|
+
return data;
|
|
28
|
+
}
|
|
29
|
+
export function resolveLocalizedValues(block, locale) {
|
|
30
|
+
if (block.component?.options && containsLocalizedValues(block.component?.options)) {
|
|
31
|
+
if (!locale) {
|
|
32
|
+
console.warn('[Builder.io] In order to use localized fields in Builder, you must pass a locale prop to the BuilderComponent or to options object while fetching the content to resolve localized fields. Learn more: https://www.builder.io/c/docs/localization-inline#targeting-and-inline-localization');
|
|
33
|
+
}
|
|
34
|
+
block.component.options = extractLocalizedValues(block.component.options, locale ?? 'Default');
|
|
35
|
+
}
|
|
36
|
+
return block;
|
|
37
|
+
}
|
|
@@ -1,2 +1,3 @@
|
|
|
1
|
+
import type { BuilderContextInterface } from '../context/types';
|
|
1
2
|
import type { BuilderBlock } from '../types/builder-block';
|
|
2
|
-
export declare function getBlockComponentOptions(block: BuilderBlock): any;
|
|
3
|
+
export declare function getBlockComponentOptions(block: BuilderBlock, context: Pick<BuilderContextInterface, 'localState' | 'context' | 'rootState' | 'rootSetState'>): any;
|
|
@@ -1,6 +1,22 @@
|
|
|
1
|
-
|
|
1
|
+
import { evaluate } from './evaluate/evaluate';
|
|
2
|
+
export function getBlockComponentOptions(block, context) {
|
|
2
3
|
return {
|
|
3
4
|
...block.component?.options,
|
|
4
|
-
...block.options
|
|
5
|
+
...block.options,
|
|
6
|
+
...evaluateTextComponentTextOption(block, context)
|
|
5
7
|
};
|
|
6
8
|
}
|
|
9
|
+
const evaluateTextComponentTextOption = (block, context) => {
|
|
10
|
+
if (block.component?.name === 'Text' && block.component.options?.text && typeof block.component.options.text === 'string') {
|
|
11
|
+
return {
|
|
12
|
+
...block.component.options,
|
|
13
|
+
text: block.component.options.text.replace(/{{([^}]+)}}/g, (_match, group) => evaluate({
|
|
14
|
+
code: group,
|
|
15
|
+
context,
|
|
16
|
+
localState: context.localState,
|
|
17
|
+
rootState: context.rootState,
|
|
18
|
+
rootSetState: context.rootSetState
|
|
19
|
+
}))
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
};
|
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
import { flatten, flattenMongoQuery } from '../../helpers/flatten';
|
|
1
|
+
import { flatten, flattenMongoQuery, unflatten } from '../../helpers/flatten';
|
|
2
2
|
import { normalizeSearchParams } from '../../helpers/search/search';
|
|
3
3
|
import { DEFAULT_API_VERSION } from '../../types/api-version';
|
|
4
4
|
import { getBuilderSearchParamsFromWindow } from '../get-builder-search-params/index';
|
|
5
|
+
import { isBrowser } from '../is-browser';
|
|
5
6
|
const isPositiveNumber = (thing) => typeof thing === 'number' && !isNaN(thing) && thing >= 0;
|
|
6
7
|
export const generateContentUrl = (options) => {
|
|
7
8
|
const { limit = 30, userAttributes, query, model, apiKey, enrich, locale, apiVersion = DEFAULT_API_VERSION, fields, omit, offset, cacheSeconds, staleCacheSeconds, sort, includeUnpublished, apiHost } = options;
|
|
@@ -20,7 +21,7 @@ export const generateContentUrl = (options) => {
|
|
|
20
21
|
url.searchParams.set('noTraverse', String(noTraverse));
|
|
21
22
|
url.searchParams.set('includeRefs', String(true));
|
|
22
23
|
const finalLocale = locale || userAttributes?.locale;
|
|
23
|
-
let finalUserAttributes = userAttributes;
|
|
24
|
+
let finalUserAttributes = userAttributes || {};
|
|
24
25
|
if (finalLocale) {
|
|
25
26
|
url.searchParams.set('locale', finalLocale);
|
|
26
27
|
finalUserAttributes = {
|
|
@@ -59,11 +60,15 @@ export const generateContentUrl = (options) => {
|
|
|
59
60
|
...getBuilderSearchParamsFromWindow(),
|
|
60
61
|
...normalizeSearchParams(options.options || {})
|
|
61
62
|
};
|
|
63
|
+
finalUserAttributes = {
|
|
64
|
+
...finalUserAttributes,
|
|
65
|
+
...getUserAttributesAsJSON(queryOptions)
|
|
66
|
+
};
|
|
62
67
|
const flattened = flatten(queryOptions);
|
|
63
68
|
for (const key in flattened) {
|
|
64
69
|
url.searchParams.set(key, String(flattened[key]));
|
|
65
70
|
}
|
|
66
|
-
if (finalUserAttributes) {
|
|
71
|
+
if (Object.keys(finalUserAttributes).length > 0) {
|
|
67
72
|
url.searchParams.set('userAttributes', JSON.stringify(finalUserAttributes));
|
|
68
73
|
}
|
|
69
74
|
if (query) {
|
|
@@ -76,3 +81,23 @@ export const generateContentUrl = (options) => {
|
|
|
76
81
|
}
|
|
77
82
|
return url;
|
|
78
83
|
};
|
|
84
|
+
const getUserAttributesFromQueryOptions = (queryOptions) => {
|
|
85
|
+
const newUserAttributes = {};
|
|
86
|
+
for (const key in queryOptions) {
|
|
87
|
+
if (key.startsWith('userAttributes.')) {
|
|
88
|
+
newUserAttributes[key] = queryOptions[key];
|
|
89
|
+
delete queryOptions[key];
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
return newUserAttributes;
|
|
93
|
+
};
|
|
94
|
+
const getUserAttributesAsJSON = (queryOptions) => {
|
|
95
|
+
if (isBrowser() && queryOptions['preview'] === 'BUILDER_STUDIO') {
|
|
96
|
+
queryOptions['userAttributes.urlPath'] = window.location.pathname;
|
|
97
|
+
queryOptions['userAttributes.host'] = window.location.host;
|
|
98
|
+
const queryOptionsForUserAttributes = getUserAttributesFromQueryOptions(queryOptions);
|
|
99
|
+
const { userAttributes } = unflatten(queryOptionsForUserAttributes);
|
|
100
|
+
return userAttributes;
|
|
101
|
+
}
|
|
102
|
+
return {};
|
|
103
|
+
};
|
|
@@ -1,11 +1,6 @@
|
|
|
1
1
|
import type { BuilderContextInterface } from '../context/types';
|
|
2
2
|
import type { BuilderBlock } from '../types/builder-block';
|
|
3
3
|
export declare function deepCloneWithConditions<T = any>(obj: T): T;
|
|
4
|
-
export declare function getProcessedBlock({ block, context,
|
|
4
|
+
export declare function getProcessedBlock({ block, context, localState, rootState, rootSetState }: {
|
|
5
5
|
block: BuilderBlock;
|
|
6
|
-
/**
|
|
7
|
-
* In some cases, we want to avoid evaluating bindings and only want framework-specific block transformation. It is
|
|
8
|
-
* also sometimes too early to consider bindings, e.g. when we might be looking at a repeated block.
|
|
9
|
-
*/
|
|
10
|
-
shouldEvaluateBindings: boolean;
|
|
11
6
|
} & Pick<BuilderContextInterface, 'localState' | 'context' | 'rootState' | 'rootSetState'>): BuilderBlock;
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { TARGET } from '../constants/target';
|
|
2
2
|
import { omit } from '../helpers/omit';
|
|
3
3
|
import { evaluate } from './evaluate/index';
|
|
4
|
+
import { resolveLocalizedValues } from './extract-localized-values';
|
|
4
5
|
import { fastClone } from './fast-clone';
|
|
5
6
|
import { set } from './set';
|
|
6
7
|
import { transformBlock } from './transform-block';
|
|
@@ -71,18 +72,14 @@ const evaluateBindings = ({ block, context, localState, rootState, rootSetState
|
|
|
71
72
|
}
|
|
72
73
|
return copied;
|
|
73
74
|
};
|
|
74
|
-
export function getProcessedBlock({ block, context,
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
}
|
|
85
|
-
else {
|
|
86
|
-
return transformedBlock;
|
|
87
|
-
}
|
|
75
|
+
export function getProcessedBlock({ block, context, localState, rootState, rootSetState }) {
|
|
76
|
+
let transformedBlock = resolveLocalizedValues(block, rootState.locale);
|
|
77
|
+
transformedBlock = transformBlock(transformedBlock);
|
|
78
|
+
return evaluateBindings({
|
|
79
|
+
block: transformedBlock,
|
|
80
|
+
localState,
|
|
81
|
+
rootState,
|
|
82
|
+
rootSetState,
|
|
83
|
+
context
|
|
84
|
+
});
|
|
88
85
|
}
|
|
@@ -10,8 +10,11 @@ const serializeFn = (fnValue) => {
|
|
|
10
10
|
// 2. `name(args) => {code}`
|
|
11
11
|
// 3. `(args) => {}`
|
|
12
12
|
// 4. `args => {}`
|
|
13
|
+
// 5. `async function(args) {code}`
|
|
14
|
+
// 6. `async (args) => {}`
|
|
15
|
+
// 7. `async args => {}`
|
|
13
16
|
const isArrowWithoutParens = /^[a-zA-Z0-9_]+\s*=>/i.test(fnStr);
|
|
14
|
-
const appendFunction = !fnStr.startsWith('function') && !fnStr.startsWith('(') && !isArrowWithoutParens;
|
|
17
|
+
const appendFunction = !fnStr.startsWith('function') && !fnStr.startsWith('async') && !fnStr.startsWith('(') && !isArrowWithoutParens;
|
|
15
18
|
return `return (${appendFunction ? 'function ' : ''}${fnStr}).apply(this, arguments)`;
|
|
16
19
|
};
|
|
17
20
|
export function serializeIncludingFunctions(info) {
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
type TraverseCallback = (value: any, update: (newValue: any) => void) => void;
|
|
2
|
+
/**
|
|
3
|
+
* Recursively traverses an object or array, invoking a callback on each value.
|
|
4
|
+
*
|
|
5
|
+
* @param {any} obj - The object or array to traverse. Can also handle primitives, null, or undefined.
|
|
6
|
+
* @param {TraverseCallback} callback - The function to invoke on each value. Receives the current value
|
|
7
|
+
* and an `update` function to modify the value in its parent container.
|
|
8
|
+
* @param {any} [parent=null] - The parent object or array of the current value. Used internally.
|
|
9
|
+
* @param {any} [key=null] - The key or index of the current value in its parent. Used internally.
|
|
10
|
+
* @param {WeakSet} [visited=new WeakSet()] - Tracks visited objects to handle circular references. Used internally.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* // Example: Doubling all numbers in an object
|
|
14
|
+
* const obj = { a: 1, b: [2, 3, { c: 4 }] };
|
|
15
|
+
* traverse(obj, (value, update) => {
|
|
16
|
+
* if (typeof value === 'number') {
|
|
17
|
+
* update(value * 2);
|
|
18
|
+
* }
|
|
19
|
+
* });
|
|
20
|
+
* console.log(obj); // { a: 2, b: [4, 6, { c: 8 }] }
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* // Example: Handling circular references
|
|
24
|
+
* const obj = { a: 1 };
|
|
25
|
+
* obj.self = obj;
|
|
26
|
+
* traverse(obj, (value, update) => {
|
|
27
|
+
* if (typeof value === 'number') {
|
|
28
|
+
* update(value * 2);
|
|
29
|
+
* }
|
|
30
|
+
* });
|
|
31
|
+
* console.log(obj.a); // 2
|
|
32
|
+
*/
|
|
33
|
+
export declare function traverse(obj: any, callback: TraverseCallback, parent?: any, key?: any, visited?: WeakSet<WeakKey>): void;
|
|
34
|
+
export {};
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Recursively traverses an object or array, invoking a callback on each value.
|
|
3
|
+
*
|
|
4
|
+
* @param {any} obj - The object or array to traverse. Can also handle primitives, null, or undefined.
|
|
5
|
+
* @param {TraverseCallback} callback - The function to invoke on each value. Receives the current value
|
|
6
|
+
* and an `update` function to modify the value in its parent container.
|
|
7
|
+
* @param {any} [parent=null] - The parent object or array of the current value. Used internally.
|
|
8
|
+
* @param {any} [key=null] - The key or index of the current value in its parent. Used internally.
|
|
9
|
+
* @param {WeakSet} [visited=new WeakSet()] - Tracks visited objects to handle circular references. Used internally.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* // Example: Doubling all numbers in an object
|
|
13
|
+
* const obj = { a: 1, b: [2, 3, { c: 4 }] };
|
|
14
|
+
* traverse(obj, (value, update) => {
|
|
15
|
+
* if (typeof value === 'number') {
|
|
16
|
+
* update(value * 2);
|
|
17
|
+
* }
|
|
18
|
+
* });
|
|
19
|
+
* console.log(obj); // { a: 2, b: [4, 6, { c: 8 }] }
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* // Example: Handling circular references
|
|
23
|
+
* const obj = { a: 1 };
|
|
24
|
+
* obj.self = obj;
|
|
25
|
+
* traverse(obj, (value, update) => {
|
|
26
|
+
* if (typeof value === 'number') {
|
|
27
|
+
* update(value * 2);
|
|
28
|
+
* }
|
|
29
|
+
* });
|
|
30
|
+
* console.log(obj.a); // 2
|
|
31
|
+
*/
|
|
32
|
+
export function traverse(obj, callback, parent = null, key = null, visited = new WeakSet()) {
|
|
33
|
+
if (obj == null || typeof obj !== 'object') {
|
|
34
|
+
callback(obj, (newValue) => {
|
|
35
|
+
if (parent !== null && key !== null) {
|
|
36
|
+
parent[key] = newValue;
|
|
37
|
+
}
|
|
38
|
+
});
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
if (visited.has(obj)) {
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
visited.add(obj);
|
|
45
|
+
if (Array.isArray(obj)) {
|
|
46
|
+
obj.forEach((item, index) => {
|
|
47
|
+
const update = (newValue) => {
|
|
48
|
+
obj[index] = newValue;
|
|
49
|
+
};
|
|
50
|
+
callback(item, update);
|
|
51
|
+
traverse(item, callback, obj, index, visited);
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
else {
|
|
55
|
+
Object.entries(obj).forEach(([key, value]) => {
|
|
56
|
+
const update = (newValue) => {
|
|
57
|
+
obj[key] = newValue;
|
|
58
|
+
};
|
|
59
|
+
callback(value, update);
|
|
60
|
+
traverse(value, callback, obj, key, visited);
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
}
|
|
@@ -14,3 +14,9 @@ export declare function flatten<T extends Record<string, any>>(object: T, path?:
|
|
|
14
14
|
export declare function flattenMongoQuery(obj: any, _current?: any, _res?: any): {
|
|
15
15
|
[key: string]: string;
|
|
16
16
|
};
|
|
17
|
+
/**
|
|
18
|
+
* Unflatten a flat object with dot-separated keys back into a nested object.
|
|
19
|
+
*
|
|
20
|
+
* { 'foo.bar': 'baz' } -> { foo: { bar: 'baz' }}
|
|
21
|
+
*/
|
|
22
|
+
export declare function unflatten(obj: any): any;
|
|
@@ -37,3 +37,26 @@ export function flattenMongoQuery(obj, _current, _res = {}) {
|
|
|
37
37
|
}
|
|
38
38
|
return _res;
|
|
39
39
|
}
|
|
40
|
+
/**
|
|
41
|
+
* Unflatten a flat object with dot-separated keys back into a nested object.
|
|
42
|
+
*
|
|
43
|
+
* { 'foo.bar': 'baz' } -> { foo: { bar: 'baz' }}
|
|
44
|
+
*/
|
|
45
|
+
export function unflatten(obj) {
|
|
46
|
+
const result = {};
|
|
47
|
+
for (const key in obj) {
|
|
48
|
+
const parts = key.split('.');
|
|
49
|
+
let current = result;
|
|
50
|
+
for (let i = 0; i < parts.length; i++) {
|
|
51
|
+
const part = parts[i];
|
|
52
|
+
if (i === parts.length - 1) {
|
|
53
|
+
current[part] = obj[key];
|
|
54
|
+
}
|
|
55
|
+
else {
|
|
56
|
+
current[part] = current[part] || {};
|
|
57
|
+
current = current[part];
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
return result;
|
|
62
|
+
}
|
|
@@ -15,7 +15,7 @@ export type { ContentVariantsPrps as ContentProps } from './components/content-v
|
|
|
15
15
|
/**
|
|
16
16
|
* General Builder types
|
|
17
17
|
*/
|
|
18
|
-
export type { RegisteredComponent } from './context/types';
|
|
18
|
+
export type { BuilderContextInterface, RegisteredComponent, RegisteredComponents } from './context/types';
|
|
19
19
|
export type { BuilderBlock } from './types/builder-block';
|
|
20
20
|
export type { BuilderContent } from './types/builder-content';
|
|
21
21
|
export type { ComponentInfo } from './types/components';
|
package/package.json
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { IMAGE_FILE_TYPES } from '../../constants/file-types';
|
|
1
2
|
import type { ComponentInfo } from '../../types/components';
|
|
2
3
|
export const componentInfo: ComponentInfo = {
|
|
3
4
|
name: 'Image',
|
|
@@ -14,7 +15,7 @@ export const componentInfo: ComponentInfo = {
|
|
|
14
15
|
name: 'image',
|
|
15
16
|
type: 'file',
|
|
16
17
|
bubble: true,
|
|
17
|
-
allowedFileTypes:
|
|
18
|
+
allowedFileTypes: IMAGE_FILE_TYPES,
|
|
18
19
|
required: true,
|
|
19
20
|
defaultValue: 'https://cdn.builder.io/api/v1/image/assets%2FYJIGb4i01jvw0SRdL5Bt%2F72c80f114dc149019051b6852a9e3b7a',
|
|
20
21
|
onChange: (options: Map<string, any>): void | Promise<void> => {
|
|
@@ -36,7 +36,10 @@
|
|
|
36
36
|
!url ||
|
|
37
37
|
// We can auto add srcset for cdn.builder.io and shopify
|
|
38
38
|
// images, otherwise you can supply this prop manually
|
|
39
|
-
!(
|
|
39
|
+
!(
|
|
40
|
+
typeof url === "string" &&
|
|
41
|
+
(url.match(/builder\.io/) || url.match(/cdn\.shopify\.com/))
|
|
42
|
+
)
|
|
40
43
|
) {
|
|
41
44
|
return srcset;
|
|
42
45
|
}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { IMAGE_FILE_TYPES } from '../../constants/file-types';
|
|
1
2
|
import type { ComponentInfo } from '../../types/components';
|
|
2
3
|
export const componentInfo: ComponentInfo = {
|
|
3
4
|
// friendlyName?
|
|
@@ -8,7 +9,7 @@ export const componentInfo: ComponentInfo = {
|
|
|
8
9
|
name: 'image',
|
|
9
10
|
bubble: true,
|
|
10
11
|
type: 'file',
|
|
11
|
-
allowedFileTypes:
|
|
12
|
+
allowedFileTypes: IMAGE_FILE_TYPES,
|
|
12
13
|
required: true
|
|
13
14
|
}],
|
|
14
15
|
noWrap: true,
|