From 2c6a7b4cbf28d960471b2d25092003e066a7d53f Mon Sep 17 00:00:00 2001 From: Tomek Zawadzki Date: Thu, 22 Feb 2024 11:03:35 +0100 Subject: [PATCH] Fix `onSelectionChange` event called twice with different payload on iOS for multiline (#192) --- ios/RCTBaseTextInputView+Markdown.m | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/ios/RCTBaseTextInputView+Markdown.m b/ios/RCTBaseTextInputView+Markdown.m index ad254869..ec2ef82c 100644 --- a/ios/RCTBaseTextInputView+Markdown.m +++ b/ios/RCTBaseTextInputView+Markdown.m @@ -27,10 +27,19 @@ - (void)markdown_updateLocalData { RCTMarkdownUtils *markdownUtils = [self getMarkdownUtils]; if (markdownUtils != nil) { - UITextRange *range = self.backedTextInputView.selectedTextRange; - NSAttributedString *attributedText = [markdownUtils parseMarkdown:self.backedTextInputView.attributedText]; - [self.backedTextInputView setAttributedText:attributedText]; - [self.backedTextInputView setSelectedTextRange:range notifyDelegate:YES]; + id backedTextInputView = self.backedTextInputView; + NSAttributedString *oldAttributedText = backedTextInputView.attributedText; + NSAttributedString *newAttributedText = [markdownUtils parseMarkdown:oldAttributedText]; + UITextRange *range = backedTextInputView.selectedTextRange; + + // update attributed text without emitting onSelectionChange event + id delegate = backedTextInputView.textInputDelegate; + backedTextInputView.textInputDelegate = nil; + [backedTextInputView setAttributedText:newAttributedText]; + backedTextInputView.textInputDelegate = delegate; + + // restore original selection and emit onSelectionChange event + [backedTextInputView setSelectedTextRange:range notifyDelegate:YES]; } // Call the original method