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

fix backward selection always changed to forward (#1354)

This commit is contained in:
Irwan Fario Subastian
2017-11-01 12:02:22 +11:00
committed by Ian Storm Taylor
parent 411e1667d6
commit ac8df1cfff

View File

@@ -131,6 +131,7 @@ class Content extends React.Component {
const { editor } = this.props const { editor } = this.props
const { value } = editor const { value } = editor
const { selection } = value const { selection } = value
const { isBackward } = selection
const window = getWindow(this.element) const window = getWindow(this.element)
const native = window.getSelection() const native = window.getSelection()
const { rangeCount } = native const { rangeCount } = native
@@ -161,20 +162,33 @@ class Content extends React.Component {
} }
// If the new range matches the current selection, do nothing. // If the new range matches the current selection, do nothing.
if ( if (current) {
current && if (
range.startContainer == current.startContainer && range.startContainer == current.startContainer &&
range.startOffset == current.startOffset && range.startOffset == current.startOffset &&
range.endContainer == current.endContainer && range.endContainer == current.endContainer &&
range.endOffset == current.endOffset range.endOffset == current.endOffset
) { ) {
return return
}
if (
range.startContainer == current.endContainer &&
range.startOffset == current.endOffset &&
range.endContainer == current.startContainer &&
range.endOffset == current.startOffset
) {
return
}
} }
// Otherwise, set the `isUpdatingSelection` flag and update the selection. // Otherwise, set the `isUpdatingSelection` flag and update the selection.
this.tmp.isUpdatingSelection = true this.tmp.isUpdatingSelection = true
native.removeAllRanges() native.removeAllRanges()
native.addRange(range) if (isBackward) {
native.setBaseAndExtent(range.endContainer, range.endOffset, range.startContainer, range.startOffset)
} else {
native.setBaseAndExtent(range.startContainer, range.startOffset, range.endContainer, range.endOffset)
}
scrollToSelection(native) scrollToSelection(native)
// Then unset the `isUpdatingSelection` flag after a delay. // Then unset the `isUpdatingSelection` flag after a delay.