From 06f3c837f6b65868ec1ed048ba8843fbed247677 Mon Sep 17 00:00:00 2001 From: John Preston Date: Wed, 21 Sep 2022 18:54:37 +0400 Subject: [PATCH] Add TextForMimeData::WithExpandedLinks method. --- ui/text/text.cpp | 1 + ui/text/text_entity.cpp | 37 +++++++++++++++++++++++++++++++++++++ ui/text/text_entity.h | 1 + 3 files changed, 39 insertions(+) diff --git a/ui/text/text.cpp b/ui/text/text.cpp index 1991f70..24185b1 100644 --- a/ui/text/text.cpp +++ b/ui/text/text.cpp @@ -932,6 +932,7 @@ TextForMimeData String::toText( if (!handler || (!composeExpanded && !composeEntities)) { return; } + // This logic is duplicated in TextForMimeData::WithExpandedLinks. const auto entity = handler->getTextEntity(); const auto plainUrl = (entity.type == EntityType::Url) || (entity.type == EntityType::Email); diff --git a/ui/text/text_entity.cpp b/ui/text/text_entity.cpp index c4878de..fdb5c09 100644 --- a/ui/text/text_entity.cpp +++ b/ui/text/text_entity.cpp @@ -12,6 +12,7 @@ #include "ui/text/text.h" #include "ui/widgets/input_fields.h" #include "ui/emoji_config.h" +#include "ui/basic_click_handlers.h" #include "base/qt/qt_common_adapters.h" #include @@ -2262,6 +2263,42 @@ void SetClipboardText( } // namespace TextUtilities +TextForMimeData TextForMimeData::WithExpandedLinks( + const TextWithEntities &text) { + auto result = TextForMimeData{ .rich = text }; + if (!ranges::contains( + text.entities, + EntityType::CustomUrl, + &EntityInText::type)) { + result.expanded = text.text; + } else { + auto from = 0; + for (const auto &entity : text.entities) { + if (entity.type() != EntityType::CustomUrl) { + continue; + } + // This logic is duplicated in Ui::Text::String::toText. + const auto &data = entity.data(); + if (!data.startsWith(qstr("internal:")) + && (data != UrlClickHandler::EncodeForOpening( + text.text.mid(entity.offset(), entity.length())))) { + const auto till = entity.offset() + entity.length(); + if (const auto add = till - from; add > 0) { + result.expanded.append(text.text.data() + from, add); + from = till; + } + result.expanded.append(qstr(" (")).append(data).append(')'); + } + } + const auto till = text.text.size(); + if (const auto add = till - from; add > 0) { + result.expanded.append(text.text.data() + from, add); + from = till; + } + } + return result; +} + EntityInText::EntityInText( EntityType type, int offset, diff --git a/ui/text/text_entity.h b/ui/text/text_entity.h index 0a18f83..1986dca 100644 --- a/ui/text/text_entity.h +++ b/ui/text/text_entity.h @@ -222,6 +222,7 @@ struct TextForMimeData { return *this; } + static TextForMimeData WithExpandedLinks(const TextWithEntities &text); static TextForMimeData Rich(TextWithEntities &&rich) { auto result = TextForMimeData(); result.expanded = rich.text;