diff --git a/CHANGELOG.md b/CHANGELOG.md index ab186b0bc..408a1b135 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +- While composing a note, a space is now automatically inserted after any mention of a user or note to ensure it’s formatted correctly. - Fixed an issue where tapping the Feed tab did not scroll to the top of the Feed. - Fixed an issue where tapping the Profile tab did not scroll to the top of the Profile. - Search now starts automatically after entering three characters instead of one. diff --git a/Nos/Models/EditableNoteText.swift b/Nos/Models/EditableNoteText.swift index fd4ba2ac6..c32ea8e5b 100644 --- a/Nos/Models/EditableNoteText.swift +++ b/Nos/Models/EditableNoteText.swift @@ -84,37 +84,42 @@ struct EditableNoteText: Equatable { return } - let mention = AttributedString( + var mention = AttributedString( "@\(author.safeName)", attributes: defaultAttributes.merging( AttributeContainer([NSAttributedString.Key.link: url.absoluteString]) ) ) - + mention.append(AttributedString(stringLiteral: " ")) + attributedString.replaceSubrange((attributedString.index(beforeCharacter: index))..<index, with: mention) } /// Inserts the mention of an author as a link at the given index of the string. The `index` should be the index /// after a `@` character, which this function will replace. mutating func insertMention(npub: String, at range: Range<AttributedString.Index>) { - let mention = AttributedString( + var mention = AttributedString( "@\(npub.prefix(10).appending("..."))", attributes: defaultAttributes.merging( AttributeContainer([NSAttributedString.Key.link: "nostr:\(npub)"]) ) ) + mention.append(AttributedString(stringLiteral: " ")) + attributedString.replaceSubrange(range, with: mention) } /// Inserts the mention of an author as a link at the given index of the string. The `index` should be the index /// after a `@` character, which this function will replace. mutating func insertMention(note: String, at range: Range<AttributedString.Index>) { - let mention = AttributedString( + var mention = AttributedString( "@\(note.prefix(10).appending("..."))", attributes: defaultAttributes.merging( AttributeContainer([NSAttributedString.Key.link: "nostr:\(note)"]) ) ) + mention.append(AttributedString(stringLiteral: " ")) + attributedString.replaceSubrange(range, with: mention) }