1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-08-18 13:11:17 +02:00

fix backwards selection logic, closes #125

This commit is contained in:
Ian Storm Taylor
2016-07-19 09:37:09 -07:00
parent c8aaa2d260
commit f7905d754f

View File

@@ -64,18 +64,18 @@ class Leaf extends React.Component {
const { node, start, end } = this.props const { node, start, end } = this.props
// If neither matches, the selection doesn't start or end here, so exit. // If neither matches, the selection doesn't start or end here, so exit.
const hasStart = selection.hasStartBetween(node, start, end) const hasAnchor = selection.hasAnchorBetween(node, start, end)
const hasEnd = selection.hasEndBetween(node, start, end) const hasFocus = selection.hasFocusBetween(node, start, end)
if (!hasStart && !hasEnd) return if (!hasAnchor && !hasFocus) return
// We have a selection to render, so prepare a few things... // We have a selection to render, so prepare a few things...
const native = window.getSelection() const native = window.getSelection()
const el = ReactDOM.findDOMNode(this).firstChild const el = ReactDOM.findDOMNode(this).firstChild
// If both the start and end are here, set the selection all at once. // If both the start and end are here, set the selection all at once.
if (hasStart && hasEnd) { if (hasAnchor && hasFocus) {
native.removeAllRanges() native.removeAllRanges()
const range = document.createRange() const range = window.document.createRange()
range.setStart(el, anchorOffset - start) range.setStart(el, anchorOffset - start)
native.addRange(range) native.addRange(range)
native.extend(el, focusOffset - start) 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 // 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. // then in the second leaf to render, extend to the new end.
if (selection.isForward) { if (selection.isForward) {
if (hasStart) { if (hasAnchor) {
native.removeAllRanges() native.removeAllRanges()
const range = document.createRange() const range = window.document.createRange()
range.setStart(el, anchorOffset - start) range.setStart(el, anchorOffset - start)
native.addRange(range) native.addRange(range)
} else if (hasEnd) { } else if (hasFocus) {
native.extend(el, focusOffset - start) 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 // end position. And then in the second leaf to render, set the start and
// extend the end to the stored value. // extend the end to the stored value.
else { else {
if (hasEnd) { if (hasFocus) {
native.removeAllRanges() native.removeAllRanges()
const range = document.createRange() const range = window.document.createRange()
range.setStart(el, focusOffset - start) range.setStart(el, focusOffset - start)
native.addRange(range) native.addRange(range)
} else if (hasStart) { } else if (hasAnchor) {
const endNode = native.focusNode const endNode = native.focusNode
const endOffset = native.focusOffset const endOffset = native.focusOffset
native.removeAllRanges() native.removeAllRanges()
const range = document.createRange() const range = window.document.createRange()
range.setStart(el, anchorOffset - start) range.setStart(el, anchorOffset - start)
native.addRange(range) native.addRange(range)
native.extend(endNode, endOffset) native.extend(endNode, endOffset)