|
| 1 | +import { Extension } from '@tiptap/core'; |
| 2 | +import { AllSelection, TextSelection, Transaction } from 'prosemirror-state'; |
| 3 | + |
| 4 | +declare module '@tiptap/core' { |
| 5 | + interface Commands<ReturnType> { |
| 6 | + indent: { |
| 7 | + /** |
| 8 | + * Indent content |
| 9 | + */ |
| 10 | + indent: () => ReturnType; |
| 11 | + /** |
| 12 | + * Outdent content |
| 13 | + */ |
| 14 | + outdent: () => ReturnType; |
| 15 | + }; |
| 16 | + } |
| 17 | +} |
| 18 | + |
| 19 | +export interface IndentOptions { |
| 20 | + /** |
| 21 | + * The types of nodes that should be indented |
| 22 | + */ |
| 23 | + types: string[]; |
| 24 | + |
| 25 | + /** |
| 26 | + * The minimum indentation level |
| 27 | + */ |
| 28 | + minIndentLevel: number; |
| 29 | + |
| 30 | + /** |
| 31 | + * The maximum indentation level |
| 32 | + */ |
| 33 | + maxIndentLevel: number; |
| 34 | + |
| 35 | + /** |
| 36 | + * The indent size |
| 37 | + */ |
| 38 | + indentSize: number; |
| 39 | +} |
| 40 | + |
| 41 | +function clamp(val: number, min: number, max: number): number { |
| 42 | + if (val < min) { |
| 43 | + return min; |
| 44 | + } |
| 45 | + if (val > max) { |
| 46 | + return max; |
| 47 | + } |
| 48 | + return val; |
| 49 | +} |
| 50 | + |
| 51 | +const INDENT_MIN = 0; |
| 52 | +const INDENT_MAX = 400; // Maximum limit of 400px |
| 53 | +const INDENT_MORE = 40; // Increment of 40px |
| 54 | +const INDENT_LESS = -40; |
| 55 | + |
| 56 | +function setNodeIndentMarkup(tr: Transaction, pos: number, delta: number): Transaction { |
| 57 | + if (!tr.doc) return tr; |
| 58 | + |
| 59 | + const node = tr.doc.nodeAt(pos); |
| 60 | + if (!node) return tr; |
| 61 | + |
| 62 | + const minIndent = INDENT_MIN; |
| 63 | + const maxIndent = INDENT_MAX; |
| 64 | + const indent = clamp((node.attrs.indent || 0) + delta, minIndent, maxIndent); |
| 65 | + |
| 66 | + if (indent === node.attrs.indent) return tr; |
| 67 | + |
| 68 | + const nodeAttrs = { |
| 69 | + ...node.attrs, |
| 70 | + indent |
| 71 | + }; |
| 72 | + |
| 73 | + return tr.setNodeMarkup(pos, node.type, nodeAttrs, node.marks); |
| 74 | +} |
| 75 | + |
| 76 | +function updateIndentLevel(tr: Transaction, delta: number): Transaction { |
| 77 | + const { doc, selection } = tr; |
| 78 | + |
| 79 | + if (!doc || !selection) return tr; |
| 80 | + |
| 81 | + if (!(selection instanceof TextSelection || selection instanceof AllSelection)) { |
| 82 | + return tr; |
| 83 | + } |
| 84 | + |
| 85 | + const { from, to } = selection; |
| 86 | + |
| 87 | + doc.nodesBetween(from, to, (node, pos) => { |
| 88 | + const nodeType = node.type; |
| 89 | + |
| 90 | + // Only handle paragraphs, headings and blockquotes - NO lists |
| 91 | + if ( |
| 92 | + nodeType.name === 'paragraph' || |
| 93 | + nodeType.name === 'heading' || |
| 94 | + nodeType.name === 'blockquote' |
| 95 | + ) { |
| 96 | + tr = setNodeIndentMarkup(tr, pos, delta); |
| 97 | + return false; |
| 98 | + } |
| 99 | + |
| 100 | + return true; |
| 101 | + }); |
| 102 | + |
| 103 | + return tr; |
| 104 | +} |
| 105 | + |
| 106 | +export const IndentExtension = Extension.create<IndentOptions>({ |
| 107 | + name: 'indent', |
| 108 | + |
| 109 | + addOptions() { |
| 110 | + return { |
| 111 | + types: ['heading', 'paragraph', 'blockquote'], |
| 112 | + minIndentLevel: 0, |
| 113 | + maxIndentLevel: 10, |
| 114 | + indentSize: 40 |
| 115 | + }; |
| 116 | + }, |
| 117 | + |
| 118 | + addGlobalAttributes() { |
| 119 | + return [ |
| 120 | + { |
| 121 | + types: this.options.types, |
| 122 | + attributes: { |
| 123 | + indent: { |
| 124 | + default: 0, |
| 125 | + renderHTML: (attributes) => { |
| 126 | + if (!attributes.indent || attributes.indent <= 0) { |
| 127 | + return {}; |
| 128 | + } |
| 129 | + return { |
| 130 | + style: `margin-left: ${attributes.indent}px;` |
| 131 | + }; |
| 132 | + }, |
| 133 | + parseHTML: (element) => { |
| 134 | + const marginLeft = element.style.marginLeft; |
| 135 | + if (!marginLeft) return 0; |
| 136 | + |
| 137 | + const value = parseInt(marginLeft, 10); |
| 138 | + return value |
| 139 | + ? Math.round(value / this.options.indentSize) * |
| 140 | + this.options.indentSize |
| 141 | + : 0; |
| 142 | + } |
| 143 | + } |
| 144 | + } |
| 145 | + } |
| 146 | + ]; |
| 147 | + }, |
| 148 | + |
| 149 | + addCommands() { |
| 150 | + return { |
| 151 | + indent: |
| 152 | + () => |
| 153 | + ({ state, dispatch }) => { |
| 154 | + // Only for paragraphs, headings, blockquotes - NO lists |
| 155 | + const { selection } = state; |
| 156 | + const newTr = state.tr.setSelection(selection); |
| 157 | + const updatedTr = updateIndentLevel(newTr, INDENT_MORE); |
| 158 | + |
| 159 | + if (updatedTr.docChanged && dispatch) { |
| 160 | + dispatch(updatedTr); |
| 161 | + return true; |
| 162 | + } |
| 163 | + |
| 164 | + return false; |
| 165 | + }, |
| 166 | + |
| 167 | + outdent: |
| 168 | + () => |
| 169 | + ({ state, dispatch }) => { |
| 170 | + // Only for paragraphs, headings, blockquotes - NO lists |
| 171 | + const { selection } = state; |
| 172 | + const newTr = state.tr.setSelection(selection); |
| 173 | + const updatedTr = updateIndentLevel(newTr, INDENT_LESS); |
| 174 | + |
| 175 | + if (updatedTr.docChanged && dispatch) { |
| 176 | + dispatch(updatedTr); |
| 177 | + return true; |
| 178 | + } |
| 179 | + |
| 180 | + return false; |
| 181 | + } |
| 182 | + }; |
| 183 | + }, |
| 184 | + |
| 185 | + addKeyboardShortcuts() { |
| 186 | + return { |
| 187 | + Tab: () => { |
| 188 | + // Don't handle Tab if we're in a list |
| 189 | + if (this.editor.isActive('bulletList') || this.editor.isActive('orderedList')) { |
| 190 | + return false; |
| 191 | + } |
| 192 | + return this.editor.commands.indent(); |
| 193 | + }, |
| 194 | + 'Shift-Tab': () => { |
| 195 | + // Don't handle Shift-Tab if we're in a list |
| 196 | + if (this.editor.isActive('bulletList') || this.editor.isActive('orderedList')) { |
| 197 | + return false; |
| 198 | + } |
| 199 | + return this.editor.commands.outdent(); |
| 200 | + }, |
| 201 | + Backspace: () => { |
| 202 | + const { state } = this.editor; |
| 203 | + const { from, to } = state.selection; |
| 204 | + |
| 205 | + // Only outdent if we're at the beginning of the node without selection |
| 206 | + const hasTextBeforeCursor = |
| 207 | + from > 1 && state.doc.textBetween(from - 1, from).trim().length > 0; |
| 208 | + const hasSelection = from !== to; |
| 209 | + |
| 210 | + if (hasTextBeforeCursor || hasSelection) { |
| 211 | + return false; |
| 212 | + } |
| 213 | + |
| 214 | + // Don't handle if we're in a list |
| 215 | + if (this.editor.isActive('bulletList') || this.editor.isActive('orderedList')) { |
| 216 | + return false; |
| 217 | + } |
| 218 | + |
| 219 | + return this.editor.commands.outdent(); |
| 220 | + } |
| 221 | + }; |
| 222 | + } |
| 223 | +}); |
0 commit comments