mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-08-17 20:51:20 +02:00
fix backwards selection logic, closes #125
This commit is contained in:
@@ -64,18 +64,18 @@ class Leaf extends React.Component {
|
||||
const { node, start, end } = this.props
|
||||
|
||||
// If neither matches, the selection doesn't start or end here, so exit.
|
||||
const hasStart = selection.hasStartBetween(node, start, end)
|
||||
const hasEnd = selection.hasEndBetween(node, start, end)
|
||||
if (!hasStart && !hasEnd) return
|
||||
const hasAnchor = selection.hasAnchorBetween(node, start, end)
|
||||
const hasFocus = selection.hasFocusBetween(node, start, end)
|
||||
if (!hasAnchor && !hasFocus) return
|
||||
|
||||
// We have a selection to render, so prepare a few things...
|
||||
const native = window.getSelection()
|
||||
const el = ReactDOM.findDOMNode(this).firstChild
|
||||
|
||||
// If both the start and end are here, set the selection all at once.
|
||||
if (hasStart && hasEnd) {
|
||||
if (hasAnchor && hasFocus) {
|
||||
native.removeAllRanges()
|
||||
const range = document.createRange()
|
||||
const range = window.document.createRange()
|
||||
range.setStart(el, anchorOffset - start)
|
||||
native.addRange(range)
|
||||
native.extend(el, focusOffset - start)
|
||||
@@ -86,12 +86,12 @@ class Leaf extends React.Component {
|
||||
// the first leaf to render, reset the selection and set the new start. And
|
||||
// then in the second leaf to render, extend to the new end.
|
||||
if (selection.isForward) {
|
||||
if (hasStart) {
|
||||
if (hasAnchor) {
|
||||
native.removeAllRanges()
|
||||
const range = document.createRange()
|
||||
const range = window.document.createRange()
|
||||
range.setStart(el, anchorOffset - start)
|
||||
native.addRange(range)
|
||||
} else if (hasEnd) {
|
||||
} else if (hasFocus) {
|
||||
native.extend(el, focusOffset - start)
|
||||
}
|
||||
}
|
||||
@@ -101,16 +101,16 @@ class Leaf extends React.Component {
|
||||
// end position. And then in the second leaf to render, set the start and
|
||||
// extend the end to the stored value.
|
||||
else {
|
||||
if (hasEnd) {
|
||||
if (hasFocus) {
|
||||
native.removeAllRanges()
|
||||
const range = document.createRange()
|
||||
const range = window.document.createRange()
|
||||
range.setStart(el, focusOffset - start)
|
||||
native.addRange(range)
|
||||
} else if (hasStart) {
|
||||
} else if (hasAnchor) {
|
||||
const endNode = native.focusNode
|
||||
const endOffset = native.focusOffset
|
||||
native.removeAllRanges()
|
||||
const range = document.createRange()
|
||||
const range = window.document.createRange()
|
||||
range.setStart(el, anchorOffset - start)
|
||||
native.addRange(range)
|
||||
native.extend(endNode, endOffset)
|
||||
|
Reference in New Issue
Block a user