@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.
Files changed (94) hide show
  1. package/package/--package.json +10 -5
  2. package/package/CHANGELOG.md +17 -0
  3. package/package/blocks/columns/columns.svelte +1 -1
  4. package/package/blocks/symbol/symbol.svelte +1 -1
  5. package/package/components/render-block/block-styles.svelte +3 -1
  6. package/package/components/render-block/render-block.helpers.d.ts +0 -1
  7. package/package/components/render-block/render-block.helpers.js +8 -20
  8. package/package/components/render-block/render-block.svelte +25 -19
  9. package/package/components/render-block/render-repeated-block.svelte +3 -2
  10. package/package/components/render-content/components/render-styles.svelte +1 -1
  11. package/package/components/render-content/render-content.svelte +99 -10
  12. package/package/components/render-content/render-content.svelte.d.ts +5 -0
  13. package/package/components/render-content/render-content.types.d.ts +11 -2
  14. package/package/components/render-content/wrap-component-ref.d.ts +6 -0
  15. package/package/components/render-content/wrap-component-ref.js +6 -0
  16. package/package/components/render-content-variants/helpers.d.ts +17 -0
  17. package/package/components/render-content-variants/helpers.js +182 -0
  18. package/package/components/render-content-variants/render-content-variants.svelte +70 -0
  19. package/package/components/render-content-variants/render-content-variants.svelte.d.ts +23 -0
  20. package/package/components/render-inlined-styles.svelte +3 -15
  21. package/package/components/render-inlined-styles.svelte.d.ts +2 -0
  22. package/package/constants/sdk-version.d.ts +1 -0
  23. package/package/constants/sdk-version.js +1 -0
  24. package/package/context/builder.context.d.ts +3 -2
  25. package/package/context/builder.context.js +3 -2
  26. package/package/context/types.d.ts +17 -2
  27. package/package/functions/evaluate.d.ts +4 -3
  28. package/package/functions/evaluate.js +23 -2
  29. package/package/functions/evaluate.test.d.ts +1 -0
  30. package/package/functions/evaluate.test.js +17 -0
  31. package/package/functions/get-block-actions-handler.d.ts +1 -1
  32. package/package/functions/get-block-actions-handler.js +3 -1
  33. package/package/functions/get-block-actions.d.ts +1 -1
  34. package/package/functions/get-content/generate-content-url.js +2 -2
  35. package/package/functions/get-content/generate-content-url.test.js +15 -0
  36. package/package/functions/get-content/index.d.ts +7 -2
  37. package/package/functions/get-content/index.js +43 -20
  38. package/package/functions/get-content/types.d.ts +6 -0
  39. package/package/functions/get-processed-block.d.ts +2 -2
  40. package/package/functions/get-processed-block.js +16 -4
  41. package/package/functions/get-processed-block.test.js +3 -1
  42. package/package/helpers/ab-tests.d.ts +8 -7
  43. package/package/helpers/ab-tests.js +103 -4
  44. package/package/helpers/canTrack.d.ts +1 -0
  45. package/package/helpers/canTrack.js +2 -0
  46. package/package/helpers/cookie.d.ts +7 -3
  47. package/package/helpers/cookie.js +9 -6
  48. package/package/helpers/logger.d.ts +1 -0
  49. package/package/helpers/logger.js +1 -0
  50. package/package/index-helpers/blocks-exports.d.ts +1 -1
  51. package/package/index-helpers/blocks-exports.js +1 -1
  52. package/package/index.d.ts +8 -7
  53. package/package/index.js +6 -7
  54. package/package/scripts/init-editing.d.ts +1 -0
  55. package/package/scripts/init-editing.js +2 -0
  56. package/package/types/builder-content.d.ts +1 -3
  57. package/package.json +4 -8
  58. package/src/blocks/columns/columns.svelte +1 -5
  59. package/src/blocks/symbol/symbol.svelte +1 -1
  60. package/src/components/render-block/block-styles.svelte +3 -1
  61. package/src/components/render-block/render-block.helpers.ts +9 -31
  62. package/src/components/render-block/render-block.svelte +24 -19
  63. package/src/components/render-block/render-repeated-block.svelte +3 -2
  64. package/src/components/render-content/components/render-styles.svelte +1 -1
  65. package/src/components/render-content/render-content.svelte +101 -10
  66. package/src/components/render-content/render-content.types.ts +11 -2
  67. package/src/components/render-content/wrap-component-ref.ts +6 -0
  68. package/src/components/render-content-variants/helpers.ts +245 -0
  69. package/src/components/render-content-variants/render-content-variants.svelte +83 -0
  70. package/src/components/render-inlined-styles.svelte +3 -16
  71. package/src/constants/sdk-version.ts +1 -0
  72. package/src/context/builder.context.ts +3 -2
  73. package/src/context/types.ts +17 -2
  74. package/src/functions/evaluate.test.ts +21 -0
  75. package/src/functions/evaluate.ts +46 -4
  76. package/src/functions/get-block-actions-handler.ts +7 -2
  77. package/src/functions/get-block-actions.ts +4 -1
  78. package/src/functions/get-content/generate-content-url.test.ts +17 -0
  79. package/src/functions/get-content/generate-content-url.ts +2 -1
  80. package/src/functions/get-content/index.ts +57 -26
  81. package/src/functions/get-content/types.ts +7 -0
  82. package/src/functions/get-processed-block.test.ts +3 -1
  83. package/src/functions/get-processed-block.ts +28 -6
  84. package/src/helpers/ab-tests.ts +164 -11
  85. package/src/helpers/canTrack.ts +4 -0
  86. package/src/helpers/cookie.ts +16 -11
  87. package/src/helpers/logger.ts +1 -0
  88. package/src/index-helpers/blocks-exports.ts +1 -1
  89. package/src/index.ts +17 -8
  90. package/src/scripts/init-editing.ts +3 -0
  91. package/src/types/builder-content.ts +1 -3
  92. package/package/functions/get-content/ab-testing.d.ts +0 -5
  93. package/package/functions/get-content/ab-testing.js +0 -78
  94. 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 | undefined;
40
+ [id: string]: BuilderContentVariation;
43
41
  };
44
42
  testVariationId?: string;
45
43
  testVariationName?: string;
@@ -1,5 +0,0 @@
1
- import type { BuilderContent } from '../../types/builder-content.js';
2
- import type { CanTrack } from '../../types/can-track.js';
3
- export declare const handleABTesting: ({ item, canTrack, }: {
4
- item: BuilderContent;
5
- } & CanTrack) => Promise<void>;
@@ -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
- };