@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.
@@ -51,6 +51,11 @@ android {
51
51
  disable 'GradleCompatible'
52
52
  }
53
53
 
54
+ // Robolectric needs the merged resources and the real manifest to inflate a view.
55
+ testOptions {
56
+ unitTests.includeAndroidResources = true
57
+ }
58
+
54
59
  compileOptions {
55
60
  sourceCompatibility JavaVersion.VERSION_17
56
61
  targetCompatibility JavaVersion.VERSION_17
@@ -67,6 +72,18 @@ dependencies {
67
72
  implementation "com.facebook.react:react-android"
68
73
  implementation 'androidx.appcompat:appcompat:1.7.1'
69
74
  implementation 'com.google.android.material:material:1.12.0'
75
+ // Voice-note playback: ExoPlayer's own WebM extractor (vendor MediaPlayer extractors truncate web notes).
76
+ // Host apps can pin the version (same key react-native-video reads), e.g. to keep compileSdk 34:
77
+ // ext { RNVideo_media3Version = "1.4.1" } — media3 1.8.0 needs compileSdk 35.
78
+ def media3Version = rootProject.ext.has("RNVideo_media3Version") ? rootProject.ext.get("RNVideo_media3Version") : "1.8.0"
79
+ implementation "androidx.media3:media3-exoplayer:${media3Version}"
80
+
81
+ // Unit tests for the native editor. Robolectric runs real Android framework code off-device,
82
+ // which is the only way to assert on spans: the inline-style rules live in a TextWatcher and
83
+ // their result is a set of spans on an Editable, which no JS-level test can see.
84
+ testImplementation 'junit:junit:4.13.2'
85
+ testImplementation 'org.robolectric:robolectric:4.17'
86
+ testImplementation 'androidx.test:core:1.7.0'
70
87
  }
71
88
 
72
89
  if (isNewArchitectureEnabled()) {
@@ -165,6 +165,8 @@ class RichTextEditorView(context: Context) : androidx.appcompat.widget.AppCompat
165
165
  private var isInitialized = false
166
166
 
167
167
  private var previousText: String = ""
168
+ // Text length at the last onSelectionChanged, for telling typing apart from a caret move.
169
+ private var lastSelectionTextLength = 0
168
170
  private var pendingDelta: Map<String, Any>? = null
169
171
  private var pendingPrefixDeletion: Pair<Int, Int>? = null // (lineStart, prefixLength) for backspace-in-prefix
170
172
  // Saved selected text for URL-paste-over-selection detection in TextWatcher
@@ -228,6 +230,48 @@ class RichTextEditorView(context: Context) : androidx.appcompat.widget.AppCompat
228
230
  // pendingStyles (rather than as independent fields) so they inherit its existing
229
231
  // caret-moved/cleared invalidation instead of needing a parallel copy of it.
230
232
  private var pendingExternalColor: Int? = null
233
+
234
+ /**
235
+ * Inline formatting carried over when the keyboard REPLACES a run of text — accepting a
236
+ * suggestion, autocorrect on space, or swapping a whole word. The styles are read from the
237
+ * first character of the run being replaced, before the text changes, and re-applied to
238
+ * whatever is inserted in its place.
239
+ *
240
+ * Without this the formatting dies with the text it was anchored to: a span records a
241
+ * character range, not the user's intent, so replacing those characters drops it.
242
+ */
243
+ private data class InlineStyleSnapshot(
244
+ val bold: Boolean,
245
+ val italic: Boolean,
246
+ val underline: Boolean,
247
+ val strikethrough: Boolean,
248
+ val externalColor: Int?,
249
+ val externalBackgroundColor: Int?,
250
+ ) {
251
+ val isEmpty: Boolean
252
+ get() = !bold && !italic && !underline && !strikethrough &&
253
+ externalColor == null && externalBackgroundColor == null
254
+
255
+ /**
256
+ * Drops whatever the user has explicitly switched off at the cursor.
257
+ *
258
+ * An explicit "off" outranks inheritance. Without this, clearing a colour and then
259
+ * letting the keyboard replace the word resurrects the colour from the replaced run —
260
+ * the opposite of what the user just asked for. The newly-typed path already honours
261
+ * [explicitlyOffStyles]; the replacement path has to as well.
262
+ */
263
+ fun minus(off: Set<String>): InlineStyleSnapshot = copy(
264
+ bold = bold && !off.contains("bold"),
265
+ italic = italic && !off.contains("italic"),
266
+ underline = underline && !off.contains("underline"),
267
+ strikethrough = strikethrough && !off.contains("strikethrough"),
268
+ externalColor = externalColor.takeIf { !off.contains("externalColor") },
269
+ externalBackgroundColor =
270
+ externalBackgroundColor.takeIf { !off.contains("externalBackgroundColor") },
271
+ )
272
+ }
273
+
274
+ private var inheritedStyles: InlineStyleSnapshot? = null
231
275
  private var pendingExternalBackgroundColor: Int? = null
232
276
 
233
277
  // Store selection for toolbar actions (selection might be lost when clicking toolbar)
@@ -387,6 +431,14 @@ class RichTextEditorView(context: Context) : androidx.appcompat.widget.AppCompat
387
431
  // the user had exactly that text highlighted. Keyboards also replace text while typing
388
432
  // (rewriting "www" -> "Www.", autocorrect) but with a bare cursor, and those must not
389
433
  // become links. No clipboard read here: Android 12+ toasts on every read.
434
+ // A replacement is about to happen: remember the formatting of the text being
435
+ // replaced so the incoming text can inherit it (see InlineStyleSnapshot).
436
+ inheritedStyles = if (count > 0 && after > 0 && s != null && start < s.length) {
437
+ captureInlineStylesAt(s, start)
438
+ } else {
439
+ null
440
+ }
441
+
390
442
  val selStart = minOf(selectionStart, selectionEnd)
391
443
  val selEnd = maxOf(selectionStart, selectionEnd)
392
444
  if (count > 0 && after > 0 && s != null && start + count <= s.length &&
@@ -465,6 +517,58 @@ class RichTextEditorView(context: Context) : androidx.appcompat.widget.AppCompat
465
517
  }
466
518
  }
467
519
 
520
+ // The keyboard replaced a run of text (suggestion, autocorrect, word swap).
521
+ // Give the incoming text the formatting the replaced text had, so the colour
522
+ // follows the user's intent rather than the character range it was pinned to.
523
+ if (removedCount > 0 && addedCount > 0 && s != null) {
524
+ inheritedStyles?.let { captured ->
525
+ // Anything the user explicitly switched off wins over what the
526
+ // replaced run happened to carry.
527
+ var snapshot = captured.minus(explicitlyOffStyles)
528
+ // A colour ARMED at the caret is the user's newest intent, so it
529
+ // outranks the colour of the text being replaced. Without this the
530
+ // replaced run won: arm red over a blue word, accept a suggestion, and
531
+ // the replacement came back blue while the armed red survived to
532
+ // colour the next character — two colours in one word. The typed-text
533
+ // path below already defers to pendingStyles; this one has to as well.
534
+ if (pendingStyles.contains("externalColor")) {
535
+ snapshot = snapshot.copy(externalColor = pendingExternalColor)
536
+ }
537
+ if (pendingStyles.contains("externalBackgroundColor")) {
538
+ snapshot = snapshot.copy(
539
+ externalBackgroundColor = pendingExternalBackgroundColor
540
+ )
541
+ }
542
+ val spanStart = changeStart
543
+ val spanEnd = changeStart + addedCount
544
+ if (!snapshot.isEmpty && spanStart < spanEnd && spanEnd <= s.length) {
545
+ isInternalChange = true
546
+ applyInlineStyleSnapshot(s, snapshot, spanStart, spanEnd)
547
+ isInternalChange = false
548
+ }
549
+ }
550
+ inheritedStyles = null
551
+ }
552
+
553
+ // A colour the user switched off at the caret must stay off for what they type
554
+ // next — including inside an existing coloured run. A span that covers the
555
+ // caret on both sides grows over inserted text on its own, whatever its flags,
556
+ // so the clear was ignored the moment the caret sat mid-word: tap into red
557
+ // text, clear, type, and the new text came out red (ENG-40922, found by
558
+ // InlineColourFuzzTest). Strip the colour from the inserted range explicitly.
559
+ if (addedCount > 0 && s != null && changeStart + addedCount <= s.length) {
560
+ val start = changeStart
561
+ val end = changeStart + addedCount
562
+ isInternalChange = true
563
+ if (explicitlyOffStyles.contains("externalColor")) {
564
+ clearInlineStyleSpans(s, "color", start, end)
565
+ }
566
+ if (explicitlyOffStyles.contains("externalBackgroundColor")) {
567
+ clearInlineStyleSpans(s, "backgroundColor", start, end)
568
+ }
569
+ isInternalChange = false
570
+ }
571
+
468
572
  // Apply pending styles to newly typed text
469
573
  if (addedCount > 0 && removedCount == 0 && s != null) {
470
574
  val spanStart = changeStart
@@ -537,6 +641,78 @@ class RichTextEditorView(context: Context) : androidx.appcompat.widget.AppCompat
537
641
  }
538
642
  }
539
643
 
644
+ // --- Insertion inherits from the character before the caret ---
645
+ //
646
+ // A colour applied over a SELECTION is frozen to that range, so text
647
+ // typed straight after it does not join the run. That is what makes
648
+ // "colour Hello, delete two characters, type p" come out as
649
+ // <color>Hel</color>p. Inherit the colour from the preceding character
650
+ // instead of relying on the span to grow.
651
+ //
652
+ // This covers the character styles too, not only the colours. The
653
+ // earlier scoping to colours was wrong: pendingStyles carries a style
654
+ // the user ARMED at the caret, but a style applied over a SELECTION
655
+ // exists only as a frozen span and is in no Set, so typing on dropped
656
+ // it. Measured on a Pixel: strike through "Doe", colour it, then let
657
+ // the keyboard append the "s" of "Does" — the s came out coloured but
658
+ // not struck, because only the colour was carried here (ENG-40419).
659
+ //
660
+ // pendingStyles wins (the user just picked a colour) and
661
+ // explicitlyOffStyles wins (the user just cleared one); this only fills
662
+ // the gap where neither has an opinion. At position 0, or with the
663
+ // field empty, there is nothing to inherit and the text stays plain.
664
+ //
665
+ // Code is skipped wholesale: inline code and a code block own their
666
+ // own foreground, so inheriting a consumer colour from the character
667
+ // before the caret would paint over the code styling the user just
668
+ // armed.
669
+ val codeArmed = pendingStyles.contains("code") ||
670
+ pendingStyles.contains("codeBlock")
671
+ if (changeStart > 0 && !codeArmed) {
672
+ val neighbour = captureInlineStylesAt(s, changeStart - 1)
673
+ if (!pendingStyles.contains("externalColor") &&
674
+ !explicitlyOffStyles.contains("externalColor")
675
+ ) {
676
+ neighbour?.externalColor?.let { inherited ->
677
+ clearInlineStyleSpans(s, "color", spanStart, spanEnd, keepColor = inherited)
678
+ s.setSpan(
679
+ ExternalForegroundColorSpan(inherited),
680
+ spanStart, spanEnd, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
681
+ )
682
+ }
683
+ }
684
+ if (!pendingStyles.contains("externalBackgroundColor") &&
685
+ !explicitlyOffStyles.contains("externalBackgroundColor")
686
+ ) {
687
+ neighbour?.externalBackgroundColor?.let { inherited ->
688
+ clearInlineStyleSpans(s, "backgroundColor", spanStart, spanEnd, keepColor = inherited)
689
+ s.setSpan(
690
+ ExternalBackgroundColorSpan(inherited),
691
+ spanStart, spanEnd, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
692
+ )
693
+ }
694
+ }
695
+
696
+ // Carry bold/italic/underline/strikethrough the same way. Skipped
697
+ // when the style is already armed — the pendingStyles block above
698
+ // has applied it, and setting a second span over the same range
699
+ // would leave two identical spans to no purpose. (Not a wire-format
700
+ // problem: spannedToMarkdown segments by span boundary and
701
+ // de-duplicates per segment, so a duplicate emits one tag.)
702
+ if (neighbour != null) {
703
+ fun carry(key: String, present: Boolean, makeSpan: () -> Any) {
704
+ if (!present) return
705
+ if (pendingStyles.contains(key)) return
706
+ if (explicitlyOffStyles.contains(key)) return
707
+ s.setSpan(makeSpan(), spanStart, spanEnd, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
708
+ }
709
+ carry("bold", neighbour.bold) { StyleSpan(Typeface.BOLD) }
710
+ carry("italic", neighbour.italic) { StyleSpan(Typeface.ITALIC) }
711
+ carry("underline", neighbour.underline) { UnderlineSpan() }
712
+ carry("strikethrough", neighbour.strikethrough) { StrikethroughSpan() }
713
+ }
714
+ }
715
+
540
716
  // Remove explicitly-off styles from newly typed text
541
717
  if (explicitlyOffStyles.isNotEmpty()) {
542
718
  for (offStyle in explicitlyOffStyles) {
@@ -725,6 +901,14 @@ class RichTextEditorView(context: Context) : androidx.appcompat.widget.AppCompat
725
901
  s.getSpans(0, 0, StrikethroughSpan::class.java).forEach { s.removeSpan(it) }
726
902
  s.getSpans(0, 0, InlineCodeSpan::class.java).forEach { s.removeSpan(it) }
727
903
  s.getSpans(0, 0, ForegroundColorSpan::class.java).forEach { s.removeSpan(it) }
904
+ // ExternalForegroundColorSpan is not a ForegroundColorSpan, so the line above
905
+ // missed it: delete everything and the next character came back in the old
906
+ // colour, while iOS correctly started plain (ENG-40922, found by
907
+ // InlineColourFuzzTest).
908
+ s.getSpans(0, 0, ExternalForegroundColorSpan::class.java).forEach { s.removeSpan(it) }
909
+ s.getSpans(0, 0, ExternalBackgroundColorSpan::class.java).forEach { s.removeSpan(it) }
910
+ pendingExternalColor = null
911
+ pendingExternalBackgroundColor = null
728
912
  s.getSpans(0, 0, AbsoluteSizeSpan::class.java).forEach { s.removeSpan(it) }
729
913
  s.getSpans(0, 0, CodeBlockBorderSpan::class.java).forEach { s.removeSpan(it) }
730
914
  s.getSpans(0, 0, TypefaceSpan::class.java).forEach { s.removeSpan(it) }
@@ -1232,13 +1416,27 @@ class RichTextEditorView(context: Context) : androidx.appcompat.widget.AppCompat
1232
1416
  // Skip if not initialized yet (during construction)
1233
1417
  if (!isInitialized) return
1234
1418
 
1419
+ // How many characters landed since the last selection change, measured against the length
1420
+ // recorded here rather than previousText. A keyboard (Gboard, composing mid-word) can run
1421
+ // afterTextChanged — which refreshes previousText — before this callback, so measuring
1422
+ // against previousText read every keystroke as "nothing typed" and disarmed the colour on
1423
+ // the first character (ENG-40922). Tracked unconditionally, as iOS does (ENG-38248).
1424
+ val currentLen = text?.length ?: 0
1425
+ val insertedCount = currentLen - lastSelectionTextLength
1426
+ lastSelectionTextLength = currentLen
1427
+
1235
1428
  // Clear pending/explicitly-off styles when cursor moves WITHOUT text changing
1236
1429
  // (i.e., user tapped somewhere else or used arrow keys)
1237
1430
  if ((pendingStyles.isNotEmpty() || explicitlyOffStyles.isNotEmpty()) && pendingStylesInsertPos >= 0) {
1238
1431
  if (selStart != pendingStylesInsertPos) {
1239
- val currentLen = text?.length ?: 0
1240
- // Allow cursor to advance by 1 (typing) — don't clear in that case
1241
- if (selStart != pendingStylesInsertPos + 1 || currentLen == previousText.length) {
1432
+ // Typing advances the caret by however many characters landed: one for a plain
1433
+ // keystroke, more when the keyboard commits an autocorrection or suggestion (which
1434
+ // "." and space both do). A hardcoded +1 read those as a caret move and disarmed
1435
+ // the colour, so text after a full stop sent plain (ENG-40922). Mirrors iOS.
1436
+ // A backspace at the anchor moves the caret back by exactly what it removed — that is
1437
+ // editing, not a caret jump, so the colour follows it (ENG-40922).
1438
+ val isTypingAdvance = insertedCount != 0 && selStart == pendingStylesInsertPos + insertedCount
1439
+ if (!isTypingAdvance) {
1242
1440
  // Preserve "codeBlock" across cursor movements — it's a persistent
1243
1441
  // block-level style that should survive until explicitly toggled off
1244
1442
  // or exited via triple-Enter. Without this, the clear-and-re-add
@@ -1253,7 +1451,7 @@ class RichTextEditorView(context: Context) : androidx.appcompat.widget.AppCompat
1253
1451
  pendingStylesInsertPos = selStart
1254
1452
  }
1255
1453
  } else {
1256
- // Cursor advanced by 1 due to typing — update insert pos
1454
+ // Text was typed at the anchor — follow it, however many characters landed
1257
1455
  pendingStylesInsertPos = selStart
1258
1456
  }
1259
1457
  }
@@ -1601,6 +1799,55 @@ class RichTextEditorView(context: Context) : androidx.appcompat.widget.AppCompat
1601
1799
  }
1602
1800
 
1603
1801
  // Synchronous style detection - emits current active styles to JS
1802
+ /** Inline formatting present at [pos] — used to let replacement text inherit it. */
1803
+ private fun captureInlineStylesAt(s: CharSequence, pos: Int): InlineStyleSnapshot? {
1804
+ val spanned = s as? Spanned ?: return null
1805
+ if (pos < 0 || pos >= spanned.length) return null
1806
+ val end = pos + 1
1807
+
1808
+ var bold = false
1809
+ var italic = false
1810
+ spanned.getSpans(pos, end, StyleSpan::class.java).forEach {
1811
+ when (it.style) {
1812
+ Typeface.BOLD -> bold = true
1813
+ Typeface.ITALIC -> italic = true
1814
+ Typeface.BOLD_ITALIC -> { bold = true; italic = true }
1815
+ }
1816
+ }
1817
+
1818
+ return InlineStyleSnapshot(
1819
+ bold = bold,
1820
+ italic = italic,
1821
+ underline = spanned.getSpans(pos, end, UnderlineSpan::class.java).isNotEmpty(),
1822
+ strikethrough = spanned.getSpans(pos, end, StrikethroughSpan::class.java).isNotEmpty(),
1823
+ externalColor = spanned.getSpans(pos, end, ExternalForegroundColorSpan::class.java)
1824
+ .firstOrNull()?.color,
1825
+ externalBackgroundColor = spanned.getSpans(pos, end, ExternalBackgroundColorSpan::class.java)
1826
+ .firstOrNull()?.color,
1827
+ )
1828
+ }
1829
+
1830
+ /** Re-applies [snapshot] over [start, end). Caller owns the isInternalChange guard. */
1831
+ private fun applyInlineStyleSnapshot(
1832
+ s: Editable,
1833
+ snapshot: InlineStyleSnapshot,
1834
+ start: Int,
1835
+ end: Int,
1836
+ ) {
1837
+ if (snapshot.bold) s.setSpan(StyleSpan(Typeface.BOLD), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
1838
+ if (snapshot.italic) s.setSpan(StyleSpan(Typeface.ITALIC), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
1839
+ if (snapshot.underline) s.setSpan(UnderlineSpan(), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
1840
+ if (snapshot.strikethrough) s.setSpan(StrikethroughSpan(), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
1841
+ snapshot.externalColor?.let {
1842
+ clearInlineStyleSpans(s, "color", start, end, keepColor = it)
1843
+ s.setSpan(ExternalForegroundColorSpan(it), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
1844
+ }
1845
+ snapshot.externalBackgroundColor?.let {
1846
+ clearInlineStyleSpans(s, "backgroundColor", start, end, keepColor = it)
1847
+ s.setSpan(ExternalBackgroundColorSpan(it), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
1848
+ }
1849
+ }
1850
+
1604
1851
  private fun emitActiveStyles() {
1605
1852
  val start = selectionStart
1606
1853
  val end = selectionEnd
@@ -4089,9 +4336,11 @@ class RichTextEditorView(context: Context) : androidx.appcompat.widget.AppCompat
4089
4336
  if (key == "color") {
4090
4337
  pendingExternalColor = color
4091
4338
  pendingStyles.add("externalColor")
4339
+ explicitlyOffStyles.remove("externalColor")
4092
4340
  } else {
4093
4341
  pendingExternalBackgroundColor = color
4094
4342
  pendingStyles.add("externalBackgroundColor")
4343
+ explicitlyOffStyles.remove("externalBackgroundColor")
4095
4344
  }
4096
4345
  pendingStylesInsertPos = selectionStart
4097
4346
  return
@@ -4128,13 +4377,25 @@ class RichTextEditorView(context: Context) : androidx.appcompat.widget.AppCompat
4128
4377
  // No selection: cancel the pending type-ahead style instead of no-oping.
4129
4378
  val target = styleTargetRange()
4130
4379
  if (target == null) {
4380
+ // Record the clear in explicitlyOffStyles, not just by dropping the pending value.
4381
+ // Both inheritance rules ask that set whether the user has switched a style off, and
4382
+ // until this line nothing ever put a colour key in it — so `minus(explicitlyOffStyles)`
4383
+ // and the rule-2 guard were dead for colours and an explicit clear was ignored.
4384
+ // Measured on a Pixel 9 Pro XL: colour "Doe", clear at the caret, type "s", and the
4385
+ // "s" came out red. Using the existing set rather than a new flag also means the clear
4386
+ // expires on a caret move for free, through the invalidation in onSelectionChanged.
4131
4387
  if (key == "color") {
4132
4388
  pendingExternalColor = null
4133
4389
  pendingStyles.remove("externalColor")
4390
+ explicitlyOffStyles.add("externalColor")
4134
4391
  } else {
4135
4392
  pendingExternalBackgroundColor = null
4136
4393
  pendingStyles.remove("externalBackgroundColor")
4394
+ explicitlyOffStyles.add("externalBackgroundColor")
4137
4395
  }
4396
+ // That invalidation is gated on an anchor being set, so a clear has to set one too —
4397
+ // otherwise the "off" would outlive the caret and silence colours elsewhere.
4398
+ pendingStylesInsertPos = selectionStart
4138
4399
  // Clearing the pending colour is not enough. A colour span applied by type-ahead is
4139
4400
  // SPAN_EXCLUSIVE_INCLUSIVE so it grows with typing — a span ending exactly at the cursor
4140
4401
  // would keep swallowing everything typed after the user asked for NO colour. Pin it shut