@@ -20,6 +20,7 @@ export default function Input({
20
20
// State to track if the IME is composing (i.e., in the middle of Japanese IME input)
21
21
const [ isComposing , setIsComposing ] = useState ( false ) ;
22
22
const textAreaRef = useRef < HTMLTextAreaElement > ( null ) ;
23
+ const [ cursorPos , setCursorPos ] = useState ( 0 ) ;
23
24
24
25
useEffect ( ( ) => {
25
26
if ( textAreaRef . current && ! disabled ) {
@@ -47,6 +48,12 @@ export default function Input({
47
48
setValue ( val ) ;
48
49
} ;
49
50
51
+ const handleCursorUpdate = (
52
+ e : React . KeyboardEvent < HTMLTextAreaElement > | React . MouseEvent < HTMLTextAreaElement >
53
+ ) => {
54
+ setCursorPos ( e . currentTarget . selectionStart ?? 0 ) ;
55
+ } ;
56
+
50
57
// Handlers for composition events, which are crucial for proper IME behavior
51
58
const handleCompositionStart = ( evt : React . CompositionEvent < HTMLTextAreaElement > ) => {
52
59
setIsComposing ( true ) ;
@@ -63,6 +70,7 @@ export default function Input({
63
70
if ( value . trim ( ) ) {
64
71
handleSubmit ( new CustomEvent ( 'submit' , { detail : { value } } ) ) ;
65
72
setValue ( '' ) ;
73
+ setCursorPos ( 0 ) ;
66
74
}
67
75
}
68
76
} ;
@@ -72,13 +80,17 @@ export default function Input({
72
80
if ( value . trim ( ) ) {
73
81
handleSubmit ( new CustomEvent ( 'submit' , { detail : { value } } ) ) ;
74
82
setValue ( '' ) ;
83
+ setCursorPos ( 0 ) ;
75
84
}
76
85
} ;
77
86
78
87
const handleFileSelect = async ( ) => {
79
88
const path = await window . electron . selectFileOrDirectory ( ) ;
80
89
if ( path ) {
81
- setValue ( path ) ;
90
+ const currentValue = value ;
91
+ const newValue = currentValue . slice ( 0 , cursorPos ) + path + currentValue . slice ( cursorPos ) ;
92
+
93
+ setValue ( newValue ) ;
82
94
textAreaRef . current ?. focus ( ) ;
83
95
}
84
96
} ;
@@ -96,6 +108,8 @@ export default function Input({
96
108
onChange = { handleChange }
97
109
onCompositionStart = { handleCompositionStart }
98
110
onCompositionEnd = { handleCompositionEnd }
111
+ onKeyUp = { handleCursorUpdate }
112
+ onClick = { handleCursorUpdate }
99
113
onKeyDown = { handleKeyDown }
100
114
disabled = { disabled }
101
115
ref = { textAreaRef }
0 commit comments