@@ -427,47 +427,6 @@ 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-
471430 const perform_paste = ( text : string ) => {
472431 const selection = window . getSelection ( )
473432 if ( ! selection || ! selection . rangeCount || ! params . input_ref . current ) return
@@ -530,7 +489,6 @@ export const use_handlers = (
530489 if ( e . clipboardData . files && e . clipboardData . files . length > 0 ) {
531490 const file = e . clipboardData . files [ 0 ]
532491 if ( file . type . startsWith ( 'image/' ) ) {
533- delete_selection_if_any ( )
534492 const reader = new FileReader ( )
535493 reader . onload = ( event ) => {
536494 if ( event . target ?. result ) {
@@ -553,7 +511,6 @@ export const use_handlers = (
553511 ! is_shift_pressed_ref . current &&
554512 / ^ h t t p s ? : \/ \/ [ ^ \s ] + $ / . test ( text . trim ( ) )
555513 ) {
556- delete_selection_if_any ( )
557514 props . on_paste_url ( text . trim ( ) )
558515 return
559516 }
@@ -568,7 +525,44 @@ export const use_handlers = (
568525 text . includes ( '\n' ) &&
569526 ! is_fragment_paste
570527 ) {
571- delete_selection_if_any ( )
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+ }
572566 props . on_paste_pasted_text ( text )
573567 return
574568 }
0 commit comments