lib0 1.0.0-rc.11 → 1.0.0-rc.12

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "lib0",
3
- "version": "1.0.0-rc.11",
3
+ "version": "1.0.0-rc.12",
4
4
  "description": "",
5
5
  "sideEffects": false,
6
6
  "type": "module",
@@ -180,7 +180,12 @@ export class TextOp extends list.ListNode {
180
180
  * @param {TextOp} other
181
181
  */
182
182
  [equalityTrait.EqualityTraitSymbol] (other) {
183
- return $textOp.check(other) && fun.equalityDeep(this.insert, other.insert) && fun.equalityDeep(this.format, other.format) && fun.equalityDeep(this.attribution, other.attribution)
183
+ return $textOp.check(other) &&
184
+ fun.equalityDeep(this.insert, other.insert) &&
185
+ (
186
+ (object.isEmpty(this.format) && object.isEmpty(other.format)) || fun.equalityDeep(this.format, other.format)
187
+ ) &&
188
+ fun.equalityDeep(this.attribution, other.attribution)
184
189
  }
185
190
 
186
191
  /**
@@ -297,7 +302,12 @@ export class InsertOp extends list.ListNode {
297
302
  * @param {InsertOp<ArrayContent>} other
298
303
  */
299
304
  [equalityTrait.EqualityTraitSymbol] (other) {
300
- return $insertOp.check(other) && fun.equalityDeep(this.insert, other.insert) && fun.equalityDeep(this.format, other.format) && fun.equalityDeep(this.attribution, other.attribution)
305
+ return $insertOp.check(other) &&
306
+ fun.equalityDeep(this.insert, other.insert) &&
307
+ (
308
+ (object.isEmpty(this.format) && object.isEmpty(other.format)) || fun.equalityDeep(this.format, other.format)
309
+ ) &&
310
+ fun.equalityDeep(this.attribution, other.attribution)
301
311
  }
302
312
 
303
313
  /**
@@ -454,7 +464,12 @@ export class RetainOp extends list.ListNode {
454
464
  * @param {RetainOp} other
455
465
  */
456
466
  [equalityTrait.EqualityTraitSymbol] (other) {
457
- return $retainOp.check(other) && this.retain === other.retain && fun.equalityDeep(this.format, other.format) && fun.equalityDeep(this.attribution, other.attribution)
467
+ return $retainOp.check(other) &&
468
+ this.retain === other.retain &&
469
+ (
470
+ (object.isEmpty(this.format) && object.isEmpty(other.format)) || fun.equalityDeep(this.format, other.format)
471
+ ) &&
472
+ fun.equalityDeep(this.attribution, other.attribution)
458
473
  }
459
474
 
460
475
  clone (start = 0, end = this.retain) {
@@ -554,7 +569,12 @@ export class ModifyOp extends list.ListNode {
554
569
  * @param {ModifyOp<any>} other
555
570
  */
556
571
  [equalityTrait.EqualityTraitSymbol] (other) {
557
- return $modifyOp.check(other) && this.value[equalityTrait.EqualityTraitSymbol](other.value) && fun.equalityDeep(this.format, other.format) && fun.equalityDeep(this.attribution, other.attribution)
572
+ return $modifyOp.check(other) &&
573
+ this.value[equalityTrait.EqualityTraitSymbol](other.value) &&
574
+ (
575
+ (object.isEmpty(this.format) && object.isEmpty(other.format)) || fun.equalityDeep(this.format, other.format)
576
+ ) &&
577
+ fun.equalityDeep(this.attribution, other.attribution)
558
578
  }
559
579
 
560
580
  /**
@@ -1244,6 +1264,7 @@ export class DeltaBuilder extends Delta {
1244
1264
  */
1245
1265
  constructor (name, $schema) {
1246
1266
  super(name, $schema)
1267
+ // @todo rename to usedFormats !!
1247
1268
  /**
1248
1269
  * @type {FormattingAttributes?}
1249
1270
  */
@@ -1324,8 +1345,8 @@ export class DeltaBuilder extends Delta {
1324
1345
  */
1325
1346
  insert (insert, formatting = null, attribution = null) {
1326
1347
  modDeltaCheck(this)
1327
- const mergedAttributes = mergeAttrs(this.usedAttributes, formatting)
1328
- const mergedAttribution = mergeAttrs(this.usedAttribution, attribution)
1348
+ const mergedAttributes = mergeFormats(this.usedAttributes, formatting)
1349
+ const mergedAttribution = mergeAttributions(this.usedAttribution, attribution)
1329
1350
  /**
1330
1351
  * @param {TextOp | InsertOp<any>} lastOp
1331
1352
  */
@@ -1335,7 +1356,7 @@ export class DeltaBuilder extends Delta {
1335
1356
  if ($textOp.check(end) && checkMergedEquals(end)) {
1336
1357
  end._updateInsert(end.insert + insert)
1337
1358
  } else if (insert.length > 0) {
1338
- list.pushEnd(this.children, new TextOp(insert, mergedAttributes, mergedAttribution))
1359
+ list.pushEnd(this.children, new TextOp(insert, object.isEmpty(mergedAttributes) ? null : mergedAttributes, mergedAttribution))
1339
1360
  }
1340
1361
  this.childCnt += insert.length
1341
1362
  } else if (arr.isArray(insert)) {
@@ -1344,7 +1365,7 @@ export class DeltaBuilder extends Delta {
1344
1365
  end.insert.push(...insert)
1345
1366
  end._fingerprint = null
1346
1367
  } else if (insert.length > 0) {
1347
- list.pushEnd(this.children, new InsertOp(insert.slice() /* ensures that we don't reuse an existing array */, mergedAttributes, mergedAttribution))
1368
+ list.pushEnd(this.children, new InsertOp(insert.slice() /* ensures that we don't reuse an existing array */, object.isEmpty(mergedAttributes) ? null : mergedAttributes, mergedAttribution))
1348
1369
  }
1349
1370
  this.childCnt += insert.length
1350
1371
  }
@@ -1360,9 +1381,9 @@ export class DeltaBuilder extends Delta {
1360
1381
  */
1361
1382
  modify (modify, formatting = null, attribution = null) {
1362
1383
  modDeltaCheck(this)
1363
- const mergedAttributes = mergeAttrs(this.usedAttributes, formatting)
1364
- const mergedAttribution = mergeAttrs(this.usedAttribution, attribution)
1365
- list.pushEnd(this.children, new ModifyOp(modify, mergedAttributes, mergedAttribution))
1384
+ const mergedAttributes = mergeFormats(this.usedAttributes, formatting)
1385
+ const mergedAttribution = mergeAttributions(this.usedAttribution, attribution)
1386
+ list.pushEnd(this.children, new ModifyOp(modify, object.isEmpty(mergedAttributes) ? null : mergedAttributes, mergedAttribution))
1366
1387
  this.childCnt += 1
1367
1388
  return /** @type {any} */ (this)
1368
1389
  }
@@ -1374,14 +1395,14 @@ export class DeltaBuilder extends Delta {
1374
1395
  */
1375
1396
  retain (len, format = null, attribution = null) {
1376
1397
  modDeltaCheck(this)
1377
- const mergedFormats = mergeAttrs(this.usedAttributes, format)
1378
- const mergedAttribution = mergeAttrs(this.usedAttribution, attribution)
1398
+ const mergedFormats = mergeFormats(this.usedAttributes, format)
1399
+ const mergedAttribution = mergeAttributions(this.usedAttribution, attribution)
1379
1400
  const lastOp = /** @type {RetainOp|InsertOp<any>} */ (this.children.end)
1380
1401
  if ($retainOp.check(lastOp) && fun.equalityDeep(mergedFormats, lastOp.format) && fun.equalityDeep(mergedAttribution, lastOp.attribution)) {
1381
1402
  // @ts-ignore
1382
1403
  lastOp.retain += len
1383
1404
  } else if (len > 0) {
1384
- list.pushEnd(this.children, new RetainOp(len, mergedFormats, mergedAttribution))
1405
+ list.pushEnd(this.children, new RetainOp(len, object.isEmpty(mergedFormats) ? null : mergedFormats, mergedAttribution))
1385
1406
  }
1386
1407
  this.childCnt += len
1387
1408
  return this
@@ -1419,7 +1440,7 @@ export class DeltaBuilder extends Delta {
1419
1440
  modDeltaCheck(this)
1420
1441
  // @ts-ignore
1421
1442
  this.attrs[key] /** @type {any} */ =
1422
- (new SetAttrOp(/** @type {any} */ (key), val, prevValue, mergeAttrs(this.usedAttribution, attribution)))
1443
+ (new SetAttrOp(/** @type {any} */ (key), val, prevValue, mergeAttributions(this.usedAttribution, attribution)))
1423
1444
  return /** @type {any} */ (this)
1424
1445
  }
1425
1446
 
@@ -1454,7 +1475,7 @@ export class DeltaBuilder extends Delta {
1454
1475
  modDeltaCheck(this)
1455
1476
  // @ts-ignore
1456
1477
  this.attrs[key] /** @type {any} */ =
1457
- (new DeleteAttrOp(/** @type {any} */ (key), prevValue, mergeAttrs(this.usedAttribution, attribution)))
1478
+ (new DeleteAttrOp(/** @type {any} */ (key), prevValue, mergeAttributions(this.usedAttribution, attribution)))
1458
1479
  return /** @type {any} */ (this)
1459
1480
  }
1460
1481
 
@@ -1910,7 +1931,7 @@ const updateOpFormat = (op, formatUpdate) => {
1910
1931
  /** @type {any} */ (op).format = object.assign({}, op.format, { [k]: v })
1911
1932
  } else if (op.format != null) {
1912
1933
  const { [k]: _, ...rest } = op.format
1913
- ;/** @type {any} */ (op).format = rest ? null : rest
1934
+ ;/** @type {any} */ (op).format = object.isEmpty(rest) ? null : rest
1914
1935
  }
1915
1936
  }
1916
1937
  }
@@ -2038,10 +2059,21 @@ export const $deltaBuilderAny = /** @type {s.Schema<DeltaBuilderAny>} */ (/* @__
2038
2059
  * @param {T | null} a
2039
2060
  * @param {T | null} b
2040
2061
  */
2041
- export const mergeAttrs = (a, b) => a == null
2062
+ export const mergeAttributions = (a, b) => a == null
2042
2063
  ? b
2043
2064
  : (b == null ? a : object.assign({}, a, b))
2044
2065
 
2066
+ /**
2067
+ * Helper function to merge formats. The latter input "wins".
2068
+ *
2069
+ * @template {{ [key: string]: any }} T
2070
+ * @param {T | null} a
2071
+ * @param {T | null} b
2072
+ */
2073
+ export const mergeFormats = (a, b) => object.isEmpty(a)
2074
+ ? (object.isEmpty(b) ? null : b)
2075
+ : (object.isEmpty(b) ? a : object.assign({}, a, b))
2076
+
2045
2077
  /**
2046
2078
  * @template {DeltaAny|null} D
2047
2079
  * @param {D} a
@@ -750,7 +750,10 @@ export function $delta<DeltaConf extends ReadableDeltaConf = {}>({ name, attrs,
750
750
  export const $$delta: s.Schema<$Delta<DeltaConf>>;
751
751
  export const $deltaAny: s.Schema<Delta<any>>;
752
752
  export const $deltaBuilderAny: s.Schema<DeltaBuilderAny>;
753
- export function mergeAttrs<T extends {
753
+ export function mergeAttributions<T extends {
754
+ [key: string]: any;
755
+ }>(a: T | null, b: T | null): T | null;
756
+ export function mergeFormats<T extends {
754
757
  [key: string]: any;
755
758
  }>(a: T | null, b: T | null): T | null;
756
759
  export function mergeDeltas<D extends DeltaAny | null>(a: D, b: D): D;