@builder.io/sdk-svelte 3.0.5 → 3.0.6

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 (84) hide show
  1. package/lib/browser/blocks/personalization-container/component-info.d.ts +2 -0
  2. package/lib/browser/blocks/personalization-container/component-info.js +36 -0
  3. package/lib/browser/blocks/personalization-container/helpers/inlined-fns.d.ts +8 -0
  4. package/lib/browser/blocks/personalization-container/helpers/inlined-fns.js +87 -0
  5. package/lib/browser/blocks/personalization-container/helpers.d.ts +27 -0
  6. package/lib/browser/blocks/personalization-container/helpers.js +58 -0
  7. package/lib/browser/blocks/personalization-container/index.d.ts +1 -0
  8. package/lib/browser/blocks/personalization-container/index.js +1 -0
  9. package/lib/browser/blocks/personalization-container/personalization-container.svelte +118 -0
  10. package/lib/browser/blocks/personalization-container/personalization-container.svelte.d.ts +21 -0
  11. package/lib/browser/blocks/personalization-container/personalization-container.types.d.ts +14 -0
  12. package/lib/browser/blocks/personalization-container/personalization-container.types.js +1 -0
  13. package/lib/browser/components/blocks/blocks-wrapper.svelte +6 -1
  14. package/lib/browser/constants/builder-registered-components.js +6 -1
  15. package/lib/browser/constants/sdk-version.d.ts +1 -1
  16. package/lib/browser/constants/sdk-version.js +1 -1
  17. package/lib/browser/helpers/canTrack.js +6 -1
  18. package/lib/browser/helpers/user-attributes.d.ts +17 -0
  19. package/lib/browser/helpers/user-attributes.js +46 -0
  20. package/lib/browser/index-helpers/blocks-exports.d.ts +1 -0
  21. package/lib/browser/index-helpers/blocks-exports.js +1 -0
  22. package/lib/browser/scripts/init-editing.js +2 -1
  23. package/lib/browser/types/input.d.ts +1 -0
  24. package/lib/edge/blocks/personalization-container/component-info.d.ts +2 -0
  25. package/lib/edge/blocks/personalization-container/component-info.js +36 -0
  26. package/lib/edge/blocks/personalization-container/helpers/inlined-fns.d.ts +8 -0
  27. package/lib/edge/blocks/personalization-container/helpers/inlined-fns.js +87 -0
  28. package/lib/edge/blocks/personalization-container/helpers.d.ts +27 -0
  29. package/lib/edge/blocks/personalization-container/helpers.js +58 -0
  30. package/lib/edge/blocks/personalization-container/index.d.ts +1 -0
  31. package/lib/edge/blocks/personalization-container/index.js +1 -0
  32. package/lib/edge/blocks/personalization-container/personalization-container.svelte +118 -0
  33. package/lib/edge/blocks/personalization-container/personalization-container.svelte.d.ts +21 -0
  34. package/lib/edge/blocks/personalization-container/personalization-container.types.d.ts +14 -0
  35. package/lib/edge/blocks/personalization-container/personalization-container.types.js +1 -0
  36. package/lib/edge/components/blocks/blocks-wrapper.svelte +6 -1
  37. package/lib/edge/constants/builder-registered-components.js +6 -1
  38. package/lib/edge/constants/sdk-version.d.ts +1 -1
  39. package/lib/edge/constants/sdk-version.js +1 -1
  40. package/lib/edge/helpers/canTrack.js +6 -1
  41. package/lib/edge/helpers/user-attributes.d.ts +17 -0
  42. package/lib/edge/helpers/user-attributes.js +46 -0
  43. package/lib/edge/index-helpers/blocks-exports.d.ts +1 -0
  44. package/lib/edge/index-helpers/blocks-exports.js +1 -0
  45. package/lib/edge/scripts/init-editing.js +2 -1
  46. package/lib/edge/types/input.d.ts +1 -0
  47. package/lib/node/blocks/personalization-container/component-info.d.ts +2 -0
  48. package/lib/node/blocks/personalization-container/component-info.js +36 -0
  49. package/lib/node/blocks/personalization-container/helpers/inlined-fns.d.ts +8 -0
  50. package/lib/node/blocks/personalization-container/helpers/inlined-fns.js +87 -0
  51. package/lib/node/blocks/personalization-container/helpers.d.ts +27 -0
  52. package/lib/node/blocks/personalization-container/helpers.js +58 -0
  53. package/lib/node/blocks/personalization-container/index.d.ts +1 -0
  54. package/lib/node/blocks/personalization-container/index.js +1 -0
  55. package/lib/node/blocks/personalization-container/personalization-container.svelte +118 -0
  56. package/lib/node/blocks/personalization-container/personalization-container.svelte.d.ts +21 -0
  57. package/lib/node/blocks/personalization-container/personalization-container.types.d.ts +14 -0
  58. package/lib/node/blocks/personalization-container/personalization-container.types.js +1 -0
  59. package/lib/node/components/blocks/blocks-wrapper.svelte +6 -1
  60. package/lib/node/constants/builder-registered-components.js +6 -1
  61. package/lib/node/constants/sdk-version.d.ts +1 -1
  62. package/lib/node/constants/sdk-version.js +1 -1
  63. package/lib/node/helpers/canTrack.js +6 -1
  64. package/lib/node/helpers/user-attributes.d.ts +17 -0
  65. package/lib/node/helpers/user-attributes.js +46 -0
  66. package/lib/node/index-helpers/blocks-exports.d.ts +1 -0
  67. package/lib/node/index-helpers/blocks-exports.js +1 -0
  68. package/lib/node/scripts/init-editing.js +2 -1
  69. package/lib/node/types/input.d.ts +1 -0
  70. package/package.json +1 -1
  71. package/src/blocks/personalization-container/component-info.ts +37 -0
  72. package/src/blocks/personalization-container/helpers/inlined-fns.ts +88 -0
  73. package/src/blocks/personalization-container/helpers.ts +84 -0
  74. package/src/blocks/personalization-container/index.ts +1 -0
  75. package/src/blocks/personalization-container/personalization-container.svelte +155 -0
  76. package/src/blocks/personalization-container/personalization-container.types.ts +14 -0
  77. package/src/components/blocks/blocks-wrapper.svelte +6 -1
  78. package/src/constants/builder-registered-components.ts +6 -1
  79. package/src/constants/sdk-version.ts +1 -1
  80. package/src/helpers/canTrack.ts +6 -1
  81. package/src/helpers/user-attributes.ts +49 -0
  82. package/src/index-helpers/blocks-exports.ts +2 -1
  83. package/src/scripts/init-editing.ts +2 -1
  84. package/src/types/input.ts +1 -0
@@ -0,0 +1,84 @@
1
+ import { TARGET } from '../../constants/target';
2
+ import { isBrowser } from '../../functions/is-browser';
3
+ import { isEditing } from '../../functions/is-editing';
4
+ import type { BuilderBlock } from '../../types/builder-block';
5
+ import { FILTER_WITH_CUSTOM_TARGETING_SCRIPT, PERSONALIZATION_SCRIPT } from './helpers/inlined-fns';
6
+ import type { PersonalizationContainerProps } from './personalization-container.types';
7
+ export type UserAttributes = {
8
+ date?: string | Date;
9
+ urlPath?: string;
10
+ [key: string]: any;
11
+ };
12
+ type QueryOperator = 'is' | 'isNot' | 'contains' | 'startsWith' | 'endsWith' | 'greaterThan' | 'lessThan' | 'greaterThanOrEqualTo' | 'lessThanOrEqualTo';
13
+ type QueryValue = string | number | boolean | Array<string | number | boolean>;
14
+ export type Query = {
15
+ property: string;
16
+ operator: QueryOperator;
17
+ value: QueryValue;
18
+ };
19
+ export function checkShouldRenderVariants(variants: PersonalizationContainerProps['variants'], canTrack: boolean) {
20
+ const hasVariants = variants && variants.length > 0;
21
+ if (TARGET === 'reactNative') return false;
22
+ if (!hasVariants) return false;
23
+ if (!canTrack) return false;
24
+ if (TARGET === 'vue' || TARGET === 'svelte') return true;
25
+ if (isBrowser()) return false;
26
+ return true;
27
+ }
28
+ export function getBlocksToRender({
29
+ variants,
30
+ previewingIndex,
31
+ isHydrated,
32
+ filteredVariants,
33
+ fallbackBlocks
34
+ }: {
35
+ variants: PersonalizationContainerProps['variants'];
36
+ previewingIndex?: number | null;
37
+ isHydrated: boolean;
38
+ filteredVariants: PersonalizationContainerProps['variants'];
39
+ fallbackBlocks?: BuilderBlock[];
40
+ }): {
41
+ blocks: BuilderBlock[];
42
+ path: string | undefined;
43
+ } {
44
+ const fallback = {
45
+ blocks: fallbackBlocks ?? [],
46
+ path: 'this.children'
47
+ };
48
+ if (isHydrated && isEditing()) {
49
+ // If editing a specific variant
50
+ if (typeof previewingIndex === 'number' && previewingIndex < (variants?.length ?? 0)) {
51
+ const variant = variants![previewingIndex];
52
+ return {
53
+ blocks: variant.blocks,
54
+ path: `component.options.variants.${previewingIndex}.blocks`
55
+ };
56
+ }
57
+ // Otherwise we're editing the default variant
58
+ return fallback;
59
+ }
60
+
61
+ // If we're on the browser, check if there's a winning variant
62
+ if (isBrowser()) {
63
+ const winningVariant = filteredVariants?.[0];
64
+ if (winningVariant) {
65
+ return {
66
+ blocks: winningVariant.blocks,
67
+ path: `component.options.variants.${variants?.indexOf(winningVariant)}.blocks`
68
+ };
69
+ }
70
+ }
71
+
72
+ // If no winning variant or we are on the server, return the default variant
73
+ return fallback;
74
+ }
75
+ export const getPersonalizationScript = (variants: PersonalizationContainerProps['variants'], blockId: string, locale?: string) => {
76
+ return `
77
+ (function() {
78
+ ${FILTER_WITH_CUSTOM_TARGETING_SCRIPT}
79
+ ${PERSONALIZATION_SCRIPT}
80
+ getPersonalizedVariant(${JSON.stringify(variants)}, "${blockId}"${locale ? `, "${locale}"` : ''})
81
+ })();
82
+ `;
83
+ };
84
+ export { filterWithCustomTargeting } from './helpers/inlined-fns'
@@ -0,0 +1 @@
1
+ export { default } from './personalization-container.svelte'
@@ -0,0 +1,155 @@
1
+ <script lang="ts">
2
+ import { onDestroy, onMount } from "svelte";
3
+
4
+ import Blocks from "../../components/blocks/blocks.svelte";
5
+ import InlinedScript from "../../components/inlined-script.svelte";
6
+ import InlinedStyles from "../../components/inlined-styles.svelte";
7
+ import { isEditing } from "../../functions/is-editing";
8
+ import { isPreviewing } from "../../functions/is-previewing";
9
+ import { getDefaultCanTrack } from "../../helpers/canTrack";
10
+ import { userAttributesService } from "../../helpers/user-attributes";
11
+ import {
12
+ checkShouldRenderVariants,
13
+ filterWithCustomTargeting,
14
+ getBlocksToRender,
15
+ getPersonalizationScript,
16
+ } from "./helpers";
17
+ import type { PersonalizationContainerProps } from "./personalization-container.types";
18
+
19
+ export let builderContext: PersonalizationContainerProps["builderContext"];
20
+ export let variants: PersonalizationContainerProps["variants"] = undefined;
21
+ export let builderBlock: PersonalizationContainerProps["builderBlock"];
22
+ export let previewingIndex: PersonalizationContainerProps["previewingIndex"] =
23
+ undefined;
24
+ export let attributes: PersonalizationContainerProps["attributes"] =
25
+ undefined;
26
+
27
+ $: filteredVariants = () => {
28
+ return (variants || []).filter((variant) => {
29
+ return filterWithCustomTargeting(
30
+ {
31
+ ...($builderContext?.rootState?.locale
32
+ ? {
33
+ locale: $builderContext?.rootState?.locale,
34
+ }
35
+ : {}),
36
+ ...(userAttributes as any),
37
+ },
38
+ variant.query,
39
+ variant.startDate,
40
+ variant.endDate
41
+ );
42
+ });
43
+ };
44
+ $: blocksToRender = () => {
45
+ return getBlocksToRender({
46
+ variants: variants,
47
+ fallbackBlocks: builderBlock?.children,
48
+ isHydrated: isHydrated,
49
+ filteredVariants: filteredVariants(),
50
+ previewingIndex: previewingIndex,
51
+ });
52
+ };
53
+ $: hideVariantsStyleString = () => {
54
+ return (variants || [])
55
+ .map(
56
+ (_, index) =>
57
+ `[data-variant-id="${builderBlock?.id}-${index}"] { display: none; } `
58
+ )
59
+ .join("");
60
+ };
61
+
62
+ let rootRef;
63
+
64
+ let userAttributes = userAttributesService.getUserAttributes();
65
+ let scriptStr = getPersonalizationScript(
66
+ variants,
67
+ builderBlock?.id || "none",
68
+ $builderContext?.rootState?.locale as string | undefined
69
+ );
70
+ let unsubscribers = [];
71
+ let shouldRenderVariants = checkShouldRenderVariants(
72
+ variants,
73
+ getDefaultCanTrack($builderContext?.canTrack)
74
+ );
75
+ let isHydrated = false;
76
+
77
+ onMount(() => {
78
+ isHydrated = true;
79
+ const unsub = userAttributesService.subscribeOnUserAttributesChange(
80
+ (attrs) => {
81
+ userAttributes = attrs;
82
+ }
83
+ );
84
+ if (!(isEditing() || isPreviewing())) {
85
+ const variant = filteredVariants()[0];
86
+ if (rootRef) {
87
+ rootRef.dispatchEvent(
88
+ new CustomEvent("builder.variantLoaded", {
89
+ detail: {
90
+ variant: variant || "default",
91
+ content: $builderContext?.content,
92
+ },
93
+ bubbles: true,
94
+ })
95
+ );
96
+ const observer = new IntersectionObserver((entries) => {
97
+ entries.forEach((entry) => {
98
+ if (entry.isIntersecting && rootRef) {
99
+ rootRef.dispatchEvent(
100
+ new CustomEvent("builder.variantDisplayed", {
101
+ detail: {
102
+ variant: variant || "default",
103
+ content: $builderContext?.content,
104
+ },
105
+ bubbles: true,
106
+ })
107
+ );
108
+ }
109
+ });
110
+ });
111
+ observer.observe(rootRef);
112
+ }
113
+ }
114
+ unsubscribers.push(unsub);
115
+ });
116
+
117
+ onDestroy(() => {
118
+ unsubscribers.forEach((unsub) => unsub());
119
+ });
120
+ </script>
121
+
122
+ <div
123
+ bind:this={rootRef}
124
+ {...attributes}
125
+ class={`builder-personalization-container ${attributes?.className || ""}`}
126
+ >
127
+ {#if shouldRenderVariants}
128
+ {#each variants as variant, index (index)}
129
+ <svelte:element
130
+ this={"template"}
131
+ data-variant-id={`${builderBlock?.id}-${index}`}
132
+ ><Blocks
133
+ blocks={variant.blocks}
134
+ parent={builderBlock?.id}
135
+ path={`component.options.variants.${index}.blocks`}
136
+ /></svelte:element
137
+ >
138
+ {/each}
139
+
140
+ <InlinedStyles
141
+ nonce={$builderContext?.nonce || ""}
142
+ styles={hideVariantsStyleString()}
143
+ id={`variants-styles-${builderBlock?.id}`}
144
+ />
145
+ <InlinedScript
146
+ nonce={$builderContext?.nonce || ""}
147
+ {scriptStr}
148
+ id={`variants-script-${builderBlock?.id}`}
149
+ />
150
+ {/if}<Blocks
151
+ blocks={blocksToRender().blocks}
152
+ parent={builderBlock?.id}
153
+ path={blocksToRender().path}
154
+ />
155
+ </div>
@@ -0,0 +1,14 @@
1
+ import type { BuilderBlock } from '../../types/builder-block';
2
+ import type { BuilderDataProps } from '../../types/builder-props';
3
+ import type { Query } from './helpers';
4
+ export type PersonalizationContainerProps = {
5
+ children?: any;
6
+ attributes?: any;
7
+ previewingIndex?: number | null;
8
+ variants?: Array<{
9
+ blocks: BuilderBlock[];
10
+ query: Query[];
11
+ startDate?: string;
12
+ endDate?: string;
13
+ }>;
14
+ } & BuilderDataProps
@@ -79,8 +79,13 @@
79
79
  if (!path) {
80
80
  return undefined;
81
81
  }
82
+ const thisPrefix = "this.";
82
83
  const pathPrefix = "component.options.";
83
- return path.startsWith(pathPrefix) ? path : `${pathPrefix}${path || ""}`;
84
+ return path.startsWith(thisPrefix)
85
+ ? path.replace(thisPrefix, "")
86
+ : path.startsWith(pathPrefix)
87
+ ? path
88
+ : `${pathPrefix}${path || ""}`;
84
89
  };
85
90
 
86
91
  let blocksWrapperRef;
@@ -8,6 +8,8 @@ import { componentInfo as fragmentComponentInfo } from '../blocks/fragment/compo
8
8
  import { default as Fragment } from '../blocks/fragment/index';
9
9
  import { componentInfo as imageComponentInfo } from '../blocks/image/component-info';
10
10
  import { default as Image } from '../blocks/image/index';
11
+ import { componentInfo as personalizationContainerComponentInfo } from '../blocks/personalization-container/component-info';
12
+ import { default as PersonalizationContainer } from '../blocks/personalization-container/index';
11
13
  import { componentInfo as sectionComponentInfo } from '../blocks/section/component-info';
12
14
  import { default as Section } from '../blocks/section/index';
13
15
  import { componentInfo as slotComponentInfo } from '../blocks/slot/component-info';
@@ -50,7 +52,10 @@ export const getDefaultRegisteredComponents: () => RegisteredComponent[] = () =>
50
52
  }, {
51
53
  component: Text,
52
54
  ...textComponentInfo
53
- }, ...(TARGET === 'rsc' ? [] : [{
55
+ }, ...(TARGET === 'react' ? [{
56
+ component: PersonalizationContainer,
57
+ ...personalizationContainerComponentInfo
58
+ }] : []), ...(TARGET === 'rsc' ? [] : [{
54
59
  component: Tabs,
55
60
  ...tabsComponentInfo
56
61
  }, {
@@ -1 +1 @@
1
- export const SDK_VERSION = "3.0.5"
1
+ export const SDK_VERSION = "3.0.6"
@@ -1,2 +1,7 @@
1
1
  import { checkIsDefined } from './nullable';
2
- export const getDefaultCanTrack = (canTrack?: boolean) => checkIsDefined(canTrack) ? canTrack : true
2
+ import { userAttributesService } from './user-attributes';
3
+ export const getDefaultCanTrack = (canTrack?: boolean) => {
4
+ const result = checkIsDefined(canTrack) ? canTrack : true;
5
+ userAttributesService.setCanTrack(result);
6
+ return result;
7
+ }
@@ -0,0 +1,49 @@
1
+ import { isBrowser } from '../functions/is-browser';
2
+ import { getCookieSync, setCookie } from './cookie';
3
+ export interface UserAttributes {
4
+ [key: string]: any;
5
+ }
6
+ export const USER_ATTRIBUTES_COOKIE_NAME = 'builder.userAttributes';
7
+ export function createUserAttributesService() {
8
+ let canTrack = true;
9
+ const subscribers = new Set<(attrs: UserAttributes) => void>();
10
+ return {
11
+ setUserAttributes(newAttrs: UserAttributes) {
12
+ if (!isBrowser()) {
13
+ return;
14
+ }
15
+ const userAttributes: UserAttributes = {
16
+ ...this.getUserAttributes(),
17
+ ...newAttrs
18
+ };
19
+ setCookie({
20
+ name: USER_ATTRIBUTES_COOKIE_NAME,
21
+ value: JSON.stringify(userAttributes),
22
+ canTrack
23
+ });
24
+ subscribers.forEach(callback => callback(userAttributes));
25
+ },
26
+ getUserAttributes() {
27
+ if (!isBrowser()) {
28
+ return {};
29
+ }
30
+ return JSON.parse(getCookieSync({
31
+ name: USER_ATTRIBUTES_COOKIE_NAME,
32
+ canTrack
33
+ }) || '{}');
34
+ },
35
+ subscribeOnUserAttributesChange(callback: (attrs: UserAttributes) => void) {
36
+ subscribers.add(callback);
37
+ return () => {
38
+ subscribers.delete(callback);
39
+ };
40
+ },
41
+ setCanTrack(value: boolean) {
42
+ canTrack = value;
43
+ }
44
+ };
45
+ }
46
+ export const userAttributesService = createUserAttributesService();
47
+ export const setClientUserAttributes = (attributes: UserAttributes) => {
48
+ userAttributesService.setUserAttributes(attributes);
49
+ }
@@ -12,4 +12,5 @@ export { default as Content } from '../components/content-variants/index';
12
12
  /**
13
13
  * Builder Context
14
14
  */
15
- export { BuilderContext } from '../context/index'
15
+ export { BuilderContext } from '../context/index';
16
+ export { setClientUserAttributes } from '../helpers/user-attributes'
@@ -47,7 +47,8 @@ export const setupBrowserForEditing = (options: {
47
47
  // Supports builder-model="..." attribute which is needed to
48
48
  // scope our '+ add block' button styling
49
49
  supportsAddBlockScoping: true,
50
- supportsCustomBreakpoints: true
50
+ supportsCustomBreakpoints: true,
51
+ blockLevelPersonalization: true
51
52
  }
52
53
  }, '*');
53
54
  window.parent?.postMessage({
@@ -125,6 +125,7 @@ export interface Input {
125
125
  * Use optionally with inputs of type `reference`. Restricts the content entry picker to a specific model by name.
126
126
  */
127
127
  model?: string;
128
+ behavior?: string;
128
129
  valueType?: {
129
130
  type?: string;
130
131
  };