Skip to content

Commit 1eba845

Browse files
feat: add support for codeblock on iOS
1 parent 684eeec commit 1eba845

7 files changed

Lines changed: 410 additions & 21 deletions

File tree

ios/EnrichedTextInputView.mm

Lines changed: 46 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,8 @@ - (void)setDefaults {
9494
@([H3Style getStyleType]): [[H3Style alloc] initWithInput:self],
9595
@([UnorderedListStyle getStyleType]): [[UnorderedListStyle alloc] initWithInput:self],
9696
@([OrderedListStyle getStyleType]): [[OrderedListStyle alloc] initWithInput:self],
97-
@([BlockQuoteStyle getStyleType]): [[BlockQuoteStyle alloc] initWithInput:self]
97+
@([BlockQuoteStyle getStyleType]): [[BlockQuoteStyle alloc] initWithInput:self],
98+
@([CodeBlockStyle getStyleType]): [[CodeBlockStyle alloc] initWithInput:self]
9899
};
99100

100101
_conflictingStyles = @{
@@ -105,28 +106,31 @@ - (void)setDefaults {
105106
@([InlineCodeStyle getStyleType]) : @[@([LinkStyle getStyleType]), @([MentionStyle getStyleType])],
106107
@([LinkStyle getStyleType]): @[@([InlineCodeStyle getStyleType]), @([LinkStyle getStyleType]), @([MentionStyle getStyleType])],
107108
@([MentionStyle getStyleType]): @[@([InlineCodeStyle getStyleType]), @([LinkStyle getStyleType])],
108-
@([H1Style getStyleType]): @[@([H2Style getStyleType]), @([H3Style getStyleType]), @([UnorderedListStyle getStyleType]), @([OrderedListStyle getStyleType]), @([BlockQuoteStyle getStyleType])],
109-
@([H2Style getStyleType]): @[@([H1Style getStyleType]), @([H3Style getStyleType]), @([UnorderedListStyle getStyleType]), @([OrderedListStyle getStyleType]), @([BlockQuoteStyle getStyleType])],
110-
@([H3Style getStyleType]): @[@([H1Style getStyleType]), @([H2Style getStyleType]), @([UnorderedListStyle getStyleType]), @([OrderedListStyle getStyleType]), @([BlockQuoteStyle getStyleType])],
111-
@([UnorderedListStyle getStyleType]): @[@([H1Style getStyleType]), @([H2Style getStyleType]), @([H3Style getStyleType]), @([OrderedListStyle getStyleType]), @([BlockQuoteStyle getStyleType])],
112-
@([OrderedListStyle getStyleType]): @[@([H1Style getStyleType]), @([H2Style getStyleType]), @([H3Style getStyleType]), @([UnorderedListStyle getStyleType]), @([BlockQuoteStyle getStyleType])],
113-
@([BlockQuoteStyle getStyleType]): @[@([H1Style getStyleType]), @([H2Style getStyleType]), @([H3Style getStyleType]), @([UnorderedListStyle getStyleType]), @([OrderedListStyle getStyleType])]
109+
@([H1Style getStyleType]): @[@([H2Style getStyleType]), @([H3Style getStyleType]), @([UnorderedListStyle getStyleType]), @([OrderedListStyle getStyleType]), @([BlockQuoteStyle getStyleType]), @([CodeBlockStyle getStyleType])],
110+
@([H2Style getStyleType]): @[@([H1Style getStyleType]), @([H3Style getStyleType]), @([UnorderedListStyle getStyleType]), @([OrderedListStyle getStyleType]), @([BlockQuoteStyle getStyleType]), @([CodeBlockStyle getStyleType])],
111+
@([H3Style getStyleType]): @[@([H1Style getStyleType]), @([H2Style getStyleType]), @([UnorderedListStyle getStyleType]), @([OrderedListStyle getStyleType]), @([BlockQuoteStyle getStyleType]), @([CodeBlockStyle getStyleType])],
112+
@([UnorderedListStyle getStyleType]): @[@([H1Style getStyleType]), @([H2Style getStyleType]), @([H3Style getStyleType]), @([OrderedListStyle getStyleType]), @([BlockQuoteStyle getStyleType]), @([CodeBlockStyle getStyleType])],
113+
@([OrderedListStyle getStyleType]): @[@([H1Style getStyleType]), @([H2Style getStyleType]), @([H3Style getStyleType]), @([UnorderedListStyle getStyleType]), @([BlockQuoteStyle getStyleType]), @([CodeBlockStyle getStyleType])],
114+
@([BlockQuoteStyle getStyleType]): @[@([H1Style getStyleType]), @([H2Style getStyleType]), @([H3Style getStyleType]), @([UnorderedListStyle getStyleType]), @([OrderedListStyle getStyleType]), @([CodeBlockStyle getStyleType])],
115+
@([CodeBlockStyle getStyleType]): @[@([H1Style getStyleType]), @([H2Style getStyleType]), @([H3Style getStyleType]),
116+
@([BoldStyle getStyleType]), @([ItalicStyle getStyleType]), @([UnderlineStyle getStyleType]), @([StrikethroughStyle getStyleType]), @([UnorderedListStyle getStyleType]), @([OrderedListStyle getStyleType]), @([BlockQuoteStyle getStyleType]), @([InlineCodeStyle getStyleType])]
114117
};
115118

116119
_blockingStyles = @{
117-
@([BoldStyle getStyleType]) : @[],
118-
@([ItalicStyle getStyleType]) : @[],
119-
@([UnderlineStyle getStyleType]) : @[],
120-
@([StrikethroughStyle getStyleType]) : @[],
121-
@([InlineCodeStyle getStyleType]) : @[],
122-
@([LinkStyle getStyleType]): @[],
123-
@([MentionStyle getStyleType]): @[],
120+
@([BoldStyle getStyleType]) : @[@([CodeBlockStyle getStyleType])],
121+
@([ItalicStyle getStyleType]) : @[@([CodeBlockStyle getStyleType])],
122+
@([UnderlineStyle getStyleType]) : @[@([CodeBlockStyle getStyleType])],
123+
@([StrikethroughStyle getStyleType]) : @[@([CodeBlockStyle getStyleType])],
124+
@([InlineCodeStyle getStyleType]) : @[@([CodeBlockStyle getStyleType])],
125+
@([LinkStyle getStyleType]): @[@([CodeBlockStyle getStyleType])],
126+
@([MentionStyle getStyleType]): @[@([CodeBlockStyle getStyleType])],
124127
@([H1Style getStyleType]): @[],
125128
@([H2Style getStyleType]): @[],
126129
@([H3Style getStyleType]): @[],
127130
@([UnorderedListStyle getStyleType]): @[],
128131
@([OrderedListStyle getStyleType]): @[],
129132
@([BlockQuoteStyle getStyleType]): @[],
133+
@([CodeBlockStyle getStyleType]): @[],
130134
};
131135

132136
parser = [[InputParser alloc] initWithInput:self];
@@ -346,6 +350,25 @@ - (void)updateProps:(Props::Shared const &)props oldProps:(Props::Shared const &
346350
}
347351
}
348352

353+
if(newViewProps.htmlStyle.codeblock.color != oldViewProps.htmlStyle.codeblock.color) {
354+
if(isColorMeaningful(newViewProps.htmlStyle.codeblock.color)) {
355+
[newConfig setCodeBlockFgColor:RCTUIColorFromSharedColor(newViewProps.htmlStyle.codeblock.color)];
356+
stylePropChanged = YES;
357+
}
358+
}
359+
360+
if(newViewProps.htmlStyle.codeblock.backgroundColor != oldViewProps.htmlStyle.codeblock.backgroundColor) {
361+
if(isColorMeaningful(newViewProps.htmlStyle.codeblock.backgroundColor)) {
362+
[newConfig setCodeBlockBgColor:RCTUIColorFromSharedColor(newViewProps.htmlStyle.codeblock.backgroundColor)];
363+
stylePropChanged = YES;
364+
}
365+
}
366+
367+
if(newViewProps.htmlStyle.codeblock.borderRadius != oldViewProps.htmlStyle.codeblock.borderRadius) {
368+
[newConfig setCodeBlockBorderRadius:newViewProps.htmlStyle.codeblock.borderRadius];
369+
stylePropChanged = YES;
370+
}
371+
349372
if(newViewProps.htmlStyle.a.textDecorationLine != oldViewProps.htmlStyle.a.textDecorationLine) {
350373
NSString *objcString = [NSString fromCppString:newViewProps.htmlStyle.a.textDecorationLine];
351374
if([objcString isEqualToString:DecorationUnderline]) {
@@ -696,7 +719,7 @@ - (void)tryUpdatingActiveStyles {
696719
.isUnorderedList = [_activeStyles containsObject: @([UnorderedListStyle getStyleType])],
697720
.isOrderedList = [_activeStyles containsObject: @([OrderedListStyle getStyleType])],
698721
.isBlockQuote = [_activeStyles containsObject: @([BlockQuoteStyle getStyleType])],
699-
.isCodeBlock = NO, // [_activeStyles containsObject: @([CodeBlockStyle getStyleType])],
722+
.isCodeBlock = [_activeStyles containsObject: @([CodeBlockStyle getStyleType])],
700723
.isImage = NO // [_activeStyles containsObject: @([ImageStyle getStyleType]])],
701724
});
702725
}
@@ -768,6 +791,8 @@ - (void)handleCommand:(const NSString *)commandName args:(const NSArray *)args {
768791
[self toggleParagraphStyle:[OrderedListStyle getStyleType]];
769792
} else if([commandName isEqualToString:@"toggleBlockQuote"]) {
770793
[self toggleParagraphStyle:[BlockQuoteStyle getStyleType]];
794+
} else if([commandName isEqualToString:@"toggleCodeBlock"]) {
795+
[self toggleParagraphStyle:[CodeBlockStyle getStyleType]];
771796
}
772797
}
773798

@@ -1033,6 +1058,12 @@ - (void)anyTextMayHaveBeenModified {
10331058
[bqStyle manageBlockquoteColor];
10341059
}
10351060

1061+
// codeblock colors management
1062+
CodeBlockStyle *codeBlockStyle = stylesDict[@([CodeBlockStyle getStyleType])];
1063+
if(codeBlockStyle != nullptr) {
1064+
[codeBlockStyle manageCodeBlockColor];
1065+
}
1066+
10361067
// improper headings fix
10371068
H1Style *h1Style = stylesDict[@([H1Style getStyleType])];
10381069
H2Style *h2Style = stylesDict[@([H2Style getStyleType])];

ios/config/InputConfig.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,10 @@
6464
- (void)setLinkDecorationLine:(TextDecorationLineEnum)newValue;
6565
- (void)setMentionStyleProps:(NSDictionary *)newValue;
6666
- (MentionStyleProps *)mentionStylePropsForIndicator:(NSString *)indicator;
67+
- (UIColor *)codeBlockFgColor;
68+
- (void)setCodeBlockFgColor:(UIColor *)newValue;
69+
- (UIColor *)codeBlockBgColor;
70+
- (void)setCodeBlockBgColor:(UIColor *)newValue;
71+
- (CGFloat)codeBlockBorderRadius;
72+
- (void)setCodeBlockBorderRadius:(CGFloat)newValue;
6773
@end

ios/config/InputConfig.mm

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ @implementation InputConfig {
3636
UIColor *_linkColor;
3737
TextDecorationLineEnum _linkDecorationLine;
3838
NSDictionary *_mentionProperties;
39+
UIColor *_codeBlockFgColor;
40+
CGFloat _codeBlockBorderRadius;
41+
UIColor *_codeBlockBgColor;
3942
}
4043

4144
- (instancetype) init {
@@ -79,6 +82,9 @@ - (id)copyWithZone:(NSZone *)zone {
7982
copy->_linkColor = [_linkColor copy];
8083
copy->_linkDecorationLine = [_linkDecorationLine copy];
8184
copy->_mentionProperties = [_mentionProperties mutableCopy];
85+
copy->_codeBlockFgColor = [_codeBlockFgColor copy];
86+
copy->_codeBlockBgColor = [_codeBlockBgColor copy];
87+
copy->_codeBlockBorderRadius = _codeBlockBorderRadius;
8288
return copy;
8389
}
8490

@@ -379,4 +385,28 @@ - (MentionStyleProps *)mentionStylePropsForIndicator:(NSString *)indicator {
379385
return fallbackProps;
380386
}
381387

388+
- (UIColor *)codeBlockFgColor {
389+
return _codeBlockFgColor;
390+
}
391+
392+
- (void)setCodeBlockFgColor:(UIColor *)newValue {
393+
_codeBlockFgColor = newValue;
394+
}
395+
396+
- (UIColor *)codeBlockBgColor {
397+
return _codeBlockBgColor;
398+
}
399+
400+
- (void)setCodeBlockBgColor:(UIColor *)newValue {
401+
_codeBlockBgColor = newValue;
402+
}
403+
404+
- (CGFloat)codeBlockBorderRadius {
405+
return _codeBlockBorderRadius;
406+
}
407+
408+
- (void)setCodeBlockBorderRadius:(CGFloat)newValue {
409+
_codeBlockBorderRadius = newValue;
410+
}
411+
382412
@end

0 commit comments

Comments
 (0)