improved text indices
This commit is contained in:
parent
180dac84d2
commit
e111e258ba
|
@ -7,6 +7,7 @@ import android.text.style.URLSpan;
|
||||||
|
|
||||||
import com.bluelinelabs.logansquare.annotation.JsonField;
|
import com.bluelinelabs.logansquare.annotation.JsonField;
|
||||||
import com.bluelinelabs.logansquare.annotation.JsonObject;
|
import com.bluelinelabs.logansquare.annotation.JsonObject;
|
||||||
|
import com.hannesdorfmann.parcelableplease.annotation.ParcelableNoThanks;
|
||||||
import com.hannesdorfmann.parcelableplease.annotation.ParcelablePlease;
|
import com.hannesdorfmann.parcelableplease.annotation.ParcelablePlease;
|
||||||
import com.hannesdorfmann.parcelableplease.annotation.ParcelableThisPlease;
|
import com.hannesdorfmann.parcelableplease.annotation.ParcelableThisPlease;
|
||||||
|
|
||||||
|
@ -40,12 +41,19 @@ public class SpanItem implements Parcelable {
|
||||||
@ParcelableThisPlease
|
@ParcelableThisPlease
|
||||||
public String link;
|
public String link;
|
||||||
|
|
||||||
|
@ParcelableNoThanks
|
||||||
|
public int orig_start = -1;
|
||||||
|
@ParcelableNoThanks
|
||||||
|
public int orig_end = -1;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return "SpanItem{" +
|
return "SpanItem{" +
|
||||||
"start=" + start +
|
"start=" + start +
|
||||||
", end=" + end +
|
", end=" + end +
|
||||||
", link='" + link + '\'' +
|
", link='" + link + '\'' +
|
||||||
|
", orig_start=" + orig_start +
|
||||||
|
", orig_end=" + orig_end +
|
||||||
'}';
|
'}';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -103,6 +103,8 @@ public class HtmlBuilder {
|
||||||
final SpanItem item = new SpanItem();
|
final SpanItem item = new SpanItem();
|
||||||
item.start = spanStart;
|
item.start = spanStart;
|
||||||
item.end = sb.length();
|
item.end = sb.length();
|
||||||
|
item.orig_start = start;
|
||||||
|
item.orig_end = end;
|
||||||
if (spec instanceof LinkSpec) {
|
if (spec instanceof LinkSpec) {
|
||||||
item.link = ((LinkSpec) spec).link;
|
item.link = ((LinkSpec) spec).link;
|
||||||
}
|
}
|
||||||
|
|
|
@ -287,14 +287,42 @@ public class InternalTwitterContentUtils {
|
||||||
textWithIndices.text = pair.first;
|
textWithIndices.text = pair.first;
|
||||||
textWithIndices.spans = pair.second;
|
textWithIndices.spans = pair.second;
|
||||||
if (range != null && range.length == 2) {
|
if (range != null && range.length == 2) {
|
||||||
final int length = source.length();
|
|
||||||
textWithIndices.range = new int[2];
|
textWithIndices.range = new int[2];
|
||||||
textWithIndices.range[0] = source.charCount(0, range[0]);
|
textWithIndices.range[0] = getResultRangeLength(source, pair.second, 0, range[0]);
|
||||||
textWithIndices.range[1] = pair.first.length() - source.charCount(range[1], length);
|
textWithIndices.range[1] = pair.first.length() - getResultRangeLength(source,
|
||||||
|
pair.second, range[1], source.length());
|
||||||
}
|
}
|
||||||
return textWithIndices;
|
return textWithIndices;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param spans Ordered spans
|
||||||
|
* @param start orig_start
|
||||||
|
* @param end orig_end
|
||||||
|
*/
|
||||||
|
@NonNull
|
||||||
|
static List<SpanItem> findByOrigRange(SpanItem[] spans, int start, int end) {
|
||||||
|
List<SpanItem> result = new ArrayList<>();
|
||||||
|
for (SpanItem span : spans) {
|
||||||
|
if (span.orig_start >= start && span.orig_end <= end) {
|
||||||
|
result.add(span);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int getResultRangeLength(CodePointArray source, SpanItem[] spans, int origStart, int origEnd) {
|
||||||
|
List<SpanItem> findResult = findByOrigRange(spans, origStart, origEnd);
|
||||||
|
if (findResult.isEmpty()) {
|
||||||
|
return source.charCount(origStart, origEnd);
|
||||||
|
}
|
||||||
|
SpanItem first = findResult.get(0), last = findResult.get(findResult.size() - 1);
|
||||||
|
if (first.orig_start == -1 || last.orig_end == -1)
|
||||||
|
return source.charCount(origStart, origEnd);
|
||||||
|
return source.charCount(origStart, first.orig_start) + (last.end - first.start)
|
||||||
|
+ source.charCount(first.orig_end, origEnd);
|
||||||
|
}
|
||||||
|
|
||||||
public static class StatusTextWithIndices {
|
public static class StatusTextWithIndices {
|
||||||
public String text;
|
public String text;
|
||||||
public SpanItem[] spans;
|
public SpanItem[] spans;
|
||||||
|
|
Loading…
Reference in New Issue