@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
|
@@ -1,8 +1,6 @@
|
|
|
1
1
|
<script lang="ts">
|
|
2
|
-
import { evaluate } from "../../functions/evaluate/index";
|
|
3
2
|
import type { TextProps } from "./text.types";
|
|
4
3
|
|
|
5
|
-
export let builderContext: TextProps["builderContext"];
|
|
6
4
|
export let text: TextProps["text"] = undefined;
|
|
7
5
|
function stringifyStyles(stylesObj) {
|
|
8
6
|
let styles = "";
|
|
@@ -14,27 +12,6 @@
|
|
|
14
12
|
}
|
|
15
13
|
return styles;
|
|
16
14
|
}
|
|
17
|
-
|
|
18
|
-
$: processedText = () => {
|
|
19
|
-
const context = $builderContext;
|
|
20
|
-
const {
|
|
21
|
-
context: contextContext,
|
|
22
|
-
localState,
|
|
23
|
-
rootState,
|
|
24
|
-
rootSetState,
|
|
25
|
-
} = context;
|
|
26
|
-
return String(text?.toString() || "").replace(
|
|
27
|
-
/{{([^}]+)}}/g,
|
|
28
|
-
(match, group) =>
|
|
29
|
-
evaluate({
|
|
30
|
-
code: group,
|
|
31
|
-
context: contextContext,
|
|
32
|
-
localState,
|
|
33
|
-
rootState,
|
|
34
|
-
rootSetState,
|
|
35
|
-
}) as string
|
|
36
|
-
);
|
|
37
|
-
};
|
|
38
15
|
</script>
|
|
39
16
|
|
|
40
17
|
<div
|
|
@@ -43,5 +20,5 @@
|
|
|
43
20
|
})}
|
|
44
21
|
class="builder-text"
|
|
45
22
|
>
|
|
46
|
-
{@html
|
|
23
|
+
{@html text?.toString() || ""}
|
|
47
24
|
</div>
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { IMAGE_FILE_TYPES, VIDEO_FILE_TYPES } from '../../constants/file-types';
|
|
1
2
|
import type { ComponentInfo } from '../../types/components';
|
|
2
3
|
export const componentInfo: ComponentInfo = {
|
|
3
4
|
name: 'Video',
|
|
@@ -10,14 +11,14 @@ export const componentInfo: ComponentInfo = {
|
|
|
10
11
|
inputs: [{
|
|
11
12
|
name: 'video',
|
|
12
13
|
type: 'file',
|
|
13
|
-
allowedFileTypes:
|
|
14
|
+
allowedFileTypes: VIDEO_FILE_TYPES,
|
|
14
15
|
bubble: true,
|
|
15
16
|
defaultValue: 'https://cdn.builder.io/o/assets%2FYJIGb4i01jvw0SRdL5Bt%2Fd27731a526464deba0016216f5f9e570%2Fcompressed?apiKey=YJIGb4i01jvw0SRdL5Bt&token=d27731a526464deba0016216f5f9e570&alt=media&optimized=true',
|
|
16
17
|
required: true
|
|
17
18
|
}, {
|
|
18
19
|
name: 'posterImage',
|
|
19
20
|
type: 'file',
|
|
20
|
-
allowedFileTypes:
|
|
21
|
+
allowedFileTypes: IMAGE_FILE_TYPES,
|
|
21
22
|
helperText: 'Image to show before the video plays'
|
|
22
23
|
}, {
|
|
23
24
|
name: 'autoPlay',
|
|
@@ -5,19 +5,26 @@ import { extractTextStyles } from '../../functions/extract-text-styles';
|
|
|
5
5
|
import { getStyle } from '../../functions/get-style';
|
|
6
6
|
import type { BuilderBlock } from '../../types/builder-block';
|
|
7
7
|
import type { RepeatData } from './types';
|
|
8
|
+
const checkIsComponentRestricted = (component: RegisteredComponent | null | undefined, model: string) => {
|
|
9
|
+
if (!component) return true;
|
|
10
|
+
if (!model) return false;
|
|
11
|
+
return component.models && component.models.length > 0 && !component.models.includes(model);
|
|
12
|
+
};
|
|
8
13
|
export const getComponent = ({
|
|
9
14
|
block,
|
|
10
|
-
registeredComponents
|
|
15
|
+
registeredComponents,
|
|
16
|
+
model
|
|
11
17
|
}: {
|
|
12
18
|
block: BuilderBlock;
|
|
13
19
|
registeredComponents: RegisteredComponents;
|
|
20
|
+
model: string;
|
|
14
21
|
}) => {
|
|
15
22
|
const componentName = block.component?.name;
|
|
16
23
|
if (!componentName) {
|
|
17
24
|
return null;
|
|
18
25
|
}
|
|
19
26
|
const ref = registeredComponents[componentName];
|
|
20
|
-
if (!ref) {
|
|
27
|
+
if (!ref || checkIsComponentRestricted(ref, model)) {
|
|
21
28
|
// TODO: Public doc page with more info about this message
|
|
22
29
|
console.warn(`
|
|
23
30
|
Could not find a registered component named "${componentName}".
|
|
@@ -94,10 +101,15 @@ export const provideLinkComponent = (block: RegisteredComponent | null | undefin
|
|
|
94
101
|
};
|
|
95
102
|
return {};
|
|
96
103
|
};
|
|
97
|
-
export const provideRegisteredComponents = (block: RegisteredComponent | null | undefined, registeredComponents: RegisteredComponents) => {
|
|
98
|
-
if (block?.shouldReceiveBuilderProps?.builderComponents)
|
|
99
|
-
|
|
100
|
-
|
|
104
|
+
export const provideRegisteredComponents = (block: RegisteredComponent | null | undefined, registeredComponents: RegisteredComponents, model: string) => {
|
|
105
|
+
if (block?.shouldReceiveBuilderProps?.builderComponents) {
|
|
106
|
+
const filteredRegisteredComponents = Object.fromEntries(Object.entries(registeredComponents).filter(([_, component]) => {
|
|
107
|
+
return !checkIsComponentRestricted(component, model);
|
|
108
|
+
}));
|
|
109
|
+
return {
|
|
110
|
+
builderComponents: filteredRegisteredComponents
|
|
111
|
+
};
|
|
112
|
+
}
|
|
101
113
|
return {};
|
|
102
114
|
};
|
|
103
115
|
export const provideBuilderBlock = (block: RegisteredComponent | null | undefined, builderBlock: BuilderBlock) => {
|
|
@@ -57,7 +57,6 @@
|
|
|
57
57
|
rootState: $context.rootState,
|
|
58
58
|
rootSetState: $context.rootSetState,
|
|
59
59
|
context: $context.context,
|
|
60
|
-
shouldEvaluateBindings: true,
|
|
61
60
|
});
|
|
62
61
|
return blockToUse;
|
|
63
62
|
};
|
|
@@ -65,6 +64,7 @@
|
|
|
65
64
|
return getComponent({
|
|
66
65
|
block: processedBlock(),
|
|
67
66
|
registeredComponents: registeredComponents,
|
|
67
|
+
model: $context.model,
|
|
68
68
|
});
|
|
69
69
|
};
|
|
70
70
|
$: Tag = () => {
|
|
@@ -106,11 +106,15 @@
|
|
|
106
106
|
blockChildren: processedBlock().children ?? [],
|
|
107
107
|
componentRef: blockComponent?.()?.component,
|
|
108
108
|
componentOptions: {
|
|
109
|
-
...getBlockComponentOptions(processedBlock()),
|
|
109
|
+
...getBlockComponentOptions(processedBlock(), $context),
|
|
110
110
|
...provideBuilderBlock(blockComponent(), processedBlock()),
|
|
111
111
|
...provideBuilderContext(blockComponent(), context),
|
|
112
112
|
...provideLinkComponent(blockComponent(), linkComponent),
|
|
113
|
-
...provideRegisteredComponents(
|
|
113
|
+
...provideRegisteredComponents(
|
|
114
|
+
blockComponent(),
|
|
115
|
+
registeredComponents,
|
|
116
|
+
$context.model
|
|
117
|
+
),
|
|
114
118
|
},
|
|
115
119
|
context: context,
|
|
116
120
|
linkComponent: linkComponent,
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { TARGET } from '../../../constants/target';
|
|
2
|
+
import { isEditing } from '../../../functions/is-editing';
|
|
3
|
+
import { isPreviewing } from '../../../functions/is-previewing';
|
|
4
|
+
import type { Target } from '../../../types/targets';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* SDKS that use the elementRef approach to enable visual editing.
|
|
8
|
+
* We don't need to render the div for other SDKs as they attach event listeners to the window.
|
|
9
|
+
*/
|
|
10
|
+
export const SDKS_USING_ELEMENT_REF_APPROACH = (['svelte', 'qwik', 'vue'] as Target[]);
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* We need to attach this div only when content exists or isPreviewing/isEditing even when content is null,
|
|
14
|
+
* as we need to set the elementRef and allow previewing and visual editing
|
|
15
|
+
*/
|
|
16
|
+
export const needsElementRefDivForEditing = () => {
|
|
17
|
+
return SDKS_USING_ELEMENT_REF_APPROACH.includes(TARGET) && (isEditing() || isPreviewing());
|
|
18
|
+
}
|
|
@@ -45,6 +45,7 @@
|
|
|
45
45
|
BuilderComponentStateChange,
|
|
46
46
|
ContentProps,
|
|
47
47
|
} from "../content.types";
|
|
48
|
+
import { needsElementRefDivForEditing } from "./enable-editor.helpers";
|
|
48
49
|
import { getWrapperClassName } from "./styles.helpers";
|
|
49
50
|
import type { Writable } from "svelte/store";
|
|
50
51
|
|
|
@@ -58,13 +59,24 @@
|
|
|
58
59
|
export let trustedHosts: BuilderEditorProps["trustedHosts"] = undefined;
|
|
59
60
|
export let model: BuilderEditorProps["model"] = undefined;
|
|
60
61
|
export let content: BuilderEditorProps["content"] = undefined;
|
|
62
|
+
export let context: BuilderEditorProps["context"] = undefined;
|
|
61
63
|
export let data: BuilderEditorProps["data"] = undefined;
|
|
62
64
|
export let showContent: BuilderEditorProps["showContent"];
|
|
63
65
|
export let contentWrapper: BuilderEditorProps["contentWrapper"] = undefined;
|
|
64
|
-
export let context: BuilderEditorProps["context"] = undefined;
|
|
65
66
|
export let contentWrapperProps: BuilderEditorProps["contentWrapperProps"] =
|
|
66
67
|
undefined;
|
|
67
68
|
|
|
69
|
+
function stringifyStyles(stylesObj) {
|
|
70
|
+
let styles = "";
|
|
71
|
+
for (let key in stylesObj) {
|
|
72
|
+
const dashedKey = key.replace(/[A-Z]/g, function (match) {
|
|
73
|
+
return "-" + match.toLowerCase();
|
|
74
|
+
});
|
|
75
|
+
styles += dashedKey + ":" + stylesObj[key] + ";";
|
|
76
|
+
}
|
|
77
|
+
return styles;
|
|
78
|
+
}
|
|
79
|
+
|
|
68
80
|
function mergeNewRootState(newData: Dictionary<any>) {
|
|
69
81
|
const combinedState = {
|
|
70
82
|
...$builderContextSignal.rootState,
|
|
@@ -220,8 +232,14 @@
|
|
|
220
232
|
});
|
|
221
233
|
Object.values<ComponentInfo>($builderContextSignal.componentInfos).forEach(
|
|
222
234
|
(registeredComponent) => {
|
|
223
|
-
|
|
224
|
-
|
|
235
|
+
if (
|
|
236
|
+
!model ||
|
|
237
|
+
!registeredComponent.models?.length ||
|
|
238
|
+
registeredComponent.models.includes(model)
|
|
239
|
+
) {
|
|
240
|
+
const message = createRegisterComponentMessage(registeredComponent);
|
|
241
|
+
window.parent?.postMessage(message, "*");
|
|
242
|
+
}
|
|
225
243
|
}
|
|
226
244
|
);
|
|
227
245
|
window.addEventListener(
|
|
@@ -248,14 +266,22 @@
|
|
|
248
266
|
* TO-DO: should we only update the state when there is a change?
|
|
249
267
|
**/
|
|
250
268
|
if (
|
|
251
|
-
searchParamPreviewModel ===
|
|
252
|
-
|
|
253
|
-
|
|
269
|
+
searchParamPreviewModel === "BUILDER_STUDIO" ||
|
|
270
|
+
(searchParamPreviewModel === model &&
|
|
271
|
+
previewApiKey === apiKey &&
|
|
272
|
+
(!content || searchParamPreviewId === content.id))
|
|
254
273
|
) {
|
|
255
274
|
fetchOneEntry({
|
|
256
|
-
model: model,
|
|
275
|
+
model: model || "",
|
|
257
276
|
apiKey: apiKey,
|
|
258
277
|
apiVersion: $builderContextSignal.apiVersion,
|
|
278
|
+
...(searchParamPreviewModel === "BUILDER_STUDIO" && context?.symbolId
|
|
279
|
+
? {
|
|
280
|
+
query: {
|
|
281
|
+
id: context.symbolId,
|
|
282
|
+
},
|
|
283
|
+
}
|
|
284
|
+
: {}),
|
|
259
285
|
}).then((content) => {
|
|
260
286
|
if (content) {
|
|
261
287
|
mergeNewContent(content);
|
|
@@ -362,9 +388,15 @@
|
|
|
362
388
|
});
|
|
363
389
|
</script>
|
|
364
390
|
|
|
365
|
-
{#if $builderContextSignal.content}
|
|
391
|
+
{#if $builderContextSignal.content || needsElementRefDivForEditing()}
|
|
366
392
|
<svelte:element
|
|
367
393
|
this={ContentWrapper}
|
|
394
|
+
style={stringifyStyles({
|
|
395
|
+
display:
|
|
396
|
+
!$builderContextSignal.content && needsElementRefDivForEditing()
|
|
397
|
+
? "none"
|
|
398
|
+
: undefined,
|
|
399
|
+
})}
|
|
368
400
|
{...{}}
|
|
369
401
|
bind:this={elementRef}
|
|
370
402
|
on:click={(event) => {
|
|
@@ -32,7 +32,6 @@
|
|
|
32
32
|
export let context: ContentProps["context"] = undefined;
|
|
33
33
|
export let content: ContentProps["content"] = undefined;
|
|
34
34
|
export let customComponents: ContentProps["customComponents"] = undefined;
|
|
35
|
-
export let model: ContentProps["model"] = undefined;
|
|
36
35
|
export let data: ContentProps["data"] = undefined;
|
|
37
36
|
export let locale: ContentProps["locale"] = undefined;
|
|
38
37
|
export let canTrack: ContentProps["canTrack"] = undefined;
|
|
@@ -40,6 +39,7 @@
|
|
|
40
39
|
export let blocksWrapper: ContentProps["blocksWrapper"] = undefined;
|
|
41
40
|
export let blocksWrapperProps: ContentProps["blocksWrapperProps"] = undefined;
|
|
42
41
|
export let nonce: ContentProps["nonce"] = undefined;
|
|
42
|
+
export let model: ContentProps["model"] = undefined;
|
|
43
43
|
export let apiHost: ContentProps["apiHost"] = undefined;
|
|
44
44
|
export let enrich: ContentProps["enrich"] = undefined;
|
|
45
45
|
export let showContent: ContentProps["showContent"];
|
|
@@ -63,11 +63,7 @@
|
|
|
63
63
|
});
|
|
64
64
|
let registeredComponents = [
|
|
65
65
|
...getDefaultRegisteredComponents(),
|
|
66
|
-
...(customComponents
|
|
67
|
-
if (!models?.length) return true;
|
|
68
|
-
if (!model) return true;
|
|
69
|
-
return models.includes(model);
|
|
70
|
-
}) || []),
|
|
66
|
+
...(customComponents || []),
|
|
71
67
|
].reduce<RegisteredComponents>(
|
|
72
68
|
(acc, { component, ...info }) => ({
|
|
73
69
|
...acc,
|
|
@@ -96,11 +92,7 @@
|
|
|
96
92
|
apiVersion: apiVersion,
|
|
97
93
|
componentInfos: [
|
|
98
94
|
...getDefaultRegisteredComponents(),
|
|
99
|
-
...(customComponents
|
|
100
|
-
if (!models?.length) return true;
|
|
101
|
-
if (!model) return true;
|
|
102
|
-
return models.includes(model);
|
|
103
|
-
}) || []),
|
|
95
|
+
...(customComponents || []),
|
|
104
96
|
].reduce<Dictionary<ComponentInfo>>(
|
|
105
97
|
(acc, { component: _, ...info }) => ({
|
|
106
98
|
...acc,
|
|
@@ -112,6 +104,7 @@
|
|
|
112
104
|
BlocksWrapper: blocksWrapper || "div",
|
|
113
105
|
BlocksWrapperProps: blocksWrapperProps || {},
|
|
114
106
|
nonce: nonce || "",
|
|
107
|
+
model: model || "",
|
|
115
108
|
});
|
|
116
109
|
if (!apiKey) {
|
|
117
110
|
logger.error(
|
|
@@ -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 const SDK_VERSION = "3.0.
|
|
1
|
+
export const SDK_VERSION = "3.0.2"
|
package/src/context/types.ts
CHANGED
|
@@ -37,5 +37,6 @@ export interface BuilderContextInterface extends Pick<BlocksWrapperProps, 'Block
|
|
|
37
37
|
// Used to recursively store all CSS coming from a parent that would apply to a Text block
|
|
38
38
|
inheritedStyles: Record<string, unknown>;
|
|
39
39
|
nonce: string;
|
|
40
|
+
model: string;
|
|
40
41
|
canTrack?: boolean;
|
|
41
42
|
}
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { logger } from '../../../helpers/logger';
|
|
2
1
|
import type { ExecutorArgs } from '../helpers';
|
|
3
2
|
import { flattenState, getFunctionArguments } from '../helpers';
|
|
4
3
|
import Interpreter from './acorn-interpreter';
|
|
@@ -95,10 +94,7 @@ export const runInEdge = ({
|
|
|
95
94
|
return `var ${key} = ${jsonValName} === undefined ? undefined : JSON.parse(${jsonValName});`;
|
|
96
95
|
}).join('\n');
|
|
97
96
|
const cleanedCode = processCode(code);
|
|
98
|
-
if (cleanedCode === '')
|
|
99
|
-
logger.warn('Skipping evaluation of empty code block.');
|
|
100
|
-
return;
|
|
101
|
-
}
|
|
97
|
+
if (cleanedCode === '') return;
|
|
102
98
|
const transformed = `
|
|
103
99
|
function theFunction() {
|
|
104
100
|
${prependedCode}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import type { BuilderBlock } from '../types/builder-block';
|
|
2
|
+
import { traverse } from './traverse';
|
|
3
|
+
function isLocalizedField(value: any) {
|
|
4
|
+
return value && typeof value === 'object' && value['@type'] === '@builder.io/core:LocalizedValue';
|
|
5
|
+
}
|
|
6
|
+
function containsLocalizedValues(data: Record<string, any>) {
|
|
7
|
+
if (!data || !Object.getOwnPropertyNames(data).length) {
|
|
8
|
+
return false;
|
|
9
|
+
}
|
|
10
|
+
let hasLocalizedValues = false;
|
|
11
|
+
traverse(data, value => {
|
|
12
|
+
if (isLocalizedField(value)) {
|
|
13
|
+
hasLocalizedValues = true;
|
|
14
|
+
return;
|
|
15
|
+
}
|
|
16
|
+
});
|
|
17
|
+
return hasLocalizedValues;
|
|
18
|
+
}
|
|
19
|
+
function extractLocalizedValues(data: Record<string, any>, locale: string) {
|
|
20
|
+
if (!data || !Object.getOwnPropertyNames(data).length) {
|
|
21
|
+
return {};
|
|
22
|
+
}
|
|
23
|
+
traverse(data, (value, update) => {
|
|
24
|
+
if (isLocalizedField(value)) {
|
|
25
|
+
update(value[locale] ?? undefined);
|
|
26
|
+
}
|
|
27
|
+
});
|
|
28
|
+
return data;
|
|
29
|
+
}
|
|
30
|
+
export function resolveLocalizedValues(block: BuilderBlock, locale: string | undefined) {
|
|
31
|
+
if (block.component?.options && containsLocalizedValues(block.component?.options)) {
|
|
32
|
+
if (!locale) {
|
|
33
|
+
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');
|
|
34
|
+
}
|
|
35
|
+
block.component.options = extractLocalizedValues(block.component.options, locale ?? 'Default');
|
|
36
|
+
}
|
|
37
|
+
return block;
|
|
38
|
+
}
|
|
@@ -1,7 +1,24 @@
|
|
|
1
|
+
import type { BuilderContextInterface } from '../context/types';
|
|
1
2
|
import type { BuilderBlock } from '../types/builder-block';
|
|
2
|
-
|
|
3
|
+
import { evaluate } from './evaluate/evaluate';
|
|
4
|
+
export function getBlockComponentOptions(block: BuilderBlock, context: Pick<BuilderContextInterface, 'localState' | 'context' | 'rootState' | 'rootSetState'>) {
|
|
3
5
|
return {
|
|
4
6
|
...block.component?.options,
|
|
5
|
-
...(block as any).options
|
|
7
|
+
...(block as any).options,
|
|
8
|
+
...evaluateTextComponentTextOption(block, context)
|
|
6
9
|
};
|
|
10
|
+
}
|
|
11
|
+
const evaluateTextComponentTextOption = (block: BuilderBlock, context: Pick<BuilderContextInterface, 'localState' | 'context' | 'rootState' | 'rootSetState'>) => {
|
|
12
|
+
if (block.component?.name === 'Text' && block.component.options?.text && typeof block.component.options.text === 'string') {
|
|
13
|
+
return {
|
|
14
|
+
...block.component.options,
|
|
15
|
+
text: block.component.options.text.replace(/{{([^}]+)}}/g, (_match: string, group: string) => (evaluate({
|
|
16
|
+
code: group,
|
|
17
|
+
context,
|
|
18
|
+
localState: context.localState,
|
|
19
|
+
rootState: context.rootState,
|
|
20
|
+
rootSetState: context.rootSetState
|
|
21
|
+
}) as string))
|
|
22
|
+
};
|
|
23
|
+
}
|
|
7
24
|
}
|
|
@@ -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
|
import type { GetContentOptions } from './types';
|
|
6
7
|
const isPositiveNumber = (thing: unknown) => typeof thing === 'number' && !isNaN(thing) && thing >= 0;
|
|
7
8
|
export const generateContentUrl = (options: GetContentOptions): URL => {
|
|
@@ -39,7 +40,7 @@ export const generateContentUrl = (options: GetContentOptions): URL => {
|
|
|
39
40
|
url.searchParams.set('noTraverse', String(noTraverse));
|
|
40
41
|
url.searchParams.set('includeRefs', String(true));
|
|
41
42
|
const finalLocale = locale || userAttributes?.locale;
|
|
42
|
-
let finalUserAttributes = userAttributes;
|
|
43
|
+
let finalUserAttributes: Record<string, any> = userAttributes || {};
|
|
43
44
|
if (finalLocale) {
|
|
44
45
|
url.searchParams.set('locale', finalLocale);
|
|
45
46
|
finalUserAttributes = {
|
|
@@ -79,11 +80,15 @@ export const generateContentUrl = (options: GetContentOptions): URL => {
|
|
|
79
80
|
...getBuilderSearchParamsFromWindow(),
|
|
80
81
|
...normalizeSearchParams(options.options || {})
|
|
81
82
|
};
|
|
83
|
+
finalUserAttributes = {
|
|
84
|
+
...finalUserAttributes,
|
|
85
|
+
...getUserAttributesAsJSON(queryOptions)
|
|
86
|
+
};
|
|
82
87
|
const flattened = flatten(queryOptions);
|
|
83
88
|
for (const key in flattened) {
|
|
84
89
|
url.searchParams.set(key, String(flattened[key]));
|
|
85
90
|
}
|
|
86
|
-
if (finalUserAttributes) {
|
|
91
|
+
if (Object.keys(finalUserAttributes).length > 0) {
|
|
87
92
|
url.searchParams.set('userAttributes', JSON.stringify(finalUserAttributes));
|
|
88
93
|
}
|
|
89
94
|
if (query) {
|
|
@@ -95,4 +100,26 @@ export const generateContentUrl = (options: GetContentOptions): URL => {
|
|
|
95
100
|
}
|
|
96
101
|
}
|
|
97
102
|
return url;
|
|
103
|
+
};
|
|
104
|
+
const getUserAttributesFromQueryOptions = (queryOptions: any) => {
|
|
105
|
+
const newUserAttributes: any = {};
|
|
106
|
+
for (const key in queryOptions) {
|
|
107
|
+
if (key.startsWith('userAttributes.')) {
|
|
108
|
+
newUserAttributes[key] = queryOptions[key];
|
|
109
|
+
delete queryOptions[key];
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
return newUserAttributes;
|
|
113
|
+
};
|
|
114
|
+
const getUserAttributesAsJSON = (queryOptions: any) => {
|
|
115
|
+
if (isBrowser() && queryOptions['preview'] === 'BUILDER_STUDIO') {
|
|
116
|
+
queryOptions['userAttributes.urlPath'] = window.location.pathname;
|
|
117
|
+
queryOptions['userAttributes.host'] = window.location.host;
|
|
118
|
+
const queryOptionsForUserAttributes = getUserAttributesFromQueryOptions(queryOptions);
|
|
119
|
+
const {
|
|
120
|
+
userAttributes
|
|
121
|
+
} = unflatten(queryOptionsForUserAttributes);
|
|
122
|
+
return userAttributes;
|
|
123
|
+
}
|
|
124
|
+
return {};
|
|
98
125
|
}
|
|
@@ -3,6 +3,7 @@ import type { BuilderContextInterface } from '../context/types';
|
|
|
3
3
|
import { omit } from '../helpers/omit';
|
|
4
4
|
import type { BuilderBlock } from '../types/builder-block';
|
|
5
5
|
import { evaluate } from './evaluate/index';
|
|
6
|
+
import { resolveLocalizedValues } from './extract-localized-values';
|
|
6
7
|
import { fastClone } from './fast-clone';
|
|
7
8
|
import { set } from './set';
|
|
8
9
|
import { transformBlock } from './transform-block';
|
|
@@ -84,28 +85,19 @@ const evaluateBindings = ({
|
|
|
84
85
|
export function getProcessedBlock({
|
|
85
86
|
block,
|
|
86
87
|
context,
|
|
87
|
-
shouldEvaluateBindings,
|
|
88
88
|
localState,
|
|
89
89
|
rootState,
|
|
90
90
|
rootSetState
|
|
91
91
|
}: {
|
|
92
92
|
block: BuilderBlock;
|
|
93
|
-
/**
|
|
94
|
-
* In some cases, we want to avoid evaluating bindings and only want framework-specific block transformation. It is
|
|
95
|
-
* also sometimes too early to consider bindings, e.g. when we might be looking at a repeated block.
|
|
96
|
-
*/
|
|
97
|
-
shouldEvaluateBindings: boolean;
|
|
98
93
|
} & Pick<BuilderContextInterface, 'localState' | 'context' | 'rootState' | 'rootSetState'>): BuilderBlock {
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
} else {
|
|
109
|
-
return transformedBlock;
|
|
110
|
-
}
|
|
94
|
+
let transformedBlock = resolveLocalizedValues(block, (rootState.locale as string | undefined));
|
|
95
|
+
transformedBlock = transformBlock(transformedBlock);
|
|
96
|
+
return evaluateBindings({
|
|
97
|
+
block: transformedBlock,
|
|
98
|
+
localState,
|
|
99
|
+
rootState,
|
|
100
|
+
rootSetState,
|
|
101
|
+
context
|
|
102
|
+
});
|
|
111
103
|
}
|
|
@@ -13,8 +13,11 @@ const serializeFn = (fnValue: Function) => {
|
|
|
13
13
|
// 2. `name(args) => {code}`
|
|
14
14
|
// 3. `(args) => {}`
|
|
15
15
|
// 4. `args => {}`
|
|
16
|
+
// 5. `async function(args) {code}`
|
|
17
|
+
// 6. `async (args) => {}`
|
|
18
|
+
// 7. `async args => {}`
|
|
16
19
|
const isArrowWithoutParens = /^[a-zA-Z0-9_]+\s*=>/i.test(fnStr);
|
|
17
|
-
const appendFunction = !fnStr.startsWith('function') && !fnStr.startsWith('(') && !isArrowWithoutParens;
|
|
20
|
+
const appendFunction = !fnStr.startsWith('function') && !fnStr.startsWith('async') && !fnStr.startsWith('(') && !isArrowWithoutParens;
|
|
18
21
|
return `return (${appendFunction ? 'function ' : ''}${fnStr}).apply(this, arguments)`;
|
|
19
22
|
};
|
|
20
23
|
export function serializeIncludingFunctions(info: ComponentInfo) {
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
type TraverseCallback = (value: any, update: (newValue: any) => void) => void;
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Recursively traverses an object or array, invoking a callback on each value.
|
|
5
|
+
*
|
|
6
|
+
* @param {any} obj - The object or array to traverse. Can also handle primitives, null, or undefined.
|
|
7
|
+
* @param {TraverseCallback} callback - The function to invoke on each value. Receives the current value
|
|
8
|
+
* and an `update` function to modify the value in its parent container.
|
|
9
|
+
* @param {any} [parent=null] - The parent object or array of the current value. Used internally.
|
|
10
|
+
* @param {any} [key=null] - The key or index of the current value in its parent. Used internally.
|
|
11
|
+
* @param {WeakSet} [visited=new WeakSet()] - Tracks visited objects to handle circular references. Used internally.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* // Example: Doubling all numbers in an object
|
|
15
|
+
* const obj = { a: 1, b: [2, 3, { c: 4 }] };
|
|
16
|
+
* traverse(obj, (value, update) => {
|
|
17
|
+
* if (typeof value === 'number') {
|
|
18
|
+
* update(value * 2);
|
|
19
|
+
* }
|
|
20
|
+
* });
|
|
21
|
+
* console.log(obj); // { a: 2, b: [4, 6, { c: 8 }] }
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* // Example: Handling circular references
|
|
25
|
+
* const obj = { a: 1 };
|
|
26
|
+
* obj.self = obj;
|
|
27
|
+
* traverse(obj, (value, update) => {
|
|
28
|
+
* if (typeof value === 'number') {
|
|
29
|
+
* update(value * 2);
|
|
30
|
+
* }
|
|
31
|
+
* });
|
|
32
|
+
* console.log(obj.a); // 2
|
|
33
|
+
*/
|
|
34
|
+
export function traverse(obj: any, callback: TraverseCallback, parent: any = null, key: any = null, visited = new WeakSet()): void {
|
|
35
|
+
if (obj == null || typeof obj !== 'object') {
|
|
36
|
+
callback(obj, (newValue: any) => {
|
|
37
|
+
if (parent !== null && key !== null) {
|
|
38
|
+
parent[key] = newValue;
|
|
39
|
+
}
|
|
40
|
+
});
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
if (visited.has(obj)) {
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
visited.add(obj);
|
|
47
|
+
if (Array.isArray(obj)) {
|
|
48
|
+
obj.forEach((item, index) => {
|
|
49
|
+
const update = (newValue: any) => {
|
|
50
|
+
obj[index] = newValue;
|
|
51
|
+
};
|
|
52
|
+
callback(item, update);
|
|
53
|
+
traverse(item, callback, obj, index, visited);
|
|
54
|
+
});
|
|
55
|
+
} else {
|
|
56
|
+
Object.entries(obj).forEach(([key, value]) => {
|
|
57
|
+
const update = (newValue: any) => {
|
|
58
|
+
obj[key] = newValue;
|
|
59
|
+
};
|
|
60
|
+
callback(value, update);
|
|
61
|
+
traverse(value, callback, obj, key, visited);
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
}
|
package/src/helpers/flatten.ts
CHANGED
|
@@ -38,4 +38,27 @@ export function flattenMongoQuery(obj: any, _current?: any, _res: any = {}): {
|
|
|
38
38
|
}
|
|
39
39
|
}
|
|
40
40
|
return _res;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Unflatten a flat object with dot-separated keys back into a nested object.
|
|
45
|
+
*
|
|
46
|
+
* { 'foo.bar': 'baz' } -> { foo: { bar: 'baz' }}
|
|
47
|
+
*/
|
|
48
|
+
export function unflatten(obj: any): any {
|
|
49
|
+
const result: any = {};
|
|
50
|
+
for (const key in obj) {
|
|
51
|
+
const parts = key.split('.');
|
|
52
|
+
let current = result;
|
|
53
|
+
for (let i = 0; i < parts.length; i++) {
|
|
54
|
+
const part = parts[i];
|
|
55
|
+
if (i === parts.length - 1) {
|
|
56
|
+
current[part] = obj[key];
|
|
57
|
+
} else {
|
|
58
|
+
current[part] = current[part] || {};
|
|
59
|
+
current = current[part];
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
return result;
|
|
41
64
|
}
|
package/src/server-index.ts
CHANGED
|
@@ -17,7 +17,7 @@ export type { ContentVariantsPrps as ContentProps } from './components/content-v
|
|
|
17
17
|
/**
|
|
18
18
|
* General Builder types
|
|
19
19
|
*/
|
|
20
|
-
export type { RegisteredComponent } from './context/types';
|
|
20
|
+
export type { BuilderContextInterface, RegisteredComponent, RegisteredComponents } from './context/types';
|
|
21
21
|
export type { BuilderBlock } from './types/builder-block';
|
|
22
22
|
export type { BuilderContent } from './types/builder-content';
|
|
23
23
|
export type { ComponentInfo } from './types/components';
|
package/src/types/components.ts
CHANGED