@@ -427,6 +427,47 @@ export const use_handlers = (
427427 update_value ( new_value , raw_start )
428428 }
429429
430+ const delete_selection_if_any = ( ) => {
431+ const selection = window . getSelection ( )
432+ if (
433+ selection &&
434+ selection . rangeCount > 0 &&
435+ ! selection . isCollapsed &&
436+ params . input_ref . current
437+ ) {
438+ const range = selection . getRangeAt ( 0 )
439+ const input_element = params . input_ref . current
440+
441+ if ( input_element . contains ( range . startContainer ) ) {
442+ const pre_selection_range = document . createRange ( )
443+ pre_selection_range . selectNodeContents ( input_element )
444+ pre_selection_range . setEnd ( range . startContainer , range . startOffset )
445+ const display_start = pre_selection_range . toString ( ) . length
446+
447+ pre_selection_range . setEnd ( range . endContainer , range . endOffset )
448+ const display_end = pre_selection_range . toString ( ) . length
449+
450+ const raw_start = map_display_pos_to_raw_pos ( {
451+ display_pos : display_start ,
452+ raw_text : props . value ,
453+ context_file_paths : props . context_file_paths ?? [ ]
454+ } )
455+ const raw_end = map_display_pos_to_raw_pos ( {
456+ display_pos : display_end ,
457+ raw_text : props . value ,
458+ context_file_paths : props . context_file_paths ?? [ ]
459+ } )
460+
461+ const new_value =
462+ props . value . substring ( 0 , raw_start ) + props . value . substring ( raw_end )
463+
464+ update_value ( new_value , raw_start )
465+ props . on_caret_position_change ( raw_start )
466+ raw_caret_pos_ref . current = raw_start
467+ }
468+ }
469+ }
470+
430471 const perform_paste = ( text : string ) => {
431472 const selection = window . getSelection ( )
432473 if ( ! selection || ! selection . rangeCount || ! params . input_ref . current ) return
@@ -489,6 +530,7 @@ export const use_handlers = (
489530 if ( e . clipboardData . files && e . clipboardData . files . length > 0 ) {
490531 const file = e . clipboardData . files [ 0 ]
491532 if ( file . type . startsWith ( 'image/' ) ) {
533+ delete_selection_if_any ( )
492534 const reader = new FileReader ( )
493535 reader . onload = ( event ) => {
494536 if ( event . target ?. result ) {
@@ -511,6 +553,7 @@ export const use_handlers = (
511553 ! is_shift_pressed_ref . current &&
512554 / ^ h t t p s ? : \/ \/ [ ^ \s ] + $ / . test ( text . trim ( ) )
513555 ) {
556+ delete_selection_if_any ( )
514557 props . on_paste_url ( text . trim ( ) )
515558 return
516559 }
@@ -525,44 +568,7 @@ export const use_handlers = (
525568 text . includes ( '\n' ) &&
526569 ! is_fragment_paste
527570 ) {
528- const selection = window . getSelection ( )
529- if (
530- selection &&
531- selection . rangeCount > 0 &&
532- ! selection . isCollapsed &&
533- params . input_ref . current
534- ) {
535- const range = selection . getRangeAt ( 0 )
536- const input_element = params . input_ref . current
537-
538- if ( input_element . contains ( range . startContainer ) ) {
539- const pre_selection_range = document . createRange ( )
540- pre_selection_range . selectNodeContents ( input_element )
541- pre_selection_range . setEnd ( range . startContainer , range . startOffset )
542- const display_start = pre_selection_range . toString ( ) . length
543-
544- pre_selection_range . setEnd ( range . endContainer , range . endOffset )
545- const display_end = pre_selection_range . toString ( ) . length
546-
547- const raw_start = map_display_pos_to_raw_pos ( {
548- display_pos : display_start ,
549- raw_text : props . value ,
550- context_file_paths : props . context_file_paths ?? [ ]
551- } )
552- const raw_end = map_display_pos_to_raw_pos ( {
553- display_pos : display_end ,
554- raw_text : props . value ,
555- context_file_paths : props . context_file_paths ?? [ ]
556- } )
557-
558- const new_value =
559- props . value . substring ( 0 , raw_start ) + props . value . substring ( raw_end )
560-
561- update_value ( new_value , raw_start )
562- props . on_caret_position_change ( raw_start )
563- raw_caret_pos_ref . current = raw_start
564- }
565- }
571+ delete_selection_if_any ( )
566572 props . on_paste_pasted_text ( text )
567573 return
568574 }
0 commit comments