Skip to content

Commit 178e7e6

Browse files
PR Batch Testerclaude
andcommitted
fix(mobile): skip the tap caret restore when the draft changed during the touch
The caret saved at touch-down is put back after the tap only if the draft is still the same and no text is being composed, so a transcript that lands mid-tap keeps its caret. The saved caret is in source offsets, so chips rebuilt in between still map correctly. The native-insertion check now types past the visible lines. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 23481f0 commit 178e7e6

2 files changed

Lines changed: 16 additions & 11 deletions

File tree

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

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,7 @@ public final class T3ComposerEditorView: ExpoView, UITextViewDelegate, UITextDro
451451
private var hasDeferredForcedRebuild = false
452452
private var pendingContentOffset: CGPoint?
453453
private var needsSelectionReveal = false
454-
private var selectionBeforeFocusingTap: NSRange?
454+
private var selectionBeforeFocusingTap: (selection: ComposerSelectionPayload, value: String)?
455455

456456
let onComposerChange = EventDispatcher()
457457
let onComposerSelectionChange = EventDispatcher()
@@ -523,11 +523,11 @@ 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 selection = selectionBeforeFocusingTap {
526+
if let snapshot = selectionBeforeFocusingTap {
527527
selectionBeforeFocusingTap = nil
528528
// UIKit places the caret from its own tap recognizer during this same touch.
529529
DispatchQueue.main.async { [weak self] in
530-
self?.restoreSelection(selection)
530+
self?.restoreSelection(snapshot.selection, ifValueIs: snapshot.value)
531531
}
532532
}
533533
return
@@ -571,7 +571,7 @@ public final class T3ComposerEditorView: ExpoView, UITextViewDelegate, UITextDro
571571
// The collapsed editor only shows the draft's first line, so a tap that expands an
572572
// overflowing draft returns to the caret it was left at instead of moving it there.
573573
let collapsedOverflow = !textView.isFirstResponder && !textView.isScrollEnabled && draftOverflowsBounds()
574-
selectionBeforeFocusingTap = collapsedOverflow ? textView.selectedRange : nil
574+
selectionBeforeFocusingTap = collapsedOverflow ? (sourceSelection(), value) : nil
575575
return true
576576
}
577577

@@ -1174,13 +1174,16 @@ public final class T3ComposerEditorView: ExpoView, UITextViewDelegate, UITextDro
11741174
revealSelectionAfterLayout()
11751175
}
11761176

1177-
private func restoreSelection(_ selection: NSRange) {
1178-
guard textView.isFirstResponder,
1179-
!NSEqualRanges(selection, textView.selectedRange),
1180-
NSMaxRange(selection) <= textView.attributedText.length else {
1177+
private func restoreSelection(_ selection: ComposerSelectionPayload, ifValueIs expectedValue: String) {
1178+
// 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 {
11811180
return
11821181
}
1183-
textView.selectedRange = selection
1182+
let range = displayRange(for: selection)
1183+
guard !NSEqualRanges(range, textView.selectedRange) else {
1184+
return
1185+
}
1186+
textView.selectedRange = range
11841187
revealSelectionAfterLayout()
11851188
}
11861189

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,11 @@ for resizeBeforeProp in [false, true] {
100100
composer.layoutIfNeeded()
101101
check(caretIsVisible(editor), "Reveal a caret moved to the end")
102102

103-
editor.insertText(" typed text")
103+
let typedLines = String(repeating: "\nTyped line", count: 12)
104+
editor.insertText(typedLines)
104105
composer.layoutIfNeeded()
105-
check(caretIsVisible(editor), "Reveal caret after native insertion")
106+
check(editor.text.hasSuffix(typedLines), "Insert typed text at the caret")
107+
check(caretIsVisible(editor), "Reveal caret after native insertion past the visible lines")
106108

107109
collapse()
108110
document(composer, longerDraft + String(repeating: "\nMore dictated text", count: 10))

0 commit comments

Comments
 (0)