WIP - add handling when backspace is pressed at start of a list item

This commit is contained in:
Dominik Biedebach 2023-03-04 02:17:52 +01:00
parent 0b7120c21b
commit 4ab1bbe64a
3 changed files with 52 additions and 10 deletions

View File

@ -1,9 +1,16 @@
import { RawCommands } from '@tiptap/core'
import { joinPoint } from '@tiptap/pm/transform'
export const joinListItemBackward: RawCommands['splitListItem'] = () => ({
tr, state, dispatch,
}) => {
tr.join(state.selection.$from.pos - 2, 2)
const point = joinPoint(state.doc, state.selection.$from.pos, -1)
if (point === null || point === undefined) {
return false
}
tr.join(point, 2)
if (dispatch) {
dispatch(tr)

View File

@ -1,5 +1,5 @@
import { getNodeType } from '@tiptap/core'
import { NodeType } from '@tiptap/pm/model'
import { Node, NodeType } from '@tiptap/pm/model'
import { EditorState } from '@tiptap/pm/state'
export const findListItemPos = (typeOrName: string | NodeType, state: EditorState) => {
@ -55,11 +55,32 @@ export const hasPreviousListItem = (typeOrName: string, state: EditorState) => {
return false
}
const $item = state.doc.resolve(listItemPos.$pos.pos)
const $prev = state.doc.resolve(listItemPos.$pos.pos - 2)
if (listItemPos.$pos.depth === $prev.depth) {
return true
const prevNode = $prev.node($item.depth)
if (!prevNode) {
return false
}
return false
return prevNode.type.name === typeOrName
}
export const listItemHasSubList = (typeOrName: string, state: EditorState, node?: Node) => {
if (!node) {
return false
}
const nodeType = getNodeType(typeOrName, state.schema)
let hasSubList = false
node.descendants(child => {
if (child.type === nodeType) {
hasSubList = true
}
})
return hasSubList
}

View File

@ -3,7 +3,9 @@ import { NodeType } from '@tiptap/pm/model'
import { joinListItemBackward } from './commands/joinListItemBackward'
import { joinListItemForward } from './commands/joinListItemForward'
import { hasPreviousListItem, isAtStartOfNode, isNodeAtCursor } from './helpers'
import {
findListItemPos, hasPreviousListItem, isAtStartOfNode, isNodeAtCursor, listItemHasSubList,
} from './helpers'
declare module '@tiptap/core' {
interface Commands<ReturnType> {
@ -67,13 +69,25 @@ export const ListItem = Node.create<ListItemOptions>({
return false
}
// check if the current list item has
// a previous list item on the same depth
console.log(hasPreviousListItem(this.name, editor.state))
if (hasPreviousListItem(this.name, editor.state)) {
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) {
return editor.commands.joinListItemBackward(this.name)
}
// otherwise in the end, a backspace should
// always just lift the list item if
// joining / merging is not possible
return editor.chain().liftListItem(this.name).run()
},
}