prosemirror-transform 1.7.4 → 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,17 @@
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
+
9
+ ## 1.7.5 (2023-08-22)
10
+
11
+ ### Bug fixes
12
+
13
+ Fix a failure in `replaceRange` to drop wrapper nodes when the same wrapper is already present.
14
+
1
15
  ## 1.7.4 (2023-07-28)
2
16
 
3
17
  ### Bug fixes
package/dist/index.cjs CHANGED
@@ -28,10 +28,6 @@ function _defineProperties(target, props) { for (var i = 0; i < props.length; i+
28
28
 
29
29
  function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
30
30
 
31
- Object.defineProperty(exports, '__esModule', {
32
- value: true
33
- });
34
-
35
31
  var prosemirrorModel = require('prosemirror-model');
36
32
 
37
33
  var lower16 = 0xffff;
@@ -1663,9 +1659,9 @@ function _replaceRange(tr, from, to, slice) {
1663
1659
  }
1664
1660
 
1665
1661
  for (var _d3 = preferredDepth - 1; _d3 >= 0; _d3--) {
1666
- var type = leftNodes[_d3].type,
1667
- def = definesContent(type);
1668
- if (def && $from.node(preferredTargetIndex).type != type) preferredDepth = _d3;else if (def || !type.isTextblock) break;
1662
+ var leftNode = leftNodes[_d3],
1663
+ def = definesContent(leftNode.type);
1664
+ if (def && !leftNode.sameMarkup($from.node(Math.abs(preferredTarget) - 1))) preferredDepth = _d3;else if (def || !leftNode.type.isTextblock) break;
1669
1665
  }
1670
1666
 
1671
1667
  for (var j = slice.openStart; j >= 0; j--) {
@@ -1827,18 +1823,84 @@ var AttrStep = function (_Step7) {
1827
1823
 
1828
1824
  Step.jsonID("attr", AttrStep);
1829
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
+
1830
1892
  exports.TransformError = function (_Error) {
1831
- _inherits(_class, _Error);
1893
+ _inherits(TransformError, _Error);
1832
1894
 
1833
- var _super8 = _createSuper(_class);
1895
+ var _super9 = _createSuper(TransformError);
1834
1896
 
1835
- function _class() {
1836
- _classCallCheck(this, _class);
1897
+ function TransformError() {
1898
+ _classCallCheck(this, TransformError);
1837
1899
 
1838
- return _super8.apply(this, arguments);
1900
+ return _super9.apply(this, arguments);
1839
1901
  }
1840
1902
 
1841
- return _createClass(_class);
1903
+ return _createClass(TransformError);
1842
1904
  }(_wrapNativeSuper(Error));
1843
1905
 
1844
1906
  exports.TransformError = function TransformError(message) {
@@ -1988,6 +2050,12 @@ var Transform = function () {
1988
2050
  this.step(new AttrStep(pos, attr, value));
1989
2051
  return this;
1990
2052
  }
2053
+ }, {
2054
+ key: "setDocAttribute",
2055
+ value: function setDocAttribute(attr, value) {
2056
+ this.step(new DocAttrStep(attr, value));
2057
+ return this;
2058
+ }
1991
2059
  }, {
1992
2060
  key: "addNodeMark",
1993
2061
  value: function addNodeMark(pos, mark) {
@@ -2046,6 +2114,7 @@ var Transform = function () {
2046
2114
  exports.AddMarkStep = AddMarkStep;
2047
2115
  exports.AddNodeMarkStep = AddNodeMarkStep;
2048
2116
  exports.AttrStep = AttrStep;
2117
+ exports.DocAttrStep = DocAttrStep;
2049
2118
  exports.MapResult = MapResult;
2050
2119
  exports.Mapping = Mapping;
2051
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
@@ -1601,10 +1601,10 @@ function replaceRange(tr, from, to, slice) {
1601
1601
  // Back up preferredDepth to cover defining textblocks directly
1602
1602
  // above it, possibly skipping a non-defining textblock.
1603
1603
  for (let d = preferredDepth - 1; d >= 0; d--) {
1604
- let type = leftNodes[d].type, def = definesContent(type);
1605
- if (def && $from.node(preferredTargetIndex).type != type)
1604
+ let leftNode = leftNodes[d], def = definesContent(leftNode.type);
1605
+ if (def && !leftNode.sameMarkup($from.node(Math.abs(preferredTarget) - 1)))
1606
1606
  preferredDepth = d;
1607
- else if (def || !type.isTextblock)
1607
+ else if (def || !leftNode.type.isTextblock)
1608
1608
  break;
1609
1609
  }
1610
1610
  for (let j = slice.openStart; j >= 0; j--) {
@@ -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.4",
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/replace.ts CHANGED
@@ -359,7 +359,7 @@ export function replaceRange(tr: Transform, from: number, to: number, slice: Sli
359
359
  // target depth, starting with the preferred depths.
360
360
  let preferredTargetIndex = targetDepths.indexOf(preferredTarget)
361
361
 
362
- let leftNodes = [], preferredDepth = slice.openStart
362
+ let leftNodes: Node[] = [], preferredDepth = slice.openStart
363
363
  for (let content = slice.content, i = 0;; i++) {
364
364
  let node = content.firstChild!
365
365
  leftNodes.push(node)
@@ -370,9 +370,9 @@ export function replaceRange(tr: Transform, from: number, to: number, slice: Sli
370
370
  // Back up preferredDepth to cover defining textblocks directly
371
371
  // above it, possibly skipping a non-defining textblock.
372
372
  for (let d = preferredDepth - 1; d >= 0; d--) {
373
- let type = leftNodes[d].type, def = definesContent(type)
374
- if (def && $from.node(preferredTargetIndex).type != type) preferredDepth = d
375
- else if (def || !type.isTextblock) break
373
+ let leftNode = leftNodes[d], def = definesContent(leftNode.type)
374
+ if (def && !leftNode.sameMarkup($from.node(Math.abs(preferredTarget) - 1))) preferredDepth = d
375
+ else if (def || !leftNode.type.isTextblock) break
376
376
  }
377
377
 
378
378
  for (let j = slice.openStart; j >= 0; j--) {
@@ -443,7 +443,7 @@ export function deleteRange(tr: Transform, from: number, to: number) {
443
443
  // Returns an array of all depths for which $from - $to spans the
444
444
  // whole content of the nodes at that depth.
445
445
  function coveredDepths($from: ResolvedPos, $to: ResolvedPos) {
446
- let result = [], minDepth = Math.min($from.depth, $to.depth)
446
+ let result: number[] = [], minDepth = Math.min($from.depth, $to.depth)
447
447
  for (let d = minDepth; d >= 0; d--) {
448
448
  let start = $from.start(d)
449
449
  if (start < $from.pos - ($from.depth - d) ||
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))