mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-08-10 17:24:02 +02:00
refactor <Leaf> updateSelection
This commit is contained in:
@@ -140,9 +140,10 @@ class Leaf extends React.Component {
|
|||||||
// If the selection is blurred we have nothing to do.
|
// If the selection is blurred we have nothing to do.
|
||||||
if (selection.isBlurred) return
|
if (selection.isBlurred) return
|
||||||
|
|
||||||
const { anchorOffset, focusOffset } = selection
|
|
||||||
const { node, index } = this.props
|
const { node, index } = this.props
|
||||||
const { start, end } = OffsetKey.findBounds(index, ranges)
|
const { start, end } = OffsetKey.findBounds(index, ranges)
|
||||||
|
const anchorOffset = selection.anchorOffset - start
|
||||||
|
const focusOffset = selection.focusOffset - start
|
||||||
|
|
||||||
// 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 hasAnchor = selection.hasAnchorBetween(node, start, end)
|
const hasAnchor = selection.hasAnchorBetween(node, start, end)
|
||||||
@@ -167,9 +168,9 @@ class Leaf extends React.Component {
|
|||||||
if (hasAnchor && hasFocus) {
|
if (hasAnchor && hasFocus) {
|
||||||
native.removeAllRanges()
|
native.removeAllRanges()
|
||||||
const range = window.document.createRange()
|
const range = window.document.createRange()
|
||||||
range.setStart(el, anchorOffset - start)
|
range.setStart(el, anchorOffset)
|
||||||
native.addRange(range)
|
native.addRange(range)
|
||||||
native.extend(el, focusOffset - start)
|
native.extend(el, focusOffset)
|
||||||
focus()
|
focus()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -182,10 +183,10 @@ class Leaf extends React.Component {
|
|||||||
if (hasAnchor) {
|
if (hasAnchor) {
|
||||||
native.removeAllRanges()
|
native.removeAllRanges()
|
||||||
const range = window.document.createRange()
|
const range = window.document.createRange()
|
||||||
range.setStart(el, anchorOffset - start)
|
range.setStart(el, anchorOffset)
|
||||||
native.addRange(range)
|
native.addRange(range)
|
||||||
} else if (hasFocus) {
|
} else if (hasFocus) {
|
||||||
native.extend(el, focusOffset - start)
|
native.extend(el, focusOffset)
|
||||||
focus()
|
focus()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -198,14 +199,14 @@ class Leaf extends React.Component {
|
|||||||
if (hasFocus) {
|
if (hasFocus) {
|
||||||
native.removeAllRanges()
|
native.removeAllRanges()
|
||||||
const range = window.document.createRange()
|
const range = window.document.createRange()
|
||||||
range.setStart(el, focusOffset - start)
|
range.setStart(el, focusOffset)
|
||||||
native.addRange(range)
|
native.addRange(range)
|
||||||
} else if (hasAnchor) {
|
} 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 = window.document.createRange()
|
const range = window.document.createRange()
|
||||||
range.setStart(el, anchorOffset - start)
|
range.setStart(el, anchorOffset)
|
||||||
native.addRange(range)
|
native.addRange(range)
|
||||||
native.extend(endNode, endOffset)
|
native.extend(endNode, endOffset)
|
||||||
focus()
|
focus()
|
||||||
|
Reference in New Issue
Block a user