import { TextField } from "./types" ; type TextFieldWithSelectionRange = TextField & { selectionRange ? : [ number , number ] ; } ; function setSelectionRangeOnSelect ( event ) { const { target } = event ; if ( target instanceof HTMLInputElement || target instanceof HTMLTextAreaElement ) { const textField = target as TextFieldWithSelectionRange ; const start = textField . selectionStart || 0 ; const end = textField . selectionEnd || start ; setSelectionRange ( textField , start , end ) ; } } export function getSelectionRange ( element ) { const { selectionRange , selectionStart , selectionEnd , value , } = element as TextFieldWithSelectionRange ; if ( selectionRange ) { return selectionRange ; } const start = selectionStart || value . length ; const end = selectionEnd || start ; return [ start , end ] as const ; } export function setSelectionRange ( element , start , end = start ) { const textField = element as TextFieldWithSelectionRange ; textField . selectionRange = [ start , end ] ; textField . addEventListener ( "select" , setSelectionRangeOnSelect ) ; }