Skip to content

Commit

Permalink
Fix bug with updating a deselected selection and void block selection…
Browse files Browse the repository at this point in the history
… 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
  • Loading branch information
skogsmaskin authored and ianstormtaylor committed Sep 7, 2017
1 parent 697a771 commit 86c69f9
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions src/components/content.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 86c69f9

Please sign in to comment.