-
Notifications
You must be signed in to change notification settings - Fork 31
/
font-family.ts
72 lines (63 loc) · 2.38 KB
/
font-family.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import type { FontFamilyProps } from '@/constants/define'
import type { GeneralOptions } from '@/type'
import type { FontFamilyOptions as TiptapFontFamilyOptions } from '@tiptap/extension-font-family'
import type { Item } from './components/ActionMenuButton.vue'
import { DEFAULT_FONT_FAMILY_LIST, DEFAULT_FONT_FAMILY_VALUE } from '@/constants/define'
import { FontFamily as TiptapFontFamily } from '@tiptap/extension-font-family'
import ActionMenuButton from './components/ActionMenuButton.vue'
/**
* Represents the interface for font family options, extending TiptapFontFamilyOptions and GeneralOptions.
*/
export interface FontFamilyOptions extends TiptapFontFamilyOptions, GeneralOptions<FontFamilyOptions> {
/**
* List of available font family properties
*
* @default DEFAULT_FONT_FAMILY_LIST
*/
fontFamilies: FontFamilyProps[]
}
export const FontFamily = /* @__PURE__*/ TiptapFontFamily.extend<FontFamilyOptions>({
addOptions() {
return {
...this.parent?.(),
fontFamilies: DEFAULT_FONT_FAMILY_LIST,
button: ({ editor, extension, t }) => {
const fontFamilies = (extension.options?.fontFamilies as FontFamilyProps[]) || []
const items: Item[] = fontFamilies.map(k => ({
title: t(k.title),
isActive: () => {
const { fontFamily } = editor.getAttributes('textStyle')
const isDefault = k.value === DEFAULT_FONT_FAMILY_VALUE
const notFontFamily = fontFamily === undefined
if (isDefault && notFontFamily) {
return true
}
return editor.isActive({ fontFamily: k.value }) || false
},
action: () => {
if (k.value === DEFAULT_FONT_FAMILY_VALUE) {
editor.commands.unsetFontFamily()
return
}
editor.commands.setFontFamily(k.value)
},
disabled: !editor.can().setFontFamily(k.value),
style: { fontFamily: k.value },
divider: k.divider ?? false,
default: k.default ?? false
}))
const disabled = items.filter(k => k.disabled).length === items.length
return {
component: ActionMenuButton,
componentProps: {
icon: 'fontFamily',
tooltip: t('editor.fontFamily.tooltip'),
disabled,
items,
maxHeight: 280
}
}
}
}
}
})