prosemirror-transform 1.3.2 → 1.4.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/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
  }
@@ -1586,10 +1599,12 @@ Transform.prototype.replaceRange = function(from, to, slice) {
1586
1599
  }
1587
1600
  // Back up if the node directly above openStart, or the node above
1588
1601
  // that separated only by a non-defining textblock node, is defining.
1589
- if (preferredDepth > 0 && leftNodes[preferredDepth - 1].type.spec.defining &&
1602
+ if (preferredDepth > 0 && definesContent(leftNodes[preferredDepth - 1].type) &&
1590
1603
  $from.node(preferredTargetIndex).type != leftNodes[preferredDepth - 1].type)
1591
1604
  { preferredDepth -= 1; }
1592
- else if (preferredDepth >= 2 && leftNodes[preferredDepth - 1].isTextblock && leftNodes[preferredDepth - 2].type.spec.defining &&
1605
+ else if (preferredDepth >= 2 &&
1606
+ leftNodes[preferredDepth - 1].isTextblock &&
1607
+ definesContent(leftNodes[preferredDepth - 2].type) &&
1593
1608
  $from.node(preferredTargetIndex).type != leftNodes[preferredDepth - 2].type)
1594
1609
  { preferredDepth -= 2; }
1595
1610
 
@@ -1681,7 +1696,10 @@ function coveredDepths($from, $to) {
1681
1696
  $to.end(d) > $to.pos + ($to.depth - d) ||
1682
1697
  $from.node(d).type.spec.isolating ||
1683
1698
  $to.node(d).type.spec.isolating) { break }
1684
- if (start == $to.start(d)) { result.push(d); }
1699
+ if (start == $to.start(d) ||
1700
+ (d == $from.depth && d == $to.depth && $from.parent.inlineContent && $to.parent.inlineContent &&
1701
+ d && $to.start(d - 1) == start - 1))
1702
+ { result.push(d); }
1685
1703
  }
1686
1704
  return result
1687
1705
  }