@@ -24,6 +24,13 @@ private struct ComposerControlledDocumentPayload: Decodable {
2424 let tokensJson : String
2525 let mostRecentEventCount : Int
2626 let isNativeEcho : Bool
27+ let paragraphDirections : [ ComposerParagraphDirectionPayload ] ?
28+ }
29+
30+ private struct ComposerParagraphDirectionPayload : Decodable {
31+ let start : Int
32+ let end : Int
33+ let direction : String
2734}
2835
2936private struct ComposerThemePayload : Decodable {
@@ -424,6 +431,8 @@ public final class T3ComposerEditorView: ExpoView, UITextViewDelegate, UITextDro
424431 private var value = " "
425432 private var tokensJson = " [] "
426433 private var tokens : [ ComposerTokenPayload ] = [ ]
434+ private var paragraphDirections : [ ComposerParagraphDirectionPayload ] = [ ]
435+ private var paragraphDirectionValue = " "
427436 private var requestedSelection : ComposerSelectionPayload ?
428437 private var theme = ComposerThemePayload (
429438 text : " #262626 " ,
@@ -659,6 +668,8 @@ public final class T3ComposerEditorView: ExpoView, UITextViewDelegate, UITextDro
659668 }
660669 let previousSelection = sourceSelection ( )
661670 value = document . value
671+ paragraphDirections = document . paragraphDirections ?? [ ]
672+ paragraphDirectionValue = document . value
662673 requestedSelection = document . selection
663674 applyControlledDocument ( force : tokensNeedRebuild )
664675 applyRequestedSelection ( )
@@ -869,6 +880,7 @@ public final class T3ComposerEditorView: ExpoView, UITextViewDelegate, UITextDro
869880 let shouldForce = force || hasDeferredForcedRebuild
870881 let currentSource = textView . serializedText ( )
871882 guard shouldForce || currentSource != value || ! documentMatchesExpectedTokens ( ) else {
883+ applyParagraphDirections ( )
872884 updatePlaceholderVisibility ( )
873885 return
874886 }
@@ -884,6 +896,7 @@ public final class T3ComposerEditorView: ExpoView, UITextViewDelegate, UITextDro
884896 requestedSelection = nil
885897 textView . selectedRange = displayRange ( for : targetSelection )
886898 restoreBaseTypingAttributes ( )
899+ applyParagraphDirections ( )
887900 isApplyingControlledValue = false
888901 if currentSource != value || previousSelection . start != targetSelection . start || previousSelection . end != targetSelection . end {
889902 revealSelectionAfterLayout ( )
@@ -1060,20 +1073,57 @@ public final class T3ComposerEditorView: ExpoView, UITextViewDelegate, UITextDro
10601073 return nil
10611074 }
10621075
1063- private func baseAttributes ( ) -> [ NSAttributedString . Key : Any ] {
1064- let font = UIFont ( name : fontFamily , size : fontSize )
1065- ?? UIFont . systemFont ( ofSize : fontSize )
1076+ private func paragraphStyle ( direction : String ? = nil ) -> NSMutableParagraphStyle {
10661077 let paragraph = NSMutableParagraphStyle ( )
1067- paragraph . alignment = . left
1078+ if let direction {
1079+ paragraph . alignment = direction == " rtl " ? . right : . left
1080+ paragraph . baseWritingDirection = direction == " rtl " ? . rightToLeft : . leftToRight
1081+ }
10681082 paragraph . minimumLineHeight = lineHeight
10691083 paragraph . maximumLineHeight = lineHeight
1084+ return paragraph
1085+ }
1086+
1087+ private func baseAttributes ( ) -> [ NSAttributedString . Key : Any ] {
1088+ let font = UIFont ( name : fontFamily , size : fontSize )
1089+ ?? UIFont . systemFont ( ofSize : fontSize )
10701090 return [
10711091 . font : font ,
10721092 . foregroundColor : UIColor ( composerHex : theme . text ) ?? . label ,
1073- . paragraphStyle : paragraph ,
1093+ . paragraphStyle : paragraphStyle ( ) ,
10741094 ]
10751095 }
10761096
1097+ private func applyParagraphDirections ( ) {
1098+ guard ! paragraphDirections . isEmpty , textView . markedTextRange == nil ,
1099+ textView . serializedText ( ) == paragraphDirectionValue else { return }
1100+ let wasApplyingControlledValue = isApplyingControlledValue
1101+ isApplyingControlledValue = true
1102+ let selection = textView . selectedRange
1103+ let sourceLength = ( paragraphDirectionValue as NSString ) . length
1104+ textView . textStorage . beginEditing ( )
1105+ for paragraph in paragraphDirections {
1106+ guard paragraph . start >= 0 , paragraph . end >= paragraph . start ,
1107+ paragraph . end <= sourceLength else { continue }
1108+ let range = displayRange ( for : ComposerSelectionPayload ( start : paragraph . start , end : paragraph . end ) )
1109+ guard range . length > 0 else { continue }
1110+ let style = paragraphStyle ( direction : paragraph . direction )
1111+ let current = textView . textStorage . attribute ( . paragraphStyle , at : range . location , effectiveRange : nil ) as? NSParagraphStyle
1112+ if current != style {
1113+ textView . textStorage . addAttribute ( . paragraphStyle , value : style , range : range )
1114+ }
1115+ }
1116+ textView . textStorage . endEditing ( )
1117+ if textView . selectedRange != selection { textView . selectedRange = selection }
1118+ let sourceCaret = textView . sourceOffset ( forDisplayOffset : selection . location )
1119+ if let paragraph = paragraphDirections . last ( where : { $0 . start <= sourceCaret } ) {
1120+ var attributes = baseAttributes ( )
1121+ attributes [ . paragraphStyle ] = paragraphStyle ( direction : paragraph . direction )
1122+ textView . typingAttributes = attributes
1123+ }
1124+ isApplyingControlledValue = wasApplyingControlledValue
1125+ }
1126+
10771127 private func applyTypography ( ) {
10781128 let font = UIFont ( name : fontFamily , size : fontSize )
10791129 ?? UIFont . systemFont ( ofSize : fontSize )
@@ -1087,7 +1137,12 @@ public final class T3ComposerEditorView: ExpoView, UITextViewDelegate, UITextDro
10871137 guard textView . markedTextRange == nil else {
10881138 return
10891139 }
1090- textView . typingAttributes = baseAttributes ( )
1140+ var attributes = baseAttributes ( )
1141+ // UIKit selects the paragraph's style when the caret moves; retain its bidi direction.
1142+ if let paragraph = textView . typingAttributes [ . paragraphStyle ] {
1143+ attributes [ . paragraphStyle ] = paragraph
1144+ }
1145+ textView . typingAttributes = attributes
10911146 }
10921147
10931148 private func applyTheme ( ) {
0 commit comments