Skip to content

Commit

Permalink
#363 Context based formatting
Browse files Browse the repository at this point in the history
- Use selection as link target instead of label when it starts with `http`
  • Loading branch information
stefan-niedermann committed Feb 8, 2019
1 parent f89357e commit 4f88b37
Showing 1 changed file with 14 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.graphics.Typeface;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.text.style.StyleSpan;
import android.util.SparseIntArray;
import android.view.ActionMode;
Expand Down Expand Up @@ -80,12 +81,22 @@ public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
editText.setSelection(end + markdown.length() * 2);
break;
case R.id.link:
ssb.insert(end, "]()");
ssb.insert(start, "[");
boolean textToFormatIsLink = TextUtils.indexOf(editText.getText().subSequence(start, end), "http") == 0;
if(textToFormatIsLink) {
ssb.insert(end, ")");
ssb.insert(start, "[](");
} else {
ssb.insert(end, "]()");
ssb.insert(start, "[");
}
end++;
ssb.setSpan(new StyleSpan(Typeface.NORMAL), start, end, 1);
editText.setText(ssb);
editText.setSelection(end + 2); // after <end>](
if(textToFormatIsLink) {
editText.setSelection(start + 1);
} else {
editText.setSelection(end + 2); // after <end>](
}
return true;
}
return false;
Expand Down

0 comments on commit 4f88b37

Please sign in to comment.