@builder.io/sdk-react-nextjs 0.4.5

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 (244) hide show
  1. package/README.md +80 -0
  2. package/dist/blocks/button/button.d.ts +11 -0
  3. package/dist/blocks/button/button.js +12 -0
  4. package/dist/blocks/button/component-info.d.ts +2 -0
  5. package/dist/blocks/button/component-info.js +34 -0
  6. package/dist/blocks/columns/columns.d.ts +16 -0
  7. package/dist/blocks/columns/columns.js +119 -0
  8. package/dist/blocks/columns/component-info.d.ts +2 -0
  9. package/dist/blocks/columns/component-info.js +219 -0
  10. package/dist/blocks/custom-code/component-info.d.ts +2 -0
  11. package/dist/blocks/custom-code/component-info.js +23 -0
  12. package/dist/blocks/custom-code/custom-code.d.ts +6 -0
  13. package/dist/blocks/custom-code/custom-code.js +51 -0
  14. package/dist/blocks/embed/component-info.d.ts +2 -0
  15. package/dist/blocks/embed/component-info.js +38 -0
  16. package/dist/blocks/embed/embed.d.ts +5 -0
  17. package/dist/blocks/embed/embed.js +42 -0
  18. package/dist/blocks/embed/helpers.d.ts +1 -0
  19. package/dist/blocks/embed/helpers.js +2 -0
  20. package/dist/blocks/fragment/component-info.d.ts +2 -0
  21. package/dist/blocks/fragment/component-info.js +7 -0
  22. package/dist/blocks/fragment/fragment.d.ts +7 -0
  23. package/dist/blocks/fragment/fragment.js +6 -0
  24. package/dist/blocks/helpers.d.ts +13 -0
  25. package/dist/blocks/helpers.js +40 -0
  26. package/dist/blocks/image/component-info.d.ts +2 -0
  27. package/dist/blocks/image/component-info.js +121 -0
  28. package/dist/blocks/image/image.d.ts +21 -0
  29. package/dist/blocks/image/image.helpers.d.ts +1 -0
  30. package/dist/blocks/image/image.helpers.js +47 -0
  31. package/dist/blocks/image/image.js +81 -0
  32. package/dist/blocks/img/component-info.d.ts +2 -0
  33. package/dist/blocks/img/component-info.js +15 -0
  34. package/dist/blocks/img/img.d.ts +13 -0
  35. package/dist/blocks/img/img.js +10 -0
  36. package/dist/blocks/section/component-info.d.ts +2 -0
  37. package/dist/blocks/section/component-info.js +40 -0
  38. package/dist/blocks/section/section.d.ts +11 -0
  39. package/dist/blocks/section/section.js +17 -0
  40. package/dist/blocks/symbol/component-info.d.ts +2 -0
  41. package/dist/blocks/symbol/component-info.js +34 -0
  42. package/dist/blocks/symbol/symbol.d.ts +25 -0
  43. package/dist/blocks/symbol/symbol.helpers.d.ts +14 -0
  44. package/dist/blocks/symbol/symbol.helpers.js +30 -0
  45. package/dist/blocks/symbol/symbol.js +29 -0
  46. package/dist/blocks/text/component-info.d.ts +2 -0
  47. package/dist/blocks/text/component-info.js +19 -0
  48. package/dist/blocks/text/text.d.ts +5 -0
  49. package/dist/blocks/text/text.js +7 -0
  50. package/dist/blocks/video/component-info.d.ts +2 -0
  51. package/dist/blocks/video/component-info.js +82 -0
  52. package/dist/blocks/video/video.d.ts +19 -0
  53. package/dist/blocks/video/video.js +47 -0
  54. package/dist/components/block/block.d.ts +9 -0
  55. package/dist/components/block/block.helpers.d.ts +13 -0
  56. package/dist/components/block/block.helpers.js +67 -0
  57. package/dist/components/block/block.js +92 -0
  58. package/dist/components/block/components/block-styles.d.ts +8 -0
  59. package/dist/components/block/components/block-styles.js +65 -0
  60. package/dist/components/block/components/block-wrapper.d.ts +17 -0
  61. package/dist/components/block/components/block-wrapper.js +36 -0
  62. package/dist/components/block/components/component-ref/component-ref.d.ts +3 -0
  63. package/dist/components/block/components/component-ref/component-ref.helpers.d.ts +28 -0
  64. package/dist/components/block/components/component-ref/component-ref.helpers.js +21 -0
  65. package/dist/components/block/components/component-ref/component-ref.js +21 -0
  66. package/dist/components/block/components/interactive-element.d.ts +11 -0
  67. package/dist/components/block/components/interactive-element.js +20 -0
  68. package/dist/components/block/components/repeated-block.d.ts +9 -0
  69. package/dist/components/block/components/repeated-block.js +7 -0
  70. package/dist/components/block/types.d.ts +6 -0
  71. package/dist/components/block/types.js +1 -0
  72. package/dist/components/blocks/blocks-wrapper.d.ts +10 -0
  73. package/dist/components/blocks/blocks-wrapper.js +38 -0
  74. package/dist/components/blocks/blocks.d.ts +8 -0
  75. package/dist/components/blocks/blocks.js +10 -0
  76. package/dist/components/content/components/enable-editor.d.ts +9 -0
  77. package/dist/components/content/components/enable-editor.js +249 -0
  78. package/dist/components/content/components/styles.d.ts +8 -0
  79. package/dist/components/content/components/styles.helpers.d.ts +15 -0
  80. package/dist/components/content/components/styles.helpers.js +56 -0
  81. package/dist/components/content/components/styles.js +30 -0
  82. package/dist/components/content/content.d.ts +3 -0
  83. package/dist/components/content/content.helpers.d.ts +7 -0
  84. package/dist/components/content/content.helpers.js +25 -0
  85. package/dist/components/content/content.js +72 -0
  86. package/dist/components/content/content.types.d.ts +24 -0
  87. package/dist/components/content/content.types.js +1 -0
  88. package/dist/components/content/wrap-component-ref.d.ts +6 -0
  89. package/dist/components/content/wrap-component-ref.js +6 -0
  90. package/dist/components/content-variants/content-variants.d.ts +9 -0
  91. package/dist/components/content-variants/content-variants.js +39 -0
  92. package/dist/components/content-variants/content-variants.types.d.ts +20 -0
  93. package/dist/components/content-variants/content-variants.types.js +1 -0
  94. package/dist/components/content-variants/helpers.d.ts +41 -0
  95. package/dist/components/content-variants/helpers.js +188 -0
  96. package/dist/components/inlined-script.d.ts +6 -0
  97. package/dist/components/inlined-script.js +5 -0
  98. package/dist/components/inlined-styles.d.ts +6 -0
  99. package/dist/components/inlined-styles.js +5 -0
  100. package/dist/constants/builder-registered-components.d.ts +6 -0
  101. package/dist/constants/builder-registered-components.js +60 -0
  102. package/dist/constants/device-sizes.d.ts +13 -0
  103. package/dist/constants/device-sizes.js +45 -0
  104. package/dist/constants/sdk-version.d.ts +1 -0
  105. package/dist/constants/sdk-version.js +1 -0
  106. package/dist/constants/target.d.ts +2 -0
  107. package/dist/constants/target.js +2 -0
  108. package/dist/context/builder.context.d.ts +3 -0
  109. package/dist/context/builder.context.js +12 -0
  110. package/dist/context/types.d.ts +35 -0
  111. package/dist/context/types.js +1 -0
  112. package/dist/functions/apply-patch-with-mutation.d.ts +10 -0
  113. package/dist/functions/apply-patch-with-mutation.js +54 -0
  114. package/dist/functions/camel-to-kebab-case.d.ts +1 -0
  115. package/dist/functions/camel-to-kebab-case.js +1 -0
  116. package/dist/functions/evaluate.d.ts +7 -0
  117. package/dist/functions/evaluate.js +46 -0
  118. package/dist/functions/event-handler-name.d.ts +1 -0
  119. package/dist/functions/event-handler-name.js +4 -0
  120. package/dist/functions/extract-text-styles.d.ts +4 -0
  121. package/dist/functions/extract-text-styles.js +21 -0
  122. package/dist/functions/fast-clone.d.ts +4 -0
  123. package/dist/functions/fast-clone.js +4 -0
  124. package/dist/functions/get-block-actions-handler.d.ts +8 -0
  125. package/dist/functions/get-block-actions-handler.js +10 -0
  126. package/dist/functions/get-block-actions.d.ts +10 -0
  127. package/dist/functions/get-block-actions.js +28 -0
  128. package/dist/functions/get-block-component-options.d.ts +2 -0
  129. package/dist/functions/get-block-component-options.js +8 -0
  130. package/dist/functions/get-block-properties.d.ts +6 -0
  131. package/dist/functions/get-block-properties.js +71 -0
  132. package/dist/functions/get-builder-search-params/index.d.ts +12 -0
  133. package/dist/functions/get-builder-search-params/index.js +38 -0
  134. package/dist/functions/get-content/generate-content-url.d.ts +2 -0
  135. package/dist/functions/get-content/generate-content-url.js +32 -0
  136. package/dist/functions/get-content/index.d.ts +16 -0
  137. package/dist/functions/get-content/index.js +82 -0
  138. package/dist/functions/get-content/processCookies.d.ts +2 -0
  139. package/dist/functions/get-content/processCookies.js +35 -0
  140. package/dist/functions/get-content/types.d.ts +51 -0
  141. package/dist/functions/get-content/types.js +1 -0
  142. package/dist/functions/get-fetch.d.ts +1 -0
  143. package/dist/functions/get-fetch.js +11 -0
  144. package/dist/functions/get-global-this.d.ts +4 -0
  145. package/dist/functions/get-global-this.js +15 -0
  146. package/dist/functions/get-processed-block.d.ts +10 -0
  147. package/dist/functions/get-processed-block.js +43 -0
  148. package/dist/functions/get-react-native-block-styles.d.ts +7 -0
  149. package/dist/functions/get-react-native-block-styles.js +16 -0
  150. package/dist/functions/is-browser.d.ts +1 -0
  151. package/dist/functions/is-browser.js +3 -0
  152. package/dist/functions/is-editing.d.ts +1 -0
  153. package/dist/functions/is-editing.js +5 -0
  154. package/dist/functions/is-iframe.d.ts +1 -0
  155. package/dist/functions/is-iframe.js +4 -0
  156. package/dist/functions/is-previewing.d.ts +1 -0
  157. package/dist/functions/is-previewing.js +11 -0
  158. package/dist/functions/register-component.d.ts +15 -0
  159. package/dist/functions/register-component.js +34 -0
  160. package/dist/functions/register.d.ts +16 -0
  161. package/dist/functions/register.js +27 -0
  162. package/dist/functions/sanitize-react-native-block-styles.d.ts +3 -0
  163. package/dist/functions/sanitize-react-native-block-styles.js +58 -0
  164. package/dist/functions/set-editor-settings.d.ts +4 -0
  165. package/dist/functions/set-editor-settings.js +12 -0
  166. package/dist/functions/set.d.ts +7 -0
  167. package/dist/functions/set.js +14 -0
  168. package/dist/functions/track/helpers.d.ts +5 -0
  169. package/dist/functions/track/helpers.js +50 -0
  170. package/dist/functions/track/index.d.ts +51 -0
  171. package/dist/functions/track/index.js +69 -0
  172. package/dist/functions/track/interaction.d.ts +13 -0
  173. package/dist/functions/track/interaction.js +53 -0
  174. package/dist/functions/transform-block-properties.d.ts +1 -0
  175. package/dist/functions/transform-block-properties.js +5 -0
  176. package/dist/functions/transform-block.d.ts +2 -0
  177. package/dist/functions/transform-block.js +3 -0
  178. package/dist/helpers/ab-tests.d.ts +9 -0
  179. package/dist/helpers/ab-tests.js +116 -0
  180. package/dist/helpers/canTrack.d.ts +1 -0
  181. package/dist/helpers/canTrack.js +2 -0
  182. package/dist/helpers/cookie.d.ts +19 -0
  183. package/dist/helpers/cookie.js +54 -0
  184. package/dist/helpers/css.d.ts +7 -0
  185. package/dist/helpers/css.js +27 -0
  186. package/dist/helpers/flatten.d.ts +6 -0
  187. package/dist/helpers/flatten.js +17 -0
  188. package/dist/helpers/localStorage.d.ts +9 -0
  189. package/dist/helpers/localStorage.js +24 -0
  190. package/dist/helpers/logger.d.ts +6 -0
  191. package/dist/helpers/logger.js +7 -0
  192. package/dist/helpers/nullable.d.ts +2 -0
  193. package/dist/helpers/nullable.js +1 -0
  194. package/dist/helpers/preview-lru-cache/get.d.ts +1 -0
  195. package/dist/helpers/preview-lru-cache/get.js +7 -0
  196. package/dist/helpers/preview-lru-cache/helpers.d.ts +1 -0
  197. package/dist/helpers/preview-lru-cache/helpers.js +9 -0
  198. package/dist/helpers/preview-lru-cache/init.d.ts +1 -0
  199. package/dist/helpers/preview-lru-cache/init.js +15 -0
  200. package/dist/helpers/preview-lru-cache/set.d.ts +7 -0
  201. package/dist/helpers/preview-lru-cache/set.js +9 -0
  202. package/dist/helpers/preview-lru-cache/types.d.ts +12 -0
  203. package/dist/helpers/preview-lru-cache/types.js +1 -0
  204. package/dist/helpers/sessionId.d.ts +6 -0
  205. package/dist/helpers/sessionId.js +30 -0
  206. package/dist/helpers/url.d.ts +6 -0
  207. package/dist/helpers/url.js +15 -0
  208. package/dist/helpers/uuid.d.ts +8 -0
  209. package/dist/helpers/uuid.js +15 -0
  210. package/dist/helpers/visitorId.d.ts +6 -0
  211. package/dist/helpers/visitorId.js +30 -0
  212. package/dist/index-helpers/blocks-exports.d.ts +10 -0
  213. package/dist/index-helpers/blocks-exports.js +10 -0
  214. package/dist/index-helpers/top-of-file.d.ts +1 -0
  215. package/dist/index-helpers/top-of-file.js +4 -0
  216. package/dist/index.d.ts +15 -0
  217. package/dist/index.js +10 -0
  218. package/dist/scripts/init-editing.d.ts +6 -0
  219. package/dist/scripts/init-editing.js +109 -0
  220. package/dist/types/api-version.d.ts +2 -0
  221. package/dist/types/api-version.js +1 -0
  222. package/dist/types/builder-block.d.ts +66 -0
  223. package/dist/types/builder-block.js +1 -0
  224. package/dist/types/builder-content.d.ts +44 -0
  225. package/dist/types/builder-content.js +1 -0
  226. package/dist/types/builder-props.d.ts +9 -0
  227. package/dist/types/builder-props.js +1 -0
  228. package/dist/types/can-track.d.ts +3 -0
  229. package/dist/types/can-track.js +1 -0
  230. package/dist/types/components.d.ts +111 -0
  231. package/dist/types/components.js +1 -0
  232. package/dist/types/deep-partial.d.ts +3 -0
  233. package/dist/types/deep-partial.js +1 -0
  234. package/dist/types/element.d.ts +59 -0
  235. package/dist/types/element.js +1 -0
  236. package/dist/types/enforced-partials.d.ts +21 -0
  237. package/dist/types/enforced-partials.js +1 -0
  238. package/dist/types/input.d.ts +121 -0
  239. package/dist/types/input.js +1 -0
  240. package/dist/types/targets.d.ts +1 -0
  241. package/dist/types/targets.js +1 -0
  242. package/dist/types/typescript.d.ts +11 -0
  243. package/dist/types/typescript.js +1 -0
  244. package/package.json +27 -0
@@ -0,0 +1,15 @@
1
+ import type { RegisteredComponent } from '../context/types.js';
2
+ import type { ComponentInfo } from '../types/components.js';
3
+ /**
4
+ * @deprecated. Use the `customComponents` prop in RenderContent instead to provide your custom components to the builder SDK.
5
+ */
6
+ export declare const components: RegisteredComponent[];
7
+ /**
8
+ * @deprecated. Use the `customComponents` prop in RenderContent instead to provide your custom components to the builder SDK.
9
+ */
10
+ export declare function registerComponent(component: any, info: ComponentInfo): void;
11
+ export declare const createRegisterComponentMessage: (info: ComponentInfo) => {
12
+ type: string;
13
+ data: ComponentInfo;
14
+ };
15
+ export declare const serializeComponentInfo: ({ inputs, ...info }: ComponentInfo) => ComponentInfo;
@@ -0,0 +1,34 @@
1
+ import { fastClone } from './fast-clone.js';
2
+ /**
3
+ * @deprecated. Use the `customComponents` prop in RenderContent instead to provide your custom components to the builder SDK.
4
+ */
5
+ export const components = [];
6
+ /**
7
+ * @deprecated. Use the `customComponents` prop in RenderContent instead to provide your custom components to the builder SDK.
8
+ */
9
+ export function registerComponent(component, info) {
10
+ components.push({
11
+ component,
12
+ ...info
13
+ });
14
+ console.warn('registerComponent is deprecated. Use the `customComponents` prop in RenderContent instead to provide your custom components to the builder SDK.');
15
+ return component;
16
+ }
17
+ export const createRegisterComponentMessage = (info) => ({
18
+ type: 'builder.registerComponent',
19
+ data: info
20
+ }); // eslint-disable-next-line @typescript-eslint/ban-types
21
+ const serializeFn = (fnValue) => {
22
+ const fnStr = fnValue.toString().trim(); // we need to account for a few different fn syntaxes:
23
+ // 1. `function name(args) => {code}`
24
+ // 2. `name(args) => {code}`
25
+ // 3. `(args) => {}`
26
+ const appendFunction = !fnStr.startsWith('function') && !fnStr.startsWith('(');
27
+ return `return (${appendFunction ? 'function ' : ''}${fnStr}).apply(this, arguments)`;
28
+ };
29
+ const serializeValue = (value) => typeof value === 'function' ? serializeFn(value) : fastClone(value);
30
+ export const serializeComponentInfo = ({ inputs, ...info }) => ({ ...fastClone(info),
31
+ inputs: inputs?.map(input => Object.entries(input).reduce((acc, [key, value]) => ({ ...acc,
32
+ [key]: serializeValue(value)
33
+ }), {}))
34
+ });
@@ -0,0 +1,16 @@
1
+ import type { BuilderBlock } from '../types/builder-block.js';
2
+ import type { DeepPartial } from '../types/deep-partial.js';
3
+ export interface InsertMenuItem {
4
+ name: string;
5
+ icon?: string;
6
+ item: DeepPartial<BuilderBlock>;
7
+ }
8
+ export interface InsertMenuConfig {
9
+ name: string;
10
+ priority?: number;
11
+ persist?: boolean;
12
+ advanced?: boolean;
13
+ items: InsertMenuItem[];
14
+ }
15
+ export declare function register(type: 'insertMenu', info: InsertMenuConfig): void;
16
+ export declare function register(type: string, info: any): void;
@@ -0,0 +1,27 @@
1
+ import { isBrowser } from './is-browser.js';
2
+ const registry = {};
3
+ export function register(type, info) {
4
+ let typeList = registry[type];
5
+ if (!typeList) {
6
+ typeList = registry[type] = [];
7
+ }
8
+ typeList.push(info);
9
+ if (isBrowser()) {
10
+ const message = {
11
+ type: 'builder.register',
12
+ data: {
13
+ type,
14
+ info
15
+ }
16
+ };
17
+ try {
18
+ parent.postMessage(message, '*');
19
+ if (parent !== window) {
20
+ window.postMessage(message, '*');
21
+ }
22
+ }
23
+ catch (err) {
24
+ console.debug('Could not postmessage', err);
25
+ }
26
+ }
27
+ }
@@ -0,0 +1,3 @@
1
+ type Styles = Record<string, string | number>;
2
+ export declare const sanitizeReactNativeBlockStyles: (styles: Styles) => Styles;
3
+ export {};
@@ -0,0 +1,58 @@
1
+ const propertiesThatMustBeNumber = new Set(['lineHeight']);
2
+ const displayValues = new Set(['flex', 'none']);
3
+ const SHOW_WARNINGS = false;
4
+ const normalizeNumber = (value) => {
5
+ if (Number.isNaN(value)) {
6
+ return undefined;
7
+ }
8
+ else if (value < 0) {
9
+ // TODO: why are negative values not allowed?
10
+ return 0;
11
+ }
12
+ else {
13
+ return value;
14
+ }
15
+ };
16
+ export const sanitizeReactNativeBlockStyles = (styles) => {
17
+ return Object.keys(styles).reduce((acc, key) => {
18
+ const propertyValue = styles[key];
19
+ if (key === 'display' && !displayValues.has(propertyValue)) {
20
+ if (SHOW_WARNINGS) {
21
+ console.warn(`Style value for key "display" must be "flex" or "none" but had ${propertyValue}`);
22
+ }
23
+ return acc;
24
+ }
25
+ if (propertiesThatMustBeNumber.has(key) && typeof propertyValue !== 'number') {
26
+ if (SHOW_WARNINGS) {
27
+ console.warn(`Style key ${key} must be a number, but had value \`${styles[key]}\``);
28
+ }
29
+ return acc;
30
+ }
31
+ if (typeof propertyValue === 'string') {
32
+ // `px` units need to be stripped and replaced with numbers
33
+ // https://regexr.com/6ualn
34
+ const isPixelUnit = propertyValue.match(/^-?(\d*)(\.?)(\d*)*px$/);
35
+ if (isPixelUnit) {
36
+ const newValue = parseFloat(propertyValue);
37
+ const normalizedValue = normalizeNumber(newValue);
38
+ if (normalizedValue) {
39
+ return { ...acc,
40
+ [key]: normalizedValue
41
+ };
42
+ }
43
+ else {
44
+ return acc;
45
+ }
46
+ }
47
+ else if (propertyValue === '0') {
48
+ // 0 edge case needs to be handled
49
+ return { ...acc,
50
+ [key]: 0
51
+ };
52
+ }
53
+ }
54
+ return { ...acc,
55
+ [key]: propertyValue
56
+ };
57
+ }, {});
58
+ };
@@ -0,0 +1,4 @@
1
+ export type Settings = {
2
+ customInsertMenu?: boolean;
3
+ };
4
+ export declare function setEditorSettings(newSettings: Settings): void;
@@ -0,0 +1,12 @@
1
+ import { isBrowser } from './is-browser.js';
2
+ const settings = {};
3
+ export function setEditorSettings(newSettings) {
4
+ if (isBrowser()) {
5
+ Object.assign(settings, newSettings);
6
+ const message = {
7
+ type: 'builder.settingsChange',
8
+ data: settings
9
+ };
10
+ parent.postMessage(message, '*');
11
+ }
12
+ }
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Minimal implementation of lodash's _.set
3
+ * https://lodash.com/docs/4.17.15#set
4
+ *
5
+ * See ./set.test.ts for usage examples
6
+ */
7
+ export declare const set: (obj: any, _path: string | string[], value: any) => any;
@@ -0,0 +1,14 @@
1
+ /**
2
+ * Minimal implementation of lodash's _.set
3
+ * https://lodash.com/docs/4.17.15#set
4
+ *
5
+ * See ./set.test.ts for usage examples
6
+ */
7
+ export const set = (obj, _path, value) => {
8
+ if (Object(obj) !== obj) {
9
+ return obj;
10
+ }
11
+ const path = Array.isArray(_path) ? _path : _path.toString().match(/[^.[\]]+/g);
12
+ path.slice(0, -1).reduce((a, c, i) => Object(a[c]) === a[c] ? a[c] : a[c] = Math.abs(Number(path[i + 1])) >> 0 === +path[i + 1] ? [] : {}, obj)[path[path.length - 1]] = value;
13
+ return obj;
14
+ };
@@ -0,0 +1,5 @@
1
+ export declare const getUserAttributes: () => {
2
+ urlPath: string;
3
+ host: string;
4
+ device: string;
5
+ };
@@ -0,0 +1,50 @@
1
+ import { TARGET } from '../../constants/target.js';
2
+ import { isBrowser } from '../is-browser.js';
3
+ const getLocation = () => {
4
+ if (TARGET === 'reactNative') {
5
+ return null;
6
+ }
7
+ else if (isBrowser()) {
8
+ const parsedLocation = new URL(location.href); // IE11 bug with parsed path being empty string
9
+ // causes issues with our user targeting
10
+ if (parsedLocation.pathname === '') {
11
+ parsedLocation.pathname = '/';
12
+ }
13
+ return parsedLocation;
14
+ }
15
+ else {
16
+ console.warn('Cannot get location for tracking in non-browser environment');
17
+ return null;
18
+ }
19
+ };
20
+ const getUserAgent = () => typeof navigator === 'object' && navigator.userAgent || '';
21
+ export const getUserAttributes = () => {
22
+ const userAgent = getUserAgent();
23
+ const isMobile = {
24
+ Android() {
25
+ return userAgent.match(/Android/i);
26
+ },
27
+ BlackBerry() {
28
+ return userAgent.match(/BlackBerry/i);
29
+ },
30
+ iOS() {
31
+ return userAgent.match(/iPhone|iPod/i);
32
+ },
33
+ Opera() {
34
+ return userAgent.match(/Opera Mini/i);
35
+ },
36
+ Windows() {
37
+ return userAgent.match(/IEMobile/i) || userAgent.match(/WPDesktop/i);
38
+ },
39
+ any() {
40
+ return isMobile.Android() || isMobile.BlackBerry() || isMobile.iOS() || isMobile.Opera() || isMobile.Windows() || TARGET === 'reactNative';
41
+ }
42
+ };
43
+ const isTablet = userAgent.match(/Tablet|iPad/i);
44
+ const url = getLocation();
45
+ return {
46
+ urlPath: url?.pathname,
47
+ host: url?.host || url?.hostname,
48
+ device: isTablet ? 'tablet' : isMobile.any() ? 'mobile' : 'desktop'
49
+ };
50
+ };
@@ -0,0 +1,51 @@
1
+ import type { CanTrack } from '../../types/can-track.js';
2
+ import type { Dictionary } from '../../types/typescript.js';
3
+ interface Event {
4
+ /**
5
+ * The type of your event.
6
+ *
7
+ * Examples: `click`, `conversion`, `pageview`, `impression`
8
+ */
9
+ type: string;
10
+ data: {
11
+ /**
12
+ * (Optional) The content's ID. Useful if this event pertains to a specific piece of content.
13
+ */
14
+ contentId?: string;
15
+ /**
16
+ * This is the ID of the space that the content belongs to.
17
+ */
18
+ ownerId: string;
19
+ /**
20
+ * (Optional) metadata that you want to provide with your event.
21
+ */
22
+ metadata?: Dictionary<any>;
23
+ /**
24
+ * Session ID of the user. This is provided by the SDK by checking session storage.
25
+ */
26
+ sessionId: string | undefined;
27
+ /**
28
+ * Visitor ID of the user. This is provided by the SDK by checking cookies.
29
+ */
30
+ visitorId: string | undefined;
31
+ /**
32
+ * (Optional) If running an A/B test, the ID of the variation that the user is in.
33
+ */
34
+ variationId?: string;
35
+ [index: string]: any;
36
+ };
37
+ }
38
+ type EventProperties = Pick<Event, 'type'> & Pick<Event['data'], 'contentId' | 'variationId' | 'metadata'> & {
39
+ /**
40
+ * Your organization's API key.
41
+ */
42
+ apiKey: Event['data']['ownerId'];
43
+ /**
44
+ * (Optional) Any additional (non-metadata) properties to add to the event.
45
+ */
46
+ [index: string]: any;
47
+ };
48
+ export type EventProps = EventProperties & CanTrack;
49
+ export declare function _track(eventProps: EventProps): Promise<void | Response>;
50
+ export declare const track: (args: EventProperties) => Promise<void | Response>;
51
+ export {};
@@ -0,0 +1,69 @@
1
+ import { TARGET } from '../../constants/target.js';
2
+ import { logger } from '../../helpers/logger.js';
3
+ import { getSessionId } from '../../helpers/sessionId.js';
4
+ import { getVisitorId } from '../../helpers/visitorId.js';
5
+ import { isBrowser } from '../is-browser.js';
6
+ import { isEditing } from '../is-editing.js';
7
+ import { getUserAttributes } from './helpers.js';
8
+ const getTrackingEventData = async ({ canTrack }) => {
9
+ if (!canTrack) {
10
+ return {
11
+ visitorId: undefined,
12
+ sessionId: undefined
13
+ };
14
+ }
15
+ const sessionId = await getSessionId({
16
+ canTrack
17
+ });
18
+ const visitorId = getVisitorId({
19
+ canTrack
20
+ });
21
+ return {
22
+ sessionId,
23
+ visitorId
24
+ };
25
+ };
26
+ const createEvent = async ({ type: eventType, canTrack, apiKey, metadata, ...properties }) => ({
27
+ type: eventType,
28
+ data: { ...properties,
29
+ metadata: {
30
+ url: location.href,
31
+ ...metadata
32
+ },
33
+ ...(await getTrackingEventData({
34
+ canTrack
35
+ })),
36
+ userAttributes: getUserAttributes(),
37
+ ownerId: apiKey
38
+ }
39
+ });
40
+ export async function _track(eventProps) {
41
+ if (!eventProps.apiKey) {
42
+ logger.error('Missing API key for track call. Please provide your API key.');
43
+ return;
44
+ }
45
+ if (!eventProps.canTrack) {
46
+ return;
47
+ }
48
+ if (isEditing()) {
49
+ return;
50
+ }
51
+ if (!(isBrowser() || TARGET === 'reactNative')) {
52
+ return;
53
+ }
54
+ return fetch(`https://cdn.builder.io/api/v1/track`, {
55
+ method: 'POST',
56
+ body: JSON.stringify({
57
+ events: [await createEvent(eventProps)]
58
+ }),
59
+ headers: {
60
+ 'content-type': 'application/json'
61
+ },
62
+ mode: 'cors'
63
+ }).catch(err => {
64
+ console.error('Failed to track: ', err);
65
+ });
66
+ }
67
+ export const track = (args) => _track({ ...args,
68
+ canTrack: true
69
+ });
@@ -0,0 +1,13 @@
1
+ type Offset = {
2
+ x: number;
3
+ y: number;
4
+ };
5
+ export declare const getInteractionPropertiesForEvent: (event: MouseEvent) => {
6
+ targetBuilderElement: string;
7
+ metadata: {
8
+ targetOffset: Offset;
9
+ builderTargetOffset: Offset;
10
+ builderElementIndex: any;
11
+ };
12
+ };
13
+ export {};
@@ -0,0 +1,53 @@
1
+ function round(num) {
2
+ return Math.round(num * 1000) / 1000;
3
+ }
4
+ const findParentElement = (target, callback, checkElement = true) => {
5
+ if (!(target instanceof HTMLElement)) {
6
+ return null;
7
+ }
8
+ let parent = checkElement ? target : target.parentElement;
9
+ do {
10
+ if (!parent) {
11
+ return null;
12
+ }
13
+ const matches = callback(parent);
14
+ if (matches) {
15
+ return parent;
16
+ }
17
+ } while (parent = parent.parentElement);
18
+ return null;
19
+ };
20
+ const findBuilderParent = (target) => findParentElement(target, el => {
21
+ const id = el.getAttribute('builder-id') || el.id;
22
+ return Boolean(id?.indexOf('builder-') === 0);
23
+ });
24
+ const computeOffset = ({ event, target }) => {
25
+ const targetRect = target.getBoundingClientRect();
26
+ const xOffset = event.clientX - targetRect.left;
27
+ const yOffset = event.clientY - targetRect.top;
28
+ const xRatio = round(xOffset / targetRect.width);
29
+ const yRatio = round(yOffset / targetRect.height);
30
+ return {
31
+ x: xRatio,
32
+ y: yRatio
33
+ };
34
+ };
35
+ export const getInteractionPropertiesForEvent = (event) => {
36
+ const target = event.target;
37
+ const targetBuilderElement = target && findBuilderParent(target);
38
+ const builderId = targetBuilderElement?.getAttribute('builder-id') || targetBuilderElement?.id;
39
+ return {
40
+ targetBuilderElement: builderId || undefined,
41
+ metadata: {
42
+ targetOffset: target ? computeOffset({
43
+ event,
44
+ target
45
+ }) : undefined,
46
+ builderTargetOffset: targetBuilderElement ? computeOffset({
47
+ event,
48
+ target: targetBuilderElement
49
+ }) : undefined,
50
+ builderElementIndex: targetBuilderElement && builderId ? [].slice.call(document.getElementsByClassName(builderId)).indexOf(targetBuilderElement) : undefined
51
+ }
52
+ };
53
+ };
@@ -0,0 +1 @@
1
+ export declare function transformBlockProperties(properties: any): any;
@@ -0,0 +1,5 @@
1
+ export function transformBlockProperties(properties) {
2
+ properties.className = properties.class;
3
+ delete properties.class;
4
+ return properties;
5
+ }
@@ -0,0 +1,2 @@
1
+ import type { BuilderBlock } from '../types/builder-block.js';
2
+ export declare function transformBlock(block: BuilderBlock): BuilderBlock;
@@ -0,0 +1,3 @@
1
+ export function transformBlock(block) {
2
+ return block;
3
+ }
@@ -0,0 +1,9 @@
1
+ import type { CanTrack } from '../types/can-track.js';
2
+ import type { BuilderContent } from '../types/builder-content.js';
3
+ import type { Nullable } from '../types/typescript.js';
4
+ export declare const handleABTestingSync: ({ item, canTrack }: {
5
+ item: Nullable<BuilderContent>;
6
+ } & CanTrack) => Nullable<BuilderContent>;
7
+ export declare const handleABTesting: ({ item, canTrack }: {
8
+ item: BuilderContent;
9
+ } & CanTrack) => Promise<BuilderContent>;
@@ -0,0 +1,116 @@
1
+ import { getCookie, getCookieSync, setCookie } from './cookie.js';
2
+ import { checkIsDefined } from '../helpers/nullable.js';
3
+ import { logger } from './logger.js';
4
+ import { TARGET } from '../constants/target.js';
5
+ const BUILDER_STORE_PREFIX = 'builder.tests';
6
+ const getContentTestKey = (id) => `${BUILDER_STORE_PREFIX}.${id}`;
7
+ const getContentVariationCookie = ({ contentId }) => getCookie({
8
+ name: getContentTestKey(contentId),
9
+ canTrack: true
10
+ });
11
+ const getContentVariationCookieSync = ({ contentId }) => getCookieSync({
12
+ name: getContentTestKey(contentId),
13
+ canTrack: true
14
+ });
15
+ const setContentVariationCookie = ({ contentId, value }) => setCookie({
16
+ name: getContentTestKey(contentId),
17
+ value,
18
+ canTrack: true
19
+ });
20
+ const checkIsBuilderContentWithVariations = (item) => checkIsDefined(item.id) && checkIsDefined(item.variations) && Object.keys(item.variations).length > 0;
21
+ /**
22
+ * Randomly assign a variation to a user
23
+ */
24
+ const getRandomVariationId = ({ id, variations }) => {
25
+ let n = 0;
26
+ const random = Math.random(); // loop over variations test ratios, incrementing a counter,
27
+ // until we find the variation that this user should be assigned to
28
+ for (const id in variations) {
29
+ const testRatio = variations[id]?.testRatio;
30
+ n += testRatio;
31
+ if (random < n) {
32
+ return id;
33
+ }
34
+ } // the variations array does not include the default variation.
35
+ // if we arrive here, then it means that the random number fits in the default variation bucket.
36
+ return id;
37
+ };
38
+ const getAndSetVariantId = (args) => {
39
+ // if variation not found in storage, assign a random variation to this user
40
+ const randomVariationId = getRandomVariationId(args); // store variation in cookies/storage
41
+ setContentVariationCookie({
42
+ contentId: args.id,
43
+ value: randomVariationId
44
+ }).catch(err => {
45
+ logger.error('could not store A/B test variation: ', err);
46
+ });
47
+ return randomVariationId;
48
+ };
49
+ const getTestFields = ({ item, testGroupId }) => {
50
+ const variationValue = item.variations[testGroupId];
51
+ if (testGroupId === item.id || // handle edge-case where `testGroupId` points to non-existing variation
52
+ !variationValue) {
53
+ return {
54
+ testVariationId: item.id,
55
+ testVariationName: 'Default'
56
+ };
57
+ }
58
+ else {
59
+ return {
60
+ data: variationValue.data,
61
+ testVariationId: variationValue.id,
62
+ testVariationName: variationValue.name || (variationValue.id === item.id ? 'Default' : '')
63
+ };
64
+ }
65
+ };
66
+ export const handleABTestingSync = ({ item, canTrack }) => {
67
+ /**
68
+ * We cannot SSR in React-Native.
69
+ */
70
+ if (TARGET === 'reactNative')
71
+ return item;
72
+ if (!canTrack) {
73
+ return item;
74
+ }
75
+ if (!item) {
76
+ return undefined;
77
+ }
78
+ if (!checkIsBuilderContentWithVariations(item)) {
79
+ return item;
80
+ }
81
+ const testGroupId = getContentVariationCookieSync({
82
+ contentId: item.id
83
+ }) || getAndSetVariantId({
84
+ variations: item.variations,
85
+ id: item.id
86
+ });
87
+ const variationValue = getTestFields({
88
+ item,
89
+ testGroupId
90
+ });
91
+ return { ...item,
92
+ ...variationValue
93
+ };
94
+ };
95
+ export const handleABTesting = async ({ item, canTrack }) => {
96
+ if (!canTrack) {
97
+ return item;
98
+ }
99
+ if (!checkIsBuilderContentWithVariations(item)) {
100
+ return item;
101
+ }
102
+ const cookieValue = await getContentVariationCookie({
103
+ contentId: item.id
104
+ });
105
+ const testGroupId = cookieValue || getAndSetVariantId({
106
+ variations: item.variations,
107
+ id: item.id
108
+ });
109
+ const variationValue = getTestFields({
110
+ item,
111
+ testGroupId
112
+ });
113
+ return { ...item,
114
+ ...variationValue
115
+ };
116
+ };
@@ -0,0 +1 @@
1
+ export declare const getDefaultCanTrack: (canTrack?: boolean) => boolean;
@@ -0,0 +1,2 @@
1
+ import { checkIsDefined } from './nullable.js';
2
+ export const getDefaultCanTrack = (canTrack) => checkIsDefined(canTrack) ? canTrack : true;
@@ -0,0 +1,19 @@
1
+ import type { CanTrack } from '../types/can-track.js';
2
+ type GetCookieArgs = {
3
+ name: string;
4
+ } & CanTrack;
5
+ export declare const getCookieSync: ({ name, canTrack }: GetCookieArgs) => string | undefined;
6
+ /**
7
+ * NOTE: This function is `async` because its react-native override is async. Do not remove the `async` keyword!
8
+ * The sync version is only safe to use in code blocks that `react-native` is guaranteed not to not run.
9
+ */
10
+ export declare const getCookie: (args: GetCookieArgs) => Promise<string>;
11
+ /**
12
+ * NOTE: This function is `async` because its react-native override is async. Do not remove the `async` keyword!
13
+ */
14
+ export declare const setCookie: ({ name, value, expires, canTrack }: {
15
+ name: string;
16
+ value: string;
17
+ expires?: Date;
18
+ } & CanTrack) => Promise<void>;
19
+ export {};