Implement quick edit

This commit is contained in:
Carl Schwan
2021-05-06 16:14:38 +00:00
parent 7b1c5f5aab
commit 0a51c845e6
5 changed files with 62 additions and 3 deletions

View File

@@ -339,6 +339,7 @@ ToolBar {
function postMessage() {
checkForFancyEffectsReason();
if (ChatBoxHelper.hasAttachment) {
// send attachment but don't reset the text
actionsHandler.postMessage("", ChatBoxHelper.attachmentPath,
@@ -347,8 +348,16 @@ ToolBar {
messageSent();
return;
}
actionsHandler.postMessage(inputField.text.trim(), ChatBoxHelper.attachmentPath,
ChatBoxHelper.replyEventId, ChatBoxHelper.editEventId, userAutocompleted);
const re = /^s\/([^\/]*)\/([^\/]*)/;
if (Config.allowQuickEdit && re.test(inputField.text)) {
// send edited messages
actionsHandler.postEdit(inputField.text);
} else {
// send normal message
actionsHandler.postMessage(inputField.text.trim(), ChatBoxHelper.attachmentPath,
ChatBoxHelper.replyEventId, ChatBoxHelper.editEventId, userAutocompleted);
}
currentRoom.markAllMessagesAsRead();
inputField.clear();
inputField.text = Qt.binding(function() {

View File

@@ -76,5 +76,13 @@ Kirigami.ScrollablePage {
Config.save()
}
}
QQC2.CheckBox {
text: i18n("Use s/text/replacement syntax to edit your last message")
checked: Config.allowQuickEdit
onToggled: {
Config.allowQuickEdit = checked
Config.save()
}
}
}
}

View File

@@ -2,8 +2,10 @@
// SPDX-License-Identifier: GPL-3.0-or-later
#include "actionshandler.h"
#include "controller.h"
#include "csapi/joining.h"
#include <csapi/joining.h>
#include <events/roommessageevent.h>
#include <KLocalizedString>
#include <QDebug>
@@ -56,6 +58,36 @@ void ActionsHandler::setConnection(Connection *connection)
Q_EMIT connectionChanged();
}
void ActionsHandler::postEdit(const QString &text)
{
const auto localId = Controller::instance().activeConnection()->userId();
for (auto it = m_room->messageEvents().crbegin(); it != m_room->messageEvents().crend(); ++it) {
const auto &evt = **it;
if (const auto event = eventCast<const RoomMessageEvent>(&evt)) {
if (event->senderId() == localId && event->hasTextContent()) {
static QRegularExpression re("^s/([^/]*)/([^/]*)");
auto match = re.match(text);
if (!match.hasMatch()) {
// should not happen but still make sure to send the message normally
// just in case.
postMessage(text, QString(), QString(), QString(), QVariantMap());
}
const QString regex = match.captured(1);
const QString replacement = match.captured(2);
QString originalString;
if (event->content()) {
originalString = static_cast<const Quotient::EventContent::TextContent *>(event->content())->body;
} else {
originalString = event->plainBody();
}
m_room->postHtmlMessage(text, originalString.replace(regex, replacement), event->msgtype(), "", event->id());
return;
}
}
}
}
void ActionsHandler::postMessage(const QString &text,
const QString &attachementPath,
const QString &replyEventId,

View File

@@ -58,6 +58,12 @@ public Q_SLOTS:
void
postMessage(const QString &text, const QString &attachementPath, const QString &replyEventId, const QString &editEventId, const QVariantMap &usernames);
/// \brief Send edit instructions (.e.g s/hallo/hello/)
///
/// This will automatically edit the last message posted and send the sed
/// instruction to IRC.
void postEdit(const QString &text);
private:
Connection *m_connection = nullptr;
NeoChatRoom *m_room = nullptr;

View File

@@ -30,6 +30,10 @@
<label>Show leave and join events in the timeline</label>
<default>true</default>
</entry>
<entry name="AllowQuickEdit" type="bool">
<label>Use s/text/replacement syntax to edit your last message.</label>
<default>false</default>
</entry>
</group>
<group name="Timeline">
<entry name="ShowAvatarInTimeline" type="bool">