Skip to content

Commit

Permalink
Add nice constructors for MarkdownTextStorageDelegate and `Markdown…
Browse files Browse the repository at this point in the history
…TextFieldObserver`
  • Loading branch information
tomekzaw committed Nov 5, 2024
1 parent 9ff01d5 commit 40abeba
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 24 deletions.
6 changes: 1 addition & 5 deletions apple/MarkdownTextFieldObserver.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
23 changes: 18 additions & 5 deletions apple/MarkdownTextFieldObserver.mm
Original file line number Diff line number Diff line change
@@ -1,20 +1,33 @@
#import <RNLiveMarkdown/MarkdownTextFieldObserver.h>
#import "react_native_assert.h"

@implementation MarkdownTextFieldObserver
@implementation MarkdownTextFieldObserver {
RCTUITextField *_textField;
RCTMarkdownUtils *_markdownUtils;
BOOL _active;
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)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<NSKeyValueChangeKey,id> *)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) {
Expand Down
10 changes: 2 additions & 8 deletions apple/MarkdownTextInputDecoratorView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand All @@ -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
Expand Down
4 changes: 1 addition & 3 deletions apple/MarkdownTextStorageDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ NS_ASSUME_NONNULL_BEGIN

@interface MarkdownTextStorageDelegate : NSObject <NSTextStorageDelegate>

@property(nonatomic, nullable) RCTMarkdownUtils *markdownUtils;

@property(nonatomic, nullable, strong) RCTUITextView *textView;
- (instancetype)initWithTextView:(nonnull RCTUITextView *)textView markdownUtils:(nonnull RCTMarkdownUtils *)markdownUtils;

@end

Expand Down
19 changes: 16 additions & 3 deletions apple/MarkdownTextStorageDelegate.mm
Original file line number Diff line number Diff line change
@@ -1,11 +1,24 @@
#import <RNLiveMarkdown/MarkdownTextStorageDelegate.h>
#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];
Expand Down

0 comments on commit 40abeba

Please sign in to comment.