diff --git a/Libraries/Text/RCTTextField.h b/Libraries/Text/RCTTextField.h index 466a48778..0df80898c 100644 --- a/Libraries/Text/RCTTextField.h +++ b/Libraries/Text/RCTTextField.h @@ -21,7 +21,6 @@ @property (nonatomic, assign) BOOL caretHidden; @property (nonatomic, assign) BOOL selectTextOnFocus; @property (nonatomic, assign) BOOL blurOnSubmit; -@property (nonatomic, assign) NSInteger mostRecentEventCount; @property (nonatomic, strong) NSNumber *maxLength; @property (nonatomic, copy) RCTDirectEventBlock onSelectionChange; diff --git a/Libraries/Text/RCTTextField.m b/Libraries/Text/RCTTextField.m index 99d3ac427..9fe22eb81 100644 --- a/Libraries/Text/RCTTextField.m +++ b/Libraries/Text/RCTTextField.m @@ -27,9 +27,7 @@ @implementation RCTTextField { RCTUITextField *_backedTextInput; - NSInteger _nativeEventCount; BOOL _submitted; - UITextRange *_previousSelectionRange; NSString *_finalText; CGSize _previousContentSize; } @@ -71,26 +69,6 @@ RCT_NOT_IMPLEMENTED(- (instancetype)initWithCoder:(NSCoder *)aDecoder) #pragma mark - Properties -- (void)setSelection:(RCTTextSelection *)selection -{ - if (!selection) { - return; - } - - UITextRange *currentSelection = _backedTextInput.selectedTextRange; - UITextPosition *start = [_backedTextInput positionFromPosition:_backedTextInput.beginningOfDocument offset:selection.start]; - UITextPosition *end = [_backedTextInput positionFromPosition:_backedTextInput.beginningOfDocument offset:selection.end]; - UITextRange *selectedTextRange = [_backedTextInput textRangeFromPosition:start toPosition:end]; - - NSInteger eventLag = _nativeEventCount - _mostRecentEventCount; - if (eventLag == 0 && ![currentSelection isEqual:selectedTextRange]) { - _previousSelectionRange = selectedTextRange; - _backedTextInput.selectedTextRange = selectedTextRange; - } else if (eventLag > RCTTextUpdateLagWarningThreshold) { - RCTLogWarn(@"Native TextInput(%@) is %zd events ahead of JS - try to make your JS faster.", self.text, eventLag); - } -} - - (NSString *)text { return _backedTextInput.text; diff --git a/Libraries/Text/RCTTextInput.h b/Libraries/Text/RCTTextInput.h index a565afc14..c71210e5d 100644 --- a/Libraries/Text/RCTTextInput.h +++ b/Libraries/Text/RCTTextInput.h @@ -20,6 +20,9 @@ @protected RCTBridge *_bridge; RCTEventDispatcher *_eventDispatcher; + UITextRange *_previousSelectionRange; + NSInteger _nativeEventCount; + NSInteger _mostRecentEventCount; } - (instancetype)initWithBridge:(RCTBridge *)bridge NS_DESIGNATED_INITIALIZER; @@ -36,6 +39,8 @@ @property (nonatomic, copy) RCTDirectEventBlock onContentSizeChange; +@property (nonatomic, assign) NSInteger mostRecentEventCount; + - (void)invalidateContentSize; @end diff --git a/Libraries/Text/RCTTextInput.m b/Libraries/Text/RCTTextInput.m index 86591a149..5884f0d54 100644 --- a/Libraries/Text/RCTTextInput.m +++ b/Libraries/Text/RCTTextInput.m @@ -16,6 +16,8 @@ #import #import +#import "RCTTextSelection.h" + @implementation RCTTextInput { CGSize _previousContentSize; } @@ -60,6 +62,28 @@ RCT_NOT_IMPLEMENTED(- (instancetype)initWithFrame:(CGRect)frame) [self setNeedsLayout]; } +- (void)setSelection:(RCTTextSelection *)selection +{ + if (!selection) { + return; + } + + id backedTextInput = self.backedTextInputView; + + UITextRange *currentSelection = backedTextInput.selectedTextRange; + UITextPosition *start = [backedTextInput positionFromPosition:backedTextInput.beginningOfDocument offset:selection.start]; + UITextPosition *end = [backedTextInput positionFromPosition:backedTextInput.beginningOfDocument offset:selection.end]; + UITextRange *selectedTextRange = [backedTextInput textRangeFromPosition:start toPosition:end]; + + NSInteger eventLag = _nativeEventCount - _mostRecentEventCount; + if (eventLag == 0 && ![currentSelection isEqual:selectedTextRange]) { + _previousSelectionRange = selectedTextRange; + backedTextInput.selectedTextRange = selectedTextRange; + } else if (eventLag > RCTTextUpdateLagWarningThreshold) { + RCTLogWarn(@"Native TextInput(%@) is %zd events ahead of JS - try to make your JS faster.", backedTextInput.text, eventLag); + } +} + #pragma mark - Content Size (in Yoga terms, without any insets) - (CGSize)contentSize diff --git a/Libraries/Text/RCTTextView.h b/Libraries/Text/RCTTextView.h index f3f9a3e12..ecb37f008 100644 --- a/Libraries/Text/RCTTextView.h +++ b/Libraries/Text/RCTTextView.h @@ -28,7 +28,6 @@ @property (nonatomic, strong) UIColor *placeholderTextColor; @property (nonatomic, copy) NSString *placeholder; @property (nonatomic, strong) UIFont *font; -@property (nonatomic, assign) NSInteger mostRecentEventCount; @property (nonatomic, strong) NSNumber *maxLength; @property (nonatomic, copy) RCTDirectEventBlock onChange; diff --git a/Libraries/Text/RCTTextView.m b/Libraries/Text/RCTTextView.m index 93ab58f6b..a34d53efa 100644 --- a/Libraries/Text/RCTTextView.m +++ b/Libraries/Text/RCTTextView.m @@ -30,12 +30,10 @@ RCTText *_richTextView; NSAttributedString *_pendingAttributedText; - UITextRange *_previousSelectionRange; NSString *_predictedText; BOOL _blockTextShouldChange; BOOL _nativeUpdatesInFlight; - NSInteger _nativeEventCount; } - (instancetype)initWithBridge:(RCTBridge *)bridge @@ -208,26 +206,6 @@ static NSAttributedString *removeReactTagFromString(NSAttributedString *string) [self setNeedsLayout]; } -- (void)setSelection:(RCTTextSelection *)selection -{ - if (!selection) { - return; - } - - UITextRange *currentSelection = _backedTextInput.selectedTextRange; - UITextPosition *start = [_backedTextInput positionFromPosition:_backedTextInput.beginningOfDocument offset:selection.start]; - UITextPosition *end = [_backedTextInput positionFromPosition:_backedTextInput.beginningOfDocument offset:selection.end]; - UITextRange *selectedTextRange = [_backedTextInput textRangeFromPosition:start toPosition:end]; - - NSInteger eventLag = _nativeEventCount - _mostRecentEventCount; - if (eventLag == 0 && ![currentSelection isEqual:selectedTextRange]) { - _previousSelectionRange = selectedTextRange; - _backedTextInput.selectedTextRange = selectedTextRange; - } else if (eventLag > RCTTextUpdateLagWarningThreshold) { - RCTLogWarn(@"Native TextInput(%@) is %zd events ahead of JS - try to make your JS faster.", self.text, eventLag); - } -} - - (NSString *)text { return _backedTextInput.text;