Fix cursor jumping when pressing return
Changelog-Fixed: Fix cursor jumping when pressing return Closes: #775
This commit is contained in:
@@ -15,6 +15,10 @@ enum NostrPostResult {
|
||||
|
||||
let POST_PLACEHOLDER = NSLocalizedString("Type your post here...", comment: "Text box prompt to ask user to type their post.")
|
||||
|
||||
class TagModel: ObservableObject {
|
||||
var diff = 0
|
||||
}
|
||||
|
||||
enum PostAction {
|
||||
case replying_to(NostrEvent)
|
||||
case quoting(NostrEvent)
|
||||
@@ -50,6 +54,7 @@ struct PostView: View {
|
||||
@State var mediaToUpload: MediaUpload? = nil
|
||||
|
||||
@StateObject var image_upload: ImageUploadModel = ImageUploadModel()
|
||||
@StateObject var tagModel: TagModel = TagModel()
|
||||
|
||||
let action: PostAction
|
||||
let damus_state: DamusState
|
||||
@@ -208,6 +213,7 @@ struct PostView: View {
|
||||
focusWordAttributes = (word, range)
|
||||
self.newCursorIndex = nil
|
||||
})
|
||||
.environmentObject(tagModel)
|
||||
.focused($focus)
|
||||
.textInputAutocapitalization(.sentences)
|
||||
.onChange(of: post) { p in
|
||||
@@ -338,6 +344,7 @@ struct PostView: View {
|
||||
if let searching {
|
||||
UserSearch(damus_state: damus_state, search: searching, focusWordAttributes: $focusWordAttributes, newCursorIndex: $newCursorIndex, postTextViewCanScroll: $postTextViewCanScroll, post: $post)
|
||||
.frame(maxHeight: .infinity)
|
||||
.environmentObject(tagModel)
|
||||
} else {
|
||||
Divider()
|
||||
VStack(alignment: .leading) {
|
||||
|
||||
@@ -25,6 +25,7 @@ struct UserSearch: View {
|
||||
@Binding var postTextViewCanScroll: Bool
|
||||
|
||||
@Binding var post: NSMutableAttributedString
|
||||
@EnvironmentObject var tagModel: TagModel
|
||||
|
||||
var users: [SearchedUser] {
|
||||
guard let contacts = damus_state.contacts.event else {
|
||||
@@ -48,6 +49,9 @@ struct UserSearch: View {
|
||||
}
|
||||
let mutableString = NSMutableAttributedString(attributedString: post)
|
||||
mutableString.replaceCharacters(in: wordRange, with: tagAttributedString)
|
||||
///adjust cursor position appropriately: ('diff' used in TextViewWrapper / updateUIView after below update of 'post')
|
||||
tagModel.diff = tagAttributedString.length - wordRange.length
|
||||
|
||||
post = mutableString
|
||||
focusWordAttributes = (nil, nil)
|
||||
newCursorIndex = wordRange.location + tagAttributedString.string.count
|
||||
|
||||
@@ -10,6 +10,8 @@ import SwiftUI
|
||||
struct TextViewWrapper: UIViewRepresentable {
|
||||
@Binding var attributedText: NSMutableAttributedString
|
||||
@Binding var postTextViewCanScroll: Bool
|
||||
@EnvironmentObject var tagModel: TagModel
|
||||
|
||||
let cursorIndex: Int?
|
||||
var getFocusWordForMention: ((String?, NSRange?) -> Void)? = nil
|
||||
|
||||
@@ -32,9 +34,14 @@ struct TextViewWrapper: UIViewRepresentable {
|
||||
|
||||
func updateUIView(_ uiView: UITextView, context: Context) {
|
||||
uiView.isScrollEnabled = postTextViewCanScroll
|
||||
let range = uiView.selectedRange
|
||||
uiView.attributedText = attributedText
|
||||
|
||||
TextViewWrapper.setTextProperties(uiView)
|
||||
setCursorPosition(textView: uiView)
|
||||
|
||||
uiView.selectedRange = NSRange(location: range.location + tagModel.diff, length: range.length)
|
||||
tagModel.diff = 0
|
||||
}
|
||||
|
||||
private func setCursorPosition(textView: UITextView) {
|
||||
|
||||
Reference in New Issue
Block a user