|
1 | 1 | #import "EnrichedTextInputView.h" |
| 2 | +#import "ColorExtension.h" |
2 | 3 | #import "CoreText/CoreText.h" |
3 | 4 | #import "LayoutManagerExtension.h" |
4 | 5 | #import "ParagraphAttributesUtils.h" |
|
15 | 16 | #import <ReactNativeEnriched/RCTComponentViewHelpers.h> |
16 | 17 | #import <folly/dynamic.h> |
17 | 18 | #import <react/utils/ManagedObjectWrapper.h> |
18 | | -#import "ColorExtension.h" |
19 | 19 |
|
20 | 20 | using namespace facebook::react; |
21 | 21 |
|
@@ -123,13 +123,16 @@ - (void)setDefaults { |
123 | 123 | @([ItalicStyle getStyleType]) : @[], |
124 | 124 | @([UnderlineStyle getStyleType]) : @[], |
125 | 125 | @([StrikethroughStyle getStyleType]) : @[], |
126 | | - @([InlineCodeStyle getStyleType]) : |
127 | | - @[ @([LinkStyle getStyleType]), @([MentionStyle getStyleType]) ], |
| 126 | + @([InlineCodeStyle getStyleType]) : @[ |
| 127 | + @([LinkStyle getStyleType]), @([MentionStyle getStyleType]), |
| 128 | + @([ColorStyle getStyleType]) |
| 129 | + ], |
128 | 130 | @([LinkStyle getStyleType]) : @[ |
129 | 131 | @([InlineCodeStyle getStyleType]), @([LinkStyle getStyleType]), |
130 | 132 | @([MentionStyle getStyleType]) |
131 | 133 | ], |
132 | | - @([ColorStyle getStyleType]) : @[@([InlineCodeStyle getStyleType]), @([MentionStyle getStyleType])], |
| 134 | + @([ColorStyle getStyleType]) : |
| 135 | + @[ @([InlineCodeStyle getStyleType]), @([MentionStyle getStyleType]) ], |
133 | 136 | @([MentionStyle getStyleType]) : |
134 | 137 | @[ @([InlineCodeStyle getStyleType]), @([LinkStyle getStyleType]) ], |
135 | 138 | @([H1Style getStyleType]) : @[ |
@@ -193,7 +196,7 @@ - (void)setDefaults { |
193 | 196 | @([H3Style getStyleType]), @([H4Style getStyleType]), |
194 | 197 | @([H5Style getStyleType]), @([H6Style getStyleType]), |
195 | 198 | @([UnorderedListStyle getStyleType]), @([OrderedListStyle getStyleType]), |
196 | | - @([CodeBlockStyle getStyleType]) |
| 199 | + @([CodeBlockStyle getStyleType]), @([ColorStyle getStyleType]) |
197 | 200 | ], |
198 | 201 | @([CodeBlockStyle getStyleType]) : @[ |
199 | 202 | @([H1Style getStyleType]), @([H2Style getStyleType]), |
@@ -966,7 +969,8 @@ - (void)tryUpdatingActiveStyles { |
966 | 969 | [_activeStyles containsObject:@([UnderlineStyle getStyleType])], |
967 | 970 | .isStrikeThrough = |
968 | 971 | [_activeStyles containsObject:@([StrikethroughStyle getStyleType])], |
969 | | - .isColored = [_activeStyles containsObject: @([ColorStyle getStyleType])], |
| 972 | + .isColored = |
| 973 | + [_activeStyles containsObject:@([ColorStyle getStyleType])], |
970 | 974 | .isInlineCode = |
971 | 975 | [_activeStyles containsObject:@([InlineCodeStyle getStyleType])], |
972 | 976 | .isLink = [_activeStyles containsObject:@([LinkStyle getStyleType])], |
@@ -1022,24 +1026,24 @@ - (void)handleCommand:(const NSString *)commandName args:(const NSArray *)args { |
1022 | 1026 | } else if ([commandName isEqualToString:@"setValue"]) { |
1023 | 1027 | NSString *value = (NSString *)args[0]; |
1024 | 1028 | [self setValue:value]; |
1025 | | - } else if([commandName isEqualToString:@"toggleBold"]) { |
1026 | | - [self toggleRegularStyle: [BoldStyle getStyleType]]; |
1027 | | - } else if([commandName isEqualToString:@"toggleItalic"]) { |
1028 | | - [self toggleRegularStyle: [ItalicStyle getStyleType]]; |
1029 | | - } else if([commandName isEqualToString:@"toggleUnderline"]) { |
1030 | | - [self toggleRegularStyle: [UnderlineStyle getStyleType]]; |
1031 | | - } else if([commandName isEqualToString:@"toggleStrikeThrough"]) { |
1032 | | - [self toggleRegularStyle: [StrikethroughStyle getStyleType]]; |
1033 | | - } else if([commandName isEqualToString:@"setColor"]) { |
| 1029 | + } else if ([commandName isEqualToString:@"toggleBold"]) { |
| 1030 | + [self toggleRegularStyle:[BoldStyle getStyleType]]; |
| 1031 | + } else if ([commandName isEqualToString:@"toggleItalic"]) { |
| 1032 | + [self toggleRegularStyle:[ItalicStyle getStyleType]]; |
| 1033 | + } else if ([commandName isEqualToString:@"toggleUnderline"]) { |
| 1034 | + [self toggleRegularStyle:[UnderlineStyle getStyleType]]; |
| 1035 | + } else if ([commandName isEqualToString:@"toggleStrikeThrough"]) { |
| 1036 | + [self toggleRegularStyle:[StrikethroughStyle getStyleType]]; |
| 1037 | + } else if ([commandName isEqualToString:@"setColor"]) { |
1034 | 1038 | NSString *colorText = (NSString *)args[0]; |
1035 | | - [self setColor: colorText]; |
| 1039 | + [self setColor:colorText]; |
1036 | 1040 | } else if ([commandName isEqualToString:@"removeColor"]) { |
1037 | 1041 | [self removeColor]; |
1038 | | - } else if([commandName isEqualToString:@"toggleInlineCode"]) { |
1039 | | - [self toggleRegularStyle: [InlineCodeStyle getStyleType]]; |
1040 | | - } else if([commandName isEqualToString:@"addLink"]) { |
1041 | | - NSInteger start = [((NSNumber*)args[0]) integerValue]; |
1042 | | - NSInteger end = [((NSNumber*)args[1]) integerValue]; |
| 1042 | + } else if ([commandName isEqualToString:@"toggleInlineCode"]) { |
| 1043 | + [self toggleRegularStyle:[InlineCodeStyle getStyleType]]; |
| 1044 | + } else if ([commandName isEqualToString:@"addLink"]) { |
| 1045 | + NSInteger start = [((NSNumber *)args[0]) integerValue]; |
| 1046 | + NSInteger end = [((NSNumber *)args[1]) integerValue]; |
1043 | 1047 | NSString *text = (NSString *)args[2]; |
1044 | 1048 | NSString *url = (NSString *)args[3]; |
1045 | 1049 | [self addLinkAt:start end:end text:text url:url]; |
@@ -1177,47 +1181,51 @@ - (void)emitOnLinkDetectedEvent:(NSString *)text |
1177 | 1181 | - (void)emitCurrentSelectionColorIfChanged { |
1178 | 1182 | NSRange selRange = textView.selectedRange; |
1179 | 1183 | UIColor *uniformColor = nil; |
1180 | | - |
| 1184 | + |
1181 | 1185 | if (selRange.length == 0) { |
1182 | 1186 | id colorAttr = textView.typingAttributes[NSForegroundColorAttributeName]; |
1183 | 1187 | uniformColor = colorAttr ? (UIColor *)colorAttr : [config primaryColor]; |
1184 | 1188 | } else { |
1185 | 1189 | // Selection range: check for uniform color |
1186 | 1190 | __block UIColor *firstColor = nil; |
1187 | 1191 | __block BOOL hasMultiple = NO; |
1188 | | - |
1189 | | - [textView.textStorage enumerateAttribute:NSForegroundColorAttributeName |
1190 | | - inRange:selRange |
1191 | | - options:0 |
1192 | | - usingBlock:^(id _Nullable value, NSRange range, BOOL *_Nonnull stop) { |
1193 | | - UIColor *thisColor = value ? (UIColor *)value : [config primaryColor]; |
1194 | | - if (firstColor == nil) { |
1195 | | - firstColor = thisColor; |
1196 | | - } else if (![firstColor isEqual:thisColor]) { |
1197 | | - hasMultiple = YES; |
1198 | | - *stop = YES; |
1199 | | - } |
1200 | | - }]; |
1201 | | - |
| 1192 | + |
| 1193 | + [textView.textStorage |
| 1194 | + enumerateAttribute:NSForegroundColorAttributeName |
| 1195 | + inRange:selRange |
| 1196 | + options:0 |
| 1197 | + usingBlock:^(id _Nullable value, NSRange range, |
| 1198 | + BOOL *_Nonnull stop) { |
| 1199 | + UIColor *thisColor = |
| 1200 | + value ? (UIColor *)value : [config primaryColor]; |
| 1201 | + if (firstColor == nil) { |
| 1202 | + firstColor = thisColor; |
| 1203 | + } else if (![firstColor isEqual:thisColor]) { |
| 1204 | + hasMultiple = YES; |
| 1205 | + *stop = YES; |
| 1206 | + } |
| 1207 | + }]; |
| 1208 | + |
1202 | 1209 | if (!hasMultiple && firstColor != nil) { |
1203 | 1210 | uniformColor = firstColor; |
1204 | 1211 | } |
1205 | 1212 | } |
1206 | | - |
1207 | | - NSString *hexColor = uniformColor ? [uniformColor hexString] : [config.primaryColor hexString]; |
1208 | | - |
1209 | | - if(![_recentlyEmittedColor isEqual: hexColor]) { |
| 1213 | + |
| 1214 | + NSString *hexColor = |
| 1215 | + uniformColor ? [uniformColor hexString] : [config.primaryColor hexString]; |
| 1216 | + |
| 1217 | + if (![_recentlyEmittedColor isEqual:hexColor]) { |
1210 | 1218 | auto emitter = [self getEventEmitter]; |
1211 | | - if(emitter != nullptr) { |
1212 | | - emitter->onColorChangeInSelection({ |
1213 | | - .color = [hexColor toCppString] |
1214 | | - }); |
| 1219 | + if (emitter != nullptr) { |
| 1220 | + emitter->onColorChangeInSelection({.color = [hexColor toCppString]}); |
1215 | 1221 | } |
1216 | 1222 | _recentlyEmittedColor = hexColor; |
1217 | 1223 | } |
1218 | 1224 | } |
1219 | 1225 |
|
1220 | | -- (void)emitOnMentionDetectedEvent:(NSString *)text indicator:(NSString *)indicator attributes:(NSString *)attributes { |
| 1226 | +- (void)emitOnMentionDetectedEvent:(NSString *)text |
| 1227 | + indicator:(NSString *)indicator |
| 1228 | + attributes:(NSString *)attributes { |
1221 | 1229 | auto emitter = [self getEventEmitter]; |
1222 | 1230 | if (emitter != nullptr) { |
1223 | 1231 | emitter->onMentionDetected({.text = [text toCppString], |
@@ -1276,9 +1284,9 @@ - (void)requestHTML:(NSInteger)requestId { |
1276 | 1284 |
|
1277 | 1285 | - (void)setColor:(NSString *)colorText { |
1278 | 1286 | ColorStyle *colorStyle = stylesDict[@(Colored)]; |
1279 | | - UIColor *color = [UIColor colorFromString: colorText]; |
1280 | | - |
1281 | | - [colorStyle applyStyle:textView.selectedRange color: color]; |
| 1287 | + UIColor *color = [UIColor colorFromString:colorText]; |
| 1288 | + |
| 1289 | + [colorStyle applyStyle:textView.selectedRange color:color]; |
1282 | 1290 | [self anyTextMayHaveBeenModified]; |
1283 | 1291 | } |
1284 | 1292 |
|
@@ -1659,13 +1667,15 @@ - (void)textViewDidBeginEditing:(UITextView *)textView { |
1659 | 1667 | if (_emitFocusBlur) { |
1660 | 1668 | emitter->onInputFocus({}); |
1661 | 1669 | } |
1662 | | - |
1663 | | - NSString *textAtSelection = [[[NSMutableString alloc] initWithString:textView.textStorage.string] substringWithRange: textView.selectedRange]; |
1664 | | - emitter->onChangeSelection({ |
1665 | | - .start = static_cast<int>(textView.selectedRange.location), |
1666 | | - .end = static_cast<int>(textView.selectedRange.location + textView.selectedRange.length), |
1667 | | - .text = [textAtSelection toCppString] |
1668 | | - }); |
| 1670 | + |
| 1671 | + NSString *textAtSelection = |
| 1672 | + [[[NSMutableString alloc] initWithString:textView.textStorage.string] |
| 1673 | + substringWithRange:textView.selectedRange]; |
| 1674 | + emitter->onChangeSelection( |
| 1675 | + {.start = static_cast<int>(textView.selectedRange.location), |
| 1676 | + .end = static_cast<int>(textView.selectedRange.location + |
| 1677 | + textView.selectedRange.length), |
| 1678 | + .text = [textAtSelection toCppString]}); |
1669 | 1679 | [self emitCurrentSelectionColorIfChanged]; |
1670 | 1680 | } |
1671 | 1681 | // manage selection changes since textViewDidChangeSelection sometimes doesn't |
|
0 commit comments