Skip to content

Commit

Permalink
Enable Markdown only for selected TextInput
Browse files Browse the repository at this point in the history
  • Loading branch information
tomekzaw committed Dec 6, 2023
1 parent 167720e commit 429438e
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 14 deletions.
27 changes: 26 additions & 1 deletion example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,42 @@ export default function App() {

return (
<View style={styles.container}>
<Text>MarkdownTextInput singleline</Text>
<MarkdownTextInput
multiline
autoCapitalize="none"
value={value}
onChangeText={setValue}
style={styles.input}
ref={ref}
/>
<Text>MarkdownTextInput multiline</Text>
<MarkdownTextInput
multiline
autoCapitalize="none"
value={value}
onChangeText={setValue}
style={styles.input}
/>
<Text>TextInput singleline</Text>
<TextInput
autoCapitalize="none"
value={value}
onChangeText={setValue}
style={styles.input}
/>
<Text>TextInput multiline</Text>
<TextInput
multiline
autoCapitalize="none"
value={value}
onChangeText={setValue}
style={styles.input}
/>
<Text style={styles.text}>{JSON.stringify(value)}</Text>
<Button title="Focus" onPress={() => ref.current?.focus()} />
<Button title="Blur" onPress={() => ref.current?.blur()} />
<Button title="Reset" onPress={() => setValue('Hello, *world*!')} />
<Button title="Clear" onPress={() => setValue('')} />
</View>
);
}
Expand Down
28 changes: 25 additions & 3 deletions ios/MarkdownTextInputViewManager.mm
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
#import <React/RCTViewManager.h>
#import <React/RCTUIManager.h>
#import <React/RCTUITextField.h>
#import <React/RCTBaseTextInputView.h>
#import <objc/runtime.h>
#import "RCTBridge.h"
#import "Utils.h"

#import <react-native-markdown-text-input/RCTBackedTextFieldDelegateAdapter+Markdown.h>
#import <react-native-markdown-text-input/RCTBaseTextInputView+Markdown.h>
#import <react-native-markdown-text-input/RCTUITextView+Markdown.h>
#import <react-native-markdown-text-input/RCTMarkdownUtils.h>

@interface MarkdownTextInputView : UIView
Expand All @@ -16,11 +22,27 @@ - (void)didMoveToWindow {
if (currentIndex == 0 || currentIndex == NSNotFound) {
return;
}
UIView *found = [viewsArray objectAtIndex:currentIndex - 1];
// TODO: enable Markdown only for this specific view
return;
UIView *view = [viewsArray objectAtIndex:currentIndex - 1];
if (![view isKindOfClass:[RCTBaseTextInputView class]]) {
return;
}
RCTBaseTextInputView *textInput = (RCTBaseTextInputView *)view;
[textInput setMarkdownEnabled:YES];
UIView<RCTBackedTextInputViewProtocol> *backedTextInputView = textInput.backedTextInputView;
if ([backedTextInputView isKindOfClass:[RCTUITextField class]]) {
RCTUITextField *textField = (RCTUITextField *)backedTextInputView;
RCTBackedTextFieldDelegateAdapter *adapter = [textField valueForKey:@"textInputDelegateAdapter"];
[adapter setMarkdownEnabled:YES];
} else if ([backedTextInputView isKindOfClass:[RCTUITextView class]]) {
RCTUITextView *textView = (RCTUITextView *)backedTextInputView;
[textView setMarkdownEnabled:YES];
} else {
NSLog(@"Cannot enable Markdown for this type of TextInput.");
}
}

// TODO: call setMarkdownEnabled:NO

@end

@interface MarkdownTextInputViewManager : RCTViewManager
Expand Down
2 changes: 2 additions & 0 deletions ios/RCTBackedTextFieldDelegateAdapter+Markdown.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ NS_ASSUME_NONNULL_BEGIN

@interface RCTBackedTextFieldDelegateAdapter (Markdown)

@property(nonatomic, getter=isMarkdownEnabled) BOOL markdownEnabled;

- (void)markdown_textFieldDidChange;

@end
Expand Down
20 changes: 16 additions & 4 deletions ios/RCTBackedTextFieldDelegateAdapter+Markdown.m
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,24 @@

@implementation RCTBackedTextFieldDelegateAdapter (Markdown)

- (void)setMarkdownEnabled:(BOOL)markdownEnabled {
NSNumber *markdownEnabledNumber = [NSNumber numberWithBool:markdownEnabled];
objc_setAssociatedObject(self, @selector(isMarkdownEnabled), markdownEnabledNumber, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (BOOL)isMarkdownEnabled {
NSNumber *markdownEnabledNumber = objc_getAssociatedObject(self, @selector(isMarkdownEnabled));
return [markdownEnabledNumber boolValue];
}

- (void)markdown_textFieldDidChange
{
RCTUITextField *backedTextInputView = [self valueForKey:@"_backedTextInputView"];
UITextRange *range = backedTextInputView.selectedTextRange;
backedTextInputView.attributedText = [RCTMarkdownUtils parseMarkdown:backedTextInputView.attributedText.string];
[backedTextInputView setSelectedTextRange:range notifyDelegate:YES];
if ([self isMarkdownEnabled]) {
RCTUITextField *backedTextInputView = [self valueForKey:@"_backedTextInputView"];
UITextRange *range = backedTextInputView.selectedTextRange;
backedTextInputView.attributedText = [RCTMarkdownUtils parseMarkdown:backedTextInputView.attributedText.string];
[backedTextInputView setSelectedTextRange:range notifyDelegate:YES];
}

// Call the original method
[self markdown_textFieldDidChange];
Expand Down
2 changes: 2 additions & 0 deletions ios/RCTBaseTextInputView+Markdown.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ NS_ASSUME_NONNULL_BEGIN

@interface RCTBaseTextInputView (Markdown)

@property(nonatomic, getter=isMarkdownEnabled) BOOL markdownEnabled;

- (void)markdown_setAttributedText:(NSAttributedString *)attributedText;

@end
Expand Down
12 changes: 11 additions & 1 deletion ios/RCTBaseTextInputView+Markdown.m
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,19 @@

@implementation RCTBaseTextInputView (Markdown)

- (void)setMarkdownEnabled:(BOOL)markdownEnabled {
NSNumber *markdownEnabledNumber = [NSNumber numberWithBool:markdownEnabled];
objc_setAssociatedObject(self, @selector(isMarkdownEnabled), markdownEnabledNumber, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (BOOL)isMarkdownEnabled {
NSNumber *markdownEnabledNumber = objc_getAssociatedObject(self, @selector(isMarkdownEnabled));
return [markdownEnabledNumber boolValue];
}

- (void)markdown_setAttributedText:(NSAttributedString *)attributedText
{
if (attributedText != nil) {
if ([self isMarkdownEnabled] && attributedText != nil) {
attributedText = [RCTMarkdownUtils parseMarkdown:attributedText.string];
}

Expand Down
2 changes: 2 additions & 0 deletions ios/RCTUITextView+Markdown.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ NS_ASSUME_NONNULL_BEGIN

@interface RCTUITextView (Markdown)

@property(nonatomic, getter=isMarkdownEnabled) BOOL markdownEnabled;

- (void)markdown_textDidChange;

@end
Expand Down
22 changes: 17 additions & 5 deletions ios/RCTUITextView+Markdown.mm
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,25 @@

@implementation RCTUITextView (Markdown)

- (void)setMarkdownEnabled:(BOOL)markdownEnabled {
NSNumber *markdownEnabledNumber = [NSNumber numberWithBool:markdownEnabled];
objc_setAssociatedObject(self, @selector(isMarkdownEnabled), markdownEnabledNumber, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (BOOL)isMarkdownEnabled {
NSNumber *markdownEnabledNumber = objc_getAssociatedObject(self, @selector(isMarkdownEnabled));
return [markdownEnabledNumber boolValue];
}

- (void)markdown_textDidChange
{
UITextRange *range = self.selectedTextRange;
super.attributedText = [RCTMarkdownUtils parseMarkdown:self.attributedText.string];
[super setSelectedTextRange:range]; // prevents cursor from jumping at the end when typing in the middle of the text
self.typingAttributes = @{NSFontAttributeName: [UIFont boldSystemFontOfSize:20]}; // removes indent in new line when typing after quote

if ([self isMarkdownEnabled]) {
UITextRange *range = self.selectedTextRange;
super.attributedText = [RCTMarkdownUtils parseMarkdown:self.attributedText.string];
[super setSelectedTextRange:range]; // prevents cursor from jumping at the end when typing in the middle of the text
self.typingAttributes = @{NSFontAttributeName: [UIFont boldSystemFontOfSize:20]}; // removes indent in new line when typing after quote
}

// Call the original method
[self markdown_textDidChange];
}
Expand Down

0 comments on commit 429438e

Please sign in to comment.