import type { Editor } from '@tiptap/react' import { TextSelection } from '@tiptap/pm/state' // Puts the cursor on an empty paragraph at the end of the document, adding one unless the last // block already is one, so the phone focus view opens ready to keep writing. export function appendTrailingParagraph(editor: Editor): void { const { state, view } = editor const paragraphType = state.schema.nodes.paragraph if (!paragraphType) return const lastBlock = state.doc.lastChild const tr = state.tr if (!(lastBlock?.type === paragraphType && lastBlock.content.size === 0)) { tr.insert(state.doc.content.size, paragraphType.create()) } tr.setSelection(TextSelection.atEnd(tr.doc)) view.dispatch(tr.scrollIntoView()) }