-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground-color.ts
76 lines (69 loc) · 1.65 KB
/
background-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
import { Extension } from '@tiptap/core'
export type BackgroundColorOptions = {
/**
* The types where the color can be applied
* @default ['textStyle']
* @example ['heading', 'paragraph']
*/
types: string[]
}
declare module '@tiptap/core' {
interface Commands<ReturnType> {
backgroundColor: {
/**
* Set the background color
*/
setBackgroundColor: (color: string) => ReturnType
/**
* Unset the background color
*/
unsetBackgroundColor: () => ReturnType
}
}
}
export const BackgroundColor = Extension.create<BackgroundColorOptions>({
name: 'backgroundColor',
addOptions() {
return {
types: ['textStyle']
}
},
addGlobalAttributes() {
return [
{
types: this.options.types,
attributes: {
backgroundColor: {
default: null,
parseHTML: (element) => element.style.backgroundColor?.replace(/['"]+/g, ''),
renderHTML: (attributes) => {
if (!attributes.backgroundColor) {
return {}
}
return {
style: `background-color: ${attributes.backgroundColor}`
}
}
}
}
}
]
},
addCommands() {
return {
setBackgroundColor:
(color) =>
({ chain }) => {
return chain().setMark('textStyle', { backgroundColor: color }).run()
},
unsetBackgroundColor:
() =>
({ chain }) => {
return chain()
.setMark('textStyle', { backgroundColor: null })
.removeEmptyTextStyle()
.run()
}
}
}
})