Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve removing spans on Android #537

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@
import com.facebook.react.views.text.internal.span.CustomLineHeightSpan;
import com.facebook.systrace.Systrace;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class MarkdownFormatter {
private final @NonNull AssetManager mAssetManager;
private final List<MarkdownSpan> mMarkdownSpans = new ArrayList<>();

public MarkdownFormatter(@NonNull AssetManager assetManager) {
mAssetManager = assetManager;
Expand All @@ -34,11 +36,11 @@ public void format(@NonNull SpannableStringBuilder ssb, @NonNull List<MarkdownRa
private void removeSpans(@NonNull SpannableStringBuilder ssb) {
try {
Systrace.beginSection(0, "removeSpans");
// We shouldn't use `removeSpans()` because it also removes SpellcheckSpan, SuggestionSpan etc.
MarkdownSpan[] spans = ssb.getSpans(0, ssb.length(), MarkdownSpan.class);
for (MarkdownSpan span : spans) {
// We shouldn't use `ssb.removeSpans()` because it also removes SpellcheckSpan, SuggestionSpan etc.
for (MarkdownSpan span : mMarkdownSpans) {
ssb.removeSpan(span);
}
mMarkdownSpans.clear();
} finally {
Systrace.endSection(0);
}
Expand Down Expand Up @@ -128,5 +130,6 @@ private void applyRange(@NonNull SpannableStringBuilder ssb, @NonNull MarkdownRa

private void setSpan(@NonNull SpannableStringBuilder ssb, @NonNull MarkdownSpan span, int start, int end) {
ssb.setSpan(span, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
mMarkdownSpans.add(span);
}
}
Loading