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 deleting backwards when there are only inlines #2850

Closed
wants to merge 2 commits into from
Closed
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
39 changes: 27 additions & 12 deletions packages/slate/src/commands/at-range.js
Original file line number Diff line number Diff line change
Expand Up @@ -375,23 +375,38 @@ Commands.deleteBackwardAtRange = (editor, range, n = 1) => {

// Otherwise, we need to see how many nodes backwards to go.
let node = text
let offset = 0
let traversed = focus.offset
const prevSibling = document.getPreviousSibling(node.key)

while (n > traversed) {
node = document.getPreviousText(node.key)
const next = traversed + node.text.length
// If the next sibling is an inline, has no text and the focus offset is 0,
// then delete the inline
if (
traversed === 0 &&
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would you want to refactor this compound conditional to its own method? something like 'shouldDeleteInline'

Inline.isInline(prevSibling) &&
prevSibling.text === ''
) {
editor.removeNodeByKey(prevSibling.key)
} else {
let offset = 0
let prevNode = document.getPreviousText(node.key)

if (n <= next) {
offset = next - n
break
} else {
traversed = next
while (prevNode && n > traversed) {
node = prevNode
const next = traversed + node.text.length

if (n <= next) {
offset = next - n
break
} else {
traversed = next
}

prevNode = document.getPreviousText(node.key)
}
}

range = range.moveAnchorTo(node.key, offset)
editor.deleteAtRange(range)
range = range.moveAnchorTo(node.key, offset)
editor.deleteAtRange(range)
}
}

/**
Expand Down