diff --git a/app/src/main/java/com/keylesspalace/tusky/components/compose/ComposeActivity.kt b/app/src/main/java/com/keylesspalace/tusky/components/compose/ComposeActivity.kt index 492731d3..78ed98da 100644 --- a/app/src/main/java/com/keylesspalace/tusky/components/compose/ComposeActivity.kt +++ b/app/src/main/java/com/keylesspalace/tusky/components/compose/ComposeActivity.kt @@ -524,6 +524,8 @@ class ComposeActivity : BaseActivity(), @ColorInt val color = ThemeUtils.getColor(this, if(enable) R.attr.colorPrimary else android.R.attr.textColorTertiary); composeFormattingSyntax.drawable.colorFilter = PorterDuffColorFilter(color, PorterDuff.Mode.SRC_IN); + + enableMarkdownWYSIWYGButtons(enable); } private fun setIconForSyntax(syntax: String, enable: Boolean) { @@ -644,23 +646,43 @@ class ComposeActivity : BaseActivity(), } private fun codeButtonClicked() { - MarkdownEdit.addCode(composeEditField); + when(viewModel.formattingSyntax) { + "text/markdown" -> MarkdownEdit.addCode(composeEditField) + "text/bbcode" -> BBCodeEdit.addCode(composeEditField) + "text/html" -> HTMLEdit.addCode(composeEditField) + } } private fun linkButtonClicked() { - MarkdownEdit.addLink(composeEditField); + when(viewModel.formattingSyntax) { + "text/markdown" -> MarkdownEdit.addLink(composeEditField) + "text/bbcode" -> BBCodeEdit.addLink(composeEditField) + "text/html" -> HTMLEdit.addLink(composeEditField) + } } private fun strikethroughButtonClicked() { - MarkdownEdit.addStrikeThrough(composeEditField); + when(viewModel.formattingSyntax) { + "text/markdown" -> MarkdownEdit.addStrikeThrough(composeEditField) + "text/bbcode" -> BBCodeEdit.addStrikeThrough(composeEditField) + "text/html" -> HTMLEdit.addStrikeThrough(composeEditField) + } } private fun italicButtonClicked() { - MarkdownEdit.addItalic(composeEditField); + when(viewModel.formattingSyntax) { + "text/markdown" -> MarkdownEdit.addItalic(composeEditField) + "text/bbcode" -> BBCodeEdit.addItalic(composeEditField) + "text/html" -> HTMLEdit.addItalic(composeEditField) + } } private fun boldButtonClicked() { - MarkdownEdit.addBold(composeEditField); + when(viewModel.formattingSyntax) { + "text/markdown" -> MarkdownEdit.addBold(composeEditField) + "text/bbcode" -> BBCodeEdit.addBold(composeEditField) + "text/html" -> HTMLEdit.addBold(composeEditField) + } } override fun onSaveInstanceState(outState: Bundle) { diff --git a/app/src/main/java/com/keylesspalace/tusky/util/BBCodeEdit.java b/app/src/main/java/com/keylesspalace/tusky/util/BBCodeEdit.java new file mode 100644 index 00000000..f67e3ad4 --- /dev/null +++ b/app/src/main/java/com/keylesspalace/tusky/util/BBCodeEdit.java @@ -0,0 +1,83 @@ +package com.keylesspalace.tusky.util; + +import androidx.annotation.IntDef; +import androidx.annotation.IntRange; +import androidx.annotation.NonNull; +import androidx.annotation.VisibleForTesting; +import android.text.Editable; +import android.text.Selection; +import android.text.Spannable; +import android.widget.EditText; +import me.thanel.markdownedit.SelectionUtils; + +public class BBCodeEdit { + private BBCodeEdit() { /* cannot be instantiated */ } + + public static void addBold(@NonNull Editable text) { + HTMLEdit.surroundSelectionWith(text, "[b]", "[/b]"); + } + + public static void addBold(@NonNull EditText editText) { + addBold(editText.getText()); + } + + public static void addItalic(@NonNull Editable text) { + HTMLEdit.surroundSelectionWith(text, "[i]", "[/i]"); + } + + public static void addItalic(@NonNull EditText editText) { + addItalic(editText.getText()); + } + + public static void addStrikeThrough(@NonNull Editable text) { + HTMLEdit.surroundSelectionWith(text, "[s]", "[/s]"); + } + + public static void addStrikeThrough(@NonNull EditText editText) { + addStrikeThrough(editText.getText()); + } + + public static void addLink(@NonNull Editable text) { + if (!SelectionUtils.hasSelection(text)) { + SelectionUtils.selectWordAroundCursor(text); + } + String selectedText = SelectionUtils.getSelectedText(text).toString().trim(); + + int selectionStart = SelectionUtils.getSelectionStart(text); + + String begin = "[url=url]"; + String end = "[/url]"; + String result = begin + selectedText + end; + SelectionUtils.replaceSelectedText(text, result); + + if (selectedText.length() == 0) { + Selection.setSelection(text, selectionStart + begin.length()); + } else { + selectionStart = selectionStart + 5; // [url=".length() + Selection.setSelection(text, selectionStart, selectionStart + 3); + } + } + + public static void addLink(@NonNull EditText editText) { + addLink(editText.getText()); + } + + /** + * Inserts a markdown code block to the specified EditText at the currently selected position. + * + * @param text The {@link Editable} view to which to add markdown code block. + */ + public static void addCode(@NonNull Editable text) { + HTMLEdit.surroundSelectionWith(text, "[code]", "[/code]"); + } + + /** + * Inserts a markdown code block to the specified EditText at the currently selected position. + * + * @param editText The {@link EditText} view to which to add markdown code block. + */ + public static void addCode(@NonNull EditText editText) { + addCode(editText.getText()); + } +} + diff --git a/app/src/main/java/com/keylesspalace/tusky/util/HTMLEdit.java b/app/src/main/java/com/keylesspalace/tusky/util/HTMLEdit.java new file mode 100644 index 00000000..bb344253 --- /dev/null +++ b/app/src/main/java/com/keylesspalace/tusky/util/HTMLEdit.java @@ -0,0 +1,104 @@ +package com.keylesspalace.tusky.util; + +import androidx.annotation.IntDef; +import androidx.annotation.IntRange; +import androidx.annotation.NonNull; +import androidx.annotation.VisibleForTesting; +import android.text.Editable; +import android.text.Selection; +import android.text.Spannable; +import android.widget.EditText; +import me.thanel.markdownedit.SelectionUtils; + +public class HTMLEdit { + private HTMLEdit() { /* cannot be instantiated */ } + + public static void addBold(@NonNull Editable text) { + surroundSelectionWith(text, "", ""); + } + + public static void addBold(@NonNull EditText editText) { + addBold(editText.getText()); + } + + public static void addItalic(@NonNull Editable text) { + surroundSelectionWith(text, "", ""); + } + + public static void addItalic(@NonNull EditText editText) { + addItalic(editText.getText()); + } + + public static void addStrikeThrough(@NonNull Editable text) { + surroundSelectionWith(text, "", ""); + } + + public static void addStrikeThrough(@NonNull EditText editText) { + addStrikeThrough(editText.getText()); + } + + public static void addLink(@NonNull Editable text) { + if (!SelectionUtils.hasSelection(text)) { + SelectionUtils.selectWordAroundCursor(text); + } + String selectedText = SelectionUtils.getSelectedText(text).toString().trim(); + + int selectionStart = SelectionUtils.getSelectionStart(text); + + String begin = ""; + String end = ""; + String result = begin + selectedText + end; + SelectionUtils.replaceSelectedText(text, result); + + if (selectedText.length() == 0) { + Selection.setSelection(text, selectionStart + begin.length()); + } else { + selectionStart = selectionStart + 9; // ", ""); + } + + /** + * Inserts a markdown code block to the specified EditText at the currently selected position. + * + * @param editText The {@link EditText} view to which to add markdown code block. + */ + public static void addCode(@NonNull EditText editText) { + addCode(editText.getText()); + } + + public static void surroundSelectionWith(@NonNull Editable text, @NonNull String surroundText, @NonNull String surroundText2) { + if (!SelectionUtils.hasSelection(text)) { + SelectionUtils.selectWordAroundCursor(text); + } + CharSequence selectedText = SelectionUtils.getSelectedText(text); + int selectionStart = SelectionUtils.getSelectionStart(text); + + selectedText = selectedText.toString().trim(); + + StringBuilder result = new StringBuilder(); + result.append(surroundText).append(selectedText).append(surroundText2); + + int charactersToGoBack = 0; + if (selectedText.length() == 0) { + charactersToGoBack = surroundText2.length(); + } + + SelectionUtils.replaceSelectedText(text, result); + Selection.setSelection(text, selectionStart + result.length() - charactersToGoBack); + } + +}