/** * Tab key support for TipTap docs editor. * * - In lists: Tab indents (sinks), Shift+Tab outdents (lifts) * - In code blocks: Tab inserts a tab character * - In normal paragraphs: Tab inserts 4 spaces */ import { Extension } from '@tiptap/core'; export const TabSupport = Extension.create({ name: 'tabSupport', addKeyboardShortcuts() { return { Tab: () => { const { editor } = this; // In a list? Sink the list item (increase indent) if (editor.isActive('listItem') || editor.isActive('taskItem')) { return editor.chain().focus().sinkListItem('listItem').run() || editor.chain().focus().sinkListItem('taskItem').run(); } // In a code block? Insert a tab character if (editor.isActive('codeBlock')) { return editor.chain().focus().command(({ tr, state }) => { tr.insertText('\t', state.selection.from, state.selection.to); return true; }).run(); } // Normal paragraph: insert 4 spaces return editor.chain().focus().command(({ tr, state }) => { tr.insertText(' ', state.selection.from, state.selection.to); return true; }).run(); }, 'Shift-Tab': () => { const { editor } = this; // In a list? Lift the list item (decrease indent) if (editor.isActive('listItem') || editor.isActive('taskItem')) { return editor.chain().focus().liftListItem('listItem').run() || editor.chain().focus().liftListItem('taskItem').run(); } // In code block or normal text: do nothing special return false; }, }; }, });