prosemirror-transform 1.12.2 → 1.12.4

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/CHANGELOG.md CHANGED
@@ -1,3 +1,15 @@
1
+ ## 1.12.4 (2026-10-22)
2
+
3
+ ### Bug fixes
4
+
5
+ Fix broken deserialization of mark set steps.
6
+
7
+ ## 1.12.3 (2026-10-22)
8
+
9
+ ### Bug fixes
10
+
11
+ Fix an issue that could make inverting a mapped `AddMarkStep` or `RemoveMarkStep` sometimes produce an inverse that didn't actually cancel out with the step itself.
12
+
1
13
  ## 1.12.2 (2026-09-25)
2
14
 
3
15
  ### Bug fixes
package/dist/index.cjs CHANGED
@@ -3,6 +3,9 @@
3
3
  function _wrapNativeSuper(Class) { var _cache = typeof Map === "function" ? new Map() : undefined; _wrapNativeSuper = function _wrapNativeSuper(Class) { if (Class === null || !_isNativeFunction(Class)) return Class; if (typeof Class !== "function") { throw new TypeError("Super expression must either be null or a function"); } if (typeof _cache !== "undefined") { if (_cache.has(Class)) return _cache.get(Class); _cache.set(Class, Wrapper); } function Wrapper() { return _construct(Class, arguments, _getPrototypeOf(this).constructor); } Wrapper.prototype = Object.create(Class.prototype, { constructor: { value: Wrapper, enumerable: false, writable: true, configurable: true } }); return _setPrototypeOf(Wrapper, Class); }; return _wrapNativeSuper(Class); }
4
4
  function _construct(Parent, args, Class) { if (_isNativeReflectConstruct()) { _construct = Reflect.construct.bind(); } else { _construct = function _construct(Parent, args, Class) { var a = [null]; a.push.apply(a, args); var Constructor = Function.bind.apply(Parent, a); var instance = new Constructor(); if (Class) _setPrototypeOf(instance, Class.prototype); return instance; }; } return _construct.apply(null, arguments); }
5
5
  function _isNativeFunction(fn) { try { return Function.toString.call(fn).indexOf("[native code]") !== -1; } catch (e) { return typeof fn === "function"; } }
6
+ function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; }
7
+ function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
8
+ function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
6
9
  function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); Object.defineProperty(subClass, "prototype", { writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); }
7
10
  function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
8
11
  function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
@@ -374,16 +377,40 @@ var AddMarkStep = function (_Step) {
374
377
  }
375
378
  }, {
376
379
  key: "invert",
377
- value: function invert() {
378
- return new RemoveMarkStep(this.from, this.to, this.mark);
380
+ value: function invert(doc) {
381
+ var _this3 = this;
382
+ var steps = [];
383
+ doc.nodesBetween(this.from, this.to, function (node, pos) {
384
+ if (node.isInline && node.isAtom) {
385
+ var added = _this3.mark.addToSet(node.marks);
386
+ if (!prosemirrorModel.Mark.sameSet(added, node.marks)) {
387
+ var from = Math.max(_this3.from, pos),
388
+ to = Math.min(_this3.to, pos + node.nodeSize);
389
+ pushMarkStep(RemoveMarkStep, from, to, _this3.mark, steps);
390
+ var _iterator = _createForOfIteratorHelper(node.marks),
391
+ _step;
392
+ try {
393
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
394
+ var mark = _step.value;
395
+ if (!mark.isInSet(added)) pushMarkStep(AddMarkStep, from, to, mark, steps);
396
+ }
397
+ } catch (err) {
398
+ _iterator.e(err);
399
+ } finally {
400
+ _iterator.f();
401
+ }
402
+ }
403
+ }
404
+ });
405
+ return steps.length == 1 ? steps[0] : new MarkSetStep(steps);
379
406
  }
380
407
  }, {
381
408
  key: "map",
382
409
  value: function map(mapping) {
383
- var from = mapping.mapResult(this.from, 1),
384
- to = mapping.mapResult(this.to, -1);
385
- if (from.deleted && to.deleted || from.pos >= to.pos) return null;
386
- return new AddMarkStep(from.pos, to.pos, this.mark);
410
+ var from = mapping.map(this.from, 1),
411
+ to = mapping.map(this.to, -1);
412
+ if (from >= to) return null;
413
+ return new AddMarkStep(from, to, this.mark);
387
414
  }
388
415
  }, {
389
416
  key: "merge",
@@ -415,36 +442,41 @@ var RemoveMarkStep = function (_Step2) {
415
442
  _inherits(RemoveMarkStep, _Step2);
416
443
  var _super2 = _createSuper(RemoveMarkStep);
417
444
  function RemoveMarkStep(from, to, mark) {
418
- var _this3;
445
+ var _this4;
419
446
  _classCallCheck(this, RemoveMarkStep);
420
- _this3 = _super2.call(this);
421
- _this3.from = from;
422
- _this3.to = to;
423
- _this3.mark = mark;
424
- return _this3;
447
+ _this4 = _super2.call(this);
448
+ _this4.from = from;
449
+ _this4.to = to;
450
+ _this4.mark = mark;
451
+ return _this4;
425
452
  }
426
453
  _createClass(RemoveMarkStep, [{
427
454
  key: "apply",
428
455
  value: function apply(doc) {
429
- var _this4 = this;
456
+ var _this5 = this;
430
457
  var oldSlice = doc.slice(this.from, this.to);
431
458
  var slice = new prosemirrorModel.Slice(mapFragment(oldSlice.content, function (node) {
432
- return node.mark(_this4.mark.removeFromSet(node.marks));
459
+ return node.mark(_this5.mark.removeFromSet(node.marks));
433
460
  }, doc), oldSlice.openStart, oldSlice.openEnd);
434
461
  return StepResult.fromReplace(doc, this.from, this.to, slice);
435
462
  }
436
463
  }, {
437
464
  key: "invert",
438
- value: function invert() {
439
- return new AddMarkStep(this.from, this.to, this.mark);
465
+ value: function invert(doc) {
466
+ var _this6 = this;
467
+ var steps = [];
468
+ doc.nodesBetween(this.from, this.to, function (node, pos) {
469
+ if (node.isInline && node.isAtom && _this6.mark.isInSet(node.marks)) pushMarkStep(AddMarkStep, Math.max(_this6.from, pos), Math.min(_this6.to, pos + node.nodeSize), _this6.mark, steps);
470
+ });
471
+ return steps.length == 1 ? steps[0] : new MarkSetStep(steps);
440
472
  }
441
473
  }, {
442
474
  key: "map",
443
475
  value: function map(mapping) {
444
- var from = mapping.mapResult(this.from, 1),
445
- to = mapping.mapResult(this.to, -1);
446
- if (from.deleted && to.deleted || from.pos >= to.pos) return null;
447
- return new RemoveMarkStep(from.pos, to.pos, this.mark);
476
+ var from = mapping.map(this.from, 1),
477
+ to = mapping.map(this.to, -1);
478
+ if (from >= to) return null;
479
+ return new RemoveMarkStep(from, to, this.mark);
448
480
  }
449
481
  }, {
450
482
  key: "merge",
@@ -472,16 +504,103 @@ var RemoveMarkStep = function (_Step2) {
472
504
  return RemoveMarkStep;
473
505
  }(Step);
474
506
  Step.jsonID("removeMark", RemoveMarkStep);
475
- var AddNodeMarkStep = function (_Step3) {
476
- _inherits(AddNodeMarkStep, _Step3);
477
- var _super3 = _createSuper(AddNodeMarkStep);
507
+ function pushMarkStep(cls, from, to, mark, target) {
508
+ var last = target.length ? target[target.length - 1] : null;
509
+ if (last && last instanceof cls && last.to == from && last.mark.eq(mark)) {
510
+ target[target.length - 1] = new cls(last.from, to, mark);
511
+ } else {
512
+ target.push(new cls(from, to, mark));
513
+ }
514
+ }
515
+ var MarkSetStep = function (_Step3) {
516
+ _inherits(MarkSetStep, _Step3);
517
+ var _super3 = _createSuper(MarkSetStep);
518
+ function MarkSetStep(steps) {
519
+ var _this7;
520
+ _classCallCheck(this, MarkSetStep);
521
+ _this7 = _super3.call(this);
522
+ _this7.steps = steps;
523
+ return _this7;
524
+ }
525
+ _createClass(MarkSetStep, [{
526
+ key: "apply",
527
+ value: function apply(doc) {
528
+ var _iterator2 = _createForOfIteratorHelper(this.steps),
529
+ _step2;
530
+ try {
531
+ for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
532
+ var step = _step2.value;
533
+ var result = step.apply(doc);
534
+ if (!result.doc) return result;
535
+ doc = result.doc;
536
+ }
537
+ } catch (err) {
538
+ _iterator2.e(err);
539
+ } finally {
540
+ _iterator2.f();
541
+ }
542
+ return new StepResult(doc, null);
543
+ }
544
+ }, {
545
+ key: "invert",
546
+ value: function invert(doc) {
547
+ return new MarkSetStep(this.steps.map(function (s) {
548
+ return s.invert(doc);
549
+ }));
550
+ }
551
+ }, {
552
+ key: "map",
553
+ value: function map(mapping) {
554
+ var result = [];
555
+ var _iterator3 = _createForOfIteratorHelper(this.steps),
556
+ _step3;
557
+ try {
558
+ for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
559
+ var step = _step3.value;
560
+ var mapped = step.map(mapping);
561
+ if (mapped) result.push(mapped);
562
+ }
563
+ } catch (err) {
564
+ _iterator3.e(err);
565
+ } finally {
566
+ _iterator3.f();
567
+ }
568
+ return result.length ? new MarkSetStep(result) : null;
569
+ }
570
+ }, {
571
+ key: "toJSON",
572
+ value: function toJSON() {
573
+ return {
574
+ stepType: "markSet",
575
+ steps: this.steps.map(function (s) {
576
+ return s.toJSON();
577
+ })
578
+ };
579
+ }
580
+ }], [{
581
+ key: "fromJSON",
582
+ value: function fromJSON(schema, json) {
583
+ if (!Array.isArray(json.steps) || !json.steps.every(function (s) {
584
+ return s.stepType == "addMark" || s.stepType == "removeMark";
585
+ })) throw new RangeError("Invalid JSON for MarkSetStep");
586
+ return new MarkSetStep(json.steps.map(function (s) {
587
+ return Step.fromJSON(schema, s);
588
+ }));
589
+ }
590
+ }]);
591
+ return MarkSetStep;
592
+ }(Step);
593
+ Step.jsonID("markSet", MarkSetStep);
594
+ var AddNodeMarkStep = function (_Step4) {
595
+ _inherits(AddNodeMarkStep, _Step4);
596
+ var _super4 = _createSuper(AddNodeMarkStep);
478
597
  function AddNodeMarkStep(pos, mark) {
479
- var _this5;
598
+ var _this8;
480
599
  _classCallCheck(this, AddNodeMarkStep);
481
- _this5 = _super3.call(this);
482
- _this5.pos = pos;
483
- _this5.mark = mark;
484
- return _this5;
600
+ _this8 = _super4.call(this);
601
+ _this8.pos = pos;
602
+ _this8.mark = mark;
603
+ return _this8;
485
604
  }
486
605
  _createClass(AddNodeMarkStep, [{
487
606
  key: "apply",
@@ -529,16 +648,16 @@ var AddNodeMarkStep = function (_Step3) {
529
648
  return AddNodeMarkStep;
530
649
  }(Step);
531
650
  Step.jsonID("addNodeMark", AddNodeMarkStep);
532
- var RemoveNodeMarkStep = function (_Step4) {
533
- _inherits(RemoveNodeMarkStep, _Step4);
534
- var _super4 = _createSuper(RemoveNodeMarkStep);
651
+ var RemoveNodeMarkStep = function (_Step5) {
652
+ _inherits(RemoveNodeMarkStep, _Step5);
653
+ var _super5 = _createSuper(RemoveNodeMarkStep);
535
654
  function RemoveNodeMarkStep(pos, mark) {
536
- var _this6;
655
+ var _this9;
537
656
  _classCallCheck(this, RemoveNodeMarkStep);
538
- _this6 = _super4.call(this);
539
- _this6.pos = pos;
540
- _this6.mark = mark;
541
- return _this6;
657
+ _this9 = _super5.call(this);
658
+ _this9.pos = pos;
659
+ _this9.mark = mark;
660
+ return _this9;
542
661
  }
543
662
  _createClass(RemoveNodeMarkStep, [{
544
663
  key: "apply",
@@ -580,19 +699,19 @@ var RemoveNodeMarkStep = function (_Step4) {
580
699
  return RemoveNodeMarkStep;
581
700
  }(Step);
582
701
  Step.jsonID("removeNodeMark", RemoveNodeMarkStep);
583
- var ReplaceStep = function (_Step5) {
584
- _inherits(ReplaceStep, _Step5);
585
- var _super5 = _createSuper(ReplaceStep);
702
+ var ReplaceStep = function (_Step6) {
703
+ _inherits(ReplaceStep, _Step6);
704
+ var _super6 = _createSuper(ReplaceStep);
586
705
  function ReplaceStep(from, to, slice) {
587
- var _this7;
706
+ var _this10;
588
707
  var structure = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false;
589
708
  _classCallCheck(this, ReplaceStep);
590
- _this7 = _super5.call(this);
591
- _this7.from = from;
592
- _this7.to = to;
593
- _this7.slice = slice;
594
- _this7.structure = structure;
595
- return _this7;
709
+ _this10 = _super6.call(this);
710
+ _this10.from = from;
711
+ _this10.to = to;
712
+ _this10.slice = slice;
713
+ _this10.structure = structure;
714
+ return _this10;
596
715
  }
597
716
  _createClass(ReplaceStep, [{
598
717
  key: "apply",
@@ -655,22 +774,22 @@ var ReplaceStep = function (_Step5) {
655
774
  }(Step);
656
775
  ReplaceStep.MAP_BIAS = 1;
657
776
  Step.jsonID("replace", ReplaceStep);
658
- var ReplaceAroundStep = function (_Step6) {
659
- _inherits(ReplaceAroundStep, _Step6);
660
- var _super6 = _createSuper(ReplaceAroundStep);
777
+ var ReplaceAroundStep = function (_Step7) {
778
+ _inherits(ReplaceAroundStep, _Step7);
779
+ var _super7 = _createSuper(ReplaceAroundStep);
661
780
  function ReplaceAroundStep(from, to, gapFrom, gapTo, slice, insert) {
662
- var _this8;
781
+ var _this11;
663
782
  var structure = arguments.length > 6 && arguments[6] !== undefined ? arguments[6] : false;
664
783
  _classCallCheck(this, ReplaceAroundStep);
665
- _this8 = _super6.call(this);
666
- _this8.from = from;
667
- _this8.to = to;
668
- _this8.gapFrom = gapFrom;
669
- _this8.gapTo = gapTo;
670
- _this8.slice = slice;
671
- _this8.insert = insert;
672
- _this8.structure = structure;
673
- return _this8;
784
+ _this11 = _super7.call(this);
785
+ _this11.from = from;
786
+ _this11.to = to;
787
+ _this11.gapFrom = gapFrom;
788
+ _this11.gapTo = gapTo;
789
+ _this11.slice = slice;
790
+ _this11.insert = insert;
791
+ _this11.structure = structure;
792
+ return _this11;
674
793
  }
675
794
  _createClass(ReplaceAroundStep, [{
676
795
  key: "apply",
@@ -747,27 +866,16 @@ function contentBetween(doc, from, to) {
747
866
  return false;
748
867
  }
749
868
  function _addMark(tr, from, to, mark) {
750
- var removed = [],
751
- added = [];
752
- var removing, adding;
869
+ var added = [];
753
870
  tr.doc.nodesBetween(from, to, function (node, pos, parent) {
754
871
  if (!node.isInline) return;
755
872
  var marks = node.marks;
756
873
  if (!mark.isInSet(marks) && parent.type.allowsMarkType(mark.type)) {
757
874
  var start = Math.max(pos, from),
758
875
  end = Math.min(pos + node.nodeSize, to);
759
- var newSet = mark.addToSet(marks);
760
- for (var i = 0; i < marks.length; i++) {
761
- if (!marks[i].isInSet(newSet)) {
762
- if (removing && removing.to == start && removing.mark.eq(marks[i])) removing.to = end;else removed.push(removing = new RemoveMarkStep(start, end, marks[i]));
763
- }
764
- }
765
- if (adding && adding.to == start) adding.to = end;else added.push(adding = new AddMarkStep(start, end, mark));
876
+ pushMarkStep(AddMarkStep, start, end, mark, added);
766
877
  }
767
878
  });
768
- removed.forEach(function (s) {
769
- return tr.step(s);
770
- });
771
879
  added.forEach(function (s) {
772
880
  return tr.step(s);
773
881
  });
@@ -776,7 +884,7 @@ function _removeMark(tr, from, to, mark) {
776
884
  var matched = [],
777
885
  step = 0;
778
886
  tr.doc.nodesBetween(from, to, function (node, pos) {
779
- if (!node.isInline) return;
887
+ if (!node.isInline || !node.isAtom) return;
780
888
  step++;
781
889
  var toRemove = null;
782
890
  if (mark instanceof prosemirrorModel.MarkType) {
@@ -1552,17 +1660,17 @@ function coveredDepths($from, $to) {
1552
1660
  }
1553
1661
  return result;
1554
1662
  }
1555
- var AttrStep = function (_Step7) {
1556
- _inherits(AttrStep, _Step7);
1557
- var _super7 = _createSuper(AttrStep);
1663
+ var AttrStep = function (_Step8) {
1664
+ _inherits(AttrStep, _Step8);
1665
+ var _super8 = _createSuper(AttrStep);
1558
1666
  function AttrStep(pos, attr, value) {
1559
- var _this9;
1667
+ var _this12;
1560
1668
  _classCallCheck(this, AttrStep);
1561
- _this9 = _super7.call(this);
1562
- _this9.pos = pos;
1563
- _this9.attr = attr;
1564
- _this9.value = value;
1565
- return _this9;
1669
+ _this12 = _super8.call(this);
1670
+ _this12.pos = pos;
1671
+ _this12.attr = attr;
1672
+ _this12.value = value;
1673
+ return _this12;
1566
1674
  }
1567
1675
  _createClass(AttrStep, [{
1568
1676
  key: "apply",
@@ -1611,16 +1719,16 @@ var AttrStep = function (_Step7) {
1611
1719
  return AttrStep;
1612
1720
  }(Step);
1613
1721
  Step.jsonID("attr", AttrStep);
1614
- var DocAttrStep = function (_Step8) {
1615
- _inherits(DocAttrStep, _Step8);
1616
- var _super8 = _createSuper(DocAttrStep);
1722
+ var DocAttrStep = function (_Step9) {
1723
+ _inherits(DocAttrStep, _Step9);
1724
+ var _super9 = _createSuper(DocAttrStep);
1617
1725
  function DocAttrStep(attr, value) {
1618
- var _this10;
1726
+ var _this13;
1619
1727
  _classCallCheck(this, DocAttrStep);
1620
- _this10 = _super8.call(this);
1621
- _this10.attr = attr;
1622
- _this10.value = value;
1623
- return _this10;
1728
+ _this13 = _super9.call(this);
1729
+ _this13.attr = attr;
1730
+ _this13.value = value;
1731
+ return _this13;
1624
1732
  }
1625
1733
  _createClass(DocAttrStep, [{
1626
1734
  key: "apply",
@@ -1667,10 +1775,10 @@ var DocAttrStep = function (_Step8) {
1667
1775
  Step.jsonID("docAttr", DocAttrStep);
1668
1776
  exports.TransformError = function (_Error) {
1669
1777
  _inherits(TransformError, _Error);
1670
- var _super9 = _createSuper(TransformError);
1778
+ var _super10 = _createSuper(TransformError);
1671
1779
  function TransformError() {
1672
1780
  _classCallCheck(this, TransformError);
1673
- return _super9.apply(this, arguments);
1781
+ return _super10.apply(this, arguments);
1674
1782
  }
1675
1783
  return _createClass(TransformError);
1676
1784
  }(_wrapNativeSuper(Error));
@@ -1697,8 +1805,8 @@ var Transform = function () {
1697
1805
  }
1698
1806
  }, {
1699
1807
  key: "step",
1700
- value: function step(_step) {
1701
- var result = this.maybeStep(_step);
1808
+ value: function step(_step4) {
1809
+ var result = this.maybeStep(_step4);
1702
1810
  if (result.failed) throw new exports.TransformError(result.failed);
1703
1811
  return this;
1704
1812
  }
package/dist/index.d.cts CHANGED
@@ -554,7 +554,7 @@ declare class AddMarkStep extends Step {
554
554
  */
555
555
  mark: Mark);
556
556
  apply(doc: Node): StepResult;
557
- invert(): Step;
557
+ invert(doc: Node): Step;
558
558
  map(mapping: Mappable): Step | null;
559
559
  merge(other: Step): Step | null;
560
560
  toJSON(): any;
@@ -592,7 +592,7 @@ declare class RemoveMarkStep extends Step {
592
592
  */
593
593
  mark: Mark);
594
594
  apply(doc: Node): StepResult;
595
- invert(): Step;
595
+ invert(doc: Node): Step;
596
596
  map(mapping: Mappable): Step | null;
597
597
  merge(other: Step): Step | null;
598
598
  toJSON(): any;
package/dist/index.d.ts CHANGED
@@ -554,7 +554,7 @@ declare class AddMarkStep extends Step {
554
554
  */
555
555
  mark: Mark);
556
556
  apply(doc: Node): StepResult;
557
- invert(): Step;
557
+ invert(doc: Node): Step;
558
558
  map(mapping: Mappable): Step | null;
559
559
  merge(other: Step): Step | null;
560
560
  toJSON(): any;
@@ -592,7 +592,7 @@ declare class RemoveMarkStep extends Step {
592
592
  */
593
593
  mark: Mark);
594
594
  apply(doc: Node): StepResult;
595
- invert(): Step;
595
+ invert(doc: Node): Step;
596
596
  map(mapping: Mappable): Step | null;
597
597
  merge(other: Step): Step | null;
598
598
  toJSON(): any;
package/dist/index.js CHANGED
@@ -1,4 +1,4 @@
1
- import { ReplaceError, Slice, Fragment, MarkType, Mark } from 'prosemirror-model';
1
+ import { ReplaceError, Slice, Mark, Fragment, MarkType } from 'prosemirror-model';
2
2
 
3
3
  // Recovery values encode a range index and an offset. They are
4
4
  // represented as numbers, because tons of them will be created when
@@ -466,14 +466,27 @@ class AddMarkStep extends Step {
466
466
  }, parent), oldSlice.openStart, oldSlice.openEnd);
467
467
  return StepResult.fromReplace(doc, this.from, this.to, slice);
468
468
  }
469
- invert() {
470
- return new RemoveMarkStep(this.from, this.to, this.mark);
469
+ invert(doc) {
470
+ let steps = [];
471
+ doc.nodesBetween(this.from, this.to, (node, pos) => {
472
+ if (node.isInline && node.isAtom) {
473
+ let added = this.mark.addToSet(node.marks);
474
+ if (!Mark.sameSet(added, node.marks)) {
475
+ let from = Math.max(this.from, pos), to = Math.min(this.to, pos + node.nodeSize);
476
+ pushMarkStep(RemoveMarkStep, from, to, this.mark, steps);
477
+ for (let mark of node.marks)
478
+ if (!mark.isInSet(added))
479
+ pushMarkStep(AddMarkStep, from, to, mark, steps);
480
+ }
481
+ }
482
+ });
483
+ return steps.length == 1 ? steps[0] : new MarkSetStep(steps);
471
484
  }
472
485
  map(mapping) {
473
- let from = mapping.mapResult(this.from, 1), to = mapping.mapResult(this.to, -1);
474
- if (from.deleted && to.deleted || from.pos >= to.pos)
486
+ let from = mapping.map(this.from, 1), to = mapping.map(this.to, -1);
487
+ if (from >= to)
475
488
  return null;
476
- return new AddMarkStep(from.pos, to.pos, this.mark);
489
+ return new AddMarkStep(from, to, this.mark);
477
490
  }
478
491
  merge(other) {
479
492
  if (other instanceof AddMarkStep &&
@@ -528,14 +541,19 @@ class RemoveMarkStep extends Step {
528
541
  }, doc), oldSlice.openStart, oldSlice.openEnd);
529
542
  return StepResult.fromReplace(doc, this.from, this.to, slice);
530
543
  }
531
- invert() {
532
- return new AddMarkStep(this.from, this.to, this.mark);
544
+ invert(doc) {
545
+ let steps = [];
546
+ doc.nodesBetween(this.from, this.to, (node, pos) => {
547
+ if (node.isInline && node.isAtom && this.mark.isInSet(node.marks))
548
+ pushMarkStep(AddMarkStep, Math.max(this.from, pos), Math.min(this.to, pos + node.nodeSize), this.mark, steps);
549
+ });
550
+ return steps.length == 1 ? steps[0] : new MarkSetStep(steps);
533
551
  }
534
552
  map(mapping) {
535
- let from = mapping.mapResult(this.from, 1), to = mapping.mapResult(this.to, -1);
536
- if (from.deleted && to.deleted || from.pos >= to.pos)
553
+ let from = mapping.map(this.from, 1), to = mapping.map(this.to, -1);
554
+ if (from >= to)
537
555
  return null;
538
- return new RemoveMarkStep(from.pos, to.pos, this.mark);
556
+ return new RemoveMarkStep(from, to, this.mark);
539
557
  }
540
558
  merge(other) {
541
559
  if (other instanceof RemoveMarkStep &&
@@ -558,6 +576,51 @@ class RemoveMarkStep extends Step {
558
576
  }
559
577
  }
560
578
  Step.jsonID("removeMark", RemoveMarkStep);
579
+ function pushMarkStep(cls, from, to, mark, target) {
580
+ let last = target.length ? target[target.length - 1] : null;
581
+ if (last && last instanceof cls && last.to == from && last.mark.eq(mark)) {
582
+ target[target.length - 1] = new cls(last.from, to, mark);
583
+ }
584
+ else {
585
+ target.push(new cls(from, to, mark));
586
+ }
587
+ }
588
+ class MarkSetStep extends Step {
589
+ constructor(steps) {
590
+ super();
591
+ this.steps = steps;
592
+ }
593
+ apply(doc) {
594
+ for (let step of this.steps) {
595
+ let result = step.apply(doc);
596
+ if (!result.doc)
597
+ return result;
598
+ doc = result.doc;
599
+ }
600
+ return new StepResult(doc, null);
601
+ }
602
+ invert(doc) {
603
+ return new MarkSetStep(this.steps.map(s => s.invert(doc)));
604
+ }
605
+ map(mapping) {
606
+ let result = [];
607
+ for (let step of this.steps) {
608
+ let mapped = step.map(mapping);
609
+ if (mapped)
610
+ result.push(mapped);
611
+ }
612
+ return result.length ? new MarkSetStep(result) : null;
613
+ }
614
+ toJSON() {
615
+ return { stepType: "markSet", steps: this.steps.map(s => s.toJSON()) };
616
+ }
617
+ static fromJSON(schema, json) {
618
+ if (!Array.isArray(json.steps) || !json.steps.every((s) => s.stepType == "addMark" || s.stepType == "removeMark"))
619
+ throw new RangeError("Invalid JSON for MarkSetStep");
620
+ return new MarkSetStep(json.steps.map((s) => Step.fromJSON(schema, s)));
621
+ }
622
+ }
623
+ Step.jsonID("markSet", MarkSetStep);
561
624
  /**
562
625
  Add a mark to a specific node.
563
626
  */
@@ -882,36 +945,22 @@ function contentBetween(doc, from, to) {
882
945
  }
883
946
 
884
947
  function addMark(tr, from, to, mark) {
885
- let removed = [], added = [];
886
- let removing, adding;
948
+ let added = [];
887
949
  tr.doc.nodesBetween(from, to, (node, pos, parent) => {
888
950
  if (!node.isInline)
889
951
  return;
890
952
  let marks = node.marks;
891
953
  if (!mark.isInSet(marks) && parent.type.allowsMarkType(mark.type)) {
892
954
  let start = Math.max(pos, from), end = Math.min(pos + node.nodeSize, to);
893
- let newSet = mark.addToSet(marks);
894
- for (let i = 0; i < marks.length; i++) {
895
- if (!marks[i].isInSet(newSet)) {
896
- if (removing && removing.to == start && removing.mark.eq(marks[i]))
897
- removing.to = end;
898
- else
899
- removed.push(removing = new RemoveMarkStep(start, end, marks[i]));
900
- }
901
- }
902
- if (adding && adding.to == start)
903
- adding.to = end;
904
- else
905
- added.push(adding = new AddMarkStep(start, end, mark));
955
+ pushMarkStep(AddMarkStep, start, end, mark, added);
906
956
  }
907
957
  });
908
- removed.forEach(s => tr.step(s));
909
958
  added.forEach(s => tr.step(s));
910
959
  }
911
960
  function removeMark(tr, from, to, mark) {
912
961
  let matched = [], step = 0;
913
962
  tr.doc.nodesBetween(from, to, (node, pos) => {
914
- if (!node.isInline)
963
+ if (!node.isInline || !node.isAtom)
915
964
  return;
916
965
  step++;
917
966
  let toRemove = null;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "prosemirror-transform",
3
- "version": "1.12.2",
3
+ "version": "1.12.4",
4
4
  "description": "ProseMirror document transformations",
5
5
  "type": "module",
6
6
  "main": "dist/index.cjs",
package/src/mark.ts CHANGED
@@ -2,43 +2,26 @@ import {Mark, MarkType, Slice, Fragment, NodeType} from "prosemirror-model"
2
2
 
3
3
  import {Step} from "./step"
4
4
  import {Transform} from "./transform"
5
- import {AddMarkStep, RemoveMarkStep} from "./mark_step"
5
+ import {AddMarkStep, RemoveMarkStep, pushMarkStep} from "./mark_step"
6
6
  import {ReplaceStep} from "./replace_step"
7
7
 
8
8
  export function addMark(tr: Transform, from: number, to: number, mark: Mark) {
9
- let removed: Step[] = [], added: Step[] = []
10
- let removing: RemoveMarkStep | undefined, adding: AddMarkStep | undefined
9
+ let added: AddMarkStep[] = []
11
10
  tr.doc.nodesBetween(from, to, (node, pos, parent) => {
12
11
  if (!node.isInline) return
13
12
  let marks = node.marks
14
13
  if (!mark.isInSet(marks) && parent!.type.allowsMarkType(mark.type)) {
15
14
  let start = Math.max(pos, from), end = Math.min(pos + node.nodeSize, to)
16
- let newSet = mark.addToSet(marks)
17
-
18
- for (let i = 0; i < marks.length; i++) {
19
- if (!marks[i].isInSet(newSet)) {
20
- if (removing && removing.to == start && removing.mark.eq(marks[i]))
21
- (removing as any).to = end
22
- else
23
- removed.push(removing = new RemoveMarkStep(start, end, marks[i]))
24
- }
25
- }
26
-
27
- if (adding && adding.to == start)
28
- (adding as any).to = end
29
- else
30
- added.push(adding = new AddMarkStep(start, end, mark))
15
+ pushMarkStep(AddMarkStep, start, end, mark, added)
31
16
  }
32
17
  })
33
-
34
- removed.forEach(s => tr.step(s))
35
18
  added.forEach(s => tr.step(s))
36
19
  }
37
20
 
38
21
  export function removeMark(tr: Transform, from: number, to: number, mark?: Mark | MarkType | null) {
39
22
  let matched: {style: Mark, from: number, to: number, step: number}[] = [], step = 0
40
23
  tr.doc.nodesBetween(from, to, (node, pos) => {
41
- if (!node.isInline) return
24
+ if (!node.isInline || !node.isAtom) return
42
25
  step++
43
26
  let toRemove = null
44
27
  if (mark instanceof MarkType) {
package/src/mark_step.ts CHANGED
@@ -37,14 +37,26 @@ export class AddMarkStep extends Step {
37
37
  return StepResult.fromReplace(doc, this.from, this.to, slice)
38
38
  }
39
39
 
40
- invert(): Step {
41
- return new RemoveMarkStep(this.from, this.to, this.mark)
40
+ invert(doc: Node): Step {
41
+ let steps: (RemoveMarkStep | AddMarkStep)[] = []
42
+ doc.nodesBetween(this.from, this.to, (node, pos) => {
43
+ if (node.isInline && node.isAtom) {
44
+ let added = this.mark.addToSet(node.marks)
45
+ if (!Mark.sameSet(added, node.marks)) {
46
+ let from = Math.max(this.from, pos), to = Math.min(this.to, pos + node.nodeSize)
47
+ pushMarkStep(RemoveMarkStep, from, to, this.mark, steps)
48
+ for (let mark of node.marks) if (!mark.isInSet(added))
49
+ pushMarkStep(AddMarkStep, from, to, mark, steps)
50
+ }
51
+ }
52
+ })
53
+ return steps.length == 1 ? steps[0] : new MarkSetStep(steps)
42
54
  }
43
55
 
44
56
  map(mapping: Mappable): Step | null {
45
- let from = mapping.mapResult(this.from, 1), to = mapping.mapResult(this.to, -1)
46
- if (from.deleted && to.deleted || from.pos >= to.pos) return null
47
- return new AddMarkStep(from.pos, to.pos, this.mark)
57
+ let from = mapping.map(this.from, 1), to = mapping.map(this.to, -1)
58
+ if (from >= to) return null
59
+ return new AddMarkStep(from, to, this.mark)
48
60
  }
49
61
 
50
62
  merge(other: Step): Step | null {
@@ -93,14 +105,19 @@ export class RemoveMarkStep extends Step {
93
105
  return StepResult.fromReplace(doc, this.from, this.to, slice)
94
106
  }
95
107
 
96
- invert(): Step {
97
- return new AddMarkStep(this.from, this.to, this.mark)
108
+ invert(doc: Node): Step {
109
+ let steps: (AddMarkStep | RemoveMarkStep)[] = []
110
+ doc.nodesBetween(this.from, this.to, (node, pos) => {
111
+ if (node.isInline && node.isAtom && this.mark.isInSet(node.marks))
112
+ pushMarkStep(AddMarkStep, Math.max(this.from, pos), Math.min(this.to, pos + node.nodeSize), this.mark, steps)
113
+ })
114
+ return steps.length == 1 ? steps[0] : new MarkSetStep(steps)
98
115
  }
99
116
 
100
117
  map(mapping: Mappable): Step | null {
101
- let from = mapping.mapResult(this.from, 1), to = mapping.mapResult(this.to, -1)
102
- if (from.deleted && to.deleted || from.pos >= to.pos) return null
103
- return new RemoveMarkStep(from.pos, to.pos, this.mark)
118
+ let from = mapping.map(this.from, 1), to = mapping.map(this.to, -1)
119
+ if (from >= to) return null
120
+ return new RemoveMarkStep(from, to, this.mark)
104
121
  }
105
122
 
106
123
  merge(other: Step): Step | null {
@@ -127,6 +144,60 @@ export class RemoveMarkStep extends Step {
127
144
 
128
145
  Step.jsonID("removeMark", RemoveMarkStep)
129
146
 
147
+ export function pushMarkStep(
148
+ cls: typeof AddMarkStep | typeof RemoveMarkStep,
149
+ from: number, to: number, mark: Mark,
150
+ target: (AddMarkStep | RemoveMarkStep)[]) {
151
+ let last = target.length ? target[target.length - 1] : null
152
+ if (last && last instanceof cls && last.to == from && last.mark.eq(mark)) {
153
+ target[target.length - 1] = new cls(last.from, to, mark)
154
+ } else {
155
+ target.push(new cls(from, to, mark))
156
+ }
157
+ }
158
+
159
+ class MarkSetStep extends Step {
160
+ constructor(
161
+ readonly steps: readonly (AddMarkStep | RemoveMarkStep)[]
162
+ ) {
163
+ super()
164
+ }
165
+
166
+ apply(doc: Node) {
167
+ for (let step of this.steps) {
168
+ let result = step.apply(doc)
169
+ if (!result.doc) return result
170
+ doc = result.doc
171
+ }
172
+ return new StepResult(doc, null)
173
+ }
174
+
175
+ invert(doc: Node): Step {
176
+ return new MarkSetStep(this.steps.map(s => s.invert(doc) as AddMarkStep | RemoveMarkStep))
177
+ }
178
+
179
+ map(mapping: Mappable) {
180
+ let result: (AddMarkStep | RemoveMarkStep)[] = []
181
+ for (let step of this.steps) {
182
+ let mapped = step.map(mapping)
183
+ if (mapped) result.push(mapped as AddMarkStep | RemoveMarkStep)
184
+ }
185
+ return result.length ? new MarkSetStep(result) : null
186
+ }
187
+
188
+ toJSON() {
189
+ return {stepType: "markSet", steps: this.steps.map(s => s.toJSON())}
190
+ }
191
+
192
+ static fromJSON(schema: Schema, json: any) {
193
+ if (!Array.isArray(json.steps) || !json.steps.every((s: any) => s.stepType == "addMark" || s.stepType == "removeMark"))
194
+ throw new RangeError("Invalid JSON for MarkSetStep")
195
+ return new MarkSetStep(json.steps.map((s: any) => Step.fromJSON(schema, s)))
196
+ }
197
+ }
198
+
199
+ Step.jsonID("markSet", MarkSetStep)
200
+
130
201
  /// Add a mark to a specific node.
131
202
  export class AddNodeMarkStep extends Step {
132
203
  /// Create a node mark step.