From 066dd14bb14720db7971bff088c134b8f9eb3677 Mon Sep 17 00:00:00 2001 From: Tomek Zawadzki Date: Tue, 10 Dec 2024 11:49:27 +0100 Subject: [PATCH] Minor improvements in iOS codebase (#575) --- apple/MarkdownCommitHook.mm | 4 ++-- apple/MarkdownFormatter.h | 2 +- apple/MarkdownFormatter.mm | 4 ++-- apple/MarkdownParser.h | 3 ++- apple/MarkdownParser.mm | 4 +++- apple/RCTBackedTextFieldDelegateAdapter+Markdown.mm | 2 +- apple/RCTBaseTextInputView+Markdown.mm | 4 ++-- apple/RCTMarkdownUtils.h | 3 ++- apple/RCTMarkdownUtils.mm | 11 ++++++----- apple/RCTTextInputComponentView+Markdown.mm | 4 ++-- apple/RCTUITextView+Markdown.mm | 2 +- example/ios/Podfile.lock | 8 ++++---- 12 files changed, 28 insertions(+), 23 deletions(-) diff --git a/apple/MarkdownCommitHook.mm b/apple/MarkdownCommitHook.mm index f98f12194..9ff5be224 100644 --- a/apple/MarkdownCommitHook.mm +++ b/apple/MarkdownCommitHook.mm @@ -198,7 +198,7 @@ // apply markdown auto newString = [usedUtils parseMarkdown:nsAttributedString - withAttributes:defaultNSTextAttributes]; + withDefaultTextAttributes:defaultNSTextAttributes]; // create a clone of the old TextInputState and update the // attributed string box to point to the string with markdown @@ -247,7 +247,7 @@ // apply markdown auto newString = [usedUtils parseMarkdown:nsAttributedString - withAttributes:defaultNSTextAttributes]; + withDefaultTextAttributes:defaultNSTextAttributes]; // create a clone of the old TextInputState and update the // attributed string box to point to the string with markdown diff --git a/apple/MarkdownFormatter.h b/apple/MarkdownFormatter.h index 1f5b1a2e1..1cf86c851 100644 --- a/apple/MarkdownFormatter.h +++ b/apple/MarkdownFormatter.h @@ -9,7 +9,7 @@ const NSAttributedStringKey RCTLiveMarkdownBlockquoteDepthAttributeName = @"RCTL @interface MarkdownFormatter : NSObject - (nonnull NSAttributedString *)format:(nonnull NSString *)text - withAttributes:(nullable NSDictionary*)attributes + withDefaultTextAttributes:(nonnull NSDictionary *)defaultTextAttributes withMarkdownRanges:(nonnull NSArray *)markdownRanges withMarkdownStyle:(nonnull RCTMarkdownStyle *)markdownStyle; diff --git a/apple/MarkdownFormatter.mm b/apple/MarkdownFormatter.mm index 642b487bb..b84200c97 100644 --- a/apple/MarkdownFormatter.mm +++ b/apple/MarkdownFormatter.mm @@ -4,11 +4,11 @@ @implementation MarkdownFormatter - (nonnull NSAttributedString *)format:(nonnull NSString *)text - withAttributes:(nullable NSDictionary *)attributes + withDefaultTextAttributes:(nonnull NSDictionary *)defaultTextAttributes withMarkdownRanges:(nonnull NSArray *)markdownRanges withMarkdownStyle:(nonnull RCTMarkdownStyle *)markdownStyle { - NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:text attributes:attributes]; + NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:text attributes:defaultTextAttributes]; [attributedString beginEditing]; diff --git a/apple/MarkdownParser.h b/apple/MarkdownParser.h index 7ec8d1951..407e49b07 100644 --- a/apple/MarkdownParser.h +++ b/apple/MarkdownParser.h @@ -5,7 +5,8 @@ NS_ASSUME_NONNULL_BEGIN @interface MarkdownParser : NSObject -- (NSArray *)parse:(NSString *)text withParserId:(NSNumber *)parserId; +- (NSArray *)parse:(nonnull NSString *)text + withParserId:(nonnull NSNumber *)parserId; NS_ASSUME_NONNULL_END diff --git a/apple/MarkdownParser.mm b/apple/MarkdownParser.mm index 739f106d4..4e96050a1 100644 --- a/apple/MarkdownParser.mm +++ b/apple/MarkdownParser.mm @@ -9,7 +9,9 @@ @implementation MarkdownParser { NSArray *_prevMarkdownRanges; } -- (NSArray *)parse:(NSString *)text withParserId:(nonnull NSNumber *)parserId { +- (NSArray *)parse:(nonnull NSString *)text + withParserId:(nonnull NSNumber *)parserId +{ @synchronized (self) { if ([text isEqualToString:_prevText] && [parserId isEqualToNumber:_prevParserId]) { return _prevMarkdownRanges; diff --git a/apple/RCTBackedTextFieldDelegateAdapter+Markdown.mm b/apple/RCTBackedTextFieldDelegateAdapter+Markdown.mm index 11c3baf8b..d4d8f8214 100644 --- a/apple/RCTBackedTextFieldDelegateAdapter+Markdown.mm +++ b/apple/RCTBackedTextFieldDelegateAdapter+Markdown.mm @@ -19,7 +19,7 @@ - (void)markdown_textFieldDidChange if (markdownUtils != nil) { RCTUITextField *backedTextInputView = [self valueForKey:@"_backedTextInputView"]; UITextRange *range = backedTextInputView.selectedTextRange; - backedTextInputView.attributedText = [markdownUtils parseMarkdown:backedTextInputView.attributedText withAttributes:backedTextInputView.defaultTextAttributes]; + backedTextInputView.attributedText = [markdownUtils parseMarkdown:backedTextInputView.attributedText withDefaultTextAttributes:backedTextInputView.defaultTextAttributes]; [backedTextInputView setSelectedTextRange:range notifyDelegate:YES]; } diff --git a/apple/RCTBaseTextInputView+Markdown.mm b/apple/RCTBaseTextInputView+Markdown.mm index 7662d5455..ec4200682 100644 --- a/apple/RCTBaseTextInputView+Markdown.mm +++ b/apple/RCTBaseTextInputView+Markdown.mm @@ -16,7 +16,7 @@ - (void)markdown_setAttributedText:(NSAttributedString *)attributedText { RCTMarkdownUtils *markdownUtils = [self getMarkdownUtils]; if (markdownUtils != nil) { - attributedText = [markdownUtils parseMarkdown:attributedText withAttributes:self.backedTextInputView.defaultTextAttributes]; + attributedText = [markdownUtils parseMarkdown:attributedText withDefaultTextAttributes:self.backedTextInputView.defaultTextAttributes]; } // Call the original method @@ -46,7 +46,7 @@ - (void)markdown_updateLocalData if (markdownUtils != nil) { id backedTextInputView = self.backedTextInputView; NSAttributedString *oldAttributedText = backedTextInputView.attributedText; - NSAttributedString *newAttributedText = [markdownUtils parseMarkdown:oldAttributedText withAttributes:backedTextInputView.defaultTextAttributes]; + NSAttributedString *newAttributedText = [markdownUtils parseMarkdown:oldAttributedText withDefaultTextAttributes:backedTextInputView.defaultTextAttributes]; UITextRange *range = backedTextInputView.selectedTextRange; // update attributed text without emitting onSelectionChange event diff --git a/apple/RCTMarkdownUtils.h b/apple/RCTMarkdownUtils.h index ea1264722..fed14596d 100644 --- a/apple/RCTMarkdownUtils.h +++ b/apple/RCTMarkdownUtils.h @@ -8,7 +8,8 @@ NS_ASSUME_NONNULL_BEGIN @property (nonatomic) RCTMarkdownStyle *markdownStyle; @property (nonatomic) NSNumber *parserId; -- (NSAttributedString *)parseMarkdown:(nullable NSAttributedString *)input withAttributes:(nullable NSDictionary*)attributes; +- (NSAttributedString *)parseMarkdown:(nullable NSAttributedString *)input + withDefaultTextAttributes:(nonnull NSDictionary *)defaultTextAttributes; @end diff --git a/apple/RCTMarkdownUtils.mm b/apple/RCTMarkdownUtils.mm index 3c90238be..a932e572c 100644 --- a/apple/RCTMarkdownUtils.mm +++ b/apple/RCTMarkdownUtils.mm @@ -7,7 +7,7 @@ @implementation RCTMarkdownUtils { MarkdownFormatter *_markdownFormatter; NSString *_prevInputString; NSAttributedString *_prevAttributedString; - NSDictionary *_prevTextAttributes; + NSDictionary *_prevDefaultTextAttributes; __weak RCTMarkdownStyle *_prevMarkdownStyle; __weak NSNumber *_prevParserId; } @@ -22,7 +22,8 @@ - (instancetype)init return self; } -- (NSAttributedString *)parseMarkdown:(nullable NSAttributedString *)input withAttributes:(nullable NSDictionary *)attributes +- (NSAttributedString *)parseMarkdown:(nullable NSAttributedString *)input + withDefaultTextAttributes:(nonnull NSDictionary *)defaultTextAttributes { @synchronized (self) { if (input == nil) { @@ -30,19 +31,19 @@ - (NSAttributedString *)parseMarkdown:(nullable NSAttributedString *)input withA } NSString *inputString = [input string]; - if ([inputString isEqualToString:_prevInputString] && [attributes isEqualToDictionary:_prevTextAttributes] && [_markdownStyle isEqual:_prevMarkdownStyle] && [_parserId isEqualToNumber:_prevParserId]) { + if ([inputString isEqualToString:_prevInputString] && [defaultTextAttributes isEqualToDictionary:_prevDefaultTextAttributes] && [_markdownStyle isEqual:_prevMarkdownStyle] && [_parserId isEqualToNumber:_prevParserId]) { return _prevAttributedString; } NSArray *markdownRanges = [_markdownParser parse:inputString withParserId:_parserId]; NSAttributedString *attributedString = [_markdownFormatter format:inputString - withAttributes:attributes + withDefaultTextAttributes:defaultTextAttributes withMarkdownRanges:markdownRanges withMarkdownStyle:_markdownStyle]; _prevInputString = inputString; _prevAttributedString = attributedString; - _prevTextAttributes = attributes; + _prevDefaultTextAttributes = defaultTextAttributes; _prevMarkdownStyle = _markdownStyle; _prevParserId = _parserId; diff --git a/apple/RCTTextInputComponentView+Markdown.mm b/apple/RCTTextInputComponentView+Markdown.mm index 5ce1e63e0..6570097c1 100644 --- a/apple/RCTTextInputComponentView+Markdown.mm +++ b/apple/RCTTextInputComponentView+Markdown.mm @@ -18,7 +18,7 @@ - (void)setMarkdownUtils:(RCTMarkdownUtils *)markdownUtils { if (markdownUtils != nil) { // force Markdown formatting on first render because `_setAttributedText` is called before `setMarkdownUtils` RCTUITextField *backedTextInputView = [self getBackedTextInputView]; - backedTextInputView.attributedText = [markdownUtils parseMarkdown:backedTextInputView.attributedText withAttributes:backedTextInputView.defaultTextAttributes]; + backedTextInputView.attributedText = [markdownUtils parseMarkdown:backedTextInputView.attributedText withDefaultTextAttributes:backedTextInputView.defaultTextAttributes]; } } @@ -36,7 +36,7 @@ - (void)markdown__setAttributedString:(NSAttributedString *)attributedString RCTMarkdownUtils *markdownUtils = [self getMarkdownUtils]; RCTUITextField *backedTextInputView = [self getBackedTextInputView]; if (markdownUtils != nil && backedTextInputView != nil) { - attributedString = [markdownUtils parseMarkdown:attributedString withAttributes:backedTextInputView.defaultTextAttributes]; + attributedString = [markdownUtils parseMarkdown:attributedString withDefaultTextAttributes:backedTextInputView.defaultTextAttributes]; } else { // If markdownUtils is undefined, the text input hasn't been mounted yet. It will // update its state with the unformatted attributed string, we want to prevent displaying diff --git a/apple/RCTUITextView+Markdown.mm b/apple/RCTUITextView+Markdown.mm index 70f2d8820..5a49abe95 100644 --- a/apple/RCTUITextView+Markdown.mm +++ b/apple/RCTUITextView+Markdown.mm @@ -17,7 +17,7 @@ - (void)markdown_textDidChange RCTMarkdownUtils *markdownUtils = [self getMarkdownUtils]; if (markdownUtils != nil) { UITextRange *range = self.selectedTextRange; - super.attributedText = [markdownUtils parseMarkdown:self.attributedText withAttributes:self.defaultTextAttributes]; + super.attributedText = [markdownUtils parseMarkdown:self.attributedText withDefaultTextAttributes:self.defaultTextAttributes]; [super setSelectedTextRange:range]; // prevents cursor from jumping at the end when typing in the middle of the text self.typingAttributes = self.defaultTextAttributes; // removes indent in new line when typing after blockquote } diff --git a/example/ios/Podfile.lock b/example/ios/Podfile.lock index f3bb3529c..aba22425e 100644 --- a/example/ios/Podfile.lock +++ b/example/ios/Podfile.lock @@ -1497,7 +1497,7 @@ PODS: - React-logger (= 0.75.3) - React-perflogger (= 0.75.3) - React-utils (= 0.75.3) - - RNLiveMarkdown (0.1.199): + - RNLiveMarkdown (0.1.203): - DoubleConversion - glog - hermes-engine @@ -1517,10 +1517,10 @@ PODS: - ReactCodegen - ReactCommon/turbomodule/bridging - ReactCommon/turbomodule/core - - RNLiveMarkdown/newarch (= 0.1.199) + - RNLiveMarkdown/newarch (= 0.1.203) - RNReanimated/worklets - Yoga - - RNLiveMarkdown/newarch (0.1.199): + - RNLiveMarkdown/newarch (0.1.203): - DoubleConversion - glog - hermes-engine @@ -1897,7 +1897,7 @@ SPEC CHECKSUMS: React-utils: f2afa6acd905ca2ce7bb8ffb4a22f7f8a12534e8 ReactCodegen: e35c23cdd36922f6d2990c6c1f1b022ade7ad74d ReactCommon: 289214026502e6a93484f4a46bcc0efa4f3f2864 - RNLiveMarkdown: 18dd4ceada29d66a6b7c29b1b0df589e2fc82183 + RNLiveMarkdown: ed779eaf35a346f5254079b825a13f0a032ba64a RNReanimated: ab6c33a61e90c4cbe5dbcbe65bd6c7cb3be167e6 SocketRocket: abac6f5de4d4d62d24e11868d7a2f427e0ef940d Yoga: 1354c027ab07c7736f99a3bef16172d6f1b12b47