Skip to content

Commit

Permalink
Add nice constructor for MarkdownTextStorageDelegate
Browse files Browse the repository at this point in the history
  • Loading branch information
tomekzaw committed Nov 5, 2024
1 parent 9321419 commit 9a4f995
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
5 changes: 1 addition & 4 deletions apple/MarkdownTextInputDecoratorView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -81,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 {
RCTMarkdownUtils *markdownUtils;
RCTUITextView *textView;
}

- (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 9a4f995

Please sign in to comment.