File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11/* @flow strict */
22
33export function insertText ( textarea : HTMLInputElement | HTMLTextAreaElement , text : string ) : void {
4- const point = textarea . selectionEnd
5- const beginning = textarea . value . substring ( 0 , point )
6- const remaining = textarea . value . substring ( point )
7- const newline = textarea . value === '' || beginning . match ( / \n $ / ) ? '' : '\n'
4+ const beginning = textarea . value . substring ( 0 , textarea . selectionStart )
5+ const remaining = textarea . value . substring ( textarea . selectionEnd )
86
9- textarea . value = beginning + newline + text + remaining
10- textarea . selectionStart = point + text . length
11- textarea . selectionEnd = point + text . length
7+ const newline = beginning . length === 0 || beginning . match ( / \n $ / ) ? '' : '\n'
8+ const textBeforeCursor = beginning + newline + text
9+
10+ textarea . value = textBeforeCursor + remaining
11+ textarea . selectionStart = textBeforeCursor . length
12+ textarea . selectionEnd = textarea . selectionStart
1213
1314 textarea . dispatchEvent (
1415 new CustomEvent ( 'change' , {
You can’t perform that action at this time.
0 commit comments