@builder.io/sdk-react-native 0.0.1-5 → 0.0.1-52

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 (138) hide show
  1. package/CHANGELOG.md +28 -0
  2. package/README.md +15 -0
  3. package/package.json +6 -4
  4. package/src/blocks/button/button.js +31 -0
  5. package/src/blocks/{button.lite.tsx → button/button.lite.tsx} +7 -11
  6. package/src/blocks/button/component-info.js +42 -0
  7. package/src/blocks/columns/columns.js +59 -0
  8. package/src/blocks/columns/columns.lite.tsx +70 -0
  9. package/src/blocks/columns/component-info.js +218 -0
  10. package/src/blocks/custom-code/component-info.js +32 -0
  11. package/src/blocks/custom-code/custom-code.js +50 -0
  12. package/src/blocks/{custom-code.lite.tsx → custom-code/custom-code.lite.tsx} +2 -10
  13. package/src/blocks/embed/component-info.js +25 -0
  14. package/src/blocks/embed/embed.js +50 -0
  15. package/src/blocks/{embed.lite.tsx → embed/embed.lite.tsx} +2 -6
  16. package/src/blocks/form/component-info.js +263 -0
  17. package/src/blocks/form/form.js +221 -0
  18. package/src/blocks/{form.lite.tsx → form/form.lite.tsx} +24 -27
  19. package/src/blocks/fragment/component-info.js +12 -0
  20. package/src/blocks/fragment/fragment.js +8 -0
  21. package/src/blocks/fragment/fragment.lite.tsx +10 -0
  22. package/src/blocks/{image.js → image/component-info.js} +5 -23
  23. package/src/blocks/image/image.js +41 -0
  24. package/src/blocks/image/image.lite.tsx +67 -0
  25. package/src/blocks/img/component-info.js +21 -0
  26. package/src/blocks/img/img.js +36 -0
  27. package/src/blocks/{img.lite.tsx → img/img.lite.tsx} +2 -3
  28. package/src/blocks/input/component-info.js +75 -0
  29. package/src/blocks/input/input.js +36 -0
  30. package/src/blocks/{input.lite.tsx → input/input.lite.tsx} +2 -5
  31. package/src/blocks/raw-text/component-info.js +17 -0
  32. package/src/blocks/raw-text/raw-text.js +10 -0
  33. package/src/blocks/raw-text/raw-text.lite.tsx +6 -0
  34. package/src/blocks/section/component-info.js +50 -0
  35. package/src/blocks/section/section.js +31 -0
  36. package/src/blocks/{section.lite.tsx → section/section.lite.tsx} +1 -2
  37. package/src/blocks/select/component-info.js +60 -0
  38. package/src/blocks/select/select.js +36 -0
  39. package/src/blocks/{select.lite.tsx → select/select.lite.tsx} +6 -7
  40. package/src/blocks/submit-button/component-info.js +29 -0
  41. package/src/blocks/submit-button/submit-button.js +29 -0
  42. package/src/blocks/{submit-button.lite.tsx → submit-button/submit-button.lite.tsx} +1 -2
  43. package/src/blocks/symbol/component-info.js +43 -0
  44. package/src/blocks/symbol/symbol.js +68 -0
  45. package/src/blocks/symbol/symbol.lite.tsx +60 -0
  46. package/src/blocks/{text.js → text/component-info.js} +4 -10
  47. package/src/blocks/text/text.js +65 -0
  48. package/src/blocks/text/text.lite.tsx +6 -0
  49. package/src/blocks/textarea/component-info.js +48 -0
  50. package/src/blocks/textarea/textarea.js +32 -0
  51. package/src/blocks/{textarea.lite.tsx → textarea/textarea.lite.tsx} +0 -1
  52. package/src/blocks/video/component-info.js +107 -0
  53. package/src/blocks/video/video.js +33 -0
  54. package/src/blocks/{video.lite.tsx → video/video.lite.tsx} +0 -1
  55. package/src/components/error-boundary.js +25 -0
  56. package/src/components/error-boundary.lite.tsx +6 -0
  57. package/src/components/render-block/block-styles.js +29 -0
  58. package/src/components/render-block/block-styles.lite.tsx +35 -0
  59. package/src/components/render-block/render-block.helpers.js +24 -0
  60. package/src/components/render-block/render-block.js +116 -0
  61. package/src/components/render-block/render-block.lite.tsx +139 -0
  62. package/src/components/render-blocks.js +14 -7
  63. package/src/components/render-blocks.lite.tsx +18 -9
  64. package/src/components/render-content/components/render-styles.js +58 -0
  65. package/src/components/render-content/components/render-styles.lite.tsx +70 -0
  66. package/src/components/render-content/index.js +5 -0
  67. package/src/components/render-content/render-content.js +254 -0
  68. package/src/components/render-content/render-content.lite.tsx +283 -0
  69. package/src/components/render-inlined-styles.js +18 -0
  70. package/src/components/render-inlined-styles.lite.tsx +31 -0
  71. package/src/constants/builder-registered-components.js +27 -0
  72. package/src/constants/target.js +5 -0
  73. package/src/context/builder.context.js +7 -1
  74. package/src/functions/evaluate.js +17 -8
  75. package/src/functions/event-handler-name.js +8 -0
  76. package/src/functions/fast-clone.js +5 -0
  77. package/src/functions/get-block-actions.js +14 -13
  78. package/src/functions/get-block-component-options.js +17 -1
  79. package/src/functions/get-block-properties.js +7 -4
  80. package/src/functions/get-block-styles.js +31 -1
  81. package/src/functions/get-builder-search-params/fn.test.js +14 -0
  82. package/src/functions/get-builder-search-params/index.js +23 -0
  83. package/src/functions/get-content/fn.test.js +32 -0
  84. package/src/functions/get-content/index.js +139 -0
  85. package/src/functions/get-fetch.js +35 -0
  86. package/src/functions/get-global-this.js +19 -0
  87. package/src/functions/get-processed-block.js +5 -3
  88. package/src/functions/get-processed-block.test.js +13 -8
  89. package/src/functions/if-target.js +11 -2
  90. package/src/functions/is-browser.js +1 -2
  91. package/src/functions/is-editing.js +1 -1
  92. package/src/functions/is-iframe.js +1 -1
  93. package/src/functions/is-previewing.js +15 -0
  94. package/src/functions/is-react-native.js +1 -1
  95. package/src/functions/on-change.test.js +1 -2
  96. package/src/functions/previewing-model-name.js +12 -0
  97. package/src/functions/register-component.js +31 -13
  98. package/src/functions/register.js +30 -0
  99. package/src/functions/set-editor-settings.js +16 -0
  100. package/src/functions/set.test.js +1 -2
  101. package/src/functions/track.js +7 -3
  102. package/src/functions/transform-block.js +38 -0
  103. package/src/index-helpers/blocks-exports.js +19 -0
  104. package/src/index-helpers/top-of-file.js +2 -0
  105. package/src/index.js +10 -23
  106. package/src/scripts/init-editing.js +73 -47
  107. package/src/types/components.js +1 -0
  108. package/src/types/deep-partial.js +1 -0
  109. package/src/types/element.js +1 -0
  110. package/src/types/targets.js +1 -0
  111. package/src/types/typescript.js +1 -0
  112. package/index.js +0 -11
  113. package/src/blocks/button.js +0 -37
  114. package/src/blocks/columns.js +0 -47
  115. package/src/blocks/columns.lite.tsx +0 -46
  116. package/src/blocks/custom-code.js +0 -56
  117. package/src/blocks/embed.js +0 -56
  118. package/src/blocks/form.js +0 -229
  119. package/src/blocks/image.lite.tsx +0 -35
  120. package/src/blocks/img.js +0 -41
  121. package/src/blocks/input.js +0 -41
  122. package/src/blocks/raw-text.js +0 -17
  123. package/src/blocks/raw-text.lite.tsx +0 -12
  124. package/src/blocks/section.js +0 -36
  125. package/src/blocks/select.js +0 -40
  126. package/src/blocks/submit-button.js +0 -34
  127. package/src/blocks/symbol.js +0 -20
  128. package/src/blocks/symbol.lite.tsx +0 -20
  129. package/src/blocks/text.lite.tsx +0 -12
  130. package/src/blocks/textarea.js +0 -37
  131. package/src/blocks/video.js +0 -49
  132. package/src/components/render-block.js +0 -84
  133. package/src/components/render-block.lite.tsx +0 -92
  134. package/src/components/render-content.js +0 -66
  135. package/src/components/render-content.lite.tsx +0 -95
  136. package/src/functions/get-content.js +0 -103
  137. package/src/functions/get-target.js +0 -7
  138. package/src/package.json +0 -18
@@ -0,0 +1,254 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __defProps = Object.defineProperties;
3
+ var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
4
+ var __getOwnPropSymbols = Object.getOwnPropertySymbols;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __propIsEnum = Object.prototype.propertyIsEnumerable;
7
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
8
+ var __spreadValues = (a, b) => {
9
+ for (var prop in b || (b = {}))
10
+ if (__hasOwnProp.call(b, prop))
11
+ __defNormalProp(a, prop, b[prop]);
12
+ if (__getOwnPropSymbols)
13
+ for (var prop of __getOwnPropSymbols(b)) {
14
+ if (__propIsEnum.call(b, prop))
15
+ __defNormalProp(a, prop, b[prop]);
16
+ }
17
+ return a;
18
+ };
19
+ var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
20
+ var __async = (__this, __arguments, generator) => {
21
+ return new Promise((resolve, reject) => {
22
+ var fulfilled = (value) => {
23
+ try {
24
+ step(generator.next(value));
25
+ } catch (e) {
26
+ reject(e);
27
+ }
28
+ };
29
+ var rejected = (value) => {
30
+ try {
31
+ step(generator.throw(value));
32
+ } catch (e) {
33
+ reject(e);
34
+ }
35
+ };
36
+ var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
37
+ step((generator = generator.apply(__this, __arguments)).next());
38
+ });
39
+ };
40
+ import * as React from "react";
41
+ import { View } from "react-native";
42
+ import { useState, useEffect } from "react";
43
+ import { getDefaultRegisteredComponents } from "../../constants/builder-registered-components.js";
44
+ import { TARGET } from "../../constants/target.js";
45
+ import BuilderContext from "../../context/builder.context";
46
+ import { evaluate } from "../../functions/evaluate.js";
47
+ import {
48
+ convertSearchParamsToQueryObject,
49
+ getBuilderSearchParams
50
+ } from "../../functions/get-builder-search-params/index.js";
51
+ import { getContent } from "../../functions/get-content/index.js";
52
+ import { getFetch } from "../../functions/get-fetch.js";
53
+ import { isBrowser } from "../../functions/is-browser.js";
54
+ import { isEditing } from "../../functions/is-editing.js";
55
+ import { isPreviewing } from "../../functions/is-previewing.js";
56
+ import { previewingModelName } from "../../functions/previewing-model-name.js";
57
+ import {
58
+ components,
59
+ createRegisterComponentMessage
60
+ } from "../../functions/register-component.js";
61
+ import { track } from "../../functions/track.js";
62
+ import RenderBlocks from "../render-blocks.js";
63
+ import RenderContentStyles from "./components/render-styles.js";
64
+ function RenderContent(props) {
65
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l;
66
+ function useContent() {
67
+ var _a2;
68
+ const mergedContent = __spreadProps(__spreadValues(__spreadValues({}, props.content), overrideContent), {
69
+ data: __spreadValues(__spreadValues(__spreadValues({}, (_a2 = props.content) == null ? void 0 : _a2.data), props.data), overrideContent == null ? void 0 : overrideContent.data)
70
+ });
71
+ return mergedContent;
72
+ }
73
+ const [overrideContent, setOverrideContent] = useState(() => null);
74
+ const [update, setUpdate] = useState(() => 0);
75
+ const [overrideState, setOverrideState] = useState(() => ({}));
76
+ function contentState() {
77
+ var _a2, _b2;
78
+ return __spreadValues(__spreadValues(__spreadValues({}, (_b2 = (_a2 = props.content) == null ? void 0 : _a2.data) == null ? void 0 : _b2.state), props.data), overrideState);
79
+ }
80
+ function context() {
81
+ return {};
82
+ }
83
+ function allRegisteredComponents() {
84
+ const allComponentsArray = [
85
+ ...getDefaultRegisteredComponents(),
86
+ ...components,
87
+ ...props.customComponents || []
88
+ ];
89
+ const allComponents = allComponentsArray.reduce((acc, curr) => __spreadProps(__spreadValues({}, acc), { [curr.info.name]: curr }), {});
90
+ return allComponents;
91
+ }
92
+ function processMessage(event) {
93
+ const { data } = event;
94
+ if (data) {
95
+ switch (data.type) {
96
+ case "builder.contentUpdate": {
97
+ const messageContent = data.data;
98
+ const key = messageContent.key || messageContent.alias || messageContent.entry || messageContent.modelName;
99
+ const contentData = messageContent.data;
100
+ if (key === props.model) {
101
+ setOverrideContent(contentData);
102
+ }
103
+ break;
104
+ }
105
+ case "builder.patchUpdates": {
106
+ break;
107
+ }
108
+ }
109
+ }
110
+ }
111
+ function evaluateJsCode() {
112
+ var _a2, _b2;
113
+ const jsCode = (_b2 = (_a2 = useContent == null ? void 0 : useContent()) == null ? void 0 : _a2.data) == null ? void 0 : _b2.jsCode;
114
+ if (jsCode) {
115
+ evaluate({
116
+ code: jsCode,
117
+ context: context(),
118
+ state: contentState()
119
+ });
120
+ }
121
+ }
122
+ function httpReqsData() {
123
+ return {};
124
+ }
125
+ function evalExpression(expression) {
126
+ return expression.replace(/{{([^}]+)}}/g, (_match, group) => evaluate({
127
+ code: group,
128
+ context: context(),
129
+ state: contentState()
130
+ }));
131
+ }
132
+ function handleRequest({ url, key }) {
133
+ const fetchAndSetState = () => __async(this, null, function* () {
134
+ const fetch = yield getFetch();
135
+ const response = yield fetch(url);
136
+ const json = yield response.json();
137
+ const newOverrideState = __spreadProps(__spreadValues({}, overrideState), { [key]: json });
138
+ setOverrideState(newOverrideState);
139
+ });
140
+ fetchAndSetState();
141
+ }
142
+ function runHttpRequests() {
143
+ var _a2, _b2, _c2;
144
+ const requests = (_c2 = (_b2 = (_a2 = useContent == null ? void 0 : useContent()) == null ? void 0 : _a2.data) == null ? void 0 : _b2.httpRequests) != null ? _c2 : {};
145
+ Object.entries(requests).forEach(([key, url]) => {
146
+ if (url && (!httpReqsData()[key] || isEditing())) {
147
+ const evaluatedUrl = evalExpression(url);
148
+ handleRequest({
149
+ url: evaluatedUrl,
150
+ key
151
+ });
152
+ }
153
+ });
154
+ }
155
+ function emitStateUpdate() {
156
+ if (isEditing()) {
157
+ window.dispatchEvent(new CustomEvent("builder:component:stateChange", {
158
+ detail: {
159
+ state: contentState(),
160
+ ref: {
161
+ name: props.model
162
+ }
163
+ }
164
+ }));
165
+ }
166
+ }
167
+ useEffect(() => {
168
+ if (isBrowser()) {
169
+ if (isEditing()) {
170
+ Object.values(allRegisteredComponents()).forEach((registeredComponent) => {
171
+ var _a2;
172
+ const message = createRegisterComponentMessage(registeredComponent);
173
+ (_a2 = window.parent) == null ? void 0 : _a2.postMessage(message, "*");
174
+ });
175
+ window.addEventListener("message", processMessage);
176
+ window.addEventListener("builder:component:stateChangeListenerActivated", emitStateUpdate);
177
+ }
178
+ if (useContent()) {
179
+ track("impression", {
180
+ contentId: useContent().id
181
+ });
182
+ }
183
+ if (isPreviewing()) {
184
+ if (props.model && previewingModelName() === props.model) {
185
+ const currentUrl = new URL(location.href);
186
+ const previewApiKey = currentUrl.searchParams.get("apiKey");
187
+ if (previewApiKey) {
188
+ getContent({
189
+ model: props.model,
190
+ apiKey: previewApiKey,
191
+ options: getBuilderSearchParams(convertSearchParamsToQueryObject(currentUrl.searchParams))
192
+ }).then((content) => {
193
+ if (content) {
194
+ setOverrideContent(content);
195
+ }
196
+ });
197
+ }
198
+ }
199
+ }
200
+ evaluateJsCode();
201
+ runHttpRequests();
202
+ emitStateUpdate();
203
+ }
204
+ }, []);
205
+ useEffect(() => {
206
+ evaluateJsCode();
207
+ }, [(_b = (_a = useContent == null ? void 0 : useContent()) == null ? void 0 : _a.data) == null ? void 0 : _b.jsCode]);
208
+ useEffect(() => {
209
+ runHttpRequests();
210
+ }, [(_d = (_c = useContent == null ? void 0 : useContent()) == null ? void 0 : _c.data) == null ? void 0 : _d.httpRequests]);
211
+ useEffect(() => {
212
+ emitStateUpdate();
213
+ }, [contentState()]);
214
+ useEffect(() => {
215
+ return () => {
216
+ if (isBrowser()) {
217
+ window.removeEventListener("message", processMessage);
218
+ window.removeEventListener("builder:component:stateChangeListenerActivated", emitStateUpdate);
219
+ }
220
+ };
221
+ }, []);
222
+ return /* @__PURE__ */ React.createElement(BuilderContext.Provider, {
223
+ value: {
224
+ get content() {
225
+ return useContent();
226
+ },
227
+ get state() {
228
+ return contentState();
229
+ },
230
+ get context() {
231
+ return context();
232
+ },
233
+ get apiKey() {
234
+ return props.apiKey;
235
+ },
236
+ get registeredComponents() {
237
+ return allRegisteredComponents();
238
+ }
239
+ }
240
+ }, useContent() ? /* @__PURE__ */ React.createElement(React.Fragment, null, /* @__PURE__ */ React.createElement(View, {
241
+ onClick: (event) => track("click", {
242
+ contentId: useContent().id
243
+ }),
244
+ "data-builder-content-id": (_e = useContent == null ? void 0 : useContent()) == null ? void 0 : _e.id
245
+ }, (((_g = (_f = useContent == null ? void 0 : useContent()) == null ? void 0 : _f.data) == null ? void 0 : _g.cssCode) || ((_j = (_i = (_h = useContent == null ? void 0 : useContent()) == null ? void 0 : _h.data) == null ? void 0 : _i.customFonts) == null ? void 0 : _j.length)) && TARGET !== "reactNative" ? /* @__PURE__ */ React.createElement(RenderContentStyles, {
246
+ cssCode: useContent().data.cssCode,
247
+ customFonts: useContent().data.customFonts
248
+ }) : null, /* @__PURE__ */ React.createElement(RenderBlocks, {
249
+ blocks: (_l = (_k = useContent == null ? void 0 : useContent()) == null ? void 0 : _k.data) == null ? void 0 : _l.blocks
250
+ }))) : null);
251
+ }
252
+ export {
253
+ RenderContent as default
254
+ };
@@ -0,0 +1,283 @@
1
+ import * as React from "react";
2
+ import { View, StyleSheet, Image, Text } from "react-native";
3
+ import { useState, useContext, useEffect } from "react";
4
+ import { getDefaultRegisteredComponents } from "../../constants/builder-registered-components.js";
5
+ import { TARGET } from "../../constants/target.js";
6
+ import BuilderContext from "../../context/builder.context";
7
+ import { evaluate } from "../../functions/evaluate.js";
8
+ import {
9
+ convertSearchParamsToQueryObject,
10
+ getBuilderSearchParams,
11
+ } from "../../functions/get-builder-search-params/index.js";
12
+ import { getContent } from "../../functions/get-content/index.js";
13
+ import { getFetch } from "../../functions/get-fetch.js";
14
+ import { isBrowser } from "../../functions/is-browser.js";
15
+ import { isEditing } from "../../functions/is-editing.js";
16
+ import { isPreviewing } from "../../functions/is-previewing.js";
17
+ import { previewingModelName } from "../../functions/previewing-model-name.js";
18
+ import {
19
+ components,
20
+ createRegisterComponentMessage,
21
+ } from "../../functions/register-component.js";
22
+ import { track } from "../../functions/track.js";
23
+ import RenderBlocks from "../render-blocks.lite";
24
+ import RenderContentStyles from "./components/render-styles.lite";
25
+
26
+ export default function RenderContent(props) {
27
+ function useContent() {
28
+ const mergedContent = {
29
+ ...props.content,
30
+ ...overrideContent,
31
+ data: { ...props.content?.data, ...props.data, ...overrideContent?.data },
32
+ };
33
+ return mergedContent;
34
+ }
35
+
36
+ const [overrideContent, setOverrideContent] = useState(() => null);
37
+
38
+ const [update, setUpdate] = useState(() => 0);
39
+
40
+ const [overrideState, setOverrideState] = useState(() => ({}));
41
+
42
+ function contentState() {
43
+ return { ...props.content?.data?.state, ...props.data, ...overrideState };
44
+ }
45
+
46
+ function context() {
47
+ return {};
48
+ }
49
+
50
+ function allRegisteredComponents() {
51
+ const allComponentsArray = [
52
+ ...getDefaultRegisteredComponents(), // While this `components` object is deprecated, we must maintain support for it.
53
+ // Since users are able to override our default components, we need to make sure that we do not break such
54
+ // existing usage.
55
+ // This is why we spread `components` after the default Builder.io components, but before the `props.customComponents`,
56
+ // which is the new standard way of providing custom components, and must therefore take precedence.
57
+ ...components,
58
+ ...(props.customComponents || []),
59
+ ];
60
+ const allComponents = allComponentsArray.reduce(
61
+ (acc, curr) => ({ ...acc, [curr.info.name]: curr }),
62
+ {}
63
+ );
64
+ return allComponents;
65
+ }
66
+
67
+ function processMessage(event) {
68
+ const { data } = event;
69
+
70
+ if (data) {
71
+ switch (data.type) {
72
+ case "builder.contentUpdate": {
73
+ const messageContent = data.data;
74
+ const key =
75
+ messageContent.key ||
76
+ messageContent.alias ||
77
+ messageContent.entry ||
78
+ messageContent.modelName;
79
+ const contentData = messageContent.data;
80
+
81
+ if (key === props.model) {
82
+ setOverrideContent(contentData);
83
+ }
84
+
85
+ break;
86
+ }
87
+
88
+ case "builder.patchUpdates": {
89
+ // TODO
90
+ break;
91
+ }
92
+ }
93
+ }
94
+ }
95
+
96
+ function evaluateJsCode() {
97
+ // run any dynamic JS code attached to content
98
+ const jsCode = useContent?.()?.data?.jsCode;
99
+
100
+ if (jsCode) {
101
+ evaluate({
102
+ code: jsCode,
103
+ context: context(),
104
+ state: contentState(),
105
+ });
106
+ }
107
+ }
108
+
109
+ function httpReqsData() {
110
+ return {};
111
+ }
112
+
113
+ function evalExpression(expression) {
114
+ return expression.replace(/{{([^}]+)}}/g, (_match, group) =>
115
+ evaluate({
116
+ code: group,
117
+ context: context(),
118
+ state: contentState(),
119
+ })
120
+ );
121
+ }
122
+
123
+ function handleRequest({ url, key }) {
124
+ const fetchAndSetState = async () => {
125
+ const fetch = await getFetch();
126
+ const response = await fetch(url);
127
+ const json = await response.json();
128
+ const newOverrideState = { ...overrideState, [key]: json };
129
+ setOverrideState(newOverrideState);
130
+ };
131
+
132
+ fetchAndSetState();
133
+ }
134
+
135
+ function runHttpRequests() {
136
+ const requests = useContent?.()?.data?.httpRequests ?? {};
137
+ Object.entries(requests).forEach(([key, url]) => {
138
+ if (url && (!httpReqsData()[key] || isEditing())) {
139
+ const evaluatedUrl = evalExpression(url);
140
+ handleRequest({
141
+ url: evaluatedUrl,
142
+ key,
143
+ });
144
+ }
145
+ });
146
+ }
147
+
148
+ function emitStateUpdate() {
149
+ if (isEditing()) {
150
+ window.dispatchEvent(
151
+ new CustomEvent("builder:component:stateChange", {
152
+ detail: {
153
+ state: contentState(),
154
+ ref: {
155
+ name: props.model,
156
+ },
157
+ },
158
+ })
159
+ );
160
+ }
161
+ }
162
+
163
+ useEffect(() => {
164
+ if (isBrowser()) {
165
+ if (isEditing()) {
166
+ Object.values(allRegisteredComponents()).forEach(
167
+ (registeredComponent) => {
168
+ const message = createRegisterComponentMessage(registeredComponent);
169
+ window.parent?.postMessage(message, "*");
170
+ }
171
+ );
172
+ window.addEventListener("message", processMessage);
173
+ window.addEventListener(
174
+ "builder:component:stateChangeListenerActivated",
175
+ emitStateUpdate
176
+ );
177
+ }
178
+
179
+ if (useContent()) {
180
+ track("impression", {
181
+ contentId: useContent().id,
182
+ });
183
+ } // override normal content in preview mode
184
+
185
+ if (isPreviewing()) {
186
+ if (props.model && previewingModelName() === props.model) {
187
+ const currentUrl = new URL(location.href);
188
+ const previewApiKey = currentUrl.searchParams.get("apiKey");
189
+
190
+ if (previewApiKey) {
191
+ getContent({
192
+ model: props.model,
193
+ apiKey: previewApiKey,
194
+ options: getBuilderSearchParams(
195
+ convertSearchParamsToQueryObject(currentUrl.searchParams)
196
+ ),
197
+ }).then((content) => {
198
+ if (content) {
199
+ setOverrideContent(content);
200
+ }
201
+ });
202
+ }
203
+ }
204
+ }
205
+
206
+ evaluateJsCode();
207
+ runHttpRequests();
208
+ emitStateUpdate();
209
+ }
210
+ }, []);
211
+
212
+ useEffect(() => {
213
+ evaluateJsCode();
214
+ }, [useContent?.()?.data?.jsCode]);
215
+ useEffect(() => {
216
+ runHttpRequests();
217
+ }, [useContent?.()?.data?.httpRequests]);
218
+ useEffect(() => {
219
+ emitStateUpdate();
220
+ }, [contentState()]);
221
+
222
+ useEffect(() => {
223
+ return () => {
224
+ if (isBrowser()) {
225
+ window.removeEventListener("message", processMessage);
226
+ window.removeEventListener(
227
+ "builder:component:stateChangeListenerActivated",
228
+ emitStateUpdate
229
+ );
230
+ }
231
+ };
232
+ }, []);
233
+
234
+ return (
235
+ <BuilderContext.Provider
236
+ value={{
237
+ get content() {
238
+ return useContent();
239
+ },
240
+
241
+ get state() {
242
+ return contentState();
243
+ },
244
+
245
+ get context() {
246
+ return context();
247
+ },
248
+
249
+ get apiKey() {
250
+ return props.apiKey;
251
+ },
252
+
253
+ get registeredComponents() {
254
+ return allRegisteredComponents();
255
+ },
256
+ }}
257
+ >
258
+ {useContent() ? (
259
+ <>
260
+ <View
261
+ onClick={(event) =>
262
+ track("click", {
263
+ contentId: useContent().id,
264
+ })
265
+ }
266
+ data-builder-content-id={useContent?.()?.id}
267
+ >
268
+ {(useContent?.()?.data?.cssCode ||
269
+ useContent?.()?.data?.customFonts?.length) &&
270
+ TARGET !== "reactNative" ? (
271
+ <RenderContentStyles
272
+ cssCode={useContent().data.cssCode}
273
+ customFonts={useContent().data.customFonts}
274
+ />
275
+ ) : null}
276
+
277
+ <RenderBlocks blocks={useContent?.()?.data?.blocks} />
278
+ </View>
279
+ </>
280
+ ) : null}
281
+ </BuilderContext.Provider>
282
+ );
283
+ }
@@ -0,0 +1,18 @@
1
+ import * as React from "react";
2
+ import { View, Text } from "react-native";
3
+ import { TARGET } from "../constants/target.js";
4
+ function RenderInlinedStyles(props) {
5
+ function injectedStyleScript() {
6
+ return `<${tagName()}>${props.styles}</${tagName()}>`;
7
+ }
8
+ function tagName() {
9
+ return "style";
10
+ }
11
+ const TagNameRef = tagName();
12
+ return /* @__PURE__ */ React.createElement(React.Fragment, null, TARGET === "svelte" ? /* @__PURE__ */ React.createElement(React.Fragment, null, /* @__PURE__ */ React.createElement(View, {
13
+ dangerouslySetInnerHTML: { __html: injectedStyleScript() }
14
+ })) : /* @__PURE__ */ React.createElement(TagNameRef, null, /* @__PURE__ */ React.createElement(Text, null, props.styles)));
15
+ }
16
+ export {
17
+ RenderInlinedStyles as default
18
+ };
@@ -0,0 +1,31 @@
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 `<${tagName()}>${props.styles}</${tagName()}>`;
8
+ }
9
+
10
+ function tagName() {
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
+ const TagNameRef = tagName();
17
+
18
+ return (
19
+ <>
20
+ {TARGET === "svelte" ? (
21
+ <>
22
+ <View dangerouslySetInnerHTML={{ __html: injectedStyleScript() }} />
23
+ </>
24
+ ) : (
25
+ <TagNameRef>
26
+ <Text>{props.styles}</Text>
27
+ </TagNameRef>
28
+ )}
29
+ </>
30
+ );
31
+ }
@@ -0,0 +1,27 @@
1
+ import * as React from 'react';
2
+ import { default as Button } from "../blocks/button/button.js";
3
+ import { componentInfo as buttonComponentInfo } from "../blocks/button/component-info";
4
+ import { default as Columns } from "../blocks/columns/columns.js";
5
+ import { componentInfo as columnsComponentInfo } from "../blocks/columns/component-info";
6
+ import { componentInfo as fragmentComponentInfo } from "../blocks/fragment/component-info";
7
+ import { default as Fragment } from "../blocks/fragment/fragment.js";
8
+ import { componentInfo as imageComponentInfo } from "../blocks/image/component-info";
9
+ import { default as Image } from "../blocks/image/image.js";
10
+ import { componentInfo as sectionComponentInfo } from "../blocks/section/component-info";
11
+ import { default as Section } from "../blocks/section/section.js";
12
+ import { componentInfo as symbolComponentInfo } from "../blocks/symbol/component-info";
13
+ import { default as Symbol } from "../blocks/symbol/symbol.js";
14
+ import { componentInfo as textComponentInfo } from "../blocks/text/component-info";
15
+ import { default as Text } from "../blocks/text/text.js";
16
+ const getDefaultRegisteredComponents = () => [
17
+ { component: Columns, info: columnsComponentInfo },
18
+ { component: Image, info: imageComponentInfo },
19
+ { component: Text, info: textComponentInfo },
20
+ { component: Symbol, info: symbolComponentInfo },
21
+ { component: Button, info: buttonComponentInfo },
22
+ { component: Section, info: sectionComponentInfo },
23
+ { component: Fragment, info: fragmentComponentInfo }
24
+ ];
25
+ export {
26
+ getDefaultRegisteredComponents
27
+ };
@@ -0,0 +1,5 @@
1
+ import * as React from 'react';
2
+ const TARGET = "reactNative";
3
+ export {
4
+ TARGET
5
+ };
@@ -1,5 +1,11 @@
1
1
  import { createContext } from "react";
2
- var stdin_default = createContext({ content: null, context: {}, state: {} });
2
+ var stdin_default = createContext({
3
+ content: null,
4
+ context: {},
5
+ state: {},
6
+ apiKey: null,
7
+ registeredComponents: {}
8
+ });
3
9
  export {
4
10
  stdin_default as default
5
11
  };
@@ -1,18 +1,27 @@
1
1
  import * as React from 'react';
2
- import { isBrowser } from "./is-browser";
3
- import { isEditing } from "./is-editing";
4
- function evaluate(options) {
5
- const { code } = options;
2
+ import { isBrowser } from "./is-browser.js";
3
+ import { isEditing } from "./is-editing.js";
4
+ function evaluate({
5
+ code,
6
+ context,
7
+ state,
8
+ event
9
+ }) {
10
+ if (code === "") {
11
+ console.warn("Skipping evaluation of empty code block.");
12
+ return;
13
+ }
6
14
  const builder = {
7
15
  isEditing: isEditing(),
8
- isBrowser: isBrowser()
16
+ isBrowser: isBrowser(),
17
+ isServer: !isBrowser()
9
18
  };
10
19
  const useReturn = !(code.includes(";") || code.includes(" return ") || code.trim().startsWith("return "));
11
- const useCode = `${useReturn ? `return (${code});` : code}`;
20
+ const useCode = useReturn ? `return (${code});` : code;
12
21
  try {
13
- return new Function("builder", "Builder", "state", "context", "event", useCode)(builder, builder, options.state, options.context, options.event);
22
+ return new Function("builder", "Builder", "state", "context", "event", useCode)(builder, builder, state, context, event);
14
23
  } catch (e) {
15
- console.warn("Builder custom code error", e);
24
+ console.warn("Builder custom code error: ", e);
16
25
  }
17
26
  }
18
27
  export {
@@ -0,0 +1,8 @@
1
+ import * as React from 'react';
2
+ function capitalizeFirstLetter(string) {
3
+ return string.charAt(0).toUpperCase() + string.slice(1);
4
+ }
5
+ const getEventHandlerName = (key) => `on${capitalizeFirstLetter(key)}`;
6
+ export {
7
+ getEventHandlerName
8
+ };
@@ -0,0 +1,5 @@
1
+ import * as React from 'react';
2
+ const fastClone = (obj) => JSON.parse(JSON.stringify(obj));
3
+ export {
4
+ fastClone
5
+ };