import type { Editor } from '@tiptap/react' import { TextSelection } from '@tiptap/pm/state' // Removes the block that holds the cursor. The last block is cleared to an empty paragraph // instead, so the document never loses its only line. export function deleteTopLevelBlock(editor: Editor): boolean { const { state, view } = editor const { $from } = state.selection if ($from.depth < 1) return false const block = state.doc.child($from.index(0)) const blockStart = $from.before(1) const paragraphType = state.schema.nodes.paragraph const tr = state.doc.childCount === 1 && paragraphType ? state.tr.replaceWith(blockStart, blockStart + block.nodeSize, paragraphType.create()) : state.tr.delete(blockStart, blockStart + block.nodeSize) const nextPosition = Math.min(blockStart, tr.doc.content.size) tr.setSelection(TextSelection.near(tr.doc.resolve(nextPosition))) view.dispatch(tr.scrollIntoView()) return true }