diff --git a/src/node.js b/src/node.js index 44175da..2546b55 100644 --- a/src/node.js +++ b/src/node.js @@ -230,7 +230,7 @@ class Node { // In an empty parent, return the empty array if (parent.content.size == 0) return Mark.none // When inside a text node or at the start of the parent node, return the node's marks - if (useAfter || index == 0 || !$pos.atNodeBoundary) return parent.child(index).marks + if (useAfter || index == 0 || $pos.textOffset) return parent.child(index).marks let marks = parent.child(index - 1).marks for (var i = 0; i < marks.length; i++) if (marks[i].type.spec.inclusiveRight === false) diff --git a/src/replace.js b/src/replace.js index 476ffa0..371d2a1 100644 --- a/src/replace.js +++ b/src/replace.js @@ -151,13 +151,13 @@ function addRange($start, $end, depth, target) { startIndex = $start.index(depth) if ($start.depth > depth) { startIndex++ - } else if (!$start.atNodeBoundary) { + } else if ($start.textOffset) { addNode($start.nodeAfter, target) startIndex++ } } for (let i = startIndex; i < endIndex; i++) addNode(node.child(i), target) - if ($end && $end.depth == depth && !$end.atNodeBoundary) + if ($end && $end.depth == depth && $end.textOffset) addNode($end.nodeBefore, target) } diff --git a/src/resolvedpos.js b/src/resolvedpos.js index 1c91863..6efe032 100644 --- a/src/resolvedpos.js +++ b/src/resolvedpos.js @@ -1,3 +1,5 @@ +let warnedAboutBoundary = false + // ::- You'll often have to '[resolve](#model.Node.resolve)' a // position to get the context you need. Objects of this class // represent such a resolved position, providing various pieces of @@ -48,7 +50,7 @@ class ResolvedPos { // given level. indexAfter(depth) { depth = this.resolveDepth(depth) - return this.index(depth) + (depth == this.depth && this.atNodeBoundary ? 0 : 1) + return this.index(depth) + (depth == this.depth && !this.textOffset ? 0 : 1) } // :: (?number) → number @@ -87,10 +89,19 @@ class ResolvedPos { return depth == this.depth + 1 ? this.pos : this.path[depth * 3 - 1] + this.path[depth * 3].nodeSize } - // :: bool - // True if this position points at a node boundary, false if it - // points into a text node. - get atNodeBoundary() { return this.path[this.path.length - 1] == this.pos } + get atNodeBoundary() { + if (!warnedAboutBoundary && typeof console != "undefined") { + warnedAboutBoundary = true + console.warn("ResolvedPos.atNodeBoundary is deprecated. Use textOffset > 0 instead") + } + return !this.textOffset + } + + // :: number + // When this position points into a text node, this returns the + // distance between the position and the start of the text node. + // Will be zero for positions that point between nodes. + get textOffset() { return this.pos - this.path[this.path.length - 1] } // :: ?Node // Get the node directly after the position, if any. If the position