ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
[TOC] ## getBounds <!-- Retrieves the pixel position (relative to the editor container) and dimensions of a selection at a given location. The user's current selection need not be at that index. Useful for calculating where to place tooltips. --> 返回给定选区相对于编辑器容器的的像素位置和尺寸。用户的当前选区不需要在索引下。用于计算提示框的位置。 **方法** ```javascript getBounds(index: Number, length: Number = 0): { left: Number, top: Number, height: Number, width: Number } ``` **示例** ```javascript quill.setText('Hello\nWorld\n'); quill.getBounds(7); // Returns { height: 15, width: 0, left: 27, top: 31 } ``` ## getSelection <!-- Retrieves the user's selection range, optionally to focus the editor first. Otherwise `null` may be returned if editor does not have focus. --> 计算用户选择的范围,可选是否要求焦点在编辑器上(如果focus为true,且焦点会一直在编辑器内,且不改变选区)。如果焦点不在编辑器上,将会返回null。 **方法** ```javascript getSelection(focus = false): { index: Number, length: Number } ``` **示例** ```javascript 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 <!-- Sets user selection to given range, which will also focus the editor. Providing `null` as the selection range will blur the editor. [Source](/docs/api/#events) may be `"user"`, `"api"`, or `"silent"`. --> 给定范围设置用户的选区,同时焦点在编辑器上。作为选区传入 `null`,将会使编辑器失去焦点。[Source](事件events.md) 可能是 `"user"`, `"api"`,或者 `"silent"`. **方法** ```javascript setSelection(index: Number, length: Number, source: String = 'api') setSelection(range: { index: Number, length: Number }, souce: String = 'api') ``` **示例** ```javascript quill.setSelection(0, 5); ```