Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 3 additions & 26 deletions ios/styles/BlockQuoteStyle.mm
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

@implementation BlockQuoteStyle {
EnrichedTextInputView *_input;
NSArray *_stylesToExclude;
}

+ (StyleType)getStyleType { return BlockQuote; }
Expand All @@ -16,6 +17,7 @@ + (BOOL)isParagraphStyle { return YES; }
- (instancetype)initWithInput:(id)input {
self = [super init];
_input = (EnrichedTextInputView *)input;
_stylesToExclude = @[ @(InlineCode), @(Mention), @(Link) ];
return self;
}

Expand Down Expand Up @@ -177,31 +179,6 @@ - (BOOL)anyOccurence:(NSRange)range {
];
}

// gets ranges that aren't link, mention or inline code
- (NSArray *)getProperColorRangesIn:(NSRange)range {
LinkStyle *linkStyle = _input->stylesDict[@([LinkStyle getStyleType])];
MentionStyle *mentionStyle = _input->stylesDict[@([MentionStyle getStyleType])];
InlineCodeStyle *codeStyle = _input->stylesDict[@([InlineCodeStyle getStyleType])];

NSMutableArray *newRanges = [[NSMutableArray alloc] init];
int lastRangeLocation = range.location;

for(int i = range.location; i < range.location + range.length; i++) {
NSRange currentRange = NSMakeRange(i, 1);
if([linkStyle detectStyle:currentRange] || [mentionStyle detectStyle:currentRange] || [codeStyle detectStyle:currentRange]) {
if(i - lastRangeLocation > 0) {
[newRanges addObject:[NSValue valueWithRange:NSMakeRange(lastRangeLocation, i - lastRangeLocation)]];
}
lastRangeLocation = i+1;
}
}
if(lastRangeLocation < range.location + range.length) {
[newRanges addObject:[NSValue valueWithRange:NSMakeRange(lastRangeLocation, range.location + range.length - lastRangeLocation)]];
}

return newRanges;
}

// general checkup correcting blockquote color
// since links, mentions and inline code affects coloring, the checkup gets done only outside of them
- (void)manageBlockquoteColor {
Expand All @@ -214,7 +191,7 @@ - (void)manageBlockquoteColor {
NSArray *paragraphs = [ParagraphsUtils getSeparateParagraphsRangesIn:_input->textView range:wholeRange];
for(NSValue *pValue in paragraphs) {
NSRange paragraphRange = [pValue rangeValue];
NSArray *properRanges = [self getProperColorRangesIn:paragraphRange];
NSArray *properRanges = [OccurenceUtils getRangesWithout:_stylesToExclude withInput:_input inRange:paragraphRange];

for(NSValue *value in properRanges) {
NSRange currRange = [value rangeValue];
Expand Down
48 changes: 27 additions & 21 deletions ios/styles/CodeBlockStyle.mm
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

@implementation CodeBlockStyle {
EnrichedTextInputView *_input;
NSArray *_stylesToExclude;
}

+ (StyleType)getStyleType { return CodeBlock; }
Expand All @@ -17,6 +18,7 @@ + (BOOL)isParagraphStyle { return YES; }
- (instancetype)initWithInput:(id)input {
self = [super init];
_input = (EnrichedTextInputView *)input;
_stylesToExclude = @[ @(InlineCode), @(Mention), @(Link) ];
return self;
}

Expand Down Expand Up @@ -180,28 +182,32 @@ - (void)manageCodeBlockFontAndColor {

for(NSValue *pValue in paragraphs) {
NSRange paragraphRange = [pValue rangeValue];
BOOL selfDetected = [self detectStyle:paragraphRange];
NSArray *properRanges = [OccurenceUtils getRangesWithout:_stylesToExclude withInput:_input inRange:paragraphRange];

[_input->textView.textStorage enumerateAttribute:NSFontAttributeName inRange:paragraphRange options:0
usingBlock:^(id _Nullable value, NSRange range, BOOL * _Nonnull stop) {
UIFont *currentFont = (UIFont *)value;
UIFont *newFont = nullptr;
for(NSValue *value in properRanges) {
NSRange currRange = [value rangeValue];
BOOL selfDetected = [self detectStyle:currRange];

BOOL isCodeFont = [[currentFont familyName] isEqualToString:[[_input->config monospacedFont] familyName]];

if (isCodeFont && !selfDetected) {
newFont = [[[_input->config primaryFont] withFontTraits:currentFont] setSize:currentFont.pointSize];
} else if (!isCodeFont && selfDetected) {
newFont = [[[_input->config monospacedFont] withFontTraits:currentFont] setSize:currentFont.pointSize];
}
[_input->textView.textStorage enumerateAttribute:NSFontAttributeName inRange:currRange options:0
usingBlock:^(id _Nullable value, NSRange range, BOOL * _Nonnull stop) {
UIFont *currentFont = (UIFont *)value;
UIFont *newFont = nullptr;

BOOL isCodeFont = [[currentFont familyName] isEqualToString:[[_input->config monospacedFont] familyName]];

if (isCodeFont && !selfDetected) {
newFont = [[[_input->config primaryFont] withFontTraits:currentFont] setSize:currentFont.pointSize];
} else if (!isCodeFont && selfDetected) {
newFont = [[[_input->config monospacedFont] withFontTraits:currentFont] setSize:currentFont.pointSize];
}

if (newFont != nullptr) {
[_input->textView.textStorage addAttribute:NSFontAttributeName value:newFont range:range];
}
}];

if (newFont != nullptr) {
[_input->textView.textStorage addAttribute:NSFontAttributeName value:newFont range:range];
}
}];

[_input->textView.textStorage enumerateAttribute:NSForegroundColorAttributeName inRange:paragraphRange options:0
usingBlock:^(id _Nullable value, NSRange range, BOOL * _Nonnull stop) {
[_input->textView.textStorage enumerateAttribute:NSForegroundColorAttributeName inRange:currRange options:0
usingBlock:^(id _Nullable value, NSRange range, BOOL * _Nonnull stop) {
UIColor *newColor = nullptr;
BOOL colorApplied = [(UIColor *)value isEqualToColor:[_input->config codeBlockFgColor]];

Expand All @@ -214,8 +220,8 @@ - (void)manageCodeBlockFontAndColor {
if(newColor != nullptr) {
[_input->textView.textStorage addAttribute:NSForegroundColorAttributeName value:newColor range:range];
}
}
];
}];
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions ios/styles/LinkStyle.mm
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ - (NSRange)getFullLinkRangeAt:(NSUInteger)location {
}
}

[_input->textView.textStorage
NSString *manualUrl = [_input->textView.textStorage
attribute:ManualLinkAttributeName
atIndex:searchLocation
longestEffectiveRange: &manualLinkRange
Expand All @@ -239,7 +239,7 @@ - (NSRange)getFullLinkRangeAt:(NSUInteger)location {
inRange:inputRange
];

return manualLinkRange.length == 0 ? automaticLinkRange : manualLinkRange;
return manualUrl == nil ? automaticLinkRange : manualLinkRange;
Comment thread
kacperzolkiewski marked this conversation as resolved.
Outdated
}

- (void)manageLinkTypingAttributes {
Expand Down
2 changes: 2 additions & 0 deletions ios/utils/OccurenceUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,6 @@
withInput:(EnrichedTextInputView* _Nonnull)input
inRange:(NSRange)range
withCondition:(BOOL (NS_NOESCAPE ^_Nonnull)(id _Nullable value, NSRange range))condition;
+ (NSArray *_Nonnull)getRangesWithout:(NSArray<NSNumber *> *_Nonnull)types withInput:(EnrichedTextInputView* _Nonnull)input
Comment thread
kacperzolkiewski marked this conversation as resolved.
Outdated
inRange:(NSRange)range;
@end
45 changes: 45 additions & 0 deletions ios/utils/OccurenceUtils.mm
Original file line number Diff line number Diff line change
Expand Up @@ -152,4 +152,49 @@ + (BOOL)anyMultiple
return occurences;
}

+ (NSArray *_Nonnull)getRangesWithout:(NSArray<NSNumber *> *_Nonnull)types withInput:(EnrichedTextInputView* _Nonnull)input
Comment thread
kacperzolkiewski marked this conversation as resolved.
Outdated
inRange:(NSRange)range
{
NSMutableArray<id> *activeStyleObjects = [[NSMutableArray alloc] init];
for(NSNumber *type in types) {
id<BaseStyleProtocol> styleClass = input->stylesDict[type];
[activeStyleObjects addObject:styleClass];
}

if (activeStyleObjects.count == 0) {
return @[[NSValue valueWithRange:range]];
}

NSMutableArray<NSValue *> *newRanges = [[NSMutableArray alloc] init];
NSUInteger lastRangeLocation = range.location;
NSUInteger endLocation = range.location + range.length;

for (NSUInteger i = range.location; i < endLocation; i++) {
NSRange currentRange = NSMakeRange(i, 1);
BOOL forbiddenStyleFound = NO;

for (id style in activeStyleObjects) {
if ([style detectStyle:currentRange]) {
forbiddenStyleFound = YES;
break;
}
}

if (forbiddenStyleFound) {
if (i > lastRangeLocation) {
NSRange cleanRange = NSMakeRange(lastRangeLocation, i - lastRangeLocation);
[newRanges addObject:[NSValue valueWithRange:cleanRange]];
}
lastRangeLocation = i + 1;
}
}

if (lastRangeLocation < endLocation) {
NSRange remainingRange = NSMakeRange(lastRangeLocation, endLocation - lastRangeLocation);
[newRanges addObject:[NSValue valueWithRange:remainingRange]];
}

return newRanges;
}

@end
Loading