prosemirror-transform 1.9.0 → 1.10.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,9 @@
1
+ ## 1.10.0 (2024-08-13)
2
+
3
+ ### New features
4
+
5
+ `setBlockType` can now take a function that computes attributes for the new nodes, instead of a static attribute object.
6
+
1
7
  ## 1.9.0 (2024-05-06)
2
8
 
3
9
  ### Bug fixes
package/dist/index.cjs CHANGED
@@ -942,7 +942,8 @@ function _setBlockType(tr, from, to, type, attrs) {
942
942
  if (!type.isTextblock) throw new RangeError("Type given to setBlockType should be a textblock");
943
943
  var mapFrom = tr.steps.length;
944
944
  tr.doc.nodesBetween(from, to, function (node, pos) {
945
- if (node.isTextblock && !node.hasMarkup(type, attrs) && canChangeType(tr.doc, tr.mapping.slice(mapFrom).map(pos), type)) {
945
+ var attrsHere = typeof attrs == "function" ? attrs(node) : attrs;
946
+ if (node.isTextblock && !node.hasMarkup(type, attrsHere) && canChangeType(tr.doc, tr.mapping.slice(mapFrom).map(pos), type)) {
946
947
  var convertNewlines = null;
947
948
  if (type.schema.linebreakReplacement) {
948
949
  var pre = type.whitespace == "pre",
@@ -954,7 +955,7 @@ function _setBlockType(tr, from, to, type, attrs) {
954
955
  var mapping = tr.mapping.slice(mapFrom);
955
956
  var startM = mapping.map(pos, 1),
956
957
  endM = mapping.map(pos + node.nodeSize, 1);
957
- tr.step(new ReplaceAroundStep(startM, endM, startM + 1, endM - 1, new prosemirrorModel.Slice(prosemirrorModel.Fragment.from(type.create(attrs, null, node.marks)), 0, 0), 1, true));
958
+ tr.step(new ReplaceAroundStep(startM, endM, startM + 1, endM - 1, new prosemirrorModel.Slice(prosemirrorModel.Fragment.from(type.create(attrsHere, null, node.marks)), 0, 0), 1, true));
958
959
  if (convertNewlines === true) replaceNewlines(tr, node, pos, mapFrom);
959
960
  return false;
960
961
  }
package/dist/index.d.cts CHANGED
@@ -406,7 +406,7 @@ declare class Transform {
406
406
  Set the type of all textblocks (partly) between `from` and `to` to
407
407
  the given node type with the given attributes.
408
408
  */
409
- setBlockType(from: number, to: number | undefined, type: NodeType, attrs?: Attrs | null): this;
409
+ setBlockType(from: number, to: number | undefined, type: NodeType, attrs?: Attrs | null | ((oldNode: Node) => Attrs)): this;
410
410
  /**
411
411
  Change the type, attributes, and/or marks of the node at `pos`.
412
412
  When `type` isn't given, the existing node type is preserved,
package/dist/index.d.ts CHANGED
@@ -406,7 +406,7 @@ declare class Transform {
406
406
  Set the type of all textblocks (partly) between `from` and `to` to
407
407
  the given node type with the given attributes.
408
408
  */
409
- setBlockType(from: number, to: number | undefined, type: NodeType, attrs?: Attrs | null): this;
409
+ setBlockType(from: number, to: number | undefined, type: NodeType, attrs?: Attrs | null | ((oldNode: Node) => Attrs)): this;
410
410
  /**
411
411
  Change the type, attributes, and/or marks of the node at `pos`.
412
412
  When `type` isn't given, the existing node type is preserved,
package/dist/index.js CHANGED
@@ -1075,7 +1075,9 @@ function setBlockType(tr, from, to, type, attrs) {
1075
1075
  throw new RangeError("Type given to setBlockType should be a textblock");
1076
1076
  let mapFrom = tr.steps.length;
1077
1077
  tr.doc.nodesBetween(from, to, (node, pos) => {
1078
- if (node.isTextblock && !node.hasMarkup(type, attrs) && canChangeType(tr.doc, tr.mapping.slice(mapFrom).map(pos), type)) {
1078
+ let attrsHere = typeof attrs == "function" ? attrs(node) : attrs;
1079
+ if (node.isTextblock && !node.hasMarkup(type, attrsHere) &&
1080
+ canChangeType(tr.doc, tr.mapping.slice(mapFrom).map(pos), type)) {
1079
1081
  let convertNewlines = null;
1080
1082
  if (type.schema.linebreakReplacement) {
1081
1083
  let pre = type.whitespace == "pre", supportLinebreak = !!type.contentMatch.matchType(type.schema.linebreakReplacement);
@@ -1090,7 +1092,7 @@ function setBlockType(tr, from, to, type, attrs) {
1090
1092
  clearIncompatible(tr, tr.mapping.slice(mapFrom).map(pos, 1), type, undefined, convertNewlines === null);
1091
1093
  let mapping = tr.mapping.slice(mapFrom);
1092
1094
  let startM = mapping.map(pos, 1), endM = mapping.map(pos + node.nodeSize, 1);
1093
- tr.step(new ReplaceAroundStep(startM, endM, startM + 1, endM - 1, new Slice(Fragment.from(type.create(attrs, null, node.marks)), 0, 0), 1, true));
1095
+ tr.step(new ReplaceAroundStep(startM, endM, startM + 1, endM - 1, new Slice(Fragment.from(type.create(attrsHere, null, node.marks)), 0, 0), 1, true));
1094
1096
  if (convertNewlines === true)
1095
1097
  replaceNewlines(tr, node, pos, mapFrom);
1096
1098
  return false;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "prosemirror-transform",
3
- "version": "1.9.0",
3
+ "version": "1.10.0",
4
4
  "description": "ProseMirror document transformations",
5
5
  "type": "module",
6
6
  "main": "dist/index.cjs",
package/src/structure.ts CHANGED
@@ -112,11 +112,14 @@ export function wrap(tr: Transform, range: NodeRange, wrappers: readonly {type:
112
112
  tr.step(new ReplaceAroundStep(start, end, start, end, new Slice(content, 0, 0), wrappers.length, true))
113
113
  }
114
114
 
115
- export function setBlockType(tr: Transform, from: number, to: number, type: NodeType, attrs: Attrs | null) {
115
+ export function setBlockType(tr: Transform, from: number, to: number,
116
+ type: NodeType, attrs: Attrs | null | ((oldNode: Node) => Attrs)) {
116
117
  if (!type.isTextblock) throw new RangeError("Type given to setBlockType should be a textblock")
117
118
  let mapFrom = tr.steps.length
118
119
  tr.doc.nodesBetween(from, to, (node, pos) => {
119
- if (node.isTextblock && !node.hasMarkup(type, attrs) && canChangeType(tr.doc, tr.mapping.slice(mapFrom).map(pos), type)) {
120
+ let attrsHere = typeof attrs == "function" ? attrs(node) : attrs
121
+ if (node.isTextblock && !node.hasMarkup(type, attrsHere) &&
122
+ canChangeType(tr.doc, tr.mapping.slice(mapFrom).map(pos), type)) {
120
123
  let convertNewlines = null
121
124
  if (type.schema.linebreakReplacement) {
122
125
  let pre = type.whitespace == "pre", supportLinebreak = !!type.contentMatch.matchType(type.schema.linebreakReplacement)
@@ -129,7 +132,7 @@ export function setBlockType(tr: Transform, from: number, to: number, type: Node
129
132
  let mapping = tr.mapping.slice(mapFrom)
130
133
  let startM = mapping.map(pos, 1), endM = mapping.map(pos + node.nodeSize, 1)
131
134
  tr.step(new ReplaceAroundStep(startM, endM, startM + 1, endM - 1,
132
- new Slice(Fragment.from(type.create(attrs, null, node.marks)), 0, 0), 1, true))
135
+ new Slice(Fragment.from(type.create(attrsHere, null, node.marks)), 0, 0), 1, true))
133
136
  if (convertNewlines === true) replaceNewlines(tr, node, pos, mapFrom)
134
137
  return false
135
138
  }
package/src/transform.ts CHANGED
@@ -166,7 +166,7 @@ export class Transform {
166
166
 
167
167
  /// Set the type of all textblocks (partly) between `from` and `to` to
168
168
  /// the given node type with the given attributes.
169
- setBlockType(from: number, to = from, type: NodeType, attrs: Attrs | null = null): this {
169
+ setBlockType(from: number, to = from, type: NodeType, attrs: Attrs | null | ((oldNode: Node) => Attrs) = null): this {
170
170
  setBlockType(this, from, to, type, attrs)
171
171
  return this
172
172
  }