slate 0.115.1 → 0.116.0

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.es.js CHANGED
@@ -1,4 +1,4 @@
1
- import { createDraft, finishDraft, isDraft, produce } from 'immer';
1
+ import { produce } from 'immer';
2
2
 
3
3
  // eslint-disable-next-line no-redeclare
4
4
  var PathRef = {
@@ -372,280 +372,337 @@ function _defineProperty(obj, key, value) {
372
372
 
373
373
  function ownKeys$e(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
374
374
  function _objectSpread$e(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys$e(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys$e(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
375
- var applyToDraft = (editor, selection, op) => {
376
- switch (op.type) {
377
- case 'insert_node':
378
- {
379
- var {
380
- path,
381
- node
382
- } = op;
383
- var parent = Node.parent(editor, path);
384
- var index = path[path.length - 1];
385
- if (index > parent.children.length) {
386
- throw new Error("Cannot apply an \"insert_node\" operation at path [".concat(path, "] because the destination is past the end of the node."));
387
- }
388
- parent.children.splice(index, 0, node);
389
- if (selection) {
390
- for (var [point, key] of Range.points(selection)) {
391
- selection[key] = Point.transform(point, op);
392
- }
393
- }
394
- break;
395
- }
396
- case 'insert_text':
397
- {
398
- var {
399
- path: _path,
400
- offset,
401
- text
402
- } = op;
403
- if (text.length === 0) break;
404
- var _node = Node.leaf(editor, _path);
405
- var before = _node.text.slice(0, offset);
406
- var after = _node.text.slice(offset);
407
- _node.text = before + text + after;
408
- if (selection) {
409
- for (var [_point, _key] of Range.points(selection)) {
410
- selection[_key] = Point.transform(_point, op);
411
- }
412
- }
413
- break;
375
+ var insertChildren = function insertChildren(xs, index) {
376
+ for (var _len = arguments.length, newValues = new Array(_len > 2 ? _len - 2 : 0), _key = 2; _key < _len; _key++) {
377
+ newValues[_key - 2] = arguments[_key];
378
+ }
379
+ return [...xs.slice(0, index), ...newValues, ...xs.slice(index)];
380
+ };
381
+ var replaceChildren = function replaceChildren(xs, index, removeCount) {
382
+ for (var _len2 = arguments.length, newValues = new Array(_len2 > 3 ? _len2 - 3 : 0), _key2 = 3; _key2 < _len2; _key2++) {
383
+ newValues[_key2 - 3] = arguments[_key2];
384
+ }
385
+ return [...xs.slice(0, index), ...newValues, ...xs.slice(index + removeCount)];
386
+ };
387
+ var removeChildren = replaceChildren;
388
+ /**
389
+ * Replace a descendant with a new node, replacing all ancestors
390
+ */
391
+ var modifyDescendant = (editor, path, f) => {
392
+ if (path.length === 0) {
393
+ throw new Error('Cannot modify the editor');
394
+ }
395
+ var node = Node.get(editor, path);
396
+ var slicedPath = path.slice();
397
+ var modifiedNode = f(node);
398
+ while (slicedPath.length > 1) {
399
+ var _index = slicedPath.pop();
400
+ var ancestorNode = Node.get(editor, slicedPath);
401
+ modifiedNode = _objectSpread$e(_objectSpread$e({}, ancestorNode), {}, {
402
+ children: replaceChildren(ancestorNode.children, _index, 1, modifiedNode)
403
+ });
404
+ }
405
+ var index = slicedPath.pop();
406
+ editor.children = replaceChildren(editor.children, index, 1, modifiedNode);
407
+ };
408
+ /**
409
+ * Replace the children of a node, replacing all ancestors
410
+ */
411
+ var modifyChildren = (editor, path, f) => {
412
+ if (path.length === 0) {
413
+ editor.children = f(editor.children);
414
+ } else {
415
+ modifyDescendant(editor, path, node => {
416
+ if (Text.isText(node)) {
417
+ throw new Error("Cannot get the element at path [".concat(path, "] because it refers to a leaf node: ").concat(Scrubber.stringify(node)));
414
418
  }
415
- case 'merge_node':
416
- {
417
- var {
418
- path: _path2
419
- } = op;
420
- var _node2 = Node.get(editor, _path2);
421
- var prevPath = Path.previous(_path2);
422
- var prev = Node.get(editor, prevPath);
423
- var _parent = Node.parent(editor, _path2);
424
- var _index = _path2[_path2.length - 1];
425
- if (Text.isText(_node2) && Text.isText(prev)) {
426
- prev.text += _node2.text;
427
- } else if (!Text.isText(_node2) && !Text.isText(prev)) {
428
- prev.children.push(..._node2.children);
429
- } else {
430
- throw new Error("Cannot apply a \"merge_node\" operation at path [".concat(_path2, "] to nodes of different interfaces: ").concat(Scrubber.stringify(_node2), " ").concat(Scrubber.stringify(prev)));
419
+ return _objectSpread$e(_objectSpread$e({}, node), {}, {
420
+ children: f(node.children)
421
+ });
422
+ });
423
+ }
424
+ };
425
+ /**
426
+ * Replace a leaf, replacing all ancestors
427
+ */
428
+ var modifyLeaf = (editor, path, f) => modifyDescendant(editor, path, node => {
429
+ if (!Text.isText(node)) {
430
+ throw new Error("Cannot get the leaf node at path [".concat(path, "] because it refers to a non-leaf node: ").concat(Scrubber.stringify(node)));
431
+ }
432
+ return f(node);
433
+ });
434
+ // eslint-disable-next-line no-redeclare
435
+ var GeneralTransforms = {
436
+ transform(editor, op) {
437
+ var transformSelection = false;
438
+ switch (op.type) {
439
+ case 'insert_node':
440
+ {
441
+ var {
442
+ path,
443
+ node
444
+ } = op;
445
+ modifyChildren(editor, Path.parent(path), children => {
446
+ var index = path[path.length - 1];
447
+ if (index > children.length) {
448
+ throw new Error("Cannot apply an \"insert_node\" operation at path [".concat(path, "] because the destination is past the end of the node."));
449
+ }
450
+ return insertChildren(children, index, node);
451
+ });
452
+ transformSelection = true;
453
+ break;
431
454
  }
432
- _parent.children.splice(_index, 1);
433
- if (selection) {
434
- for (var [_point2, _key2] of Range.points(selection)) {
435
- selection[_key2] = Point.transform(_point2, op);
436
- }
455
+ case 'insert_text':
456
+ {
457
+ var {
458
+ path: _path,
459
+ offset,
460
+ text
461
+ } = op;
462
+ if (text.length === 0) break;
463
+ modifyLeaf(editor, _path, node => {
464
+ var before = node.text.slice(0, offset);
465
+ var after = node.text.slice(offset);
466
+ return _objectSpread$e(_objectSpread$e({}, node), {}, {
467
+ text: before + text + after
468
+ });
469
+ });
470
+ transformSelection = true;
471
+ break;
437
472
  }
438
- break;
439
- }
440
- case 'move_node':
441
- {
442
- var {
443
- path: _path3,
444
- newPath
445
- } = op;
446
- if (Path.isAncestor(_path3, newPath)) {
447
- throw new Error("Cannot move a path [".concat(_path3, "] to new path [").concat(newPath, "] because the destination is inside itself."));
473
+ case 'merge_node':
474
+ {
475
+ var {
476
+ path: _path2
477
+ } = op;
478
+ var index = _path2[_path2.length - 1];
479
+ var prevPath = Path.previous(_path2);
480
+ var prevIndex = prevPath[prevPath.length - 1];
481
+ modifyChildren(editor, Path.parent(_path2), children => {
482
+ var node = children[index];
483
+ var prev = children[prevIndex];
484
+ var newNode;
485
+ if (Text.isText(node) && Text.isText(prev)) {
486
+ newNode = _objectSpread$e(_objectSpread$e({}, prev), {}, {
487
+ text: prev.text + node.text
488
+ });
489
+ } else if (!Text.isText(node) && !Text.isText(prev)) {
490
+ newNode = _objectSpread$e(_objectSpread$e({}, prev), {}, {
491
+ children: prev.children.concat(node.children)
492
+ });
493
+ } else {
494
+ throw new Error("Cannot apply a \"merge_node\" operation at path [".concat(_path2, "] to nodes of different interfaces: ").concat(Scrubber.stringify(node), " ").concat(Scrubber.stringify(prev)));
495
+ }
496
+ return replaceChildren(children, prevIndex, 2, newNode);
497
+ });
498
+ transformSelection = true;
499
+ break;
448
500
  }
449
- var _node3 = Node.get(editor, _path3);
450
- var _parent2 = Node.parent(editor, _path3);
451
- var _index2 = _path3[_path3.length - 1];
452
- // This is tricky, but since the `path` and `newPath` both refer to
453
- // the same snapshot in time, there's a mismatch. After either
454
- // removing the original position, the second step's path can be out
455
- // of date. So instead of using the `op.newPath` directly, we
456
- // transform `op.path` to ascertain what the `newPath` would be after
457
- // the operation was applied.
458
- _parent2.children.splice(_index2, 1);
459
- var truePath = Path.transform(_path3, op);
460
- var newParent = Node.get(editor, Path.parent(truePath));
461
- var newIndex = truePath[truePath.length - 1];
462
- newParent.children.splice(newIndex, 0, _node3);
463
- if (selection) {
464
- for (var [_point3, _key3] of Range.points(selection)) {
465
- selection[_key3] = Point.transform(_point3, op);
501
+ case 'move_node':
502
+ {
503
+ var {
504
+ path: _path3,
505
+ newPath
506
+ } = op;
507
+ var _index2 = _path3[_path3.length - 1];
508
+ if (Path.isAncestor(_path3, newPath)) {
509
+ throw new Error("Cannot move a path [".concat(_path3, "] to new path [").concat(newPath, "] because the destination is inside itself."));
466
510
  }
511
+ var _node = Node.get(editor, _path3);
512
+ modifyChildren(editor, Path.parent(_path3), children => removeChildren(children, _index2, 1));
513
+ // This is tricky, but since the `path` and `newPath` both refer to
514
+ // the same snapshot in time, there's a mismatch. After either
515
+ // removing the original position, the second step's path can be out
516
+ // of date. So instead of using the `op.newPath` directly, we
517
+ // transform `op.path` to ascertain what the `newPath` would be after
518
+ // the operation was applied.
519
+ var truePath = Path.transform(_path3, op);
520
+ var newIndex = truePath[truePath.length - 1];
521
+ modifyChildren(editor, Path.parent(truePath), children => insertChildren(children, newIndex, _node));
522
+ transformSelection = true;
523
+ break;
467
524
  }
468
- break;
469
- }
470
- case 'remove_node':
471
- {
472
- var {
473
- path: _path4
474
- } = op;
475
- var _index3 = _path4[_path4.length - 1];
476
- var _parent3 = Node.parent(editor, _path4);
477
- _parent3.children.splice(_index3, 1);
478
- // Transform all the points in the value, but if the point was in the
479
- // node that was removed we need to update the range or remove it.
480
- if (selection) {
481
- for (var [_point4, _key4] of Range.points(selection)) {
482
- var result = Point.transform(_point4, op);
483
- if (selection != null && result != null) {
484
- selection[_key4] = result;
485
- } else {
486
- var _prev = void 0;
487
- var next = void 0;
488
- for (var [n, p] of Node.texts(editor)) {
489
- if (Path.compare(p, _path4) === -1) {
490
- _prev = [n, p];
491
- } else {
492
- next = [n, p];
493
- break;
525
+ case 'remove_node':
526
+ {
527
+ var {
528
+ path: _path4
529
+ } = op;
530
+ var _index3 = _path4[_path4.length - 1];
531
+ modifyChildren(editor, Path.parent(_path4), children => removeChildren(children, _index3, 1));
532
+ // Transform all the points in the value, but if the point was in the
533
+ // node that was removed we need to update the range or remove it.
534
+ if (editor.selection) {
535
+ var selection = _objectSpread$e({}, editor.selection);
536
+ for (var [point, key] of Range.points(selection)) {
537
+ var result = Point.transform(point, op);
538
+ if (selection != null && result != null) {
539
+ selection[key] = result;
540
+ } else {
541
+ var prev = void 0;
542
+ var next = void 0;
543
+ for (var [n, p] of Node.texts(editor)) {
544
+ if (Path.compare(p, _path4) === -1) {
545
+ prev = [n, p];
546
+ } else {
547
+ next = [n, p];
548
+ break;
549
+ }
494
550
  }
495
- }
496
- var preferNext = false;
497
- if (_prev && next) {
498
- if (Path.equals(next[1], _path4)) {
499
- preferNext = !Path.hasPrevious(next[1]);
551
+ var preferNext = false;
552
+ if (prev && next) {
553
+ if (Path.equals(next[1], _path4)) {
554
+ preferNext = !Path.hasPrevious(next[1]);
555
+ } else {
556
+ preferNext = Path.common(prev[1], _path4).length < Path.common(next[1], _path4).length;
557
+ }
558
+ }
559
+ if (prev && !preferNext) {
560
+ selection[key] = {
561
+ path: prev[1],
562
+ offset: prev[0].text.length
563
+ };
564
+ } else if (next) {
565
+ selection[key] = {
566
+ path: next[1],
567
+ offset: 0
568
+ };
500
569
  } else {
501
- preferNext = Path.common(_prev[1], _path4).length < Path.common(next[1], _path4).length;
570
+ selection = null;
502
571
  }
503
572
  }
504
- if (_prev && !preferNext) {
505
- _point4.path = _prev[1];
506
- _point4.offset = _prev[0].text.length;
507
- } else if (next) {
508
- _point4.path = next[1];
509
- _point4.offset = 0;
510
- } else {
511
- selection = null;
512
- }
513
573
  }
574
+ editor.selection = selection;
514
575
  }
576
+ break;
515
577
  }
516
- break;
517
- }
518
- case 'remove_text':
519
- {
520
- var {
521
- path: _path5,
522
- offset: _offset,
523
- text: _text
524
- } = op;
525
- if (_text.length === 0) break;
526
- var _node4 = Node.leaf(editor, _path5);
527
- var _before = _node4.text.slice(0, _offset);
528
- var _after = _node4.text.slice(_offset + _text.length);
529
- _node4.text = _before + _after;
530
- if (selection) {
531
- for (var [_point5, _key5] of Range.points(selection)) {
532
- selection[_key5] = Point.transform(_point5, op);
533
- }
534
- }
535
- break;
536
- }
537
- case 'set_node':
538
- {
539
- var {
540
- path: _path6,
541
- properties,
542
- newProperties
543
- } = op;
544
- if (_path6.length === 0) {
545
- throw new Error("Cannot set properties on the root node!");
578
+ case 'remove_text':
579
+ {
580
+ var {
581
+ path: _path5,
582
+ offset: _offset,
583
+ text: _text
584
+ } = op;
585
+ if (_text.length === 0) break;
586
+ modifyLeaf(editor, _path5, node => {
587
+ var before = node.text.slice(0, _offset);
588
+ var after = node.text.slice(_offset + _text.length);
589
+ return _objectSpread$e(_objectSpread$e({}, node), {}, {
590
+ text: before + after
591
+ });
592
+ });
593
+ transformSelection = true;
594
+ break;
546
595
  }
547
- var _node5 = Node.get(editor, _path6);
548
- for (var _key6 in newProperties) {
549
- if (_key6 === 'children' || _key6 === 'text') {
550
- throw new Error("Cannot set the \"".concat(_key6, "\" property of nodes!"));
551
- }
552
- var value = newProperties[_key6];
553
- if (value == null) {
554
- delete _node5[_key6];
555
- } else {
556
- _node5[_key6] = value;
596
+ case 'set_node':
597
+ {
598
+ var {
599
+ path: _path6,
600
+ properties,
601
+ newProperties
602
+ } = op;
603
+ if (_path6.length === 0) {
604
+ throw new Error("Cannot set properties on the root node!");
557
605
  }
606
+ modifyDescendant(editor, _path6, node => {
607
+ var newNode = _objectSpread$e({}, node);
608
+ for (var _key3 in newProperties) {
609
+ if (_key3 === 'children' || _key3 === 'text') {
610
+ throw new Error("Cannot set the \"".concat(_key3, "\" property of nodes!"));
611
+ }
612
+ var value = newProperties[_key3];
613
+ if (value == null) {
614
+ delete newNode[_key3];
615
+ } else {
616
+ newNode[_key3] = value;
617
+ }
618
+ }
619
+ // properties that were previously defined, but are now missing, must be deleted
620
+ for (var _key4 in properties) {
621
+ if (!newProperties.hasOwnProperty(_key4)) {
622
+ delete newNode[_key4];
623
+ }
624
+ }
625
+ return newNode;
626
+ });
627
+ break;
558
628
  }
559
- // properties that were previously defined, but are now missing, must be deleted
560
- for (var _key7 in properties) {
561
- if (!newProperties.hasOwnProperty(_key7)) {
562
- delete _node5[_key7];
629
+ case 'set_selection':
630
+ {
631
+ var {
632
+ newProperties: _newProperties
633
+ } = op;
634
+ if (_newProperties == null) {
635
+ editor.selection = null;
636
+ break;
563
637
  }
564
- }
565
- break;
566
- }
567
- case 'set_selection':
568
- {
569
- var {
570
- newProperties: _newProperties
571
- } = op;
572
- if (_newProperties == null) {
573
- selection = _newProperties;
574
- } else {
575
- if (selection == null) {
638
+ if (editor.selection == null) {
576
639
  if (!Range.isRange(_newProperties)) {
577
640
  throw new Error("Cannot apply an incomplete \"set_selection\" operation properties ".concat(Scrubber.stringify(_newProperties), " when there is no current selection."));
578
641
  }
579
- selection = _objectSpread$e({}, _newProperties);
642
+ editor.selection = _objectSpread$e({}, _newProperties);
643
+ break;
580
644
  }
581
- for (var _key8 in _newProperties) {
582
- var _value = _newProperties[_key8];
583
- if (_value == null) {
584
- if (_key8 === 'anchor' || _key8 === 'focus') {
585
- throw new Error("Cannot remove the \"".concat(_key8, "\" selection property"));
645
+ var _selection = _objectSpread$e({}, editor.selection);
646
+ for (var _key5 in _newProperties) {
647
+ var value = _newProperties[_key5];
648
+ if (value == null) {
649
+ if (_key5 === 'anchor' || _key5 === 'focus') {
650
+ throw new Error("Cannot remove the \"".concat(_key5, "\" selection property"));
586
651
  }
587
- delete selection[_key8];
652
+ delete _selection[_key5];
588
653
  } else {
589
- selection[_key8] = _value;
654
+ _selection[_key5] = value;
590
655
  }
591
656
  }
657
+ editor.selection = _selection;
658
+ break;
592
659
  }
593
- break;
594
- }
595
- case 'split_node':
596
- {
597
- var {
598
- path: _path7,
599
- position,
600
- properties: _properties
601
- } = op;
602
- if (_path7.length === 0) {
603
- throw new Error("Cannot apply a \"split_node\" operation at path [".concat(_path7, "] because the root node cannot be split."));
604
- }
605
- var _node6 = Node.get(editor, _path7);
606
- var _parent4 = Node.parent(editor, _path7);
607
- var _index4 = _path7[_path7.length - 1];
608
- var newNode;
609
- if (Text.isText(_node6)) {
610
- var _before2 = _node6.text.slice(0, position);
611
- var _after2 = _node6.text.slice(position);
612
- _node6.text = _before2;
613
- newNode = _objectSpread$e(_objectSpread$e({}, _properties), {}, {
614
- text: _after2
615
- });
616
- } else {
617
- var _before3 = _node6.children.slice(0, position);
618
- var _after3 = _node6.children.slice(position);
619
- _node6.children = _before3;
620
- newNode = _objectSpread$e(_objectSpread$e({}, _properties), {}, {
621
- children: _after3
622
- });
623
- }
624
- _parent4.children.splice(_index4 + 1, 0, newNode);
625
- if (selection) {
626
- for (var [_point6, _key9] of Range.points(selection)) {
627
- selection[_key9] = Point.transform(_point6, op);
660
+ case 'split_node':
661
+ {
662
+ var {
663
+ path: _path7,
664
+ position,
665
+ properties: _properties
666
+ } = op;
667
+ var _index4 = _path7[_path7.length - 1];
668
+ if (_path7.length === 0) {
669
+ throw new Error("Cannot apply a \"split_node\" operation at path [".concat(_path7, "] because the root node cannot be split."));
628
670
  }
671
+ modifyChildren(editor, Path.parent(_path7), children => {
672
+ var node = children[_index4];
673
+ var newNode;
674
+ var nextNode;
675
+ if (Text.isText(node)) {
676
+ var before = node.text.slice(0, position);
677
+ var after = node.text.slice(position);
678
+ newNode = _objectSpread$e(_objectSpread$e({}, node), {}, {
679
+ text: before
680
+ });
681
+ nextNode = _objectSpread$e(_objectSpread$e({}, _properties), {}, {
682
+ text: after
683
+ });
684
+ } else {
685
+ var _before = node.children.slice(0, position);
686
+ var _after = node.children.slice(position);
687
+ newNode = _objectSpread$e(_objectSpread$e({}, node), {}, {
688
+ children: _before
689
+ });
690
+ nextNode = _objectSpread$e(_objectSpread$e({}, _properties), {}, {
691
+ children: _after
692
+ });
693
+ }
694
+ return replaceChildren(children, _index4, 1, newNode, nextNode);
695
+ });
696
+ transformSelection = true;
697
+ break;
629
698
  }
630
- break;
631
- }
632
- }
633
- return selection;
634
- };
635
- // eslint-disable-next-line no-redeclare
636
- var GeneralTransforms = {
637
- transform(editor, op) {
638
- editor.children = createDraft(editor.children);
639
- var selection = editor.selection && createDraft(editor.selection);
640
- try {
641
- selection = applyToDraft(editor, selection, op);
642
- } finally {
643
- editor.children = finishDraft(editor.children);
644
- if (selection) {
645
- editor.selection = isDraft(selection) ? finishDraft(selection) : selection;
646
- } else {
647
- editor.selection = null;
699
+ }
700
+ if (transformSelection && editor.selection) {
701
+ var _selection2 = _objectSpread$e({}, editor.selection);
702
+ for (var [_point, _key6] of Range.points(_selection2)) {
703
+ _selection2[_key6] = Point.transform(_point, op);
648
704
  }
705
+ editor.selection = _selection2;
649
706
  }
650
707
  }
651
708
  };
@@ -874,51 +931,51 @@ var Range = {
874
931
  },
875
932
  transform(range, op) {
876
933
  var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
877
- return produce(range, r => {
878
- if (r === null) {
879
- return null;
880
- }
881
- var {
882
- affinity = 'inward'
883
- } = options;
884
- var affinityAnchor;
885
- var affinityFocus;
886
- if (affinity === 'inward') {
887
- // If the range is collapsed, make sure to use the same affinity to
888
- // avoid the two points passing each other and expanding in the opposite
889
- // direction
890
- var isCollapsed = Range.isCollapsed(r);
891
- if (Range.isForward(r)) {
892
- affinityAnchor = 'forward';
893
- affinityFocus = isCollapsed ? affinityAnchor : 'backward';
894
- } else {
895
- affinityAnchor = 'backward';
896
- affinityFocus = isCollapsed ? affinityAnchor : 'forward';
897
- }
898
- } else if (affinity === 'outward') {
899
- if (Range.isForward(r)) {
900
- affinityAnchor = 'backward';
901
- affinityFocus = 'forward';
902
- } else {
903
- affinityAnchor = 'forward';
904
- affinityFocus = 'backward';
905
- }
934
+ if (range === null) {
935
+ return null;
936
+ }
937
+ var {
938
+ affinity = 'inward'
939
+ } = options;
940
+ var affinityAnchor;
941
+ var affinityFocus;
942
+ if (affinity === 'inward') {
943
+ // If the range is collapsed, make sure to use the same affinity to
944
+ // avoid the two points passing each other and expanding in the opposite
945
+ // direction
946
+ var isCollapsed = Range.isCollapsed(range);
947
+ if (Range.isForward(range)) {
948
+ affinityAnchor = 'forward';
949
+ affinityFocus = isCollapsed ? affinityAnchor : 'backward';
906
950
  } else {
907
- affinityAnchor = affinity;
908
- affinityFocus = affinity;
951
+ affinityAnchor = 'backward';
952
+ affinityFocus = isCollapsed ? affinityAnchor : 'forward';
909
953
  }
910
- var anchor = Point.transform(r.anchor, op, {
911
- affinity: affinityAnchor
912
- });
913
- var focus = Point.transform(r.focus, op, {
914
- affinity: affinityFocus
915
- });
916
- if (!anchor || !focus) {
917
- return null;
954
+ } else if (affinity === 'outward') {
955
+ if (Range.isForward(range)) {
956
+ affinityAnchor = 'backward';
957
+ affinityFocus = 'forward';
958
+ } else {
959
+ affinityAnchor = 'forward';
960
+ affinityFocus = 'backward';
918
961
  }
919
- r.anchor = anchor;
920
- r.focus = focus;
962
+ } else {
963
+ affinityAnchor = affinity;
964
+ affinityFocus = affinity;
965
+ }
966
+ var anchor = Point.transform(range.anchor, op, {
967
+ affinity: affinityAnchor
968
+ });
969
+ var focus = Point.transform(range.focus, op, {
970
+ affinity: affinityFocus
921
971
  });
972
+ if (!anchor || !focus) {
973
+ return null;
974
+ }
975
+ return {
976
+ anchor,
977
+ focus
978
+ };
922
979
  }
923
980
  };
924
981
 
@@ -1680,72 +1737,76 @@ var Point = {
1680
1737
  },
1681
1738
  transform(point, op) {
1682
1739
  var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
1683
- return produce(point, p => {
1684
- if (p === null) {
1685
- return null;
1686
- }
1687
- var {
1688
- affinity = 'forward'
1689
- } = options;
1690
- var {
1691
- path,
1692
- offset
1693
- } = p;
1694
- switch (op.type) {
1695
- case 'insert_node':
1696
- case 'move_node':
1697
- {
1698
- p.path = Path.transform(path, op, options);
1699
- break;
1740
+ if (point === null) {
1741
+ return null;
1742
+ }
1743
+ var {
1744
+ affinity = 'forward'
1745
+ } = options;
1746
+ var {
1747
+ path,
1748
+ offset
1749
+ } = point;
1750
+ switch (op.type) {
1751
+ case 'insert_node':
1752
+ case 'move_node':
1753
+ {
1754
+ path = Path.transform(path, op, options);
1755
+ break;
1756
+ }
1757
+ case 'insert_text':
1758
+ {
1759
+ if (Path.equals(op.path, path) && (op.offset < offset || op.offset === offset && affinity === 'forward')) {
1760
+ offset += op.text.length;
1700
1761
  }
1701
- case 'insert_text':
1702
- {
1703
- if (Path.equals(op.path, path) && (op.offset < offset || op.offset === offset && affinity === 'forward')) {
1704
- p.offset += op.text.length;
1705
- }
1706
- break;
1762
+ break;
1763
+ }
1764
+ case 'merge_node':
1765
+ {
1766
+ if (Path.equals(op.path, path)) {
1767
+ offset += op.position;
1707
1768
  }
1708
- case 'merge_node':
1709
- {
1710
- if (Path.equals(op.path, path)) {
1711
- p.offset += op.position;
1712
- }
1713
- p.path = Path.transform(path, op, options);
1714
- break;
1769
+ path = Path.transform(path, op, options);
1770
+ break;
1771
+ }
1772
+ case 'remove_text':
1773
+ {
1774
+ if (Path.equals(op.path, path) && op.offset <= offset) {
1775
+ offset -= Math.min(offset - op.offset, op.text.length);
1715
1776
  }
1716
- case 'remove_text':
1717
- {
1718
- if (Path.equals(op.path, path) && op.offset <= offset) {
1719
- p.offset -= Math.min(offset - op.offset, op.text.length);
1720
- }
1721
- break;
1777
+ break;
1778
+ }
1779
+ case 'remove_node':
1780
+ {
1781
+ if (Path.equals(op.path, path) || Path.isAncestor(op.path, path)) {
1782
+ return null;
1722
1783
  }
1723
- case 'remove_node':
1724
- {
1725
- if (Path.equals(op.path, path) || Path.isAncestor(op.path, path)) {
1784
+ path = Path.transform(path, op, options);
1785
+ break;
1786
+ }
1787
+ case 'split_node':
1788
+ {
1789
+ if (Path.equals(op.path, path)) {
1790
+ if (op.position === offset && affinity == null) {
1726
1791
  return null;
1792
+ } else if (op.position < offset || op.position === offset && affinity === 'forward') {
1793
+ offset -= op.position;
1794
+ path = Path.transform(path, op, _objectSpread$b(_objectSpread$b({}, options), {}, {
1795
+ affinity: 'forward'
1796
+ }));
1727
1797
  }
1728
- p.path = Path.transform(path, op, options);
1729
- break;
1730
- }
1731
- case 'split_node':
1732
- {
1733
- if (Path.equals(op.path, path)) {
1734
- if (op.position === offset && affinity == null) {
1735
- return null;
1736
- } else if (op.position < offset || op.position === offset && affinity === 'forward') {
1737
- p.offset -= op.position;
1738
- p.path = Path.transform(path, op, _objectSpread$b(_objectSpread$b({}, options), {}, {
1739
- affinity: 'forward'
1740
- }));
1741
- }
1742
- } else {
1743
- p.path = Path.transform(path, op, options);
1744
- }
1745
- break;
1798
+ } else {
1799
+ path = Path.transform(path, op, options);
1746
1800
  }
1747
- }
1748
- });
1801
+ break;
1802
+ }
1803
+ default:
1804
+ return point;
1805
+ }
1806
+ return {
1807
+ path,
1808
+ offset
1809
+ };
1749
1810
  }
1750
1811
  };
1751
1812