Skip to content

Commit ec0a671

Browse files
Merge branch 'main' into add-inline-image-ios
2 parents 1152ff9 + 571fa10 commit ec0a671

29 files changed

Lines changed: 746 additions & 112 deletions

ios/EnrichedTextInputView.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,16 @@ NS_ASSUME_NONNULL_BEGIN
1818
@public InputParser *parser;
1919
@public NSMutableDictionary<NSAttributedStringKey, id> *defaultTypingAttributes;
2020
@public NSDictionary<NSNumber *, id<BaseStyleProtocol>> *stylesDict;
21+
NSDictionary<NSNumber *, NSArray<NSNumber *> *> *conflictingStyles;
22+
NSDictionary<NSNumber *, NSArray<NSNumber *> *> *blockingStyles;
2123
@public BOOL blockEmitting;
2224
}
2325
- (CGSize)measureSize:(CGFloat)maxWidth;
2426
- (void)emitOnLinkDetectedEvent:(NSString *)text url:(NSString *)url range:(NSRange)range;
2527
- (void)emitOnMentionEvent:(NSString *)indicator text:(nullable NSString *)text;
2628
- (void)anyTextMayHaveBeenModified;
2729
- (BOOL)handleStyleBlocksAndConflicts:(StyleType)type range:(NSRange)range;
30+
- (NSArray<NSNumber *> *)getPresentStyleTypesFrom:(NSArray<NSNumber *> *)types range:(NSRange)range;
2831
@end
2932

3033
NS_ASSUME_NONNULL_END

ios/EnrichedTextInputView.mm

Lines changed: 60 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -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;

ios/config/InputConfig.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,12 @@
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
- (void)setImageWidth:(CGFloat)newValue;
6874
- (CGFloat)imageWidth;
6975
- (void)setImageHeight:(CGFloat)newValue;

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
CGFloat _imageWidth;
4043
CGFloat _imageHeight;
4144
}
@@ -81,6 +84,9 @@ - (id)copyWithZone:(NSZone *)zone {
8184
copy->_linkColor = [_linkColor copy];
8285
copy->_linkDecorationLine = [_linkDecorationLine copy];
8386
copy->_mentionProperties = [_mentionProperties mutableCopy];
87+
copy->_codeBlockFgColor = [_codeBlockFgColor copy];
88+
copy->_codeBlockBgColor = [_codeBlockBgColor copy];
89+
copy->_codeBlockBorderRadius = _codeBlockBorderRadius;
8490
copy->_imageWidth = _imageWidth;
8591
copy->_imageHeight = _imageHeight;
8692
return copy;
@@ -383,6 +389,30 @@ - (MentionStyleProps *)mentionStylePropsForIndicator:(NSString *)indicator {
383389
return fallbackProps;
384390
}
385391

392+
- (UIColor *)codeBlockFgColor {
393+
return _codeBlockFgColor;
394+
}
395+
396+
- (void)setCodeBlockFgColor:(UIColor *)newValue {
397+
_codeBlockFgColor = newValue;
398+
}
399+
400+
- (UIColor *)codeBlockBgColor {
401+
return _codeBlockBgColor;
402+
}
403+
404+
- (void)setCodeBlockBgColor:(UIColor *)newValue {
405+
_codeBlockBgColor = newValue;
406+
}
407+
408+
- (CGFloat)codeBlockBorderRadius {
409+
return _codeBlockBorderRadius;
410+
}
411+
412+
- (void)setCodeBlockBorderRadius:(CGFloat)newValue {
413+
_codeBlockBorderRadius = newValue;
414+
}
415+
386416
- (CGFloat)imageWidth {
387417
return _imageWidth;
388418
}

0 commit comments

Comments
 (0)