Skip to content

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

editor: improve backspace handling with lists

alihamuhc7d26d2444fa
diff --git a/packages/editor/src/extensions/key-map/key-map.ts b/packages/editor/src/extensions/key-map/key-map.ts
index cf94d7a81..3c414f3f6 100644
--- a/packages/editor/src/extensions/key-map/key-map.ts
+++ b/packages/editor/src/extensions/key-map/key-map.ts
@@ -17,9 +17,13 @@ You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
-import { Extension } from "@tiptap/core";
+import { Editor, Extension } from "@tiptap/core";
import { isInTable } from "@tiptap/pm/tables";
-import { isListActive } from "../../utils/prosemirror";
+import {
+ isListActive,
+ LIST_ITEM_NODE_TYPES,
+ LIST_NODE_TYPES
+} from "../../utils/prosemirror";
export const KeyMap = Extension.create({
name: "key-map",
@@ -33,7 +37,53 @@ export const KeyMap = Extension.create({
"Shift-Tab": ({ editor }) => {
if (isListActive(editor)) return false;
return true;
+ },
+ Backspace: ({ editor }) => {
+ return joinUpWithLastListItem(editor);
}
};
}
});
+
+/**
+ * By default, in ProseMirror, if the cursor is right below a list,
+ * pressing Backspace creates a new empty list item at the end.
+ * This function corrects this behavior and moves the cursor to the
+ * end of the list.
+ */
+function joinUpWithLastListItem(editor: Editor) {
+ const { selection, doc } = editor.state;
+ const { $from, empty } = selection;
+
+ const parentPos = $from.pos - $from.parentOffset - 1;
+ const before = doc.resolve(parentPos).nodeBefore;
+
+ if (
+ empty &&
+ $from.parentOffset === 0 &&
+ before &&
+ LIST_NODE_TYPES.includes(before.type.name)
+ ) {
+ // The last list item can be at any depth and the number of joinBackwards
+ // depend on how deep it is.
+ let lastItemDepth = 0;
+ before.descendants((node, pos) => {
+ if (LIST_ITEM_NODE_TYPES.includes(node.type.name)) {
+ const { depth } = before.resolve(pos);
+ lastItemDepth = depth / 2;
+ }
+ });
+ // The depth of the last list item does not take into account
+ // the parent depth (i.e. 1) so we have to increment it.
+ lastItemDepth++;
+
+ let commands = editor.chain().joinBackward();
+ for (let i = 0; i < lastItemDepth; ++i) {
+ commands = commands.joinBackward().joinBackward();
+ }
+
+ return commands.run();
+ }
+
+ return false;
+}
diff --git a/packages/editor/src/extensions/list-item/commands.ts b/packages/editor/src/extensions/list-item/commands.ts
index 7c7838445..884b64a01 100644
--- a/packages/editor/src/extensions/list-item/commands.ts
+++ b/packages/editor/src/extensions/list-item/commands.ts
@@ -34,6 +34,7 @@ export function onBackspacePressed(
) {
const { selection } = editor.state;
const { empty, $from } = selection;
+
if (
!empty ||
!isInside(name, type, editor.state) ||
@@ -52,12 +53,11 @@ export function onBackspacePressed(
if (parentList.childCount > 1) {
return editor.commands.liftListItem(type);
}
- return editor.commands.deleteNode(parentList.type);
}
- return editor.commands.deleteNode(type);
+ return editor.chain().joinBackward().joinBackward().run();
} else if (isFirstOfType(type, editor.state)) {
- return false;
+ return editor.commands.liftListItem(type);
} else {
const block = findParentNodeOfType(type)(selection);
if (block && block.start === $from.pos - 1) {
diff --git a/packages/editor/src/utils/prosemirror.ts b/packages/editor/src/utils/prosemirror.ts
index fbd7b743d..744950399 100644
--- a/packages/editor/src/utils/prosemirror.ts
+++ b/packages/editor/src/utils/prosemirror.ts
@@ -32,6 +32,32 @@ import {
Attrs
} from "prosemirror-model";
import { EditorState, Selection } from "prosemirror-state";
+import BulletList from "../extensions/bullet-list";
+import { ListItem } from "../extensions/list-item";
+import OrderedList from "../extensions/ordered-list";
+import { OutlineList } from "../extensions/outline-list";
+import { OutlineListItem } from "../extensions/outline-list-item";
+import { TaskItemNode } from "../extensions/task-item";
+import { TaskListNode } from "../extensions/task-list";
+
+export type NodeWithOffset = {
+ node?: ProsemirrorNode;
+ from: number;
+ to: number;
+};
+
+export const LIST_NODE_TYPES = [
+ TaskListNode.name,
+ OutlineList.name,
+ BulletList.name,
+ OrderedList.name
+];
+
+export const LIST_ITEM_NODE_TYPES = [
+ TaskItemNode.name,
+ OutlineListItem.name,
+ ListItem.name
+];
export function hasSameAttributes(prev: Attrs, next: Attrs) {
for (const key in prev) {
@@ -42,11 +68,24 @@ export function hasSameAttributes(prev: Attrs, next: Attrs) {
return true;
}
-export type NodeWithOffset = {
- node?: ProsemirrorNode;
- from: number;
- to: number;
-};
+export function findListItemType(editor: Editor): string | null {
+ const isTaskList = editor.isActive(TaskListNode.name);
+ const isOutlineList = editor.isActive(OutlineList.name);
+ const isList =
+ editor.isActive(BulletList.name) || editor.isActive(OrderedList.name);
+
+ return isList
+ ? ListItem.name
+ : isOutlineList
+ ? OutlineListItem.name
+ : isTaskList
+ ? TaskItemNode.name
+ : null;
+}
+
+export function isListActive(editor: Editor): boolean {
+ return LIST_NODE_TYPES.some((name) => editor.isActive(name));
+}
export function findSelectedDOMNode(
editor: Editor,
@@ -100,30 +139,6 @@ export function selectionToOffset(state: EditorState): NodeWithOffset {
};
}
-export function findListItemType(editor: Editor): string | null {
- const isTaskList = editor.isActive("taskList");
- const isOutlineList = editor.isActive("outlineList");
- const isList =
- editor.isActive("bulletList") || editor.isActive("orderedList");
-
- return isList
- ? "listItem"
- : isOutlineList
- ? "outlineListItem"
- : isTaskList
- ? "taskItem"
- : null;
-}
-
-export function isListActive(editor: Editor): boolean {
- const isTaskList = editor.isActive("taskList");
- const isOutlineList = editor.isActive("outlineList");
- const isList =
- editor.isActive("bulletList") || editor.isActive("orderedList");
-
- return isTaskList || isOutlineList || isList;
-}
-
export const findChildren = (
node: ProsemirrorNode,
predicate: Predicate,