2
0
mirror of https://github.com/telegramdesktop/tdesktop synced 2025-08-31 06:26:18 +00:00

Copy text with expanded links only to external.

Paste valid custom links in message field if copied from messages.
This commit is contained in:
John Preston
2019-04-08 19:10:06 +04:00
parent 0f0c3b7461
commit b5be6df5e2
64 changed files with 772 additions and 647 deletions

View File

@@ -50,12 +50,6 @@ bool ClickHandler::setActive(const ClickHandlerPtr &p, ClickHandlerHost *host) {
return true;
}
TextWithEntities ClickHandler::getExpandedLinkTextWithEntities(int entityOffset, const QStringRef &textPart) const {
return { QString(), EntitiesInText() };
}
TextWithEntities ClickHandler::simpleTextWithEntity(const EntityInText &entity) const {
TextWithEntities result;
result.entities.push_back(entity);
return result;
auto ClickHandler::getTextEntity() const -> TextEntity {
return { EntityType::Invalid };
}

View File

@@ -15,11 +15,6 @@ struct ClickContext {
QVariant other;
};
enum ExpandLinksMode {
ExpandLinksShortened,
ExpandLinksAll,
};
class ClickHandlerHost {
protected:
virtual void clickHandlerActiveChanged(const ClickHandlerPtr &action, bool active) {
@@ -31,8 +26,7 @@ protected:
};
class EntityInText;
struct TextWithEntities;
enum class EntityType;
class ClickHandler {
public:
virtual ~ClickHandler() {
@@ -59,9 +53,11 @@ public:
}
// Entities in text support.
// This method returns empty string if just textPart should be used (nothing to expand).
virtual TextWithEntities getExpandedLinkTextWithEntities(int entityOffset, const QStringRef &textPart) const;
struct TextEntity {
EntityType type = EntityType();
QString data;
};
virtual TextEntity getTextEntity() const;
// This method should be called on mouse over a click handler.
// It returns true if the active handler was changed or false otherwise.
@@ -146,11 +142,6 @@ public:
}
}
protected:
// For click handlers like mention or hashtag in getExpandedLinkTextWithEntities()
// we return just an empty string ("use original string part") with single entity.
TextWithEntities simpleTextWithEntity(const EntityInText &entity) const;
private:
static NeverFreedPointer<ClickHandlerPtr> _active;
static NeverFreedPointer<ClickHandlerPtr> _pressed;

View File

@@ -133,15 +133,11 @@ void UrlClickHandler::Open(QString url, QVariant context) {
}
}
TextWithEntities UrlClickHandler::getExpandedLinkTextWithEntities(int entityOffset, const QStringRef &textPart) const {
auto result = TextWithEntities();
result.text = _originalUrl;
const auto entityLength = _originalUrl.size();
const auto entityType = isEmail(_originalUrl)
? EntityInTextEmail
: EntityInTextUrl;
result.entities.push_back({ entityType, entityOffset, entityLength });
return result;
auto UrlClickHandler::getTextEntity() const -> TextEntity {
const auto type = isEmail(_originalUrl)
? EntityType::Email
: EntityType::Url;
return { type, _originalUrl };
}
void HiddenUrlClickHandler::Open(QString url, QVariant context) {
@@ -200,8 +196,8 @@ void BotGameUrlClickHandler::onClick(ClickContext context) const {
}
}
TextWithEntities HiddenUrlClickHandler::getExpandedLinkTextWithEntities(int entityOffset, const QStringRef &textPart) const {
return simpleTextWithEntity({ EntityInTextCustomUrl, entityOffset, textPart.size(), url() });
auto HiddenUrlClickHandler::getTextEntity() const -> TextEntity {
return { EntityType::CustomUrl, url() };
}
QString MentionClickHandler::copyToClipboardContextItemText() const {
@@ -215,8 +211,8 @@ void MentionClickHandler::onClick(ClickContext context) const {
}
}
TextWithEntities MentionClickHandler::getExpandedLinkTextWithEntities(int entityOffset, const QStringRef &textPart) const {
return simpleTextWithEntity({ EntityInTextMention, entityOffset, textPart.size() });
auto MentionClickHandler::getTextEntity() const -> TextEntity {
return { EntityType::Mention };
}
void MentionNameClickHandler::onClick(ClickContext context) const {
@@ -228,9 +224,9 @@ void MentionNameClickHandler::onClick(ClickContext context) const {
}
}
TextWithEntities MentionNameClickHandler::getExpandedLinkTextWithEntities(int entityOffset, const QStringRef &textPart) const {
auto MentionNameClickHandler::getTextEntity() const -> TextEntity {
auto data = QString::number(_userId) + '.' + QString::number(_accessHash);
return simpleTextWithEntity({ EntityInTextMentionName, entityOffset, textPart.size(), data });
return { EntityType::MentionName, data };
}
QString MentionNameClickHandler::tooltip() const {
@@ -254,8 +250,8 @@ void HashtagClickHandler::onClick(ClickContext context) const {
}
}
TextWithEntities HashtagClickHandler::getExpandedLinkTextWithEntities(int entityOffset, const QStringRef &textPart) const {
return simpleTextWithEntity({ EntityInTextHashtag, entityOffset, textPart.size() });
auto HashtagClickHandler::getTextEntity() const -> TextEntity {
return { EntityType::Hashtag };
}
QString CashtagClickHandler::copyToClipboardContextItemText() const {
@@ -269,10 +265,8 @@ void CashtagClickHandler::onClick(ClickContext context) const {
}
}
TextWithEntities CashtagClickHandler::getExpandedLinkTextWithEntities(
int entityOffset,
const QStringRef &textPart) const {
return simpleTextWithEntity({ EntityInTextCashtag, entityOffset, textPart.size() });
auto CashtagClickHandler::getTextEntity() const -> TextEntity {
return { EntityType::Cashtag };
}
PeerData *BotCommandClickHandler::_peer = nullptr;
@@ -304,6 +298,6 @@ void BotCommandClickHandler::onClick(ClickContext context) const {
}
}
TextWithEntities BotCommandClickHandler::getExpandedLinkTextWithEntities(int entityOffset, const QStringRef &textPart) const {
return simpleTextWithEntity({ EntityInTextBotCommand, entityOffset, textPart.size() });
auto BotCommandClickHandler::getTextEntity() const -> TextEntity {
return { EntityType::BotCommand };
}

View File

@@ -47,9 +47,7 @@ public:
return url();
}
TextWithEntities getExpandedLinkTextWithEntities(
int entityOffset,
const QStringRef &textPart) const override;
TextEntity getTextEntity() const override;
static void Open(QString url, QVariant context = {});
void onClick(ClickContext context) const override {
@@ -96,9 +94,7 @@ public:
}
}
TextWithEntities getExpandedLinkTextWithEntities(
int entityOffset,
const QStringRef &textPart) const override;
TextEntity getTextEntity() const override;
};
@@ -128,9 +124,7 @@ public:
QString copyToClipboardContextItemText() const override;
TextWithEntities getExpandedLinkTextWithEntities(
int entityOffset,
const QStringRef &textPart) const override;
TextEntity getTextEntity() const override;
protected:
QString url() const override {
@@ -152,9 +146,7 @@ public:
void onClick(ClickContext context) const override;
TextWithEntities getExpandedLinkTextWithEntities(
int entityOffset,
const QStringRef &textPart) const override;
TextEntity getTextEntity() const override;
QString tooltip() const override;
@@ -178,9 +170,7 @@ public:
QString copyToClipboardContextItemText() const override;
TextWithEntities getExpandedLinkTextWithEntities(
int entityOffset,
const QStringRef &textPart) const override;
TextEntity getTextEntity() const override;
protected:
QString url() const override {
@@ -205,9 +195,7 @@ public:
QString copyToClipboardContextItemText() const override;
TextWithEntities getExpandedLinkTextWithEntities(
int entityOffset,
const QStringRef &textPart) const override;
TextEntity getTextEntity() const override;
protected:
QString url() const override {
@@ -239,9 +227,7 @@ public:
_bot = bot;
}
TextWithEntities getExpandedLinkTextWithEntities(
int entityOffset,
const QStringRef &textPart) const override;
TextEntity getTextEntity() const override;
protected:
QString url() const override {