-
Notifications
You must be signed in to change notification settings - Fork 32
/
text-align.ts
54 lines (46 loc) · 1.74 KB
/
text-align.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import type { GeneralOptions } from '@/type'
import type { TextAlignOptions as TiptapTextAlignOptions } from '@tiptap/extension-text-align'
import type { Item } from './components/ActionMenuButton.vue'
import { TextAlign as TiptapTextAlign } from '@tiptap/extension-text-align'
import ActionMenuButton from './components/ActionMenuButton.vue'
/** Represents the type for text alignments */
type Alignments = 'left' | 'center' | 'right' | 'justify'
/**
* Represents the interface for text align options, extending TiptapTextAlignOptions and GeneralOptions.
*/
export interface TextAlignOptions extends TiptapTextAlignOptions, GeneralOptions<TextAlignOptions> {
/**
* List of available alignment options
*
* @default ['left', 'center', 'right', 'justify']
*/
alignments: Alignments[]
}
export const TextAlign = /* @__PURE__*/ TiptapTextAlign.extend<TextAlignOptions>({
addOptions() {
return {
...this.parent?.(),
types: ['heading', 'paragraph', 'image'],
button: ({ editor, extension, t }) => {
const alignments = (extension.options?.alignments as Alignments[]) || []
const items: Item[] = alignments.map(k => ({
title: t(`editor.textalign.${k}.tooltip`),
icon: k,
isActive: () => editor.isActive({ textAlign: k }) || false,
action: () => editor.chain().focus().setTextAlign(k).run(),
disabled: !editor.can().setTextAlign(k)
}))
const disabled = items.filter(k => k.disabled).length === items.length
return {
component: ActionMenuButton,
componentProps: {
icon: 'center',
tooltip: t('editor.textalign.tooltip'),
disabled,
items
}
}
}
}
}
})