2021-06-05 03:56:29 +08:00
|
|
|
import { Extension } from '@tiptap/core'
|
2020-10-23 21:23:40 +08:00
|
|
|
|
2021-07-26 21:14:22 +08:00
|
|
|
export interface TextAlignOptions {
|
2024-05-11 20:30:44 +08:00
|
|
|
/**
|
|
|
|
* The types where the text align attribute can be applied.
|
|
|
|
* @default []
|
|
|
|
* @example ['heading', 'paragraph']
|
|
|
|
*/
|
2020-10-23 23:19:12 +08:00
|
|
|
types: string[],
|
2024-05-11 20:30:44 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The alignments which are allowed.
|
|
|
|
* @default ['left', 'center', 'right', 'justify']
|
|
|
|
* @example ['left', 'right']
|
|
|
|
*/
|
2020-10-23 23:50:28 +08:00
|
|
|
alignments: string[],
|
2024-05-11 20:30:44 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The default alignment.
|
|
|
|
* @default 'left'
|
|
|
|
* @example 'center'
|
|
|
|
*/
|
2020-10-27 18:55:13 +08:00
|
|
|
defaultAlignment: string,
|
2020-10-23 23:19:12 +08:00
|
|
|
}
|
|
|
|
|
2021-02-10 16:59:35 +08:00
|
|
|
declare module '@tiptap/core' {
|
2021-06-05 03:56:29 +08:00
|
|
|
interface Commands<ReturnType> {
|
2021-02-16 18:27:58 +08:00
|
|
|
textAlign: {
|
|
|
|
/**
|
|
|
|
* Set the text align attribute
|
2024-05-11 20:30:44 +08:00
|
|
|
* @param alignment The alignment
|
|
|
|
* @example editor.commands.setTextAlign('left')
|
2021-02-16 18:27:58 +08:00
|
|
|
*/
|
2021-06-05 03:56:29 +08:00
|
|
|
setTextAlign: (alignment: string) => ReturnType,
|
2021-02-16 18:27:58 +08:00
|
|
|
/**
|
|
|
|
* Unset the text align attribute
|
2024-05-11 20:30:44 +08:00
|
|
|
* @example editor.commands.unsetTextAlign()
|
2021-02-16 18:27:58 +08:00
|
|
|
*/
|
2021-06-05 03:56:29 +08:00
|
|
|
unsetTextAlign: () => ReturnType,
|
2021-02-16 18:27:58 +08:00
|
|
|
}
|
2021-02-10 16:59:35 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-11 20:30:44 +08:00
|
|
|
/**
|
|
|
|
* This extension allows you to align text.
|
|
|
|
* @see https://www.tiptap.dev/api/extensions/text-align
|
|
|
|
*/
|
2021-02-11 01:25:08 +08:00
|
|
|
export const TextAlign = Extension.create<TextAlignOptions>({
|
2020-12-02 16:44:46 +08:00
|
|
|
name: 'textAlign',
|
|
|
|
|
2021-10-27 00:31:13 +08:00
|
|
|
addOptions() {
|
|
|
|
return {
|
|
|
|
types: [],
|
|
|
|
alignments: ['left', 'center', 'right', 'justify'],
|
|
|
|
defaultAlignment: 'left',
|
|
|
|
}
|
2020-10-23 23:19:12 +08:00
|
|
|
},
|
|
|
|
|
2020-10-23 21:23:40 +08:00
|
|
|
addGlobalAttributes() {
|
|
|
|
return [
|
|
|
|
{
|
2020-10-23 23:19:12 +08:00
|
|
|
types: this.options.types,
|
2020-10-23 21:23:40 +08:00
|
|
|
attributes: {
|
2020-10-23 23:19:12 +08:00
|
|
|
textAlign: {
|
2020-10-27 18:55:13 +08:00
|
|
|
default: this.options.defaultAlignment,
|
2024-05-30 23:09:44 +08:00
|
|
|
parseHTML: element => {
|
|
|
|
const alignment = element.style.textAlign || this.options.defaultAlignment
|
|
|
|
|
|
|
|
return this.options.alignments.includes(alignment) ? alignment : this.options.defaultAlignment
|
|
|
|
},
|
2021-05-03 07:54:03 +08:00
|
|
|
renderHTML: attributes => {
|
2021-05-07 04:06:28 +08:00
|
|
|
if (attributes.textAlign === this.options.defaultAlignment) {
|
2021-05-03 07:54:03 +08:00
|
|
|
return {}
|
|
|
|
}
|
2021-05-11 23:05:13 +08:00
|
|
|
|
|
|
|
return { style: `text-align: ${attributes.textAlign}` }
|
2021-05-03 07:54:03 +08:00
|
|
|
},
|
2020-10-23 21:23:40 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
]
|
|
|
|
},
|
2020-10-23 23:50:28 +08:00
|
|
|
|
|
|
|
addCommands() {
|
|
|
|
return {
|
2021-02-10 16:59:35 +08:00
|
|
|
setTextAlign: (alignment: string) => ({ commands }) => {
|
2020-10-23 23:50:28 +08:00
|
|
|
if (!this.options.alignments.includes(alignment)) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2024-05-10 08:32:59 +08:00
|
|
|
return this.options.types
|
|
|
|
.map(type => commands.updateAttributes(type, { textAlign: alignment }))
|
|
|
|
.every(response => response)
|
2020-10-23 23:50:28 +08:00
|
|
|
},
|
2021-12-02 21:56:57 +08:00
|
|
|
|
2021-02-10 16:59:35 +08:00
|
|
|
unsetTextAlign: () => ({ commands }) => {
|
2024-05-10 08:32:59 +08:00
|
|
|
return this.options.types
|
|
|
|
.map(type => commands.resetAttributes(type, 'textAlign'))
|
|
|
|
.every(response => response)
|
2020-11-18 19:34:06 +08:00
|
|
|
},
|
2020-10-23 23:50:28 +08:00
|
|
|
}
|
|
|
|
},
|
2020-10-27 19:37:20 +08:00
|
|
|
|
|
|
|
addKeyboardShortcuts() {
|
|
|
|
return {
|
2020-11-19 08:16:10 +08:00
|
|
|
'Mod-Shift-l': () => this.editor.commands.setTextAlign('left'),
|
|
|
|
'Mod-Shift-e': () => this.editor.commands.setTextAlign('center'),
|
|
|
|
'Mod-Shift-r': () => this.editor.commands.setTextAlign('right'),
|
|
|
|
'Mod-Shift-j': () => this.editor.commands.setTextAlign('justify'),
|
2020-10-27 19:37:20 +08:00
|
|
|
}
|
|
|
|
},
|
2020-10-23 21:23:40 +08:00
|
|
|
})
|