add textAlign command
This commit is contained in:
@@ -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" />
|
||||||
|
|||||||
@@ -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
|
||||||
|
|||||||
Reference in New Issue
Block a user