Fix parsing links with markup to entities.

This commit is contained in:
John Preston 2021-06-22 19:04:29 +04:00
parent 2d71162f4a
commit 0028a534d2

View file

@ -3167,7 +3167,8 @@ void String::enumerateText(TextSelection selection, AppendPartCallback appendPar
if (rangeTo > rangeFrom) { // handle click handler if (rangeTo > rangeFrom) { // handle click handler
QStringRef r = _text.midRef(rangeFrom, rangeTo - rangeFrom); QStringRef r = _text.midRef(rangeFrom, rangeTo - rangeFrom);
if (lnkFrom != rangeFrom || blockFrom != rangeTo) { if (lnkFrom != rangeFrom || blockFrom != rangeTo) {
appendPartCallback(r); // Ignore links that are partially copied.
clickHandlerFinishCallback(r, nullptr);
} else { } else {
clickHandlerFinishCallback(r, _links.at(lnkIndex - 1)); clickHandlerFinishCallback(r, _links.at(lnkIndex - 1));
} }
@ -3192,12 +3193,10 @@ void String::enumerateText(TextSelection selection, AppendPartCallback appendPar
if ((*i)->type() == TextBlockTSkip) continue; if ((*i)->type() == TextBlockTSkip) continue;
if (!blockLnkIndex) { auto rangeFrom = qMax(selection.from, blockFrom);
auto rangeFrom = qMax(selection.from, blockFrom); auto rangeTo = qMin(selection.to, uint16(blockFrom + countBlockLength(i, e)));
auto rangeTo = qMin(selection.to, uint16(blockFrom + countBlockLength(i, e))); if (rangeTo > rangeFrom) {
if (rangeTo > rangeFrom) { appendPartCallback(_text.midRef(rangeFrom, rangeTo - rangeFrom));
appendPartCallback(_text.midRef(rangeFrom, rangeTo - rangeFrom));
}
} }
} }
} }
@ -3270,23 +3269,21 @@ TextForMimeData String::toText(
linkStart = result.rich.text.size(); linkStart = result.rich.text.size();
}; };
const auto clickHandlerFinishCallback = [&]( const auto clickHandlerFinishCallback = [&](
const QStringRef &part, const QStringRef &inText,
const ClickHandlerPtr &handler) { const ClickHandlerPtr &handler) {
if (!handler || (!composeExpanded && !composeEntities)) {
return;
}
const auto entity = handler->getTextEntity(); const auto entity = handler->getTextEntity();
const auto plainUrl = (entity.type == EntityType::Url) const auto plainUrl = (entity.type == EntityType::Url)
|| (entity.type == EntityType::Email); || (entity.type == EntityType::Email);
const auto full = plainUrl const auto full = plainUrl
? entity.data.midRef(0, entity.data.size()) ? entity.data.midRef(0, entity.data.size())
: part; : inText;
result.rich.text.append(full);
if (!composeExpanded && !composeEntities) {
return;
}
const auto customTextLink = (entity.type == EntityType::CustomUrl); const auto customTextLink = (entity.type == EntityType::CustomUrl);
const auto internalLink = customTextLink const auto internalLink = customTextLink
&& entity.data.startsWith(qstr("internal:")); && entity.data.startsWith(qstr("internal:"));
if (composeExpanded) { if (composeExpanded) {
result.expanded.append(full);
const auto sameAsTextLink = customTextLink const auto sameAsTextLink = customTextLink
&& (entity.data && (entity.data
== UrlClickHandler::EncodeForOpening(full.toString())); == UrlClickHandler::EncodeForOpening(full.toString()));
@ -3299,7 +3296,7 @@ TextForMimeData String::toText(
insertEntity({ insertEntity({
entity.type, entity.type,
linkStart, linkStart,
full.size(), (result.rich.text.size() - linkStart),
plainUrl ? QString() : entity.data }); plainUrl ? QString() : entity.data });
} }
}; };
@ -3317,6 +3314,21 @@ TextForMimeData String::toText(
clickHandlerFinishCallback, clickHandlerFinishCallback,
flagsChangeCallback); flagsChangeCallback);
if (composeEntities) {
const auto proj = [](const EntityInText &entity) {
const auto type = entity.type();
const auto isUrl = (type == EntityType::Url)
|| (type == EntityType::CustomUrl)
|| (type == EntityType::BotCommand)
|| (type == EntityType::Mention)
|| (type == EntityType::MentionName)
|| (type == EntityType::Hashtag)
|| (type == EntityType::Cashtag);
return std::pair{ entity.offset(), isUrl ? 0 : 1 };
};
ranges::sort(result.rich.entities, std::less<>(), proj);
}
return result; return result;
} }