Fix autocompletion

Now it will save a map from display name to id and use that to generate
clean matrix.to links. This also make sure the colors used for the
preview are correct by using NeoChatUser and fix the bug with the regex
by simply removing the regex.
This commit is contained in:
Carl Schwan
2021-01-12 21:47:57 +01:00
parent bfd6d2ffe2
commit 4e197c3cc8
3 changed files with 24 additions and 6 deletions

View File

@@ -133,6 +133,7 @@ ToolBar {
keyNavigationWraps: true keyNavigationWraps: true
delegate: Control { delegate: Control {
readonly property string userId: modelData.id ?? ""
readonly property string displayText: modelData.displayName ?? modelData.unicode readonly property string displayText: modelData.displayName ?? modelData.unicode
readonly property bool isEmoji: modelData.unicode != null readonly property bool isEmoji: modelData.unicode != null
readonly property bool highlighted: autoCompleteListView.currentIndex == index readonly property bool highlighted: autoCompleteListView.currentIndex == index
@@ -437,13 +438,18 @@ ToolBar {
autoCompleteEndPosition = cursorPosition autoCompleteEndPosition = cursorPosition
} }
// store each user we autoComplete here, this will be helpful later to generate
// the matrix.to links.
// This use an hack to define: https://doc.qt.io/qt-5/qml-var.html#property-value-initialization-semantics
property var userAutocompleted: ({})
function postMessage() { function postMessage() {
// Qt wraps lines so we need to use a small hack // Qt wraps lines so we need to use a small hack
// to remove the wrapped lines but not break the empty // to remove the wrapped lines but not break the empty
// lines. // lines.
const updatedText = inputField.text.trim() documentHandler.postMessage(inputField.text.trim(), attachmentPath, replyEventID,
.replace(/@([^: ]*):([^ ]*\.[^ ]*)/, "[@$1:$2](https://matrix.to/#/@$1:$2)"); inputField.userAutocompleted);
documentHandler.postMessage(updatedText, attachmentPath, replyEventID);
clearAttachment(); clearAttachment();
currentRoom.markAllMessagesAsRead(); currentRoom.markAllMessagesAsRead();
clear(); clear();
@@ -454,6 +460,9 @@ ToolBar {
function autoComplete() { function autoComplete() {
documentHandler.replaceAutoComplete(autoCompleteListView.currentItem.displayText) documentHandler.replaceAutoComplete(autoCompleteListView.currentItem.displayText)
if (!autoCompleteListView.currentItem.isEmoji) {
inputField.userAutocompleted[autoCompleteListView.currentItem.displayText] = autoCompleteListView.currentItem.userId;
}
} }
} }
@@ -543,6 +552,7 @@ ToolBar {
function clear() { function clear() {
inputField.clear() inputField.clear()
inputField.userAutocompleted = {};
} }
function clearReply() { function clearReply() {

View File

@@ -167,7 +167,8 @@ QVariantMap ChatDocumentHandler::getAutocompletionInfo()
}; };
} }
void ChatDocumentHandler::postMessage(const QString &text, const QString &attachementPath, const QString &replyEventId) const void ChatDocumentHandler::postMessage(const QString &text, const QString &attachementPath,
const QString &replyEventId, const QVariantMap usernames) const
{ {
if (!m_room || !m_document) { if (!m_room || !m_document) {
return; return;
@@ -177,6 +178,13 @@ void ChatDocumentHandler::postMessage(const QString &text, const QString &attach
cleanedText = cleanedText.trimmed(); cleanedText = cleanedText.trimmed();
for (const auto username : usernames.keys()) {
const auto replacement = usernames.value(username);
cleanedText = cleanedText.replace(username,
"[" + username + "](https://matrix.to/#/" + replacement.toString() + ")");
}
if (attachementPath.length() > 0) { if (attachementPath.length() > 0) {
m_room->uploadFile(attachementPath, cleanedText); m_room->uploadFile(attachementPath, cleanedText);
} }
@@ -200,7 +208,7 @@ void ChatDocumentHandler::postMessage(const QString &text, const QString &attach
for (int i = 0; i < cleanedText.length(); i++) { for (int i = 0; i < cleanedText.length(); i++) {
rainbowText = rainbowText % QStringLiteral("<font color='") % rainbowColors.at(i % rainbowColors.length()) % "'>" % cleanedText.at(i) % "</font>"; rainbowText = rainbowText % QStringLiteral("<font color='") % rainbowColors.at(i % rainbowColors.length()) % "'>" % cleanedText.at(i) % "</font>";
} }
m_room->postHtmlMessage(cleanedText, rainbowText, messageEventType, replyEventId); m_room->postHtmlMessage(text, rainbowText, messageEventType, replyEventId);
return; return;
} }

View File

@@ -51,7 +51,7 @@ public:
[[nodiscard]] NeoChatRoom *room() const; [[nodiscard]] NeoChatRoom *room() const;
void setRoom(NeoChatRoom *room); void setRoom(NeoChatRoom *room);
Q_INVOKABLE void postMessage(const QString &text, const QString &attachementPath, const QString &replyEventId) const; Q_INVOKABLE void postMessage(const QString &text, const QString &attachementPath, const QString &replyEventId, const QVariantMap usernames) const;
/// This function will look at the current QTextCursor and determine if there /// This function will look at the current QTextCursor and determine if there
/// is the posibility to autocomplete it. /// is the posibility to autocomplete it.