@builder.io/sdk-solid 0.0.8-21 → 0.0.8-24
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/package.json +1 -1
- package/src/blocks/button/button.jsx +6 -1
- package/src/blocks/columns/columns.jsx +44 -47
- package/src/blocks/columns/component-info.js +3 -2
- package/src/blocks/custom-code/custom-code.jsx +34 -37
- package/src/blocks/embed/component-info.js +3 -2
- package/src/blocks/embed/embed.jsx +28 -31
- package/src/blocks/form/form.jsx +172 -173
- package/src/blocks/image/component-info.js +3 -2
- package/src/blocks/image/image.jsx +26 -29
- package/src/blocks/img/img.jsx +1 -1
- package/src/blocks/symbol/symbol.jsx +10 -13
- package/src/blocks/util.js +7 -0
- package/src/blocks/video/video.jsx +19 -23
- package/src/components/render-block/block-styles.jsx +22 -27
- package/src/components/render-block/render-block.jsx +155 -159
- package/src/components/render-block/render-component.jsx +2 -2
- package/src/components/render-block/render-repeated-block.jsx +1 -1
- package/src/components/render-blocks.jsx +32 -33
- package/src/components/render-content/components/render-styles.jsx +38 -41
- package/src/components/render-content/render-content.jsx +170 -156
- package/src/components/render-inlined-styles.jsx +10 -13
- package/src/constants/builder-registered-components.js +3 -0
- package/src/functions/get-fetch.js +2 -2
- package/src/functions/get-processed-block.js +10 -6
- package/src/functions/get-processed-block.test.js +1 -1
- package/src/functions/track.js +71 -2
- package/src/helpers/cookie.js +59 -0
- package/src/helpers/localStorage.js +34 -0
- package/src/helpers/nullable.js +4 -0
- package/src/helpers/sessionId.js +26 -0
- package/src/helpers/time.js +5 -0
- package/src/helpers/url.js +10 -0
- package/src/helpers/url.test.js +15 -0
- package/src/helpers/uuid.js +13 -0
- package/src/helpers/visitorId.js +33 -0
- package/src/scripts/init-editing.js +4 -5
- package/src/types/can-track.js +0 -0
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import { Show, onMount, on, createEffect } from "solid-js";
|
|
1
|
+
import { Show, onMount, on, createEffect, createSignal } from "solid-js";
|
|
2
2
|
import { Dynamic } from "solid-js/web";
|
|
3
|
-
import { createMutable } from "solid-js/store";
|
|
4
3
|
import { getDefaultRegisteredComponents } from "../../constants/builder-registered-components.js";
|
|
5
4
|
import { TARGET } from "../../constants/target.js";
|
|
6
5
|
import BuilderContext from "../../context/builder.context";
|
|
@@ -14,172 +13,187 @@ import { components, createRegisterComponentMessage } from "../../functions/regi
|
|
|
14
13
|
import { track } from "../../functions/track.js";
|
|
15
14
|
import RenderBlocks from "../render-blocks.jsx";
|
|
16
15
|
import RenderContentStyles from "./components/render-styles.jsx";
|
|
16
|
+
import { registerInsertMenu, setupBrowserForEditing } from "../../scripts/init-editing.js";
|
|
17
17
|
|
|
18
18
|
function RenderContent(props) {
|
|
19
|
-
const
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
return mergedContent;
|
|
29
|
-
},
|
|
30
|
-
|
|
31
|
-
overrideContent: null,
|
|
32
|
-
update: 0,
|
|
33
|
-
overrideState: {},
|
|
34
|
-
|
|
35
|
-
get contentState() {
|
|
36
|
-
return { ...props.content?.data?.state,
|
|
19
|
+
const [forceReRenderCount, setForceReRenderCount] = createSignal(0);
|
|
20
|
+
const [overrideContent, setOverrideContent] = createSignal(null);
|
|
21
|
+
const [update, setUpdate] = createSignal(0);
|
|
22
|
+
const [overrideState, setOverrideState] = createSignal({});
|
|
23
|
+
|
|
24
|
+
function useContent() {
|
|
25
|
+
const mergedContent = { ...props.content,
|
|
26
|
+
...overrideContent(),
|
|
27
|
+
data: { ...props.content?.data,
|
|
37
28
|
...props.data,
|
|
38
|
-
...
|
|
39
|
-
}
|
|
40
|
-
}
|
|
29
|
+
...overrideContent()?.data
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
return mergedContent;
|
|
33
|
+
}
|
|
41
34
|
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
35
|
+
function canTrackToUse() {
|
|
36
|
+
return props.canTrack || true;
|
|
37
|
+
}
|
|
45
38
|
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
...components, ...(props.customComponents || [])];
|
|
53
|
-
const allComponents = allComponentsArray.reduce((acc, curr) => ({ ...acc,
|
|
54
|
-
[curr.name]: curr
|
|
55
|
-
}), {});
|
|
56
|
-
return allComponents;
|
|
57
|
-
},
|
|
39
|
+
function contentState() {
|
|
40
|
+
return { ...props.content?.data?.state,
|
|
41
|
+
...props.data,
|
|
42
|
+
...overrideState()
|
|
43
|
+
};
|
|
44
|
+
}
|
|
58
45
|
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
} = event;
|
|
63
|
-
|
|
64
|
-
if (data) {
|
|
65
|
-
switch (data.type) {
|
|
66
|
-
case "builder.contentUpdate":
|
|
67
|
-
{
|
|
68
|
-
const messageContent = data.data;
|
|
69
|
-
const key = messageContent.key || messageContent.alias || messageContent.entry || messageContent.modelName;
|
|
70
|
-
const contentData = messageContent.data;
|
|
71
|
-
|
|
72
|
-
if (key === props.model) {
|
|
73
|
-
state.overrideContent = contentData;
|
|
74
|
-
}
|
|
46
|
+
function contextContext() {
|
|
47
|
+
return props.context || {};
|
|
48
|
+
}
|
|
75
49
|
|
|
76
|
-
|
|
77
|
-
|
|
50
|
+
function allRegisteredComponents() {
|
|
51
|
+
const allComponentsArray = [...getDefaultRegisteredComponents(), // While this `components` object is deprecated, we must maintain support for it.
|
|
52
|
+
// Since users are able to override our default components, we need to make sure that we do not break such
|
|
53
|
+
// existing usage.
|
|
54
|
+
// This is why we spread `components` after the default Builder.io components, but before the `props.customComponents`,
|
|
55
|
+
// which is the new standard way of providing custom components, and must therefore take precedence.
|
|
56
|
+
...components, ...(props.customComponents || [])];
|
|
57
|
+
const allComponents = allComponentsArray.reduce((acc, curr) => ({ ...acc,
|
|
58
|
+
[curr.name]: curr
|
|
59
|
+
}), {});
|
|
60
|
+
return allComponents;
|
|
61
|
+
}
|
|
78
62
|
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
63
|
+
function processMessage(event) {
|
|
64
|
+
const {
|
|
65
|
+
data
|
|
66
|
+
} = event;
|
|
67
|
+
|
|
68
|
+
if (data) {
|
|
69
|
+
switch (data.type) {
|
|
70
|
+
case "builder.contentUpdate":
|
|
71
|
+
{
|
|
72
|
+
const messageContent = data.data;
|
|
73
|
+
const key = messageContent.key || messageContent.alias || messageContent.entry || messageContent.modelName;
|
|
74
|
+
const contentData = messageContent.data;
|
|
75
|
+
|
|
76
|
+
if (key === props.model) {
|
|
77
|
+
setOverrideContent(contentData);
|
|
83
78
|
}
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
},
|
|
87
79
|
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
const jsCode = state.useContent?.data?.jsCode;
|
|
80
|
+
break;
|
|
81
|
+
}
|
|
91
82
|
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
});
|
|
83
|
+
case "builder.patchUpdates":
|
|
84
|
+
{
|
|
85
|
+
// TODO
|
|
86
|
+
break;
|
|
87
|
+
}
|
|
98
88
|
}
|
|
99
|
-
}
|
|
89
|
+
}
|
|
90
|
+
}
|
|
100
91
|
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
92
|
+
function evaluateJsCode() {
|
|
93
|
+
// run any dynamic JS code attached to content
|
|
94
|
+
const jsCode = useContent()?.data?.jsCode;
|
|
104
95
|
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
}
|
|
111
|
-
}
|
|
96
|
+
if (jsCode) {
|
|
97
|
+
evaluate({
|
|
98
|
+
code: jsCode,
|
|
99
|
+
context: contextContext(),
|
|
100
|
+
state: contentState()
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
}
|
|
112
104
|
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
context: state.contextContext,
|
|
117
|
-
state: state.contentState
|
|
118
|
-
}));
|
|
119
|
-
},
|
|
105
|
+
function httpReqsData() {
|
|
106
|
+
return {};
|
|
107
|
+
}
|
|
120
108
|
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
};
|
|
132
|
-
state.overrideState = newOverrideState;
|
|
133
|
-
};
|
|
109
|
+
function onClick(_event) {
|
|
110
|
+
if (useContent()) {
|
|
111
|
+
track({
|
|
112
|
+
type: "click",
|
|
113
|
+
canTrack: canTrackToUse(),
|
|
114
|
+
contentId: useContent().id,
|
|
115
|
+
orgId: props.apiKey
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
}
|
|
134
119
|
|
|
135
|
-
|
|
136
|
-
},
|
|
120
|
+
function evalExpression(expression) {
|
|
121
|
+
return expression.replace(/{{([^}]+)}}/g, (_match, group) => evaluate({
|
|
122
|
+
code: group,
|
|
123
|
+
context: contextContext(),
|
|
124
|
+
state: contentState()
|
|
125
|
+
}));
|
|
126
|
+
}
|
|
137
127
|
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
}
|
|
149
|
-
|
|
128
|
+
function handleRequest({
|
|
129
|
+
url,
|
|
130
|
+
key
|
|
131
|
+
}) {
|
|
132
|
+
const fetchAndSetState = async () => {
|
|
133
|
+
const fetch = await getFetch();
|
|
134
|
+
const response = await fetch(url);
|
|
135
|
+
const json = await response.json();
|
|
136
|
+
const newOverrideState = { ...overrideState(),
|
|
137
|
+
[key]: json
|
|
138
|
+
};
|
|
139
|
+
setOverrideState(newOverrideState);
|
|
140
|
+
};
|
|
150
141
|
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
142
|
+
fetchAndSetState();
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
function runHttpRequests() {
|
|
146
|
+
const requests = useContent()?.data?.httpRequests ?? {};
|
|
147
|
+
Object.entries(requests).forEach(([key, url]) => {
|
|
148
|
+
if (url && (!httpReqsData()[key] || isEditing())) {
|
|
149
|
+
const evaluatedUrl = evalExpression(url);
|
|
150
|
+
handleRequest({
|
|
151
|
+
url: evaluatedUrl,
|
|
152
|
+
key
|
|
153
|
+
});
|
|
161
154
|
}
|
|
162
|
-
}
|
|
155
|
+
});
|
|
156
|
+
}
|
|
163
157
|
|
|
164
|
-
|
|
165
|
-
|
|
158
|
+
function emitStateUpdate() {
|
|
159
|
+
if (isEditing()) {
|
|
160
|
+
window.dispatchEvent(new CustomEvent("builder:component:stateChange", {
|
|
161
|
+
detail: {
|
|
162
|
+
state: contentState(),
|
|
163
|
+
ref: {
|
|
164
|
+
name: props.model
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
}));
|
|
166
168
|
}
|
|
169
|
+
}
|
|
167
170
|
|
|
168
|
-
|
|
171
|
+
function shouldRenderContentStyles() {
|
|
172
|
+
return Boolean((useContent()?.data?.cssCode || useContent()?.data?.customFonts?.length) && TARGET !== "reactNative");
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
let elementRef;
|
|
169
176
|
onMount(() => {
|
|
170
177
|
if (isBrowser()) {
|
|
171
178
|
if (isEditing()) {
|
|
172
|
-
|
|
179
|
+
setForceReRenderCount(forceReRenderCount() + 1); // QWIK-REPLACE: _useMutableProps
|
|
180
|
+
|
|
181
|
+
registerInsertMenu();
|
|
182
|
+
setupBrowserForEditing();
|
|
183
|
+
Object.values(allRegisteredComponents()).forEach(registeredComponent => {
|
|
173
184
|
const message = createRegisterComponentMessage(registeredComponent);
|
|
174
185
|
window.parent?.postMessage(message, "*");
|
|
175
186
|
});
|
|
176
|
-
window.addEventListener("message",
|
|
177
|
-
window.addEventListener("builder:component:stateChangeListenerActivated",
|
|
187
|
+
window.addEventListener("message", processMessage);
|
|
188
|
+
window.addEventListener("builder:component:stateChangeListenerActivated", emitStateUpdate);
|
|
178
189
|
}
|
|
179
190
|
|
|
180
|
-
if (
|
|
181
|
-
track(
|
|
182
|
-
|
|
191
|
+
if (useContent()) {
|
|
192
|
+
track({
|
|
193
|
+
type: "impression",
|
|
194
|
+
canTrack: canTrackToUse(),
|
|
195
|
+
contentId: useContent().id,
|
|
196
|
+
orgId: props.apiKey
|
|
183
197
|
});
|
|
184
198
|
} // override normal content in preview mode
|
|
185
199
|
|
|
@@ -196,47 +210,47 @@ function RenderContent(props) {
|
|
|
196
210
|
apiKey: previewApiKey
|
|
197
211
|
}).then(content => {
|
|
198
212
|
if (content) {
|
|
199
|
-
|
|
213
|
+
setOverrideContent(content);
|
|
200
214
|
}
|
|
201
215
|
});
|
|
202
216
|
}
|
|
203
217
|
}
|
|
204
218
|
}
|
|
205
219
|
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
220
|
+
evaluateJsCode();
|
|
221
|
+
runHttpRequests();
|
|
222
|
+
emitStateUpdate();
|
|
209
223
|
}
|
|
210
224
|
});
|
|
211
225
|
|
|
212
226
|
function onUpdateFn_0() {
|
|
213
|
-
|
|
227
|
+
evaluateJsCode();
|
|
214
228
|
}
|
|
215
229
|
|
|
216
|
-
createEffect(on(() => [
|
|
230
|
+
createEffect(on(() => [useContent()?.data?.jsCode], onUpdateFn_0));
|
|
217
231
|
|
|
218
232
|
function onUpdateFn_1() {
|
|
219
|
-
|
|
233
|
+
runHttpRequests();
|
|
220
234
|
}
|
|
221
235
|
|
|
222
|
-
createEffect(on(() => [
|
|
236
|
+
createEffect(on(() => [useContent()?.data?.httpRequests], onUpdateFn_1));
|
|
223
237
|
|
|
224
238
|
function onUpdateFn_2() {
|
|
225
|
-
|
|
239
|
+
emitStateUpdate();
|
|
226
240
|
}
|
|
227
241
|
|
|
228
|
-
createEffect(on(() => [
|
|
242
|
+
createEffect(on(() => [contentState()], onUpdateFn_2));
|
|
229
243
|
return <Dynamic value={{
|
|
230
244
|
get content() {
|
|
231
|
-
return
|
|
245
|
+
return useContent();
|
|
232
246
|
},
|
|
233
247
|
|
|
234
248
|
get state() {
|
|
235
|
-
return
|
|
249
|
+
return contentState();
|
|
236
250
|
},
|
|
237
251
|
|
|
238
252
|
get context() {
|
|
239
|
-
return
|
|
253
|
+
return contextContext();
|
|
240
254
|
},
|
|
241
255
|
|
|
242
256
|
get apiKey() {
|
|
@@ -244,16 +258,16 @@ function RenderContent(props) {
|
|
|
244
258
|
},
|
|
245
259
|
|
|
246
260
|
get registeredComponents() {
|
|
247
|
-
return
|
|
261
|
+
return allRegisteredComponents();
|
|
248
262
|
}
|
|
249
263
|
|
|
250
264
|
}} component={BuilderContext.Provider}>
|
|
251
|
-
<Show when={
|
|
252
|
-
<div onClick={event =>
|
|
253
|
-
<Show when={
|
|
254
|
-
<RenderContentStyles cssCode={
|
|
265
|
+
<Show when={useContent()}>
|
|
266
|
+
<div ref={elementRef} onClick={event => onClick(event)} builder-content-id={useContent()?.id}>
|
|
267
|
+
<Show when={shouldRenderContentStyles()}>
|
|
268
|
+
<RenderContentStyles cssCode={useContent()?.data?.cssCode} customFonts={useContent()?.data?.customFonts}></RenderContentStyles>
|
|
255
269
|
</Show>
|
|
256
|
-
<RenderBlocks blocks={
|
|
270
|
+
<RenderBlocks blocks={useContent()?.data?.blocks} key={forceReRenderCount()}></RenderBlocks>
|
|
257
271
|
</div>
|
|
258
272
|
</Show>
|
|
259
273
|
</Dynamic>;
|
|
@@ -1,23 +1,20 @@
|
|
|
1
1
|
import { Show } from "solid-js";
|
|
2
2
|
import { Dynamic } from "solid-js/web";
|
|
3
|
-
import { createMutable } from "solid-js/store";
|
|
4
3
|
import { TARGET } from "../constants/target.js";
|
|
5
4
|
|
|
6
5
|
function RenderInlinedStyles(props) {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
},
|
|
6
|
+
function injectedStyleScript() {
|
|
7
|
+
return `<${tagName()}>${props.styles}</${tagName()}>`;
|
|
8
|
+
}
|
|
11
9
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
10
|
+
function tagName() {
|
|
11
|
+
// NOTE: we have to obfusctate the name of the tag due to a limitation in the svelte-preprocessor plugin.
|
|
12
|
+
// https://github.com/sveltejs/vite-plugin-svelte/issues/315#issuecomment-1109000027
|
|
13
|
+
return "sty" + "le";
|
|
14
|
+
}
|
|
17
15
|
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
<div innerHTML={state.injectedStyleScript}></div>
|
|
16
|
+
return <Show fallback={<Dynamic component={tagName()}>{props.styles}</Dynamic>} when={TARGET === "svelte"}>
|
|
17
|
+
<div innerHTML={injectedStyleScript()}></div>
|
|
21
18
|
</Show>;
|
|
22
19
|
}
|
|
23
20
|
|
|
@@ -32,9 +32,12 @@ import { componentInfo as videoComponentInfo } from "../blocks/video/component-i
|
|
|
32
32
|
import { default as Video } from "../blocks/video/video.jsx";
|
|
33
33
|
import { componentInfo as embedComponentInfo } from "../blocks/embed/component-info";
|
|
34
34
|
import { default as embed } from "../blocks/embed/embed.jsx";
|
|
35
|
+
import { default as Img } from "../blocks/img/img.jsx";
|
|
36
|
+
import { componentInfo as imgComponentInfo } from "../blocks/img/component-info";
|
|
35
37
|
const getDefaultRegisteredComponents = () => [
|
|
36
38
|
__spreadValues({ component: Columns }, columnsComponentInfo),
|
|
37
39
|
__spreadValues({ component: Image }, imageComponentInfo),
|
|
40
|
+
__spreadValues({ component: Img }, imgComponentInfo),
|
|
38
41
|
__spreadValues({ component: Text }, textComponentInfo),
|
|
39
42
|
__spreadValues({ component: Video }, videoComponentInfo),
|
|
40
43
|
__spreadValues({ component: Symbol }, symbolComponentInfo),
|
|
@@ -24,9 +24,9 @@ function getFetch() {
|
|
|
24
24
|
const globalFetch = getGlobalThis().fetch;
|
|
25
25
|
if (typeof globalFetch === "undefined" && typeof global !== "undefined") {
|
|
26
26
|
const nodeFetch = import("node-fetch").then((d) => d.default);
|
|
27
|
-
return nodeFetch;
|
|
27
|
+
return nodeFetch.default || nodeFetch;
|
|
28
28
|
}
|
|
29
|
-
return globalFetch;
|
|
29
|
+
return globalFetch.default || globalFetch;
|
|
30
30
|
});
|
|
31
31
|
}
|
|
32
32
|
export {
|
|
@@ -39,13 +39,17 @@ const evaluateBindings = ({
|
|
|
39
39
|
}
|
|
40
40
|
return copied;
|
|
41
41
|
};
|
|
42
|
-
function getProcessedBlock(
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
42
|
+
function getProcessedBlock({
|
|
43
|
+
block,
|
|
44
|
+
context,
|
|
45
|
+
shouldEvaluateBindings,
|
|
46
|
+
state
|
|
47
|
+
}) {
|
|
48
|
+
const transformedBlock = transformBlock(block);
|
|
49
|
+
if (shouldEvaluateBindings) {
|
|
50
|
+
return evaluateBindings({ block: transformedBlock, state, context });
|
|
47
51
|
} else {
|
|
48
|
-
return
|
|
52
|
+
return transformedBlock;
|
|
49
53
|
}
|
|
50
54
|
}
|
|
51
55
|
export {
|
|
@@ -21,7 +21,7 @@ test("Can process bindings", () => {
|
|
|
21
21
|
block,
|
|
22
22
|
context: {},
|
|
23
23
|
state: { test: "hello" },
|
|
24
|
-
|
|
24
|
+
shouldEvaluateBindings: true
|
|
25
25
|
});
|
|
26
26
|
expect(processed).not.toEqual(block);
|
|
27
27
|
expect((_a = processed.properties) == null ? void 0 : _a.foo).toEqual("baz");
|
package/src/functions/track.js
CHANGED
|
@@ -1,7 +1,74 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __defProps = Object.defineProperties;
|
|
3
|
+
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
4
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
7
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
8
|
+
var __spreadValues = (a, b) => {
|
|
9
|
+
for (var prop in b || (b = {}))
|
|
10
|
+
if (__hasOwnProp.call(b, prop))
|
|
11
|
+
__defNormalProp(a, prop, b[prop]);
|
|
12
|
+
if (__getOwnPropSymbols)
|
|
13
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
14
|
+
if (__propIsEnum.call(b, prop))
|
|
15
|
+
__defNormalProp(a, prop, b[prop]);
|
|
16
|
+
}
|
|
17
|
+
return a;
|
|
18
|
+
};
|
|
19
|
+
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
20
|
+
var __objRest = (source, exclude) => {
|
|
21
|
+
var target = {};
|
|
22
|
+
for (var prop in source)
|
|
23
|
+
if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
|
|
24
|
+
target[prop] = source[prop];
|
|
25
|
+
if (source != null && __getOwnPropSymbols)
|
|
26
|
+
for (var prop of __getOwnPropSymbols(source)) {
|
|
27
|
+
if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
|
|
28
|
+
target[prop] = source[prop];
|
|
29
|
+
}
|
|
30
|
+
return target;
|
|
31
|
+
};
|
|
1
32
|
import { TARGET } from "../constants/target.js";
|
|
33
|
+
import { getSessionId } from "../helpers/sessionId.js";
|
|
34
|
+
import { getVisitorId } from "../helpers/visitorId.js";
|
|
2
35
|
import { isBrowser } from "./is-browser.js";
|
|
3
36
|
import { isEditing } from "./is-editing.js";
|
|
4
|
-
|
|
37
|
+
const getTrackingEventData = ({ canTrack }) => {
|
|
38
|
+
if (!canTrack) {
|
|
39
|
+
return { visitorId: void 0, sessionId: void 0 };
|
|
40
|
+
}
|
|
41
|
+
const sessionId = getSessionId({ canTrack });
|
|
42
|
+
const visitorId = getVisitorId({ canTrack });
|
|
43
|
+
return {
|
|
44
|
+
sessionId,
|
|
45
|
+
visitorId
|
|
46
|
+
};
|
|
47
|
+
};
|
|
48
|
+
const createEvent = (_a) => {
|
|
49
|
+
var _b = _a, {
|
|
50
|
+
type: eventType,
|
|
51
|
+
canTrack,
|
|
52
|
+
orgId,
|
|
53
|
+
contentId
|
|
54
|
+
} = _b, properties = __objRest(_b, [
|
|
55
|
+
"type",
|
|
56
|
+
"canTrack",
|
|
57
|
+
"orgId",
|
|
58
|
+
"contentId"
|
|
59
|
+
]);
|
|
60
|
+
return {
|
|
61
|
+
type: eventType,
|
|
62
|
+
data: __spreadProps(__spreadValues(__spreadValues({}, properties), getTrackingEventData({ canTrack })), {
|
|
63
|
+
ownerId: orgId,
|
|
64
|
+
contentId
|
|
65
|
+
})
|
|
66
|
+
};
|
|
67
|
+
};
|
|
68
|
+
function track(eventProps) {
|
|
69
|
+
if (!eventProps.canTrack) {
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
5
72
|
if (isEditing()) {
|
|
6
73
|
return;
|
|
7
74
|
}
|
|
@@ -10,7 +77,9 @@ function track(event, properties) {
|
|
|
10
77
|
}
|
|
11
78
|
return fetch(`https://builder.io/api/v1/track`, {
|
|
12
79
|
method: "POST",
|
|
13
|
-
body: JSON.stringify({
|
|
80
|
+
body: JSON.stringify({
|
|
81
|
+
events: [createEvent(eventProps)]
|
|
82
|
+
}),
|
|
14
83
|
headers: {
|
|
15
84
|
"content-type": "application/json"
|
|
16
85
|
},
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { isBrowser } from "../functions/is-browser";
|
|
2
|
+
import { getTopLevelDomain } from "./url";
|
|
3
|
+
const getCookie = ({
|
|
4
|
+
name,
|
|
5
|
+
canTrack
|
|
6
|
+
}) => {
|
|
7
|
+
var _a;
|
|
8
|
+
try {
|
|
9
|
+
if (!canTrack) {
|
|
10
|
+
return void 0;
|
|
11
|
+
}
|
|
12
|
+
return (_a = document.cookie.split("; ").find((row) => row.startsWith(`${name}=`))) == null ? void 0 : _a.split("=")[1];
|
|
13
|
+
} catch (err) {
|
|
14
|
+
console.debug("[COOKIE] GET error: ", err);
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
const stringifyCookie = (cookie) => cookie.map(([key, value]) => value ? `${key}=${value}` : key).join("; ");
|
|
18
|
+
const SECURE_CONFIG = [
|
|
19
|
+
["secure", ""],
|
|
20
|
+
["SameSite", "None"]
|
|
21
|
+
];
|
|
22
|
+
const createCookieString = ({
|
|
23
|
+
name,
|
|
24
|
+
value,
|
|
25
|
+
expires
|
|
26
|
+
}) => {
|
|
27
|
+
const secure = isBrowser() ? location.protocol === "https:" : true;
|
|
28
|
+
const secureObj = secure ? SECURE_CONFIG : [[]];
|
|
29
|
+
const expiresObj = expires ? [["expires", expires.toUTCString()]] : [[]];
|
|
30
|
+
const cookieValue = [
|
|
31
|
+
[name, value],
|
|
32
|
+
...expiresObj,
|
|
33
|
+
["path", "/"],
|
|
34
|
+
["domain", getTopLevelDomain(window.location.hostname)],
|
|
35
|
+
...secureObj
|
|
36
|
+
];
|
|
37
|
+
const cookie = stringifyCookie(cookieValue);
|
|
38
|
+
return cookie;
|
|
39
|
+
};
|
|
40
|
+
const setCookie = ({
|
|
41
|
+
name,
|
|
42
|
+
value,
|
|
43
|
+
expires,
|
|
44
|
+
canTrack
|
|
45
|
+
}) => {
|
|
46
|
+
try {
|
|
47
|
+
if (!canTrack) {
|
|
48
|
+
return void 0;
|
|
49
|
+
}
|
|
50
|
+
const cookie = createCookieString({ name, value, expires });
|
|
51
|
+
document.cookie = cookie;
|
|
52
|
+
} catch (err) {
|
|
53
|
+
console.warn("[COOKIE] SET error: ", err);
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
export {
|
|
57
|
+
getCookie,
|
|
58
|
+
setCookie
|
|
59
|
+
};
|