Skip to content

Commit

Permalink
Fix a bug where moving a new line in the composer could move the caret.
Browse files Browse the repository at this point in the history
  • Loading branch information
pixlwave committed Aug 5, 2024
1 parent cdaa88e commit d916dbd
Showing 1 changed file with 7 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@ private struct UITextViewWrapper: UIViewRepresentable {
.foregroundColor: UIColor(.compound.textPrimary)]

if textView.attributedText != text {
// Remember the selection if only the attributes have changed.
let selection = textView.attributedText.string == text.string ? textView.selectedTextRange : nil

textView.attributedText = text

// Re-apply the default font when setting text for e.g. edits.
Expand All @@ -120,6 +123,10 @@ private struct UITextViewWrapper: UIViewRepresentable {
textView.keyboardType = .default
textView.reloadInputViews()
}
} else if let selection {
// Fixes a bug where pressing Return in the middle of two paragraphs
// moves the caret back to the bottom of the composer.
textView.selectedTextRange = selection
}
}
}
Expand Down

0 comments on commit d916dbd

Please sign in to comment.