-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
color.ts
84 lines (75 loc) · 1.73 KB
/
color.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
import '@tiptap/extension-text-style'
import { Extension } from '@tiptap/core'
export type ColorOptions = {
/**
* The types where the color can be applied
* @default ['textStyle']
* @example ['heading', 'paragraph']
*/
types: string[],
}
declare module '@tiptap/core' {
interface Commands<ReturnType> {
color: {
/**
* Set the text color
* @param color The color to set
* @example editor.commands.setColor('red')
*/
setColor: (color: string) => ReturnType,
/**
* Unset the text color
* @example editor.commands.unsetColor()
*/
unsetColor: () => ReturnType,
}
}
}
/**
* This extension allows you to color your text.
* @see https://tiptap.dev/api/extensions/color
*/
export const Color = Extension.create<ColorOptions>({
name: 'color',
addOptions() {
return {
types: ['textStyle'],
}
},
addGlobalAttributes() {
return [
{
types: this.options.types,
attributes: {
color: {
default: null,
parseHTML: element => element.style.color?.replace(/['"]+/g, ''),
renderHTML: attributes => {
if (!attributes.color) {
return {}
}
return {
style: `color: ${attributes.color}`,
}
},
},
},
},
]
},
addCommands() {
return {
setColor: color => ({ chain }) => {
return chain()
.setMark('textStyle', { color })
.run()
},
unsetColor: () => ({ chain }) => {
return chain()
.setMark('textStyle', { color: null })
.removeEmptyTextStyle()
.run()
},
}
},
})