@@ -4260,11 +4260,25 @@ void InputField::editMarkdownLink(EditLinkSelection selection) {
4260
4260
return ;
4261
4261
}
4262
4262
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);
4268
4282
}
4269
4283
4270
4284
void InputField::inputMethodEventInner (QInputMethodEvent *e) {
@@ -4745,22 +4759,56 @@ QString InputField::CustomEmojiEntityData(QStringView link) {
4745
4759
4746
4760
void InputField::commitMarkdownLinkEdit (
4747
4761
EditLinkSelection selection,
4748
- const QString &text ,
4762
+ const TextWithTags &textWithTags ,
4749
4763
const QString &link ) {
4750
- if (text.isEmpty ()
4764
+ if (textWithTags. text .isEmpty ()
4751
4765
|| !IsValidMarkdownLink (link )
4752
4766
|| !_editLinkCallback) {
4753
4767
return ;
4754
4768
}
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 ;
4757
4804
4758
4805
auto cursor = textCursor ();
4759
4806
const auto editData = selectionEditLinkData (selection);
4760
4807
cursor.setPosition (editData.from );
4761
4808
cursor.setPosition (editData.till , QTextCursor::KeepAnchor);
4762
4809
auto format = _defaultCharFormat;
4763
4810
_insertedTagsAreFromMime = false ;
4811
+ const auto text = prepared.text ;
4764
4812
cursor.insertText (
4765
4813
(editData.from == editData.till ) ? (text + QChar (' ' )) : text,
4766
4814
_defaultCharFormat);
@@ -5169,7 +5217,7 @@ void InputField::setPlaceholder(
5169
5217
void InputField::setEditLinkCallback (
5170
5218
Fn<bool (
5171
5219
EditLinkSelection selection,
5172
- QString text,
5220
+ TextWithTags text,
5173
5221
QString link ,
5174
5222
EditLinkAction action)> callback) {
5175
5223
_editLinkCallback = std::move (callback);
0 commit comments