search: refactor appendUserTag to make logic more clear

ocd mostly
This commit is contained in:
William Casarin
2023-07-17 08:43:24 -07:00
parent 4f56ff3dfb
commit deaf5f042a

View File

@@ -41,18 +41,18 @@ struct UserSearch: View {
appendUserTag(withTag: user_tag) appendUserTag(withTag: user_tag)
} }
private func appendUserTag(withTag tagAttributedString: NSMutableAttributedString) { private func appendUserTag(withTag tag: NSMutableAttributedString) {
guard let wordRange = focusWordAttributes.1 else { guard let wordRange = focusWordAttributes.1 else { return }
return
}
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 let new_post = NSMutableAttributedString(attributedString: post)
new_post.replaceCharacters(in: wordRange, with: tag)
/// adjust cursor position appropriately: ('diff' used in TextViewWrapper / updateUIView after below update of 'post')
tagModel.diff = tag.length - wordRange.length
post = new_post
focusWordAttributes = (nil, nil) focusWordAttributes = (nil, nil)
newCursorIndex = wordRange.location + tagAttributedString.string.count newCursorIndex = wordRange.location + tag.string.count
} }
var body: some View { var body: some View {