@builder.io/sdk-react-native 0.0.1-44 → 0.0.1-50

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 (88) hide show
  1. package/CHANGELOG.md +14 -0
  2. package/package.json +1 -1
  3. package/src/blocks/button.js +23 -73
  4. package/src/blocks/button.lite.tsx +3 -3
  5. package/src/blocks/columns.js +56 -288
  6. package/src/blocks/columns.lite.tsx +22 -22
  7. package/src/blocks/custom-code.js +46 -76
  8. package/src/blocks/custom-code.lite.tsx +11 -19
  9. package/src/blocks/embed.js +46 -69
  10. package/src/blocks/embed.lite.tsx +11 -15
  11. package/src/blocks/form.js +194 -491
  12. package/src/blocks/form.lite.tsx +51 -51
  13. package/src/blocks/fragment.js +8 -16
  14. package/src/blocks/fragment.lite.tsx +2 -2
  15. package/src/blocks/image.js +91 -113
  16. package/src/blocks/image.lite.tsx +12 -13
  17. package/src/blocks/img.js +28 -51
  18. package/src/blocks/img.lite.tsx +6 -6
  19. package/src/blocks/input.js +28 -100
  20. package/src/blocks/input.lite.tsx +4 -4
  21. package/src/blocks/raw-text.js +10 -16
  22. package/src/blocks/raw-text.lite.tsx +3 -8
  23. package/src/blocks/section.js +23 -71
  24. package/src/blocks/section.lite.tsx +3 -3
  25. package/src/blocks/select.js +28 -78
  26. package/src/blocks/select.lite.tsx +5 -5
  27. package/src/blocks/submit-button.js +21 -47
  28. package/src/blocks/submit-button.lite.tsx +2 -2
  29. package/src/blocks/symbol.js +71 -20
  30. package/src/blocks/symbol.lite.tsx +48 -8
  31. package/src/blocks/text.js +27 -40
  32. package/src/blocks/text.lite.tsx +3 -8
  33. package/src/blocks/textarea.js +24 -60
  34. package/src/blocks/textarea.lite.tsx +2 -2
  35. package/src/blocks/video.js +66 -71
  36. package/src/blocks/video.lite.tsx +5 -5
  37. package/src/components/block-styles.js +4 -2
  38. package/src/components/block-styles.lite.tsx +2 -2
  39. package/src/components/error-boundary.js +9 -11
  40. package/src/components/error-boundary.lite.tsx +2 -2
  41. package/src/components/render-block.js +44 -100
  42. package/src/components/render-block.lite.tsx +23 -28
  43. package/src/components/render-blocks.js +41 -60
  44. package/src/components/render-blocks.lite.tsx +20 -14
  45. package/src/components/render-content.js +189 -142
  46. package/src/components/render-content.lite.tsx +166 -60
  47. package/src/constants/device-sizes.js +11 -8
  48. package/src/context/builder.context.js +10 -3
  49. package/src/functions/evaluate.js +20 -20
  50. package/src/functions/event-handler-name.js +3 -1
  51. package/src/functions/get-block-actions.js +17 -16
  52. package/src/functions/get-block-component-options.js +9 -16
  53. package/src/functions/get-block-properties.js +14 -18
  54. package/src/functions/get-block-styles.js +17 -34
  55. package/src/functions/get-block-tag.js +4 -2
  56. package/src/functions/get-builder-search-params/fn.test.js +14 -0
  57. package/src/functions/get-builder-search-params/index.js +23 -0
  58. package/src/functions/get-content/fn.test.js +32 -0
  59. package/src/functions/get-content/index.js +138 -0
  60. package/src/functions/get-fetch.js +7 -5
  61. package/src/functions/get-global-this.js +7 -5
  62. package/src/functions/get-processed-block.js +13 -17
  63. package/src/functions/get-processed-block.test.js +17 -16
  64. package/src/functions/get-target.js +4 -2
  65. package/src/functions/if-target.js +3 -1
  66. package/src/functions/is-browser.js +4 -2
  67. package/src/functions/is-editing.js +5 -5
  68. package/src/functions/is-iframe.js +4 -2
  69. package/src/functions/is-previewing.js +6 -4
  70. package/src/functions/is-react-native.js +3 -1
  71. package/src/functions/macro-eval.js +5 -2
  72. package/src/functions/on-change.js +7 -4
  73. package/src/functions/on-change.test.js +9 -10
  74. package/src/functions/previewing-model-name.js +5 -3
  75. package/src/functions/register-component.js +27 -38
  76. package/src/functions/register.js +10 -8
  77. package/src/functions/set-editor-settings.js +7 -5
  78. package/src/functions/set.js +5 -14
  79. package/src/functions/set.test.js +13 -14
  80. package/src/functions/track.js +12 -6
  81. package/src/functions/transform-block.js +13 -17
  82. package/src/index-helpers/blocks-exports.js +19 -0
  83. package/src/index-helpers/top-of-file.js +2 -0
  84. package/src/index.js +11 -26
  85. package/src/scripts/init-editing.js +40 -64
  86. package/src/types/typescript.js +1 -0
  87. package/src/functions/get-content.js +0 -152
  88. package/src/functions/get-content.test.js +0 -58
@@ -1,37 +1,85 @@
1
- import * as React from 'react';
2
- import { View, Text } from 'react-native';
3
- import { useState, useEffect } from 'react';
4
- import { isBrowser } from '../functions/is-browser';
5
- import RenderBlock from './render-block';
6
- import BuilderContext from '../context/builder.context';
7
- import { track } from '../functions/track';
8
- import { isReactNative } from '../functions/is-react-native';
9
- import { isEditing } from '../functions/is-editing';
10
- import { isPreviewing } from '../functions/is-previewing';
11
- import { previewingModelName } from '../functions/previewing-model-name';
12
- import { getContent } from '../functions/get-content';
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, Text } from "react-native";
42
+ import { useState, useEffect } from "react";
43
+ import { isBrowser } from "../functions/is-browser";
44
+ import BuilderContext from "../context/builder.context";
45
+ import { track } from "../functions/track";
46
+ import { isReactNative } from "../functions/is-react-native";
47
+ import { isEditing } from "../functions/is-editing";
48
+ import { isPreviewing } from "../functions/is-previewing";
49
+ import { previewingModelName } from "../functions/previewing-model-name";
50
+ import { getContent } from "../functions/get-content";
51
+ import {
52
+ convertSearchParamsToQueryObject,
53
+ getBuilderSearchParams
54
+ } from "../functions/get-builder-search-params";
55
+ import RenderBlocks from "./render-blocks";
56
+ import { evaluate } from "../functions/evaluate";
57
+ import { getFetch } from "../functions/get-fetch";
13
58
  function RenderContent(props) {
14
- var _a, _b, _c, _d, _e, _f, _g, _h, _i;
59
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l;
15
60
  function useContent() {
16
- return overrideContent || props.content;
61
+ var _a2;
62
+ const mergedContent = __spreadProps(__spreadValues(__spreadValues({}, props.content), overrideContent), {
63
+ data: __spreadValues(__spreadValues({}, (_a2 = props.content) == null ? void 0 : _a2.data), overrideContent == null ? void 0 : overrideContent.data)
64
+ });
65
+ return mergedContent;
17
66
  }
18
- const [update, setUpdate] = useState(() => 0);
19
- const [state, setState] = useState(() => ({}));
20
- const [context, setContext] = useState(() => ({}));
21
67
  const [overrideContent, setOverrideContent] = useState(() => null);
68
+ const [update, setUpdate] = useState(() => 0);
69
+ const [overrideState, setOverrideState] = useState(() => ({}));
70
+ function state() {
71
+ var _a2, _b2;
72
+ return __spreadValues(__spreadValues({}, (_b2 = (_a2 = props.content) == null ? void 0 : _a2.data) == null ? void 0 : _b2.state), overrideState);
73
+ }
74
+ function context() {
75
+ return {};
76
+ }
22
77
  function getCssFromFont(font, data) {
23
78
  var _a2, _b2;
24
- const family =
25
- font.family +
26
- (font.kind && !font.kind.includes('#') ? ', ' + font.kind : '');
27
- const name = family.split(',')[0];
28
- const url =
29
- (_b2 = font.fileUrl) != null
30
- ? _b2
31
- : (_a2 = font == null ? void 0 : font.files) == null
32
- ? void 0
33
- : _a2.regular;
34
- let str = '';
79
+ const family = font.family + (font.kind && !font.kind.includes("#") ? ", " + font.kind : "");
80
+ const name = family.split(",")[0];
81
+ const url = (_b2 = font.fileUrl) != null ? _b2 : (_a2 = font == null ? void 0 : font.files) == null ? void 0 : _a2.regular;
82
+ let str = "";
35
83
  if (url && family && name) {
36
84
  str += `
37
85
  @font-face {
@@ -65,63 +113,100 @@ function RenderContent(props) {
65
113
  }
66
114
  function getFontCss(data) {
67
115
  var _a2, _b2;
68
- return (
69
- ((_b2 =
70
- (_a2 = data == null ? void 0 : data.customFonts) == null
71
- ? void 0
72
- : _a2.map((font) => getCssFromFont(font, data))) == null
73
- ? void 0
74
- : _b2.join(' ')) || ''
75
- );
116
+ return ((_b2 = (_a2 = data == null ? void 0 : data.customFonts) == null ? void 0 : _a2.map((font) => getCssFromFont(font, data))) == null ? void 0 : _b2.join(" ")) || "";
76
117
  }
77
118
  function processMessage(event) {
78
119
  const { data } = event;
79
120
  if (data) {
80
121
  switch (data.type) {
81
- case 'builder.contentUpdate': {
82
- const key =
83
- data.data.key ||
84
- data.data.alias ||
85
- data.data.entry ||
86
- data.data.modelName;
87
- const contentData = data.data.data;
122
+ case "builder.contentUpdate": {
123
+ const messageContent = data.data;
124
+ const key = messageContent.key || messageContent.alias || messageContent.entry || messageContent.modelName;
125
+ const contentData = messageContent.data;
88
126
  if (key === props.model) {
89
127
  setOverrideContent(contentData);
90
128
  }
91
129
  break;
92
130
  }
93
- case 'builder.patchUpdates': {
131
+ case "builder.patchUpdates": {
94
132
  break;
95
133
  }
96
134
  }
97
135
  }
98
136
  }
137
+ function evaluateJsCode() {
138
+ var _a2, _b2;
139
+ const jsCode = (_b2 = (_a2 = useContent == null ? void 0 : useContent()) == null ? void 0 : _a2.data) == null ? void 0 : _b2.jsCode;
140
+ if (jsCode) {
141
+ evaluate({
142
+ code: jsCode,
143
+ context: context(),
144
+ state: state()
145
+ });
146
+ }
147
+ }
148
+ function httpReqsData() {
149
+ return {};
150
+ }
151
+ function evalExpression(expression) {
152
+ return expression.replace(/{{([^}]+)}}/g, (_match, group) => evaluate({
153
+ code: group,
154
+ context: context(),
155
+ state: state()
156
+ }));
157
+ }
158
+ function handleRequest({ url, key }) {
159
+ const fetchAndSetState = () => __async(this, null, function* () {
160
+ const response = yield getFetch()(url);
161
+ const json = yield response.json();
162
+ const newOverrideState = __spreadProps(__spreadValues({}, overrideState), { [key]: json });
163
+ setOverrideState(newOverrideState);
164
+ });
165
+ fetchAndSetState();
166
+ }
167
+ function runHttpRequests() {
168
+ var _a2, _b2, _c2;
169
+ const requests = (_c2 = (_b2 = (_a2 = useContent == null ? void 0 : useContent()) == null ? void 0 : _a2.data) == null ? void 0 : _b2.httpRequests) != null ? _c2 : {};
170
+ Object.entries(requests).forEach(([key, url]) => {
171
+ if (url && (!httpReqsData()[key] || isEditing())) {
172
+ const evaluatedUrl = evalExpression(url);
173
+ handleRequest({
174
+ url: evaluatedUrl,
175
+ key
176
+ });
177
+ }
178
+ });
179
+ }
180
+ function emitStateUpdate() {
181
+ window.dispatchEvent(new CustomEvent("builder:component:stateChange", {
182
+ detail: {
183
+ state: state(),
184
+ ref: {
185
+ name: props.model
186
+ }
187
+ }
188
+ }));
189
+ }
99
190
  useEffect(() => {
100
191
  if (isBrowser()) {
101
192
  if (isEditing()) {
102
- window.addEventListener('message', processMessage);
193
+ window.addEventListener("message", processMessage);
194
+ window.addEventListener("builder:component:stateChangeListenerActivated", emitStateUpdate);
103
195
  }
104
- if (useContent() && !isEditing()) {
105
- track('impression', {
106
- contentId: useContent().id,
196
+ if (useContent()) {
197
+ track("impression", {
198
+ contentId: useContent().id
107
199
  });
108
200
  }
109
201
  if (isPreviewing()) {
110
202
  if (props.model && previewingModelName() === props.model) {
111
- const options = {};
112
203
  const currentUrl = new URL(location.href);
113
- const apiKey = currentUrl.searchParams.get('apiKey');
114
- if (apiKey) {
115
- const builderPrefix = 'builder.';
116
- currentUrl.searchParams.forEach((value, key) => {
117
- if (key.startsWith(builderPrefix)) {
118
- options[key.replace(builderPrefix, '')] = value;
119
- }
120
- });
204
+ const previewApiKey = currentUrl.searchParams.get("apiKey");
205
+ if (previewApiKey) {
121
206
  getContent({
122
207
  model: props.model,
123
- apiKey,
124
- options,
208
+ apiKey: previewApiKey,
209
+ options: getBuilderSearchParams(convertSearchParamsToQueryObject(currentUrl.searchParams))
125
210
  }).then((content) => {
126
211
  if (content) {
127
212
  setOverrideContent(content);
@@ -130,90 +215,52 @@ function RenderContent(props) {
130
215
  }
131
216
  }
132
217
  }
218
+ evaluateJsCode();
219
+ runHttpRequests();
220
+ emitStateUpdate();
133
221
  }
134
222
  }, []);
135
- return /* @__PURE__ */ React.createElement(
136
- BuilderContext.Provider,
137
- {
138
- value: {
139
- get content() {
140
- return useContent();
141
- },
142
- get state() {
143
- return state;
144
- },
145
- get context() {
146
- return context;
147
- },
223
+ useEffect(() => {
224
+ evaluateJsCode();
225
+ }, [(_b = (_a = useContent == null ? void 0 : useContent()) == null ? void 0 : _a.data) == null ? void 0 : _b.jsCode]);
226
+ useEffect(() => {
227
+ runHttpRequests();
228
+ }, [(_d = (_c = useContent == null ? void 0 : useContent()) == null ? void 0 : _c.data) == null ? void 0 : _d.httpRequests]);
229
+ useEffect(() => {
230
+ emitStateUpdate();
231
+ }, [state()]);
232
+ useEffect(() => {
233
+ return () => {
234
+ if (isBrowser()) {
235
+ window.removeEventListener("message", processMessage);
236
+ window.removeEventListener("builder:component:stateChangeListenerActivated", emitStateUpdate);
237
+ }
238
+ };
239
+ }, []);
240
+ return /* @__PURE__ */ React.createElement(BuilderContext.Provider, {
241
+ value: {
242
+ get content() {
243
+ return useContent();
148
244
  },
149
- },
150
- useContent()
151
- ? /* @__PURE__ */ React.createElement(
152
- React.Fragment,
153
- null,
154
- /* @__PURE__ */ React.createElement(
155
- View,
156
- {
157
- onClick: (event) => {
158
- if (!isEditing()) {
159
- track('click', {
160
- contentId: useContent().id,
161
- });
162
- }
163
- },
164
- 'data-builder-content-id':
165
- (_a = useContent == null ? void 0 : useContent()) == null
166
- ? void 0
167
- : _a.id,
168
- },
169
- (((_c =
170
- (_b = useContent == null ? void 0 : useContent()) == null
171
- ? void 0
172
- : _b.data) == null
173
- ? void 0
174
- : _c.cssCode) ||
175
- ((_f =
176
- (_e =
177
- (_d = useContent == null ? void 0 : useContent()) == null
178
- ? void 0
179
- : _d.data) == null
180
- ? void 0
181
- : _e.customFonts) == null
182
- ? void 0
183
- : _f.length)) &&
184
- !isReactNative()
185
- ? /* @__PURE__ */ React.createElement(
186
- View,
187
- null,
188
- /* @__PURE__ */ React.createElement(
189
- Text,
190
- null,
191
- useContent().data.cssCode
192
- ),
193
- /* @__PURE__ */ React.createElement(
194
- Text,
195
- null,
196
- getFontCss(useContent().data)
197
- )
198
- )
199
- : null,
200
- (_i =
201
- (_h =
202
- (_g = useContent == null ? void 0 : useContent()) == null
203
- ? void 0
204
- : _g.data) == null
205
- ? void 0
206
- : _h.blocks) == null
207
- ? void 0
208
- : _i.map((block) =>
209
- /* @__PURE__ */ React.createElement(RenderBlock, {
210
- key: block.id,
211
- block,
212
- })
213
- )
214
- )
215
- )
216
- : null
217
- );
245
+ get state() {
246
+ return state();
247
+ },
248
+ get context() {
249
+ return context();
250
+ },
251
+ get apiKey() {
252
+ return props.apiKey;
253
+ }
254
+ }
255
+ }, useContent() ? /* @__PURE__ */ React.createElement(React.Fragment, null, /* @__PURE__ */ React.createElement(View, {
256
+ onClick: (event) => track("click", {
257
+ contentId: useContent().id
258
+ }),
259
+ "data-builder-content-id": (_e = useContent == null ? void 0 : useContent()) == null ? void 0 : _e.id
260
+ }, (((_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)) && !isReactNative() ? /* @__PURE__ */ React.createElement(View, null, /* @__PURE__ */ React.createElement(Text, null, useContent().data.cssCode), /* @__PURE__ */ React.createElement(Text, null, state().getFontCss(useContent().data))) : null, /* @__PURE__ */ React.createElement(RenderBlocks, {
261
+ blocks: (_l = (_k = useContent == null ? void 0 : useContent()) == null ? void 0 : _k.data) == null ? void 0 : _l.blocks
262
+ }))) : null);
218
263
  }
219
- export { RenderContent as default };
264
+ export {
265
+ RenderContent as default
266
+ };