Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix backticks not removed when entering diacritics using option key commands on iOS #182

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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];
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had to use mutableCopy here because creating NSMutableAttributedString from NSString loses the information about diacritics context.

[attributedString beginEditing];

NSMutableDictionary<NSAttributedStringKey, id> *attributes = [_backedTextInputView.defaultTextAttributes mutableCopy];
[attributes removeObjectForKey:RCTTextAttributesTagAttributeName];
[attributedString addAttributes:attributes range:NSMakeRange(0, attributedString.length)];
Comment on lines +54 to +56
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because of the above change, now the attributed string also contains previous styling so I need to apply default text attributes in order to reset it.

For some reason, restoring RCTTextAttributesTagAttributeName attribute also breaks the diacritics context so I need to make sure it is not applied. This can be done either by removing it from the dictionary or calling [NSAttributedString removeAttribute] later on.

This attribute is also removed by React Native itself: https://github.com/facebook/react-native/blob/f9a5b30e5a805061416123d037351e5c03860756/packages/react-native/Libraries/Text/TextInput/RCTBaseTextInputView.mm#L175-L179


// 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
Loading