Skip to content

useTextSelection

类别
导出体积
755 B
上次更改
2 months ago

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

示例

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

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

所选矩形区域:

[]

用法

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

类型声明

ts
export interface UseTextSelectionOptions extends ConfigurableWindow {}
export interface UseTextSelectionReturn {
  
text
:
ComputedRef
<string>
rects
:
ComputedRef
<DOMRect[]>
ranges
:
ComputedRef
<Range[]>
selection
:
ShallowRef
<Selection | null>
} /** * 基于 [`Window.getSelection`](https://developer.mozilla.org/en-US/docs/Web/API/Window/getSelection) 响应式跟踪用户文本选择。 * * @see https://vueuse.org/useTextSelection * * @__NO_SIDE_EFFECTS__ */ export declare function
useTextSelection
(
options
?: UseTextSelectionOptions,
): UseTextSelectionReturn

源码

源码演示文档

贡献者

Anthony Fu
一纸忘忧
Anthony Fu
Vida Xie
Kevin Luo
James Garbutt
SerKo
Fernando Fernández
Lehoczky Zoltán
wheat
Jelf
webfansplz

更新日志

dbb3e - feat: Set initial value for use text selection (#5092)
d32f8 - refactor: add @__NO_SIDE_EFFECTS__ annotations to all pure functions (#4907)
dd316 - feat: use passive event handlers everywhere is possible (#4477)
0a9ed - feat!: drop Vue 2 support, optimize bundles and clean up (#4349)