Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve performance on iOS #356

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 3 additions & 9 deletions ios/MarkdownTextInputDecoratorView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

#import <RNLiveMarkdown/MarkdownLayoutManager.h>
#import <RNLiveMarkdown/MarkdownTextInputDecoratorView.h>
#import <RNLiveMarkdown/RCTBackedTextFieldDelegateAdapter+Markdown.h>
#import <RNLiveMarkdown/RCTUITextView+Markdown.h>

#ifdef RCT_NEW_ARCH_ENABLED
Expand All @@ -23,7 +22,6 @@ @implementation MarkdownTextInputDecoratorView {
__weak RCTBaseTextInputView *_textInput;
#endif /* RCT_NEW_ARCH_ENABLED */
__weak UIView<RCTBackedTextInputViewProtocol> *_backedTextInputView;
__weak RCTBackedTextFieldDelegateAdapter *_adapter;
__weak RCTUITextView *_textView;
}

Expand Down Expand Up @@ -65,9 +63,7 @@ - (void)didMoveToWindow {

[_textInput setMarkdownUtils:_markdownUtils];
if ([_backedTextInputView isKindOfClass:[RCTUITextField class]]) {
RCTUITextField *textField = (RCTUITextField *)_backedTextInputView;
_adapter = [textField valueForKey:@"textInputDelegateAdapter"];
[_adapter setMarkdownUtils:_markdownUtils];
// Do nothing
} else if ([_backedTextInputView isKindOfClass:[RCTUITextView class]]) {
_textView = (RCTUITextView *)_backedTextInputView;
[_textView setMarkdownUtils:_markdownUtils];
Expand All @@ -93,9 +89,6 @@ - (void)willMoveToWindow:(UIWindow *)newWindow
if (_textInput != nil) {
[_textInput setMarkdownUtils:nil];
}
if (_adapter != nil) {
[_adapter setMarkdownUtils:nil];
}
if (_textView != nil) {
[_textView setMarkdownUtils:nil];
if (_textView.layoutManager != nil && [object_getClass(_textView.layoutManager) isEqual:[MarkdownLayoutManager class]]) {
Expand All @@ -110,11 +103,12 @@ - (void)setMarkdownStyle:(RCTMarkdownStyle *)markdownStyle
_markdownStyle = markdownStyle;
[_markdownUtils setMarkdownStyle:markdownStyle];

// apply new styles
if (_textView != nil) {
// We want to use `textStorage` for applying markdown when possible. Currently it's only available for UITextView
[_textView textDidChange];
} else {
// apply new styles
// Set attributed string for all other types of views
#ifdef RCT_NEW_ARCH_ENABLED
[_textInput _setAttributedString:_backedTextInputView.attributedText];
#else
Expand Down
14 changes: 0 additions & 14 deletions ios/RCTBackedTextFieldDelegateAdapter+Markdown.h

This file was deleted.

43 changes: 0 additions & 43 deletions ios/RCTBackedTextFieldDelegateAdapter+Markdown.mm

This file was deleted.

11 changes: 9 additions & 2 deletions ios/RCTBaseTextInputView+Markdown.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
// This guard prevent this file to be compiled in the new architecture.
#ifndef RCT_NEW_ARCH_ENABLED

#import <React/RCTBaseTextInputView.h>
#import <RNLiveMarkdown/RCTMarkdownUtils.h>

NS_ASSUME_NONNULL_BEGIN

@interface RCTBaseTextInputView (Private)
- (BOOL)textOf:(NSAttributedString *)newText equals:(NSAttributedString *)oldText;
@end

@interface RCTBaseTextInputView (Markdown)

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

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

- (void)markdown_updateLocalData;

@end

NS_ASSUME_NONNULL_END

#endif /* RCT_NEW_ARCH_ENABLED */
47 changes: 12 additions & 35 deletions ios/RCTBaseTextInputView+Markdown.mm
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
// This guard prevent this file to be compiled in the new architecture.
#ifndef RCT_NEW_ARCH_ENABLED

#import <React/RCTUITextField.h>
#import <RNLiveMarkdown/RCTUITextView+Markdown.h>
#import <RNLiveMarkdown/RCTBaseTextInputView+Markdown.h>
#import <RNLiveMarkdown/RCTMarkdownUtils.h>
#import <objc/message.h>
Expand All @@ -14,9 +19,11 @@ - (RCTMarkdownUtils *)getMarkdownUtils {

- (void)markdown_setAttributedText:(NSAttributedString *)attributedText
{
RCTMarkdownUtils *markdownUtils = [self getMarkdownUtils];
if (markdownUtils != nil) {
attributedText = [markdownUtils parseMarkdown:attributedText withAttributes:self.backedTextInputView.defaultTextAttributes];
if (![self.backedTextInputView isKindOfClass:[RCTUITextView class]]) {
RCTMarkdownUtils *markdownUtils = [self getMarkdownUtils];
if (markdownUtils != nil) {
attributedText = [markdownUtils parseMarkdown:attributedText withAttributes:self.backedTextInputView.defaultTextAttributes];
}
}

// Call the original method
Expand All @@ -40,29 +47,6 @@ - (BOOL)markdown_textOf:(NSAttributedString *)newText equals:(NSAttributedString
return [self markdown_textOf:newText equals:oldText];
}

- (void)markdown_updateLocalData
{
RCTMarkdownUtils *markdownUtils = [self getMarkdownUtils];
if (markdownUtils != nil) {
id<RCTBackedTextInputViewProtocol> backedTextInputView = self.backedTextInputView;
NSAttributedString *oldAttributedText = backedTextInputView.attributedText;
NSAttributedString *newAttributedText = [markdownUtils parseMarkdown:oldAttributedText withAttributes:backedTextInputView.defaultTextAttributes];
UITextRange *range = backedTextInputView.selectedTextRange;

// update attributed text without emitting onSelectionChange event
id<RCTBackedTextInputDelegate> delegate = backedTextInputView.textInputDelegate;
backedTextInputView.textInputDelegate = nil;
[backedTextInputView setAttributedText:newAttributedText];
backedTextInputView.textInputDelegate = delegate;

// restore original selection and emit onSelectionChange event
[backedTextInputView setSelectedTextRange:range notifyDelegate:YES];
}

// Call the original method
[self markdown_updateLocalData];
}

+ (void)load
{
static dispatch_once_t onceToken;
Expand All @@ -78,15 +62,6 @@ + (void)load
method_exchangeImplementations(originalMethod, swizzledMethod);
}

{
// swizzle updateLocalData
SEL originalSelector = @selector(updateLocalData);
SEL swizzledSelector = @selector(markdown_updateLocalData);
Method originalMethod = class_getInstanceMethod(cls, originalSelector);
Method swizzledMethod = class_getInstanceMethod(cls, swizzledSelector);
method_exchangeImplementations(originalMethod, swizzledMethod);
}

{
// swizzle textOf
SEL originalSelector = @selector(textOf:equals:);
Expand All @@ -99,3 +74,5 @@ + (void)load
}

@end

#endif /* RCT_NEW_ARCH_ENABLED */
3 changes: 2 additions & 1 deletion ios/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) NSMutableArray<NSDictionary *> *blockquoteRangesAndLevels;

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

@end

Expand Down
Loading
Loading