import type { Editor } from '@tiptap/react' import { TextSelection } from '@tiptap/pm/state' export function selectCurrentBlockContent(editor: Editor) { const { selection, doc } = editor.state if (!selection.empty) return const $pos = selection.$from let blockNode = null let blockPos = -1 for (let depth = $pos.depth; depth >= 0; depth--) { const node = $pos.node(depth) const pos = $pos.start(depth) if (node.isBlock && node.textContent.trim()) { blockNode = node blockPos = pos break } } if (blockNode && blockPos >= 0) { const from = blockPos const to = blockPos + blockNode.nodeSize - 2 // -2 to exclude the closing tag if (from < to) { const $from = doc.resolve(from) const $to = doc.resolve(to) const newSelection = TextSelection.between($from, $to, 1) if (newSelection && !selection.eq(newSelection)) { editor.view.dispatch(editor.state.tr.setSelection(newSelection)) } } } }