Skip to content

Commit

Permalink
Add MarkdownUtils class
Browse files Browse the repository at this point in the history
  • Loading branch information
tomekzaw committed Dec 1, 2023
1 parent 53f8a9e commit 60602b9
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,14 @@ public void onTextChanged(CharSequence s, int start, int before, int count) {
@Override
public void afterTextChanged(Editable editable) {
if (!mIsFormatting) {
mIsFormatting = true;
mIsFormatting = true; // prevent recursive calls
if (editable instanceof SpannableStringBuilder) {
SpannableStringBuilder ssb = (SpannableStringBuilder) editable;
int selectionStart = mReactEditText.getSelectionStart();
int selectionEnd = mReactEditText.getSelectionEnd();
String text = ssb.toString();
int length = text.length();
ssb.clear();
ssb.append(text);
MarkdownUtils.applyMarkdownFormatting(ssb);
int length = ssb.length();
mReactEditText.setSelection(Math.min(selectionStart, length), Math.min(selectionEnd, length));
int flag = Spanned.SPAN_EXCLUSIVE_EXCLUSIVE;
ssb.setSpan(new StyleSpan(Typeface.BOLD), 0, Math.min(3, ssb.length()), flag);
}
mIsFormatting = false;
}
Expand Down
20 changes: 20 additions & 0 deletions android/src/main/java/com/markdowntextinput/MarkdownUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.markdowntextinput;

import android.graphics.Typeface;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.StyleSpan;

public class MarkdownUtils {
public static void applyMarkdownFormatting(SpannableStringBuilder ssb) {
String text = ssb.toString();
ssb.clear();
ssb.append(text);

// TODO: parse Markdown here

// TODO: apply formatting here
int flag = Spanned.SPAN_EXCLUSIVE_EXCLUSIVE;
ssb.setSpan(new StyleSpan(Typeface.BOLD), 0, Math.min(5, text.length()), flag);
}
}

0 comments on commit 60602b9

Please sign in to comment.