damus

nostr ios client
git clone git://jb55.com/damus
Log | Files | Refs | README | LICENSE

commit 8dad8e67033b6bbe04bc1f11048f2ebab107b85b
parent e30d38e69fb2b0a411ed27949b9e213fd854bbd2
Author: Jericho Hasselbush <jericho@sal-et-lucem.com>
Date:   Sat,  9 Sep 2023 17:08:27 -0400

posting: fix issue with username and multiple emojis

Fixes issue where username with multiple emojis would place cursor in
strange position. Now properly moves the cursor to space past the
multiple emoji user name.

Any amount would be great. Not a complex issue to fix!

Tipjar: lnbc1pj0eddtpp5km07jgrfm47nfswqqp33ngv374gzad2hshkra7zm3l0cmpusnp3qdqqcqzzsxqyz5vqsp5rklkzj9upf32z3c3nmc9xg4pdlz5p5mp3s332ygefexf79tq8ucs9qyyssqxfh4kz3sg9zczsnj49w23aw35z87jwyx9m5su8kkyxlspyjk4ajy7vhxuw2rzw4lz8vfutfakm2rggvpzhzs9ehfus4nl683dl99f4sqgm9zkq
Changelog-Fixed: Fixes issue where username with multiple emojis would place cursor in strange position.
Signed-off-by: Jericho Hasselbush <jericho@sal-et-lucem.com>
Signed-off-by: William Casarin <jb55@jb55.com>

Diffstat:
Mdamus/Views/Posting/UserSearch.swift | 2+-
AdamusTests/UserSearchAppendTests.swift | 54++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 55 insertions(+), 1 deletion(-)

diff --git a/damus/Views/Posting/UserSearch.swift b/damus/Views/Posting/UserSearch.swift @@ -47,7 +47,7 @@ struct UserSearch: View { tagModel.diff = appended.tag.length - wordRange.length focusWordAttributes = (nil, nil) - newCursorIndex = wordRange.location + appended.tag.string.count + newCursorIndex = wordRange.location + appended.tag.length } var body: some View { diff --git a/damusTests/UserSearchAppendTests.swift b/damusTests/UserSearchAppendTests.swift @@ -0,0 +1,54 @@ +// +// Created by Jericho Hasselbush on 9/9/23. +// + + +// Test fix for https://github.com/damus-io/damus/issues/1525 +// Only change in damus source is in UserSearch.swift +// UserSearch.appendUserTag + +import XCTest +@testable import damus + +final class UserSearchAppendTests: XCTestCase { + func testCursorShouldBeAtEndOfEmoji() throws { + let simpleTag = NSMutableAttributedString("@JB55") + let emojiTag = NSMutableAttributedString("@BTCapsule 🏴🧡") + let post = NSMutableAttributedString("A Post") + + var cursorIndex: Int = 0 + appendUserTag(withTag: simpleTag, post: post, word_range: .init(location: 0, length: 0), newCursorIndex: &cursorIndex, spy: simulatedCursor ) + XCTAssertEqual(cursorIndex, simpleTag.length + 1) // +1 for past end of tag + cursorIndex = 0 + appendUserTag(withTag: emojiTag, post: post, word_range: .init(location: 0, length: 0), newCursorIndex: &cursorIndex, spy: simulatedCursor) + XCTAssertEqual(cursorIndex, emojiTag.length + 1) // +1 for past end of tag + } +} + +typealias CursorSpy = (Int, NSMutableAttributedString) -> Void + +var simulatedCursor: CursorSpy = { cursorIndex, tag in + let tagWithSimulatedCursor = NSMutableAttributedString(attributedString: tag) + if tagWithSimulatedCursor.length < cursorIndex { + tagWithSimulatedCursor.append(.init(string: "|")) + } else { + tagWithSimulatedCursor.insert(.init(string: "|"), at: cursorIndex) + } + print(tagWithSimulatedCursor.string) +} + +func appendUserTag(withTag tag: NSMutableAttributedString, + post: NSMutableAttributedString, + word_range: NSRange, + newCursorIndex: inout Int, + spy: CursorSpy = { _, _ in }) { + let appended = append_user_tag(tag: tag, post: post, word_range: word_range) + + // faulty call +// newCursorIndex = word_range.location + appended.tag.string.count + + // good call + newCursorIndex = word_range.location + appended.tag.length + + spy(newCursorIndex, tag) +}