Skip to content

Commit d4fe65d

Browse files
committed
fix: fix automatic detection just after toggle
1 parent 644d819 commit d4fe65d

2 files changed

Lines changed: 32 additions & 23 deletions

File tree

ios/styles/LinkStyle.mm

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,15 @@ - (void)removeAttributes:(NSRange)range {
4141
[_editor->textView.textStorage removeAttribute:ManualLinkAttributeName range:linkRange];
4242
}
4343
[_editor->textView.textStorage endEditing];
44+
// remove typing attributes as well
45+
NSMutableDictionary *newTypingAttrs = [_editor->textView.typingAttributes mutableCopy];
46+
[newTypingAttrs removeObjectForKey:NSLinkAttributeName];
47+
_editor->textView.typingAttributes = newTypingAttrs;
4448
}
4549

4650
// used for conflicts or removeLink ref method, we have to remove the whole link
4751
- (void)removeTypingAttributes {
48-
NSRange linkRange = [self getFullLinkRangeAt:_editor->currentSelection.location];
52+
NSRange linkRange = [self getFullLinkRangeAt:_editor->textView.selectedRange.location];
4953
[_editor->textView.textStorage beginEditing];
5054
[_editor->textView.textStorage removeAttribute:NSLinkAttributeName range:linkRange];
5155
[_editor->textView.textStorage removeAttribute:ManualLinkAttributeName range:linkRange];
@@ -60,9 +64,9 @@ - (BOOL)styleCondition:(id _Nullable)value :(NSRange)range {
6064
- (BOOL)detectStyle:(NSRange)range {
6165
if(range.length >= 1) {
6266
BOOL onlyLinks = [OccurenceUtils detect:NSLinkAttributeName withEditor:_editor inRange:range
63-
withCondition: ^BOOL(id _Nullable value, NSRange range) {
64-
return [self styleCondition:value :range];
65-
}
67+
withCondition: ^BOOL(id _Nullable value, NSRange range) {
68+
return [self styleCondition:value :range];
69+
}
6670
];
6771
return onlyLinks ? [self isSingleLinkIn:range] : NO;
6872
} else {
@@ -102,7 +106,7 @@ - (void)addLink:(NSString*)text url:(NSString*)url range:(NSRange)range manual:(
102106
// insert link
103107
[TextInsertionUtils insertText:text inView:_editor->textView at:range.location additionalAttributes:newAttrs];
104108
} else if([currentText isEqualToString:text]) {
105-
// apply link attributes and change selection
109+
// apply link attributes
106110
[_editor->textView.textStorage addAttributes:newAttrs range:range];
107111
// TextInsertionUtils take care of the selection but here we have to manually set it behind the link
108112
// ONLY with manual links, automatic ones don't need the selection fix
@@ -176,13 +180,13 @@ - (void)manageLinkTypingAttributes {
176180
// manual link can be extended via typing attributes only if it's done from the inside
177181
// adding text before or after shouldn't be considered a link
178182
// that's why we remove typing attributes in these cases here
179-
if(_editor->currentSelection.length > 0) {
183+
if(_editor->textView.selectedRange.length > 0) {
180184
return;
181185
}
182186

183187
__block BOOL linkBefore = NO;
184-
if(_editor->currentSelection.location >= 1) {
185-
NSRange rangeBefore = NSMakeRange(_editor->currentSelection.location - 1, 1);
188+
if(_editor->textView.selectedRange.location >= 1) {
189+
NSRange rangeBefore = NSMakeRange(_editor->textView.selectedRange.location - 1, 1);
186190
[_editor->textView.textStorage enumerateAttribute:NSLinkAttributeName inRange:rangeBefore options:0
187191
usingBlock:^(id _Nullable value, NSRange range, BOOL * _Nonnull stop) {
188192
NSString *linkValue = (NSString *)value;
@@ -202,8 +206,8 @@ - (void)manageLinkTypingAttributes {
202206
}
203207

204208
__block BOOL linkAfter = NO;
205-
if(_editor->currentSelection.location < _editor->textView.textStorage.length) {
206-
NSRange rangeAfter = NSMakeRange(_editor->currentSelection.location, 1);
209+
if(_editor->textView.selectedRange.location < _editor->textView.textStorage.length) {
210+
NSRange rangeAfter = NSMakeRange(_editor->textView.selectedRange.location, 1);
207211
[_editor->textView.textStorage enumerateAttribute:NSLinkAttributeName inRange:rangeAfter options:0
208212
usingBlock:^(id _Nullable value, NSRange range, BOOL * _Nonnull stop) {
209213
NSString *linkValue = (NSString *)value;
@@ -223,12 +227,12 @@ - (void)manageLinkTypingAttributes {
223227
}
224228

225229
// handles automatic links, returns true if there were some changes and we possibly might need new style recalculations
226-
- (BOOL)handleAutomaticLinks:(NSString *)word inRange:(NSRange)wordRange {
230+
- (void)handleAutomaticLinks:(NSString *)word inRange:(NSRange)wordRange {
227231
InlineCodeStyle *inlineCodeStyle = [_editor->stylesDict objectForKey:@([InlineCodeStyle getStyleType])];
228232
//MentionStyle *mentionStyle = [[_editor->stylesDict objectForKey:@([MentionStyle getStyleType])];
229233

230234
if (inlineCodeStyle == nullptr /*|| mentionStyle == nullptr*/) {
231-
return NO;
235+
return;
232236
}
233237

234238
// firstly, we look for manual links within the word
@@ -257,17 +261,14 @@ - (BOOL)handleAutomaticLinks:(NSString *)word inRange:(NSRange)wordRange {
257261
BOOL manualLinkPresent = manualLinkMinIdx != -1 && manualLinkMaxIdx != -1;
258262
if (manualLinkPresent) {
259263
// this is a heuristic for refreshing manual links:
260-
// since links cannot be extended from the sides, but can be from the inside,
264+
// since manual links cannot be extended from the sides, but can be from the inside,
261265
// we update the attribute between the bounds of the already existing one
262266
// we do that only if the bounds are the same one link though
263267
if ([manualLinkMinValue isEqualToString:manualLinkMaxValue]) {
264268
NSRange newManualAttributeRange = NSMakeRange(manualLinkMinIdx, manualLinkMaxIdx - manualLinkMinIdx + 1);
265269
[_editor->textView.textStorage addAttribute:ManualLinkAttributeName value:manualLinkMinValue range:newManualAttributeRange];
266-
// manual link was extended so for the safety we check style updates
267-
return YES;
268270
}
269-
// manual link not extended, automatic link doesn't get recognized in there, no changes here
270-
return NO;
271+
return;
271272
}
272273

273274
BOOL anyAutomaticLinksFound = [self anyOccurence:wordRange];
@@ -279,7 +280,7 @@ - (BOOL)handleAutomaticLinks:(NSString *)word inRange:(NSRange)wordRange {
279280

280281
// we don't recognize links among inline code
281282
if ([inlineCodeStyle anyOccurence:wordRange]) {
282-
return NO;
283+
return;
283284
}
284285

285286
NSRegularExpression *fullRegex = [NSRegularExpression regularExpressionWithPattern:@"http(s)?:\\/\\/www\\.[-a-zA-Z0-9@:%._\\+~#=]{1,256}\\.[a-z]{2,6}\\b([-a-zA-Z0-9@:%_\\+.~#?&//=]*)"
@@ -311,15 +312,23 @@ - (BOOL)handleAutomaticLinks:(NSString *)word inRange:(NSRange)wordRange {
311312
} else if (anyAutomaticLinksFound) {
312313
// there was some automatic link yet it didn't pass any regex - needs to be removed
313314
[self removeAttributes:wordRange];
314-
return YES;
315315
}
316316

317317
if(regexPassedUrl != nullptr) {
318+
// emit onLinkDetected
318319
[_editor emitOnLinkDetectedEvent:word url:regexPassedUrl];
319-
return YES;
320+
321+
// fix typing attributes if we made automatic link while being inside of its detection range
322+
NSRange editorRange = _editor->textView.selectedRange;
323+
if(editorRange.length == 0 &&
324+
editorRange.location > wordRange.location &&
325+
editorRange.location < wordRange.location + wordRange.length
326+
) {
327+
NSMutableDictionary *newTypingAttrs = [_editor->textView.typingAttributes mutableCopy];
328+
newTypingAttrs[NSLinkAttributeName] = regexPassedUrl;
329+
_editor->textView.typingAttributes = newTypingAttrs;
330+
}
320331
}
321-
322-
return NO;
323332
}
324333

325334
// MARK: - Private non-standard methods

ios/utils/StyleHeaders.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,5 @@
2222
- (LinkData *)getCurrentLinkDataIn:(NSRange)range;
2323
- (NSRange)getFullLinkRangeAt:(NSUInteger)location;
2424
- (void)manageLinkTypingAttributes;
25-
- (BOOL)handleAutomaticLinks:(NSString *)word inRange:(NSRange)wordRange;
25+
- (void)handleAutomaticLinks:(NSString *)word inRange:(NSRange)wordRange;
2626
@end

0 commit comments

Comments
 (0)