@@ -19,7 +19,6 @@ import { calculateNewCursorPosition } from '../utils/word-wrap-utils'
1919import type { InputValue } from '../state/chat-store'
2020import type {
2121 KeyEvent ,
22- PasteEvent ,
2322 ScrollBoxRenderable ,
2423 TextBufferView ,
2524 TextRenderable ,
@@ -94,6 +93,7 @@ interface MultilineInputProps {
9493 onChange : ( value : InputValue ) => void
9594 onSubmit : ( ) => void
9695 onKeyIntercept ?: ( key : KeyEvent ) => boolean
96+ onPaste : ( fallbackText ?: string ) => void
9797 placeholder ?: string
9898 focused ?: boolean
9999 shouldBlinkCursor ?: boolean
@@ -115,6 +115,7 @@ export const MultilineInput = forwardRef<
115115 value,
116116 onChange,
117117 onSubmit,
118+ onPaste,
118119 placeholder = '' ,
119120 focused = true ,
120121 shouldBlinkCursor,
@@ -134,9 +135,6 @@ export const MultilineInput = forwardRef<
134135 const [ measuredCols , setMeasuredCols ] = useState < number | null > ( null )
135136 const [ lastActivity , setLastActivity ] = useState ( Date . now ( ) )
136137
137- // Refs to track latest values for paste handler (prevents stale closure issues)
138- const valueRef = useRef ( value )
139- const cursorPositionRef = useRef ( cursorPosition )
140138 const stickyColumnRef = useRef < number | null > ( null )
141139
142140 // Helper to get or set the sticky column for vertical navigation
@@ -146,27 +144,19 @@ export const MultilineInput = forwardRef<
146144 return stickyColumnRef . current
147145 }
148146 const lineIndex = lineStarts . findLastIndex (
149- ( lineStart ) => lineStart <= cursorPositionRef . current ,
147+ ( lineStart ) => lineStart <= cursorPosition ,
150148 )
151149 // Account for cursorIsChar offset like cursorDown does
152150 const column =
153151 lineIndex === - 1
154152 ? 0
155- : cursorPositionRef . current -
156- lineStarts [ lineIndex ] +
157- ( cursorIsChar ? - 1 : 0 )
153+ : cursorPosition - lineStarts [ lineIndex ] + ( cursorIsChar ? - 1 : 0 )
158154 stickyColumnRef . current = Math . max ( 0 , column )
159155 return stickyColumnRef . current
160156 } ,
161157 [ ] ,
162158 )
163159
164- // Keep refs in sync with props
165- useEffect ( ( ) => {
166- valueRef . current = value
167- cursorPositionRef . current = cursorPosition
168- } , [ value , cursorPosition ] )
169-
170160 // Update last activity on value or cursor changes
171161 useEffect ( ( ) => {
172162 setLastActivity ( Date . now ( ) )
@@ -194,35 +184,6 @@ export const MultilineInput = forwardRef<
194184 [ ] ,
195185 )
196186
197- const handlePaste = useCallback (
198- ( event : PasteEvent ) => {
199- if ( ! focused ) return
200-
201- const text = event . text ?? ''
202- if ( ! text ) return
203-
204- // Use refs to get the latest values, avoiding stale closure issues
205- // when multiple paste events fire rapidly before React re-renders
206- const currentValue = valueRef . current
207- const currentCursor = cursorPositionRef . current
208-
209- const newValue =
210- currentValue . slice ( 0 , currentCursor ) + text + currentValue . slice ( currentCursor )
211- const newCursor = currentCursor + text . length
212-
213- // Update refs immediately so subsequent rapid events see the new state
214- valueRef . current = newValue
215- cursorPositionRef . current = newCursor
216-
217- onChange ( {
218- text : newValue ,
219- cursorPosition : newCursor ,
220- lastEditDueToNav : false ,
221- } )
222- } ,
223- [ focused , onChange ] ,
224- )
225-
226187 const cursorRow = lineInfo
227188 ? Math . max (
228189 0 ,
@@ -727,7 +688,10 @@ export const MultilineInput = forwardRef<
727688 preventKeyDefault ( key )
728689
729690 const lineStarts = lineInfo ?. lineStarts ?? [ ]
730- const desiredIndex = getOrSetStickyColumn ( lineStarts , ! shouldHighlight )
691+ const desiredIndex = getOrSetStickyColumn (
692+ lineStarts ,
693+ ! shouldHighlight ,
694+ )
731695
732696 onChange ( {
733697 text : value ,
@@ -746,7 +710,10 @@ export const MultilineInput = forwardRef<
746710 // Down arrow (no modifiers)
747711 if ( key . name === 'down' && ! key . ctrl && ! key . meta && ! key . option ) {
748712 const lineStarts = lineInfo ?. lineStarts ?? [ ]
749- const desiredIndex = getOrSetStickyColumn ( lineStarts , ! shouldHighlight )
713+ const desiredIndex = getOrSetStickyColumn (
714+ lineStarts ,
715+ ! shouldHighlight ,
716+ )
750717
751718 onChange ( {
752719 text : value ,
@@ -844,7 +811,7 @@ export const MultilineInput = forwardRef<
844811 stickyScroll = { true }
845812 stickyStart = "bottom"
846813 scrollbarOptions = { { visible : false } }
847- onPaste = { handlePaste }
814+ onPaste = { ( event ) => onPaste ( event . text ) }
848815 style = { {
849816 flexGrow : 0 ,
850817 flexShrink : 0 ,
0 commit comments