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
0 commit comments