diff --git a/damus/Views/PostView.swift b/damus/Views/PostView.swift index 9e554b2f..b775f43a 100644 --- a/damus/Views/PostView.swift +++ b/damus/Views/PostView.swift @@ -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) { diff --git a/damus/Views/Posting/UserSearch.swift b/damus/Views/Posting/UserSearch.swift index 8873aa09..9da736f8 100644 --- a/damus/Views/Posting/UserSearch.swift +++ b/damus/Views/Posting/UserSearch.swift @@ -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 diff --git a/damus/Views/TextViewWrapper.swift b/damus/Views/TextViewWrapper.swift index 7a6d1bc7..1d6cda72 100644 --- a/damus/Views/TextViewWrapper.swift +++ b/damus/Views/TextViewWrapper.swift @@ -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) {