|
@ -8,6 +8,7 @@ import android.content.Context; |
|
|
import android.content.res.TypedArray; |
|
|
import android.content.res.TypedArray; |
|
|
import android.graphics.drawable.Drawable; |
|
|
import android.graphics.drawable.Drawable; |
|
|
import android.os.AsyncTask; |
|
|
import android.os.AsyncTask; |
|
|
|
|
|
import android.os.Build; |
|
|
import android.text.Editable; |
|
|
import android.text.Editable; |
|
|
import android.text.TextUtils; |
|
|
import android.text.TextUtils; |
|
|
import android.util.AttributeSet; |
|
|
import android.util.AttributeSet; |
|
@ -126,58 +127,79 @@ public class EditorView extends LinearLayout implements EmojiInputField { |
|
|
getResources().getDimension(R.dimen.editor_format_button_margin_between); |
|
|
getResources().getDimension(R.dimen.editor_format_button_margin_between); |
|
|
int columns = (int) Math.floor(displayMetrics.widthPixels / itemWidth); |
|
|
int columns = (int) Math.floor(displayMetrics.widthPixels / itemWidth); |
|
|
formatButtonsRecyclerview.setLayoutManager(new GridLayoutManager(context, columns)); |
|
|
formatButtonsRecyclerview.setLayoutManager(new GridLayoutManager(context, columns)); |
|
|
formatButtonsRecyclerview.setAdapter(new FormatButtonsAdapter((view, drawableId) -> { |
|
|
formatButtonsRecyclerview.setAdapter( |
|
|
|
|
|
new FormatButtonsAdapter( |
|
|
|
|
|
(view, drawableId) -> { |
|
|
|
|
|
boolean hadTextSelection; |
|
|
switch (drawableId) { |
|
|
switch (drawableId) { |
|
|
case R.drawable.ic_format_bold: { |
|
|
case R.drawable.ic_format_bold: |
|
|
boolean hadTextSelection = editText.hasSelection(); |
|
|
hadTextSelection = editText.hasSelection(); |
|
|
getText().insert(editText.getSelectionStart(), "[b]"); |
|
|
getText().insert(editText.getSelectionStart(), "[b]"); |
|
|
getText().insert(editText.getSelectionEnd(), "[/b]"); |
|
|
getText().insert(editText.getSelectionEnd(), "[/b]"); |
|
|
editText.setSelection(hadTextSelection ? editText.getSelectionEnd() : editText.getSelectionStart() - 4); |
|
|
editText.setSelection( |
|
|
|
|
|
hadTextSelection |
|
|
|
|
|
? editText.getSelectionEnd() |
|
|
|
|
|
: editText.getSelectionStart() - 4); |
|
|
break; |
|
|
break; |
|
|
} |
|
|
case R.drawable.ic_format_italic: |
|
|
case R.drawable.ic_format_italic: { |
|
|
hadTextSelection = editText.hasSelection(); |
|
|
boolean hadTextSelection = editText.hasSelection(); |
|
|
|
|
|
getText().insert(editText.getSelectionStart(), "[i]"); |
|
|
getText().insert(editText.getSelectionStart(), "[i]"); |
|
|
getText().insert(editText.getSelectionEnd(), "[/i]"); |
|
|
getText().insert(editText.getSelectionEnd(), "[/i]"); |
|
|
editText.setSelection(hadTextSelection ? editText.getSelectionEnd() : editText.getSelectionStart() - 4); |
|
|
editText.setSelection( |
|
|
|
|
|
hadTextSelection |
|
|
|
|
|
? editText.getSelectionEnd() |
|
|
|
|
|
: editText.getSelectionStart() - 4); |
|
|
break; |
|
|
break; |
|
|
} |
|
|
case R.drawable.ic_format_underlined: |
|
|
case R.drawable.ic_format_underlined: { |
|
|
hadTextSelection = editText.hasSelection(); |
|
|
boolean hadTextSelection = editText.hasSelection(); |
|
|
|
|
|
getText().insert(editText.getSelectionStart(), "[u]"); |
|
|
getText().insert(editText.getSelectionStart(), "[u]"); |
|
|
getText().insert(editText.getSelectionEnd(), "[/u]"); |
|
|
getText().insert(editText.getSelectionEnd(), "[/u]"); |
|
|
editText.setSelection(hadTextSelection ? editText.getSelectionEnd() : editText.getSelectionStart() - 4); |
|
|
editText.setSelection( |
|
|
|
|
|
hadTextSelection |
|
|
|
|
|
? editText.getSelectionEnd() |
|
|
|
|
|
: editText.getSelectionStart() - 4); |
|
|
break; |
|
|
break; |
|
|
} |
|
|
case R.drawable.ic_strikethrough_s: |
|
|
case R.drawable.ic_strikethrough_s: { |
|
|
hadTextSelection = editText.hasSelection(); |
|
|
boolean hadTextSelection = editText.hasSelection(); |
|
|
|
|
|
getText().insert(editText.getSelectionStart(), "[s]"); |
|
|
getText().insert(editText.getSelectionStart(), "[s]"); |
|
|
getText().insert(editText.getSelectionEnd(), "[/s]"); |
|
|
getText().insert(editText.getSelectionEnd(), "[/s]"); |
|
|
editText.setSelection(hadTextSelection ? editText.getSelectionEnd() : editText.getSelectionStart() - 4); |
|
|
editText.setSelection( |
|
|
|
|
|
hadTextSelection |
|
|
|
|
|
? editText.getSelectionEnd() |
|
|
|
|
|
: editText.getSelectionStart() - 4); |
|
|
break; |
|
|
break; |
|
|
} |
|
|
case R.drawable.ic_format_color_text: |
|
|
case R.drawable.ic_format_color_text: { |
|
|
|
|
|
boolean hadTextSelection = editText.hasSelection(); |
|
|
|
|
|
int selectionStart = editText.getSelectionStart(); |
|
|
int selectionStart = editText.getSelectionStart(); |
|
|
int selectionEnd = editText.getSelectionEnd(); |
|
|
int selectionEnd = editText.getSelectionEnd(); |
|
|
PopupWindow popupWindow = new PopupWindow(view.getContext()); |
|
|
PopupWindow popupWindow = new PopupWindow(view.getContext()); |
|
|
popupWindow.setHeight(LayoutParams.WRAP_CONTENT); |
|
|
popupWindow.setHeight(LayoutParams.WRAP_CONTENT); |
|
|
popupWindow.setWidth(LayoutParams.WRAP_CONTENT); |
|
|
popupWindow.setWidth(LayoutParams.WRAP_CONTENT); |
|
|
popupWindow.setFocusable(true); |
|
|
popupWindow.setFocusable(true); |
|
|
ScrollView colorPickerScrollview = (ScrollView) LayoutInflater.from(context).inflate(R.layout.editor_view_color_picker, null); |
|
|
ScrollView colorPickerScrollview = |
|
|
|
|
|
(ScrollView) |
|
|
|
|
|
LayoutInflater.from(context) |
|
|
|
|
|
.inflate(R.layout.editor_view_color_picker, null); |
|
|
LinearLayout colorPicker = (LinearLayout) colorPickerScrollview.getChildAt(0); |
|
|
LinearLayout colorPicker = (LinearLayout) colorPickerScrollview.getChildAt(0); |
|
|
popupWindow.setContentView(colorPickerScrollview); |
|
|
popupWindow.setContentView(colorPickerScrollview); |
|
|
for (int i = 0; i < colorPicker.getChildCount(); i++) { |
|
|
for (int i = 0; i < colorPicker.getChildCount(); i++) { |
|
|
TextView child = (TextView) colorPicker.getChildAt(i); |
|
|
TextView child = (TextView) colorPicker.getChildAt(i); |
|
|
child.setOnClickListener(v -> { |
|
|
child.setOnClickListener( |
|
|
|
|
|
v -> { |
|
|
boolean hadTextSelection2 = editText.hasSelection(); |
|
|
boolean hadTextSelection2 = editText.hasSelection(); |
|
|
getText().insert(editText.getSelectionStart(), "[color=" + colors.get(v.getId()) + "]"); |
|
|
getText() |
|
|
|
|
|
.insert( |
|
|
|
|
|
editText.getSelectionStart(), |
|
|
|
|
|
"[color=" + colors.get(v.getId()) + "]"); |
|
|
getText().insert(editText.getSelectionEnd(), "[/color]"); |
|
|
getText().insert(editText.getSelectionEnd(), "[/color]"); |
|
|
editText.setSelection(hadTextSelection2 ? editText.getSelectionEnd() : editText.getSelectionStart() - 8); |
|
|
editText.setSelection( |
|
|
|
|
|
hadTextSelection2 |
|
|
|
|
|
? editText.getSelectionEnd() |
|
|
|
|
|
: editText.getSelectionStart() - 8); |
|
|
popupWindow.dismiss(); |
|
|
popupWindow.dismiss(); |
|
|
}); |
|
|
}); |
|
|
} |
|
|
} |
|
|
popupWindow.showAsDropDown(view); |
|
|
popupWindow.showAsDropDown(view); |
|
|
|
|
|
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) { |
|
|
new AsyncTask<Void, Void, Void>() { |
|
|
new AsyncTask<Void, Void, Void>() { |
|
|
@Override |
|
|
@Override |
|
|
protected Void doInBackground(Void... voids) { |
|
|
protected Void doInBackground(Void... voids) { |
|
@ -194,114 +216,153 @@ public class EditorView extends LinearLayout implements EmojiInputField { |
|
|
editText.setSelection(selectionStart, selectionEnd); |
|
|
editText.setSelection(selectionStart, selectionEnd); |
|
|
} |
|
|
} |
|
|
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); |
|
|
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); |
|
|
break; |
|
|
|
|
|
} |
|
|
} |
|
|
case R.drawable.ic_format_size: { |
|
|
break; |
|
|
boolean hadTextSelection = editText.hasSelection(); |
|
|
case R.drawable.ic_format_size: |
|
|
|
|
|
hadTextSelection = editText.hasSelection(); |
|
|
getText().insert(editText.getSelectionStart(), "[size=10pt]"); |
|
|
getText().insert(editText.getSelectionStart(), "[size=10pt]"); |
|
|
getText().insert(editText.getSelectionEnd(), "[/size]"); |
|
|
getText().insert(editText.getSelectionEnd(), "[/size]"); |
|
|
editText.setSelection(hadTextSelection ? editText.getSelectionEnd() : editText.getSelectionStart() - 7); |
|
|
editText.setSelection( |
|
|
|
|
|
hadTextSelection |
|
|
|
|
|
? editText.getSelectionEnd() |
|
|
|
|
|
: editText.getSelectionStart() - 7); |
|
|
break; |
|
|
break; |
|
|
} |
|
|
case R.drawable.ic_text_format: |
|
|
case R.drawable.ic_text_format: { |
|
|
hadTextSelection = editText.hasSelection(); |
|
|
boolean hadTextSelection = editText.hasSelection(); |
|
|
|
|
|
getText().insert(editText.getSelectionStart(), "[font=Verdana]"); |
|
|
getText().insert(editText.getSelectionStart(), "[font=Verdana]"); |
|
|
getText().insert(editText.getSelectionEnd(), "[/font]"); |
|
|
getText().insert(editText.getSelectionEnd(), "[/font]"); |
|
|
editText.setSelection(hadTextSelection ? editText.getSelectionEnd() : editText.getSelectionStart() - 7); |
|
|
editText.setSelection( |
|
|
|
|
|
hadTextSelection |
|
|
|
|
|
? editText.getSelectionEnd() |
|
|
|
|
|
: editText.getSelectionStart() - 7); |
|
|
break; |
|
|
break; |
|
|
} |
|
|
case R.drawable.ic_format_list_bulleted: |
|
|
case R.drawable.ic_format_list_bulleted: { |
|
|
hadTextSelection = editText.hasSelection(); |
|
|
boolean hadTextSelection = editText.hasSelection(); |
|
|
|
|
|
getText().insert(editText.getSelectionStart(), "[list]\n[li]"); |
|
|
getText().insert(editText.getSelectionStart(), "[list]\n[li]"); |
|
|
getText().insert(editText.getSelectionEnd(), "[/li]\n[li][/li]\n[/list]"); |
|
|
getText().insert(editText.getSelectionEnd(), "[/li]\n[li][/li]\n[/list]"); |
|
|
editText.setSelection(hadTextSelection ? editText.getSelectionEnd() - 13 : editText.getSelectionStart() - 23); |
|
|
editText.setSelection( |
|
|
|
|
|
hadTextSelection |
|
|
|
|
|
? editText.getSelectionEnd() - 13 |
|
|
|
|
|
: editText.getSelectionStart() - 23); |
|
|
break; |
|
|
break; |
|
|
} |
|
|
case R.drawable.ic_format_align_left: |
|
|
case R.drawable.ic_format_align_left: { |
|
|
hadTextSelection = editText.hasSelection(); |
|
|
boolean hadTextSelection = editText.hasSelection(); |
|
|
|
|
|
getText().insert(editText.getSelectionStart(), "[left]"); |
|
|
getText().insert(editText.getSelectionStart(), "[left]"); |
|
|
getText().insert(editText.getSelectionEnd(), "[/left]"); |
|
|
getText().insert(editText.getSelectionEnd(), "[/left]"); |
|
|
editText.setSelection(hadTextSelection ? editText.getSelectionEnd() : editText.getSelectionStart() - 7); |
|
|
editText.setSelection( |
|
|
|
|
|
hadTextSelection |
|
|
|
|
|
? editText.getSelectionEnd() |
|
|
|
|
|
: editText.getSelectionStart() - 7); |
|
|
break; |
|
|
break; |
|
|
} |
|
|
case R.drawable.ic_format_align_center: |
|
|
case R.drawable.ic_format_align_center: { |
|
|
hadTextSelection = editText.hasSelection(); |
|
|
boolean hadTextSelection = editText.hasSelection(); |
|
|
|
|
|
getText().insert(editText.getSelectionStart(), "[center]"); |
|
|
getText().insert(editText.getSelectionStart(), "[center]"); |
|
|
getText().insert(editText.getSelectionEnd(), "[/center]"); |
|
|
getText().insert(editText.getSelectionEnd(), "[/center]"); |
|
|
editText.setSelection(hadTextSelection ? editText.getSelectionEnd() : editText.getSelectionStart() - 9); |
|
|
editText.setSelection( |
|
|
|
|
|
hadTextSelection |
|
|
|
|
|
? editText.getSelectionEnd() |
|
|
|
|
|
: editText.getSelectionStart() - 9); |
|
|
break; |
|
|
break; |
|
|
} |
|
|
case R.drawable.ic_format_align_right: |
|
|
case R.drawable.ic_format_align_right: { |
|
|
hadTextSelection = editText.hasSelection(); |
|
|
boolean hadTextSelection = editText.hasSelection(); |
|
|
|
|
|
getText().insert(editText.getSelectionStart(), "[right]"); |
|
|
getText().insert(editText.getSelectionStart(), "[right]"); |
|
|
getText().insert(editText.getSelectionEnd(), "[/right]"); |
|
|
getText().insert(editText.getSelectionEnd(), "[/right]"); |
|
|
editText.setSelection(hadTextSelection ? editText.getSelectionEnd() : editText.getSelectionStart() - 8); |
|
|
editText.setSelection( |
|
|
|
|
|
hadTextSelection |
|
|
|
|
|
? editText.getSelectionEnd() |
|
|
|
|
|
: editText.getSelectionStart() - 8); |
|
|
break; |
|
|
break; |
|
|
} |
|
|
case R.drawable.ic_insert_link: |
|
|
case R.drawable.ic_insert_link: { |
|
|
LinearLayout dialogBody = |
|
|
LinearLayout dialogBody = (LinearLayout) LayoutInflater.from(context) |
|
|
(LinearLayout) |
|
|
.inflate(R.layout.dialog_create_link, null); |
|
|
LayoutInflater.from(context).inflate(R.layout.dialog_create_link, null); |
|
|
TextInputLayout linkUrl = dialogBody.findViewById(R.id.link_url_input); |
|
|
TextInputLayout linkUrl = dialogBody.findViewById(R.id.link_url_input); |
|
|
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(); |
|
|
hadTextSelection = editText.hasSelection(); |
|
|
int start = editText.getSelectionStart(), end = editText.getSelectionEnd(); |
|
|
int start = editText.getSelectionStart(), end = editText.getSelectionEnd(); |
|
|
if (editText.hasSelection()) { |
|
|
if (editText.hasSelection()) { |
|
|
linkText.getEditText().setText( |
|
|
linkText |
|
|
editText.getText().toString().substring(editText.getSelectionStart(), editText.getSelectionEnd())); |
|
|
.getEditText() |
|
|
} |
|
|
.setText( |
|
|
AlertDialog linkDialog = new AlertDialog.Builder(context, R.style.AppTheme_Dark_Dialog) |
|
|
editText |
|
|
|
|
|
.getText() |
|
|
|
|
|
.toString() |
|
|
|
|
|
.substring( |
|
|
|
|
|
editText.getSelectionStart(), editText.getSelectionEnd())); |
|
|
|
|
|
} |
|
|
|
|
|
AlertDialog linkDialog = |
|
|
|
|
|
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) |
|
|
.setPositiveButton(R.string.ok, null) |
|
|
.setPositiveButton(R.string.ok, null) |
|
|
.setNegativeButton(R.string.cancel, (dialog, which) -> dialog.dismiss()) |
|
|
.setNegativeButton(R.string.cancel, (dialog, which) -> dialog.dismiss()) |
|
|
.create(); |
|
|
.create(); |
|
|
linkDialog.setOnShowListener(dialogInterface -> { |
|
|
linkDialog.setOnShowListener( |
|
|
|
|
|
dialogInterface -> { |
|
|
Button button = linkDialog.getButton(AlertDialog.BUTTON_POSITIVE); |
|
|
Button button = linkDialog.getButton(AlertDialog.BUTTON_POSITIVE); |
|
|
button.setOnClickListener(view12 -> { |
|
|
button.setOnClickListener( |
|
|
if (TextUtils.isEmpty(Objects.requireNonNull(linkUrl.getEditText()).getText().toString())) { |
|
|
view12 -> { |
|
|
|
|
|
if (TextUtils.isEmpty( |
|
|
|
|
|
Objects.requireNonNull(linkUrl.getEditText()) |
|
|
|
|
|
.getText() |
|
|
|
|
|
.toString())) { |
|
|
linkUrl.setError(context.getString(R.string.input_field_required)); |
|
|
linkUrl.setError(context.getString(R.string.input_field_required)); |
|
|
return; |
|
|
return; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if (hadTextSelection) editText.getText().delete(start, end); |
|
|
if (hadTextSelection) editText.getText().delete(start, end); |
|
|
if (!TextUtils.isEmpty(linkText.getEditText().getText())) { |
|
|
if (!TextUtils.isEmpty(linkText.getEditText().getText())) { |
|
|
getText().insert(editText.getSelectionStart(), "[url=" + |
|
|
getText() |
|
|
linkUrl.getEditText().getText().toString() + "]" + |
|
|
.insert( |
|
|
linkText.getEditText().getText().toString() + "[/url]"); |
|
|
editText.getSelectionStart(), |
|
|
} |
|
|
"[url=" |
|
|
else |
|
|
+ linkUrl.getEditText().getText().toString() |
|
|
getText().insert(editText.getSelectionStart(), "[url]" + |
|
|
+ "]" |
|
|
linkUrl.getEditText().getText().toString() + "[/url]"); |
|
|
+ linkText.getEditText().getText().toString() |
|
|
|
|
|
+ "[/url]"); |
|
|
|
|
|
} else |
|
|
|
|
|
getText() |
|
|
|
|
|
.insert( |
|
|
|
|
|
editText.getSelectionStart(), |
|
|
|
|
|
"[url]" |
|
|
|
|
|
+ linkUrl.getEditText().getText().toString() |
|
|
|
|
|
+ "[/url]"); |
|
|
linkDialog.dismiss(); |
|
|
linkDialog.dismiss(); |
|
|
}); |
|
|
}); |
|
|
}); |
|
|
}); |
|
|
linkDialog.show(); |
|
|
linkDialog.show(); |
|
|
break; |
|
|
break; |
|
|
} |
|
|
case R.drawable.ic_format_quote: |
|
|
case R.drawable.ic_format_quote: { |
|
|
hadTextSelection = editText.hasSelection(); |
|
|
boolean hadTextSelection = editText.hasSelection(); |
|
|
|
|
|
getText().insert(editText.getSelectionStart(), "[quote]"); |
|
|
getText().insert(editText.getSelectionStart(), "[quote]"); |
|
|
getText().insert(editText.getSelectionEnd(), "[/quote]"); |
|
|
getText().insert(editText.getSelectionEnd(), "[/quote]"); |
|
|
editText.setSelection(hadTextSelection ? editText.getSelectionEnd() : editText.getSelectionStart() - 8); |
|
|
editText.setSelection( |
|
|
|
|
|
hadTextSelection |
|
|
|
|
|
? editText.getSelectionEnd() |
|
|
|
|
|
: editText.getSelectionStart() - 8); |
|
|
break; |
|
|
break; |
|
|
} |
|
|
case R.drawable.ic_code: |
|
|
case R.drawable.ic_code: { |
|
|
hadTextSelection = editText.hasSelection(); |
|
|
boolean hadTextSelection = editText.hasSelection(); |
|
|
|
|
|
getText().insert(editText.getSelectionStart(), "[code]"); |
|
|
getText().insert(editText.getSelectionStart(), "[code]"); |
|
|
getText().insert(editText.getSelectionEnd(), "[/code]"); |
|
|
getText().insert(editText.getSelectionEnd(), "[/code]"); |
|
|
editText.setSelection(hadTextSelection ? editText.getSelectionEnd() : editText.getSelectionStart() - 7); |
|
|
editText.setSelection( |
|
|
|
|
|
hadTextSelection |
|
|
|
|
|
? editText.getSelectionEnd() |
|
|
|
|
|
: editText.getSelectionStart() - 7); |
|
|
break; |
|
|
break; |
|
|
} |
|
|
case R.drawable.ic_functions: |
|
|
case R.drawable.ic_functions: { |
|
|
hadTextSelection = editText.hasSelection(); |
|
|
boolean hadTextSelection = editText.hasSelection(); |
|
|
|
|
|
getText().insert(editText.getSelectionStart(), "[tex]"); |
|
|
getText().insert(editText.getSelectionStart(), "[tex]"); |
|
|
getText().insert(editText.getSelectionEnd(), "[/tex]"); |
|
|
getText().insert(editText.getSelectionEnd(), "[/tex]"); |
|
|
editText.setSelection(hadTextSelection ? editText.getSelectionEnd() : editText.getSelectionStart() - 6); |
|
|
editText.setSelection( |
|
|
|
|
|
hadTextSelection |
|
|
|
|
|
? editText.getSelectionEnd() |
|
|
|
|
|
: editText.getSelectionStart() - 6); |
|
|
break; |
|
|
break; |
|
|
} |
|
|
default: |
|
|
default: throw new IllegalArgumentException("Unknown format button click"); |
|
|
throw new IllegalArgumentException("Unknown format button click"); |
|
|
} |
|
|
} |
|
|
})); |
|
|
})); |
|
|
|
|
|
|
|
|