-
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.
Fix cursor position in new line after blockquote
- Loading branch information
Showing
3 changed files
with
94 additions
and
0 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,12 @@ | ||
#import <React/RCTBackedTextInputDelegate.h> | ||
#import <React/RCTUITextView.h> | ||
|
||
NS_ASSUME_NONNULL_BEGIN | ||
|
||
@interface MarkdownBackedTextInputDelegate : NSObject <RCTBackedTextInputDelegate> | ||
|
||
- (instancetype)initWithTextView:(RCTUITextView *)textView; | ||
|
||
@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,76 @@ | ||
#import "MarkdownBackedTextInputDelegate.h" | ||
|
||
@implementation MarkdownBackedTextInputDelegate { | ||
__weak RCTUITextView *_textView; | ||
id<RCTBackedTextInputDelegate> _originalTextInputDelegate; | ||
} | ||
|
||
- (instancetype)initWithTextView:(RCTUITextView *)textView | ||
{ | ||
if (self = [super init]) { | ||
_textView = textView; | ||
_originalTextInputDelegate = _textView.textInputDelegate; | ||
_textView.textInputDelegate = self; | ||
} | ||
return self; | ||
} | ||
|
||
- (void)dealloc | ||
{ | ||
// Restore original text input delegate | ||
_textView.textInputDelegate = _originalTextInputDelegate; | ||
} | ||
|
||
- (void)textInputDidChange | ||
{ | ||
// After adding a newline at the end of the blockquote, the typing attributes in the new line | ||
// still contain NSParagraphStyle with non-zero firstLineHeadIndent and headIntent. | ||
// This causes the cursor to be shifted to the right instead of being located at the beginning of the line. | ||
// The following code removes NSParagraphStyle from typing attributes to fix the position of the cursor. | ||
NSMutableDictionary *typingAttributes = [_textView.typingAttributes mutableCopy]; | ||
[typingAttributes removeObjectForKey:NSParagraphStyleAttributeName]; | ||
_textView.typingAttributes = typingAttributes; | ||
|
||
// Delegate the call to the original text input delegate | ||
[_originalTextInputDelegate textInputDidChange]; | ||
} | ||
|
||
// Delegate all remaining calls to the original text input delegate | ||
|
||
- (void)textInputDidBeginEditing { | ||
[_originalTextInputDelegate textInputDidBeginEditing]; | ||
} | ||
|
||
- (void)textInputDidChangeSelection { | ||
[_originalTextInputDelegate textInputDidChangeSelection]; | ||
} | ||
|
||
- (void)textInputDidEndEditing { | ||
[_originalTextInputDelegate textInputDidEndEditing]; | ||
} | ||
|
||
- (void)textInputDidReturn { | ||
[_originalTextInputDelegate textInputDidReturn]; | ||
} | ||
|
||
- (BOOL)textInputShouldBeginEditing { | ||
return [_originalTextInputDelegate textInputShouldBeginEditing]; | ||
} | ||
|
||
- (nonnull NSString *)textInputShouldChangeText:(nonnull NSString *)text inRange:(NSRange)range { | ||
return [_originalTextInputDelegate textInputShouldChangeText:text inRange:range]; | ||
} | ||
|
||
- (BOOL)textInputShouldEndEditing { | ||
return [_originalTextInputDelegate textInputShouldEndEditing]; | ||
} | ||
|
||
- (BOOL)textInputShouldReturn { | ||
return [_originalTextInputDelegate textInputShouldReturn]; | ||
} | ||
|
||
- (BOOL)textInputShouldSubmitOnReturn { | ||
return [_originalTextInputDelegate textInputShouldSubmitOnReturn]; | ||
} | ||
|
||
@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