TypeScript
fix(mobile): the iOS composer reopens where you left a long draft (#23096)
shivamefa538124667
diff --git a/apps/mobile/modules/t3-composer-editor/ios/T3ComposerEditorView.swift b/apps/mobile/modules/t3-composer-editor/ios/T3ComposerEditorView.swiftindex 9a5dae1db3f..24f653fb7d4 100644--- a/apps/mobile/modules/t3-composer-editor/ios/T3ComposerEditorView.swift+++ b/apps/mobile/modules/t3-composer-editor/ios/T3ComposerEditorView.swift@@ -13,7 +13,7 @@ private struct ComposerTokenPayload: Decodable {let end: Int}-private struct ComposerSelectionPayload: Decodable {+private struct ComposerSelectionPayload: Decodable, Equatable {let start: Intlet end: Int}@@ -449,6 +449,9 @@ public final class T3ComposerEditorView: ExpoView, UITextViewDelegate, UITextDroprivate var tokensNeedRebuild = falseprivate var chipsNeedMeasuredWidth = falseprivate var hasDeferredForcedRebuild = false+ private var pendingContentOffset: CGPoint?+ private var needsSelectionReveal = false+ private var selectionBeforeFocusingTap: (selection: ComposerSelectionPayload, value: String)?let onComposerChange = EventDispatcher()let onComposerSelectionChange = EventDispatcher()@@ -520,6 +523,12 @@ public final class T3ComposerEditorView: ExpoView, UITextViewDelegate, UITextDro@objc private func openContext(_ recognizer: UITapGestureRecognizer) {guard let (index, attachment) = contextAttachment(at: recognizer.location(in: textView)) else {textView.becomeFirstResponder()+ if selectionBeforeFocusingTap != nil {+ // UIKit places the caret from its own tap recognizer during this same touch.+ DispatchQueue.main.async { [weak self] in+ self?.restoreSelectionBeforeFocusingTap()+ }+ }return}openContext(index: index, attachment: attachment)@@ -558,7 +567,11 @@ public final class T3ComposerEditorView: ExpoView, UITextViewDelegate, UITextDro}public func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {- true+ // The collapsed editor only shows the draft's first line, so a tap that expands an+ // overflowing draft returns to the caret it was left at instead of moving it there.+ let collapsedOverflow = !textView.isFirstResponder && !textView.isScrollEnabled && draftOverflowsBounds()+ selectionBeforeFocusingTap = collapsedOverflow ? (sourceSelection(), value) : nil+ return true}public func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {@@ -583,11 +596,27 @@ public final class T3ComposerEditorView: ExpoView, UITextViewDelegate, UITextDropublic override func layoutSubviews() {super.layoutSubviews()+ let previousSize = textView.bounds.sizetextView.frame = boundsif chipsNeedMeasuredWidth, bounds.width > 0 {chipsNeedMeasuredWidth = falseapplyControlledDocument(force: true)}+ if textView.isScrollEnabled, bounds.width > 0, bounds.height > 0 {+ textView.layoutIfNeeded()+ if let offset = pendingContentOffset {+ let minY = -textView.adjustedContentInset.top+ let maxY = max(minY, textView.contentSize.height - bounds.height + textView.adjustedContentInset.bottom)+ textView.setContentOffset(CGPoint(x: offset.x, y: min(maxY, max(minY, offset.y))), animated: false)+ pendingContentOffset = nil+ }+ if needsSelectionReveal || (previousSize != bounds.size && textView.isFirstResponder) {+ needsSelectionReveal = false+ if let selection = textView.selectedTextRange {+ textView.scrollRectToVisible(textView.caretRect(for: selection.end), animated: false)+ }+ }+ }let placeholderX = textView.textContainerInset.left + textView.textContainer.lineFragmentPaddinglet placeholderY = textView.textContainerInset.toplet placeholderWidth = max(@@ -632,10 +661,12 @@ public final class T3ComposerEditorView: ExpoView, UITextViewDelegate, UITextDrotokens = decode([ComposerTokenPayload].self, from: document.tokensJson) ?? []tokensNeedRebuild = true}+ let previousSelection = sourceSelection()value = document.valuerequestedSelection = document.selectionapplyControlledDocument(force: tokensNeedRebuild)applyRequestedSelection()+ cancelFocusingTapRestoreIfSelectionMoved(from: previousSelection)if tokensMatchCurrentValue() {tokensNeedRebuild = false}@@ -694,7 +725,16 @@ public final class T3ComposerEditorView: ExpoView, UITextViewDelegate, UITextDro}func setScrollEnabled(_ scrollEnabled: Bool) {+ guard textView.isScrollEnabled != scrollEnabled else { return }+ if !scrollEnabled {+ // Disabling scrolling discards UITextView's offset. Restore it after+ // expansion has laid out the text, regardless of prop update order.+ pendingContentOffset = pendingContentOffset ?? textView.contentOffset+ }textView.isScrollEnabled = scrollEnabled+ if scrollEnabled {+ revealSelectionAfterLayout()+ }}func setAutoFocus(_ autoFocus: Bool) {@@ -738,8 +778,10 @@ public final class T3ComposerEditorView: ExpoView, UITextViewDelegate, UITextDro}func setSelection(start: Int, end: Int) {+ let previousSelection = sourceSelection()requestedSelection = ComposerSelectionPayload(start: start, end: end)applyRequestedSelection()+ cancelFocusingTapRestoreIfSelectionMoved(from: previousSelection)}public func textViewDidChange(_ textView: UITextView) {@@ -811,6 +853,7 @@ public final class T3ComposerEditorView: ExpoView, UITextViewDelegate, UITextDro}public func textViewDidBeginEditing(_ textView: UITextView) {+ revealSelectionAfterLayout()onComposerFocus()}@@ -836,6 +879,9 @@ public final class T3ComposerEditorView: ExpoView, UITextViewDelegate, UITextDrohasDeferredForcedRebuild = falselet previousSelection = sourceSelection()+ if textView.isScrollEnabled {+ pendingContentOffset = pendingContentOffset ?? textView.contentOffset+ }isApplyingControlledValue = truetextView.attributedText = makeAttributedDocument()let targetSelection = requestedSelection ?? previousSelection@@ -843,6 +889,11 @@ public final class T3ComposerEditorView: ExpoView, UITextViewDelegate, UITextDrotextView.selectedRange = displayRange(for: targetSelection)restoreBaseTypingAttributes()isApplyingControlledValue = false+ if currentSource != value || previousSelection.start != targetSelection.start || previousSelection.end != targetSelection.end {+ revealSelectionAfterLayout()+ } else {+ setNeedsLayout()+ }updatePlaceholderVisibility()emitContentSizeIfNeeded()}@@ -1053,6 +1104,7 @@ public final class T3ComposerEditorView: ExpoView, UITextViewDelegate, UITextDroreturn}value = textView.serializedText()+ revealSelectionAfterLayout()let selection = sourceSelection()nativeEventCount += 1onComposerChange([@@ -1122,6 +1174,40 @@ public final class T3ComposerEditorView: ExpoView, UITextViewDelegate, UITextDroisApplyingControlledValue = truetextView.selectedRange = nextRangeisApplyingControlledValue = false+ revealSelectionAfterLayout()+ }++ private func restoreSelectionBeforeFocusingTap() {+ guard let snapshot = selectionBeforeFocusingTap else {+ return+ }+ selectionBeforeFocusingTap = nil+ // A draft that changed during the touch, such as a transcript landing, owns the caret now.+ guard textView.isFirstResponder, textView.markedTextRange == nil, value == snapshot.value else {+ return+ }+ let range = displayRange(for: snapshot.selection)+ guard !NSEqualRanges(range, textView.selectedRange) else {+ return+ }+ textView.selectedRange = range+ revealSelectionAfterLayout()+ }++ private func cancelFocusingTapRestoreIfSelectionMoved(from previousSelection: ComposerSelectionPayload) {+ if sourceSelection() != previousSelection {+ selectionBeforeFocusingTap = nil+ }+ }++ private func draftOverflowsBounds() -> Bool {+ let size = textView.sizeThatFits(CGSize(width: textView.bounds.width, height: .greatestFiniteMagnitude))+ return size.height > textView.bounds.height + 1+ }++ private func revealSelectionAfterLayout() {+ needsSelectionReveal = true+ setNeedsLayout()}private func updatePlaceholderVisibility() {