Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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