1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-08-22 23:12:52 +02:00

Fix bug with updating a deselected selection and void block selection issues (#1075)

* If the selection has no anchorKey and focusKey (deselected) do nothing

* Fix bug related to selecting void blocks and non-void-blocks with a trailing void-block

* Update content.js

* Update content.js
This commit is contained in:
Per-Kristian Nordnes
2017-09-07 18:18:04 +02:00
committed by Ian Storm Taylor
parent 697a771014
commit 86c69f9d8d

View File

@@ -164,6 +164,9 @@ class Content extends React.Component {
return
}
// If the selection isn't set, do nothing.
if (selection.isUnset) return
// Otherwise, figure out which DOM nodes should be selected...
const { anchorText, focusText } = state
const { anchorKey, anchorOffset, focusKey, focusOffset } = selection
@@ -785,13 +788,29 @@ class Content extends React.Component {
isBackward: null
}
// If the selection is at the end of a non-void inline node, and there is
// a node after it, put it in the node after instead.
const anchorText = document.getNode(anchor.key)
const focusText = document.getNode(focus.key)
const anchorInline = document.getClosestInline(anchor.key)
const focusInline = document.getClosestInline(focus.key)
const focusBlock = document.getClosestBlock(focus.key)
const anchorBlock = document.getClosestBlock(anchor.key)
// When going from a non-void block to the start of a void-block
// the focus is most of the time collpased to the end of the void block.
// This is getting the void-block selected as well when it shouldn't.
// Make sure it is collapsed to the start in those cases.
if (anchorBlock && !anchorBlock.isVoid && focusBlock && focusBlock.isVoid && focus.offset == 1) {
properties.focusOffset = 0
}
// If anchor and focus block is the same void block make sure it is anchored to start
// so we are able to select and delete it
if (anchorBlock && anchorBlock.isVoid && focusBlock && focusBlock.key == anchorBlock.key) {
properties.anchorOffset = 0
}
// If the selection is at the end of a non-void inline node, and there is
// a node after it, put it in the node after instead.
if (anchorInline && !anchorInline.isVoid && anchor.offset == anchorText.text.length) {
const block = document.getClosestBlock(anchor.key)
const next = block.getNextText(anchor.key)