使用 TipTap 版本 2,如何判断是否选择了整个段落而不是段落中的部分文本?

问题描述 投票:0回答:1

我正在尝试 TipTap v2,并尝试在用户选择某些文本时显示

BubbleMenu
。 我想模仿中型编辑器的工作方式,因为当用户选择整个段落时,气泡菜单会显示用于设置/取消设置标题的选项,但如果他们仅选择部分文本,则菜单会显示文本样式格式,例如如粗体、斜体等

我不知道如何判断用户是否选择了一个段落或只是一些文本?

我编写了一个简单的函数来检查用户是否选择了某些文本。 这有效:

const isTextSelection = (editor: Editor) => {
  if (!editor) return false
  const { state } = editor
  const { from, to } = state.selection
   return from !== to
}

但尝试检查所选文本是否为段落是行不通的

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
}

判断用户是否选择了段落的推荐方法是什么?

javascript reactjs typescript tiptap
1个回答
0
投票

你可以,我没有尝试过,检查“from”= 0和“to”是否等于范围的长度...

© www.soinside.com 2019 - 2024. All rights reserved.