Skip to content

Commit

Permalink
fix: lost focus after backspace after break
Browse files Browse the repository at this point in the history
  • Loading branch information
demshy committed Aug 7, 2024
1 parent 65b9349 commit 044ca1d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import isHotkey from 'is-hotkey';
import { Editor, Transforms } from 'slate';

import keyDownEnter from './keyDownEnter';
import keyDownBackspace from './keyDownBackspace';
import isCursorInNonDefaultBlock from '../locations/isCursorInNonDefaultBlock';
import toggleBlock from './toggleBlock';
import isCursorCollapsedAfterSoftBreak from '../locations/isCursorCollapsedAfterSoftBreak';

const HEADING_HOTKEYS = {
'mod+1': 'heading-one',
Expand All @@ -25,6 +27,13 @@ function keyDown(event, editor) {
}
}

if (isHotkey('backspace', event) && isCursorCollapsedAfterSoftBreak(editor)) {
const [, path] = Editor.previous(editor);
Transforms.removeNodes(editor, { at: path });
event.preventDefault();
return false;
}

if (!isCursorInNonDefaultBlock(editor)) return;

if (isHotkey('enter', event)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Editor, Range } from 'slate';

function isCursorCollapsedAfterSoftBreak(editor) {
const { selection } = editor;
if (!selection) return false;
if (Range.isExpanded(selection)) return false;

const previous = Editor.previous(editor);

return previous && previous[0].type == 'break';
}

export default isCursorCollapsedAfterSoftBreak;

0 comments on commit 044ca1d

Please sign in to comment.