I’m experimenting with TipTap v2 and am trying to show a BubbleMenu
when the user selects some text. I’d like to mimic the way the Medium editor works, in that when the user selects a whole paragraph the bubble menu shows options for setting/unsetting headers, but if they select just some of the text then the menu shows text style formatting such as bold, italic, etc.
What I can’t work out is how to tell if the user has selected a paragraph or just some text?
I’ve written a simple function that checks if the user has selected some text. This works:
const isTextSelection = (editor: Editor) => {
if (!editor) return false
const { state } = editor
const { from, to } = state.selection
return from !== to
}
but trying to check if the selected text is a paragraph isn’t working
const isParagraphSelected = (editor: Editor) => {
if (!editor) return false
const { state } = editor
const { from, to } = state.selection
const node = state.doc.nodeAt(from)
return node?.type.name === 'paragraph' && from !== to
}
What is the recommended way to tell if the user has selected a paragraph?