-
Notifications
You must be signed in to change notification settings - Fork 97
[iOS & Android] Enable borderRadius in all mention types #720
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
Changes from all commits
0986812
195ad24
1b26ad4
521e78c
9d04008
5a7c73c
ddeba51
ac410ee
870b5da
99cf1fb
8b416a5
28b6a13
98dafda
a105c10
db307b2
8d5e9b2
bd8aaf3
2a5069e
cbab252
f1b8c74
46ad23f
fe24512
39e0318
0da8207
48830e5
6665f83
c556257
eb521b5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -63,19 +63,27 @@ public class MarkdownStyle { | |
| @ColorInt | ||
| private final int mMentionHereBackgroundColor; | ||
|
|
||
| private final float mMentionHereBorderRadius; | ||
|
|
||
| @ColorInt | ||
| private final int mMentionUserColor; | ||
|
|
||
| @ColorInt | ||
| private final int mMentionUserBackgroundColor; | ||
|
|
||
| private final float mMentionUserBorderRadius; | ||
|
|
||
| @ColorInt | ||
| private final int mMentionReportColor; | ||
|
|
||
| @ColorInt | ||
| private final int mMentionReportBackgroundColor; | ||
|
|
||
| private final float mMentionReportBorderRadius; | ||
|
|
||
| public MarkdownStyle(@NonNull ReadableMap map, @NonNull Context context) { | ||
| float screenDensity = context.getResources().getDisplayMetrics().density; | ||
|
|
||
| mSyntaxColor = parseColor(map, "syntax", "color", context); | ||
| mLinkColor = parseColor(map, "link", "color", context); | ||
| mH1FontSize = parseFloat(map, "h1", "fontSize"); | ||
|
|
@@ -95,10 +103,13 @@ public MarkdownStyle(@NonNull ReadableMap map, @NonNull Context context) { | |
| mPreBackgroundColor = parseColor(map, "pre", "backgroundColor", context); | ||
| mMentionHereColor = parseColor(map, "mentionHere", "color", context); | ||
| mMentionHereBackgroundColor = parseColor(map, "mentionHere", "backgroundColor", context); | ||
| mMentionHereBorderRadius = parseFloat(map, "mentionHere", "borderRadius") * screenDensity; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd rather keep the original value of
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I tried that but context is not available in span so I had to pass it down and the code looked messy |
||
| mMentionUserColor = parseColor(map, "mentionUser", "color", context); | ||
| mMentionUserBackgroundColor = parseColor(map, "mentionUser", "backgroundColor", context); | ||
| mMentionUserBorderRadius = parseFloat(map, "mentionUser", "borderRadius") * screenDensity; | ||
| mMentionReportColor = parseColor(map, "mentionReport", "color", context); | ||
| mMentionReportBackgroundColor = parseColor(map, "mentionReport", "backgroundColor", context); | ||
| mMentionReportBorderRadius = parseFloat(map, "mentionReport", "borderRadius") * screenDensity; | ||
| } | ||
|
|
||
| private static int parseColor(@NonNull ReadableMap map, @NonNull String key, @NonNull String prop, @NonNull Context context) { | ||
|
|
@@ -213,6 +224,10 @@ public int getMentionHereBackgroundColor() { | |
| return mMentionHereBackgroundColor; | ||
| } | ||
|
|
||
| public float getMentionHereBorderRadius() { | ||
| return mMentionHereBorderRadius; | ||
| } | ||
|
|
||
| @ColorInt | ||
| public int getMentionUserColor() { | ||
| return mMentionUserColor; | ||
|
|
@@ -223,6 +238,10 @@ public int getMentionUserBackgroundColor() { | |
| return mMentionUserBackgroundColor; | ||
| } | ||
|
|
||
| public float getMentionUserBorderRadius() { | ||
| return mMentionUserBorderRadius; | ||
| } | ||
|
|
||
| @ColorInt | ||
| public int getMentionReportColor() { | ||
| return mMentionReportColor; | ||
|
|
@@ -232,4 +251,8 @@ public int getMentionReportColor() { | |
| public int getMentionReportBackgroundColor() { | ||
| return mMentionReportBackgroundColor; | ||
| } | ||
|
|
||
| public float getMentionReportBorderRadius() { | ||
| return mMentionReportBorderRadius; | ||
| } | ||
| } | ||
war-in marked this conversation as resolved.
Show resolved
Hide resolved
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| package com.expensify.livemarkdown.spans; | ||
|
|
||
| import android.graphics.Canvas; | ||
| import android.graphics.Paint; | ||
| import android.graphics.Path; | ||
| import android.graphics.RectF; | ||
| import android.text.StaticLayout; | ||
| import android.text.TextPaint; | ||
| import android.text.style.LineBackgroundSpan; | ||
|
|
||
| import androidx.annotation.ColorInt; | ||
| import androidx.annotation.NonNull; | ||
|
|
||
| public class MarkdownBackgroundSpan implements MarkdownSpan, LineBackgroundSpan { | ||
|
|
||
| private final int backgroundColor; | ||
| private final int mentionStart; | ||
| private final int mentionEnd; | ||
| private final float borderRadius; | ||
|
|
||
| private StaticLayout layout; | ||
| private Path backgroundPath; | ||
|
|
||
| public MarkdownBackgroundSpan(@ColorInt int backgroundColor, float borderRadius, int mentionStart, int mentionEnd) { | ||
| this.backgroundColor = backgroundColor; | ||
| this.borderRadius = borderRadius; | ||
| this.mentionStart = mentionStart; | ||
| this.mentionEnd = mentionEnd; | ||
| this.backgroundPath = new Path(); | ||
| } | ||
|
|
||
| @Override | ||
| public void drawBackground( | ||
| @NonNull Canvas canvas, | ||
| @NonNull Paint paint, | ||
| int left, | ||
| int right, | ||
| int top, | ||
| int baseline, | ||
| int bottom, | ||
| @NonNull CharSequence text, | ||
| int start, | ||
| int end, | ||
| int lnum | ||
| ) { | ||
| CharSequence lineText = text.subSequence(start, end); | ||
| if (layout == null || layout.getText() != lineText || layout.getWidth() != right || layout.getLineEnd(0) != lineText.length()) { | ||
| int currentLineStart = 0; | ||
| int currentLineEnd = lineText.length(); | ||
| // Create layout for the current line only | ||
| layout = StaticLayout.Builder.obtain(lineText, currentLineStart, currentLineEnd, (TextPaint) paint, right).build(); | ||
|
|
||
| int relativeMentionStart = mentionStart - start; | ||
| int relativeMentionEnd = mentionEnd - start; | ||
|
|
||
| boolean mentionStartsInCurrentLine = currentLineStart <= relativeMentionStart; | ||
| boolean mentionEndsInCurrentLine = currentLineEnd >= relativeMentionEnd; | ||
|
|
||
| float startX = layout.getPrimaryHorizontal(mentionStartsInCurrentLine ? relativeMentionStart: currentLineStart); | ||
| float endX = layout.getPrimaryHorizontal(mentionEndsInCurrentLine ? relativeMentionEnd : currentLineEnd); | ||
|
|
||
| Paint.FontMetrics fm = paint.getFontMetrics(); | ||
| float startY = baseline + fm.ascent; | ||
| float endY = baseline + fm.descent; | ||
|
|
||
| RectF lineRect = new RectF(startX, startY, endX, endY); | ||
| backgroundPath.reset(); | ||
| backgroundPath.addRoundRect(lineRect, createRadii(mentionStartsInCurrentLine, mentionEndsInCurrentLine), Path.Direction.CW); | ||
| } | ||
|
|
||
| int originalColor = paint.getColor(); | ||
| paint.setColor(backgroundColor); | ||
|
|
||
| canvas.drawPath(backgroundPath, paint); | ||
|
|
||
| paint.setColor(originalColor); | ||
| } | ||
|
|
||
| private float[] createRadii(boolean roundedLeft, boolean roundedRight) { | ||
| float[] radii = new float[8]; | ||
|
|
||
| if (roundedLeft) { | ||
| radii[0] = radii[1] = borderRadius; // top-left | ||
| radii[6] = radii[7] = borderRadius; // bottom-left | ||
| } | ||
|
|
||
| if (roundedRight) { | ||
| radii[2] = radii[3] = borderRadius; // top-right | ||
| radii[4] = radii[5] = borderRadius; // bottom-right | ||
| } | ||
|
|
||
| return radii; | ||
| } | ||
| } |
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,15 +1,18 @@ | ||
| #import <RNLiveMarkdown/RCTMarkdownUtils.h> | ||
| #import <RNLiveMarkdown/RCTMarkdownTextBackgroundWithRange.h> | ||
| #import <UIKit/UIKit.h> | ||
|
|
||
| NS_ASSUME_NONNULL_BEGIN | ||
|
|
||
| API_AVAILABLE(ios(15.0)) | ||
| @interface BlockquoteTextLayoutFragment : NSTextLayoutFragment | ||
| @interface MarkdownTextLayoutFragment : NSTextLayoutFragment | ||
|
|
||
| @property (nonnull, atomic) RCTMarkdownUtils *markdownUtils; | ||
|
|
||
| @property NSUInteger depth; | ||
war-in marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| @property NSMutableArray<RCTMarkdownTextBackgroundWithRange *> *mentions; | ||
|
|
||
| @end | ||
|
|
||
| NS_ASSUME_NONNULL_END | ||
Uh oh!
There was an error while loading. Please reload this page.