From 72e16d188f988654672df4d83bce2d75816e12e2 Mon Sep 17 00:00:00 2001 From: digger Date: Thu, 1 Nov 2018 16:43:53 +0100 Subject: [PATCH 1/7] initial implementation of multiline action --- .../markdown/MarkdownTextModuleActions.java | 90 ++++++++++++------- 1 file changed, 56 insertions(+), 34 deletions(-) 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 8b1d237efd..757b2a7192 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 @@ -180,49 +180,71 @@ public boolean onLongClick(View v) { return false; } - private void runMarkdownRegularPrefixAction(String action) { - if (_hlEditor.hasSelection()) { - String text = _hlEditor.getText().toString(); - int selectionStart = _hlEditor.getSelectionStart(); - int selectionEnd = _hlEditor.getSelectionEnd(); - - //Check if Selection includes the shortcut characters - if (text.substring(selectionStart, selectionEnd) - .matches("(>|#{1,3}|-|[1-9]\\.)(\\s)?[a-zA-Z0-9\\s]*")) { + private void insertText(int startIndex, String text) + { + Editable s = _hlEditor.getText(); + s.insert(startIndex, text); + } - text = text.substring(selectionStart + action.length(), selectionEnd); - _hlEditor.getText() - .replace(selectionStart, selectionEnd, text); + private void removeText(int startIndex, String text) + { + Editable s = _hlEditor.getText(); + s.delete(startIndex, startIndex+text.length()); + } + private int findLineStart(int cursor, String text) + { + int i = cursor - 1; + for (; i >= 0; i--) { + if (text.charAt(i) == '\n') { + break; } - //Check if Selection is Preceded by shortcut characters - else if ((selectionStart >= action.length()) && (text.substring(selectionStart - action.length(), selectionEnd) - .matches("(>|#{1,3}|-|[1-9]\\.)(\\s)?[a-zA-Z0-9\\s]*"))) { + } - text = text.substring(selectionStart, selectionEnd); - _hlEditor.getText() - .replace(selectionStart - action.length(), selectionEnd, text); + return i+1; + } + private int findNextLine(int startIndex, int endIndex, String text) + { + int index = -1; + for(int i = startIndex; i < endIndex; i++) + { + if(text.charAt(i) == '\n') + { + index = i + 1; + break; } - //Condition to insert shortcut preceding the selection - else { - _hlEditor.getText().insert(selectionStart, action); - } - } else { - //Condition for Empty Selection. Should insert the action at the start of the line - int cursor = _hlEditor.getSelectionStart(); - int i = cursor - 1; - Editable s = _hlEditor.getText(); - for (; i >= 0; i--) { - if (s.charAt(i) == '\n') { - break; - } - } - - s.insert(i + 1, action); } + + return index; } + private void runMarkdownRegularPrefixAction(String action) + { + int selectionStart = _hlEditor.getSelectionStart(); + int selectionEnd = _hlEditor.getSelectionEnd(); + String text = _hlEditor.getText().toString(); + + int lineStart = findLineStart(selectionStart, text); + + while(lineStart != -1) + { + if(text.substring(lineStart, selectionEnd).startsWith(action)) + { + removeText(lineStart, action); + selectionEnd -= action.length(); + text = _hlEditor.getText().toString(); + } + else { + insertText(lineStart, action); + selectionEnd += action.length(); + text = _hlEditor.getText().toString(); + + } + + lineStart = findNextLine(lineStart, selectionEnd, text); + } + } private void runMarkdownInlineAction(String _action) { if (_hlEditor.hasSelection()) { From 94e5f403b2ef999b628c776f46509f994d070490 Mon Sep 17 00:00:00 2001 From: digger Date: Thu, 1 Nov 2018 17:15:01 +0100 Subject: [PATCH 2/7] create own class for text selection to couple insertion and deletion of text and selection indexes --- .../markdown/MarkdownTextModuleActions.java | 62 +++++++++++++------ 1 file changed, 42 insertions(+), 20 deletions(-) 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 757b2a7192..83b72f2ff2 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 @@ -180,16 +180,41 @@ public boolean onLongClick(View v) { return false; } - private void insertText(int startIndex, String text) - { - Editable s = _hlEditor.getText(); - s.insert(startIndex, text); - } + class TextSelection { - private void removeText(int startIndex, String text) - { - Editable s = _hlEditor.getText(); - s.delete(startIndex, startIndex+text.length()); + private int selectionStart; + private int selectionEnd; + private Editable editable; + + + TextSelection(int start, int end, Editable editable) + { + this.selectionStart = start; + this.selectionEnd = end; + this.editable = editable; + } + + void insertText(int location, String text) + { + editable.insert(location, text); + this.selectionEnd += text.length(); + } + + void removeText(int location, String text) + { + editable.delete(location, location + text.length()); + this.selectionEnd -= text.length(); + } + + int getSelectionStart() + { + return selectionStart; + } + + int getSelectionEnd() + { + return selectionEnd; + } } private int findLineStart(int cursor, String text) @@ -221,28 +246,25 @@ private int findNextLine(int startIndex, int endIndex, String text) private void runMarkdownRegularPrefixAction(String action) { - int selectionStart = _hlEditor.getSelectionStart(); - int selectionEnd = _hlEditor.getSelectionEnd(); String text = _hlEditor.getText().toString(); + TextSelection textSelection = new TextSelection(_hlEditor.getSelectionStart(), _hlEditor.getSelectionEnd(), _hlEditor.getText()); - int lineStart = findLineStart(selectionStart, text); + int lineStart = findLineStart(textSelection.getSelectionStart(), text); while(lineStart != -1) { - if(text.substring(lineStart, selectionEnd).startsWith(action)) + if(text.substring(lineStart, textSelection.getSelectionEnd()).startsWith(action)) { - removeText(lineStart, action); - selectionEnd -= action.length(); - text = _hlEditor.getText().toString(); + textSelection.removeText(lineStart, action); } else { - insertText(lineStart, action); - selectionEnd += action.length(); - text = _hlEditor.getText().toString(); + textSelection.insertText(lineStart, action); } - lineStart = findNextLine(lineStart, selectionEnd, text); + text = _hlEditor.getText().toString(); + + lineStart = findNextLine(lineStart, textSelection.getSelectionEnd(), text); } } From f277bc7510d91ef22e2b4fe54324f8fd4824cd54 Mon Sep 17 00:00:00 2001 From: digger Date: Fri, 2 Nov 2018 19:01:27 +0100 Subject: [PATCH 3/7] added new button to insert checkbox --- .../format/markdown/MarkdownTextModuleActions.java | 5 +++++ app/src/main/res/drawable/checkbox.xml | 9 +++++++++ app/src/main/res/values/string-not_translatable.xml | 1 + 3 files changed, 15 insertions(+) create mode 100644 app/src/main/res/drawable/checkbox.xml 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 83b72f2ff2..132c26a03b 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.checkbox}, }; 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("- [ ] "); + break; + } case R.string.tmaid_markdown_bold: { runMarkdownInlineAction("**"); break; diff --git a/app/src/main/res/drawable/checkbox.xml b/app/src/main/res/drawable/checkbox.xml new file mode 100644 index 0000000000..cf8bfa24b5 --- /dev/null +++ b/app/src/main/res/drawable/checkbox.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 From 992bc8cbc7893301a5c9c03fbb694ff8692ffc40 Mon Sep 17 00:00:00 2001 From: digger Date: Fri, 2 Nov 2018 19:17:22 +0100 Subject: [PATCH 4/7] reformatted code --- .../markdown/MarkdownTextModuleActions.java | 81 ++++++++----------- 1 file changed, 34 insertions(+), 47 deletions(-) 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 83b72f2ff2..59a564c1f1 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 @@ -147,8 +147,8 @@ public void onClick(View v) { new CommonTextModuleActions(_activity, _document, _hlEditor).runAction(CommonTextModuleActions.ACTION_SPECIAL_KEY); break; } - case R.string.tmaid_color_picker:{ - new CommonTextModuleActions(_activity,_document,_hlEditor).runAction(CommonTextModuleActions.ACTION_COLOR_PICKER); + case R.string.tmaid_color_picker: { + new CommonTextModuleActions(_activity, _document, _hlEditor).runAction(CommonTextModuleActions.ACTION_COLOR_PICKER); break; } case R.string.tmaid_markdown_insert_link: @@ -182,43 +182,37 @@ public boolean onLongClick(View v) { class TextSelection { - private int selectionStart; - private int selectionEnd; - private Editable editable; + private int _selectionStart; + private int _selectionEnd; + private Editable _editable; - TextSelection(int start, int end, Editable editable) - { - this.selectionStart = start; - this.selectionEnd = end; - this.editable = editable; + TextSelection(int start, int end, Editable editable) { + _selectionStart = start; + _selectionEnd = end; + _editable = editable; } - void insertText(int location, String text) - { - editable.insert(location, text); - this.selectionEnd += text.length(); + void insertText(int location, String text) { + _editable.insert(location, text); + _selectionEnd += text.length(); } - void removeText(int location, String text) - { - editable.delete(location, location + text.length()); - this.selectionEnd -= text.length(); + void removeText(int location, String text) { + _editable.delete(location, location + text.length()); + _selectionEnd -= text.length(); } - int getSelectionStart() - { - return selectionStart; + int getSelectionStart() { + return _selectionStart; } - int getSelectionEnd() - { - return selectionEnd; + int getSelectionEnd() { + return _selectionEnd; } } - private int findLineStart(int cursor, String text) - { + private int findLineStart(int cursor, String text) { int i = cursor - 1; for (; i >= 0; i--) { if (text.charAt(i) == '\n') { @@ -226,16 +220,13 @@ private int findLineStart(int cursor, String text) } } - return i+1; + return i + 1; } - private int findNextLine(int startIndex, int endIndex, String text) - { + private int findNextLine(int startIndex, int endIndex, String text) { int index = -1; - for(int i = startIndex; i < endIndex; i++) - { - if(text.charAt(i) == '\n') - { + for (int i = startIndex; i < endIndex; i++) { + if (text.charAt(i) == '\n') { index = i + 1; break; } @@ -244,28 +235,24 @@ private int findNextLine(int startIndex, int endIndex, String text) return index; } - private void runMarkdownRegularPrefixAction(String action) - { + private void runMarkdownRegularPrefixAction(String action) { 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); - } - else { - textSelection.insertText(lineStart, action); + while (lineStart != -1) { + if (text.substring(lineStart, textSelection.getSelectionEnd()).startsWith(action)) { + textSelection.removeText(lineStart, action); + } else { + textSelection.insertText(lineStart, action); - } + } - text = _hlEditor.getText().toString(); + text = _hlEditor.getText().toString(); - lineStart = findNextLine(lineStart, textSelection.getSelectionEnd(), text); - } + lineStart = findNextLine(lineStart, textSelection.getSelectionEnd(), text); + } } private void runMarkdownInlineAction(String _action) { From f231febc5bee32651cb35ec352ba48eb4b94ca38 Mon Sep 17 00:00:00 2001 From: digger Date: Tue, 13 Nov 2018 21:29:27 +0100 Subject: [PATCH 5/7] added support for checking of check box --- .../markdown/MarkdownTextModuleActions.java | 25 +++++++++++++++---- 1 file changed, 20 insertions(+), 5 deletions(-) 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 8eb540b0b6..80974cf3b7 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 @@ -117,7 +117,7 @@ public void onClick(View v) { break; } case R.string.tmaid_checkbox: { - runMarkdownRegularPrefixAction("- [ ] "); + runMarkdownRegularPrefixAction("- [ ] ", "- [x] "); break; } case R.string.tmaid_markdown_bold: { @@ -241,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(); From e0d24c1fd314567948714712bc992ddb6dbf8a7b Mon Sep 17 00:00:00 2001 From: digger Date: Tue, 13 Nov 2018 21:33:24 +0100 Subject: [PATCH 6/7] added support for auto insert for checkbox --- .../net/gsantner/markor/format/markdown/MarkdownAutoFormat.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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()) { From 9a4ee987ae1afa76f4635a1af975e89d9a102644 Mon Sep 17 00:00:00 2001 From: digger Date: Tue, 13 Nov 2018 21:44:57 +0100 Subject: [PATCH 7/7] exchanged checkbox icon --- .../markor/format/markdown/MarkdownTextModuleActions.java | 2 +- .../res/drawable/{checkbox.xml => ic_check_box_black_24dp.xml} | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) rename app/src/main/res/drawable/{checkbox.xml => ic_check_box_black_24dp.xml} (58%) 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 80974cf3b7..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,7 +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.checkbox}, + {R.string.tmaid_checkbox, R.drawable.ic_check_box_black_24dp}, }; private class MarkdownTextModuleActionsImpl implements View.OnClickListener, View.OnLongClickListener { diff --git a/app/src/main/res/drawable/checkbox.xml b/app/src/main/res/drawable/ic_check_box_black_24dp.xml similarity index 58% rename from app/src/main/res/drawable/checkbox.xml rename to app/src/main/res/drawable/ic_check_box_black_24dp.xml index cf8bfa24b5..9948171c21 100644 --- a/app/src/main/res/drawable/checkbox.xml +++ b/app/src/main/res/drawable/ic_check_box_black_24dp.xml @@ -5,5 +5,5 @@ android:viewportHeight="24.0"> + 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"/>