2021-06-05 03:56:29 +08:00
|
|
|
import { Node, mergeAttributes } from '@tiptap/core'
|
2020-10-30 23:06:30 +08:00
|
|
|
|
2020-11-13 23:44:22 +08:00
|
|
|
export interface TaskListOptions {
|
2021-12-11 07:13:24 +08:00
|
|
|
itemTypeName: string,
|
2021-04-21 15:43:31 +08:00
|
|
|
HTMLAttributes: Record<string, any>,
|
2020-11-13 23:44:22 +08:00
|
|
|
}
|
|
|
|
|
2021-02-10 16:59:35 +08:00
|
|
|
declare module '@tiptap/core' {
|
2021-06-05 03:56:29 +08:00
|
|
|
interface Commands<ReturnType> {
|
2021-02-16 18:27:58 +08:00
|
|
|
taskList: {
|
|
|
|
/**
|
|
|
|
* Toggle a task list
|
|
|
|
*/
|
2021-06-05 03:56:29 +08:00
|
|
|
toggleTaskList: () => ReturnType,
|
2021-02-16 18:27:58 +08:00
|
|
|
}
|
2021-02-10 16:59:35 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-11 01:25:08 +08:00
|
|
|
export const TaskList = Node.create<TaskListOptions>({
|
2020-11-05 21:51:34 +08:00
|
|
|
name: 'taskList',
|
2020-10-30 20:19:06 +08:00
|
|
|
|
2021-10-27 00:31:13 +08:00
|
|
|
addOptions() {
|
|
|
|
return {
|
2021-12-11 07:13:24 +08:00
|
|
|
itemTypeName: 'taskItem',
|
2021-10-27 00:31:13 +08:00
|
|
|
HTMLAttributes: {},
|
|
|
|
}
|
2020-11-13 23:44:22 +08:00
|
|
|
},
|
|
|
|
|
2020-11-02 22:18:03 +08:00
|
|
|
group: 'block list',
|
2020-10-30 20:19:06 +08:00
|
|
|
|
2021-12-11 07:13:24 +08:00
|
|
|
content() {
|
|
|
|
return `${this.options.itemTypeName}+`
|
|
|
|
},
|
2020-10-30 20:19:06 +08:00
|
|
|
|
|
|
|
parseHTML() {
|
|
|
|
return [
|
|
|
|
{
|
2021-12-02 21:56:57 +08:00
|
|
|
tag: `ul[data-type="${this.name}"]`,
|
2020-10-30 20:19:06 +08:00
|
|
|
priority: 51,
|
|
|
|
},
|
|
|
|
]
|
|
|
|
},
|
|
|
|
|
2020-11-13 23:07:20 +08:00
|
|
|
renderHTML({ HTMLAttributes }) {
|
2021-12-02 21:56:57 +08:00
|
|
|
return ['ul', mergeAttributes(this.options.HTMLAttributes, HTMLAttributes, { 'data-type': this.name }), 0]
|
2020-10-30 20:19:06 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
addCommands() {
|
|
|
|
return {
|
2021-02-10 16:59:35 +08:00
|
|
|
toggleTaskList: () => ({ commands }) => {
|
2021-12-11 07:13:24 +08:00
|
|
|
return commands.toggleList(this.name, this.options.itemTypeName)
|
2020-10-30 20:19:06 +08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
2020-11-19 08:16:10 +08:00
|
|
|
|
|
|
|
addKeyboardShortcuts() {
|
|
|
|
return {
|
2021-03-25 03:39:56 +08:00
|
|
|
'Mod-Shift-9': () => this.editor.commands.toggleTaskList(),
|
2020-11-19 08:16:10 +08:00
|
|
|
}
|
|
|
|
},
|
2020-10-30 20:19:06 +08:00
|
|
|
})
|