@cometchat/chat-uikit-react-native 5.4.1 → 5.5.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/android/src/main/java/com/reactnativecometchatuikit/CometChatClipboardModule.kt +34 -0
- package/android/src/main/java/com/reactnativecometchatuikit/RichTextEditorView.kt +562 -5
- package/android/src/main/java/com/reactnativecometchatuikit/RichTextEditorViewManager.kt +28 -1
- package/ios/CometChatClipboardModule.m +5 -0
- package/ios/CometChatClipboardModule.swift +22 -0
- package/ios/RichTextEditorView.swift +636 -31
- package/ios/RichTextEditorViewManager.m +3 -0
- package/ios/RichTextEditorViewManager.swift +24 -0
- package/package.json +5 -2
- package/src/CometChatCompactMessageComposer/CometChatCompactMessageComposer.tsx +283 -64
- package/src/CometChatCompactMessageComposer/index.ts +2 -0
- package/src/CometChatConversations/CometChatConversations.tsx +299 -15
- package/src/CometChatGroupMembers/CometChatGroupMembers.tsx +2 -2
- package/src/CometChatMessageComposer/CometChatMessageComposer.tsx +15 -3
- package/src/CometChatMessageHeader/CometChatMessageHeader.tsx +181 -12
- package/src/CometChatMessageInformation/CometChatMessageInformation.tsx +10 -1
- package/src/CometChatMessageList/CometChatMessageList.tsx +572 -16
- package/src/CometChatMessageList/components/MessageModals.tsx +30 -0
- package/src/CometChatMessageList/components/MessageOptionsSheet.tsx +21 -9
- package/src/CometChatMessageList/components/MessageQuickActions.tsx +162 -0
- package/src/CometChatPinnedMessages/CometChatPinnedMessages.tsx +964 -0
- package/src/CometChatPinnedMessages/index.ts +5 -0
- package/src/CometChatPinnedMessages/style.ts +216 -0
- package/src/CometChatRichTextEditor/RichTextEditorViewNativeComponent.ts +5 -0
- package/src/CometChatRichTextEditor/index.tsx +31 -1
- package/src/CometChatRichTextEditor/types.ts +21 -1
- package/src/CometChatSavedMessages/CometChatSavedMessages.tsx +871 -0
- package/src/CometChatSavedMessages/index.ts +5 -0
- package/src/CometChatSavedMessages/style.ts +159 -0
- package/src/CometChatSearch/CometChatSearch.tsx +71 -341
- package/src/CometChatThreadHeader/CometChatThreadHeader.tsx +22 -2
- package/src/index.ts +60 -0
- package/src/shared/CometChatUiKit/CometChatUIKit.ts +18 -1
- package/src/shared/base/Types.ts +5 -0
- package/src/shared/constants/UIKitConstants.ts +16 -17
- package/src/shared/constants/textPatterns.ts +27 -0
- package/src/shared/events/CometChatUIEventHandler/CometChatUIEventHandler.ts +25 -225
- package/src/shared/events/CometChatUIEventHandler/Listener.ts +37 -0
- package/src/shared/events/ListenerInitializer.ts +20 -1
- package/src/shared/events/messages.ts +18 -0
- package/src/shared/formatters/CometChatRichTextFormatter/index.tsx +123 -25
- package/src/shared/formatters/CometChatTextFormatter.ts +22 -0
- package/src/shared/formatters/applyRawFormatters.ts +31 -0
- package/src/shared/formatters/index.ts +14 -0
- package/src/shared/formatters/richTextWireFormat.ts +165 -0
- package/src/shared/framework/MessageDataSource.tsx +309 -10
- package/src/shared/icons/components/thread-follow.tsx +13 -0
- package/src/shared/icons/components/thread-unfollow.tsx +13 -0
- package/src/shared/icons/components/unsave.tsx +13 -0
- package/src/shared/icons/icon-mapping.tsx +6 -0
- package/src/shared/index.ts +37 -0
- package/src/shared/resources/CometChatLocalizeNew/CometChatI18nProvider.tsx +15 -30
- package/src/shared/resources/CometChatLocalizeNew/CometChatLocalizationHelper.ts +138 -19
- package/src/shared/resources/CometChatLocalizeNew/resources/de/translation.json +74 -1
- package/src/shared/resources/CometChatLocalizeNew/resources/en/translation.json +54 -1
- package/src/shared/resources/CometChatLocalizeNew/resources/es/translation.json +54 -1
- package/src/shared/resources/CometChatLocalizeNew/resources/fr/translation.json +54 -1
- package/src/shared/resources/CometChatLocalizeNew/resources/hi/translation.json +54 -1
- package/src/shared/resources/CometChatLocalizeNew/resources/hu/translation.json +54 -1
- package/src/shared/resources/CometChatLocalizeNew/resources/it/translation.json +54 -1
- package/src/shared/resources/CometChatLocalizeNew/resources/ja/translation.json +54 -1
- package/src/shared/resources/CometChatLocalizeNew/resources/ko/translation.json +54 -1
- package/src/shared/resources/CometChatLocalizeNew/resources/lt/translation.json +54 -1
- package/src/shared/resources/CometChatLocalizeNew/resources/ms/translation.json +54 -1
- package/src/shared/resources/CometChatLocalizeNew/resources/nl/translation.json +54 -1
- package/src/shared/resources/CometChatLocalizeNew/resources/pt/translation.json +54 -1
- package/src/shared/resources/CometChatLocalizeNew/resources/ru/translation.json +54 -1
- package/src/shared/resources/CometChatLocalizeNew/resources/sv/translation.json +54 -1
- package/src/shared/resources/CometChatLocalizeNew/resources/tr/translation.json +54 -1
- package/src/shared/resources/CometChatLocalizeNew/resources/zh/translation.json +54 -1
- package/src/shared/resources/CometChatLocalizeNew/resources/zh-tw/translation.json +54 -1
- package/src/shared/utils/CometChatMessagePreview/CometChatMessagePreview.tsx +11 -0
- package/src/shared/utils/MarkdownUtils.ts +57 -6
- package/src/shared/utils/MessageUtils.tsx +80 -5
- package/src/shared/utils/PinConversationHelper.ts +245 -0
- package/src/shared/utils/PinSaveFeatureGates.ts +178 -0
- package/src/shared/utils/PinSaveHelper.ts +549 -0
- package/src/shared/utils/PinnedMessagesHelper.ts +65 -0
- package/src/shared/utils/SavedMessagesHelper.ts +267 -0
- package/src/shared/utils/ThreadSubscriptionHelper.ts +427 -0
- package/src/shared/utils/__testUtils__/messagesRequestBuilderMock.ts +37 -0
- package/src/shared/utils/cloneRequestBuilder.ts +25 -0
- package/src/shared/utils/conversationUtils.ts +8 -1
- package/src/shared/utils/stripRichText.ts +7 -0
- package/src/shared/views/ClipboardPasteHandler/ClipboardPasteHandler.ts +32 -0
- package/src/shared/views/ClipboardPasteHandler/NativeCometChatClipboardModule.ts +6 -0
- package/src/shared/views/CometChatActionSheet/CometChatActionSheet.tsx +4 -0
- package/src/shared/views/CometChatAudiosBubble/CometChatAudiosBubble.tsx +5 -0
- package/src/shared/views/CometChatAvatar/CometChatAvatar.tsx +17 -5
- package/src/shared/views/CometChatBottomSheet/CometChatBottomSheet.tsx +5 -1
- package/src/shared/views/CometChatCaptionText/CometChatCaptionText.tsx +8 -1
- package/src/shared/views/CometChatConfirmDialog/CometChatConfirmDialog.tsx +2 -0
- package/src/shared/views/CometChatConversationStarter/CometChatConversationStarter.tsx +91 -20
- package/src/shared/views/CometChatFilesBubble/CometChatFilesBubble.tsx +5 -0
- package/src/shared/views/CometChatImagesBubble/CometChatImagesBubble.tsx +5 -0
- package/src/shared/views/CometChatList/CometChatList.tsx +6 -1
- package/src/shared/views/CometChatList/Header.tsx +8 -2
- package/src/shared/views/CometChatListItem/CometChatListItem.tsx +4 -0
- package/src/shared/views/CometChatRetryButton/CometChatRetryButton.tsx +1 -0
- package/src/shared/views/CometChatSuggestionList/CometChatSuggestionList.tsx +4 -0
- package/src/shared/views/CometChatVideosBubble/CometChatVideosBubble.tsx +5 -0
- package/src/shared/views/CometChatVoiceNoteBubble/CometChatVoiceNoteBubble.tsx +5 -0
- package/src/shared/views/ErrorEmptyView/ErrorEmptyView.tsx +4 -1
- package/src/theme/default/default.ts +6 -0
- package/src/theme/type.ts +4 -0
|
@@ -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()
|
|
1144
1223
|
pendingStylesInsertPos = textView.selectedRange.location
|
|
1145
1224
|
}
|
|
1146
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 }
|
|
1234
|
+
pendingStylesInsertPos = textView.selectedRange.location
|
|
1235
|
+
}
|
|
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
|
|
|
@@ -2987,6 +3141,11 @@ class RichTextEditorView: UIView, UITextViewDelegate, UIGestureRecognizerDelegat
|
|
|
2987
3141
|
|
|
2988
3142
|
/// Returns true when a UTI represents pasteable media (image/video/audio/file),
|
|
2989
3143
|
/// and false for plain text / URLs which should paste as text.
|
|
3144
|
+
///
|
|
3145
|
+
/// Rich-text containers are excluded too. Copying TEXT puts one on the pasteboard next to
|
|
3146
|
+
/// the plain string — Safari adds a web archive, Notes / Mail / Pages / TextEdit add RTFD.
|
|
3147
|
+
/// Both conform to public.data, so without this a text copy from those apps was uploaded
|
|
3148
|
+
/// as a WEBARCHIVE / RTFD attachment and the text itself was never pasted.
|
|
2990
3149
|
private static func isMediaTypeIdentifier(_ typeId: String) -> Bool {
|
|
2991
3150
|
if #available(iOS 14.0, *) {
|
|
2992
3151
|
guard let type = UTType(typeId) else { return false }
|
|
@@ -2994,6 +3153,9 @@ class RichTextEditorView: UIView, UITextViewDelegate, UIGestureRecognizerDelegat
|
|
|
2994
3153
|
if type.conforms(to: .plainText) || type.conforms(to: .url) || type.conforms(to: .text) {
|
|
2995
3154
|
return false
|
|
2996
3155
|
}
|
|
3156
|
+
if type.conforms(to: .webArchive) || type.conforms(to: .rtfd) || type.conforms(to: .flatRTFD) {
|
|
3157
|
+
return false
|
|
3158
|
+
}
|
|
2997
3159
|
return type.conforms(to: .image)
|
|
2998
3160
|
|| type.conforms(to: .movie)
|
|
2999
3161
|
|| type.conforms(to: .audio)
|
|
@@ -3006,6 +3168,9 @@ class RichTextEditorView: UIView, UITextViewDelegate, UIGestureRecognizerDelegat
|
|
|
3006
3168
|
if UTTypeConformsTo(cf, kUTTypePlainText) || UTTypeConformsTo(cf, kUTTypeText) || UTTypeConformsTo(cf, kUTTypeURL) {
|
|
3007
3169
|
return false
|
|
3008
3170
|
}
|
|
3171
|
+
if UTTypeConformsTo(cf, kUTTypeWebArchive) || UTTypeConformsTo(cf, kUTTypeRTFD) || UTTypeConformsTo(cf, kUTTypeFlatRTFD) {
|
|
3172
|
+
return false
|
|
3173
|
+
}
|
|
3009
3174
|
return UTTypeConformsTo(cf, kUTTypeImage)
|
|
3010
3175
|
|| UTTypeConformsTo(cf, kUTTypeMovie)
|
|
3011
3176
|
|| UTTypeConformsTo(cf, kUTTypeAudio)
|
|
@@ -3466,7 +3631,7 @@ class RichTextEditorView: UIView, UITextViewDelegate, UIGestureRecognizerDelegat
|
|
|
3466
3631
|
}
|
|
3467
3632
|
|
|
3468
3633
|
if !hasMonospace {
|
|
3469
|
-
|
|
3634
|
+
paintInlineCodeForeground(on: mutableAttrString, range: range)
|
|
3470
3635
|
mutableAttrString.addAttribute(.backgroundColor, value: inlineCodeBgColor, range: range)
|
|
3471
3636
|
} else {
|
|
3472
3637
|
mutableAttrString.removeAttribute(.backgroundColor, range: range)
|
|
@@ -3483,6 +3648,237 @@ class RichTextEditorView: UIView, UITextViewDelegate, UIGestureRecognizerDelegat
|
|
|
3483
3648
|
updateToolbarButtonStates()
|
|
3484
3649
|
}
|
|
3485
3650
|
|
|
3651
|
+
// MARK: - Arbitrary inline styles (consumer formatters)
|
|
3652
|
+
|
|
3653
|
+
/// Maps a public style key to (the attribute it drives, its external marker).
|
|
3654
|
+
/// ponytail: colors are the only inline style a trailing-button formatter needs today —
|
|
3655
|
+
/// widen the switch if fonts/spacing ever follow.
|
|
3656
|
+
private static func inlineStyleAttributes(for key: String) -> (NSAttributedString.Key, NSAttributedString.Key)? {
|
|
3657
|
+
switch key {
|
|
3658
|
+
case "color": return (.foregroundColor, ExternalForegroundKey)
|
|
3659
|
+
case "backgroundColor": return (.backgroundColor, ExternalBackgroundKey)
|
|
3660
|
+
default: return nil
|
|
3661
|
+
}
|
|
3662
|
+
}
|
|
3663
|
+
|
|
3664
|
+
/// Unpacks an RN `processColor()` ARGB int.
|
|
3665
|
+
private static func color(fromARGB argb: Int) -> UIColor {
|
|
3666
|
+
UIColor(
|
|
3667
|
+
red: CGFloat((argb >> 16) & 0xFF) / 255.0,
|
|
3668
|
+
green: CGFloat((argb >> 8) & 0xFF) / 255.0,
|
|
3669
|
+
blue: CGFloat(argb & 0xFF) / 255.0,
|
|
3670
|
+
alpha: CGFloat((argb >> 24) & 0xFF) / 255.0
|
|
3671
|
+
)
|
|
3672
|
+
}
|
|
3673
|
+
|
|
3674
|
+
/// Sub-ranges of `range` a consumer style may touch: mentions AND links are protected.
|
|
3675
|
+
/// The general-purpose `nonMentionRanges` above only guards mentions, but Android's
|
|
3676
|
+
/// equivalent also guards `URLSpan` — and colour is exactly what a link uses to signal
|
|
3677
|
+
/// itself, so recolouring one would diverge visibly between platforms.
|
|
3678
|
+
private func inlineStyleTargetRanges(in range: NSRange, attributedText: NSAttributedString) -> [NSRange] {
|
|
3679
|
+
var protectedRanges: [NSRange] = []
|
|
3680
|
+
for key in [MentionMarkerKey, LinkURLAttributeKey] {
|
|
3681
|
+
attributedText.enumerateAttribute(key, in: range, options: []) { value, attrRange, _ in
|
|
3682
|
+
if value != nil { protectedRanges.append(attrRange) }
|
|
3683
|
+
}
|
|
3684
|
+
}
|
|
3685
|
+
if protectedRanges.isEmpty { return [range] }
|
|
3686
|
+
protectedRanges.sort { $0.location < $1.location }
|
|
3687
|
+
|
|
3688
|
+
var result: [NSRange] = []
|
|
3689
|
+
var cursor = range.location
|
|
3690
|
+
for pr in protectedRanges {
|
|
3691
|
+
if cursor < pr.location {
|
|
3692
|
+
result.append(NSRange(location: cursor, length: pr.location - cursor))
|
|
3693
|
+
}
|
|
3694
|
+
cursor = max(cursor, pr.location + pr.length)
|
|
3695
|
+
}
|
|
3696
|
+
let rangeEnd = range.location + range.length
|
|
3697
|
+
if cursor < rangeEnd {
|
|
3698
|
+
result.append(NSRange(location: cursor, length: rangeEnd - cursor))
|
|
3699
|
+
}
|
|
3700
|
+
return result
|
|
3701
|
+
}
|
|
3702
|
+
|
|
3703
|
+
/// Rebuilds every marker-derived `.foregroundColor` on the given string.
|
|
3704
|
+
///
|
|
3705
|
+
/// Call after any blanket foreground write (`textView.textColor = …`,
|
|
3706
|
+
/// `removeAttribute(.foregroundColor, range: fullRange)`), which UIKit applies across the
|
|
3707
|
+
/// whole attributed string. The order below IS the paint-order policy: the consumer's inline
|
|
3708
|
+
/// colour is laid down first, then the blockquote bar, links, inline code and mentions —
|
|
3709
|
+
/// which own their colour and always win.
|
|
3710
|
+
private func reapplyMarkerForegroundColors(on mutableAttr: NSMutableAttributedString) {
|
|
3711
|
+
let fullRange = NSRange(location: 0, length: mutableAttr.length)
|
|
3712
|
+
guard fullRange.length > 0 else { return }
|
|
3713
|
+
|
|
3714
|
+
// 1. Consumer-applied colour (marker is the source of truth, `.foregroundColor` is paint)
|
|
3715
|
+
mutableAttr.enumerateAttribute(ExternalForegroundKey, in: fullRange, options: []) { value, range, _ in
|
|
3716
|
+
if let color = value as? UIColor {
|
|
3717
|
+
mutableAttr.addAttribute(.foregroundColor, value: color, range: range)
|
|
3718
|
+
}
|
|
3719
|
+
}
|
|
3720
|
+
// 2. Blockquote ▎ (U+258E) stays invisible — the grey bar is custom-drawn in draw(_:)
|
|
3721
|
+
let str = mutableAttr.string as NSString
|
|
3722
|
+
for i in 0..<str.length where str.character(at: i) == 0x258E {
|
|
3723
|
+
mutableAttr.addAttribute(.foregroundColor, value: UIColor.clear, range: NSRange(location: i, length: 1))
|
|
3724
|
+
}
|
|
3725
|
+
// 3. Links
|
|
3726
|
+
mutableAttr.enumerateAttribute(LinkURLAttributeKey, in: fullRange, options: []) { value, range, _ in
|
|
3727
|
+
if value != nil {
|
|
3728
|
+
mutableAttr.addAttribute(.foregroundColor, value: UIColor.systemBlue, range: range)
|
|
3729
|
+
}
|
|
3730
|
+
}
|
|
3731
|
+
// 4. Inline code (identified by its background, matching setMentionRanges).
|
|
3732
|
+
// Skips any sub-range carrying a consumer colour: this step runs after step 1 and
|
|
3733
|
+
// would otherwise repaint it, hiding an applied colour in the composer while the
|
|
3734
|
+
// wire format — which serializes from the style ranges, not from `.foregroundColor` —
|
|
3735
|
+
// still carried it, so the colour reappeared only once sent (ENG-38248).
|
|
3736
|
+
// Colour wins over the inline-code default; the code background is untouched.
|
|
3737
|
+
mutableAttr.enumerateAttribute(.backgroundColor, in: fullRange, options: []) { value, range, _ in
|
|
3738
|
+
guard let bgColor = value as? UIColor, bgColor == inlineCodeBgColor else { return }
|
|
3739
|
+
mutableAttr.enumerateAttribute(ExternalForegroundKey, in: range, options: []) { external, subRange, _ in
|
|
3740
|
+
if external == nil {
|
|
3741
|
+
mutableAttr.addAttribute(.foregroundColor, value: inlineCodeTextColor, range: subRange)
|
|
3742
|
+
}
|
|
3743
|
+
}
|
|
3744
|
+
}
|
|
3745
|
+
// 5. Mentions
|
|
3746
|
+
mutableAttr.enumerateAttribute(MentionMarkerKey, in: fullRange, options: []) { value, range, _ in
|
|
3747
|
+
if value != nil {
|
|
3748
|
+
mutableAttr.addAttribute(.foregroundColor, value: mentionForegroundColor, range: range)
|
|
3749
|
+
}
|
|
3750
|
+
}
|
|
3751
|
+
}
|
|
3752
|
+
|
|
3753
|
+
/// `reapplyMarkerForegroundColors(on:)` against the live text view, preserving the selection
|
|
3754
|
+
/// and typing attributes that `attributedText =` would otherwise reset.
|
|
3755
|
+
private func reapplyMarkerForegroundColors() {
|
|
3756
|
+
guard let attributed = textView.attributedText, attributed.length > 0 else { return }
|
|
3757
|
+
let mutableAttr = NSMutableAttributedString(attributedString: attributed)
|
|
3758
|
+
reapplyMarkerForegroundColors(on: mutableAttr)
|
|
3759
|
+
let savedRange = textView.selectedRange
|
|
3760
|
+
let savedTypingAttrs = textView.typingAttributes
|
|
3761
|
+
isInternalChange = true
|
|
3762
|
+
textView.attributedText = mutableAttr
|
|
3763
|
+
textView.selectedRange = savedRange
|
|
3764
|
+
textView.typingAttributes = savedTypingAttrs
|
|
3765
|
+
isInternalChange = false
|
|
3766
|
+
textView.setNeedsDisplay()
|
|
3767
|
+
}
|
|
3768
|
+
|
|
3769
|
+
private func commitInlineStyle(_ mutableAttrString: NSMutableAttributedString, range: NSRange) {
|
|
3770
|
+
isInternalChange = true
|
|
3771
|
+
textView.attributedText = mutableAttrString
|
|
3772
|
+
textView.selectedRange = range
|
|
3773
|
+
isInternalChange = false
|
|
3774
|
+
saveToUndoStack()
|
|
3775
|
+
sendContentChange()
|
|
3776
|
+
}
|
|
3777
|
+
|
|
3778
|
+
/// Applies an arbitrary inline style to the current selection, skipping mentions so the
|
|
3779
|
+
/// consumer's formatter cannot recolour a mention (§8.2 S4). `value` is an ARGB int from
|
|
3780
|
+
/// RN's `processColor()`. Existing styles are left alone, so styles compose (S3).
|
|
3781
|
+
func applyInlineStyle(key: String, value: Int) {
|
|
3782
|
+
guard let (attribute, marker) = Self.inlineStyleAttributes(for: key) else { return }
|
|
3783
|
+
// An explicit colour choice overrides any deferred restore left by list continuation,
|
|
3784
|
+
// which would otherwise stomp the new colour a keystroke later.
|
|
3785
|
+
deferredTypingAttrs = nil
|
|
3786
|
+
let range = textView.selectedRange
|
|
3787
|
+
let color = Self.color(fromARGB: value)
|
|
3788
|
+
|
|
3789
|
+
// No selection: arm the colour for type-ahead, matching toggleCode/toggleBold rather
|
|
3790
|
+
// than silently doing nothing. Routed through pendingStyles so the Set-gated lifecycle
|
|
3791
|
+
// sites invalidate it on a cursor jump instead of leaking it onto text typed elsewhere.
|
|
3792
|
+
if range.length == 0 {
|
|
3793
|
+
guard attribute == .foregroundColor else {
|
|
3794
|
+
// ponytail: background type-ahead has no pendingStyles hook; it needs a selection.
|
|
3795
|
+
return
|
|
3796
|
+
}
|
|
3797
|
+
pendingExternalForeground = color
|
|
3798
|
+
pendingExternalForegroundCleared = false
|
|
3799
|
+
pendingStyles.insert(ExternalColorPendingKey)
|
|
3800
|
+
pendingStylesInsertPos = range.location
|
|
3801
|
+
applyPendingStylesToTypingAttributes()
|
|
3802
|
+
emitActiveStyles()
|
|
3803
|
+
return
|
|
3804
|
+
}
|
|
3805
|
+
|
|
3806
|
+
let mutableAttrString = NSMutableAttributedString(attributedString: textView.attributedText)
|
|
3807
|
+
for subRange in inlineStyleTargetRanges(in: range, attributedText: mutableAttrString) {
|
|
3808
|
+
mutableAttrString.addAttribute(attribute, value: color, range: subRange)
|
|
3809
|
+
mutableAttrString.addAttribute(marker, value: color, range: subRange)
|
|
3810
|
+
}
|
|
3811
|
+
commitInlineStyle(mutableAttrString, range: range)
|
|
3812
|
+
}
|
|
3813
|
+
|
|
3814
|
+
/// Removes a previously applied inline style from the current selection. Only ranges the
|
|
3815
|
+
/// consumer coloured are touched — mention/link/code colouring is left intact.
|
|
3816
|
+
func removeInlineStyle(key: String) {
|
|
3817
|
+
guard let (attribute, marker) = Self.inlineStyleAttributes(for: key) else { return }
|
|
3818
|
+
// Same as applyInlineStyle: an explicit clear outranks a deferred restore.
|
|
3819
|
+
deferredTypingAttrs = nil
|
|
3820
|
+
let range = textView.selectedRange
|
|
3821
|
+
|
|
3822
|
+
// No selection: cancel the pending type-ahead style instead of no-oping. The sentinel
|
|
3823
|
+
// goes in for the clear too, so the lifecycle sites see and invalidate the decision.
|
|
3824
|
+
if range.length == 0 {
|
|
3825
|
+
guard attribute == .foregroundColor else {
|
|
3826
|
+
textView.typingAttributes.removeValue(forKey: marker)
|
|
3827
|
+
textView.typingAttributes.removeValue(forKey: attribute)
|
|
3828
|
+
return
|
|
3829
|
+
}
|
|
3830
|
+
pendingExternalForeground = nil
|
|
3831
|
+
pendingExternalForegroundCleared = true
|
|
3832
|
+
pendingStyles.insert(ExternalColorPendingKey)
|
|
3833
|
+
pendingStylesInsertPos = range.location
|
|
3834
|
+
applyPendingStylesToTypingAttributes()
|
|
3835
|
+
emitActiveStyles()
|
|
3836
|
+
return
|
|
3837
|
+
}
|
|
3838
|
+
|
|
3839
|
+
let mutableAttrString = NSMutableAttributedString(attributedString: textView.attributedText)
|
|
3840
|
+
// Collect first, then mutate — removing the attribute being enumerated mid-walk is unsafe.
|
|
3841
|
+
var markedRanges: [NSRange] = []
|
|
3842
|
+
mutableAttrString.enumerateAttribute(marker, in: range, options: []) { value, subRange, _ in
|
|
3843
|
+
if value != nil { markedRanges.append(subRange) }
|
|
3844
|
+
}
|
|
3845
|
+
for subRange in markedRanges {
|
|
3846
|
+
mutableAttrString.removeAttribute(marker, range: subRange)
|
|
3847
|
+
mutableAttrString.removeAttribute(attribute, range: subRange)
|
|
3848
|
+
if attribute == .foregroundColor {
|
|
3849
|
+
mutableAttrString.addAttribute(.foregroundColor, value: UIColor.label, range: subRange)
|
|
3850
|
+
}
|
|
3851
|
+
}
|
|
3852
|
+
commitInlineStyle(mutableAttrString, range: range)
|
|
3853
|
+
}
|
|
3854
|
+
|
|
3855
|
+
/// Current mention ranges in "clean" (ZWS-stripped) coordinates — the same space
|
|
3856
|
+
/// `setMentionRanges` accepts and JS works in. Emitted on every content change so a
|
|
3857
|
+
/// consumer formatter can read back where mentions ended up after edits (§8.2 S4).
|
|
3858
|
+
private func currentMentionRangesJson() -> String {
|
|
3859
|
+
let attributed = NSAttributedString(attributedString: textView.attributedText)
|
|
3860
|
+
guard attributed.length > 0 else { return "[]" }
|
|
3861
|
+
|
|
3862
|
+
let raw = attributed.string as NSString
|
|
3863
|
+
var rawToClean = [Int](repeating: 0, count: raw.length + 1)
|
|
3864
|
+
var clean = 0
|
|
3865
|
+
for i in 0..<raw.length {
|
|
3866
|
+
rawToClean[i] = clean
|
|
3867
|
+
if raw.character(at: i) != 0x200B { clean += 1 }
|
|
3868
|
+
}
|
|
3869
|
+
rawToClean[raw.length] = clean
|
|
3870
|
+
|
|
3871
|
+
var ranges: [[String: Int]] = []
|
|
3872
|
+
attributed.enumerateAttribute(MentionMarkerKey, in: NSRange(location: 0, length: attributed.length), options: []) { value, range, _ in
|
|
3873
|
+
guard value != nil else { return }
|
|
3874
|
+
ranges.append(["start": rawToClean[range.location], "end": rawToClean[range.location + range.length]])
|
|
3875
|
+
}
|
|
3876
|
+
guard !ranges.isEmpty,
|
|
3877
|
+
let data = try? JSONSerialization.data(withJSONObject: ranges, options: []),
|
|
3878
|
+
let json = String(data: data, encoding: .utf8) else { return "[]" }
|
|
3879
|
+
return json
|
|
3880
|
+
}
|
|
3881
|
+
|
|
3486
3882
|
func toggleHighlight(color: String?) {
|
|
3487
3883
|
let range = textView.selectedRange
|
|
3488
3884
|
guard range.length > 0 else { return }
|
|
@@ -4111,6 +4507,24 @@ class RichTextEditorView: UIView, UITextViewDelegate, UIGestureRecognizerDelegat
|
|
|
4111
4507
|
|
|
4112
4508
|
/// Applies pending style toggles to typingAttributes so newly typed text inherits them.
|
|
4113
4509
|
/// Matches Android's pendingStyles mechanism via iOS's native typingAttributes API.
|
|
4510
|
+
/// Paints the inline-code foreground over `range`, but never over a sub-range carrying a
|
|
4511
|
+
/// consumer colour.
|
|
4512
|
+
///
|
|
4513
|
+
/// Inline code owns a foreground of its own, and several passes re-assert it — `toggleCode`,
|
|
4514
|
+
/// `setMentionRanges` (which the JS bridge calls on every content change), and the typed
|
|
4515
|
+
/// ``code`` markdown shortcut. Each one used to repaint the whole range, so a consumer colour
|
|
4516
|
+
/// applied to inline code was overwritten within a frame and the composer showed #6852D6
|
|
4517
|
+
/// instead of the chosen colour — while the wire, serialized from the style ranges rather than
|
|
4518
|
+
/// from `.foregroundColor`, still carried it. The colour therefore only "appeared" after
|
|
4519
|
+
/// sending (ENG-38248). Colour wins; the code background is untouched either way.
|
|
4520
|
+
private func paintInlineCodeForeground(on attr: NSMutableAttributedString, range: NSRange) {
|
|
4521
|
+
attr.enumerateAttribute(ExternalForegroundKey, in: range, options: []) { external, subRange, _ in
|
|
4522
|
+
if external == nil {
|
|
4523
|
+
attr.addAttribute(.foregroundColor, value: inlineCodeTextColor, range: subRange)
|
|
4524
|
+
}
|
|
4525
|
+
}
|
|
4526
|
+
}
|
|
4527
|
+
|
|
4114
4528
|
private func applyPendingStylesToTypingAttributes() {
|
|
4115
4529
|
var attrs = textView.typingAttributes
|
|
4116
4530
|
var baseFont = attrs[.font] as? UIFont ?? UIFont.systemFont(ofSize: 16)
|
|
@@ -4178,6 +4592,19 @@ class RichTextEditorView: UIView, UITextViewDelegate, UIGestureRecognizerDelegat
|
|
|
4178
4592
|
attrs.removeValue(forKey: .strikethroughStyle)
|
|
4179
4593
|
}
|
|
4180
4594
|
|
|
4595
|
+
// Consumer inline colour — must land AFTER the `UIColor.label` / inline-code foreground
|
|
4596
|
+
// writes above so it overrides them, and marker + paint go in together.
|
|
4597
|
+
if let color = pendingExternalForeground {
|
|
4598
|
+
attrs[ExternalForegroundKey] = color
|
|
4599
|
+
attrs[.foregroundColor] = color
|
|
4600
|
+
} else if pendingExternalForegroundCleared {
|
|
4601
|
+
attrs.removeValue(forKey: ExternalForegroundKey)
|
|
4602
|
+
// Inline code / code block keep their own foreground when the colour is cleared
|
|
4603
|
+
if !pendingStyles.contains("code") && !pendingStyles.contains("codeBlock") {
|
|
4604
|
+
attrs[.foregroundColor] = UIColor.label
|
|
4605
|
+
}
|
|
4606
|
+
}
|
|
4607
|
+
|
|
4181
4608
|
textView.typingAttributes = attrs
|
|
4182
4609
|
}
|
|
4183
4610
|
|
|
@@ -4190,6 +4617,9 @@ class RichTextEditorView: UIView, UITextViewDelegate, UIGestureRecognizerDelegat
|
|
|
4190
4617
|
}
|
|
4191
4618
|
|
|
4192
4619
|
private func toggleListStyle(bullet: Bool) {
|
|
4620
|
+
// Arm the active consumer colour before the attributedText resets below wipe
|
|
4621
|
+
// typingAttributes — applyPendingStylesToTypingAttributes() rebuilds from pendingStyles.
|
|
4622
|
+
captureExternalColorToPending()
|
|
4193
4623
|
// Mutual exclusivity: exit code block mode before applying list (matches Android toggleListPrefix)
|
|
4194
4624
|
// Remove code block attributes inline rather than calling toggleCodeBlock() to avoid
|
|
4195
4625
|
// textView.attributedText reassignment mid-flow.
|
|
@@ -4247,6 +4677,11 @@ class RichTextEditorView: UIView, UITextViewDelegate, UIGestureRecognizerDelegat
|
|
|
4247
4677
|
pendingStyles.remove("underline")
|
|
4248
4678
|
pendingStyles.remove("strikethrough")
|
|
4249
4679
|
pendingStyles.remove("code")
|
|
4680
|
+
// This branch converts a code-block line to a plain list line and drops every other
|
|
4681
|
+
// inline style, so the consumer colour goes with them rather than surviving alone.
|
|
4682
|
+
pendingStyles.remove(ExternalColorPendingKey)
|
|
4683
|
+
pendingExternalForeground = nil
|
|
4684
|
+
pendingExternalForegroundCleared = false
|
|
4250
4685
|
explicitlyOffStyles.removeAll()
|
|
4251
4686
|
// Reset typingAttributes to plain so list prefix doesn't inherit code block styling
|
|
4252
4687
|
let plainParaStyle = NSMutableParagraphStyle()
|
|
@@ -4311,7 +4746,7 @@ class RichTextEditorView: UIView, UITextViewDelegate, UIGestureRecognizerDelegat
|
|
|
4311
4746
|
var lineOffsets: [(start: Int, line: String)] = []
|
|
4312
4747
|
for line in lines {
|
|
4313
4748
|
lineOffsets.append((start: lineOffset, line: line))
|
|
4314
|
-
lineOffset += line.
|
|
4749
|
+
lineOffset += (line as NSString).length + 1 // +1 for \n (UTF-16 units, to match NSRange)
|
|
4315
4750
|
}
|
|
4316
4751
|
|
|
4317
4752
|
for (index, entry) in lineOffsets.enumerated().reversed() {
|
|
@@ -4394,6 +4829,7 @@ class RichTextEditorView: UIView, UITextViewDelegate, UIGestureRecognizerDelegat
|
|
|
4394
4829
|
// set textView.attributedText and wipe typingAttributes.
|
|
4395
4830
|
applyPendingStylesToTypingAttributes()
|
|
4396
4831
|
deferredTypingAttrs = textView.typingAttributes
|
|
4832
|
+
syncPendingStyleAnchor()
|
|
4397
4833
|
|
|
4398
4834
|
// Re-check placeholder after all post-processing (applyListIndentation /
|
|
4399
4835
|
// renumberNumberedLists may set textView.attributedText which can cause
|
|
@@ -4499,7 +4935,12 @@ class RichTextEditorView: UIView, UITextViewDelegate, UIGestureRecognizerDelegat
|
|
|
4499
4935
|
guard let start = style["start"] as? Int,
|
|
4500
4936
|
let end = style["end"] as? Int,
|
|
4501
4937
|
let styleType = style["style"] as? String,
|
|
4502
|
-
|
|
4938
|
+
// UTF-16, not graphemes. JS hands these offsets over in UTF-16 units
|
|
4939
|
+
// (that is what String slicing and NSRange both use); `text.count` counts
|
|
4940
|
+
// GRAPHEMES, so one emoji made it one short and the guard silently dropped
|
|
4941
|
+
// every style on the line — the edit composer then opened a coloured/bold
|
|
4942
|
+
// message as plain text. Same bug the serializer had in ENG-38257.
|
|
4943
|
+
start < end && end <= (text as NSString).length else { continue }
|
|
4503
4944
|
|
|
4504
4945
|
let range = NSRange(location: start + prefixLength, length: end - start)
|
|
4505
4946
|
|
|
@@ -4528,6 +4969,14 @@ class RichTextEditorView: UIView, UITextViewDelegate, UIGestureRecognizerDelegat
|
|
|
4528
4969
|
blockAttrString.addAttribute(.underlineStyle, value: NSUnderlineStyle.single.rawValue, range: range)
|
|
4529
4970
|
case "strikethrough":
|
|
4530
4971
|
blockAttrString.addAttribute(.strikethroughStyle, value: NSUnderlineStyle.single.rawValue, range: range)
|
|
4972
|
+
case "textColor":
|
|
4973
|
+
// Edit round-trip: restore a consumer colour from the wire format so the
|
|
4974
|
+
// composer shows the colour rather than raw <color=…> text. Marker and
|
|
4975
|
+
// paint go in together, as everywhere else.
|
|
4976
|
+
if let hex = style["color"] as? String, let color = UIColor.fromHexString(hex) {
|
|
4977
|
+
blockAttrString.addAttribute(ExternalForegroundKey, value: color, range: range)
|
|
4978
|
+
blockAttrString.addAttribute(.foregroundColor, value: color, range: range)
|
|
4979
|
+
}
|
|
4531
4980
|
case "code":
|
|
4532
4981
|
let monoFont = UIFont.monospacedSystemFont(ofSize: 15, weight: .regular)
|
|
4533
4982
|
blockAttrString.addAttribute(.font, value: monoFont, range: range)
|
|
@@ -4543,6 +4992,22 @@ class RichTextEditorView: UIView, UITextViewDelegate, UIGestureRecognizerDelegat
|
|
|
4543
4992
|
break
|
|
4544
4993
|
}
|
|
4545
4994
|
}
|
|
4995
|
+
|
|
4996
|
+
// Order-independent re-assert of the consumer colour.
|
|
4997
|
+
//
|
|
4998
|
+
// The styles above are applied in whatever order the wire listed them, and both
|
|
4999
|
+
// `code` and `link` write a foreground of their own. When a colour run overlapped
|
|
5000
|
+
// one of those and happened to be listed first, the later case painted straight
|
|
5001
|
+
// over it and the edit composer opened showing #6852D6 (or link blue) instead of
|
|
5002
|
+
// the colour the message was sent with (ENG-38248, on the edit round-trip).
|
|
5003
|
+
// ExternalForegroundKey is the marker the serializer reads, so re-deriving the
|
|
5004
|
+
// paint from it makes the outcome independent of style order.
|
|
5005
|
+
let blockRange = NSRange(location: 0, length: blockAttrString.length)
|
|
5006
|
+
blockAttrString.enumerateAttribute(ExternalForegroundKey, in: blockRange, options: []) { value, subRange, _ in
|
|
5007
|
+
if let color = value as? UIColor {
|
|
5008
|
+
blockAttrString.addAttribute(.foregroundColor, value: color, range: subRange)
|
|
5009
|
+
}
|
|
5010
|
+
}
|
|
4546
5011
|
}
|
|
4547
5012
|
|
|
4548
5013
|
if blockIndex < blocks.count - 1 {
|
|
@@ -4582,12 +5047,32 @@ class RichTextEditorView: UIView, UITextViewDelegate, UIGestureRecognizerDelegat
|
|
|
4582
5047
|
pendingStylesInsertPos = endPosition
|
|
4583
5048
|
let cbParaStyle = NSMutableParagraphStyle()
|
|
4584
5049
|
cbParaStyle.lineHeightMultiple = RichTextEditorView.defaultLineHeightMultiple
|
|
5050
|
+
// Deliberately no colour: a code BLOCK owns its styling and destroys the colour
|
|
5051
|
+
// on its line, which is the documented rule on both platforms.
|
|
4585
5052
|
textView.typingAttributes = [
|
|
4586
5053
|
.font: UIFont.monospacedSystemFont(ofSize: 16, weight: .regular),
|
|
4587
5054
|
.foregroundColor: UIColor.label,
|
|
4588
5055
|
.paragraphStyle: cbParaStyle,
|
|
4589
5056
|
CodeBlockAttributeKey: true
|
|
4590
5057
|
]
|
|
5058
|
+
} else if let color = attrText.attribute(
|
|
5059
|
+
ExternalForegroundKey, at: checkPos, effectiveRange: nil
|
|
5060
|
+
) as? UIColor {
|
|
5061
|
+
// Arm the trailing run's consumer colour so text typed after an edit CONTINUES
|
|
5062
|
+
// in it. The caret already inherits the font-level styling from the character
|
|
5063
|
+
// before it, which is why inline code carried across an edit while the colour
|
|
5064
|
+
// did not — the colour is not a typing attribute the editor maintains by
|
|
5065
|
+
// itself, it rides `pendingExternalForeground`, and nothing armed that here.
|
|
5066
|
+
// Result: editing a red inline-code message and typing continued the code and
|
|
5067
|
+
// came out in the default label colour (ENG-38253).
|
|
5068
|
+
//
|
|
5069
|
+
// Marker and paint together, as everywhere else: ExternalForegroundKey is what
|
|
5070
|
+
// the serializer reads back, so without it the colour would show and then not
|
|
5071
|
+
// survive the send.
|
|
5072
|
+
textView.typingAttributes[ExternalForegroundKey] = color
|
|
5073
|
+
textView.typingAttributes[.foregroundColor] = color
|
|
5074
|
+
captureExternalColorToPending()
|
|
5075
|
+
pendingStylesInsertPos = endPosition
|
|
4591
5076
|
}
|
|
4592
5077
|
}
|
|
4593
5078
|
}
|
|
@@ -4608,6 +5093,15 @@ class RichTextEditorView: UIView, UITextViewDelegate, UIGestureRecognizerDelegat
|
|
|
4608
5093
|
return textView.text ?? ""
|
|
4609
5094
|
}
|
|
4610
5095
|
|
|
5096
|
+
/// Serializes the composer into the blocks array the JS side turns into wire markup.
|
|
5097
|
+
///
|
|
5098
|
+
/// EVERY offset here is a UTF-16 offset, because that is what `NSRange` /
|
|
5099
|
+
/// `NSAttributedString` / `NSRegularExpression` use — and what JS string slicing uses on the
|
|
5100
|
+
/// other side of the bridge. Swift's `String.count` is a GRAPHEME count, so the two disagree
|
|
5101
|
+
/// for exactly the characters users reach for most: emoji, flags, skin-tone sequences,
|
|
5102
|
+
/// combining accents. Mixing them made every style range on a line stop one UTF-16 unit short
|
|
5103
|
+
/// per preceding emoji, so the tail of a coloured message serialized as plain text even though
|
|
5104
|
+
/// the composer showed it coloured (ENG-38257). Use `NSString.length`, never `String.count`.
|
|
4611
5105
|
func getBlocksArray() -> [[String: Any]] {
|
|
4612
5106
|
let text = textView.text ?? ""
|
|
4613
5107
|
let attributedText = textView.attributedText ?? NSAttributedString()
|
|
@@ -4628,22 +5122,22 @@ class RichTextEditorView: UIView, UITextViewDelegate, UIGestureRecognizerDelegat
|
|
|
4628
5122
|
if line.hasPrefix("▎ • ") {
|
|
4629
5123
|
blockType = "quoteBullet"
|
|
4630
5124
|
displayText = String(line.dropFirst(4))
|
|
4631
|
-
} else if let qnMatch = quoteNumberedRegex?.firstMatch(in: line, range: NSRange(location: 0, length: line.
|
|
5125
|
+
} else if let qnMatch = quoteNumberedRegex?.firstMatch(in: line, range: NSRange(location: 0, length: (line as NSString).length)) {
|
|
4632
5126
|
blockType = "quoteNumbered"
|
|
4633
|
-
displayText =
|
|
5127
|
+
displayText = (line as NSString).substring(from: qnMatch.range.length)
|
|
4634
5128
|
} else if line.hasPrefix("• ") {
|
|
4635
5129
|
blockType = "bullet"
|
|
4636
5130
|
displayText = String(line.dropFirst(2))
|
|
4637
|
-
} else if let match = numberedRegex?.firstMatch(in: line, range: NSRange(location: 0, length: line.
|
|
5131
|
+
} else if let match = numberedRegex?.firstMatch(in: line, range: NSRange(location: 0, length: (line as NSString).length)) {
|
|
4638
5132
|
blockType = "numbered"
|
|
4639
|
-
displayText =
|
|
5133
|
+
displayText = (line as NSString).substring(from: match.range.length)
|
|
4640
5134
|
} else if line.hasPrefix("▎ ") {
|
|
4641
5135
|
blockType = "quote"
|
|
4642
5136
|
displayText = String(line.dropFirst(2))
|
|
4643
5137
|
}
|
|
4644
5138
|
|
|
4645
5139
|
// Detect code block: check the line range for CodeBlockAttributeKey
|
|
4646
|
-
let lineRange = NSRange(location: currentIndex, length: line.
|
|
5140
|
+
let lineRange = NSRange(location: currentIndex, length: (line as NSString).length)
|
|
4647
5141
|
if lineRange.location + lineRange.length <= attributedText.length && lineRange.length > 0 {
|
|
4648
5142
|
var hasCodeBlockAttr = false
|
|
4649
5143
|
attributedText.enumerateAttribute(CodeBlockAttributeKey, in: lineRange, options: []) { value, _, _ in
|
|
@@ -4667,9 +5161,9 @@ class RichTextEditorView: UIView, UITextViewDelegate, UIGestureRecognizerDelegat
|
|
|
4667
5161
|
displayText = displayText.replacingOccurrences(of: "\u{200B}", with: "")
|
|
4668
5162
|
|
|
4669
5163
|
var styles: [[String: Any]] = []
|
|
4670
|
-
let prefixLength = line.
|
|
5164
|
+
let prefixLength = (line as NSString).length - (displayText as NSString).length
|
|
4671
5165
|
let styleRangeStart = currentIndex + prefixLength
|
|
4672
|
-
let styleRangeLength = displayText.
|
|
5166
|
+
let styleRangeLength = (displayText as NSString).length
|
|
4673
5167
|
let styleLineRange = NSRange(location: styleRangeStart, length: styleRangeLength)
|
|
4674
5168
|
|
|
4675
5169
|
if styleLineRange.location + styleLineRange.length <= attributedText.length {
|
|
@@ -4709,6 +5203,17 @@ class RichTextEditorView: UIView, UITextViewDelegate, UIGestureRecognizerDelegat
|
|
|
4709
5203
|
if attrs[.strikethroughStyle] != nil {
|
|
4710
5204
|
styles.append(["style": "strikethrough", "start": relativeStart, "end": relativeEnd])
|
|
4711
5205
|
}
|
|
5206
|
+
// Consumer inline colour. Offsets are already relative to the block prefix
|
|
5207
|
+
// (`• `/`▎ `/`1. ` and ZWS are stripped by the callers before extraction),
|
|
5208
|
+
// so a colour range can never cover a list/quote glyph.
|
|
5209
|
+
if let color = attrs[ExternalForegroundKey] as? UIColor {
|
|
5210
|
+
styles.append([
|
|
5211
|
+
"style": "textColor",
|
|
5212
|
+
"start": relativeStart,
|
|
5213
|
+
"end": relativeEnd,
|
|
5214
|
+
"color": color.toHexString()
|
|
5215
|
+
])
|
|
5216
|
+
}
|
|
4712
5217
|
}
|
|
4713
5218
|
}
|
|
4714
5219
|
|
|
@@ -4718,7 +5223,7 @@ class RichTextEditorView: UIView, UITextViewDelegate, UIGestureRecognizerDelegat
|
|
|
4718
5223
|
"styles": styles
|
|
4719
5224
|
])
|
|
4720
5225
|
|
|
4721
|
-
currentIndex += line.
|
|
5226
|
+
currentIndex += (line as NSString).length + 1 // +1 for the "\n" the split removed
|
|
4722
5227
|
}
|
|
4723
5228
|
|
|
4724
5229
|
return blocks
|
|
@@ -4733,6 +5238,7 @@ class RichTextEditorView: UIView, UITextViewDelegate, UIGestureRecognizerDelegat
|
|
|
4733
5238
|
pendingStyles.removeAll()
|
|
4734
5239
|
explicitlyOffStyles.removeAll()
|
|
4735
5240
|
pendingStylesInsertPos = -1
|
|
5241
|
+
lastKnownTextLength = 0
|
|
4736
5242
|
// Reset typingAttributes to plain so next typed text has no formatting
|
|
4737
5243
|
let paragraphStyle = NSMutableParagraphStyle()
|
|
4738
5244
|
paragraphStyle.lineHeightMultiple = RichTextEditorView.defaultLineHeightMultiple
|
|
@@ -4797,6 +5303,38 @@ class RichTextEditorView: UIView, UITextViewDelegate, UIGestureRecognizerDelegat
|
|
|
4797
5303
|
emitActiveStyles()
|
|
4798
5304
|
}
|
|
4799
5305
|
|
|
5306
|
+
/// Replaces the current selection with plain text, or inserts at the caret when nothing is
|
|
5307
|
+
/// selected — the editor-side half of a consumer's toolbar button.
|
|
5308
|
+
///
|
|
5309
|
+
/// The run goes in with the editor's current typing attributes rather than the attributes of
|
|
5310
|
+
/// whatever it replaced, so a consumer's own wire token lands as plain characters instead of
|
|
5311
|
+
/// inheriting, say, the bold of the word it wrapped.
|
|
5312
|
+
func replaceSelection(with text: String) {
|
|
5313
|
+
let range = textView.selectedRange
|
|
5314
|
+
let mutableAttrString = NSMutableAttributedString(attributedString: textView.attributedText)
|
|
5315
|
+
let insertion = NSAttributedString(string: text, attributes: textView.typingAttributes)
|
|
5316
|
+
|
|
5317
|
+
isInternalChange = true
|
|
5318
|
+
if range.length > 0 {
|
|
5319
|
+
mutableAttrString.replaceCharacters(in: range, with: insertion)
|
|
5320
|
+
} else {
|
|
5321
|
+
mutableAttrString.insert(insertion, at: range.location)
|
|
5322
|
+
}
|
|
5323
|
+
textView.attributedText = mutableAttrString
|
|
5324
|
+
|
|
5325
|
+
// Caret after the inserted run. NSString length, not `text.count`: an emoji is one
|
|
5326
|
+
// Character but two UTF-16 units, and the text view counts in UTF-16.
|
|
5327
|
+
let caret = range.location + (text as NSString).length
|
|
5328
|
+
textView.selectedRange = NSRange(location: caret, length: 0)
|
|
5329
|
+
|
|
5330
|
+
placeholderLabel.isHidden = !textView.text.isEmpty
|
|
5331
|
+
isInternalChange = false
|
|
5332
|
+
saveToUndoStack()
|
|
5333
|
+
sendContentChange()
|
|
5334
|
+
updateToolbarButtonStates()
|
|
5335
|
+
emitActiveStyles()
|
|
5336
|
+
}
|
|
5337
|
+
|
|
4800
5338
|
func undo() {
|
|
4801
5339
|
guard undoStack.count > 1 else { return }
|
|
4802
5340
|
|
|
@@ -4897,6 +5435,11 @@ class RichTextEditorView: UIView, UITextViewDelegate, UIGestureRecognizerDelegat
|
|
|
4897
5435
|
]
|
|
4898
5436
|
// Track code block as a pending style so it survives textDidChange clearing
|
|
4899
5437
|
pendingStyles.insert("codeBlock")
|
|
5438
|
+
// Drop an armed consumer colour, or the first character typed into the empty block
|
|
5439
|
+
// would come out coloured instead of the code block default.
|
|
5440
|
+
pendingExternalForeground = nil
|
|
5441
|
+
pendingExternalForegroundCleared = false
|
|
5442
|
+
pendingStyles.remove(ExternalColorPendingKey)
|
|
4900
5443
|
pendingStylesInsertPos = textView.selectedRange.location
|
|
4901
5444
|
isInternalChange = false
|
|
4902
5445
|
textView.setNeedsDisplay()
|
|
@@ -4955,6 +5498,17 @@ class RichTextEditorView: UIView, UITextViewDelegate, UIGestureRecognizerDelegat
|
|
|
4955
5498
|
mutableAttr.addAttribute(CodeBlockAttributeKey, value: true, range: lineRange)
|
|
4956
5499
|
// Strip mention markers so setMentionRanges() won't re-apply mention styling
|
|
4957
5500
|
mutableAttr.removeAttribute(MentionMarkerKey, range: lineRange)
|
|
5501
|
+
// Same reason, for consumer colour: the blanket .foregroundColor reset above only
|
|
5502
|
+
// changes what is drawn. Leaving the marker behind lets the re-apply passes that run
|
|
5503
|
+
// after any mention re-styling paint the colour straight back over the block, so a
|
|
5504
|
+
// code block would keep whatever colour the text had before it became code.
|
|
5505
|
+
mutableAttr.removeAttribute(ExternalForegroundKey, range: lineRange)
|
|
5506
|
+
mutableAttr.removeAttribute(ExternalBackgroundKey, range: lineRange)
|
|
5507
|
+
// A colour armed for type-ahead would otherwise colour the next character typed
|
|
5508
|
+
// inside the block.
|
|
5509
|
+
pendingExternalForeground = nil
|
|
5510
|
+
pendingExternalForegroundCleared = false
|
|
5511
|
+
pendingStyles.remove(ExternalColorPendingKey)
|
|
4958
5512
|
// Set pendingStyles + typingAttributes so newly typed characters
|
|
4959
5513
|
// inherit code block mode (matches empty-line activation path)
|
|
4960
5514
|
pendingStyles.insert("codeBlock")
|
|
@@ -5175,7 +5729,9 @@ class RichTextEditorView: UIView, UITextViewDelegate, UIGestureRecognizerDelegat
|
|
|
5175
5729
|
bgColor.getRed(&r, green: &g, blue: &b, alpha: &a)
|
|
5176
5730
|
// Mention bg: rgba(104/255, 82/255, 214/255, 0.15) — low alpha purple
|
|
5177
5731
|
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
|
-
|
|
5732
|
+
// A consumer-applied background that happens to look like the mention purple is not one.
|
|
5733
|
+
let isExternal = mutableAttr.attribute(ExternalBackgroundKey, at: range.location, effectiveRange: nil) != nil
|
|
5734
|
+
if isMentionBg && !isExternal {
|
|
5179
5735
|
mutableAttr.removeAttribute(.backgroundColor, range: range)
|
|
5180
5736
|
}
|
|
5181
5737
|
}
|
|
@@ -5194,6 +5750,14 @@ class RichTextEditorView: UIView, UITextViewDelegate, UIGestureRecognizerDelegat
|
|
|
5194
5750
|
}
|
|
5195
5751
|
}
|
|
5196
5752
|
|
|
5753
|
+
// Re-apply consumer-applied foreground color (applyInlineStyle) after the blanket reset,
|
|
5754
|
+
// so a colour set by a trailing-button formatter is not clobbered by mention re-styling.
|
|
5755
|
+
mutableAttr.enumerateAttribute(ExternalForegroundKey, in: fullRange, options: []) { value, range, _ in
|
|
5756
|
+
if let color = value as? UIColor {
|
|
5757
|
+
mutableAttr.addAttribute(.foregroundColor, value: color, range: range)
|
|
5758
|
+
}
|
|
5759
|
+
}
|
|
5760
|
+
|
|
5197
5761
|
// Re-apply link foreground color (systemBlue) to ranges with LinkURLAttributeKey
|
|
5198
5762
|
// so that links remain visually blue after the blanket reset above.
|
|
5199
5763
|
mutableAttr.enumerateAttribute(LinkURLAttributeKey, in: fullRange, options: []) { value, range, _ in
|
|
@@ -5202,10 +5766,11 @@ class RichTextEditorView: UIView, UITextViewDelegate, UIGestureRecognizerDelegat
|
|
|
5202
5766
|
}
|
|
5203
5767
|
}
|
|
5204
5768
|
|
|
5205
|
-
// Re-apply inline code text color to ranges with inline code background
|
|
5769
|
+
// Re-apply inline code text color to ranges with inline code background, except where a
|
|
5770
|
+
// consumer colour is set — see paintInlineCodeForeground (ENG-38248).
|
|
5206
5771
|
mutableAttr.enumerateAttribute(.backgroundColor, in: fullRange, options: []) { value, range, _ in
|
|
5207
5772
|
if let bgColor = value as? UIColor, bgColor == inlineCodeBgColor {
|
|
5208
|
-
|
|
5773
|
+
self.paintInlineCodeForeground(on: mutableAttr, range: range)
|
|
5209
5774
|
}
|
|
5210
5775
|
}
|
|
5211
5776
|
|
|
@@ -5286,8 +5851,10 @@ class RichTextEditorView: UIView, UITextViewDelegate, UIGestureRecognizerDelegat
|
|
|
5286
5851
|
cleanAttrs.removeValue(forKey: .backgroundColor)
|
|
5287
5852
|
}
|
|
5288
5853
|
}
|
|
5289
|
-
// Remove mention foreground color — use component comparison
|
|
5290
|
-
|
|
5854
|
+
// Remove mention foreground color — use component comparison. Skipped when a consumer
|
|
5855
|
+
// inline colour is armed, so a swatch near mention purple isn't silently reverted.
|
|
5856
|
+
if cleanAttrs[ExternalForegroundKey] == nil,
|
|
5857
|
+
let fg = cleanAttrs[.foregroundColor] as? UIColor {
|
|
5291
5858
|
var r: CGFloat = 0, g: CGFloat = 0, b: CGFloat = 0, a: CGFloat = 0
|
|
5292
5859
|
fg.getRed(&r, green: &g, blue: &b, alpha: &a)
|
|
5293
5860
|
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 +6063,7 @@ class RichTextEditorView: UIView, UITextViewDelegate, UIGestureRecognizerDelegat
|
|
|
5496
6063
|
case "code":
|
|
5497
6064
|
let monoFont = UIFont.monospacedSystemFont(ofSize: 15, weight: .regular)
|
|
5498
6065
|
mutableAttr.addAttribute(.font, value: monoFont, range: fmtRange)
|
|
5499
|
-
|
|
6066
|
+
paintInlineCodeForeground(on: mutableAttr, range: fmtRange)
|
|
5500
6067
|
mutableAttr.addAttribute(.backgroundColor, value: inlineCodeBgColor, range: fmtRange)
|
|
5501
6068
|
case "codeBlock":
|
|
5502
6069
|
let monoFont = UIFont.monospacedSystemFont(ofSize: 16, weight: .regular)
|
|
@@ -5555,6 +6122,8 @@ class RichTextEditorView: UIView, UITextViewDelegate, UIGestureRecognizerDelegat
|
|
|
5555
6122
|
/// Returns true if the string contains markdown syntax that should be parsed into rich text.
|
|
5556
6123
|
/// Matches Android looksLikeMarkdown().
|
|
5557
6124
|
func looksLikeMarkdown(_ str: String) -> Bool {
|
|
6125
|
+
// Rich-text wire format — only ever reaches here from our own private pasteboard type.
|
|
6126
|
+
if str.lowercased().contains("<color=") { return true }
|
|
5558
6127
|
if str.contains("[") && str.contains("](") { return true }
|
|
5559
6128
|
if str.contains("**") { return true }
|
|
5560
6129
|
if str.contains("~~") { return true }
|
|
@@ -5579,6 +6148,7 @@ class RichTextEditorView: UIView, UITextViewDelegate, UIGestureRecognizerDelegat
|
|
|
5579
6148
|
var isUnderline = false
|
|
5580
6149
|
var isStrikethrough = false
|
|
5581
6150
|
var isCode = false
|
|
6151
|
+
var activePasteColor: UIColor? = nil
|
|
5582
6152
|
|
|
5583
6153
|
let defaultFont = UIFont.systemFont(ofSize: 16)
|
|
5584
6154
|
let monoFont = UIFont.monospacedSystemFont(ofSize: 15, weight: .regular)
|
|
@@ -5613,11 +6183,46 @@ class RichTextEditorView: UIView, UITextViewDelegate, UIGestureRecognizerDelegat
|
|
|
5613
6183
|
}
|
|
5614
6184
|
}
|
|
5615
6185
|
|
|
6186
|
+
// Consumer colour wins over BOTH branches' foreground, applied after them rather than
|
|
6187
|
+
// inside the `else`. Inline code paints `inlineCodeTextColor`, so while this lived in the
|
|
6188
|
+
// non-code branch a pasted run that was ALSO code came out uniformly #6852D6 and the
|
|
6189
|
+
// colour was silently dropped — copying a coloured message and pasting it back lost its
|
|
6190
|
+
// colour (ENG-38253). `setContent` already re-asserts the colour over inline code for the
|
|
6191
|
+
// edit path (ENG-38248); the paste path has to agree with it, or the same message looks
|
|
6192
|
+
// different depending on how it reached the composer.
|
|
6193
|
+
// Marker and paint go in together, as everywhere else: ExternalForegroundKey is what the
|
|
6194
|
+
// serializer reads back, so without it the colour would show and then not survive a send.
|
|
6195
|
+
if let color = activePasteColor {
|
|
6196
|
+
attrs[ExternalForegroundKey] = color
|
|
6197
|
+
attrs[.foregroundColor] = color
|
|
6198
|
+
}
|
|
6199
|
+
|
|
5616
6200
|
result.append(NSAttributedString(string: currentText, attributes: attrs))
|
|
5617
6201
|
currentText = ""
|
|
5618
6202
|
}
|
|
5619
6203
|
|
|
5620
6204
|
while i < len {
|
|
6205
|
+
// <color=#rrggbb> … </color> — same grammar the formatter renders.
|
|
6206
|
+
if chars[i] == "<" {
|
|
6207
|
+
let rest = String(chars[i...])
|
|
6208
|
+
if rest.lowercased().hasPrefix("</color>") {
|
|
6209
|
+
flush()
|
|
6210
|
+
activePasteColor = nil
|
|
6211
|
+
i += 8
|
|
6212
|
+
continue
|
|
6213
|
+
}
|
|
6214
|
+
if rest.lowercased().hasPrefix("<color="),
|
|
6215
|
+
let close = rest.firstIndex(of: ">") {
|
|
6216
|
+
let hex = String(rest[rest.index(rest.startIndex, offsetBy: 7)..<close])
|
|
6217
|
+
.trimmingCharacters(in: .whitespaces)
|
|
6218
|
+
if let color = UIColor.fromHexString(hex) {
|
|
6219
|
+
flush()
|
|
6220
|
+
activePasteColor = color
|
|
6221
|
+
i += rest.distance(from: rest.startIndex, to: close) + 1
|
|
6222
|
+
continue
|
|
6223
|
+
}
|
|
6224
|
+
}
|
|
6225
|
+
}
|
|
5621
6226
|
if chars[i] == "[" {
|
|
5622
6227
|
if let (linkText, linkURL, fullEnd) = parseLinkAt(chars, from: i) {
|
|
5623
6228
|
flush()
|
|
@@ -5847,7 +6452,7 @@ class RichTextEditorView: UIView, UITextViewDelegate, UIGestureRecognizerDelegat
|
|
|
5847
6452
|
|
|
5848
6453
|
isInternalChange = true
|
|
5849
6454
|
textView.attributedText = mutableAttr
|
|
5850
|
-
let newCursorPos = range.location + displayText.
|
|
6455
|
+
let newCursorPos = range.location + (displayText as NSString).length
|
|
5851
6456
|
textView.selectedRange = NSRange(location: newCursorPos, length: 0)
|
|
5852
6457
|
|
|
5853
6458
|
let paragraphStyle = NSMutableParagraphStyle()
|