From 4d333907bdc5c524ac6d17b0cd161fd85d3714cb Mon Sep 17 00:00:00 2001 From: Terry Yiu <963907+tyiu@users.noreply.github.com> Date: Sun, 25 Jun 2023 09:27:57 -0400 Subject: [PATCH] Fix taps on mentions in note drafts to not redirect to other Nostr clients Changelog-Fixed: Fix taps on mentions in note drafts to not redirect to other Nostr clients --- damus/Views/PostView.swift | 18 ++++++++++++++++-- damus/Views/Posting/UserSearch.swift | 3 ++- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/damus/Views/PostView.swift b/damus/Views/PostView.swift index 79506895..59bd9524 100644 --- a/damus/Views/PostView.swift +++ b/damus/Views/PostView.swift @@ -79,13 +79,27 @@ struct PostView: View { post.enumerateAttributes(in: NSRange(location: 0, length: post.length), options: []) { attributes, range, stop in if let link = attributes[.link] as? String { - post.replaceCharacters(in: range, with: link) + let normalized_link: String + if link.hasPrefix("damus:nostr:") { + // Replace damus:nostr: URI prefix with nostr: since the former is for internal navigation and not meant to be posted. + normalized_link = String(link.dropFirst(6)) + } else { + normalized_link = link + } + + // Add zero-width space in case text preceding the mention is not a whitespace. + // In the case where the character preceding the mention is a whitespace, the added zero-width space will be stripped out. + post.replaceCharacters(in: range, with: "\u{200B}\(normalized_link)\u{200B}") } } var content = self.post.string + // If two zero-width spaces are next to each other, normalize it to just one zero-width space. + .replacingOccurrences(of: "\u{200B}\u{200B}", with: "\u{200B}") + // If zero-width space is next to an actual whitespace, remove the zero-width space. + .replacingOccurrences(of: " \u{200B}", with: " ") + .replacingOccurrences(of: "\u{200B} ", with: " ") .trimmingCharacters(in: CharacterSet.whitespacesAndNewlines) - .replacingOccurrences(of: "\u{200B}", with: "") // these characters are added when adding mentions. let imagesString = uploadedMedias.map { $0.uploadedURL.absoluteString }.joined(separator: " ") diff --git a/damus/Views/Posting/UserSearch.swift b/damus/Views/Posting/UserSearch.swift index 4ac4358a..3393c2c8 100644 --- a/damus/Views/Posting/UserSearch.swift +++ b/damus/Views/Posting/UserSearch.swift @@ -63,7 +63,8 @@ struct UserSearch: View { let tagAttributedString = NSMutableAttributedString(string: tagString, attributes: [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 18.0), - NSAttributedString.Key.link: "nostr:\(pk)"]) + // Add damus: URI so that taps within the post creation view redirect internally within Damus instead of redirecting to a different Nostr client. The damus: prefix will be stripped out prior to sending the note to relays. + NSAttributedString.Key.link: "damus:nostr:\(pk)"]) tagAttributedString.removeAttribute(.link, range: NSRange(location: tagAttributedString.length - 2, length: 2)) tagAttributedString.addAttributes([NSAttributedString.Key.foregroundColor: UIColor.label], range: NSRange(location: tagAttributedString.length - 2, length: 2))