add defaultAlignment option to text align extension

This commit is contained in:
Philipp Kühn
2020-10-27 11:55:13 +01:00
parent 2c52f0ce06
commit b962890de5

View File

@@ -3,12 +3,14 @@ import { Command, createExtension } from '@tiptap/core'
type TextAlignOptions = { type TextAlignOptions = {
types: string[], types: string[],
alignments: string[], alignments: string[],
defaultAlignment: string,
} }
const TextAlign = createExtension({ const TextAlign = createExtension({
defaultOptions: <TextAlignOptions>{ defaultOptions: <TextAlignOptions>{
types: ['heading', 'paragraph'], types: ['heading', 'paragraph'],
alignments: ['left', 'center', 'right'], alignments: ['left', 'center', 'right'],
defaultAlignment: 'left',
}, },
addGlobalAttributes() { addGlobalAttributes() {
@@ -17,12 +19,12 @@ const TextAlign = createExtension({
types: this.options.types, types: this.options.types,
attributes: { attributes: {
textAlign: { textAlign: {
default: 'left', default: this.options.defaultAlignment,
renderHTML: attributes => ({ renderHTML: attributes => ({
style: `text-align: ${attributes.textAlign}`, style: `text-align: ${attributes.textAlign}`,
}), }),
parseHTML: node => ({ parseHTML: element => ({
textAlign: node.style.textAlign || 'left', textAlign: element.style.textAlign || this.options.defaultAlignment,
}), }),
}, },
}, },