prosemirror-transform 1.6.0 → 1.7.1

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,19 @@
1
+ ## 1.7.1 (2023-01-20)
2
+
3
+ ### Bug fixes
4
+
5
+ Keep content in isolating nodes inside their parent when fitting a replace step.
6
+
7
+ `Transform.setNodeMarkup` will no longer clear the node's marks when it isn't given an array of marks.
8
+
9
+ ## 1.7.0 (2022-08-16)
10
+
11
+ ### New features
12
+
13
+ The new `AttrStep` (and `Transform.setNodeAttribute`) can be used to set individual attributes on a node.
14
+
15
+ `AddNodeMarkStep` and `RemoveNodeMarkStep` can now be used to add and remove marks on individual nodes. `Transform.addNodeMark`/`removeNodeMark` provide an interface to these in transform objects.
16
+
1
17
  ## 1.6.0 (2022-06-01)
2
18
 
3
19
  ### Bug fixes
package/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # prosemirror-transform
2
2
 
3
- [ [**WEBSITE**](https://prosemirror.net) | [**ISSUES**](https://github.com/prosemirror/prosemirror/issues) | [**FORUM**](https://discuss.prosemirror.net) | [**GITTER**](https://gitter.im/ProseMirror/prosemirror) | [**CHANGELOG**](https://github.com/ProseMirror/prosemirror-transform/blob/master/CHANGELOG.md) ]
3
+ [ [**WEBSITE**](https://prosemirror.net) | [**ISSUES**](https://github.com/prosemirror/prosemirror/issues) | [**FORUM**](https://discuss.prosemirror.net) | [**CHANGELOG**](https://github.com/ProseMirror/prosemirror-transform/blob/master/CHANGELOG.md) ]
4
4
 
5
5
  This is a [core module](https://prosemirror.net/docs/ref/#transform) of [ProseMirror](https://prosemirror.net).
6
6
  ProseMirror is a well-behaved rich semantic content editor based on
package/dist/index.cjs CHANGED
@@ -551,24 +551,154 @@ var RemoveMarkStep = function (_Step2) {
551
551
 
552
552
  Step.jsonID("removeMark", RemoveMarkStep);
553
553
 
554
- var ReplaceStep = function (_Step3) {
555
- _inherits(ReplaceStep, _Step3);
554
+ var AddNodeMarkStep = function (_Step3) {
555
+ _inherits(AddNodeMarkStep, _Step3);
556
556
 
557
- var _super3 = _createSuper(ReplaceStep);
557
+ var _super3 = _createSuper(AddNodeMarkStep);
558
558
 
559
- function ReplaceStep(from, to, slice) {
559
+ function AddNodeMarkStep(pos, mark) {
560
560
  var _this5;
561
561
 
562
+ _classCallCheck(this, AddNodeMarkStep);
563
+
564
+ _this5 = _super3.call(this);
565
+ _this5.pos = pos;
566
+ _this5.mark = mark;
567
+ return _this5;
568
+ }
569
+
570
+ _createClass(AddNodeMarkStep, [{
571
+ key: "apply",
572
+ value: function apply(doc) {
573
+ var node = doc.nodeAt(this.pos);
574
+ if (!node) return StepResult.fail("No node at mark step's position");
575
+ var updated = node.type.create(node.attrs, null, this.mark.addToSet(node.marks));
576
+ return StepResult.fromReplace(doc, this.pos, this.pos + 1, new prosemirrorModel.Slice(prosemirrorModel.Fragment.from(updated), 0, node.isLeaf ? 0 : 1));
577
+ }
578
+ }, {
579
+ key: "invert",
580
+ value: function invert(doc) {
581
+ var node = doc.nodeAt(this.pos);
582
+
583
+ if (node) {
584
+ var newSet = this.mark.addToSet(node.marks);
585
+
586
+ if (newSet.length == node.marks.length) {
587
+ for (var i = 0; i < node.marks.length; i++) {
588
+ if (!node.marks[i].isInSet(newSet)) return new AddNodeMarkStep(this.pos, node.marks[i]);
589
+ }
590
+
591
+ return new AddNodeMarkStep(this.pos, this.mark);
592
+ }
593
+ }
594
+
595
+ return new RemoveNodeMarkStep(this.pos, this.mark);
596
+ }
597
+ }, {
598
+ key: "map",
599
+ value: function map(mapping) {
600
+ var pos = mapping.mapResult(this.pos, 1);
601
+ return pos.deletedAfter ? null : new AddNodeMarkStep(pos.pos, this.mark);
602
+ }
603
+ }, {
604
+ key: "toJSON",
605
+ value: function toJSON() {
606
+ return {
607
+ stepType: "addNodeMark",
608
+ pos: this.pos,
609
+ mark: this.mark.toJSON()
610
+ };
611
+ }
612
+ }], [{
613
+ key: "fromJSON",
614
+ value: function fromJSON(schema, json) {
615
+ if (typeof json.pos != "number") throw new RangeError("Invalid input for AddNodeMarkStep.fromJSON");
616
+ return new AddNodeMarkStep(json.pos, schema.markFromJSON(json.mark));
617
+ }
618
+ }]);
619
+
620
+ return AddNodeMarkStep;
621
+ }(Step);
622
+
623
+ Step.jsonID("addNodeMark", AddNodeMarkStep);
624
+
625
+ var RemoveNodeMarkStep = function (_Step4) {
626
+ _inherits(RemoveNodeMarkStep, _Step4);
627
+
628
+ var _super4 = _createSuper(RemoveNodeMarkStep);
629
+
630
+ function RemoveNodeMarkStep(pos, mark) {
631
+ var _this6;
632
+
633
+ _classCallCheck(this, RemoveNodeMarkStep);
634
+
635
+ _this6 = _super4.call(this);
636
+ _this6.pos = pos;
637
+ _this6.mark = mark;
638
+ return _this6;
639
+ }
640
+
641
+ _createClass(RemoveNodeMarkStep, [{
642
+ key: "apply",
643
+ value: function apply(doc) {
644
+ var node = doc.nodeAt(this.pos);
645
+ if (!node) return StepResult.fail("No node at mark step's position");
646
+ var updated = node.type.create(node.attrs, null, this.mark.removeFromSet(node.marks));
647
+ return StepResult.fromReplace(doc, this.pos, this.pos + 1, new prosemirrorModel.Slice(prosemirrorModel.Fragment.from(updated), 0, node.isLeaf ? 0 : 1));
648
+ }
649
+ }, {
650
+ key: "invert",
651
+ value: function invert(doc) {
652
+ var node = doc.nodeAt(this.pos);
653
+ if (!node || !this.mark.isInSet(node.marks)) return this;
654
+ return new AddNodeMarkStep(this.pos, this.mark);
655
+ }
656
+ }, {
657
+ key: "map",
658
+ value: function map(mapping) {
659
+ var pos = mapping.mapResult(this.pos, 1);
660
+ return pos.deletedAfter ? null : new RemoveNodeMarkStep(pos.pos, this.mark);
661
+ }
662
+ }, {
663
+ key: "toJSON",
664
+ value: function toJSON() {
665
+ return {
666
+ stepType: "removeNodeMark",
667
+ pos: this.pos,
668
+ mark: this.mark.toJSON()
669
+ };
670
+ }
671
+ }], [{
672
+ key: "fromJSON",
673
+ value: function fromJSON(schema, json) {
674
+ if (typeof json.pos != "number") throw new RangeError("Invalid input for RemoveNodeMarkStep.fromJSON");
675
+ return new RemoveNodeMarkStep(json.pos, schema.markFromJSON(json.mark));
676
+ }
677
+ }]);
678
+
679
+ return RemoveNodeMarkStep;
680
+ }(Step);
681
+
682
+ Step.jsonID("removeNodeMark", RemoveNodeMarkStep);
683
+
684
+ var ReplaceStep = function (_Step5) {
685
+ _inherits(ReplaceStep, _Step5);
686
+
687
+ var _super5 = _createSuper(ReplaceStep);
688
+
689
+ function ReplaceStep(from, to, slice) {
690
+ var _this7;
691
+
562
692
  var structure = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false;
563
693
 
564
694
  _classCallCheck(this, ReplaceStep);
565
695
 
566
- _this5 = _super3.call(this);
567
- _this5.from = from;
568
- _this5.to = to;
569
- _this5.slice = slice;
570
- _this5.structure = structure;
571
- return _this5;
696
+ _this7 = _super5.call(this);
697
+ _this7.from = from;
698
+ _this7.to = to;
699
+ _this7.slice = slice;
700
+ _this7.structure = structure;
701
+ return _this7;
572
702
  }
573
703
 
574
704
  _createClass(ReplaceStep, [{
@@ -636,27 +766,27 @@ var ReplaceStep = function (_Step3) {
636
766
 
637
767
  Step.jsonID("replace", ReplaceStep);
638
768
 
639
- var ReplaceAroundStep = function (_Step4) {
640
- _inherits(ReplaceAroundStep, _Step4);
769
+ var ReplaceAroundStep = function (_Step6) {
770
+ _inherits(ReplaceAroundStep, _Step6);
641
771
 
642
- var _super4 = _createSuper(ReplaceAroundStep);
772
+ var _super6 = _createSuper(ReplaceAroundStep);
643
773
 
644
774
  function ReplaceAroundStep(from, to, gapFrom, gapTo, slice, insert) {
645
- var _this6;
775
+ var _this8;
646
776
 
647
777
  var structure = arguments.length > 6 && arguments[6] !== undefined ? arguments[6] : false;
648
778
 
649
779
  _classCallCheck(this, ReplaceAroundStep);
650
780
 
651
- _this6 = _super4.call(this);
652
- _this6.from = from;
653
- _this6.to = to;
654
- _this6.gapFrom = gapFrom;
655
- _this6.gapTo = gapTo;
656
- _this6.slice = slice;
657
- _this6.insert = insert;
658
- _this6.structure = structure;
659
- return _this6;
781
+ _this8 = _super6.call(this);
782
+ _this8.from = from;
783
+ _this8.to = to;
784
+ _this8.gapFrom = gapFrom;
785
+ _this8.gapTo = gapTo;
786
+ _this8.slice = slice;
787
+ _this8.insert = insert;
788
+ _this8.structure = structure;
789
+ return _this8;
660
790
  }
661
791
 
662
792
  _createClass(ReplaceAroundStep, [{
@@ -1211,8 +1341,22 @@ var Fitter = function () {
1211
1341
  }, {
1212
1342
  key: "findFittable",
1213
1343
  value: function findFittable() {
1344
+ var startDepth = this.unplaced.openStart;
1345
+
1346
+ for (var cur = this.unplaced.content, d = 0, openEnd = this.unplaced.openEnd; d < startDepth; d++) {
1347
+ var node = cur.firstChild;
1348
+ if (cur.childCount > 1) openEnd = 0;
1349
+
1350
+ if (node.type.spec.isolating && openEnd <= d) {
1351
+ startDepth = d;
1352
+ break;
1353
+ }
1354
+
1355
+ cur = node.content;
1356
+ }
1357
+
1214
1358
  for (var pass = 1; pass <= 2; pass++) {
1215
- for (var sliceDepth = this.unplaced.openStart; sliceDepth >= 0; sliceDepth--) {
1359
+ for (var sliceDepth = pass == 1 ? startDepth : this.unplaced.openStart; sliceDepth >= 0; sliceDepth--) {
1216
1360
  var fragment = void 0,
1217
1361
  parent = null;
1218
1362
 
@@ -1600,15 +1744,86 @@ function coveredDepths($from, $to) {
1600
1744
  return result;
1601
1745
  }
1602
1746
 
1747
+ var AttrStep = function (_Step7) {
1748
+ _inherits(AttrStep, _Step7);
1749
+
1750
+ var _super7 = _createSuper(AttrStep);
1751
+
1752
+ function AttrStep(pos, attr, value) {
1753
+ var _this9;
1754
+
1755
+ _classCallCheck(this, AttrStep);
1756
+
1757
+ _this9 = _super7.call(this);
1758
+ _this9.pos = pos;
1759
+ _this9.attr = attr;
1760
+ _this9.value = value;
1761
+ return _this9;
1762
+ }
1763
+
1764
+ _createClass(AttrStep, [{
1765
+ key: "apply",
1766
+ value: function apply(doc) {
1767
+ var node = doc.nodeAt(this.pos);
1768
+ if (!node) return StepResult.fail("No node at attribute step's position");
1769
+ var attrs = Object.create(null);
1770
+
1771
+ for (var name in node.attrs) {
1772
+ attrs[name] = node.attrs[name];
1773
+ }
1774
+
1775
+ attrs[this.attr] = this.value;
1776
+ var updated = node.type.create(attrs, null, node.marks);
1777
+ return StepResult.fromReplace(doc, this.pos, this.pos + 1, new prosemirrorModel.Slice(prosemirrorModel.Fragment.from(updated), 0, node.isLeaf ? 0 : 1));
1778
+ }
1779
+ }, {
1780
+ key: "getMap",
1781
+ value: function getMap() {
1782
+ return StepMap.empty;
1783
+ }
1784
+ }, {
1785
+ key: "invert",
1786
+ value: function invert(doc) {
1787
+ return new AttrStep(this.pos, this.attr, doc.nodeAt(this.pos).attrs[this.attr]);
1788
+ }
1789
+ }, {
1790
+ key: "map",
1791
+ value: function map(mapping) {
1792
+ var pos = mapping.mapResult(this.pos, 1);
1793
+ return pos.deletedAfter ? null : new AttrStep(pos.pos, this.attr, this.value);
1794
+ }
1795
+ }, {
1796
+ key: "toJSON",
1797
+ value: function toJSON() {
1798
+ return {
1799
+ stepType: "attr",
1800
+ pos: this.pos,
1801
+ attr: this.attr,
1802
+ value: this.value
1803
+ };
1804
+ }
1805
+ }], [{
1806
+ key: "fromJSON",
1807
+ value: function fromJSON(schema, json) {
1808
+ if (typeof json.pos != "number" || typeof json.attr != "string") throw new RangeError("Invalid input for AttrStep.fromJSON");
1809
+ return new AttrStep(json.pos, json.attr, json.value);
1810
+ }
1811
+ }]);
1812
+
1813
+ return AttrStep;
1814
+ }(Step);
1815
+
1816
+ Step.jsonID("attr", AttrStep);
1817
+
1603
1818
  exports.TransformError = function (_Error) {
1604
1819
  _inherits(_class, _Error);
1605
1820
 
1606
- var _super5 = _createSuper(_class);
1821
+ var _super8 = _createSuper(_class);
1607
1822
 
1608
1823
  function _class() {
1609
1824
  _classCallCheck(this, _class);
1610
1825
 
1611
- return _super5.apply(this, arguments);
1826
+ return _super8.apply(this, arguments);
1612
1827
  }
1613
1828
 
1614
1829
  return _createClass(_class);
@@ -1749,13 +1964,38 @@ var Transform = function () {
1749
1964
  key: "setNodeMarkup",
1750
1965
  value: function setNodeMarkup(pos, type) {
1751
1966
  var attrs = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null;
1752
- var marks = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : [];
1967
+ var marks = arguments.length > 3 ? arguments[3] : undefined;
1753
1968
 
1754
1969
  _setNodeMarkup(this, pos, type, attrs, marks);
1755
1970
 
1756
1971
  return this;
1757
1972
  }
1758
1973
  }, {
1974
+ key: "setNodeAttribute",
1975
+ value: function setNodeAttribute(pos, attr, value) {
1976
+ this.step(new AttrStep(pos, attr, value));
1977
+ return this;
1978
+ }
1979
+ }, {
1980
+ key: "addNodeMark",
1981
+ value: function addNodeMark(pos, mark) {
1982
+ this.step(new AddNodeMarkStep(pos, mark));
1983
+ return this;
1984
+ }
1985
+ }, {
1986
+ key: "removeNodeMark",
1987
+ value: function removeNodeMark(pos, mark) {
1988
+ if (!(mark instanceof prosemirrorModel.Mark)) {
1989
+ var node = this.doc.nodeAt(pos);
1990
+ if (!node) throw new RangeError("No node at position " + pos);
1991
+ mark = mark.isInSet(node.marks);
1992
+ if (!mark) return this;
1993
+ }
1994
+
1995
+ this.step(new RemoveNodeMarkStep(pos, mark));
1996
+ return this;
1997
+ }
1998
+ }, {
1759
1999
  key: "split",
1760
2000
  value: function split(pos) {
1761
2001
  var depth = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1;
@@ -1792,9 +2032,12 @@ var Transform = function () {
1792
2032
  }();
1793
2033
 
1794
2034
  exports.AddMarkStep = AddMarkStep;
2035
+ exports.AddNodeMarkStep = AddNodeMarkStep;
2036
+ exports.AttrStep = AttrStep;
1795
2037
  exports.MapResult = MapResult;
1796
2038
  exports.Mapping = Mapping;
1797
2039
  exports.RemoveMarkStep = RemoveMarkStep;
2040
+ exports.RemoveNodeMarkStep = RemoveNodeMarkStep;
1798
2041
  exports.ReplaceAroundStep = ReplaceAroundStep;
1799
2042
  exports.ReplaceStep = ReplaceStep;
1800
2043
  exports.Step = Step;
package/dist/index.d.ts CHANGED
@@ -413,6 +413,19 @@ declare class Transform {
413
413
  */
414
414
  setNodeMarkup(pos: number, type?: NodeType | null, attrs?: Attrs | null, marks?: readonly Mark[]): this;
415
415
  /**
416
+ Set a single attribute on a given node to a new value.
417
+ */
418
+ setNodeAttribute(pos: number, attr: string, value: any): this;
419
+ /**
420
+ Add a mark to the node at position `pos`.
421
+ */
422
+ addNodeMark(pos: number, mark: Mark): this;
423
+ /**
424
+ Remove a mark (or a mark of the given type) from the node at
425
+ position `pos`.
426
+ */
427
+ removeNodeMark(pos: number, mark: Mark | MarkType): this;
428
+ /**
416
429
  Split the node at the given position, and optionally, if `depth` is
417
430
  greater than one, any number of nodes above that. By default, the
418
431
  parts split off will inherit the node type of the original node.
@@ -570,6 +583,64 @@ declare class RemoveMarkStep extends Step {
570
583
  merge(other: Step): Step | null;
571
584
  toJSON(): any;
572
585
  }
586
+ /**
587
+ Add a mark to a specific node.
588
+ */
589
+ declare class AddNodeMarkStep extends Step {
590
+ /**
591
+ The position of the target node.
592
+ */
593
+ readonly pos: number;
594
+ /**
595
+ The mark to add.
596
+ */
597
+ readonly mark: Mark;
598
+ /**
599
+ Create a node mark step.
600
+ */
601
+ constructor(
602
+ /**
603
+ The position of the target node.
604
+ */
605
+ pos: number,
606
+ /**
607
+ The mark to add.
608
+ */
609
+ mark: Mark);
610
+ apply(doc: Node): StepResult;
611
+ invert(doc: Node): Step;
612
+ map(mapping: Mappable): Step | null;
613
+ toJSON(): any;
614
+ }
615
+ /**
616
+ Remove a mark from a specific node.
617
+ */
618
+ declare class RemoveNodeMarkStep extends Step {
619
+ /**
620
+ The position of the target node.
621
+ */
622
+ readonly pos: number;
623
+ /**
624
+ The mark to remove.
625
+ */
626
+ readonly mark: Mark;
627
+ /**
628
+ Create a mark-removing step.
629
+ */
630
+ constructor(
631
+ /**
632
+ The position of the target node.
633
+ */
634
+ pos: number,
635
+ /**
636
+ The mark to remove.
637
+ */
638
+ mark: Mark);
639
+ apply(doc: Node): StepResult;
640
+ invert(doc: Node): Step;
641
+ map(mapping: Mappable): Step | null;
642
+ toJSON(): any;
643
+ }
573
644
 
574
645
  /**
575
646
  Replace a part of the document with a slice of new content.
@@ -694,6 +765,39 @@ declare class ReplaceAroundStep extends Step {
694
765
  toJSON(): any;
695
766
  }
696
767
 
768
+ /**
769
+ Update an attribute in a specific node.
770
+ */
771
+ declare class AttrStep extends Step {
772
+ /**
773
+ The position of the target node.
774
+ */
775
+ readonly pos: number;
776
+ /**
777
+ The attribute to set.
778
+ */
779
+ readonly attr: string;
780
+ readonly value: any;
781
+ /**
782
+ Construct an attribute step.
783
+ */
784
+ constructor(
785
+ /**
786
+ The position of the target node.
787
+ */
788
+ pos: number,
789
+ /**
790
+ The attribute to set.
791
+ */
792
+ attr: string, value: any);
793
+ apply(doc: Node): StepResult;
794
+ getMap(): StepMap;
795
+ invert(doc: Node): AttrStep;
796
+ map(mapping: Mappable): AttrStep | null;
797
+ toJSON(): any;
798
+ static fromJSON(schema: Schema, json: any): AttrStep;
799
+ }
800
+
697
801
  /**
698
802
  ‘Fit’ a slice into a given position in the document, producing a
699
803
  [step](https://prosemirror.net/docs/ref/#transform.Step) that inserts it. Will return null if
@@ -702,4 +806,4 @@ would be a no-op (an empty slice over an empty range).
702
806
  */
703
807
  declare function replaceStep(doc: Node, from: number, to?: number, slice?: Slice): Step | null;
704
808
 
705
- export { AddMarkStep, MapResult, Mappable, Mapping, RemoveMarkStep, ReplaceAroundStep, ReplaceStep, Step, StepMap, StepResult, Transform, canJoin, canSplit, dropPoint, findWrapping, insertPoint, joinPoint, liftTarget, replaceStep };
809
+ export { AddMarkStep, AddNodeMarkStep, AttrStep, MapResult, Mappable, Mapping, RemoveMarkStep, RemoveNodeMarkStep, ReplaceAroundStep, ReplaceStep, Step, StepMap, StepResult, Transform, canJoin, canSplit, dropPoint, findWrapping, insertPoint, joinPoint, liftTarget, replaceStep };