Browse Source

improvements in editor view

pull/45/head
Thodoris1999 6 years ago
parent
commit
6aee7529b6
  1. 121
      app/src/main/java/gr/thmmy/mthmmy/editorview/EditorView.java

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

Loading…
Cancel
Save