prosemirror-transform 1.7.5 → 1.8.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.8.0 (2023-10-01)
2
+
3
+ ### New features
4
+
5
+ The new `DocAttrStep` can be used to set attributes on the document's top node.
6
+
7
+ `Transform.setDocAttribute` can be used to create a `DocAttrStep` in a transform.
8
+
1
9
  ## 1.7.5 (2023-08-22)
2
10
 
3
11
  ### Bug fixes
package/dist/index.cjs CHANGED
@@ -1823,15 +1823,81 @@ var AttrStep = function (_Step7) {
1823
1823
 
1824
1824
  Step.jsonID("attr", AttrStep);
1825
1825
 
1826
+ var DocAttrStep = function (_Step8) {
1827
+ _inherits(DocAttrStep, _Step8);
1828
+
1829
+ var _super8 = _createSuper(DocAttrStep);
1830
+
1831
+ function DocAttrStep(attr, value) {
1832
+ var _this10;
1833
+
1834
+ _classCallCheck(this, DocAttrStep);
1835
+
1836
+ _this10 = _super8.call(this);
1837
+ _this10.attr = attr;
1838
+ _this10.value = value;
1839
+ return _this10;
1840
+ }
1841
+
1842
+ _createClass(DocAttrStep, [{
1843
+ key: "apply",
1844
+ value: function apply(doc) {
1845
+ var attrs = Object.create(null);
1846
+
1847
+ for (var name in doc.attrs) {
1848
+ attrs[name] = doc.attrs[name];
1849
+ }
1850
+
1851
+ attrs[this.attr] = this.value;
1852
+ var updated = doc.type.create(attrs, doc.content, doc.marks);
1853
+ return StepResult.ok(updated);
1854
+ }
1855
+ }, {
1856
+ key: "getMap",
1857
+ value: function getMap() {
1858
+ return StepMap.empty;
1859
+ }
1860
+ }, {
1861
+ key: "invert",
1862
+ value: function invert(doc) {
1863
+ return new DocAttrStep(this.attr, doc.attrs[this.attr]);
1864
+ }
1865
+ }, {
1866
+ key: "map",
1867
+ value: function map(mapping) {
1868
+ return this;
1869
+ }
1870
+ }, {
1871
+ key: "toJSON",
1872
+ value: function toJSON() {
1873
+ return {
1874
+ stepType: "docAttr",
1875
+ attr: this.attr,
1876
+ value: this.value
1877
+ };
1878
+ }
1879
+ }], [{
1880
+ key: "fromJSON",
1881
+ value: function fromJSON(schema, json) {
1882
+ if (typeof json.attr != "string") throw new RangeError("Invalid input for DocAttrStep.fromJSON");
1883
+ return new DocAttrStep(json.attr, json.value);
1884
+ }
1885
+ }]);
1886
+
1887
+ return DocAttrStep;
1888
+ }(Step);
1889
+
1890
+ Step.jsonID("docAttr", DocAttrStep);
1891
+
1826
1892
  exports.TransformError = function (_Error) {
1827
1893
  _inherits(TransformError, _Error);
1828
1894
 
1829
- var _super8 = _createSuper(TransformError);
1895
+ var _super9 = _createSuper(TransformError);
1830
1896
 
1831
1897
  function TransformError() {
1832
1898
  _classCallCheck(this, TransformError);
1833
1899
 
1834
- return _super8.apply(this, arguments);
1900
+ return _super9.apply(this, arguments);
1835
1901
  }
1836
1902
 
1837
1903
  return _createClass(TransformError);
@@ -1984,6 +2050,12 @@ var Transform = function () {
1984
2050
  this.step(new AttrStep(pos, attr, value));
1985
2051
  return this;
1986
2052
  }
2053
+ }, {
2054
+ key: "setDocAttribute",
2055
+ value: function setDocAttribute(attr, value) {
2056
+ this.step(new DocAttrStep(attr, value));
2057
+ return this;
2058
+ }
1987
2059
  }, {
1988
2060
  key: "addNodeMark",
1989
2061
  value: function addNodeMark(pos, mark) {
@@ -2042,6 +2114,7 @@ var Transform = function () {
2042
2114
  exports.AddMarkStep = AddMarkStep;
2043
2115
  exports.AddNodeMarkStep = AddNodeMarkStep;
2044
2116
  exports.AttrStep = AttrStep;
2117
+ exports.DocAttrStep = DocAttrStep;
2045
2118
  exports.MapResult = MapResult;
2046
2119
  exports.Mapping = Mapping;
2047
2120
  exports.RemoveMarkStep = RemoveMarkStep;
package/dist/index.d.cts CHANGED
@@ -414,9 +414,15 @@ declare class Transform {
414
414
  setNodeMarkup(pos: number, type?: NodeType | null, attrs?: Attrs | null, marks?: readonly Mark[]): this;
415
415
  /**
416
416
  Set a single attribute on a given node to a new value.
417
+ The `pos` addresses the document content. Use `setDocAttribute`
418
+ to set attributes on the document itself.
417
419
  */
418
420
  setNodeAttribute(pos: number, attr: string, value: any): this;
419
421
  /**
422
+ Set a single attribute on the document to a new value.
423
+ */
424
+ setDocAttribute(attr: string, value: any): this;
425
+ /**
420
426
  Add a mark to the node at position `pos`.
421
427
  */
422
428
  addNodeMark(pos: number, mark: Mark): this;
@@ -797,6 +803,30 @@ declare class AttrStep extends Step {
797
803
  toJSON(): any;
798
804
  static fromJSON(schema: Schema, json: any): AttrStep;
799
805
  }
806
+ /**
807
+ Update an attribute in the doc node.
808
+ */
809
+ declare class DocAttrStep extends Step {
810
+ /**
811
+ The attribute to set.
812
+ */
813
+ readonly attr: string;
814
+ readonly value: any;
815
+ /**
816
+ Construct an attribute step.
817
+ */
818
+ constructor(
819
+ /**
820
+ The attribute to set.
821
+ */
822
+ attr: string, value: any);
823
+ apply(doc: Node): StepResult;
824
+ getMap(): StepMap;
825
+ invert(doc: Node): DocAttrStep;
826
+ map(mapping: Mappable): this;
827
+ toJSON(): any;
828
+ static fromJSON(schema: Schema, json: any): DocAttrStep;
829
+ }
800
830
 
801
831
  /**
802
832
  ‘Fit’ a slice into a given position in the document, producing a
@@ -806,4 +836,4 @@ would be a no-op (an empty slice over an empty range).
806
836
  */
807
837
  declare function replaceStep(doc: Node, from: number, to?: number, slice?: Slice): Step | null;
808
838
 
809
- export { AddMarkStep, AddNodeMarkStep, AttrStep, MapResult, Mappable, Mapping, RemoveMarkStep, RemoveNodeMarkStep, ReplaceAroundStep, ReplaceStep, Step, StepMap, StepResult, Transform, canJoin, canSplit, dropPoint, findWrapping, insertPoint, joinPoint, liftTarget, replaceStep };
839
+ export { AddMarkStep, AddNodeMarkStep, AttrStep, DocAttrStep, MapResult, Mappable, Mapping, RemoveMarkStep, RemoveNodeMarkStep, ReplaceAroundStep, ReplaceStep, Step, StepMap, StepResult, Transform, canJoin, canSplit, dropPoint, findWrapping, insertPoint, joinPoint, liftTarget, replaceStep };
package/dist/index.d.ts CHANGED
@@ -414,9 +414,15 @@ declare class Transform {
414
414
  setNodeMarkup(pos: number, type?: NodeType | null, attrs?: Attrs | null, marks?: readonly Mark[]): this;
415
415
  /**
416
416
  Set a single attribute on a given node to a new value.
417
+ The `pos` addresses the document content. Use `setDocAttribute`
418
+ to set attributes on the document itself.
417
419
  */
418
420
  setNodeAttribute(pos: number, attr: string, value: any): this;
419
421
  /**
422
+ Set a single attribute on the document to a new value.
423
+ */
424
+ setDocAttribute(attr: string, value: any): this;
425
+ /**
420
426
  Add a mark to the node at position `pos`.
421
427
  */
422
428
  addNodeMark(pos: number, mark: Mark): this;
@@ -797,6 +803,30 @@ declare class AttrStep extends Step {
797
803
  toJSON(): any;
798
804
  static fromJSON(schema: Schema, json: any): AttrStep;
799
805
  }
806
+ /**
807
+ Update an attribute in the doc node.
808
+ */
809
+ declare class DocAttrStep extends Step {
810
+ /**
811
+ The attribute to set.
812
+ */
813
+ readonly attr: string;
814
+ readonly value: any;
815
+ /**
816
+ Construct an attribute step.
817
+ */
818
+ constructor(
819
+ /**
820
+ The attribute to set.
821
+ */
822
+ attr: string, value: any);
823
+ apply(doc: Node): StepResult;
824
+ getMap(): StepMap;
825
+ invert(doc: Node): DocAttrStep;
826
+ map(mapping: Mappable): this;
827
+ toJSON(): any;
828
+ static fromJSON(schema: Schema, json: any): DocAttrStep;
829
+ }
800
830
 
801
831
  /**
802
832
  ‘Fit’ a slice into a given position in the document, producing a
@@ -806,4 +836,4 @@ would be a no-op (an empty slice over an empty range).
806
836
  */
807
837
  declare function replaceStep(doc: Node, from: number, to?: number, slice?: Slice): Step | null;
808
838
 
809
- export { AddMarkStep, AddNodeMarkStep, AttrStep, MapResult, Mappable, Mapping, RemoveMarkStep, RemoveNodeMarkStep, ReplaceAroundStep, ReplaceStep, Step, StepMap, StepResult, Transform, canJoin, canSplit, dropPoint, findWrapping, insertPoint, joinPoint, liftTarget, replaceStep };
839
+ export { AddMarkStep, AddNodeMarkStep, AttrStep, DocAttrStep, MapResult, Mappable, Mapping, RemoveMarkStep, RemoveNodeMarkStep, ReplaceAroundStep, ReplaceStep, Step, StepMap, StepResult, Transform, canJoin, canSplit, dropPoint, findWrapping, insertPoint, joinPoint, liftTarget, replaceStep };
package/dist/index.js CHANGED
@@ -1746,6 +1746,51 @@ class AttrStep extends Step {
1746
1746
  }
1747
1747
  }
1748
1748
  Step.jsonID("attr", AttrStep);
1749
+ /**
1750
+ Update an attribute in the doc node.
1751
+ */
1752
+ class DocAttrStep extends Step {
1753
+ /**
1754
+ Construct an attribute step.
1755
+ */
1756
+ constructor(
1757
+ /**
1758
+ The attribute to set.
1759
+ */
1760
+ attr,
1761
+ // The attribute's new value.
1762
+ value) {
1763
+ super();
1764
+ this.attr = attr;
1765
+ this.value = value;
1766
+ }
1767
+ apply(doc) {
1768
+ let attrs = Object.create(null);
1769
+ for (let name in doc.attrs)
1770
+ attrs[name] = doc.attrs[name];
1771
+ attrs[this.attr] = this.value;
1772
+ let updated = doc.type.create(attrs, doc.content, doc.marks);
1773
+ return StepResult.ok(updated);
1774
+ }
1775
+ getMap() {
1776
+ return StepMap.empty;
1777
+ }
1778
+ invert(doc) {
1779
+ return new DocAttrStep(this.attr, doc.attrs[this.attr]);
1780
+ }
1781
+ map(mapping) {
1782
+ return this;
1783
+ }
1784
+ toJSON() {
1785
+ return { stepType: "docAttr", attr: this.attr, value: this.value };
1786
+ }
1787
+ static fromJSON(schema, json) {
1788
+ if (typeof json.attr != "string")
1789
+ throw new RangeError("Invalid input for DocAttrStep.fromJSON");
1790
+ return new DocAttrStep(json.attr, json.value);
1791
+ }
1792
+ }
1793
+ Step.jsonID("docAttr", DocAttrStep);
1749
1794
 
1750
1795
  /**
1751
1796
  @internal
@@ -1950,12 +1995,21 @@ class Transform {
1950
1995
  }
1951
1996
  /**
1952
1997
  Set a single attribute on a given node to a new value.
1998
+ The `pos` addresses the document content. Use `setDocAttribute`
1999
+ to set attributes on the document itself.
1953
2000
  */
1954
2001
  setNodeAttribute(pos, attr, value) {
1955
2002
  this.step(new AttrStep(pos, attr, value));
1956
2003
  return this;
1957
2004
  }
1958
2005
  /**
2006
+ Set a single attribute on the document to a new value.
2007
+ */
2008
+ setDocAttribute(attr, value) {
2009
+ this.step(new DocAttrStep(attr, value));
2010
+ return this;
2011
+ }
2012
+ /**
1959
2013
  Add a mark to the node at position `pos`.
1960
2014
  */
1961
2015
  addNodeMark(pos, mark) {
@@ -2018,4 +2072,4 @@ class Transform {
2018
2072
  }
2019
2073
  }
2020
2074
 
2021
- export { AddMarkStep, AddNodeMarkStep, AttrStep, MapResult, Mapping, RemoveMarkStep, RemoveNodeMarkStep, ReplaceAroundStep, ReplaceStep, Step, StepMap, StepResult, Transform, TransformError, canJoin, canSplit, dropPoint, findWrapping, insertPoint, joinPoint, liftTarget, replaceStep };
2075
+ export { AddMarkStep, AddNodeMarkStep, AttrStep, DocAttrStep, 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.7.5",
3
+ "version": "1.8.0",
4
4
  "description": "ProseMirror document transformations",
5
5
  "type": "module",
6
6
  "main": "dist/index.cjs",
package/src/README.md CHANGED
@@ -23,6 +23,7 @@ called a [`Transform`](#transform.Transform).
23
23
  @AddNodeMarkStep
24
24
  @RemoveNodeMarkStep
25
25
  @AttrStep
26
+ @DocAttrStep
26
27
 
27
28
  ### Position Mapping
28
29
 
package/src/attr_step.ts CHANGED
@@ -51,3 +51,48 @@ export class AttrStep extends Step {
51
51
  }
52
52
 
53
53
  Step.jsonID("attr", AttrStep)
54
+
55
+ /// Update an attribute in the doc node.
56
+ export class DocAttrStep extends Step {
57
+ /// Construct an attribute step.
58
+ constructor(
59
+ /// The attribute to set.
60
+ readonly attr: string,
61
+ // The attribute's new value.
62
+ readonly value: any
63
+ ) {
64
+ super()
65
+ }
66
+
67
+ apply(doc: Node) {
68
+ let attrs = Object.create(null)
69
+ for (let name in doc.attrs) attrs[name] = doc.attrs[name]
70
+ attrs[this.attr] = this.value
71
+ let updated = doc.type.create(attrs, doc.content, doc.marks)
72
+ return StepResult.ok(updated)
73
+ }
74
+
75
+ getMap() {
76
+ return StepMap.empty
77
+ }
78
+
79
+ invert(doc: Node) {
80
+ return new DocAttrStep(this.attr, doc.attrs[this.attr])
81
+ }
82
+
83
+ map(mapping: Mappable) {
84
+ return this
85
+ }
86
+
87
+ toJSON(): any {
88
+ return {stepType: "docAttr", attr: this.attr, value: this.value}
89
+ }
90
+
91
+ static fromJSON(schema: Schema, json: any) {
92
+ if (typeof json.attr != "string")
93
+ throw new RangeError("Invalid input for DocAttrStep.fromJSON")
94
+ return new DocAttrStep(json.attr, json.value)
95
+ }
96
+ }
97
+
98
+ Step.jsonID("docAttr", DocAttrStep)
package/src/index.ts CHANGED
@@ -6,6 +6,6 @@ export {joinPoint, canJoin, canSplit, insertPoint, dropPoint, liftTarget, findWr
6
6
  export {StepMap, MapResult, Mapping, Mappable} from "./map"
7
7
  export {AddMarkStep, RemoveMarkStep, AddNodeMarkStep, RemoveNodeMarkStep} from "./mark_step"
8
8
  export {ReplaceStep, ReplaceAroundStep} from "./replace_step"
9
- export {AttrStep} from "./attr_step"
9
+ export {AttrStep, DocAttrStep} from "./attr_step"
10
10
  import "./mark"
11
11
  export {replaceStep} from "./replace"
package/src/transform.ts CHANGED
@@ -5,7 +5,7 @@ import {Step} from "./step"
5
5
  import {addMark, removeMark, clearIncompatible} from "./mark"
6
6
  import {replaceStep, replaceRange, replaceRangeWith, deleteRange} from "./replace"
7
7
  import {lift, wrap, setBlockType, setNodeMarkup, split, join} from "./structure"
8
- import {AttrStep} from "./attr_step"
8
+ import {AttrStep, DocAttrStep} from "./attr_step"
9
9
  import {AddNodeMarkStep, RemoveNodeMarkStep} from "./mark_step"
10
10
 
11
11
  /// @internal
@@ -179,11 +179,19 @@ export class Transform {
179
179
  }
180
180
 
181
181
  /// Set a single attribute on a given node to a new value.
182
+ /// The `pos` addresses the document content. Use `setDocAttribute`
183
+ /// to set attributes on the document itself.
182
184
  setNodeAttribute(pos: number, attr: string, value: any): this {
183
185
  this.step(new AttrStep(pos, attr, value))
184
186
  return this
185
187
  }
186
188
 
189
+ /// Set a single attribute on the document to a new value.
190
+ setDocAttribute(attr: string, value: any): this {
191
+ this.step(new DocAttrStep(attr, value))
192
+ return this
193
+ }
194
+
187
195
  /// Add a mark to the node at position `pos`.
188
196
  addNodeMark(pos: number, mark: Mark): this {
189
197
  this.step(new AddNodeMarkStep(pos, mark))