Skip to content

Commit

Permalink
Minor improvements in iOS codebase (#575)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomekzaw authored Dec 10, 2024
1 parent c35696d commit 066dd14
Show file tree
Hide file tree
Showing 12 changed files with 28 additions and 23 deletions.
4 changes: 2 additions & 2 deletions apple/MarkdownCommitHook.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion apple/MarkdownFormatter.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const NSAttributedStringKey RCTLiveMarkdownBlockquoteDepthAttributeName = @"RCTL
@interface MarkdownFormatter : NSObject

- (nonnull NSAttributedString *)format:(nonnull NSString *)text
withAttributes:(nullable NSDictionary<NSAttributedStringKey, id>*)attributes
withDefaultTextAttributes:(nonnull NSDictionary<NSAttributedStringKey, id> *)defaultTextAttributes
withMarkdownRanges:(nonnull NSArray<MarkdownRange *> *)markdownRanges
withMarkdownStyle:(nonnull RCTMarkdownStyle *)markdownStyle;

Expand Down
4 changes: 2 additions & 2 deletions apple/MarkdownFormatter.mm
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
@implementation MarkdownFormatter

- (nonnull NSAttributedString *)format:(nonnull NSString *)text
withAttributes:(nullable NSDictionary<NSAttributedStringKey, id> *)attributes
withDefaultTextAttributes:(nonnull NSDictionary<NSAttributedStringKey, id> *)defaultTextAttributes
withMarkdownRanges:(nonnull NSArray<MarkdownRange *> *)markdownRanges
withMarkdownStyle:(nonnull RCTMarkdownStyle *)markdownStyle
{
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:text attributes:attributes];
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:text attributes:defaultTextAttributes];

[attributedString beginEditing];

Expand Down
3 changes: 2 additions & 1 deletion apple/MarkdownParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ NS_ASSUME_NONNULL_BEGIN

@interface MarkdownParser : NSObject

- (NSArray<MarkdownRange *> *)parse:(NSString *)text withParserId:(NSNumber *)parserId;
- (NSArray<MarkdownRange *> *)parse:(nonnull NSString *)text
withParserId:(nonnull NSNumber *)parserId;

NS_ASSUME_NONNULL_END

Expand Down
4 changes: 3 additions & 1 deletion apple/MarkdownParser.mm
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ @implementation MarkdownParser {
NSArray<MarkdownRange *> *_prevMarkdownRanges;
}

- (NSArray<MarkdownRange *> *)parse:(NSString *)text withParserId:(nonnull NSNumber *)parserId {
- (NSArray<MarkdownRange *> *)parse:(nonnull NSString *)text
withParserId:(nonnull NSNumber *)parserId
{
@synchronized (self) {
if ([text isEqualToString:_prevText] && [parserId isEqualToNumber:_prevParserId]) {
return _prevMarkdownRanges;
Expand Down
2 changes: 1 addition & 1 deletion apple/RCTBackedTextFieldDelegateAdapter+Markdown.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}

Expand Down
4 changes: 2 additions & 2 deletions apple/RCTBaseTextInputView+Markdown.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -46,7 +46,7 @@ - (void)markdown_updateLocalData
if (markdownUtils != nil) {
id<RCTBackedTextInputViewProtocol> 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
Expand Down
3 changes: 2 additions & 1 deletion apple/RCTMarkdownUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ NS_ASSUME_NONNULL_BEGIN
@property (nonatomic) RCTMarkdownStyle *markdownStyle;
@property (nonatomic) NSNumber *parserId;

- (NSAttributedString *)parseMarkdown:(nullable NSAttributedString *)input withAttributes:(nullable NSDictionary<NSAttributedStringKey, id>*)attributes;
- (NSAttributedString *)parseMarkdown:(nullable NSAttributedString *)input
withDefaultTextAttributes:(nonnull NSDictionary<NSAttributedStringKey, id> *)defaultTextAttributes;

@end

Expand Down
11 changes: 6 additions & 5 deletions apple/RCTMarkdownUtils.mm
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ @implementation RCTMarkdownUtils {
MarkdownFormatter *_markdownFormatter;
NSString *_prevInputString;
NSAttributedString *_prevAttributedString;
NSDictionary<NSAttributedStringKey, id> *_prevTextAttributes;
NSDictionary<NSAttributedStringKey, id> *_prevDefaultTextAttributes;
__weak RCTMarkdownStyle *_prevMarkdownStyle;
__weak NSNumber *_prevParserId;
}
Expand All @@ -22,27 +22,28 @@ - (instancetype)init
return self;
}

- (NSAttributedString *)parseMarkdown:(nullable NSAttributedString *)input withAttributes:(nullable NSDictionary<NSAttributedStringKey,id> *)attributes
- (NSAttributedString *)parseMarkdown:(nullable NSAttributedString *)input
withDefaultTextAttributes:(nonnull NSDictionary<NSAttributedStringKey, id> *)defaultTextAttributes
{
@synchronized (self) {
if (input == nil) {
return nil;
}

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<MarkdownRange *> *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;

Expand Down
4 changes: 2 additions & 2 deletions apple/RCTTextInputComponentView+Markdown.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}
}

Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion apple/RCTUITextView+Markdown.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
8 changes: 4 additions & 4 deletions example/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -1897,7 +1897,7 @@ SPEC CHECKSUMS:
React-utils: f2afa6acd905ca2ce7bb8ffb4a22f7f8a12534e8
ReactCodegen: e35c23cdd36922f6d2990c6c1f1b022ade7ad74d
ReactCommon: 289214026502e6a93484f4a46bcc0efa4f3f2864
RNLiveMarkdown: 18dd4ceada29d66a6b7c29b1b0df589e2fc82183
RNLiveMarkdown: ed779eaf35a346f5254079b825a13f0a032ba64a
RNReanimated: ab6c33a61e90c4cbe5dbcbe65bd6c7cb3be167e6
SocketRocket: abac6f5de4d4d62d24e11868d7a2f427e0ef940d
Yoga: 1354c027ab07c7736f99a3bef16172d6f1b12b47
Expand Down

0 comments on commit 066dd14

Please sign in to comment.