commit e4e477a2acbe7c790ff15ebca418159aa3ddbcfa
parent 59e7a42b5f7eca485327ce7729a9ba4c07976947
Author: gladius <garthsnyder@protonmail.com>
Date: Mon, 6 Mar 2023 03:31:38 -0500
Fix cursor jumping when pressing return
Changelog-Fixed: Fix cursor jumping when pressing return
Closes: #775
Diffstat:
3 files changed, 18 insertions(+), 0 deletions(-)
diff --git 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
@@ -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
@@ -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) {