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

Closed
wants to merge 6 commits into from
Closed
Changes from all 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
11 changes: 8 additions & 3 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)];
// The workaround is to remove NSUnderlineStyleAttributeName attribute for whole string before iterating over ranges.
[attributedString removeAttribute:NSUnderlineStyleAttributeName range:NSMakeRange(0, attributedString.length)];
Comment on lines +60 to +61
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This previous version of this workaround breaks the diacritics context:

Screen.Recording.2024-02-13.at.15.20.11.mov

If we remove the workaround, the diacritics context works correctly but the link underline persists:

Screen.Recording.2024-02-13.at.15.18.25.mov

If we modify the workaround so it uses removeAttribute, it works correctly:

Screen.Recording.2024-02-13.at.15.22.56.mov

Copy link
Collaborator

Choose a reason for hiding this comment

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

Can you check on the single line input as well? I'm testing on it, and the current version of the underline workaroud doesn't work for me when multiline={false}

Reproduction steps
  1. set multiline prop of MarkdownTextInput to false
  2. Write any link inside text input
  3. press enter to submit - notice that it blurs the input
  4. focus the input and start typing - notice that underline is applied to whole input
Screen.Recording.2024-02-13.at.19.01.46.mov

Copy link
Collaborator Author

@tomekzaw tomekzaw Feb 13, 2024

Choose a reason for hiding this comment

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

Okay, thanks, didn't notice this comment. I confirm this is reproducible on my end as well. Will investigate that!

Screen.Recording.2024-02-14.at.10.54.36.mov


_blockquoteRanges = [NSMutableArray new];

Expand Down
Loading