Skip to content

Commit 7fd08fd

Browse files
fix: applying codeblock styles
1 parent f3ce536 commit 7fd08fd

5 files changed

Lines changed: 79 additions & 49 deletions

File tree

ios/styles/BlockQuoteStyle.mm

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
@implementation BlockQuoteStyle {
99
EnrichedTextInputView *_input;
10+
NSArray *_stylesToExclude;
1011
}
1112

1213
+ (StyleType)getStyleType { return BlockQuote; }
@@ -16,6 +17,7 @@ + (BOOL)isParagraphStyle { return YES; }
1617
- (instancetype)initWithInput:(id)input {
1718
self = [super init];
1819
_input = (EnrichedTextInputView *)input;
20+
_stylesToExclude = @[ @(InlineCode), @(Mention), @(Link) ];
1921
return self;
2022
}
2123

@@ -177,31 +179,6 @@ - (BOOL)anyOccurence:(NSRange)range {
177179
];
178180
}
179181

180-
// gets ranges that aren't link, mention or inline code
181-
- (NSArray *)getProperColorRangesIn:(NSRange)range {
182-
LinkStyle *linkStyle = _input->stylesDict[@([LinkStyle getStyleType])];
183-
MentionStyle *mentionStyle = _input->stylesDict[@([MentionStyle getStyleType])];
184-
InlineCodeStyle *codeStyle = _input->stylesDict[@([InlineCodeStyle getStyleType])];
185-
186-
NSMutableArray *newRanges = [[NSMutableArray alloc] init];
187-
int lastRangeLocation = range.location;
188-
189-
for(int i = range.location; i < range.location + range.length; i++) {
190-
NSRange currentRange = NSMakeRange(i, 1);
191-
if([linkStyle detectStyle:currentRange] || [mentionStyle detectStyle:currentRange] || [codeStyle detectStyle:currentRange]) {
192-
if(i - lastRangeLocation > 0) {
193-
[newRanges addObject:[NSValue valueWithRange:NSMakeRange(lastRangeLocation, i - lastRangeLocation)]];
194-
}
195-
lastRangeLocation = i+1;
196-
}
197-
}
198-
if(lastRangeLocation < range.location + range.length) {
199-
[newRanges addObject:[NSValue valueWithRange:NSMakeRange(lastRangeLocation, range.location + range.length - lastRangeLocation)]];
200-
}
201-
202-
return newRanges;
203-
}
204-
205182
// general checkup correcting blockquote color
206183
// since links, mentions and inline code affects coloring, the checkup gets done only outside of them
207184
- (void)manageBlockquoteColor {
@@ -214,7 +191,7 @@ - (void)manageBlockquoteColor {
214191
NSArray *paragraphs = [ParagraphsUtils getSeparateParagraphsRangesIn:_input->textView range:wholeRange];
215192
for(NSValue *pValue in paragraphs) {
216193
NSRange paragraphRange = [pValue rangeValue];
217-
NSArray *properRanges = [self getProperColorRangesIn:paragraphRange];
194+
NSArray *properRanges = [OccurenceUtils getRangesWithout:_stylesToExclude withInput:_input inRange:paragraphRange];
218195

219196
for(NSValue *value in properRanges) {
220197
NSRange currRange = [value rangeValue];

ios/styles/CodeBlockStyle.mm

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
@implementation CodeBlockStyle {
1010
EnrichedTextInputView *_input;
11+
NSArray *_stylesToExclude;
1112
}
1213

1314
+ (StyleType)getStyleType { return CodeBlock; }
@@ -17,6 +18,7 @@ + (BOOL)isParagraphStyle { return YES; }
1718
- (instancetype)initWithInput:(id)input {
1819
self = [super init];
1920
_input = (EnrichedTextInputView *)input;
21+
_stylesToExclude = @[ @(InlineCode), @(Mention), @(Link) ];
2022
return self;
2123
}
2224

@@ -180,28 +182,32 @@ - (void)manageCodeBlockFontAndColor {
180182

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

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

190-
BOOL isCodeFont = [[currentFont familyName] isEqualToString:[[_input->config monospacedFont] familyName]];
191-
192-
if (isCodeFont && !selfDetected) {
193-
newFont = [[[_input->config primaryFont] withFontTraits:currentFont] setSize:currentFont.pointSize];
194-
} else if (!isCodeFont && selfDetected) {
195-
newFont = [[[_input->config monospacedFont] withFontTraits:currentFont] setSize:currentFont.pointSize];
196-
}
191+
[_input->textView.textStorage enumerateAttribute:NSFontAttributeName inRange:currRange options:0
192+
usingBlock:^(id _Nullable value, NSRange range, BOOL * _Nonnull stop) {
193+
UIFont *currentFont = (UIFont *)value;
194+
UIFont *newFont = nullptr;
195+
196+
BOOL isCodeFont = [[currentFont familyName] isEqualToString:[[_input->config monospacedFont] familyName]];
197+
198+
if (isCodeFont && !selfDetected) {
199+
newFont = [[[_input->config primaryFont] withFontTraits:currentFont] setSize:currentFont.pointSize];
200+
} else if (!isCodeFont && selfDetected) {
201+
newFont = [[[_input->config monospacedFont] withFontTraits:currentFont] setSize:currentFont.pointSize];
202+
}
203+
204+
if (newFont != nullptr) {
205+
[_input->textView.textStorage addAttribute:NSFontAttributeName value:newFont range:range];
206+
}
207+
}];
197208

198-
if (newFont != nullptr) {
199-
[_input->textView.textStorage addAttribute:NSFontAttributeName value:newFont range:range];
200-
}
201-
}];
202-
203-
[_input->textView.textStorage enumerateAttribute:NSForegroundColorAttributeName inRange:paragraphRange options:0
204-
usingBlock:^(id _Nullable value, NSRange range, BOOL * _Nonnull stop) {
209+
[_input->textView.textStorage enumerateAttribute:NSForegroundColorAttributeName inRange:currRange options:0
210+
usingBlock:^(id _Nullable value, NSRange range, BOOL * _Nonnull stop) {
205211
UIColor *newColor = nullptr;
206212
BOOL colorApplied = [(UIColor *)value isEqualToColor:[_input->config codeBlockFgColor]];
207213

@@ -214,8 +220,8 @@ - (void)manageCodeBlockFontAndColor {
214220
if(newColor != nullptr) {
215221
[_input->textView.textStorage addAttribute:NSForegroundColorAttributeName value:newColor range:range];
216222
}
217-
}
218-
];
223+
}];
224+
}
219225
}
220226
}
221227

ios/styles/LinkStyle.mm

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ - (NSRange)getFullLinkRangeAt:(NSUInteger)location {
226226
}
227227
}
228228

229-
[_input->textView.textStorage
229+
NSString *manualUrl = [_input->textView.textStorage
230230
attribute:ManualLinkAttributeName
231231
atIndex:searchLocation
232232
longestEffectiveRange: &manualLinkRange
@@ -239,7 +239,7 @@ - (NSRange)getFullLinkRangeAt:(NSUInteger)location {
239239
inRange:inputRange
240240
];
241241

242-
return manualLinkRange.length == 0 ? automaticLinkRange : manualLinkRange;
242+
return manualUrl == nil ? automaticLinkRange : manualLinkRange;
243243
}
244244

245245
- (void)manageLinkTypingAttributes {

ios/utils/OccurenceUtils.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,6 @@
4040
withInput:(EnrichedTextInputView* _Nonnull)input
4141
inRange:(NSRange)range
4242
withCondition:(BOOL (NS_NOESCAPE ^_Nonnull)(id _Nullable value, NSRange range))condition;
43+
+ (NSArray *_Nonnull)getRangesWithout:(NSArray<NSNumber *> *_Nonnull)types withInput:(EnrichedTextInputView* _Nonnull)input
44+
inRange:(NSRange)range;
4345
@end

ios/utils/OccurenceUtils.mm

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,4 +152,49 @@ + (BOOL)anyMultiple
152152
return occurences;
153153
}
154154

155+
+ (NSArray *_Nonnull)getRangesWithout:(NSArray<NSNumber *> *_Nonnull)types withInput:(EnrichedTextInputView* _Nonnull)input
156+
inRange:(NSRange)range
157+
{
158+
NSMutableArray<id> *activeStyleObjects = [[NSMutableArray alloc] init];
159+
for(NSNumber *type in types) {
160+
id<BaseStyleProtocol> styleClass = input->stylesDict[type];
161+
[activeStyleObjects addObject:styleClass];
162+
}
163+
164+
if (activeStyleObjects.count == 0) {
165+
return @[[NSValue valueWithRange:range]];
166+
}
167+
168+
NSMutableArray<NSValue *> *newRanges = [[NSMutableArray alloc] init];
169+
NSUInteger lastRangeLocation = range.location;
170+
NSUInteger endLocation = range.location + range.length;
171+
172+
for (NSUInteger i = range.location; i < endLocation; i++) {
173+
NSRange currentRange = NSMakeRange(i, 1);
174+
BOOL forbiddenStyleFound = NO;
175+
176+
for (id style in activeStyleObjects) {
177+
if ([style detectStyle:currentRange]) {
178+
forbiddenStyleFound = YES;
179+
break;
180+
}
181+
}
182+
183+
if (forbiddenStyleFound) {
184+
if (i > lastRangeLocation) {
185+
NSRange cleanRange = NSMakeRange(lastRangeLocation, i - lastRangeLocation);
186+
[newRanges addObject:[NSValue valueWithRange:cleanRange]];
187+
}
188+
lastRangeLocation = i + 1;
189+
}
190+
}
191+
192+
if (lastRangeLocation < endLocation) {
193+
NSRange remainingRange = NSMakeRange(lastRangeLocation, endLocation - lastRangeLocation);
194+
[newRanges addObject:[NSValue valueWithRange:remainingRange]];
195+
}
196+
197+
return newRanges;
198+
}
199+
155200
@end

0 commit comments

Comments
 (0)