Skip to content

Commit

Permalink
Markdown: Checkbox textaction, by @jdckr (#429)
Browse files Browse the repository at this point in the history
* initial implementation of multiline action

* create own class for text selection to couple insertion and deletion of text and selection indexes

* added new button to insert checkbox

* reformatted code

* added support for checking of check box

* added support for auto insert for checkbox

* exchanged checkbox icon
  • Loading branch information
jdckr authored and gsantner committed Nov 21, 2018
1 parent 556a852 commit dea8ff3
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ private String createIndentForNextLine(Spanned dest, int dend, int istart) {
// This is for any line that is not the first line in a file
Matcher listMatcher = MarkdownHighlighterPattern.LIST_UNORDERED.pattern.matcher(dest.toString().substring(iend, dend));
if (listMatcher.find()) {
return dest.subSequence(istart, iend) + Character.toString(dest.charAt(iend)) + " ";
return listMatcher.group() + " ";
} else {
Matcher m = MarkdownHighlighterPattern.LIST_ORDERED.pattern.matcher(dest.toString().substring(iend, dend));
if (m.find()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ public void appendTextModuleActionsToBar(ViewGroup barLayout) {
{R.string.tmaid_markdown_ul, R.drawable.ic_list_black_24dp},
{R.string.tmaid_markdown_ol, R.drawable.ic_format_list_numbered_black_24dp},
{R.string.tmaid_color_picker, CommonTextModuleActions.ACTION_COLOR_PICKER_ICON},
{R.string.tmaid_checkbox, R.drawable.ic_check_box_black_24dp},
};

private class MarkdownTextModuleActionsImpl implements View.OnClickListener, View.OnLongClickListener {
Expand Down Expand Up @@ -115,6 +116,10 @@ public void onClick(View v) {
runMarkdownRegularPrefixAction("1. ");
break;
}
case R.string.tmaid_checkbox: {
runMarkdownRegularPrefixAction("- [ ] ", "- [x] ");
break;
}
case R.string.tmaid_markdown_bold: {
runMarkdownInlineAction("**");
break;
Expand Down Expand Up @@ -236,17 +241,32 @@ private int findNextLine(int startIndex, int endIndex, String text) {
}

private void runMarkdownRegularPrefixAction(String action) {
runMarkdownRegularPrefixAction(action, null);
}

private void runMarkdownRegularPrefixAction(String action, String replaceString) {
String text = _hlEditor.getText().toString();
TextSelection textSelection = new TextSelection(_hlEditor.getSelectionStart(), _hlEditor.getSelectionEnd(), _hlEditor.getText());

int lineStart = findLineStart(textSelection.getSelectionStart(), text);

while (lineStart != -1) {
if (text.substring(lineStart, textSelection.getSelectionEnd()).startsWith(action)) {
textSelection.removeText(lineStart, action);
if (replaceString == null) {
if (text.substring(lineStart, textSelection.getSelectionEnd()).startsWith(action)) {
textSelection.removeText(lineStart, action);
} else {
textSelection.insertText(lineStart, action);
}
} else {
textSelection.insertText(lineStart, action);

if (text.substring(lineStart, textSelection.getSelectionEnd()).startsWith(action)) {
textSelection.removeText(lineStart, action);
textSelection.insertText(lineStart, replaceString);
} else if (text.substring(lineStart, textSelection.getSelectionEnd()).startsWith(replaceString)) {
textSelection.removeText(lineStart, replaceString);
textSelection.insertText(lineStart, action);
} else {
textSelection.insertText(lineStart, action);
}
}

text = _hlEditor.getText().toString();
Expand Down
9 changes: 9 additions & 0 deletions app/src/main/res/drawable/ic_check_box_black_24dp.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#FF000000"
android:pathData="M19,3L5,3c-1.11,0 -2,0.9 -2,2v14c0,1.1 0.89,2 2,2h14c1.11,0 2,-0.9 2,-2L21,5c0,-1.1 -0.89,-2 -2,-2zM10,17l-5,-5 1.41,-1.41L10,14.17l7.59,-7.59L19,8l-9,9z"/>
</vector>
1 change: 1 addition & 0 deletions app/src/main/res/values/string-not_translatable.xml
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ work. If not, see <https://creativecommons.org/publicdomain/zero/1.0/>.
<string name="tmaid_markdown_bold" translatable="false">tmaid_markdown_bold</string>
<string name="tmaid_markdown_italic" translatable="false">tmaid_markdown_italic</string>
<string name="tmaid_color_picker" translatable="false">tmaid_color_picker</string>
<string name="tmaid_checkbox" translatable="false">tmaid_checkbox</string>
<string name="tmaid_markdown_strikeout" translatable="false">tmaid_markdown_strikeout</string>
<string name="tmaid_markdown_code_inline" translatable="false">tmaid_markdown_code_inline</string>
<string name="tmaid_markdown_horizontal_line" translatable="false">tmaid_markdown_horizontal_line</string>
Expand Down

0 comments on commit dea8ff3

Please sign in to comment.