Skip to content

Commit

Permalink
Make it build
Browse files Browse the repository at this point in the history
  • Loading branch information
tomekzaw committed Oct 10, 2024
1 parent 2427010 commit 3f34111
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 30 deletions.
18 changes: 9 additions & 9 deletions apple/MarkdownTextInputDecoratorView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ @implementation MarkdownTextInputDecoratorView {
#else
__weak RCTBaseTextInputView *_textInput;
#endif /* RCT_NEW_ARCH_ENABLED */
__weak UIView<RCTBackedTextInputViewProtocol> *_backedTextInputView;
__weak RCTUIView<RCTBackedTextInputViewProtocol> *_backedTextInputView;
__weak RCTBackedTextFieldDelegateAdapter *_adapter;
__weak RCTUITextView *_textView;
}
Expand Down Expand Up @@ -78,11 +78,11 @@ - (void)didMoveToWindow {
CGSize contentSize = _textView.contentSize;
CGRect textBounds = [layoutManager usedRectForTextContainer:_textView.textContainer];
contentSize.height = textBounds.size.height + _textView.textContainerInset.top + _textView.textContainerInset.bottom;
[_textView setContentSize:contentSize];
// [_textView setContentSize:contentSize];

layoutManager.allowsNonContiguousLayout = NO; // workaround for onScroll issue
object_setClass(layoutManager, [MarkdownLayoutManager class]);
objc_setAssociatedObject(layoutManager, @selector(markdownUtils), _markdownUtils, OBJC_ASSOCIATION_RETAIN);
// layoutManager.allowsNonContiguousLayout = NO; // workaround for onScroll issue
// object_setClass(layoutManager, [MarkdownLayoutManager class]);
// objc_setAssociatedObject(layoutManager, @selector(markdownUtils), _markdownUtils, OBJC_ASSOCIATION_RETAIN);
} else {
react_native_assert(false && "Cannot enable Markdown for this type of TextInput.");
}
Expand All @@ -99,10 +99,10 @@ - (void)willMoveToWindow:(NSWindow *)newWindow
if (_textView != nil) {
[_textView setMarkdownUtils:nil];
NSLayoutManager *layoutManager = _textView.layoutManager;
if (layoutManager != nil && [object_getClass(layoutManager) isEqual:[MarkdownLayoutManager class]]) {
objc_setAssociatedObject(layoutManager, @selector(markdownUtils), nil, OBJC_ASSOCIATION_RETAIN);
object_setClass(layoutManager, [NSLayoutManager class]);
}
// if (layoutManager != nil && [object_getClass(layoutManager) isEqual:[MarkdownLayoutManager class]]) {
// objc_setAssociatedObject(layoutManager, @selector(markdownUtils), nil, OBJC_ASSOCIATION_RETAIN);
// object_setClass(layoutManager, [NSLayoutManager class]);
// }
}
}

Expand Down
5 changes: 0 additions & 5 deletions apple/RCTBackedTextFieldDelegateAdapter+Markdown.m
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,8 @@ - (void)markdown_textFieldDidChange
RCTMarkdownUtils *markdownUtils = [self getMarkdownUtils];
if (markdownUtils != nil) {
RCTUITextField *backedTextInputView = [self valueForKey:@"_backedTextInputView"];
<<<<<<<< HEAD:apple/RCTBackedTextFieldDelegateAdapter+Markdown.m
NSRange range = backedTextInputView.selectedRange;
backedTextInputView.attributedText = [markdownUtils parseMarkdown:backedTextInputView.attributedText];
========
UITextRange *range = backedTextInputView.selectedTextRange;
backedTextInputView.attributedText = [markdownUtils parseMarkdown:backedTextInputView.attributedText withAttributes:backedTextInputView.defaultTextAttributes];
>>>>>>>> main:apple/RCTBackedTextFieldDelegateAdapter+Markdown.mm
[backedTextInputView setSelectedTextRange:range notifyDelegate:YES];
}

Expand Down
5 changes: 0 additions & 5 deletions apple/RCTBackedTextFieldDelegateAdapter+Markdown.mm
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,8 @@ - (void)markdown_textFieldDidChange
RCTMarkdownUtils *markdownUtils = [self getMarkdownUtils];
if (markdownUtils != nil) {
RCTUITextField *backedTextInputView = [self valueForKey:@"_backedTextInputView"];
<<<<<<<< HEAD:apple/RCTBackedTextFieldDelegateAdapter+Markdown.m
NSRange range = backedTextInputView.selectedRange;
backedTextInputView.attributedText = [markdownUtils parseMarkdown:backedTextInputView.attributedText];
========
UITextRange *range = backedTextInputView.selectedTextRange;
backedTextInputView.attributedText = [markdownUtils parseMarkdown:backedTextInputView.attributedText withAttributes:backedTextInputView.defaultTextAttributes];
>>>>>>>> main:apple/RCTBackedTextFieldDelegateAdapter+Markdown.mm
[backedTextInputView setSelectedTextRange:range notifyDelegate:YES];
}

Expand Down
51 changes: 43 additions & 8 deletions apple/RCTBaseTextInputView+Markdown.m
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#import <react-native-live-markdown/RCTBaseTextInputView+Markdown.h>
#import <react-native-live-markdown/RCTMarkdownUtils.h>
#import <RNLiveMarkdown/RCTBaseTextInputView+Markdown.h>
#import <RNLiveMarkdown/RCTMarkdownUtils.h>
#import <objc/message.h>

@implementation RCTBaseTextInputView (Markdown)
Expand All @@ -16,21 +16,47 @@ - (void)markdown_setAttributedText:(NSAttributedString *)attributedText
{
RCTMarkdownUtils *markdownUtils = [self getMarkdownUtils];
if (markdownUtils != nil) {
attributedText = [markdownUtils parseMarkdown:attributedText];
attributedText = [markdownUtils parseMarkdown:attributedText withAttributes:self.backedTextInputView.defaultTextAttributes];
}

// Call the original method
[self markdown_setAttributedText:attributedText];
}

- (BOOL)markdown_textOf:(NSAttributedString *)newText equals:(NSAttributedString *)oldText
{
RCTMarkdownUtils *markdownUtils = [self getMarkdownUtils];
if (markdownUtils != nil) {
// Emoji characters are automatically assigned an AppleColorEmoji NSFont and the original font is moved to NSOriginalFont
// We need to remove these attributes before comparison
NSMutableAttributedString *newTextCopy = [newText mutableCopy];
NSMutableAttributedString *oldTextCopy = [oldText mutableCopy];
[newTextCopy removeAttribute:@"NSFont" range:NSMakeRange(0, newTextCopy.length)];
[oldTextCopy removeAttribute:@"NSFont" range:NSMakeRange(0, oldTextCopy.length)];
[oldTextCopy removeAttribute:@"NSOriginalFont" range:NSMakeRange(0, oldTextCopy.length)];
return [newTextCopy isEqualToAttributedString:oldTextCopy];
}

return [self markdown_textOf:newText equals:oldText];
}

- (void)markdown_updateLocalData
{
RCTMarkdownUtils *markdownUtils = [self getMarkdownUtils];
if (markdownUtils != nil) {
NSRange range = self.backedTextInputView.selectedRange;
NSAttributedString *attributedText = [markdownUtils parseMarkdown:self.backedTextInputView.attributedText];
[self.backedTextInputView setAttributedText:attributedText];
[self.backedTextInputView setSelectedTextRange:range notifyDelegate:YES];
id<RCTBackedTextInputViewProtocol> backedTextInputView = self.backedTextInputView;
NSAttributedString *oldAttributedText = backedTextInputView.attributedText;
NSAttributedString *newAttributedText = [markdownUtils parseMarkdown:oldAttributedText withAttributes:backedTextInputView.defaultTextAttributes];
NSRange range = backedTextInputView.selectedTextRange;

// update attributed text without emitting onSelectionChange event
id<RCTBackedTextInputDelegate> 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
Expand Down Expand Up @@ -58,7 +84,16 @@ + (void)load
SEL swizzledSelector = @selector(markdown_updateLocalData);
Method originalMethod = class_getInstanceMethod(cls, originalSelector);
Method swizzledMethod = class_getInstanceMethod(cls, swizzledSelector);
// method_exchangeImplementations(originalMethod, swizzledMethod);
method_exchangeImplementations(originalMethod, swizzledMethod);
}

{
// swizzle textOf
SEL originalSelector = @selector(textOf:equals:);
SEL swizzledSelector = @selector(markdown_textOf:equals:);
Method originalMethod = class_getInstanceMethod(cls, originalSelector);
Method swizzledMethod = class_getInstanceMethod(cls, swizzledSelector);
method_exchangeImplementations(originalMethod, swizzledMethod);
}
});
}
Expand Down
2 changes: 1 addition & 1 deletion apple/RCTBaseTextInputView+Markdown.mm
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ - (void)markdown_updateLocalData
id<RCTBackedTextInputViewProtocol> backedTextInputView = self.backedTextInputView;
NSAttributedString *oldAttributedText = backedTextInputView.attributedText;
NSAttributedString *newAttributedText = [markdownUtils parseMarkdown:oldAttributedText withAttributes:backedTextInputView.defaultTextAttributes];
UITextRange *range = backedTextInputView.selectedTextRange;
NSRange range = backedTextInputView.selectedTextRange;

// update attributed text without emitting onSelectionChange event
id<RCTBackedTextInputDelegate> delegate = backedTextInputView.textInputDelegate;
Expand Down
2 changes: 1 addition & 1 deletion apple/RCTMarkdownUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ NS_ASSUME_NONNULL_BEGIN
@interface RCTMarkdownUtils : NSObject

@property (nonatomic) RCTMarkdownStyle *markdownStyle;
@property (nonatomic) NSMutableArray<NSValue *> *blockquoteRangesAndLevels;
@property (nonatomic) NSMutableArray<NSDictionary *> *blockquoteRangesAndLevels;

- (NSAttributedString *)parseMarkdown:(nullable NSAttributedString *)input withAttributes:(nullable NSDictionary<NSAttributedStringKey, id>*)attributes;

Expand Down
2 changes: 1 addition & 1 deletion apple/RCTMarkdownUtils.mm
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ static void RCTApplyBaselineOffset(NSMutableAttributedString *attributedText)
return;
}

maximumFontLineHeight = MAX(font.lineHeight, maximumFontLineHeight);
maximumFontLineHeight = MAX(UIFontLineHeight(font), maximumFontLineHeight); // [macOS]
}];

if (maximumLineHeight < maximumFontLineHeight) {
Expand Down

0 comments on commit 3f34111

Please sign in to comment.