diff --git a/apple/MarkdownTextFieldObserver.h b/apple/MarkdownTextFieldObserver.h index c9a1b0b8..f063c30f 100644 --- a/apple/MarkdownTextFieldObserver.h +++ b/apple/MarkdownTextFieldObserver.h @@ -6,11 +6,7 @@ NS_ASSUME_NONNULL_BEGIN @interface MarkdownTextFieldObserver : NSObject -@property (nonatomic, nullable, strong) RCTMarkdownUtils *markdownUtils; - -@property (nonatomic, nullable, strong) RCTUITextField *textField; - -@property (nonatomic) BOOL active; +- (instancetype)initWithTextField:(nonnull RCTUITextField *)textField markdownUtils:(nonnull RCTMarkdownUtils *)markdownUtils; - (void)textFieldDidChange:(UITextField *)textField; diff --git a/apple/MarkdownTextFieldObserver.mm b/apple/MarkdownTextFieldObserver.mm index 7ad54e25..8544c1d0 100644 --- a/apple/MarkdownTextFieldObserver.mm +++ b/apple/MarkdownTextFieldObserver.mm @@ -1,20 +1,33 @@ #import #import "react_native_assert.h" -@implementation MarkdownTextFieldObserver +@implementation MarkdownTextFieldObserver { + RCTUITextField *_textField; + RCTMarkdownUtils *_markdownUtils; + BOOL _active; +} -- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context +- (instancetype)initWithTextField:(nonnull RCTUITextField *)textField markdownUtils:(nonnull RCTMarkdownUtils *)markdownUtils { - react_native_assert(_textField != nil); + if ((self = [super init])) { + react_native_assert(textField != nil); + react_native_assert(markdownUtils != nil); + _textField = textField; + _markdownUtils = markdownUtils; + _active = YES; + } + return self; +} + +- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context +{ if (_active && ([keyPath isEqualToString:@"text"] || [keyPath isEqualToString:@"attributedText"])) { [self textFieldDidChange:_textField]; } } - (void)textFieldDidChange:(__unused UITextField *)textField { - react_native_assert(_markdownUtils != nil); - react_native_assert(_textField != nil); react_native_assert(_textField.defaultTextAttributes != nil); if (_textField.markedTextRange != nil) { diff --git a/apple/MarkdownTextInputDecoratorView.mm b/apple/MarkdownTextInputDecoratorView.mm index d2ef2cdd..bc9cbf4b 100644 --- a/apple/MarkdownTextInputDecoratorView.mm +++ b/apple/MarkdownTextInputDecoratorView.mm @@ -66,10 +66,7 @@ - (void)didMoveToWindow { // make sure `adjustsFontSizeToFitWidth` is disabled, otherwise formatting will be overwritten react_native_assert(_textField.adjustsFontSizeToFitWidth == NO); - _markdownTextFieldObserver = [[MarkdownTextFieldObserver alloc] init]; - _markdownTextFieldObserver.markdownUtils = _markdownUtils; - _markdownTextFieldObserver.textField = _textField; - _markdownTextFieldObserver.active = YES; + _markdownTextFieldObserver = [[MarkdownTextFieldObserver alloc] initWithTextField:_textField markdownUtils:_markdownUtils]; // register observers for future edits [_textField addTarget:_markdownTextFieldObserver action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged]; @@ -84,12 +81,9 @@ - (void)didMoveToWindow { } else if ([backedTextInputView isKindOfClass:[RCTUITextView class]]) { _textView = (RCTUITextView *)backedTextInputView; - _markdownTextStorageDelegate = [[MarkdownTextStorageDelegate alloc] init]; - _markdownTextStorageDelegate.markdownUtils = _markdownUtils; - _markdownTextStorageDelegate.textView = _textView; - // register delegate for future edits react_native_assert(_textView.textStorage.delegate == nil); + _markdownTextStorageDelegate = [[MarkdownTextStorageDelegate alloc] initWithTextView:_textView markdownUtils:_markdownUtils]; _textView.textStorage.delegate = _markdownTextStorageDelegate; #ifdef RCT_NEW_ARCH_ENABLED diff --git a/apple/MarkdownTextStorageDelegate.h b/apple/MarkdownTextStorageDelegate.h index bfdebbbd..e7cb244a 100644 --- a/apple/MarkdownTextStorageDelegate.h +++ b/apple/MarkdownTextStorageDelegate.h @@ -6,9 +6,7 @@ NS_ASSUME_NONNULL_BEGIN @interface MarkdownTextStorageDelegate : NSObject -@property(nonatomic, nullable) RCTMarkdownUtils *markdownUtils; - -@property(nonatomic, nullable, strong) RCTUITextView *textView; +- (instancetype)initWithTextView:(nonnull RCTUITextView *)textView markdownUtils:(nonnull RCTMarkdownUtils *)markdownUtils; @end diff --git a/apple/MarkdownTextStorageDelegate.mm b/apple/MarkdownTextStorageDelegate.mm index ffd13a58..9bcb40dd 100644 --- a/apple/MarkdownTextStorageDelegate.mm +++ b/apple/MarkdownTextStorageDelegate.mm @@ -1,11 +1,24 @@ #import #import "react_native_assert.h" -@implementation MarkdownTextStorageDelegate +@implementation MarkdownTextStorageDelegate { + RCTUITextView *_textView; + RCTMarkdownUtils *_markdownUtils; +} + +- (instancetype)initWithTextView:(nonnull RCTUITextView *)textView markdownUtils:(nonnull RCTMarkdownUtils *)markdownUtils +{ + if ((self = [super init])) { + react_native_assert(textView != nil); + react_native_assert(markdownUtils != nil); + + _textView = textView; + _markdownUtils = markdownUtils; + } + return self; +} - (void)textStorage:(NSTextStorage *)textStorage didProcessEditing:(NSTextStorageEditActions)editedMask range:(NSRange)editedRange changeInLength:(NSInteger)delta { - react_native_assert(_markdownUtils != nil); - react_native_assert(_textView != nil); react_native_assert(_textView.defaultTextAttributes != nil); [_markdownUtils applyFormatting:textStorage withDefaultTextAttributes:_textView.defaultTextAttributes];