diff --git a/android/src/main/java/com/expensify/livemarkdown/MarkdownRange.java b/android/src/main/java/com/expensify/livemarkdown/MarkdownRange.java new file mode 100644 index 00000000..9c3aedd3 --- /dev/null +++ b/android/src/main/java/com/expensify/livemarkdown/MarkdownRange.java @@ -0,0 +1,31 @@ +package com.expensify.livemarkdown; + +public class MarkdownRange { + private final String mType; + private final int mStart; + private final int mLength; + private final int mDepth; + + public MarkdownRange(String type, int start, int length, int depth) { + mType = type; + mStart = start; + mLength = length; + mDepth = depth; + } + + public String getType() { + return mType; + } + + public int getStart() { + return mStart; + } + + public int getLength() { + return mLength; + } + + public int getDepth() { + return mDepth; + } +} diff --git a/android/src/main/java/com/expensify/livemarkdown/MarkdownUtils.java b/android/src/main/java/com/expensify/livemarkdown/MarkdownUtils.java index 02811c98..3451fdb4 100644 --- a/android/src/main/java/com/expensify/livemarkdown/MarkdownUtils.java +++ b/android/src/main/java/com/expensify/livemarkdown/MarkdownUtils.java @@ -16,6 +16,8 @@ import org.json.JSONException; import org.json.JSONObject; +import java.util.LinkedList; +import java.util.List; import java.util.Objects; public class MarkdownUtils { @@ -68,6 +70,7 @@ public void applyMarkdownFormatting(SpannableStringBuilder ssb) { mPrevParserId = mParserId; } + List markdownRanges = new LinkedList<>(); try { JSONArray ranges = new JSONArray(output); for (int i = 0; i < ranges.length(); i++) { @@ -80,14 +83,21 @@ public void applyMarkdownFormatting(SpannableStringBuilder ssb) { if (length == 0 || end > input.length()) { continue; } - applyRange(ssb, type, start, end, depth); + markdownRanges.add(new MarkdownRange(type, start, length, depth)); } } catch (JSONException e) { RNLog.w(mReactContext, "[react-native-live-markdown] Incorrect schema of worklet parser output: " + e.getMessage()); } + + for (MarkdownRange markdownRange : markdownRanges) { + applyRange(ssb, markdownRange); + } } - private void applyRange(SpannableStringBuilder ssb, String type, int start, int end, int depth) { + private void applyRange(SpannableStringBuilder ssb, MarkdownRange markdownRange) { + String type = markdownRange.getType(); + int start = markdownRange.getStart(); + int end = start + markdownRange.getLength(); switch (type) { case "bold": setSpan(ssb, new MarkdownBoldSpan(), start, end); @@ -149,7 +159,7 @@ private void applyRange(SpannableStringBuilder ssb, String type, int start, int mMarkdownStyle.getBlockquoteBorderWidth(), mMarkdownStyle.getBlockquoteMarginLeft(), mMarkdownStyle.getBlockquotePaddingLeft(), - depth); + markdownRange.getDepth()); setSpan(ssb, span, start, end); break; }