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.")
|
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 {
|
enum PostAction {
|
||||||
case replying_to(NostrEvent)
|
case replying_to(NostrEvent)
|
||||||
case quoting(NostrEvent)
|
case quoting(NostrEvent)
|
||||||
@@ -50,6 +54,7 @@ struct PostView: View {
|
|||||||
@State var mediaToUpload: MediaUpload? = nil
|
@State var mediaToUpload: MediaUpload? = nil
|
||||||
|
|
||||||
@StateObject var image_upload: ImageUploadModel = ImageUploadModel()
|
@StateObject var image_upload: ImageUploadModel = ImageUploadModel()
|
||||||
|
@StateObject var tagModel: TagModel = TagModel()
|
||||||
|
|
||||||
let action: PostAction
|
let action: PostAction
|
||||||
let damus_state: DamusState
|
let damus_state: DamusState
|
||||||
@@ -208,6 +213,7 @@ struct PostView: View {
|
|||||||
focusWordAttributes = (word, range)
|
focusWordAttributes = (word, range)
|
||||||
self.newCursorIndex = nil
|
self.newCursorIndex = nil
|
||||||
})
|
})
|
||||||
|
.environmentObject(tagModel)
|
||||||
.focused($focus)
|
.focused($focus)
|
||||||
.textInputAutocapitalization(.sentences)
|
.textInputAutocapitalization(.sentences)
|
||||||
.onChange(of: post) { p in
|
.onChange(of: post) { p in
|
||||||
@@ -338,6 +344,7 @@ struct PostView: View {
|
|||||||
if let searching {
|
if let searching {
|
||||||
UserSearch(damus_state: damus_state, search: searching, focusWordAttributes: $focusWordAttributes, newCursorIndex: $newCursorIndex, postTextViewCanScroll: $postTextViewCanScroll, post: $post)
|
UserSearch(damus_state: damus_state, search: searching, focusWordAttributes: $focusWordAttributes, newCursorIndex: $newCursorIndex, postTextViewCanScroll: $postTextViewCanScroll, post: $post)
|
||||||
.frame(maxHeight: .infinity)
|
.frame(maxHeight: .infinity)
|
||||||
|
.environmentObject(tagModel)
|
||||||
} else {
|
} else {
|
||||||
Divider()
|
Divider()
|
||||||
VStack(alignment: .leading) {
|
VStack(alignment: .leading) {
|
||||||
|
|||||||
@@ -25,6 +25,7 @@ struct UserSearch: View {
|
|||||||
@Binding var postTextViewCanScroll: Bool
|
@Binding var postTextViewCanScroll: Bool
|
||||||
|
|
||||||
@Binding var post: NSMutableAttributedString
|
@Binding var post: NSMutableAttributedString
|
||||||
|
@EnvironmentObject var tagModel: TagModel
|
||||||
|
|
||||||
var users: [SearchedUser] {
|
var users: [SearchedUser] {
|
||||||
guard let contacts = damus_state.contacts.event else {
|
guard let contacts = damus_state.contacts.event else {
|
||||||
@@ -48,6 +49,9 @@ struct UserSearch: View {
|
|||||||
}
|
}
|
||||||
let mutableString = NSMutableAttributedString(attributedString: post)
|
let mutableString = NSMutableAttributedString(attributedString: post)
|
||||||
mutableString.replaceCharacters(in: wordRange, with: tagAttributedString)
|
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
|
post = mutableString
|
||||||
focusWordAttributes = (nil, nil)
|
focusWordAttributes = (nil, nil)
|
||||||
newCursorIndex = wordRange.location + tagAttributedString.string.count
|
newCursorIndex = wordRange.location + tagAttributedString.string.count
|
||||||
|
|||||||
@@ -10,6 +10,8 @@ import SwiftUI
|
|||||||
struct TextViewWrapper: UIViewRepresentable {
|
struct TextViewWrapper: UIViewRepresentable {
|
||||||
@Binding var attributedText: NSMutableAttributedString
|
@Binding var attributedText: NSMutableAttributedString
|
||||||
@Binding var postTextViewCanScroll: Bool
|
@Binding var postTextViewCanScroll: Bool
|
||||||
|
@EnvironmentObject var tagModel: TagModel
|
||||||
|
|
||||||
let cursorIndex: Int?
|
let cursorIndex: Int?
|
||||||
var getFocusWordForMention: ((String?, NSRange?) -> Void)? = nil
|
var getFocusWordForMention: ((String?, NSRange?) -> Void)? = nil
|
||||||
|
|
||||||
@@ -32,9 +34,14 @@ struct TextViewWrapper: UIViewRepresentable {
|
|||||||
|
|
||||||
func updateUIView(_ uiView: UITextView, context: Context) {
|
func updateUIView(_ uiView: UITextView, context: Context) {
|
||||||
uiView.isScrollEnabled = postTextViewCanScroll
|
uiView.isScrollEnabled = postTextViewCanScroll
|
||||||
|
let range = uiView.selectedRange
|
||||||
uiView.attributedText = attributedText
|
uiView.attributedText = attributedText
|
||||||
|
|
||||||
TextViewWrapper.setTextProperties(uiView)
|
TextViewWrapper.setTextProperties(uiView)
|
||||||
setCursorPosition(textView: uiView)
|
setCursorPosition(textView: uiView)
|
||||||
|
|
||||||
|
uiView.selectedRange = NSRange(location: range.location + tagModel.diff, length: range.length)
|
||||||
|
tagModel.diff = 0
|
||||||
}
|
}
|
||||||
|
|
||||||
private func setCursorPosition(textView: UITextView) {
|
private func setCursorPosition(textView: UITextView) {
|
||||||
|
|||||||
Reference in New Issue
Block a user