Created
August 13, 2024 06:40
-
-
Save rchasman/1504332a3b50eb4aa616f1d9341d4001 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Extension } from "@tiptap/core"; | |
const TAB_CHAR = "\u0009"; | |
const IndentHandler = Extension.create({ | |
name: "indentHandler", | |
addKeyboardShortcuts() { | |
return { | |
Tab: ({ editor }) => { | |
const { selection } = editor.state; | |
const { $from } = selection; | |
// Check if we're at the start of a list item | |
if (editor.isActive("listItem") && $from.parentOffset === 0) { | |
// Attempt to sink the list item | |
const sinkResult = editor.chain().sinkListItem("listItem").run(); | |
// If sinking was successful, return true | |
if (sinkResult) { | |
return true; | |
} | |
// If sinking failed, we'll fall through to inserting a tab | |
} | |
// Insert a tab character | |
editor | |
.chain() | |
.command(({ tr }) => { | |
tr.insertText(TAB_CHAR); | |
return true; | |
}) | |
.run(); | |
// Prevent default behavior (losing focus) | |
return true; | |
}, | |
"Shift-Tab": ({ editor }) => { | |
const { selection, doc } = editor.state; | |
const { $from } = selection; | |
const pos = $from.pos; | |
// Check if we're at the start of a list item | |
if (editor.isActive("listItem") && $from.parentOffset === 0) { | |
// If so, lift the list item | |
return editor.chain().liftListItem("listItem").run(); | |
} | |
// Check if the previous character is a tab | |
if (doc.textBetween(pos - 1, pos) === TAB_CHAR) { | |
// If so, delete it | |
editor | |
.chain() | |
.command(({ tr }) => { | |
tr.delete(pos - 1, pos); | |
return true; | |
}) | |
.run(); | |
return true; | |
} | |
// Prevent default behavior (losing focus) | |
return true; | |
}, | |
}; | |
}, | |
}); | |
export default IndentHandler; |
Thank you for sharing, but taskItem
should also be account for.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is great! Thank you for sharing