Skip to content

Commit

Permalink
simplify listOnBackspace
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonas-C committed Jan 16, 2025
1 parent 9f4d67e commit ae3917d
Showing 1 changed file with 11 additions and 12 deletions.
23 changes: 11 additions & 12 deletions packages/editor/src/plugins/list/handlers/listOnBackspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,23 @@ import type { ShortcutHandler } from "../../../core";
import { LIST_ELEMENT_TYPE, LIST_ITEM_ELEMENT_TYPE } from "../listTypes";
import { hasNodeOfType } from "../../../queries/hasNodeOfType";
import { getCurrentBlock } from "../../../queries/getCurrentBlock";
import { isListItemElement } from "../queries/listElementQueries";

// This function only has one purpose: to remove a list item when the user presses backspace at the start of a list item.
// Can probably be simplified further.
export const listOnBackspace: ShortcutHandler = (editor, event) => {
if (!editor.selection || !hasNodeOfType(editor, LIST_ELEMENT_TYPE)) return false;
if (!editor.selection || !Range.isCollapsed(editor.selection) || !hasNodeOfType(editor, LIST_ELEMENT_TYPE))
return false;

const entry = getCurrentBlock(editor, LIST_ITEM_ELEMENT_TYPE);
if (!entry) return false;

const [currentItemNode, currentItemPath] = entry;
if (isListItemElement(currentItemNode) && Range.isCollapsed(editor.selection)) {
// Check that cursor is not expanded.
const [, firstItemNodePath] = editor.node([...currentItemPath, 0]);
// If cursor is placed at start of first item child
if (Point.equals(Range.start(editor.selection), editor.start(firstItemNodePath))) {
event.preventDefault();
Transforms.liftNodes(editor, { at: currentItemPath });
return true;
}
const [, currentItemPath] = entry;
// Check that cursor is not expanded.
// If cursor is placed at start of first item child
if (Point.equals(Range.start(editor.selection), editor.start(currentItemPath.concat(0)))) {
event.preventDefault();
Transforms.liftNodes(editor, { at: currentItemPath });
return true;
}
return false;
};

0 comments on commit ae3917d

Please sign in to comment.