textarea-selection-bounds
Version:
A handy package to get the bounds of the current text selection in a textarea element
41 lines (40 loc) • 1.81 kB
TypeScript
import { Options, SelectionBounds, TextElement, TextSelection } from './types.js';
export declare class TextareaSelectionBounds {
/**
* Creates a new instance of TextareaSelectionBounds.
* @param textElement The textarea or input element to get the selection bounds for.
* @param options The options to use.
*/
constructor(textElement: TextElement, options?: Partial<Options>);
private get window();
/**
* Deletes the style cache. Call this is the textElement style has changed (e.g. font size, padding, etc.)
*/
deleteStyleCache(): void;
/**
* Returns the current selection bounds.
* @returns The current selection bounds.
* @example
* const bounds = textareaSelectionBounds.getCurrentSelection();
* console.log(bounds);
* // { from: 0, to: 5 }
*/
getCurrentSelection(): TextSelection;
/**
* Returns the bounds of the selection.
* @param selection The selection to get the bounds for. If not provided, the current selection will be used. If 'full' is provided, it is assumed that all text is selected.
* @returns The bounds of the selection, a changed flag, and the selected text.
* @example
* const bounds = textareaSelectionBounds.getBounds();
* console.log(bounds);
* // { top: 10, left: 20, width: 30, height: 40, changed: true, text: 'Hello' }
*/
getBounds(selection?: TextSelection | 'full'): SelectionBounds;
/**
* Returns the bounding client rect of the selection.
* @param selection The selection to get the bounding client rect for. If not provided, the current selection will be used.
* @returns The bounding client rect of the selection.
*/
getBoundingClientRect(selection?: TextSelection): DOMRect;
private pxToNumber;
}