-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
task-list.ts
77 lines (66 loc) · 1.52 KB
/
task-list.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
import { mergeAttributes, Node } from '@tiptap/core'
export interface TaskListOptions {
/**
* The node type name for a task item.
* @default 'taskItem'
* @example 'myCustomTaskItem'
*/
itemTypeName: string,
/**
* The HTML attributes for a task list node.
* @default {}
* @example { class: 'foo' }
*/
HTMLAttributes: Record<string, any>,
}
declare module '@tiptap/core' {
interface Commands<ReturnType> {
taskList: {
/**
* Toggle a task list
* @example editor.commands.toggleTaskList()
*/
toggleTaskList: () => ReturnType,
}
}
}
/**
* This extension allows you to create task lists.
* @see https://www.tiptap.dev/api/nodes/task-list
*/
export const TaskList = Node.create<TaskListOptions>({
name: 'taskList',
addOptions() {
return {
itemTypeName: 'taskItem',
HTMLAttributes: {},
}
},
group: 'block list',
content() {
return `${this.options.itemTypeName}+`
},
parseHTML() {
return [
{
tag: `ul[data-type="${this.name}"]`,
priority: 51,
},
]
},
renderHTML({ HTMLAttributes }) {
return ['ul', mergeAttributes(this.options.HTMLAttributes, HTMLAttributes, { 'data-type': this.name }), 0]
},
addCommands() {
return {
toggleTaskList: () => ({ commands }) => {
return commands.toggleList(this.name, this.options.itemTypeName)
},
}
},
addKeyboardShortcuts() {
return {
'Mod-Shift-9': () => this.editor.commands.toggleTaskList(),
}
},
})