Skip to content

Commit

Permalink
Introduce ResolvedPos.textOffset, deprecate atNodeBoundary
Browse files Browse the repository at this point in the history
FEATURE: Resolved positions have a new getter
[`textOffset`](##model.ResolvedPos.textOffset) to find their position
within a text node (if any).

BREAKING: `ResolvedPos.atNodeBoundary` is deprecated and will be
removed in the next release. Use `textOffset > 0` instead.
  • Loading branch information
marijnh committed Dec 9, 2016
1 parent c5a169e commit e7c4d80
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/node.js
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions src/replace.js
Expand Up @@ -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)
}

Expand Down
21 changes: 16 additions & 5 deletions 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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit e7c4d80

Please sign in to comment.