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 backward delete with previous empty line #1469

Merged
merged 1 commit into from
Oct 21, 2023
Merged
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
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