diff --git a/.changeset/sour-lions-juggle.md b/.changeset/sour-lions-juggle.md new file mode 100644 index 000000000..88cc197d8 --- /dev/null +++ b/.changeset/sour-lions-juggle.md @@ -0,0 +1,5 @@ +--- +'slate-react': patch +--- + +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 diff --git a/packages/slate-react/src/hooks/android-input-manager/android-input-manager.ts b/packages/slate-react/src/hooks/android-input-manager/android-input-manager.ts index 90a324489..5a9cbf324 100644 --- a/packages/slate-react/src/hooks/android-input-manager/android-input-manager.ts +++ b/packages/slate-react/src/hooks/android-input-manager/android-input-manager.ts @@ -396,25 +396,32 @@ export function createAndroidInputManager({ let canStoreDiff = true if (type.startsWith('delete')) { - if (Range.isExpanded(targetRange)) { - const [start, end] = Range.edges(targetRange) - const leaf = Node.leaf(editor, start.path) + const direction = type.endsWith('Backward') ? 'backward' : 'forward' + let [start, end] = Range.edges(targetRange) + let [leaf, path] = Editor.leaf(editor, start.path) + if (Range.isExpanded(targetRange)) { if (leaf.text.length === start.offset && end.offset === 0) { const next = Editor.next(editor, { at: start.path, match: Text.isText, }) if (next && Path.equals(next[1], end.path)) { - targetRange = { anchor: end, focus: end } + // when deleting a linebreak, targetRange will span across the break (ie start in the node before and end in the node after) + // 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 + // so to avoid this we collapse the target range to default to single character deletion + if (direction === 'backward') { + targetRange = { anchor: end, focus: end } + start = end + ;[leaf, path] = next + } else { + targetRange = { anchor: start, focus: start } + end = start + } } } } - const direction = type.endsWith('Backward') ? 'backward' : 'forward' - const [start, end] = Range.edges(targetRange) - const [leaf, path] = Editor.leaf(editor, start.path) - const diff = { text: '', start: start.offset,