Fix bug where you could only mention users at the end of a post
Changelog-Fixed: Fix bug where you could only mention users at the end of a post Closes: #1102
This commit is contained in:
@@ -20,6 +20,8 @@ struct SearchedUser: Identifiable {
|
||||
struct UserSearch: View {
|
||||
let damus_state: DamusState
|
||||
let search: String
|
||||
@Binding var focusWordAttributes: (String?, NSRange?)
|
||||
@Binding var newCursorIndex: Int?
|
||||
|
||||
@Binding var post: NSMutableAttributedString
|
||||
|
||||
@@ -35,20 +37,19 @@ struct UserSearch: View {
|
||||
guard let pk = bech32_pubkey(user.pubkey) else {
|
||||
return
|
||||
}
|
||||
|
||||
// Remove all characters after the last '@'
|
||||
removeCharactersAfterLastAtSymbol()
|
||||
|
||||
// Create and append the user tag
|
||||
let tagAttributedString = createUserTag(for: user, with: pk)
|
||||
appendUserTag(tagAttributedString)
|
||||
appendUserTag(withTag: tagAttributedString)
|
||||
}
|
||||
|
||||
private func removeCharactersAfterLastAtSymbol() {
|
||||
while post.string.last != "@" {
|
||||
post.deleteCharacters(in: NSRange(location: post.length - 1, length: 1))
|
||||
|
||||
private func appendUserTag(withTag tagAttributedString: NSMutableAttributedString) {
|
||||
guard let wordRange = focusWordAttributes.1 else {
|
||||
return
|
||||
}
|
||||
post.deleteCharacters(in: NSRange(location: post.length - 1, length: 1))
|
||||
let mutableString = NSMutableAttributedString(attributedString: post)
|
||||
mutableString.replaceCharacters(in: wordRange, with: tagAttributedString)
|
||||
post = mutableString
|
||||
focusWordAttributes = (nil, nil)
|
||||
newCursorIndex = wordRange.location + tagAttributedString.string.count
|
||||
}
|
||||
|
||||
private func createUserTag(for user: SearchedUser, with pk: String) -> NSMutableAttributedString {
|
||||
@@ -97,9 +98,11 @@ struct UserSearch: View {
|
||||
struct UserSearch_Previews: PreviewProvider {
|
||||
static let search: String = "jb55"
|
||||
@State static var post: NSMutableAttributedString = NSMutableAttributedString(string: "some @jb55")
|
||||
@State static var word: (String?, NSRange?) = (nil, nil)
|
||||
@State static var newCursorIndex: Int?
|
||||
|
||||
static var previews: some View {
|
||||
UserSearch(damus_state: test_damus_state(), search: search, post: $post)
|
||||
UserSearch(damus_state: test_damus_state(), search: search, focusWordAttributes: $word, newCursorIndex: $newCursorIndex, post: $post)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user