@builder.io/sdk-react-native 0.1.2 → 0.1.3

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 (178) hide show
  1. package/dist/blocks/BaseText.js +40 -0
  2. package/dist/blocks/button/button.js +39 -0
  3. package/dist/blocks/button/component-info.js +42 -0
  4. package/dist/blocks/columns/columns.js +149 -0
  5. package/dist/blocks/columns/component-info.js +244 -0
  6. package/dist/blocks/custom-code/component-info.js +32 -0
  7. package/dist/blocks/custom-code/custom-code.js +76 -0
  8. package/dist/blocks/embed/component-info.js +47 -0
  9. package/dist/blocks/embed/embed.js +67 -0
  10. package/dist/blocks/embed/helpers.js +10 -0
  11. package/dist/blocks/form/component-info.js +263 -0
  12. package/dist/blocks/form/form.js +232 -0
  13. package/dist/blocks/fragment/component-info.js +12 -0
  14. package/dist/blocks/fragment/fragment.js +36 -0
  15. package/dist/blocks/image/component-info.js +152 -0
  16. package/dist/blocks/image/image.helpers.js +49 -0
  17. package/dist/blocks/image/image.js +81 -0
  18. package/dist/blocks/img/component-info.js +21 -0
  19. package/dist/blocks/img/img.js +35 -0
  20. package/dist/blocks/input/component-info.js +75 -0
  21. package/dist/blocks/input/input.js +32 -0
  22. package/dist/blocks/raw-text/component-info.js +17 -0
  23. package/dist/blocks/raw-text/raw-text.js +31 -0
  24. package/dist/blocks/section/component-info.js +50 -0
  25. package/dist/blocks/section/section.js +49 -0
  26. package/dist/blocks/select/component-info.js +60 -0
  27. package/dist/blocks/select/select.js +38 -0
  28. package/dist/blocks/submit-button/component-info.js +29 -0
  29. package/dist/blocks/submit-button/submit-button.js +36 -0
  30. package/dist/blocks/symbol/component-info.js +44 -0
  31. package/dist/blocks/symbol/symbol.js +97 -0
  32. package/dist/blocks/text/component-info.js +25 -0
  33. package/dist/blocks/text/text.js +112 -0
  34. package/dist/blocks/textarea/component-info.js +48 -0
  35. package/dist/blocks/textarea/textarea.js +31 -0
  36. package/dist/blocks/util.js +9 -0
  37. package/dist/blocks/video/component-info.js +107 -0
  38. package/dist/blocks/video/video.js +81 -0
  39. package/dist/components/render-block/block-styles.js +79 -0
  40. package/dist/components/render-block/render-block.helpers.js +109 -0
  41. package/dist/components/render-block/render-block.js +154 -0
  42. package/dist/components/render-block/render-component-with-context.js +45 -0
  43. package/dist/components/render-block/render-component.js +50 -0
  44. package/dist/components/render-block/render-repeated-block.js +45 -0
  45. package/dist/components/render-block/types.js +1 -0
  46. package/dist/components/render-blocks.js +76 -0
  47. package/dist/components/render-content/builder-editing.js +31 -0
  48. package/dist/components/render-content/components/render-styles.js +91 -0
  49. package/dist/components/render-content/index.js +8 -0
  50. package/dist/components/render-content/render-content.js +325 -0
  51. package/dist/components/render-inlined-styles.js +42 -0
  52. package/dist/constants/builder-registered-components.js +46 -0
  53. package/dist/constants/device-sizes.js +49 -0
  54. package/dist/constants/target.js +5 -0
  55. package/dist/context/builder.context.js +13 -0
  56. package/dist/context/types.js +1 -0
  57. package/dist/functions/camel-to-kebab-case.js +5 -0
  58. package/dist/functions/evaluate.js +25 -0
  59. package/dist/functions/event-handler-name.js +8 -0
  60. package/dist/functions/extract-text-styles.js +23 -0
  61. package/dist/functions/fast-clone.js +5 -0
  62. package/dist/functions/get-block-actions-handler.js +12 -0
  63. package/dist/functions/get-block-actions.js +19 -0
  64. package/dist/functions/get-block-component-options.js +29 -0
  65. package/dist/functions/get-block-properties.js +51 -0
  66. package/dist/functions/get-block-tag.js +8 -0
  67. package/dist/functions/get-builder-search-params/fn.test.js +15 -0
  68. package/dist/functions/get-builder-search-params/index.js +39 -0
  69. package/dist/functions/get-content/ab-testing.js +89 -0
  70. package/dist/functions/get-content/generate-content-url.js +44 -0
  71. package/dist/functions/get-content/generate-content-url.test.js +33 -0
  72. package/dist/functions/get-content/index.js +68 -0
  73. package/dist/functions/get-content/types.js +1 -0
  74. package/dist/functions/get-fetch.js +15 -0
  75. package/dist/functions/get-global-this.js +19 -0
  76. package/dist/functions/get-processed-block.js +52 -0
  77. package/dist/functions/get-processed-block.test.js +34 -0
  78. package/dist/functions/get-react-native-block-styles.js +30 -0
  79. package/dist/functions/if-target.js +13 -0
  80. package/dist/functions/is-browser.js +7 -0
  81. package/dist/functions/is-editing.js +8 -0
  82. package/dist/functions/is-iframe.js +8 -0
  83. package/dist/functions/is-previewing.js +15 -0
  84. package/dist/functions/on-change.js +28 -0
  85. package/dist/functions/on-change.test.js +21 -0
  86. package/dist/functions/register-component.js +65 -0
  87. package/dist/functions/register.js +31 -0
  88. package/dist/functions/sanitize-react-native-block-styles.js +71 -0
  89. package/dist/functions/set-editor-settings.js +16 -0
  90. package/dist/functions/set.js +12 -0
  91. package/dist/functions/set.test.js +18 -0
  92. package/dist/functions/track/helpers.js +53 -0
  93. package/dist/functions/track/index.js +124 -0
  94. package/dist/functions/track/interaction.js +51 -0
  95. package/dist/functions/track.js +120 -0
  96. package/dist/functions/transform-block-properties.js +18 -0
  97. package/dist/functions/transform-block.js +38 -0
  98. package/dist/helpers/ab-tests.js +10 -0
  99. package/dist/helpers/cookie.js +70 -0
  100. package/dist/helpers/css.js +33 -0
  101. package/dist/helpers/flatten.js +35 -0
  102. package/dist/helpers/localStorage.js +31 -0
  103. package/dist/helpers/nullable.js +5 -0
  104. package/dist/helpers/sessionId.js +51 -0
  105. package/dist/helpers/time.js +6 -0
  106. package/dist/helpers/url.js +14 -0
  107. package/dist/helpers/url.test.js +23 -0
  108. package/dist/helpers/uuid.js +14 -0
  109. package/dist/helpers/visitorId.js +33 -0
  110. package/dist/index-helpers/blocks-exports.js +22 -0
  111. package/dist/index-helpers/top-of-file.js +3 -0
  112. package/dist/index.js +29 -0
  113. package/dist/scripts/init-editing.js +97 -0
  114. package/dist/types/builder-block.js +1 -0
  115. package/dist/types/builder-content.js +1 -0
  116. package/dist/types/can-track.js +1 -0
  117. package/dist/types/components.js +1 -0
  118. package/dist/types/deep-partial.js +1 -0
  119. package/dist/types/element.js +1 -0
  120. package/dist/types/targets.js +1 -0
  121. package/dist/types/typescript.js +1 -0
  122. package/package.json +13 -5
  123. package/src/blocks/BaseText.jsx +17 -0
  124. package/src/blocks/button/button.jsx +28 -0
  125. package/src/blocks/columns/{columns.js → columns.jsx} +59 -36
  126. package/src/blocks/custom-code/{custom-code.js → custom-code.jsx} +22 -15
  127. package/src/blocks/embed/{embed.js → embed.jsx} +12 -11
  128. package/src/blocks/form/form.jsx +251 -0
  129. package/src/blocks/fragment/fragment.jsx +11 -0
  130. package/src/blocks/image/image.jsx +78 -0
  131. package/src/blocks/img/img.jsx +18 -0
  132. package/src/blocks/input/input.jsx +20 -0
  133. package/src/blocks/raw-text/raw-text.jsx +6 -0
  134. package/src/blocks/section/section.jsx +29 -0
  135. package/src/blocks/select/select.jsx +24 -0
  136. package/src/blocks/submit-button/submit-button.jsx +11 -0
  137. package/src/blocks/symbol/symbol.jsx +86 -0
  138. package/src/blocks/text/text.jsx +100 -0
  139. package/src/blocks/textarea/textarea.jsx +14 -0
  140. package/src/blocks/video/video.jsx +70 -0
  141. package/src/components/render-block/block-styles.jsx +70 -0
  142. package/src/components/render-block/render-block.helpers.js +90 -0
  143. package/src/components/render-block/render-block.jsx +183 -0
  144. package/src/components/render-block/render-component.jsx +45 -0
  145. package/src/components/render-block/render-repeated-block.jsx +22 -0
  146. package/src/components/render-blocks.jsx +86 -0
  147. package/src/components/render-content/builder-editing.jsx +6 -0
  148. package/src/components/render-content/components/{render-styles.js → render-styles.jsx} +21 -14
  149. package/src/components/render-content/render-content.jsx +373 -0
  150. package/src/components/render-inlined-styles.jsx +29 -0
  151. package/src/functions/track/helpers.js +50 -0
  152. package/src/functions/{track.js → track/index.js} +10 -6
  153. package/src/functions/track/interaction.js +53 -0
  154. package/src/index.js +1 -1
  155. package/src/blocks/BaseText.js +0 -31
  156. package/src/blocks/button/button.js +0 -36
  157. package/src/blocks/form/form.js +0 -222
  158. package/src/blocks/fragment/fragment.js +0 -9
  159. package/src/blocks/image/image.js +0 -55
  160. package/src/blocks/img/img.js +0 -33
  161. package/src/blocks/input/input.js +0 -36
  162. package/src/blocks/raw-text/raw-text.js +0 -10
  163. package/src/blocks/section/section.js +0 -41
  164. package/src/blocks/select/select.js +0 -37
  165. package/src/blocks/submit-button/submit-button.js +0 -27
  166. package/src/blocks/symbol/symbol.js +0 -73
  167. package/src/blocks/text/text.js +0 -67
  168. package/src/blocks/textarea/textarea.js +0 -32
  169. package/src/blocks/video/video.js +0 -55
  170. package/src/components/render-block/block-styles.js +0 -50
  171. package/src/components/render-block/render-block.js +0 -202
  172. package/src/components/render-block/render-component-with-context.js +0 -35
  173. package/src/components/render-block/render-component.js +0 -35
  174. package/src/components/render-block/render-repeated-block.js +0 -33
  175. package/src/components/render-blocks.js +0 -63
  176. package/src/components/render-content/builder-editing.js +0 -8
  177. package/src/components/render-content/render-content.js +0 -297
  178. package/src/components/render-inlined-styles.js +0 -17
@@ -0,0 +1,45 @@
1
+ import * as React from "react";
2
+ import { View, StyleSheet, Image, Text } from "react-native";
3
+ import { useContext } from "react";
4
+ import BlockStyles from "./block-styles.js";
5
+ import RenderBlock from "./render-block.js";
6
+ import BuilderContext from "../../context/builder.context.js";
7
+
8
+ export default function RenderComponent(props) {
9
+ const ComponentRefRef = props.componentRef;
10
+
11
+ return (
12
+ <BuilderContext.Provider
13
+ value={{
14
+ content: props.context.content,
15
+ state: props.context.state,
16
+ context: props.context.context,
17
+ apiKey: props.context.apiKey,
18
+ registeredComponents: props.context.registeredComponents,
19
+ inheritedStyles: props.context.inheritedStyles,
20
+ }}
21
+ >
22
+ {props.componentRef ? (
23
+ <>
24
+ <ComponentRefRef {...props.componentOptions}>
25
+ {props.blockChildren?.map((child) => (
26
+ <RenderBlock
27
+ key={"render-block-" + child.id}
28
+ block={child}
29
+ context={props.context}
30
+ />
31
+ ))}
32
+
33
+ {props.blockChildren?.map((child) => (
34
+ <BlockStyles
35
+ key={"block-style-" + child.id}
36
+ block={child}
37
+ context={props.context}
38
+ />
39
+ ))}
40
+ </ComponentRefRef>
41
+ </>
42
+ ) : null}
43
+ </BuilderContext.Provider>
44
+ );
45
+ }
@@ -0,0 +1,22 @@
1
+ import * as React from "react";
2
+ import { View, StyleSheet, Image, Text } from "react-native";
3
+ import { useContext } from "react";
4
+ import BuilderContext from "../../context/builder.context.js";
5
+ import RenderBlock from "./render-block.js";
6
+
7
+ export default function RenderRepeatedBlock(props) {
8
+ return (
9
+ <BuilderContext.Provider
10
+ value={{
11
+ content: props.repeatContext.content,
12
+ state: props.repeatContext.state,
13
+ context: props.repeatContext.context,
14
+ apiKey: props.repeatContext.apiKey,
15
+ registeredComponents: props.repeatContext.registeredComponents,
16
+ inheritedStyles: props.repeatContext.inheritedStyles,
17
+ }}
18
+ >
19
+ <RenderBlock block={props.block} context={props.repeatContext} />
20
+ </BuilderContext.Provider>
21
+ );
22
+ }
@@ -0,0 +1,86 @@
1
+ import * as React from "react";
2
+ import { View, StyleSheet, Image, Text } from "react-native";
3
+ import { useContext } from "react";
4
+ import BuilderContext from "../context/builder.context.js";
5
+ import { isEditing } from "../functions/is-editing.js";
6
+ import BlockStyles from "./render-block/block-styles.js";
7
+ import RenderBlock from "./render-block/render-block.js";
8
+
9
+ export default function RenderBlocks(props) {
10
+ function className() {
11
+ return "builder-blocks" + (!props.blocks?.length ? " no-blocks" : "");
12
+ }
13
+
14
+ function onClick() {
15
+ if (isEditing() && !props.blocks?.length) {
16
+ window.parent?.postMessage(
17
+ {
18
+ type: "builder.clickEmptyBlocks",
19
+ data: {
20
+ parentElementId: props.parent,
21
+ dataPath: props.path,
22
+ },
23
+ },
24
+ "*"
25
+ );
26
+ }
27
+ }
28
+
29
+ function onMouseEnter() {
30
+ if (isEditing() && !props.blocks?.length) {
31
+ window.parent?.postMessage(
32
+ {
33
+ type: "builder.hoverEmptyBlocks",
34
+ data: {
35
+ parentElementId: props.parent,
36
+ dataPath: props.path,
37
+ },
38
+ },
39
+ "*"
40
+ );
41
+ }
42
+ }
43
+
44
+ const builderContext = useContext(BuilderContext);
45
+
46
+ return (
47
+ <View
48
+ builder-path={props.path}
49
+ builder-parent-id={props.parent}
50
+ dataSet={{
51
+ class: className(),
52
+ }}
53
+ style={styles.view1}
54
+ onClick={(event) => onClick()}
55
+ onMouseEnter={(event) => onMouseEnter()}
56
+ >
57
+ {props.blocks ? (
58
+ <>
59
+ {props.blocks?.map((block) => (
60
+ <RenderBlock
61
+ key={"render-block-" + block.id}
62
+ block={block}
63
+ context={builderContext}
64
+ />
65
+ ))}
66
+ </>
67
+ ) : null}
68
+
69
+ {props.blocks ? (
70
+ <>
71
+ {props.blocks?.map((block) => (
72
+ <BlockStyles
73
+ key={"block-style-" + block.id}
74
+ block={block}
75
+ context={builderContext}
76
+ />
77
+ ))}
78
+ </>
79
+ ) : null}
80
+ </View>
81
+ );
82
+ }
83
+
84
+ const styles = StyleSheet.create({
85
+ view1: { display: "flex", flexDirection: "column", alignItems: "stretch" },
86
+ });
@@ -0,0 +1,6 @@
1
+ import * as React from "react";
2
+ import { View, StyleSheet, Image, Text } from "react-native";
3
+
4
+ export default function BuilderEditing(props) {
5
+ return <View />;
6
+ }
@@ -1,11 +1,15 @@
1
1
  import * as React from "react";
2
+ import { View, StyleSheet, Image, Text } from "react-native";
2
3
  import RenderInlinedStyles from "../../render-inlined-styles.js";
3
- function RenderContentStyles(props) {
4
+
5
+ export default function RenderContentStyles(props) {
4
6
  function getCssFromFont(font) {
5
- var _a, _b;
6
- const family = font.family + (font.kind && !font.kind.includes("#") ? ", " + font.kind : "");
7
+ // TODO: compute what font sizes are used and only load those.......
8
+ const family =
9
+ font.family +
10
+ (font.kind && !font.kind.includes("#") ? ", " + font.kind : "");
7
11
  const name = family.split(",")[0];
8
- const url = (_b = font.fileUrl) != null ? _b : (_a = font == null ? void 0 : font.files) == null ? void 0 : _a.regular;
12
+ const url = font.fileUrl ?? font?.files?.regular;
9
13
  let str = "";
10
14
  if (url && family && name) {
11
15
  str += `
@@ -23,6 +27,7 @@ function RenderContentStyles(props) {
23
27
  if (!isNumber) {
24
28
  continue;
25
29
  }
30
+ // TODO: maybe limit number loaded
26
31
  const weightUrl = font.files[weight];
27
32
  if (weightUrl && weightUrl !== url) {
28
33
  str += `
@@ -38,21 +43,23 @@ function RenderContentStyles(props) {
38
43
  }
39
44
  return str;
40
45
  }
46
+
41
47
  function getFontCss({ customFonts }) {
42
- var _a;
43
- return ((_a = customFonts == null ? void 0 : customFonts.map((font) => getCssFromFont(font))) == null ? void 0 : _a.join(" ")) || "";
48
+ // TODO: flag for this
49
+ // if (!builder.allowCustomFonts) {
50
+ // return '';
51
+ // }
52
+ // TODO: separate internal data from external
53
+ return customFonts?.map((font) => getCssFromFont(font))?.join(" ") || "";
44
54
  }
55
+
45
56
  function injectedStyles() {
46
57
  return `
47
58
  ${props.cssCode || ""}
48
59
  ${getFontCss({
49
- customFonts: props.customFonts
50
- })}`;
60
+ customFonts: props.customFonts,
61
+ })}`;
51
62
  }
52
- return /* @__PURE__ */ React.createElement(RenderInlinedStyles, {
53
- styles: injectedStyles()
54
- });
63
+
64
+ return <RenderInlinedStyles styles={injectedStyles()} />;
55
65
  }
56
- export {
57
- RenderContentStyles as default
58
- };
@@ -0,0 +1,373 @@
1
+ import * as React from "react";
2
+ import { View, StyleSheet, Image, Text } from "react-native";
3
+ import { useState, useContext, useRef, useEffect } from "react";
4
+ import { getDefaultRegisteredComponents } from "../../constants/builder-registered-components.js";
5
+ import { TARGET } from "../../constants/target.js";
6
+ import { evaluate } from "../../functions/evaluate.js";
7
+ import { getContent } from "../../functions/get-content/index.js";
8
+ import { fetch } from "../../functions/get-fetch.js";
9
+ import { isBrowser } from "../../functions/is-browser.js";
10
+ import { isEditing } from "../../functions/is-editing.js";
11
+ import { isPreviewing } from "../../functions/is-previewing.js";
12
+ import {
13
+ components,
14
+ createRegisterComponentMessage,
15
+ } from "../../functions/register-component.js";
16
+ import { _track } from "../../functions/track/index.js";
17
+ import RenderBlocks from "../render-blocks.js";
18
+ import RenderContentStyles from "./components/render-styles.js";
19
+ import builderContext from "../../context/builder.context.js";
20
+ import {
21
+ registerInsertMenu,
22
+ setupBrowserForEditing,
23
+ } from "../../scripts/init-editing.js";
24
+ import { checkIsDefined } from "../../helpers/nullable.js";
25
+ import { getInteractionPropertiesForEvent } from "../../functions/track/interaction.js";
26
+
27
+ export default function RenderContent(props) {
28
+ const elementRef = useRef(null);
29
+ const [forceReRenderCount, setForceReRenderCount] = useState(() => 0);
30
+
31
+ const [overrideContent, setOverrideContent] = useState(() => null);
32
+
33
+ function useContent() {
34
+ if (!props.content && !overrideContent) {
35
+ return undefined;
36
+ }
37
+ return {
38
+ ...props.content,
39
+ ...overrideContent,
40
+ data: {
41
+ ...props.content?.data,
42
+ ...props.data,
43
+ ...overrideContent?.data,
44
+ },
45
+ meta: {
46
+ ...props.content?.meta,
47
+ ...overrideContent?.meta,
48
+ breakpoints:
49
+ useBreakpoints ||
50
+ overrideContent?.meta?.breakpoints ||
51
+ props.content?.meta?.breakpoints,
52
+ },
53
+ };
54
+ }
55
+
56
+ const [update, setUpdate] = useState(() => 0);
57
+
58
+ const [useBreakpoints, setUseBreakpoints] = useState(() => null);
59
+
60
+ const [canTrackToUse, setCanTrackToUse] = useState(() =>
61
+ checkIsDefined(props.canTrack) ? props.canTrack : true
62
+ );
63
+
64
+ const [overrideState, setOverrideState] = useState(() => ({}));
65
+
66
+ function contentState() {
67
+ return {
68
+ ...props.content?.data?.state,
69
+ ...props.data,
70
+ ...(props.locale
71
+ ? {
72
+ locale: props.locale,
73
+ }
74
+ : {}),
75
+ ...overrideState,
76
+ };
77
+ }
78
+
79
+ const [contextContext, setContextContext] = useState(
80
+ () => props.context || {}
81
+ );
82
+
83
+ const [allRegisteredComponents, setAllRegisteredComponents] = useState(() =>
84
+ [
85
+ ...getDefaultRegisteredComponents(),
86
+ // While this `components` object is deprecated, we must maintain support for it.
87
+ // Since users are able to override our default components, we need to make sure that we do not break such
88
+ // existing usage.
89
+ // This is why we spread `components` after the default Builder.io components, but before the `props.customComponents`,
90
+ // which is the new standard way of providing custom components, and must therefore take precedence.
91
+ ...components,
92
+ ...(props.customComponents || []),
93
+ ].reduce(
94
+ (acc, curr) => ({
95
+ ...acc,
96
+ [curr.name]: curr,
97
+ }),
98
+ {}
99
+ )
100
+ );
101
+
102
+ function processMessage(event) {
103
+ const { data } = event;
104
+ if (data) {
105
+ switch (data.type) {
106
+ case "builder.configureSdk": {
107
+ const messageContent = data.data;
108
+ const { breakpoints, contentId } = messageContent;
109
+ if (!contentId || contentId !== useContent?.()?.id) {
110
+ return;
111
+ }
112
+ setUseBreakpoints(breakpoints);
113
+ setForceReRenderCount(forceReRenderCount + 1); // This is a hack to force Qwik to re-render.
114
+ break;
115
+ }
116
+ case "builder.contentUpdate": {
117
+ const messageContent = data.data;
118
+ const key =
119
+ messageContent.key ||
120
+ messageContent.alias ||
121
+ messageContent.entry ||
122
+ messageContent.modelName;
123
+ const contentData = messageContent.data;
124
+ if (key === props.model) {
125
+ setOverrideContent(contentData);
126
+ setForceReRenderCount(forceReRenderCount + 1); // This is a hack to force Qwik to re-render.
127
+ }
128
+
129
+ break;
130
+ }
131
+ case "builder.patchUpdates": {
132
+ // TODO
133
+ break;
134
+ }
135
+ }
136
+ }
137
+ }
138
+
139
+ function evaluateJsCode() {
140
+ // run any dynamic JS code attached to content
141
+ const jsCode = useContent?.()?.data?.jsCode;
142
+ if (jsCode) {
143
+ evaluate({
144
+ code: jsCode,
145
+ context: contextContext,
146
+ state: contentState(),
147
+ });
148
+ }
149
+ }
150
+
151
+ const [httpReqsData, setHttpReqsData] = useState(() => ({}));
152
+
153
+ const [clicked, setClicked] = useState(() => false);
154
+
155
+ function onClick(event) {
156
+ if (useContent()) {
157
+ const variationId = useContent?.()?.testVariationId;
158
+ const contentId = useContent?.()?.id;
159
+ _track({
160
+ type: "click",
161
+ canTrack: canTrackToUse,
162
+ contentId,
163
+ apiKey: props.apiKey,
164
+ variationId: variationId !== contentId ? variationId : undefined,
165
+ ...getInteractionPropertiesForEvent(event),
166
+ unique: !clicked,
167
+ });
168
+ }
169
+ if (!clicked) {
170
+ setClicked(true);
171
+ }
172
+ }
173
+
174
+ function evalExpression(expression) {
175
+ return expression.replace(/{{([^}]+)}}/g, (_match, group) =>
176
+ evaluate({
177
+ code: group,
178
+ context: contextContext,
179
+ state: contentState(),
180
+ })
181
+ );
182
+ }
183
+
184
+ function handleRequest({ url, key }) {
185
+ fetch(url)
186
+ .then((response) => response.json())
187
+ .then((json) => {
188
+ const newOverrideState = {
189
+ ...overrideState,
190
+ [key]: json,
191
+ };
192
+ setOverrideState(newOverrideState);
193
+ })
194
+ .catch((err) => {
195
+ console.log("error fetching dynamic data", url, err);
196
+ });
197
+ }
198
+
199
+ function runHttpRequests() {
200
+ const requests = useContent?.()?.data?.httpRequests ?? {};
201
+ Object.entries(requests).forEach(([key, url]) => {
202
+ if (url && (!httpReqsData[key] || isEditing())) {
203
+ const evaluatedUrl = evalExpression(url);
204
+ handleRequest({
205
+ url: evaluatedUrl,
206
+ key,
207
+ });
208
+ }
209
+ });
210
+ }
211
+
212
+ function emitStateUpdate() {
213
+ if (isEditing()) {
214
+ window.dispatchEvent(
215
+ new CustomEvent("builder:component:stateChange", {
216
+ detail: {
217
+ state: contentState(),
218
+ ref: {
219
+ name: props.model,
220
+ },
221
+ },
222
+ })
223
+ );
224
+ }
225
+ }
226
+
227
+ function shouldRenderContentStyles() {
228
+ return Boolean(
229
+ (useContent?.()?.data?.cssCode ||
230
+ useContent?.()?.data?.customFonts?.length) &&
231
+ TARGET !== "reactNative"
232
+ );
233
+ }
234
+
235
+ useEffect(() => {
236
+ if (!props.apiKey) {
237
+ console.error(
238
+ "[Builder.io]: No API key provided to `RenderContent` component. This can cause issues. Please provide an API key using the `apiKey` prop."
239
+ );
240
+ }
241
+ if (isBrowser()) {
242
+ if (isEditing()) {
243
+ setForceReRenderCount(forceReRenderCount + 1);
244
+ registerInsertMenu();
245
+ setupBrowserForEditing({
246
+ ...(props.locale
247
+ ? {
248
+ locale: props.locale,
249
+ }
250
+ : {}),
251
+ ...(props.includeRefs
252
+ ? {
253
+ includeRefs: props.includeRefs,
254
+ }
255
+ : {}),
256
+ });
257
+ Object.values(allRegisteredComponents).forEach(
258
+ (registeredComponent) => {
259
+ const message = createRegisterComponentMessage(registeredComponent);
260
+ window.parent?.postMessage(message, "*");
261
+ }
262
+ );
263
+ window.addEventListener("message", processMessage);
264
+ window.addEventListener(
265
+ "builder:component:stateChangeListenerActivated",
266
+ emitStateUpdate
267
+ );
268
+ }
269
+ if (useContent()) {
270
+ const variationId = useContent?.()?.testVariationId;
271
+ const contentId = useContent?.()?.id;
272
+ _track({
273
+ type: "impression",
274
+ canTrack: canTrackToUse,
275
+ contentId,
276
+ apiKey: props.apiKey,
277
+ variationId: variationId !== contentId ? variationId : undefined,
278
+ });
279
+ }
280
+
281
+ // override normal content in preview mode
282
+ if (isPreviewing()) {
283
+ const searchParams = new URL(location.href).searchParams;
284
+ const searchParamPreview = searchParams.get("builder.preview");
285
+ const previewApiKey =
286
+ searchParams.get("apiKey") || searchParams.get("builder.space");
287
+
288
+ /**
289
+ * Make sure that:
290
+ * - the preview model name is the same as the one we're rendering, since there can be multiple models rendered * at the same time, e.g. header/page/footer. * - the API key is the same, since we don't want to preview content from other organizations.
291
+ *
292
+ * TO-DO: should we check that the preview item ID is the same as the initial one being rendered? Or would
293
+ * this break scenarios where the item is not published yet?
294
+ *
295
+ * TO-DO: should we only update the state when there is a change?
296
+ **/
297
+ if (
298
+ searchParamPreview === props.model &&
299
+ previewApiKey === props.apiKey
300
+ ) {
301
+ getContent({
302
+ model: props.model,
303
+ apiKey: props.apiKey,
304
+ }).then((content) => {
305
+ if (content) {
306
+ setOverrideContent(content);
307
+ }
308
+ });
309
+ }
310
+ }
311
+ evaluateJsCode();
312
+ runHttpRequests();
313
+ emitStateUpdate();
314
+ }
315
+ }, []);
316
+
317
+ useEffect(() => {
318
+ evaluateJsCode();
319
+ }, [useContent?.()?.data?.jsCode, contentState()]);
320
+ useEffect(() => {
321
+ runHttpRequests();
322
+ }, [useContent?.()?.data?.httpRequests]);
323
+ useEffect(() => {
324
+ emitStateUpdate();
325
+ }, [contentState()]);
326
+
327
+ useEffect(() => {
328
+ return () => {
329
+ if (isBrowser()) {
330
+ window.removeEventListener("message", processMessage);
331
+ window.removeEventListener(
332
+ "builder:component:stateChangeListenerActivated",
333
+ emitStateUpdate
334
+ );
335
+ }
336
+ };
337
+ }, []);
338
+
339
+ return (
340
+ <builderContext.Provider
341
+ value={{
342
+ content: useContent(),
343
+ state: contentState(),
344
+ context: contextContext,
345
+ apiKey: props.apiKey,
346
+ registeredComponents: allRegisteredComponents,
347
+ }}
348
+ >
349
+ {useContent() ? (
350
+ <>
351
+ <View
352
+ ref={elementRef}
353
+ onClick={(event) => onClick(event)}
354
+ builder-content-id={useContent?.()?.id}
355
+ builder-model={props.model}
356
+ >
357
+ {shouldRenderContentStyles() ? (
358
+ <RenderContentStyles
359
+ cssCode={useContent?.()?.data?.cssCode}
360
+ customFonts={useContent?.()?.data?.customFonts}
361
+ />
362
+ ) : null}
363
+
364
+ <RenderBlocks
365
+ blocks={useContent?.()?.data?.blocks}
366
+ key={forceReRenderCount}
367
+ />
368
+ </View>
369
+ </>
370
+ ) : null}
371
+ </builderContext.Provider>
372
+ );
373
+ }
@@ -0,0 +1,29 @@
1
+ import * as React from "react";
2
+ import { View, StyleSheet, Image, Text } from "react-native";
3
+ import { TARGET } from "../constants/target.js";
4
+
5
+ export default function RenderInlinedStyles(props) {
6
+ function injectedStyleScript() {
7
+ return `<${tag()}>${props.styles}</${tag()}>`;
8
+ }
9
+
10
+ function tag() {
11
+ // NOTE: we have to obfusctate the name of the tag due to a limitation in the svelte-preprocessor plugin.
12
+ // https://github.com/sveltejs/vite-plugin-svelte/issues/315#issuecomment-1109000027
13
+ return "sty" + "le";
14
+ }
15
+
16
+ return (
17
+ <>
18
+ {TARGET === "svelte" ? (
19
+ <>
20
+ <View dangerouslySetInnerHTML={{ __html: injectedStyleScript() }} />
21
+ </>
22
+ ) : (
23
+ <View>
24
+ <Text>{props.styles}</Text>
25
+ </View>
26
+ )}
27
+ </>
28
+ );
29
+ }
@@ -0,0 +1,50 @@
1
+ import { TARGET } from "../../constants/target";
2
+ import { isBrowser } from "../is-browser";
3
+ const getLocation = () => {
4
+ if (TARGET === "reactNative") {
5
+ return null;
6
+ } else if (isBrowser()) {
7
+ const parsedLocation = new URL(location.href);
8
+ if (parsedLocation.pathname === "") {
9
+ parsedLocation.pathname = "/";
10
+ }
11
+ return parsedLocation;
12
+ } else {
13
+ console.warn("Cannot get location for tracking in non-browser environment");
14
+ return null;
15
+ }
16
+ };
17
+ const getUserAgent = () => typeof navigator === "object" && navigator.userAgent || "";
18
+ const getUserAttributes = () => {
19
+ const userAgent = getUserAgent();
20
+ const isMobile = {
21
+ Android() {
22
+ return userAgent.match(/Android/i);
23
+ },
24
+ BlackBerry() {
25
+ return userAgent.match(/BlackBerry/i);
26
+ },
27
+ iOS() {
28
+ return userAgent.match(/iPhone|iPod/i);
29
+ },
30
+ Opera() {
31
+ return userAgent.match(/Opera Mini/i);
32
+ },
33
+ Windows() {
34
+ return userAgent.match(/IEMobile/i) || userAgent.match(/WPDesktop/i);
35
+ },
36
+ any() {
37
+ return isMobile.Android() || isMobile.BlackBerry() || isMobile.iOS() || isMobile.Opera() || isMobile.Windows() || TARGET === "reactNative";
38
+ }
39
+ };
40
+ const isTablet = userAgent.match(/Tablet|iPad/i);
41
+ const url = getLocation();
42
+ return {
43
+ urlPath: url == null ? void 0 : url.pathname,
44
+ host: (url == null ? void 0 : url.host) || (url == null ? void 0 : url.hostname),
45
+ device: isTablet ? "tablet" : isMobile.any() ? "mobile" : "desktop"
46
+ };
47
+ };
48
+ export {
49
+ getUserAttributes
50
+ };