Skip to content

Commit 06b21fd

Browse files
authored
Fix android linebreak deletion bug (#5908)
* removed unnecessary edge case deletion logic on android * woops looks like that edge case was necessary after all. readding it with extra logic to handle forward deletion * changeset and lint
1 parent 94fb04a commit 06b21fd

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

.changeset/sour-lions-juggle.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'slate-react': patch
3+
---
4+
5+
Fixed issue on android where deleting forward at the end of a block would delete the first character in the next block instead of the linebreak

packages/slate-react/src/hooks/android-input-manager/android-input-manager.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -396,25 +396,32 @@ export function createAndroidInputManager({
396396
let canStoreDiff = true
397397

398398
if (type.startsWith('delete')) {
399-
if (Range.isExpanded(targetRange)) {
400-
const [start, end] = Range.edges(targetRange)
401-
const leaf = Node.leaf(editor, start.path)
399+
const direction = type.endsWith('Backward') ? 'backward' : 'forward'
400+
let [start, end] = Range.edges(targetRange)
401+
let [leaf, path] = Editor.leaf(editor, start.path)
402402

403+
if (Range.isExpanded(targetRange)) {
403404
if (leaf.text.length === start.offset && end.offset === 0) {
404405
const next = Editor.next(editor, {
405406
at: start.path,
406407
match: Text.isText,
407408
})
408409
if (next && Path.equals(next[1], end.path)) {
409-
targetRange = { anchor: end, focus: end }
410+
// when deleting a linebreak, targetRange will span across the break (ie start in the node before and end in the node after)
411+
// if the node before is empty, this will look like a hanging range and get unhung later--which will take the break we want to remove out of the range
412+
// so to avoid this we collapse the target range to default to single character deletion
413+
if (direction === 'backward') {
414+
targetRange = { anchor: end, focus: end }
415+
start = end
416+
;[leaf, path] = next
417+
} else {
418+
targetRange = { anchor: start, focus: start }
419+
end = start
420+
}
410421
}
411422
}
412423
}
413424

414-
const direction = type.endsWith('Backward') ? 'backward' : 'forward'
415-
const [start, end] = Range.edges(targetRange)
416-
const [leaf, path] = Editor.leaf(editor, start.path)
417-
418425
const diff = {
419426
text: '',
420427
start: start.offset,

0 commit comments

Comments
 (0)