@cometchat/chat-uikit-react-native 5.4.1 → 5.5.1
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.
- package/android/src/main/java/com/reactnativecometchatuikit/CometChatClipboardModule.kt +34 -0
- package/android/src/main/java/com/reactnativecometchatuikit/RichTextEditorView.kt +562 -5
- package/android/src/main/java/com/reactnativecometchatuikit/RichTextEditorViewManager.kt +28 -1
- package/ios/CometChatClipboardModule.m +5 -0
- package/ios/CometChatClipboardModule.swift +22 -0
- package/ios/RichTextEditorView.swift +636 -31
- package/ios/RichTextEditorViewManager.m +3 -0
- package/ios/RichTextEditorViewManager.swift +24 -0
- package/package.json +5 -2
- package/src/CometChatCompactMessageComposer/CometChatCompactMessageComposer.tsx +283 -64
- package/src/CometChatCompactMessageComposer/index.ts +2 -0
- package/src/CometChatConversations/CometChatConversations.tsx +299 -15
- package/src/CometChatGroupMembers/CometChatGroupMembers.tsx +2 -2
- package/src/CometChatMessageComposer/CometChatMessageComposer.tsx +15 -3
- package/src/CometChatMessageHeader/CometChatMessageHeader.tsx +181 -12
- package/src/CometChatMessageInformation/CometChatMessageInformation.tsx +10 -1
- package/src/CometChatMessageList/CometChatMessageList.tsx +572 -16
- package/src/CometChatMessageList/components/MessageModals.tsx +30 -0
- package/src/CometChatMessageList/components/MessageOptionsSheet.tsx +21 -9
- package/src/CometChatMessageList/components/MessageQuickActions.tsx +162 -0
- package/src/CometChatPinnedMessages/CometChatPinnedMessages.tsx +964 -0
- package/src/CometChatPinnedMessages/index.ts +5 -0
- package/src/CometChatPinnedMessages/style.ts +216 -0
- package/src/CometChatRichTextEditor/RichTextEditorViewNativeComponent.ts +5 -0
- package/src/CometChatRichTextEditor/index.tsx +31 -1
- package/src/CometChatRichTextEditor/types.ts +21 -1
- package/src/CometChatSavedMessages/CometChatSavedMessages.tsx +871 -0
- package/src/CometChatSavedMessages/index.ts +5 -0
- package/src/CometChatSavedMessages/style.ts +159 -0
- package/src/CometChatSearch/CometChatSearch.tsx +71 -341
- package/src/CometChatThreadHeader/CometChatThreadHeader.tsx +22 -2
- package/src/index.ts +60 -0
- package/src/shared/CometChatUiKit/CometChatUIKit.ts +18 -1
- package/src/shared/base/Types.ts +5 -0
- package/src/shared/constants/UIKitConstants.ts +16 -17
- package/src/shared/constants/textPatterns.ts +27 -0
- package/src/shared/events/CometChatUIEventHandler/CometChatUIEventHandler.ts +25 -225
- package/src/shared/events/CometChatUIEventHandler/Listener.ts +37 -0
- package/src/shared/events/ListenerInitializer.ts +20 -1
- package/src/shared/events/messages.ts +18 -0
- package/src/shared/formatters/CometChatRichTextFormatter/index.tsx +123 -25
- package/src/shared/formatters/CometChatTextFormatter.ts +22 -0
- package/src/shared/formatters/applyRawFormatters.ts +31 -0
- package/src/shared/formatters/index.ts +14 -0
- package/src/shared/formatters/richTextWireFormat.ts +165 -0
- package/src/shared/framework/MessageDataSource.tsx +309 -10
- package/src/shared/icons/components/thread-follow.tsx +13 -0
- package/src/shared/icons/components/thread-unfollow.tsx +13 -0
- package/src/shared/icons/components/unsave.tsx +13 -0
- package/src/shared/icons/icon-mapping.tsx +6 -0
- package/src/shared/index.ts +37 -0
- package/src/shared/resources/CometChatLocalizeNew/CometChatI18nProvider.tsx +15 -30
- package/src/shared/resources/CometChatLocalizeNew/CometChatLocalizationHelper.ts +138 -19
- package/src/shared/resources/CometChatLocalizeNew/resources/de/translation.json +74 -1
- package/src/shared/resources/CometChatLocalizeNew/resources/en/translation.json +54 -1
- package/src/shared/resources/CometChatLocalizeNew/resources/es/translation.json +54 -1
- package/src/shared/resources/CometChatLocalizeNew/resources/fr/translation.json +54 -1
- package/src/shared/resources/CometChatLocalizeNew/resources/hi/translation.json +54 -1
- package/src/shared/resources/CometChatLocalizeNew/resources/hu/translation.json +54 -1
- package/src/shared/resources/CometChatLocalizeNew/resources/it/translation.json +54 -1
- package/src/shared/resources/CometChatLocalizeNew/resources/ja/translation.json +54 -1
- package/src/shared/resources/CometChatLocalizeNew/resources/ko/translation.json +54 -1
- package/src/shared/resources/CometChatLocalizeNew/resources/lt/translation.json +54 -1
- package/src/shared/resources/CometChatLocalizeNew/resources/ms/translation.json +54 -1
- package/src/shared/resources/CometChatLocalizeNew/resources/nl/translation.json +54 -1
- package/src/shared/resources/CometChatLocalizeNew/resources/pt/translation.json +54 -1
- package/src/shared/resources/CometChatLocalizeNew/resources/ru/translation.json +54 -1
- package/src/shared/resources/CometChatLocalizeNew/resources/sv/translation.json +54 -1
- package/src/shared/resources/CometChatLocalizeNew/resources/tr/translation.json +54 -1
- package/src/shared/resources/CometChatLocalizeNew/resources/zh/translation.json +54 -1
- package/src/shared/resources/CometChatLocalizeNew/resources/zh-tw/translation.json +54 -1
- package/src/shared/utils/CometChatMessagePreview/CometChatMessagePreview.tsx +11 -0
- package/src/shared/utils/MarkdownUtils.ts +57 -6
- package/src/shared/utils/MessageUtils.tsx +80 -5
- package/src/shared/utils/PinConversationHelper.ts +245 -0
- package/src/shared/utils/PinSaveFeatureGates.ts +178 -0
- package/src/shared/utils/PinSaveHelper.ts +549 -0
- package/src/shared/utils/PinnedMessagesHelper.ts +65 -0
- package/src/shared/utils/SavedMessagesHelper.ts +267 -0
- package/src/shared/utils/ThreadSubscriptionHelper.ts +427 -0
- package/src/shared/utils/__testUtils__/messagesRequestBuilderMock.ts +37 -0
- package/src/shared/utils/cloneRequestBuilder.ts +25 -0
- package/src/shared/utils/conversationUtils.ts +8 -1
- package/src/shared/utils/stripRichText.ts +7 -0
- package/src/shared/views/ClipboardPasteHandler/ClipboardPasteHandler.ts +32 -0
- package/src/shared/views/ClipboardPasteHandler/NativeCometChatClipboardModule.ts +6 -0
- package/src/shared/views/CometChatActionSheet/CometChatActionSheet.tsx +4 -0
- package/src/shared/views/CometChatAudiosBubble/CometChatAudiosBubble.tsx +5 -0
- package/src/shared/views/CometChatAvatar/CometChatAvatar.tsx +17 -5
- package/src/shared/views/CometChatBottomSheet/CometChatBottomSheet.tsx +5 -1
- package/src/shared/views/CometChatCaptionText/CometChatCaptionText.tsx +8 -1
- package/src/shared/views/CometChatConfirmDialog/CometChatConfirmDialog.tsx +2 -0
- package/src/shared/views/CometChatConversationStarter/CometChatConversationStarter.tsx +91 -20
- package/src/shared/views/CometChatFilesBubble/CometChatFilesBubble.tsx +5 -0
- package/src/shared/views/CometChatImagesBubble/CometChatImagesBubble.tsx +5 -0
- package/src/shared/views/CometChatList/CometChatList.tsx +6 -1
- package/src/shared/views/CometChatList/Header.tsx +8 -2
- package/src/shared/views/CometChatListItem/CometChatListItem.tsx +4 -0
- package/src/shared/views/CometChatRetryButton/CometChatRetryButton.tsx +1 -0
- package/src/shared/views/CometChatSuggestionList/CometChatSuggestionList.tsx +4 -0
- package/src/shared/views/CometChatVideosBubble/CometChatVideosBubble.tsx +5 -0
- package/src/shared/views/CometChatVoiceNoteBubble/CometChatVoiceNoteBubble.tsx +5 -0
- package/src/shared/views/ErrorEmptyView/ErrorEmptyView.tsx +4 -1
- package/src/theme/default/default.ts +6 -0
- package/src/theme/type.ts +4 -0
|
@@ -106,6 +106,38 @@ class MentionSpan(val uid: String, val displayName: String) : CharacterStyle() {
|
|
|
106
106
|
}
|
|
107
107
|
}
|
|
108
108
|
|
|
109
|
+
/**
|
|
110
|
+
* Foreground colour applied by a consumer formatter via applyInlineStyle().
|
|
111
|
+
*
|
|
112
|
+
* Deliberately NOT a `ForegroundColorSpan` subclass. Two reasons:
|
|
113
|
+
* 1. 17 sites in this file sweep `ForegroundColorSpan` indiscriminately (they own the
|
|
114
|
+
* inline-code purple, link blue, code-block grey, transparent quote prefix and mention
|
|
115
|
+
* purple). A subclass would be silently clobbered by every one of them; a sibling type
|
|
116
|
+
* is invisible to all of them by construction.
|
|
117
|
+
* 2. `ForegroundColorSpan` is a `ParcelableSpan`; a subclass reports the *parent's* span
|
|
118
|
+
* type id, so clipboard / saved-instance-state round-trips would downgrade this marker
|
|
119
|
+
* into a plain colour span, indistinguishable from the editor's own colour plumbing.
|
|
120
|
+
*
|
|
121
|
+
* It actually paints, so it implements `UpdateAppearance` to make repaints correct.
|
|
122
|
+
*/
|
|
123
|
+
class ExternalForegroundColorSpan(val color: Int) : CharacterStyle(), UpdateAppearance {
|
|
124
|
+
override fun updateDrawState(tp: android.text.TextPaint?) {
|
|
125
|
+
tp?.color = color
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Background colour applied by a consumer formatter. A sibling type for the same reasons as
|
|
131
|
+
* [ExternalForegroundColorSpan] — 12 sites sweep `BackgroundColorSpan` indiscriminately (mention
|
|
132
|
+
* background, highlight, inline code, code block). Setting `TextPaint.bgColor` is exactly how
|
|
133
|
+
* `BackgroundColorSpan` itself renders, so this paints identically without being caught by them.
|
|
134
|
+
*/
|
|
135
|
+
class ExternalBackgroundColorSpan(val color: Int) : CharacterStyle(), UpdateAppearance {
|
|
136
|
+
override fun updateDrawState(tp: android.text.TextPaint?) {
|
|
137
|
+
tp?.bgColor = color
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
109
141
|
/**
|
|
110
142
|
* Custom URLSpan that suppresses the default underline drawn by ClickableSpan.
|
|
111
143
|
* Links are visually distinguished by ForegroundColorSpan (blue) only, matching iOS parity.
|
|
@@ -166,6 +198,10 @@ class RichTextEditorView(context: Context) : androidx.appcompat.widget.AppCompat
|
|
|
166
198
|
/// Produces a stable map key for an IntRange.
|
|
167
199
|
private fun rangeKey(range: IntRange): String = "${range.first}_${range.last}"
|
|
168
200
|
|
|
201
|
+
/// Formats a colour int as the lowercase `#rrggbb` string the JS wire format expects.
|
|
202
|
+
/// Alpha is intentionally dropped — the wire contract is `#rrggbb` only.
|
|
203
|
+
private fun hexOf(color: Int): String = String.format("#%06x", color and 0xFFFFFF)
|
|
204
|
+
|
|
169
205
|
// Theme-derived colors for inline code container drawing (set via React props)
|
|
170
206
|
var inlineCodeBackgroundColor: Int? = null
|
|
171
207
|
var inlineCodeBorderColor: Int? = null
|
|
@@ -188,6 +224,11 @@ class RichTextEditorView(context: Context) : androidx.appcompat.widget.AppCompat
|
|
|
188
224
|
private val pendingStyles = mutableSetOf<String>()
|
|
189
225
|
private val explicitlyOffStyles = mutableSetOf<String>() // Styles user explicitly turned off at cursor
|
|
190
226
|
private var pendingStylesInsertPos = -1
|
|
227
|
+
// Colour values for the "externalColor"/"externalBackgroundColor" pending styles. Kept beside
|
|
228
|
+
// pendingStyles (rather than as independent fields) so they inherit its existing
|
|
229
|
+
// caret-moved/cleared invalidation instead of needing a parallel copy of it.
|
|
230
|
+
private var pendingExternalColor: Int? = null
|
|
231
|
+
private var pendingExternalBackgroundColor: Int? = null
|
|
191
232
|
|
|
192
233
|
// Store selection for toolbar actions (selection might be lost when clicking toolbar)
|
|
193
234
|
private var savedSelectionStart: Int = 0
|
|
@@ -434,6 +475,19 @@ class RichTextEditorView(context: Context) : androidx.appcompat.widget.AppCompat
|
|
|
434
475
|
"italic" -> s.setSpan(StyleSpan(Typeface.ITALIC), spanStart, spanEnd, Spanned.SPAN_EXCLUSIVE_INCLUSIVE)
|
|
435
476
|
"underline" -> s.setSpan(UnderlineSpan(), spanStart, spanEnd, Spanned.SPAN_EXCLUSIVE_INCLUSIVE)
|
|
436
477
|
"strikethrough" -> s.setSpan(StrikethroughSpan(), spanStart, spanEnd, Spanned.SPAN_EXCLUSIVE_INCLUSIVE)
|
|
478
|
+
// These spans are EXCLUSIVE_INCLUSIVE so a colour keeps
|
|
479
|
+
// applying as the user types. That also means an OLDER
|
|
480
|
+
// colour span grows over text typed after a new colour was
|
|
481
|
+
// armed, leaving two colours on the same characters. Cut
|
|
482
|
+
// the previous colour back to where this one starts.
|
|
483
|
+
"externalColor" -> pendingExternalColor?.let {
|
|
484
|
+
clearInlineStyleSpans(s, "color", spanStart, spanEnd, keepColor = it)
|
|
485
|
+
s.setSpan(ExternalForegroundColorSpan(it), spanStart, spanEnd, Spanned.SPAN_EXCLUSIVE_INCLUSIVE)
|
|
486
|
+
}
|
|
487
|
+
"externalBackgroundColor" -> pendingExternalBackgroundColor?.let {
|
|
488
|
+
clearInlineStyleSpans(s, "backgroundColor", spanStart, spanEnd, keepColor = it)
|
|
489
|
+
s.setSpan(ExternalBackgroundColorSpan(it), spanStart, spanEnd, Spanned.SPAN_EXCLUSIVE_INCLUSIVE)
|
|
490
|
+
}
|
|
437
491
|
"code" -> {
|
|
438
492
|
val fontSize = inlineCodeFontSize ?: 12f
|
|
439
493
|
s.setSpan(AbsoluteSizeSpan(fontSize.toInt(), true), spanStart, spanEnd, Spanned.SPAN_EXCLUSIVE_INCLUSIVE)
|
|
@@ -558,7 +612,14 @@ class RichTextEditorView(context: Context) : androidx.appcompat.widget.AppCompat
|
|
|
558
612
|
if (addedCount > 1 && removedCount == 0 && s != null) {
|
|
559
613
|
val insertedStart = changeStart
|
|
560
614
|
val insertedEnd = (changeStart + addedCount).coerceAtMost(s.length)
|
|
561
|
-
|
|
615
|
+
// A copy from CometChat puts the PLAIN text on the clipboard (so `<color=…>`
|
|
616
|
+
// can never leak into another app) and the wire form in a private clip
|
|
617
|
+
// extra. Nothing read that extra back, so a copy/paste inside CometChat
|
|
618
|
+
// always arrived stripped of its colour — `markdownToSpannable` below
|
|
619
|
+
// handles `<color=…>` fine, it was simply never given it (ENG-38253).
|
|
620
|
+
val insertedText = richTextForPaste(
|
|
621
|
+
s.subSequence(insertedStart, insertedEnd).toString()
|
|
622
|
+
)
|
|
562
623
|
if (looksLikeMarkdown(insertedText)) {
|
|
563
624
|
// Check that the inserted text doesn't already have styled spans
|
|
564
625
|
// (would mean it came through onTextContextMenuItem or blocksToSpannable)
|
|
@@ -684,6 +745,13 @@ class RichTextEditorView(context: Context) : androidx.appcompat.widget.AppCompat
|
|
|
684
745
|
s.removeSpan(span)
|
|
685
746
|
}
|
|
686
747
|
}
|
|
748
|
+
// Consumer colours are applied SPAN_EXCLUSIVE_INCLUSIVE while typing, so a
|
|
749
|
+
// zero-length ghost would resurrect the colour after the text is deleted.
|
|
750
|
+
s.getSpans(cursorPos, cursorPos, ExternalForegroundColorSpan::class.java).forEach { span ->
|
|
751
|
+
if (s.getSpanStart(span) == s.getSpanEnd(span)) {
|
|
752
|
+
s.removeSpan(span)
|
|
753
|
+
}
|
|
754
|
+
}
|
|
687
755
|
}
|
|
688
756
|
|
|
689
757
|
// Detect markdown shortcuts (e.g., **text** → bold) and convert to
|
|
@@ -2303,6 +2371,29 @@ class RichTextEditorView(context: Context) : androidx.appcompat.widget.AppCompat
|
|
|
2303
2371
|
val clipboard = context.getSystemService(Context.CLIPBOARD_SERVICE) as? ClipboardManager
|
|
2304
2372
|
val clip = clipboard?.primaryClip
|
|
2305
2373
|
|
|
2374
|
+
// 0. Our own rich-text wire format, carried in the clip's extras. Checked before
|
|
2375
|
+
// HTML because a CometChat copy sets plain text only — the colour lives here,
|
|
2376
|
+
// and the plain-text type is deliberately clean so other apps never see markup.
|
|
2377
|
+
val wireText = clip?.description?.extras
|
|
2378
|
+
?.getString(CometChatClipboardModule.EXTRA_RICH_TEXT)
|
|
2379
|
+
if (!wireText.isNullOrEmpty()) {
|
|
2380
|
+
val s = text as? Editable
|
|
2381
|
+
if (s != null) {
|
|
2382
|
+
val start = selectionStart
|
|
2383
|
+
val end = selectionEnd
|
|
2384
|
+
isInternalChange = true
|
|
2385
|
+
s.replace(
|
|
2386
|
+
start.coerceAtMost(end),
|
|
2387
|
+
start.coerceAtLeast(end),
|
|
2388
|
+
markdownToSpannable(wireText)
|
|
2389
|
+
)
|
|
2390
|
+
isInternalChange = false
|
|
2391
|
+
sendContentChangeWithDelta()
|
|
2392
|
+
saveToUndoStack()
|
|
2393
|
+
return true
|
|
2394
|
+
}
|
|
2395
|
+
}
|
|
2396
|
+
|
|
2306
2397
|
// 1. Check for HTML rich content first (Req 18.2)
|
|
2307
2398
|
if (clip != null && clip.description.hasMimeType("text/html")) {
|
|
2308
2399
|
val htmlText = clip.getItemAt(0)?.htmlText
|
|
@@ -2595,6 +2686,8 @@ class RichTextEditorView(context: Context) : androidx.appcompat.widget.AppCompat
|
|
|
2595
2686
|
|
|
2596
2687
|
/** Returns true if the string contains markdown syntax that should be parsed into rich text. */
|
|
2597
2688
|
private fun looksLikeMarkdown(str: String): Boolean {
|
|
2689
|
+
// Rich-text wire format — only ever reaches here from our own clipboard extra.
|
|
2690
|
+
if (str.contains("<color=", ignoreCase = true)) return true
|
|
2598
2691
|
if (str.contains("[") && str.contains("](")) return true
|
|
2599
2692
|
if (str.contains("**")) return true
|
|
2600
2693
|
if (str.contains("~~")) return true
|
|
@@ -2605,6 +2698,28 @@ class RichTextEditorView(context: Context) : androidx.appcompat.widget.AppCompat
|
|
|
2605
2698
|
return false
|
|
2606
2699
|
}
|
|
2607
2700
|
|
|
2701
|
+
/**
|
|
2702
|
+
* The rich-text WIRE form of [inserted] when this paste came from a CometChat copy, else
|
|
2703
|
+
* [inserted] unchanged.
|
|
2704
|
+
*
|
|
2705
|
+
* The match is on the clipboard's own plain text: only a paste of exactly what we copied may be
|
|
2706
|
+
* swapped for the wire form. A partial paste, or a clipboard that has moved on since, falls
|
|
2707
|
+
* through untouched rather than pasting the wrong message's markup.
|
|
2708
|
+
*/
|
|
2709
|
+
private fun richTextForPaste(inserted: String): String {
|
|
2710
|
+
return try {
|
|
2711
|
+
val cm = context.getSystemService(Context.CLIPBOARD_SERVICE) as? ClipboardManager
|
|
2712
|
+
?: return inserted
|
|
2713
|
+
val clip = cm.primaryClip ?: return inserted
|
|
2714
|
+
val wire = clip.description?.extras
|
|
2715
|
+
?.getString(CometChatClipboardModule.EXTRA_RICH_TEXT) ?: return inserted
|
|
2716
|
+
val plain = clip.getItemAt(0)?.text?.toString() ?: return inserted
|
|
2717
|
+
if (inserted == plain) wire else inserted
|
|
2718
|
+
} catch (e: Exception) {
|
|
2719
|
+
inserted
|
|
2720
|
+
}
|
|
2721
|
+
}
|
|
2722
|
+
|
|
2608
2723
|
/** Normalizes a URL by prepending https:// if no recognized scheme is present. */
|
|
2609
2724
|
private fun normalizeURL(url: String): String {
|
|
2610
2725
|
val lower = url.lowercase()
|
|
@@ -2629,6 +2744,7 @@ class RichTextEditorView(context: Context) : androidx.appcompat.widget.AppCompat
|
|
|
2629
2744
|
|
|
2630
2745
|
// Active style state
|
|
2631
2746
|
var isBold = false
|
|
2747
|
+
var activePasteColor: Int? = null
|
|
2632
2748
|
var isItalic = false
|
|
2633
2749
|
var isUnderline = false
|
|
2634
2750
|
var isStrikethrough = false
|
|
@@ -2658,10 +2774,41 @@ class RichTextEditorView(context: Context) : androidx.appcompat.widget.AppCompat
|
|
|
2658
2774
|
val codeColor = if (isDarkMode()) Color.parseColor("#E0E0E0") else Color.parseColor("#333333")
|
|
2659
2775
|
result.setSpan(ForegroundColorSpan(codeColor), segStart, segEnd, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
|
|
2660
2776
|
}
|
|
2777
|
+
// A pasted colour behaves like any other consumer-applied colour once inserted — and it
|
|
2778
|
+
// wins over inline code's own foreground, so this goes in AFTER the code span. Two
|
|
2779
|
+
// foreground spans over one range are applied in the order they were added, so while the
|
|
2780
|
+
// colour was set first the code colour painted straight over it and a copied coloured
|
|
2781
|
+
// message pasted back uniformly grey (ENG-38253). iOS had the same defect in mirror image.
|
|
2782
|
+
activePasteColor?.let {
|
|
2783
|
+
result.setSpan(ExternalForegroundColorSpan(it), segStart, segEnd, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
|
|
2784
|
+
}
|
|
2661
2785
|
currentText.clear()
|
|
2662
2786
|
}
|
|
2663
2787
|
|
|
2664
2788
|
while (i < len) {
|
|
2789
|
+
// <color=#rrggbb> … </color> — same grammar the formatter renders.
|
|
2790
|
+
if (chars[i] == '<') {
|
|
2791
|
+
val rest = markdown.substring(i)
|
|
2792
|
+
if (rest.startsWith("</color>", ignoreCase = true)) {
|
|
2793
|
+
flush()
|
|
2794
|
+
activePasteColor = null
|
|
2795
|
+
i += 8
|
|
2796
|
+
continue
|
|
2797
|
+
}
|
|
2798
|
+
if (rest.startsWith("<color=", ignoreCase = true)) {
|
|
2799
|
+
val close = rest.indexOf('>')
|
|
2800
|
+
if (close > 0) {
|
|
2801
|
+
val hex = rest.substring(7, close).trim()
|
|
2802
|
+
val parsed = try { Color.parseColor(hex) } catch (e: Exception) { null }
|
|
2803
|
+
if (parsed != null) {
|
|
2804
|
+
flush()
|
|
2805
|
+
activePasteColor = parsed
|
|
2806
|
+
i += close + 1
|
|
2807
|
+
continue
|
|
2808
|
+
}
|
|
2809
|
+
}
|
|
2810
|
+
}
|
|
2811
|
+
}
|
|
2665
2812
|
// Markdown link: [text](url)
|
|
2666
2813
|
if (chars[i] == '[') {
|
|
2667
2814
|
val linkResult = parseLinkAt(chars, i)
|
|
@@ -3094,6 +3241,7 @@ class RichTextEditorView(context: Context) : androidx.appcompat.widget.AppCompat
|
|
|
3094
3241
|
map.putString("text", (text?.toString() ?: "").replace("\u200B", ""))
|
|
3095
3242
|
// Serialize blocks to JSON string (codegen doesn't support nested arrays)
|
|
3096
3243
|
map.putString("blocksJson", getBlocksJsonString())
|
|
3244
|
+
map.putString("mentionRangesJson", currentMentionRangesJson())
|
|
3097
3245
|
|
|
3098
3246
|
// Include delta information if available
|
|
3099
3247
|
if (delta != null) {
|
|
@@ -3303,6 +3451,15 @@ class RichTextEditorView(context: Context) : androidx.appcompat.widget.AppCompat
|
|
|
3303
3451
|
spannable.setSpan(ForegroundColorSpan(codeColor), absoluteStart, absoluteEnd, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
|
|
3304
3452
|
}
|
|
3305
3453
|
"highlight" -> spannable.setSpan(BackgroundColorSpan(Color.parseColor("#80FFFF00")), absoluteStart, absoluteEnd, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
|
|
3454
|
+
"textColor" -> {
|
|
3455
|
+
// Edit round-trip: restore a consumer colour from the wire format so the
|
|
3456
|
+
// composer shows the colour rather than raw <color=…> text.
|
|
3457
|
+
val hex = styleInfo["color"] as? String
|
|
3458
|
+
val parsed = try { hex?.let { Color.parseColor(it) } } catch (e: Exception) { null }
|
|
3459
|
+
if (parsed != null) {
|
|
3460
|
+
spannable.setSpan(ExternalForegroundColorSpan(parsed), absoluteStart, absoluteEnd, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
|
|
3461
|
+
}
|
|
3462
|
+
}
|
|
3306
3463
|
"link" -> {
|
|
3307
3464
|
val url = styleInfo["url"] as? String ?: ""
|
|
3308
3465
|
if (url.isNotEmpty()) {
|
|
@@ -3354,6 +3511,52 @@ class RichTextEditorView(context: Context) : androidx.appcompat.widget.AppCompat
|
|
|
3354
3511
|
pendingStyles.add("codeBlock")
|
|
3355
3512
|
pendingStylesInsertPos = endPos
|
|
3356
3513
|
}
|
|
3514
|
+
|
|
3515
|
+
// Arm the INLINE styles the caret lands in, so typing CONTINUES the run the message
|
|
3516
|
+
// ended with. iOS gets this for free — UITextView derives its typingAttributes from
|
|
3517
|
+
// the character before the insertion point — but here every span above is
|
|
3518
|
+
// SPAN_EXCLUSIVE_EXCLUSIVE, so text appended at the end extends none of them and
|
|
3519
|
+
// only `codeBlock` was ever armed. Opening a message that ended in inline code and
|
|
3520
|
+
// typing therefore dropped straight out of the code (and its colour) mid-word, while
|
|
3521
|
+
// the same edit on iOS carried both (ENG-38253).
|
|
3522
|
+
//
|
|
3523
|
+
// `pendingStyles` is the established mechanism: the TextWatcher applies each armed
|
|
3524
|
+
// style over the inserted range as SPAN_EXCLUSIVE_INCLUSIVE, which is exactly the
|
|
3525
|
+
// "keep applying as the user types" behaviour wanted here.
|
|
3526
|
+
var armedInline = false
|
|
3527
|
+
if (spannable.getSpans(checkPos, checkPos + 1, InlineCodeSpan::class.java).isNotEmpty()) {
|
|
3528
|
+
pendingStyles.add("code")
|
|
3529
|
+
armedInline = true
|
|
3530
|
+
}
|
|
3531
|
+
spannable.getSpans(checkPos, checkPos + 1, StyleSpan::class.java).forEach { span ->
|
|
3532
|
+
// BOLD_ITALIC is a bitmask, not a third case — test the bits, don't match values.
|
|
3533
|
+
if (span.style and Typeface.BOLD != 0) {
|
|
3534
|
+
pendingStyles.add("bold")
|
|
3535
|
+
armedInline = true
|
|
3536
|
+
}
|
|
3537
|
+
if (span.style and Typeface.ITALIC != 0) {
|
|
3538
|
+
pendingStyles.add("italic")
|
|
3539
|
+
armedInline = true
|
|
3540
|
+
}
|
|
3541
|
+
}
|
|
3542
|
+
if (spannable.getSpans(checkPos, checkPos + 1, UnderlineSpan::class.java).isNotEmpty()) {
|
|
3543
|
+
pendingStyles.add("underline")
|
|
3544
|
+
armedInline = true
|
|
3545
|
+
}
|
|
3546
|
+
if (spannable.getSpans(checkPos, checkPos + 1, StrikethroughSpan::class.java).isNotEmpty()) {
|
|
3547
|
+
pendingStyles.add("strikethrough")
|
|
3548
|
+
armedInline = true
|
|
3549
|
+
}
|
|
3550
|
+
spannable.getSpans(checkPos, checkPos + 1, ExternalForegroundColorSpan::class.java)
|
|
3551
|
+
.firstOrNull()?.let { span ->
|
|
3552
|
+
// The consumer colour rides its own pending slot, not `pendingStyles` alone.
|
|
3553
|
+
pendingExternalColor = span.color
|
|
3554
|
+
pendingStyles.add("externalColor")
|
|
3555
|
+
armedInline = true
|
|
3556
|
+
}
|
|
3557
|
+
if (armedInline) {
|
|
3558
|
+
pendingStylesInsertPos = endPos
|
|
3559
|
+
}
|
|
3357
3560
|
}
|
|
3358
3561
|
}
|
|
3359
3562
|
|
|
@@ -3431,6 +3634,24 @@ class RichTextEditorView(context: Context) : androidx.appcompat.widget.AppCompat
|
|
|
3431
3634
|
styles.add(mapOf("style" to "code", "start" to start, "end" to end))
|
|
3432
3635
|
}
|
|
3433
3636
|
|
|
3637
|
+
// Consumer inline colour. Offsets are already relative to the block prefix
|
|
3638
|
+
// (`• `/`▎ `/`1. ` and ZWS are stripped by the callers before extraction), so a colour
|
|
3639
|
+
// range can never cover a list/quote glyph.
|
|
3640
|
+
spannable.getSpans(lineStart, lineEnd, ExternalForegroundColorSpan::class.java).forEach { span ->
|
|
3641
|
+
val start = (spannable.getSpanStart(span) - lineStart).coerceAtLeast(0)
|
|
3642
|
+
val end = (spannable.getSpanEnd(span) - lineStart).coerceAtMost(lineEnd - lineStart)
|
|
3643
|
+
if (end > start) {
|
|
3644
|
+
styles.add(
|
|
3645
|
+
mapOf(
|
|
3646
|
+
"style" to "textColor",
|
|
3647
|
+
"start" to start,
|
|
3648
|
+
"end" to end,
|
|
3649
|
+
"color" to hexOf(span.color)
|
|
3650
|
+
)
|
|
3651
|
+
)
|
|
3652
|
+
}
|
|
3653
|
+
}
|
|
3654
|
+
|
|
3434
3655
|
spannable.getSpans(lineStart, lineEnd, BackgroundColorSpan::class.java).filter {
|
|
3435
3656
|
val color = it.backgroundColor
|
|
3436
3657
|
color == Color.parseColor("#80FFFF00") || color == Color.YELLOW
|
|
@@ -3521,6 +3742,8 @@ class RichTextEditorView(context: Context) : androidx.appcompat.widget.AppCompat
|
|
|
3521
3742
|
styleMap.putInt("end", style["end"] as Int)
|
|
3522
3743
|
val url = style["url"] as? String
|
|
3523
3744
|
if (url != null) styleMap.putString("url", url)
|
|
3745
|
+
val color = style["color"] as? String
|
|
3746
|
+
if (color != null) styleMap.putString("color", color)
|
|
3524
3747
|
stylesArray.pushMap(styleMap)
|
|
3525
3748
|
}
|
|
3526
3749
|
block.putArray("styles", stylesArray)
|
|
@@ -3585,6 +3808,10 @@ class RichTextEditorView(context: Context) : androidx.appcompat.widget.AppCompat
|
|
|
3585
3808
|
styleObj.put("end", style["end"])
|
|
3586
3809
|
val url = style["url"] as? String
|
|
3587
3810
|
if (url != null) styleObj.put("url", url)
|
|
3811
|
+
// textColor is value-carrying: without the hex the JS side sees a colour range it
|
|
3812
|
+
// cannot render, and drops it — colour shows in the composer but not in the bubble.
|
|
3813
|
+
val color = style["color"] as? String
|
|
3814
|
+
if (color != null) styleObj.put("color", color)
|
|
3588
3815
|
stylesJson.put(styleObj)
|
|
3589
3816
|
}
|
|
3590
3817
|
block.put("styles", stylesJson)
|
|
@@ -3767,6 +3994,211 @@ class RichTextEditorView(context: Context) : androidx.appcompat.widget.AppCompat
|
|
|
3767
3994
|
updateToolbarButtonStates()
|
|
3768
3995
|
}
|
|
3769
3996
|
|
|
3997
|
+
/**
|
|
3998
|
+
* Selection to style: the live selection, falling back to the saved one because tapping a
|
|
3999
|
+
* toolbar button can clear it. Returns null when there is nothing to style.
|
|
4000
|
+
*/
|
|
4001
|
+
private fun styleTargetRange(): Pair<Int, Int>? {
|
|
4002
|
+
var start = selectionStart
|
|
4003
|
+
var end = selectionEnd
|
|
4004
|
+
if (start >= end && savedSelectionStart < savedSelectionEnd) {
|
|
4005
|
+
start = savedSelectionStart
|
|
4006
|
+
end = savedSelectionEnd
|
|
4007
|
+
}
|
|
4008
|
+
val textLength = text?.length ?: 0
|
|
4009
|
+
start = start.coerceIn(0, textLength)
|
|
4010
|
+
end = end.coerceIn(start, textLength)
|
|
4011
|
+
return if (start < end) Pair(start, end) else null
|
|
4012
|
+
}
|
|
4013
|
+
|
|
4014
|
+
/**
|
|
4015
|
+
* Drops consumer-applied [key] spans inside [start]..[end], preserving the parts that fall
|
|
4016
|
+
* outside it. getSpans() also returns spans that merely OVERLAP the range, so removing them
|
|
4017
|
+
* outright would clear colour beyond it; the outside parts are re-applied instead. This is
|
|
4018
|
+
* what iOS gets implicitly, since an attributed-string attribute is per-character.
|
|
4019
|
+
*/
|
|
4020
|
+
private fun clearInlineStyleSpans(
|
|
4021
|
+
spannable: Editable,
|
|
4022
|
+
key: String,
|
|
4023
|
+
start: Int,
|
|
4024
|
+
end: Int,
|
|
4025
|
+
keepColor: Int? = null,
|
|
4026
|
+
) {
|
|
4027
|
+
val spans: Array<out Any> = when (key) {
|
|
4028
|
+
"color" -> spannable.getSpans(start, end, ExternalForegroundColorSpan::class.java)
|
|
4029
|
+
"backgroundColor" -> spannable.getSpans(start, end, ExternalBackgroundColorSpan::class.java)
|
|
4030
|
+
else -> return
|
|
4031
|
+
}
|
|
4032
|
+
spans.forEach { span ->
|
|
4033
|
+
val spanStart = spannable.getSpanStart(span)
|
|
4034
|
+
val spanEnd = spannable.getSpanEnd(span)
|
|
4035
|
+
val color = when (span) {
|
|
4036
|
+
is ExternalForegroundColorSpan -> span.color
|
|
4037
|
+
is ExternalBackgroundColorSpan -> span.color
|
|
4038
|
+
else -> return@forEach
|
|
4039
|
+
}
|
|
4040
|
+
// Leave a span that already carries the colour being applied. Trimming and re-setting
|
|
4041
|
+
// it per keystroke would shatter one run into a chain of one-character spans, each
|
|
4042
|
+
// emitting its own <color=…> tag.
|
|
4043
|
+
if (keepColor != null && color == keepColor) return@forEach
|
|
4044
|
+
spannable.removeSpan(span)
|
|
4045
|
+
if (spanStart < start) {
|
|
4046
|
+
spannable.setSpan(
|
|
4047
|
+
if (key == "color") ExternalForegroundColorSpan(color) else ExternalBackgroundColorSpan(color),
|
|
4048
|
+
spanStart, start, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
|
|
4049
|
+
)
|
|
4050
|
+
}
|
|
4051
|
+
if (spanEnd > end) {
|
|
4052
|
+
spannable.setSpan(
|
|
4053
|
+
if (key == "color") ExternalForegroundColorSpan(color) else ExternalBackgroundColorSpan(color),
|
|
4054
|
+
end, spanEnd, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
|
|
4055
|
+
)
|
|
4056
|
+
}
|
|
4057
|
+
}
|
|
4058
|
+
}
|
|
4059
|
+
|
|
4060
|
+
/**
|
|
4061
|
+
* Applies an arbitrary inline style to the current selection, skipping mentions so a
|
|
4062
|
+
* consumer's formatter cannot recolour a mention (§8.2 S4). [color] is an ARGB int from
|
|
4063
|
+
* RN's processColor(). Styles of *different* kinds compose (S3), but a second colour
|
|
4064
|
+
* replaces the first — a character has exactly one foreground colour.
|
|
4065
|
+
*
|
|
4066
|
+
* ponytail: colours are the only inline style a trailing-button formatter needs today —
|
|
4067
|
+
* widen the `when` if fonts/spacing ever follow.
|
|
4068
|
+
*/
|
|
4069
|
+
fun applyInlineStyle(key: String, color: Int) {
|
|
4070
|
+
if (key != "color" && key != "backgroundColor") return
|
|
4071
|
+
|
|
4072
|
+
// No selection: style what gets typed next, matching the built-in bold/italic/code
|
|
4073
|
+
// type-ahead rather than silently doing nothing.
|
|
4074
|
+
val target = styleTargetRange()
|
|
4075
|
+
if (target == null) {
|
|
4076
|
+
if (key == "color") {
|
|
4077
|
+
pendingExternalColor = color
|
|
4078
|
+
pendingStyles.add("externalColor")
|
|
4079
|
+
} else {
|
|
4080
|
+
pendingExternalBackgroundColor = color
|
|
4081
|
+
pendingStyles.add("externalBackgroundColor")
|
|
4082
|
+
}
|
|
4083
|
+
pendingStylesInsertPos = selectionStart
|
|
4084
|
+
return
|
|
4085
|
+
}
|
|
4086
|
+
|
|
4087
|
+
val (start, end) = target
|
|
4088
|
+
val spannable = text as? Editable ?: return
|
|
4089
|
+
|
|
4090
|
+
isInternalChange = true
|
|
4091
|
+
nonMentionRanges(start, end, spannable).forEach { range ->
|
|
4092
|
+
val rangeStart = range.first
|
|
4093
|
+
val rangeEnd = range.last + 1
|
|
4094
|
+
// Recolouring must REPLACE, not stack. Leaving the old span underneath makes
|
|
4095
|
+
// extraction report two overlapping textColor ranges for the same characters, and the
|
|
4096
|
+
// JS side keeps whichever it sees first — so the earlier colour wins in the sent
|
|
4097
|
+
// message even though the newer one is what the composer paints.
|
|
4098
|
+
clearInlineStyleSpans(spannable, key, rangeStart, rangeEnd)
|
|
4099
|
+
val span: Any =
|
|
4100
|
+
if (key == "color") ExternalForegroundColorSpan(color)
|
|
4101
|
+
else ExternalBackgroundColorSpan(color)
|
|
4102
|
+
spannable.setSpan(span, rangeStart, rangeEnd, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
|
|
4103
|
+
}
|
|
4104
|
+
setSelection(start, end)
|
|
4105
|
+
isInternalChange = false
|
|
4106
|
+
invalidate()
|
|
4107
|
+
sendContentChange()
|
|
4108
|
+
}
|
|
4109
|
+
|
|
4110
|
+
/**
|
|
4111
|
+
* Removes a previously applied inline style from the current selection. Only consumer-applied
|
|
4112
|
+
* spans are removed — mention/link/code colouring is left intact.
|
|
4113
|
+
*/
|
|
4114
|
+
fun removeInlineStyle(key: String) {
|
|
4115
|
+
// No selection: cancel the pending type-ahead style instead of no-oping.
|
|
4116
|
+
val target = styleTargetRange()
|
|
4117
|
+
if (target == null) {
|
|
4118
|
+
if (key == "color") {
|
|
4119
|
+
pendingExternalColor = null
|
|
4120
|
+
pendingStyles.remove("externalColor")
|
|
4121
|
+
} else {
|
|
4122
|
+
pendingExternalBackgroundColor = null
|
|
4123
|
+
pendingStyles.remove("externalBackgroundColor")
|
|
4124
|
+
}
|
|
4125
|
+
// Clearing the pending colour is not enough. A colour span applied by type-ahead is
|
|
4126
|
+
// SPAN_EXCLUSIVE_INCLUSIVE so it grows with typing — a span ending exactly at the cursor
|
|
4127
|
+
// would keep swallowing everything typed after the user asked for NO colour. Pin it shut
|
|
4128
|
+
// by re-applying it as EXCLUSIVE_EXCLUSIVE.
|
|
4129
|
+
(text as? Editable)?.let { s ->
|
|
4130
|
+
val cursor = selectionStart.coerceIn(0, s.length)
|
|
4131
|
+
val spans: Array<out Any> =
|
|
4132
|
+
if (key == "color") s.getSpans(cursor, cursor, ExternalForegroundColorSpan::class.java)
|
|
4133
|
+
else s.getSpans(cursor, cursor, ExternalBackgroundColorSpan::class.java)
|
|
4134
|
+
spans.forEach { span ->
|
|
4135
|
+
val spanStart = s.getSpanStart(span)
|
|
4136
|
+
val spanEnd = s.getSpanEnd(span)
|
|
4137
|
+
if (spanEnd != cursor || spanStart >= spanEnd) return@forEach
|
|
4138
|
+
val colour = when (span) {
|
|
4139
|
+
is ExternalForegroundColorSpan -> span.color
|
|
4140
|
+
is ExternalBackgroundColorSpan -> span.color
|
|
4141
|
+
else -> return@forEach
|
|
4142
|
+
}
|
|
4143
|
+
isInternalChange = true
|
|
4144
|
+
s.removeSpan(span)
|
|
4145
|
+
s.setSpan(
|
|
4146
|
+
if (key == "color") ExternalForegroundColorSpan(colour)
|
|
4147
|
+
else ExternalBackgroundColorSpan(colour),
|
|
4148
|
+
spanStart, spanEnd, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
|
|
4149
|
+
)
|
|
4150
|
+
isInternalChange = false
|
|
4151
|
+
}
|
|
4152
|
+
}
|
|
4153
|
+
return
|
|
4154
|
+
}
|
|
4155
|
+
|
|
4156
|
+
val (start, end) = target
|
|
4157
|
+
val spannable = text as? Editable ?: return
|
|
4158
|
+
|
|
4159
|
+
val spans: Array<out Any> = when (key) {
|
|
4160
|
+
"color" -> spannable.getSpans(start, end, ExternalForegroundColorSpan::class.java)
|
|
4161
|
+
"backgroundColor" -> spannable.getSpans(start, end, ExternalBackgroundColorSpan::class.java)
|
|
4162
|
+
else -> return
|
|
4163
|
+
}
|
|
4164
|
+
if (spans.isEmpty()) return
|
|
4165
|
+
|
|
4166
|
+
isInternalChange = true
|
|
4167
|
+
clearInlineStyleSpans(spannable, key, start, end)
|
|
4168
|
+
setSelection(start, end)
|
|
4169
|
+
isInternalChange = false
|
|
4170
|
+
invalidate()
|
|
4171
|
+
sendContentChange()
|
|
4172
|
+
}
|
|
4173
|
+
|
|
4174
|
+
/**
|
|
4175
|
+
* Current mention ranges in "clean" (ZWS-stripped) coordinates — the same space
|
|
4176
|
+
* setMentionRanges() accepts and JS works in. Emitted on every content change so a consumer
|
|
4177
|
+
* formatter can read back where mentions ended up after edits (§8.2 S4).
|
|
4178
|
+
*/
|
|
4179
|
+
private fun currentMentionRangesJson(): String {
|
|
4180
|
+
val spannable = text as? Spanned ?: return "[]"
|
|
4181
|
+
val spans = spannable.getSpans(0, spannable.length, MentionSpan::class.java)
|
|
4182
|
+
if (spans.isEmpty()) return "[]"
|
|
4183
|
+
|
|
4184
|
+
val raw = spannable.toString()
|
|
4185
|
+
val rawToClean = IntArray(raw.length + 1)
|
|
4186
|
+
var clean = 0
|
|
4187
|
+
for (i in raw.indices) {
|
|
4188
|
+
rawToClean[i] = clean
|
|
4189
|
+
if (raw[i] != '\u200B') clean++
|
|
4190
|
+
}
|
|
4191
|
+
rawToClean[raw.length] = clean
|
|
4192
|
+
|
|
4193
|
+
return spans
|
|
4194
|
+
.map { Pair(spannable.getSpanStart(it), spannable.getSpanEnd(it)) }
|
|
4195
|
+
.filter { it.first >= 0 && it.second in (it.first + 1)..raw.length }
|
|
4196
|
+
.sortedBy { it.first }
|
|
4197
|
+
.joinToString(",", "[", "]") {
|
|
4198
|
+
"""{"start":${rawToClean[it.first]},"end":${rawToClean[it.second]}}"""
|
|
4199
|
+
}
|
|
4200
|
+
}
|
|
4201
|
+
|
|
3770
4202
|
fun onHighlightClick() {
|
|
3771
4203
|
// Use saved selection if current selection is empty
|
|
3772
4204
|
var start = selectionStart
|
|
@@ -4172,6 +4604,7 @@ class RichTextEditorView(context: Context) : androidx.appcompat.widget.AppCompat
|
|
|
4172
4604
|
|
|
4173
4605
|
// Snapshot mention spans before text modification (Req 5.1, 5.2, 5.7)
|
|
4174
4606
|
val mentionSnapshots = snapshotMentionSpans(lineStart, lineEnd)
|
|
4607
|
+
val colorSnapshots = snapshotInlineColorSpans(lineStart, lineEnd)
|
|
4175
4608
|
|
|
4176
4609
|
isInternalChange = true
|
|
4177
4610
|
val editable = text ?: return
|
|
@@ -4182,6 +4615,7 @@ class RichTextEditorView(context: Context) : androidx.appcompat.widget.AppCompat
|
|
|
4182
4615
|
val offsetDelta = newText.length - (lineEnd - lineStart)
|
|
4183
4616
|
restoreMentionSpans(editable, mentionSnapshots, offsetDelta)
|
|
4184
4617
|
}
|
|
4618
|
+
restoreInlineColorSpans(editable, colorSnapshots, lineStart, selectedText, newText)
|
|
4185
4619
|
|
|
4186
4620
|
setSelection(lineStart + newText.length)
|
|
4187
4621
|
renumberNumberedLists()
|
|
@@ -4273,6 +4707,7 @@ class RichTextEditorView(context: Context) : androidx.appcompat.widget.AppCompat
|
|
|
4273
4707
|
|
|
4274
4708
|
// Snapshot mention spans before text modification (Req 5.1, 5.2, 5.7)
|
|
4275
4709
|
val mentionSnapshots = snapshotMentionSpans(lineStart, lineEnd)
|
|
4710
|
+
val colorSnapshots = snapshotInlineColorSpans(lineStart, lineEnd)
|
|
4276
4711
|
|
|
4277
4712
|
isInternalChange = true
|
|
4278
4713
|
val editable = text ?: return
|
|
@@ -4283,6 +4718,7 @@ class RichTextEditorView(context: Context) : androidx.appcompat.widget.AppCompat
|
|
|
4283
4718
|
val offsetDelta = newText.length - (lineEnd - lineStart)
|
|
4284
4719
|
restoreMentionSpans(editable, mentionSnapshots, offsetDelta)
|
|
4285
4720
|
}
|
|
4721
|
+
restoreInlineColorSpans(editable, colorSnapshots, lineStart, selectedText, newText)
|
|
4286
4722
|
|
|
4287
4723
|
setSelection(lineStart + newText.length)
|
|
4288
4724
|
renumberNumberedLists()
|
|
@@ -4631,6 +5067,79 @@ class RichTextEditorView(context: Context) : androidx.appcompat.widget.AppCompat
|
|
|
4631
5067
|
}
|
|
4632
5068
|
}
|
|
4633
5069
|
|
|
5070
|
+
/** A consumer colour span captured before a block-prefix rewrite. */
|
|
5071
|
+
private class ColorSnapshot(val start: Int, val end: Int, val color: Int, val isBackground: Boolean)
|
|
5072
|
+
|
|
5073
|
+
private fun snapshotInlineColorSpans(start: Int, end: Int): List<ColorSnapshot> {
|
|
5074
|
+
val s = text as? Spanned ?: return emptyList()
|
|
5075
|
+
val out = ArrayList<ColorSnapshot>()
|
|
5076
|
+
s.getSpans(start, end, ExternalForegroundColorSpan::class.java).forEach {
|
|
5077
|
+
out.add(ColorSnapshot(s.getSpanStart(it), s.getSpanEnd(it), it.color, false))
|
|
5078
|
+
}
|
|
5079
|
+
s.getSpans(start, end, ExternalBackgroundColorSpan::class.java).forEach {
|
|
5080
|
+
out.add(ColorSnapshot(s.getSpanStart(it), s.getSpanEnd(it), it.color, true))
|
|
5081
|
+
}
|
|
5082
|
+
return out
|
|
5083
|
+
}
|
|
5084
|
+
|
|
5085
|
+
/**
|
|
5086
|
+
* Re-applies snapshotted consumer colours after a block-prefix rewrite. replace() drops every
|
|
5087
|
+
* span in the replaced range, so without this the user's colour disappears the moment the line
|
|
5088
|
+
* becomes a list item / quote / checklist.
|
|
5089
|
+
*
|
|
5090
|
+
* Positions cannot shift by one uniform delta the way restoreMentionSpans() assumes: each line's
|
|
5091
|
+
* prefix can grow or shrink by a different amount (`• ` → `1. `, quote kept, empty lines
|
|
5092
|
+
* untouched). The prefix rewrite never touches the line's *content*, so the common suffix of the
|
|
5093
|
+
* old and new line is that content — which gives each line's prefix length on both sides, and
|
|
5094
|
+
* with it an exact content-character mapping.
|
|
5095
|
+
*/
|
|
5096
|
+
private fun restoreInlineColorSpans(
|
|
5097
|
+
editable: Editable,
|
|
5098
|
+
snapshots: List<ColorSnapshot>,
|
|
5099
|
+
lineStart: Int,
|
|
5100
|
+
oldRangeText: String,
|
|
5101
|
+
newRangeText: String,
|
|
5102
|
+
) {
|
|
5103
|
+
if (snapshots.isEmpty()) return
|
|
5104
|
+
val oldLines = oldRangeText.split("\n")
|
|
5105
|
+
val newLines = newRangeText.split("\n")
|
|
5106
|
+
|
|
5107
|
+
// old absolute offset -> new absolute offset, for content characters only
|
|
5108
|
+
val map = HashMap<Int, Int>()
|
|
5109
|
+
var oldPos = lineStart
|
|
5110
|
+
var newPos = lineStart
|
|
5111
|
+
for (i in oldLines.indices) {
|
|
5112
|
+
val oldLine = oldLines[i]
|
|
5113
|
+
val newLine = newLines.getOrElse(i) { oldLine }
|
|
5114
|
+
var contentLen = 0
|
|
5115
|
+
while (contentLen < oldLine.length && contentLen < newLine.length &&
|
|
5116
|
+
oldLine[oldLine.length - 1 - contentLen] == newLine[newLine.length - 1 - contentLen]
|
|
5117
|
+
) {
|
|
5118
|
+
contentLen++
|
|
5119
|
+
}
|
|
5120
|
+
val oldPrefix = oldLine.length - contentLen
|
|
5121
|
+
val newPrefix = newLine.length - contentLen
|
|
5122
|
+
for (c in 0..contentLen) map[oldPos + oldPrefix + c] = newPos + newPrefix + c
|
|
5123
|
+
oldPos += oldLine.length + 1
|
|
5124
|
+
newPos += newLine.length + 1
|
|
5125
|
+
}
|
|
5126
|
+
|
|
5127
|
+
val oldRangeEnd = lineStart + oldRangeText.length
|
|
5128
|
+
for (snap in snapshots) {
|
|
5129
|
+
// getSpans() also returns spans overhanging the rewritten range; only the part inside
|
|
5130
|
+
// it was destroyed, so clamp and restore just that.
|
|
5131
|
+
val newStart = map[snap.start.coerceIn(lineStart, oldRangeEnd)] ?: continue
|
|
5132
|
+
val newEnd = map[snap.end.coerceIn(lineStart, oldRangeEnd)] ?: continue
|
|
5133
|
+
if (newEnd > newStart && newStart >= 0 && newEnd <= editable.length) {
|
|
5134
|
+
editable.setSpan(
|
|
5135
|
+
if (snap.isBackground) ExternalBackgroundColorSpan(snap.color)
|
|
5136
|
+
else ExternalForegroundColorSpan(snap.color),
|
|
5137
|
+
newStart, newEnd, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
|
|
5138
|
+
)
|
|
5139
|
+
}
|
|
5140
|
+
}
|
|
5141
|
+
}
|
|
5142
|
+
|
|
4634
5143
|
private fun toggleQuote() {
|
|
4635
5144
|
val (lineStart, lineEnd) = getLineRange()
|
|
4636
5145
|
val currentText = text?.toString() ?: return
|
|
@@ -4686,6 +5195,7 @@ class RichTextEditorView(context: Context) : androidx.appcompat.widget.AppCompat
|
|
|
4686
5195
|
|
|
4687
5196
|
// Snapshot mention spans before text modification (Req 5.1, 5.3, 5.7)
|
|
4688
5197
|
val mentionSnapshots = snapshotMentionSpans(lineStart, lineEnd)
|
|
5198
|
+
val colorSnapshots = snapshotInlineColorSpans(lineStart, lineEnd)
|
|
4689
5199
|
|
|
4690
5200
|
isInternalChange = true
|
|
4691
5201
|
val editable = text ?: return
|
|
@@ -4698,15 +5208,18 @@ class RichTextEditorView(context: Context) : androidx.appcompat.widget.AppCompat
|
|
|
4698
5208
|
if (mentionSnapshots.isNotEmpty()) {
|
|
4699
5209
|
restoreMentionSpans(editable, mentionSnapshots, -quotePrefix.length)
|
|
4700
5210
|
}
|
|
5211
|
+
restoreInlineColorSpans(editable, colorSnapshots, lineStart, lineText, unquoted)
|
|
4701
5212
|
} else {
|
|
4702
5213
|
// Add quote prefix — preserve existing list prefix
|
|
4703
|
-
|
|
5214
|
+
val quoted = "$quotePrefix$lineText"
|
|
5215
|
+
editable.replace(lineStart, lineEnd, quoted)
|
|
4704
5216
|
// Make ▎ invisible — bar is custom-drawn in onDraw()
|
|
4705
5217
|
editable.setSpan(ForegroundColorSpan(Color.TRANSPARENT), lineStart, lineStart + 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
|
|
4706
5218
|
// Restore mention spans shifted forward by prefix length
|
|
4707
5219
|
if (mentionSnapshots.isNotEmpty()) {
|
|
4708
5220
|
restoreMentionSpans(editable, mentionSnapshots, quotePrefix.length)
|
|
4709
5221
|
}
|
|
5222
|
+
restoreInlineColorSpans(editable, colorSnapshots, lineStart, lineText, quoted)
|
|
4710
5223
|
}
|
|
4711
5224
|
isInternalChange = false
|
|
4712
5225
|
applyListIndentation()
|
|
@@ -4775,9 +5288,13 @@ class RichTextEditorView(context: Context) : androidx.appcompat.widget.AppCompat
|
|
|
4775
5288
|
editable.delete(lineStart, lineStart + 2)
|
|
4776
5289
|
}
|
|
4777
5290
|
else -> {
|
|
4778
|
-
// Add checklist
|
|
5291
|
+
// Add checklist. Only this branch needs colour re-applied — delete() above shifts
|
|
5292
|
+
// spans rather than dropping them.
|
|
4779
5293
|
val cleanLine = removeExistingPrefix(lineText)
|
|
4780
|
-
|
|
5294
|
+
val checked = "☐ $cleanLine"
|
|
5295
|
+
val colorSnapshots = snapshotInlineColorSpans(lineStart, lineEnd)
|
|
5296
|
+
editable.replace(lineStart, lineEnd, checked)
|
|
5297
|
+
restoreInlineColorSpans(editable, colorSnapshots, lineStart, lineText, checked)
|
|
4781
5298
|
}
|
|
4782
5299
|
}
|
|
4783
5300
|
isInternalChange = false
|
|
@@ -4997,6 +5514,26 @@ class RichTextEditorView(context: Context) : androidx.appcompat.widget.AppCompat
|
|
|
4997
5514
|
sendContentChange()
|
|
4998
5515
|
}
|
|
4999
5516
|
|
|
5517
|
+
/**
|
|
5518
|
+
* Replaces the current selection with plain text, or inserts at the caret when nothing is
|
|
5519
|
+
* selected — the editor-side half of a consumer's toolbar button.
|
|
5520
|
+
*
|
|
5521
|
+
* The run is inserted without carrying the spans of whatever it replaced, so a consumer's own
|
|
5522
|
+
* wire token lands as plain characters rather than inheriting the styling around it.
|
|
5523
|
+
*/
|
|
5524
|
+
fun replaceSelection(replacement: String) {
|
|
5525
|
+
val editable = text ?: return
|
|
5526
|
+
val start = selectionStart.coerceAtMost(selectionEnd).coerceIn(0, editable.length)
|
|
5527
|
+
val end = selectionStart.coerceAtLeast(selectionEnd).coerceIn(0, editable.length)
|
|
5528
|
+
|
|
5529
|
+
isInternalChange = true
|
|
5530
|
+
editable.replace(start, end, replacement)
|
|
5531
|
+
isInternalChange = false
|
|
5532
|
+
|
|
5533
|
+
setSelection((start + replacement.length).coerceAtMost(text?.length ?: 0))
|
|
5534
|
+
sendContentChange()
|
|
5535
|
+
}
|
|
5536
|
+
|
|
5000
5537
|
fun removeLink(location: Int, length: Int) {
|
|
5001
5538
|
val editable = text ?: return
|
|
5002
5539
|
val end = location + length
|
|
@@ -5279,6 +5816,12 @@ class RichTextEditorView(context: Context) : androidx.appcompat.widget.AppCompat
|
|
|
5279
5816
|
pendingStyles.remove("code")
|
|
5280
5817
|
explicitlyOffStyles.remove("code")
|
|
5281
5818
|
explicitlyOffStyles.remove(key)
|
|
5819
|
+
// Drop an armed consumer colour, or the first character typed into the empty block
|
|
5820
|
+
// comes out coloured instead of the code block default (matches iOS).
|
|
5821
|
+
pendingExternalColor = null
|
|
5822
|
+
pendingExternalBackgroundColor = null
|
|
5823
|
+
pendingStyles.remove("externalColor")
|
|
5824
|
+
pendingStyles.remove("externalBackgroundColor")
|
|
5282
5825
|
// Insert ZWS with code block spans for immediate container
|
|
5283
5826
|
val zws = "\u200B"
|
|
5284
5827
|
val insertPos = selectionStart.coerceIn(0, s.length)
|
|
@@ -5394,6 +5937,18 @@ class RichTextEditorView(context: Context) : androidx.appcompat.widget.AppCompat
|
|
|
5394
5937
|
.forEach { s.removeSpan(it) }
|
|
5395
5938
|
s.getSpans(lineStart, lineEnd, ForegroundColorSpan::class.java)
|
|
5396
5939
|
.forEach { s.removeSpan(it) }
|
|
5940
|
+
// Consumer colour is a bare CharacterStyle, so the ForegroundColorSpan sweep above does
|
|
5941
|
+
// NOT catch it. Without this the span merely sits under the code block's own colour and
|
|
5942
|
+
// paints again the moment the block is toggled off — iOS drops it for good, and a code
|
|
5943
|
+
// block is destructive to every other inline style here too.
|
|
5944
|
+
s.getSpans(lineStart, lineEnd, ExternalForegroundColorSpan::class.java)
|
|
5945
|
+
.forEach { s.removeSpan(it) }
|
|
5946
|
+
s.getSpans(lineStart, lineEnd, ExternalBackgroundColorSpan::class.java)
|
|
5947
|
+
.forEach { s.removeSpan(it) }
|
|
5948
|
+
pendingExternalColor = null
|
|
5949
|
+
pendingExternalBackgroundColor = null
|
|
5950
|
+
pendingStyles.remove("externalColor")
|
|
5951
|
+
pendingStyles.remove("externalBackgroundColor")
|
|
5397
5952
|
s.getSpans(lineStart, lineEnd, AbsoluteSizeSpan::class.java)
|
|
5398
5953
|
.filter { s.getSpanStart(it) >= lineStart && s.getSpanEnd(it) <= lineEnd }
|
|
5399
5954
|
.forEach { s.removeSpan(it) }
|
|
@@ -5622,7 +6177,9 @@ class RichTextEditorView(context: Context) : androidx.appcompat.widget.AppCompat
|
|
|
5622
6177
|
spannable.getSpans(msStart, msEnd, android.text.style.StyleSpan::class.java)
|
|
5623
6178
|
.filter { it.style == android.graphics.Typeface.BOLD }
|
|
5624
6179
|
.forEach { spannable.removeSpan(it) }
|
|
5625
|
-
// Remove ForegroundColorSpan and BackgroundColorSpan on this mention range
|
|
6180
|
+
// Remove ForegroundColorSpan and BackgroundColorSpan on this mention range.
|
|
6181
|
+
// Consumer-applied colours are sibling types, not subclasses, so they are invisible
|
|
6182
|
+
// to these sweeps by construction — no filter needed.
|
|
5626
6183
|
spannable.getSpans(msStart, msEnd, android.text.style.ForegroundColorSpan::class.java)
|
|
5627
6184
|
.forEach { spannable.removeSpan(it) }
|
|
5628
6185
|
spannable.getSpans(msStart, msEnd, android.text.style.BackgroundColorSpan::class.java)
|