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/dist/index.js CHANGED
@@ -61,6 +61,7 @@ var MapResult = function MapResult(pos, deleted, recover) {
61
61
  var StepMap = function StepMap(ranges, inverted) {
62
62
  if ( inverted === void 0 ) inverted = false;
63
63
 
64
+ if (!ranges.length && StepMap.empty) { return StepMap.empty }
64
65
  this.ranges = ranges;
65
66
  this.inverted = inverted;
66
67
  };
@@ -145,6 +146,8 @@ StepMap.offset = function offset (n) {
145
146
  return n == 0 ? StepMap.empty : new StepMap(n < 0 ? [0, -n, 0] : [0, 0, n])
146
147
  };
147
148
 
149
+ // :: StepMap
150
+ // A StepMap that contains no changed ranges.
148
151
  StepMap.empty = new StepMap([]);
149
152
 
150
153
  // :: class extends Mappable
@@ -724,8 +727,14 @@ function findWrappingInside(range, type) {
724
727
  // probably be computed with [`findWrapping`](#transform.findWrapping).
725
728
  Transform.prototype.wrap = function(range, wrappers) {
726
729
  var content = prosemirrorModel.Fragment.empty;
727
- for (var i = wrappers.length - 1; i >= 0; i--)
728
- { content = prosemirrorModel.Fragment.from(wrappers[i].type.create(wrappers[i].attrs, content)); }
730
+ for (var i = wrappers.length - 1; i >= 0; i--) {
731
+ if (content.size) {
732
+ var match = wrappers[i].type.contentMatch.matchFragment(content);
733
+ if (!match || !match.validEnd)
734
+ { throw new RangeError("Wrapper type given to Transform.wrap does not form valid content of its parent wrapper") }
735
+ }
736
+ content = prosemirrorModel.Fragment.from(wrappers[i].type.create(wrappers[i].attrs, content));
737
+ }
729
738
 
730
739
  var start = range.start, end = range.end;
731
740
  return this.step(new ReplaceAroundStep(start, end, start, end, new prosemirrorModel.Slice(content, 0, 0), wrappers.length, true))
@@ -1533,6 +1542,10 @@ function invalidMarks(type, fragment, start) {
1533
1542
  return false
1534
1543
  }
1535
1544
 
1545
+ function definesContent(type) {
1546
+ return type.spec.defining || type.spec.definingForContent
1547
+ }
1548
+
1536
1549
  // :: (number, number, Slice) → this
1537
1550
  // Replace a range of the document with a given slice, using `from`,
1538
1551
  // `to`, and the slice's [`openStart`](#model.Slice.openStart) property
@@ -1540,9 +1553,9 @@ function invalidMarks(type, fragment, start) {
1540
1553
  // grow the replaced area or close open nodes in the slice in order to
1541
1554
  // get a fit that is more in line with WYSIWYG expectations, by
1542
1555
  // dropping fully covered parent nodes of the replaced region when
1543
- // they are marked [non-defining](#model.NodeSpec.defining), or
1556
+ // they are marked [non-defining as context](#model.NodeSpec.definingAsContext), or
1544
1557
  // including an open parent node from the slice that _is_ marked as
1545
- // [defining](#model.NodeSpec.defining).
1558
+ // [defining its content](#model.NodeSpec.definingForContent).
1546
1559
  //
1547
1560
  // This is the method, for example, to handle paste. The similar
1548
1561
  // [`replace`](#transform.Transform.replace) method is a more
@@ -1569,7 +1582,7 @@ Transform.prototype.replaceRange = function(from, to, slice) {
1569
1582
  // cross a defining node.
1570
1583
  for (var d = $from.depth, pos = $from.pos - 1; d > 0; d--, pos--) {
1571
1584
  var spec = $from.node(d).type.spec;
1572
- if (spec.defining || spec.isolating) { break }
1585
+ if (spec.defining || spec.definingAsContext || spec.isolating) { break }
1573
1586
  if (targetDepths.indexOf(d) > -1) { preferredTarget = d; }
1574
1587
  else if ($from.before(d) == pos) { targetDepths.splice(1, 0, -d); }
1575
1588
  }
@@ -1584,14 +1597,14 @@ Transform.prototype.replaceRange = function(from, to, slice) {
1584
1597
  if (i == slice.openStart) { break }
1585
1598
  content = node.content;
1586
1599
  }
1587
- // Back up if the node directly above openStart, or the node above
1588
- // that separated only by a non-defining textblock node, is defining.
1589
- if (preferredDepth > 0 && leftNodes[preferredDepth - 1].type.spec.defining &&
1590
- $from.node(preferredTargetIndex).type != leftNodes[preferredDepth - 1].type)
1591
- { preferredDepth -= 1; }
1592
- else if (preferredDepth >= 2 && leftNodes[preferredDepth - 1].isTextblock && leftNodes[preferredDepth - 2].type.spec.defining &&
1593
- $from.node(preferredTargetIndex).type != leftNodes[preferredDepth - 2].type)
1594
- { preferredDepth -= 2; }
1600
+
1601
+ // Back up preferredDepth to cover defining textblocks directly
1602
+ // above it, possibly skipping a non-defining textblock.
1603
+ for (var d$1 = preferredDepth - 1; d$1 >= 0; d$1--) {
1604
+ var type = leftNodes[d$1].type, def = definesContent(type);
1605
+ if (def && $from.node(preferredTargetIndex).type != type) { preferredDepth = d$1; }
1606
+ else if (def || !type.isTextblock) { break }
1607
+ }
1595
1608
 
1596
1609
  for (var j = slice.openStart; j >= 0; j--) {
1597
1610
  var openDepth = (j + preferredDepth + 1) % (slice.openStart + 1);