Skip to content

Commit 7d65196

Browse files
fix: setting foreground color in codeblock
1 parent 9acc42c commit 7d65196

4 files changed

Lines changed: 40 additions & 40 deletions

File tree

ios/EnrichedTextInputView.mm

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1025,7 +1025,7 @@ - (void)handleWordModificationBasedChanges:(NSString*)word inRange:(NSRange)rang
10251025
// manual links refreshing and automatic links detection handling
10261026
LinkStyle* linkStyle = [stylesDict objectForKey:@([LinkStyle getStyleType])];
10271027

1028-
if(linkStyle != nullptr && [self handleStyleBlocksAndConflicts:[LinkStyle getStyleType] range:range]) {
1028+
if(linkStyle != nullptr) {
10291029
// manual links need to be handled first because they can block automatic links after being refreshed
10301030
[linkStyle handleManualLinks:word inRange:range];
10311031
[linkStyle handleAutomaticLinks:word inRange:range];
@@ -1059,12 +1059,6 @@ - (void)anyTextMayHaveBeenModified {
10591059
[bqStyle manageBlockquoteColor];
10601060
}
10611061

1062-
// codeblock colors management
1063-
CodeBlockStyle *codeBlockStyle = stylesDict[@([CodeBlockStyle getStyleType])];
1064-
if(codeBlockStyle != nullptr) {
1065-
[codeBlockStyle manageCodeBlockColor];
1066-
}
1067-
10681062
// improper headings fix
10691063
H1Style *h1Style = stylesDict[@([H1Style getStyleType])];
10701064
H2Style *h2Style = stylesDict[@([H2Style getStyleType])];
@@ -1189,6 +1183,7 @@ - (bool)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range r
11891183
UnorderedListStyle *uStyle = stylesDict[@([UnorderedListStyle getStyleType])];
11901184
OrderedListStyle *oStyle = stylesDict[@([OrderedListStyle getStyleType])];
11911185
BlockQuoteStyle *bqStyle = stylesDict[@([BlockQuoteStyle getStyleType])];
1186+
CodeBlockStyle *cbStyle = stylesDict[@([CodeBlockStyle getStyleType])];
11921187
LinkStyle *linkStyle = stylesDict[@([LinkStyle getStyleType])];
11931188
MentionStyle *mentionStyle = stylesDict[@([MentionStyle getStyleType])];
11941189
H1Style *h1Style = stylesDict[@([H1Style getStyleType])];
@@ -1204,6 +1199,7 @@ - (bool)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range r
12041199
[oStyle handleBackspaceInRange:range replacementText:text] ||
12051200
[oStyle tryHandlingListShorcutInRange:range replacementText:text] ||
12061201
[bqStyle handleBackspaceInRange:range replacementText:text] ||
1202+
[cbStyle handleBackspaceInRange:range replacementText:text] ||
12071203
[linkStyle handleLeadingLinkReplacement:range replacementText:text] ||
12081204
[mentionStyle handleLeadingMentionReplacement:range replacementText:text] ||
12091205
[h1Style handleNewlinesInRange:range replacementText:text] ||

ios/styles/CodeBlockStyle.mm

Lines changed: 30 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,12 @@ - (void)addAttributes:(NSRange)range {
6060
}
6161
}];
6262

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+
6369
[_input->textView.textStorage enumerateAttribute:NSParagraphStyleAttributeName inRange:pRange options:0
6470
usingBlock:^(id _Nullable value, NSRange range, BOOL * _Nonnull stop) {
6571
NSMutableParagraphStyle *pStyle = [(NSParagraphStyle *)value mutableCopy];
@@ -85,6 +91,7 @@ - (void)addAttributes:(NSRange)range {
8591
NSMutableParagraphStyle *pStyle = [typingAttrs[NSParagraphStyleAttributeName] mutableCopy];
8692
pStyle.textLists = @[codeBlockList];
8793
typingAttrs[NSParagraphStyleAttributeName] = pStyle;
94+
typingAttrs[NSForegroundColorAttributeName] = [_input->config codeBlockFgColor];
8895
UIFont *currentFont = typingAttrs[NSFontAttributeName];
8996
if(currentFont != nullptr) {
9097
typingAttrs[NSFontAttributeName] = [[[_input->config monospacedFont] withFontTraits:currentFont] setSize:currentFont.pointSize];
@@ -114,6 +121,12 @@ - (void)removeAttributes:(NSRange)range {
114121
}
115122
];
116123

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+
117130
[_input->textView.textStorage enumerateAttribute:NSParagraphStyleAttributeName inRange:range options:0
118131
usingBlock:^(id _Nullable value, NSRange range, BOOL * _Nonnull stop) {
119132
NSMutableParagraphStyle *pStyle = [(NSParagraphStyle *)value mutableCopy];
@@ -131,6 +144,7 @@ - (void)removeAttributes:(NSRange)range {
131144
pStyle.textLists = @[];
132145

133146
typingAttrs[NSParagraphStyleAttributeName] = pStyle;
147+
typingAttrs[NSForegroundColorAttributeName] = [_input->config primaryColor];
134148
UIFont *currentFont = typingAttrs[NSFontAttributeName];
135149
if(currentFont != nullptr) {
136150
typingAttrs[NSFontAttributeName] = [[[_input->config primaryFont] withFontTraits:currentFont] setSize:currentFont.pointSize];
@@ -143,6 +157,22 @@ - (void)removeTypingAttributes {
143157
[self removeAttributes:_input->textView.selectedRange];
144158
}
145159

160+
- (BOOL)handleBackspaceInRange:(NSRange)range replacementText:(NSString *)text {
161+
if([self detectStyle:_input->textView.selectedRange] && text.length == 0) {
162+
// backspace while the style is active
163+
164+
NSRange paragraphRange = [_input->textView.textStorage.string paragraphRangeForRange:_input->textView.selectedRange];
165+
166+
if(NSEqualRanges(_input->textView.selectedRange, NSMakeRange(0, 0))) {
167+
// a backspace on the very first input's line quote
168+
// it doesn't run textVieDidChange so we need to manually remove attributes
169+
[self removeAttributes:paragraphRange];
170+
return YES;
171+
}
172+
}
173+
return NO;
174+
}
175+
146176
- (BOOL)styleCondition:(id _Nullable)value :(NSRange)range {
147177
NSParagraphStyle *paragraph = (NSParagraphStyle *)value;
148178
return paragraph != nullptr && paragraph.textLists.count == 1 && [paragraph.textLists.firstObject.markerFormat isEqual: @"codeblock"];
@@ -180,37 +210,4 @@ - (BOOL)anyOccurence:(NSRange)range {
180210
];
181211
}
182212

183-
- (void)manageCodeBlockColor {
184-
if([[_input->config codeBlockFgColor] isEqualToColor:[_input->config primaryColor]]) {
185-
return;
186-
}
187-
188-
NSRange wholeRange = NSMakeRange(0, _input->textView.textStorage.string.length);
189-
190-
NSArray *paragraphs = [ParagraphsUtils getSeparateParagraphsRangesIn:_input->textView range:wholeRange];
191-
for(NSValue *pValue in paragraphs) {
192-
NSRange paragraphRange = [pValue rangeValue];
193-
BOOL selfDetected = [self detectStyle:paragraphRange];
194-
195-
[_input->textView.textStorage enumerateAttribute:NSForegroundColorAttributeName inRange:paragraphRange options:0
196-
usingBlock:^(id _Nullable value, NSRange range, BOOL * _Nonnull stop) {
197-
UIColor *newColor = nullptr;
198-
BOOL colorApplied = [(UIColor *)value isEqualToColor:[_input->config codeBlockFgColor]];
199-
200-
if(colorApplied && !selfDetected) {
201-
newColor = [_input->config primaryColor];
202-
} else if(!colorApplied && selfDetected) {
203-
newColor = [_input->config codeBlockFgColor];
204-
}
205-
206-
if(newColor != nullptr) {
207-
[_input->textView.textStorage addAttribute:NSForegroundColorAttributeName value:newColor range:range];
208-
[_input->textView.textStorage addAttribute:NSUnderlineColorAttributeName value:newColor range:range];
209-
[_input->textView.textStorage addAttribute:NSStrikethroughColorAttributeName value:newColor range:range];
210-
}
211-
}
212-
];
213-
}
214-
}
215-
216213
@end

ios/styles/LinkStyle.mm

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,7 @@ - (void)manageLinkTypingAttributes {
279279
- (void)handleAutomaticLinks:(NSString *)word inRange:(NSRange)wordRange {
280280
InlineCodeStyle *inlineCodeStyle = [_input->stylesDict objectForKey:@([InlineCodeStyle getStyleType])];
281281
MentionStyle *mentionStyle = [_input->stylesDict objectForKey:@([MentionStyle getStyleType])];
282+
CodeBlockStyle *codeBlockStyle = [_input->stylesDict objectForKey:@([CodeBlockStyle getStyleType])];
282283

283284
if (inlineCodeStyle == nullptr || mentionStyle == nullptr) {
284285
return;
@@ -294,6 +295,11 @@ - (void)handleAutomaticLinks:(NSString *)word inRange:(NSRange)wordRange {
294295
return;
295296
}
296297

298+
// we don't recognize links among codeblock
299+
if ([codeBlockStyle anyOccurence:wordRange]) {
300+
return;
301+
}
302+
297303
// remove connected different links
298304
[self removeConnectedLinksIfNeeded:word range:wordRange];
299305

ios/utils/StyleHeaders.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,5 @@
7777

7878
@interface CodeBlockStyle : NSObject<BaseStyleProtocol>
7979
- (void)manageCodeBlockColor;
80+
- (BOOL)handleBackspaceInRange:(NSRange)range replacementText:(NSString *)text;
8081
@end

0 commit comments

Comments
 (0)