Skip to content

Commit

Permalink
Merge pull request #887 from planetary-social/feature/add-space-after…
Browse files Browse the repository at this point in the history
…-mention

Append a space after inserting a mention
  • Loading branch information
joshuatbrown authored Feb 14, 2024
2 parents a4f547a + 4a981cc commit 20b39ed
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
13 changes: 9 additions & 4 deletions Nos/Models/EditableNoteText.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down

0 comments on commit 20b39ed

Please sign in to comment.