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,12 @@
<template> <template>
<div v-if="editor"> <div v-if="editor">
<button @click="editor.chain().focus().setNodeAttributes({ textAlign: 'left' }).run()"> <button @click="editor.chain().focus().textAlign('left').run()">
left left
</button> </button>
<button @click="editor.chain().focus().setNodeAttributes({ textAlign: 'center' }).run()"> <button @click="editor.chain().focus().textAlign('center').run()">
center center
</button> </button>
<button @click="editor.chain().focus().setNodeAttributes({ textAlign: 'right' }).run()"> <button @click="editor.chain().focus().textAlign('right').run()">
right right
</button> </button>
<editor-content :editor="editor" /> <editor-content :editor="editor" />

View File

@@ -1,12 +1,14 @@
import { createExtension } from '@tiptap/core' import { Command, createExtension } from '@tiptap/core'
type TextAlignOptions = { type TextAlignOptions = {
types: string[], types: string[],
alignments: string[],
} }
const TextAlign = createExtension({ const TextAlign = createExtension({
defaultOptions: <TextAlignOptions>{ defaultOptions: <TextAlignOptions>{
types: ['heading', 'paragraph'], types: ['heading', 'paragraph'],
alignments: ['left', 'center', 'right'],
}, },
addGlobalAttributes() { 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 export default TextAlign