From dea8ff3983dc968656cc60453e03276afaa28d1b Mon Sep 17 00:00:00 2001
From: jdckr <40456550+jdckr@users.noreply.github.com>
Date: Wed, 21 Nov 2018 15:18:02 +0100
Subject: [PATCH] Markdown: Checkbox textaction, by @jdckr (#429)
* 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
---
.../format/markdown/MarkdownAutoFormat.java | 2 +-
.../markdown/MarkdownTextModuleActions.java | 28 ++++++++++++++++---
.../res/drawable/ic_check_box_black_24dp.xml | 9 ++++++
.../res/values/string-not_translatable.xml | 1 +
4 files changed, 35 insertions(+), 5 deletions(-)
create mode 100644 app/src/main/res/drawable/ic_check_box_black_24dp.xml
diff --git a/app/src/main/java/net/gsantner/markor/format/markdown/MarkdownAutoFormat.java b/app/src/main/java/net/gsantner/markor/format/markdown/MarkdownAutoFormat.java
index 96aafbacee..e0ff1c0487 100644
--- a/app/src/main/java/net/gsantner/markor/format/markdown/MarkdownAutoFormat.java
+++ b/app/src/main/java/net/gsantner/markor/format/markdown/MarkdownAutoFormat.java
@@ -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()) {
diff --git a/app/src/main/java/net/gsantner/markor/format/markdown/MarkdownTextModuleActions.java b/app/src/main/java/net/gsantner/markor/format/markdown/MarkdownTextModuleActions.java
index 59a564c1f1..d0eecab7f5 100644
--- a/app/src/main/java/net/gsantner/markor/format/markdown/MarkdownTextModuleActions.java
+++ b/app/src/main/java/net/gsantner/markor/format/markdown/MarkdownTextModuleActions.java
@@ -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 {
@@ -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;
@@ -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();
diff --git a/app/src/main/res/drawable/ic_check_box_black_24dp.xml b/app/src/main/res/drawable/ic_check_box_black_24dp.xml
new file mode 100644
index 0000000000..9948171c21
--- /dev/null
+++ b/app/src/main/res/drawable/ic_check_box_black_24dp.xml
@@ -0,0 +1,9 @@
+
+
+
diff --git a/app/src/main/res/values/string-not_translatable.xml b/app/src/main/res/values/string-not_translatable.xml
index 9ab683c62a..3a1fcf9bee 100644
--- a/app/src/main/res/values/string-not_translatable.xml
+++ b/app/src/main/res/values/string-not_translatable.xml
@@ -196,6 +196,7 @@ work. If not, see .
tmaid_markdown_bold
tmaid_markdown_italic
tmaid_color_picker
+ tmaid_checkbox
tmaid_markdown_strikeout
tmaid_markdown_code_inline
tmaid_markdown_horizontal_line