Skip to content

Commit 23481f0

Browse files
PR Batch Testervdoshi96claude
committed
fix(mobile): tapping the collapsed iOS composer returns to your place in the draft
The collapsed composer shows only the draft's first line, so the tap that reopens it put the caret on that line and the editor opened at the top of a long draft. That tap now keeps the caret where the draft was left. Drafts that fit the collapsed row still place the caret where you tap. Co-authored-by: Vishal Doshi <[email protected]> Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 2eda49e commit 23481f0

1 file changed

Lines changed: 28 additions & 1 deletion

File tree

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

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -451,6 +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?
454455

455456
let onComposerChange = EventDispatcher()
456457
let onComposerSelectionChange = EventDispatcher()
@@ -522,6 +523,13 @@ public final class T3ComposerEditorView: ExpoView, UITextViewDelegate, UITextDro
522523
@objc private func openContext(_ recognizer: UITapGestureRecognizer) {
523524
guard let (index, attachment) = contextAttachment(at: recognizer.location(in: textView)) else {
524525
textView.becomeFirstResponder()
526+
if let selection = selectionBeforeFocusingTap {
527+
selectionBeforeFocusingTap = nil
528+
// UIKit places the caret from its own tap recognizer during this same touch.
529+
DispatchQueue.main.async { [weak self] in
530+
self?.restoreSelection(selection)
531+
}
532+
}
525533
return
526534
}
527535
openContext(index: index, attachment: attachment)
@@ -560,7 +568,11 @@ public final class T3ComposerEditorView: ExpoView, UITextViewDelegate, UITextDro
560568
}
561569

562570
public func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
563-
true
571+
// The collapsed editor only shows the draft's first line, so a tap that expands an
572+
// overflowing draft returns to the caret it was left at instead of moving it there.
573+
let collapsedOverflow = !textView.isFirstResponder && !textView.isScrollEnabled && draftOverflowsBounds()
574+
selectionBeforeFocusingTap = collapsedOverflow ? textView.selectedRange : nil
575+
return true
564576
}
565577

566578
public func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
@@ -1162,6 +1174,21 @@ public final class T3ComposerEditorView: ExpoView, UITextViewDelegate, UITextDro
11621174
revealSelectionAfterLayout()
11631175
}
11641176

1177+
private func restoreSelection(_ selection: NSRange) {
1178+
guard textView.isFirstResponder,
1179+
!NSEqualRanges(selection, textView.selectedRange),
1180+
NSMaxRange(selection) <= textView.attributedText.length else {
1181+
return
1182+
}
1183+
textView.selectedRange = selection
1184+
revealSelectionAfterLayout()
1185+
}
1186+
1187+
private func draftOverflowsBounds() -> Bool {
1188+
let size = textView.sizeThatFits(CGSize(width: textView.bounds.width, height: .greatestFiniteMagnitude))
1189+
return size.height > textView.bounds.height + 1
1190+
}
1191+
11651192
private func revealSelectionAfterLayout() {
11661193
needsSelectionReveal = true
11671194
setNeedsLayout()

0 commit comments

Comments
 (0)