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

@@ -12,6 +12,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "ui/emoji_config.h"
#include "emoji_suggestions_data.h"
#include "chat_helpers/emoji_suggestions_helper.h"
#include "chat_helpers/message_field.h" // ConvertTextTagsToEntities
#include "window/themes/window_theme.h"
#include "lang/lang_keys.h"
#include "data/data_user.h"
@@ -759,6 +760,33 @@ struct FormattingAction {
};
QString ExpandCustomLinks(const TextWithTags &text) {
const auto entities = ConvertTextTagsToEntities(text.tags);
auto &&urls = ranges::view::all(
entities
) | ranges::view::filter([](const EntityInText &entity) {
return entity.type() == EntityType::CustomUrl;
});
const auto &original = text.text;
if (urls.begin() == urls.end()) {
return original;
}
auto result = QString();
auto offset = 0;
for (const auto &entity : urls) {
const auto till = entity.offset() + entity.length();
if (till > offset) {
result.append(original.midRef(offset, till - offset));
}
result.append(qstr(" (")).append(entity.data()).append(')');
offset = till;
}
if (original.size() > offset) {
result.append(original.midRef(offset));
}
return result;
}
} // namespace
const QString InputField::kTagBold = qsl("**");
@@ -2309,13 +2337,16 @@ QMimeData *InputField::createMimeDataFromSelectionInner() const {
const auto end = cursor.selectionEnd();
if (end > start) {
auto textWithTags = getTextWithTagsPart(start, end);
result->setText(textWithTags.text);
result->setText(ExpandCustomLinks(textWithTags));
if (!textWithTags.tags.isEmpty()) {
if (_tagMimeProcessor) {
for (auto &tag : textWithTags.tags) {
tag.id = _tagMimeProcessor->mimeTagFromTag(tag.id);
}
}
result->setData(
TextUtilities::TagsTextMimeType(),
textWithTags.text.toUtf8());
result->setData(
TextUtilities::TagsMimeType(),
TextUtilities::SerializeTags(textWithTags.tags));
@@ -3368,21 +3399,27 @@ void InputField::insertFromMimeDataInner(const QMimeData *source) {
&& _mimeDataHook(source, MimeAction::Insert)) {
return;
}
auto mime = TextUtilities::TagsMimeType();
auto text = source->text();
if (source->hasFormat(mime)) {
auto tagsData = source->data(mime);
const auto text = [&] {
const auto textMime = TextUtilities::TagsTextMimeType();
const auto tagsMime = TextUtilities::TagsMimeType();
if (!source->hasFormat(textMime) || !source->hasFormat(tagsMime)) {
_insertedTags.clear();
return source->text();
}
auto result = QString::fromUtf8(source->data(textMime));
_insertedTags = TextUtilities::DeserializeTags(
tagsData,
text.size());
source->data(tagsMime),
result.size());
_insertedTagsAreFromMime = true;
} else {
_insertedTags.clear();
}
return result;
}();
auto cursor = textCursor();
_realInsertPosition = cursor.selectionStart();
_realCharsAdded = text.size();
_inner->QTextEdit::insertFromMimeData(source);
if (_realCharsAdded > 0) {
cursor.insertFragment(QTextDocumentFragment::fromPlainText(text));
}
ensureCursorVisible();
if (!_inDrop) {
_insertedTags.clear();
_realInsertPosition = -1;

View File

@@ -8,6 +8,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "ui/widgets/labels.h"
#include "ui/widgets/popup_menu.h"
#include "chat_helpers/message_field.h" // SetClipboardText/MimeDataFromText
#include "mainwindow.h"
#include "lang/lang_keys.h"
@@ -569,12 +570,12 @@ void FlatLabel::showContextMenu(QContextMenuEvent *e, ContextMenuReason reason)
void FlatLabel::onCopySelectedText() {
const auto selection = _selection.empty() ? (_contextMenu ? _savedSelection : _selection) : _selection;
if (!selection.empty()) {
QApplication::clipboard()->setText(_text.toString(selection, ExpandLinksAll));
SetClipboardText(_text.toTextForMimeData(selection));
}
}
void FlatLabel::onCopyContextText() {
QApplication::clipboard()->setText(_text.toString(AllTextSelection, ExpandLinksAll));
SetClipboardText(_text.toTextForMimeData());
}
void FlatLabel::onTouchSelect() {
@@ -599,18 +600,18 @@ void FlatLabel::onExecuteDrag() {
}
}
ClickHandlerPtr pressedHandler = ClickHandler::getPressed();
QString selectedText;
if (uponSelected) {
selectedText = _text.toString(_selection, ExpandLinksAll);
} else if (pressedHandler) {
selectedText = pressedHandler->dragText();
}
if (!selectedText.isEmpty()) {
auto mimeData = new QMimeData();
mimeData->setText(selectedText);
const auto pressedHandler = ClickHandler::getPressed();
const auto selectedText = [&] {
if (uponSelected) {
return _text.toTextForMimeData(_selection);
} else if (pressedHandler) {
return TextForMimeData::Simple(pressedHandler->dragText());
}
return TextForMimeData();
}();
if (auto mimeData = MimeDataFromText(selectedText)) {
auto drag = new QDrag(App::wnd());
drag->setMimeData(mimeData);
drag->setMimeData(mimeData.release());
drag->exec(Qt::CopyAction);
// We don't receive mouseReleaseEvent when drag is finished.