-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathfont-size.ts
95 lines (86 loc) · 2.14 KB
/
font-size.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import type { GeneralOptions } from '@/type'
import { DEFAULT_FONT_SIZE_LIST } from '@/constants/define'
import { getCssUnitWithDefault } from '@/utils/utils'
import { Extension } from '@tiptap/core'
import { FontSizeActionMenuButton } from './components/ActionMenuButton'
/**
* Represents the interface for font size options, extending GeneralOptions.
*/
export interface FontSizeOptions extends GeneralOptions<FontSizeOptions> {
types: string[]
/**
* List of available font size values
*
* @default DEFAULT_FONT_SIZE_LIST
*/
fontSizes: number[]
}
declare module '@tiptap/core' {
interface Commands<ReturnType> {
fontSize: {
/**
* Set the font size
*/
setFontSize: (fontSize: string) => ReturnType
/**
* Unset the font size
*/
unsetFontSize: () => ReturnType
}
}
}
export const FontSize = /* @__PURE__*/ Extension.create<FontSizeOptions>({
name: 'fontSize',
addOptions() {
return {
...this.parent?.(),
types: ['textStyle'],
fontSizes: [...DEFAULT_FONT_SIZE_LIST],
button: ({ editor, extension, t }) => {
return {
component: FontSizeActionMenuButton,
componentProps: {
editor,
extension,
t
}
}
}
}
},
addGlobalAttributes() {
return [
{
types: this.options.types,
attributes: {
fontSize: {
default: null,
parseHTML: element => element.style.fontSize || '',
renderHTML: attributes => {
if (!attributes.fontSize) {
return {}
}
return {
style: `font-size: ${getCssUnitWithDefault(attributes.fontSize)}`
}
}
}
}
}
]
},
addCommands() {
return {
setFontSize:
fontSize =>
({ chain }) => {
return chain().setMark('textStyle', { fontSize }).run()
},
unsetFontSize:
() =>
({ chain }) => {
return chain().setMark('textStyle', { fontSize: null }).removeEmptyTextStyle().run()
}
}
}
})