mirror of
https://github.com/zhigang1992/react-native.git
synced 2026-04-05 09:29:07 +08:00
Summary: Nothing behavioral changed in this diff; just moving code around. `RCTBackedTextInputDelegate` is the new protocol which supposed to be common determinator among of UITextFieldDelegate and UITextViewDelegate (and bunch of events and notifications around UITextInput and UITextView). We need this reach two goals in the future: * Incapsulate UIKit imperfections related hack in dedicated protocol adapter. So, doing this we can fix more UIKit related bugs without touching real RN text handling logic. (Yes, we still have a bunch of bugs, which we cannot fix because it is undoable with the current architecture. This diff does NOT fix anything though.) * We can unify logic in RCTTextField and RCTTextView (even more!), moving it to a superclass. If we do so, we can fix another bunch of bugs related to RN imperfections. And have singleline/multiline inputs implementations even more consistent. Reviewed By: mmmulani Differential Revision: D5296041 fbshipit-source-id: 318fd850e946a3c34933002a6bde34a0a45a6293
525 lines
18 KiB
Objective-C
525 lines
18 KiB
Objective-C
/**
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
* All rights reserved.
|
|
*
|
|
* This source code is licensed under the BSD-style license found in the
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
|
*/
|
|
|
|
#import "RCTTextView.h"
|
|
|
|
#import <React/RCTConvert.h>
|
|
#import <React/RCTEventDispatcher.h>
|
|
#import <React/RCTUIManager.h>
|
|
#import <React/RCTUtils.h>
|
|
#import <React/UIView+React.h>
|
|
|
|
#import "RCTShadowText.h"
|
|
#import "RCTText.h"
|
|
#import "RCTTextSelection.h"
|
|
#import "RCTUITextView.h"
|
|
|
|
@interface RCTTextView () <RCTBackedTextInputDelegate>
|
|
|
|
@end
|
|
|
|
@implementation RCTTextView
|
|
{
|
|
RCTUITextView *_backedTextInput;
|
|
RCTText *_richTextView;
|
|
NSAttributedString *_pendingAttributedText;
|
|
|
|
UITextRange *_previousSelectionRange;
|
|
NSString *_predictedText;
|
|
|
|
BOOL _blockTextShouldChange;
|
|
BOOL _nativeUpdatesInFlight;
|
|
NSInteger _nativeEventCount;
|
|
}
|
|
|
|
- (instancetype)initWithBridge:(RCTBridge *)bridge
|
|
{
|
|
RCTAssertParam(bridge);
|
|
|
|
if (self = [super initWithBridge:bridge]) {
|
|
_blurOnSubmit = NO;
|
|
|
|
_backedTextInput = [[RCTUITextView alloc] initWithFrame:self.bounds];
|
|
_backedTextInput.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
|
|
_backedTextInput.backgroundColor = [UIColor clearColor];
|
|
_backedTextInput.textColor = [UIColor blackColor];
|
|
// This line actually removes 5pt (default value) left and right padding in UITextView.
|
|
_backedTextInput.textContainer.lineFragmentPadding = 0;
|
|
#if !TARGET_OS_TV
|
|
_backedTextInput.scrollsToTop = NO;
|
|
#endif
|
|
_backedTextInput.scrollEnabled = YES;
|
|
|
|
_backedTextInput.textInputDelegate = self;
|
|
|
|
[self addSubview:_backedTextInput];
|
|
}
|
|
return self;
|
|
}
|
|
|
|
RCT_NOT_IMPLEMENTED(- (instancetype)initWithFrame:(CGRect)frame)
|
|
RCT_NOT_IMPLEMENTED(- (instancetype)initWithCoder:(NSCoder *)aDecoder)
|
|
|
|
- (id<RCTBackedTextInputViewProtocol>)backedTextInputView
|
|
{
|
|
return _backedTextInput;
|
|
}
|
|
|
|
#pragma mark - RCTComponent
|
|
|
|
- (void)insertReactSubview:(UIView *)subview atIndex:(NSInteger)index
|
|
{
|
|
[super insertReactSubview:subview atIndex:index];
|
|
|
|
if ([subview isKindOfClass:[RCTText class]]) {
|
|
if (_richTextView) {
|
|
RCTLogError(@"Tried to insert a second <Text> into <TextInput> - there can only be one.");
|
|
}
|
|
_richTextView = (RCTText *)subview;
|
|
|
|
// If this <TextInput> is in rich text editing mode, and the child <Text> node providing rich text
|
|
// styling has a backgroundColor, then the attributedText produced by the child <Text> node will have an
|
|
// NSBackgroundColor attribute. We need to forward this attribute to the text view manually because the text view
|
|
// always has a clear background color in `initWithBridge:`.
|
|
//
|
|
// TODO: This should be removed when the related hack in -performPendingTextUpdate is removed.
|
|
if (subview.backgroundColor) {
|
|
NSMutableDictionary<NSString *, id> *attrs = [_backedTextInput.typingAttributes mutableCopy];
|
|
attrs[NSBackgroundColorAttributeName] = subview.backgroundColor;
|
|
_backedTextInput.typingAttributes = attrs;
|
|
}
|
|
|
|
[self performTextUpdate];
|
|
}
|
|
}
|
|
|
|
- (void)removeReactSubview:(UIView *)subview
|
|
{
|
|
[super removeReactSubview:subview];
|
|
if (_richTextView == subview) {
|
|
_richTextView = nil;
|
|
[self performTextUpdate];
|
|
}
|
|
}
|
|
|
|
- (void)didUpdateReactSubviews
|
|
{
|
|
// Do nothing, as we don't allow non-text subviews.
|
|
}
|
|
|
|
#pragma mark - Routine
|
|
|
|
- (void)setMostRecentEventCount:(NSInteger)mostRecentEventCount
|
|
{
|
|
_mostRecentEventCount = mostRecentEventCount;
|
|
|
|
// Props are set after uiBlockToAmendWithShadowViewRegistry, which means that
|
|
// at the time performTextUpdate is called, _mostRecentEventCount will be
|
|
// behind _eventCount, with the result that performPendingTextUpdate will do
|
|
// nothing. For that reason we call it again here after mostRecentEventCount
|
|
// has been set.
|
|
[self performPendingTextUpdate];
|
|
}
|
|
|
|
- (void)performTextUpdate
|
|
{
|
|
if (_richTextView) {
|
|
_pendingAttributedText = _richTextView.textStorage;
|
|
[self performPendingTextUpdate];
|
|
} else if (!self.text) {
|
|
_backedTextInput.attributedText = nil;
|
|
}
|
|
}
|
|
|
|
static NSAttributedString *removeReactTagFromString(NSAttributedString *string)
|
|
{
|
|
if (string.length == 0) {
|
|
return string;
|
|
} else {
|
|
NSMutableAttributedString *mutableString = [[NSMutableAttributedString alloc] initWithAttributedString:string];
|
|
[mutableString removeAttribute:RCTReactTagAttributeName range:NSMakeRange(0, mutableString.length)];
|
|
return mutableString;
|
|
}
|
|
}
|
|
|
|
- (void)performPendingTextUpdate
|
|
{
|
|
if (!_pendingAttributedText || _mostRecentEventCount < _nativeEventCount || _nativeUpdatesInFlight) {
|
|
return;
|
|
}
|
|
|
|
// The underlying <Text> node that produces _pendingAttributedText has a react tag attribute on it that causes the
|
|
// -isEqualToAttributedString: comparison below to spuriously fail. We don't want that comparison to fail unless it
|
|
// needs to because when the comparison fails, we end up setting attributedText on the text view, which clears
|
|
// autocomplete state for CKJ text input.
|
|
//
|
|
// TODO: Kill this after we finish passing all style/attribute info into JS.
|
|
_pendingAttributedText = removeReactTagFromString(_pendingAttributedText);
|
|
|
|
if ([_backedTextInput.attributedText isEqualToAttributedString:_pendingAttributedText]) {
|
|
_pendingAttributedText = nil; // Don't try again.
|
|
return;
|
|
}
|
|
|
|
// When we update the attributed text, there might be pending autocorrections
|
|
// that will get accepted by default. In order for this to not garble our text,
|
|
// we temporarily block all textShouldChange events so they are not applied.
|
|
_blockTextShouldChange = YES;
|
|
|
|
UITextRange *selection = _backedTextInput.selectedTextRange;
|
|
NSInteger oldTextLength = _backedTextInput.attributedText.length;
|
|
|
|
_backedTextInput.attributedText = _pendingAttributedText;
|
|
_predictedText = _pendingAttributedText.string;
|
|
_pendingAttributedText = nil;
|
|
|
|
if (selection.empty) {
|
|
// maintain cursor position relative to the end of the old text
|
|
NSInteger start = [_backedTextInput offsetFromPosition:_backedTextInput.beginningOfDocument toPosition:selection.start];
|
|
NSInteger offsetFromEnd = oldTextLength - start;
|
|
NSInteger newOffset = _backedTextInput.attributedText.length - offsetFromEnd;
|
|
UITextPosition *position = [_backedTextInput positionFromPosition:_backedTextInput.beginningOfDocument offset:newOffset];
|
|
_backedTextInput.selectedTextRange = [_backedTextInput textRangeFromPosition:position toPosition:position];
|
|
}
|
|
|
|
[_backedTextInput layoutIfNeeded];
|
|
|
|
[self invalidateContentSize];
|
|
|
|
_blockTextShouldChange = NO;
|
|
}
|
|
|
|
#pragma mark - Properties
|
|
|
|
- (UIFont *)font
|
|
{
|
|
return _backedTextInput.font;
|
|
}
|
|
|
|
- (void)setFont:(UIFont *)font
|
|
{
|
|
_backedTextInput.font = font;
|
|
[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;
|
|
}
|
|
|
|
- (void)setText:(NSString *)text
|
|
{
|
|
NSInteger eventLag = _nativeEventCount - _mostRecentEventCount;
|
|
if (eventLag == 0 && ![text isEqualToString:_backedTextInput.text]) {
|
|
UITextRange *selection = _backedTextInput.selectedTextRange;
|
|
NSInteger oldTextLength = _backedTextInput.text.length;
|
|
|
|
_predictedText = text;
|
|
_backedTextInput.text = text;
|
|
|
|
if (selection.empty) {
|
|
// maintain cursor position relative to the end of the old text
|
|
NSInteger start = [_backedTextInput offsetFromPosition:_backedTextInput.beginningOfDocument toPosition:selection.start];
|
|
NSInteger offsetFromEnd = oldTextLength - start;
|
|
NSInteger newOffset = text.length - offsetFromEnd;
|
|
UITextPosition *position = [_backedTextInput positionFromPosition:_backedTextInput.beginningOfDocument offset:newOffset];
|
|
_backedTextInput.selectedTextRange = [_backedTextInput textRangeFromPosition:position toPosition:position];
|
|
}
|
|
|
|
[self invalidateContentSize];
|
|
} else if (eventLag > RCTTextUpdateLagWarningThreshold) {
|
|
RCTLogWarn(@"Native TextInput(%@) is %zd events ahead of JS - try to make your JS faster.", self.text, eventLag);
|
|
}
|
|
}
|
|
|
|
#pragma mark - RCTBackedTextInputDelegate
|
|
|
|
- (BOOL)textInputShouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
|
|
{
|
|
if (!_backedTextInput.textWasPasted) {
|
|
[_eventDispatcher sendTextEventWithType:RCTTextEventTypeKeyPress
|
|
reactTag:self.reactTag
|
|
text:nil
|
|
key:text
|
|
eventCount:_nativeEventCount];
|
|
|
|
if (_blurOnSubmit && [text isEqualToString:@"\n"]) {
|
|
// TODO: the purpose of blurOnSubmit on RCTextField is to decide if the
|
|
// field should lose focus when return is pressed or not. We're cheating a
|
|
// bit here by using it on RCTextView to decide if return character should
|
|
// submit the form, or be entered into the field.
|
|
//
|
|
// The reason this is cheating is because there's no way to specify that
|
|
// you want the return key to be swallowed *and* have the field retain
|
|
// focus (which was what blurOnSubmit was originally for). For the case
|
|
// where _blurOnSubmit = YES, this is still the correct and expected
|
|
// behavior though, so we'll leave the don't-blur-or-add-newline problem
|
|
// to be solved another day.
|
|
[_eventDispatcher sendTextEventWithType:RCTTextEventTypeSubmit
|
|
reactTag:self.reactTag
|
|
text:self.text
|
|
key:nil
|
|
eventCount:_nativeEventCount];
|
|
[_backedTextInput resignFirstResponder];
|
|
return NO;
|
|
}
|
|
}
|
|
|
|
// So we need to track that there is a native update in flight just in case JS manages to come back around and update
|
|
// things /before/ UITextView can update itself asynchronously. If there is a native update in flight, we defer the
|
|
// JS update when it comes in and apply the deferred update once textViewDidChange fires with the native update applied.
|
|
if (_blockTextShouldChange) {
|
|
return NO;
|
|
}
|
|
|
|
if (_maxLength) {
|
|
NSUInteger allowedLength = _maxLength.integerValue - _backedTextInput.text.length + range.length;
|
|
if (text.length > allowedLength) {
|
|
// If we typed/pasted more than one character, limit the text inputted
|
|
if (text.length > 1) {
|
|
// Truncate the input string so the result is exactly maxLength
|
|
NSString *limitedString = [text substringToIndex:allowedLength];
|
|
NSMutableString *newString = _backedTextInput.text.mutableCopy;
|
|
[newString replaceCharactersInRange:range withString:limitedString];
|
|
_backedTextInput.text = newString;
|
|
_predictedText = newString;
|
|
|
|
// Collapse selection at end of insert to match normal paste behavior
|
|
UITextPosition *insertEnd = [_backedTextInput positionFromPosition:_backedTextInput.beginningOfDocument
|
|
offset:(range.location + allowedLength)];
|
|
_backedTextInput.selectedTextRange = [_backedTextInput textRangeFromPosition:insertEnd toPosition:insertEnd];
|
|
|
|
[self textInputDidChange];
|
|
}
|
|
return NO;
|
|
}
|
|
}
|
|
|
|
_nativeUpdatesInFlight = YES;
|
|
|
|
if (range.location + range.length > _predictedText.length) {
|
|
// _predictedText got out of sync in a bad way, so let's just force sync it. Haven't been able to repro this, but
|
|
// it's causing a real crash here: #6523822
|
|
_predictedText = _backedTextInput.text;
|
|
}
|
|
|
|
NSString *previousText = [_predictedText substringWithRange:range];
|
|
if (_predictedText) {
|
|
_predictedText = [_predictedText stringByReplacingCharactersInRange:range withString:text];
|
|
} else {
|
|
_predictedText = text;
|
|
}
|
|
|
|
if (_onTextInput) {
|
|
_onTextInput(@{
|
|
@"text": text,
|
|
@"previousText": previousText ?: @"",
|
|
@"range": @{
|
|
@"start": @(range.location),
|
|
@"end": @(range.location + range.length)
|
|
},
|
|
@"eventCount": @(_nativeEventCount),
|
|
});
|
|
}
|
|
|
|
return YES;
|
|
}
|
|
|
|
- (void)textInputDidChangeSelection
|
|
{
|
|
if (_onSelectionChange &&
|
|
_backedTextInput.selectedTextRange != _previousSelectionRange &&
|
|
![_backedTextInput.selectedTextRange isEqual:_previousSelectionRange]) {
|
|
|
|
_previousSelectionRange = _backedTextInput.selectedTextRange;
|
|
|
|
UITextRange *selection = _backedTextInput.selectedTextRange;
|
|
NSInteger start = [_backedTextInput offsetFromPosition:_backedTextInput.beginningOfDocument toPosition:selection.start];
|
|
NSInteger end = [_backedTextInput offsetFromPosition:_backedTextInput.beginningOfDocument toPosition:selection.end];
|
|
_onSelectionChange(@{
|
|
@"selection": @{
|
|
@"start": @(start),
|
|
@"end": @(end),
|
|
},
|
|
});
|
|
}
|
|
}
|
|
|
|
- (BOOL)textInputShouldBeginEditing
|
|
{
|
|
if (_selectTextOnFocus) {
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
|
[self->_backedTextInput selectAll:nil];
|
|
});
|
|
}
|
|
return YES;
|
|
}
|
|
|
|
- (void)textInputDidBeginEditing
|
|
{
|
|
if (_clearTextOnFocus) {
|
|
_backedTextInput.text = @"";
|
|
}
|
|
|
|
[_eventDispatcher sendTextEventWithType:RCTTextEventTypeFocus
|
|
reactTag:self.reactTag
|
|
text:nil
|
|
key:nil
|
|
eventCount:_nativeEventCount];
|
|
}
|
|
|
|
static BOOL findMismatch(NSString *first, NSString *second, NSRange *firstRange, NSRange *secondRange)
|
|
{
|
|
NSInteger firstMismatch = -1;
|
|
for (NSUInteger ii = 0; ii < MAX(first.length, second.length); ii++) {
|
|
if (ii >= first.length || ii >= second.length || [first characterAtIndex:ii] != [second characterAtIndex:ii]) {
|
|
firstMismatch = ii;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (firstMismatch == -1) {
|
|
return NO;
|
|
}
|
|
|
|
NSUInteger ii = second.length;
|
|
NSUInteger lastMismatch = first.length;
|
|
while (ii > firstMismatch && lastMismatch > firstMismatch) {
|
|
if ([first characterAtIndex:(lastMismatch - 1)] != [second characterAtIndex:(ii - 1)]) {
|
|
break;
|
|
}
|
|
ii--;
|
|
lastMismatch--;
|
|
}
|
|
|
|
*firstRange = NSMakeRange(firstMismatch, lastMismatch - firstMismatch);
|
|
*secondRange = NSMakeRange(firstMismatch, ii - firstMismatch);
|
|
return YES;
|
|
}
|
|
|
|
- (void)textInputDidChange
|
|
{
|
|
[self invalidateContentSize];
|
|
|
|
// Detect when _backedTextInput updates happend that didn't invoke `shouldChangeTextInRange`
|
|
// (e.g. typing simplified chinese in pinyin will insert and remove spaces without
|
|
// calling shouldChangeTextInRange). This will cause JS to get out of sync so we
|
|
// update the mismatched range.
|
|
NSRange currentRange;
|
|
NSRange predictionRange;
|
|
if (findMismatch(_backedTextInput.text, _predictedText, ¤tRange, &predictionRange)) {
|
|
NSString *replacement = [_backedTextInput.text substringWithRange:currentRange];
|
|
[self textInputShouldChangeTextInRange:predictionRange replacementText:replacement];
|
|
// JS will assume the selection changed based on the location of our shouldChangeTextInRange, so reset it.
|
|
[self textInputDidChangeSelection];
|
|
_predictedText = _backedTextInput.text;
|
|
}
|
|
|
|
_nativeUpdatesInFlight = NO;
|
|
_nativeEventCount++;
|
|
|
|
if (!self.reactTag || !_onChange) {
|
|
return;
|
|
}
|
|
|
|
_onChange(@{
|
|
@"text": self.text,
|
|
@"target": self.reactTag,
|
|
@"eventCount": @(_nativeEventCount),
|
|
});
|
|
}
|
|
|
|
|
|
- (BOOL)textInputShouldEndEditing
|
|
{
|
|
return YES;
|
|
}
|
|
|
|
- (void)textInputDidEndEditing
|
|
{
|
|
if (_nativeUpdatesInFlight) {
|
|
// iOS does't call `textViewDidChange:` delegate method if the change was happened because of autocorrection
|
|
// which was triggered by loosing focus. So, we call it manually.
|
|
[self textInputDidChange];
|
|
}
|
|
|
|
[_eventDispatcher sendTextEventWithType:RCTTextEventTypeEnd
|
|
reactTag:self.reactTag
|
|
text:_backedTextInput.text
|
|
key:nil
|
|
eventCount:_nativeEventCount];
|
|
|
|
[_eventDispatcher sendTextEventWithType:RCTTextEventTypeBlur
|
|
reactTag:self.reactTag
|
|
text:nil
|
|
key:nil
|
|
eventCount:_nativeEventCount];
|
|
}
|
|
|
|
- (void)textInputDidEndEditingOnExit
|
|
{
|
|
// Do nothing.
|
|
}
|
|
|
|
#pragma mark - UIScrollViewDelegate
|
|
|
|
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
|
|
{
|
|
if (_onScroll) {
|
|
CGPoint contentOffset = scrollView.contentOffset;
|
|
CGSize contentSize = scrollView.contentSize;
|
|
CGSize size = scrollView.bounds.size;
|
|
UIEdgeInsets contentInset = scrollView.contentInset;
|
|
|
|
_onScroll(@{
|
|
@"contentOffset": @{
|
|
@"x": @(contentOffset.x),
|
|
@"y": @(contentOffset.y)
|
|
},
|
|
@"contentInset": @{
|
|
@"top": @(contentInset.top),
|
|
@"left": @(contentInset.left),
|
|
@"bottom": @(contentInset.bottom),
|
|
@"right": @(contentInset.right)
|
|
},
|
|
@"contentSize": @{
|
|
@"width": @(contentSize.width),
|
|
@"height": @(contentSize.height)
|
|
},
|
|
@"layoutMeasurement": @{
|
|
@"width": @(size.width),
|
|
@"height": @(size.height)
|
|
},
|
|
@"zoomScale": @(scrollView.zoomScale ?: 1),
|
|
});
|
|
}
|
|
}
|
|
|
|
@end
|