@cometchat/chat-uikit-react-native 5.4.1 → 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/.gradle/7.4/checksums/checksums.lock +0 -0
- package/android/.gradle/7.4/fileChanges/last-build.bin +0 -0
- package/android/.gradle/7.4/fileHashes/fileHashes.lock +0 -0
- package/android/.gradle/7.4/gc.properties +0 -0
- package/android/.gradle/vcs-1/gc.properties +0 -0
- package/android/src/main/java/com/reactnativecometchatuikit/CometChatClipboardModule.kt +34 -0
- package/android/src/main/java/com/reactnativecometchatuikit/RichTextEditorView.kt +542 -5
- 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 +593 -31
- package/ios/RichTextEditorViewManager.m +2 -0
- package/ios/RichTextEditorViewManager.swift +16 -0
- package/package.json +1 -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/MANUAL_TEST_CASES.md +154 -0
- 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 +92 -6
- 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/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/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 +1 -0
- 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/ErrorEmptyView/ErrorEmptyView.tsx +4 -1
- package/src/theme/default/default.ts +6 -0
- package/src/theme/type.ts +4 -0
- package/src/CometChatMessageList/PERFORMANCE_ARCHITECTURE.md +0 -478
- package/src/CometChatMessageList/SEARCH_NAV_PERF_PLAN.md +0 -613
- package/src/CometChatMessageList/SEARCH_NAV_SLOWNESS_FINDINGS_AND_FIX.md +0 -313
- package/src/shared/CometChatUiKit/CometChatUIKit.changes.md +0 -76
- package/src/shared/CometChatUiKit/checkAuthSettings-review.md +0 -211
|
@@ -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
|
}
|
|
@@ -754,7 +760,7 @@ class RichTextView: UITextView {
|
|
|
754
760
|
var quoteRegions: [(Int, Int)] = []
|
|
755
761
|
var offset = 0
|
|
756
762
|
for line in lines {
|
|
757
|
-
let lineEnd = offset + line.
|
|
763
|
+
let lineEnd = offset + (line as NSString).length
|
|
758
764
|
if line.hasPrefix("▎") {
|
|
759
765
|
if !quoteRegions.isEmpty && offset <= quoteRegions.last!.1 + 1 {
|
|
760
766
|
quoteRegions[quoteRegions.count - 1] = (quoteRegions.last!.0, lineEnd)
|
|
@@ -873,6 +879,56 @@ private let CodeBlockAttributeKey = NSAttributedString.Key("cometchat.codeBlock"
|
|
|
873
879
|
// affecting inline code or link styling.
|
|
874
880
|
private let MentionMarkerKey = NSAttributedString.Key("cometchat.mention")
|
|
875
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
|
+
|
|
876
932
|
// Code block visual constants (matching Android CodeBlockBorderSpan companion)
|
|
877
933
|
private let codeBlockCornerRadius: CGFloat = 6.0
|
|
878
934
|
private let codeBlockPadding: CGFloat = 12.0
|
|
@@ -1100,6 +1156,12 @@ class RichTextEditorView: UIView, UITextViewDelegate, UIGestureRecognizerDelegat
|
|
|
1100
1156
|
b = CGFloat(val & 0xFF) / 255.0
|
|
1101
1157
|
}
|
|
1102
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()
|
|
1103
1165
|
}
|
|
1104
1166
|
}
|
|
1105
1167
|
updateContentSize()
|
|
@@ -1120,6 +1182,22 @@ class RichTextEditorView: UIView, UITextViewDelegate, UIGestureRecognizerDelegat
|
|
|
1120
1182
|
private var explicitlyOffStyles: Set<String> = []
|
|
1121
1183
|
private var pendingStylesInsertPos: Int = -1
|
|
1122
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
|
+
|
|
1123
1201
|
/// Typing attributes to forcefully restore after all processing (including async
|
|
1124
1202
|
/// JS bridge callbacks like syncMentionRanges) completes. Set by autoContinueListOnEnter
|
|
1125
1203
|
/// so that inline styles (bold, italic, etc.) survive across list line continuation.
|
|
@@ -1141,9 +1219,36 @@ class RichTextEditorView: UIView, UITextViewDelegate, UIGestureRecognizerDelegat
|
|
|
1141
1219
|
if let strike = attrs[.strikethroughStyle] as? Int, strike != 0 {
|
|
1142
1220
|
pendingStyles.insert("strikethrough")
|
|
1143
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 }
|
|
1144
1234
|
pendingStylesInsertPos = textView.selectedRange.location
|
|
1145
1235
|
}
|
|
1146
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
|
+
|
|
1147
1252
|
override init(frame: CGRect) {
|
|
1148
1253
|
super.init(frame: frame)
|
|
1149
1254
|
setupView()
|
|
@@ -1376,25 +1481,48 @@ class RichTextEditorView: UIView, UITextViewDelegate, UIGestureRecognizerDelegat
|
|
|
1376
1481
|
savedSelectionRange = range
|
|
1377
1482
|
}
|
|
1378
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
|
+
|
|
1379
1497
|
// Skip toolbar/style updates during internal changes (e.g. autoContinueListOnEnter)
|
|
1380
1498
|
// to avoid emitting stale style state before typingAttributes are restored.
|
|
1381
1499
|
guard !isInternalChange else { return }
|
|
1382
1500
|
|
|
1383
1501
|
// Invalidate pending styles if cursor moved away from insert position
|
|
1384
1502
|
if pendingStylesInsertPos >= 0 && (!pendingStyles.isEmpty || !explicitlyOffStyles.isEmpty) {
|
|
1385
|
-
|
|
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 {
|
|
1386
1510
|
// Preserve "codeBlock" across cursor movements — it's a persistent
|
|
1387
1511
|
// block-level style that should survive until explicitly toggled off
|
|
1388
1512
|
let hadCodeBlock = pendingStyles.contains("codeBlock")
|
|
1389
1513
|
pendingStyles.removeAll()
|
|
1390
1514
|
explicitlyOffStyles.removeAll()
|
|
1391
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
|
|
1392
1520
|
if hadCodeBlock {
|
|
1393
1521
|
pendingStyles.insert("codeBlock")
|
|
1394
1522
|
pendingStylesInsertPos = range.location
|
|
1395
1523
|
}
|
|
1396
|
-
} else if
|
|
1397
|
-
//
|
|
1524
|
+
} else if isTypingAdvance {
|
|
1525
|
+
// Text was typed at the anchor — follow it, however many characters landed.
|
|
1398
1526
|
pendingStylesInsertPos = range.location
|
|
1399
1527
|
}
|
|
1400
1528
|
}
|
|
@@ -1427,7 +1555,11 @@ class RichTextEditorView: UIView, UITextViewDelegate, UIGestureRecognizerDelegat
|
|
|
1427
1555
|
var cleanAttrs = textView.typingAttributes
|
|
1428
1556
|
cleanAttrs.removeValue(forKey: .backgroundColor)
|
|
1429
1557
|
cleanAttrs.removeValue(forKey: MentionMarkerKey)
|
|
1430
|
-
|
|
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 {
|
|
1431
1563
|
var r: CGFloat = 0, g: CGFloat = 0, b: CGFloat = 0, a: CGFloat = 0
|
|
1432
1564
|
fg.getRed(&r, green: &g, blue: &b, alpha: &a)
|
|
1433
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 {
|
|
@@ -1671,7 +1803,11 @@ class RichTextEditorView: UIView, UITextViewDelegate, UIGestureRecognizerDelegat
|
|
|
1671
1803
|
cleanAttrs.removeValue(forKey: .backgroundColor)
|
|
1672
1804
|
cleanAttrs.removeValue(forKey: MentionMarkerKey)
|
|
1673
1805
|
// Restore foreground if it's mention purple (#6852D6)
|
|
1674
|
-
|
|
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 {
|
|
1675
1811
|
var r: CGFloat = 0, g: CGFloat = 0, b: CGFloat = 0, a: CGFloat = 0
|
|
1676
1812
|
fg.getRed(&r, green: &g, blue: &b, alpha: &a)
|
|
1677
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 {
|
|
@@ -1896,6 +2032,7 @@ class RichTextEditorView: UIView, UITextViewDelegate, UIGestureRecognizerDelegat
|
|
|
1896
2032
|
textView.selectedRange = NSRange(location: insertPos + prefix.count + 1, length: 0)
|
|
1897
2033
|
applyPendingStylesToTypingAttributes()
|
|
1898
2034
|
deferredTypingAttrs = textView.typingAttributes
|
|
2035
|
+
syncPendingStyleAnchor()
|
|
1899
2036
|
isInternalChange = false
|
|
1900
2037
|
renumberNumberedLists()
|
|
1901
2038
|
DispatchQueue.main.async { [weak self] in
|
|
@@ -1957,6 +2094,7 @@ class RichTextEditorView: UIView, UITextViewDelegate, UIGestureRecognizerDelegat
|
|
|
1957
2094
|
textView.selectedRange = NSRange(location: insertPos + 5, length: 0) // \n + ▎ + " • " = 5
|
|
1958
2095
|
applyPendingStylesToTypingAttributes()
|
|
1959
2096
|
deferredTypingAttrs = textView.typingAttributes
|
|
2097
|
+
syncPendingStyleAnchor()
|
|
1960
2098
|
isInternalChange = false
|
|
1961
2099
|
DispatchQueue.main.async { [weak self] in
|
|
1962
2100
|
self?.textView.setNeedsDisplay()
|
|
@@ -1993,6 +2131,7 @@ class RichTextEditorView: UIView, UITextViewDelegate, UIGestureRecognizerDelegat
|
|
|
1993
2131
|
// Restore inline styles via pendingStyles mechanism
|
|
1994
2132
|
applyPendingStylesToTypingAttributes()
|
|
1995
2133
|
deferredTypingAttrs = textView.typingAttributes
|
|
2134
|
+
syncPendingStyleAnchor()
|
|
1996
2135
|
isInternalChange = false
|
|
1997
2136
|
saveToUndoStack()
|
|
1998
2137
|
sendContentChange()
|
|
@@ -2035,6 +2174,7 @@ class RichTextEditorView: UIView, UITextViewDelegate, UIGestureRecognizerDelegat
|
|
|
2035
2174
|
renumberNumberedLists()
|
|
2036
2175
|
applyPendingStylesToTypingAttributes()
|
|
2037
2176
|
deferredTypingAttrs = textView.typingAttributes
|
|
2177
|
+
syncPendingStyleAnchor()
|
|
2038
2178
|
isInternalChange = false
|
|
2039
2179
|
saveToUndoStack()
|
|
2040
2180
|
sendContentChange()
|
|
@@ -2068,6 +2208,7 @@ class RichTextEditorView: UIView, UITextViewDelegate, UIGestureRecognizerDelegat
|
|
|
2068
2208
|
textView.insertText("\n☐ ")
|
|
2069
2209
|
applyPendingStylesToTypingAttributes()
|
|
2070
2210
|
deferredTypingAttrs = textView.typingAttributes
|
|
2211
|
+
syncPendingStyleAnchor()
|
|
2071
2212
|
isInternalChange = false
|
|
2072
2213
|
saveToUndoStack()
|
|
2073
2214
|
sendContentChange()
|
|
@@ -2128,6 +2269,7 @@ class RichTextEditorView: UIView, UITextViewDelegate, UIGestureRecognizerDelegat
|
|
|
2128
2269
|
textView.selectedRange = NSRange(location: insertPos + 3, length: 0)
|
|
2129
2270
|
applyPendingStylesToTypingAttributes()
|
|
2130
2271
|
deferredTypingAttrs = textView.typingAttributes
|
|
2272
|
+
syncPendingStyleAnchor()
|
|
2131
2273
|
isInternalChange = false
|
|
2132
2274
|
// Trigger redraw for blockquote bar asynchronously to avoid layout issues
|
|
2133
2275
|
DispatchQueue.main.async { [weak self] in
|
|
@@ -2300,6 +2442,9 @@ class RichTextEditorView: UIView, UITextViewDelegate, UIGestureRecognizerDelegat
|
|
|
2300
2442
|
let hadItalic = pendingStyles.contains("italic")
|
|
2301
2443
|
let hadUnderline = pendingStyles.contains("underline")
|
|
2302
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)
|
|
2303
2448
|
let hadOffBold = explicitlyOffStyles.contains("bold")
|
|
2304
2449
|
let hadOffItalic = explicitlyOffStyles.contains("italic")
|
|
2305
2450
|
let hadOffUnderline = explicitlyOffStyles.contains("underline")
|
|
@@ -2316,6 +2461,7 @@ class RichTextEditorView: UIView, UITextViewDelegate, UIGestureRecognizerDelegat
|
|
|
2316
2461
|
if hadItalic { pendingStyles.insert("italic") }
|
|
2317
2462
|
if hadUnderline { pendingStyles.insert("underline") }
|
|
2318
2463
|
if hadStrikethrough { pendingStyles.insert("strikethrough") }
|
|
2464
|
+
if hadExternalColor { pendingStyles.insert(ExternalColorPendingKey) }
|
|
2319
2465
|
if hadOffBold { explicitlyOffStyles.insert("bold") }
|
|
2320
2466
|
if hadOffItalic { explicitlyOffStyles.insert("italic") }
|
|
2321
2467
|
if hadOffUnderline { explicitlyOffStyles.insert("underline") }
|
|
@@ -2340,6 +2486,9 @@ class RichTextEditorView: UIView, UITextViewDelegate, UIGestureRecognizerDelegat
|
|
|
2340
2486
|
pendingStyles.removeAll()
|
|
2341
2487
|
explicitlyOffStyles.removeAll()
|
|
2342
2488
|
pendingStylesInsertPos = -1
|
|
2489
|
+
lastKnownTextLength = 0
|
|
2490
|
+
pendingExternalForeground = nil
|
|
2491
|
+
pendingExternalForegroundCleared = false
|
|
2343
2492
|
deferredTypingAttrs = nil
|
|
2344
2493
|
// Reset typingAttributes to plain so next typed text has no ghost formatting
|
|
2345
2494
|
let paragraphStyle = NSMutableParagraphStyle()
|
|
@@ -2404,7 +2553,11 @@ class RichTextEditorView: UIView, UITextViewDelegate, UIGestureRecognizerDelegat
|
|
|
2404
2553
|
var cleanAttrs = textView.typingAttributes
|
|
2405
2554
|
cleanAttrs.removeValue(forKey: .backgroundColor)
|
|
2406
2555
|
cleanAttrs.removeValue(forKey: MentionMarkerKey)
|
|
2407
|
-
|
|
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 {
|
|
2408
2561
|
var r: CGFloat = 0, g: CGFloat = 0, b: CGFloat = 0, a: CGFloat = 0
|
|
2409
2562
|
fg.getRed(&r, green: &g, blue: &b, alpha: &a)
|
|
2410
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 {
|
|
@@ -2659,27 +2812,27 @@ class RichTextEditorView: UIView, UITextViewDelegate, UIGestureRecognizerDelegat
|
|
|
2659
2812
|
|
|
2660
2813
|
for line in lines {
|
|
2661
2814
|
// Check for blockquote + numbered: "▎ N. "
|
|
2662
|
-
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)) {
|
|
2663
2816
|
counter += 1
|
|
2664
2817
|
let oldPrefix = (line as NSString).substring(with: quoteMatch.range)
|
|
2665
2818
|
let newPrefix = "▎ \(counter). "
|
|
2666
2819
|
if oldPrefix != newPrefix {
|
|
2667
2820
|
replacements.append((NSRange(location: offset, length: oldPrefix.count), newPrefix))
|
|
2668
2821
|
}
|
|
2669
|
-
} 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)) {
|
|
2670
2823
|
counter += 1
|
|
2671
2824
|
let oldPrefix = (line as NSString).substring(with: match.range)
|
|
2672
2825
|
let newPrefix = "\(counter). "
|
|
2673
2826
|
if oldPrefix != newPrefix {
|
|
2674
2827
|
replacements.append((NSRange(location: offset, length: oldPrefix.count), newPrefix))
|
|
2675
2828
|
}
|
|
2676
|
-
} 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 {
|
|
2677
2830
|
// Bullet lines don't reset the counter — ordered numbering
|
|
2678
2831
|
// continues across bullet interruptions (Slack behavior).
|
|
2679
2832
|
} else {
|
|
2680
2833
|
counter = 0
|
|
2681
2834
|
}
|
|
2682
|
-
offset += line.
|
|
2835
|
+
offset += (line as NSString).length + 1
|
|
2683
2836
|
}
|
|
2684
2837
|
|
|
2685
2838
|
if !replacements.isEmpty {
|
|
@@ -2939,7 +3092,8 @@ class RichTextEditorView: UIView, UITextViewDelegate, UIGestureRecognizerDelegat
|
|
|
2939
3092
|
let cleanText = (textView.text ?? "").replacingOccurrences(of: "\u{200B}", with: "")
|
|
2940
3093
|
onContentChange?([
|
|
2941
3094
|
"text": cleanText,
|
|
2942
|
-
"blocksJson": blocksJson
|
|
3095
|
+
"blocksJson": blocksJson,
|
|
3096
|
+
"mentionRangesJson": currentMentionRangesJson()
|
|
2943
3097
|
])
|
|
2944
3098
|
}
|
|
2945
3099
|
|
|
@@ -3466,7 +3620,7 @@ class RichTextEditorView: UIView, UITextViewDelegate, UIGestureRecognizerDelegat
|
|
|
3466
3620
|
}
|
|
3467
3621
|
|
|
3468
3622
|
if !hasMonospace {
|
|
3469
|
-
|
|
3623
|
+
paintInlineCodeForeground(on: mutableAttrString, range: range)
|
|
3470
3624
|
mutableAttrString.addAttribute(.backgroundColor, value: inlineCodeBgColor, range: range)
|
|
3471
3625
|
} else {
|
|
3472
3626
|
mutableAttrString.removeAttribute(.backgroundColor, range: range)
|
|
@@ -3483,6 +3637,237 @@ class RichTextEditorView: UIView, UITextViewDelegate, UIGestureRecognizerDelegat
|
|
|
3483
3637
|
updateToolbarButtonStates()
|
|
3484
3638
|
}
|
|
3485
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
|
+
|
|
3486
3871
|
func toggleHighlight(color: String?) {
|
|
3487
3872
|
let range = textView.selectedRange
|
|
3488
3873
|
guard range.length > 0 else { return }
|
|
@@ -4111,6 +4496,24 @@ class RichTextEditorView: UIView, UITextViewDelegate, UIGestureRecognizerDelegat
|
|
|
4111
4496
|
|
|
4112
4497
|
/// Applies pending style toggles to typingAttributes so newly typed text inherits them.
|
|
4113
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
|
+
|
|
4114
4517
|
private func applyPendingStylesToTypingAttributes() {
|
|
4115
4518
|
var attrs = textView.typingAttributes
|
|
4116
4519
|
var baseFont = attrs[.font] as? UIFont ?? UIFont.systemFont(ofSize: 16)
|
|
@@ -4178,6 +4581,19 @@ class RichTextEditorView: UIView, UITextViewDelegate, UIGestureRecognizerDelegat
|
|
|
4178
4581
|
attrs.removeValue(forKey: .strikethroughStyle)
|
|
4179
4582
|
}
|
|
4180
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
|
+
|
|
4181
4597
|
textView.typingAttributes = attrs
|
|
4182
4598
|
}
|
|
4183
4599
|
|
|
@@ -4190,6 +4606,9 @@ class RichTextEditorView: UIView, UITextViewDelegate, UIGestureRecognizerDelegat
|
|
|
4190
4606
|
}
|
|
4191
4607
|
|
|
4192
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()
|
|
4193
4612
|
// Mutual exclusivity: exit code block mode before applying list (matches Android toggleListPrefix)
|
|
4194
4613
|
// Remove code block attributes inline rather than calling toggleCodeBlock() to avoid
|
|
4195
4614
|
// textView.attributedText reassignment mid-flow.
|
|
@@ -4247,6 +4666,11 @@ class RichTextEditorView: UIView, UITextViewDelegate, UIGestureRecognizerDelegat
|
|
|
4247
4666
|
pendingStyles.remove("underline")
|
|
4248
4667
|
pendingStyles.remove("strikethrough")
|
|
4249
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
|
|
4250
4674
|
explicitlyOffStyles.removeAll()
|
|
4251
4675
|
// Reset typingAttributes to plain so list prefix doesn't inherit code block styling
|
|
4252
4676
|
let plainParaStyle = NSMutableParagraphStyle()
|
|
@@ -4311,7 +4735,7 @@ class RichTextEditorView: UIView, UITextViewDelegate, UIGestureRecognizerDelegat
|
|
|
4311
4735
|
var lineOffsets: [(start: Int, line: String)] = []
|
|
4312
4736
|
for line in lines {
|
|
4313
4737
|
lineOffsets.append((start: lineOffset, line: line))
|
|
4314
|
-
lineOffset += line.
|
|
4738
|
+
lineOffset += (line as NSString).length + 1 // +1 for \n (UTF-16 units, to match NSRange)
|
|
4315
4739
|
}
|
|
4316
4740
|
|
|
4317
4741
|
for (index, entry) in lineOffsets.enumerated().reversed() {
|
|
@@ -4394,6 +4818,7 @@ class RichTextEditorView: UIView, UITextViewDelegate, UIGestureRecognizerDelegat
|
|
|
4394
4818
|
// set textView.attributedText and wipe typingAttributes.
|
|
4395
4819
|
applyPendingStylesToTypingAttributes()
|
|
4396
4820
|
deferredTypingAttrs = textView.typingAttributes
|
|
4821
|
+
syncPendingStyleAnchor()
|
|
4397
4822
|
|
|
4398
4823
|
// Re-check placeholder after all post-processing (applyListIndentation /
|
|
4399
4824
|
// renumberNumberedLists may set textView.attributedText which can cause
|
|
@@ -4499,7 +4924,12 @@ class RichTextEditorView: UIView, UITextViewDelegate, UIGestureRecognizerDelegat
|
|
|
4499
4924
|
guard let start = style["start"] as? Int,
|
|
4500
4925
|
let end = style["end"] as? Int,
|
|
4501
4926
|
let styleType = style["style"] as? String,
|
|
4502
|
-
|
|
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 }
|
|
4503
4933
|
|
|
4504
4934
|
let range = NSRange(location: start + prefixLength, length: end - start)
|
|
4505
4935
|
|
|
@@ -4528,6 +4958,14 @@ class RichTextEditorView: UIView, UITextViewDelegate, UIGestureRecognizerDelegat
|
|
|
4528
4958
|
blockAttrString.addAttribute(.underlineStyle, value: NSUnderlineStyle.single.rawValue, range: range)
|
|
4529
4959
|
case "strikethrough":
|
|
4530
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
|
+
}
|
|
4531
4969
|
case "code":
|
|
4532
4970
|
let monoFont = UIFont.monospacedSystemFont(ofSize: 15, weight: .regular)
|
|
4533
4971
|
blockAttrString.addAttribute(.font, value: monoFont, range: range)
|
|
@@ -4543,6 +4981,22 @@ class RichTextEditorView: UIView, UITextViewDelegate, UIGestureRecognizerDelegat
|
|
|
4543
4981
|
break
|
|
4544
4982
|
}
|
|
4545
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
|
+
}
|
|
4546
5000
|
}
|
|
4547
5001
|
|
|
4548
5002
|
if blockIndex < blocks.count - 1 {
|
|
@@ -4582,12 +5036,32 @@ class RichTextEditorView: UIView, UITextViewDelegate, UIGestureRecognizerDelegat
|
|
|
4582
5036
|
pendingStylesInsertPos = endPosition
|
|
4583
5037
|
let cbParaStyle = NSMutableParagraphStyle()
|
|
4584
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.
|
|
4585
5041
|
textView.typingAttributes = [
|
|
4586
5042
|
.font: UIFont.monospacedSystemFont(ofSize: 16, weight: .regular),
|
|
4587
5043
|
.foregroundColor: UIColor.label,
|
|
4588
5044
|
.paragraphStyle: cbParaStyle,
|
|
4589
5045
|
CodeBlockAttributeKey: true
|
|
4590
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
|
|
4591
5065
|
}
|
|
4592
5066
|
}
|
|
4593
5067
|
}
|
|
@@ -4608,6 +5082,15 @@ class RichTextEditorView: UIView, UITextViewDelegate, UIGestureRecognizerDelegat
|
|
|
4608
5082
|
return textView.text ?? ""
|
|
4609
5083
|
}
|
|
4610
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`.
|
|
4611
5094
|
func getBlocksArray() -> [[String: Any]] {
|
|
4612
5095
|
let text = textView.text ?? ""
|
|
4613
5096
|
let attributedText = textView.attributedText ?? NSAttributedString()
|
|
@@ -4628,22 +5111,22 @@ class RichTextEditorView: UIView, UITextViewDelegate, UIGestureRecognizerDelegat
|
|
|
4628
5111
|
if line.hasPrefix("▎ • ") {
|
|
4629
5112
|
blockType = "quoteBullet"
|
|
4630
5113
|
displayText = String(line.dropFirst(4))
|
|
4631
|
-
} 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)) {
|
|
4632
5115
|
blockType = "quoteNumbered"
|
|
4633
|
-
displayText =
|
|
5116
|
+
displayText = (line as NSString).substring(from: qnMatch.range.length)
|
|
4634
5117
|
} else if line.hasPrefix("• ") {
|
|
4635
5118
|
blockType = "bullet"
|
|
4636
5119
|
displayText = String(line.dropFirst(2))
|
|
4637
|
-
} 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)) {
|
|
4638
5121
|
blockType = "numbered"
|
|
4639
|
-
displayText =
|
|
5122
|
+
displayText = (line as NSString).substring(from: match.range.length)
|
|
4640
5123
|
} else if line.hasPrefix("▎ ") {
|
|
4641
5124
|
blockType = "quote"
|
|
4642
5125
|
displayText = String(line.dropFirst(2))
|
|
4643
5126
|
}
|
|
4644
5127
|
|
|
4645
5128
|
// Detect code block: check the line range for CodeBlockAttributeKey
|
|
4646
|
-
let lineRange = NSRange(location: currentIndex, length: line.
|
|
5129
|
+
let lineRange = NSRange(location: currentIndex, length: (line as NSString).length)
|
|
4647
5130
|
if lineRange.location + lineRange.length <= attributedText.length && lineRange.length > 0 {
|
|
4648
5131
|
var hasCodeBlockAttr = false
|
|
4649
5132
|
attributedText.enumerateAttribute(CodeBlockAttributeKey, in: lineRange, options: []) { value, _, _ in
|
|
@@ -4667,9 +5150,9 @@ class RichTextEditorView: UIView, UITextViewDelegate, UIGestureRecognizerDelegat
|
|
|
4667
5150
|
displayText = displayText.replacingOccurrences(of: "\u{200B}", with: "")
|
|
4668
5151
|
|
|
4669
5152
|
var styles: [[String: Any]] = []
|
|
4670
|
-
let prefixLength = line.
|
|
5153
|
+
let prefixLength = (line as NSString).length - (displayText as NSString).length
|
|
4671
5154
|
let styleRangeStart = currentIndex + prefixLength
|
|
4672
|
-
let styleRangeLength = displayText.
|
|
5155
|
+
let styleRangeLength = (displayText as NSString).length
|
|
4673
5156
|
let styleLineRange = NSRange(location: styleRangeStart, length: styleRangeLength)
|
|
4674
5157
|
|
|
4675
5158
|
if styleLineRange.location + styleLineRange.length <= attributedText.length {
|
|
@@ -4709,6 +5192,17 @@ class RichTextEditorView: UIView, UITextViewDelegate, UIGestureRecognizerDelegat
|
|
|
4709
5192
|
if attrs[.strikethroughStyle] != nil {
|
|
4710
5193
|
styles.append(["style": "strikethrough", "start": relativeStart, "end": relativeEnd])
|
|
4711
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
|
+
}
|
|
4712
5206
|
}
|
|
4713
5207
|
}
|
|
4714
5208
|
|
|
@@ -4718,7 +5212,7 @@ class RichTextEditorView: UIView, UITextViewDelegate, UIGestureRecognizerDelegat
|
|
|
4718
5212
|
"styles": styles
|
|
4719
5213
|
])
|
|
4720
5214
|
|
|
4721
|
-
currentIndex += line.
|
|
5215
|
+
currentIndex += (line as NSString).length + 1 // +1 for the "\n" the split removed
|
|
4722
5216
|
}
|
|
4723
5217
|
|
|
4724
5218
|
return blocks
|
|
@@ -4733,6 +5227,7 @@ class RichTextEditorView: UIView, UITextViewDelegate, UIGestureRecognizerDelegat
|
|
|
4733
5227
|
pendingStyles.removeAll()
|
|
4734
5228
|
explicitlyOffStyles.removeAll()
|
|
4735
5229
|
pendingStylesInsertPos = -1
|
|
5230
|
+
lastKnownTextLength = 0
|
|
4736
5231
|
// Reset typingAttributes to plain so next typed text has no formatting
|
|
4737
5232
|
let paragraphStyle = NSMutableParagraphStyle()
|
|
4738
5233
|
paragraphStyle.lineHeightMultiple = RichTextEditorView.defaultLineHeightMultiple
|
|
@@ -4897,6 +5392,11 @@ class RichTextEditorView: UIView, UITextViewDelegate, UIGestureRecognizerDelegat
|
|
|
4897
5392
|
]
|
|
4898
5393
|
// Track code block as a pending style so it survives textDidChange clearing
|
|
4899
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)
|
|
4900
5400
|
pendingStylesInsertPos = textView.selectedRange.location
|
|
4901
5401
|
isInternalChange = false
|
|
4902
5402
|
textView.setNeedsDisplay()
|
|
@@ -4955,6 +5455,17 @@ class RichTextEditorView: UIView, UITextViewDelegate, UIGestureRecognizerDelegat
|
|
|
4955
5455
|
mutableAttr.addAttribute(CodeBlockAttributeKey, value: true, range: lineRange)
|
|
4956
5456
|
// Strip mention markers so setMentionRanges() won't re-apply mention styling
|
|
4957
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)
|
|
4958
5469
|
// Set pendingStyles + typingAttributes so newly typed characters
|
|
4959
5470
|
// inherit code block mode (matches empty-line activation path)
|
|
4960
5471
|
pendingStyles.insert("codeBlock")
|
|
@@ -5175,7 +5686,9 @@ class RichTextEditorView: UIView, UITextViewDelegate, UIGestureRecognizerDelegat
|
|
|
5175
5686
|
bgColor.getRed(&r, green: &g, blue: &b, alpha: &a)
|
|
5176
5687
|
// Mention bg: rgba(104/255, 82/255, 214/255, 0.15) — low alpha purple
|
|
5177
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
|
|
5178
|
-
|
|
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 {
|
|
5179
5692
|
mutableAttr.removeAttribute(.backgroundColor, range: range)
|
|
5180
5693
|
}
|
|
5181
5694
|
}
|
|
@@ -5194,6 +5707,14 @@ class RichTextEditorView: UIView, UITextViewDelegate, UIGestureRecognizerDelegat
|
|
|
5194
5707
|
}
|
|
5195
5708
|
}
|
|
5196
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
|
+
|
|
5197
5718
|
// Re-apply link foreground color (systemBlue) to ranges with LinkURLAttributeKey
|
|
5198
5719
|
// so that links remain visually blue after the blanket reset above.
|
|
5199
5720
|
mutableAttr.enumerateAttribute(LinkURLAttributeKey, in: fullRange, options: []) { value, range, _ in
|
|
@@ -5202,10 +5723,11 @@ class RichTextEditorView: UIView, UITextViewDelegate, UIGestureRecognizerDelegat
|
|
|
5202
5723
|
}
|
|
5203
5724
|
}
|
|
5204
5725
|
|
|
5205
|
-
// 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).
|
|
5206
5728
|
mutableAttr.enumerateAttribute(.backgroundColor, in: fullRange, options: []) { value, range, _ in
|
|
5207
5729
|
if let bgColor = value as? UIColor, bgColor == inlineCodeBgColor {
|
|
5208
|
-
|
|
5730
|
+
self.paintInlineCodeForeground(on: mutableAttr, range: range)
|
|
5209
5731
|
}
|
|
5210
5732
|
}
|
|
5211
5733
|
|
|
@@ -5286,8 +5808,10 @@ class RichTextEditorView: UIView, UITextViewDelegate, UIGestureRecognizerDelegat
|
|
|
5286
5808
|
cleanAttrs.removeValue(forKey: .backgroundColor)
|
|
5287
5809
|
}
|
|
5288
5810
|
}
|
|
5289
|
-
// Remove mention foreground color — use component comparison
|
|
5290
|
-
|
|
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 {
|
|
5291
5815
|
var r: CGFloat = 0, g: CGFloat = 0, b: CGFloat = 0, a: CGFloat = 0
|
|
5292
5816
|
fg.getRed(&r, green: &g, blue: &b, alpha: &a)
|
|
5293
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 {
|
|
@@ -5496,7 +6020,7 @@ class RichTextEditorView: UIView, UITextViewDelegate, UIGestureRecognizerDelegat
|
|
|
5496
6020
|
case "code":
|
|
5497
6021
|
let monoFont = UIFont.monospacedSystemFont(ofSize: 15, weight: .regular)
|
|
5498
6022
|
mutableAttr.addAttribute(.font, value: monoFont, range: fmtRange)
|
|
5499
|
-
|
|
6023
|
+
paintInlineCodeForeground(on: mutableAttr, range: fmtRange)
|
|
5500
6024
|
mutableAttr.addAttribute(.backgroundColor, value: inlineCodeBgColor, range: fmtRange)
|
|
5501
6025
|
case "codeBlock":
|
|
5502
6026
|
let monoFont = UIFont.monospacedSystemFont(ofSize: 16, weight: .regular)
|
|
@@ -5555,6 +6079,8 @@ class RichTextEditorView: UIView, UITextViewDelegate, UIGestureRecognizerDelegat
|
|
|
5555
6079
|
/// Returns true if the string contains markdown syntax that should be parsed into rich text.
|
|
5556
6080
|
/// Matches Android looksLikeMarkdown().
|
|
5557
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 }
|
|
5558
6084
|
if str.contains("[") && str.contains("](") { return true }
|
|
5559
6085
|
if str.contains("**") { return true }
|
|
5560
6086
|
if str.contains("~~") { return true }
|
|
@@ -5579,6 +6105,7 @@ class RichTextEditorView: UIView, UITextViewDelegate, UIGestureRecognizerDelegat
|
|
|
5579
6105
|
var isUnderline = false
|
|
5580
6106
|
var isStrikethrough = false
|
|
5581
6107
|
var isCode = false
|
|
6108
|
+
var activePasteColor: UIColor? = nil
|
|
5582
6109
|
|
|
5583
6110
|
let defaultFont = UIFont.systemFont(ofSize: 16)
|
|
5584
6111
|
let monoFont = UIFont.monospacedSystemFont(ofSize: 15, weight: .regular)
|
|
@@ -5613,11 +6140,46 @@ class RichTextEditorView: UIView, UITextViewDelegate, UIGestureRecognizerDelegat
|
|
|
5613
6140
|
}
|
|
5614
6141
|
}
|
|
5615
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
|
+
|
|
5616
6157
|
result.append(NSAttributedString(string: currentText, attributes: attrs))
|
|
5617
6158
|
currentText = ""
|
|
5618
6159
|
}
|
|
5619
6160
|
|
|
5620
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
|
+
}
|
|
5621
6183
|
if chars[i] == "[" {
|
|
5622
6184
|
if let (linkText, linkURL, fullEnd) = parseLinkAt(chars, from: i) {
|
|
5623
6185
|
flush()
|
|
@@ -5847,7 +6409,7 @@ class RichTextEditorView: UIView, UITextViewDelegate, UIGestureRecognizerDelegat
|
|
|
5847
6409
|
|
|
5848
6410
|
isInternalChange = true
|
|
5849
6411
|
textView.attributedText = mutableAttr
|
|
5850
|
-
let newCursorPos = range.location + displayText.
|
|
6412
|
+
let newCursorPos = range.location + (displayText as NSString).length
|
|
5851
6413
|
textView.selectedRange = NSRange(location: newCursorPos, length: 0)
|
|
5852
6414
|
|
|
5853
6415
|
let paragraphStyle = NSMutableParagraphStyle()
|