Skip to content

Commit bc558d9

Browse files
committed
Improve performance on iOS
1 parent 377ab57 commit bc558d9

11 files changed

+212
-255
lines changed

ios/MarkdownTextInputDecoratorView.mm

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
#import <RNLiveMarkdown/MarkdownLayoutManager.h>
55
#import <RNLiveMarkdown/MarkdownTextInputDecoratorView.h>
6-
#import <RNLiveMarkdown/RCTBackedTextFieldDelegateAdapter+Markdown.h>
76
#import <RNLiveMarkdown/RCTUITextView+Markdown.h>
87

98
#ifdef RCT_NEW_ARCH_ENABLED
@@ -23,7 +22,6 @@ @implementation MarkdownTextInputDecoratorView {
2322
__weak RCTBaseTextInputView *_textInput;
2423
#endif /* RCT_NEW_ARCH_ENABLED */
2524
__weak UIView<RCTBackedTextInputViewProtocol> *_backedTextInputView;
26-
__weak RCTBackedTextFieldDelegateAdapter *_adapter;
2725
__weak RCTUITextView *_textView;
2826
}
2927

@@ -64,18 +62,14 @@ - (void)didMoveToWindow {
6462
[_markdownUtils setMarkdownStyle:_markdownStyle];
6563

6664
[_textInput setMarkdownUtils:_markdownUtils];
67-
if ([_backedTextInputView isKindOfClass:[RCTUITextField class]]) {
68-
RCTUITextField *textField = (RCTUITextField *)_backedTextInputView;
69-
_adapter = [textField valueForKey:@"textInputDelegateAdapter"];
70-
[_adapter setMarkdownUtils:_markdownUtils];
71-
} else if ([_backedTextInputView isKindOfClass:[RCTUITextView class]]) {
65+
if ([_backedTextInputView isKindOfClass:[RCTUITextView class]]) {
7266
_textView = (RCTUITextView *)_backedTextInputView;
7367
[_textView setMarkdownUtils:_markdownUtils];
7468
NSLayoutManager *layoutManager = _textView.layoutManager; // switching to TextKit 1 compatibility mode
7569
layoutManager.allowsNonContiguousLayout = NO; // workaround for onScroll issue
7670
object_setClass(layoutManager, [MarkdownLayoutManager class]);
7771
[layoutManager setValue:_markdownUtils forKey:@"markdownUtils"];
78-
} else {
72+
} else if (![_backedTextInputView isKindOfClass:[RCTUITextField class]]) {
7973
react_native_assert(false && "Cannot enable Markdown for this type of TextInput.");
8074
}
8175
}
@@ -85,9 +79,6 @@ - (void)willMoveToWindow:(UIWindow *)newWindow
8579
if (_textInput != nil) {
8680
[_textInput setMarkdownUtils:nil];
8781
}
88-
if (_adapter != nil) {
89-
[_adapter setMarkdownUtils:nil];
90-
}
9182
if (_textView != nil) {
9283
[_textView setMarkdownUtils:nil];
9384
if (_textView.layoutManager != nil && [object_getClass(_textView.layoutManager) isEqual:[MarkdownLayoutManager class]]) {
@@ -103,11 +94,15 @@ - (void)setMarkdownStyle:(RCTMarkdownStyle *)markdownStyle
10394
[_markdownUtils setMarkdownStyle:markdownStyle];
10495

10596
// apply new styles
97+
if (_textView != nil) {
98+
[_textView performSelector:@selector(textDidChange)];
99+
} else {
106100
#ifdef RCT_NEW_ARCH_ENABLED
107-
[_textInput _setAttributedString:_backedTextInputView.attributedText];
101+
[_textInput _setAttributedString:_backedTextInputView.attributedText];
108102
#else
109-
[_textInput setAttributedText:_textInput.attributedText];
103+
[_textInput setAttributedText:_textInput.attributedText];
110104
#endif /* RCT_NEW_ARCH_ENABLED */
105+
}
111106
}
112107

113108
@end

ios/RCTBackedTextFieldDelegateAdapter+Markdown.h

Lines changed: 0 additions & 14 deletions
This file was deleted.

ios/RCTBackedTextFieldDelegateAdapter+Markdown.mm

Lines changed: 0 additions & 43 deletions
This file was deleted.

ios/RCTBaseTextInputView+Markdown.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
1+
// This guard prevent this file to be compiled in the new architecture.
2+
#ifndef RCT_NEW_ARCH_ENABLED
3+
14
#import <React/RCTBaseTextInputView.h>
25
#import <RNLiveMarkdown/RCTMarkdownUtils.h>
36

47
NS_ASSUME_NONNULL_BEGIN
58

9+
@interface RCTBaseTextInputView (Private)
10+
- (BOOL)textOf:(NSAttributedString *)newText equals:(NSAttributedString *)oldText;
11+
@end
12+
613
@interface RCTBaseTextInputView (Markdown)
714

815
@property(nonatomic, nullable, getter=getMarkdownUtils) RCTMarkdownUtils *markdownUtils;
@@ -11,8 +18,8 @@ NS_ASSUME_NONNULL_BEGIN
1118

1219
- (BOOL)markdown_textOf:(NSAttributedString *)newText equals:(NSAttributedString *)oldText;
1320

14-
- (void)markdown_updateLocalData;
15-
1621
@end
1722

1823
NS_ASSUME_NONNULL_END
24+
25+
#endif /* RCT_NEW_ARCH_ENABLED */

ios/RCTBaseTextInputView+Markdown.mm

Lines changed: 12 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
// This guard prevent this file to be compiled in the new architecture.
2+
#ifndef RCT_NEW_ARCH_ENABLED
3+
4+
#import <React/RCTUITextField.h>
5+
#import <RNLiveMarkdown/RCTUITextView+Markdown.h>
16
#import <RNLiveMarkdown/RCTBaseTextInputView+Markdown.h>
27
#import <RNLiveMarkdown/RCTMarkdownUtils.h>
38
#import <objc/message.h>
@@ -14,9 +19,11 @@ - (RCTMarkdownUtils *)getMarkdownUtils {
1419

1520
- (void)markdown_setAttributedText:(NSAttributedString *)attributedText
1621
{
17-
RCTMarkdownUtils *markdownUtils = [self getMarkdownUtils];
18-
if (markdownUtils != nil) {
19-
attributedText = [markdownUtils parseMarkdown:attributedText withAttributes:self.backedTextInputView.defaultTextAttributes];
22+
if (![self.backedTextInputView isKindOfClass:[RCTUITextView class]]) {
23+
RCTMarkdownUtils *markdownUtils = [self getMarkdownUtils];
24+
if (markdownUtils != nil) {
25+
attributedText = [markdownUtils parseMarkdown:attributedText withAttributes:self.backedTextInputView.defaultTextAttributes];
26+
}
2027
}
2128

2229
// Call the original method
@@ -33,29 +40,6 @@ - (BOOL)markdown_textOf:(NSAttributedString *)newText equals:(NSAttributedString
3340
return [self markdown_textOf:newText equals:oldText];
3441
}
3542

36-
- (void)markdown_updateLocalData
37-
{
38-
RCTMarkdownUtils *markdownUtils = [self getMarkdownUtils];
39-
if (markdownUtils != nil) {
40-
id<RCTBackedTextInputViewProtocol> backedTextInputView = self.backedTextInputView;
41-
NSAttributedString *oldAttributedText = backedTextInputView.attributedText;
42-
NSAttributedString *newAttributedText = [markdownUtils parseMarkdown:oldAttributedText withAttributes:backedTextInputView.defaultTextAttributes];
43-
UITextRange *range = backedTextInputView.selectedTextRange;
44-
45-
// update attributed text without emitting onSelectionChange event
46-
id<RCTBackedTextInputDelegate> delegate = backedTextInputView.textInputDelegate;
47-
backedTextInputView.textInputDelegate = nil;
48-
[backedTextInputView setAttributedText:newAttributedText];
49-
backedTextInputView.textInputDelegate = delegate;
50-
51-
// restore original selection and emit onSelectionChange event
52-
[backedTextInputView setSelectedTextRange:range notifyDelegate:YES];
53-
}
54-
55-
// Call the original method
56-
[self markdown_updateLocalData];
57-
}
58-
5943
+ (void)load
6044
{
6145
static dispatch_once_t onceToken;
@@ -71,15 +55,6 @@ + (void)load
7155
method_exchangeImplementations(originalMethod, swizzledMethod);
7256
}
7357

74-
{
75-
// swizzle updateLocalData
76-
SEL originalSelector = @selector(updateLocalData);
77-
SEL swizzledSelector = @selector(markdown_updateLocalData);
78-
Method originalMethod = class_getInstanceMethod(cls, originalSelector);
79-
Method swizzledMethod = class_getInstanceMethod(cls, swizzledSelector);
80-
method_exchangeImplementations(originalMethod, swizzledMethod);
81-
}
82-
8358
{
8459
// swizzle textOf
8560
SEL originalSelector = @selector(textOf:equals:);
@@ -92,3 +67,5 @@ + (void)load
9267
}
9368

9469
@end
70+
71+
#endif /* RCT_NEW_ARCH_ENABLED */

ios/RCTMarkdownUtils.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ NS_ASSUME_NONNULL_BEGIN
99
@property (nonatomic) NSMutableArray<NSDictionary *> *blockquoteRangesAndLevels;
1010

1111
- (NSAttributedString *)parseMarkdown:(nullable NSAttributedString *)input withAttributes:(nullable NSDictionary<NSAttributedStringKey, id>*)attributes;
12+
- (void)parseMarkdown:(nullable NSMutableAttributedString *)attributedString;
1213

1314
@end
1415

0 commit comments

Comments
 (0)