Skip to content

Commit

Permalink
Allow formatter to detect if range contains custom link keys
Browse files Browse the repository at this point in the history
  • Loading branch information
tonisevener committed Jan 5, 2024
1 parent a2db26a commit 896acbe
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
NS_ASSUME_NONNULL_BEGIN

@interface WKSourceEditorFormatterLink : WKSourceEditorFormatter

- (BOOL)attributedString:(NSMutableAttributedString *)attributedString isSimpleLinkInRange:(NSRange)range;
- (BOOL)attributedString:(NSMutableAttributedString *)attributedString isLinkWithNestedLinkInRange:(NSRange)range;
@end

NS_ASSUME_NONNULL_END
78 changes: 78 additions & 0 deletions Components/Sources/ComponentsObjC/WKSourceEditorFormatterLink.m
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,84 @@ - (void)updateFonts:(WKSourceEditorFonts *)fonts inAttributedString:(NSMutableAt
// No special font handling needed
}

#pragma mark - Public

- (BOOL)attributedString:(NSMutableAttributedString *)attributedString isSimpleLinkInRange:(NSRange)range {
__block BOOL isContentKey = NO;
if (range.length == 0) {

if (attributedString.length > range.location) {
NSDictionary<NSAttributedStringKey,id> *attrs = [attributedString attributesAtIndex:range.location effectiveRange:nil];

if (attrs[WKSourceEditorCustomKeyContentLink] != nil) {
isContentKey = YES;
} else {
// Edge case, check previous character if we are up against closing markup
if (attrs[WKSourceEditorCustomKeyMarkupLink]) {
attrs = [attributedString attributesAtIndex:range.location - 1 effectiveRange:nil];
if (attrs[WKSourceEditorCustomKeyContentLink] != nil) {
isContentKey = YES;
}
}
}
}

} else {
__block NSRange unionRange = NSMakeRange(NSNotFound, 0);
[attributedString enumerateAttributesInRange:range options:nil usingBlock:^(NSDictionary<NSAttributedStringKey,id> * _Nonnull attrs, NSRange loopRange, BOOL * _Nonnull stop) {
if (attrs[WKSourceEditorCustomKeyContentLink] != nil) {
if (unionRange.location == NSNotFound) {
unionRange = loopRange;
} else {
unionRange = NSUnionRange(unionRange, loopRange);
}
stop = YES;
}
}];

if (NSEqualRanges(unionRange, range)) {
isContentKey = YES;
}
}

return isContentKey;
}

- (BOOL)attributedString:(NSMutableAttributedString *)attributedString isLinkWithNestedLinkInRange:(NSRange)range {

__block BOOL isKey = NO;
if (range.length == 0) {

if (attributedString.length > range.location) {
NSDictionary<NSAttributedStringKey,id> *attrs = [attributedString attributesAtIndex:range.location effectiveRange:nil];

if (attrs[WKSourceEditorCustomKeyMarkupAndContentLinkWithNestedLink] != nil) {
isKey = YES;
}
}

} else {
__block NSRange unionRange = NSMakeRange(NSNotFound, 0);
[attributedString enumerateAttributesInRange:range options:nil usingBlock:^(NSDictionary<NSAttributedStringKey,id> * _Nonnull attrs, NSRange loopRange, BOOL * _Nonnull stop) {
if (attrs[WKSourceEditorCustomKeyMarkupAndContentLinkWithNestedLink] != nil) {
if (unionRange.location == NSNotFound) {
unionRange = loopRange;
} else {
unionRange = NSUnionRange(unionRange, loopRange);
}
stop = YES;
}
}];

if (NSEqualRanges(unionRange, range)) {
isKey = YES;
}
}

return isKey;

}

#pragma mark - Private

- (NSArray *)linkWithNestedLinkRangesInString: (NSString *)string startingIndex: (NSUInteger)index {
Expand Down

0 comments on commit 896acbe

Please sign in to comment.