import type { Editor } from '@tiptap/react' import { TextSelection } from '@tiptap/pm/state' export function duplicateTopLevelBlock(editor: Editor): boolean { const { state, view } = editor const { $from, from } = state.selection if ($from.depth < 1) return false const block = state.doc.child($from.index(0)) const blockStart = $from.before(1) const insertAt = blockStart + block.nodeSize const tr = state.tr.insert(insertAt, block.copy(block.content)) tr.setSelection(TextSelection.near(tr.doc.resolve(insertAt + (from - blockStart)))) view.dispatch(tr.scrollIntoView()) return true }