# selection

## getBounds

检索给定位置的像素位置（相对于编辑器容器）和选区的尺寸。用户的当前选择不需要在该索引处。用于计算工具提示的放置位置。

**Methods**

```
getBounds(index: Number, length: Number = 0):
  { left: Number, top: Number, height: Number, width: Number }
```

**Examples**

```
quill.setText('Hello\nWorld\n');
quill.getBounds(7);  // 返回值 { height: 15, width: 0, left: 27, top: 31 }
```

## getSelection

检索用户的选择范围，可选地首先关注编辑器。除此之外，如果编辑没有焦点，可能会返回一个`null`。

**Methods**

```
getSelection(focus = false): { index: Number, length: Number }
```

**Examples**

```
var range = quill.getSelection();
if (range) {
  if (range.length == 0) {
    console.log('User cursor is at index', range.index);
  } else {
    var text = quill.getText(range.index, range.length);
    console.log('User has highlighted: ', text);
  }
} else {
  console.log('User cursor is not in editor');
}
```

## setSelection

将用户的选择设置为给定的范围，这个主要用在编辑器上。提供`null`作为选择范围将模糊编辑器。来源可能是‘user’、‘api’或者‘silent’。

**Methods**

```
setSelection(index: Number, length: Number, source: String = 'api')
setSelection(range: { index: Number, length: Number },
             souce: String = 'api')
```

**Examples**

```
quill.setSelection(0, 5);
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://kang-bing-kui.gitbook.io/quill/wen-dang-document/api/selection.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
