Skip to content

Commit

Permalink
Merge branch 'main' into merge
Browse files Browse the repository at this point in the history
  • Loading branch information
robertKozik committed Jan 16, 2024
2 parents 2e7c5d2 + 45bd891 commit d4cdddb
Show file tree
Hide file tree
Showing 17 changed files with 99 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@

import com.facebook.react.uimanager.PixelUtil;

public class QuoteSpan implements LeadingMarginSpan {
public class BlockquoteSpan implements LeadingMarginSpan {
private final int borderColor;
private final float borderWidth;
private final float marginLeft;
private final float paddingLeft;

public QuoteSpan(int borderColor, float borderWidth, float marginLeft, float paddingLeft) {
public BlockquoteSpan(int borderColor, float borderWidth, float marginLeft, float paddingLeft) {
this.borderColor = borderColor;
this.borderWidth = PixelUtil.toPixelFromDIP(borderWidth);
this.marginLeft = PixelUtil.toPixelFromDIP(marginLeft);
Expand Down
32 changes: 16 additions & 16 deletions android/src/main/java/com/expensify/livemarkdown/MarkdownStyle.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ public class MarkdownStyle {
private final int mSyntaxColor;
private final int mLinkColor;
private final float mH1FontSize;
private final int mQuoteBorderColor;
private final float mQuoteBorderWidth;
private final float mQuoteMarginLeft;
private final float mQuotePaddingLeft;
private final int mBlockquoteBorderColor;
private final float mBlockquoteBorderWidth;
private final float mBlockquoteMarginLeft;
private final float mBlockquotePaddingLeft;
private final String mCodeFontFamily;
private final int mCodeColor;
private final int mCodeBackgroundColor;
Expand All @@ -32,10 +32,10 @@ public MarkdownStyle(@NonNull ReadableMap map, @NonNull Context context) {
mSyntaxColor = parseColor(map, "syntax", "color", context);
mLinkColor = parseColor(map, "link", "color", context);
mH1FontSize = parseFloat(map, "h1", "fontSize");
mQuoteBorderColor = parseColor(map, "quote", "borderColor", context);
mQuoteBorderWidth = parseFloat(map, "quote", "borderWidth");
mQuoteMarginLeft = parseFloat(map, "quote", "marginLeft");
mQuotePaddingLeft = parseFloat(map, "quote", "paddingLeft");
mBlockquoteBorderColor = parseColor(map, "blockquote", "borderColor", context);
mBlockquoteBorderWidth = parseFloat(map, "blockquote", "borderWidth");
mBlockquoteMarginLeft = parseFloat(map, "blockquote", "marginLeft");
mBlockquotePaddingLeft = parseFloat(map, "blockquote", "paddingLeft");
mCodeFontFamily = parseString(map, "code", "fontFamily");
mCodeColor = parseColor(map, "code", "color", context);
mCodeBackgroundColor = parseColor(map, "code", "backgroundColor", context);
Expand Down Expand Up @@ -85,20 +85,20 @@ public float getH1FontSize() {
return mH1FontSize;
}

public int getQuoteBorderColor() {
return mQuoteBorderColor;
public int getBlockquoteBorderColor() {
return mBlockquoteBorderColor;
}

public float getQuoteBorderWidth() {
return mQuoteBorderWidth;
public float getBlockquoteBorderWidth() {
return mBlockquoteBorderWidth;
}

public float getQuoteMarginLeft() {
return mQuoteMarginLeft;
public float getBlockquoteMarginLeft() {
return mBlockquoteMarginLeft;
}

public float getQuotePaddingLeft() {
return mQuotePaddingLeft;
public float getBlockquotePaddingLeft() {
return mBlockquotePaddingLeft;
}

public String getCodeFontFamily() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,19 +144,19 @@ private Object makeH1FontSizeSpan() {
return new AbsoluteSizeSpan((int) mMarkdownStyle.getH1FontSize(), true);
}

private static Object makeHeadingLineHeightSpan(float lineHeight) {
private static Object makeH1LineHeightSpan(float lineHeight) {
return (LineHeightSpan) (text, start, end, spanstartv, lh, fm) -> {
fm.top -= lineHeight / 4;
fm.ascent -= lineHeight / 4;
};
}

private Object makeBlockquoteMarginSpan() {
return new QuoteSpan(
mMarkdownStyle.getQuoteBorderColor(),
mMarkdownStyle.getQuoteBorderWidth(),
mMarkdownStyle.getQuoteMarginLeft(),
mMarkdownStyle.getQuotePaddingLeft());
return new BlockquoteSpan(
mMarkdownStyle.getBlockquoteBorderColor(),
mMarkdownStyle.getBlockquoteBorderWidth(),
mMarkdownStyle.getBlockquoteMarginLeft(),
mMarkdownStyle.getBlockquotePaddingLeft());
}

public void applyMarkdownFormatting(SpannableStringBuilder ssb) {
Expand Down Expand Up @@ -199,7 +199,7 @@ private void applyRange(SpannableStringBuilder ssb, String type, int start, int
case "strikethrough":
setSpan(ssb, makeStrikethroughSpan(), start, end);
break;
case "mention":
case "mention-here":
setSpan(ssb, makeBoldSpan(), start, end);
setSpan(ssb, makeMentionHereBackgroundSpan(), start, end);
break;
Expand Down Expand Up @@ -231,7 +231,7 @@ private void applyRange(SpannableStringBuilder ssb, String type, int start, int
CustomLineHeightSpan[] spans = ssb.getSpans(0, ssb.length(), CustomLineHeightSpan.class);
if (spans.length >= 1) {
int lineHeight = spans[0].getLineHeight();
setSpan(ssb, makeHeadingLineHeightSpan(lineHeight * 1.5f), start, end);
setSpan(ssb, makeH1LineHeightSpan(lineHeight * 1.5f), start, end);
}
// NOTE: size span must be set after line height span to avoid height jumps
setSpan(ssb, makeH1FontSizeSpan(), start, end);
Expand Down
4 changes: 4 additions & 0 deletions ios/MarkdownLayoutManager.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
#import <UIKit/UIKit.h>
#import <react-native-live-markdown/RCTMarkdownUtils.h>

NS_ASSUME_NONNULL_BEGIN

@interface MarkdownLayoutManager : NSLayoutManager

@property(nonatomic) RCTMarkdownUtils *markdownUtils;

@end

NS_ASSUME_NONNULL_END
14 changes: 7 additions & 7 deletions ios/MarkdownLayoutManager.mm
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,27 @@ - (void)drawBackgroundForGlyphRange:(NSRange)glyphsToShow atPoint:(CGPoint)origi
[super drawBackgroundForGlyphRange:glyphsToShow atPoint:origin];

[self enumerateLineFragmentsForGlyphRange:glyphsToShow usingBlock:^(CGRect rect, CGRect usedRect, NSTextContainer * _Nonnull textContainer, NSRange glyphRange, BOOL * _Nonnull stop) {
__block BOOL isQuote = NO;
__block BOOL isBlockquote = NO;
RCTMarkdownUtils *markdownUtils = [self valueForKey:@"markdownUtils"];
[markdownUtils.quoteRanges enumerateObjectsUsingBlock:^(NSValue *item, NSUInteger idx, BOOL * _Nonnull stop) {
[markdownUtils.blockquoteRanges enumerateObjectsUsingBlock:^(NSValue *item, NSUInteger idx, BOOL * _Nonnull stop) {
NSRange range = [item rangeValue];
NSUInteger start = range.location;
NSUInteger end = start + range.length;
NSUInteger location = glyphRange.location;
if (location >= start && location < end) {
isQuote = YES;
isBlockquote = YES;
*stop = YES;
}
}];
if (isQuote) {
if (isBlockquote) {
CGFloat paddingLeft = markdownUtils.backedTextInputView.textContainerInset.left;
CGFloat paddingTop = markdownUtils.backedTextInputView.textContainerInset.top;
CGFloat x = paddingLeft + markdownUtils.markdownStyle.quoteMarginLeft;
CGFloat x = paddingLeft + markdownUtils.markdownStyle.blockquoteMarginLeft;
CGFloat y = paddingTop + rect.origin.y;
CGFloat width = markdownUtils.markdownStyle.quoteBorderWidth;
CGFloat width = markdownUtils.markdownStyle.blockquoteBorderWidth;
CGFloat height = rect.size.height;
CGRect lineRect = CGRectMake(x, y, width, height);
[markdownUtils.markdownStyle.quoteBorderColor setFill];
[markdownUtils.markdownStyle.blockquoteBorderColor setFill];
UIRectFill(lineRect);
}
}];
Expand Down
6 changes: 5 additions & 1 deletion ios/MarkdownTextInputDecoratorView.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
#import <UIKit/UIKit.h>
#import <react-native-live-markdown/RCTMarkdownStyle.h>

NS_ASSUME_NONNULL_BEGIN

@interface MarkdownTextInputDecoratorView : UIView

- (void)setMarkdownStyle:(nonnull RCTMarkdownStyle *)markdownStyle;
- (void)setMarkdownStyle:(RCTMarkdownStyle *)markdownStyle;

@end

NS_ASSUME_NONNULL_END
2 changes: 1 addition & 1 deletion ios/MarkdownTextInputDecoratorView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ - (void)willMoveToWindow:(UIWindow *)newWindow
}
}

- (void)setMarkdownStyle:(nonnull RCTMarkdownStyle *)markdownStyle
- (void)setMarkdownStyle:(RCTMarkdownStyle *)markdownStyle
{
_markdownStyle = markdownStyle;
[_markdownUtils setMarkdownStyle:markdownStyle];
Expand Down
5 changes: 4 additions & 1 deletion ios/MarkdownTextInputDecoratorViewManager.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#import <React/RCTViewManager.h>

@interface MarkdownTextInputDecoratorViewManager : RCTViewManager
NS_ASSUME_NONNULL_BEGIN

@interface MarkdownTextInputDecoratorViewManager : RCTViewManager
@end

NS_ASSUME_NONNULL_END
32 changes: 18 additions & 14 deletions ios/RCTMarkdownStyle.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,25 @@
#import <react/renderer/components/RNLiveMarkdownSpec/Props.h>
#endif /* RCT_NEW_ARCH_ENABLED */

NS_ASSUME_NONNULL_BEGIN

@interface RCTMarkdownStyle : NSObject

@property (nonatomic, nonnull) UIColor *syntaxColor;
@property (nonatomic, nonnull) UIColor *linkColor;
@property (nonatomic) UIColor *syntaxColor;
@property (nonatomic) UIColor *linkColor;
@property (nonatomic) CGFloat h1FontSize;
@property (nonatomic, nonnull) UIColor *quoteBorderColor;
@property (nonatomic) CGFloat quoteBorderWidth;
@property (nonatomic) CGFloat quoteMarginLeft;
@property (nonatomic) CGFloat quotePaddingLeft;
@property (nonatomic, nonnull) NSString *codeFontFamily;
@property (nonatomic, nonnull) UIColor *codeColor;
@property (nonatomic, nonnull) UIColor *codeBackgroundColor;
@property (nonatomic, nonnull) NSString *preFontFamily;
@property (nonatomic, nonnull) UIColor *preColor;
@property (nonatomic, nonnull) UIColor *preBackgroundColor;
@property (nonatomic, nonnull) UIColor *mentionHereBackgroundColor;
@property (nonatomic, nonnull) UIColor *mentionUserBackgroundColor;
@property (nonatomic) UIColor *blockquoteBorderColor;
@property (nonatomic) CGFloat blockquoteBorderWidth;
@property (nonatomic) CGFloat blockquoteMarginLeft;
@property (nonatomic) CGFloat blockquotePaddingLeft;
@property (nonatomic) NSString *codeFontFamily;
@property (nonatomic) UIColor *codeColor;
@property (nonatomic) UIColor *codeBackgroundColor;
@property (nonatomic) NSString *preFontFamily;
@property (nonatomic) UIColor *preColor;
@property (nonatomic) UIColor *preBackgroundColor;
@property (nonatomic) UIColor *mentionHereBackgroundColor;
@property (nonatomic) UIColor *mentionUserBackgroundColor;

#ifdef RCT_NEW_ARCH_ENABLED
- (instancetype)initWithStruct:(const facebook::react::MarkdownTextInputDecoratorViewMarkdownStyleStruct &)style;
Expand All @@ -27,3 +29,5 @@
#endif /* RCT_NEW_ARCH_ENABLED */

@end

NS_ASSUME_NONNULL_END
20 changes: 10 additions & 10 deletions ios/RCTMarkdownStyle.mm
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ - (instancetype)initWithStruct:(const facebook::react::MarkdownTextInputDecorato

_h1FontSize = style.h1.fontSize;

_quoteBorderColor = RCTUIColorFromSharedColor(style.quote.borderColor);
_quoteBorderWidth = style.quote.borderWidth;
_quoteMarginLeft = style.quote.marginLeft;
_quotePaddingLeft = style.quote.paddingLeft;
_blockquoteBorderColor = RCTUIColorFromSharedColor(style.blockquote.borderColor);
_blockquoteBorderWidth = style.blockquote.borderWidth;
_blockquoteMarginLeft = style.blockquote.marginLeft;
_blockquotePaddingLeft = style.blockquote.paddingLeft;

_codeFontFamily = RCTNSStringFromString(style.code.fontFamily);
_codeColor = RCTUIColorFromSharedColor(style.code.color);
Expand All @@ -42,19 +42,19 @@ - (instancetype)initWithStruct:(const facebook::react::MarkdownTextInputDecorato

#else

- (instancetype)initWithDictionary:(nonnull NSDictionary *)json
- (instancetype)initWithDictionary:(NSDictionary *)json
{
if (self = [super init]) {
_syntaxColor = [RCTConvert UIColor:json[@"syntax"][@"color"]];

_linkColor = [RCTConvert UIColor:json[@"link"][@"color"]];

_h1FontSize = [json[@"h1"][@"fontSize"] floatValue];
_h1FontSize = [RCTConvert CGFloat:json[@"h1"][@"fontSize"]];

_quoteBorderColor = [RCTConvert UIColor:json[@"quote"][@"borderColor"]];
_quoteBorderWidth = [json[@"quote"][@"borderWidth"] floatValue];
_quoteMarginLeft = [json[@"quote"][@"marginLeft"] floatValue];
_quotePaddingLeft = [json[@"quote"][@"paddingLeft"] floatValue];
_quoteBorderColor = [RCTConvert UIColor:json[@"blockquote"][@"borderColor"]];
_quoteBorderWidth = [RCTConvert CGFloat:json[@"blockquote"][@"borderWidth"]];
_quoteMarginLeft = [RCTConvert CGFloat:json[@"blockquote"][@"marginLeft"]];
_quotePaddingLeft = [RCTConvert CGFloat:json[@"blockquote"][@"paddingLeft"]];

_codeFontFamily = [RCTConvert NSString:json[@"code"][@"fontFamily"]];
_codeColor = [RCTConvert UIColor:json[@"code"][@"color"]];
Expand Down
8 changes: 6 additions & 2 deletions ios/RCTMarkdownUtils.h
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
#import <React/RCTBackedTextInputViewProtocol.h>
#import <react-native-live-markdown/RCTMarkdownStyle.h>

NS_ASSUME_NONNULL_BEGIN

@interface RCTMarkdownUtils : NSObject

@property (nonatomic) RCTMarkdownStyle *markdownStyle;
@property (nonatomic) NSMutableArray<NSValue *> *quoteRanges;
@property (nonatomic) NSMutableArray<NSValue *> *blockquoteRanges;
@property (weak, nonatomic) UIView<RCTBackedTextInputViewProtocol> *backedTextInputView;

- (instancetype)initWithBackedTextInputView:(UIView<RCTBackedTextInputViewProtocol> *)backedTextInputView;

- (NSAttributedString *)parseMarkdown:(NSAttributedString *)input;
- (NSAttributedString *)parseMarkdown:(nullable NSAttributedString *)input;

@end

NS_ASSUME_NONNULL_END
12 changes: 6 additions & 6 deletions ios/RCTMarkdownUtils.mm
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ - (NSAttributedString *)parseMarkdown:(nullable NSAttributedString *)input
// 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)];

_quoteRanges = [NSMutableArray new];
_blockquoteRanges = [NSMutableArray new];

[ranges enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
NSArray *item = obj;
Expand All @@ -64,9 +64,9 @@ - (NSAttributedString *)parseMarkdown:(nullable NSAttributedString *)input
NSUInteger length = [item[2] unsignedIntegerValue];
NSRange range = NSMakeRange(location, length);

if ([type isEqualToString:@"bold"] || [type isEqualToString:@"mention"] || [type isEqualToString:@"mention-user"] || [type isEqualToString:@"syntax"] || [type isEqualToString:@"italic"] || [type isEqualToString:@"code"] || [type isEqualToString:@"pre"] || [type isEqualToString:@"h1"]) {
if ([type isEqualToString:@"bold"] || [type isEqualToString:@"mention-here"] || [type isEqualToString:@"mention-user"] || [type isEqualToString:@"syntax"] || [type isEqualToString:@"italic"] || [type isEqualToString:@"code"] || [type isEqualToString:@"pre"] || [type isEqualToString:@"h1"]) {
UIFont *font = [attributedString attribute:NSFontAttributeName atIndex:location effectiveRange:NULL];
if ([type isEqualToString:@"bold"] || [type isEqualToString:@"mention"] || [type isEqualToString:@"mention-user"] || [type isEqualToString:@"syntax"]) {
if ([type isEqualToString:@"bold"] || [type isEqualToString:@"mention-here"] || [type isEqualToString:@"mention-user"] || [type isEqualToString:@"syntax"]) {
font = [RCTFont updateFont:font withWeight:@"bold"];
} else if ([type isEqualToString:@"italic"]) {
font = [RCTFont updateFont:font withStyle:@"italic"];
Expand All @@ -92,7 +92,7 @@ - (NSAttributedString *)parseMarkdown:(nullable NSAttributedString *)input
} else if ([type isEqualToString:@"code"]) {
[attributedString addAttribute:NSForegroundColorAttributeName value:_markdownStyle.codeColor range:range];
[attributedString addAttribute:NSBackgroundColorAttributeName value:_markdownStyle.codeBackgroundColor range:range];
} else if ([type isEqualToString:@"mention"]) {
} else if ([type isEqualToString:@"mention-here"]) {
[attributedString addAttribute:NSBackgroundColorAttributeName value:_markdownStyle.mentionHereBackgroundColor range:range];
} else if ([type isEqualToString:@"mention-user"]) {
// TODO: change mention color when it mentions current user
Expand All @@ -101,12 +101,12 @@ - (NSAttributedString *)parseMarkdown:(nullable NSAttributedString *)input
[attributedString addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:range];
[attributedString addAttribute:NSForegroundColorAttributeName value:_markdownStyle.linkColor range:range];
} else if ([type isEqualToString:@"blockquote"]) {
CGFloat indent = _markdownStyle.quoteMarginLeft + _markdownStyle.quoteBorderWidth + _markdownStyle.quotePaddingLeft;
CGFloat indent = _markdownStyle.blockquoteMarginLeft + _markdownStyle.blockquoteBorderWidth + _markdownStyle.blockquotePaddingLeft;
NSMutableParagraphStyle *paragraphStyle = [NSMutableParagraphStyle new];
paragraphStyle.firstLineHeadIndent = indent;
paragraphStyle.headIndent = indent;
[attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:range];
[_quoteRanges addObject:[NSValue valueWithRange:range]];
[_blockquoteRanges addObject:[NSValue valueWithRange:range]];
} else if ([type isEqualToString:@"pre"]) {
[attributedString addAttribute:NSForegroundColorAttributeName value:_markdownStyle.preColor range:range];
NSRange rangeForBackground = [inputString characterAtIndex:range.location] == '\n' ? NSMakeRange(range.location + 1, range.length - 1) : range;
Expand Down
Loading

0 comments on commit d4cdddb

Please sign in to comment.