add textAlign command

This commit is contained in:
Philipp Kühn
2020-10-23 17:50:28 +02:00
parent 9f908b2cbf
commit 4bff000751
2 changed files with 18 additions and 4 deletions

View File

@@ -1,12 +1,14 @@
import { createExtension } from '@tiptap/core'
import { Command, createExtension } from '@tiptap/core'
type TextAlignOptions = {
types: string[],
alignments: string[],
}
const TextAlign = createExtension({
defaultOptions: <TextAlignOptions>{
types: ['heading', 'paragraph'],
alignments: ['left', 'center', 'right'],
},
addGlobalAttributes() {
@@ -24,6 +26,18 @@ const TextAlign = createExtension({
},
]
},
addCommands() {
return {
textAlign: (alignment: string): Command => ({ commands }) => {
if (!this.options.alignments.includes(alignment)) {
return false
}
return commands.setNodeAttributes({ textAlign: alignment })
},
}
},
})
export default TextAlign