Skip to content

useTextSelection

Category
Export Size
681 B
Last Changed
15 minutes ago

基于 Window.getSelection 响应式地跟踪用户的文本选择。

Demo

您可以在页面上选择任何文本。

所选文本:未选择任何内容

所选矩形区域:

[]

用法

vue
<script setup lang="ts">
import { useTextSelection } from '@vueuse/core'
const state = useTextSelection()
</script>

<template>
  <p>{{ state.text }}</p>
</template>

类型声明

显示类型声明
typescript
/**
 * 基于 [`Window.getSelection`](https://developer.mozilla.org/en-US/docs/Web/API/Window/getSelection) 响应式跟踪用户文本选择。
 *
 * @see https://vueuse.org/useTextSelection
 */
export declare function useTextSelection(options?: ConfigurableWindow): {
  text: ComputedRef<string>
  rects: ComputedRef<DOMRect[]>
  ranges: ComputedRef<Range[]>
  selection: Ref<
    {
      readonly anchorNode: Node | null
      readonly anchorOffset: number
      readonly direction: string
      readonly focusNode: Node | null
      readonly focusOffset: number
      readonly isCollapsed: boolean
      readonly rangeCount: number
      readonly type: string
      addRange: (range: Range) => void
      collapse: (node: Node | null, offset?: number) => void
      collapseToEnd: () => void
      collapseToStart: () => void
      containsNode: (node: Node, allowPartialContainment?: boolean) => boolean
      deleteFromDocument: () => void
      empty: () => void
      extend: (node: Node, offset?: number) => void
      getRangeAt: (index: number) => Range
      modify: (alter?: string, direction?: string, granularity?: string) => void
      removeAllRanges: () => void
      removeRange: (range: Range) => void
      selectAllChildren: (node: Node) => void
      setBaseAndExtent: (
        anchorNode: Node,
        anchorOffset: number,
        focusNode: Node,
        focusOffset: number,
      ) => void
      setPosition: (node: Node | null, offset?: number) => void
      toString: () => string
    } | null,
    | Selection
    | {
        readonly anchorNode: Node | null
        readonly anchorOffset: number
        readonly direction: string
        readonly focusNode: Node | null
        readonly focusOffset: number
        readonly isCollapsed: boolean
        readonly rangeCount: number
        readonly type: string
        addRange: (range: Range) => void
        collapse: (node: Node | null, offset?: number) => void
        collapseToEnd: () => void
        collapseToStart: () => void
        containsNode: (node: Node, allowPartialContainment?: boolean) => boolean
        deleteFromDocument: () => void
        empty: () => void
        extend: (node: Node, offset?: number) => void
        getRangeAt: (index: number) => Range
        modify: (
          alter?: string,
          direction?: string,
          granularity?: string,
        ) => void
        removeAllRanges: () => void
        removeRange: (range: Range) => void
        selectAllChildren: (node: Node) => void
        setBaseAndExtent: (
          anchorNode: Node,
          anchorOffset: number,
          focusNode: Node,
          focusOffset: number,
        ) => void
        setPosition: (node: Node | null, offset?: number) => void
        toString: () => string
      }
    | null
  >
}
export type UseTextSelectionReturn = ReturnType<typeof useTextSelection>

Source

SourceDemoDocs

贡献者

Anthony Fu
丶远方
Anthony Fu
Lehoczky Zoltán
wheat
Jelf
webfansplz

变更日志

No recent changes

Released under the MIT License.