@@ -13,7 +13,7 @@ private struct ComposerTokenPayload: Decodable { |
13 | 13 | let end: Int |
14 | 14 | } |
15 | 15 |
|
16 | | -private struct ComposerSelectionPayload: Decodable { |
| 16 | +private struct ComposerSelectionPayload: Decodable, Equatable { |
17 | 17 | let start: Int |
18 | 18 | let end: Int |
19 | 19 | } |
@@ -449,6 +449,9 @@ public final class T3ComposerEditorView: ExpoView, UITextViewDelegate, UITextDro |
449 | 449 | private var tokensNeedRebuild = false |
450 | 450 | private var chipsNeedMeasuredWidth = false |
451 | 451 | private var hasDeferredForcedRebuild = false |
| 452 | + private var pendingContentOffset: CGPoint? |
| 453 | + private var needsSelectionReveal = false |
| 454 | + private var selectionBeforeFocusingTap: (selection: ComposerSelectionPayload, value: String)? |
452 | 455 |
|
453 | 456 | let onComposerChange = EventDispatcher() |
454 | 457 | let onComposerSelectionChange = EventDispatcher() |
@@ -520,6 +523,12 @@ public final class T3ComposerEditorView: ExpoView, UITextViewDelegate, UITextDro |
520 | 523 | @objc private func openContext(_ recognizer: UITapGestureRecognizer) { |
521 | 524 | guard let (index, attachment) = contextAttachment(at: recognizer.location(in: textView)) else { |
522 | 525 | textView.becomeFirstResponder() |
| 526 | + if selectionBeforeFocusingTap != nil { |
| 527 | + // UIKit places the caret from its own tap recognizer during this same touch. |
| 528 | + DispatchQueue.main.async { [weak self] in |
| 529 | + self?.restoreSelectionBeforeFocusingTap() |
| 530 | + } |
| 531 | + } |
523 | 532 | return |
524 | 533 | } |
525 | 534 | openContext(index: index, attachment: attachment) |
@@ -558,7 +567,11 @@ public final class T3ComposerEditorView: ExpoView, UITextViewDelegate, UITextDro |
558 | 567 | } |
559 | 568 |
|
560 | 569 | public func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool { |
561 | | - true |
| 570 | + // The collapsed editor only shows the draft's first line, so a tap that expands an |
| 571 | + // overflowing draft returns to the caret it was left at instead of moving it there. |
| 572 | + let collapsedOverflow = !textView.isFirstResponder && !textView.isScrollEnabled && draftOverflowsBounds() |
| 573 | + selectionBeforeFocusingTap = collapsedOverflow ? (sourceSelection(), value) : nil |
| 574 | + return true |
562 | 575 | } |
563 | 576 |
|
564 | 577 | public func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool { |
@@ -583,11 +596,27 @@ public final class T3ComposerEditorView: ExpoView, UITextViewDelegate, UITextDro |
583 | 596 |
|
584 | 597 | public override func layoutSubviews() { |
585 | 598 | super.layoutSubviews() |
| 599 | + let previousSize = textView.bounds.size |
586 | 600 | textView.frame = bounds |
587 | 601 | if chipsNeedMeasuredWidth, bounds.width > 0 { |
588 | 602 | chipsNeedMeasuredWidth = false |
589 | 603 | applyControlledDocument(force: true) |
590 | 604 | } |
| 605 | + if textView.isScrollEnabled, bounds.width > 0, bounds.height > 0 { |
| 606 | + textView.layoutIfNeeded() |
| 607 | + if let offset = pendingContentOffset { |
| 608 | + let minY = -textView.adjustedContentInset.top |
| 609 | + let maxY = max(minY, textView.contentSize.height - bounds.height + textView.adjustedContentInset.bottom) |
| 610 | + textView.setContentOffset(CGPoint(x: offset.x, y: min(maxY, max(minY, offset.y))), animated: false) |
| 611 | + pendingContentOffset = nil |
| 612 | + } |
| 613 | + if needsSelectionReveal || (previousSize != bounds.size && textView.isFirstResponder) { |
| 614 | + needsSelectionReveal = false |
| 615 | + if let selection = textView.selectedTextRange { |
| 616 | + textView.scrollRectToVisible(textView.caretRect(for: selection.end), animated: false) |
| 617 | + } |
| 618 | + } |
| 619 | + } |
591 | 620 | let placeholderX = textView.textContainerInset.left + textView.textContainer.lineFragmentPadding |
592 | 621 | let placeholderY = textView.textContainerInset.top |
593 | 622 | let placeholderWidth = max( |
@@ -632,10 +661,12 @@ public final class T3ComposerEditorView: ExpoView, UITextViewDelegate, UITextDro |
632 | 661 | tokens = decode([ComposerTokenPayload].self, from: document.tokensJson) ?? [] |
633 | 662 | tokensNeedRebuild = true |
634 | 663 | } |
| 664 | + let previousSelection = sourceSelection() |
635 | 665 | value = document.value |
636 | 666 | requestedSelection = document.selection |
637 | 667 | applyControlledDocument(force: tokensNeedRebuild) |
638 | 668 | applyRequestedSelection() |
| 669 | + cancelFocusingTapRestoreIfSelectionMoved(from: previousSelection) |
639 | 670 | if tokensMatchCurrentValue() { |
640 | 671 | tokensNeedRebuild = false |
641 | 672 | } |
@@ -694,7 +725,16 @@ public final class T3ComposerEditorView: ExpoView, UITextViewDelegate, UITextDro |
694 | 725 | } |
695 | 726 |
|
696 | 727 | func setScrollEnabled(_ scrollEnabled: Bool) { |
| 728 | + guard textView.isScrollEnabled != scrollEnabled else { return } |
| 729 | + if !scrollEnabled { |
| 730 | + // Disabling scrolling discards UITextView's offset. Restore it after |
| 731 | + // expansion has laid out the text, regardless of prop update order. |
| 732 | + pendingContentOffset = pendingContentOffset ?? textView.contentOffset |
| 733 | + } |
697 | 734 | textView.isScrollEnabled = scrollEnabled |
| 735 | + if scrollEnabled { |
| 736 | + revealSelectionAfterLayout() |
| 737 | + } |
698 | 738 | } |
699 | 739 |
|
700 | 740 | func setAutoFocus(_ autoFocus: Bool) { |
@@ -738,8 +778,10 @@ public final class T3ComposerEditorView: ExpoView, UITextViewDelegate, UITextDro |
738 | 778 | } |
739 | 779 |
|
740 | 780 | func setSelection(start: Int, end: Int) { |
| 781 | + let previousSelection = sourceSelection() |
741 | 782 | requestedSelection = ComposerSelectionPayload(start: start, end: end) |
742 | 783 | applyRequestedSelection() |
| 784 | + cancelFocusingTapRestoreIfSelectionMoved(from: previousSelection) |
743 | 785 | } |
744 | 786 |
|
745 | 787 | public func textViewDidChange(_ textView: UITextView) { |
@@ -811,6 +853,7 @@ public final class T3ComposerEditorView: ExpoView, UITextViewDelegate, UITextDro |
811 | 853 | } |
812 | 854 |
|
813 | 855 | public func textViewDidBeginEditing(_ textView: UITextView) { |
| 856 | + revealSelectionAfterLayout() |
814 | 857 | onComposerFocus() |
815 | 858 | } |
816 | 859 |
|
@@ -836,13 +879,21 @@ public final class T3ComposerEditorView: ExpoView, UITextViewDelegate, UITextDro |
836 | 879 | hasDeferredForcedRebuild = false |
837 | 880 |
|
838 | 881 | let previousSelection = sourceSelection() |
| 882 | + if textView.isScrollEnabled { |
| 883 | + pendingContentOffset = pendingContentOffset ?? textView.contentOffset |
| 884 | + } |
839 | 885 | isApplyingControlledValue = true |
840 | 886 | textView.attributedText = makeAttributedDocument() |
841 | 887 | let targetSelection = requestedSelection ?? previousSelection |
842 | 888 | requestedSelection = nil |
843 | 889 | textView.selectedRange = displayRange(for: targetSelection) |
844 | 890 | restoreBaseTypingAttributes() |
845 | 891 | isApplyingControlledValue = false |
| 892 | + if currentSource != value || previousSelection.start != targetSelection.start || previousSelection.end != targetSelection.end { |
| 893 | + revealSelectionAfterLayout() |
| 894 | + } else { |
| 895 | + setNeedsLayout() |
| 896 | + } |
846 | 897 | updatePlaceholderVisibility() |
847 | 898 | emitContentSizeIfNeeded() |
848 | 899 | } |
@@ -1053,6 +1104,7 @@ public final class T3ComposerEditorView: ExpoView, UITextViewDelegate, UITextDro |
1053 | 1104 | return |
1054 | 1105 | } |
1055 | 1106 | value = textView.serializedText() |
| 1107 | + revealSelectionAfterLayout() |
1056 | 1108 | let selection = sourceSelection() |
1057 | 1109 | nativeEventCount += 1 |
1058 | 1110 | onComposerChange([ |
@@ -1122,6 +1174,40 @@ public final class T3ComposerEditorView: ExpoView, UITextViewDelegate, UITextDro |
1122 | 1174 | isApplyingControlledValue = true |
1123 | 1175 | textView.selectedRange = nextRange |
1124 | 1176 | isApplyingControlledValue = false |
| 1177 | + revealSelectionAfterLayout() |
| 1178 | + } |
| 1179 | + |
| 1180 | + private func restoreSelectionBeforeFocusingTap() { |
| 1181 | + guard let snapshot = selectionBeforeFocusingTap else { |
| 1182 | + return |
| 1183 | + } |
| 1184 | + selectionBeforeFocusingTap = nil |
| 1185 | + // A draft that changed during the touch, such as a transcript landing, owns the caret now. |
| 1186 | + guard textView.isFirstResponder, textView.markedTextRange == nil, value == snapshot.value else { |
| 1187 | + return |
| 1188 | + } |
| 1189 | + let range = displayRange(for: snapshot.selection) |
| 1190 | + guard !NSEqualRanges(range, textView.selectedRange) else { |
| 1191 | + return |
| 1192 | + } |
| 1193 | + textView.selectedRange = range |
| 1194 | + revealSelectionAfterLayout() |
| 1195 | + } |
| 1196 | + |
| 1197 | + private func cancelFocusingTapRestoreIfSelectionMoved(from previousSelection: ComposerSelectionPayload) { |
| 1198 | + if sourceSelection() != previousSelection { |
| 1199 | + selectionBeforeFocusingTap = nil |
| 1200 | + } |
| 1201 | + } |
| 1202 | + |
| 1203 | + private func draftOverflowsBounds() -> Bool { |
| 1204 | + let size = textView.sizeThatFits(CGSize(width: textView.bounds.width, height: .greatestFiniteMagnitude)) |
| 1205 | + return size.height > textView.bounds.height + 1 |
| 1206 | + } |
| 1207 | + |
| 1208 | + private func revealSelectionAfterLayout() { |
| 1209 | + needsSelectionReveal = true |
| 1210 | + setNeedsLayout() |
1125 | 1211 | } |
1126 | 1212 |
|
1127 | 1213 | private func updatePlaceholderVisibility() { |
|