Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pref: optimize code block backspace shortcut key logic #5936

Merged
merged 2 commits into from
May 21, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions ui/packages/editor/src/extensions/code-block/code-block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
isActive,
findParentNode,
VueNodeViewRenderer,
isNodeActive,
} from "@/tiptap/vue-3";
import {
EditorState,
Expand Down Expand Up @@ -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();
Expand Down