Skip to content

Commit 3e1a7d3

Browse files
PR Batch Testerclaude
andcommitted
fix(mobile): a selection set from JS during the tap cancels its caret restore
The tap restore waits for the next main-queue turn, and a controlled document or setSelection that moves the caret in that window keeps the draft text the same, so it slipped past the draft check. Those calls now cancel the pending restore when they move the selection. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 178e7e6 commit 3e1a7d3

1 file changed

Lines changed: 20 additions & 7 deletions

File tree

‎apps/mobile/modules/t3-composer-editor/ios/T3ComposerEditorView.swift‎

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ private struct ComposerTokenPayload: Decodable {
1313
let end: Int
1414
}
1515

16-
private struct ComposerSelectionPayload: Decodable {
16+
private struct ComposerSelectionPayload: Decodable, Equatable {
1717
let start: Int
1818
let end: Int
1919
}
@@ -523,11 +523,10 @@ public final class T3ComposerEditorView: ExpoView, UITextViewDelegate, UITextDro
523523
@objc private func openContext(_ recognizer: UITapGestureRecognizer) {
524524
guard let (index, attachment) = contextAttachment(at: recognizer.location(in: textView)) else {
525525
textView.becomeFirstResponder()
526-
if let snapshot = selectionBeforeFocusingTap {
527-
selectionBeforeFocusingTap = nil
526+
if selectionBeforeFocusingTap != nil {
528527
// UIKit places the caret from its own tap recognizer during this same touch.
529528
DispatchQueue.main.async { [weak self] in
530-
self?.restoreSelection(snapshot.selection, ifValueIs: snapshot.value)
529+
self?.restoreSelectionBeforeFocusingTap()
531530
}
532531
}
533532
return
@@ -662,10 +661,12 @@ public final class T3ComposerEditorView: ExpoView, UITextViewDelegate, UITextDro
662661
tokens = decode([ComposerTokenPayload].self, from: document.tokensJson) ?? []
663662
tokensNeedRebuild = true
664663
}
664+
let previousSelection = sourceSelection()
665665
value = document.value
666666
requestedSelection = document.selection
667667
applyControlledDocument(force: tokensNeedRebuild)
668668
applyRequestedSelection()
669+
cancelFocusingTapRestoreIfSelectionMoved(from: previousSelection)
669670
if tokensMatchCurrentValue() {
670671
tokensNeedRebuild = false
671672
}
@@ -777,8 +778,10 @@ public final class T3ComposerEditorView: ExpoView, UITextViewDelegate, UITextDro
777778
}
778779

779780
func setSelection(start: Int, end: Int) {
781+
let previousSelection = sourceSelection()
780782
requestedSelection = ComposerSelectionPayload(start: start, end: end)
781783
applyRequestedSelection()
784+
cancelFocusingTapRestoreIfSelectionMoved(from: previousSelection)
782785
}
783786

784787
public func textViewDidChange(_ textView: UITextView) {
@@ -1174,19 +1177,29 @@ public final class T3ComposerEditorView: ExpoView, UITextViewDelegate, UITextDro
11741177
revealSelectionAfterLayout()
11751178
}
11761179

1177-
private func restoreSelection(_ selection: ComposerSelectionPayload, ifValueIs expectedValue: String) {
1180+
private func restoreSelectionBeforeFocusingTap() {
1181+
guard let snapshot = selectionBeforeFocusingTap else {
1182+
return
1183+
}
1184+
selectionBeforeFocusingTap = nil
11781185
// A draft that changed during the touch, such as a transcript landing, owns the caret now.
1179-
guard textView.isFirstResponder, textView.markedTextRange == nil, value == expectedValue else {
1186+
guard textView.isFirstResponder, textView.markedTextRange == nil, value == snapshot.value else {
11801187
return
11811188
}
1182-
let range = displayRange(for: selection)
1189+
let range = displayRange(for: snapshot.selection)
11831190
guard !NSEqualRanges(range, textView.selectedRange) else {
11841191
return
11851192
}
11861193
textView.selectedRange = range
11871194
revealSelectionAfterLayout()
11881195
}
11891196

1197+
private func cancelFocusingTapRestoreIfSelectionMoved(from previousSelection: ComposerSelectionPayload) {
1198+
if sourceSelection() != previousSelection {
1199+
selectionBeforeFocusingTap = nil
1200+
}
1201+
}
1202+
11901203
private func draftOverflowsBounds() -> Bool {
11911204
let size = textView.sizeThatFits(CGSize(width: textView.bounds.width, height: .greatestFiniteMagnitude))
11921205
return size.height > textView.bounds.height + 1

0 commit comments

Comments
 (0)