@@ -18,8 +18,7 @@ const insertAtCaret = (target: HTMLElement, text: string) => {
18
18
// Move caret to the end of the newly inserted text node.
19
19
range . setStart ( node , node . length ) ;
20
20
range . setEnd ( node , node . length ) ;
21
- selection . removeAllRanges ( ) ;
22
- selection . addRange ( range ) ;
21
+ selection . setBaseAndExtent ( range . startContainer , range . startOffset , range . endContainer , range . endOffset ) ;
23
22
24
23
// Dispatch paste event to simulate real browser behavior
25
24
target . dispatchEvent ( new Event ( 'paste' , { bubbles : true } ) ) ;
@@ -30,7 +29,7 @@ const insertAtCaret = (target: HTMLElement, text: string) => {
30
29
}
31
30
} ;
32
31
33
- const useHtmlPaste : UseHtmlPaste = ( textInputRef , preHtmlPasteCallback , removeListenerOnScreenBlur = false ) => {
32
+ const useHtmlPaste : UseHtmlPaste = ( textInputRef , preHtmlPasteCallback , removeListenerOnScreenBlur = false , shouldRefocusAfterPaste = true ) => {
34
33
const navigation = useNavigation ( ) ;
35
34
36
35
/**
@@ -47,7 +46,11 @@ const useHtmlPaste: UseHtmlPaste = (textInputRef, preHtmlPasteCallback, removeLi
47
46
}
48
47
49
48
// Pointer will go out of sight when a large paragraph is pasted on the web. Refocusing the input keeps the cursor in view.
50
- textInputRef . current ?. blur ( ) ;
49
+ // If shouldRefocusAfterPaste = false, we want to call `focus()` only as it won't change the focus state of the input since it is already focused
50
+ if ( shouldRefocusAfterPaste ) {
51
+ textInputRef . current ?. blur ( ) ;
52
+ }
53
+
51
54
textInputRef . current ?. focus ( ) ;
52
55
// eslint-disable-next-line no-empty
53
56
} catch ( e ) { }
0 commit comments