@cometchat/chat-uikit-react-native 5.5.2 → 5.5.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -832,6 +832,11 @@ class RichTextView: UITextView {
832
832
  return true
833
833
  }
834
834
  }
835
+ // Allow the spelling correction so tapping a floating spell-check suggestion replaces the
836
+ // misspelled word. Only this selector, not all of super (keeps Look Up/Share/system B/I/U hidden).
837
+ if action == NSSelectorFromString("replace:") {
838
+ return super.canPerformAction(action, withSender: sender)
839
+ }
835
840
  return false
836
841
  }
837
842
 
@@ -1188,6 +1193,9 @@ class RichTextEditorView: UIView, UITextViewDelegate, UIGestureRecognizerDelegat
1188
1193
  /// emoji, a paste, an IME commit — and without this the anchor below treats every one of those
1189
1194
  /// as "the user moved the caret" and disarms the pending colour mid-sentence (ENG-38257).
1190
1195
  private var lastKnownTextLength: Int = 0
1196
+ /// The text as of the last keyboard edit, so colourEditThatBypassedInheritance can find what
1197
+ /// the next edit changed.
1198
+ private var lastEditSnapshot: NSAttributedString?
1191
1199
 
1192
1200
  /// Caret-armed consumer inline colour (no selection). A UIColor cannot live in a
1193
1201
  /// `Set<String>` cleanly, so the value lives here while `ExternalColorPendingKey` goes into
@@ -1197,6 +1205,20 @@ class RichTextEditorView: UIView, UITextViewDelegate, UIGestureRecognizerDelegat
1197
1205
  /// True when the colour was cleared with the caret parked, so the next typed character is
1198
1206
  /// `UIColor.label` rather than inheriting a neighbouring colour.
1199
1207
  private var pendingExternalForegroundCleared: Bool = false
1208
+ /// The background equivalent. removeInlineStyle strips the background straight out of
1209
+ /// typingAttributes rather than arming a pending value, so there is no sentinel in
1210
+ /// `pendingStyles` to read — without this flag a keyboard replacing the word would lift the
1211
+ /// background back off the run it replaced and undo the clear. Reset wherever
1212
+ /// `pendingExternalForegroundCleared` is reset, so both decisions expire together.
1213
+ private var pendingExternalBackgroundCleared: Bool = false
1214
+
1215
+ /// Colour to re-apply to text a keyboard just inserted, with the range it landed in.
1216
+ /// Set in shouldChangeTextIn (before the text changes, while the old run is still
1217
+ /// readable) and consumed in textViewDidChange. Going through typingAttributes does not
1218
+ /// survive — many later sites in this file overwrite it before UIKit inserts.
1219
+ private var pendingInheritColour: UIColor? = nil
1220
+ private var pendingInheritBackground: UIColor? = nil
1221
+ private var pendingInheritRange: NSRange? = nil
1200
1222
 
1201
1223
  /// Typing attributes to forcefully restore after all processing (including async
1202
1224
  /// JS bridge callbacks like syncMentionRanges) completes. Set by autoContinueListOnEnter
@@ -1245,7 +1267,7 @@ class RichTextEditorView: UIView, UITextViewDelegate, UIGestureRecognizerDelegat
1245
1267
  return
1246
1268
  }
1247
1269
  pendingExternalForeground = color
1248
- pendingExternalForegroundCleared = false
1270
+ pendingExternalForegroundCleared = false; pendingExternalBackgroundCleared = false
1249
1271
  pendingStyles.insert(ExternalColorPendingKey)
1250
1272
  }
1251
1273
 
@@ -1475,6 +1497,165 @@ class RichTextEditorView: UIView, UITextViewDelegate, UIGestureRecognizerDelegat
1475
1497
  return true
1476
1498
  }
1477
1499
 
1500
+ /// Drops every armed, cleared or deferred inline-style decision and returns typingAttributes
1501
+ /// to plain. The composer is empty, so none of it has anything left to apply to.
1502
+ ///
1503
+ /// Called from two places, and it has to be: `textDidChange` runs only for edits the USER
1504
+ /// made, because UIKit does not post `textDidChangeNotification` for a programmatic
1505
+ /// assignment. Clearing the composer after a send is programmatic — `setTextContent("")` —
1506
+ /// so relying on the notification left the colour armed and the NEXT message came out
1507
+ /// coloured without the user asking. Android resets here because its `setText` does run its
1508
+ /// TextWatcher; this is what gives iOS the same behaviour.
1509
+ private func resetInlineStateForEmptyComposer() {
1510
+ pendingStyles.removeAll()
1511
+ explicitlyOffStyles.removeAll()
1512
+ pendingStylesInsertPos = -1
1513
+ lastKnownTextLength = 0
1514
+ pendingExternalForeground = nil
1515
+ pendingExternalForegroundCleared = false
1516
+ pendingExternalBackgroundCleared = false
1517
+ deferredTypingAttrs = nil
1518
+ // A captured inheritance would otherwise be painted onto the first character of the
1519
+ // next message, which is the same leak by a different route.
1520
+ clearPendingInherit()
1521
+ lastEditSnapshot = nil
1522
+ // Reset typingAttributes to plain so next typed text has no ghost formatting
1523
+ let paragraphStyle = NSMutableParagraphStyle()
1524
+ paragraphStyle.lineHeightMultiple = RichTextEditorView.defaultLineHeightMultiple
1525
+ textView.typingAttributes = [
1526
+ .font: UIFont.systemFont(ofSize: 16),
1527
+ .foregroundColor: UIColor.label,
1528
+ .paragraphStyle: paragraphStyle
1529
+ ]
1530
+ }
1531
+
1532
+ private func clearPendingInherit() {
1533
+ pendingInheritColour = nil
1534
+ pendingInheritBackground = nil
1535
+ pendingInheritRange = nil
1536
+ }
1537
+
1538
+ /// Paints the colour captured in shouldChangeTextIn onto the text the keyboard just inserted.
1539
+ ///
1540
+ /// Called from textDidChange rather than from a textViewDidChange delegate method. Two reasons,
1541
+ /// both learned the hard way:
1542
+ ///
1543
+ /// - textView.insertText (list continuation) fires the delegate synchronously from inside an
1544
+ /// isInternalChange window, so a delegate implementation re-enters mid-edit and, by resetting
1545
+ /// the flag, makes the rest of that edit look like user typing. textDidChange already guards
1546
+ /// on isInternalChange, so it is re-entrancy-safe.
1547
+ /// - textDidChange owns saveToUndoStack and sendContentChange. Mutating here, ahead of both,
1548
+ /// means the inherited colour is in the text before it is emitted to JS or snapshotted for
1549
+ /// undo, rather than depending on UIKit's delegate/notification ordering.
1550
+ private func applyPendingInheritedColour() {
1551
+ guard let range = pendingInheritRange,
1552
+ let attrText = textView.attributedText,
1553
+ range.location >= 0, range.location + range.length <= attrText.length,
1554
+ range.length > 0,
1555
+ pendingInheritColour != nil || pendingInheritBackground != nil else {
1556
+ clearPendingInherit()
1557
+ return
1558
+ }
1559
+
1560
+ // Add the attributes in place rather than assigning `textView.attributedText`.
1561
+ //
1562
+ // Assigning attributedText replaces the whole string, and UITextView ends any marked text
1563
+ // when it does — marked text being the uncommitted, underlined run an input method shows
1564
+ // while a word is still being composed. This method runs on every keystroke that inherits
1565
+ // a colour, so with a Japanese, Chinese or Korean keyboard the composition was torn down
1566
+ // character by character and the word could not be typed at all. Mutating textStorage
1567
+ // leaves the string, the composition and the caret untouched, so none of that arises and
1568
+ // the caret no longer has to be saved and restored either.
1569
+ let storage = textView.textStorage
1570
+ let wasInternal = isInternalChange
1571
+ isInternalChange = true
1572
+ storage.beginEditing()
1573
+ if let colour = pendingInheritColour {
1574
+ storage.addAttribute(ExternalForegroundKey, value: colour, range: range)
1575
+ storage.addAttribute(.foregroundColor, value: colour, range: range)
1576
+ }
1577
+ if let background = pendingInheritBackground {
1578
+ storage.addAttribute(ExternalBackgroundKey, value: background, range: range)
1579
+ storage.addAttribute(.backgroundColor, value: background, range: range)
1580
+ }
1581
+ storage.endEditing()
1582
+ isInternalChange = wasInternal
1583
+
1584
+ clearPendingInherit()
1585
+ }
1586
+
1587
+ /// Colours a keyboard edit that bypassed the inheritance capture in shouldChangeTextIn.
1588
+ ///
1589
+ /// iOS makes the double-space full stop by swapping the space for ". " without that capture
1590
+ /// taking effect, so the full stop and the space after it came out in the default colour and
1591
+ /// broke the coloured run — at the end of the text and mid-text alike (ENG-40922). This diffs
1592
+ /// the text against the last edit and gives the changed run the colour the shouldChangeTextIn
1593
+ /// rules would have: an armed colour first, else the run it replaced, else the character before.
1594
+ ///
1595
+ /// Only keyboard-sized edits (a few characters) are considered, so a programmatic change that
1596
+ /// slipped between two snapshots — a send, setText from JS — is never mistaken for one.
1597
+ private func colourEditThatBypassedInheritance() {
1598
+ guard let now = textView.attributedText else { return }
1599
+ let before = lastEditSnapshot
1600
+ lastEditSnapshot = NSAttributedString(attributedString: now)
1601
+ // No snapshot yet means nothing was typed since the composer was last empty, so the old
1602
+ // text is empty — the first keystroke still gets diffed rather than skipped.
1603
+ let old = before ?? NSAttributedString()
1604
+ guard !pendingExternalForegroundCleared,
1605
+ !pendingStyles.contains("code"), !pendingStyles.contains("codeBlock") else { return }
1606
+
1607
+ let oldS = old.string as NSString, newS = now.string as NSString
1608
+ let minLen = min(oldS.length, newS.length)
1609
+ var prefix = 0
1610
+ while prefix < minLen && oldS.character(at: prefix) == newS.character(at: prefix) { prefix += 1 }
1611
+ var suffix = 0
1612
+ while suffix < minLen - prefix &&
1613
+ oldS.character(at: oldS.length - 1 - suffix) == newS.character(at: newS.length - 1 - suffix) { suffix += 1 }
1614
+ let oldRun = NSRange(location: prefix, length: oldS.length - prefix - suffix)
1615
+ var newRun = NSRange(location: prefix, length: newS.length - prefix - suffix)
1616
+ guard newRun.length > 0, newRun.length <= 3, oldRun.length <= 3 else { return }
1617
+
1618
+ func isColoured(_ r: NSRange) -> Bool {
1619
+ var found = false
1620
+ now.enumerateAttribute(ExternalForegroundKey, in: r) { v, _, stop in
1621
+ if v != nil { found = true; stop.pointee = true }
1622
+ }
1623
+ return found
1624
+ }
1625
+ // An insertion next to identical characters is ambiguous: a space typed beside an existing
1626
+ // space diffs as if the existing one were new, and colouring that one recolours text the
1627
+ // user did not touch. A keyboard insertion always ends at the caret, so among the equally
1628
+ // valid positions (sliding left through equal characters) take the one ending there.
1629
+ // Pure insertions only — a replacement has a definite position.
1630
+ if oldRun.length == 0 {
1631
+ var leftmost = newRun.location
1632
+ while leftmost > 0,
1633
+ newS.character(at: leftmost - 1) == newS.character(at: leftmost - 1 + newRun.length) {
1634
+ leftmost -= 1
1635
+ }
1636
+ let atCaret = textView.selectedRange.location - newRun.length
1637
+ if textView.selectedRange.length == 0, atCaret >= leftmost, atCaret <= newRun.location {
1638
+ newRun.location = atCaret
1639
+ }
1640
+ }
1641
+ // Leave it alone if the run already carries a colour — the normal rules ran.
1642
+ guard !isColoured(newRun) else { return }
1643
+
1644
+ let source: [NSAttributedString.Key: Any]? =
1645
+ oldRun.length > 0 ? old.attributes(at: oldRun.location, effectiveRange: nil)
1646
+ : (newRun.location > 0 ? now.attributes(at: newRun.location - 1, effectiveRange: nil) : nil)
1647
+ let armed = pendingStyles.contains(ExternalColorPendingKey) ? pendingExternalForeground : nil
1648
+ guard source?[MentionMarkerKey] == nil, source?[LinkURLAttributeKey] == nil,
1649
+ let colour = armed ?? source?[ExternalForegroundKey] as? UIColor else { return }
1650
+
1651
+ let wasInternal = isInternalChange
1652
+ isInternalChange = true
1653
+ textView.textStorage.addAttribute(ExternalForegroundKey, value: colour, range: newRun)
1654
+ textView.textStorage.addAttribute(.foregroundColor, value: colour, range: newRun)
1655
+ isInternalChange = wasInternal
1656
+ lastEditSnapshot = NSAttributedString(attributedString: textView.attributedText)
1657
+ }
1658
+
1478
1659
  func textViewDidChangeSelection(_ textView: UITextView) {
1479
1660
  let range = textView.selectedRange
1480
1661
  if range.length > 0 {
@@ -1498,14 +1679,23 @@ class RichTextEditorView: UIView, UITextViewDelegate, UIGestureRecognizerDelegat
1498
1679
  // to avoid emitting stale style state before typingAttributes are restored.
1499
1680
  guard !isInternalChange else { return }
1500
1681
 
1501
- // Invalidate pending styles if cursor moved away from insert position
1502
- if pendingStylesInsertPos >= 0 && (!pendingStyles.isEmpty || !explicitlyOffStyles.isEmpty) {
1682
+ // Invalidate pending styles if cursor moved away from insert position.
1683
+ // The cleared-colour flags count as pending decisions in their own right: a background
1684
+ // clear arms no sentinel and leaves both Sets empty, so without them in this condition it
1685
+ // would never expire.
1686
+ if pendingStylesInsertPos >= 0 && (!pendingStyles.isEmpty || !explicitlyOffStyles.isEmpty
1687
+ || pendingExternalForegroundCleared || pendingExternalBackgroundCleared) {
1503
1688
  // Typing advances the caret by however many characters actually landed: one for a plain
1504
1689
  // keystroke, but several for autocorrect replacing a word, predictive text, a multi-scalar
1505
1690
  // emoji, a paste, or an IME commit. Comparing against a hardcoded +1 classified all of
1506
1691
  // those as a deliberate caret move and disarmed the colour, so text typed from the first
1507
1692
  // autocorrection onwards sent as plain (ENG-38257).
1508
- let isTypingAdvance = insertedCount > 0 && range.location == pendingStylesInsertPos + insertedCount
1693
+ //
1694
+ // Deleting at the anchor is editing too: a backspace moves the caret back by exactly the
1695
+ // characters removed. Counting only forward moves read it as a caret jump and dropped the
1696
+ // colour — accept a suggestion, backspace its trailing space, double-space for a full
1697
+ // stop, and everything after it sent plain (ENG-40922).
1698
+ let isTypingAdvance = insertedCount != 0 && range.location == pendingStylesInsertPos + insertedCount
1509
1699
  if range.location != pendingStylesInsertPos && !isTypingAdvance {
1510
1700
  // Preserve "codeBlock" across cursor movements — it's a persistent
1511
1701
  // block-level style that should survive until explicitly toggled off
@@ -1516,7 +1706,7 @@ class RichTextEditorView: UIView, UITextViewDelegate, UIGestureRecognizerDelegat
1516
1706
  // Drop the caret-armed colour too — otherwise it leaks onto text typed at the
1517
1707
  // new cursor position (the removeAll() above only drops the sentinel).
1518
1708
  pendingExternalForeground = nil
1519
- pendingExternalForegroundCleared = false
1709
+ pendingExternalForegroundCleared = false; pendingExternalBackgroundCleared = false
1520
1710
  if hadCodeBlock {
1521
1711
  pendingStyles.insert("codeBlock")
1522
1712
  pendingStylesInsertPos = range.location
@@ -1773,6 +1963,93 @@ class RichTextEditorView: UIView, UITextViewDelegate, UIGestureRecognizerDelegat
1773
1963
 
1774
1964
  func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
1775
1965
 
1966
+ // --- Inline formatting follows the user's intent, not the character range ---
1967
+ //
1968
+ // Measured on iOS 26: accepting a suggestion arrives as ONE replacement —
1969
+ // loc=0 len=3 text='Respect' (after typing "Res")
1970
+ // so the styled characters are destroyed in a single call and the replacement is
1971
+ // styled from typingAttributes, which knows nothing about them.
1972
+ //
1973
+ // This MUST run before the early returns further down (list prefixes, code blocks,
1974
+ // blockquotes). Placed at the end it never executed, which is why the colour was lost
1975
+ // even with the rules in place.
1976
+ if !isInternalChange, let attrText = textView.attributedText {
1977
+ // Reset first, unconditionally. Every `return false` path below handles its own text
1978
+ // and suppresses UIKit's insertion, so textDidChange never runs and never consumes
1979
+ // what we set here. Without this reset that stale range outlives the change and gets
1980
+ // painted onto whatever text later occupies it.
1981
+ clearPendingInherit()
1982
+
1983
+ var typing = textView.typingAttributes
1984
+ var inheritFrom: [NSAttributedString.Key: Any]? = nil
1985
+
1986
+ // Inline code and a code block own their own foreground, so a consumer colour
1987
+ // inherited from the neighbouring character would paint over the code styling the
1988
+ // user just armed. Android skips the same way.
1989
+ let codeArmed = pendingStyles.contains("code") || pendingStyles.contains("codeBlock")
1990
+
1991
+ if codeArmed {
1992
+ inheritFrom = nil
1993
+ } else if range.length > 0, !text.isEmpty, range.location < attrText.length {
1994
+ // A replacement: inherit from the run being replaced, read before it is gone.
1995
+ inheritFrom = attrText.attributes(at: range.location, effectiveRange: nil)
1996
+ } else if range.length == 0, !text.isEmpty, range.location > 0,
1997
+ range.location - 1 < attrText.length,
1998
+ !pendingStyles.contains(ExternalColorPendingKey),
1999
+ !pendingExternalForegroundCleared {
2000
+ // A plain insertion: inherit from the character before the caret, so text typed
2001
+ // after a frozen run joins it. An armed or just-cleared colour wins over this.
2002
+ inheritFrom = attrText.attributes(at: range.location - 1, effectiveRange: nil)
2003
+ }
2004
+
2005
+ if let src = inheritFrom {
2006
+ // An explicit CLEAR outranks inheritance on both rules: the user turned the
2007
+ // colour off, so a keyboard replacing that word must not resurrect it.
2008
+ //
2009
+ // Only the clear, deliberately — not an armed colour. pendingStyles carries the
2010
+ // sentinel for armed and cleared alike, so testing it here also suppressed
2011
+ // inheritance while a colour was armed, and the armed colour alone does not
2012
+ // survive a replacement: typingAttributes is overwritten before UIKit inserts,
2013
+ // which is the whole reason this pendingInherit path exists. Measured on an
2014
+ // iPhone 17 Pro: colour "Doe", accept the "Doesn't" suggestion, and the word
2015
+ // came through black. explicitlyOffStyles is not consulted either — on iOS it
2016
+ // only ever holds bold/italic/underline/strikethrough/code.
2017
+ let colourCleared = pendingExternalForegroundCleared
2018
+ let backgroundCleared = pendingExternalBackgroundCleared
2019
+
2020
+ // Remember where the incoming text will land so textDidChange can style it.
2021
+ //
2022
+ // An ARMED colour beats the replaced run's: it is the user's newest intent. Before
2023
+ // this the run won, so arming red over a blue word and then accepting a suggestion
2024
+ // gave a blue replacement, while the armed red survived to colour the next
2025
+ // character — two colours in one word. Android has the same precedence at
2026
+ // RichTextEditorView.kt:525.
2027
+ pendingInheritColour = colourCleared
2028
+ ? nil
2029
+ : (pendingExternalForeground ?? src[ExternalForegroundKey] as? UIColor)
2030
+ pendingInheritBackground = backgroundCleared ? nil : src[ExternalBackgroundKey] as? UIColor
2031
+ pendingInheritRange = (pendingInheritColour != nil || pendingInheritBackground != nil)
2032
+ ? NSRange(location: range.location, length: (text as NSString).length)
2033
+ : nil
2034
+
2035
+ var changed = false
2036
+ // Marker and paint move together: the marker is what the serializer reads.
2037
+ // Read the already-filtered pending values, not `src`, so a cleared colour is not
2038
+ // re-armed here after being suppressed above.
2039
+ if let colour = pendingInheritColour {
2040
+ typing[ExternalForegroundKey] = colour
2041
+ typing[.foregroundColor] = colour
2042
+ changed = true
2043
+ }
2044
+ if let background = pendingInheritBackground {
2045
+ typing[ExternalBackgroundKey] = background
2046
+ typing[.backgroundColor] = background
2047
+ changed = true
2048
+ }
2049
+ if changed { textView.typingAttributes = typing }
2050
+ }
2051
+ }
2052
+
1776
2053
  // --- Mention bleed-through prevention (iOS equivalent of Android SPAN_EXCLUSIVE_EXCLUSIVE) ---
1777
2054
  // Check if typingAttributes carry mention styling and strip it before insertion.
1778
2055
  do {
@@ -2431,6 +2708,11 @@ class RichTextEditorView: UIView, UITextViewDelegate, UIGestureRecognizerDelegat
2431
2708
  }
2432
2709
 
2433
2710
  if !isInternalChange {
2711
+ // Before anything reads or snapshots the text: give the characters the keyboard just
2712
+ // inserted the formatting they inherited (see applyPendingInheritedColour).
2713
+ applyPendingInheritedColour()
2714
+ colourEditThatBypassedInheritance()
2715
+
2434
2716
  // Clear pending styles after text was typed (typingAttributes already applied them).
2435
2717
  // Preserve inline styles (bold, italic, underline, strikethrough) — they persist
2436
2718
  // until the cursor moves to a different position (matching Android behavior where
@@ -2483,21 +2765,7 @@ class RichTextEditorView: UIView, UITextViewDelegate, UIGestureRecognizerDelegat
2483
2765
 
2484
2766
  // Reset pending styles and typingAttributes when text becomes empty
2485
2767
  if textView.text.isEmpty {
2486
- pendingStyles.removeAll()
2487
- explicitlyOffStyles.removeAll()
2488
- pendingStylesInsertPos = -1
2489
- lastKnownTextLength = 0
2490
- pendingExternalForeground = nil
2491
- pendingExternalForegroundCleared = false
2492
- deferredTypingAttrs = nil
2493
- // Reset typingAttributes to plain so next typed text has no ghost formatting
2494
- let paragraphStyle = NSMutableParagraphStyle()
2495
- paragraphStyle.lineHeightMultiple = RichTextEditorView.defaultLineHeightMultiple
2496
- textView.typingAttributes = [
2497
- .font: UIFont.systemFont(ofSize: 16),
2498
- .foregroundColor: UIColor.label,
2499
- .paragraphStyle: paragraphStyle
2500
- ]
2768
+ resetInlineStateForEmptyComposer()
2501
2769
  }
2502
2770
 
2503
2771
  // Skip applyListIndentation/renumberNumberedLists during internal changes
@@ -3795,7 +4063,7 @@ class RichTextEditorView: UIView, UITextViewDelegate, UIGestureRecognizerDelegat
3795
4063
  return
3796
4064
  }
3797
4065
  pendingExternalForeground = color
3798
- pendingExternalForegroundCleared = false
4066
+ pendingExternalForegroundCleared = false; pendingExternalBackgroundCleared = false
3799
4067
  pendingStyles.insert(ExternalColorPendingKey)
3800
4068
  pendingStylesInsertPos = range.location
3801
4069
  applyPendingStylesToTypingAttributes()
@@ -3825,6 +4093,18 @@ class RichTextEditorView: UIView, UITextViewDelegate, UIGestureRecognizerDelegat
3825
4093
  guard attribute == .foregroundColor else {
3826
4094
  textView.typingAttributes.removeValue(forKey: marker)
3827
4095
  textView.typingAttributes.removeValue(forKey: attribute)
4096
+ // Stripping typingAttributes stops the next TYPED character carrying the
4097
+ // background, but a keyboard replacement reads the run it replaces, not
4098
+ // typingAttributes — so record the decision for the inheritance rules too.
4099
+ //
4100
+ // The anchor goes in with it. The caret-move invalidation needs one to compare
4101
+ // against, and this branch arms no sentinel, so without it a background-only
4102
+ // clear never expired: clear after one highlighted word, tap into another, accept
4103
+ // a suggestion there, and that second word silently lost its highlight.
4104
+ if attribute == .backgroundColor {
4105
+ pendingExternalBackgroundCleared = true
4106
+ pendingStylesInsertPos = range.location
4107
+ }
3828
4108
  return
3829
4109
  }
3830
4110
  pendingExternalForeground = nil
@@ -4681,7 +4961,7 @@ class RichTextEditorView: UIView, UITextViewDelegate, UIGestureRecognizerDelegat
4681
4961
  // inline style, so the consumer colour goes with them rather than surviving alone.
4682
4962
  pendingStyles.remove(ExternalColorPendingKey)
4683
4963
  pendingExternalForeground = nil
4684
- pendingExternalForegroundCleared = false
4964
+ pendingExternalForegroundCleared = false; pendingExternalBackgroundCleared = false
4685
4965
  explicitlyOffStyles.removeAll()
4686
4966
  // Reset typingAttributes to plain so list prefix doesn't inherit code block styling
4687
4967
  let plainParaStyle = NSMutableParagraphStyle()
@@ -5438,7 +5718,7 @@ class RichTextEditorView: UIView, UITextViewDelegate, UIGestureRecognizerDelegat
5438
5718
  // Drop an armed consumer colour, or the first character typed into the empty block
5439
5719
  // would come out coloured instead of the code block default.
5440
5720
  pendingExternalForeground = nil
5441
- pendingExternalForegroundCleared = false
5721
+ pendingExternalForegroundCleared = false; pendingExternalBackgroundCleared = false
5442
5722
  pendingStyles.remove(ExternalColorPendingKey)
5443
5723
  pendingStylesInsertPos = textView.selectedRange.location
5444
5724
  isInternalChange = false
@@ -5507,7 +5787,7 @@ class RichTextEditorView: UIView, UITextViewDelegate, UIGestureRecognizerDelegat
5507
5787
  // A colour armed for type-ahead would otherwise colour the next character typed
5508
5788
  // inside the block.
5509
5789
  pendingExternalForeground = nil
5510
- pendingExternalForegroundCleared = false
5790
+ pendingExternalForegroundCleared = false; pendingExternalBackgroundCleared = false
5511
5791
  pendingStyles.remove(ExternalColorPendingKey)
5512
5792
  // Set pendingStyles + typingAttributes so newly typed characters
5513
5793
  // inherit code block mode (matches empty-line activation path)
@@ -5670,6 +5950,15 @@ class RichTextEditorView: UIView, UITextViewDelegate, UIGestureRecognizerDelegat
5670
5950
 
5671
5951
  placeholderLabel.isHidden = !text.isEmpty
5672
5952
  isInternalChange = false
5953
+
5954
+ // The composer just became empty programmatically — a send, or a clear from JS. UIKit
5955
+ // posts no change notification for that, so textDidChange never runs and the reset it
5956
+ // owns would never happen: the colour stayed armed and the next message came out
5957
+ // coloured on its own. Reset here instead, matching Android.
5958
+ if (textView.text ?? "").isEmpty {
5959
+ resetInlineStateForEmptyComposer()
5960
+ }
5961
+
5673
5962
  updateContentSize()
5674
5963
  if force {
5675
5964
  sendContentChange()
@@ -5820,7 +6109,15 @@ class RichTextEditorView: UIView, UITextViewDelegate, UIGestureRecognizerDelegat
5820
6109
 
5821
6110
  isInternalChange = true
5822
6111
  let savedTypingAttrs = textView.typingAttributes
6112
+ // Assigning attributedText moves the caret to the end. JS pushes mention ranges after
6113
+ // every content change, so editing mid-text — a double-space full stop, for one — sent
6114
+ // the caret to the end of the message (ENG-40922). The string is unchanged here, only
6115
+ // its attributes, so the old caret is still valid.
6116
+ let savedSelection = textView.selectedRange
5823
6117
  textView.attributedText = mutableAttr
6118
+ if NSMaxRange(savedSelection) <= mutableAttr.length {
6119
+ textView.selectedRange = savedSelection
6120
+ }
5824
6121
  textView.typingAttributes = savedTypingAttrs
5825
6122
  // If a deferred restore is pending (from autoContinueListOnEnter),
5826
6123
  // use those attrs instead — they carry the user's active formatting.