prosemirror-transform 1.3.3 → 1.4.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,21 @@
1
+ ## 1.4.1 (2022-03-31)
2
+
3
+ ### Bug fixes
4
+
5
+ `replaceRange` will now close multiple defining parent nodes when appropriate.
6
+
7
+ ## 1.4.0 (2022-03-21)
8
+
9
+ ### New features
10
+
11
+ Node specs can now use the `definingForContent` and `definingAsContext` properties to opt in to specific parts of the existing `defining` behavior.
12
+
13
+ ## 1.3.4 (2022-02-04)
14
+
15
+ ### Bug fixes
16
+
17
+ Make sure that constructing an empty `StepMap` returns `StepMap.empty`.
18
+
1
19
  ## 1.3.3 (2021-09-29)
2
20
 
3
21
  ### Bug fixes
package/dist/index.es.js CHANGED
@@ -57,6 +57,7 @@ var MapResult = function MapResult(pos, deleted, recover) {
57
57
  var StepMap = function StepMap(ranges, inverted) {
58
58
  if ( inverted === void 0 ) inverted = false;
59
59
 
60
+ if (!ranges.length && StepMap.empty) { return StepMap.empty }
60
61
  this.ranges = ranges;
61
62
  this.inverted = inverted;
62
63
  };
@@ -141,6 +142,8 @@ StepMap.offset = function offset (n) {
141
142
  return n == 0 ? StepMap.empty : new StepMap(n < 0 ? [0, -n, 0] : [0, 0, n])
142
143
  };
143
144
 
145
+ // :: StepMap
146
+ // A StepMap that contains no changed ranges.
144
147
  StepMap.empty = new StepMap([]);
145
148
 
146
149
  // :: class extends Mappable
@@ -720,8 +723,14 @@ function findWrappingInside(range, type) {
720
723
  // probably be computed with [`findWrapping`](#transform.findWrapping).
721
724
  Transform.prototype.wrap = function(range, wrappers) {
722
725
  var content = Fragment.empty;
723
- for (var i = wrappers.length - 1; i >= 0; i--)
724
- { content = Fragment.from(wrappers[i].type.create(wrappers[i].attrs, content)); }
726
+ for (var i = wrappers.length - 1; i >= 0; i--) {
727
+ if (content.size) {
728
+ var match = wrappers[i].type.contentMatch.matchFragment(content);
729
+ if (!match || !match.validEnd)
730
+ { throw new RangeError("Wrapper type given to Transform.wrap does not form valid content of its parent wrapper") }
731
+ }
732
+ content = Fragment.from(wrappers[i].type.create(wrappers[i].attrs, content));
733
+ }
725
734
 
726
735
  var start = range.start, end = range.end;
727
736
  return this.step(new ReplaceAroundStep(start, end, start, end, new Slice(content, 0, 0), wrappers.length, true))
@@ -1529,6 +1538,10 @@ function invalidMarks(type, fragment, start) {
1529
1538
  return false
1530
1539
  }
1531
1540
 
1541
+ function definesContent(type) {
1542
+ return type.spec.defining || type.spec.definingForContent
1543
+ }
1544
+
1532
1545
  // :: (number, number, Slice) → this
1533
1546
  // Replace a range of the document with a given slice, using `from`,
1534
1547
  // `to`, and the slice's [`openStart`](#model.Slice.openStart) property
@@ -1536,9 +1549,9 @@ function invalidMarks(type, fragment, start) {
1536
1549
  // grow the replaced area or close open nodes in the slice in order to
1537
1550
  // get a fit that is more in line with WYSIWYG expectations, by
1538
1551
  // dropping fully covered parent nodes of the replaced region when
1539
- // they are marked [non-defining](#model.NodeSpec.defining), or
1552
+ // they are marked [non-defining as context](#model.NodeSpec.definingAsContext), or
1540
1553
  // including an open parent node from the slice that _is_ marked as
1541
- // [defining](#model.NodeSpec.defining).
1554
+ // [defining its content](#model.NodeSpec.definingForContent).
1542
1555
  //
1543
1556
  // This is the method, for example, to handle paste. The similar
1544
1557
  // [`replace`](#transform.Transform.replace) method is a more
@@ -1565,7 +1578,7 @@ Transform.prototype.replaceRange = function(from, to, slice) {
1565
1578
  // cross a defining node.
1566
1579
  for (var d = $from.depth, pos = $from.pos - 1; d > 0; d--, pos--) {
1567
1580
  var spec = $from.node(d).type.spec;
1568
- if (spec.defining || spec.isolating) { break }
1581
+ if (spec.defining || spec.definingAsContext || spec.isolating) { break }
1569
1582
  if (targetDepths.indexOf(d) > -1) { preferredTarget = d; }
1570
1583
  else if ($from.before(d) == pos) { targetDepths.splice(1, 0, -d); }
1571
1584
  }
@@ -1580,14 +1593,14 @@ Transform.prototype.replaceRange = function(from, to, slice) {
1580
1593
  if (i == slice.openStart) { break }
1581
1594
  content = node.content;
1582
1595
  }
1583
- // Back up if the node directly above openStart, or the node above
1584
- // that separated only by a non-defining textblock node, is defining.
1585
- if (preferredDepth > 0 && leftNodes[preferredDepth - 1].type.spec.defining &&
1586
- $from.node(preferredTargetIndex).type != leftNodes[preferredDepth - 1].type)
1587
- { preferredDepth -= 1; }
1588
- else if (preferredDepth >= 2 && leftNodes[preferredDepth - 1].isTextblock && leftNodes[preferredDepth - 2].type.spec.defining &&
1589
- $from.node(preferredTargetIndex).type != leftNodes[preferredDepth - 2].type)
1590
- { preferredDepth -= 2; }
1596
+
1597
+ // Back up preferredDepth to cover defining textblocks directly
1598
+ // above it, possibly skipping a non-defining textblock.
1599
+ for (var d$1 = preferredDepth - 1; d$1 >= 0; d$1--) {
1600
+ var type = leftNodes[d$1].type, def = definesContent(type);
1601
+ if (def && $from.node(preferredTargetIndex).type != type) { preferredDepth = d$1; }
1602
+ else if (def || !type.isTextblock) { break }
1603
+ }
1591
1604
 
1592
1605
  for (var j = slice.openStart; j >= 0; j--) {
1593
1606
  var openDepth = (j + preferredDepth + 1) % (slice.openStart + 1);