@builder.io/sdk-svelte 2.0.18 → 2.0.21
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/components/awaiter.svelte +4 -7
- package/lib/browser/components/awaiter.svelte.d.ts +3 -1
- package/lib/browser/components/content/components/enable-editor.svelte +9 -46
- package/lib/browser/components/content/content.svelte +25 -2
- package/lib/browser/components/content/content.svelte.d.ts +2 -2
- package/lib/browser/constants/sdk-version.d.ts +1 -1
- package/lib/browser/constants/sdk-version.js +1 -1
- package/lib/browser/context/component-reference-types.d.ts +6 -4
- package/lib/browser/functions/evaluate/browser-runtime/browser.d.ts +0 -6
- package/lib/browser/functions/evaluate/browser-runtime/browser.js +1 -30
- package/lib/browser/functions/evaluate/edge-runtime/edge-runtime.js +73 -34
- package/lib/browser/functions/evaluate/helpers.d.ts +5 -0
- package/lib/browser/functions/evaluate/helpers.js +29 -0
- package/lib/edge/components/awaiter.svelte +4 -7
- package/lib/edge/components/awaiter.svelte.d.ts +3 -1
- package/lib/edge/components/content/components/enable-editor.svelte +9 -46
- package/lib/edge/components/content/content.svelte +25 -2
- package/lib/edge/components/content/content.svelte.d.ts +2 -2
- package/lib/edge/constants/sdk-version.d.ts +1 -1
- package/lib/edge/constants/sdk-version.js +1 -1
- package/lib/edge/context/component-reference-types.d.ts +6 -4
- package/lib/edge/functions/evaluate/browser-runtime/browser.d.ts +0 -6
- package/lib/edge/functions/evaluate/browser-runtime/browser.js +1 -30
- package/lib/edge/functions/evaluate/edge-runtime/edge-runtime.js +73 -34
- package/lib/edge/functions/evaluate/helpers.d.ts +5 -0
- package/lib/edge/functions/evaluate/helpers.js +29 -0
- package/lib/node/components/awaiter.svelte +4 -7
- package/lib/node/components/awaiter.svelte.d.ts +3 -1
- package/lib/node/components/content/components/enable-editor.svelte +9 -46
- package/lib/node/components/content/content.svelte +25 -2
- package/lib/node/components/content/content.svelte.d.ts +2 -2
- package/lib/node/constants/sdk-version.d.ts +1 -1
- package/lib/node/constants/sdk-version.js +1 -1
- package/lib/node/context/component-reference-types.d.ts +6 -4
- package/lib/node/functions/evaluate/browser-runtime/browser.d.ts +0 -6
- package/lib/node/functions/evaluate/browser-runtime/browser.js +1 -30
- package/lib/node/functions/evaluate/edge-runtime/edge-runtime.js +73 -34
- package/lib/node/functions/evaluate/helpers.d.ts +5 -0
- package/lib/node/functions/evaluate/helpers.js +29 -0
- package/package.json +1 -1
- package/src/components/awaiter.svelte +8 -8
- package/src/components/content/components/enable-editor.svelte +9 -52
- package/src/components/content/content.svelte +28 -2
- package/src/constants/sdk-version.ts +1 -1
- package/src/context/component-reference-types.ts +5 -3
- package/src/functions/evaluate/browser-runtime/browser.ts +1 -39
- package/src/functions/evaluate/edge-runtime/edge-runtime.ts +73 -35
- package/src/functions/evaluate/helpers.ts +37 -0
|
@@ -3,7 +3,9 @@ import { writable } from "svelte/store";
|
|
|
3
3
|
import { getDefaultRegisteredComponents } from "../../constants/builder-registered-components";
|
|
4
4
|
import { TARGET } from "../../constants/target";
|
|
5
5
|
import ComponentsContext from "../../context/components.context";
|
|
6
|
+
import { evaluate } from "../../functions/evaluate/evaluate";
|
|
6
7
|
import { serializeIncludingFunctions } from "../../functions/register-component";
|
|
8
|
+
import { logger } from "../../helpers/logger";
|
|
7
9
|
import Blocks from "../blocks/blocks.svelte";
|
|
8
10
|
import { getUpdateVariantVisibilityScript } from "../content-variants/helpers";
|
|
9
11
|
import "../dynamic-div.svelte";
|
|
@@ -12,14 +14,14 @@ import EnableEditor from "./components/enable-editor.svelte";
|
|
|
12
14
|
import ContentStyles from "./components/styles.svelte";
|
|
13
15
|
import { getContentInitialValue, getRootStateInitialValue, } from "./content.helpers";
|
|
14
16
|
import "./wrap-component-ref";
|
|
17
|
+
export let apiKey;
|
|
18
|
+
export let context = undefined;
|
|
15
19
|
export let content = undefined;
|
|
16
20
|
export let customComponents = undefined;
|
|
17
21
|
export let model = undefined;
|
|
18
22
|
export let data = undefined;
|
|
19
23
|
export let locale = undefined;
|
|
20
|
-
export let context = undefined;
|
|
21
24
|
export let canTrack = undefined;
|
|
22
|
-
export let apiKey;
|
|
23
25
|
export let apiVersion = undefined;
|
|
24
26
|
export let blocksWrapper = undefined;
|
|
25
27
|
export let blocksWrapperProps = undefined;
|
|
@@ -91,6 +93,27 @@ let builderContextSignal = writable({
|
|
|
91
93
|
BlocksWrapperProps: blocksWrapperProps || {},
|
|
92
94
|
nonce: nonce || "",
|
|
93
95
|
});
|
|
96
|
+
if (!apiKey) {
|
|
97
|
+
logger.error("No API key provided to `Content` component. This can cause issues. Please provide an API key using the `apiKey` prop.");
|
|
98
|
+
}
|
|
99
|
+
// run any dynamic JS code attached to content
|
|
100
|
+
const jsCode = $builderContextSignal.content?.data?.jsCode;
|
|
101
|
+
if (jsCode) {
|
|
102
|
+
evaluate({
|
|
103
|
+
code: jsCode,
|
|
104
|
+
context: context || {},
|
|
105
|
+
localState: undefined,
|
|
106
|
+
rootState: $builderContextSignal.rootState,
|
|
107
|
+
rootSetState: (newState) => {
|
|
108
|
+
$builderContextSignal.rootSetState?.(newState);
|
|
109
|
+
},
|
|
110
|
+
isExpression: false,
|
|
111
|
+
/**
|
|
112
|
+
* We don't want to cache the result of the JS code, since it's arbitrary side effect code.
|
|
113
|
+
*/
|
|
114
|
+
enableCache: false,
|
|
115
|
+
});
|
|
116
|
+
}
|
|
94
117
|
setContext(ComponentsContext.key, {
|
|
95
118
|
registeredComponents: registeredComponents,
|
|
96
119
|
});
|
|
@@ -2,14 +2,14 @@ import { SvelteComponent } from "svelte";
|
|
|
2
2
|
import type { ContentProps } from "./content.types";
|
|
3
3
|
declare const __propDef: {
|
|
4
4
|
props: {
|
|
5
|
+
apiKey: ContentProps["apiKey"];
|
|
6
|
+
context?: ContentProps["context"];
|
|
5
7
|
content?: ContentProps["content"];
|
|
6
8
|
customComponents?: ContentProps["customComponents"];
|
|
7
9
|
model?: ContentProps["model"];
|
|
8
10
|
data?: ContentProps["data"];
|
|
9
11
|
locale?: ContentProps["locale"];
|
|
10
|
-
context?: ContentProps["context"];
|
|
11
12
|
canTrack?: ContentProps["canTrack"];
|
|
12
|
-
apiKey: ContentProps["apiKey"];
|
|
13
13
|
apiVersion?: ContentProps["apiVersion"];
|
|
14
14
|
blocksWrapper?: ContentProps["blocksWrapper"];
|
|
15
15
|
blocksWrapperProps?: ContentProps["blocksWrapperProps"];
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare const SDK_VERSION = "2.0.
|
|
1
|
+
export declare const SDK_VERSION = "2.0.21";
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export const SDK_VERSION = "2.0.
|
|
1
|
+
export const SDK_VERSION = "2.0.21";
|
|
@@ -1,13 +1,15 @@
|
|
|
1
1
|
import type { SvelteComponent } from 'svelte';
|
|
2
2
|
export type ComponentReference = {
|
|
3
3
|
/**
|
|
4
|
-
*
|
|
4
|
+
* Either:
|
|
5
|
+
* - A dynamic import function pointing to the component.
|
|
5
6
|
* Example: `() => import('../components/my-component.svelte')`
|
|
6
|
-
*
|
|
7
|
+
* - A string representing the path to the component.
|
|
8
|
+
* Example: `'../components/my-component.svelte'`
|
|
7
9
|
*/
|
|
8
|
-
load: () => Promise<{
|
|
10
|
+
load: string | (() => Promise<{
|
|
9
11
|
default: typeof SvelteComponent<any>;
|
|
10
|
-
}
|
|
12
|
+
}>);
|
|
11
13
|
/**
|
|
12
14
|
* An optional fallback component to render while the dynamic component is loading.
|
|
13
15
|
*/
|
|
@@ -1,8 +1,2 @@
|
|
|
1
|
-
import type { BuilderRenderState } from '../../../context/types';
|
|
2
1
|
import type { ExecutorArgs } from '../helpers';
|
|
3
2
|
export declare const runInBrowser: ({ code, builder, context, event, localState, rootSetState, rootState }: ExecutorArgs) => any;
|
|
4
|
-
export declare function flattenState({ rootState, localState, rootSetState }: {
|
|
5
|
-
rootState: Record<string | symbol, any>;
|
|
6
|
-
localState: Record<string | symbol, any> | undefined;
|
|
7
|
-
rootSetState: ((rootState: BuilderRenderState) => void) | undefined;
|
|
8
|
-
}): BuilderRenderState;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { getFunctionArguments } from '../helpers';
|
|
1
|
+
import { flattenState, getFunctionArguments } from '../helpers';
|
|
2
2
|
export const runInBrowser = ({ code, builder, context, event, localState, rootSetState, rootState }) => {
|
|
3
3
|
const functionArgs = getFunctionArguments({
|
|
4
4
|
builder,
|
|
@@ -12,32 +12,3 @@ export const runInBrowser = ({ code, builder, context, event, localState, rootSe
|
|
|
12
12
|
});
|
|
13
13
|
return new Function(...functionArgs.map(([name]) => name), code)(...functionArgs.map(([, value]) => value));
|
|
14
14
|
};
|
|
15
|
-
export function flattenState({ rootState, localState, rootSetState }) {
|
|
16
|
-
return new Proxy(rootState, {
|
|
17
|
-
get: (target, prop) => {
|
|
18
|
-
if (localState && prop in localState) {
|
|
19
|
-
return localState[prop];
|
|
20
|
-
}
|
|
21
|
-
const val = target[prop];
|
|
22
|
-
if (typeof val === 'object' && val !== null) {
|
|
23
|
-
return flattenState({
|
|
24
|
-
rootState: val,
|
|
25
|
-
localState: undefined,
|
|
26
|
-
rootSetState: rootSetState ? subState => {
|
|
27
|
-
target[prop] = subState;
|
|
28
|
-
rootSetState(target);
|
|
29
|
-
} : undefined
|
|
30
|
-
});
|
|
31
|
-
}
|
|
32
|
-
return val;
|
|
33
|
-
},
|
|
34
|
-
set: (target, prop, value) => {
|
|
35
|
-
if (localState && prop in localState) {
|
|
36
|
-
throw new Error('Writing to local state is not allowed as it is read-only.');
|
|
37
|
-
}
|
|
38
|
-
target[prop] = value;
|
|
39
|
-
rootSetState?.(target);
|
|
40
|
-
return true;
|
|
41
|
-
}
|
|
42
|
-
});
|
|
43
|
-
}
|
|
@@ -1,32 +1,72 @@
|
|
|
1
1
|
import { logger } from '../../../helpers/logger';
|
|
2
|
-
import {
|
|
3
|
-
import { getFunctionArguments } from '../helpers';
|
|
2
|
+
import { flattenState, getFunctionArguments } from '../helpers';
|
|
4
3
|
import Interpreter from './acorn-interpreter';
|
|
4
|
+
/**
|
|
5
|
+
* https://stackoverflow.com/a/46503625
|
|
6
|
+
*/
|
|
7
|
+
function patchInterpreter() {
|
|
8
|
+
const originalGetProperty = Interpreter.prototype.getProperty;
|
|
9
|
+
const originalSetProperty = Interpreter.prototype.setProperty;
|
|
10
|
+
function newGetProperty(obj, name) {
|
|
11
|
+
if (obj == null || !obj._connected) {
|
|
12
|
+
return originalGetProperty.call(this, obj, name);
|
|
13
|
+
}
|
|
14
|
+
const value = obj._connected[name];
|
|
15
|
+
if (Array.isArray(value)) {
|
|
16
|
+
return this.nativeToPseudo(value);
|
|
17
|
+
}
|
|
18
|
+
if (typeof value === 'object') {
|
|
19
|
+
// if the value is an object itself, create another connected object
|
|
20
|
+
return this.createConnectedObject(value);
|
|
21
|
+
}
|
|
22
|
+
return value;
|
|
23
|
+
}
|
|
24
|
+
function newSetProperty(obj, name, value, opt_descriptor) {
|
|
25
|
+
if (obj == null || !obj._connected) {
|
|
26
|
+
return originalSetProperty.call(this, obj, name, value, opt_descriptor);
|
|
27
|
+
}
|
|
28
|
+
obj._connected[name] = this.pseudoToNative(value);
|
|
29
|
+
}
|
|
30
|
+
const getKeys = [];
|
|
31
|
+
const setKeys = [];
|
|
32
|
+
for (const key of Object.keys(Interpreter.prototype)) {
|
|
33
|
+
if (Interpreter.prototype[key] === originalGetProperty) {
|
|
34
|
+
getKeys.push(key);
|
|
35
|
+
}
|
|
36
|
+
if (Interpreter.prototype[key] === originalSetProperty) {
|
|
37
|
+
setKeys.push(key);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
for (const key of getKeys) {
|
|
41
|
+
Interpreter.prototype[key] = newGetProperty;
|
|
42
|
+
}
|
|
43
|
+
for (const key of setKeys) {
|
|
44
|
+
Interpreter.prototype[key] = newSetProperty;
|
|
45
|
+
}
|
|
46
|
+
Interpreter.prototype.createConnectedObject = function (obj) {
|
|
47
|
+
const connectedObject = this.createObject(this.OBJECT);
|
|
48
|
+
connectedObject._connected = obj;
|
|
49
|
+
return connectedObject;
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
patchInterpreter();
|
|
5
53
|
const processCode = (code) => {
|
|
6
|
-
return code
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
return line;
|
|
15
|
-
const [lhs, rhs] = trimmed.split('=');
|
|
16
|
-
const setStr = lhs.replace('state.', '').trim();
|
|
17
|
-
const setExpr = `setRootState('${setStr}', ${rhs.trim()})`;
|
|
18
|
-
return `
|
|
19
|
-
${line}
|
|
20
|
-
${setExpr}
|
|
21
|
-
`;
|
|
22
|
-
}).filter(Boolean).join('\n');
|
|
54
|
+
return code
|
|
55
|
+
// strip async/await polyfill: remove anything before 'function main()'
|
|
56
|
+
.replace(/^.*?function main\(\)/, `
|
|
57
|
+
var __awaiter = function (e, t, n, r) {return r()},
|
|
58
|
+
__generator = function (e, t) { return t() };
|
|
59
|
+
function main()`)
|
|
60
|
+
// replace ?. with .
|
|
61
|
+
.replace(/\?\./g, '.');
|
|
23
62
|
};
|
|
24
63
|
const getJSONValName = (val) => val + 'JSON';
|
|
25
64
|
export const runInEdge = ({ builder, context, event, rootState, localState, rootSetState, code }) => {
|
|
26
|
-
const state = {
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
65
|
+
const state = flattenState({
|
|
66
|
+
rootState,
|
|
67
|
+
localState,
|
|
68
|
+
rootSetState
|
|
69
|
+
});
|
|
30
70
|
const properties = getFunctionArguments({
|
|
31
71
|
builder,
|
|
32
72
|
context,
|
|
@@ -38,6 +78,9 @@ export const runInEdge = ({ builder, context, event, rootState, localState, root
|
|
|
38
78
|
*/
|
|
39
79
|
const prependedCode = properties.map(([key]) => {
|
|
40
80
|
const jsonValName = getJSONValName(key);
|
|
81
|
+
if (key === 'state') {
|
|
82
|
+
return ``;
|
|
83
|
+
}
|
|
41
84
|
return `var ${key} = ${jsonValName} === undefined ? undefined : JSON.parse(${jsonValName});`;
|
|
42
85
|
}).join('\n');
|
|
43
86
|
const cleanedCode = processCode(code);
|
|
@@ -53,23 +96,19 @@ function theFunction() {
|
|
|
53
96
|
}
|
|
54
97
|
theFunction();
|
|
55
98
|
`;
|
|
56
|
-
const setRootState = (prop, value) => {
|
|
57
|
-
const newState = set(state, prop, value);
|
|
58
|
-
rootSetState?.(newState);
|
|
59
|
-
};
|
|
60
99
|
const initFunc = function (interpreter, globalObject) {
|
|
61
100
|
/**
|
|
62
101
|
* serialize all function args to JSON strings
|
|
63
102
|
*/
|
|
64
103
|
properties.forEach(([key, val]) => {
|
|
65
|
-
|
|
66
|
-
|
|
104
|
+
if (key === 'state') {
|
|
105
|
+
interpreter.setProperty(globalObject, key, interpreter.createConnectedObject(val), interpreter.READONLY_DESCRIPTOR);
|
|
106
|
+
}
|
|
107
|
+
else {
|
|
108
|
+
const jsonVal = JSON.stringify(val);
|
|
109
|
+
interpreter.setProperty(globalObject, getJSONValName(key), jsonVal);
|
|
110
|
+
}
|
|
67
111
|
});
|
|
68
|
-
/**
|
|
69
|
-
* Add a JavaScript function "setRootState" to the interpreter's global object, that will be called whenever a
|
|
70
|
-
* state property is set. This function will update the state object.
|
|
71
|
-
*/
|
|
72
|
-
interpreter.setProperty(globalObject, 'setRootState', interpreter.createNativeFunction(setRootState));
|
|
73
112
|
};
|
|
74
113
|
const myInterpreter = new Interpreter(transformed, initFunc);
|
|
75
114
|
myInterpreter.run();
|
|
@@ -23,3 +23,8 @@ export declare const getFunctionArguments: ({ builder, context, event, state }:
|
|
|
23
23
|
}) => [string, Event | BuilderRenderState | import("../../context/types").BuilderRenderContext | BuilderGlobals | undefined][];
|
|
24
24
|
export declare const getBuilderGlobals: () => BuilderGlobals;
|
|
25
25
|
export declare const parseCode: (code: string, { isExpression }: Pick<EvaluatorArgs, 'isExpression'>) => string;
|
|
26
|
+
export declare function flattenState({ rootState, localState, rootSetState }: {
|
|
27
|
+
rootState: Record<string | symbol, any>;
|
|
28
|
+
localState: Record<string | symbol, any> | undefined;
|
|
29
|
+
rootSetState: ((rootState: BuilderRenderState) => void) | undefined;
|
|
30
|
+
}): BuilderRenderState;
|
|
@@ -26,3 +26,32 @@ export const parseCode = (code, { isExpression = true }) => {
|
|
|
26
26
|
const useCode = useReturn ? `return (${code});` : code;
|
|
27
27
|
return useCode;
|
|
28
28
|
};
|
|
29
|
+
export function flattenState({ rootState, localState, rootSetState }) {
|
|
30
|
+
return new Proxy(rootState, {
|
|
31
|
+
get: (target, prop) => {
|
|
32
|
+
if (localState && prop in localState) {
|
|
33
|
+
return localState[prop];
|
|
34
|
+
}
|
|
35
|
+
const val = target[prop];
|
|
36
|
+
if (typeof val === 'object' && val !== null) {
|
|
37
|
+
return flattenState({
|
|
38
|
+
rootState: val,
|
|
39
|
+
localState: undefined,
|
|
40
|
+
rootSetState: rootSetState ? subState => {
|
|
41
|
+
target[prop] = subState;
|
|
42
|
+
rootSetState(target);
|
|
43
|
+
} : undefined
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
return val;
|
|
47
|
+
},
|
|
48
|
+
set: (target, prop, value) => {
|
|
49
|
+
if (localState && prop in localState) {
|
|
50
|
+
throw new Error('Writing to local state is not allowed as it is read-only.');
|
|
51
|
+
}
|
|
52
|
+
target[prop] = value;
|
|
53
|
+
rootSetState?.(target);
|
|
54
|
+
return true;
|
|
55
|
+
}
|
|
56
|
+
});
|
|
57
|
+
}
|
|
@@ -2,18 +2,15 @@
|
|
|
2
2
|
export let fallback;
|
|
3
3
|
export let props;
|
|
4
4
|
export let attributes;
|
|
5
|
+
const componentImport = typeof load === 'string' ? import(load) : load();
|
|
5
6
|
</script>
|
|
6
7
|
|
|
7
|
-
{#await
|
|
8
|
+
{#await componentImport}
|
|
8
9
|
{#if fallback}
|
|
9
10
|
<svelte:component this={fallback} />
|
|
10
11
|
{/if}
|
|
11
|
-
|
|
12
|
-
<svelte:component
|
|
13
|
-
this={Component}
|
|
14
|
-
{...props}
|
|
15
|
-
attributes={attributes}
|
|
16
|
-
>
|
|
12
|
+
{:then { default: Component }}
|
|
13
|
+
<svelte:component this={Component} {...props} {attributes}>
|
|
17
14
|
<slot />
|
|
18
15
|
</svelte:component>
|
|
19
16
|
{/await}
|
|
@@ -5,7 +5,6 @@ import builderContext from "../../../context/builder.context";
|
|
|
5
5
|
import { evaluate } from "../../../functions/evaluate/index";
|
|
6
6
|
import { fastClone } from "../../../functions/fast-clone";
|
|
7
7
|
import { fetchOneEntry } from "../../../functions/get-content/index";
|
|
8
|
-
import { fetch } from "../../../functions/get-fetch";
|
|
9
8
|
import { isBrowser } from "../../../functions/is-browser";
|
|
10
9
|
import { isEditing } from "../../../functions/is-editing";
|
|
11
10
|
import { isPreviewing } from "../../../functions/is-previewing";
|
|
@@ -13,7 +12,6 @@ import { createRegisterComponentMessage } from "../../../functions/register-comp
|
|
|
13
12
|
import { _track } from "../../../functions/track/index";
|
|
14
13
|
import { getInteractionPropertiesForEvent } from "../../../functions/track/interaction";
|
|
15
14
|
import { getDefaultCanTrack } from "../../../helpers/canTrack";
|
|
16
|
-
import { logger } from "../../../helpers/logger";
|
|
17
15
|
import "../../../helpers/preview-lru-cache/set";
|
|
18
16
|
import { createEditorListener } from "../../../helpers/subscribe-to-editor";
|
|
19
17
|
import { registerInsertMenu, setupBrowserForEditing, } from "../../../scripts/init-editing";
|
|
@@ -89,23 +87,6 @@ function processMessage(event) {
|
|
|
89
87
|
},
|
|
90
88
|
})(event);
|
|
91
89
|
}
|
|
92
|
-
function evaluateJsCode() {
|
|
93
|
-
// run any dynamic JS code attached to content
|
|
94
|
-
const jsCode = $builderContextSignal.content?.data?.jsCode;
|
|
95
|
-
if (jsCode) {
|
|
96
|
-
evaluate({
|
|
97
|
-
code: jsCode,
|
|
98
|
-
context: context || {},
|
|
99
|
-
localState: undefined,
|
|
100
|
-
rootState: $builderContextSignal.rootState,
|
|
101
|
-
rootSetState: $builderContextSignal.rootSetState,
|
|
102
|
-
/**
|
|
103
|
-
* We don't want to cache the result of the JS code, since it's arbitrary side effect code.
|
|
104
|
-
*/
|
|
105
|
-
enableCache: false,
|
|
106
|
-
});
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
90
|
function onClick(event) {
|
|
110
91
|
if ($builderContextSignal.content) {
|
|
111
92
|
const variationId = $builderContextSignal.content?.testVariationId;
|
|
@@ -239,6 +220,8 @@ let ContentWrapper = contentWrapper || "div";
|
|
|
239
220
|
let httpReqsData = {};
|
|
240
221
|
let httpReqsPending = {};
|
|
241
222
|
let clicked = false;
|
|
223
|
+
runHttpRequests();
|
|
224
|
+
emitStateUpdate();
|
|
242
225
|
onMount(() => {
|
|
243
226
|
if (isBrowser()) {
|
|
244
227
|
if (isEditing()) {
|
|
@@ -270,14 +253,6 @@ onMount(() => {
|
|
|
270
253
|
}
|
|
271
254
|
}
|
|
272
255
|
});
|
|
273
|
-
onMount(() => {
|
|
274
|
-
if (!apiKey) {
|
|
275
|
-
logger.error("No API key provided to `Content` component. This can cause issues. Please provide an API key using the `apiKey` prop.");
|
|
276
|
-
}
|
|
277
|
-
evaluateJsCode();
|
|
278
|
-
runHttpRequests();
|
|
279
|
-
emitStateUpdate();
|
|
280
|
-
});
|
|
281
256
|
function onUpdateFn_0(..._args) {
|
|
282
257
|
if (content) {
|
|
283
258
|
mergeNewContent(content);
|
|
@@ -285,37 +260,25 @@ function onUpdateFn_0(..._args) {
|
|
|
285
260
|
}
|
|
286
261
|
$: onUpdateFn_0(...[content]);
|
|
287
262
|
function onUpdateFn_1(..._args) {
|
|
288
|
-
evaluateJsCode();
|
|
289
|
-
}
|
|
290
|
-
$: onUpdateFn_1_builderContextSignal_content__data__jsCode =
|
|
291
|
-
$builderContextSignal.content?.data?.jsCode;
|
|
292
|
-
$: onUpdateFn_1(...[onUpdateFn_1_builderContextSignal_content__data__jsCode]);
|
|
293
|
-
function onUpdateFn_2(..._args) {
|
|
294
|
-
runHttpRequests();
|
|
295
|
-
}
|
|
296
|
-
$: onUpdateFn_2_builderContextSignal_content__data__httpRequests =
|
|
297
|
-
$builderContextSignal.content?.data?.httpRequests;
|
|
298
|
-
$: onUpdateFn_2(...[onUpdateFn_2_builderContextSignal_content__data__httpRequests]);
|
|
299
|
-
function onUpdateFn_3(..._args) {
|
|
300
263
|
emitStateUpdate();
|
|
301
264
|
}
|
|
302
|
-
$:
|
|
265
|
+
$: onUpdateFn_1_builderContextSignal_rootState =
|
|
303
266
|
$builderContextSignal.rootState;
|
|
304
|
-
$:
|
|
305
|
-
function
|
|
267
|
+
$: onUpdateFn_1(...[onUpdateFn_1_builderContextSignal_rootState]);
|
|
268
|
+
function onUpdateFn_2(..._args) {
|
|
306
269
|
if (data) {
|
|
307
270
|
mergeNewRootState(data);
|
|
308
271
|
}
|
|
309
272
|
}
|
|
310
|
-
$:
|
|
311
|
-
function
|
|
273
|
+
$: onUpdateFn_2(...[data]);
|
|
274
|
+
function onUpdateFn_3(..._args) {
|
|
312
275
|
if (locale) {
|
|
313
276
|
mergeNewRootState({
|
|
314
277
|
locale: locale,
|
|
315
278
|
});
|
|
316
279
|
}
|
|
317
280
|
}
|
|
318
|
-
$:
|
|
281
|
+
$: onUpdateFn_3(...[locale]);
|
|
319
282
|
setContext(builderContext.key, builderContextSignal);
|
|
320
283
|
onDestroy(() => {
|
|
321
284
|
if (isBrowser()) {
|
|
@@ -335,10 +298,10 @@ onDestroy(() => {
|
|
|
335
298
|
}}
|
|
336
299
|
builder-content-id={$builderContextSignal.content?.id}
|
|
337
300
|
builder-model={model}
|
|
301
|
+
className={getWrapperClassName(content?.testVariationId || content?.id)}
|
|
338
302
|
{...{}}
|
|
339
303
|
{...showContentProps()}
|
|
340
304
|
{...contentWrapperProps}
|
|
341
|
-
class={getWrapperClassName(content?.testVariationId || content?.id)}
|
|
342
305
|
on:initeditingbldr={(event) => {
|
|
343
306
|
elementRef_onIniteditingbldr(event);
|
|
344
307
|
}}
|
|
@@ -3,7 +3,9 @@ import { writable } from "svelte/store";
|
|
|
3
3
|
import { getDefaultRegisteredComponents } from "../../constants/builder-registered-components";
|
|
4
4
|
import { TARGET } from "../../constants/target";
|
|
5
5
|
import ComponentsContext from "../../context/components.context";
|
|
6
|
+
import { evaluate } from "../../functions/evaluate/evaluate";
|
|
6
7
|
import { serializeIncludingFunctions } from "../../functions/register-component";
|
|
8
|
+
import { logger } from "../../helpers/logger";
|
|
7
9
|
import Blocks from "../blocks/blocks.svelte";
|
|
8
10
|
import { getUpdateVariantVisibilityScript } from "../content-variants/helpers";
|
|
9
11
|
import "../dynamic-div.svelte";
|
|
@@ -12,14 +14,14 @@ import EnableEditor from "./components/enable-editor.svelte";
|
|
|
12
14
|
import ContentStyles from "./components/styles.svelte";
|
|
13
15
|
import { getContentInitialValue, getRootStateInitialValue, } from "./content.helpers";
|
|
14
16
|
import "./wrap-component-ref";
|
|
17
|
+
export let apiKey;
|
|
18
|
+
export let context = undefined;
|
|
15
19
|
export let content = undefined;
|
|
16
20
|
export let customComponents = undefined;
|
|
17
21
|
export let model = undefined;
|
|
18
22
|
export let data = undefined;
|
|
19
23
|
export let locale = undefined;
|
|
20
|
-
export let context = undefined;
|
|
21
24
|
export let canTrack = undefined;
|
|
22
|
-
export let apiKey;
|
|
23
25
|
export let apiVersion = undefined;
|
|
24
26
|
export let blocksWrapper = undefined;
|
|
25
27
|
export let blocksWrapperProps = undefined;
|
|
@@ -91,6 +93,27 @@ let builderContextSignal = writable({
|
|
|
91
93
|
BlocksWrapperProps: blocksWrapperProps || {},
|
|
92
94
|
nonce: nonce || "",
|
|
93
95
|
});
|
|
96
|
+
if (!apiKey) {
|
|
97
|
+
logger.error("No API key provided to `Content` component. This can cause issues. Please provide an API key using the `apiKey` prop.");
|
|
98
|
+
}
|
|
99
|
+
// run any dynamic JS code attached to content
|
|
100
|
+
const jsCode = $builderContextSignal.content?.data?.jsCode;
|
|
101
|
+
if (jsCode) {
|
|
102
|
+
evaluate({
|
|
103
|
+
code: jsCode,
|
|
104
|
+
context: context || {},
|
|
105
|
+
localState: undefined,
|
|
106
|
+
rootState: $builderContextSignal.rootState,
|
|
107
|
+
rootSetState: (newState) => {
|
|
108
|
+
$builderContextSignal.rootSetState?.(newState);
|
|
109
|
+
},
|
|
110
|
+
isExpression: false,
|
|
111
|
+
/**
|
|
112
|
+
* We don't want to cache the result of the JS code, since it's arbitrary side effect code.
|
|
113
|
+
*/
|
|
114
|
+
enableCache: false,
|
|
115
|
+
});
|
|
116
|
+
}
|
|
94
117
|
setContext(ComponentsContext.key, {
|
|
95
118
|
registeredComponents: registeredComponents,
|
|
96
119
|
});
|
|
@@ -2,14 +2,14 @@ import { SvelteComponent } from "svelte";
|
|
|
2
2
|
import type { ContentProps } from "./content.types";
|
|
3
3
|
declare const __propDef: {
|
|
4
4
|
props: {
|
|
5
|
+
apiKey: ContentProps["apiKey"];
|
|
6
|
+
context?: ContentProps["context"];
|
|
5
7
|
content?: ContentProps["content"];
|
|
6
8
|
customComponents?: ContentProps["customComponents"];
|
|
7
9
|
model?: ContentProps["model"];
|
|
8
10
|
data?: ContentProps["data"];
|
|
9
11
|
locale?: ContentProps["locale"];
|
|
10
|
-
context?: ContentProps["context"];
|
|
11
12
|
canTrack?: ContentProps["canTrack"];
|
|
12
|
-
apiKey: ContentProps["apiKey"];
|
|
13
13
|
apiVersion?: ContentProps["apiVersion"];
|
|
14
14
|
blocksWrapper?: ContentProps["blocksWrapper"];
|
|
15
15
|
blocksWrapperProps?: ContentProps["blocksWrapperProps"];
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare const SDK_VERSION = "2.0.
|
|
1
|
+
export declare const SDK_VERSION = "2.0.21";
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export const SDK_VERSION = "2.0.
|
|
1
|
+
export const SDK_VERSION = "2.0.21";
|
|
@@ -1,13 +1,15 @@
|
|
|
1
1
|
import type { SvelteComponent } from 'svelte';
|
|
2
2
|
export type ComponentReference = {
|
|
3
3
|
/**
|
|
4
|
-
*
|
|
4
|
+
* Either:
|
|
5
|
+
* - A dynamic import function pointing to the component.
|
|
5
6
|
* Example: `() => import('../components/my-component.svelte')`
|
|
6
|
-
*
|
|
7
|
+
* - A string representing the path to the component.
|
|
8
|
+
* Example: `'../components/my-component.svelte'`
|
|
7
9
|
*/
|
|
8
|
-
load: () => Promise<{
|
|
10
|
+
load: string | (() => Promise<{
|
|
9
11
|
default: typeof SvelteComponent<any>;
|
|
10
|
-
}
|
|
12
|
+
}>);
|
|
11
13
|
/**
|
|
12
14
|
* An optional fallback component to render while the dynamic component is loading.
|
|
13
15
|
*/
|
|
@@ -1,8 +1,2 @@
|
|
|
1
|
-
import type { BuilderRenderState } from '../../../context/types';
|
|
2
1
|
import type { ExecutorArgs } from '../helpers';
|
|
3
2
|
export declare const runInBrowser: ({ code, builder, context, event, localState, rootSetState, rootState }: ExecutorArgs) => any;
|
|
4
|
-
export declare function flattenState({ rootState, localState, rootSetState }: {
|
|
5
|
-
rootState: Record<string | symbol, any>;
|
|
6
|
-
localState: Record<string | symbol, any> | undefined;
|
|
7
|
-
rootSetState: ((rootState: BuilderRenderState) => void) | undefined;
|
|
8
|
-
}): BuilderRenderState;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { getFunctionArguments } from '../helpers';
|
|
1
|
+
import { flattenState, getFunctionArguments } from '../helpers';
|
|
2
2
|
export const runInBrowser = ({ code, builder, context, event, localState, rootSetState, rootState }) => {
|
|
3
3
|
const functionArgs = getFunctionArguments({
|
|
4
4
|
builder,
|
|
@@ -12,32 +12,3 @@ export const runInBrowser = ({ code, builder, context, event, localState, rootSe
|
|
|
12
12
|
});
|
|
13
13
|
return new Function(...functionArgs.map(([name]) => name), code)(...functionArgs.map(([, value]) => value));
|
|
14
14
|
};
|
|
15
|
-
export function flattenState({ rootState, localState, rootSetState }) {
|
|
16
|
-
return new Proxy(rootState, {
|
|
17
|
-
get: (target, prop) => {
|
|
18
|
-
if (localState && prop in localState) {
|
|
19
|
-
return localState[prop];
|
|
20
|
-
}
|
|
21
|
-
const val = target[prop];
|
|
22
|
-
if (typeof val === 'object' && val !== null) {
|
|
23
|
-
return flattenState({
|
|
24
|
-
rootState: val,
|
|
25
|
-
localState: undefined,
|
|
26
|
-
rootSetState: rootSetState ? subState => {
|
|
27
|
-
target[prop] = subState;
|
|
28
|
-
rootSetState(target);
|
|
29
|
-
} : undefined
|
|
30
|
-
});
|
|
31
|
-
}
|
|
32
|
-
return val;
|
|
33
|
-
},
|
|
34
|
-
set: (target, prop, value) => {
|
|
35
|
-
if (localState && prop in localState) {
|
|
36
|
-
throw new Error('Writing to local state is not allowed as it is read-only.');
|
|
37
|
-
}
|
|
38
|
-
target[prop] = value;
|
|
39
|
-
rootSetState?.(target);
|
|
40
|
-
return true;
|
|
41
|
-
}
|
|
42
|
-
});
|
|
43
|
-
}
|