2015-04-29 08:29:00 +00:00
|
|
|
/**
|
|
|
|
* 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"
|
|
|
|
|
2016-11-23 15:47:52 +00:00
|
|
|
#import <React/RCTConvert.h>
|
|
|
|
#import <React/RCTEventDispatcher.h>
|
2017-03-20 07:00:18 +00:00
|
|
|
#import <React/RCTUIManager.h>
|
2016-11-23 15:47:52 +00:00
|
|
|
#import <React/RCTUtils.h>
|
|
|
|
#import <React/UIView+React.h>
|
|
|
|
|
2016-04-21 19:09:16 +00:00
|
|
|
#import "RCTShadowText.h"
|
2015-11-06 15:25:19 +00:00
|
|
|
#import "RCTText.h"
|
2016-08-26 00:18:05 +00:00
|
|
|
#import "RCTTextSelection.h"
|
2017-03-20 07:00:23 +00:00
|
|
|
#import "RCTUITextView.h"
|
2015-11-02 17:13:41 +00:00
|
|
|
|
2015-04-29 08:29:00 +00:00
|
|
|
@implementation RCTTextView
|
|
|
|
{
|
2017-03-20 07:00:18 +00:00
|
|
|
RCTBridge *_bridge;
|
2015-04-29 08:29:00 +00:00
|
|
|
RCTEventDispatcher *_eventDispatcher;
|
2016-06-24 13:28:38 +00:00
|
|
|
|
2017-03-20 07:00:23 +00:00
|
|
|
RCTUITextView *_textView;
|
2015-11-06 15:25:19 +00:00
|
|
|
RCTText *_richTextView;
|
|
|
|
NSAttributedString *_pendingAttributedText;
|
2016-06-24 13:28:38 +00:00
|
|
|
|
2015-11-14 17:41:59 +00:00
|
|
|
UITextRange *_previousSelectionRange;
|
2016-06-24 13:28:38 +00:00
|
|
|
NSString *_predictedText;
|
|
|
|
|
|
|
|
BOOL _blockTextShouldChange;
|
|
|
|
BOOL _nativeUpdatesInFlight;
|
|
|
|
NSInteger _nativeEventCount;
|
2016-07-07 15:44:59 +00:00
|
|
|
|
|
|
|
CGSize _previousContentSize;
|
2015-04-29 08:29:00 +00:00
|
|
|
}
|
|
|
|
|
2017-03-20 07:00:18 +00:00
|
|
|
- (instancetype)initWithBridge:(RCTBridge *)bridge
|
2015-04-29 08:29:00 +00:00
|
|
|
{
|
2017-03-20 07:00:18 +00:00
|
|
|
RCTAssertParam(bridge);
|
2015-06-15 14:53:45 +00:00
|
|
|
|
2017-03-20 07:00:18 +00:00
|
|
|
if (self = [super initWithFrame:CGRectZero]) {
|
|
|
|
_bridge = bridge;
|
|
|
|
_eventDispatcher = bridge.eventDispatcher;
|
2015-12-02 15:11:20 +00:00
|
|
|
_blurOnSubmit = NO;
|
2015-04-29 08:29:00 +00:00
|
|
|
|
2017-03-20 07:00:18 +00:00
|
|
|
_textView = [[RCTUITextView alloc] initWithFrame:self.bounds];
|
|
|
|
_textView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
|
2015-04-29 08:29:00 +00:00
|
|
|
_textView.backgroundColor = [UIColor clearColor];
|
2016-06-06 19:54:37 +00:00
|
|
|
_textView.textColor = [UIColor blackColor];
|
2017-03-20 07:00:18 +00:00
|
|
|
// This line actually removes 5pt (default value) left and right padding in UITextView.
|
|
|
|
_textView.textContainer.lineFragmentPadding = 0;
|
2016-09-27 13:19:45 +00:00
|
|
|
#if !TARGET_OS_TV
|
2015-09-01 10:23:29 +00:00
|
|
|
_textView.scrollsToTop = NO;
|
2016-09-27 13:19:45 +00:00
|
|
|
#endif
|
2017-03-20 07:00:17 +00:00
|
|
|
_textView.scrollEnabled = YES;
|
2015-04-29 08:29:00 +00:00
|
|
|
_textView.delegate = self;
|
2015-11-06 15:25:19 +00:00
|
|
|
|
2017-03-20 07:00:17 +00:00
|
|
|
[self addSubview:_textView];
|
2015-04-29 08:29:00 +00:00
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2015-08-24 10:14:33 +00:00
|
|
|
RCT_NOT_IMPLEMENTED(- (instancetype)initWithFrame:(CGRect)frame)
|
|
|
|
RCT_NOT_IMPLEMENTED(- (instancetype)initWithCoder:(NSCoder *)aDecoder)
|
2015-06-15 14:53:45 +00:00
|
|
|
|
2017-03-20 07:00:17 +00:00
|
|
|
#pragma mark - RCTComponent
|
|
|
|
|
2015-11-14 18:25:00 +00:00
|
|
|
- (void)insertReactSubview:(UIView *)subview atIndex:(NSInteger)index
|
2015-11-06 15:25:19 +00:00
|
|
|
{
|
2016-06-07 07:08:16 +00:00
|
|
|
[super insertReactSubview:subview atIndex:index];
|
2017-03-20 07:00:17 +00:00
|
|
|
|
2015-11-06 15:25:19 +00:00
|
|
|
if ([subview isKindOfClass:[RCTText class]]) {
|
|
|
|
if (_richTextView) {
|
|
|
|
RCTLogError(@"Tried to insert a second <Text> into <TextInput> - there can only be one.");
|
|
|
|
}
|
|
|
|
_richTextView = (RCTText *)subview;
|
2016-04-21 19:09:16 +00:00
|
|
|
|
|
|
|
// 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
|
2017-03-20 07:00:18 +00:00
|
|
|
// always has a clear background color in `initWithBridge:`.
|
2016-04-21 19:09:16 +00:00
|
|
|
//
|
|
|
|
// TODO: This should be removed when the related hack in -performPendingTextUpdate is removed.
|
|
|
|
if (subview.backgroundColor) {
|
|
|
|
NSMutableDictionary<NSString *, id> *attrs = [_textView.typingAttributes mutableCopy];
|
|
|
|
attrs[NSBackgroundColorAttributeName] = subview.backgroundColor;
|
|
|
|
_textView.typingAttributes = attrs;
|
|
|
|
}
|
2016-06-23 10:53:50 +00:00
|
|
|
|
|
|
|
[self performTextUpdate];
|
2015-11-06 15:25:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-14 18:25:00 +00:00
|
|
|
- (void)removeReactSubview:(UIView *)subview
|
2015-11-06 15:25:19 +00:00
|
|
|
{
|
2016-06-07 07:08:16 +00:00
|
|
|
[super removeReactSubview:subview];
|
2015-11-06 15:25:19 +00:00
|
|
|
if (_richTextView == subview) {
|
|
|
|
_richTextView = nil;
|
2016-06-23 10:53:50 +00:00
|
|
|
[self performTextUpdate];
|
2015-11-06 15:25:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-07 15:36:07 +00:00
|
|
|
- (void)didUpdateReactSubviews
|
2016-06-07 07:08:16 +00:00
|
|
|
{
|
2017-03-20 07:00:17 +00:00
|
|
|
// Do nothing, as we don't allow non-text subviews.
|
2016-06-07 07:08:16 +00:00
|
|
|
}
|
|
|
|
|
2017-03-20 07:00:17 +00:00
|
|
|
#pragma mark - Routine
|
|
|
|
|
2015-11-06 15:25:19 +00:00
|
|
|
- (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) {
|
|
|
|
_textView.attributedText = nil;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-21 19:09:16 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-06 15:25:19 +00:00
|
|
|
- (void)performPendingTextUpdate
|
|
|
|
{
|
2016-06-24 13:28:38 +00:00
|
|
|
if (!_pendingAttributedText || _mostRecentEventCount < _nativeEventCount || _nativeUpdatesInFlight) {
|
2015-11-06 15:25:19 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-04-21 19:09:16 +00:00
|
|
|
// 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);
|
|
|
|
|
2015-11-06 15:25:19 +00:00
|
|
|
if ([_textView.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;
|
|
|
|
|
2015-12-17 18:22:56 +00:00
|
|
|
UITextRange *selection = _textView.selectedTextRange;
|
|
|
|
NSInteger oldTextLength = _textView.attributedText.length;
|
|
|
|
|
2015-11-06 15:25:19 +00:00
|
|
|
_textView.attributedText = _pendingAttributedText;
|
2016-06-24 13:28:38 +00:00
|
|
|
_predictedText = _pendingAttributedText.string;
|
2015-11-06 15:25:19 +00:00
|
|
|
_pendingAttributedText = nil;
|
2015-12-17 18:22:56 +00:00
|
|
|
|
|
|
|
if (selection.empty) {
|
|
|
|
// maintain cursor position relative to the end of the old text
|
|
|
|
NSInteger start = [_textView offsetFromPosition:_textView.beginningOfDocument toPosition:selection.start];
|
|
|
|
NSInteger offsetFromEnd = oldTextLength - start;
|
|
|
|
NSInteger newOffset = _textView.attributedText.length - offsetFromEnd;
|
|
|
|
UITextPosition *position = [_textView positionFromPosition:_textView.beginningOfDocument offset:newOffset];
|
|
|
|
_textView.selectedTextRange = [_textView textRangeFromPosition:position toPosition:position];
|
|
|
|
}
|
|
|
|
|
2015-11-06 15:25:19 +00:00
|
|
|
[_textView layoutIfNeeded];
|
|
|
|
|
2017-03-20 07:00:18 +00:00
|
|
|
[self invalidateContentSize];
|
2015-11-06 15:25:19 +00:00
|
|
|
|
|
|
|
_blockTextShouldChange = NO;
|
|
|
|
}
|
|
|
|
|
2017-03-20 07:00:23 +00:00
|
|
|
#pragma mark - Properties
|
2015-04-29 08:29:00 +00:00
|
|
|
|
2015-05-14 16:37:39 +00:00
|
|
|
- (UIFont *)font
|
|
|
|
{
|
|
|
|
return _textView.font;
|
|
|
|
}
|
|
|
|
|
2015-04-29 08:29:00 +00:00
|
|
|
- (void)setFont:(UIFont *)font
|
|
|
|
{
|
2015-05-14 16:37:39 +00:00
|
|
|
_textView.font = font;
|
2017-05-29 22:56:46 +00:00
|
|
|
[self setNeedsLayout];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setReactPaddingInsets:(UIEdgeInsets)reactPaddingInsets
|
|
|
|
{
|
|
|
|
_reactPaddingInsets = reactPaddingInsets;
|
|
|
|
// We apply `paddingInsets` as `_textView`'s `textContainerInset`.
|
|
|
|
_textView.textContainerInset = reactPaddingInsets;
|
|
|
|
[self setNeedsLayout];
|
2017-03-20 07:00:23 +00:00
|
|
|
}
|
|
|
|
|
2017-05-29 22:56:46 +00:00
|
|
|
- (void)setReactBorderInsets:(UIEdgeInsets)reactBorderInsets
|
2017-03-20 07:00:23 +00:00
|
|
|
{
|
2017-05-29 22:56:46 +00:00
|
|
|
_reactBorderInsets = reactBorderInsets;
|
|
|
|
// We apply `borderInsets` as `_textView` layout offset.
|
|
|
|
_textView.frame = UIEdgeInsetsInsetRect(self.bounds, reactBorderInsets);
|
2017-03-20 07:00:23 +00:00
|
|
|
[self setNeedsLayout];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setSelection:(RCTTextSelection *)selection
|
|
|
|
{
|
|
|
|
if (!selection) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
UITextRange *currentSelection = _textView.selectedTextRange;
|
|
|
|
UITextPosition *start = [_textView positionFromPosition:_textView.beginningOfDocument offset:selection.start];
|
|
|
|
UITextPosition *end = [_textView positionFromPosition:_textView.beginningOfDocument offset:selection.end];
|
|
|
|
UITextRange *selectedTextRange = [_textView textRangeFromPosition:start toPosition:end];
|
|
|
|
|
|
|
|
NSInteger eventLag = _nativeEventCount - _mostRecentEventCount;
|
|
|
|
if (eventLag == 0 && ![currentSelection isEqual:selectedTextRange]) {
|
|
|
|
_previousSelectionRange = selectedTextRange;
|
|
|
|
_textView.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 _textView.text;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setText:(NSString *)text
|
|
|
|
{
|
|
|
|
NSInteger eventLag = _nativeEventCount - _mostRecentEventCount;
|
|
|
|
if (eventLag == 0 && ![text isEqualToString:_textView.text]) {
|
|
|
|
UITextRange *selection = _textView.selectedTextRange;
|
|
|
|
NSInteger oldTextLength = _textView.text.length;
|
|
|
|
|
|
|
|
_predictedText = text;
|
|
|
|
_textView.text = text;
|
|
|
|
|
|
|
|
if (selection.empty) {
|
|
|
|
// maintain cursor position relative to the end of the old text
|
|
|
|
NSInteger start = [_textView offsetFromPosition:_textView.beginningOfDocument toPosition:selection.start];
|
|
|
|
NSInteger offsetFromEnd = oldTextLength - start;
|
|
|
|
NSInteger newOffset = text.length - offsetFromEnd;
|
|
|
|
UITextPosition *position = [_textView positionFromPosition:_textView.beginningOfDocument offset:newOffset];
|
|
|
|
_textView.selectedTextRange = [_textView 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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString *)placeholder
|
|
|
|
{
|
|
|
|
return _textView.placeholderText;
|
2015-04-29 08:29:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setPlaceholder:(NSString *)placeholder
|
|
|
|
{
|
2017-03-20 07:00:23 +00:00
|
|
|
_textView.placeholderText = placeholder;
|
2017-05-29 22:56:46 +00:00
|
|
|
[self setNeedsLayout];
|
2017-03-20 07:00:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (UIColor *)placeholderTextColor
|
|
|
|
{
|
|
|
|
return _textView.placeholderTextColor;
|
2015-04-29 08:29:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setPlaceholderTextColor:(UIColor *)placeholderTextColor
|
|
|
|
{
|
2017-03-20 07:00:23 +00:00
|
|
|
_textView.placeholderTextColor = placeholderTextColor;
|
2015-04-29 08:29:00 +00:00
|
|
|
}
|
|
|
|
|
2017-03-20 07:00:23 +00:00
|
|
|
- (void)setAutocorrectionType:(UITextAutocorrectionType)autocorrectionType
|
2015-04-29 08:29:00 +00:00
|
|
|
{
|
2017-03-20 07:00:23 +00:00
|
|
|
_textView.autocorrectionType = autocorrectionType;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (UITextAutocorrectionType)autocorrectionType
|
|
|
|
{
|
|
|
|
return _textView.autocorrectionType;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setSpellCheckingType:(UITextSpellCheckingType)spellCheckingType
|
|
|
|
{
|
|
|
|
_textView.spellCheckingType = spellCheckingType;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (UITextSpellCheckingType)spellCheckingType
|
|
|
|
{
|
|
|
|
return _textView.spellCheckingType;
|
2015-04-29 08:29:00 +00:00
|
|
|
}
|
|
|
|
|
2017-01-25 00:43:24 +00:00
|
|
|
#pragma mark - UITextViewDelegate
|
|
|
|
|
2015-11-02 17:13:41 +00:00
|
|
|
- (BOOL)textView:(RCTUITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
|
2015-07-21 19:37:24 +00:00
|
|
|
{
|
2017-03-20 07:00:23 +00:00
|
|
|
if (!textView.textWasPasted) {
|
2015-11-02 17:13:41 +00:00
|
|
|
[_eventDispatcher sendTextEventWithType:RCTTextEventTypeKeyPress
|
|
|
|
reactTag:self.reactTag
|
|
|
|
text:nil
|
|
|
|
key:text
|
|
|
|
eventCount:_nativeEventCount];
|
2015-12-02 15:11:20 +00:00
|
|
|
|
|
|
|
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];
|
2017-04-03 22:11:02 +00:00
|
|
|
[_textView resignFirstResponder];
|
2015-12-02 15:11:20 +00:00
|
|
|
return NO;
|
|
|
|
}
|
2015-11-02 17:13:41 +00:00
|
|
|
}
|
|
|
|
|
2016-06-24 13:28:38 +00:00
|
|
|
// 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;
|
2015-07-21 19:37:24 +00:00
|
|
|
}
|
2016-06-24 13:28:38 +00:00
|
|
|
|
|
|
|
if (_maxLength) {
|
|
|
|
NSUInteger allowedLength = _maxLength.integerValue - textView.text.length + range.length;
|
|
|
|
if (text.length > allowedLength) {
|
2016-07-14 14:39:18 +00:00
|
|
|
// If we typed/pasted more than one character, limit the text inputted
|
2016-06-24 13:28:38 +00:00
|
|
|
if (text.length > 1) {
|
|
|
|
// Truncate the input string so the result is exactly maxLength
|
|
|
|
NSString *limitedString = [text substringToIndex:allowedLength];
|
|
|
|
NSMutableString *newString = textView.text.mutableCopy;
|
|
|
|
[newString replaceCharactersInRange:range withString:limitedString];
|
|
|
|
textView.text = newString;
|
2016-07-14 14:39:18 +00:00
|
|
|
_predictedText = newString;
|
|
|
|
|
2016-06-24 13:28:38 +00:00
|
|
|
// Collapse selection at end of insert to match normal paste behavior
|
|
|
|
UITextPosition *insertEnd = [textView positionFromPosition:textView.beginningOfDocument
|
|
|
|
offset:(range.location + allowedLength)];
|
|
|
|
textView.selectedTextRange = [textView textRangeFromPosition:insertEnd toPosition:insertEnd];
|
2016-07-14 14:39:18 +00:00
|
|
|
|
2016-06-24 13:28:38 +00:00
|
|
|
[self textViewDidChange:textView];
|
|
|
|
}
|
|
|
|
return NO;
|
2015-07-21 19:37:24 +00:00
|
|
|
}
|
2016-06-24 13:28:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_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 = textView.text;
|
|
|
|
}
|
|
|
|
|
|
|
|
NSString *previousText = [_predictedText substringWithRange:range];
|
|
|
|
if (_predictedText) {
|
|
|
|
_predictedText = [_predictedText stringByReplacingCharactersInRange:range withString:text];
|
2015-07-21 19:37:24 +00:00
|
|
|
} else {
|
2016-06-24 13:28:38 +00:00
|
|
|
_predictedText = text;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_onTextInput) {
|
|
|
|
_onTextInput(@{
|
|
|
|
@"text": text,
|
|
|
|
@"previousText": previousText ?: @"",
|
|
|
|
@"range": @{
|
|
|
|
@"start": @(range.location),
|
|
|
|
@"end": @(range.location + range.length)
|
|
|
|
},
|
|
|
|
@"eventCount": @(_nativeEventCount),
|
|
|
|
});
|
2015-07-21 19:37:24 +00:00
|
|
|
}
|
2016-06-24 13:28:38 +00:00
|
|
|
|
|
|
|
return YES;
|
2015-07-21 19:37:24 +00:00
|
|
|
}
|
|
|
|
|
2015-11-14 17:41:59 +00:00
|
|
|
- (void)textViewDidChangeSelection:(RCTUITextView *)textView
|
|
|
|
{
|
|
|
|
if (_onSelectionChange &&
|
|
|
|
textView.selectedTextRange != _previousSelectionRange &&
|
|
|
|
![textView.selectedTextRange isEqual:_previousSelectionRange]) {
|
|
|
|
|
|
|
|
_previousSelectionRange = textView.selectedTextRange;
|
|
|
|
|
|
|
|
UITextRange *selection = textView.selectedTextRange;
|
2015-12-17 18:22:56 +00:00
|
|
|
NSInteger start = [textView offsetFromPosition:textView.beginningOfDocument toPosition:selection.start];
|
|
|
|
NSInteger end = [textView offsetFromPosition:textView.beginningOfDocument toPosition:selection.end];
|
2015-11-14 17:41:59 +00:00
|
|
|
_onSelectionChange(@{
|
|
|
|
@"selection": @{
|
|
|
|
@"start": @(start),
|
|
|
|
@"end": @(end),
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-29 22:55:36 +00:00
|
|
|
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
|
|
|
|
{
|
|
|
|
if (_selectTextOnFocus) {
|
|
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
|
|
|
[textView selectAll:nil];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
2015-04-29 08:29:00 +00:00
|
|
|
- (void)textViewDidBeginEditing:(UITextView *)textView
|
|
|
|
{
|
2015-04-29 22:55:36 +00:00
|
|
|
if (_clearTextOnFocus) {
|
|
|
|
_textView.text = @"";
|
|
|
|
}
|
|
|
|
|
2015-04-29 08:29:00 +00:00
|
|
|
[_eventDispatcher sendTextEventWithType:RCTTextEventTypeFocus
|
|
|
|
reactTag:self.reactTag
|
2015-11-27 14:52:29 +00:00
|
|
|
text:nil
|
2015-11-02 17:13:41 +00:00
|
|
|
key:nil
|
2015-07-21 19:37:24 +00:00
|
|
|
eventCount:_nativeEventCount];
|
2015-04-29 08:29:00 +00:00
|
|
|
}
|
|
|
|
|
2016-06-24 13:28:38 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-04-29 08:29:00 +00:00
|
|
|
- (void)textViewDidChange:(UITextView *)textView
|
|
|
|
{
|
2017-03-20 07:00:18 +00:00
|
|
|
[self invalidateContentSize];
|
2016-06-24 13:28:38 +00:00
|
|
|
|
|
|
|
// Detect when textView 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(textView.text, _predictedText, ¤tRange, &predictionRange)) {
|
|
|
|
NSString *replacement = [textView.text substringWithRange:currentRange];
|
|
|
|
[self textView:textView shouldChangeTextInRange:predictionRange replacementText:replacement];
|
|
|
|
// JS will assume the selection changed based on the location of our shouldChangeTextInRange, so reset it.
|
|
|
|
[self textViewDidChangeSelection:textView];
|
|
|
|
_predictedText = textView.text;
|
|
|
|
}
|
|
|
|
|
|
|
|
_nativeUpdatesInFlight = NO;
|
2015-07-21 19:37:24 +00:00
|
|
|
_nativeEventCount++;
|
2017-05-02 04:05:38 +00:00
|
|
|
|
|
|
|
if (!self.reactTag || !_onChange) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
_onChange(@{
|
|
|
|
@"text": self.text,
|
|
|
|
@"target": self.reactTag,
|
|
|
|
@"eventCount": @(_nativeEventCount),
|
|
|
|
});
|
2015-04-29 08:29:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)textViewDidEndEditing:(UITextView *)textView
|
|
|
|
{
|
2017-01-25 00:43:24 +00:00
|
|
|
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 textViewDidChange:textView];
|
|
|
|
}
|
|
|
|
|
2015-04-29 08:29:00 +00:00
|
|
|
[_eventDispatcher sendTextEventWithType:RCTTextEventTypeEnd
|
|
|
|
reactTag:self.reactTag
|
2015-07-21 19:37:24 +00:00
|
|
|
text:textView.text
|
2015-11-02 17:13:41 +00:00
|
|
|
key:nil
|
2015-07-21 19:37:24 +00:00
|
|
|
eventCount:_nativeEventCount];
|
2015-11-27 14:52:29 +00:00
|
|
|
|
|
|
|
[_eventDispatcher sendTextEventWithType:RCTTextEventTypeBlur
|
|
|
|
reactTag:self.reactTag
|
|
|
|
text:nil
|
|
|
|
key:nil
|
|
|
|
eventCount:_nativeEventCount];
|
|
|
|
}
|
|
|
|
|
2017-04-03 22:11:02 +00:00
|
|
|
#pragma mark - Focus control deledation
|
2017-03-20 07:00:18 +00:00
|
|
|
|
2017-04-03 22:11:02 +00:00
|
|
|
- (void)reactFocus
|
2015-12-02 15:08:29 +00:00
|
|
|
{
|
2017-04-03 22:11:02 +00:00
|
|
|
[_textView reactFocus];
|
2015-12-02 15:08:29 +00:00
|
|
|
}
|
|
|
|
|
2017-04-03 22:11:02 +00:00
|
|
|
- (void)reactBlur
|
2015-12-02 15:08:29 +00:00
|
|
|
{
|
2017-04-03 22:11:02 +00:00
|
|
|
[_textView reactBlur];
|
2015-12-02 15:08:29 +00:00
|
|
|
}
|
|
|
|
|
2017-04-03 22:11:02 +00:00
|
|
|
- (void)didMoveToWindow
|
2015-11-27 14:52:29 +00:00
|
|
|
{
|
2017-04-03 22:11:02 +00:00
|
|
|
[_textView reactFocusIfNeeded];
|
2015-04-29 08:29:00 +00:00
|
|
|
}
|
|
|
|
|
2017-04-03 22:11:02 +00:00
|
|
|
#pragma mark - Content size
|
2017-03-20 07:00:18 +00:00
|
|
|
|
|
|
|
- (CGSize)contentSize
|
2015-04-29 08:29:00 +00:00
|
|
|
{
|
2017-05-29 22:56:46 +00:00
|
|
|
// Returning value does NOT include border and padding insets.
|
2017-03-20 07:00:18 +00:00
|
|
|
CGSize contentSize = self.intrinsicContentSize;
|
2017-05-29 22:56:46 +00:00
|
|
|
UIEdgeInsets compoundInsets = self.reactCompoundInsets;
|
|
|
|
contentSize.width -= compoundInsets.left + compoundInsets.right;
|
|
|
|
contentSize.height -= compoundInsets.top + compoundInsets.bottom;
|
2017-03-20 07:00:18 +00:00
|
|
|
return contentSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)invalidateContentSize
|
|
|
|
{
|
|
|
|
CGSize contentSize = self.contentSize;
|
|
|
|
|
|
|
|
if (CGSizeEqualToSize(_previousContentSize, contentSize)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_previousContentSize = contentSize;
|
2016-07-07 15:44:59 +00:00
|
|
|
|
2017-03-20 07:00:18 +00:00
|
|
|
[_bridge.uiManager setIntrinsicContentSize:contentSize forView:self];
|
2016-07-07 15:44:59 +00:00
|
|
|
|
2017-03-20 07:00:18 +00:00
|
|
|
if (_onContentSizeChange) {
|
|
|
|
_onContentSizeChange(@{
|
|
|
|
@"contentSize": @{
|
|
|
|
@"height": @(contentSize.height),
|
|
|
|
@"width": @(contentSize.width),
|
|
|
|
},
|
|
|
|
@"target": self.reactTag,
|
|
|
|
});
|
|
|
|
}
|
2015-04-29 08:29:00 +00:00
|
|
|
}
|
|
|
|
|
2017-03-20 07:00:18 +00:00
|
|
|
#pragma mark - Layout
|
|
|
|
|
|
|
|
- (CGSize)intrinsicContentSize
|
|
|
|
{
|
|
|
|
// Calling `sizeThatFits:` is probably more expensive method to compute
|
|
|
|
// content size compare to direct access `_textView.contentSize` property,
|
|
|
|
// but seems `sizeThatFits:` returns more reliable and consistent result.
|
2017-05-29 22:56:46 +00:00
|
|
|
// Returning value DOES include border and padding insets.
|
2017-03-20 07:00:18 +00:00
|
|
|
return [self sizeThatFits:CGSizeMake(self.bounds.size.width, INFINITY)];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (CGSize)sizeThatFits:(CGSize)size
|
|
|
|
{
|
2017-05-29 22:56:46 +00:00
|
|
|
CGFloat compoundHorizontalBorderInset = _reactBorderInsets.left + _reactBorderInsets.right;
|
|
|
|
CGFloat compoundVerticalBorderInset = _reactBorderInsets.top + _reactBorderInsets.bottom;
|
|
|
|
|
|
|
|
size.width -= compoundHorizontalBorderInset;
|
|
|
|
size.height -= compoundVerticalBorderInset;
|
|
|
|
|
|
|
|
// Note: `paddingInsets` already included in `_textView` size
|
|
|
|
// because it was applied as `textContainerInset`.
|
|
|
|
CGSize fittingSize = [_textView sizeThatFits:size];
|
|
|
|
|
|
|
|
fittingSize.width += compoundHorizontalBorderInset;
|
|
|
|
fittingSize.height += compoundVerticalBorderInset;
|
|
|
|
|
|
|
|
return fittingSize;
|
2017-03-20 07:00:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)layoutSubviews
|
|
|
|
{
|
|
|
|
[super layoutSubviews];
|
|
|
|
[self invalidateContentSize];
|
|
|
|
}
|
|
|
|
|
2017-01-25 00:43:24 +00:00
|
|
|
#pragma mark - UIScrollViewDelegate
|
|
|
|
|
2016-11-22 19:52:18 +00:00
|
|
|
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
|
|
|
|
{
|
|
|
|
if (_onScroll) {
|
2017-03-20 07:00:17 +00:00
|
|
|
CGPoint contentOffset = scrollView.contentOffset;
|
|
|
|
CGSize contentSize = scrollView.contentSize;
|
|
|
|
CGSize size = scrollView.bounds.size;
|
|
|
|
UIEdgeInsets contentInset = scrollView.contentInset;
|
|
|
|
|
2016-11-22 19:52:18 +00:00
|
|
|
_onScroll(@{
|
|
|
|
@"contentOffset": @{
|
2017-03-20 07:00:17 +00:00
|
|
|
@"x": @(contentOffset.x),
|
|
|
|
@"y": @(contentOffset.y)
|
2016-11-22 19:52:18 +00:00
|
|
|
},
|
|
|
|
@"contentInset": @{
|
2017-03-20 07:00:17 +00:00
|
|
|
@"top": @(contentInset.top),
|
|
|
|
@"left": @(contentInset.left),
|
|
|
|
@"bottom": @(contentInset.bottom),
|
|
|
|
@"right": @(contentInset.right)
|
2016-11-22 19:52:18 +00:00
|
|
|
},
|
|
|
|
@"contentSize": @{
|
2017-03-20 07:00:17 +00:00
|
|
|
@"width": @(contentSize.width),
|
|
|
|
@"height": @(contentSize.height)
|
2016-11-22 19:52:18 +00:00
|
|
|
},
|
|
|
|
@"layoutMeasurement": @{
|
2017-03-20 07:00:17 +00:00
|
|
|
@"width": @(size.width),
|
|
|
|
@"height": @(size.height)
|
2016-11-22 19:52:18 +00:00
|
|
|
},
|
2017-03-20 07:00:17 +00:00
|
|
|
@"zoomScale": @(scrollView.zoomScale ?: 1),
|
2016-11-22 19:52:18 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-29 08:29:00 +00:00
|
|
|
@end
|