Reenable message edits
This commit is contained in:
@@ -14,6 +14,7 @@
|
||||
#include "enums/messagecomponenttype.h"
|
||||
#include "enums/richformat.h"
|
||||
#include "messagecontentmodel.h"
|
||||
#include "neochatroom.h"
|
||||
|
||||
namespace
|
||||
{
|
||||
@@ -27,46 +28,13 @@ ChatBarMessageContentModel::ChatBarMessageContentModel(QObject *parent)
|
||||
{
|
||||
m_editableActive = true;
|
||||
|
||||
connect(this, &ChatBarMessageContentModel::roomChanged, this, [this]() {
|
||||
connect(this, &ChatBarMessageContentModel::roomChanged, this, [this](NeoChatRoom *oldRoom) {
|
||||
if (m_type == ChatBarType::None || !m_room) {
|
||||
return;
|
||||
}
|
||||
|
||||
connect(m_room->cacheForType(m_type), &ChatBarCache::relationIdChanged, this, &ChatBarMessageContentModel::updateReplyModel);
|
||||
connect(m_room->cacheForType(m_type), &ChatBarCache::attachmentPathChanged, this, [this]() {
|
||||
if (m_room->cacheForType(m_type)->attachmentPath().length() > 0) {
|
||||
addAttachment(QUrl(m_room->cacheForType(m_type)->attachmentPath()));
|
||||
}
|
||||
});
|
||||
|
||||
if (m_room->cacheForType(m_type)->attachmentPath().length() > 0) {
|
||||
addAttachment(QUrl(m_room->cacheForType(m_type)->attachmentPath()));
|
||||
}
|
||||
|
||||
clearModel();
|
||||
|
||||
const auto textSections = m_room->cacheForType(m_type)->text().split(u"\n\n"_s);
|
||||
if (textSections.length() == 1 && textSections[0].isEmpty()) {
|
||||
initializeModel();
|
||||
return;
|
||||
}
|
||||
|
||||
beginResetModel();
|
||||
for (const auto §ion : textSections) {
|
||||
const auto type = MessageComponentType::typeForString(section);
|
||||
auto cleanText = section;
|
||||
if (type == MessageComponentType::Code) {
|
||||
cleanText.remove(0, 4);
|
||||
cleanText.remove(cleanText.length() - 4, 4);
|
||||
} else if (type == MessageComponentType::Quote) {
|
||||
cleanText.remove(0, 2);
|
||||
}
|
||||
insertComponent(rowCount(), type, {}, cleanText);
|
||||
}
|
||||
endResetModel();
|
||||
|
||||
m_currentFocusComponent = QPersistentModelIndex(index(rowCount() - 1));
|
||||
Q_EMIT focusRowChanged();
|
||||
connectCache(oldRoom ? oldRoom->cacheForType(m_type) : nullptr);
|
||||
initializeFromCache();
|
||||
});
|
||||
connect(this, &ChatBarMessageContentModel::focusRowChanged, this, [this]() {
|
||||
m_markdownHelper->setTextItem(focusedTextItem());
|
||||
@@ -80,19 +48,43 @@ ChatBarMessageContentModel::ChatBarMessageContentModel(QObject *parent)
|
||||
}
|
||||
m_keyHelper->room = m_room;
|
||||
});
|
||||
connect(this, &ChatBarMessageContentModel::typeChanged, this, [this]() {
|
||||
connect(this, &ChatBarMessageContentModel::typeChanged, this, [this](ChatBarType::Type oldType) {
|
||||
for (const auto &component : m_components) {
|
||||
if (const auto textItem = textItemForComponent(component)) {
|
||||
textItem->setType(m_type);
|
||||
}
|
||||
}
|
||||
if (!m_room) {
|
||||
return;
|
||||
}
|
||||
connectCache(m_room->cacheForType(oldType));
|
||||
initializeFromCache();
|
||||
});
|
||||
connect(m_markdownHelper, &ChatMarkdownHelper::unhandledBlockFormat, this, &ChatBarMessageContentModel::insertStyleAtCursor);
|
||||
|
||||
connectCache();
|
||||
connectKeyHelper();
|
||||
initializeModel();
|
||||
}
|
||||
|
||||
void ChatBarMessageContentModel::connectCache(ChatBarCache *oldCache)
|
||||
{
|
||||
if (m_type == ChatBarType::None || !m_room) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (oldCache) {
|
||||
oldCache->disconnect(this);
|
||||
}
|
||||
|
||||
connect(m_room->cacheForType(m_type), &ChatBarCache::relationIdChanged, this, &ChatBarMessageContentModel::updateReplyModel);
|
||||
connect(m_room->cacheForType(m_type), &ChatBarCache::attachmentPathChanged, this, [this]() {
|
||||
if (m_room->cacheForType(m_type)->attachmentPath().length() > 0) {
|
||||
addAttachment(QUrl(m_room->cacheForType(m_type)->attachmentPath()));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void ChatBarMessageContentModel::initializeModel(const QString &initialText)
|
||||
{
|
||||
updateReplyModel();
|
||||
@@ -114,6 +106,38 @@ void ChatBarMessageContentModel::initializeModel(const QString &initialText)
|
||||
Q_EMIT focusRowChanged();
|
||||
}
|
||||
|
||||
void ChatBarMessageContentModel::initializeFromCache()
|
||||
{
|
||||
if (m_room->cacheForType(m_type)->attachmentPath().length() > 0) {
|
||||
addAttachment(QUrl(m_room->cacheForType(m_type)->attachmentPath()));
|
||||
}
|
||||
|
||||
clearModel();
|
||||
|
||||
const auto textSections = m_room->cacheForType(m_type)->text().split(u"\n\n"_s);
|
||||
if (textSections.length() == 1 && textSections[0].isEmpty()) {
|
||||
initializeModel();
|
||||
return;
|
||||
}
|
||||
|
||||
beginResetModel();
|
||||
for (const auto §ion : textSections) {
|
||||
const auto type = MessageComponentType::typeForString(section);
|
||||
auto cleanText = section;
|
||||
if (type == MessageComponentType::Code) {
|
||||
cleanText.remove(0, 4);
|
||||
cleanText.remove(cleanText.length() - 4, 4);
|
||||
} else if (type == MessageComponentType::Quote) {
|
||||
cleanText.remove(0, 2);
|
||||
}
|
||||
insertComponent(rowCount(), type, {}, cleanText);
|
||||
}
|
||||
endResetModel();
|
||||
|
||||
m_currentFocusComponent = QPersistentModelIndex(index(rowCount() - 1));
|
||||
Q_EMIT focusRowChanged();
|
||||
}
|
||||
|
||||
ChatBarType::Type ChatBarMessageContentModel::type() const
|
||||
{
|
||||
return m_type;
|
||||
@@ -124,8 +148,8 @@ void ChatBarMessageContentModel::setType(ChatBarType::Type type)
|
||||
if (type == m_type) {
|
||||
return;
|
||||
}
|
||||
m_type = type;
|
||||
Q_EMIT typeChanged();
|
||||
const auto oldType = std::exchange(m_type, type);
|
||||
Q_EMIT typeChanged(oldType, m_type);
|
||||
}
|
||||
|
||||
ChatKeyHelper *ChatBarMessageContentModel::keyHelper() const
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
#include <QAbstractListModel>
|
||||
#include <QQmlEngine>
|
||||
|
||||
#include "chatbarcache.h"
|
||||
#include "chatkeyhelper.h"
|
||||
#include "chatmarkdownhelper.h"
|
||||
#include "chattextitemhelper.h"
|
||||
@@ -100,15 +101,17 @@ public:
|
||||
Q_INVOKABLE void postMessage();
|
||||
|
||||
Q_SIGNALS:
|
||||
void typeChanged();
|
||||
void typeChanged(ChatBarType::Type oldType, ChatBarType::Type newType);
|
||||
void focusRowChanged();
|
||||
void hasRichFormattingChanged();
|
||||
void sendMessageWithEnterChanged();
|
||||
|
||||
private:
|
||||
ChatBarType::Type m_type = ChatBarType::None;
|
||||
void connectCache(ChatBarCache *oldCache = nullptr);
|
||||
|
||||
void initializeModel(const QString &initialText = {});
|
||||
void initializeFromCache();
|
||||
|
||||
std::optional<QString> getReplyEventId() override;
|
||||
|
||||
|
||||
@@ -67,7 +67,7 @@ void MessageContentModel::setRoom(NeoChatRoom *room)
|
||||
m_room->disconnect(this);
|
||||
}
|
||||
|
||||
m_room = room;
|
||||
const auto oldRoom = std::exchange(m_room, room);
|
||||
|
||||
if (m_room) {
|
||||
connect(m_room, &NeoChatRoom::newFileTransfer, this, [this](const QString &eventId) {
|
||||
@@ -103,7 +103,7 @@ void MessageContentModel::setRoom(NeoChatRoom *room)
|
||||
connect(m_room, &NeoChatRoom::urlPreviewEnabledChanged, this, &MessageContentModel::componentsUpdated);
|
||||
}
|
||||
|
||||
Q_EMIT roomChanged();
|
||||
Q_EMIT roomChanged(oldRoom, m_room);
|
||||
}
|
||||
|
||||
QString MessageContentModel::eventId() const
|
||||
|
||||
@@ -123,7 +123,7 @@ public:
|
||||
Q_INVOKABLE void toggleSpoiler(QModelIndex index);
|
||||
|
||||
Q_SIGNALS:
|
||||
void roomChanged();
|
||||
void roomChanged(NeoChatRoom *oldRoom, NeoChatRoom *newRoom);
|
||||
void authorChanged();
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user