Skip to content

Commit 17cf35e

Browse files
fix: setting font and color in codeblock
1 parent 8ad26ca commit 17cf35e

4 files changed

Lines changed: 64 additions & 45 deletions

File tree

ios/EnrichedTextInputView.mm

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1059,6 +1059,12 @@ - (void)anyTextMayHaveBeenModified {
10591059
[bqStyle manageBlockquoteColor];
10601060
}
10611061

1062+
// codeblock font and color management
1063+
CodeBlockStyle *codeBlockStyle = stylesDict[@([CodeBlockStyle getStyleType])];
1064+
if(codeBlockStyle != nullptr) {
1065+
[codeBlockStyle manageCodeBlockFontAndColor];
1066+
}
1067+
10621068
// improper headings fix
10631069
H1Style *h1Style = stylesDict[@([H1Style getStyleType])];
10641070
H2Style *h2Style = stylesDict[@([H2Style getStyleType])];

ios/styles/CodeBlockStyle.mm

Lines changed: 51 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -49,23 +49,6 @@ - (void)addAttributes:(NSRange)range {
4949
offset += 1;
5050
}
5151

52-
[_input->textView.textStorage enumerateAttribute:NSFontAttributeName
53-
inRange:pRange
54-
options:0
55-
usingBlock:^(id _Nullable value, NSRange range, BOOL * _Nonnull stop) {
56-
UIFont *font = (UIFont *)value;
57-
if(font != nullptr) {
58-
UIFont *newFont = [[[_input->config monospacedFont] withFontTraits:font] setSize:font.pointSize];
59-
[_input->textView.textStorage addAttribute:NSFontAttributeName value:newFont range:range];
60-
}
61-
}];
62-
63-
[_input->textView.textStorage enumerateAttribute:NSForegroundColorAttributeName inRange:pRange options:0
64-
usingBlock:^(id _Nullable value, NSRange range, BOOL * _Nonnull stop) {
65-
[_input->textView.textStorage addAttribute:NSForegroundColorAttributeName value:[_input->config codeBlockFgColor] range:range];
66-
}
67-
];
68-
6952
[_input->textView.textStorage enumerateAttribute:NSParagraphStyleAttributeName inRange:pRange options:0
7053
usingBlock:^(id _Nullable value, NSRange range, BOOL * _Nonnull stop) {
7154
NSMutableParagraphStyle *pStyle = [(NSParagraphStyle *)value mutableCopy];
@@ -91,11 +74,6 @@ - (void)addAttributes:(NSRange)range {
9174
NSMutableParagraphStyle *pStyle = [typingAttrs[NSParagraphStyleAttributeName] mutableCopy];
9275
pStyle.textLists = @[codeBlockList];
9376
typingAttrs[NSParagraphStyleAttributeName] = pStyle;
94-
typingAttrs[NSForegroundColorAttributeName] = [_input->config codeBlockFgColor];
95-
UIFont *currentFont = typingAttrs[NSFontAttributeName];
96-
if(currentFont != nullptr) {
97-
typingAttrs[NSFontAttributeName] = [[[_input->config monospacedFont] withFontTraits:currentFont] setSize:currentFont.pointSize];
98-
}
9977

10078
_input->textView.typingAttributes = typingAttrs;
10179
}
@@ -111,23 +89,8 @@ - (void)removeAttributes:(NSRange)range {
11189

11290
for(NSValue *value in paragraphs) {
11391
NSRange pRange = [value rangeValue];
114-
[_input->textView.textStorage enumerateAttribute:NSFontAttributeName inRange:pRange options:0
115-
usingBlock:^(id _Nullable value, NSRange range, BOOL * _Nonnull stop) {
116-
UIFont *font = (UIFont *)value;
117-
if(font != nullptr) {
118-
UIFont *newFont = [[[_input->config primaryFont] withFontTraits:font] setSize:font.pointSize];
119-
[_input->textView.textStorage addAttribute:NSFontAttributeName value:newFont range:range];
120-
}
121-
}
122-
];
12392

124-
[_input->textView.textStorage enumerateAttribute:NSForegroundColorAttributeName inRange:pRange options:0
125-
usingBlock:^(id _Nullable value, NSRange range, BOOL * _Nonnull stop) {
126-
[_input->textView.textStorage addAttribute:NSForegroundColorAttributeName value:[_input->config primaryColor] range:range];
127-
}
128-
];
129-
130-
[_input->textView.textStorage enumerateAttribute:NSParagraphStyleAttributeName inRange:range options:0
93+
[_input->textView.textStorage enumerateAttribute:NSParagraphStyleAttributeName inRange:pRange options:0
13194
usingBlock:^(id _Nullable value, NSRange range, BOOL * _Nonnull stop) {
13295
NSMutableParagraphStyle *pStyle = [(NSParagraphStyle *)value mutableCopy];
13396
pStyle.textLists = @[];
@@ -144,11 +107,6 @@ - (void)removeAttributes:(NSRange)range {
144107
pStyle.textLists = @[];
145108

146109
typingAttrs[NSParagraphStyleAttributeName] = pStyle;
147-
typingAttrs[NSForegroundColorAttributeName] = [_input->config primaryColor];
148-
UIFont *currentFont = typingAttrs[NSFontAttributeName];
149-
if(currentFont != nullptr) {
150-
typingAttrs[NSFontAttributeName] = [[[_input->config primaryFont] withFontTraits:currentFont] setSize:currentFont.pointSize];
151-
}
152110

153111
_input->textView.typingAttributes = typingAttrs;
154112
}
@@ -175,7 +133,7 @@ - (BOOL)handleBackspaceInRange:(NSRange)range replacementText:(NSString *)text {
175133

176134
- (BOOL)styleCondition:(id _Nullable)value :(NSRange)range {
177135
NSParagraphStyle *paragraph = (NSParagraphStyle *)value;
178-
return paragraph != nullptr && paragraph.textLists.count == 1 && [paragraph.textLists.firstObject.markerFormat isEqual: @"codeblock"];
136+
return paragraph != nullptr && paragraph.textLists.count == 1 && [paragraph.textLists.firstObject.markerFormat isEqual:@"codeblock"];
179137
}
180138

181139
- (BOOL)detectStyle:(NSRange)range {
@@ -210,4 +168,53 @@ - (BOOL)anyOccurence:(NSRange)range {
210168
];
211169
}
212170

171+
- (void)manageCodeBlockFontAndColor {
172+
if([[_input->config codeBlockFgColor] isEqualToColor:[_input->config primaryColor]]) {
173+
return;
174+
}
175+
176+
NSRange wholeRange = NSMakeRange(0, _input->textView.textStorage.string.length);
177+
NSArray *paragraphs = [ParagraphsUtils getSeparateParagraphsRangesIn:_input->textView range:wholeRange];
178+
179+
for(NSValue *pValue in paragraphs) {
180+
NSRange paragraphRange = [pValue rangeValue];
181+
BOOL selfDetected = [self detectStyle:paragraphRange];
182+
183+
[_input->textView.textStorage enumerateAttribute:NSFontAttributeName inRange:paragraphRange options:0
184+
usingBlock:^(id _Nullable value, NSRange range, BOOL * _Nonnull stop) {
185+
UIFont *currentFont = (UIFont *)value;
186+
UIFont *newFont = nullptr;
187+
188+
BOOL isCodeFont = [[currentFont familyName] isEqualToString:[[_input->config monospacedFont] familyName]];
189+
190+
if (isCodeFont && !selfDetected) {
191+
newFont = [[[_input->config primaryFont] withFontTraits:currentFont] setSize:currentFont.pointSize];
192+
} else if (!isCodeFont && selfDetected) {
193+
newFont = [[[_input->config monospacedFont] withFontTraits:currentFont] setSize:currentFont.pointSize];
194+
}
195+
196+
if (newFont != nullptr) {
197+
[_input->textView.textStorage addAttribute:NSFontAttributeName value:newFont range:range];
198+
}
199+
}];
200+
201+
[_input->textView.textStorage enumerateAttribute:NSForegroundColorAttributeName inRange:paragraphRange options:0
202+
usingBlock:^(id _Nullable value, NSRange range, BOOL * _Nonnull stop) {
203+
UIColor *newColor = nullptr;
204+
BOOL colorApplied = [(UIColor *)value isEqualToColor:[_input->config codeBlockFgColor]];
205+
206+
if(colorApplied && !selfDetected) {
207+
newColor = [_input->config primaryColor];
208+
} else if(!colorApplied && selfDetected) {
209+
newColor = [_input->config codeBlockFgColor];
210+
}
211+
212+
if(newColor != nullptr) {
213+
[_input->textView.textStorage addAttribute:NSForegroundColorAttributeName value:newColor range:range];
214+
}
215+
}
216+
];
217+
}
218+
}
219+
213220
@end

ios/utils/ParagraphAttributesUtils.mm

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ + (BOOL)handleBackspaceInRange:(NSRange)range replacementText:(NSString *)text i
1414
UnorderedListStyle *ulStyle = typedInput->stylesDict[@([UnorderedListStyle getStyleType])];
1515
OrderedListStyle *olStyle = typedInput->stylesDict[@([OrderedListStyle getStyleType])];
1616
BlockQuoteStyle *bqStyle = typedInput->stylesDict[@([BlockQuoteStyle getStyleType])];
17+
CodeBlockStyle *cbStyle = typedInput->stylesDict[@([CodeBlockStyle getStyleType])];
1718

1819
if(typedInput == nullptr) {
1920
return NO;
@@ -53,6 +54,11 @@ + (BOOL)handleBackspaceInRange:(NSRange)range replacementText:(NSString *)text i
5354
[bqStyle addAttributes:NSMakeRange(range.location, 0)];
5455
return YES;
5556
}
57+
if([cbStyle detectStyle:nonNewlineRange]) {
58+
[TextInsertionUtils replaceText:text at:range additionalAttributes:nullptr input:typedInput withSelection:YES];
59+
[cbStyle addAttributes:NSMakeRange(range.location, 0)];
60+
return YES;
61+
}
5662

5763
// do the replacement manually
5864
[TextInsertionUtils replaceText:text at:range additionalAttributes:nullptr input:typedInput withSelection:YES];

ios/utils/StyleHeaders.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,6 @@
7676
@end
7777

7878
@interface CodeBlockStyle : NSObject<BaseStyleProtocol>
79-
- (void)manageCodeBlockColor;
79+
- (void)manageCodeBlockFontAndColor;
8080
- (BOOL)handleBackspaceInRange:(NSRange)range replacementText:(NSString *)text;
8181
@end

0 commit comments

Comments
 (0)