@textbus/collaborate 5.4.1 → 5.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.
package/dist/index.js CHANGED
@@ -547,7 +547,9 @@ exports.Collaborate = /*#__PURE__*/ function() {
547
547
  ev.delta.forEach(function(action) {
548
548
  if (Reflect.has(action, 'retain')) {
549
549
  if (action.attributes) {
550
- var formats = remoteFormatsToLocal(_this.registry, action.attributes);
550
+ var start = localSlot.index;
551
+ var end = start + action.retain;
552
+ var formats = remoteRetainFormatsToLocal(_this.registry, localSlot, start, end, action.attributes);
551
553
  if (formats.length) {
552
554
  localSlot.retain(action.retain, formats);
553
555
  }
@@ -558,7 +560,7 @@ exports.Collaborate = /*#__PURE__*/ function() {
558
560
  var length = 1;
559
561
  if (typeof action.insert === 'string') {
560
562
  length = action.insert.length;
561
- localSlot.insert(action.insert, remoteFormatsToLocal(_this.registry, action.attributes));
563
+ localSlot.insert(action.insert, remoteInsertFormatsToLocal(_this.registry, action.attributes));
562
564
  } else {
563
565
  var sharedComponent = action.insert;
564
566
  var component = _this.createLocalComponentBySharedComponent(sharedComponent);
@@ -594,22 +596,14 @@ exports.Collaborate = /*#__PURE__*/ function() {
594
596
  var length = 0;
595
597
  var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
596
598
  try {
597
- var _loop = function() {
599
+ for(var _iterator = actions[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
598
600
  var action = _step.value;
599
601
  if (action.type === 'retain') {
600
602
  var formats = action.formats;
601
603
  if (formats) {
602
- var keys = Object.keys(formats);
603
- var length1 = keys.length;
604
- keys.forEach(function(key) {
605
- var formatter = _this.registry.getFormatter(key);
606
- if (!formatter) {
607
- length1--;
608
- Reflect.deleteProperty(formats, key);
609
- }
610
- });
611
- if (length1) {
612
- sharedSlot.format(offset, action.offset, formats);
604
+ var attrs = localFormatsToYjsAttributes(_this.registry, localSlot, offset, offset + action.offset, formats);
605
+ if (Object.keys(attrs).length) {
606
+ sharedSlot.format(offset, action.offset, attrs);
613
607
  }
614
608
  } else {
615
609
  offset = action.offset;
@@ -619,11 +613,11 @@ exports.Collaborate = /*#__PURE__*/ function() {
619
613
  var isEmpty = delta.length === 1 && delta[0].insert === core.Slot.emptyPlaceholder;
620
614
  if (typeof action.content === 'string') {
621
615
  length = action.content.length;
622
- sharedSlot.insert(offset, action.content, action.formats || {});
616
+ sharedSlot.insert(offset, action.content, localFormatsRecordToRemote(_this.registry, action.formats) || {});
623
617
  } else {
624
618
  length = 1;
625
619
  var sharedComponent = _this.createSharedComponentByLocalComponent(action.ref);
626
- sharedSlot.insertEmbed(offset, sharedComponent, action.formats || {});
620
+ sharedSlot.insertEmbed(offset, sharedComponent, localFormatsRecordToRemote(_this.registry, action.formats) || {});
627
621
  }
628
622
  if (isEmpty && offset === 0) {
629
623
  sharedSlot.delete(sharedSlot.length - 1, 1);
@@ -643,8 +637,7 @@ exports.Collaborate = /*#__PURE__*/ function() {
643
637
  } else if (action.type === 'attrDelete') {
644
638
  sharedSlot.removeAttribute(action.name);
645
639
  }
646
- };
647
- for(var _iterator = actions[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true)_loop();
640
+ }
648
641
  } catch (err) {
649
642
  _didIteratorError = true;
650
643
  _iteratorError = err;
@@ -799,14 +792,7 @@ exports.Collaborate = /*#__PURE__*/ function() {
799
792
  var _this = this;
800
793
  var offset = 0;
801
794
  localSlot.toDelta().forEach(function(i) {
802
- var formats = {};
803
- if (i.formats) {
804
- i.formats.forEach(function(item) {
805
- formats[item[0].name] = item[1];
806
- });
807
- } else {
808
- formats = null;
809
- }
795
+ var formats = i.formats ? formatsArrayToRemoteRecord(_this.registry, i.formats) : undefined;
810
796
  if (typeof i.insert === 'string') {
811
797
  sharedContent.insert(offset, i.insert, formats);
812
798
  } else {
@@ -897,12 +883,12 @@ exports.Collaborate = /*#__PURE__*/ function() {
897
883
  var action = _step.value;
898
884
  if (action.insert) {
899
885
  if (typeof action.insert === 'string') {
900
- var formats = remoteFormatsToLocal(this.registry, action.attributes);
886
+ var formats = remoteInsertFormatsToLocal(this.registry, action.attributes);
901
887
  localSlot.insert(action.insert, formats);
902
888
  } else {
903
889
  var sharedComponent = action.insert;
904
890
  var component = this.createLocalComponentBySharedComponent(sharedComponent);
905
- localSlot.insert(component, remoteFormatsToLocal(this.registry, action.attributes));
891
+ localSlot.insert(component, remoteInsertFormatsToLocal(this.registry, action.attributes));
906
892
  }
907
893
  } else {
908
894
  throw collaborateErrorFn('unexpected delta action.');
@@ -1381,19 +1367,180 @@ exports.Collaborate = _ts_decorate$2([
1381
1367
  typeof SubModelLoader === "undefined" ? Object : SubModelLoader
1382
1368
  ])
1383
1369
  ], exports.Collaborate);
1384
- function remoteFormatsToLocal(registry, attrs) {
1370
+ function collectStackableValuesInRange(slot, formatter, start, end) {
1371
+ var values = [];
1372
+ slot.getFormatRangesByFormatter(formatter, start, end).forEach(function(range) {
1373
+ var v = range.value;
1374
+ if (!values.some(function(existing) {
1375
+ return core.Format.equal(existing, v);
1376
+ })) {
1377
+ values.push(v);
1378
+ }
1379
+ });
1380
+ return values;
1381
+ }
1382
+ function serializeFormatForYjs(value) {
1383
+ return core.valueToJSON(value);
1384
+ }
1385
+ function localFormatsToYjsAttributes(registry, slot, start, end, localFormats) {
1386
+ var attrs = {};
1387
+ Object.keys(localFormats).forEach(function(key) {
1388
+ var formatter = registry.getFormatter(key);
1389
+ if (!formatter) {
1390
+ return;
1391
+ }
1392
+ if (_instanceof$1(formatter, core.StackableFormatter)) {
1393
+ var values = collectStackableValuesInRange(slot, formatter, start, end);
1394
+ attrs[key] = values.length ? values.map(serializeFormatForYjs) : null;
1395
+ } else {
1396
+ attrs[key] = localFormats[key];
1397
+ }
1398
+ });
1399
+ return attrs;
1400
+ }
1401
+ function localFormatsRecordToRemote(registry, formats) {
1402
+ if (!formats) {
1403
+ return undefined;
1404
+ }
1405
+ var record = {};
1406
+ Object.keys(formats).forEach(function(key) {
1407
+ var formatter = registry.getFormatter(key);
1408
+ if (!formatter) {
1409
+ return;
1410
+ }
1411
+ var incoming = formats[key];
1412
+ if (_instanceof$1(formatter, core.StackableFormatter)) {
1413
+ if (incoming === null || incoming === undefined) {
1414
+ record[key] = null;
1415
+ } else {
1416
+ record[key] = [
1417
+ serializeFormatForYjs(incoming)
1418
+ ];
1419
+ }
1420
+ } else {
1421
+ record[key] = incoming;
1422
+ }
1423
+ });
1424
+ return Object.keys(record).length ? record : undefined;
1425
+ }
1426
+ function formatsArrayToRemoteRecord(registry, formats) {
1427
+ var record = {};
1428
+ formats.forEach(function(param) {
1429
+ var _param = _sliced_to_array(param, 2), formatter = _param[0], value = _param[1];
1430
+ if (!registry.getFormatter(formatter.name)) {
1431
+ return;
1432
+ }
1433
+ if (_instanceof$1(formatter, core.StackableFormatter)) {
1434
+ var serialized = serializeFormatForYjs(value);
1435
+ var current = record[formatter.name];
1436
+ if (Array.isArray(current)) {
1437
+ current.push(serialized);
1438
+ } else {
1439
+ record[formatter.name] = [
1440
+ serialized
1441
+ ];
1442
+ }
1443
+ } else {
1444
+ record[formatter.name] = value;
1445
+ }
1446
+ });
1447
+ return Object.keys(record).length ? record : undefined;
1448
+ }
1449
+ function stackableRemoteRetainToLocal(slot, formatter, start, end, raw) {
1385
1450
  var formats = [];
1386
- if (attrs) {
1387
- Object.keys(attrs).forEach(function(key) {
1388
- var formatter = registry.getFormatter(key);
1389
- if (formatter) {
1390
- formats.push([
1391
- formatter,
1392
- attrs[key]
1393
- ]);
1394
- }
1395
- });
1451
+ var localValues = collectStackableValuesInRange(slot, formatter, start, end);
1452
+ if (raw === null || raw === undefined || Array.isArray(raw) && raw.length === 0) {
1453
+ if (localValues.length > 0) {
1454
+ formats.push([
1455
+ formatter,
1456
+ new core.PendingErasure(true)
1457
+ ]);
1458
+ }
1459
+ return formats;
1460
+ }
1461
+ var remoteValues = Array.isArray(raw) ? raw : [
1462
+ raw
1463
+ ];
1464
+ localValues.forEach(function(localValue) {
1465
+ if (!remoteValues.some(function(remoteValue) {
1466
+ return core.Format.equal(localValue, remoteValue);
1467
+ })) {
1468
+ formats.push([
1469
+ formatter,
1470
+ new core.PendingErasure(false, localValue)
1471
+ ]);
1472
+ }
1473
+ });
1474
+ remoteValues.forEach(function(remoteValue) {
1475
+ if (!localValues.some(function(localValue) {
1476
+ return core.Format.equal(localValue, remoteValue);
1477
+ })) {
1478
+ formats.push([
1479
+ formatter,
1480
+ remoteValue
1481
+ ]);
1482
+ }
1483
+ });
1484
+ return formats;
1485
+ }
1486
+ function stackableRemoteInsertToLocal(formatter, raw) {
1487
+ if (raw === null || raw === undefined || Array.isArray(raw) && raw.length === 0) {
1488
+ return [];
1489
+ }
1490
+ var remoteValues = Array.isArray(raw) ? raw : [
1491
+ raw
1492
+ ];
1493
+ return remoteValues.map(function(value) {
1494
+ return [
1495
+ formatter,
1496
+ value
1497
+ ];
1498
+ });
1499
+ }
1500
+ /** 远程插入内容的格式 → 本地 Formats(新内容无旧状态,可堆叠仅展开远程数组) */ function remoteInsertFormatsToLocal(registry, attrs) {
1501
+ var formats = [];
1502
+ if (!attrs) {
1503
+ return formats;
1504
+ }
1505
+ Object.keys(attrs).forEach(function(key) {
1506
+ var formatter = registry.getFormatter(key);
1507
+ if (!formatter) {
1508
+ return;
1509
+ }
1510
+ var raw = attrs[key];
1511
+ if (_instanceof$1(formatter, core.StackableFormatter)) {
1512
+ var _formats;
1513
+ (_formats = formats).push.apply(_formats, _to_consumable_array(stackableRemoteInsertToLocal(formatter, raw)));
1514
+ return;
1515
+ }
1516
+ formats.push([
1517
+ formatter,
1518
+ raw
1519
+ ]);
1520
+ });
1521
+ return formats;
1522
+ }
1523
+ /** 远程 retain 格式 → 本地 Formats(可堆叠与区间内本地快照做差量对齐) */ function remoteRetainFormatsToLocal(registry, slot, start, end, attrs) {
1524
+ var formats = [];
1525
+ if (!attrs) {
1526
+ return formats;
1396
1527
  }
1528
+ Object.keys(attrs).forEach(function(key) {
1529
+ var formatter = registry.getFormatter(key);
1530
+ if (!formatter) {
1531
+ return;
1532
+ }
1533
+ var raw = attrs[key];
1534
+ if (_instanceof$1(formatter, core.StackableFormatter)) {
1535
+ var _formats;
1536
+ (_formats = formats).push.apply(_formats, _to_consumable_array(stackableRemoteRetainToLocal(slot, formatter, start, end, raw)));
1537
+ return;
1538
+ }
1539
+ formats.push([
1540
+ formatter,
1541
+ raw
1542
+ ]);
1543
+ });
1397
1544
  return formats;
1398
1545
  }
1399
1546
 
@@ -2734,4 +2881,9 @@ exports.MultipleDocumentCollaborateModule = MultipleDocumentCollaborateModule;
2734
2881
  exports.SubModelLoader = SubModelLoader;
2735
2882
  exports.SyncConnector = SyncConnector;
2736
2883
  exports.YWebsocketConnector = YWebsocketConnector;
2884
+ exports.formatsArrayToRemoteRecord = formatsArrayToRemoteRecord;
2885
+ exports.localFormatsRecordToRemote = localFormatsRecordToRemote;
2886
+ exports.localFormatsToYjsAttributes = localFormatsToYjsAttributes;
2887
+ exports.remoteInsertFormatsToLocal = remoteInsertFormatsToLocal;
2888
+ exports.remoteRetainFormatsToLocal = remoteRetainFormatsToLocal;
2737
2889
  //# sourceMappingURL=index.js.map