Use the rich text char format to store mentions rather than a separate structure in ChatBarCache.

This removes mentions from ChatBarCache and instead sets mentions as an anchor using the QTextCursor. Saving and restoring the chatbar text content is then done using QTextDocumentFragments which retain the rich text formatting.
This commit is contained in:
James Graham
2026-02-19 16:08:35 +00:00
parent a235f39c84
commit d2d48110cb
16 changed files with 128 additions and 198 deletions

View File

@@ -33,6 +33,11 @@ ChatBarCache::ChatBarCache(QObject *parent)
connect(this, &ChatBarCache::relationIdChanged, this, &ChatBarCache::relationAuthorIsPresentChanged);
}
Block::Cache &ChatBarCache::cache()
{
return m_cache;
}
QString ChatBarCache::text() const
{
return m_text;
@@ -55,27 +60,7 @@ QString ChatBarCache::sendText() const
return text().isEmpty() ? path.mid(path.lastIndexOf(u'/') + 1) : text();
}
return formatMentions();
}
QString ChatBarCache::formatMentions() const
{
auto mentions = m_mentions;
std::sort(mentions.begin(), mentions.end(), [](const auto &a, const auto &b) {
return a.cursor.anchor() > b.cursor.anchor();
});
auto formattedText = text();
for (const auto &mention : mentions) {
if (mention.text.isEmpty() || mention.id.isEmpty()) {
continue;
}
formattedText = formattedText.replace(mention.cursor.anchor(),
mention.cursor.position() - mention.cursor.anchor(),
u"[%1](https://matrix.to/#/%2)"_s.arg(mention.text.toHtmlEscaped(), mention.id));
}
return formattedText;
return text();
}
bool ChatBarCache::isReplying() const
@@ -232,11 +217,6 @@ void ChatBarCache::clearRelations()
Q_EMIT attachmentPathChanged();
}
QList<Mention> *ChatBarCache::mentions()
{
return &m_mentions;
}
QString ChatBarCache::savedText() const
{
return m_savedText;
@@ -294,7 +274,6 @@ void ChatBarCache::postMessage()
void ChatBarCache::clearCache()
{
setText({});
m_mentions.clear();
m_savedText = QString();
clearRelations();
}