@@ -68,6 +68,11 @@ interface NativeComposerEditorRef {
6868 focus : ( ) => Promise < void > ;
6969 blur : ( ) => Promise < void > ;
7070 setSelection : ( start : number , end : number ) => Promise < void > ;
71+ readState : ( ) => Promise < {
72+ readonly value : string ;
73+ readonly selection : ComposerEditorSelection ;
74+ readonly eventCount : number ;
75+ } > ;
7176}
7277
7378interface NativeComposerEditorProps extends ViewProps {
@@ -163,17 +168,6 @@ export function ComposerEditor({
163168 const theme = useUniwindTheme ( ) ;
164169 const fontFamily = FONT_FAMILIES . regular ;
165170
166- useImperativeHandle (
167- ref ,
168- ( ) => ( {
169- focus : ( ) => settleViewCommand ( nativeRef . current ?. focus ( ) ) ,
170- blur : ( ) => settleViewCommand ( nativeRef . current ?. blur ( ) ) ,
171- setSelection : ( nextSelection ) =>
172- settleViewCommand ( nativeRef . current ?. setSelection ( nextSelection . start , nextSelection . end ) ) ,
173- } ) ,
174- [ ] ,
175- ) ;
176-
177171 const skillLabels = useMemo ( ( ) => composerSkillLabels ( skills ) , [ skills ] ) ;
178172 const chipTextColor = createNativeComposerTheme ( theme ) . text ;
179173 const tokensJson = useMemo ( ( ) => {
@@ -280,6 +274,35 @@ export function ComposerEditor({
280274 } ,
281275 [ nativeEventSnapshotsRef ] ,
282276 ) ;
277+ const applyNativeChange = (
278+ eventCount : number ,
279+ value : string ,
280+ nextSelection : ComposerEditorSelection ,
281+ ) => {
282+ const acknowledgedEventCount = acceptNativeEvent ( eventCount , value , nextSelection ) ;
283+ if ( acknowledgedEventCount === false ) return ;
284+ onChangeText ( value ) ;
285+ onSelectionChange ?.( nextSelection ) ;
286+ setMostRecentEventCount ( acknowledgedEventCount ) ;
287+ forceNativeEventRender ( ( sequence ) => sequence + 1 ) ;
288+ } ;
289+ useImperativeHandle ( ref , ( ) => ( {
290+ focus : ( ) => settleViewCommand ( nativeRef . current ?. focus ( ) ) ,
291+ blur : ( ) => settleViewCommand ( nativeRef . current ?. blur ( ) ) ,
292+ setSelection : ( nextSelection ) =>
293+ settleViewCommand ( nativeRef . current ?. setSelection ( nextSelection . start , nextSelection . end ) ) ,
294+ // Tapping outside the editor makes iOS accept a pending autocorrection, and that edit's
295+ // event can reach JS after the tap's handler has already read the draft.
296+ flushPendingInput : async ( ) => {
297+ const state = await Promise . resolve ( )
298+ . then ( ( ) => nativeRef . current ?. readState ( ) )
299+ . catch ( ( ) => undefined ) ;
300+ // Paste events advance the count without changing text; consuming one here would drop it.
301+ if ( state && state . value !== props . value ) {
302+ applyNativeChange ( state . eventCount , state . value , state . selection ) ;
303+ }
304+ } ,
305+ } ) ) ;
283306 const themeJson = JSON . stringify ( createNativeComposerTheme ( theme ) ) ;
284307 const resolvedTextStyle = StyleSheet . flatten ( textStyle ) ?? { } ;
285308 return (
@@ -315,18 +338,13 @@ export function ComposerEditor({
315338 textPasteThresholdBytes = { onPasteText ? PASTED_TEXT_ATTACHMENT_THRESHOLD_BYTES : 0 }
316339 maxInputChars = { PROVIDER_SEND_TURN_MAX_INPUT_CHARS }
317340 style = { style as StyleProp < ViewStyle > }
318- onComposerChange = { ( event ) => {
319- const acknowledgedEventCount = acceptNativeEvent (
341+ onComposerChange = { ( event ) =>
342+ applyNativeChange (
320343 event . nativeEvent . eventCount ,
321344 event . nativeEvent . value ,
322345 event . nativeEvent . selection ,
323- ) ;
324- if ( acknowledgedEventCount === false ) return ;
325- onChangeText ( event . nativeEvent . value ) ;
326- onSelectionChange ?.( event . nativeEvent . selection ) ;
327- setMostRecentEventCount ( acknowledgedEventCount ) ;
328- forceNativeEventRender ( ( sequence ) => sequence + 1 ) ;
329- } }
346+ )
347+ }
330348 onComposerSelectionChange = { ( event ) => {
331349 const acknowledgedEventCount = acceptNativeEvent (
332350 event . nativeEvent . eventCount ,
0 commit comments