textarea-selection-bounds
Version:
A handy package to get the bounds of the current text selection in a textarea element
55 lines (54 loc) • 2.33 kB
TypeScript
import { Options, SelectionBounds, TextElement, TextSelection } from './types.js';
export declare class TextareaSelectionBounds {
private _scrollTop;
private _scrollLeft;
private _disposed;
private _scrollEventListener;
private _mutationObserver;
/**
* 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>);
/**
* Disposes the instance by removing event listeners, disconnecting the MutationObserver,
* and clearing references to DOM elements and options. Call this when the text element is
* being destroyed to prevent memory leaks.
*
* After calling dispose(), the instance should not be used anymore.
*/
dispose(): void;
private get window();
private getComputedStyle;
/**
* 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;
}