Skip to content

Commit

Permalink
fix backward delete with previous empty line (#1469)
Browse files Browse the repository at this point in the history
  • Loading branch information
ajbura authored Oct 21, 2023
1 parent d5ff55e commit 9200e22
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions src/app/components/editor/keyboard.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { isKeyHotkey } from 'is-hotkey';
import { KeyboardEvent } from 'react';
import { Editor, Range } from 'slate';
import { Editor, Element as SlateElement, Range, Transforms } from 'slate';
import { isAnyMarkActive, isBlockActive, removeAllMark, toggleBlock, toggleMark } from './utils';
import { BlockType, MarkType } from './types';

Expand Down Expand Up @@ -31,6 +31,9 @@ export const toggleKeyboardShortcut = (editor: Editor, event: KeyboardEvent<Elem
if (startPoint.offset !== 0) return false;

const [parentNode, parentPath] = Editor.parent(editor, startPoint);
const parentLocation = { at: parentPath };
const [previousNode] = Editor.previous(editor, parentLocation) ?? [];
const [nextNode] = Editor.next(editor, parentLocation) ?? [];

if (Editor.isEditor(parentNode)) return false;

Expand All @@ -45,14 +48,20 @@ export const toggleKeyboardShortcut = (editor: Editor, event: KeyboardEvent<Elem
) {
// exit formatting only when line block
// is first of last of it's parent
const parentLocation = { at: parentPath };
const [previousNode] = Editor.previous(editor, parentLocation) ?? [];
const [nextNode] = Editor.next(editor, parentLocation) ?? [];
if (!previousNode || !nextNode) {
toggleBlock(editor, BlockType.Paragraph);
return true;
}
}
// Unwrap paragraph children to put them
// in previous none paragraph element
if (SlateElement.isElement(previousNode) && previousNode.type !== BlockType.Paragraph) {
Transforms.unwrapNodes(editor, {
at: startPoint,
});
}
Editor.deleteBackward(editor);
return true;
}

if (isKeyHotkey('mod+e', event) || isKeyHotkey('escape', event)) {
Expand Down

0 comments on commit 9200e22

Please sign in to comment.