@builder.io/sdk-svelte 0.6.4 → 0.7.1-0
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/custom-code/custom-code.svelte +35 -37
- package/lib/browser/components/content/components/enable-editor.svelte +86 -70
- package/lib/browser/components/content/components/enable-editor.svelte.d.ts +4 -4
- package/lib/browser/constants/sdk-version.d.ts +1 -1
- package/lib/browser/constants/sdk-version.js +1 -1
- package/lib/browser/functions/evaluate/choose-eval.d.ts +6 -0
- package/lib/browser/functions/evaluate/choose-eval.js +19 -0
- package/lib/browser/functions/evaluate/evaluate.d.ts +1 -5
- package/lib/browser/functions/evaluate/evaluate.js +7 -30
- package/lib/browser/functions/evaluate/helpers.d.ts +7 -1
- package/lib/browser/functions/evaluate/helpers.js +18 -0
- package/lib/browser/functions/evaluate/node-runtime/index.d.ts +1 -1
- package/lib/browser/functions/evaluate/node-runtime/index.js +1 -1
- package/lib/browser/functions/evaluate/node-runtime/node-runtime.d.ts +2 -0
- package/lib/browser/functions/evaluate/node-runtime/node-runtime.js +120 -0
- package/lib/browser/functions/evaluate/node-runtime/safeDynamicRequire.d.ts +13 -0
- package/lib/browser/functions/evaluate/node-runtime/safeDynamicRequire.js +18 -0
- package/lib/browser/functions/get-content/generate-content-url.js +32 -1
- package/lib/browser/functions/get-content/types.d.ts +94 -3
- package/lib/browser/functions/is-edge-runtime.js +2 -2
- package/lib/browser/functions/is-node-runtime.d.ts +4 -0
- package/lib/browser/functions/is-node-runtime.js +7 -0
- package/lib/edge/blocks/custom-code/custom-code.svelte +35 -37
- package/lib/edge/components/content/components/enable-editor.svelte +86 -70
- package/lib/edge/components/content/components/enable-editor.svelte.d.ts +4 -4
- package/lib/edge/constants/sdk-version.d.ts +1 -1
- package/lib/edge/constants/sdk-version.js +1 -1
- package/lib/edge/functions/evaluate/choose-eval.d.ts +6 -0
- package/lib/edge/functions/evaluate/choose-eval.js +19 -0
- package/lib/edge/functions/evaluate/evaluate.d.ts +1 -5
- package/lib/edge/functions/evaluate/evaluate.js +7 -30
- package/lib/edge/functions/evaluate/helpers.d.ts +7 -1
- package/lib/edge/functions/evaluate/helpers.js +18 -0
- package/lib/edge/functions/evaluate/node-runtime/index.d.ts +1 -1
- package/lib/edge/functions/evaluate/node-runtime/index.js +1 -1
- package/lib/edge/functions/evaluate/node-runtime/node-runtime.d.ts +2 -0
- package/lib/edge/functions/evaluate/node-runtime/node-runtime.js +120 -0
- package/lib/edge/functions/evaluate/node-runtime/safeDynamicRequire.d.ts +13 -0
- package/lib/edge/functions/evaluate/node-runtime/safeDynamicRequire.js +18 -0
- package/lib/edge/functions/get-content/generate-content-url.js +32 -1
- package/lib/edge/functions/get-content/types.d.ts +94 -3
- package/lib/edge/functions/is-edge-runtime.js +2 -2
- package/lib/edge/functions/is-node-runtime.d.ts +4 -0
- package/lib/edge/functions/is-node-runtime.js +7 -0
- package/lib/node/blocks/custom-code/custom-code.svelte +35 -37
- package/lib/node/components/content/components/enable-editor.svelte +86 -70
- package/lib/node/components/content/components/enable-editor.svelte.d.ts +4 -4
- package/lib/node/constants/sdk-version.d.ts +1 -1
- package/lib/node/constants/sdk-version.js +1 -1
- package/lib/node/functions/evaluate/choose-eval.d.ts +6 -0
- package/lib/node/functions/evaluate/choose-eval.js +19 -0
- package/lib/node/functions/evaluate/evaluate.d.ts +1 -5
- package/lib/node/functions/evaluate/evaluate.js +7 -30
- package/lib/node/functions/evaluate/helpers.d.ts +7 -1
- package/lib/node/functions/evaluate/helpers.js +18 -0
- package/lib/node/functions/evaluate/node-runtime/index.d.ts +1 -1
- package/lib/node/functions/evaluate/node-runtime/index.js +1 -1
- package/lib/node/functions/evaluate/node-runtime/node-runtime.d.ts +2 -0
- package/lib/node/functions/evaluate/node-runtime/node-runtime.js +120 -0
- package/lib/node/functions/evaluate/node-runtime/safeDynamicRequire.d.ts +13 -0
- package/lib/node/functions/evaluate/node-runtime/safeDynamicRequire.js +18 -0
- package/lib/node/functions/get-content/generate-content-url.js +32 -1
- package/lib/node/functions/get-content/types.d.ts +94 -3
- package/lib/node/functions/is-edge-runtime.js +2 -2
- package/lib/node/functions/is-node-runtime.d.ts +4 -0
- package/lib/node/functions/is-node-runtime.js +7 -0
- package/package.json +3 -1
- package/src/blocks/custom-code/custom-code.svelte +37 -40
- package/src/blocks/image/image.svelte +1 -1
- package/src/components/content/components/enable-editor.svelte +101 -84
- package/src/constants/sdk-version.ts +1 -1
- package/src/functions/evaluate/choose-eval.ts +22 -0
- package/src/functions/evaluate/evaluate.ts +8 -37
- package/src/functions/evaluate/helpers.ts +24 -1
- package/src/functions/evaluate/node-runtime/index.ts +1 -1
- package/src/functions/evaluate/node-runtime/node-runtime.ts +138 -0
- package/src/functions/evaluate/node-runtime/safeDynamicRequire.ts +18 -0
- package/src/functions/get-content/generate-content-url.ts +44 -2
- package/src/functions/get-content/types.ts +101 -3
- package/src/functions/is-edge-runtime.ts +2 -2
- package/src/functions/is-node-runtime.ts +8 -0
|
@@ -5,11 +5,42 @@ export interface GetContentOptions {
|
|
|
5
5
|
apiKey: string;
|
|
6
6
|
/** Number of items to fetch. Default is 1 */
|
|
7
7
|
limit?: number;
|
|
8
|
-
/**
|
|
8
|
+
/**
|
|
9
|
+
* Use to specify an offset for pagination of results. The default is 0.
|
|
10
|
+
*/
|
|
11
|
+
offset?: number;
|
|
12
|
+
/**
|
|
13
|
+
* User attribute key value pairs to be used for targeting
|
|
14
|
+
* https://www.builder.io/c/docs/custom-targeting-attributes
|
|
15
|
+
*
|
|
16
|
+
* e.g.
|
|
17
|
+
* ```js
|
|
18
|
+
* userAttributes: {
|
|
19
|
+
* urlPath: '/',
|
|
20
|
+
* returnVisitor: true,
|
|
21
|
+
* device: 'mobile'
|
|
22
|
+
* }
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
9
25
|
userAttributes?: (Record<string, string> & {
|
|
10
26
|
urlPath?: string;
|
|
11
27
|
}) | null;
|
|
12
|
-
/**
|
|
28
|
+
/**
|
|
29
|
+
* Mongodb style query of your data. E.g.:
|
|
30
|
+
*
|
|
31
|
+
* ```js
|
|
32
|
+
* query: {
|
|
33
|
+
* id: 'abc123',
|
|
34
|
+
* data: {
|
|
35
|
+
* myCustomField: { $gt: 20 },
|
|
36
|
+
* }
|
|
37
|
+
* }
|
|
38
|
+
* ```
|
|
39
|
+
*
|
|
40
|
+
* See more info on MongoDB's query operators and format.
|
|
41
|
+
*
|
|
42
|
+
* @see {@link https://docs.mongodb.com/manual/reference/operator/query/}
|
|
43
|
+
*/
|
|
13
44
|
query?: Record<string, any>;
|
|
14
45
|
/**
|
|
15
46
|
* Any other API options.
|
|
@@ -30,7 +61,7 @@ export interface GetContentOptions {
|
|
|
30
61
|
*/
|
|
31
62
|
canTrack?: boolean;
|
|
32
63
|
/**
|
|
33
|
-
* Include references in the response. Defaults to `true`.
|
|
64
|
+
* Include content of references in the response. Defaults to `true`.
|
|
34
65
|
* @deprecated use `enrich` instead
|
|
35
66
|
*/
|
|
36
67
|
includeRefs?: boolean;
|
|
@@ -48,4 +79,64 @@ export interface GetContentOptions {
|
|
|
48
79
|
* Defaults to `v3`.
|
|
49
80
|
*/
|
|
50
81
|
apiVersion?: 'v2' | 'v3';
|
|
82
|
+
/**
|
|
83
|
+
* Only include these fields.
|
|
84
|
+
* Note: 'omit' takes precedence over 'fields'
|
|
85
|
+
*
|
|
86
|
+
* @example
|
|
87
|
+
* ```
|
|
88
|
+
* fields: 'id, name, data.customField'
|
|
89
|
+
* ```
|
|
90
|
+
*/
|
|
91
|
+
fields?: string;
|
|
92
|
+
/**
|
|
93
|
+
* Omit only these fields.
|
|
94
|
+
* Note: 'omit' takes precedence over 'fields'
|
|
95
|
+
*
|
|
96
|
+
* @example
|
|
97
|
+
* ```
|
|
98
|
+
* omit: 'data.bigField,data.blocks'
|
|
99
|
+
* ```
|
|
100
|
+
*/
|
|
101
|
+
omit?: string;
|
|
102
|
+
/**
|
|
103
|
+
* Seconds to cache content. Sets the max-age of the cache-control header
|
|
104
|
+
* response header.
|
|
105
|
+
*
|
|
106
|
+
* Use a higher value for better performance, lower for content that will change more frequently
|
|
107
|
+
*
|
|
108
|
+
* @see {@link https://www.builder.io/c/docs/query-api#__next:~:text=%26includeRefs%3Dtrue-,cacheSeconds,-No}
|
|
109
|
+
*/
|
|
110
|
+
cacheSeconds?: number;
|
|
111
|
+
/**
|
|
112
|
+
* Builder.io uses stale-while-revalidate caching at the CDN level. This means we always serve
|
|
113
|
+
* from edge cache and update caches in the background for maximum possible performance. This also
|
|
114
|
+
* means that the more frequently content is requested, the more fresh it will be. The longest we
|
|
115
|
+
* will ever hold something in stale cache is 1 day by default, and you can set this to be shorter
|
|
116
|
+
* yourself as well. We suggest keeping this high unless you have content that must change rapidly
|
|
117
|
+
* and gets very little traffic.
|
|
118
|
+
*
|
|
119
|
+
* @see {@link https://www.fastly.com/blog/prevent-application-network-instability-serve-stale-content}
|
|
120
|
+
*/
|
|
121
|
+
staleCacheSeconds?: number;
|
|
122
|
+
/**
|
|
123
|
+
* Property to order results by.
|
|
124
|
+
* Use 1 for ascending and -1 for descending.
|
|
125
|
+
*
|
|
126
|
+
* The key is what you're sorting on, so the following example sorts by createdDate
|
|
127
|
+
* and because the value is 1, the sort is ascending.
|
|
128
|
+
*
|
|
129
|
+
* @example
|
|
130
|
+
* ```
|
|
131
|
+
* createdDate: 1
|
|
132
|
+
* ```
|
|
133
|
+
*/
|
|
134
|
+
sort?: {
|
|
135
|
+
[key: string]: 1 | -1;
|
|
136
|
+
};
|
|
137
|
+
/**
|
|
138
|
+
* Include content entries in a response that are still in
|
|
139
|
+
* draft mode and un-archived. Default is false.
|
|
140
|
+
*/
|
|
141
|
+
includeUnpublished?: boolean;
|
|
51
142
|
}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { isBrowser } from './is-browser.js';
|
|
2
|
+
import { isNodeRuntime } from './is-node-runtime.js';
|
|
2
3
|
/**
|
|
3
4
|
* Identifies edge runtimes (edge, workers, serverless, etc.)
|
|
4
5
|
*/
|
|
5
6
|
export function isEdgeRuntime() {
|
|
6
|
-
|
|
7
|
-
return !isBrowser() && !hasNode();
|
|
7
|
+
return !isBrowser() && !isNodeRuntime();
|
|
8
8
|
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@builder.io/sdk-svelte",
|
|
3
3
|
"description": "Builder.io SDK for Svelte",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.7.1-0",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"files": [
|
|
7
7
|
"lib",
|
|
@@ -37,6 +37,7 @@
|
|
|
37
37
|
},
|
|
38
38
|
"types": "./lib/browser/index.d.ts",
|
|
39
39
|
"scripts": {
|
|
40
|
+
"release": "npm publish",
|
|
40
41
|
"prepare": "SDK_ENV=browser svelte-kit sync",
|
|
41
42
|
"build": "nx run-many -p @builder.io/sdk-svelte -t build:node build:edge build:browser",
|
|
42
43
|
"build:node": "SDK_ENV=node svelte-package -i src/ -o lib/node",
|
|
@@ -82,6 +83,7 @@
|
|
|
82
83
|
}
|
|
83
84
|
},
|
|
84
85
|
"peerDependencies": {
|
|
86
|
+
"isolated-vm": "^4.6.0",
|
|
85
87
|
"svelte": "^4.1.2"
|
|
86
88
|
}
|
|
87
89
|
}
|
|
@@ -11,55 +11,52 @@
|
|
|
11
11
|
export let replaceNodes: CustomCodeProps["replaceNodes"] = undefined;
|
|
12
12
|
export let code: CustomCodeProps["code"];
|
|
13
13
|
|
|
14
|
-
|
|
15
|
-
// TODO: Move this function to standalone one in '@builder.io/utils'
|
|
16
|
-
if (elem && elem.getElementsByTagName && typeof window !== "undefined") {
|
|
17
|
-
const scripts = elem.getElementsByTagName("script");
|
|
18
|
-
for (let i = 0; i < scripts.length; i++) {
|
|
19
|
-
const script = scripts[i];
|
|
20
|
-
if (script.src) {
|
|
21
|
-
if (scriptsInserted.includes(script.src)) {
|
|
22
|
-
continue;
|
|
23
|
-
}
|
|
24
|
-
scriptsInserted.push(script.src);
|
|
25
|
-
const newScript = document.createElement("script");
|
|
26
|
-
newScript.async = true;
|
|
27
|
-
newScript.src = script.src;
|
|
28
|
-
document.head.appendChild(newScript);
|
|
29
|
-
} else if (
|
|
30
|
-
!script.type ||
|
|
31
|
-
[
|
|
32
|
-
"text/javascript",
|
|
33
|
-
"application/javascript",
|
|
34
|
-
"application/ecmascript",
|
|
35
|
-
].includes(script.type)
|
|
36
|
-
) {
|
|
37
|
-
if (scriptsRun.includes(script.innerText)) {
|
|
38
|
-
continue;
|
|
39
|
-
}
|
|
40
|
-
try {
|
|
41
|
-
scriptsRun.push(script.innerText);
|
|
42
|
-
new Function(script.innerText)();
|
|
43
|
-
} catch (error) {
|
|
44
|
-
console.warn("`CustomCode`: Error running script:", error);
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
let elem;
|
|
14
|
+
let elementRef;
|
|
52
15
|
|
|
53
16
|
let scriptsInserted = [];
|
|
54
17
|
let scriptsRun = [];
|
|
55
18
|
|
|
56
19
|
onMount(() => {
|
|
57
|
-
|
|
20
|
+
// TODO: Move this function to standalone one in '@builder.io/utils'
|
|
21
|
+
if (!elementRef?.getElementsByTagName || typeof window === "undefined") {
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
const scripts = elementRef.getElementsByTagName("script");
|
|
25
|
+
for (let i = 0; i < scripts.length; i++) {
|
|
26
|
+
const script = scripts[i];
|
|
27
|
+
if (script.src) {
|
|
28
|
+
if (scriptsInserted.includes(script.src)) {
|
|
29
|
+
continue;
|
|
30
|
+
}
|
|
31
|
+
scriptsInserted.push(script.src);
|
|
32
|
+
const newScript = document.createElement("script");
|
|
33
|
+
newScript.async = true;
|
|
34
|
+
newScript.src = script.src;
|
|
35
|
+
document.head.appendChild(newScript);
|
|
36
|
+
} else if (
|
|
37
|
+
!script.type ||
|
|
38
|
+
[
|
|
39
|
+
"text/javascript",
|
|
40
|
+
"application/javascript",
|
|
41
|
+
"application/ecmascript",
|
|
42
|
+
].includes(script.type)
|
|
43
|
+
) {
|
|
44
|
+
if (scriptsRun.includes(script.innerText)) {
|
|
45
|
+
continue;
|
|
46
|
+
}
|
|
47
|
+
try {
|
|
48
|
+
scriptsRun.push(script.innerText);
|
|
49
|
+
new Function(script.innerText)();
|
|
50
|
+
} catch (error) {
|
|
51
|
+
console.warn("`CustomCode`: Error running script:", error);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
58
55
|
});
|
|
59
56
|
</script>
|
|
60
57
|
|
|
61
58
|
<div
|
|
62
|
-
bind:this={
|
|
59
|
+
bind:this={elementRef}
|
|
63
60
|
class={"builder-custom-code" + (replaceNodes ? " replace-nodes" : "")}
|
|
64
61
|
>
|
|
65
62
|
{@html code}
|
|
@@ -24,33 +24,32 @@
|
|
|
24
24
|
registerInsertMenu,
|
|
25
25
|
setupBrowserForEditing,
|
|
26
26
|
} from "../../../scripts/init-editing.js";
|
|
27
|
-
import { checkIsDefined } from "../../../helpers/nullable.js";
|
|
28
27
|
import { getInteractionPropertiesForEvent } from "../../../functions/track/interaction.js";
|
|
29
28
|
import type {
|
|
30
29
|
ContentProps,
|
|
31
30
|
BuilderComponentStateChange,
|
|
32
31
|
} from "../content.types.js";
|
|
33
|
-
import { logger } from "../../../helpers/logger.js";
|
|
34
32
|
import type { ComponentInfo } from "../../../types/components.js";
|
|
35
33
|
import { fetchOneEntry } from "../../../functions/get-content/index.js";
|
|
36
34
|
import { isPreviewing } from "../../../functions/is-previewing.js";
|
|
37
35
|
import type { BuilderContent } from "../../../types/builder-content.js";
|
|
38
36
|
import { postPreviewContent } from "../../../helpers/preview-lru-cache/set.js";
|
|
39
37
|
import { fastClone } from "../../../functions/fast-clone.js";
|
|
38
|
+
import { logger } from "../../../helpers/logger.js";
|
|
39
|
+
import { getDefaultCanTrack } from "../../../helpers/canTrack.js";
|
|
40
40
|
import type { Writable } from "svelte/store";
|
|
41
41
|
|
|
42
|
-
export let canTrack: BuilderEditorProps["canTrack"] = undefined;
|
|
43
42
|
export let builderContextSignal: BuilderEditorProps["builderContextSignal"];
|
|
44
43
|
export let model: BuilderEditorProps["model"] = undefined;
|
|
45
44
|
export let context: BuilderEditorProps["context"] = undefined;
|
|
45
|
+
export let canTrack: BuilderEditorProps["canTrack"] = undefined;
|
|
46
46
|
export let apiKey: BuilderEditorProps["apiKey"];
|
|
47
|
-
export let showContent: BuilderEditorProps["showContent"];
|
|
48
|
-
export let classNameProp: BuilderEditorProps["classNameProp"];
|
|
49
|
-
|
|
50
|
-
export let content: BuilderEditorProps["content"] = undefined;
|
|
51
47
|
export let locale: BuilderEditorProps["locale"] = undefined;
|
|
52
48
|
export let includeRefs: BuilderEditorProps["includeRefs"] = undefined;
|
|
53
49
|
export let enrich: BuilderEditorProps["enrich"] = undefined;
|
|
50
|
+
export let content: BuilderEditorProps["content"] = undefined;
|
|
51
|
+
export let showContent: BuilderEditorProps["showContent"];
|
|
52
|
+
export let classNameProp: BuilderEditorProps["classNameProp"];
|
|
54
53
|
|
|
55
54
|
function mergeNewContent(newContent: BuilderContent) {
|
|
56
55
|
const newContentValue = {
|
|
@@ -127,7 +126,7 @@
|
|
|
127
126
|
const contentId = $builderContextSignal.content?.id;
|
|
128
127
|
_track({
|
|
129
128
|
type: "click",
|
|
130
|
-
canTrack:
|
|
129
|
+
canTrack: getDefaultCanTrack(canTrack),
|
|
131
130
|
contentId,
|
|
132
131
|
apiKey: apiKey,
|
|
133
132
|
variationId: variationId !== contentId ? variationId : undefined,
|
|
@@ -196,10 +195,75 @@
|
|
|
196
195
|
);
|
|
197
196
|
}
|
|
198
197
|
}
|
|
198
|
+
function elementRef_onIniteditingbldr(event) {
|
|
199
|
+
forceReRenderCount = forceReRenderCount + 1;
|
|
200
|
+
window.addEventListener("message", processMessage);
|
|
201
|
+
registerInsertMenu();
|
|
202
|
+
setupBrowserForEditing({
|
|
203
|
+
...(locale
|
|
204
|
+
? {
|
|
205
|
+
locale: locale,
|
|
206
|
+
}
|
|
207
|
+
: {}),
|
|
208
|
+
...(includeRefs
|
|
209
|
+
? {
|
|
210
|
+
includeRefs: includeRefs,
|
|
211
|
+
}
|
|
212
|
+
: {}),
|
|
213
|
+
...(enrich
|
|
214
|
+
? {
|
|
215
|
+
enrich: enrich,
|
|
216
|
+
}
|
|
217
|
+
: {}),
|
|
218
|
+
});
|
|
219
|
+
Object.values<ComponentInfo>($builderContextSignal.componentInfos).forEach(
|
|
220
|
+
(registeredComponent) => {
|
|
221
|
+
const message = createRegisterComponentMessage(registeredComponent);
|
|
222
|
+
window.parent?.postMessage(message, "*");
|
|
223
|
+
}
|
|
224
|
+
);
|
|
225
|
+
window.addEventListener(
|
|
226
|
+
"builder:component:stateChangeListenerActivated",
|
|
227
|
+
emitStateUpdate
|
|
228
|
+
);
|
|
229
|
+
}
|
|
230
|
+
function elementRef_onInitpreviewingbldr(event) {
|
|
231
|
+
const searchParams = new URL(location.href).searchParams;
|
|
232
|
+
const searchParamPreviewModel = searchParams.get("builder.preview");
|
|
233
|
+
const searchParamPreviewId = searchParams.get(
|
|
234
|
+
`builder.preview.${searchParamPreviewModel}`
|
|
235
|
+
);
|
|
236
|
+
const previewApiKey =
|
|
237
|
+
searchParams.get("apiKey") || searchParams.get("builder.space");
|
|
238
|
+
|
|
239
|
+
/**
|
|
240
|
+
* Make sure that:
|
|
241
|
+
* - the preview model name is the same as the one we're rendering, since there can be multiple models rendered
|
|
242
|
+
* at the same time, e.g. header/page/footer.
|
|
243
|
+
* - the API key is the same, since we don't want to preview content from other organizations.
|
|
244
|
+
* - if there is content, that the preview ID is the same as that of the one we receive.
|
|
245
|
+
*
|
|
246
|
+
* TO-DO: should we only update the state when there is a change?
|
|
247
|
+
**/
|
|
248
|
+
if (
|
|
249
|
+
searchParamPreviewModel === model &&
|
|
250
|
+
previewApiKey === apiKey &&
|
|
251
|
+
(!content || searchParamPreviewId === content.id)
|
|
252
|
+
) {
|
|
253
|
+
fetchOneEntry({
|
|
254
|
+
model: model,
|
|
255
|
+
apiKey: apiKey,
|
|
256
|
+
apiVersion: $builderContextSignal.apiVersion,
|
|
257
|
+
}).then((content) => {
|
|
258
|
+
if (content) {
|
|
259
|
+
mergeNewContent(content);
|
|
260
|
+
}
|
|
261
|
+
});
|
|
262
|
+
}
|
|
263
|
+
}
|
|
199
264
|
|
|
200
265
|
let elementRef;
|
|
201
266
|
|
|
202
|
-
let canTrackToUse = checkIsDefined(canTrack) ? canTrack : true;
|
|
203
267
|
let forceReRenderCount = 0;
|
|
204
268
|
let lastUpdated = 0;
|
|
205
269
|
let shouldSendResetCookie = false;
|
|
@@ -207,95 +271,41 @@
|
|
|
207
271
|
let clicked = false;
|
|
208
272
|
|
|
209
273
|
onMount(() => {
|
|
210
|
-
if (!apiKey) {
|
|
211
|
-
logger.error(
|
|
212
|
-
"No API key provided to `RenderContent` component. This can cause issues. Please provide an API key using the `apiKey` prop."
|
|
213
|
-
);
|
|
214
|
-
}
|
|
215
274
|
if (isBrowser()) {
|
|
216
|
-
if (isEditing()) {
|
|
217
|
-
|
|
218
|
-
window.addEventListener("message", processMessage);
|
|
219
|
-
registerInsertMenu();
|
|
220
|
-
setupBrowserForEditing({
|
|
221
|
-
...(locale
|
|
222
|
-
? {
|
|
223
|
-
locale: locale,
|
|
224
|
-
}
|
|
225
|
-
: {}),
|
|
226
|
-
...(includeRefs
|
|
227
|
-
? {
|
|
228
|
-
includeRefs: includeRefs,
|
|
229
|
-
}
|
|
230
|
-
: {}),
|
|
231
|
-
...(enrich
|
|
232
|
-
? {
|
|
233
|
-
enrich: enrich,
|
|
234
|
-
}
|
|
235
|
-
: {}),
|
|
236
|
-
});
|
|
237
|
-
Object.values<ComponentInfo>(
|
|
238
|
-
$builderContextSignal.componentInfos
|
|
239
|
-
).forEach((registeredComponent) => {
|
|
240
|
-
const message = createRegisterComponentMessage(registeredComponent);
|
|
241
|
-
window.parent?.postMessage(message, "*");
|
|
242
|
-
});
|
|
243
|
-
window.addEventListener(
|
|
244
|
-
"builder:component:stateChangeListenerActivated",
|
|
245
|
-
emitStateUpdate
|
|
246
|
-
);
|
|
275
|
+
if (isEditing() && elementRef) {
|
|
276
|
+
elementRef.dispatchEvent(new CustomEvent("initeditingbldr"));
|
|
247
277
|
}
|
|
248
|
-
|
|
278
|
+
const shouldTrackImpression =
|
|
279
|
+
$builderContextSignal.content && getDefaultCanTrack(canTrack);
|
|
280
|
+
if (shouldTrackImpression) {
|
|
249
281
|
const variationId = $builderContextSignal.content?.testVariationId;
|
|
250
282
|
const contentId = $builderContextSignal.content?.id;
|
|
283
|
+
const apiKeyProp = apiKey;
|
|
251
284
|
_track({
|
|
252
285
|
type: "impression",
|
|
253
|
-
canTrack:
|
|
286
|
+
canTrack: true,
|
|
254
287
|
contentId,
|
|
255
|
-
apiKey:
|
|
288
|
+
apiKey: apiKeyProp!,
|
|
256
289
|
variationId: variationId !== contentId ? variationId : undefined,
|
|
257
290
|
});
|
|
258
291
|
}
|
|
259
|
-
// override normal content in preview mode
|
|
260
|
-
if (isPreviewing()) {
|
|
261
|
-
const searchParams = new URL(location.href).searchParams;
|
|
262
|
-
const searchParamPreviewModel = searchParams.get("builder.preview");
|
|
263
|
-
const searchParamPreviewId = searchParams.get(
|
|
264
|
-
`builder.preview.${searchParamPreviewModel}`
|
|
265
|
-
);
|
|
266
|
-
const previewApiKey =
|
|
267
|
-
searchParams.get("apiKey") || searchParams.get("builder.space");
|
|
268
292
|
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
* at the same time, e.g. header/page/footer.
|
|
273
|
-
* - the API key is the same, since we don't want to preview content from other organizations.
|
|
274
|
-
* - if there is content, that the preview ID is the same as that of the one we receive.
|
|
275
|
-
*
|
|
276
|
-
* TO-DO: should we only update the state when there is a change?
|
|
277
|
-
**/
|
|
278
|
-
if (
|
|
279
|
-
searchParamPreviewModel === model &&
|
|
280
|
-
previewApiKey === apiKey &&
|
|
281
|
-
(!content || searchParamPreviewId === content.id)
|
|
282
|
-
) {
|
|
283
|
-
fetchOneEntry({
|
|
284
|
-
model: model,
|
|
285
|
-
apiKey: apiKey,
|
|
286
|
-
apiVersion: $builderContextSignal.apiVersion,
|
|
287
|
-
}).then((content) => {
|
|
288
|
-
if (content) {
|
|
289
|
-
mergeNewContent(content);
|
|
290
|
-
}
|
|
291
|
-
});
|
|
292
|
-
}
|
|
293
|
+
// override normal content in preview mode
|
|
294
|
+
if (isPreviewing() && elementRef) {
|
|
295
|
+
elementRef.dispatchEvent(new CustomEvent("initpreviewingbldr"));
|
|
293
296
|
}
|
|
294
|
-
evaluateJsCode();
|
|
295
|
-
runHttpRequests();
|
|
296
|
-
emitStateUpdate();
|
|
297
297
|
}
|
|
298
298
|
});
|
|
299
|
+
onMount(() => {
|
|
300
|
+
if (!apiKey) {
|
|
301
|
+
logger.error(
|
|
302
|
+
"No API key provided to `RenderContent` component. This can cause issues. Please provide an API key using the `apiKey` prop."
|
|
303
|
+
);
|
|
304
|
+
}
|
|
305
|
+
evaluateJsCode();
|
|
306
|
+
runHttpRequests();
|
|
307
|
+
emitStateUpdate();
|
|
308
|
+
});
|
|
299
309
|
|
|
300
310
|
function onUpdateFn_0(..._args: any[]) {
|
|
301
311
|
if (content) {
|
|
@@ -338,6 +348,7 @@
|
|
|
338
348
|
|
|
339
349
|
{#if $builderContextSignal.content}
|
|
340
350
|
<div
|
|
351
|
+
{...{}}
|
|
341
352
|
key={forceReRenderCount}
|
|
342
353
|
bind:this={elementRef}
|
|
343
354
|
on:click={(event) => {
|
|
@@ -353,6 +364,12 @@
|
|
|
353
364
|
"aria-hidden": true,
|
|
354
365
|
}}
|
|
355
366
|
class={classNameProp}
|
|
367
|
+
on:initeditingbldr={(event) => {
|
|
368
|
+
elementRef_onIniteditingbldr(event);
|
|
369
|
+
}}
|
|
370
|
+
on:initpreviewingbldr={(event) => {
|
|
371
|
+
elementRef_onInitpreviewingbldr(event);
|
|
372
|
+
}}
|
|
356
373
|
>
|
|
357
374
|
<slot />
|
|
358
375
|
</div>
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export const SDK_VERSION = "0.
|
|
1
|
+
export const SDK_VERSION = "0.7.1-0"
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { runInBrowser } from './browser-runtime/browser.js';
|
|
2
|
+
import type { ExecutorArgs } from './helpers.js';
|
|
3
|
+
import { isBrowser } from '../is-browser.js';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* THIS IS A MAGICAL IMPORT. It is aliased by the build process of every SDK configuration, so that
|
|
7
|
+
* it points to the correct runtime for that configuration, which are expected to live exactly at:
|
|
8
|
+
* - ./browser-runtime/index.js
|
|
9
|
+
* - ./node-runtime/index.js
|
|
10
|
+
* - ./edge-runtime/index.js
|
|
11
|
+
*
|
|
12
|
+
* We have code in `/output-generation` that does this aliasing, and is re-used by each SDK.
|
|
13
|
+
* Also, each individual `tsconfig.json` aliases this import to the browser runtime so that the
|
|
14
|
+
* types can be resolved correctly.
|
|
15
|
+
*/
|
|
16
|
+
import { evaluator } from 'placeholder-runtime';
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Even though we have separate runtimes for browser/node/edge, sometimes frameworks will
|
|
20
|
+
* end up sending the server runtime code to the browser (most notably in dev mode).
|
|
21
|
+
*/
|
|
22
|
+
export const chooseBrowserOrServerEval = (args: ExecutorArgs) => isBrowser() ? runInBrowser(args) : evaluator(args)
|
|
@@ -1,25 +1,7 @@
|
|
|
1
1
|
import { logger } from '../../helpers/logger.js';
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import
|
|
5
|
-
import { getUserAttributes } from '../track/helpers.js';
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* THIS IS A MAGICAL IMPORT. It is aliased by the build process of every SDK configuration, so that
|
|
9
|
-
* it points to the correct runtime for that configuration, which are expected to live exactly at:
|
|
10
|
-
* - ./browser-runtime/index.js
|
|
11
|
-
* - ./node-runtime/index.js
|
|
12
|
-
* - ./edge-runtime/index.js
|
|
13
|
-
*
|
|
14
|
-
* We have code in `/output-generation` that does this aliasing, and is re-used by each SDK.
|
|
15
|
-
* Also, each individual `tsconfig.json` aliases this import to the browser runtime so that the
|
|
16
|
-
* types can be resolved correctly.
|
|
17
|
-
*/
|
|
18
|
-
import { evaluator } from 'placeholder-runtime';
|
|
19
|
-
export type EvaluatorArgs = Omit<ExecutorArgs, 'builder' | 'event'> & {
|
|
20
|
-
event?: Event;
|
|
21
|
-
isExpression?: boolean;
|
|
22
|
-
};
|
|
2
|
+
import type { EvaluatorArgs, ExecutorArgs } from './helpers.js';
|
|
3
|
+
import { getBuilderGlobals, parseCode } from './helpers.js';
|
|
4
|
+
import { chooseBrowserOrServerEval } from './choose-eval.js';
|
|
23
5
|
export function evaluate({
|
|
24
6
|
code,
|
|
25
7
|
context,
|
|
@@ -33,22 +15,11 @@ export function evaluate({
|
|
|
33
15
|
logger.warn('Skipping evaluation of empty code block.');
|
|
34
16
|
return;
|
|
35
17
|
}
|
|
36
|
-
const builder: BuilderGlobals = {
|
|
37
|
-
isEditing: isEditing(),
|
|
38
|
-
isBrowser: isBrowser(),
|
|
39
|
-
isServer: !isBrowser(),
|
|
40
|
-
getUserAttributes: () => getUserAttributes()
|
|
41
|
-
};
|
|
42
|
-
|
|
43
|
-
// Be able to handle simple expressions like "state.foo" or "1 + 1"
|
|
44
|
-
// as well as full blocks like "var foo = "bar"; return foo"
|
|
45
|
-
const useReturn =
|
|
46
|
-
// we disable this for cases where we definitely don't want a return
|
|
47
|
-
isExpression && !(code.includes(';') || code.includes(' return ') || code.trim().startsWith('return '));
|
|
48
|
-
const useCode = useReturn ? `return (${code});` : code;
|
|
49
18
|
const args: ExecutorArgs = {
|
|
50
|
-
code:
|
|
51
|
-
|
|
19
|
+
code: parseCode(code, {
|
|
20
|
+
isExpression
|
|
21
|
+
}),
|
|
22
|
+
builder: getBuilderGlobals(),
|
|
52
23
|
context,
|
|
53
24
|
event,
|
|
54
25
|
rootSetState,
|
|
@@ -56,7 +27,7 @@ export function evaluate({
|
|
|
56
27
|
localState
|
|
57
28
|
};
|
|
58
29
|
try {
|
|
59
|
-
return
|
|
30
|
+
return chooseBrowserOrServerEval(args);
|
|
60
31
|
} catch (e: any) {
|
|
61
32
|
logger.error('Failed code evaluation: ' + e.message, {
|
|
62
33
|
code
|
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
import type { BuilderContextInterface, BuilderRenderState } from '../../context/types.js';
|
|
2
|
-
import
|
|
2
|
+
import { isBrowser } from '../is-browser.js';
|
|
3
|
+
import { isEditing } from '../is-editing.js';
|
|
4
|
+
import { getUserAttributes } from '../track/helpers.js';
|
|
5
|
+
export type EvaluatorArgs = Omit<ExecutorArgs, 'builder' | 'event'> & {
|
|
6
|
+
event?: Event;
|
|
7
|
+
isExpression?: boolean;
|
|
8
|
+
};
|
|
3
9
|
export type BuilderGlobals = {
|
|
4
10
|
isEditing: boolean | undefined;
|
|
5
11
|
isBrowser: boolean | undefined;
|
|
@@ -29,4 +35,21 @@ export const getFunctionArguments = ({
|
|
|
29
35
|
context,
|
|
30
36
|
event
|
|
31
37
|
});
|
|
38
|
+
};
|
|
39
|
+
export const getBuilderGlobals = (): BuilderGlobals => ({
|
|
40
|
+
isEditing: isEditing(),
|
|
41
|
+
isBrowser: isBrowser(),
|
|
42
|
+
isServer: !isBrowser(),
|
|
43
|
+
getUserAttributes: () => getUserAttributes()
|
|
44
|
+
});
|
|
45
|
+
export const parseCode = (code: string, {
|
|
46
|
+
isExpression = true
|
|
47
|
+
}: Pick<EvaluatorArgs, 'isExpression'>) => {
|
|
48
|
+
// Be able to handle simple expressions like "state.foo" or "1 + 1"
|
|
49
|
+
// as well as full blocks like "var foo = "bar"; return foo"
|
|
50
|
+
const useReturn =
|
|
51
|
+
// we disable this for cases where we definitely don't want a return
|
|
52
|
+
isExpression && !(code.includes(';') || code.includes(' return ') || code.trim().startsWith('return '));
|
|
53
|
+
const useCode = useReturn ? `return (${code});` : code;
|
|
54
|
+
return useCode;
|
|
32
55
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export { evaluator } from '
|
|
1
|
+
export { runInNode as evaluator } from './node-runtime.js'
|