@cometchat/chat-uikit-react-native 5.4.0 → 5.5.0
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 +605 -6
- package/android/src/main/java/com/reactnativecometchatuikit/RichTextEditorViewManager.kt +20 -1
- package/ios/CometChatClipboardModule.m +5 -0
- package/ios/CometChatClipboardModule.swift +22 -0
- package/ios/RichTextEditorView.swift +648 -35
- package/ios/RichTextEditorViewManager.m +2 -0
- package/ios/RichTextEditorViewManager.swift +16 -0
- package/package.json +2 -1
- package/src/CometChatCompactMessageComposer/CometChatCompactMessageComposer.tsx +245 -55
- package/src/CometChatCompactMessageComposer/index.ts +2 -0
- package/src/CometChatConversations/CometChatConversations.tsx +291 -14
- package/src/CometChatMessageHeader/CometChatMessageHeader.tsx +181 -12
- package/src/CometChatMessageList/CometChatMessageList.tsx +568 -15
- package/src/CometChatMessageList/components/MessageModals.tsx +30 -0
- package/src/CometChatMessageList/components/MessageOptionsSheet.tsx +14 -1
- package/src/CometChatMessageList/components/MessageQuickActions.tsx +162 -0
- package/src/CometChatPinnedMessages/CometChatPinnedMessages.tsx +940 -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 +27 -1
- package/src/CometChatRichTextEditor/types.ts +19 -1
- package/src/CometChatSavedMessages/CometChatSavedMessages.tsx +634 -0
- package/src/CometChatSavedMessages/index.ts +5 -0
- package/src/CometChatSavedMessages/style.ts +159 -0
- package/src/CometChatSearch/CometChatSearch.tsx +47 -340
- package/src/CometChatThreadHeader/CometChatThreadHeader.tsx +22 -2
- package/src/index.ts +60 -0
- package/src/shared/CometChatUiKit/CometChatUIKit.ts +16 -0
- package/src/shared/base/Types.ts +5 -0
- package/src/shared/constants/UIKitConstants.ts +13 -0
- package/src/shared/events/CometChatUIEventHandler/CometChatUIEventHandler.ts +34 -0
- 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 +95 -8
- package/src/shared/formatters/index.ts +14 -0
- package/src/shared/formatters/richTextWireFormat.ts +165 -0
- package/src/shared/framework/MessageDataSource.tsx +296 -3
- 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/resources/en/translation.json +54 -0
- package/src/shared/utils/CometChatMessagePreview/CometChatMessagePreview.tsx +2 -1
- package/src/shared/utils/MarkdownUtils.ts +25 -4
- 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/SavedMessagesHelper.ts +231 -0
- package/src/shared/utils/ThreadSubscriptionHelper.ts +427 -0
- package/src/shared/utils/conversationUtils.ts +8 -1
- package/src/shared/utils/elementType.tsx +38 -0
- package/src/shared/utils/stripRichText.ts +7 -0
- package/src/shared/views/ClipboardPasteHandler/ClipboardPasteHandler.ts +21 -0
- package/src/shared/views/ClipboardPasteHandler/NativeCometChatClipboardModule.ts +6 -0
- package/src/shared/views/CometChatActionSheet/CometChatActionSheet.tsx +4 -0
- package/src/shared/views/CometChatAvatar/CometChatAvatar.tsx +17 -5
- package/src/shared/views/CometChatBottomSheet/CometChatBottomSheet.tsx +28 -14
- package/src/shared/views/CometChatConfirmDialog/CometChatConfirmDialog.tsx +2 -0
- package/src/shared/views/CometChatConversationStarter/CometChatConversationStarter.tsx +91 -20
- package/src/shared/views/CometChatList/CometChatList.tsx +6 -1
- 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/CometChatTextBubble/CometChatTextBubble.tsx +4 -3
- package/src/shared/views/ErrorEmptyView/ErrorEmptyView.tsx +4 -1
- package/src/theme/default/default.ts +6 -0
- package/src/theme/type.ts +4 -0
|
@@ -592,7 +592,13 @@ class RichTextView: UITextView {
|
|
|
592
592
|
return
|
|
593
593
|
}
|
|
594
594
|
|
|
595
|
-
|
|
595
|
+
// Prefer our private rich-text representation when the copy came from CometChat —
|
|
596
|
+
// it carries the <color=…> wire format that the plain-text type deliberately omits.
|
|
597
|
+
let richPaste = UIPasteboard.general.data(
|
|
598
|
+
forPasteboardType: CometChatRichTextPasteboardType
|
|
599
|
+
).flatMap { String(data: $0, encoding: .utf8) }
|
|
600
|
+
|
|
601
|
+
guard let pastedString = richPaste ?? UIPasteboard.general.string else {
|
|
596
602
|
super.paste(sender)
|
|
597
603
|
return
|
|
598
604
|
}
|
|
@@ -613,9 +619,6 @@ class RichTextView: UITextView {
|
|
|
613
619
|
}
|
|
614
620
|
|
|
615
621
|
private func disableAutofill() {
|
|
616
|
-
autocorrectionType = .no
|
|
617
|
-
autocapitalizationType = .none
|
|
618
|
-
spellCheckingType = .no
|
|
619
622
|
smartQuotesType = .no
|
|
620
623
|
smartDashesType = .no
|
|
621
624
|
smartInsertDeleteType = .no
|
|
@@ -637,6 +640,15 @@ class RichTextView: UITextView {
|
|
|
637
640
|
|
|
638
641
|
isSecureTextEntry = true
|
|
639
642
|
isSecureTextEntry = false
|
|
643
|
+
|
|
644
|
+
// The secure round-trip above rebuilds the input traits and leaves autocorrect
|
|
645
|
+
// and spell check suppressed, so the typing traits have to be re-asserted AFTER
|
|
646
|
+
// it — assigning them earlier in this method does not survive. `.default` and
|
|
647
|
+
// `.sentences` are the "let the platform decide" values, the same ones a plain
|
|
648
|
+
// UITextView / RN TextInput starts from; nothing is pinned on or off here.
|
|
649
|
+
autocorrectionType = .default
|
|
650
|
+
autocapitalizationType = .sentences
|
|
651
|
+
spellCheckingType = .default
|
|
640
652
|
}
|
|
641
653
|
|
|
642
654
|
/// Custom drawing for blockquote vertical bars (matches Android onDraw implementation).
|
|
@@ -748,7 +760,7 @@ class RichTextView: UITextView {
|
|
|
748
760
|
var quoteRegions: [(Int, Int)] = []
|
|
749
761
|
var offset = 0
|
|
750
762
|
for line in lines {
|
|
751
|
-
let lineEnd = offset + line.
|
|
763
|
+
let lineEnd = offset + (line as NSString).length
|
|
752
764
|
if line.hasPrefix("▎") {
|
|
753
765
|
if !quoteRegions.isEmpty && offset <= quoteRegions.last!.1 + 1 {
|
|
754
766
|
quoteRegions[quoteRegions.count - 1] = (quoteRegions.last!.0, lineEnd)
|
|
@@ -867,6 +879,56 @@ private let CodeBlockAttributeKey = NSAttributedString.Key("cometchat.codeBlock"
|
|
|
867
879
|
// affecting inline code or link styling.
|
|
868
880
|
private let MentionMarkerKey = NSAttributedString.Key("cometchat.mention")
|
|
869
881
|
|
|
882
|
+
// Mention foreground color (#6852D6). Same value as `inlineCodeTextColor`, kept separate
|
|
883
|
+
// so the two call sites read for what they mean.
|
|
884
|
+
private let mentionForegroundColor = UIColor(red: 104.0/255.0, green: 82.0/255.0, blue: 214.0/255.0, alpha: 1.0)
|
|
885
|
+
|
|
886
|
+
extension UIColor {
|
|
887
|
+
/// Parses `#rgb`, `#rrggbb` or `#aarrggbb` (leading `#` optional). nil for malformed input.
|
|
888
|
+
static func fromHexString(_ hex: String) -> UIColor? {
|
|
889
|
+
var s = hex.trimmingCharacters(in: .whitespacesAndNewlines)
|
|
890
|
+
if s.hasPrefix("#") { s = String(s.dropFirst()) }
|
|
891
|
+
if s.count == 3 { s = s.map { "\($0)\($0)" }.joined() }
|
|
892
|
+
guard s.count == 6 || s.count == 8, let val = UInt64(s, radix: 16) else { return nil }
|
|
893
|
+
let r, g, b, a: CGFloat
|
|
894
|
+
if s.count == 8 {
|
|
895
|
+
a = CGFloat((val >> 24) & 0xFF) / 255.0
|
|
896
|
+
r = CGFloat((val >> 16) & 0xFF) / 255.0
|
|
897
|
+
g = CGFloat((val >> 8) & 0xFF) / 255.0
|
|
898
|
+
b = CGFloat(val & 0xFF) / 255.0
|
|
899
|
+
} else {
|
|
900
|
+
a = 1.0
|
|
901
|
+
r = CGFloat((val >> 16) & 0xFF) / 255.0
|
|
902
|
+
g = CGFloat((val >> 8) & 0xFF) / 255.0
|
|
903
|
+
b = CGFloat(val & 0xFF) / 255.0
|
|
904
|
+
}
|
|
905
|
+
return UIColor(red: r, green: g, blue: b, alpha: a)
|
|
906
|
+
}
|
|
907
|
+
|
|
908
|
+
/// Serialises to lowercase `#rrggbb` for the JS wire format. Alpha is dropped — the wire
|
|
909
|
+
/// contract is `#rrggbb` only. Returns "" for colors with no RGB components (pattern colors).
|
|
910
|
+
func toHexString() -> String {
|
|
911
|
+
var r: CGFloat = 0, g: CGFloat = 0, b: CGFloat = 0, a: CGFloat = 0
|
|
912
|
+
guard getRed(&r, green: &g, blue: &b, alpha: &a) else { return "" }
|
|
913
|
+
let ri = Int((max(0, min(1, r)) * 255.0).rounded())
|
|
914
|
+
let gi = Int((max(0, min(1, g)) * 255.0).rounded())
|
|
915
|
+
let bi = Int((max(0, min(1, b)) * 255.0).rounded())
|
|
916
|
+
return String(format: "#%02x%02x%02x", ri, gi, bi)
|
|
917
|
+
}
|
|
918
|
+
}
|
|
919
|
+
|
|
920
|
+
// Markers for colors applied by a consumer formatter via applyInlineStyle(). They mirror
|
|
921
|
+
// .foregroundColor / .backgroundColor so those colors can be told apart from the ones the
|
|
922
|
+
// editor itself owns (mention purple, link blue, inline code) and survive mention re-styling.
|
|
923
|
+
private let ExternalForegroundKey = NSAttributedString.Key("cometchat.externalForeground")
|
|
924
|
+
|
|
925
|
+
// Sentinel placed in `pendingStyles` while a caret-armed consumer colour is active. The colour
|
|
926
|
+
// itself lives in `pendingExternalForeground`; the sentinel exists because three lifecycle sites
|
|
927
|
+
// are Set-gated and would never fire for a value stored outside the Set — which leaks the colour
|
|
928
|
+
// onto text typed elsewhere after a cursor jump.
|
|
929
|
+
private let ExternalColorPendingKey = "externalColor"
|
|
930
|
+
private let ExternalBackgroundKey = NSAttributedString.Key("cometchat.externalBackground")
|
|
931
|
+
|
|
870
932
|
// Code block visual constants (matching Android CodeBlockBorderSpan companion)
|
|
871
933
|
private let codeBlockCornerRadius: CGFloat = 6.0
|
|
872
934
|
private let codeBlockPadding: CGFloat = 12.0
|
|
@@ -969,6 +1031,48 @@ class RichTextEditorView: UIView, UITextViewDelegate, UIGestureRecognizerDelegat
|
|
|
969
1031
|
|
|
970
1032
|
@objc var showToolbar: Bool = true
|
|
971
1033
|
|
|
1034
|
+
// MARK: - Typing traits
|
|
1035
|
+
// Autocorrect / autocapitalization / spell check are never pinned to a fixed value.
|
|
1036
|
+
// In prose the text view is left at the UIKit defaults — the same behaviour a plain
|
|
1037
|
+
// RN `TextInput` gets for free, so the keyboard's own language and user settings
|
|
1038
|
+
// decide — and the editor drops all three by itself while the caret sits in code
|
|
1039
|
+
// (see `isCodeContext`), so autocorrect never rewrites a code sample.
|
|
1040
|
+
private var isCodeContext = false
|
|
1041
|
+
|
|
1042
|
+
|
|
1043
|
+
private func applyTypingTraits() {
|
|
1044
|
+
let correction: UITextAutocorrectionType
|
|
1045
|
+
let capitalization: UITextAutocapitalizationType
|
|
1046
|
+
let spellChecking: UITextSpellCheckingType
|
|
1047
|
+
|
|
1048
|
+
if isCodeContext {
|
|
1049
|
+
correction = .no
|
|
1050
|
+
capitalization = .none
|
|
1051
|
+
spellChecking = .no
|
|
1052
|
+
} else {
|
|
1053
|
+
correction = .default
|
|
1054
|
+
capitalization = .sentences
|
|
1055
|
+
spellChecking = .default
|
|
1056
|
+
}
|
|
1057
|
+
|
|
1058
|
+
guard textView.autocorrectionType != correction
|
|
1059
|
+
|| textView.autocapitalizationType != capitalization
|
|
1060
|
+
|| textView.spellCheckingType != spellChecking else { return }
|
|
1061
|
+
|
|
1062
|
+
textView.autocorrectionType = correction
|
|
1063
|
+
textView.autocapitalizationType = capitalization
|
|
1064
|
+
textView.spellCheckingType = spellChecking
|
|
1065
|
+
if textView.isFirstResponder {
|
|
1066
|
+
textView.reloadInputViews()
|
|
1067
|
+
}
|
|
1068
|
+
}
|
|
1069
|
+
|
|
1070
|
+
private func setCodeContext(_ inCode: Bool) {
|
|
1071
|
+
guard inCode != isCodeContext else { return }
|
|
1072
|
+
isCodeContext = inCode
|
|
1073
|
+
applyTypingTraits()
|
|
1074
|
+
}
|
|
1075
|
+
|
|
972
1076
|
/// When true, Bold/Italic/Underline/Strikethrough appear in the text selection context menu.
|
|
973
1077
|
@objc var showTextSelectionMenuItems: Bool = true {
|
|
974
1078
|
didSet {
|
|
@@ -1052,6 +1156,12 @@ class RichTextEditorView: UIView, UITextViewDelegate, UIGestureRecognizerDelegat
|
|
|
1052
1156
|
b = CGFloat(val & 0xFF) / 255.0
|
|
1053
1157
|
}
|
|
1054
1158
|
textView.textColor = UIColor(red: r, green: g, blue: b, alpha: a)
|
|
1159
|
+
// `textView.textColor` re-applies `.foregroundColor` across the ENTIRE
|
|
1160
|
+
// attributed string, wiping link blue, inline-code purple, mention purple,
|
|
1161
|
+
// the invisible blockquote bar and any consumer-applied inline colour. This
|
|
1162
|
+
// setter re-fires on every prop identity change, so rebuild them from their
|
|
1163
|
+
// marker attributes.
|
|
1164
|
+
reapplyMarkerForegroundColors()
|
|
1055
1165
|
}
|
|
1056
1166
|
}
|
|
1057
1167
|
updateContentSize()
|
|
@@ -1072,6 +1182,22 @@ class RichTextEditorView: UIView, UITextViewDelegate, UIGestureRecognizerDelegat
|
|
|
1072
1182
|
private var explicitlyOffStyles: Set<String> = []
|
|
1073
1183
|
private var pendingStylesInsertPos: Int = -1
|
|
1074
1184
|
|
|
1185
|
+
/// Text length as of the last selection change, so `textViewDidChangeSelection` can tell a
|
|
1186
|
+
/// caret move caused by TYPING from a deliberate reposition. Typing can advance the caret by
|
|
1187
|
+
/// more than one character — autocorrect replacing a word, predictive text, a multi-scalar
|
|
1188
|
+
/// emoji, a paste, an IME commit — and without this the anchor below treats every one of those
|
|
1189
|
+
/// as "the user moved the caret" and disarms the pending colour mid-sentence (ENG-38257).
|
|
1190
|
+
private var lastKnownTextLength: Int = 0
|
|
1191
|
+
|
|
1192
|
+
/// Caret-armed consumer inline colour (no selection). A UIColor cannot live in a
|
|
1193
|
+
/// `Set<String>` cleanly, so the value lives here while `ExternalColorPendingKey` goes into
|
|
1194
|
+
/// `pendingStyles` to drive the Set-gated lifecycle (cursor-jump invalidation, post-keystroke
|
|
1195
|
+
/// restore, empty-text reset). nil = nothing armed.
|
|
1196
|
+
private var pendingExternalForeground: UIColor? = nil
|
|
1197
|
+
/// True when the colour was cleared with the caret parked, so the next typed character is
|
|
1198
|
+
/// `UIColor.label` rather than inheriting a neighbouring colour.
|
|
1199
|
+
private var pendingExternalForegroundCleared: Bool = false
|
|
1200
|
+
|
|
1075
1201
|
/// Typing attributes to forcefully restore after all processing (including async
|
|
1076
1202
|
/// JS bridge callbacks like syncMentionRanges) completes. Set by autoContinueListOnEnter
|
|
1077
1203
|
/// so that inline styles (bold, italic, etc.) survive across list line continuation.
|
|
@@ -1093,9 +1219,36 @@ class RichTextEditorView: UIView, UITextViewDelegate, UIGestureRecognizerDelegat
|
|
|
1093
1219
|
if let strike = attrs[.strikethroughStyle] as? Int, strike != 0 {
|
|
1094
1220
|
pendingStyles.insert("strikethrough")
|
|
1095
1221
|
}
|
|
1222
|
+
captureExternalColorToPending()
|
|
1223
|
+
pendingStylesInsertPos = textView.selectedRange.location
|
|
1224
|
+
}
|
|
1225
|
+
|
|
1226
|
+
/// Re-anchors `pendingStylesInsertPos` to the current caret.
|
|
1227
|
+
///
|
|
1228
|
+
/// List operations capture pending styles BEFORE inserting the "1. "/"• "/quote prefix, then
|
|
1229
|
+
/// move the caret past it. That leaves the anchor stale by the prefix length, so the next
|
|
1230
|
+
/// keystroke falls outside the insertPos/insertPos+1 window and textViewDidChangeSelection
|
|
1231
|
+
/// invalidates every pending style — silently dropping an armed colour mid-line.
|
|
1232
|
+
private func syncPendingStyleAnchor() {
|
|
1233
|
+
guard !pendingStyles.isEmpty || !explicitlyOffStyles.isEmpty else { return }
|
|
1096
1234
|
pendingStylesInsertPos = textView.selectedRange.location
|
|
1097
1235
|
}
|
|
1098
1236
|
|
|
1237
|
+
/// Arms the consumer inline colour currently in `typingAttributes` as a pending style.
|
|
1238
|
+
///
|
|
1239
|
+
/// A consumer colour is persistent like bold/italic, so it must survive the `attributedText`
|
|
1240
|
+
/// resets that list operations perform — those wipe `typingAttributes` and rebuild them from
|
|
1241
|
+
/// `pendingStyles` alone. Needed on both paths: Enter-continuation (via
|
|
1242
|
+
/// `captureInlineStylesToPending`) and the list buttons, which don't call that.
|
|
1243
|
+
private func captureExternalColorToPending() {
|
|
1244
|
+
guard let color = textView.typingAttributes[ExternalForegroundKey] as? UIColor else {
|
|
1245
|
+
return
|
|
1246
|
+
}
|
|
1247
|
+
pendingExternalForeground = color
|
|
1248
|
+
pendingExternalForegroundCleared = false
|
|
1249
|
+
pendingStyles.insert(ExternalColorPendingKey)
|
|
1250
|
+
}
|
|
1251
|
+
|
|
1099
1252
|
override init(frame: CGRect) {
|
|
1100
1253
|
super.init(frame: frame)
|
|
1101
1254
|
setupView()
|
|
@@ -1328,25 +1481,48 @@ class RichTextEditorView: UIView, UITextViewDelegate, UIGestureRecognizerDelegat
|
|
|
1328
1481
|
savedSelectionRange = range
|
|
1329
1482
|
}
|
|
1330
1483
|
|
|
1484
|
+
// Text length is tracked BEFORE the isInternalChange guard, and unconditionally.
|
|
1485
|
+
//
|
|
1486
|
+
// Every programmatic mutation — clearing the composer on send, setText, list/quote prefix
|
|
1487
|
+
// insertion, the attributedText rebuilds applyInlineStyle performs — runs with
|
|
1488
|
+
// isInternalChange set. Capturing the length after the guard meant those never updated it,
|
|
1489
|
+
// so the counter kept the length of the PREVIOUS message. The next keystroke then computed
|
|
1490
|
+
// a negative `insertedCount` (1 - 6 = -5 after sending a six-character message), read as a
|
|
1491
|
+
// deliberate caret move, and disarmed the colour — which is why the colour survived on the
|
|
1492
|
+
// first character of the second message in a session and nothing after it (ENG-38248).
|
|
1493
|
+
let currentTextLength = (textView.text as NSString).length
|
|
1494
|
+
let insertedCount = currentTextLength - lastKnownTextLength
|
|
1495
|
+
lastKnownTextLength = currentTextLength
|
|
1496
|
+
|
|
1331
1497
|
// Skip toolbar/style updates during internal changes (e.g. autoContinueListOnEnter)
|
|
1332
1498
|
// to avoid emitting stale style state before typingAttributes are restored.
|
|
1333
1499
|
guard !isInternalChange else { return }
|
|
1334
1500
|
|
|
1335
1501
|
// Invalidate pending styles if cursor moved away from insert position
|
|
1336
1502
|
if pendingStylesInsertPos >= 0 && (!pendingStyles.isEmpty || !explicitlyOffStyles.isEmpty) {
|
|
1337
|
-
|
|
1503
|
+
// Typing advances the caret by however many characters actually landed: one for a plain
|
|
1504
|
+
// keystroke, but several for autocorrect replacing a word, predictive text, a multi-scalar
|
|
1505
|
+
// emoji, a paste, or an IME commit. Comparing against a hardcoded +1 classified all of
|
|
1506
|
+
// those as a deliberate caret move and disarmed the colour, so text typed from the first
|
|
1507
|
+
// autocorrection onwards sent as plain (ENG-38257).
|
|
1508
|
+
let isTypingAdvance = insertedCount > 0 && range.location == pendingStylesInsertPos + insertedCount
|
|
1509
|
+
if range.location != pendingStylesInsertPos && !isTypingAdvance {
|
|
1338
1510
|
// Preserve "codeBlock" across cursor movements — it's a persistent
|
|
1339
1511
|
// block-level style that should survive until explicitly toggled off
|
|
1340
1512
|
let hadCodeBlock = pendingStyles.contains("codeBlock")
|
|
1341
1513
|
pendingStyles.removeAll()
|
|
1342
1514
|
explicitlyOffStyles.removeAll()
|
|
1343
1515
|
pendingStylesInsertPos = -1
|
|
1516
|
+
// Drop the caret-armed colour too — otherwise it leaks onto text typed at the
|
|
1517
|
+
// new cursor position (the removeAll() above only drops the sentinel).
|
|
1518
|
+
pendingExternalForeground = nil
|
|
1519
|
+
pendingExternalForegroundCleared = false
|
|
1344
1520
|
if hadCodeBlock {
|
|
1345
1521
|
pendingStyles.insert("codeBlock")
|
|
1346
1522
|
pendingStylesInsertPos = range.location
|
|
1347
1523
|
}
|
|
1348
|
-
} else if
|
|
1349
|
-
//
|
|
1524
|
+
} else if isTypingAdvance {
|
|
1525
|
+
// Text was typed at the anchor — follow it, however many characters landed.
|
|
1350
1526
|
pendingStylesInsertPos = range.location
|
|
1351
1527
|
}
|
|
1352
1528
|
}
|
|
@@ -1379,7 +1555,11 @@ class RichTextEditorView: UIView, UITextViewDelegate, UIGestureRecognizerDelegat
|
|
|
1379
1555
|
var cleanAttrs = textView.typingAttributes
|
|
1380
1556
|
cleanAttrs.removeValue(forKey: .backgroundColor)
|
|
1381
1557
|
cleanAttrs.removeValue(forKey: MentionMarkerKey)
|
|
1382
|
-
|
|
1558
|
+
// Skip the RGB sniff entirely when a consumer inline colour is armed — a palette
|
|
1559
|
+
// swatch within ±0.05 of mention purple (#6852D6) would otherwise be silently
|
|
1560
|
+
// reverted to UIColor.label mid-typing.
|
|
1561
|
+
if cleanAttrs[ExternalForegroundKey] == nil,
|
|
1562
|
+
let fg = cleanAttrs[.foregroundColor] as? UIColor {
|
|
1383
1563
|
var r: CGFloat = 0, g: CGFloat = 0, b: CGFloat = 0, a: CGFloat = 0
|
|
1384
1564
|
fg.getRed(&r, green: &g, blue: &b, alpha: &a)
|
|
1385
1565
|
if abs(r - 104.0/255.0) < 0.05 && abs(g - 82.0/255.0) < 0.05 && abs(b - 214.0/255.0) < 0.05 && a > 0.9 {
|
|
@@ -1444,6 +1624,7 @@ class RichTextEditorView: UIView, UITextViewDelegate, UIGestureRecognizerDelegat
|
|
|
1444
1624
|
let checkRange = range.length > 0 ? range : NSRange(location: max(0, range.location - 1), length: 1)
|
|
1445
1625
|
|
|
1446
1626
|
guard checkRange.location >= 0, checkRange.location < attributedText.length else {
|
|
1627
|
+
setCodeContext(pendingStyles.contains("code") || pendingStyles.contains("codeBlock"))
|
|
1447
1628
|
onActiveStylesChange?([
|
|
1448
1629
|
"bold": pendingStyles.contains("bold") && !explicitlyOffStyles.contains("bold"),
|
|
1449
1630
|
"italic": pendingStyles.contains("italic") && !explicitlyOffStyles.contains("italic"),
|
|
@@ -1574,13 +1755,15 @@ class RichTextEditorView: UIView, UITextViewDelegate, UIGestureRecognizerDelegat
|
|
|
1574
1755
|
}
|
|
1575
1756
|
}
|
|
1576
1757
|
let codeBlockActive = hasCodeBlock || pendingStyles.contains("codeBlock")
|
|
1758
|
+
let codeActive = (hasCode || pendingStyles.contains("code") || pendingStyles.contains("codeBlock")) && !explicitlyOffStyles.contains("code")
|
|
1759
|
+
setCodeContext(codeActive || codeBlockActive)
|
|
1577
1760
|
|
|
1578
1761
|
onActiveStylesChange?([
|
|
1579
1762
|
"bold": (hasBold || pendingStyles.contains("bold")) && !explicitlyOffStyles.contains("bold"),
|
|
1580
1763
|
"italic": (hasItalic || pendingStyles.contains("italic")) && !explicitlyOffStyles.contains("italic"),
|
|
1581
1764
|
"underline": (hasUnderline || pendingStyles.contains("underline")) && !explicitlyOffStyles.contains("underline"),
|
|
1582
1765
|
"strikethrough": (hasStrikethrough || pendingStyles.contains("strikethrough")) && !explicitlyOffStyles.contains("strikethrough"),
|
|
1583
|
-
"code":
|
|
1766
|
+
"code": codeActive,
|
|
1584
1767
|
"codeBlock": codeBlockActive,
|
|
1585
1768
|
"highlight": hasHighlight,
|
|
1586
1769
|
"blockType": blockType,
|
|
@@ -1620,7 +1803,11 @@ class RichTextEditorView: UIView, UITextViewDelegate, UIGestureRecognizerDelegat
|
|
|
1620
1803
|
cleanAttrs.removeValue(forKey: .backgroundColor)
|
|
1621
1804
|
cleanAttrs.removeValue(forKey: MentionMarkerKey)
|
|
1622
1805
|
// Restore foreground if it's mention purple (#6852D6)
|
|
1623
|
-
|
|
1806
|
+
// Skip the RGB sniff entirely when a consumer inline colour is armed — a palette
|
|
1807
|
+
// swatch within ±0.05 of mention purple (#6852D6) would otherwise be silently
|
|
1808
|
+
// reverted to UIColor.label mid-typing.
|
|
1809
|
+
if cleanAttrs[ExternalForegroundKey] == nil,
|
|
1810
|
+
let fg = cleanAttrs[.foregroundColor] as? UIColor {
|
|
1624
1811
|
var r: CGFloat = 0, g: CGFloat = 0, b: CGFloat = 0, a: CGFloat = 0
|
|
1625
1812
|
fg.getRed(&r, green: &g, blue: &b, alpha: &a)
|
|
1626
1813
|
if abs(r - 104.0/255.0) < 0.05 && abs(g - 82.0/255.0) < 0.05 && abs(b - 214.0/255.0) < 0.05 && a > 0.9 {
|
|
@@ -1845,6 +2032,7 @@ class RichTextEditorView: UIView, UITextViewDelegate, UIGestureRecognizerDelegat
|
|
|
1845
2032
|
textView.selectedRange = NSRange(location: insertPos + prefix.count + 1, length: 0)
|
|
1846
2033
|
applyPendingStylesToTypingAttributes()
|
|
1847
2034
|
deferredTypingAttrs = textView.typingAttributes
|
|
2035
|
+
syncPendingStyleAnchor()
|
|
1848
2036
|
isInternalChange = false
|
|
1849
2037
|
renumberNumberedLists()
|
|
1850
2038
|
DispatchQueue.main.async { [weak self] in
|
|
@@ -1906,6 +2094,7 @@ class RichTextEditorView: UIView, UITextViewDelegate, UIGestureRecognizerDelegat
|
|
|
1906
2094
|
textView.selectedRange = NSRange(location: insertPos + 5, length: 0) // \n + ▎ + " • " = 5
|
|
1907
2095
|
applyPendingStylesToTypingAttributes()
|
|
1908
2096
|
deferredTypingAttrs = textView.typingAttributes
|
|
2097
|
+
syncPendingStyleAnchor()
|
|
1909
2098
|
isInternalChange = false
|
|
1910
2099
|
DispatchQueue.main.async { [weak self] in
|
|
1911
2100
|
self?.textView.setNeedsDisplay()
|
|
@@ -1942,6 +2131,7 @@ class RichTextEditorView: UIView, UITextViewDelegate, UIGestureRecognizerDelegat
|
|
|
1942
2131
|
// Restore inline styles via pendingStyles mechanism
|
|
1943
2132
|
applyPendingStylesToTypingAttributes()
|
|
1944
2133
|
deferredTypingAttrs = textView.typingAttributes
|
|
2134
|
+
syncPendingStyleAnchor()
|
|
1945
2135
|
isInternalChange = false
|
|
1946
2136
|
saveToUndoStack()
|
|
1947
2137
|
sendContentChange()
|
|
@@ -1984,6 +2174,7 @@ class RichTextEditorView: UIView, UITextViewDelegate, UIGestureRecognizerDelegat
|
|
|
1984
2174
|
renumberNumberedLists()
|
|
1985
2175
|
applyPendingStylesToTypingAttributes()
|
|
1986
2176
|
deferredTypingAttrs = textView.typingAttributes
|
|
2177
|
+
syncPendingStyleAnchor()
|
|
1987
2178
|
isInternalChange = false
|
|
1988
2179
|
saveToUndoStack()
|
|
1989
2180
|
sendContentChange()
|
|
@@ -2017,6 +2208,7 @@ class RichTextEditorView: UIView, UITextViewDelegate, UIGestureRecognizerDelegat
|
|
|
2017
2208
|
textView.insertText("\n☐ ")
|
|
2018
2209
|
applyPendingStylesToTypingAttributes()
|
|
2019
2210
|
deferredTypingAttrs = textView.typingAttributes
|
|
2211
|
+
syncPendingStyleAnchor()
|
|
2020
2212
|
isInternalChange = false
|
|
2021
2213
|
saveToUndoStack()
|
|
2022
2214
|
sendContentChange()
|
|
@@ -2077,6 +2269,7 @@ class RichTextEditorView: UIView, UITextViewDelegate, UIGestureRecognizerDelegat
|
|
|
2077
2269
|
textView.selectedRange = NSRange(location: insertPos + 3, length: 0)
|
|
2078
2270
|
applyPendingStylesToTypingAttributes()
|
|
2079
2271
|
deferredTypingAttrs = textView.typingAttributes
|
|
2272
|
+
syncPendingStyleAnchor()
|
|
2080
2273
|
isInternalChange = false
|
|
2081
2274
|
// Trigger redraw for blockquote bar asynchronously to avoid layout issues
|
|
2082
2275
|
DispatchQueue.main.async { [weak self] in
|
|
@@ -2249,6 +2442,9 @@ class RichTextEditorView: UIView, UITextViewDelegate, UIGestureRecognizerDelegat
|
|
|
2249
2442
|
let hadItalic = pendingStyles.contains("italic")
|
|
2250
2443
|
let hadUnderline = pendingStyles.contains("underline")
|
|
2251
2444
|
let hadStrikethrough = pendingStyles.contains("strikethrough")
|
|
2445
|
+
// A consumer colour is persistent like bold/italic — it survives until the
|
|
2446
|
+
// cursor moves away (handled in textViewDidChangeSelection).
|
|
2447
|
+
let hadExternalColor = pendingStyles.contains(ExternalColorPendingKey)
|
|
2252
2448
|
let hadOffBold = explicitlyOffStyles.contains("bold")
|
|
2253
2449
|
let hadOffItalic = explicitlyOffStyles.contains("italic")
|
|
2254
2450
|
let hadOffUnderline = explicitlyOffStyles.contains("underline")
|
|
@@ -2265,6 +2461,7 @@ class RichTextEditorView: UIView, UITextViewDelegate, UIGestureRecognizerDelegat
|
|
|
2265
2461
|
if hadItalic { pendingStyles.insert("italic") }
|
|
2266
2462
|
if hadUnderline { pendingStyles.insert("underline") }
|
|
2267
2463
|
if hadStrikethrough { pendingStyles.insert("strikethrough") }
|
|
2464
|
+
if hadExternalColor { pendingStyles.insert(ExternalColorPendingKey) }
|
|
2268
2465
|
if hadOffBold { explicitlyOffStyles.insert("bold") }
|
|
2269
2466
|
if hadOffItalic { explicitlyOffStyles.insert("italic") }
|
|
2270
2467
|
if hadOffUnderline { explicitlyOffStyles.insert("underline") }
|
|
@@ -2289,6 +2486,9 @@ class RichTextEditorView: UIView, UITextViewDelegate, UIGestureRecognizerDelegat
|
|
|
2289
2486
|
pendingStyles.removeAll()
|
|
2290
2487
|
explicitlyOffStyles.removeAll()
|
|
2291
2488
|
pendingStylesInsertPos = -1
|
|
2489
|
+
lastKnownTextLength = 0
|
|
2490
|
+
pendingExternalForeground = nil
|
|
2491
|
+
pendingExternalForegroundCleared = false
|
|
2292
2492
|
deferredTypingAttrs = nil
|
|
2293
2493
|
// Reset typingAttributes to plain so next typed text has no ghost formatting
|
|
2294
2494
|
let paragraphStyle = NSMutableParagraphStyle()
|
|
@@ -2353,7 +2553,11 @@ class RichTextEditorView: UIView, UITextViewDelegate, UIGestureRecognizerDelegat
|
|
|
2353
2553
|
var cleanAttrs = textView.typingAttributes
|
|
2354
2554
|
cleanAttrs.removeValue(forKey: .backgroundColor)
|
|
2355
2555
|
cleanAttrs.removeValue(forKey: MentionMarkerKey)
|
|
2356
|
-
|
|
2556
|
+
// Skip the RGB sniff entirely when a consumer inline colour is armed — a palette
|
|
2557
|
+
// swatch within ±0.05 of mention purple (#6852D6) would otherwise be silently
|
|
2558
|
+
// reverted to UIColor.label mid-typing.
|
|
2559
|
+
if cleanAttrs[ExternalForegroundKey] == nil,
|
|
2560
|
+
let fg = cleanAttrs[.foregroundColor] as? UIColor {
|
|
2357
2561
|
var r: CGFloat = 0, g: CGFloat = 0, b: CGFloat = 0, a: CGFloat = 0
|
|
2358
2562
|
fg.getRed(&r, green: &g, blue: &b, alpha: &a)
|
|
2359
2563
|
if abs(r - 104.0/255.0) < 0.05 && abs(g - 82.0/255.0) < 0.05 && abs(b - 214.0/255.0) < 0.05 && a > 0.9 {
|
|
@@ -2608,27 +2812,27 @@ class RichTextEditorView: UIView, UITextViewDelegate, UIGestureRecognizerDelegat
|
|
|
2608
2812
|
|
|
2609
2813
|
for line in lines {
|
|
2610
2814
|
// Check for blockquote + numbered: "▎ N. "
|
|
2611
|
-
if let quoteMatch = quoteNumberedRegex?.firstMatch(in: line, range: NSRange(location: 0, length: line.
|
|
2815
|
+
if let quoteMatch = quoteNumberedRegex?.firstMatch(in: line, range: NSRange(location: 0, length: (line as NSString).length)) {
|
|
2612
2816
|
counter += 1
|
|
2613
2817
|
let oldPrefix = (line as NSString).substring(with: quoteMatch.range)
|
|
2614
2818
|
let newPrefix = "▎ \(counter). "
|
|
2615
2819
|
if oldPrefix != newPrefix {
|
|
2616
2820
|
replacements.append((NSRange(location: offset, length: oldPrefix.count), newPrefix))
|
|
2617
2821
|
}
|
|
2618
|
-
} else if let match = numberedRegex?.firstMatch(in: line, range: NSRange(location: 0, length: line.
|
|
2822
|
+
} else if let match = numberedRegex?.firstMatch(in: line, range: NSRange(location: 0, length: (line as NSString).length)) {
|
|
2619
2823
|
counter += 1
|
|
2620
2824
|
let oldPrefix = (line as NSString).substring(with: match.range)
|
|
2621
2825
|
let newPrefix = "\(counter). "
|
|
2622
2826
|
if oldPrefix != newPrefix {
|
|
2623
2827
|
replacements.append((NSRange(location: offset, length: oldPrefix.count), newPrefix))
|
|
2624
2828
|
}
|
|
2625
|
-
} else if bulletRegex?.firstMatch(in: line, range: NSRange(location: 0, length: line.
|
|
2829
|
+
} else if bulletRegex?.firstMatch(in: line, range: NSRange(location: 0, length: (line as NSString).length)) != nil {
|
|
2626
2830
|
// Bullet lines don't reset the counter — ordered numbering
|
|
2627
2831
|
// continues across bullet interruptions (Slack behavior).
|
|
2628
2832
|
} else {
|
|
2629
2833
|
counter = 0
|
|
2630
2834
|
}
|
|
2631
|
-
offset += line.
|
|
2835
|
+
offset += (line as NSString).length + 1
|
|
2632
2836
|
}
|
|
2633
2837
|
|
|
2634
2838
|
if !replacements.isEmpty {
|
|
@@ -2888,7 +3092,8 @@ class RichTextEditorView: UIView, UITextViewDelegate, UIGestureRecognizerDelegat
|
|
|
2888
3092
|
let cleanText = (textView.text ?? "").replacingOccurrences(of: "\u{200B}", with: "")
|
|
2889
3093
|
onContentChange?([
|
|
2890
3094
|
"text": cleanText,
|
|
2891
|
-
"blocksJson": blocksJson
|
|
3095
|
+
"blocksJson": blocksJson,
|
|
3096
|
+
"mentionRangesJson": currentMentionRangesJson()
|
|
2892
3097
|
])
|
|
2893
3098
|
}
|
|
2894
3099
|
|
|
@@ -3415,7 +3620,7 @@ class RichTextEditorView: UIView, UITextViewDelegate, UIGestureRecognizerDelegat
|
|
|
3415
3620
|
}
|
|
3416
3621
|
|
|
3417
3622
|
if !hasMonospace {
|
|
3418
|
-
|
|
3623
|
+
paintInlineCodeForeground(on: mutableAttrString, range: range)
|
|
3419
3624
|
mutableAttrString.addAttribute(.backgroundColor, value: inlineCodeBgColor, range: range)
|
|
3420
3625
|
} else {
|
|
3421
3626
|
mutableAttrString.removeAttribute(.backgroundColor, range: range)
|
|
@@ -3432,6 +3637,237 @@ class RichTextEditorView: UIView, UITextViewDelegate, UIGestureRecognizerDelegat
|
|
|
3432
3637
|
updateToolbarButtonStates()
|
|
3433
3638
|
}
|
|
3434
3639
|
|
|
3640
|
+
// MARK: - Arbitrary inline styles (consumer formatters)
|
|
3641
|
+
|
|
3642
|
+
/// Maps a public style key to (the attribute it drives, its external marker).
|
|
3643
|
+
/// ponytail: colors are the only inline style a trailing-button formatter needs today —
|
|
3644
|
+
/// widen the switch if fonts/spacing ever follow.
|
|
3645
|
+
private static func inlineStyleAttributes(for key: String) -> (NSAttributedString.Key, NSAttributedString.Key)? {
|
|
3646
|
+
switch key {
|
|
3647
|
+
case "color": return (.foregroundColor, ExternalForegroundKey)
|
|
3648
|
+
case "backgroundColor": return (.backgroundColor, ExternalBackgroundKey)
|
|
3649
|
+
default: return nil
|
|
3650
|
+
}
|
|
3651
|
+
}
|
|
3652
|
+
|
|
3653
|
+
/// Unpacks an RN `processColor()` ARGB int.
|
|
3654
|
+
private static func color(fromARGB argb: Int) -> UIColor {
|
|
3655
|
+
UIColor(
|
|
3656
|
+
red: CGFloat((argb >> 16) & 0xFF) / 255.0,
|
|
3657
|
+
green: CGFloat((argb >> 8) & 0xFF) / 255.0,
|
|
3658
|
+
blue: CGFloat(argb & 0xFF) / 255.0,
|
|
3659
|
+
alpha: CGFloat((argb >> 24) & 0xFF) / 255.0
|
|
3660
|
+
)
|
|
3661
|
+
}
|
|
3662
|
+
|
|
3663
|
+
/// Sub-ranges of `range` a consumer style may touch: mentions AND links are protected.
|
|
3664
|
+
/// The general-purpose `nonMentionRanges` above only guards mentions, but Android's
|
|
3665
|
+
/// equivalent also guards `URLSpan` — and colour is exactly what a link uses to signal
|
|
3666
|
+
/// itself, so recolouring one would diverge visibly between platforms.
|
|
3667
|
+
private func inlineStyleTargetRanges(in range: NSRange, attributedText: NSAttributedString) -> [NSRange] {
|
|
3668
|
+
var protectedRanges: [NSRange] = []
|
|
3669
|
+
for key in [MentionMarkerKey, LinkURLAttributeKey] {
|
|
3670
|
+
attributedText.enumerateAttribute(key, in: range, options: []) { value, attrRange, _ in
|
|
3671
|
+
if value != nil { protectedRanges.append(attrRange) }
|
|
3672
|
+
}
|
|
3673
|
+
}
|
|
3674
|
+
if protectedRanges.isEmpty { return [range] }
|
|
3675
|
+
protectedRanges.sort { $0.location < $1.location }
|
|
3676
|
+
|
|
3677
|
+
var result: [NSRange] = []
|
|
3678
|
+
var cursor = range.location
|
|
3679
|
+
for pr in protectedRanges {
|
|
3680
|
+
if cursor < pr.location {
|
|
3681
|
+
result.append(NSRange(location: cursor, length: pr.location - cursor))
|
|
3682
|
+
}
|
|
3683
|
+
cursor = max(cursor, pr.location + pr.length)
|
|
3684
|
+
}
|
|
3685
|
+
let rangeEnd = range.location + range.length
|
|
3686
|
+
if cursor < rangeEnd {
|
|
3687
|
+
result.append(NSRange(location: cursor, length: rangeEnd - cursor))
|
|
3688
|
+
}
|
|
3689
|
+
return result
|
|
3690
|
+
}
|
|
3691
|
+
|
|
3692
|
+
/// Rebuilds every marker-derived `.foregroundColor` on the given string.
|
|
3693
|
+
///
|
|
3694
|
+
/// Call after any blanket foreground write (`textView.textColor = …`,
|
|
3695
|
+
/// `removeAttribute(.foregroundColor, range: fullRange)`), which UIKit applies across the
|
|
3696
|
+
/// whole attributed string. The order below IS the paint-order policy: the consumer's inline
|
|
3697
|
+
/// colour is laid down first, then the blockquote bar, links, inline code and mentions —
|
|
3698
|
+
/// which own their colour and always win.
|
|
3699
|
+
private func reapplyMarkerForegroundColors(on mutableAttr: NSMutableAttributedString) {
|
|
3700
|
+
let fullRange = NSRange(location: 0, length: mutableAttr.length)
|
|
3701
|
+
guard fullRange.length > 0 else { return }
|
|
3702
|
+
|
|
3703
|
+
// 1. Consumer-applied colour (marker is the source of truth, `.foregroundColor` is paint)
|
|
3704
|
+
mutableAttr.enumerateAttribute(ExternalForegroundKey, in: fullRange, options: []) { value, range, _ in
|
|
3705
|
+
if let color = value as? UIColor {
|
|
3706
|
+
mutableAttr.addAttribute(.foregroundColor, value: color, range: range)
|
|
3707
|
+
}
|
|
3708
|
+
}
|
|
3709
|
+
// 2. Blockquote ▎ (U+258E) stays invisible — the grey bar is custom-drawn in draw(_:)
|
|
3710
|
+
let str = mutableAttr.string as NSString
|
|
3711
|
+
for i in 0..<str.length where str.character(at: i) == 0x258E {
|
|
3712
|
+
mutableAttr.addAttribute(.foregroundColor, value: UIColor.clear, range: NSRange(location: i, length: 1))
|
|
3713
|
+
}
|
|
3714
|
+
// 3. Links
|
|
3715
|
+
mutableAttr.enumerateAttribute(LinkURLAttributeKey, in: fullRange, options: []) { value, range, _ in
|
|
3716
|
+
if value != nil {
|
|
3717
|
+
mutableAttr.addAttribute(.foregroundColor, value: UIColor.systemBlue, range: range)
|
|
3718
|
+
}
|
|
3719
|
+
}
|
|
3720
|
+
// 4. Inline code (identified by its background, matching setMentionRanges).
|
|
3721
|
+
// Skips any sub-range carrying a consumer colour: this step runs after step 1 and
|
|
3722
|
+
// would otherwise repaint it, hiding an applied colour in the composer while the
|
|
3723
|
+
// wire format — which serializes from the style ranges, not from `.foregroundColor` —
|
|
3724
|
+
// still carried it, so the colour reappeared only once sent (ENG-38248).
|
|
3725
|
+
// Colour wins over the inline-code default; the code background is untouched.
|
|
3726
|
+
mutableAttr.enumerateAttribute(.backgroundColor, in: fullRange, options: []) { value, range, _ in
|
|
3727
|
+
guard let bgColor = value as? UIColor, bgColor == inlineCodeBgColor else { return }
|
|
3728
|
+
mutableAttr.enumerateAttribute(ExternalForegroundKey, in: range, options: []) { external, subRange, _ in
|
|
3729
|
+
if external == nil {
|
|
3730
|
+
mutableAttr.addAttribute(.foregroundColor, value: inlineCodeTextColor, range: subRange)
|
|
3731
|
+
}
|
|
3732
|
+
}
|
|
3733
|
+
}
|
|
3734
|
+
// 5. Mentions
|
|
3735
|
+
mutableAttr.enumerateAttribute(MentionMarkerKey, in: fullRange, options: []) { value, range, _ in
|
|
3736
|
+
if value != nil {
|
|
3737
|
+
mutableAttr.addAttribute(.foregroundColor, value: mentionForegroundColor, range: range)
|
|
3738
|
+
}
|
|
3739
|
+
}
|
|
3740
|
+
}
|
|
3741
|
+
|
|
3742
|
+
/// `reapplyMarkerForegroundColors(on:)` against the live text view, preserving the selection
|
|
3743
|
+
/// and typing attributes that `attributedText =` would otherwise reset.
|
|
3744
|
+
private func reapplyMarkerForegroundColors() {
|
|
3745
|
+
guard let attributed = textView.attributedText, attributed.length > 0 else { return }
|
|
3746
|
+
let mutableAttr = NSMutableAttributedString(attributedString: attributed)
|
|
3747
|
+
reapplyMarkerForegroundColors(on: mutableAttr)
|
|
3748
|
+
let savedRange = textView.selectedRange
|
|
3749
|
+
let savedTypingAttrs = textView.typingAttributes
|
|
3750
|
+
isInternalChange = true
|
|
3751
|
+
textView.attributedText = mutableAttr
|
|
3752
|
+
textView.selectedRange = savedRange
|
|
3753
|
+
textView.typingAttributes = savedTypingAttrs
|
|
3754
|
+
isInternalChange = false
|
|
3755
|
+
textView.setNeedsDisplay()
|
|
3756
|
+
}
|
|
3757
|
+
|
|
3758
|
+
private func commitInlineStyle(_ mutableAttrString: NSMutableAttributedString, range: NSRange) {
|
|
3759
|
+
isInternalChange = true
|
|
3760
|
+
textView.attributedText = mutableAttrString
|
|
3761
|
+
textView.selectedRange = range
|
|
3762
|
+
isInternalChange = false
|
|
3763
|
+
saveToUndoStack()
|
|
3764
|
+
sendContentChange()
|
|
3765
|
+
}
|
|
3766
|
+
|
|
3767
|
+
/// Applies an arbitrary inline style to the current selection, skipping mentions so the
|
|
3768
|
+
/// consumer's formatter cannot recolour a mention (§8.2 S4). `value` is an ARGB int from
|
|
3769
|
+
/// RN's `processColor()`. Existing styles are left alone, so styles compose (S3).
|
|
3770
|
+
func applyInlineStyle(key: String, value: Int) {
|
|
3771
|
+
guard let (attribute, marker) = Self.inlineStyleAttributes(for: key) else { return }
|
|
3772
|
+
// An explicit colour choice overrides any deferred restore left by list continuation,
|
|
3773
|
+
// which would otherwise stomp the new colour a keystroke later.
|
|
3774
|
+
deferredTypingAttrs = nil
|
|
3775
|
+
let range = textView.selectedRange
|
|
3776
|
+
let color = Self.color(fromARGB: value)
|
|
3777
|
+
|
|
3778
|
+
// No selection: arm the colour for type-ahead, matching toggleCode/toggleBold rather
|
|
3779
|
+
// than silently doing nothing. Routed through pendingStyles so the Set-gated lifecycle
|
|
3780
|
+
// sites invalidate it on a cursor jump instead of leaking it onto text typed elsewhere.
|
|
3781
|
+
if range.length == 0 {
|
|
3782
|
+
guard attribute == .foregroundColor else {
|
|
3783
|
+
// ponytail: background type-ahead has no pendingStyles hook; it needs a selection.
|
|
3784
|
+
return
|
|
3785
|
+
}
|
|
3786
|
+
pendingExternalForeground = color
|
|
3787
|
+
pendingExternalForegroundCleared = false
|
|
3788
|
+
pendingStyles.insert(ExternalColorPendingKey)
|
|
3789
|
+
pendingStylesInsertPos = range.location
|
|
3790
|
+
applyPendingStylesToTypingAttributes()
|
|
3791
|
+
emitActiveStyles()
|
|
3792
|
+
return
|
|
3793
|
+
}
|
|
3794
|
+
|
|
3795
|
+
let mutableAttrString = NSMutableAttributedString(attributedString: textView.attributedText)
|
|
3796
|
+
for subRange in inlineStyleTargetRanges(in: range, attributedText: mutableAttrString) {
|
|
3797
|
+
mutableAttrString.addAttribute(attribute, value: color, range: subRange)
|
|
3798
|
+
mutableAttrString.addAttribute(marker, value: color, range: subRange)
|
|
3799
|
+
}
|
|
3800
|
+
commitInlineStyle(mutableAttrString, range: range)
|
|
3801
|
+
}
|
|
3802
|
+
|
|
3803
|
+
/// Removes a previously applied inline style from the current selection. Only ranges the
|
|
3804
|
+
/// consumer coloured are touched — mention/link/code colouring is left intact.
|
|
3805
|
+
func removeInlineStyle(key: String) {
|
|
3806
|
+
guard let (attribute, marker) = Self.inlineStyleAttributes(for: key) else { return }
|
|
3807
|
+
// Same as applyInlineStyle: an explicit clear outranks a deferred restore.
|
|
3808
|
+
deferredTypingAttrs = nil
|
|
3809
|
+
let range = textView.selectedRange
|
|
3810
|
+
|
|
3811
|
+
// No selection: cancel the pending type-ahead style instead of no-oping. The sentinel
|
|
3812
|
+
// goes in for the clear too, so the lifecycle sites see and invalidate the decision.
|
|
3813
|
+
if range.length == 0 {
|
|
3814
|
+
guard attribute == .foregroundColor else {
|
|
3815
|
+
textView.typingAttributes.removeValue(forKey: marker)
|
|
3816
|
+
textView.typingAttributes.removeValue(forKey: attribute)
|
|
3817
|
+
return
|
|
3818
|
+
}
|
|
3819
|
+
pendingExternalForeground = nil
|
|
3820
|
+
pendingExternalForegroundCleared = true
|
|
3821
|
+
pendingStyles.insert(ExternalColorPendingKey)
|
|
3822
|
+
pendingStylesInsertPos = range.location
|
|
3823
|
+
applyPendingStylesToTypingAttributes()
|
|
3824
|
+
emitActiveStyles()
|
|
3825
|
+
return
|
|
3826
|
+
}
|
|
3827
|
+
|
|
3828
|
+
let mutableAttrString = NSMutableAttributedString(attributedString: textView.attributedText)
|
|
3829
|
+
// Collect first, then mutate — removing the attribute being enumerated mid-walk is unsafe.
|
|
3830
|
+
var markedRanges: [NSRange] = []
|
|
3831
|
+
mutableAttrString.enumerateAttribute(marker, in: range, options: []) { value, subRange, _ in
|
|
3832
|
+
if value != nil { markedRanges.append(subRange) }
|
|
3833
|
+
}
|
|
3834
|
+
for subRange in markedRanges {
|
|
3835
|
+
mutableAttrString.removeAttribute(marker, range: subRange)
|
|
3836
|
+
mutableAttrString.removeAttribute(attribute, range: subRange)
|
|
3837
|
+
if attribute == .foregroundColor {
|
|
3838
|
+
mutableAttrString.addAttribute(.foregroundColor, value: UIColor.label, range: subRange)
|
|
3839
|
+
}
|
|
3840
|
+
}
|
|
3841
|
+
commitInlineStyle(mutableAttrString, range: range)
|
|
3842
|
+
}
|
|
3843
|
+
|
|
3844
|
+
/// Current mention ranges in "clean" (ZWS-stripped) coordinates — the same space
|
|
3845
|
+
/// `setMentionRanges` accepts and JS works in. Emitted on every content change so a
|
|
3846
|
+
/// consumer formatter can read back where mentions ended up after edits (§8.2 S4).
|
|
3847
|
+
private func currentMentionRangesJson() -> String {
|
|
3848
|
+
let attributed = NSAttributedString(attributedString: textView.attributedText)
|
|
3849
|
+
guard attributed.length > 0 else { return "[]" }
|
|
3850
|
+
|
|
3851
|
+
let raw = attributed.string as NSString
|
|
3852
|
+
var rawToClean = [Int](repeating: 0, count: raw.length + 1)
|
|
3853
|
+
var clean = 0
|
|
3854
|
+
for i in 0..<raw.length {
|
|
3855
|
+
rawToClean[i] = clean
|
|
3856
|
+
if raw.character(at: i) != 0x200B { clean += 1 }
|
|
3857
|
+
}
|
|
3858
|
+
rawToClean[raw.length] = clean
|
|
3859
|
+
|
|
3860
|
+
var ranges: [[String: Int]] = []
|
|
3861
|
+
attributed.enumerateAttribute(MentionMarkerKey, in: NSRange(location: 0, length: attributed.length), options: []) { value, range, _ in
|
|
3862
|
+
guard value != nil else { return }
|
|
3863
|
+
ranges.append(["start": rawToClean[range.location], "end": rawToClean[range.location + range.length]])
|
|
3864
|
+
}
|
|
3865
|
+
guard !ranges.isEmpty,
|
|
3866
|
+
let data = try? JSONSerialization.data(withJSONObject: ranges, options: []),
|
|
3867
|
+
let json = String(data: data, encoding: .utf8) else { return "[]" }
|
|
3868
|
+
return json
|
|
3869
|
+
}
|
|
3870
|
+
|
|
3435
3871
|
func toggleHighlight(color: String?) {
|
|
3436
3872
|
let range = textView.selectedRange
|
|
3437
3873
|
guard range.length > 0 else { return }
|
|
@@ -4060,6 +4496,24 @@ class RichTextEditorView: UIView, UITextViewDelegate, UIGestureRecognizerDelegat
|
|
|
4060
4496
|
|
|
4061
4497
|
/// Applies pending style toggles to typingAttributes so newly typed text inherits them.
|
|
4062
4498
|
/// Matches Android's pendingStyles mechanism via iOS's native typingAttributes API.
|
|
4499
|
+
/// Paints the inline-code foreground over `range`, but never over a sub-range carrying a
|
|
4500
|
+
/// consumer colour.
|
|
4501
|
+
///
|
|
4502
|
+
/// Inline code owns a foreground of its own, and several passes re-assert it — `toggleCode`,
|
|
4503
|
+
/// `setMentionRanges` (which the JS bridge calls on every content change), and the typed
|
|
4504
|
+
/// ``code`` markdown shortcut. Each one used to repaint the whole range, so a consumer colour
|
|
4505
|
+
/// applied to inline code was overwritten within a frame and the composer showed #6852D6
|
|
4506
|
+
/// instead of the chosen colour — while the wire, serialized from the style ranges rather than
|
|
4507
|
+
/// from `.foregroundColor`, still carried it. The colour therefore only "appeared" after
|
|
4508
|
+
/// sending (ENG-38248). Colour wins; the code background is untouched either way.
|
|
4509
|
+
private func paintInlineCodeForeground(on attr: NSMutableAttributedString, range: NSRange) {
|
|
4510
|
+
attr.enumerateAttribute(ExternalForegroundKey, in: range, options: []) { external, subRange, _ in
|
|
4511
|
+
if external == nil {
|
|
4512
|
+
attr.addAttribute(.foregroundColor, value: inlineCodeTextColor, range: subRange)
|
|
4513
|
+
}
|
|
4514
|
+
}
|
|
4515
|
+
}
|
|
4516
|
+
|
|
4063
4517
|
private func applyPendingStylesToTypingAttributes() {
|
|
4064
4518
|
var attrs = textView.typingAttributes
|
|
4065
4519
|
var baseFont = attrs[.font] as? UIFont ?? UIFont.systemFont(ofSize: 16)
|
|
@@ -4127,6 +4581,19 @@ class RichTextEditorView: UIView, UITextViewDelegate, UIGestureRecognizerDelegat
|
|
|
4127
4581
|
attrs.removeValue(forKey: .strikethroughStyle)
|
|
4128
4582
|
}
|
|
4129
4583
|
|
|
4584
|
+
// Consumer inline colour — must land AFTER the `UIColor.label` / inline-code foreground
|
|
4585
|
+
// writes above so it overrides them, and marker + paint go in together.
|
|
4586
|
+
if let color = pendingExternalForeground {
|
|
4587
|
+
attrs[ExternalForegroundKey] = color
|
|
4588
|
+
attrs[.foregroundColor] = color
|
|
4589
|
+
} else if pendingExternalForegroundCleared {
|
|
4590
|
+
attrs.removeValue(forKey: ExternalForegroundKey)
|
|
4591
|
+
// Inline code / code block keep their own foreground when the colour is cleared
|
|
4592
|
+
if !pendingStyles.contains("code") && !pendingStyles.contains("codeBlock") {
|
|
4593
|
+
attrs[.foregroundColor] = UIColor.label
|
|
4594
|
+
}
|
|
4595
|
+
}
|
|
4596
|
+
|
|
4130
4597
|
textView.typingAttributes = attrs
|
|
4131
4598
|
}
|
|
4132
4599
|
|
|
@@ -4139,6 +4606,9 @@ class RichTextEditorView: UIView, UITextViewDelegate, UIGestureRecognizerDelegat
|
|
|
4139
4606
|
}
|
|
4140
4607
|
|
|
4141
4608
|
private func toggleListStyle(bullet: Bool) {
|
|
4609
|
+
// Arm the active consumer colour before the attributedText resets below wipe
|
|
4610
|
+
// typingAttributes — applyPendingStylesToTypingAttributes() rebuilds from pendingStyles.
|
|
4611
|
+
captureExternalColorToPending()
|
|
4142
4612
|
// Mutual exclusivity: exit code block mode before applying list (matches Android toggleListPrefix)
|
|
4143
4613
|
// Remove code block attributes inline rather than calling toggleCodeBlock() to avoid
|
|
4144
4614
|
// textView.attributedText reassignment mid-flow.
|
|
@@ -4196,6 +4666,11 @@ class RichTextEditorView: UIView, UITextViewDelegate, UIGestureRecognizerDelegat
|
|
|
4196
4666
|
pendingStyles.remove("underline")
|
|
4197
4667
|
pendingStyles.remove("strikethrough")
|
|
4198
4668
|
pendingStyles.remove("code")
|
|
4669
|
+
// This branch converts a code-block line to a plain list line and drops every other
|
|
4670
|
+
// inline style, so the consumer colour goes with them rather than surviving alone.
|
|
4671
|
+
pendingStyles.remove(ExternalColorPendingKey)
|
|
4672
|
+
pendingExternalForeground = nil
|
|
4673
|
+
pendingExternalForegroundCleared = false
|
|
4199
4674
|
explicitlyOffStyles.removeAll()
|
|
4200
4675
|
// Reset typingAttributes to plain so list prefix doesn't inherit code block styling
|
|
4201
4676
|
let plainParaStyle = NSMutableParagraphStyle()
|
|
@@ -4260,7 +4735,7 @@ class RichTextEditorView: UIView, UITextViewDelegate, UIGestureRecognizerDelegat
|
|
|
4260
4735
|
var lineOffsets: [(start: Int, line: String)] = []
|
|
4261
4736
|
for line in lines {
|
|
4262
4737
|
lineOffsets.append((start: lineOffset, line: line))
|
|
4263
|
-
lineOffset += line.
|
|
4738
|
+
lineOffset += (line as NSString).length + 1 // +1 for \n (UTF-16 units, to match NSRange)
|
|
4264
4739
|
}
|
|
4265
4740
|
|
|
4266
4741
|
for (index, entry) in lineOffsets.enumerated().reversed() {
|
|
@@ -4343,6 +4818,7 @@ class RichTextEditorView: UIView, UITextViewDelegate, UIGestureRecognizerDelegat
|
|
|
4343
4818
|
// set textView.attributedText and wipe typingAttributes.
|
|
4344
4819
|
applyPendingStylesToTypingAttributes()
|
|
4345
4820
|
deferredTypingAttrs = textView.typingAttributes
|
|
4821
|
+
syncPendingStyleAnchor()
|
|
4346
4822
|
|
|
4347
4823
|
// Re-check placeholder after all post-processing (applyListIndentation /
|
|
4348
4824
|
// renumberNumberedLists may set textView.attributedText which can cause
|
|
@@ -4448,7 +4924,12 @@ class RichTextEditorView: UIView, UITextViewDelegate, UIGestureRecognizerDelegat
|
|
|
4448
4924
|
guard let start = style["start"] as? Int,
|
|
4449
4925
|
let end = style["end"] as? Int,
|
|
4450
4926
|
let styleType = style["style"] as? String,
|
|
4451
|
-
|
|
4927
|
+
// UTF-16, not graphemes. JS hands these offsets over in UTF-16 units
|
|
4928
|
+
// (that is what String slicing and NSRange both use); `text.count` counts
|
|
4929
|
+
// GRAPHEMES, so one emoji made it one short and the guard silently dropped
|
|
4930
|
+
// every style on the line — the edit composer then opened a coloured/bold
|
|
4931
|
+
// message as plain text. Same bug the serializer had in ENG-38257.
|
|
4932
|
+
start < end && end <= (text as NSString).length else { continue }
|
|
4452
4933
|
|
|
4453
4934
|
let range = NSRange(location: start + prefixLength, length: end - start)
|
|
4454
4935
|
|
|
@@ -4477,6 +4958,14 @@ class RichTextEditorView: UIView, UITextViewDelegate, UIGestureRecognizerDelegat
|
|
|
4477
4958
|
blockAttrString.addAttribute(.underlineStyle, value: NSUnderlineStyle.single.rawValue, range: range)
|
|
4478
4959
|
case "strikethrough":
|
|
4479
4960
|
blockAttrString.addAttribute(.strikethroughStyle, value: NSUnderlineStyle.single.rawValue, range: range)
|
|
4961
|
+
case "textColor":
|
|
4962
|
+
// Edit round-trip: restore a consumer colour from the wire format so the
|
|
4963
|
+
// composer shows the colour rather than raw <color=…> text. Marker and
|
|
4964
|
+
// paint go in together, as everywhere else.
|
|
4965
|
+
if let hex = style["color"] as? String, let color = UIColor.fromHexString(hex) {
|
|
4966
|
+
blockAttrString.addAttribute(ExternalForegroundKey, value: color, range: range)
|
|
4967
|
+
blockAttrString.addAttribute(.foregroundColor, value: color, range: range)
|
|
4968
|
+
}
|
|
4480
4969
|
case "code":
|
|
4481
4970
|
let monoFont = UIFont.monospacedSystemFont(ofSize: 15, weight: .regular)
|
|
4482
4971
|
blockAttrString.addAttribute(.font, value: monoFont, range: range)
|
|
@@ -4492,6 +4981,22 @@ class RichTextEditorView: UIView, UITextViewDelegate, UIGestureRecognizerDelegat
|
|
|
4492
4981
|
break
|
|
4493
4982
|
}
|
|
4494
4983
|
}
|
|
4984
|
+
|
|
4985
|
+
// Order-independent re-assert of the consumer colour.
|
|
4986
|
+
//
|
|
4987
|
+
// The styles above are applied in whatever order the wire listed them, and both
|
|
4988
|
+
// `code` and `link` write a foreground of their own. When a colour run overlapped
|
|
4989
|
+
// one of those and happened to be listed first, the later case painted straight
|
|
4990
|
+
// over it and the edit composer opened showing #6852D6 (or link blue) instead of
|
|
4991
|
+
// the colour the message was sent with (ENG-38248, on the edit round-trip).
|
|
4992
|
+
// ExternalForegroundKey is the marker the serializer reads, so re-deriving the
|
|
4993
|
+
// paint from it makes the outcome independent of style order.
|
|
4994
|
+
let blockRange = NSRange(location: 0, length: blockAttrString.length)
|
|
4995
|
+
blockAttrString.enumerateAttribute(ExternalForegroundKey, in: blockRange, options: []) { value, subRange, _ in
|
|
4996
|
+
if let color = value as? UIColor {
|
|
4997
|
+
blockAttrString.addAttribute(.foregroundColor, value: color, range: subRange)
|
|
4998
|
+
}
|
|
4999
|
+
}
|
|
4495
5000
|
}
|
|
4496
5001
|
|
|
4497
5002
|
if blockIndex < blocks.count - 1 {
|
|
@@ -4531,12 +5036,32 @@ class RichTextEditorView: UIView, UITextViewDelegate, UIGestureRecognizerDelegat
|
|
|
4531
5036
|
pendingStylesInsertPos = endPosition
|
|
4532
5037
|
let cbParaStyle = NSMutableParagraphStyle()
|
|
4533
5038
|
cbParaStyle.lineHeightMultiple = RichTextEditorView.defaultLineHeightMultiple
|
|
5039
|
+
// Deliberately no colour: a code BLOCK owns its styling and destroys the colour
|
|
5040
|
+
// on its line, which is the documented rule on both platforms.
|
|
4534
5041
|
textView.typingAttributes = [
|
|
4535
5042
|
.font: UIFont.monospacedSystemFont(ofSize: 16, weight: .regular),
|
|
4536
5043
|
.foregroundColor: UIColor.label,
|
|
4537
5044
|
.paragraphStyle: cbParaStyle,
|
|
4538
5045
|
CodeBlockAttributeKey: true
|
|
4539
5046
|
]
|
|
5047
|
+
} else if let color = attrText.attribute(
|
|
5048
|
+
ExternalForegroundKey, at: checkPos, effectiveRange: nil
|
|
5049
|
+
) as? UIColor {
|
|
5050
|
+
// Arm the trailing run's consumer colour so text typed after an edit CONTINUES
|
|
5051
|
+
// in it. The caret already inherits the font-level styling from the character
|
|
5052
|
+
// before it, which is why inline code carried across an edit while the colour
|
|
5053
|
+
// did not — the colour is not a typing attribute the editor maintains by
|
|
5054
|
+
// itself, it rides `pendingExternalForeground`, and nothing armed that here.
|
|
5055
|
+
// Result: editing a red inline-code message and typing continued the code and
|
|
5056
|
+
// came out in the default label colour (ENG-38253).
|
|
5057
|
+
//
|
|
5058
|
+
// Marker and paint together, as everywhere else: ExternalForegroundKey is what
|
|
5059
|
+
// the serializer reads back, so without it the colour would show and then not
|
|
5060
|
+
// survive the send.
|
|
5061
|
+
textView.typingAttributes[ExternalForegroundKey] = color
|
|
5062
|
+
textView.typingAttributes[.foregroundColor] = color
|
|
5063
|
+
captureExternalColorToPending()
|
|
5064
|
+
pendingStylesInsertPos = endPosition
|
|
4540
5065
|
}
|
|
4541
5066
|
}
|
|
4542
5067
|
}
|
|
@@ -4557,6 +5082,15 @@ class RichTextEditorView: UIView, UITextViewDelegate, UIGestureRecognizerDelegat
|
|
|
4557
5082
|
return textView.text ?? ""
|
|
4558
5083
|
}
|
|
4559
5084
|
|
|
5085
|
+
/// Serializes the composer into the blocks array the JS side turns into wire markup.
|
|
5086
|
+
///
|
|
5087
|
+
/// EVERY offset here is a UTF-16 offset, because that is what `NSRange` /
|
|
5088
|
+
/// `NSAttributedString` / `NSRegularExpression` use — and what JS string slicing uses on the
|
|
5089
|
+
/// other side of the bridge. Swift's `String.count` is a GRAPHEME count, so the two disagree
|
|
5090
|
+
/// for exactly the characters users reach for most: emoji, flags, skin-tone sequences,
|
|
5091
|
+
/// combining accents. Mixing them made every style range on a line stop one UTF-16 unit short
|
|
5092
|
+
/// per preceding emoji, so the tail of a coloured message serialized as plain text even though
|
|
5093
|
+
/// the composer showed it coloured (ENG-38257). Use `NSString.length`, never `String.count`.
|
|
4560
5094
|
func getBlocksArray() -> [[String: Any]] {
|
|
4561
5095
|
let text = textView.text ?? ""
|
|
4562
5096
|
let attributedText = textView.attributedText ?? NSAttributedString()
|
|
@@ -4577,22 +5111,22 @@ class RichTextEditorView: UIView, UITextViewDelegate, UIGestureRecognizerDelegat
|
|
|
4577
5111
|
if line.hasPrefix("▎ • ") {
|
|
4578
5112
|
blockType = "quoteBullet"
|
|
4579
5113
|
displayText = String(line.dropFirst(4))
|
|
4580
|
-
} else if let qnMatch = quoteNumberedRegex?.firstMatch(in: line, range: NSRange(location: 0, length: line.
|
|
5114
|
+
} else if let qnMatch = quoteNumberedRegex?.firstMatch(in: line, range: NSRange(location: 0, length: (line as NSString).length)) {
|
|
4581
5115
|
blockType = "quoteNumbered"
|
|
4582
|
-
displayText =
|
|
5116
|
+
displayText = (line as NSString).substring(from: qnMatch.range.length)
|
|
4583
5117
|
} else if line.hasPrefix("• ") {
|
|
4584
5118
|
blockType = "bullet"
|
|
4585
5119
|
displayText = String(line.dropFirst(2))
|
|
4586
|
-
} else if let match = numberedRegex?.firstMatch(in: line, range: NSRange(location: 0, length: line.
|
|
5120
|
+
} else if let match = numberedRegex?.firstMatch(in: line, range: NSRange(location: 0, length: (line as NSString).length)) {
|
|
4587
5121
|
blockType = "numbered"
|
|
4588
|
-
displayText =
|
|
5122
|
+
displayText = (line as NSString).substring(from: match.range.length)
|
|
4589
5123
|
} else if line.hasPrefix("▎ ") {
|
|
4590
5124
|
blockType = "quote"
|
|
4591
5125
|
displayText = String(line.dropFirst(2))
|
|
4592
5126
|
}
|
|
4593
5127
|
|
|
4594
5128
|
// Detect code block: check the line range for CodeBlockAttributeKey
|
|
4595
|
-
let lineRange = NSRange(location: currentIndex, length: line.
|
|
5129
|
+
let lineRange = NSRange(location: currentIndex, length: (line as NSString).length)
|
|
4596
5130
|
if lineRange.location + lineRange.length <= attributedText.length && lineRange.length > 0 {
|
|
4597
5131
|
var hasCodeBlockAttr = false
|
|
4598
5132
|
attributedText.enumerateAttribute(CodeBlockAttributeKey, in: lineRange, options: []) { value, _, _ in
|
|
@@ -4616,9 +5150,9 @@ class RichTextEditorView: UIView, UITextViewDelegate, UIGestureRecognizerDelegat
|
|
|
4616
5150
|
displayText = displayText.replacingOccurrences(of: "\u{200B}", with: "")
|
|
4617
5151
|
|
|
4618
5152
|
var styles: [[String: Any]] = []
|
|
4619
|
-
let prefixLength = line.
|
|
5153
|
+
let prefixLength = (line as NSString).length - (displayText as NSString).length
|
|
4620
5154
|
let styleRangeStart = currentIndex + prefixLength
|
|
4621
|
-
let styleRangeLength = displayText.
|
|
5155
|
+
let styleRangeLength = (displayText as NSString).length
|
|
4622
5156
|
let styleLineRange = NSRange(location: styleRangeStart, length: styleRangeLength)
|
|
4623
5157
|
|
|
4624
5158
|
if styleLineRange.location + styleLineRange.length <= attributedText.length {
|
|
@@ -4658,6 +5192,17 @@ class RichTextEditorView: UIView, UITextViewDelegate, UIGestureRecognizerDelegat
|
|
|
4658
5192
|
if attrs[.strikethroughStyle] != nil {
|
|
4659
5193
|
styles.append(["style": "strikethrough", "start": relativeStart, "end": relativeEnd])
|
|
4660
5194
|
}
|
|
5195
|
+
// Consumer inline colour. Offsets are already relative to the block prefix
|
|
5196
|
+
// (`• `/`▎ `/`1. ` and ZWS are stripped by the callers before extraction),
|
|
5197
|
+
// so a colour range can never cover a list/quote glyph.
|
|
5198
|
+
if let color = attrs[ExternalForegroundKey] as? UIColor {
|
|
5199
|
+
styles.append([
|
|
5200
|
+
"style": "textColor",
|
|
5201
|
+
"start": relativeStart,
|
|
5202
|
+
"end": relativeEnd,
|
|
5203
|
+
"color": color.toHexString()
|
|
5204
|
+
])
|
|
5205
|
+
}
|
|
4661
5206
|
}
|
|
4662
5207
|
}
|
|
4663
5208
|
|
|
@@ -4667,7 +5212,7 @@ class RichTextEditorView: UIView, UITextViewDelegate, UIGestureRecognizerDelegat
|
|
|
4667
5212
|
"styles": styles
|
|
4668
5213
|
])
|
|
4669
5214
|
|
|
4670
|
-
currentIndex += line.
|
|
5215
|
+
currentIndex += (line as NSString).length + 1 // +1 for the "\n" the split removed
|
|
4671
5216
|
}
|
|
4672
5217
|
|
|
4673
5218
|
return blocks
|
|
@@ -4682,6 +5227,7 @@ class RichTextEditorView: UIView, UITextViewDelegate, UIGestureRecognizerDelegat
|
|
|
4682
5227
|
pendingStyles.removeAll()
|
|
4683
5228
|
explicitlyOffStyles.removeAll()
|
|
4684
5229
|
pendingStylesInsertPos = -1
|
|
5230
|
+
lastKnownTextLength = 0
|
|
4685
5231
|
// Reset typingAttributes to plain so next typed text has no formatting
|
|
4686
5232
|
let paragraphStyle = NSMutableParagraphStyle()
|
|
4687
5233
|
paragraphStyle.lineHeightMultiple = RichTextEditorView.defaultLineHeightMultiple
|
|
@@ -4846,6 +5392,11 @@ class RichTextEditorView: UIView, UITextViewDelegate, UIGestureRecognizerDelegat
|
|
|
4846
5392
|
]
|
|
4847
5393
|
// Track code block as a pending style so it survives textDidChange clearing
|
|
4848
5394
|
pendingStyles.insert("codeBlock")
|
|
5395
|
+
// Drop an armed consumer colour, or the first character typed into the empty block
|
|
5396
|
+
// would come out coloured instead of the code block default.
|
|
5397
|
+
pendingExternalForeground = nil
|
|
5398
|
+
pendingExternalForegroundCleared = false
|
|
5399
|
+
pendingStyles.remove(ExternalColorPendingKey)
|
|
4849
5400
|
pendingStylesInsertPos = textView.selectedRange.location
|
|
4850
5401
|
isInternalChange = false
|
|
4851
5402
|
textView.setNeedsDisplay()
|
|
@@ -4904,6 +5455,17 @@ class RichTextEditorView: UIView, UITextViewDelegate, UIGestureRecognizerDelegat
|
|
|
4904
5455
|
mutableAttr.addAttribute(CodeBlockAttributeKey, value: true, range: lineRange)
|
|
4905
5456
|
// Strip mention markers so setMentionRanges() won't re-apply mention styling
|
|
4906
5457
|
mutableAttr.removeAttribute(MentionMarkerKey, range: lineRange)
|
|
5458
|
+
// Same reason, for consumer colour: the blanket .foregroundColor reset above only
|
|
5459
|
+
// changes what is drawn. Leaving the marker behind lets the re-apply passes that run
|
|
5460
|
+
// after any mention re-styling paint the colour straight back over the block, so a
|
|
5461
|
+
// code block would keep whatever colour the text had before it became code.
|
|
5462
|
+
mutableAttr.removeAttribute(ExternalForegroundKey, range: lineRange)
|
|
5463
|
+
mutableAttr.removeAttribute(ExternalBackgroundKey, range: lineRange)
|
|
5464
|
+
// A colour armed for type-ahead would otherwise colour the next character typed
|
|
5465
|
+
// inside the block.
|
|
5466
|
+
pendingExternalForeground = nil
|
|
5467
|
+
pendingExternalForegroundCleared = false
|
|
5468
|
+
pendingStyles.remove(ExternalColorPendingKey)
|
|
4907
5469
|
// Set pendingStyles + typingAttributes so newly typed characters
|
|
4908
5470
|
// inherit code block mode (matches empty-line activation path)
|
|
4909
5471
|
pendingStyles.insert("codeBlock")
|
|
@@ -5124,7 +5686,9 @@ class RichTextEditorView: UIView, UITextViewDelegate, UIGestureRecognizerDelegat
|
|
|
5124
5686
|
bgColor.getRed(&r, green: &g, blue: &b, alpha: &a)
|
|
5125
5687
|
// Mention bg: rgba(104/255, 82/255, 214/255, 0.15) — low alpha purple
|
|
5126
5688
|
let isMentionBg = abs(r - 104.0/255.0) < 0.05 && abs(g - 82.0/255.0) < 0.05 && abs(b - 214.0/255.0) < 0.05 && a < 0.3
|
|
5127
|
-
|
|
5689
|
+
// A consumer-applied background that happens to look like the mention purple is not one.
|
|
5690
|
+
let isExternal = mutableAttr.attribute(ExternalBackgroundKey, at: range.location, effectiveRange: nil) != nil
|
|
5691
|
+
if isMentionBg && !isExternal {
|
|
5128
5692
|
mutableAttr.removeAttribute(.backgroundColor, range: range)
|
|
5129
5693
|
}
|
|
5130
5694
|
}
|
|
@@ -5143,6 +5707,14 @@ class RichTextEditorView: UIView, UITextViewDelegate, UIGestureRecognizerDelegat
|
|
|
5143
5707
|
}
|
|
5144
5708
|
}
|
|
5145
5709
|
|
|
5710
|
+
// Re-apply consumer-applied foreground color (applyInlineStyle) after the blanket reset,
|
|
5711
|
+
// so a colour set by a trailing-button formatter is not clobbered by mention re-styling.
|
|
5712
|
+
mutableAttr.enumerateAttribute(ExternalForegroundKey, in: fullRange, options: []) { value, range, _ in
|
|
5713
|
+
if let color = value as? UIColor {
|
|
5714
|
+
mutableAttr.addAttribute(.foregroundColor, value: color, range: range)
|
|
5715
|
+
}
|
|
5716
|
+
}
|
|
5717
|
+
|
|
5146
5718
|
// Re-apply link foreground color (systemBlue) to ranges with LinkURLAttributeKey
|
|
5147
5719
|
// so that links remain visually blue after the blanket reset above.
|
|
5148
5720
|
mutableAttr.enumerateAttribute(LinkURLAttributeKey, in: fullRange, options: []) { value, range, _ in
|
|
@@ -5151,10 +5723,11 @@ class RichTextEditorView: UIView, UITextViewDelegate, UIGestureRecognizerDelegat
|
|
|
5151
5723
|
}
|
|
5152
5724
|
}
|
|
5153
5725
|
|
|
5154
|
-
// Re-apply inline code text color to ranges with inline code background
|
|
5726
|
+
// Re-apply inline code text color to ranges with inline code background, except where a
|
|
5727
|
+
// consumer colour is set — see paintInlineCodeForeground (ENG-38248).
|
|
5155
5728
|
mutableAttr.enumerateAttribute(.backgroundColor, in: fullRange, options: []) { value, range, _ in
|
|
5156
5729
|
if let bgColor = value as? UIColor, bgColor == inlineCodeBgColor {
|
|
5157
|
-
|
|
5730
|
+
self.paintInlineCodeForeground(on: mutableAttr, range: range)
|
|
5158
5731
|
}
|
|
5159
5732
|
}
|
|
5160
5733
|
|
|
@@ -5235,8 +5808,10 @@ class RichTextEditorView: UIView, UITextViewDelegate, UIGestureRecognizerDelegat
|
|
|
5235
5808
|
cleanAttrs.removeValue(forKey: .backgroundColor)
|
|
5236
5809
|
}
|
|
5237
5810
|
}
|
|
5238
|
-
// Remove mention foreground color — use component comparison
|
|
5239
|
-
|
|
5811
|
+
// Remove mention foreground color — use component comparison. Skipped when a consumer
|
|
5812
|
+
// inline colour is armed, so a swatch near mention purple isn't silently reverted.
|
|
5813
|
+
if cleanAttrs[ExternalForegroundKey] == nil,
|
|
5814
|
+
let fg = cleanAttrs[.foregroundColor] as? UIColor {
|
|
5240
5815
|
var r: CGFloat = 0, g: CGFloat = 0, b: CGFloat = 0, a: CGFloat = 0
|
|
5241
5816
|
fg.getRed(&r, green: &g, blue: &b, alpha: &a)
|
|
5242
5817
|
if abs(r - 104.0/255.0) < 0.05 && abs(g - 82.0/255.0) < 0.05 && abs(b - 214.0/255.0) < 0.05 && a > 0.9 {
|
|
@@ -5445,7 +6020,7 @@ class RichTextEditorView: UIView, UITextViewDelegate, UIGestureRecognizerDelegat
|
|
|
5445
6020
|
case "code":
|
|
5446
6021
|
let monoFont = UIFont.monospacedSystemFont(ofSize: 15, weight: .regular)
|
|
5447
6022
|
mutableAttr.addAttribute(.font, value: monoFont, range: fmtRange)
|
|
5448
|
-
|
|
6023
|
+
paintInlineCodeForeground(on: mutableAttr, range: fmtRange)
|
|
5449
6024
|
mutableAttr.addAttribute(.backgroundColor, value: inlineCodeBgColor, range: fmtRange)
|
|
5450
6025
|
case "codeBlock":
|
|
5451
6026
|
let monoFont = UIFont.monospacedSystemFont(ofSize: 16, weight: .regular)
|
|
@@ -5504,6 +6079,8 @@ class RichTextEditorView: UIView, UITextViewDelegate, UIGestureRecognizerDelegat
|
|
|
5504
6079
|
/// Returns true if the string contains markdown syntax that should be parsed into rich text.
|
|
5505
6080
|
/// Matches Android looksLikeMarkdown().
|
|
5506
6081
|
func looksLikeMarkdown(_ str: String) -> Bool {
|
|
6082
|
+
// Rich-text wire format — only ever reaches here from our own private pasteboard type.
|
|
6083
|
+
if str.lowercased().contains("<color=") { return true }
|
|
5507
6084
|
if str.contains("[") && str.contains("](") { return true }
|
|
5508
6085
|
if str.contains("**") { return true }
|
|
5509
6086
|
if str.contains("~~") { return true }
|
|
@@ -5528,6 +6105,7 @@ class RichTextEditorView: UIView, UITextViewDelegate, UIGestureRecognizerDelegat
|
|
|
5528
6105
|
var isUnderline = false
|
|
5529
6106
|
var isStrikethrough = false
|
|
5530
6107
|
var isCode = false
|
|
6108
|
+
var activePasteColor: UIColor? = nil
|
|
5531
6109
|
|
|
5532
6110
|
let defaultFont = UIFont.systemFont(ofSize: 16)
|
|
5533
6111
|
let monoFont = UIFont.monospacedSystemFont(ofSize: 15, weight: .regular)
|
|
@@ -5562,11 +6140,46 @@ class RichTextEditorView: UIView, UITextViewDelegate, UIGestureRecognizerDelegat
|
|
|
5562
6140
|
}
|
|
5563
6141
|
}
|
|
5564
6142
|
|
|
6143
|
+
// Consumer colour wins over BOTH branches' foreground, applied after them rather than
|
|
6144
|
+
// inside the `else`. Inline code paints `inlineCodeTextColor`, so while this lived in the
|
|
6145
|
+
// non-code branch a pasted run that was ALSO code came out uniformly #6852D6 and the
|
|
6146
|
+
// colour was silently dropped — copying a coloured message and pasting it back lost its
|
|
6147
|
+
// colour (ENG-38253). `setContent` already re-asserts the colour over inline code for the
|
|
6148
|
+
// edit path (ENG-38248); the paste path has to agree with it, or the same message looks
|
|
6149
|
+
// different depending on how it reached the composer.
|
|
6150
|
+
// Marker and paint go in together, as everywhere else: ExternalForegroundKey is what the
|
|
6151
|
+
// serializer reads back, so without it the colour would show and then not survive a send.
|
|
6152
|
+
if let color = activePasteColor {
|
|
6153
|
+
attrs[ExternalForegroundKey] = color
|
|
6154
|
+
attrs[.foregroundColor] = color
|
|
6155
|
+
}
|
|
6156
|
+
|
|
5565
6157
|
result.append(NSAttributedString(string: currentText, attributes: attrs))
|
|
5566
6158
|
currentText = ""
|
|
5567
6159
|
}
|
|
5568
6160
|
|
|
5569
6161
|
while i < len {
|
|
6162
|
+
// <color=#rrggbb> … </color> — same grammar the formatter renders.
|
|
6163
|
+
if chars[i] == "<" {
|
|
6164
|
+
let rest = String(chars[i...])
|
|
6165
|
+
if rest.lowercased().hasPrefix("</color>") {
|
|
6166
|
+
flush()
|
|
6167
|
+
activePasteColor = nil
|
|
6168
|
+
i += 8
|
|
6169
|
+
continue
|
|
6170
|
+
}
|
|
6171
|
+
if rest.lowercased().hasPrefix("<color="),
|
|
6172
|
+
let close = rest.firstIndex(of: ">") {
|
|
6173
|
+
let hex = String(rest[rest.index(rest.startIndex, offsetBy: 7)..<close])
|
|
6174
|
+
.trimmingCharacters(in: .whitespaces)
|
|
6175
|
+
if let color = UIColor.fromHexString(hex) {
|
|
6176
|
+
flush()
|
|
6177
|
+
activePasteColor = color
|
|
6178
|
+
i += rest.distance(from: rest.startIndex, to: close) + 1
|
|
6179
|
+
continue
|
|
6180
|
+
}
|
|
6181
|
+
}
|
|
6182
|
+
}
|
|
5570
6183
|
if chars[i] == "[" {
|
|
5571
6184
|
if let (linkText, linkURL, fullEnd) = parseLinkAt(chars, from: i) {
|
|
5572
6185
|
flush()
|
|
@@ -5796,7 +6409,7 @@ class RichTextEditorView: UIView, UITextViewDelegate, UIGestureRecognizerDelegat
|
|
|
5796
6409
|
|
|
5797
6410
|
isInternalChange = true
|
|
5798
6411
|
textView.attributedText = mutableAttr
|
|
5799
|
-
let newCursorPos = range.location + displayText.
|
|
6412
|
+
let newCursorPos = range.location + (displayText as NSString).length
|
|
5800
6413
|
textView.selectedRange = NSRange(location: newCursorPos, length: 0)
|
|
5801
6414
|
|
|
5802
6415
|
let paragraphStyle = NSMutableParagraphStyle()
|