From cc38a347bc18f39431f92b46dc9241a459dde680 Mon Sep 17 00:00:00 2001 From: LIlGG <1103069291@qq.com> Date: Fri, 17 May 2024 11:09:56 +0800 Subject: [PATCH] pref: optimize code block deletion shortcut key logic --- .../src/extensions/code-block/code-block.ts | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/ui/packages/editor/src/extensions/code-block/code-block.ts b/ui/packages/editor/src/extensions/code-block/code-block.ts index 03af3cc72c..e69aa69e0b 100644 --- a/ui/packages/editor/src/extensions/code-block/code-block.ts +++ b/ui/packages/editor/src/extensions/code-block/code-block.ts @@ -5,6 +5,7 @@ import { isActive, findParentNode, VueNodeViewRenderer, + isNodeActive, } from "@/tiptap/vue-3"; import { EditorState, @@ -130,6 +131,32 @@ export default CodeBlockLowlight.extend< }, addKeyboardShortcuts() { return { + Backspace: ({ editor }) => { + if (!isNodeActive(editor.state, this.name)) { + return false; + } + + const { selection } = editor.state; + // Clear the selected content and adapt to the all-select shortcut key operation. + if (!selection.empty) { + editor + .chain() + .focus() + .deleteSelection() + .setTextSelection(selection.$from.pos) + .run(); + return true; + } + + const { $anchor } = selection; + const isAtStart = $anchor.parentOffset === 0; + // If the cursor is at the beginning of the code block or the code block is empty, it is not deleted. + if (isAtStart || !$anchor.parent.textContent.length) { + return true; + } + + return false; + }, Tab: () => { if (this.editor.isActive("codeBlock")) { return this.editor.chain().focus().codeIndent().run();