| 1 |
import { Extension } from '@tiptap/core' |
| 2 |
import { createIndentCommand, IndentProps } from '../utils/indentUtil.ts' |
| 3 |
|
| 4 |
export interface IndentOptions { |
| 5 |
types: string[] |
| 6 |
minIndent: number |
| 7 |
maxIndent: number |
| 8 |
} |
| 9 |
|
| 10 |
declare module '@tiptap/core' { |
| 11 |
interface Commands<ReturnType> { |
| 12 |
indent: { |
| 13 |
indent: () => ReturnType |
| 14 |
outdent: () => ReturnType |
| 15 |
} |
| 16 |
} |
| 17 |
} |
| 18 |
|
| 19 |
const Indent = Extension.create<IndentOptions>({ |
| 20 |
name: 'indent', |
| 21 |
|
| 22 |
addOptions() { |
| 23 |
return { |
| 24 |
buttonIcon: ['', ''], |
| 25 |
types: ['paragraph', 'heading', 'blockquote'], |
| 26 |
minIndent: IndentProps.min, |
| 27 |
maxIndent: IndentProps.max, |
| 28 |
} |
| 29 |
}, |
| 30 |
|
| 31 |
addGlobalAttributes() { |
| 32 |
return [ |
| 33 |
{ |
| 34 |
types: this.options.types, |
| 35 |
attributes: { |
| 36 |
indent: { |
| 37 |
default: 0, |
| 38 |
parseHTML: (element) => { |
| 39 |
const indent = element.style.textIndent |
| 40 |
|
| 41 |
return (indent ? parseInt(indent, 10) : 0) || 0 |
| 42 |
}, |
| 43 |
renderHTML: (attributes) => { |
| 44 |
if (!attributes.indent) { |
| 45 |
return {} |
| 46 |
} |
| 47 |
|
| 48 |
return { style: `text-indent: ${attributes.indent}rem;` } |
| 49 |
}, |
| 50 |
}, |
| 51 |
}, |
| 52 |
}, |
| 53 |
] |
| 54 |
}, |
| 55 |
|
| 56 |
addCommands() { |
| 57 |
return { |
| 58 |
indent: () => |
| 59 |
createIndentCommand({ |
| 60 |
delta: IndentProps.more, |
| 61 |
types: this.options.types, |
| 62 |
}), |
| 63 |
outdent: () => |
| 64 |
createIndentCommand({ |
| 65 |
delta: IndentProps.less, |
| 66 |
types: this.options.types, |
| 67 |
}), |
| 68 |
} |
| 69 |
}, |
| 70 |
|
| 71 |
addKeyboardShortcuts() { |
| 72 |
return { |
| 73 |
Tab: () => this.editor.commands.indent(), |
| 74 |
'Shift-Tab': () => this.editor.commands.outdent(), |
| 75 |
} |
| 76 |
}, |
| 77 |
}) |
| 78 |
|
| 79 |
export default Indent |
| 80 |
|