prosemirror-transform 1.6.0 → 1.7.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/CHANGELOG.md CHANGED
@@ -1,3 +1,11 @@
1
+ ## 1.7.0 (2022-08-16)
2
+
3
+ ### New features
4
+
5
+ The new `AttrStep` (and `Transform.setNodeAttribute`) can be used to set individual attributes on a node.
6
+
7
+ `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.
8
+
1
9
  ## 1.6.0 (2022-06-01)
2
10
 
3
11
  ### Bug fixes
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, [{
@@ -1600,15 +1730,86 @@ function coveredDepths($from, $to) {
1600
1730
  return result;
1601
1731
  }
1602
1732
 
1733
+ var AttrStep = function (_Step7) {
1734
+ _inherits(AttrStep, _Step7);
1735
+
1736
+ var _super7 = _createSuper(AttrStep);
1737
+
1738
+ function AttrStep(pos, attr, value) {
1739
+ var _this9;
1740
+
1741
+ _classCallCheck(this, AttrStep);
1742
+
1743
+ _this9 = _super7.call(this);
1744
+ _this9.pos = pos;
1745
+ _this9.attr = attr;
1746
+ _this9.value = value;
1747
+ return _this9;
1748
+ }
1749
+
1750
+ _createClass(AttrStep, [{
1751
+ key: "apply",
1752
+ value: function apply(doc) {
1753
+ var node = doc.nodeAt(this.pos);
1754
+ if (!node) return StepResult.fail("No node at attribute step's position");
1755
+ var attrs = Object.create(null);
1756
+
1757
+ for (var name in node.attrs) {
1758
+ attrs[name] = node.attrs[name];
1759
+ }
1760
+
1761
+ attrs[this.attr] = this.value;
1762
+ var updated = node.type.create(attrs, null, node.marks);
1763
+ return StepResult.fromReplace(doc, this.pos, this.pos + 1, new prosemirrorModel.Slice(prosemirrorModel.Fragment.from(updated), 0, node.isLeaf ? 0 : 1));
1764
+ }
1765
+ }, {
1766
+ key: "getMap",
1767
+ value: function getMap() {
1768
+ return StepMap.empty;
1769
+ }
1770
+ }, {
1771
+ key: "invert",
1772
+ value: function invert(doc) {
1773
+ return new AttrStep(this.pos, this.attr, doc.nodeAt(this.pos).attrs[this.attr]);
1774
+ }
1775
+ }, {
1776
+ key: "map",
1777
+ value: function map(mapping) {
1778
+ var pos = mapping.mapResult(this.pos, 1);
1779
+ return pos.deletedAfter ? null : new AttrStep(pos.pos, this.attr, this.value);
1780
+ }
1781
+ }, {
1782
+ key: "toJSON",
1783
+ value: function toJSON() {
1784
+ return {
1785
+ stepType: "attr",
1786
+ pos: this.pos,
1787
+ attr: this.attr,
1788
+ value: this.value
1789
+ };
1790
+ }
1791
+ }], [{
1792
+ key: "fromJSON",
1793
+ value: function fromJSON(schema, json) {
1794
+ if (typeof json.pos != "number" || typeof json.attr != "string") throw new RangeError("Invalid input for AttrStep.fromJSON");
1795
+ return new AttrStep(json.pos, json.attr, json.value);
1796
+ }
1797
+ }]);
1798
+
1799
+ return AttrStep;
1800
+ }(Step);
1801
+
1802
+ Step.jsonID("attr", AttrStep);
1803
+
1603
1804
  exports.TransformError = function (_Error) {
1604
1805
  _inherits(_class, _Error);
1605
1806
 
1606
- var _super5 = _createSuper(_class);
1807
+ var _super8 = _createSuper(_class);
1607
1808
 
1608
1809
  function _class() {
1609
1810
  _classCallCheck(this, _class);
1610
1811
 
1611
- return _super5.apply(this, arguments);
1812
+ return _super8.apply(this, arguments);
1612
1813
  }
1613
1814
 
1614
1815
  return _createClass(_class);
@@ -1756,6 +1957,31 @@ var Transform = function () {
1756
1957
  return this;
1757
1958
  }
1758
1959
  }, {
1960
+ key: "setNodeAttribute",
1961
+ value: function setNodeAttribute(pos, attr, value) {
1962
+ this.step(new AttrStep(pos, attr, value));
1963
+ return this;
1964
+ }
1965
+ }, {
1966
+ key: "addNodeMark",
1967
+ value: function addNodeMark(pos, mark) {
1968
+ this.step(new AddNodeMarkStep(pos, mark));
1969
+ return this;
1970
+ }
1971
+ }, {
1972
+ key: "removeNodeMark",
1973
+ value: function removeNodeMark(pos, mark) {
1974
+ if (!(mark instanceof prosemirrorModel.Mark)) {
1975
+ var node = this.doc.nodeAt(pos);
1976
+ if (!node) throw new RangeError("No node at position " + pos);
1977
+ mark = mark.isInSet(node.marks);
1978
+ if (!mark) return this;
1979
+ }
1980
+
1981
+ this.step(new RemoveNodeMarkStep(pos, mark));
1982
+ return this;
1983
+ }
1984
+ }, {
1759
1985
  key: "split",
1760
1986
  value: function split(pos) {
1761
1987
  var depth = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1;
@@ -1792,9 +2018,12 @@ var Transform = function () {
1792
2018
  }();
1793
2019
 
1794
2020
  exports.AddMarkStep = AddMarkStep;
2021
+ exports.AddNodeMarkStep = AddNodeMarkStep;
2022
+ exports.AttrStep = AttrStep;
1795
2023
  exports.MapResult = MapResult;
1796
2024
  exports.Mapping = Mapping;
1797
2025
  exports.RemoveMarkStep = RemoveMarkStep;
2026
+ exports.RemoveNodeMarkStep = RemoveNodeMarkStep;
1798
2027
  exports.ReplaceAroundStep = ReplaceAroundStep;
1799
2028
  exports.ReplaceStep = ReplaceStep;
1800
2029
  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 };
package/dist/index.js CHANGED
@@ -1,4 +1,4 @@
1
- import { ReplaceError, Slice, Fragment, MarkType } from 'prosemirror-model';
1
+ import { ReplaceError, Slice, Fragment, MarkType, Mark } 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
@@ -555,6 +555,113 @@ class RemoveMarkStep extends Step {
555
555
  }
556
556
  }
557
557
  Step.jsonID("removeMark", RemoveMarkStep);
558
+ /**
559
+ Add a mark to a specific node.
560
+ */
561
+ class AddNodeMarkStep extends Step {
562
+ /**
563
+ Create a node mark step.
564
+ */
565
+ constructor(
566
+ /**
567
+ The position of the target node.
568
+ */
569
+ pos,
570
+ /**
571
+ The mark to add.
572
+ */
573
+ mark) {
574
+ super();
575
+ this.pos = pos;
576
+ this.mark = mark;
577
+ }
578
+ apply(doc) {
579
+ let node = doc.nodeAt(this.pos);
580
+ if (!node)
581
+ return StepResult.fail("No node at mark step's position");
582
+ let updated = node.type.create(node.attrs, null, this.mark.addToSet(node.marks));
583
+ return StepResult.fromReplace(doc, this.pos, this.pos + 1, new Slice(Fragment.from(updated), 0, node.isLeaf ? 0 : 1));
584
+ }
585
+ invert(doc) {
586
+ let node = doc.nodeAt(this.pos);
587
+ if (node) {
588
+ let newSet = this.mark.addToSet(node.marks);
589
+ if (newSet.length == node.marks.length) {
590
+ for (let i = 0; i < node.marks.length; i++)
591
+ if (!node.marks[i].isInSet(newSet))
592
+ return new AddNodeMarkStep(this.pos, node.marks[i]);
593
+ return new AddNodeMarkStep(this.pos, this.mark);
594
+ }
595
+ }
596
+ return new RemoveNodeMarkStep(this.pos, this.mark);
597
+ }
598
+ map(mapping) {
599
+ let pos = mapping.mapResult(this.pos, 1);
600
+ return pos.deletedAfter ? null : new AddNodeMarkStep(pos.pos, this.mark);
601
+ }
602
+ toJSON() {
603
+ return { stepType: "addNodeMark", pos: this.pos, mark: this.mark.toJSON() };
604
+ }
605
+ /**
606
+ @internal
607
+ */
608
+ static fromJSON(schema, json) {
609
+ if (typeof json.pos != "number")
610
+ throw new RangeError("Invalid input for AddNodeMarkStep.fromJSON");
611
+ return new AddNodeMarkStep(json.pos, schema.markFromJSON(json.mark));
612
+ }
613
+ }
614
+ Step.jsonID("addNodeMark", AddNodeMarkStep);
615
+ /**
616
+ Remove a mark from a specific node.
617
+ */
618
+ class RemoveNodeMarkStep extends Step {
619
+ /**
620
+ Create a mark-removing step.
621
+ */
622
+ constructor(
623
+ /**
624
+ The position of the target node.
625
+ */
626
+ pos,
627
+ /**
628
+ The mark to remove.
629
+ */
630
+ mark) {
631
+ super();
632
+ this.pos = pos;
633
+ this.mark = mark;
634
+ }
635
+ apply(doc) {
636
+ let node = doc.nodeAt(this.pos);
637
+ if (!node)
638
+ return StepResult.fail("No node at mark step's position");
639
+ let updated = node.type.create(node.attrs, null, this.mark.removeFromSet(node.marks));
640
+ return StepResult.fromReplace(doc, this.pos, this.pos + 1, new Slice(Fragment.from(updated), 0, node.isLeaf ? 0 : 1));
641
+ }
642
+ invert(doc) {
643
+ let node = doc.nodeAt(this.pos);
644
+ if (!node || !this.mark.isInSet(node.marks))
645
+ return this;
646
+ return new AddNodeMarkStep(this.pos, this.mark);
647
+ }
648
+ map(mapping) {
649
+ let pos = mapping.mapResult(this.pos, 1);
650
+ return pos.deletedAfter ? null : new RemoveNodeMarkStep(pos.pos, this.mark);
651
+ }
652
+ toJSON() {
653
+ return { stepType: "removeNodeMark", pos: this.pos, mark: this.mark.toJSON() };
654
+ }
655
+ /**
656
+ @internal
657
+ */
658
+ static fromJSON(schema, json) {
659
+ if (typeof json.pos != "number")
660
+ throw new RangeError("Invalid input for RemoveNodeMarkStep.fromJSON");
661
+ return new RemoveNodeMarkStep(json.pos, schema.markFromJSON(json.mark));
662
+ }
663
+ }
664
+ Step.jsonID("removeNodeMark", RemoveNodeMarkStep);
558
665
 
559
666
  /**
560
667
  Replace a part of the document with a slice of new content.
@@ -1565,6 +1672,61 @@ function coveredDepths($from, $to) {
1565
1672
  return result;
1566
1673
  }
1567
1674
 
1675
+ /**
1676
+ Update an attribute in a specific node.
1677
+ */
1678
+ class AttrStep extends Step {
1679
+ /**
1680
+ Construct an attribute step.
1681
+ */
1682
+ constructor(
1683
+ /**
1684
+ The position of the target node.
1685
+ */
1686
+ pos,
1687
+ /**
1688
+ The attribute to set.
1689
+ */
1690
+ attr,
1691
+ // The attribute's new value.
1692
+ value) {
1693
+ super();
1694
+ this.pos = pos;
1695
+ this.attr = attr;
1696
+ this.value = value;
1697
+ }
1698
+ apply(doc) {
1699
+ let node = doc.nodeAt(this.pos);
1700
+ if (!node)
1701
+ return StepResult.fail("No node at attribute step's position");
1702
+ let attrs = Object.create(null);
1703
+ for (let name in node.attrs)
1704
+ attrs[name] = node.attrs[name];
1705
+ attrs[this.attr] = this.value;
1706
+ let updated = node.type.create(attrs, null, node.marks);
1707
+ return StepResult.fromReplace(doc, this.pos, this.pos + 1, new Slice(Fragment.from(updated), 0, node.isLeaf ? 0 : 1));
1708
+ }
1709
+ getMap() {
1710
+ return StepMap.empty;
1711
+ }
1712
+ invert(doc) {
1713
+ return new AttrStep(this.pos, this.attr, doc.nodeAt(this.pos).attrs[this.attr]);
1714
+ }
1715
+ map(mapping) {
1716
+ let pos = mapping.mapResult(this.pos, 1);
1717
+ return pos.deletedAfter ? null : new AttrStep(pos.pos, this.attr, this.value);
1718
+ }
1719
+ toJSON() {
1720
+ return { stepType: "attr", pos: this.pos, attr: this.attr, value: this.value };
1721
+ }
1722
+ static fromJSON(schema, json) {
1723
+ if (typeof json.pos != "number" || typeof json.attr != "string")
1724
+ throw new RangeError("Invalid input for AttrStep.fromJSON");
1725
+ return new AttrStep(json.pos, json.attr, json.value);
1726
+ }
1727
+ }
1728
+ Step.jsonID("attr", AttrStep);
1729
+
1568
1730
  /**
1569
1731
  @internal
1570
1732
  */
@@ -1767,6 +1929,36 @@ class Transform {
1767
1929
  return this;
1768
1930
  }
1769
1931
  /**
1932
+ Set a single attribute on a given node to a new value.
1933
+ */
1934
+ setNodeAttribute(pos, attr, value) {
1935
+ this.step(new AttrStep(pos, attr, value));
1936
+ return this;
1937
+ }
1938
+ /**
1939
+ Add a mark to the node at position `pos`.
1940
+ */
1941
+ addNodeMark(pos, mark) {
1942
+ this.step(new AddNodeMarkStep(pos, mark));
1943
+ return this;
1944
+ }
1945
+ /**
1946
+ Remove a mark (or a mark of the given type) from the node at
1947
+ position `pos`.
1948
+ */
1949
+ removeNodeMark(pos, mark) {
1950
+ if (!(mark instanceof Mark)) {
1951
+ let node = this.doc.nodeAt(pos);
1952
+ if (!node)
1953
+ throw new RangeError("No node at position " + pos);
1954
+ mark = mark.isInSet(node.marks);
1955
+ if (!mark)
1956
+ return this;
1957
+ }
1958
+ this.step(new RemoveNodeMarkStep(pos, mark));
1959
+ return this;
1960
+ }
1961
+ /**
1770
1962
  Split the node at the given position, and optionally, if `depth` is
1771
1963
  greater than one, any number of nodes above that. By default, the
1772
1964
  parts split off will inherit the node type of the original node.
@@ -1806,4 +1998,4 @@ class Transform {
1806
1998
  }
1807
1999
  }
1808
2000
 
1809
- export { AddMarkStep, MapResult, Mapping, RemoveMarkStep, ReplaceAroundStep, ReplaceStep, Step, StepMap, StepResult, Transform, TransformError, canJoin, canSplit, dropPoint, findWrapping, insertPoint, joinPoint, liftTarget, replaceStep };
2001
+ export { AddMarkStep, AddNodeMarkStep, AttrStep, MapResult, Mapping, RemoveMarkStep, RemoveNodeMarkStep, ReplaceAroundStep, ReplaceStep, Step, StepMap, StepResult, Transform, TransformError, canJoin, canSplit, dropPoint, findWrapping, insertPoint, joinPoint, liftTarget, replaceStep };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "prosemirror-transform",
3
- "version": "1.6.0",
3
+ "version": "1.7.0",
4
4
  "description": "ProseMirror document transformations",
5
5
  "type": "module",
6
6
  "main": "dist/index.cjs",