Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

fix: empty list is not deleted on backspace

thecodrr7cdda1938aac
diff --git a/packages/editor/src/extensions/list-item/commands.ts b/packages/editor/src/extensions/list-item/commands.ts
index 7af21eab8..b935dad81 100644
--- a/packages/editor/src/extensions/list-item/commands.ts
+++ b/packages/editor/src/extensions/list-item/commands.ts
@@ -25,10 +25,17 @@ export function onBackspacePressed(
const isEmpty = isListItemEmpty(type, editor.state);
- if (isFirstOfType(type, editor.state)) {
+ if (isEmpty) {
+ if (isFirstOfType(type, editor.state)) {
+ const parentList = getListFromListItem(type, editor.state);
+ if (!parentList) return false;
+ return editor.commands.deleteNode(parentList.type);
+ }
+
+ return editor.commands.deleteNode(type);
+ } else if (isFirstOfType(type, editor.state)) {
return editor.commands.liftListItem(type);
- } else if (isEmpty) return editor.commands.deleteNode(type);
- else {
+ } else {
// we have to run join backward twice because on the first join
// the two list items are joined i.e., the editor just puts their
// paragraphs next to each other. The next join merges the paragraphs
@@ -61,9 +68,24 @@ const isFirstOfType = (type: NodeType, state: EditorState) => {
if (!block) return false;
const { pos } = block;
const resolved = state.doc.resolve(pos);
+ console.log("isFirstOfType", resolved);
return !resolved.nodeBefore;
};
+const getListFromListItem = (type: NodeType, state: EditorState) => {
+ const block = findParentNodeOfType(type)(state.selection);
+ if (!block) return undefined;
+ const { pos } = block;
+ const resolved = state.doc.resolve(pos);
+ if (
+ !resolved.parent.type.spec.group ||
+ resolved.parent.type.spec.group?.indexOf("list") <= -1
+ )
+ return undefined;
+
+ return resolved.parent;
+};
+
function isListItemEmpty(type: NodeType, state: EditorState) {
const block = findParentNodeOfType(type)(state.selection);
if (!block) return false;