@builder.io/sdk-svelte 0.2.3 → 0.4.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/package/--package.json +10 -5
- package/package/CHANGELOG.md +17 -0
- package/package/blocks/columns/columns.svelte +1 -1
- package/package/blocks/symbol/symbol.svelte +1 -1
- package/package/components/render-block/block-styles.svelte +3 -1
- package/package/components/render-block/render-block.helpers.d.ts +0 -1
- package/package/components/render-block/render-block.helpers.js +8 -20
- package/package/components/render-block/render-block.svelte +25 -19
- package/package/components/render-block/render-repeated-block.svelte +3 -2
- package/package/components/render-content/components/render-styles.svelte +1 -1
- package/package/components/render-content/render-content.svelte +99 -10
- package/package/components/render-content/render-content.svelte.d.ts +5 -0
- package/package/components/render-content/render-content.types.d.ts +11 -2
- package/package/components/render-content/wrap-component-ref.d.ts +6 -0
- package/package/components/render-content/wrap-component-ref.js +6 -0
- package/package/components/render-content-variants/helpers.d.ts +17 -0
- package/package/components/render-content-variants/helpers.js +182 -0
- package/package/components/render-content-variants/render-content-variants.svelte +70 -0
- package/package/components/render-content-variants/render-content-variants.svelte.d.ts +23 -0
- package/package/components/render-inlined-styles.svelte +3 -15
- package/package/components/render-inlined-styles.svelte.d.ts +2 -0
- package/package/constants/sdk-version.d.ts +1 -0
- package/package/constants/sdk-version.js +1 -0
- package/package/context/builder.context.d.ts +3 -2
- package/package/context/builder.context.js +3 -2
- package/package/context/types.d.ts +17 -2
- package/package/functions/evaluate.d.ts +4 -3
- package/package/functions/evaluate.js +23 -2
- package/package/functions/evaluate.test.d.ts +1 -0
- package/package/functions/evaluate.test.js +17 -0
- package/package/functions/get-block-actions-handler.d.ts +1 -1
- package/package/functions/get-block-actions-handler.js +3 -1
- package/package/functions/get-block-actions.d.ts +1 -1
- package/package/functions/get-content/generate-content-url.js +2 -2
- package/package/functions/get-content/generate-content-url.test.js +15 -0
- package/package/functions/get-content/index.d.ts +7 -2
- package/package/functions/get-content/index.js +43 -20
- package/package/functions/get-content/types.d.ts +6 -0
- package/package/functions/get-processed-block.d.ts +2 -2
- package/package/functions/get-processed-block.js +16 -4
- package/package/functions/get-processed-block.test.js +3 -1
- package/package/helpers/ab-tests.d.ts +8 -7
- package/package/helpers/ab-tests.js +103 -4
- package/package/helpers/canTrack.d.ts +1 -0
- package/package/helpers/canTrack.js +2 -0
- package/package/helpers/cookie.d.ts +7 -3
- package/package/helpers/cookie.js +9 -6
- package/package/helpers/logger.d.ts +1 -0
- package/package/helpers/logger.js +1 -0
- package/package/index-helpers/blocks-exports.d.ts +1 -1
- package/package/index-helpers/blocks-exports.js +1 -1
- package/package/index.d.ts +8 -7
- package/package/index.js +6 -7
- package/package/scripts/init-editing.d.ts +1 -0
- package/package/scripts/init-editing.js +2 -0
- package/package/types/builder-content.d.ts +1 -3
- package/package.json +4 -8
- package/src/blocks/columns/columns.svelte +1 -5
- package/src/blocks/symbol/symbol.svelte +1 -1
- package/src/components/render-block/block-styles.svelte +3 -1
- package/src/components/render-block/render-block.helpers.ts +9 -31
- package/src/components/render-block/render-block.svelte +24 -19
- package/src/components/render-block/render-repeated-block.svelte +3 -2
- package/src/components/render-content/components/render-styles.svelte +1 -1
- package/src/components/render-content/render-content.svelte +101 -10
- package/src/components/render-content/render-content.types.ts +11 -2
- package/src/components/render-content/wrap-component-ref.ts +6 -0
- package/src/components/render-content-variants/helpers.ts +245 -0
- package/src/components/render-content-variants/render-content-variants.svelte +83 -0
- package/src/components/render-inlined-styles.svelte +3 -16
- package/src/constants/sdk-version.ts +1 -0
- package/src/context/builder.context.ts +3 -2
- package/src/context/types.ts +17 -2
- package/src/functions/evaluate.test.ts +21 -0
- package/src/functions/evaluate.ts +46 -4
- package/src/functions/get-block-actions-handler.ts +7 -2
- package/src/functions/get-block-actions.ts +4 -1
- package/src/functions/get-content/generate-content-url.test.ts +17 -0
- package/src/functions/get-content/generate-content-url.ts +2 -1
- package/src/functions/get-content/index.ts +57 -26
- package/src/functions/get-content/types.ts +7 -0
- package/src/functions/get-processed-block.test.ts +3 -1
- package/src/functions/get-processed-block.ts +28 -6
- package/src/helpers/ab-tests.ts +164 -11
- package/src/helpers/canTrack.ts +4 -0
- package/src/helpers/cookie.ts +16 -11
- package/src/helpers/logger.ts +1 -0
- package/src/index-helpers/blocks-exports.ts +1 -1
- package/src/index.ts +17 -8
- package/src/scripts/init-editing.ts +3 -0
- package/src/types/builder-content.ts +1 -3
- package/package/functions/get-content/ab-testing.d.ts +0 -5
- package/package/functions/get-content/ab-testing.js +0 -78
- package/src/functions/get-content/ab-testing.ts +0 -132
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { SDK_VERSION } from '../constants/sdk-version.js';
|
|
1
2
|
import { TARGET } from '../constants/target.js';
|
|
2
3
|
import { isBrowser } from '../functions/is-browser.js';
|
|
3
4
|
import { register } from '../functions/register.js';
|
|
@@ -26,6 +27,7 @@ export const registerInsertMenu = () => {
|
|
|
26
27
|
let isSetupForEditing = false;
|
|
27
28
|
export const setupBrowserForEditing = (
|
|
28
29
|
options: {
|
|
30
|
+
enrich?: boolean;
|
|
29
31
|
includeRefs?: boolean;
|
|
30
32
|
locale?: string;
|
|
31
33
|
} = {}
|
|
@@ -40,6 +42,7 @@ export const setupBrowserForEditing = (
|
|
|
40
42
|
type: 'builder.sdkInfo',
|
|
41
43
|
data: {
|
|
42
44
|
target: TARGET,
|
|
45
|
+
version: SDK_VERSION,
|
|
43
46
|
// TODO: compile these in
|
|
44
47
|
// type: process.env.SDK_TYPE,
|
|
45
48
|
// version: process.env.SDK_VERSION,
|
|
@@ -30,8 +30,6 @@ export interface BuilderContentVariation {
|
|
|
30
30
|
export interface BuilderContent extends BuilderContentVariation {
|
|
31
31
|
// TODO: query
|
|
32
32
|
'@version'?: number;
|
|
33
|
-
id?: string;
|
|
34
|
-
name?: string;
|
|
35
33
|
published?: 'published' | 'draft' | 'archived';
|
|
36
34
|
modelId?: string;
|
|
37
35
|
priority?: number;
|
|
@@ -39,7 +37,7 @@ export interface BuilderContent extends BuilderContentVariation {
|
|
|
39
37
|
startDate?: number;
|
|
40
38
|
endDate?: number;
|
|
41
39
|
variations?: {
|
|
42
|
-
[id: string]: BuilderContentVariation
|
|
40
|
+
[id: string]: BuilderContentVariation;
|
|
43
41
|
};
|
|
44
42
|
testVariationId?: string;
|
|
45
43
|
testVariationName?: string;
|
|
@@ -1,78 +0,0 @@
|
|
|
1
|
-
import { getContentVariationCookie, setContentVariationCookie, } from '../../helpers/ab-tests.js';
|
|
2
|
-
import { checkIsDefined } from '../../helpers/nullable.js';
|
|
3
|
-
const checkIsBuilderContentWithVariations = (item) => checkIsDefined(item.id) &&
|
|
4
|
-
checkIsDefined(item.variations) &&
|
|
5
|
-
Object.keys(item.variations).length > 0;
|
|
6
|
-
/**
|
|
7
|
-
* Randomly assign a variation to this user and store it in cookies/storage
|
|
8
|
-
*/
|
|
9
|
-
const getRandomVariationId = ({ id, variations, }) => {
|
|
10
|
-
let n = 0;
|
|
11
|
-
const random = Math.random();
|
|
12
|
-
// loop over variations test ratios, incrementing a counter,
|
|
13
|
-
// until we find the variation that this user should be assigned to
|
|
14
|
-
for (const id in variations) {
|
|
15
|
-
const testRatio = variations[id]?.testRatio;
|
|
16
|
-
n += testRatio;
|
|
17
|
-
if (random < n) {
|
|
18
|
-
return id;
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
// `item.variations` does not include the default variation
|
|
22
|
-
// if we arrive here, then it means that the random number fits in the default variation bucket
|
|
23
|
-
return id;
|
|
24
|
-
};
|
|
25
|
-
const getTestFields = ({ item, testGroupId, }) => {
|
|
26
|
-
const variationValue = item.variations[testGroupId];
|
|
27
|
-
if (testGroupId === item.id ||
|
|
28
|
-
// handle edge-case where `testGroupId` points to non-existing variation
|
|
29
|
-
!variationValue) {
|
|
30
|
-
return {
|
|
31
|
-
testVariationId: item.id,
|
|
32
|
-
testVariationName: 'Default',
|
|
33
|
-
};
|
|
34
|
-
}
|
|
35
|
-
else {
|
|
36
|
-
return {
|
|
37
|
-
data: variationValue.data,
|
|
38
|
-
testVariationId: variationValue.id,
|
|
39
|
-
testVariationName: variationValue.name || (variationValue.id === item.id ? 'Default' : ''),
|
|
40
|
-
};
|
|
41
|
-
}
|
|
42
|
-
};
|
|
43
|
-
const getContentVariation = async ({ item, canTrack, }) => {
|
|
44
|
-
// try to find test variation in cookies/storage
|
|
45
|
-
const testGroupId = await getContentVariationCookie({
|
|
46
|
-
canTrack,
|
|
47
|
-
contentId: item.id,
|
|
48
|
-
});
|
|
49
|
-
const testFields = testGroupId
|
|
50
|
-
? getTestFields({ item, testGroupId })
|
|
51
|
-
: undefined;
|
|
52
|
-
if (testFields) {
|
|
53
|
-
return testFields;
|
|
54
|
-
}
|
|
55
|
-
else {
|
|
56
|
-
// if variation not found in storage, assign a random variation to this user
|
|
57
|
-
const randomVariationId = getRandomVariationId({
|
|
58
|
-
variations: item.variations,
|
|
59
|
-
id: item.id,
|
|
60
|
-
});
|
|
61
|
-
// store variation in cookies/storage
|
|
62
|
-
setContentVariationCookie({
|
|
63
|
-
contentId: item.id,
|
|
64
|
-
value: randomVariationId,
|
|
65
|
-
canTrack,
|
|
66
|
-
}).catch((err) => {
|
|
67
|
-
console.error('could not store A/B test variation: ', err);
|
|
68
|
-
});
|
|
69
|
-
return getTestFields({ item, testGroupId: randomVariationId });
|
|
70
|
-
}
|
|
71
|
-
};
|
|
72
|
-
export const handleABTesting = async ({ item, canTrack, }) => {
|
|
73
|
-
if (!checkIsBuilderContentWithVariations(item)) {
|
|
74
|
-
return;
|
|
75
|
-
}
|
|
76
|
-
const variationValue = await getContentVariation({ item, canTrack });
|
|
77
|
-
Object.assign(item, variationValue);
|
|
78
|
-
};
|
|
@@ -1,132 +0,0 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
BuilderContent,
|
|
3
|
-
BuilderContentVariation,
|
|
4
|
-
} from '../../types/builder-content.js';
|
|
5
|
-
import {
|
|
6
|
-
getContentVariationCookie,
|
|
7
|
-
setContentVariationCookie,
|
|
8
|
-
} from '../../helpers/ab-tests.js';
|
|
9
|
-
import type { Overwrite } from '../../types/typescript.js';
|
|
10
|
-
import type { CanTrack } from '../../types/can-track.js';
|
|
11
|
-
import { checkIsDefined } from '../../helpers/nullable.js';
|
|
12
|
-
|
|
13
|
-
type BuilderContentWithVariations = Overwrite<
|
|
14
|
-
BuilderContent,
|
|
15
|
-
Required<Pick<BuilderContent, 'variations' | 'id'>>
|
|
16
|
-
>;
|
|
17
|
-
|
|
18
|
-
const checkIsBuilderContentWithVariations = (
|
|
19
|
-
item: BuilderContent
|
|
20
|
-
): item is BuilderContentWithVariations =>
|
|
21
|
-
checkIsDefined(item.id) &&
|
|
22
|
-
checkIsDefined(item.variations) &&
|
|
23
|
-
Object.keys(item.variations).length > 0;
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* Randomly assign a variation to this user and store it in cookies/storage
|
|
27
|
-
*/
|
|
28
|
-
const getRandomVariationId = ({
|
|
29
|
-
id,
|
|
30
|
-
variations,
|
|
31
|
-
}: BuilderContentWithVariations) => {
|
|
32
|
-
let n = 0;
|
|
33
|
-
const random = Math.random();
|
|
34
|
-
|
|
35
|
-
// loop over variations test ratios, incrementing a counter,
|
|
36
|
-
// until we find the variation that this user should be assigned to
|
|
37
|
-
for (const id in variations) {
|
|
38
|
-
const testRatio = variations[id]?.testRatio;
|
|
39
|
-
n += testRatio!;
|
|
40
|
-
|
|
41
|
-
if (random < n) {
|
|
42
|
-
return id;
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
// `item.variations` does not include the default variation
|
|
47
|
-
// if we arrive here, then it means that the random number fits in the default variation bucket
|
|
48
|
-
return id;
|
|
49
|
-
};
|
|
50
|
-
|
|
51
|
-
const getTestFields = ({
|
|
52
|
-
item,
|
|
53
|
-
testGroupId,
|
|
54
|
-
}: {
|
|
55
|
-
item: BuilderContentWithVariations;
|
|
56
|
-
testGroupId: string;
|
|
57
|
-
}): TestFields => {
|
|
58
|
-
const variationValue = item.variations[testGroupId];
|
|
59
|
-
if (
|
|
60
|
-
testGroupId === item.id ||
|
|
61
|
-
// handle edge-case where `testGroupId` points to non-existing variation
|
|
62
|
-
!variationValue
|
|
63
|
-
) {
|
|
64
|
-
return {
|
|
65
|
-
testVariationId: item.id,
|
|
66
|
-
testVariationName: 'Default',
|
|
67
|
-
};
|
|
68
|
-
} else {
|
|
69
|
-
return {
|
|
70
|
-
data: variationValue.data,
|
|
71
|
-
testVariationId: variationValue.id,
|
|
72
|
-
testVariationName:
|
|
73
|
-
variationValue.name || (variationValue.id === item.id ? 'Default' : ''),
|
|
74
|
-
};
|
|
75
|
-
}
|
|
76
|
-
};
|
|
77
|
-
|
|
78
|
-
type TestFields = {
|
|
79
|
-
data?: BuilderContentVariation['data'];
|
|
80
|
-
testVariationId?: string;
|
|
81
|
-
testVariationName: string;
|
|
82
|
-
};
|
|
83
|
-
|
|
84
|
-
const getContentVariation = async ({
|
|
85
|
-
item,
|
|
86
|
-
canTrack,
|
|
87
|
-
}: {
|
|
88
|
-
item: BuilderContentWithVariations;
|
|
89
|
-
} & CanTrack): Promise<TestFields> => {
|
|
90
|
-
// try to find test variation in cookies/storage
|
|
91
|
-
const testGroupId = await getContentVariationCookie({
|
|
92
|
-
canTrack,
|
|
93
|
-
contentId: item.id,
|
|
94
|
-
});
|
|
95
|
-
|
|
96
|
-
const testFields = testGroupId
|
|
97
|
-
? getTestFields({ item, testGroupId })
|
|
98
|
-
: undefined;
|
|
99
|
-
|
|
100
|
-
if (testFields) {
|
|
101
|
-
return testFields;
|
|
102
|
-
} else {
|
|
103
|
-
// if variation not found in storage, assign a random variation to this user
|
|
104
|
-
const randomVariationId = getRandomVariationId({
|
|
105
|
-
variations: item.variations,
|
|
106
|
-
id: item.id,
|
|
107
|
-
});
|
|
108
|
-
|
|
109
|
-
// store variation in cookies/storage
|
|
110
|
-
setContentVariationCookie({
|
|
111
|
-
contentId: item.id,
|
|
112
|
-
value: randomVariationId,
|
|
113
|
-
canTrack,
|
|
114
|
-
}).catch((err) => {
|
|
115
|
-
console.error('could not store A/B test variation: ', err);
|
|
116
|
-
});
|
|
117
|
-
|
|
118
|
-
return getTestFields({ item, testGroupId: randomVariationId });
|
|
119
|
-
}
|
|
120
|
-
};
|
|
121
|
-
|
|
122
|
-
export const handleABTesting = async ({
|
|
123
|
-
item,
|
|
124
|
-
canTrack,
|
|
125
|
-
}: { item: BuilderContent } & CanTrack) => {
|
|
126
|
-
if (!checkIsBuilderContentWithVariations(item)) {
|
|
127
|
-
return;
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
const variationValue = await getContentVariation({ item, canTrack });
|
|
131
|
-
Object.assign(item, variationValue);
|
|
132
|
-
};
|