2022-06-08 20:10:25 +08:00
|
|
|
import { mergeAttributes, Node } from '@tiptap/core'
|
2023-03-04 00:43:29 +08:00
|
|
|
import { NodeType } from '@tiptap/pm/model'
|
|
|
|
|
|
|
|
import { joinListItemBackward } from './commands/joinListItemBackward'
|
|
|
|
import { joinListItemForward } from './commands/joinListItemForward'
|
2023-03-04 09:17:52 +08:00
|
|
|
import {
|
2023-03-23 18:37:03 +08:00
|
|
|
findListItemPos, hasPreviousListItem, isAtStartOfNode, isNodeAtCursor, istAtEndOfNode, listItemHasSubList,
|
2023-03-04 09:17:52 +08:00
|
|
|
} from './helpers'
|
2023-03-04 00:43:29 +08:00
|
|
|
|
|
|
|
declare module '@tiptap/core' {
|
|
|
|
interface Commands<ReturnType> {
|
|
|
|
listItem: {
|
|
|
|
/**
|
|
|
|
* Lift the list item into a wrapping list.
|
|
|
|
*/
|
|
|
|
joinListItemForward: (typeOrName: string | NodeType) => ReturnType
|
|
|
|
joinListItemBackward: (typeOrName: string | NodeType) => ReturnType
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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>,
|
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
|
|
|
|
2021-10-27 00:31:13 +08:00
|
|
|
addOptions() {
|
|
|
|
return {
|
|
|
|
HTMLAttributes: {},
|
|
|
|
}
|
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 [
|
2020-10-30 20:19:06 +08:00
|
|
|
{
|
|
|
|
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
|
|
|
},
|
|
|
|
|
2023-03-04 00:43:29 +08:00
|
|
|
addCommands() {
|
|
|
|
return {
|
|
|
|
joinListItemForward,
|
|
|
|
joinListItemBackward,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
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),
|
2023-03-23 18:37:03 +08:00
|
|
|
Delete: ({ editor }) => {
|
|
|
|
// if the cursor is not inside the current node type
|
|
|
|
// do nothing and proceed
|
|
|
|
if (!isNodeAtCursor(this.name, editor.state)) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// if the cursor is not at the end of a node
|
|
|
|
// do nothing and proceed
|
|
|
|
if (!istAtEndOfNode(editor.state)) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// check if the next node is also a listItem
|
|
|
|
return editor.commands.joinListItemForward(this.name)
|
|
|
|
},
|
2023-03-04 00:43:29 +08:00
|
|
|
Backspace: ({ editor }) => {
|
2023-03-23 18:37:03 +08:00
|
|
|
// this is required to still handle the undo handling
|
2023-03-04 09:40:52 +08:00
|
|
|
if (this.editor.commands.undoInputRule()) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2023-03-23 18:37:03 +08:00
|
|
|
// if the cursor is not inside the current node type
|
|
|
|
// do nothing and proceed
|
2023-03-04 00:43:29 +08:00
|
|
|
if (!isNodeAtCursor(this.name, editor.state)) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2023-03-23 18:37:03 +08:00
|
|
|
// if the cursor is not at the start of a node
|
|
|
|
// do nothing and proceed
|
2023-03-04 00:43:29 +08:00
|
|
|
if (!isAtStartOfNode(editor.state)) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2023-03-04 09:17:52 +08:00
|
|
|
const listItemPos = findListItemPos(this.name, editor.state)
|
|
|
|
|
|
|
|
if (!listItemPos) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
const $prev = editor.state.doc.resolve(listItemPos.$pos.pos - 2)
|
|
|
|
const prevNode = $prev.node(listItemPos.depth)
|
|
|
|
|
|
|
|
const previousListItemHasSubList = listItemHasSubList(this.name, editor.state, prevNode)
|
|
|
|
|
|
|
|
// if the previous item is a list item and doesn't have a sublist, join the list items
|
|
|
|
if (hasPreviousListItem(this.name, editor.state) && !previousListItemHasSubList) {
|
2023-03-04 00:43:29 +08:00
|
|
|
return editor.commands.joinListItemBackward(this.name)
|
|
|
|
}
|
|
|
|
|
2023-03-04 09:17:52 +08:00
|
|
|
// otherwise in the end, a backspace should
|
|
|
|
// always just lift the list item if
|
|
|
|
// joining / merging is not possible
|
2023-03-04 00:43:29 +08:00
|
|
|
return editor.chain().liftListItem(this.name).run()
|
|
|
|
},
|
2020-10-22 18:34:49 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
})
|