Skip to content

Commit 5fbb38c

Browse files
committedJan 16, 2025
Edit links with preserved formatting.
1 parent e052a8b commit 5fbb38c

File tree

2 files changed

+61
-13
lines changed

2 files changed

+61
-13
lines changed
 

‎ui/widgets/fields/input_field.cpp

+58-10
Original file line numberDiff line numberDiff line change
@@ -4260,11 +4260,25 @@ void InputField::editMarkdownLink(EditLinkSelection selection) {
42604260
return;
42614261
}
42624262
const auto data = selectionEditLinkData(selection);
4263-
_editLinkCallback(
4264-
selection,
4265-
getTextWithTagsPart(data.from, data.till).text,
4266-
data.link,
4267-
EditLinkAction::Edit);
4263+
auto text = getTextWithTagsPart(data.from, data.till);
4264+
for (auto i = text.tags.begin(); i != text.tags.end();) {
4265+
auto all = TextUtilities::SplitTags(i->id);
4266+
auto j = all.begin();
4267+
for (auto j = all.begin(); j != all.end();) {
4268+
if (IsValidMarkdownLink(*j)) {
4269+
j = all.erase(j);
4270+
} else {
4271+
++j;
4272+
}
4273+
}
4274+
if (all.empty()) {
4275+
i = text.tags.erase(i);
4276+
} else {
4277+
i->id = TextUtilities::JoinTag(all);
4278+
++i;
4279+
}
4280+
}
4281+
_editLinkCallback(selection, text, data.link, EditLinkAction::Edit);
42684282
}
42694283

42704284
void InputField::inputMethodEventInner(QInputMethodEvent *e) {
@@ -4745,22 +4759,56 @@ QString InputField::CustomEmojiEntityData(QStringView link) {
47454759

47464760
void InputField::commitMarkdownLinkEdit(
47474761
EditLinkSelection selection,
4748-
const QString &text,
4762+
const TextWithTags &textWithTags,
47494763
const QString &link) {
4750-
if (text.isEmpty()
4764+
if (textWithTags.text.isEmpty()
47514765
|| !IsValidMarkdownLink(link)
47524766
|| !_editLinkCallback) {
47534767
return;
47544768
}
4755-
_insertedTags.clear();
4756-
_insertedTags.push_back({ 0, int(text.size()), link });
4769+
auto prepared = PrepareForInsert(textWithTags);
4770+
{
4771+
auto from = 0;
4772+
const auto till = int(prepared.text.size());
4773+
auto &tags = prepared.tags;
4774+
auto i = tags.begin();
4775+
while (from < till) {
4776+
while (i != tags.end() && i->offset <= from) {
4777+
auto all = TextUtilities::SplitTags(i->id);
4778+
auto j = all.begin();
4779+
for (; j != all.end(); ++j) {
4780+
if (IsValidMarkdownLink(*j)) {
4781+
*j = link;
4782+
break;
4783+
}
4784+
}
4785+
if (j == all.end()) {
4786+
all.push_back(link);
4787+
}
4788+
i->id = TextUtilities::JoinTag(all);
4789+
from = i->offset + i->length;
4790+
++i;
4791+
}
4792+
const auto tagFrom = (i == tags.end())
4793+
? till
4794+
: i->offset;
4795+
if (from < tagFrom) {
4796+
i = tags.insert(i, { from, tagFrom - from, link });
4797+
from = tagFrom;
4798+
++i;
4799+
}
4800+
}
4801+
}
4802+
_insertedTags = prepared.tags;
4803+
_insertedTagsAreFromMime = false;
47574804

47584805
auto cursor = textCursor();
47594806
const auto editData = selectionEditLinkData(selection);
47604807
cursor.setPosition(editData.from);
47614808
cursor.setPosition(editData.till, QTextCursor::KeepAnchor);
47624809
auto format = _defaultCharFormat;
47634810
_insertedTagsAreFromMime = false;
4811+
const auto text = prepared.text;
47644812
cursor.insertText(
47654813
(editData.from == editData.till) ? (text + QChar(' ')) : text,
47664814
_defaultCharFormat);
@@ -5169,7 +5217,7 @@ void InputField::setPlaceholder(
51695217
void InputField::setEditLinkCallback(
51705218
Fn<bool(
51715219
EditLinkSelection selection,
5172-
QString text,
5220+
TextWithTags text,
51735221
QString link,
51745222
EditLinkAction action)> callback) {
51755223
_editLinkCallback = std::move(callback);

‎ui/widgets/fields/input_field.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ class InputField : public RpWidget {
229229
void setEditLinkCallback(
230230
Fn<bool(
231231
EditLinkSelection selection,
232-
QString text,
232+
TextWithTags text,
233233
QString link,
234234
EditLinkAction action)> callback);
235235
void setEditLanguageCallback(
@@ -256,7 +256,7 @@ class InputField : public RpWidget {
256256
const QString &customEmojiData);
257257
void commitMarkdownLinkEdit(
258258
EditLinkSelection selection,
259-
const QString &text,
259+
const TextWithTags &textWithTags,
260260
const QString &link);
261261
[[nodiscard]] static bool IsValidMarkdownLink(QStringView link);
262262
[[nodiscard]] static bool IsCustomEmojiLink(QStringView link);
@@ -541,7 +541,7 @@ class InputField : public RpWidget {
541541

542542
Fn<bool(
543543
EditLinkSelection selection,
544-
QString text,
544+
TextWithTags text,
545545
QString link,
546546
EditLinkAction action)> _editLinkCallback;
547547
Fn<void(QString now, Fn<void(QString)> save)> _editLanguageCallback;

0 commit comments

Comments
 (0)
Please sign in to comment.