diff --git a/app/src/main/java/gr/thmmy/mthmmy/editorview/EditorView.java b/app/src/main/java/gr/thmmy/mthmmy/editorview/EditorView.java index 0b7d8f9f..1263a1c6 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/editorview/EditorView.java +++ b/app/src/main/java/gr/thmmy/mthmmy/editorview/EditorView.java @@ -94,28 +94,28 @@ public class EditorView extends LinearLayout { submitButton = findViewById(R.id.submit_button); findViewById(R.id.bold_button).setOnClickListener(view -> { - if (editText.hasSelection()) - editText.getText().delete(editText.getSelectionStart(), editText.getSelectionEnd()); - getText().insert(editText.getSelectionStart(), "[b][/b]"); - editText.setSelection(editText.getSelectionStart() - 4); + boolean hadTextSelection = editText.hasSelection(); + getText().insert(editText.getSelectionStart(), "[b]"); + getText().insert(editText.getSelectionEnd(), "[/b]"); + editText.setSelection(hadTextSelection ? editText.getSelectionEnd() : editText.getSelectionStart() - 4); }); findViewById(R.id.italic_button).setOnClickListener(view -> { - if (editText.hasSelection()) - editText.getText().delete(editText.getSelectionStart(), editText.getSelectionEnd()); - getText().insert(editText.getSelectionStart(), "[i][/i]"); - editText.setSelection(editText.getSelectionStart() - 4); + boolean hadTextSelection = editText.hasSelection(); + getText().insert(editText.getSelectionStart(), "[i]"); + getText().insert(editText.getSelectionEnd(), "[/i]"); + editText.setSelection(hadTextSelection ? editText.getSelectionEnd() : editText.getSelectionStart() - 4); }); findViewById(R.id.underline_button).setOnClickListener(view -> { - if (editText.hasSelection()) - editText.getText().delete(editText.getSelectionStart(), editText.getSelectionEnd()); - getText().insert(editText.getSelectionStart(), "[u][/u]"); - editText.setSelection(editText.getSelectionStart() - 4); + boolean hadTextSelection = editText.hasSelection(); + getText().insert(editText.getSelectionStart(), "[u]"); + getText().insert(editText.getSelectionEnd(), "[/u]"); + editText.setSelection(hadTextSelection ? editText.getSelectionEnd() : editText.getSelectionStart() - 4); }); findViewById(R.id.strikethrough_button).setOnClickListener(view -> { - if (editText.hasSelection()) - editText.getText().delete(editText.getSelectionStart(), editText.getSelectionEnd()); - getText().insert(editText.getSelectionStart(), "[s][/s]"); - editText.setSelection(editText.getSelectionStart() - 4); + boolean hadTextSelection = editText.hasSelection(); + getText().insert(editText.getSelectionStart(), "[s]"); + getText().insert(editText.getSelectionEnd(), "[/s]"); + editText.setSelection(hadTextSelection ? editText.getSelectionEnd() : editText.getSelectionStart() - 4); }); colors.append(R.id.black, "black"); @@ -143,50 +143,50 @@ public class EditorView extends LinearLayout { popupWindow.setContentView(colorPickerScrollview); for (int i = 0; i < colorPicker.getChildCount(); i++) { colorPicker.getChildAt(i).setOnClickListener(v -> { - if (editText.hasSelection()) - editText.getText().delete(editText.getSelectionStart(), editText.getSelectionEnd()); - getText().insert(editText.getSelectionStart(), "[color=" + colors.get(v.getId()) + "][/color]"); - editText.setSelection(editText.getSelectionStart() - 8); + boolean hadTextSelection = editText.hasSelection(); + getText().insert(editText.getSelectionStart(), "[color=" + colors.get(v.getId()) + "]"); + getText().insert(editText.getSelectionEnd(), "[/color]"); + editText.setSelection(hadTextSelection ? editText.getSelectionEnd() : editText.getSelectionStart() - 8); popupWindow.dismiss(); }); } popupWindow.showAsDropDown(view); }); findViewById(R.id.text_size_button).setOnClickListener(view -> { - if (editText.hasSelection()) - editText.getText().delete(editText.getSelectionStart(), editText.getSelectionEnd()); - getText().insert(editText.getSelectionStart(), "[size=10pt][/size]"); - editText.setSelection(editText.getSelectionStart() - 7); + boolean hadTextSelection = editText.hasSelection(); + getText().insert(editText.getSelectionStart(), "[size=10pt]"); + getText().insert(editText.getSelectionEnd(), "[/size]"); + editText.setSelection(hadTextSelection ? editText.getSelectionEnd() : editText.getSelectionStart() - 7); }); findViewById(R.id.font_button).setOnClickListener(view -> { - if (editText.hasSelection()) - editText.getText().delete(editText.getSelectionStart(), editText.getSelectionEnd()); - getText().insert(editText.getSelectionStart(), "[font=Verdana][/font]"); - editText.setSelection(editText.getSelectionStart() - 7); + boolean hadTextSelection = editText.hasSelection(); + getText().insert(editText.getSelectionStart(), "[font=Verdana]"); + getText().insert(editText.getSelectionEnd(), "[/font]"); + editText.setSelection(hadTextSelection ? editText.getSelectionEnd() : editText.getSelectionStart() - 7); }); findViewById(R.id.unordered_list_button).setOnClickListener(view -> { - if (editText.hasSelection()) - editText.getText().delete(editText.getSelectionStart(), editText.getSelectionEnd()); - getText().insert(editText.getSelectionStart(), "[list]\n[li][/li]\n[li][/li]\n[/list]"); - editText.setSelection(editText.getSelectionStart() - 23); + boolean hadTextSelection = editText.hasSelection(); + getText().insert(editText.getSelectionStart(), "[list]\n[li]"); + getText().insert(editText.getSelectionEnd(), "[/li]\n[li][/li]\n[/list]"); + editText.setSelection(hadTextSelection ? editText.getSelectionEnd() - 13 : editText.getSelectionStart() - 23); }); findViewById(R.id.align_left_button).setOnClickListener(view -> { - if (editText.hasSelection()) - editText.getText().delete(editText.getSelectionStart(), editText.getSelectionEnd()); - getText().insert(editText.getSelectionStart(), "[left][/left]"); - editText.setSelection(editText.getSelectionStart() - 7); + boolean hadTextSelection = editText.hasSelection(); + getText().insert(editText.getSelectionStart(), "[left]"); + getText().insert(editText.getSelectionEnd(), "[/left]"); + editText.setSelection(hadTextSelection ? editText.getSelectionEnd() : editText.getSelectionStart() - 7); }); findViewById(R.id.align_center_button).setOnClickListener(view -> { - if (editText.hasSelection()) - editText.getText().delete(editText.getSelectionStart(), editText.getSelectionEnd()); - getText().insert(editText.getSelectionStart(), "[center][/center]"); - editText.setSelection(editText.getSelectionStart() - 9); + boolean hadTextSelection = editText.hasSelection(); + getText().insert(editText.getSelectionStart(), "[center]"); + getText().insert(editText.getSelectionEnd(), "[/center]"); + editText.setSelection(hadTextSelection ? editText.getSelectionEnd() : editText.getSelectionStart() - 9); }); findViewById(R.id.align_right_button).setOnClickListener(view -> { - if (editText.hasSelection()) - editText.getText().delete(editText.getSelectionStart(), editText.getSelectionEnd()); - getText().insert(editText.getSelectionStart(), "[right][/right]"); - editText.setSelection(editText.getSelectionStart() - 8); + boolean hadTextSelection = editText.hasSelection(); + getText().insert(editText.getSelectionStart(), "[right]"); + getText().insert(editText.getSelectionEnd(), "[/right]"); + editText.setSelection(hadTextSelection ? editText.getSelectionEnd() : editText.getSelectionStart() - 8); }); findViewById(R.id.link_button).setOnClickListener(view -> { LinearLayout dialogBody = (LinearLayout) LayoutInflater.from(context) @@ -195,6 +195,12 @@ public class EditorView extends LinearLayout { linkUrl.setOnClickListener(view1 -> linkUrl.setError(null)); TextInputLayout linkText = dialogBody.findViewById(R.id.link_text_input); linkText.setOnClickListener(view2 -> linkText.setError(null)); + boolean hadTextSelection = editText.hasSelection(); + int start = editText.getSelectionStart(), end = editText.getSelectionEnd(); + if (editText.hasSelection()) { + linkText.getEditText().setText( + editText.getText().toString().substring(editText.getSelectionStart(), editText.getSelectionEnd())); + } new AlertDialog.Builder(context, R.style.AppTheme_Dark_Dialog) .setTitle(R.string.dialog_create_link_title) .setView(dialogBody) @@ -208,8 +214,7 @@ public class EditorView extends LinearLayout { return; } - if (editText.hasSelection()) - editText.getText().delete(editText.getSelectionStart(), editText.getSelectionEnd()); + if (hadTextSelection) editText.getText().delete(start, end); getText().insert(editText.getSelectionStart(), "[url=" + Objects.requireNonNull(linkUrl.getEditText()).getText().toString() + "]" + Objects.requireNonNull(linkText.getEditText()).getText().toString() + "[/url]"); @@ -218,22 +223,22 @@ public class EditorView extends LinearLayout { .show(); }); findViewById(R.id.quote_button).setOnClickListener(view -> { - if (editText.hasSelection()) - editText.getText().delete(editText.getSelectionStart(), editText.getSelectionEnd()); - getText().insert(editText.getSelectionStart(), "[quote][/quote]"); - editText.setSelection(editText.getSelectionStart() - 8); + boolean hadTextSelection = editText.hasSelection(); + getText().insert(editText.getSelectionStart(), "[quote]"); + getText().insert(editText.getSelectionEnd(), "[/quote]"); + editText.setSelection(hadTextSelection ? editText.getSelectionEnd() : editText.getSelectionStart() - 8); }); findViewById(R.id.code_button).setOnClickListener(view -> { - if (editText.hasSelection()) - editText.getText().delete(editText.getSelectionStart(), editText.getSelectionEnd()); - getText().insert(editText.getSelectionStart(), "[code][/code]"); - editText.setSelection(editText.getSelectionStart() - 7); + boolean hadTextSelection = editText.hasSelection(); + getText().insert(editText.getSelectionStart(), "[code]"); + getText().insert(editText.getSelectionEnd(), "[/code]"); + editText.setSelection(hadTextSelection ? editText.getSelectionEnd() : editText.getSelectionStart() - 7); }); findViewById(R.id.math_button).setOnClickListener(view -> { - if (editText.hasSelection()) - editText.getText().delete(editText.getSelectionStart(), editText.getSelectionEnd()); - getText().insert(editText.getSelectionStart(), "[tex][/tex]"); - editText.setSelection(editText.getSelectionStart() - 6); + boolean hadTextSelection = editText.hasSelection(); + getText().insert(editText.getSelectionStart(), "[tex]"); + getText().insert(editText.getSelectionEnd(), "[/tex]"); + editText.setSelection(hadTextSelection ? editText.getSelectionEnd() : editText.getSelectionStart() - 6); }); }