import type { Editor } from '@tiptap/core' import { posToDOMRect } from '@tiptap/core' import { TextSelection } from '@tiptap/pm/state' // The rect the selection toolbar anchors to: the selected text, measured from the nearest text // positions inside the selection. A keyboard select-all is an `AllSelection` whose ends sit on // the document's block boundaries, which Tiptap measured as the full-width block, so the toolbar // floated in the middle of the editor instead of over the text a mouse drag would select. export function getSelectionTextRect(editor: Editor): DOMRect { const { doc, selection } = editor.state const textRange = TextSelection.between(doc.resolve(selection.from), doc.resolve(selection.to)) return posToDOMRect(editor.view, textRange.from, textRange.to) }