useTextSelection

Tags:

一个 React Hook,用于获取文档的选择状态。

演示

源码
Reprehenderit et occaecat proident fugiat voluptate consectetur enim magna!
Selected text:
(Nothing selected)

用法

请查看 API。

源码

点击下方链接跳转 GitHub 查看源代码。

API

const { text, rects, ranges, selectionRef } = useTextSelection()

返回值

export interface UseTextSelectionReturns {
  /**
   * 一个 React Ref 对象,持有当前的 Selection 对象。
   */
  selectionRef: MutableRefObject<Selection | null>
  /**
   * 所选的文本。
   */
  text: string
  /**
   * 每个选定范围的 DOMRect 对象。
   */
  rects: DOMRect[]
  /**
   * 每个选定范围的 Range 对象。
   */
  ranges: Range[]
}