-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
86 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#import <UIKit/UIKit.h> | ||
#import <React/RCTUITextField.h> | ||
#import <RNLiveMarkdown/RCTMarkdownUtils.h> | ||
|
||
NS_ASSUME_NONNULL_BEGIN | ||
|
||
@interface MarkdownTextFieldObserver : NSObject | ||
|
||
@property (nonatomic, nullable, strong) RCTMarkdownUtils *markdownUtils; | ||
|
||
@property (nonatomic, nullable, strong) RCTUITextField *textField; | ||
|
||
@property (nonatomic) BOOL active; | ||
|
||
- (void)textFieldDidChange:(UITextField *)textField; | ||
|
||
@end | ||
|
||
NS_ASSUME_NONNULL_END |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#import <RNLiveMarkdown/MarkdownTextFieldObserver.h> | ||
|
||
@implementation MarkdownTextFieldObserver | ||
|
||
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context | ||
{ | ||
react_native_assert(_textField != nil); | ||
|
||
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) { | ||
return; // skip formatting during multi-stage input to avoid breaking internal state | ||
} | ||
|
||
NSMutableAttributedString *attributedText = [textField.attributedText mutableCopy]; | ||
[_markdownUtils applyFormatting:attributedText withDefaultTextAttributes:_textField.defaultTextAttributes]; | ||
|
||
UITextRange *textRange = _textField.selectedTextRange; | ||
_active = NO; // prevent recursion | ||
_textField.attributedText = attributedText; | ||
_active = YES; | ||
[_textField setSelectedTextRange:textRange notifyDelegate:NO]; | ||
} | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters