From 2ea18a203a102780c8f6500c0544b0c6114be93a Mon Sep 17 00:00:00 2001 From: Eoin Norris Date: Wed, 13 Jun 2018 11:04:31 +0100 Subject: [PATCH 1/3] added a check for the dictation input method in setAttributedText:, if it exists then dont reset the attibutes unless the underlying string is different --- .../Text/TextInput/RCTBaseTextInputView.m | 38 +++++++++++++------ 1 file changed, 26 insertions(+), 12 deletions(-) diff --git a/Libraries/Text/TextInput/RCTBaseTextInputView.m b/Libraries/Text/TextInput/RCTBaseTextInputView.m index cbfa24d1ab4f..11c5c4baedfd 100644 --- a/Libraries/Text/TextInput/RCTBaseTextInputView.m +++ b/Libraries/Text/TextInput/RCTBaseTextInputView.m @@ -98,40 +98,54 @@ - (NSAttributedString *)attributedText return self.backedTextInputView.attributedText; } +- (BOOL)textOf:(NSAttributedString*)newText equals:(NSAttributedString*)oldText{ + UITextInputMode *currentInputMode = self.backedTextInputView.textInputMode; + if ([currentInputMode.primaryLanguage isEqualToString:@"dictation"]) { + // when the dication is running we cant update the attibuted text on the backed up text view + // because setting the attributed string will kill the dictation. This means that we cant impose the + // settings on a dication. + return ([newText.string isEqualToString:oldText.string]); + } else { + return ([newText isEqualToAttributedString:oldText]); + } +} + - (void)setAttributedText:(NSAttributedString *)attributedText { NSInteger eventLag = _nativeEventCount - _mostRecentEventCount; - + BOOL textNeedsUpdate = NO; // Remove tag attribute to ensure correct attributed string comparison. NSMutableAttributedString *const backedTextInputViewTextCopy = [self.backedTextInputView.attributedText mutableCopy]; NSMutableAttributedString *const attributedTextCopy = [attributedText mutableCopy]; - + [backedTextInputViewTextCopy removeAttribute:RCTTextAttributesTagAttributeName range:NSMakeRange(0, backedTextInputViewTextCopy.length)]; - + [attributedTextCopy removeAttribute:RCTTextAttributesTagAttributeName range:NSMakeRange(0, attributedTextCopy.length)]; - - if (eventLag == 0 && ![attributedTextCopy isEqualToAttributedString:backedTextInputViewTextCopy]) { + + textNeedsUpdate = ([self textOf:attributedTextCopy equals:backedTextInputViewTextCopy] == NO); + + if (eventLag == 0 && textNeedsUpdate) { UITextRange *selection = self.backedTextInputView.selectedTextRange; NSInteger oldTextLength = self.backedTextInputView.attributedText.string.length; - + self.backedTextInputView.attributedText = attributedText; - + if (selection.empty) { // Maintaining a cursor position relative to the end of the old text. NSInteger offsetStart = - [self.backedTextInputView offsetFromPosition:self.backedTextInputView.beginningOfDocument - toPosition:selection.start]; + [self.backedTextInputView offsetFromPosition:self.backedTextInputView.beginningOfDocument + toPosition:selection.start]; NSInteger offsetFromEnd = oldTextLength - offsetStart; NSInteger newOffset = attributedText.string.length - offsetFromEnd; UITextPosition *position = - [self.backedTextInputView positionFromPosition:self.backedTextInputView.beginningOfDocument - offset:newOffset]; + [self.backedTextInputView positionFromPosition:self.backedTextInputView.beginningOfDocument + offset:newOffset]; [self.backedTextInputView setSelectedTextRange:[self.backedTextInputView textRangeFromPosition:position toPosition:position] notifyDelegate:YES]; } - + [self updateLocalData]; } else if (eventLag > RCTTextUpdateLagWarningThreshold) { RCTLogWarn(@"Native TextInput(%@) is %lld events ahead of JS - try to make your JS faster.", self.backedTextInputView.attributedText.string, (long long)eventLag); From 93ebe016c7a37408bcaf4b26696d3486228314c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Ramos?= <165856+hramos@users.noreply.github.com> Date: Fri, 15 Jun 2018 09:50:58 -0700 Subject: [PATCH 2/3] Remove whitespace --- Libraries/Text/TextInput/RCTBaseTextInputView.m | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Libraries/Text/TextInput/RCTBaseTextInputView.m b/Libraries/Text/TextInput/RCTBaseTextInputView.m index 11c5c4baedfd..4c1ab250d359 100644 --- a/Libraries/Text/TextInput/RCTBaseTextInputView.m +++ b/Libraries/Text/TextInput/RCTBaseTextInputView.m @@ -117,10 +117,10 @@ - (void)setAttributedText:(NSAttributedString *)attributedText // Remove tag attribute to ensure correct attributed string comparison. NSMutableAttributedString *const backedTextInputViewTextCopy = [self.backedTextInputView.attributedText mutableCopy]; NSMutableAttributedString *const attributedTextCopy = [attributedText mutableCopy]; - + [backedTextInputViewTextCopy removeAttribute:RCTTextAttributesTagAttributeName range:NSMakeRange(0, backedTextInputViewTextCopy.length)]; - + [attributedTextCopy removeAttribute:RCTTextAttributesTagAttributeName range:NSMakeRange(0, attributedTextCopy.length)]; @@ -129,9 +129,9 @@ - (void)setAttributedText:(NSAttributedString *)attributedText if (eventLag == 0 && textNeedsUpdate) { UITextRange *selection = self.backedTextInputView.selectedTextRange; NSInteger oldTextLength = self.backedTextInputView.attributedText.string.length; - + self.backedTextInputView.attributedText = attributedText; - + if (selection.empty) { // Maintaining a cursor position relative to the end of the old text. NSInteger offsetStart = @@ -145,7 +145,7 @@ - (void)setAttributedText:(NSAttributedString *)attributedText [self.backedTextInputView setSelectedTextRange:[self.backedTextInputView textRangeFromPosition:position toPosition:position] notifyDelegate:YES]; } - + [self updateLocalData]; } else if (eventLag > RCTTextUpdateLagWarningThreshold) { RCTLogWarn(@"Native TextInput(%@) is %lld events ahead of JS - try to make your JS faster.", self.backedTextInputView.attributedText.string, (long long)eventLag); From 948248f1db347ee7cd6bcf2fa07c4e9826669a7b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Ramos?= <165856+hramos@users.noreply.github.com> Date: Fri, 15 Jun 2018 09:52:16 -0700 Subject: [PATCH 3/3] typo --- Libraries/Text/TextInput/RCTBaseTextInputView.m | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Libraries/Text/TextInput/RCTBaseTextInputView.m b/Libraries/Text/TextInput/RCTBaseTextInputView.m index 4c1ab250d359..4ab96c49a7da 100644 --- a/Libraries/Text/TextInput/RCTBaseTextInputView.m +++ b/Libraries/Text/TextInput/RCTBaseTextInputView.m @@ -101,9 +101,9 @@ - (NSAttributedString *)attributedText - (BOOL)textOf:(NSAttributedString*)newText equals:(NSAttributedString*)oldText{ UITextInputMode *currentInputMode = self.backedTextInputView.textInputMode; if ([currentInputMode.primaryLanguage isEqualToString:@"dictation"]) { - // when the dication is running we cant update the attibuted text on the backed up text view - // because setting the attributed string will kill the dictation. This means that we cant impose the - // settings on a dication. + // When the dictation is running we can't update the attibuted text on the backed up text view + // because setting the attributed string will kill the dictation. This means that we can't impose + // the settings on a dictation. return ([newText.string isEqualToString:oldText.string]); } else { return ([newText isEqualToAttributedString:oldText]);