From 3e954e655ddcc54892fe9ee6a363896812324412 Mon Sep 17 00:00:00 2001 From: fantasticit Date: Fri, 25 Nov 2022 15:21:10 +0800 Subject: [PATCH] close #208 --- .../src/tiptap/core/extensions/task-list.ts | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/packages/client/src/tiptap/core/extensions/task-list.ts b/packages/client/src/tiptap/core/extensions/task-list.ts index 04743e4..0cb76be 100644 --- a/packages/client/src/tiptap/core/extensions/task-list.ts +++ b/packages/client/src/tiptap/core/extensions/task-list.ts @@ -1,4 +1,7 @@ +import { getNodeType } from '@tiptap/core'; import { TaskList as BuiltInTaskList } from '@tiptap/extension-task-list'; +import { liftListItem } from 'prosemirror-schema-list'; +import { findParentNodeClosestToPos } from 'prosemirror-utils'; import { PARSE_HTML_PRIORITY_HIGHEST } from 'tiptap/core/constants'; export const TaskList = BuiltInTaskList.extend({ @@ -10,4 +13,23 @@ export const TaskList = BuiltInTaskList.extend({ }, ]; }, + + addKeyboardShortcuts() { + return { + ...this.parent?.(), + Backspace: ({ editor }) => { + const { selection } = editor.state; + const { $from } = selection; + const maybeTask = findParentNodeClosestToPos($from, (node) => node.type.name === this.name); + + if (maybeTask && maybeTask.node.childCount === 1 && !maybeTask.node.textContent) { + const name = this.editor.can().liftListItem('taskItem') ? 'taskItem' : 'listItem'; + const type = getNodeType(name, editor.view.state.schema); + return liftListItem(type)(editor.view.state, editor.view.dispatch); + } + + return false; + }, + }; + }, });