From 9c1572564b2cab8922aff670d2c6fb903efbcee4 Mon Sep 17 00:00:00 2001 From: 23rd <23rd@vivaldi.net> Date: Tue, 21 Dec 2021 17:48:06 +0300 Subject: [PATCH] Added utility to replace spoiler entities with text commands. --- ui/text/text_entity.cpp | 16 ++++++++++++++++ ui/text/text_entity.h | 3 +++ 2 files changed, 19 insertions(+) diff --git a/ui/text/text_entity.cpp b/ui/text/text_entity.cpp index df78daf..69585fe 100644 --- a/ui/text/text_entity.cpp +++ b/ui/text/text_entity.cpp @@ -2332,6 +2332,22 @@ void SetClipboardText( } } +QString TextWithSpoilerCommands(const TextWithEntities &textWithEntities) { + auto text = textWithEntities.text; + auto offset = 0; + const auto start = textcmdStartSpoiler(); + const auto stop = textcmdStopSpoiler(); + for (const auto &e : textWithEntities.entities) { + if (e.type() == EntityType::Spoiler) { + text.insert(e.offset() + offset, start); + offset += start.size(); + text.insert(e.offset() + e.length() + offset, stop); + offset += stop.size(); + } + } + return text; +} + } // namespace TextUtilities EntityInText::EntityInText( diff --git a/ui/text/text_entity.h b/ui/text/text_entity.h index 27a7847..0cf60e2 100644 --- a/ui/text/text_entity.h +++ b/ui/text/text_entity.h @@ -381,4 +381,7 @@ void SetClipboardText( const TextForMimeData &text, QClipboard::Mode mode = QClipboard::Clipboard); +[[nodiscard]] QString TextWithSpoilerCommands( + const TextWithEntities &textWithEntities); + } // namespace TextUtilities