@builder.io/react 2.0.3 → 2.0.4-2

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 (41) hide show
  1. package/CHANGELOG.md +4 -0
  2. package/dist/builder-react-lite.cjs.js +1 -1
  3. package/dist/builder-react-lite.cjs.js.map +1 -1
  4. package/dist/builder-react-lite.esm.js +1 -1
  5. package/dist/builder-react-lite.esm.js.map +1 -1
  6. package/dist/builder-react.browser.js +10 -1
  7. package/dist/builder-react.browser.js.map +1 -1
  8. package/dist/builder-react.cjs.js +1 -1
  9. package/dist/builder-react.cjs.js.map +1 -1
  10. package/dist/builder-react.es5.js +1 -1
  11. package/dist/builder-react.es5.js.map +1 -1
  12. package/dist/builder-react.unpkg.js +10 -1
  13. package/dist/builder-react.unpkg.js.map +1 -1
  14. package/dist/lib/package.json +1 -1
  15. package/dist/lib/src/blocks/Embed.js.map +1 -1
  16. package/dist/lib/src/components/builder-block.component.js +3 -6
  17. package/dist/lib/src/components/builder-block.component.js.map +1 -1
  18. package/dist/lib/src/components/builder-blocks.component.js.map +1 -1
  19. package/dist/lib/src/components/builder-component.component.js +14 -67
  20. package/dist/lib/src/components/builder-component.component.js.map +1 -1
  21. package/dist/lib/src/components/builder-content.component.js +5 -4
  22. package/dist/lib/src/components/builder-content.component.js.map +1 -1
  23. package/dist/lib/src/constants/device-sizes.constant.js.map +1 -1
  24. package/dist/lib/src/functions/apply-patch-with-mutation.js +1 -1
  25. package/dist/lib/src/functions/apply-patch-with-mutation.js.map +1 -1
  26. package/dist/lib/src/functions/apply-patch-with-mutation.test.js +54 -0
  27. package/dist/lib/src/functions/apply-patch-with-mutation.test.js.map +1 -0
  28. package/dist/lib/src/scripts/init-editing.js +1 -1
  29. package/dist/types/src/blocks/Mutation.d.ts +0 -1
  30. package/dist/types/src/blocks/Slot.d.ts +0 -1
  31. package/dist/types/src/blocks/raw/RawText.d.ts +0 -1
  32. package/dist/types/src/functions/apply-patch-with-mutation.d.ts +2 -2
  33. package/dist/types/src/functions/apply-patch-with-mutation.test.d.ts +1 -0
  34. package/package.json +1 -1
  35. package/src/components/builder-block.component.tsx +3 -6
  36. package/src/components/builder-blocks.component.tsx +1 -1
  37. package/src/components/builder-component.component.tsx +0 -53
  38. package/src/components/builder-content.component.tsx +4 -4
  39. package/src/functions/apply-patch-with-mutation.test.ts +55 -0
  40. package/src/functions/apply-patch-with-mutation.ts +5 -5
  41. package/src/scripts/init-editing.ts +1 -1
@@ -7,7 +7,7 @@ if (typeof window !== 'undefined') {
7
7
  type: 'builder.isReactSdk',
8
8
  data: {
9
9
  value: true,
10
- supportsPatchUpdates: 'v3',
10
+ supportsPatchUpdates: 'v4',
11
11
  priorVersion: package_json_1.version,
12
12
  },
13
13
  }, '*');
@@ -1,4 +1,3 @@
1
- /// <reference types="react" />
2
1
  import { BuilderElement } from '@builder.io/sdk';
3
2
  declare type MutationProps = {
4
3
  selector: string;
@@ -1,4 +1,3 @@
1
- /// <reference types="react" />
2
1
  declare type DropzoneProps = {
3
2
  name: string;
4
3
  };
@@ -1,4 +1,3 @@
1
- /// <reference types="react" />
2
1
  import { BuilderElement } from '@builder.io/sdk';
3
2
  export interface RawTextProps {
4
3
  attributes?: any;
@@ -1,5 +1,5 @@
1
- export declare const applyPatchWithMinimalMutationChain: (obj: any, patch: {
1
+ export declare const applyPatchWithMinimalMutationChain: <T extends object>(obj: T, patch: {
2
2
  path: string;
3
3
  op: 'add' | 'remove' | 'replace';
4
4
  value: any;
5
- }, preserveRoot?: boolean) => any;
5
+ }, preserveRoot?: boolean) => T;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@builder.io/react",
3
- "version": "2.0.3",
3
+ "version": "2.0.4-2",
4
4
  "description": "",
5
5
  "keywords": [],
6
6
  "main": "dist/builder-react.cjs.js",
@@ -19,7 +19,6 @@ const kebabCaseToCamelCase = (str = '') =>
19
19
  str.replace(/-([a-z])/g, match => match[1].toUpperCase());
20
20
 
21
21
  const Device = { desktop: 0, tablet: 1, mobile: 2 };
22
- const blocksMap: Record<string, BuilderElement> = {};
23
22
 
24
23
  const voidElements = new Set([
25
24
  'area',
@@ -136,7 +135,7 @@ export class BuilderBlock extends React.Component<
136
135
  }
137
136
 
138
137
  get block() {
139
- return blocksMap[this.props.block.id!] || this.props.block;
138
+ return this.props.block;
140
139
  }
141
140
 
142
141
  get emotionCss() {
@@ -249,17 +248,15 @@ export class BuilderBlock extends React.Component<
249
248
  }
250
249
 
251
250
  if (location.href.includes('builder.debug=true')) {
252
- this.eval('debugger');
251
+ eval('debugger');
253
252
  }
254
- let newBlock = { ...this.block };
255
253
  for (const patch of patches) {
256
254
  // TODO: soehow mark this.block as a new object,
257
255
  // e.g. access it's parent hm. maybe do the listning mutations
258
256
  // on hte parent element not the child (or rather
259
257
  // send the message to the parent)
260
- applyPatchWithMinimalMutationChain(newBlock, patch);
258
+ applyPatchWithMinimalMutationChain(this.props.block, patch, true);
261
259
  }
262
- blocksMap[this.props.block.id!] = newBlock;
263
260
  this.setState({ updates: this.state.updates + 1 });
264
261
 
265
262
  break;
@@ -115,7 +115,7 @@ export class BuilderBlocks extends React.Component<BuilderBlocksProps, BuilderBl
115
115
  alignItems: 'stretch',
116
116
  }),
117
117
  ...this.props.style,
118
- }}
118
+ } as any}
119
119
  onClick={() => {
120
120
  if (this.noBlocks) {
121
121
  this.onClickEmptyBlocks();
@@ -98,30 +98,6 @@ const sizeMap = {
98
98
  mobile: 'small',
99
99
  };
100
100
 
101
- function decorator(fn: Function) {
102
- return function argReceiver(...fnArgs: any[]) {
103
- // Check if the decorator is being called without arguments (ex `@foo methodName() {}`)
104
- if (fnArgs.length === 3) {
105
- const [target, key, descriptor] = fnArgs;
106
- if (descriptor && (descriptor.value || descriptor.get)) {
107
- fnArgs = [];
108
- return descriptorChecker(target, key, descriptor);
109
- }
110
- }
111
-
112
- return descriptorChecker;
113
-
114
- // descriptorChecker determines whether a method or getter is being decorated
115
- // and replaces the appropriate key with the decorated function.
116
- function descriptorChecker(target: any, key: any, descriptor: any) {
117
- const descriptorKey = descriptor.value ? 'value' : 'get';
118
- return {
119
- ...descriptor,
120
- [descriptorKey]: fn(descriptor[descriptorKey], ...fnArgs),
121
- };
122
- }
123
- };
124
- }
125
101
 
126
102
  const fetchCache: { [key: string]: any } = {};
127
103
 
@@ -1264,7 +1240,6 @@ export class BuilderComponent extends React.Component<
1264
1240
  this.notifyStateChange();
1265
1241
  }
1266
1242
 
1267
- // Unsubscribe all? TODO: maybe don't continuous fire when editing.....
1268
1243
  if (this.props.contentLoaded) {
1269
1244
  this.props.contentLoaded(data, content);
1270
1245
  }
@@ -1274,12 +1249,6 @@ export class BuilderComponent extends React.Component<
1274
1249
  data.state = {};
1275
1250
  }
1276
1251
 
1277
- // Maybe...
1278
- // if (data.context) {
1279
- // Object.assign(this.state.context, data.context)
1280
- // }
1281
- // TODO: may not want this... or make sure anything overriden
1282
- // explitily sets to null
1283
1252
  data.inputs.forEach((input: any) => {
1284
1253
  if (input) {
1285
1254
  if (
@@ -1380,8 +1349,6 @@ export class BuilderComponent extends React.Component<
1380
1349
  for (const key in data.httpRequests) {
1381
1350
  const url: string | undefined = data.httpRequests[key];
1382
1351
  if (url && (!this.data[key] || Builder.isEditing)) {
1383
- // TODO: if Builder.isEditing and url patches https://builder.io/api/v2/content/{editingModel}
1384
- // Then use this.builder.get().subscribe(...)
1385
1352
  if (Builder.isBrowser) {
1386
1353
  const finalUrl = this.evalExpression(url);
1387
1354
  if (Builder.isEditing && this.lastHttpRequests[key] === finalUrl) {
@@ -1391,17 +1358,6 @@ export class BuilderComponent extends React.Component<
1391
1358
  const builderModelRe = /builder\.io\/api\/v2\/([^\/\?]+)/i;
1392
1359
  const builderModelMatch = url.match(builderModelRe);
1393
1360
  const model = builderModelMatch && builderModelMatch[1];
1394
- if (false && Builder.isEditing && model && this.builder.editingModel === model) {
1395
- this.handleRequest(key, finalUrl);
1396
- // TODO: fix this
1397
- // this.subscriptions.add(
1398
- // this.builder.get(model).subscribe(data => {
1399
- // this.state.update((state: any) => {
1400
- // state[key] = data
1401
- // })
1402
- // })
1403
- // )
1404
- } else {
1405
1361
  this.handleRequest(key, finalUrl);
1406
1362
  const currentSubscription = this.httpSubscriptionPerKey[key];
1407
1363
  if (currentSubscription) {
@@ -1418,20 +1374,11 @@ export class BuilderComponent extends React.Component<
1418
1374
  }
1419
1375
  }));
1420
1376
  this.subscriptions.add(newSubscription);
1421
- }
1422
1377
  } else {
1423
1378
  this.handleRequest(key, this.evalExpression(url));
1424
1379
  }
1425
1380
  }
1426
1381
  }
1427
-
1428
- // @DEPRECATED
1429
- // for (const key in data.builderData) {
1430
- // const url = data.builderData[key]
1431
- // if (url && !this.data[key]) {
1432
- // this.handleBuilderRequest(key, this.evalExpression(url))
1433
- // }
1434
- // }
1435
1382
  }
1436
1383
  }
1437
1384
  };
@@ -140,17 +140,17 @@ export class BuilderContent<ContentType extends object = any> extends React.Comp
140
140
  if (location.href.includes('builder.debug=true')) {
141
141
  eval('debugger');
142
142
  }
143
+ let newData = this.state.data as any;
143
144
  for (const patch of patches) {
144
- applyPatchWithMinimalMutationChain(this.state.data, patch);
145
+ newData = applyPatchWithMinimalMutationChain(newData, patch, false);
145
146
  }
146
147
  this.setState({
147
148
  updates: this.state.updates + 1,
148
- data: this.state.data ? { ...this.state.data } : this.state.data,
149
+ data: newData,
149
150
  });
150
151
  if (this.props.contentLoaded) {
151
- this.props.contentLoaded(this.state.data?.data, this.state.data);
152
+ this.props.contentLoaded(newData.data, this.state.data);
152
153
  }
153
-
154
154
  break;
155
155
  }
156
156
  }
@@ -0,0 +1,55 @@
1
+ import { applyPatchWithMinimalMutationChain } from './apply-patch-with-mutation';
2
+
3
+ describe('applyPatchWithMinimalMutationChain', () => {
4
+ test('Basic shallow update', () => {
5
+ const obj = {
6
+ foo: 'bar',
7
+ };
8
+ const patch = {
9
+ op: 'replace',
10
+ path: '/foo',
11
+ value: '60px',
12
+ } as const;
13
+ const applied = applyPatchWithMinimalMutationChain(obj, patch);
14
+ expect(applied.foo).toBe('60px');
15
+ expect(applied).not.toBe(obj);
16
+ });
17
+
18
+ test('Deep object updates', () => {
19
+ const obj = {
20
+ foo: {
21
+ bar: true,
22
+ },
23
+ baz: {},
24
+ };
25
+ const patch = {
26
+ op: 'replace',
27
+ path: '/foo/bar',
28
+ value: '60px',
29
+ } as const;
30
+ const applied = applyPatchWithMinimalMutationChain(obj, patch);
31
+ expect(applied.foo.bar).toBe('60px');
32
+ expect(applied).not.toBe(obj);
33
+ expect(applied.foo).not.toBe(obj.foo);
34
+ expect(applied.baz).toBe(obj.baz);
35
+ });
36
+
37
+ test('Deep array updates', () => {
38
+ const obj = {
39
+ foo: [{ bar: true }],
40
+ baz: {},
41
+ };
42
+ const patch = {
43
+ op: 'replace',
44
+ path: '/foo/0/bar',
45
+ value: '60px',
46
+ } as const;
47
+
48
+ const applied = applyPatchWithMinimalMutationChain(obj, patch);
49
+ expect(applied.foo[0].bar).toBe('60px');
50
+ expect(applied).not.toBe(obj);
51
+ expect(applied.foo).not.toBe(obj.foo);
52
+ expect(applied.foo[0]).not.toBe(obj.foo[0]);
53
+ expect(applied.baz).toBe(obj.baz);
54
+ });
55
+ });
@@ -1,8 +1,8 @@
1
- export const applyPatchWithMinimalMutationChain = (
2
- obj: any,
1
+ export const applyPatchWithMinimalMutationChain = <T extends object>(
2
+ obj: T,
3
3
  patch: { path: string; op: 'add' | 'remove' | 'replace'; value: any },
4
- preserveRoot = true
5
- ) => {
4
+ preserveRoot = false
5
+ ): T => {
6
6
  if (Object(obj) !== obj) {
7
7
  return obj;
8
8
  }
@@ -13,7 +13,7 @@ export const applyPatchWithMinimalMutationChain = (
13
13
  }
14
14
 
15
15
  const newObj = preserveRoot ? obj : { ...obj };
16
- let objPart = newObj;
16
+ let objPart = newObj as any;
17
17
  for (let i = 0; i < pathArr.length; i++) {
18
18
  const isLast = i === pathArr.length - 1;
19
19
  const property = pathArr[i];
@@ -6,7 +6,7 @@ if (typeof window !== 'undefined') {
6
6
  type: 'builder.isReactSdk',
7
7
  data: {
8
8
  value: true,
9
- supportsPatchUpdates: 'v3',
9
+ supportsPatchUpdates: 'v4',
10
10
  priorVersion: version,
11
11
  },
12
12
  },