@builder.io/react 8.0.5 → 8.0.6-1

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 (231) hide show
  1. package/.npmrc +1 -0
  2. package/package.json +5 -4
  3. package/scripts/fix-core-version.sh +0 -0
  4. package/scripts/set-sdk-version.sh +0 -0
  5. package/src/components/builder-block.component.tsx +9 -0
  6. package/src/components/builder-component.component.tsx +1 -0
  7. package/src/constants/device-sizes.constant.ts +22 -6
  8. package/.yarnrc.yml +0 -1
  9. package/dist/builder-react-lite.cjs.js +0 -2
  10. package/dist/builder-react-lite.cjs.js.map +0 -1
  11. package/dist/builder-react-lite.esm.js +0 -2
  12. package/dist/builder-react-lite.esm.js.map +0 -1
  13. package/dist/builder-react.browser.js +0 -11
  14. package/dist/builder-react.browser.js.map +0 -1
  15. package/dist/builder-react.cjs.js +0 -2
  16. package/dist/builder-react.cjs.js.map +0 -1
  17. package/dist/builder-react.es5.js +0 -2
  18. package/dist/builder-react.es5.js.map +0 -1
  19. package/dist/builder-react.unpkg.js +0 -11
  20. package/dist/builder-react.unpkg.js.map +0 -1
  21. package/dist/lib/lib/on-change.js +0 -228
  22. package/dist/lib/lib/on-change.js.map +0 -1
  23. package/dist/lib/package.json +0 -109
  24. package/dist/lib/rollup.config.js +0 -131
  25. package/dist/lib/rollup.config.js.map +0 -1
  26. package/dist/lib/src/blocks/Button.js +0 -86
  27. package/dist/lib/src/blocks/Button.js.map +0 -1
  28. package/dist/lib/src/blocks/Columns.js +0 -243
  29. package/dist/lib/src/blocks/Columns.js.map +0 -1
  30. package/dist/lib/src/blocks/CustomCode.js +0 -227
  31. package/dist/lib/src/blocks/CustomCode.js.map +0 -1
  32. package/dist/lib/src/blocks/Embed.js +0 -114
  33. package/dist/lib/src/blocks/Embed.js.map +0 -1
  34. package/dist/lib/src/blocks/Fragment.js +0 -45
  35. package/dist/lib/src/blocks/Fragment.js.map +0 -1
  36. package/dist/lib/src/blocks/Image.js +0 -532
  37. package/dist/lib/src/blocks/Image.js.map +0 -1
  38. package/dist/lib/src/blocks/Mutation.js +0 -93
  39. package/dist/lib/src/blocks/Mutation.js.map +0 -1
  40. package/dist/lib/src/blocks/PersonalizationContainer.js +0 -174
  41. package/dist/lib/src/blocks/PersonalizationContainer.js.map +0 -1
  42. package/dist/lib/src/blocks/Router.js +0 -354
  43. package/dist/lib/src/blocks/Router.js.map +0 -1
  44. package/dist/lib/src/blocks/Section.js +0 -150
  45. package/dist/lib/src/blocks/Section.js.map +0 -1
  46. package/dist/lib/src/blocks/Slot.js +0 -41
  47. package/dist/lib/src/blocks/Slot.js.map +0 -1
  48. package/dist/lib/src/blocks/StateProvider.js +0 -61
  49. package/dist/lib/src/blocks/StateProvider.js.map +0 -1
  50. package/dist/lib/src/blocks/Symbol.js +0 -192
  51. package/dist/lib/src/blocks/Symbol.js.map +0 -1
  52. package/dist/lib/src/blocks/Text.js +0 -132
  53. package/dist/lib/src/blocks/Text.js.map +0 -1
  54. package/dist/lib/src/blocks/Video.js +0 -261
  55. package/dist/lib/src/blocks/Video.js.map +0 -1
  56. package/dist/lib/src/blocks/forms/Button.js +0 -74
  57. package/dist/lib/src/blocks/forms/Button.js.map +0 -1
  58. package/dist/lib/src/blocks/forms/Form.js +0 -590
  59. package/dist/lib/src/blocks/forms/Form.js.map +0 -1
  60. package/dist/lib/src/blocks/forms/Input.js +0 -133
  61. package/dist/lib/src/blocks/forms/Input.js.map +0 -1
  62. package/dist/lib/src/blocks/forms/Label.js +0 -81
  63. package/dist/lib/src/blocks/forms/Label.js.map +0 -1
  64. package/dist/lib/src/blocks/forms/Select.js +0 -104
  65. package/dist/lib/src/blocks/forms/Select.js.map +0 -1
  66. package/dist/lib/src/blocks/forms/TextArea.js +0 -89
  67. package/dist/lib/src/blocks/forms/TextArea.js.map +0 -1
  68. package/dist/lib/src/blocks/raw/Img.js +0 -66
  69. package/dist/lib/src/blocks/raw/Img.js.map +0 -1
  70. package/dist/lib/src/blocks/raw/RawText.js +0 -47
  71. package/dist/lib/src/blocks/raw/RawText.js.map +0 -1
  72. package/dist/lib/src/builder-react-lite.js +0 -31
  73. package/dist/lib/src/builder-react-lite.js.map +0 -1
  74. package/dist/lib/src/builder-react.js +0 -106
  75. package/dist/lib/src/builder-react.js.map +0 -1
  76. package/dist/lib/src/components/Link.js +0 -35
  77. package/dist/lib/src/components/Link.js.map +0 -1
  78. package/dist/lib/src/components/builder-block.component.js +0 -550
  79. package/dist/lib/src/components/builder-block.component.js.map +0 -1
  80. package/dist/lib/src/components/builder-blocks.component.js +0 -167
  81. package/dist/lib/src/components/builder-blocks.component.js.map +0 -1
  82. package/dist/lib/src/components/builder-component.component.js +0 -1125
  83. package/dist/lib/src/components/builder-component.component.js.map +0 -1
  84. package/dist/lib/src/components/builder-content.component.js +0 -321
  85. package/dist/lib/src/components/builder-content.component.js.map +0 -1
  86. package/dist/lib/src/components/insert-spacer.component.js +0 -106
  87. package/dist/lib/src/components/insert-spacer.component.js.map +0 -1
  88. package/dist/lib/src/components/no-wrap.js +0 -7
  89. package/dist/lib/src/components/no-wrap.js.map +0 -1
  90. package/dist/lib/src/components/variants-provider.component.js +0 -108
  91. package/dist/lib/src/components/variants-provider.component.js.map +0 -1
  92. package/dist/lib/src/constants/device-sizes.constant.js +0 -80
  93. package/dist/lib/src/constants/device-sizes.constant.js.map +0 -1
  94. package/dist/lib/src/constants/file-types.constant.js +0 -72
  95. package/dist/lib/src/constants/file-types.constant.js.map +0 -1
  96. package/dist/lib/src/decorators/builder-block.decorator.js +0 -10
  97. package/dist/lib/src/decorators/builder-block.decorator.js.map +0 -1
  98. package/dist/lib/src/functions/apply-patch-with-mutation.js +0 -80
  99. package/dist/lib/src/functions/apply-patch-with-mutation.js.map +0 -1
  100. package/dist/lib/src/functions/apply-patch-with-mutation.test.js +0 -54
  101. package/dist/lib/src/functions/apply-patch-with-mutation.test.js.map +0 -1
  102. package/dist/lib/src/functions/block-to-html-string.js +0 -21
  103. package/dist/lib/src/functions/block-to-html-string.js.map +0 -1
  104. package/dist/lib/src/functions/debonce-next-tick.js +0 -43
  105. package/dist/lib/src/functions/debonce-next-tick.js.map +0 -1
  106. package/dist/lib/src/functions/extract-localized-values.js +0 -35
  107. package/dist/lib/src/functions/extract-localized-values.js.map +0 -1
  108. package/dist/lib/src/functions/filter-with-custom-targeting.js +0 -88
  109. package/dist/lib/src/functions/filter-with-custom-targeting.js.map +0 -1
  110. package/dist/lib/src/functions/get-builder-pixel.js +0 -27
  111. package/dist/lib/src/functions/get-builder-pixel.js.map +0 -1
  112. package/dist/lib/src/functions/get.js +0 -12
  113. package/dist/lib/src/functions/get.js.map +0 -1
  114. package/dist/lib/src/functions/is-debug.js +0 -8
  115. package/dist/lib/src/functions/is-debug.js.map +0 -1
  116. package/dist/lib/src/functions/no-wrap.js +0 -60
  117. package/dist/lib/src/functions/no-wrap.js.map +0 -1
  118. package/dist/lib/src/functions/safe-dynamic-require.js +0 -51
  119. package/dist/lib/src/functions/safe-dynamic-require.js.map +0 -1
  120. package/dist/lib/src/functions/set.js +0 -21
  121. package/dist/lib/src/functions/set.js.map +0 -1
  122. package/dist/lib/src/functions/should-force-browser-runtime-in-node.js +0 -22
  123. package/dist/lib/src/functions/should-force-browser-runtime-in-node.js.map +0 -1
  124. package/dist/lib/src/functions/string-to-function.js +0 -166
  125. package/dist/lib/src/functions/string-to-function.js.map +0 -1
  126. package/dist/lib/src/functions/throttle.js +0 -42
  127. package/dist/lib/src/functions/throttle.js.map +0 -1
  128. package/dist/lib/src/functions/traverse.js +0 -72
  129. package/dist/lib/src/functions/traverse.js.map +0 -1
  130. package/dist/lib/src/functions/try-eval.js +0 -75
  131. package/dist/lib/src/functions/try-eval.js.map +0 -1
  132. package/dist/lib/src/functions/update-metadata.js +0 -42
  133. package/dist/lib/src/functions/update-metadata.js.map +0 -1
  134. package/dist/lib/src/functions/utils.js +0 -20
  135. package/dist/lib/src/functions/utils.js.map +0 -1
  136. package/dist/lib/src/functions/with-builder.js +0 -10
  137. package/dist/lib/src/functions/with-builder.js.map +0 -1
  138. package/dist/lib/src/functions/with-children.js +0 -77
  139. package/dist/lib/src/functions/with-children.js.map +0 -1
  140. package/dist/lib/src/hooks/useIsPreviewing.js +0 -16
  141. package/dist/lib/src/hooks/useIsPreviewing.js.map +0 -1
  142. package/dist/lib/src/scripts/init-editing.js +0 -18
  143. package/dist/lib/src/scripts/init-editing.js.map +0 -1
  144. package/dist/lib/src/sdk-version.js +0 -5
  145. package/dist/lib/src/sdk-version.js.map +0 -1
  146. package/dist/lib/src/store/builder-async-requests.js +0 -19
  147. package/dist/lib/src/store/builder-async-requests.js.map +0 -1
  148. package/dist/lib/src/store/builder-meta.js +0 -13
  149. package/dist/lib/src/store/builder-meta.js.map +0 -1
  150. package/dist/lib/src/store/builder-store.js +0 -15
  151. package/dist/lib/src/store/builder-store.js.map +0 -1
  152. package/dist/lib/src/to-error.js +0 -22
  153. package/dist/lib/src/to-error.js.map +0 -1
  154. package/dist/lib/test/basic.test.js +0 -212
  155. package/dist/lib/test/basic.test.js.map +0 -1
  156. package/dist/lib/test/functions/render-block.js +0 -24
  157. package/dist/lib/test/functions/render-block.js.map +0 -1
  158. package/dist/lib/test/image.test.js +0 -107
  159. package/dist/lib/test/image.test.js.map +0 -1
  160. package/dist/lib/test/setupTests.js +0 -8
  161. package/dist/lib/test/setupTests.js.map +0 -1
  162. package/dist/types/lib/on-change.d.ts +0 -8
  163. package/dist/types/rollup.config.d.ts +0 -44
  164. package/dist/types/src/blocks/Button.d.ts +0 -7
  165. package/dist/types/src/blocks/Columns.d.ts +0 -1
  166. package/dist/types/src/blocks/CustomCode.d.ts +0 -1
  167. package/dist/types/src/blocks/Embed.d.ts +0 -1
  168. package/dist/types/src/blocks/Fragment.d.ts +0 -5
  169. package/dist/types/src/blocks/Image.d.ts +0 -6
  170. package/dist/types/src/blocks/Mutation.d.ts +0 -8
  171. package/dist/types/src/blocks/PersonalizationContainer.d.ts +0 -19
  172. package/dist/types/src/blocks/Router.d.ts +0 -32
  173. package/dist/types/src/blocks/Section.d.ts +0 -1
  174. package/dist/types/src/blocks/Slot.d.ts +0 -5
  175. package/dist/types/src/blocks/StateProvider.d.ts +0 -1
  176. package/dist/types/src/blocks/Symbol.d.ts +0 -19
  177. package/dist/types/src/blocks/Text.d.ts +0 -6
  178. package/dist/types/src/blocks/Video.d.ts +0 -1
  179. package/dist/types/src/blocks/forms/Button.d.ts +0 -5
  180. package/dist/types/src/blocks/forms/Form.d.ts +0 -24
  181. package/dist/types/src/blocks/forms/Input.d.ts +0 -10
  182. package/dist/types/src/blocks/forms/Label.d.ts +0 -8
  183. package/dist/types/src/blocks/forms/Select.d.ts +0 -12
  184. package/dist/types/src/blocks/forms/TextArea.d.ts +0 -9
  185. package/dist/types/src/blocks/raw/Img.d.ts +0 -7
  186. package/dist/types/src/blocks/raw/RawText.d.ts +0 -8
  187. package/dist/types/src/builder-react-lite.d.ts +0 -17
  188. package/dist/types/src/builder-react.d.ts +0 -45
  189. package/dist/types/src/components/Link.d.ts +0 -9
  190. package/dist/types/src/components/builder-block.component.d.ts +0 -50
  191. package/dist/types/src/components/builder-blocks.component.d.ts +0 -29
  192. package/dist/types/src/components/builder-component.component.d.ts +0 -324
  193. package/dist/types/src/components/builder-content.component.d.ts +0 -271
  194. package/dist/types/src/components/insert-spacer.component.d.ts +0 -13
  195. package/dist/types/src/components/no-wrap.d.ts +0 -2
  196. package/dist/types/src/components/variants-provider.component.d.ts +0 -8
  197. package/dist/types/src/constants/device-sizes.constant.d.ts +0 -56
  198. package/dist/types/src/constants/file-types.constant.d.ts +0 -2
  199. package/dist/types/src/decorators/builder-block.decorator.d.ts +0 -5
  200. package/dist/types/src/functions/apply-patch-with-mutation.d.ts +0 -5
  201. package/dist/types/src/functions/apply-patch-with-mutation.test.d.ts +0 -1
  202. package/dist/types/src/functions/block-to-html-string.d.ts +0 -3
  203. package/dist/types/src/functions/debonce-next-tick.d.ts +0 -2
  204. package/dist/types/src/functions/extract-localized-values.d.ts +0 -2
  205. package/dist/types/src/functions/filter-with-custom-targeting.d.ts +0 -15
  206. package/dist/types/src/functions/get-builder-pixel.d.ts +0 -2
  207. package/dist/types/src/functions/get.d.ts +0 -1
  208. package/dist/types/src/functions/is-debug.d.ts +0 -1
  209. package/dist/types/src/functions/no-wrap.d.ts +0 -33
  210. package/dist/types/src/functions/safe-dynamic-require.d.ts +0 -3
  211. package/dist/types/src/functions/set.d.ts +0 -1
  212. package/dist/types/src/functions/should-force-browser-runtime-in-node.d.ts +0 -1
  213. package/dist/types/src/functions/string-to-function.d.ts +0 -7
  214. package/dist/types/src/functions/throttle.d.ts +0 -1
  215. package/dist/types/src/functions/traverse.d.ts +0 -34
  216. package/dist/types/src/functions/try-eval.d.ts +0 -1
  217. package/dist/types/src/functions/update-metadata.d.ts +0 -17
  218. package/dist/types/src/functions/utils.d.ts +0 -2
  219. package/dist/types/src/functions/with-builder.d.ts +0 -2
  220. package/dist/types/src/functions/with-children.d.ts +0 -29
  221. package/dist/types/src/hooks/useIsPreviewing.d.ts +0 -1
  222. package/dist/types/src/scripts/init-editing.d.ts +0 -1
  223. package/dist/types/src/sdk-version.d.ts +0 -1
  224. package/dist/types/src/store/builder-async-requests.d.ts +0 -13
  225. package/dist/types/src/store/builder-meta.d.ts +0 -6
  226. package/dist/types/src/store/builder-store.d.ts +0 -10
  227. package/dist/types/src/to-error.d.ts +0 -13
  228. package/dist/types/test/basic.test.d.ts +0 -4
  229. package/dist/types/test/functions/render-block.d.ts +0 -3
  230. package/dist/types/test/image.test.d.ts +0 -1
  231. package/dist/types/test/setupTests.d.ts +0 -0
@@ -1,1125 +0,0 @@
1
- "use strict";
2
- 'use client';
3
- var __extends = (this && this.__extends) || (function () {
4
- var extendStatics = function (d, b) {
5
- extendStatics = Object.setPrototypeOf ||
6
- ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
7
- function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
8
- return extendStatics(d, b);
9
- };
10
- return function (d, b) {
11
- if (typeof b !== "function" && b !== null)
12
- throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
13
- extendStatics(d, b);
14
- function __() { this.constructor = d; }
15
- d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
16
- };
17
- })();
18
- var __assign = (this && this.__assign) || function () {
19
- __assign = Object.assign || function(t) {
20
- for (var s, i = 1, n = arguments.length; i < n; i++) {
21
- s = arguments[i];
22
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
23
- t[p] = s[p];
24
- }
25
- return t;
26
- };
27
- return __assign.apply(this, arguments);
28
- };
29
- var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
30
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
31
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
32
- else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
33
- return c > 3 && r && Object.defineProperty(target, key, r), r;
34
- };
35
- var __metadata = (this && this.__metadata) || function (k, v) {
36
- if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
37
- };
38
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
39
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
40
- return new (P || (P = Promise))(function (resolve, reject) {
41
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
42
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
43
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
44
- step((generator = generator.apply(thisArg, _arguments || [])).next());
45
- });
46
- };
47
- var __generator = (this && this.__generator) || function (thisArg, body) {
48
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
49
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
50
- function verb(n) { return function (v) { return step([n, v]); }; }
51
- function step(op) {
52
- if (f) throw new TypeError("Generator is already executing.");
53
- while (g && (g = 0, op[0] && (_ = 0)), _) try {
54
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
55
- if (y = 0, t) op = [op[0] & 2, t.value];
56
- switch (op[0]) {
57
- case 0: case 1: t = op; break;
58
- case 4: _.label++; return { value: op[1], done: false };
59
- case 5: _.label++; y = op[1]; op = [0]; continue;
60
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
61
- default:
62
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
63
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
64
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
65
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
66
- if (t[2]) _.ops.pop();
67
- _.trys.pop(); continue;
68
- }
69
- op = body.call(thisArg, _);
70
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
71
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
72
- }
73
- };
74
- var __rest = (this && this.__rest) || function (s, e) {
75
- var t = {};
76
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
77
- t[p] = s[p];
78
- if (s != null && typeof Object.getOwnPropertySymbols === "function")
79
- for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
80
- if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
81
- t[p[i]] = s[p[i]];
82
- }
83
- return t;
84
- };
85
- var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
86
- if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
87
- if (ar || !(i in from)) {
88
- if (!ar) ar = Array.prototype.slice.call(from, 0, i);
89
- ar[i] = from[i];
90
- }
91
- }
92
- return to.concat(ar || Array.prototype.slice.call(from));
93
- };
94
- var __importDefault = (this && this.__importDefault) || function (mod) {
95
- return (mod && mod.__esModule) ? mod : { "default": mod };
96
- };
97
- Object.defineProperty(exports, "__esModule", { value: true });
98
- exports.BuilderComponent = exports.onChange = void 0;
99
- var react_1 = __importDefault(require("react"));
100
- var react_dom_1 = __importDefault(require("react-dom"));
101
- var core_1 = require("@emotion/core");
102
- var builder_content_component_1 = require("./builder-content.component");
103
- var builder_blocks_component_1 = require("./builder-blocks.component");
104
- var sdk_1 = require("@builder.io/sdk");
105
- var builder_store_1 = require("../store/builder-store");
106
- var hash_sum_1 = __importDefault(require("hash-sum"));
107
- var on_change_1 = __importDefault(require("../../lib/on-change"));
108
- exports.onChange = on_change_1.default;
109
- var device_sizes_constant_1 = require("../constants/device-sizes.constant");
110
- var builder_async_requests_1 = require("../store/builder-async-requests");
111
- var debonce_next_tick_1 = require("../functions/debonce-next-tick");
112
- var throttle_1 = require("../functions/throttle");
113
- var builder_meta_1 = require("../store/builder-meta");
114
- var try_eval_1 = require("../functions/try-eval");
115
- var to_error_1 = require("../to-error");
116
- var get_builder_pixel_1 = require("../functions/get-builder-pixel");
117
- var is_debug_1 = require("../functions/is-debug");
118
- function pick(obj) {
119
- var keys = [];
120
- for (var _i = 1; _i < arguments.length; _i++) {
121
- keys[_i - 1] = arguments[_i];
122
- }
123
- var ret = {};
124
- keys.forEach(function (key) {
125
- ret[key] = obj[key];
126
- });
127
- return ret;
128
- }
129
- function omit(obj) {
130
- var keys = [];
131
- for (var _i = 1; _i < arguments.length; _i++) {
132
- keys[_i - 1] = arguments[_i];
133
- }
134
- var ret = __assign({}, obj);
135
- keys.forEach(function (key) {
136
- delete ret[key];
137
- });
138
- return ret;
139
- }
140
- var instancesMap = new Map();
141
- var wrapComponent = function (info) {
142
- return function (props) {
143
- var _a;
144
- // TODO: convention for all of this, like builderTagProps={{ style: {} foo: 'bar' }}
145
- var Tag = props.builderTag || 'div';
146
- var inputNames = ['children'].concat(((_a = info.inputs) === null || _a === void 0 ? void 0 : _a.map(function (item) { return item.name; })) || []);
147
- var baseProps = omit.apply(void 0, __spreadArray(__spreadArray([props], inputNames, false), ['attributes'], false));
148
- var inputProps = props; // pick(props, ...inputNames);
149
- if (info.noWrap) {
150
- return react_1.default.createElement(info.class, __assign({ attributes: baseProps }, inputProps));
151
- }
152
- return (react_1.default.createElement(Tag, __assign({}, baseProps),
153
- react_1.default.createElement(info.class, __assign({}, inputProps))));
154
- };
155
- };
156
- var size = function (thing) { return Object.keys(thing).length; };
157
- function debounce(func, wait, immediate) {
158
- if (immediate === void 0) { immediate = false; }
159
- var timeout;
160
- return function () {
161
- var context = this;
162
- var args = arguments;
163
- clearTimeout(timeout);
164
- timeout = setTimeout(function () {
165
- timeout = null;
166
- if (!immediate)
167
- func.apply(context, args);
168
- }, wait);
169
- if (immediate && !timeout)
170
- func.apply(context, args);
171
- };
172
- }
173
- var fontsLoaded = new Set();
174
- var fetch;
175
- if (globalThis.fetch)
176
- fetch = globalThis.fetch;
177
- fetch !== null && fetch !== void 0 ? fetch : (fetch = require('node-fetch'));
178
- var sizeMap = {
179
- desktop: 'large',
180
- tablet: 'medium',
181
- mobile: 'small',
182
- };
183
- var fetchCache = {};
184
- function searchToObject(location) {
185
- var pairs = (location.search || '').substring(1).split('&');
186
- var obj = {};
187
- for (var i in pairs) {
188
- if (!(pairs[i] && typeof pairs[i] === 'string')) {
189
- continue;
190
- }
191
- var pair = pairs[i].split('=');
192
- obj[decodeURIComponent(pair[0])] = decodeURIComponent(pair[1]);
193
- }
194
- return obj;
195
- }
196
- /**
197
- * Responsible for rendering Builder content of type: 'page' or 'section' to
198
- * react components. It will attempt to fetch content from the API based on
199
- * defined user attributes (URL path, device type, and any custom targeting you set using `builder.setUserAttributes`) unless a `BuilderContent`
200
- * object is provided to `props.content`
201
- *
202
- * Use it to mount content in desired location, enable editing in place when
203
- * previewed in the editor.
204
- *
205
- * Supports server-side-rendering when passed the content json as
206
- * `props.content`.
207
- */
208
- var BuilderComponent = /** @class */ (function (_super) {
209
- __extends(BuilderComponent, _super);
210
- function BuilderComponent(props) {
211
- var _this = this;
212
- var _a, _b;
213
- _this = _super.call(this, props) || this;
214
- _this.subscriptions = new sdk_1.Subscription();
215
- // TODO: don't trigger initial one?
216
- _this.onStateChange = new sdk_1.BehaviorSubject(null);
217
- _this.asServer = sdk_1.Builder.isServer;
218
- _this.contentRef = null;
219
- _this.styleRef = null;
220
- _this.rootState = sdk_1.Builder.isServer ? {} : (0, on_change_1.default)({}, function () { return _this.updateState(); });
221
- _this.lastJsCode = '';
222
- _this.lastHttpRequests = {};
223
- _this.httpSubscriptionPerKey = {};
224
- _this.firstLoad = true;
225
- _this.ref = null;
226
- _this.messageListener = function (event) {
227
- var _a;
228
- var isTrusted = sdk_1.Builder.isTrustedHostForEvent(event);
229
- if (!isTrusted)
230
- return;
231
- var info = event.data;
232
- switch (info.type) {
233
- case 'builder.configureSdk': {
234
- var data = info.data;
235
- if (!data.contentId || data.contentId !== ((_a = _this.useContent) === null || _a === void 0 ? void 0 : _a.id)) {
236
- return;
237
- }
238
- _this.sizes = (0, device_sizes_constant_1.getSizesForBreakpoints)(data.breakpoints || {});
239
- _this.setState({
240
- state: Object.assign(_this.rootState, {
241
- deviceSize: _this.deviceSizeState,
242
- // TODO: will user attributes be ready here?
243
- device: _this.device,
244
- }),
245
- updates: ((_this.state && _this.state.updates) || 0) + 1,
246
- breakpoints: data.breakpoints,
247
- });
248
- break;
249
- }
250
- case 'builder.updateSpacer': {
251
- var data_1 = info.data;
252
- var currentSpacer = _this.rootState._spacer;
253
- _this.updateState(function (state) {
254
- state._spacer = data_1;
255
- });
256
- break;
257
- }
258
- case 'builder.resetState': {
259
- var _b = info.data, state = _b.state, model = _b.model;
260
- if (model === _this.name) {
261
- for (var key in _this.rootState) {
262
- // TODO: support nested functions (somehow)
263
- if (typeof _this.rootState[key] !== 'function') {
264
- delete _this.rootState[key];
265
- }
266
- }
267
- Object.assign(_this.rootState, state);
268
- _this.setState(__assign(__assign({}, _this.state), { state: _this.rootState, updates: ((_this.state && _this.state.updates) || 0) + 1 }));
269
- }
270
- break;
271
- }
272
- case 'builder.resetSymbolState': {
273
- var _c = info.data.state, state = _c.state, model = _c.model, id = _c.id;
274
- if (_this.props.builderBlock && _this.props.builderBlock === id) {
275
- for (var key in _this.rootState) {
276
- delete _this.rootState[key];
277
- }
278
- Object.assign(_this.rootState, state);
279
- _this.setState(__assign(__assign({}, _this.state), { state: _this.rootState, updates: ((_this.state && _this.state.updates) || 0) + 1 }));
280
- }
281
- break;
282
- }
283
- }
284
- };
285
- _this.resizeFn = function () {
286
- var deviceSize = _this.deviceSizeState;
287
- if (deviceSize !== _this.state.state.deviceSize) {
288
- _this.setState(__assign(__assign({}, _this.state), { updates: ((_this.state && _this.state.updates) || 0) + 1, state: Object.assign(_this.rootState, __assign(__assign({}, _this.state.state), { deviceSize: deviceSize })) }));
289
- }
290
- };
291
- _this.resizeListener = sdk_1.Builder.isEditing ? (0, throttle_1.throttle)(_this.resizeFn, 200) : debounce(_this.resizeFn, 400);
292
- _this.mounted = false;
293
- _this.updateState = function (fn) {
294
- var state = _this.rootState;
295
- if (fn) {
296
- fn(state);
297
- }
298
- if (_this.mounted) {
299
- _this.setState({
300
- update: _this.updateState,
301
- state: state,
302
- updates: ((_this.state && _this.state.updates) || 0) + 1,
303
- });
304
- }
305
- else {
306
- _this.state = __assign(__assign({}, _this.state), { update: _this.updateState, state: state, updates: ((_this.state && _this.state.updates) || 0) + 1 });
307
- }
308
- _this.notifyStateChange();
309
- };
310
- _this.onContentLoaded = function (data, content) {
311
- if (_this.name === 'page' && sdk_1.Builder.isBrowser) {
312
- if (data) {
313
- var title = data.title, pageTitle = data.pageTitle, description = data.description, pageDescription = data.pageDescription;
314
- if (title || pageTitle) {
315
- document.title = title || pageTitle;
316
- }
317
- if (description || pageDescription) {
318
- var descriptionTag = document.querySelector('meta[name="description"]');
319
- if (!descriptionTag) {
320
- descriptionTag = document.createElement('meta');
321
- descriptionTag.setAttribute('name', 'description');
322
- document.head.appendChild(descriptionTag);
323
- }
324
- descriptionTag.setAttribute('content', description || pageDescription);
325
- }
326
- }
327
- }
328
- if (sdk_1.Builder.isEditing) {
329
- _this.notifyStateChange();
330
- }
331
- if (_this.props.contentLoaded) {
332
- _this.props.contentLoaded(data, content);
333
- }
334
- if (data && data.inputs && Array.isArray(data.inputs) && data.inputs.length) {
335
- if (!data.state) {
336
- data.state = {};
337
- }
338
- data.inputs.forEach(function (input) {
339
- if (input) {
340
- if (input.name &&
341
- input.defaultValue !== undefined &&
342
- data.state[input.name] === undefined) {
343
- data.state[input.name] = input.defaultValue;
344
- }
345
- }
346
- });
347
- }
348
- if (data && data.state) {
349
- var newState = __assign(__assign({}, _this.state), { updates: ((_this.state && _this.state.updates) || 0) + 1, state: Object.assign(_this.rootState, __assign(__assign(__assign(__assign(__assign({}, _this.state.state), { location: _this.locationState, device: _this.device }), data.state), _this.externalState), { deviceSize: _this.deviceSizeState })) });
350
- if (_this.mounted) {
351
- _this.setState(newState);
352
- }
353
- else {
354
- _this.state = newState;
355
- }
356
- }
357
- // TODO: also throttle on edits maybe
358
- if (data && data.jsCode && !_this.options.codegen) {
359
- // Don't rerun js code when editing and not changed
360
- var skip = false;
361
- if (sdk_1.Builder.isEditing) {
362
- if (_this.lastJsCode === data.jsCode) {
363
- skip = true;
364
- }
365
- else {
366
- _this.lastJsCode = data.jsCode;
367
- }
368
- }
369
- if (!skip) {
370
- var state = _this.state.state;
371
- // TODO: real editing method
372
- try {
373
- var result = new Function('data', 'ref', 'state', 'update', 'element', 'Builder', 'builder', 'context', data.jsCode)(data, _this, state, _this.state.update, _this.ref, sdk_1.Builder, sdk_1.builder, _this.state.context);
374
- // TODO: allow exports = { } syntax?
375
- // TODO: do something with reuslt like view - methods, computed, actions, properties, template, etc etc
376
- }
377
- catch (err) {
378
- var error = (0, to_error_1.toError)(err);
379
- if (sdk_1.Builder.isBrowser) {
380
- console.warn('Builder custom code error:', error.message, 'in', data.jsCode, error.stack);
381
- }
382
- else {
383
- if ((0, is_debug_1.isDebug)()) {
384
- console.debug('Builder custom code error:', error.message, 'in', data.jsCode, error.stack);
385
- }
386
- // Add to req.options.errors to return to client
387
- }
388
- }
389
- }
390
- }
391
- if (data && data.httpRequests /* || data.builderData @DEPRECATED */ && !_this.props.noAsync) {
392
- // Don't rerun http requests when editing and not changed
393
- // No longer needed?
394
- var skip = false;
395
- if (!skip) {
396
- var _loop_1 = function (key) {
397
- var url = data.httpRequests[key];
398
- if (url && (!_this.data[key] || sdk_1.Builder.isEditing)) {
399
- if (sdk_1.Builder.isBrowser) {
400
- var finalUrl_1 = _this.evalExpression(url);
401
- if (sdk_1.Builder.isEditing && _this.lastHttpRequests[key] === finalUrl_1) {
402
- return "continue";
403
- }
404
- _this.lastHttpRequests[key] = finalUrl_1;
405
- var builderModelRe = /builder\.io\/api\/v2\/([^\/\?]+)/i;
406
- var builderModelMatch = url.match(builderModelRe);
407
- var model = builderModelMatch && builderModelMatch[1];
408
- _this.handleRequest(key, finalUrl_1);
409
- var currentSubscription = _this.httpSubscriptionPerKey[key];
410
- if (currentSubscription) {
411
- currentSubscription.unsubscribe();
412
- }
413
- // TODO: fix this
414
- var newSubscription = (_this.httpSubscriptionPerKey[key] =
415
- _this.onStateChange.subscribe(function () {
416
- var newUrl = _this.evalExpression(url);
417
- if (newUrl !== finalUrl_1) {
418
- _this.handleRequest(key, newUrl);
419
- _this.lastHttpRequests[key] = newUrl;
420
- }
421
- }));
422
- _this.subscriptions.add(newSubscription);
423
- }
424
- else {
425
- _this.handleRequest(key, _this.evalExpression(url));
426
- }
427
- }
428
- };
429
- // TODO: another structure for this
430
- for (var key in data.httpRequests) {
431
- _loop_1(key);
432
- }
433
- }
434
- }
435
- };
436
- var _content = _this.inlinedContent;
437
- if (_content && _content.content) {
438
- _content = _content.content;
439
- }
440
- _this.sizes = (0, device_sizes_constant_1.getSizesForBreakpoints)(((_a = _content === null || _content === void 0 ? void 0 : _content.meta) === null || _a === void 0 ? void 0 : _a.breakpoints) || {});
441
- // TODO: pass this all the way down - symbols, etc
442
- // this.asServer = Boolean(props.hydrate && Builder.isBrowser)
443
- var contentData = (_b = _this.inlinedContent) === null || _b === void 0 ? void 0 : _b.data;
444
- if (contentData && Array.isArray(contentData.inputs) && contentData.inputs.length > 0) {
445
- if (!contentData.state) {
446
- contentData.state = {};
447
- }
448
- // set default values of content inputs on state
449
- contentData.inputs.forEach(function (input) {
450
- if (input) {
451
- if (input.name &&
452
- input.defaultValue !== undefined &&
453
- contentData.state[input.name] === undefined) {
454
- contentData.state[input.name] = input.defaultValue;
455
- }
456
- }
457
- });
458
- }
459
- _this.state = {
460
- // TODO: should change if this prop changes
461
- context: __assign(__assign({}, props.context), { apiKey: _this.props.apiKey || sdk_1.builder.apiKey }),
462
- state: Object.assign(_this.rootState, __assign(__assign(__assign(__assign({}, (_this.inlinedContent && _this.inlinedContent.data && _this.inlinedContent.data.state)), { isBrowser: sdk_1.Builder.isBrowser, isServer: !sdk_1.Builder.isBrowser, _hydrate: props.hydrate, location: _this.locationState, deviceSize: _this.deviceSizeState,
463
- // TODO: will user attributes be ready here?
464
- device: _this.device }), _this.getHtmlData()), props.data)),
465
- updates: 0,
466
- key: 0,
467
- update: _this.updateState,
468
- };
469
- var key = _this.props.apiKey;
470
- if (key && key !== _this.builder.apiKey && !instancesMap.has(key)) {
471
- // We create a builder instance for each api key to support loading of symbols from other spaces
472
- var instance = new sdk_1.Builder(key, undefined, undefined, true);
473
- instancesMap.set(key, instance);
474
- }
475
- if (_this.inlinedContent) {
476
- // Sometimes with graphql we get the content as `content.content`
477
- var content = _this.inlinedContent.content || _this.inlinedContent;
478
- _this.onContentLoaded(content === null || content === void 0 ? void 0 : content.data, (0, builder_content_component_1.getContentWithInfo)(content));
479
- }
480
- _this.registerCustomComponents();
481
- return _this;
482
- }
483
- Object.defineProperty(BuilderComponent.prototype, "options", {
484
- get: function () {
485
- // TODO: for perf cache this
486
- return __assign(__assign({}, BuilderComponent.defaults), this.props);
487
- },
488
- enumerable: false,
489
- configurable: true
490
- });
491
- Object.defineProperty(BuilderComponent.prototype, "name", {
492
- get: function () {
493
- return this.props.model || this.props.modelName || this.props.name; // || this.props.model
494
- },
495
- enumerable: false,
496
- configurable: true
497
- });
498
- Object.defineProperty(BuilderComponent.prototype, "element", {
499
- get: function () {
500
- return this.ref;
501
- },
502
- enumerable: false,
503
- configurable: true
504
- });
505
- Object.defineProperty(BuilderComponent.prototype, "inlinedContent", {
506
- get: function () {
507
- if (this.isPreviewing && !this.props.inlineContent) {
508
- return undefined;
509
- }
510
- return this.props.content;
511
- },
512
- enumerable: false,
513
- configurable: true
514
- });
515
- Object.defineProperty(BuilderComponent.prototype, "builder", {
516
- get: function () {
517
- var instance = this.props.apiKey && instancesMap.get(this.props.apiKey);
518
- return instance || this.props.builder || sdk_1.builder;
519
- },
520
- enumerable: false,
521
- configurable: true
522
- });
523
- BuilderComponent.prototype.getHtmlData = function () {
524
- var id = (this.inlinedContent && this.inlinedContent.id) || this.props.entry;
525
- var script = id &&
526
- sdk_1.Builder.isBrowser &&
527
- document.querySelector("script[data-builder-json=\"".concat(id, "\"],script[data-builder-state=\"").concat(id, "\"]"));
528
- if (script) {
529
- try {
530
- var json = JSON.parse(script.innerText);
531
- return json;
532
- }
533
- catch (err) {
534
- console.warn('Could not parse Builder.io HTML data transfer', err, script.innerText);
535
- }
536
- }
537
- return {};
538
- };
539
- Object.defineProperty(BuilderComponent.prototype, "device", {
540
- // TODO: pass down with context
541
- get: function () {
542
- return this.builder.getUserAttributes().device || 'desktop';
543
- },
544
- enumerable: false,
545
- configurable: true
546
- });
547
- Object.defineProperty(BuilderComponent.prototype, "locationState", {
548
- get: function () {
549
- return __assign(__assign({}, pick(this.location, 'pathname', 'hostname', 'search', 'host')), { path: (this.location.pathname && this.location.pathname.split('/').slice(1)) || '', query: searchToObject(this.location) });
550
- },
551
- enumerable: false,
552
- configurable: true
553
- });
554
- Object.defineProperty(BuilderComponent.prototype, "deviceSizeState", {
555
- // TODO: trigger state change on screen size change
556
- get: function () {
557
- // TODO: use context to pass this down on server
558
- return sdk_1.Builder.isBrowser
559
- ? this.sizes.getSizeForWidth(window.innerWidth)
560
- : sizeMap[this.device] || 'large';
561
- },
562
- enumerable: false,
563
- configurable: true
564
- });
565
- BuilderComponent.renderInto = function (elementOrSelector, props, hydrate, fresh) {
566
- if (props === void 0) { props = {}; }
567
- if (hydrate === void 0) { hydrate = true; }
568
- if (fresh === void 0) { fresh = false; }
569
- console.debug('BuilderPage.renderInto', elementOrSelector, props, hydrate, this);
570
- if (!elementOrSelector) {
571
- return;
572
- }
573
- var element = null;
574
- if (typeof elementOrSelector === 'string') {
575
- element = document.querySelector(elementOrSelector);
576
- }
577
- else {
578
- if (elementOrSelector instanceof Element) {
579
- element = elementOrSelector;
580
- }
581
- }
582
- if (!element) {
583
- return;
584
- }
585
- var exists = element.classList.contains('builder-hydrated');
586
- if (exists && !fresh) {
587
- console.debug('Tried to hydrate multiple times');
588
- return;
589
- }
590
- element.classList.add('builder-hydrated');
591
- var shouldHydrate = hydrate && element.innerHTML.includes('builder-block');
592
- if (!element.classList.contains('builder-component')) {
593
- // TODO: maybe remove any builder-api-styles...
594
- var apiStyles_1 = element.querySelector('.builder-api-styles') ||
595
- (element.previousElementSibling &&
596
- element.previousElementSibling.matches('.builder-api-styles')
597
- ? element.previousElementSibling
598
- : null);
599
- var keepStyles_1 = '';
600
- if (apiStyles_1) {
601
- var html = apiStyles_1.innerHTML;
602
- html.replace(/\/\*start:([^\*]+?)\*\/([\s\S]*?)\/\*end:([^\*]+?)\*\//g, function (match, id, content) {
603
- var el = null;
604
- try {
605
- el = document.querySelector("[data-emotion-css=\"".concat(id, "\"]"));
606
- }
607
- catch (err) {
608
- console.warn(err);
609
- }
610
- if (el) {
611
- el.innerHTML = content;
612
- }
613
- else if (!sdk_1.Builder.isEditing) {
614
- keepStyles_1 += match;
615
- }
616
- return match;
617
- });
618
- // NextTick? or longer timeout?
619
- sdk_1.Builder.nextTick(function () {
620
- apiStyles_1.innerHTML = keepStyles_1;
621
- });
622
- }
623
- var useElement = element.querySelector('.builder-component');
624
- if (useElement) {
625
- element = useElement;
626
- }
627
- else {
628
- shouldHydrate = false;
629
- }
630
- }
631
- if (location.search.includes('builder.debug=true')) {
632
- console.debug('hydrate', shouldHydrate, element);
633
- }
634
- var useEl = element;
635
- if (!exists) {
636
- var div = document.createElement('div');
637
- element.insertAdjacentElement('beforebegin', div);
638
- div.appendChild(element);
639
- useEl = div;
640
- }
641
- if (sdk_1.Builder.isEditing || (sdk_1.Builder.isBrowser && location.search.includes('builder.preview='))) {
642
- shouldHydrate = false;
643
- }
644
- if (shouldHydrate && element) {
645
- // TODO: maybe hydrate again. Maybe...
646
- var val_1 = react_dom_1.default.render(react_1.default.createElement(BuilderComponent, __assign({}, props)), useEl, useEl.builderRootRef);
647
- useEl.builderRootRef = val_1;
648
- return val_1;
649
- }
650
- var val = react_dom_1.default.render(react_1.default.createElement(BuilderComponent, __assign({}, props)), useEl, useEl.builderRootRef);
651
- useEl.builderRootRef = val;
652
- return val;
653
- };
654
- BuilderComponent.prototype.registerCustomComponents = function () {
655
- if (this.props.customComponents) {
656
- for (var _i = 0, _a = this.props.customComponents; _i < _a.length; _i++) {
657
- var customComponent = _a[_i];
658
- if (customComponent) {
659
- var component = customComponent.component, registration = __rest(customComponent, ["component"]);
660
- sdk_1.Builder.registerComponent(component, registration);
661
- }
662
- }
663
- }
664
- };
665
- BuilderComponent.prototype.componentDidMount = function () {
666
- var _this = this;
667
- var _a;
668
- this.mounted = true;
669
- if (this.asServer) {
670
- this.asServer = false;
671
- this.updateState(function (state) {
672
- state.isBrowser = true;
673
- state.isServer = false;
674
- });
675
- }
676
- if (sdk_1.Builder.isIframe) {
677
- (_a = window.parent) === null || _a === void 0 ? void 0 : _a.postMessage({ type: 'builder.sdkInjected', data: { modelName: this.name } }, '*');
678
- }
679
- if (sdk_1.Builder.isBrowser) {
680
- // TODO: remove event on unload
681
- window.addEventListener('resize', this.resizeListener);
682
- if (sdk_1.Builder.isEditing) {
683
- window.addEventListener('message', this.messageListener);
684
- }
685
- if (sdk_1.Builder.isEditing || sdk_1.Builder.isPreviewing) {
686
- sdk_1.Builder.nextTick(function () {
687
- _this.firstLoad = false;
688
- _this.reload();
689
- });
690
- }
691
- setTimeout(function () {
692
- window.dispatchEvent(new CustomEvent('builder:component:load', {
693
- detail: {
694
- ref: _this,
695
- },
696
- }));
697
- });
698
- }
699
- };
700
- Object.defineProperty(BuilderComponent.prototype, "isPreviewing", {
701
- get: function () {
702
- return ((sdk_1.Builder.isServer || (sdk_1.Builder.isBrowser && sdk_1.Builder.isPreviewing && !this.firstLoad)) &&
703
- (sdk_1.builder.previewingModel === this.name || sdk_1.builder.previewingModel === 'BUILDER_STUDIO'));
704
- },
705
- enumerable: false,
706
- configurable: true
707
- });
708
- BuilderComponent.prototype.notifyStateChange = function () {
709
- if (sdk_1.Builder.isServer) {
710
- return;
711
- }
712
- if (!(this && this.state)) {
713
- return;
714
- }
715
- var nextState = this.state.state;
716
- // TODO: only run the below once per tick...
717
- if (this.props.onStateChange) {
718
- this.props.onStateChange(nextState);
719
- }
720
- if (sdk_1.Builder.isBrowser) {
721
- window.dispatchEvent(new CustomEvent('builder:component:stateChange', {
722
- detail: {
723
- state: nextState,
724
- ref: this,
725
- },
726
- }));
727
- }
728
- this.onStateChange.next(nextState);
729
- };
730
- BuilderComponent.prototype.processStateFromApi = function (state) {
731
- return state; // mapValues(state, value => tryEval(value, this.data, this._errors))
732
- };
733
- Object.defineProperty(BuilderComponent.prototype, "location", {
734
- get: function () {
735
- return this.props.location || (sdk_1.Builder.isBrowser ? location : {});
736
- },
737
- enumerable: false,
738
- configurable: true
739
- });
740
- BuilderComponent.prototype.getCssFromFont = function (font, data) {
741
- // TODO: compute what font sizes are used and only load those.......
742
- var family = font.family + (font.kind && !font.kind.includes('#') ? ', ' + font.kind : '');
743
- var name = family.split(',')[0];
744
- var url = font.fileUrl ? font.fileUrl : font.files && font.files.regular;
745
- var str = '';
746
- if (url && family && name) {
747
- str += "\n@font-face {\n font-family: \"".concat(family, "\";\n src: local(\"").concat(name, "\"), url('").concat(url, "') format('woff2');\n font-display: fallback;\n font-weight: 400;\n}\n ").trim();
748
- }
749
- if (font.files) {
750
- for (var weight in font.files) {
751
- var isNumber = String(Number(weight)) === weight;
752
- if (!isNumber) {
753
- continue;
754
- }
755
- // TODO: maybe limit number loaded
756
- var weightUrl = font.files[weight];
757
- if (weightUrl && weightUrl !== url) {
758
- str += "\n@font-face {\n font-family: \"".concat(family, "\";\n src: url('").concat(weightUrl, "') format('woff2');\n font-display: fallback;\n font-weight: ").concat(weight, ";\n}\n ").trim();
759
- }
760
- }
761
- }
762
- return str;
763
- };
764
- BuilderComponent.prototype.componentWillUnmount = function () {
765
- this.unsubscribe();
766
- if (sdk_1.Builder.isBrowser) {
767
- window.removeEventListener('resize', this.resizeListener);
768
- window.removeEventListener('message', this.messageListener);
769
- }
770
- };
771
- BuilderComponent.prototype.getFontCss = function (data) {
772
- var _this = this;
773
- if (!this.builder.allowCustomFonts) {
774
- return '';
775
- }
776
- // TODO: separate internal data from external
777
- return (((data === null || data === void 0 ? void 0 : data.customFonts) &&
778
- data.customFonts.length &&
779
- data.customFonts.map(function (font) { return _this.getCssFromFont(font, data); }).join(' ')) ||
780
- '');
781
- };
782
- BuilderComponent.prototype.ensureFontsLoaded = function (data) {
783
- if (this.builder.allowCustomFonts && (data === null || data === void 0 ? void 0 : data.customFonts) && Array.isArray(data.customFonts)) {
784
- for (var _i = 0, _a = data.customFonts; _i < _a.length; _i++) {
785
- var font = _a[_i];
786
- var url = font.fileUrl ? font.fileUrl : font.files && font.files.regular;
787
- if (!fontsLoaded.has(url)) {
788
- var html = this.getCssFromFont(font, data);
789
- fontsLoaded.add(url);
790
- if (!html) {
791
- continue;
792
- }
793
- var style = document.createElement('style');
794
- style.className = 'builder-custom-font';
795
- style.setAttribute('data-builder-custom-font', url);
796
- style.innerHTML = html;
797
- document.head.appendChild(style);
798
- }
799
- }
800
- }
801
- };
802
- BuilderComponent.prototype.getCss = function (data) {
803
- var _a;
804
- var contentId = (_a = this.useContent) === null || _a === void 0 ? void 0 : _a.id;
805
- var cssCode = (data === null || data === void 0 ? void 0 : data.cssCode) || '';
806
- if (contentId) {
807
- // Allow using `&` in custom CSS code like @emotion
808
- // E.g. `& .foobar { ... }` to scope CSS
809
- // TODO: handle if '&' is within a string like `content: "&"`
810
- cssCode = cssCode.replace(/&/g, ".builder-component-".concat(contentId));
811
- }
812
- return cssCode + this.getFontCss(data);
813
- };
814
- Object.defineProperty(BuilderComponent.prototype, "data", {
815
- get: function () {
816
- var _a;
817
- var data = __assign(__assign(__assign({}, (this.inlinedContent && ((_a = this.inlinedContent.data) === null || _a === void 0 ? void 0 : _a.state))), this.externalState), this.state.state);
818
- Object.assign(this.rootState, data);
819
- return data;
820
- },
821
- enumerable: false,
822
- configurable: true
823
- });
824
- BuilderComponent.prototype.componentDidUpdate = function (prevProps) {
825
- var _this = this;
826
- // TODO: shallow diff
827
- if (this.props.data && prevProps.data !== this.props.data) {
828
- this.state.update(function (state) {
829
- Object.assign(state, _this.externalState);
830
- });
831
- }
832
- if (this.props.customComponents && this.props.customComponents !== prevProps.customComponents) {
833
- this.registerCustomComponents();
834
- }
835
- if (sdk_1.Builder.isEditing) {
836
- if (this.inlinedContent && prevProps.content !== this.inlinedContent) {
837
- this.onContentLoaded(this.inlinedContent.data, this.inlinedContent);
838
- }
839
- }
840
- };
841
- // FIXME: workaround to issue with CSS extraction and then hydration
842
- // (might be preact only)
843
- BuilderComponent.prototype.checkStyles = function (data) {
844
- if (this.styleRef) {
845
- var css_1 = this.getCss(data);
846
- if (this.styleRef.innerHTML !== css_1) {
847
- this.styleRef.innerHTML = css_1;
848
- }
849
- }
850
- };
851
- BuilderComponent.prototype.reload = function () {
852
- this.setState({
853
- key: this.state.key + 1,
854
- });
855
- };
856
- Object.defineProperty(BuilderComponent.prototype, "content", {
857
- get: function () {
858
- var content = this.inlinedContent;
859
- if (content && content.content) {
860
- // GraphQL workaround
861
- content = __assign(__assign({}, content), { data: content.content });
862
- }
863
- return content;
864
- },
865
- enumerable: false,
866
- configurable: true
867
- });
868
- Object.defineProperty(BuilderComponent.prototype, "externalState", {
869
- get: function () {
870
- return __assign(__assign({}, this.props.data), (this.props.locale ? { locale: this.props.locale } : {}));
871
- },
872
- enumerable: false,
873
- configurable: true
874
- });
875
- Object.defineProperty(BuilderComponent.prototype, "useContent", {
876
- get: function () {
877
- return this.content || this.state.context.builderContent;
878
- },
879
- enumerable: false,
880
- configurable: true
881
- });
882
- BuilderComponent.prototype.render = function () {
883
- var _this = this;
884
- var _a;
885
- var content = this.content;
886
- var dataString = sdk_1.Builder.isBrowser &&
887
- this.externalState &&
888
- size(this.externalState) &&
889
- (0, hash_sum_1.default)(this.externalState);
890
- var key = sdk_1.Builder.isEditing ? this.name : this.props.entry;
891
- if (key && !sdk_1.Builder.isEditing && dataString && dataString.length < 300) {
892
- key += ':' + dataString;
893
- }
894
- var WrapComponent = this.props.dataOnly ? react_1.default.Fragment : 'div';
895
- var contentId = (_a = this.useContent) === null || _a === void 0 ? void 0 : _a.id;
896
- return (
897
- // TODO: data attributes for model, id, etc?
898
- react_1.default.createElement(WrapComponent, { onClick: function (event) {
899
- // Prevent propagation from the root content component when editing to prevent issues
900
- // like client side routing triggering when links are clicked, unless this behavior is
901
- // disabled with the stopClickPropagationWhenEditing prop
902
- if (sdk_1.Builder.isEditing &&
903
- !_this.props.isChild &&
904
- !_this.props.stopClickPropagationWhenEditing) {
905
- event.stopPropagation();
906
- }
907
- }, className: "builder-component ".concat(contentId ? "builder-component-".concat(contentId) : ''), "data-name": this.name, "data-source": "Rendered by Builder.io", key: this.state.key, ref: function (ref) { return (_this.ref = ref); } },
908
- react_1.default.createElement(builder_meta_1.BuilderMetaContext.Consumer, null, function (value) { return (react_1.default.createElement(builder_meta_1.BuilderMetaContext.Provider, { value: typeof _this.props.ampMode === 'boolean'
909
- ? __assign(__assign({}, value), { ampMode: _this.props.ampMode }) : value },
910
- react_1.default.createElement(builder_async_requests_1.BuilderAsyncRequestsContext.Consumer, null, function (value) {
911
- var _a;
912
- _this._asyncRequests = value && value.requests;
913
- _this._errors = value && value.errors;
914
- _this._logs = value && value.logs;
915
- return (react_1.default.createElement(builder_content_component_1.BuilderContent, { isStatic: _this.props.isStatic || sdk_1.Builder.isStatic, key: ((_a = _this.inlinedContent) === null || _a === void 0 ? void 0 : _a.id) ||
916
- ('content' in _this.props && !_this.isPreviewing
917
- ? 'null-content-prop'
918
- : 'no-content-prop'), builder: _this.builder, ref: function (ref) { return (_this.contentRef = ref); },
919
- // TODO: pass entry in
920
- contentLoaded: function (data, content) { return _this.onContentLoaded(data, content); }, options: __assign(__assign(__assign(__assign(__assign(__assign({ key: key, entry: _this.props.entry }, (content && { initialContent: [content] })), (!content &&
921
- 'content' in _this.props &&
922
- !_this.isPreviewing && { initialContent: [] })), (_this.props.url && { url: _this.props.url })), _this.props.options), (_this.props.locale ? { locale: _this.props.locale } : {})), (_this.options.codegen && {
923
- format: 'react',
924
- })), inline: _this.props.inlineContent || (!_this.isPreviewing && 'content' in _this.props), contentError: _this.props.contentError, modelName: _this.name || 'page' }, function (data, loading, fullData) {
925
- var _a;
926
- if (_this.props.dataOnly) {
927
- return null;
928
- }
929
- if (fullData && fullData.id) {
930
- if (_this.state.breakpoints) {
931
- fullData.meta = fullData.meta || {};
932
- fullData.meta.breakpoints = _this.state.breakpoints;
933
- }
934
- _this.state.context.builderContent = fullData;
935
- }
936
- if (sdk_1.Builder.isBrowser) {
937
- sdk_1.Builder.nextTick(function () {
938
- _this.checkStyles(data);
939
- });
940
- }
941
- var codegen = _this.options.codegen;
942
- if (codegen && !_this.Component && (data === null || data === void 0 ? void 0 : data.blocksJs)) {
943
- var builderComponentNames = Array.from(new Set(sdk_1.Builder.components.map(function (item) { return item.name; })));
944
- var reversedcomponents_1 = sdk_1.Builder.components.slice().reverse();
945
- var builderComponents = builderComponentNames.map(function (name) {
946
- return reversedcomponents_1.find(function (item) { return item.class && item.name === name; });
947
- });
948
- var useBuilderState = function (initialState) {
949
- var _a = react_1.default.useState(0), setTick = _a[1];
950
- var state = react_1.default.useState(function () {
951
- return (0, on_change_1.default)(initialState, function () {
952
- setTick(function (tick) { return tick + 1; });
953
- });
954
- })[0];
955
- return state;
956
- };
957
- var mappedComponentNames = builderComponentNames.map(function (name) {
958
- return (name || '').replace(/[^\w]+/gi, '');
959
- });
960
- var finalizedComponents = builderComponents.map(function (info) {
961
- return wrapComponent(info);
962
- });
963
- _this.Component = new (Function.bind.apply(Function, __spreadArray(__spreadArray([void 0, 'jsx',
964
- '_css',
965
- 'Builder',
966
- 'builder',
967
- 'React',
968
- 'useBuilderState'], mappedComponentNames, false), [data.blocksJs], false)))().apply(void 0, __spreadArray([core_1.jsx,
969
- core_1.css,
970
- sdk_1.Builder,
971
- sdk_1.builder,
972
- react_1.default,
973
- useBuilderState], finalizedComponents, false));
974
- }
975
- var blocks = (data === null || data === void 0 ? void 0 : data.blocks) || [];
976
- var hasPixel = blocks.find(function (block) { var _a; return (_a = block.id) === null || _a === void 0 ? void 0 : _a.startsWith('builder-pixel'); });
977
- if (data && !hasPixel && blocks.length > 0) {
978
- blocks.push((0, get_builder_pixel_1.getBuilderPixel)(sdk_1.builder.apiKey));
979
- }
980
- // TODO: loading option - maybe that is what the children is or component prop
981
- // TODO: get rid of all these wrapper divs
982
- return data ? (react_1.default.createElement("div", __assign({ "data-builder-component": _this.name, "data-builder-content-id": fullData.id }, (_this.isPreviewing
983
- ? {
984
- 'data-builder-variation-id': fullData.testVariationId || fullData.variationId || fullData.id,
985
- }
986
- : {})),
987
- !codegen && _this.getCss(data) && (react_1.default.createElement("style", { ref: function (ref) { return (_this.styleRef = ref); }, className: "builder-custom-styles", dangerouslySetInnerHTML: {
988
- __html: _this.getCss(data),
989
- } })),
990
- react_1.default.createElement(builder_store_1.BuilderStoreContext.Provider, { value: __assign(__assign({}, _this.state), { rootState: _this.rootState, state: _this.data, content: fullData, renderLink: _this.props.renderLink }) }, codegen && _this.Component ? (react_1.default.createElement(_this.Component, { data: _this.data, context: _this.state.context })) : (react_1.default.createElement(builder_blocks_component_1.BuilderBlocks, { key: String(!!((_a = data === null || data === void 0 ? void 0 : data.blocks) === null || _a === void 0 ? void 0 : _a.length)), emailMode: _this.props.emailMode, fieldName: "blocks", blocks: blocks }))))) : loading ? (react_1.default.createElement("div", { "data-builder-component": _this.name, className: "builder-loading" }, _this.props.children)) : (react_1.default.createElement("div", { "data-builder-component": _this.name, className: "builder-no-content" }));
991
- }));
992
- }))); })));
993
- };
994
- BuilderComponent.prototype.evalExpression = function (expression) {
995
- var _this = this;
996
- var data = this.data;
997
- return String(expression).replace(/{{([^}]+)}}/g, function (match, group) {
998
- return (0, try_eval_1.tryEval)(group, data, _this._errors);
999
- });
1000
- };
1001
- BuilderComponent.prototype.handleRequest = function (propertyName, url) {
1002
- return __awaiter(this, void 0, void 0, function () {
1003
- var request, existing, promise_1, promise;
1004
- var _this = this;
1005
- return __generator(this, function (_a) {
1006
- // TODO: Builder.isEditing = just checks if iframe and parent page is this.builder.io or localhost:1234
1007
- if (sdk_1.Builder.isIframe && fetchCache[url]) {
1008
- this.updateState(function (ctx) {
1009
- ctx[propertyName] = fetchCache[url];
1010
- });
1011
- return [2 /*return*/, fetchCache[url]];
1012
- }
1013
- request = function () { return __awaiter(_this, void 0, void 0, function () {
1014
- var requestStart, json, result, err_1, error;
1015
- return __generator(this, function (_a) {
1016
- switch (_a.label) {
1017
- case 0:
1018
- requestStart = Date.now();
1019
- if (!sdk_1.Builder.isBrowser) {
1020
- console.time('Fetch ' + url);
1021
- }
1022
- _a.label = 1;
1023
- case 1:
1024
- _a.trys.push([1, 4, 5, 6]);
1025
- return [4 /*yield*/, fetch(url)];
1026
- case 2:
1027
- result = _a.sent();
1028
- return [4 /*yield*/, result.json()];
1029
- case 3:
1030
- json = _a.sent();
1031
- return [3 /*break*/, 6];
1032
- case 4:
1033
- err_1 = _a.sent();
1034
- error = (0, to_error_1.toError)(err_1);
1035
- if (this._errors) {
1036
- this._errors.push(error);
1037
- }
1038
- if (this._logs) {
1039
- this._logs.push("Fetch to ".concat(url, " errored in ").concat(Date.now() - requestStart, "ms"));
1040
- }
1041
- return [2 /*return*/];
1042
- case 5:
1043
- if (!sdk_1.Builder.isBrowser) {
1044
- console.timeEnd('Fetch ' + url);
1045
- if (this._logs) {
1046
- this._logs.push("Fetched ".concat(url, " in ").concat(Date.now() - requestStart, "ms"));
1047
- }
1048
- }
1049
- return [7 /*endfinally*/];
1050
- case 6:
1051
- if (json) {
1052
- if (sdk_1.Builder.isIframe) {
1053
- fetchCache[url] = json;
1054
- }
1055
- // TODO: debounce next tick all of these when there are a bunch
1056
- this.updateState(function (ctx) {
1057
- ctx[propertyName] = json;
1058
- });
1059
- }
1060
- return [2 /*return*/, json];
1061
- }
1062
- });
1063
- }); };
1064
- existing = this._asyncRequests &&
1065
- this._asyncRequests.find(function (req) { return (0, builder_async_requests_1.isRequestInfo)(req) && req.url === url; });
1066
- if (existing) {
1067
- promise_1 = existing.promise;
1068
- promise_1.then(function (json) {
1069
- if (json) {
1070
- _this.updateState(function (ctx) {
1071
- ctx[propertyName] = json;
1072
- });
1073
- }
1074
- });
1075
- return [2 /*return*/, promise_1];
1076
- }
1077
- promise = request();
1078
- sdk_1.Builder.nextTick(function () {
1079
- if (_this._asyncRequests) {
1080
- _this._asyncRequests.push(promise);
1081
- }
1082
- });
1083
- return [2 /*return*/, promise];
1084
- });
1085
- });
1086
- };
1087
- BuilderComponent.prototype.unsubscribe = function () {
1088
- if (this.subscriptions) {
1089
- this.subscriptions.unsubscribe();
1090
- this.subscriptions = new sdk_1.Subscription();
1091
- }
1092
- };
1093
- BuilderComponent.prototype.handleBuilderRequest = function (propertyName, optionsString) {
1094
- var _this = this;
1095
- var options = (0, try_eval_1.tryEval)(optionsString, this.data, this._errors);
1096
- // TODO: this will screw up for multiple bits of data
1097
- if (this.subscriptions) {
1098
- this.unsubscribe();
1099
- }
1100
- // TODO: don't unsubscribe and resubscribe every time data changes, will make a TON of requests if that's the case when editing...
1101
- // I guess will be cached then
1102
- if (options) {
1103
- // TODO: unsubscribe on destroy
1104
- this.subscriptions.add(this.builder.queueGetContent(options.model, options).subscribe(function (matches) {
1105
- if (matches) {
1106
- _this.updateState(function (ctx) {
1107
- ctx[propertyName] = matches;
1108
- });
1109
- }
1110
- }));
1111
- }
1112
- };
1113
- BuilderComponent.defaults = {
1114
- codegen: Boolean(sdk_1.Builder.isBrowser && location.href.includes('builder.codegen=true')),
1115
- };
1116
- __decorate([
1117
- debonce_next_tick_1.debounceNextTick,
1118
- __metadata("design:type", Function),
1119
- __metadata("design:paramtypes", []),
1120
- __metadata("design:returntype", void 0)
1121
- ], BuilderComponent.prototype, "notifyStateChange", null);
1122
- return BuilderComponent;
1123
- }(react_1.default.Component));
1124
- exports.BuilderComponent = BuilderComponent;
1125
- //# sourceMappingURL=builder-component.component.js.map