|
| 1 | +/* |
| 2 | +Copyright 2023 The Matrix.org Foundation C.I.C. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +import { Attributes, MappedSuggestion } from "@matrix-org/matrix-wysiwyg"; |
| 18 | +import { MatrixClient, Room } from "matrix-js-sdk/src/matrix"; |
| 19 | + |
| 20 | +import { ICompletion } from "../../../../../autocomplete/Autocompleter"; |
| 21 | +import * as Avatar from "../../../../../Avatar"; |
| 22 | + |
| 23 | +/** |
| 24 | + * Builds the query for the `<Autocomplete />` component from the rust suggestion. This |
| 25 | + * will change as we implement handling / commands. |
| 26 | + * |
| 27 | + * @param suggestion - represents if the rust model is tracking a potential mention |
| 28 | + * @returns an empty string if we can not generate a query, otherwise a query beginning |
| 29 | + * with @ for a user query, # for a room or space query |
| 30 | + */ |
| 31 | +export function buildQuery(suggestion: MappedSuggestion | null): string { |
| 32 | + if (!suggestion || !suggestion.keyChar || suggestion.type === "command") { |
| 33 | + // if we have an empty key character, we do not build a query |
| 34 | + // TODO implement the command functionality |
| 35 | + return ""; |
| 36 | + } |
| 37 | + |
| 38 | + return `${suggestion.keyChar}${suggestion.text}`; |
| 39 | +} |
| 40 | + |
| 41 | +/** |
| 42 | + * Find the room from the completion by looking it up using the client from the context |
| 43 | + * we are currently in |
| 44 | + * |
| 45 | + * @param completion - the completion from the autocomplete |
| 46 | + * @param client - the current client we are using |
| 47 | + * @returns a Room if one is found, null otherwise |
| 48 | + */ |
| 49 | +export function getRoomFromCompletion(completion: ICompletion, client: MatrixClient): Room | null { |
| 50 | + const roomId = completion.completionId; |
| 51 | + const aliasFromCompletion = completion.completion; |
| 52 | + |
| 53 | + let roomToReturn: Room | null | undefined; |
| 54 | + |
| 55 | + // Not quite sure if the logic here makes sense - specifically calling .getRoom with an alias |
| 56 | + // that doesn't start with #, but keeping the logic the same as in PartCreator.roomPill for now |
| 57 | + if (roomId) { |
| 58 | + roomToReturn = client.getRoom(roomId); |
| 59 | + } else if (!aliasFromCompletion.startsWith("#")) { |
| 60 | + roomToReturn = client.getRoom(aliasFromCompletion); |
| 61 | + } else { |
| 62 | + roomToReturn = client.getRooms().find((r) => { |
| 63 | + return r.getCanonicalAlias() === aliasFromCompletion || r.getAltAliases().includes(aliasFromCompletion); |
| 64 | + }); |
| 65 | + } |
| 66 | + |
| 67 | + return roomToReturn ?? null; |
| 68 | +} |
| 69 | + |
| 70 | +/** |
| 71 | + * Given an autocomplete suggestion, determine the text to display in the pill |
| 72 | + * |
| 73 | + * @param completion - the item selected from the autocomplete |
| 74 | + * @param client - the MatrixClient is required for us to look up the correct room mention text |
| 75 | + * @returns the text to display in the mention |
| 76 | + */ |
| 77 | +export function getMentionDisplayText(completion: ICompletion, client: MatrixClient): string { |
| 78 | + if (completion.type === "user") { |
| 79 | + return completion.completion; |
| 80 | + } else if (completion.type === "room") { |
| 81 | + // try and get the room and use it's name, if not available, fall back to |
| 82 | + // completion.completion |
| 83 | + return getRoomFromCompletion(completion, client)?.name || completion.completion; |
| 84 | + } |
| 85 | + return ""; |
| 86 | +} |
| 87 | + |
| 88 | +/** |
| 89 | + * For a given completion, the attributes will change depending on the completion type |
| 90 | + * |
| 91 | + * @param completion - the item selected from the autocomplete |
| 92 | + * @param client - the MatrixClient is required for us to look up the correct room mention text |
| 93 | + * @returns an object of attributes containing HTMLAnchor attributes or data-* attri |
| 94 | + */ |
| 95 | +export function getMentionAttributes(completion: ICompletion, client: MatrixClient, room: Room): Attributes { |
| 96 | + // to ensure that we always have something set in the --avatar-letter CSS variable |
| 97 | + // as otherwise alignment varies depending on whether the content is empty or not |
| 98 | + const defaultLetterContent = "-"; |
| 99 | + |
| 100 | + if (completion.type === "user") { |
| 101 | + // logic as used in UserPillPart.setAvatar in parts.ts |
| 102 | + const mentionedMember = room.getMember(completion.completionId || ""); |
| 103 | + |
| 104 | + if (!mentionedMember) return {}; |
| 105 | + |
| 106 | + const name = mentionedMember.name || mentionedMember.userId; |
| 107 | + const defaultAvatarUrl = Avatar.defaultAvatarUrlForString(mentionedMember.userId); |
| 108 | + const avatarUrl = Avatar.avatarUrlForMember(mentionedMember, 16, 16, "crop"); |
| 109 | + let initialLetter = defaultLetterContent; |
| 110 | + if (avatarUrl === defaultAvatarUrl) { |
| 111 | + initialLetter = Avatar.getInitialLetter(name) ?? defaultLetterContent; |
| 112 | + } |
| 113 | + |
| 114 | + return { |
| 115 | + "data-mention-type": completion.type, |
| 116 | + "style": `--avatar-background: url(${avatarUrl}); --avatar-letter: '${initialLetter}'`, |
| 117 | + }; |
| 118 | + } else if (completion.type === "room") { |
| 119 | + // logic as used in RoomPillPart.setAvatar in parts.ts |
| 120 | + const mentionedRoom = getRoomFromCompletion(completion, client); |
| 121 | + const aliasFromCompletion = completion.completion; |
| 122 | + |
| 123 | + let initialLetter = defaultLetterContent; |
| 124 | + let avatarUrl = Avatar.avatarUrlForRoom(mentionedRoom ?? null, 16, 16, "crop"); |
| 125 | + if (!avatarUrl) { |
| 126 | + initialLetter = Avatar.getInitialLetter(mentionedRoom?.name || aliasFromCompletion) ?? defaultLetterContent; |
| 127 | + avatarUrl = Avatar.defaultAvatarUrlForString(mentionedRoom?.roomId ?? aliasFromCompletion); |
| 128 | + } |
| 129 | + |
| 130 | + return { |
| 131 | + "data-mention-type": completion.type, |
| 132 | + "style": `--avatar-background: url(${avatarUrl}); --avatar-letter: '${initialLetter}'`, |
| 133 | + }; |
| 134 | + } |
| 135 | + |
| 136 | + return {}; |
| 137 | +} |
0 commit comments