1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-08-22 06:53:25 +02:00

Fix bugs in getRelativeRange. (#2872)

This commit is contained in:
themithy
2019-07-03 22:03:26 +02:00
committed by Ian Storm Taylor
parent a0b2aa3d4f
commit 9e2e2a1cc5

View File

@@ -288,11 +288,11 @@ function getRelativeRange(node, index, range) {
start = start.setPath(startPath.rest()) start = start.setPath(startPath.rest())
} else if (startIndex < index && index <= endIndex) { } else if (startIndex < index && index <= endIndex) {
if (child.object === 'text') { if (child.object === 'text') {
start = start.moveTo(PathUtils.create([index]), 0) start = start.moveTo(PathUtils.create([index]), 0).setKey(child.key)
} else { } else {
const [first] = child.texts() const [first] = child.texts()
const [, firstPath] = first const [firstNode, firstPath] = first
start = start.moveTo(firstPath, 0) start = start.moveTo(firstPath, 0).setKey(firstNode.key)
} }
} else { } else {
start = null start = null
@@ -302,11 +302,12 @@ function getRelativeRange(node, index, range) {
end = end.setPath(endPath.rest()) end = end.setPath(endPath.rest())
} else if (startIndex <= index && index < endIndex) { } else if (startIndex <= index && index < endIndex) {
if (child.object === 'text') { if (child.object === 'text') {
end = end.moveTo(PathUtils.create([index]), child.text.length) const length = child.text.length
end = end.moveTo(PathUtils.create([index]), length).setKey(child.key)
} else { } else {
const [last] = child.texts({ direction: 'backward' }) const [last] = child.texts({ direction: 'backward' })
const [lastNode, lastPath] = last const [lastNode, lastPath] = last
end = end.moveTo(lastPath, lastNode.text.length) end = end.moveTo(lastPath, lastNode.text.length).setKey(lastNode.key)
} }
} else { } else {
end = null end = null
@@ -316,8 +317,8 @@ function getRelativeRange(node, index, range) {
return null return null
} }
range = range.setStart(start) range = range.setAnchor(start)
range = range.setEnd(end) range = range.setFocus(end)
return range return range
} }