Room mentioning in the composer (#3868)

* refactored the suggestion item structure

to scale with the room pill

* Implemented a way for the rooms

to appear in the suggestions view for the RTE, however I need to add the pills to the composer and the compatibility with the plain text composer

* small code correction

* fix

* fixed a bug where the suggestion wasn't returning

the right suggestion type and the suggestion text properly

* implementation done!

also updated some tests, but we need more of them

* updated toolbar view model tests

* updated tests

* updated preview tests

* renamed the Avatars case for the suggestions
This commit is contained in:
Mauro
2025-03-06 11:32:37 +01:00
committed by GitHub
parent 2d77a1b10b
commit 5f59e867b5
43 changed files with 564 additions and 180 deletions
+5 -2
View File
@@ -70,7 +70,7 @@ enum UserAvatarSizeOnScreen {
case readReceipt
case readReceiptSheet
case editUserDetails
case suggestions
case completionSuggestions
case blockedUsers
case knockingUsersBannerStack
case knockingUserBanner
@@ -89,7 +89,7 @@ enum UserAvatarSizeOnScreen {
return 32
case .home:
return 32
case .suggestions:
case .completionSuggestions:
return 32
case .blockedUsers:
return 32
@@ -133,6 +133,7 @@ enum RoomAvatarSizeOnScreen {
case notificationSettings
case roomDirectorySearch
case joinRoom
case completionSuggestions
var value: CGFloat {
switch self {
@@ -142,6 +143,8 @@ enum RoomAvatarSizeOnScreen {
return 32
case .roomDirectorySearch:
return 32
case .completionSuggestions:
return 32
case .messageForwarding:
return 36
case .globalSearch:
@@ -285,7 +285,7 @@ struct AttributedStringBuilder: AttributedStringBuilderProtocol {
case .room(let roomID):
mentionBuilder.handleRoomIDMention(for: attributedString, in: range, url: url, roomID: roomID)
case .roomAlias(let alias):
mentionBuilder.handleRoomAliasMention(for: attributedString, in: range, url: url, roomAlias: alias)
mentionBuilder.handleRoomAliasMention(for: attributedString, in: range, url: url, roomAlias: alias, roomDisplayName: nil)
case .eventOnRoomId(let roomID, let eventID):
mentionBuilder.handleEventOnRoomIDMention(for: attributedString, in: range, url: url, eventID: eventID, roomID: roomID)
case .eventOnRoomAlias(let alias, let eventID):
@@ -356,6 +356,7 @@ extension NSAttributedString.Key {
static let MatrixBlockquote: NSAttributedString.Key = .init(rawValue: BlockquoteAttribute.name)
static let MatrixUserID: NSAttributedString.Key = .init(rawValue: UserIDAttribute.name)
static let MatrixUserDisplayName: NSAttributedString.Key = .init(rawValue: UserDisplayNameAttribute.name)
static let MatrixRoomDisplayName: NSAttributedString.Key = .init(rawValue: RoomDisplayNameAttribute.name)
static let MatrixRoomID: NSAttributedString.Key = .init(rawValue: RoomIDAttribute.name)
static let MatrixRoomAlias: NSAttributedString.Key = .init(rawValue: RoomAliasAttribute.name)
static let MatrixEventOnRoomID: NSAttributedString.Key = .init(rawValue: EventOnRoomIDAttribute.name)
@@ -366,7 +367,7 @@ extension NSAttributedString.Key {
protocol MentionBuilderProtocol {
func handleUserMention(for attributedString: NSMutableAttributedString, in range: NSRange, url: URL, userID: String, userDisplayName: String?)
func handleRoomIDMention(for attributedString: NSMutableAttributedString, in range: NSRange, url: URL, roomID: String)
func handleRoomAliasMention(for attributedString: NSMutableAttributedString, in range: NSRange, url: URL, roomAlias: String)
func handleRoomAliasMention(for attributedString: NSMutableAttributedString, in range: NSRange, url: URL, roomAlias: String, roomDisplayName: String?)
func handleEventOnRoomAliasMention(for attributedString: NSMutableAttributedString, in range: NSRange, url: URL, eventID: String, roomAlias: String)
func handleEventOnRoomIDMention(for attributedString: NSMutableAttributedString, in range: NSRange, url: URL, eventID: String, roomID: String)
func handleAllUsersMention(for attributedString: NSMutableAttributedString, in range: NSRange)
@@ -24,6 +24,11 @@ enum UserDisplayNameAttribute: AttributedStringKey {
static var name = "MXUserDisplayNameAttribute"
}
enum RoomDisplayNameAttribute: AttributedStringKey {
typealias Value = String
static var name = "MXRoomDisplayNameAttribute"
}
enum RoomIDAttribute: AttributedStringKey {
typealias Value = String
static var name = "MXRoomIDAttribute"
@@ -64,6 +69,7 @@ extension AttributeScopes {
let userID: UserIDAttribute
let userDisplayName: UserDisplayNameAttribute
let roomDisplayName: RoomDisplayNameAttribute
let roomID: RoomIDAttribute
let roomAlias: RoomAliasAttribute
let eventOnRoomID: EventOnRoomIDAttribute
@@ -79,19 +79,22 @@ struct MentionBuilder: MentionBuilderProtocol {
.font: attributesToRestore.font,
.foregroundColor: attributesToRestore.foregroundColor]
attachmentAttributes.addBlockquoteIfNeeded(attributesToRestore.blockquote)
setPillAttachment(attachment: attachment,
attributedString: attributedString,
in: range,
with: attachmentAttributes)
}
func handleRoomAliasMention(for attributedString: NSMutableAttributedString, in range: NSRange, url: URL, roomAlias: String) {
func handleRoomAliasMention(for attributedString: NSMutableAttributedString, in range: NSRange, url: URL, roomAlias: String, roomDisplayName: String?) {
let attributesToRestore = getAttributesToRestore(for: attributedString, in: range)
let attachmentData = PillTextAttachmentData(type: .roomAlias(roomAlias), font: attributesToRestore.font)
guard let attachment = PillTextAttachment(attachmentData: attachmentData) else {
attributedString.addAttribute(.MatrixRoomAlias, value: roomAlias, range: range)
if let roomDisplayName {
attributedString.addAttribute(.MatrixRoomDisplayName, value: roomDisplayName, range: range)
}
return
}
@@ -100,6 +103,7 @@ struct MentionBuilder: MentionBuilderProtocol {
.font: attributesToRestore.font,
.foregroundColor: attributesToRestore.foregroundColor]
attachmentAttributes.addBlockquoteIfNeeded(attributesToRestore.blockquote)
attachmentAttributes.addMatrixRoomNameIfNeeded(roomDisplayName)
setPillAttachment(attachment: attachment,
attributedString: attributedString,
@@ -180,4 +184,10 @@ private extension Dictionary where Key == NSAttributedString.Key, Value == Any {
self[.MatrixUserDisplayName] = value
}
}
mutating func addMatrixRoomNameIfNeeded(_ value: String?) {
if let value {
self[.MatrixRoomDisplayName] = value
}
}
}
@@ -13,7 +13,7 @@ struct PlainMentionBuilder: MentionBuilderProtocol {
func handleEventOnRoomIDMention(for attributedString: NSMutableAttributedString, in range: NSRange, url: URL, eventID: String, roomID: String) { }
func handleRoomAliasMention(for attributedString: NSMutableAttributedString, in range: NSRange, url: URL, roomAlias: String) { }
func handleRoomAliasMention(for attributedString: NSMutableAttributedString, in range: NSRange, url: URL, roomAlias: String, roomDisplayName: String?) { }
func handleAllUsersMention(for attributedString: NSMutableAttributedString, in range: NSRange) { }