From 5382e2960fefd60e5679b974438f07fb27a5837b Mon Sep 17 00:00:00 2001 From: Tomek Zawadzki Date: Mon, 12 Feb 2024 17:01:52 +0100 Subject: [PATCH 1/5] Make it work --- ios/RCTBaseTextInputView+Markdown.m | 2 +- ios/RCTUITextView+Markdown.mm | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/ios/RCTBaseTextInputView+Markdown.m b/ios/RCTBaseTextInputView+Markdown.m index ad254869..53521cf3 100644 --- a/ios/RCTBaseTextInputView+Markdown.m +++ b/ios/RCTBaseTextInputView+Markdown.m @@ -29,7 +29,7 @@ - (void)markdown_updateLocalData if (markdownUtils != nil) { UITextRange *range = self.backedTextInputView.selectedTextRange; NSAttributedString *attributedText = [markdownUtils parseMarkdown:self.backedTextInputView.attributedText]; - [self.backedTextInputView setAttributedText:attributedText]; + // [self.backedTextInputView setAttributedText:attributedText]; [self.backedTextInputView setSelectedTextRange:range notifyDelegate:YES]; } diff --git a/ios/RCTUITextView+Markdown.mm b/ios/RCTUITextView+Markdown.mm index dd08b87b..a6c0616f 100644 --- a/ios/RCTUITextView+Markdown.mm +++ b/ios/RCTUITextView+Markdown.mm @@ -17,7 +17,7 @@ - (void)markdown_textDidChange RCTMarkdownUtils *markdownUtils = [self getMarkdownUtils]; if (markdownUtils != nil) { UITextRange *range = self.selectedTextRange; - super.attributedText = [markdownUtils parseMarkdown:self.attributedText]; + // super.attributedText = [markdownUtils parseMarkdown:self.attributedText]; [super setSelectedTextRange:range]; // prevents cursor from jumping at the end when typing in the middle of the text self.typingAttributes = self.defaultTextAttributes; // removes indent in new line when typing after blockquote } From 105b60782b3d9458a6d30bbbc8551b97f2f830b6 Mon Sep 17 00:00:00 2001 From: Tomek Zawadzki Date: Mon, 12 Feb 2024 17:14:57 +0100 Subject: [PATCH 2/5] Restore underline --- ios/RCTUITextView+Markdown.mm | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/ios/RCTUITextView+Markdown.mm b/ios/RCTUITextView+Markdown.mm index a6c0616f..7ff8057a 100644 --- a/ios/RCTUITextView+Markdown.mm +++ b/ios/RCTUITextView+Markdown.mm @@ -19,7 +19,8 @@ - (void)markdown_textDidChange UITextRange *range = self.selectedTextRange; // super.attributedText = [markdownUtils parseMarkdown:self.attributedText]; [super setSelectedTextRange:range]; // prevents cursor from jumping at the end when typing in the middle of the text - self.typingAttributes = self.defaultTextAttributes; // removes indent in new line when typing after blockquote + // self.typingAttributes = self.defaultTextAttributes; // removes indent in new line when typing after blockquote + // NOTE: setting self.typingAttributes removes underline } // Call the original method From 31cbdb220a116855d954ccb5900349f3f0d9044a Mon Sep 17 00:00:00 2001 From: Tomek Zawadzki Date: Tue, 13 Feb 2024 12:54:40 +0100 Subject: [PATCH 3/5] Found a fix --- ios/RCTBaseTextInputView+Markdown.m | 2 +- ios/RCTMarkdownUtils.mm | 9 +++++++-- ios/RCTUITextView+Markdown.mm | 2 +- 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/ios/RCTBaseTextInputView+Markdown.m b/ios/RCTBaseTextInputView+Markdown.m index 53521cf3..ad254869 100644 --- a/ios/RCTBaseTextInputView+Markdown.m +++ b/ios/RCTBaseTextInputView+Markdown.m @@ -29,7 +29,7 @@ - (void)markdown_updateLocalData if (markdownUtils != nil) { UITextRange *range = self.backedTextInputView.selectedTextRange; NSAttributedString *attributedText = [markdownUtils parseMarkdown:self.backedTextInputView.attributedText]; - // [self.backedTextInputView setAttributedText:attributedText]; + [self.backedTextInputView setAttributedText:attributedText]; [self.backedTextInputView setSelectedTextRange:range notifyDelegate:YES]; } diff --git a/ios/RCTMarkdownUtils.mm b/ios/RCTMarkdownUtils.mm index 06600099..982b9a78 100644 --- a/ios/RCTMarkdownUtils.mm +++ b/ios/RCTMarkdownUtils.mm @@ -2,6 +2,7 @@ #import "react_native_assert.h" #import #import +#import #import @implementation RCTMarkdownUtils { @@ -47,13 +48,17 @@ - (NSAttributedString *)parseMarkdown:(nullable NSAttributedString *)input JSValue *result = [function callWithArguments:@[inputString]]; NSArray *ranges = [result toArray]; - NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:inputString attributes:_backedTextInputView.defaultTextAttributes]; + NSMutableAttributedString *attributedString = [input mutableCopy]; [attributedString beginEditing]; + NSMutableDictionary *attributes = [_backedTextInputView.defaultTextAttributes mutableCopy]; + [attributes removeObjectForKey:RCTTextAttributesTagAttributeName]; + [attributedString addAttributes:attributes range:NSMakeRange(0, attributedString.length)]; + // If the attributed string ends with underlined text, blurring the single-line input imprints the underline style across the whole string. // It looks like a bug in iOS, as there is no underline style to be found in the attributed string, especially after formatting. // This is a workaround that applies the NSUnderlineStyleNone to the string before iterating over ranges which resolves this problem. - [attributedString addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleNone] range:NSMakeRange(0, attributedString.length)]; + [attributedString removeAttribute:NSUnderlineStyleAttributeName range:NSMakeRange(0, attributedString.length)]; _blockquoteRanges = [NSMutableArray new]; diff --git a/ios/RCTUITextView+Markdown.mm b/ios/RCTUITextView+Markdown.mm index 7ff8057a..7c8bbb74 100644 --- a/ios/RCTUITextView+Markdown.mm +++ b/ios/RCTUITextView+Markdown.mm @@ -17,7 +17,7 @@ - (void)markdown_textDidChange RCTMarkdownUtils *markdownUtils = [self getMarkdownUtils]; if (markdownUtils != nil) { UITextRange *range = self.selectedTextRange; - // super.attributedText = [markdownUtils parseMarkdown:self.attributedText]; + super.attributedText = [markdownUtils parseMarkdown:self.attributedText]; [super setSelectedTextRange:range]; // prevents cursor from jumping at the end when typing in the middle of the text // self.typingAttributes = self.defaultTextAttributes; // removes indent in new line when typing after blockquote // NOTE: setting self.typingAttributes removes underline From dbaed87a89603769ab04f9d6b98e1bd23d666848 Mon Sep 17 00:00:00 2001 From: Tomek Zawadzki Date: Tue, 13 Feb 2024 12:59:21 +0100 Subject: [PATCH 4/5] Uncomment some more code --- ios/RCTUITextView+Markdown.mm | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/ios/RCTUITextView+Markdown.mm b/ios/RCTUITextView+Markdown.mm index 7c8bbb74..dd08b87b 100644 --- a/ios/RCTUITextView+Markdown.mm +++ b/ios/RCTUITextView+Markdown.mm @@ -19,8 +19,7 @@ - (void)markdown_textDidChange UITextRange *range = self.selectedTextRange; super.attributedText = [markdownUtils parseMarkdown:self.attributedText]; [super setSelectedTextRange:range]; // prevents cursor from jumping at the end when typing in the middle of the text - // self.typingAttributes = self.defaultTextAttributes; // removes indent in new line when typing after blockquote - // NOTE: setting self.typingAttributes removes underline + self.typingAttributes = self.defaultTextAttributes; // removes indent in new line when typing after blockquote } // Call the original method From 642651cfce666c12975a45b7de90af6ae05e262e Mon Sep 17 00:00:00 2001 From: Tomek Zawadzki Date: Tue, 13 Feb 2024 15:16:03 +0100 Subject: [PATCH 5/5] Update comment --- ios/RCTMarkdownUtils.mm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ios/RCTMarkdownUtils.mm b/ios/RCTMarkdownUtils.mm index 982b9a78..213b426b 100644 --- a/ios/RCTMarkdownUtils.mm +++ b/ios/RCTMarkdownUtils.mm @@ -57,7 +57,7 @@ - (NSAttributedString *)parseMarkdown:(nullable NSAttributedString *)input // If the attributed string ends with underlined text, blurring the single-line input imprints the underline style across the whole string. // It looks like a bug in iOS, as there is no underline style to be found in the attributed string, especially after formatting. - // This is a workaround that applies the NSUnderlineStyleNone to the string before iterating over ranges which resolves this problem. + // The workaround is to remove NSUnderlineStyleAttributeName attribute for whole string before iterating over ranges. [attributedString removeAttribute:NSUnderlineStyleAttributeName range:NSMakeRange(0, attributedString.length)]; _blockquoteRanges = [NSMutableArray new];