tiptap/packages/extension-list-item/src/list-item.ts

51 lines
1.3 KiB
TypeScript
Raw Normal View History

2023-03-28 20:21:16 +08:00
import {
handleBackspace, handleDelete,
mergeAttributes, Node,
2023-03-28 20:21:16 +08:00
} from '@tiptap/core'
2020-09-11 04:29:15 +08:00
2020-11-13 23:44:22 +08:00
export interface ListItemOptions {
2021-04-21 15:43:31 +08:00
HTMLAttributes: Record<string, any>,
bulletListTypeName: string
orderedListTypeName: string
2020-11-13 23:44:22 +08:00
}
2021-02-11 01:25:08 +08:00
export const ListItem = Node.create<ListItemOptions>({
2020-11-04 03:57:09 +08:00
name: 'listItem',
2020-10-22 18:34:49 +08:00
addOptions() {
return {
HTMLAttributes: {},
bulletListTypeName: 'bulletList',
orderedListTypeName: 'orderedList',
}
2020-11-13 23:44:22 +08:00
},
2020-12-03 22:11:47 +08:00
content: 'paragraph block*',
2020-10-22 18:34:49 +08:00
defining: true,
parseHTML() {
return [
{
tag: 'li',
},
2020-10-22 18:34:49 +08:00
]
},
2020-11-13 23:07:20 +08:00
renderHTML({ HTMLAttributes }) {
2020-11-25 16:50:54 +08:00
return ['li', mergeAttributes(this.options.HTMLAttributes, HTMLAttributes), 0]
2020-10-22 18:34:49 +08:00
},
addKeyboardShortcuts() {
return {
2021-12-02 21:56:57 +08:00
Enter: () => this.editor.commands.splitListItem(this.name),
Tab: () => this.editor.commands.sinkListItem(this.name),
'Shift-Tab': () => this.editor.commands.liftListItem(this.name),
Delete: ({ editor }) => handleDelete(editor, this.name),
'Mod-Delete': ({ editor }) => handleDelete(editor, this.name),
Backspace: ({ editor }) => handleBackspace(editor, this.name, [this.options.bulletListTypeName, this.options.orderedListTypeName]),
'Mod-Backspace': ({ editor }) => handleBackspace(editor, this.name, [this.options.bulletListTypeName, this.options.orderedListTypeName]),
2020-10-22 18:34:49 +08:00
}
},
})