Restore character style after linking

This commit is contained in:
M66B 2021-05-30 18:15:50 +02:00
parent 39c1964d50
commit 031196d19b
1 changed files with 15 additions and 5 deletions

View File

@ -57,8 +57,10 @@ import com.flask.colorpicker.builder.ColorPickerClickListener;
import com.flask.colorpicker.builder.ColorPickerDialogBuilder; import com.flask.colorpicker.builder.ColorPickerDialogBuilder;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.Map;
public class StyleHelper { public class StyleHelper {
static boolean apply(int action, LifecycleOwner owner, View anchor, EditText etBody, Object... args) { static boolean apply(int action, LifecycleOwner owner, View anchor, EditText etBody, Object... args) {
@ -533,10 +535,15 @@ public class StyleHelper {
String url = (String) args[0]; String url = (String) args[0];
List<Object> spans = new ArrayList<>(); List<CharacterStyle> spans = new ArrayList<>();
for (Object span : edit.getSpans(start, end, Object.class)) { Map<CharacterStyle, Pair<Integer, Integer>> ranges = new HashMap<>();
if (!(span instanceof URLSpan)) Map<CharacterStyle, Integer> flags = new HashMap<>();
for (CharacterStyle span : edit.getSpans(start, end, CharacterStyle.class)) {
if (!(span instanceof URLSpan)) {
spans.add(span); spans.add(span);
ranges.put(span, new Pair<>(edit.getSpanStart(span), edit.getSpanEnd(span)));
flags.put(span, edit.getSpanFlags(span));
}
edit.removeSpan(span); edit.removeSpan(span);
} }
@ -551,8 +558,11 @@ public class StyleHelper {
} }
// Restore other spans // Restore other spans
for (Object span : spans) for (CharacterStyle span : spans)
edit.setSpan(span, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); edit.setSpan(span,
ranges.get(span).first,
ranges.get(span).second,
flags.get(span));
etBody.setText(edit); etBody.setText(edit);
etBody.setSelection(end, end); etBody.setSelection(end, end);