Skip to content

Commit 89ca463

Browse files
authored
fix: iOS links (#272)
<!-- Thanks for submitting a pull request! We appreciate you spending the time to work on these changes. Please follow the template so that the reviewers can easily understand what the code changes affect --> # Summary Firstly, it turns out that something in the internal implementation of `attribute:atIndex:longestEffectiveRange:inRange` has been changed and if no attribute has been found, longest effective range is set to whole range we are searching in. This resulted in links wrongly returning that their range is whole input and it had implications in fonts and foreground colors randomly breaking when applying links. Secondly, improved detecting links. The detection made after updating an automatic link didn't take recently emitted link into consideration and ran the event even if the automatic link wasn't updated - resulting in a double emit. The text when applying link was also double emitted because `LinkStyle's` `addLink` redundantly called `anyTextMayHaveBeenModified` ## Test Plan - To check the first issue, apply a manual link via link editor while having some inline code and blockquotes in the input. See that their styling is stil intact. - For the second one just add an automatic link and see no double emitting in console. ## Screenshots / Videos -- ## Compatibility | OS | Implemented | | ------- | :---------: | | iOS | ✅ | | Android | ❌(doesn't apply) |
1 parent 8fc6472 commit 89ca463

2 files changed

Lines changed: 14 additions & 10 deletions

File tree

ios/EnrichedTextInputView.mm

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -711,9 +711,6 @@ - (void)tryUpdatingActiveStyles {
711711
if(detectedLinkData != nullptr) {
712712
// emit onLinkeDetected event
713713
[self emitOnLinkDetectedEvent:detectedLinkData.text url:detectedLinkData.url range:detectedLinkRange];
714-
715-
_recentlyActiveLinkData = detectedLinkData;
716-
_recentlyActiveLinkRange = detectedLinkRange;
717714
}
718715

719716
if(detectedMentionParams != nullptr) {
@@ -812,6 +809,13 @@ - (void)setValue:(NSString *)value {
812809
- (void)emitOnLinkDetectedEvent:(NSString *)text url:(NSString *)url range:(NSRange)range {
813810
auto emitter = [self getEventEmitter];
814811
if(emitter != nullptr) {
812+
// update recently active link info
813+
LinkData *newLinkData = [[LinkData alloc] init];
814+
newLinkData.text = text;
815+
newLinkData.url = url;
816+
_recentlyActiveLinkData = newLinkData;
817+
_recentlyActiveLinkRange = range;
818+
815819
emitter->onLinkDetected({
816820
.text = [text toCppString],
817821
.url = [url toCppString],

ios/styles/LinkStyle.mm

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,6 @@ - (void)addLink:(NSString*)text url:(NSString*)url range:(NSRange)range manual:(
167167
}
168168

169169
[self manageLinkTypingAttributes];
170-
[_input anyTextMayHaveBeenModified];
171170
}
172171

173172
// get exact link data at the given location if it exists
@@ -224,20 +223,22 @@ - (NSRange)getFullLinkRangeAt:(NSUInteger)location {
224223
}
225224
}
226225

227-
[_input->textView.textStorage
226+
NSString *manualLink = [_input->textView.textStorage
228227
attribute:ManualLinkAttributeName
229228
atIndex:searchLocation
230229
longestEffectiveRange: &manualLinkRange
231230
inRange:inputRange
232231
];
233-
[_input->textView.textStorage
232+
NSString *automaticLink = [_input->textView.textStorage
234233
attribute:AutomaticLinkAttributeName
235234
atIndex:searchLocation
236235
longestEffectiveRange: &automaticLinkRange
237236
inRange:inputRange
238237
];
239238

240-
return manualLinkRange.length == 0 ? automaticLinkRange : manualLinkRange;
239+
return manualLink == nullptr
240+
? automaticLink == nullptr ? NSMakeRange(0, 0) : automaticLinkRange
241+
: manualLinkRange;
241242
}
242243

243244
- (void)manageLinkTypingAttributes {
@@ -349,10 +350,9 @@ - (void)handleAutomaticLinks:(NSString *)word inRange:(NSRange)wordRange {
349350
}
350351
if(addStyle) {
351352
[self addLink:word url:regexPassedUrl range:wordRange manual:NO];
353+
// emit onLinkDetected if style was added
354+
[_input emitOnLinkDetectedEvent:word url:regexPassedUrl range:wordRange];
352355
}
353-
354-
// emit onLinkDetected
355-
[_input emitOnLinkDetectedEvent:word url:regexPassedUrl range:wordRange];
356356
}
357357
}
358358

0 commit comments

Comments
 (0)