@@ -26,8 +26,6 @@ @implementation EnrichedTextInputView {
2626 EnrichedTextInputViewShadowNode::ConcreteState::Shared _state;
2727 int _componentViewHeightUpdateCounter;
2828 NSMutableSet <NSNumber *> *_activeStyles;
29- NSDictionary <NSNumber *, NSArray <NSNumber *> *> *_conflictingStyles;
30- NSDictionary <NSNumber *, NSArray <NSNumber *> *> *_blockingStyles;
3129 LinkData *_recentlyActiveLinkData;
3230 NSRange _recentlyActiveLinkRange;
3331 NSString *_recentlyEmittedString;
@@ -96,40 +94,44 @@ - (void)setDefaults {
9694 @([UnorderedListStyle getStyleType ]): [[UnorderedListStyle alloc ] initWithInput: self ],
9795 @([OrderedListStyle getStyleType ]): [[OrderedListStyle alloc ] initWithInput: self ],
9896 @([BlockQuoteStyle getStyleType ]): [[BlockQuoteStyle alloc ] initWithInput: self ],
97+ @([CodeBlockStyle getStyleType ]): [[CodeBlockStyle alloc ] initWithInput: self ],
9998 @([ImageStyle getStyleType ]): [[ImageStyle alloc ] initWithInput: self ]
10099 };
101100
102- _conflictingStyles = @{
101+ conflictingStyles = @{
103102 @([BoldStyle getStyleType ]) : @[],
104103 @([ItalicStyle getStyleType ]) : @[],
105104 @([UnderlineStyle getStyleType ]) : @[],
106105 @([StrikethroughStyle getStyleType ]) : @[],
107106 @([InlineCodeStyle getStyleType ]) : @[@([LinkStyle getStyleType ]), @([MentionStyle getStyleType ])],
108107 @([LinkStyle getStyleType ]): @[@([InlineCodeStyle getStyleType ]), @([LinkStyle getStyleType ]), @([MentionStyle getStyleType ])],
109108 @([MentionStyle getStyleType ]): @[@([InlineCodeStyle getStyleType ]), @([LinkStyle getStyleType ])],
110- @([H1Style getStyleType ]): @[@([H2Style getStyleType ]), @([H3Style getStyleType ]), @([UnorderedListStyle getStyleType ]), @([OrderedListStyle getStyleType ]), @([BlockQuoteStyle getStyleType ])],
111- @([H2Style getStyleType ]): @[@([H1Style getStyleType ]), @([H3Style getStyleType ]), @([UnorderedListStyle getStyleType ]), @([OrderedListStyle getStyleType ]), @([BlockQuoteStyle getStyleType ])],
112- @([H3Style getStyleType ]): @[@([H1Style getStyleType ]), @([H2Style getStyleType ]), @([UnorderedListStyle getStyleType ]), @([OrderedListStyle getStyleType ]), @([BlockQuoteStyle getStyleType ])],
113- @([UnorderedListStyle getStyleType ]): @[@([H1Style getStyleType ]), @([H2Style getStyleType ]), @([H3Style getStyleType ]), @([OrderedListStyle getStyleType ]), @([BlockQuoteStyle getStyleType ])],
114- @([OrderedListStyle getStyleType ]): @[@([H1Style getStyleType ]), @([H2Style getStyleType ]), @([H3Style getStyleType ]), @([UnorderedListStyle getStyleType ]), @([BlockQuoteStyle getStyleType ])],
115- @([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 ]), @([MentionStyle getStyleType ]), @([LinkStyle getStyleType ])],
116117 @([ImageStyle getStyleType ]) : @[@([LinkStyle getStyleType ]), @([MentionStyle getStyleType ])]
117118 };
118119
119- _blockingStyles = @{
120- @([BoldStyle getStyleType ]) : @[@[@([ImageStyle getStyleType ])] ],
121- @([ItalicStyle getStyleType ]) : @[@[@([ImageStyle getStyleType ])] ],
122- @([UnderlineStyle getStyleType ]) : @[@[@([ImageStyle getStyleType ])] ],
123- @([StrikethroughStyle getStyleType ]) : @[@[@([ImageStyle getStyleType ])] ],
124- @([InlineCodeStyle getStyleType ]) : @[@([ImageStyle getStyleType ])],
125- @([LinkStyle getStyleType ]): @[@([ImageStyle getStyleType ])],
126- @([MentionStyle getStyleType ]): @[@([ImageStyle getStyleType ])],
120+ blockingStyles = @{
121+ @([BoldStyle getStyleType ]) : @[@([CodeBlockStyle getStyleType ])],
122+ @([ItalicStyle getStyleType ]) : @[@([CodeBlockStyle getStyleType ])],
123+ @([UnderlineStyle getStyleType ]) : @[@([CodeBlockStyle getStyleType ])],
124+ @([StrikethroughStyle getStyleType ]) : @[@([CodeBlockStyle getStyleType ])],
125+ @([InlineCodeStyle getStyleType ]) : @[@([CodeBlockStyle getStyleType ]), @([ ImageStyle getStyleType ])],
126+ @([LinkStyle getStyleType ]): @[@([CodeBlockStyle getStyleType ]), @([ ImageStyle getStyleType ])],
127+ @([MentionStyle getStyleType ]): @[@([CodeBlockStyle getStyleType ]), @([ ImageStyle getStyleType ])],
127128 @([H1Style getStyleType ]): @[],
128129 @([H2Style getStyleType ]): @[],
129130 @([H3Style getStyleType ]): @[],
130131 @([UnorderedListStyle getStyleType ]): @[],
131132 @([OrderedListStyle getStyleType ]): @[],
132133 @([BlockQuoteStyle getStyleType ]): @[],
134+ @([CodeBlockStyle getStyleType ]): @[],
133135 @([ImageStyle getStyleType ]) : @[@([InlineCodeStyle getStyleType ])]
134136 };
135137
@@ -350,6 +352,25 @@ - (void)updateProps:(Props::Shared const &)props oldProps:(Props::Shared const &
350352 }
351353 }
352354
355+ if (newViewProps.htmlStyle .codeblock .color != oldViewProps.htmlStyle .codeblock .color ) {
356+ if (isColorMeaningful (newViewProps.htmlStyle .codeblock .color )) {
357+ [newConfig setCodeBlockFgColor: RCTUIColorFromSharedColor (newViewProps.htmlStyle.codeblock.color)];
358+ stylePropChanged = YES ;
359+ }
360+ }
361+
362+ if (newViewProps.htmlStyle .codeblock .backgroundColor != oldViewProps.htmlStyle .codeblock .backgroundColor ) {
363+ if (isColorMeaningful (newViewProps.htmlStyle .codeblock .backgroundColor )) {
364+ [newConfig setCodeBlockBgColor: RCTUIColorFromSharedColor (newViewProps.htmlStyle.codeblock.backgroundColor)];
365+ stylePropChanged = YES ;
366+ }
367+ }
368+
369+ if (newViewProps.htmlStyle .codeblock .borderRadius != oldViewProps.htmlStyle .codeblock .borderRadius ) {
370+ [newConfig setCodeBlockBorderRadius: newViewProps.htmlStyle.codeblock.borderRadius];
371+ stylePropChanged = YES ;
372+ }
373+
353374 if (newViewProps.htmlStyle .img .width != oldViewProps.htmlStyle .img .width ) {
354375 [newConfig setImageWidth: newViewProps.htmlStyle.img.width];
355376 stylePropChanged = YES ;
@@ -525,9 +546,8 @@ - (void)updateProps:(Props::Shared const &)props oldProps:(Props::Shared const &
525546 _emitHtml = newViewProps.isOnChangeHtmlSet ;
526547
527548 [super updateProps: props oldProps: oldProps];
528- // mandatory text and height checks
549+ // run the changes callback
529550 [self anyTextMayHaveBeenModified ];
530- [self tryUpdatingHeight ];
531551
532552 // autofocus - needs to be done at the very end
533553 if (isFirstMount && newViewProps.autoFocus ) {
@@ -715,7 +735,7 @@ - (void)tryUpdatingActiveStyles {
715735 .isUnorderedList = [_activeStyles containsObject: @([UnorderedListStyle getStyleType ])],
716736 .isOrderedList = [_activeStyles containsObject: @([OrderedListStyle getStyleType ])],
717737 .isBlockQuote = [_activeStyles containsObject: @([BlockQuoteStyle getStyleType ])],
718- .isCodeBlock = NO , // [_activeStyles containsObject: @([CodeBlockStyle getStyleType])],
738+ .isCodeBlock = [_activeStyles containsObject: @([CodeBlockStyle getStyleType ])],
719739 .isImage = [_activeStyles containsObject: @([ImageStyle getStyleType ])],
720740 });
721741 }
@@ -784,6 +804,8 @@ - (void)handleCommand:(const NSString *)commandName args:(const NSArray *)args {
784804 [self toggleParagraphStyle: [OrderedListStyle getStyleType ]];
785805 } else if ([commandName isEqualToString: @" toggleBlockQuote" ]) {
786806 [self toggleParagraphStyle: [BlockQuoteStyle getStyleType ]];
807+ } else if ([commandName isEqualToString: @" toggleCodeBlock" ]) {
808+ [self toggleParagraphStyle: [CodeBlockStyle getStyleType ]];
787809 } else if ([commandName isEqualToString: @" addImage" ]) {
788810 NSString *uri = (NSString *)args[0 ];
789811 [self addImage: uri];
@@ -948,7 +970,7 @@ - (void)startMentionWithIndicator:(NSString *)indicator {
948970 MentionStyle *mentionStyleClass = (MentionStyle *)stylesDict[@([MentionStyle getStyleType ])];
949971 if (mentionStyleClass == nullptr ) { return ; }
950972
951- if ([self handleStyleBlocksAndConflicts: [MentionStyle getStyleType ] range: [[mentionStyleClass getActiveMentionRange ] rangeValue ] ]) {
973+ if ([self handleStyleBlocksAndConflicts: [MentionStyle getStyleType ] range: textView.selectedRange ]) {
952974 [mentionStyleClass startMentionWithIndicator: indicator];
953975 [self anyTextMayHaveBeenModified ];
954976 }
@@ -957,13 +979,13 @@ - (void)startMentionWithIndicator:(NSString *)indicator {
957979// returns false when style shouldn't be applied and true when it can be
958980- (BOOL )handleStyleBlocksAndConflicts : (StyleType)type range : (NSRange )range {
959981 // handle blocking styles: if any is present we do not apply the toggled style
960- NSArray <NSNumber *> *blocking = [self getPresentStyleTypesFrom: _blockingStyles [@(type)] range: range];
982+ NSArray <NSNumber *> *blocking = [self getPresentStyleTypesFrom: blockingStyles [@(type)] range: range];
961983 if (blocking.count != 0 ) {
962984 return NO ;
963985 }
964986
965987 // handle conflicting styles: all of their occurences have to be removed
966- NSArray <NSNumber *> *conflicting = [self getPresentStyleTypesFrom: _conflictingStyles [@(type)] range: range];
988+ NSArray <NSNumber *> *conflicting = [self getPresentStyleTypesFrom: conflictingStyles [@(type)] range: range];
967989 if (conflicting.count != 0 ) {
968990 for (NSNumber *style in conflicting) {
969991 id <BaseStyleProtocol> styleClass = stylesDict[style];
@@ -1039,6 +1061,7 @@ - (void)manageSelectionBasedChanges {
10391061- (void )handleWordModificationBasedChanges : (NSString *)word inRange : (NSRange )range {
10401062 // manual links refreshing and automatic links detection handling
10411063 LinkStyle* linkStyle = [stylesDict objectForKey: @([LinkStyle getStyleType ])];
1064+
10421065 if (linkStyle != nullptr ) {
10431066 // manual links need to be handled first because they can block automatic links after being refreshed
10441067 [linkStyle handleManualLinks: word inRange: range];
@@ -1073,6 +1096,12 @@ - (void)anyTextMayHaveBeenModified {
10731096 [bqStyle manageBlockquoteColor ];
10741097 }
10751098
1099+ // codeblock font and color management
1100+ CodeBlockStyle *codeBlockStyle = stylesDict[@([CodeBlockStyle getStyleType ])];
1101+ if (codeBlockStyle != nullptr ) {
1102+ [codeBlockStyle manageCodeBlockFontAndColor ];
1103+ }
1104+
10761105 // improper headings fix
10771106 H1Style *h1Style = stylesDict[@([H1Style getStyleType ])];
10781107 H2Style *h2Style = stylesDict[@([H2Style getStyleType ])];
@@ -1198,6 +1227,7 @@ - (bool)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range r
11981227 UnorderedListStyle *uStyle = stylesDict[@([UnorderedListStyle getStyleType ])];
11991228 OrderedListStyle *oStyle = stylesDict[@([OrderedListStyle getStyleType ])];
12001229 BlockQuoteStyle *bqStyle = stylesDict[@([BlockQuoteStyle getStyleType ])];
1230+ CodeBlockStyle *cbStyle = stylesDict[@([CodeBlockStyle getStyleType ])];
12011231 LinkStyle *linkStyle = stylesDict[@([LinkStyle getStyleType ])];
12021232 MentionStyle *mentionStyle = stylesDict[@([MentionStyle getStyleType ])];
12031233 H1Style *h1Style = stylesDict[@([H1Style getStyleType ])];
@@ -1213,13 +1243,19 @@ - (bool)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range r
12131243 [oStyle handleBackspaceInRange: range replacementText: text] ||
12141244 [oStyle tryHandlingListShorcutInRange: range replacementText: text] ||
12151245 [bqStyle handleBackspaceInRange: range replacementText: text] ||
1246+ [cbStyle handleBackspaceInRange: range replacementText: text] ||
12161247 [linkStyle handleLeadingLinkReplacement: range replacementText: text] ||
12171248 [mentionStyle handleLeadingMentionReplacement: range replacementText: text] ||
12181249 [h1Style handleNewlinesInRange: range replacementText: text] ||
12191250 [h2Style handleNewlinesInRange: range replacementText: text] ||
12201251 [h3Style handleNewlinesInRange: range replacementText: text] ||
12211252 [ZeroWidthSpaceUtils handleBackspaceInRange: range replacementText: text input: self ] ||
1222- [ParagraphAttributesUtils handleBackspaceInRange: range replacementText: text input: self ]
1253+ [ParagraphAttributesUtils handleBackspaceInRange: range replacementText: text input: self ] ||
1254+ // CRITICAL: This callback HAS TO be always evaluated last.
1255+ //
1256+ // This function is the "Generic Fallback": if no specific style claims the backspace action
1257+ // to change its state, only then do we proceed to physically delete the newline and merge paragraphs.
1258+ [ParagraphAttributesUtils handleNewlineBackspaceInRange: range replacementText: text input: self ]
12231259 ) {
12241260 [self anyTextMayHaveBeenModified ];
12251261 return NO ;
0 commit comments