forked from shadowfacts/Tusker
Prevent caret from changing position on auto complete
This commit is contained in:
parent
ae272582ac
commit
58c6d508ec
|
@ -70,11 +70,17 @@ struct ComposeContentWarningTextField: UIViewRepresentable {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let distanceToEnd = textField.offset(from: selectedRange.start, to: textField.endOfDocument)
|
||||||
|
|
||||||
let selectedRangeStartUTF16 = textField.offset(from: textField.beginningOfDocument, to: selectedRange.start)
|
let selectedRangeStartUTF16 = textField.offset(from: textField.beginningOfDocument, to: selectedRange.start)
|
||||||
let characterBeforeCursorIndex = text.utf16.index(text.startIndex, offsetBy: selectedRangeStartUTF16)
|
let characterBeforeCursorIndex = text.utf16.index(text.startIndex, offsetBy: selectedRangeStartUTF16)
|
||||||
|
|
||||||
textField.text!.replaceSubrange(lastWordStartIndex..<characterBeforeCursorIndex, with: string)
|
textField.text!.replaceSubrange(lastWordStartIndex..<characterBeforeCursorIndex, with: string)
|
||||||
didChange(textField)
|
didChange(textField)
|
||||||
|
|
||||||
|
// keep the cursor at the same position in the text, immediately after what was inserted
|
||||||
|
let newCursorPosition = textField.position(from: textField.endOfDocument, offset: -distanceToEnd)!
|
||||||
|
textField.selectedTextRange = textField.textRange(from: newCursorPosition, to: newCursorPosition)
|
||||||
}
|
}
|
||||||
|
|
||||||
private func updateAutocompleteState(textField: UITextField) {
|
private func updateAutocompleteState(textField: UITextField) {
|
||||||
|
|
|
@ -239,10 +239,15 @@ struct MainComposeWrappedTextView: UIViewRepresentable {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let distanceToEnd = text.utf16.count - textView.selectedRange.upperBound
|
||||||
|
|
||||||
let characterBeforeCursorIndex = text.utf16.index(text.startIndex, offsetBy: textView.selectedRange.upperBound)
|
let characterBeforeCursorIndex = text.utf16.index(text.startIndex, offsetBy: textView.selectedRange.upperBound)
|
||||||
|
|
||||||
textView.text.replaceSubrange(lastWordStartIndex..<characterBeforeCursorIndex, with: string)
|
textView.text.replaceSubrange(lastWordStartIndex..<characterBeforeCursorIndex, with: string)
|
||||||
self.textViewDidChange(textView)
|
self.textViewDidChange(textView)
|
||||||
|
|
||||||
|
// keep the cursor at the same position in the text, immediately after what was inserted
|
||||||
|
textView.selectedRange = NSRange(location: textView.text.utf16.count - distanceToEnd, length: 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
private func updateAutocompleteState() {
|
private func updateAutocompleteState() {
|
||||||
|
|
Loading…
Reference in New Issue