Skip to content

Commit

Permalink
ensure every quote span has correctly set up start point
Browse files Browse the repository at this point in the history
  • Loading branch information
robertKozik committed Feb 8, 2024
1 parent 7e65264 commit 1fb94f7
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ public class MarkdownBlockquoteSpan implements MarkdownSpan, LeadingMarginSpan {
private final float borderWidth;
private final float marginLeft;
private final float paddingLeft;
private int nestingLevel = 0;

public MarkdownBlockquoteSpan(@ColorInt int borderColor, float borderWidth, float marginLeft, float paddingLeft) {
this.borderColor = borderColor;
Expand All @@ -26,10 +25,9 @@ public MarkdownBlockquoteSpan(@ColorInt int borderColor, float borderWidth, floa

@Override
public int getLeadingMargin(boolean first) {
return (int) (marginLeft + borderWidth + paddingLeft) * (nestingLevel + 1);
return (int) (marginLeft + borderWidth + paddingLeft);
}

public void increaseNestingLevel() { nestingLevel++; };
@Override
public void drawLeadingMargin(Canvas c, Paint p, int x, int dir, int top, int baseline, int bottom,
CharSequence text, int start, int end, boolean first, Layout layout) {
Expand All @@ -39,13 +37,9 @@ public void drawLeadingMargin(Canvas c, Paint p, int x, int dir, int top, int ba
p.setStyle(Paint.Style.FILL);
p.setColor(borderColor);

for(int stripe = 0; stripe <= nestingLevel; stripe++) {

float shift = (borderWidth + marginLeft + paddingLeft) * stripe;
float left = x + dir * marginLeft + shift;
float right = x + dir * (marginLeft + borderWidth) + shift;
c.drawRect(left, top, right, bottom, p);
}
float left = x + dir * marginLeft;
float right = x + dir * (marginLeft + borderWidth);
c.drawRect(left, top, right, bottom, p);

p.setStyle(originalStyle);
p.setColor(originalColor);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,32 +151,21 @@ private void applyRange(SpannableStringBuilder ssb, String type, int start, int
setSpan(ssb, new MarkdownFontSizeSpan(mMarkdownStyle.getH1FontSize()), start, end);
break;
case "blockquote":
Object containSpan = checkIfInsideSpanType(ssb, MarkdownBlockquoteSpan.class, start, end);
if (containSpan != null) {
MarkdownBlockquoteSpan blockquoteSpan = (MarkdownBlockquoteSpan) containSpan;
blockquoteSpan.increaseNestingLevel();
break;
}
MarkdownBlockquoteSpan[] overlappingBlockquotes = ssb.getSpans(start, end, MarkdownBlockquoteSpan.class);
// get the start of the first overlapping blockquote - start of the leadingMarginSpan should be the start of the line
int startIndex = overlappingBlockquotes.length > 0 ? ssb.getSpanStart(overlappingBlockquotes[0]) : start;
MarkdownBlockquoteSpan span = new MarkdownBlockquoteSpan(
mMarkdownStyle.getBlockquoteBorderColor(),
mMarkdownStyle.getBlockquoteBorderWidth(),
mMarkdownStyle.getBlockquoteMarginLeft(),
mMarkdownStyle.getBlockquotePaddingLeft());
setSpan(ssb, span, start, end);
setSpan(ssb, span, startIndex, end);
break;
default:
throw new IllegalStateException("Unsupported type: " + type);
}
}

private Object checkIfInsideSpanType(SpannableStringBuilder ssb, Class<?> spanClass, int start, int end) {
Object[] spans = ssb.getSpans(start, end, spanClass);
if(spans.length == 0) {
return null;
}
return spans[0];
}

private void setSpan(SpannableStringBuilder ssb, MarkdownSpan span, int start, int end) {
ssb.setSpan(span, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
Expand Down

0 comments on commit 1fb94f7

Please sign in to comment.