@builder.io/sdk-svelte 0.1.1 โ 0.1.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/package/--package.json +1 -1
- package/package/CHANGELOG.md +4 -0
- package/package/components/render-content/render-content.svelte +2 -1
- package/package/helpers/ab-tests.d.ts +1 -1
- package/package/helpers/cookie.d.ts +1 -1
- package/package/helpers/cookie.js +7 -2
- package/package/helpers/localStorage.d.ts +2 -1
- package/package/helpers/localStorage.js +1 -0
- package/package/helpers/sessionId.d.ts +2 -2
- package/package/helpers/sessionId.js +2 -1
- package/package/helpers/url.js +3 -0
- package/package/helpers/url.test.js +6 -0
- package/package/helpers/visitorId.js +1 -0
- package/package.json +1 -1
- package/src/blocks/embed/component-info.ts +29 -27
- package/src/blocks/image/component-info.ts +57 -55
- package/src/components/render-content/render-content.svelte +2 -1
- package/src/helpers/cookie.ts +9 -4
- package/src/helpers/localStorage.ts +3 -1
- package/src/helpers/sessionId.ts +5 -1
- package/src/helpers/url.test.ts +7 -0
- package/src/helpers/url.ts +4 -0
- package/src/helpers/visitorId.ts +3 -1
package/package/--package.json
CHANGED
package/package/CHANGELOG.md
CHANGED
|
@@ -15,6 +15,7 @@ import RenderBlocks from "../render-blocks.svelte";
|
|
|
15
15
|
import RenderContentStyles from "./components/render-styles.svelte";
|
|
16
16
|
import BuilderContext from "../../context/builder.context.js";
|
|
17
17
|
import { registerInsertMenu, setupBrowserForEditing, } from "../../scripts/init-editing.js";
|
|
18
|
+
import { checkIsDefined } from "../../helpers/nullable.js";
|
|
18
19
|
export let content;
|
|
19
20
|
export let data;
|
|
20
21
|
export let canTrack;
|
|
@@ -167,7 +168,7 @@ $: useContent = () => {
|
|
|
167
168
|
return mergedContent;
|
|
168
169
|
};
|
|
169
170
|
$: canTrackToUse = () => {
|
|
170
|
-
return canTrack
|
|
171
|
+
return checkIsDefined(canTrack) ? canTrack : true;
|
|
171
172
|
};
|
|
172
173
|
$: contentState = () => {
|
|
173
174
|
return {
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { isBrowser } from '../functions/is-browser.js';
|
|
2
|
+
import { checkIsDefined } from './nullable.js';
|
|
2
3
|
import { getTopLevelDomain } from './url.js';
|
|
3
4
|
/**
|
|
4
5
|
* NOTE: This function is `async` because its react-native override is async. Do not remove the `async` keyword!
|
|
@@ -19,9 +20,13 @@ export const getCookie = async ({ name, canTrack, }) => {
|
|
|
19
20
|
}
|
|
20
21
|
catch (err) {
|
|
21
22
|
console.debug('[COOKIE] GET error: ', err);
|
|
23
|
+
return undefined;
|
|
22
24
|
}
|
|
23
25
|
};
|
|
24
|
-
const stringifyCookie = (cookie) => cookie
|
|
26
|
+
const stringifyCookie = (cookie) => cookie
|
|
27
|
+
.map(([key, value]) => (value ? `${key}=${value}` : key))
|
|
28
|
+
.filter(checkIsDefined)
|
|
29
|
+
.join('; ');
|
|
25
30
|
const SECURE_CONFIG = [
|
|
26
31
|
['secure', ''],
|
|
27
32
|
['SameSite', 'None'],
|
|
@@ -49,7 +54,7 @@ const createCookieString = ({ name, value, expires, }) => {
|
|
|
49
54
|
export const setCookie = async ({ name, value, expires, canTrack, }) => {
|
|
50
55
|
try {
|
|
51
56
|
if (!canTrack) {
|
|
52
|
-
return
|
|
57
|
+
return;
|
|
53
58
|
}
|
|
54
59
|
const cookie = createCookieString({ name, value, expires });
|
|
55
60
|
document.cookie = cookie;
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import type { CanTrack } from '../types/can-track.js';
|
|
2
|
+
import type { Nullable } from './nullable.js';
|
|
2
3
|
export declare const getLocalStorageItem: ({ key, canTrack, }: {
|
|
3
4
|
key: string;
|
|
4
|
-
} & CanTrack) => string
|
|
5
|
+
} & CanTrack) => Nullable<string>;
|
|
5
6
|
export declare const setLocalStorageItem: ({ key, canTrack, value, }: {
|
|
6
7
|
key: string;
|
|
7
8
|
value: string;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { CanTrack } from '../types/can-track.js';
|
|
2
|
-
export declare const getSessionId: ({ canTrack }: CanTrack) => Promise<string | undefined>;
|
|
2
|
+
export declare const getSessionId: ({ canTrack, }: CanTrack) => Promise<string | undefined>;
|
|
3
3
|
export declare const createSessionId: () => string;
|
|
4
4
|
export declare const setSessionId: ({ id, canTrack, }: {
|
|
5
5
|
id: string;
|
|
6
|
-
} & CanTrack) => Promise<
|
|
6
|
+
} & CanTrack) => Promise<void>;
|
|
@@ -2,7 +2,7 @@ import { getCookie, setCookie } from './cookie.js';
|
|
|
2
2
|
import { checkIsDefined } from './nullable.js';
|
|
3
3
|
import { uuid } from './uuid.js';
|
|
4
4
|
const SESSION_LOCAL_STORAGE_KEY = 'builderSessionId';
|
|
5
|
-
export const getSessionId = async ({ canTrack }) => {
|
|
5
|
+
export const getSessionId = async ({ canTrack, }) => {
|
|
6
6
|
if (!canTrack) {
|
|
7
7
|
return undefined;
|
|
8
8
|
}
|
|
@@ -16,6 +16,7 @@ export const getSessionId = async ({ canTrack }) => {
|
|
|
16
16
|
else {
|
|
17
17
|
const newSessionId = createSessionId();
|
|
18
18
|
setSessionId({ id: newSessionId, canTrack });
|
|
19
|
+
return newSessionId;
|
|
19
20
|
}
|
|
20
21
|
};
|
|
21
22
|
export const createSessionId = () => uuid();
|
package/package/helpers/url.js
CHANGED
|
@@ -12,4 +12,10 @@ describe('getTopLevelDomain', () => {
|
|
|
12
12
|
const output = getTopLevelDomain('www.example.co.uk');
|
|
13
13
|
expect(output).toBe('example.co.uk');
|
|
14
14
|
});
|
|
15
|
+
test('handles localhost', () => {
|
|
16
|
+
const output = getTopLevelDomain('localhost');
|
|
17
|
+
expect(output).toBe('localhost');
|
|
18
|
+
const output2 = getTopLevelDomain('127.0.0.1');
|
|
19
|
+
expect(output2).toBe('127.0.0.1');
|
|
20
|
+
});
|
|
15
21
|
});
|
package/package.json
CHANGED
|
@@ -12,35 +12,37 @@ export const componentInfo: ComponentInfo = {
|
|
|
12
12
|
required: true,
|
|
13
13
|
defaultValue: '',
|
|
14
14
|
helperText: 'e.g. enter a youtube url, google map, etc',
|
|
15
|
-
onChange: serializeFn(
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
if (
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
15
|
+
onChange: serializeFn(
|
|
16
|
+
(options: Map<string, any>): void | Promise<void> => {
|
|
17
|
+
const url = options.get('url');
|
|
18
|
+
if (url) {
|
|
19
|
+
options.set('content', 'Loading...');
|
|
20
|
+
// TODO: get this out of here!
|
|
21
|
+
const apiKey = 'ae0e60e78201a3f2b0de4b';
|
|
22
|
+
return fetch(
|
|
23
|
+
`https://iframe.ly/api/iframely?url=${url}&api_key=${apiKey}`
|
|
24
|
+
)
|
|
25
|
+
.then((res) => res.json())
|
|
26
|
+
.then((data) => {
|
|
27
|
+
if (options.get('url') === url) {
|
|
28
|
+
if (data.html) {
|
|
29
|
+
options.set('content', data.html);
|
|
30
|
+
} else {
|
|
31
|
+
options.set('content', 'Invalid url, please try another');
|
|
32
|
+
}
|
|
31
33
|
}
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
);
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
34
|
+
})
|
|
35
|
+
.catch((_err) => {
|
|
36
|
+
options.set(
|
|
37
|
+
'content',
|
|
38
|
+
'There was an error embedding this URL, please try again or another URL'
|
|
39
|
+
);
|
|
40
|
+
});
|
|
41
|
+
} else {
|
|
42
|
+
options.delete('content');
|
|
43
|
+
}
|
|
42
44
|
}
|
|
43
|
-
|
|
45
|
+
),
|
|
44
46
|
},
|
|
45
47
|
{
|
|
46
48
|
name: 'content',
|
|
@@ -23,69 +23,71 @@ export const componentInfo: ComponentInfo = {
|
|
|
23
23
|
required: true,
|
|
24
24
|
defaultValue:
|
|
25
25
|
'https://cdn.builder.io/api/v1/image/assets%2Fpwgjf0RoYWbdnJSbpBAjXNRMe9F2%2Ffb27a7c790324294af8be1c35fe30f4d',
|
|
26
|
-
onChange: serializeFn(
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
26
|
+
onChange: serializeFn(
|
|
27
|
+
(options: Map<string, any>): void | Promise<void> => {
|
|
28
|
+
const DEFAULT_ASPECT_RATIO = 0.7041;
|
|
29
|
+
options.delete('srcset');
|
|
30
|
+
options.delete('noWebp');
|
|
31
|
+
function loadImage(
|
|
32
|
+
url: string,
|
|
33
|
+
timeout = 60000
|
|
34
|
+
): Promise<HTMLImageElement> {
|
|
35
|
+
return new Promise((resolve, reject) => {
|
|
36
|
+
const img = document.createElement('img');
|
|
37
|
+
let loaded = false;
|
|
38
|
+
img.onload = () => {
|
|
39
|
+
loaded = true;
|
|
40
|
+
resolve(img);
|
|
41
|
+
};
|
|
41
42
|
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
43
|
+
img.addEventListener('error', (event) => {
|
|
44
|
+
console.warn('Image load failed', event.error);
|
|
45
|
+
reject(event.error);
|
|
46
|
+
});
|
|
46
47
|
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
48
|
+
img.src = url;
|
|
49
|
+
setTimeout(() => {
|
|
50
|
+
if (!loaded) {
|
|
51
|
+
reject(new Error('Image load timed out'));
|
|
52
|
+
}
|
|
53
|
+
}, timeout);
|
|
54
|
+
});
|
|
55
|
+
}
|
|
55
56
|
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
57
|
+
function round(num: number) {
|
|
58
|
+
return Math.round(num * 1000) / 1000;
|
|
59
|
+
}
|
|
59
60
|
|
|
60
|
-
|
|
61
|
-
|
|
61
|
+
const value = options.get('image');
|
|
62
|
+
const aspectRatio = options.get('aspectRatio');
|
|
62
63
|
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
64
|
+
// For SVG images - don't render as webp, keep them as SVG
|
|
65
|
+
fetch(value)
|
|
66
|
+
.then((res) => res.blob())
|
|
67
|
+
.then((blob) => {
|
|
68
|
+
if (blob.type.includes('svg')) {
|
|
69
|
+
options.set('noWebp', true);
|
|
70
|
+
}
|
|
71
|
+
});
|
|
71
72
|
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
73
|
+
if (value && (!aspectRatio || aspectRatio === DEFAULT_ASPECT_RATIO)) {
|
|
74
|
+
return loadImage(value).then((img) => {
|
|
75
|
+
const possiblyUpdatedAspectRatio = options.get('aspectRatio');
|
|
76
|
+
if (
|
|
77
|
+
options.get('image') === value &&
|
|
78
|
+
(!possiblyUpdatedAspectRatio ||
|
|
79
|
+
possiblyUpdatedAspectRatio === DEFAULT_ASPECT_RATIO)
|
|
80
|
+
) {
|
|
81
|
+
if (img.width && img.height) {
|
|
82
|
+
options.set('aspectRatio', round(img.height / img.width));
|
|
83
|
+
options.set('height', img.height);
|
|
84
|
+
options.set('width', img.width);
|
|
85
|
+
}
|
|
84
86
|
}
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
+
});
|
|
88
|
+
}
|
|
87
89
|
}
|
|
88
|
-
|
|
90
|
+
),
|
|
89
91
|
},
|
|
90
92
|
{
|
|
91
93
|
name: 'backgroundSize',
|
|
@@ -69,6 +69,7 @@
|
|
|
69
69
|
registerInsertMenu,
|
|
70
70
|
setupBrowserForEditing,
|
|
71
71
|
} from "../../scripts/init-editing.js";
|
|
72
|
+
import { checkIsDefined } from "../../helpers/nullable.js";
|
|
72
73
|
|
|
73
74
|
export let content: RenderContentProps["content"];
|
|
74
75
|
export let data: RenderContentProps["data"];
|
|
@@ -231,7 +232,7 @@
|
|
|
231
232
|
return mergedContent;
|
|
232
233
|
};
|
|
233
234
|
$: canTrackToUse = () => {
|
|
234
|
-
return canTrack
|
|
235
|
+
return checkIsDefined(canTrack) ? canTrack : true;
|
|
235
236
|
};
|
|
236
237
|
$: contentState = () => {
|
|
237
238
|
return {
|
package/src/helpers/cookie.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { isBrowser } from '../functions/is-browser.js';
|
|
2
2
|
import type { CanTrack } from '../types/can-track.js';
|
|
3
|
+
import { checkIsDefined } from './nullable.js';
|
|
3
4
|
import { getTopLevelDomain } from './url.js';
|
|
4
5
|
|
|
5
6
|
/**
|
|
@@ -10,7 +11,7 @@ export const getCookie = async ({
|
|
|
10
11
|
canTrack,
|
|
11
12
|
}: {
|
|
12
13
|
name: string;
|
|
13
|
-
} & CanTrack) => {
|
|
14
|
+
} & CanTrack): Promise<string | undefined> => {
|
|
14
15
|
try {
|
|
15
16
|
if (!canTrack) {
|
|
16
17
|
return undefined;
|
|
@@ -26,6 +27,7 @@ export const getCookie = async ({
|
|
|
26
27
|
?.split('=')[1];
|
|
27
28
|
} catch (err) {
|
|
28
29
|
console.debug('[COOKIE] GET error: ', err);
|
|
30
|
+
return undefined;
|
|
29
31
|
}
|
|
30
32
|
};
|
|
31
33
|
|
|
@@ -40,7 +42,10 @@ type CookieConfiguration = Array<
|
|
|
40
42
|
>;
|
|
41
43
|
|
|
42
44
|
const stringifyCookie = (cookie: CookieConfiguration): string =>
|
|
43
|
-
cookie
|
|
45
|
+
cookie
|
|
46
|
+
.map(([key, value]) => (value ? `${key}=${value}` : key))
|
|
47
|
+
.filter(checkIsDefined)
|
|
48
|
+
.join('; ');
|
|
44
49
|
|
|
45
50
|
const SECURE_CONFIG: CookieConfiguration = [
|
|
46
51
|
['secure', ''],
|
|
@@ -89,10 +94,10 @@ export const setCookie = async ({
|
|
|
89
94
|
name: string;
|
|
90
95
|
value: string;
|
|
91
96
|
expires?: Date;
|
|
92
|
-
} & CanTrack) => {
|
|
97
|
+
} & CanTrack): Promise<void> => {
|
|
93
98
|
try {
|
|
94
99
|
if (!canTrack) {
|
|
95
|
-
return
|
|
100
|
+
return;
|
|
96
101
|
}
|
|
97
102
|
const cookie = createCookieString({ name, value, expires });
|
|
98
103
|
document.cookie = cookie;
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { isBrowser } from '../functions/is-browser.js';
|
|
2
2
|
import type { CanTrack } from '../types/can-track.js';
|
|
3
|
+
import type { Nullable } from './nullable.js';
|
|
3
4
|
|
|
4
5
|
const getLocalStorage = () =>
|
|
5
6
|
isBrowser() && typeof localStorage !== 'undefined' ? localStorage : undefined;
|
|
@@ -9,7 +10,7 @@ export const getLocalStorageItem = ({
|
|
|
9
10
|
canTrack,
|
|
10
11
|
}: {
|
|
11
12
|
key: string;
|
|
12
|
-
} & CanTrack) => {
|
|
13
|
+
} & CanTrack): Nullable<string> => {
|
|
13
14
|
try {
|
|
14
15
|
if (canTrack) {
|
|
15
16
|
return getLocalStorage()?.getItem(key);
|
|
@@ -17,6 +18,7 @@ export const getLocalStorageItem = ({
|
|
|
17
18
|
return undefined;
|
|
18
19
|
} catch (err) {
|
|
19
20
|
console.debug('[LocalStorage] GET error: ', err);
|
|
21
|
+
return undefined;
|
|
20
22
|
}
|
|
21
23
|
};
|
|
22
24
|
|
package/src/helpers/sessionId.ts
CHANGED
|
@@ -5,7 +5,9 @@ import { uuid } from './uuid.js';
|
|
|
5
5
|
|
|
6
6
|
const SESSION_LOCAL_STORAGE_KEY = 'builderSessionId';
|
|
7
7
|
|
|
8
|
-
export const getSessionId = async ({
|
|
8
|
+
export const getSessionId = async ({
|
|
9
|
+
canTrack,
|
|
10
|
+
}: CanTrack): Promise<string | undefined> => {
|
|
9
11
|
if (!canTrack) {
|
|
10
12
|
return undefined;
|
|
11
13
|
}
|
|
@@ -20,6 +22,8 @@ export const getSessionId = async ({ canTrack }: CanTrack) => {
|
|
|
20
22
|
} else {
|
|
21
23
|
const newSessionId = createSessionId();
|
|
22
24
|
setSessionId({ id: newSessionId, canTrack });
|
|
25
|
+
|
|
26
|
+
return newSessionId;
|
|
23
27
|
}
|
|
24
28
|
};
|
|
25
29
|
|
package/src/helpers/url.test.ts
CHANGED
|
@@ -13,4 +13,11 @@ describe('getTopLevelDomain', () => {
|
|
|
13
13
|
const output = getTopLevelDomain('www.example.co.uk');
|
|
14
14
|
expect(output).toBe('example.co.uk');
|
|
15
15
|
});
|
|
16
|
+
test('handles localhost', () => {
|
|
17
|
+
const output = getTopLevelDomain('localhost');
|
|
18
|
+
expect(output).toBe('localhost');
|
|
19
|
+
|
|
20
|
+
const output2 = getTopLevelDomain('127.0.0.1');
|
|
21
|
+
expect(output2).toBe('127.0.0.1');
|
|
22
|
+
});
|
|
16
23
|
});
|
package/src/helpers/url.ts
CHANGED
|
@@ -4,6 +4,10 @@
|
|
|
4
4
|
* www.example.co.uk -> example.co.uk
|
|
5
5
|
*/
|
|
6
6
|
export const getTopLevelDomain = (host: string) => {
|
|
7
|
+
if (host === 'localhost' || host === '127.0.0.1') {
|
|
8
|
+
return host;
|
|
9
|
+
}
|
|
10
|
+
|
|
7
11
|
const parts = host.split('.');
|
|
8
12
|
if (parts.length > 2) {
|
|
9
13
|
return parts.slice(1).join('.');
|
package/src/helpers/visitorId.ts
CHANGED
|
@@ -5,7 +5,7 @@ import { uuid } from './uuid.js';
|
|
|
5
5
|
|
|
6
6
|
const VISITOR_LOCAL_STORAGE_KEY = 'builderVisitorId';
|
|
7
7
|
|
|
8
|
-
export const getVisitorId = ({ canTrack }: CanTrack) => {
|
|
8
|
+
export const getVisitorId = ({ canTrack }: CanTrack): string | undefined => {
|
|
9
9
|
if (!canTrack) {
|
|
10
10
|
return undefined;
|
|
11
11
|
}
|
|
@@ -20,6 +20,8 @@ export const getVisitorId = ({ canTrack }: CanTrack) => {
|
|
|
20
20
|
} else {
|
|
21
21
|
const newVisitorId = createVisitorId();
|
|
22
22
|
setVisitorId({ id: newVisitorId, canTrack });
|
|
23
|
+
|
|
24
|
+
return newVisitorId;
|
|
23
25
|
}
|
|
24
26
|
};
|
|
25
27
|
|