@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
|
@@ -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';
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { IMAGE_FILE_TYPES } from '../../constants/file-types';
|
|
1
2
|
export const componentInfo = {
|
|
2
3
|
name: 'Image',
|
|
3
4
|
static: true,
|
|
@@ -13,7 +14,7 @@ export const componentInfo = {
|
|
|
13
14
|
name: 'image',
|
|
14
15
|
type: 'file',
|
|
15
16
|
bubble: true,
|
|
16
|
-
allowedFileTypes:
|
|
17
|
+
allowedFileTypes: IMAGE_FILE_TYPES,
|
|
17
18
|
required: true,
|
|
18
19
|
defaultValue: 'https://cdn.builder.io/api/v1/image/assets%2FYJIGb4i01jvw0SRdL5Bt%2F72c80f114dc149019051b6852a9e3b7a',
|
|
19
20
|
onChange: (options) => {
|
|
@@ -29,7 +29,8 @@ $: srcSetToUse = () => {
|
|
|
29
29
|
if (!url ||
|
|
30
30
|
// We can auto add srcset for cdn.builder.io and shopify
|
|
31
31
|
// images, otherwise you can supply this prop manually
|
|
32
|
-
!(url
|
|
32
|
+
!(typeof url === "string" &&
|
|
33
|
+
(url.match(/builder\.io/) || url.match(/cdn\.shopify\.com/)))) {
|
|
33
34
|
return srcset;
|
|
34
35
|
}
|
|
35
36
|
if (noWebp) {
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { IMAGE_FILE_TYPES } from '../../constants/file-types';
|
|
1
2
|
export const componentInfo = {
|
|
2
3
|
// friendlyName?
|
|
3
4
|
name: 'Raw:Img',
|
|
@@ -7,7 +8,7 @@ export const componentInfo = {
|
|
|
7
8
|
name: 'image',
|
|
8
9
|
bubble: true,
|
|
9
10
|
type: 'file',
|
|
10
|
-
allowedFileTypes:
|
|
11
|
+
allowedFileTypes: IMAGE_FILE_TYPES,
|
|
11
12
|
required: true
|
|
12
13
|
}],
|
|
13
14
|
noWrap: true,
|
|
@@ -1,6 +1,4 @@
|
|
|
1
|
-
<script>
|
|
2
|
-
export let builderContext;
|
|
3
|
-
export let text = undefined;
|
|
1
|
+
<script>export let text = undefined;
|
|
4
2
|
function stringifyStyles(stylesObj) {
|
|
5
3
|
let styles = "";
|
|
6
4
|
for (let key in stylesObj) {
|
|
@@ -11,17 +9,6 @@ function stringifyStyles(stylesObj) {
|
|
|
11
9
|
}
|
|
12
10
|
return styles;
|
|
13
11
|
}
|
|
14
|
-
$: processedText = () => {
|
|
15
|
-
const context = $builderContext;
|
|
16
|
-
const { context: contextContext, localState, rootState, rootSetState, } = context;
|
|
17
|
-
return String(text?.toString() || "").replace(/{{([^}]+)}}/g, (match, group) => evaluate({
|
|
18
|
-
code: group,
|
|
19
|
-
context: contextContext,
|
|
20
|
-
localState,
|
|
21
|
-
rootState,
|
|
22
|
-
rootSetState,
|
|
23
|
-
}));
|
|
24
|
-
};
|
|
25
12
|
</script>
|
|
26
13
|
|
|
27
14
|
<div
|
|
@@ -30,5 +17,5 @@ $: processedText = () => {
|
|
|
30
17
|
})}
|
|
31
18
|
class="builder-text"
|
|
32
19
|
>
|
|
33
|
-
{@html
|
|
20
|
+
{@html text?.toString() || ""}
|
|
34
21
|
</div>
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { IMAGE_FILE_TYPES, VIDEO_FILE_TYPES } from '../../constants/file-types';
|
|
1
2
|
export const componentInfo = {
|
|
2
3
|
name: 'Video',
|
|
3
4
|
canHaveChildren: true,
|
|
@@ -9,14 +10,14 @@ export const componentInfo = {
|
|
|
9
10
|
inputs: [{
|
|
10
11
|
name: 'video',
|
|
11
12
|
type: 'file',
|
|
12
|
-
allowedFileTypes:
|
|
13
|
+
allowedFileTypes: VIDEO_FILE_TYPES,
|
|
13
14
|
bubble: true,
|
|
14
15
|
defaultValue: 'https://cdn.builder.io/o/assets%2FYJIGb4i01jvw0SRdL5Bt%2Fd27731a526464deba0016216f5f9e570%2Fcompressed?apiKey=YJIGb4i01jvw0SRdL5Bt&token=d27731a526464deba0016216f5f9e570&alt=media&optimized=true',
|
|
15
16
|
required: true
|
|
16
17
|
}, {
|
|
17
18
|
name: 'posterImage',
|
|
18
19
|
type: 'file',
|
|
19
|
-
allowedFileTypes:
|
|
20
|
+
allowedFileTypes: IMAGE_FILE_TYPES,
|
|
20
21
|
helperText: 'Image to show before the video plays'
|
|
21
22
|
}, {
|
|
22
23
|
name: 'autoPlay',
|
|
@@ -3,9 +3,10 @@ import type { Writable } from "svelte/store";
|
|
|
3
3
|
import type { BuilderContextInterface, RegisteredComponent, RegisteredComponents } from '../../context/types';
|
|
4
4
|
import type { BuilderBlock } from '../../types/builder-block';
|
|
5
5
|
import type { RepeatData } from './types';
|
|
6
|
-
export declare const getComponent: ({ block, registeredComponents }: {
|
|
6
|
+
export declare const getComponent: ({ block, registeredComponents, model }: {
|
|
7
7
|
block: BuilderBlock;
|
|
8
8
|
registeredComponents: RegisteredComponents;
|
|
9
|
+
model: string;
|
|
9
10
|
}) => RegisteredComponent | null | undefined;
|
|
10
11
|
export declare const getRepeatItemData: ({ block, context }: {
|
|
11
12
|
block: BuilderBlock;
|
|
@@ -20,8 +21,10 @@ export declare const provideLinkComponent: (block: RegisteredComponent | null |
|
|
|
20
21
|
} | {
|
|
21
22
|
builderLinkComponent?: undefined;
|
|
22
23
|
};
|
|
23
|
-
export declare const provideRegisteredComponents: (block: RegisteredComponent | null | undefined, registeredComponents: RegisteredComponents) => {
|
|
24
|
-
builderComponents:
|
|
24
|
+
export declare const provideRegisteredComponents: (block: RegisteredComponent | null | undefined, registeredComponents: RegisteredComponents, model: string) => {
|
|
25
|
+
builderComponents: {
|
|
26
|
+
[k: string]: RegisteredComponent;
|
|
27
|
+
};
|
|
25
28
|
} | {
|
|
26
29
|
builderComponents?: undefined;
|
|
27
30
|
};
|
|
@@ -1,13 +1,20 @@
|
|
|
1
1
|
import { evaluate } from '../../functions/evaluate/index';
|
|
2
2
|
import { extractTextStyles } from '../../functions/extract-text-styles';
|
|
3
3
|
import { getStyle } from '../../functions/get-style';
|
|
4
|
-
|
|
4
|
+
const checkIsComponentRestricted = (component, model) => {
|
|
5
|
+
if (!component)
|
|
6
|
+
return true;
|
|
7
|
+
if (!model)
|
|
8
|
+
return false;
|
|
9
|
+
return component.models && component.models.length > 0 && !component.models.includes(model);
|
|
10
|
+
};
|
|
11
|
+
export const getComponent = ({ block, registeredComponents, model }) => {
|
|
5
12
|
const componentName = block.component?.name;
|
|
6
13
|
if (!componentName) {
|
|
7
14
|
return null;
|
|
8
15
|
}
|
|
9
16
|
const ref = registeredComponents[componentName];
|
|
10
|
-
if (!ref) {
|
|
17
|
+
if (!ref || checkIsComponentRestricted(ref, model)) {
|
|
11
18
|
// TODO: Public doc page with more info about this message
|
|
12
19
|
console.warn(`
|
|
13
20
|
Could not find a registered component named "${componentName}".
|
|
@@ -71,11 +78,15 @@ export const provideLinkComponent = (block, linkComponent) => {
|
|
|
71
78
|
};
|
|
72
79
|
return {};
|
|
73
80
|
};
|
|
74
|
-
export const provideRegisteredComponents = (block, registeredComponents) => {
|
|
75
|
-
if (block?.shouldReceiveBuilderProps?.builderComponents)
|
|
81
|
+
export const provideRegisteredComponents = (block, registeredComponents, model) => {
|
|
82
|
+
if (block?.shouldReceiveBuilderProps?.builderComponents) {
|
|
83
|
+
const filteredRegisteredComponents = Object.fromEntries(Object.entries(registeredComponents).filter(([_, component]) => {
|
|
84
|
+
return !checkIsComponentRestricted(component, model);
|
|
85
|
+
}));
|
|
76
86
|
return {
|
|
77
|
-
builderComponents:
|
|
87
|
+
builderComponents: filteredRegisteredComponents
|
|
78
88
|
};
|
|
89
|
+
}
|
|
79
90
|
return {};
|
|
80
91
|
};
|
|
81
92
|
export const provideBuilderBlock = (block, builderBlock) => {
|
|
@@ -31,7 +31,6 @@ $: processedBlock = () => {
|
|
|
31
31
|
rootState: $context.rootState,
|
|
32
32
|
rootSetState: $context.rootSetState,
|
|
33
33
|
context: $context.context,
|
|
34
|
-
shouldEvaluateBindings: true,
|
|
35
34
|
});
|
|
36
35
|
return blockToUse;
|
|
37
36
|
};
|
|
@@ -39,6 +38,7 @@ $: blockComponent = () => {
|
|
|
39
38
|
return getComponent({
|
|
40
39
|
block: processedBlock(),
|
|
41
40
|
registeredComponents: registeredComponents,
|
|
41
|
+
model: $context.model,
|
|
42
42
|
});
|
|
43
43
|
};
|
|
44
44
|
$: Tag = () => {
|
|
@@ -77,11 +77,11 @@ $: componentRefProps = () => {
|
|
|
77
77
|
blockChildren: processedBlock().children ?? [],
|
|
78
78
|
componentRef: blockComponent?.()?.component,
|
|
79
79
|
componentOptions: {
|
|
80
|
-
...getBlockComponentOptions(processedBlock()),
|
|
80
|
+
...getBlockComponentOptions(processedBlock(), $context),
|
|
81
81
|
...provideBuilderBlock(blockComponent(), processedBlock()),
|
|
82
82
|
...provideBuilderContext(blockComponent(), context),
|
|
83
83
|
...provideLinkComponent(blockComponent(), linkComponent),
|
|
84
|
-
...provideRegisteredComponents(blockComponent(), registeredComponents),
|
|
84
|
+
...provideRegisteredComponents(blockComponent(), registeredComponents, $context.model),
|
|
85
85
|
},
|
|
86
86
|
context: context,
|
|
87
87
|
linkComponent: linkComponent,
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { Target } from '../../../types/targets';
|
|
2
|
+
/**
|
|
3
|
+
* SDKS that use the elementRef approach to enable visual editing.
|
|
4
|
+
* We don't need to render the div for other SDKs as they attach event listeners to the window.
|
|
5
|
+
*/
|
|
6
|
+
export declare const SDKS_USING_ELEMENT_REF_APPROACH: Target[];
|
|
7
|
+
/**
|
|
8
|
+
* We need to attach this div only when content exists or isPreviewing/isEditing even when content is null,
|
|
9
|
+
* as we need to set the elementRef and allow previewing and visual editing
|
|
10
|
+
*/
|
|
11
|
+
export declare const needsElementRefDivForEditing: () => boolean;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { TARGET } from '../../../constants/target';
|
|
2
|
+
import { isEditing } from '../../../functions/is-editing';
|
|
3
|
+
import { isPreviewing } from '../../../functions/is-previewing';
|
|
4
|
+
/**
|
|
5
|
+
* SDKS that use the elementRef approach to enable visual editing.
|
|
6
|
+
* We don't need to render the div for other SDKs as they attach event listeners to the window.
|
|
7
|
+
*/
|
|
8
|
+
export const SDKS_USING_ELEMENT_REF_APPROACH = ['svelte', 'qwik', 'vue'];
|
|
9
|
+
/**
|
|
10
|
+
* We need to attach this div only when content exists or isPreviewing/isEditing even when content is null,
|
|
11
|
+
* as we need to set the elementRef and allow previewing and visual editing
|
|
12
|
+
*/
|
|
13
|
+
export const needsElementRefDivForEditing = () => {
|
|
14
|
+
return SDKS_USING_ELEMENT_REF_APPROACH.includes(TARGET) && (isEditing() || isPreviewing());
|
|
15
|
+
};
|