Skip to content

Commit

Permalink
Allow any message editing
Browse files Browse the repository at this point in the history
  • Loading branch information
Ri0n committed Jul 3, 2024
1 parent b46af0e commit b9f5588
Show file tree
Hide file tree
Showing 10 changed files with 62 additions and 39 deletions.
2 changes: 1 addition & 1 deletion src/chatdlg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -700,7 +700,7 @@ void ChatDlg::doSend()
QString id = account()->client()->genUniqueId();
m.setId(id); // we need id early for message manipulations in chatview
if (chatEdit()->isCorrection()) {
m.setReplaceId(chatEdit()->lastMessageId());
m.setReplaceId(chatEdit()->correctionId());
}
chatEdit()->setLastMessageId(id);
chatEdit()->resetCorrection();
Expand Down
1 change: 1 addition & 0 deletions src/chatview_te.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ private slots:
void nickInsertClick(const QString &nick);
void outgoingReactions(const QString &messageId, const QSet<QString> &reactions);
void outgoingMessageRetraction(const QString &messageId);
void editMessageRequested(const QString &messageId, const QString &text);

private:
bool isMuc_;
Expand Down
5 changes: 5 additions & 0 deletions src/chatview_webkit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,11 @@ class ChatViewJSObject : public ChatViewThemeSession {

Q_INVOKABLE void kick(const QString &nick) { qDebug() << "kick" << nick; }

Q_INVOKABLE void editMessage(const QString &messageId, const QString &messageHtml)
{
emit _view->editMessageRequested(messageId, TextUtil::rich2plain(messageHtml));
}

private slots:
void onUrlHeadersReady()
{
Expand Down
1 change: 1 addition & 0 deletions src/chatview_webkit.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ private slots:
void quote(const QString &text);
void outgoingReactions(const QString &messageId, const QSet<QString> &reactions);
void outgoingMessageRetraction(const QString &messageId);
void editMessageRequested(const QString &messageId, const QString &text);

private:
friend class ChatViewPrivate;
Expand Down
3 changes: 2 additions & 1 deletion src/groupchatdlg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1109,6 +1109,7 @@ GCMainDlg::GCMainDlg(PsiAccount *pa, const Jid &j, TabManager *tabManager) : Tab
setConnecting();

connect(ui_.log, &ChatView::quote, ui_.mle->chatEdit(), &ChatEdit::insertAsQuote);
connect(ui_.log, &ChatView::editMessageRequested, ui_.mle->chatEdit(), &ChatEdit::startCorrection);
connect(pa->avatarFactory(), &AvatarFactory::avatarChanged, this, &GCMainDlg::avatarUpdated);

#ifdef PSI_PLUGINS
Expand Down Expand Up @@ -1489,7 +1490,7 @@ void GCMainDlg::mle_returnPressed()
QString id = account()->client()->genUniqueId();
m.setId(id); // we need id early for message manipulations in chatview
if (ui_.mle->chatEdit()->isCorrection()) {
m.setReplaceId(ui_.mle->chatEdit()->lastMessageId());
m.setReplaceId(ui_.mle->chatEdit()->correctionId());
}
ui_.mle->chatEdit()->setLastMessageId(id);
ui_.mle->chatEdit()->resetCorrection();
Expand Down
25 changes: 15 additions & 10 deletions src/msgmle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ bool ChatEdit::event(QEvent *event)
if (event->type() == QEvent::ShortcutOverride) {
return false;
}
if (event->type() == QEvent::PaletteChange && recButton_ && !correction) {
if (event->type() == QEvent::PaletteChange && recButton_ && _correctionId.isEmpty()) {
setRecButtonIcon();
}
return QTextEdit::event(event);
Expand Down Expand Up @@ -433,7 +433,7 @@ void ChatEdit::optionsChanged(const QString &option)

void ChatEdit::showHistoryMessageNext()
{
correction = false;
_correctionId.clear();
if (!typedMsgsHistory.isEmpty()) {
if (typedMsgsIndex + 1 < typedMsgsHistory.size()) {
++typedMsgsIndex;
Expand Down Expand Up @@ -463,14 +463,14 @@ void ChatEdit::pasteAsQuote()

void ChatEdit::showHistoryMessagePrev()
{
if (!typedMsgsHistory.isEmpty() && (typedMsgsIndex > 0 || correction)) {
if (!typedMsgsHistory.isEmpty() && (typedMsgsIndex > 0 || !_correctionId.isEmpty())) {
// Save current typed text
if (typedMsgsIndex == typedMsgsHistory.size()) {
currentText = toPlainText();
correction = true;
currentText = toPlainText();
_correctionId = lastId;
}
if (typedMsgsIndex == typedMsgsHistory.size() - 1 && correction) {
correction = false;
if (typedMsgsIndex == typedMsgsHistory.size() - 1 && !_correctionId.isEmpty()) {
_correctionId.clear();
++typedMsgsIndex;
}
--typedMsgsIndex;
Expand All @@ -480,7 +480,7 @@ void ChatEdit::showHistoryMessagePrev()

void ChatEdit::showHistoryMessageFirst()
{
correction = false;
_correctionId.clear();
if (!typedMsgsHistory.isEmpty()) {
if (currentText.isEmpty()) {
typedMsgsIndex = typedMsgsHistory.size() - 1;
Expand All @@ -496,7 +496,7 @@ void ChatEdit::showHistoryMessageFirst()

void ChatEdit::showHistoryMessageLast()
{
correction = false;
_correctionId.clear();
if (!typedMsgsHistory.isEmpty()) {
typedMsgsIndex = 0;
showMessageHistory();
Expand Down Expand Up @@ -553,7 +553,6 @@ bool ChatEdit::canInsertFromMimeData(const QMimeData *source) const

void ChatEdit::updateBackground()
{
setProperty("correction", correction);
style()->unpolish(this);
style()->polish(this);
update();
Expand Down Expand Up @@ -647,6 +646,12 @@ void ChatEdit::insertAsQuote(const QString &text)
setFocus(Qt::OtherFocusReason);
}

void ChatEdit::startCorrection(const QString messageId, const QString &text)
{
_correctionId = messageId;
setEditText(text);
}

void ChatEdit::addSoundRecButton()
{
if (!recButton_) {
Expand Down
41 changes: 22 additions & 19 deletions src/msgmle.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,13 @@ class ChatEdit : public QTextEdit {
static bool checkSpellingGloballyEnabled();
void setCheckSpelling(bool);
XMPP::HTMLElement toHTMLElement();
bool isCorrection() { return correction; }
bool isCorrection() { return !_correctionId.isEmpty(); }
void setLastMessageId(const QString &id) { lastId = id; }
const QString &correctionId() const { return _correctionId; }
const QString &lastMessageId() { return lastId; }
void resetCorrection()
{
correction = false;
_correctionId.clear();
updateBackground();
}
CapitalLettersController *capitalizer();
Expand All @@ -80,6 +81,7 @@ public slots:
void doHTMLTextMenu();
void setCssString(const QString &css);
void insertAsQuote(const QString &text);
void startCorrection(const QString messageId, const QString &text);

protected slots:
void applySuggestion();
Expand Down Expand Up @@ -114,24 +116,25 @@ protected slots:
void setRecButtonIcon();

private:
QWidget *dialog_ = nullptr;
bool check_spelling_ = false;
SpellHighlighter *spellhighlighter_ = nullptr;
QPoint last_click_;
int previous_position_ = 0;
QStringList typedMsgsHistory;
int typedMsgsIndex = 0;
QAction *act_showMessagePrev = nullptr;
QAction *act_showMessageNext = nullptr;
QAction *act_showMessageFirst = nullptr;
QAction *act_showMessageLast = nullptr;
QAction *act_changeCase = nullptr;
QAction *actPasteAsQuote_ = nullptr;
QString currentText;
HTMLTextController *controller_ = nullptr;
CapitalLettersController *capitalizer_ = nullptr;
bool correction = false;
QWidget *dialog_ = nullptr;
bool check_spelling_ = false;
SpellHighlighter *spellhighlighter_ = nullptr;
QPoint last_click_;
int previous_position_ = 0;
QStringList typedMsgsHistory;
int typedMsgsIndex = 0;
QAction *act_showMessagePrev = nullptr;
QAction *act_showMessageNext = nullptr;
QAction *act_showMessageFirst = nullptr;
QAction *act_showMessageLast = nullptr;
QAction *act_changeCase = nullptr;
QAction *actPasteAsQuote_ = nullptr;
QString currentText;
HTMLTextController *controller_ = nullptr;
CapitalLettersController *capitalizer_ = nullptr;
// bool correction = false;
QString lastId;
QString _correctionId;
QPointer<QLayout> layout_;
QPointer<QToolButton> recButton_;
QPointer<QLabel> overlay_;
Expand Down
1 change: 1 addition & 0 deletions src/psichatdlg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,7 @@ void PsiChatDlg::initUi()
addAction(act_mini_cmd_);

connect(ui_.log, &ChatView::quote, ui_.mle->chatEdit(), &ChatEdit::insertAsQuote);
connect(ui_.log, &ChatView::editMessageRequested, ui_.mle->chatEdit(), &ChatEdit::startCorrection);

act_pastesend_ = new IconAction(tr("Paste and Send"), "psi/action_paste_and_send", tr("Paste and Send"), 0, this);
connect(act_pastesend_, SIGNAL(triggered()), SLOT(doPasteAndSend()));
Expand Down
16 changes: 9 additions & 7 deletions themes/chatview/psi/bubble/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -142,12 +142,14 @@
msgNode = event.target;
while (msgNode) {
const mcl = msgNode.classList;
hasGrNext = mcl.contains("grnext");
if (mcl.contains("avatar")) {
return []; // ignore avatar completely
}
if (msgNode.classList && (hasGrNext || mcl.contains("msg"))) {
break;
if (mcl) {
hasGrNext = mcl.contains("grnext");
if (mcl.contains("avatar")) {
return []; // ignore avatar completely
}
if (hasGrNext || mcl.contains("msg")) {
break;
}
}
msgNode = msgNode.parentNode;
}
Expand Down Expand Up @@ -177,7 +179,7 @@
{ text: "Reply", action: ()=>{ onReplyClicked(msgNode); } },
{ text: "Copy", action: ()=>{ shared.session.copyMessage(msgNode.id); } },
{ text: "Forward", action: ()=>{ shared.session.forwardMessage(msgNode.id); } },
{ text: "Edit", action: ()=>{ shared.session.editMessage(msgNode.id); } },
{ text: "Edit", action: ()=>{ shared.session.editMessage(msgNode.id, msgNode.getElementsByClassName("msgtext").item(0).innerHTML); } },
{ text: "Delete", action: ()=>{ shared.session.deleteMessage(msgNode.id); } }
]
}
Expand Down
6 changes: 5 additions & 1 deletion themes/chatview/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -642,9 +642,13 @@ function initPsiTheme() {
return false;
},

createHtmlNode : function(html, context) {
createHtmlNode : function(html, context, stripIndents=true) {
var range = document.createRange();
range.selectNode(context || document.body);
if (stripIndents) {
var re = new RegExp("^ +", "gm");
html = html.replace(re, "");
}
return range.createContextualFragment(html);
},

Expand Down

0 comments on commit b9f5588

Please sign in to comment.