Apply all the required styling in cpp

Apply all the required styling to show links, table, spoilers, etc in cpp. This also updates the method of revealing spoilers so now you can click to reveal then click again to hide.
This commit is contained in:
James Graham
2025-08-20 17:10:44 +01:00
parent 4498d4457b
commit 0d63fce59a
9 changed files with 297 additions and 107 deletions

View File

@@ -93,8 +93,12 @@ QString TextHandler::handleSendText()
return outputString;
}
QString
TextHandler::handleRecieveRichText(Qt::TextFormat inputFormat, const NeoChatRoom *room, const Quotient::RoomEvent *event, bool stripNewlines, bool isEdited)
QString TextHandler::handleRecieveRichText(Qt::TextFormat inputFormat,
const NeoChatRoom *room,
const Quotient::RoomEvent *event,
bool stripNewlines,
bool isEdited,
bool spoilerRevealed)
{
m_pos = 0;
m_dataBuffer = m_data;
@@ -151,7 +155,7 @@ TextHandler::handleRecieveRichText(Qt::TextFormat inputFormat, const NeoChatRoom
} else if ((getTagType(m_nextToken) == u"br"_s && stripNewlines)) {
nextTokenBuffer = u' ';
}
nextTokenBuffer = cleanAttributes(getTagType(m_nextToken), nextTokenBuffer);
nextTokenBuffer = cleanAttributes(getTagType(m_nextToken), nextTokenBuffer, true, spoilerRevealed);
}
outputString.append(nextTokenBuffer);
@@ -333,7 +337,8 @@ MessageComponent TextHandler::nextBlock(const QString &string,
Qt::TextFormat inputFormat,
const NeoChatRoom *room,
const Quotient::RoomEvent *event,
bool isEdited)
bool isEdited,
bool spoilerRevealed)
{
if (string.isEmpty()) {
return {};
@@ -355,7 +360,11 @@ MessageComponent TextHandler::nextBlock(const QString &string,
content = unescapeHtml(content);
break;
default:
content = handleRecieveRichText(inputFormat, room, event, false, isEdited);
content = handleRecieveRichText(inputFormat, room, event, false, isEdited, spoilerRevealed);
}
if (content.contains(u"data-mx-spoiler"_s)) {
attributes[u"hasSpoiler"_s] = true;
}
return MessageComponent{messageComponentType, content, attributes};
}
@@ -462,8 +471,11 @@ bool TextHandler::isAllowedLink(const QString &link, bool isImg)
}
}
QString TextHandler::cleanAttributes(const QString &tag, const QString &tagString)
QString TextHandler::cleanAttributes(const QString &tag, const QString &tagString, bool addStyle, bool spoilerRevealed)
{
if (!tagString.contains(u'<') || !tagString.contains(u'>')) {
return tagString;
}
int nextAttributeIndex = tagString.indexOf(u' ', 1);
if (nextAttributeIndex != -1) {
@@ -518,11 +530,33 @@ QString TextHandler::cleanAttributes(const QString &tag, const QString &tagStrin
nextAttributeIndex = nextSpaceIndex + 1;
}
outputString += u'>';
return outputString;
return addStyle ? this->addStyle(tag, outputString, spoilerRevealed) : outputString + u'>';
}
return tagString;
return addStyle ? this->addStyle(tag, tagString) : tagString;
}
QString TextHandler::addStyle(const QString &tag, QString cleanTagString, bool spoilerRevealed)
{
if (cleanTagString.endsWith(u'>')) {
cleanTagString.removeLast();
}
if (!cleanTagString.startsWith(u"</"_s)) {
if (tag == u"a"_s) {
cleanTagString += u" style=\"text-decoration: none;\""_s;
} else if (tag == u"table"_s) {
cleanTagString += u" style=\"width: 100%; border-collapse: collapse; border: 1px; border-style: solid;\""_s;
} else if (tag == u"th"_s || tag == u"td"_s) {
cleanTagString += u" style=\"border: 1px solid black; padding: 3px;\""_s;
} else if (tag == u"span"_s && cleanTagString.contains(u"data-mx-spoiler"_s)) {
Kirigami::Platform::PlatformTheme *theme =
static_cast<Kirigami::Platform::PlatformTheme *>(qmlAttachedPropertiesObject<Kirigami::Platform::PlatformTheme>(this, true));
cleanTagString += u" style=\"color: %1; background: %2;\""_s.arg(spoilerRevealed ? theme->highlightedTextColor().name() : u"transparent"_s,
theme->alternateBackgroundColor().name());
}
}
return cleanTagString + u'>';
}
QVariantMap TextHandler::getAttributes(const QString &tag, const QString &tagString)
@@ -567,8 +601,12 @@ QVariantMap TextHandler::getAttributes(const QString &tag, const QString &tagStr
return attributes;
}
QList<MessageComponent>
TextHandler::textComponents(QString string, Qt::TextFormat inputFormat, const NeoChatRoom *room, const Quotient::RoomEvent *event, bool isEdited)
QList<MessageComponent> TextHandler::textComponents(QString string,
Qt::TextFormat inputFormat,
const NeoChatRoom *room,
const Quotient::RoomEvent *event,
bool isEdited,
bool spoilerRevealed)
{
if (string.trimmed().isEmpty()) {
return {MessageComponent{MessageComponentType::Text, i18n("<i>This event does not have any content.</i>"), {}}};
@@ -580,7 +618,8 @@ TextHandler::textComponents(QString string, Qt::TextFormat inputFormat, const Ne
QList<MessageComponent> components;
while (!string.isEmpty()) {
const auto nextBlockPos = this->nextBlockPos(string);
const auto nextBlock = this->nextBlock(string, nextBlockPos, inputFormat, room, event, nextBlockPos == string.size() ? isEdited : false);
const auto nextBlock =
this->nextBlock(string, nextBlockPos, inputFormat, room, event, nextBlockPos == string.size() ? isEdited : false, spoilerRevealed);
components += nextBlock;
string.remove(0, nextBlockPos);
@@ -798,4 +837,20 @@ QString TextHandler::convertCodeLanguageString(const QString &languageString)
return languageString.right(languageString.length() - equalsPos - 1);
}
QString TextHandler::updateSpoilerText(QObject *object, QString string, bool spoilerRevealed)
{
auto it = QRegularExpression(u"<span[^>]*data-mx-spoiler[^>]*style=\"color: (.*?); background: (.*?);\">"_s).globalMatch(string);
Kirigami::Platform::PlatformTheme *theme =
static_cast<Kirigami::Platform::PlatformTheme *>(qmlAttachedPropertiesObject<Kirigami::Platform::PlatformTheme>(object, true));
int offset = 0;
while (it.hasNext()) {
const QRegularExpressionMatch match = it.next();
const auto newColor = spoilerRevealed ? theme->textColor().name() : u"transparent"_s;
string.replace(match.capturedStart(2) + offset, match.capturedLength(2), theme->alternateBackgroundColor().name());
string.replace(match.capturedStart(1) + offset, match.capturedLength(1), newColor);
offset = newColor.length() - match.capturedLength(1);
}
return string;
}
#include "moc_texthandler.cpp"