Skip to content

Commit

Permalink
Found a fix
Browse files Browse the repository at this point in the history
  • Loading branch information
tomekzaw committed Feb 13, 2024
1 parent 105b607 commit 31cbdb2
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 4 deletions.
2 changes: 1 addition & 1 deletion ios/RCTBaseTextInputView+Markdown.m
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}

Expand Down
9 changes: 7 additions & 2 deletions ios/RCTMarkdownUtils.mm
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#import "react_native_assert.h"
#import <React/RCTAssert.h>
#import <React/RCTFont.h>
#import <React/RCTTextAttributes.h>
#import <JavaScriptCore/JavaScriptCore.h>

@implementation RCTMarkdownUtils {
Expand Down Expand Up @@ -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<NSAttributedStringKey, id> *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];

Expand Down
2 changes: 1 addition & 1 deletion ios/RCTUITextView+Markdown.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 31cbdb2

Please sign in to comment.