@@ -24,6 +24,7 @@ private struct ComposerControlledDocumentPayload: Decodable {
2424 let tokensJson : String
2525 let mostRecentEventCount : Int
2626 let isNativeEcho : Bool
27+ let resetGeneration : Int
2728}
2829
2930private struct ComposerThemePayload : Decodable {
@@ -444,6 +445,7 @@ public final class T3ComposerEditorView: ExpoView, UITextViewDelegate, UITextDro
444445 private var isReadOnly = false
445446 private var isApplyingControlledValue = false
446447 private var nativeEventCount = 0
448+ private var resetGeneration = 0
447449 private var lastContentSize = CGSize . zero
448450 private var iconImages : [ String : UIImage ] = [ : ]
449451 private var pendingIconUris = Set < String > ( )
@@ -489,6 +491,7 @@ public final class T3ComposerEditorView: ExpoView, UITextViewDelegate, UITextDro
489491 var payload : [ String : Any ] = context
490492 payload [ " value " ] = self . textView . serializedText ( )
491493 payload [ " eventCount " ] = self . nativeEventCount
494+ payload [ " resetGeneration " ] = self . resetGeneration
492495 payload [ " selection " ] = [ " start " : selection . start , " end " : selection . end ]
493496 self . onComposerPasteContext ( payload )
494497 }
@@ -499,6 +502,7 @@ public final class T3ComposerEditorView: ExpoView, UITextViewDelegate, UITextDro
499502 self . onComposerPasteText ( [
500503 " value " : self . textView . serializedText ( ) ,
501504 " eventCount " : self . nativeEventCount ,
505+ " resetGeneration " : self . resetGeneration ,
502506 " text " : text ,
503507 " selection " : [ " start " : selection . start , " end " : selection . end ] ,
504508 ] )
@@ -651,11 +655,21 @@ public final class T3ComposerEditorView: ExpoView, UITextViewDelegate, UITextDro
651655 }
652656
653657 func setControlledDocumentJson ( _ documentJson : String ) {
654- guard let document = decode ( ComposerControlledDocumentPayload . self , from : documentJson ) ,
655- document . mostRecentEventCount >= nativeEventCount else {
658+ guard let document = decode ( ComposerControlledDocumentPayload . self , from : documentJson ) else {
656659 return
657660 }
658- if document . isNativeEcho && textView . serializedText ( ) != document . value {
661+ // A parent reset (a send clear) wins over native edits it could not see; JS drops their events
662+ // by generation, so the revision restarts where the parent last acknowledged.
663+ let isReset = document . resetGeneration > resetGeneration
664+ guard isReset || document . mostRecentEventCount >= nativeEventCount else {
665+ return
666+ }
667+ if isReset {
668+ resetGeneration = document . resetGeneration
669+ nativeEventCount = document . mostRecentEventCount
670+ endComposition ( )
671+ }
672+ if document . isNativeEcho && ! isReset && textView . serializedText ( ) != document . value {
659673 return
660674 }
661675 if tokensJson != document . tokensJson {
@@ -900,6 +914,15 @@ public final class T3ComposerEditorView: ExpoView, UITextViewDelegate, UITextDro
900914 emitContentSizeIfNeeded ( )
901915 }
902916
917+ private func endComposition ( ) {
918+ guard textView . markedTextRange != nil else {
919+ return
920+ }
921+ isApplyingControlledValue = true
922+ textView . unmarkText ( )
923+ isApplyingControlledValue = false
924+ }
925+
903926 private func makeAttributedDocument ( ) -> NSAttributedString {
904927 let result = NSMutableAttributedString ( )
905928 let source = value as NSString
@@ -1113,6 +1136,7 @@ public final class T3ComposerEditorView: ExpoView, UITextViewDelegate, UITextDro
11131136 " value " : value ,
11141137 " selection " : [ " start " : selection . start , " end " : selection . end ] ,
11151138 " eventCount " : nativeEventCount ,
1139+ " resetGeneration " : resetGeneration ,
11161140 ] )
11171141 updatePlaceholderVisibility ( )
11181142 emitContentSizeIfNeeded ( )
@@ -1129,6 +1153,7 @@ public final class T3ComposerEditorView: ExpoView, UITextViewDelegate, UITextDro
11291153 " value " : currentValue ,
11301154 " selection " : [ " start " : selection . start , " end " : selection . end ] ,
11311155 " eventCount " : nativeEventCount ,
1156+ " resetGeneration " : resetGeneration ,
11321157 ] )
11331158 }
11341159
0 commit comments