This commit is contained in:
admin 2021-04-30 21:02:03 +08:00 committed by Niedermann IT-Dienstleistungen
parent efcf6b5af9
commit 521543ac94
2 changed files with 50 additions and 20 deletions

View file

@ -300,19 +300,19 @@ public class MarkdownUtilTest extends TestCase {
assertEquals("Lorem [](https://example.com) dolor sit amet.", builder.toString());
// TODO Add link without clipboardUrl to empty selection before space character
// builder = new SpannableStringBuilder("Lorem ipsum dolor sit amet.");
// assertEquals(13, MarkdownUtil.insertLink(builder, 11, 11, null));
// assertEquals("Lorem ipsum []() dolor sit amet.", builder.toString());
builder = new SpannableStringBuilder("Lorem ipsum dolor sit amet.");
assertEquals(13, MarkdownUtil.insertLink(builder, 11, 11, null));
assertEquals("Lorem ipsum []() dolor sit amet.", builder.toString());
// TODO Add link without clipboardUrl to empty selection after space character
// builder = new SpannableStringBuilder("Lorem ipsum dolor sit amet.");
// assertEquals(13, MarkdownUtil.insertLink(builder, 12, 12, null));
// assertEquals("Lorem ipsum []() dolor sit amet.", builder.toString());
builder = new SpannableStringBuilder("Lorem ipsum dolor sit amet.");
assertEquals(13, MarkdownUtil.insertLink(builder, 12, 12, null));
assertEquals("Lorem ipsum []() dolor sit amet.", builder.toString());
// TODO Add link without clipboardUrl to empty selection in word
// builder = new SpannableStringBuilder("Lorem ipsum dolor sit amet.");
// assertEquals(20, MarkdownUtil.insertLink(builder, 14, 14, null));
// assertEquals("Lorem ipsum [dolor]() sit amet.", builder.toString());
builder = new SpannableStringBuilder("Lorem ipsum dolor sit amet.");
assertEquals(20, MarkdownUtil.insertLink(builder, 14, 14, null));
assertEquals("Lorem ipsum [dolor]() sit amet.", builder.toString());
// Add link with clipboardUrl to normal text
builder = new SpannableStringBuilder("Lorem ipsum dolor sit amet.");
@ -325,19 +325,19 @@ public class MarkdownUtilTest extends TestCase {
assertEquals("Lorem [https://example.com](https://example.de) dolor sit amet.", builder.toString());
// TODO Add link with clipboardUrl to empty selection before space character
// builder = new SpannableStringBuilder("Lorem ipsum dolor sit amet.");
// assertEquals(13, MarkdownUtil.insertLink(builder, 11, 11, "https://example.de"));
// assertEquals("Lorem ipsum []("https://example.de") dolor sit amet.", builder.toString());
builder = new SpannableStringBuilder("Lorem ipsum dolor sit amet.");
assertEquals(13, MarkdownUtil.insertLink(builder, 11, 11, "https://example.de"));
assertEquals("Lorem ipsum [](https://example.de) dolor sit amet.", builder.toString());
// TODO Add link with clipboardUrl to empty selection after space character
// builder = new SpannableStringBuilder("Lorem ipsum dolor sit amet.");
// assertEquals(13, MarkdownUtil.insertLink(builder, 12, 12, "https://example.de"));
// assertEquals("Lorem ipsum []("https://example.de") dolor sit amet.", builder.toString());
builder = new SpannableStringBuilder("Lorem ipsum dolor sit amet.");
assertEquals(13, MarkdownUtil.insertLink(builder, 12, 12, "https://example.de"));
assertEquals("Lorem ipsum [](https://example.de) dolor sit amet.", builder.toString());
// TODO Add link with clipboardUrl to empty selection in word
// builder = new SpannableStringBuilder("Lorem ipsum dolor sit amet.");
// assertEquals(18, MarkdownUtil.insertLink(builder, 14, 14, "https://example.de"));
// assertEquals("Lorem ipsum [dolor]("https://example.de") sit amet.", builder.toString());
builder = new SpannableStringBuilder("Lorem ipsum dolor sit amet.");
assertEquals(38, MarkdownUtil.insertLink(builder, 14, 14, "https://example.de"));
assertEquals("Lorem ipsum [dolor](https://example.de) sit amet.", builder.toString());
}
@Test

View file

@ -315,8 +315,38 @@ public class MarkdownUtil {
*/
public static int insertLink(@NonNull Editable editable, int selectionStart, int selectionEnd, @Nullable String clipboardUrl) {
if (selectionStart == selectionEnd) {
editable.insert(selectionStart, "[](" + (clipboardUrl == null ? "" : clipboardUrl) + ")");
return selectionStart + 1;
char start = editable.charAt(selectionStart-1);
char end = editable.charAt(selectionEnd);
if (start == ' '||end == ' ') {
if (start != ' ') {
editable.insert(selectionStart, " ");
selectionStart += 1;
}
if (end != ' ') {
editable.insert(selectionEnd, " ");
}
editable.insert(selectionStart, "[](" + (clipboardUrl == null ? "" : clipboardUrl) + ")");
return selectionStart + 1;
}else {
while (start != ' '){
selectionStart--;
start = editable.charAt(selectionStart);
}
selectionStart++;
while (end != ' '){
selectionEnd++;
end = editable.charAt(selectionEnd);
}
selectionEnd++;
editable.insert(selectionStart,"[");
editable.insert(selectionEnd,"](" + (clipboardUrl == null ? "" : clipboardUrl) + ")");
if (clipboardUrl!=null){
selectionEnd += clipboardUrl.length();
}
return selectionEnd + 2;
}
} else {
final boolean textToFormatIsLink = TextUtils.indexOf(editable.subSequence(selectionStart, selectionEnd), "http") == 0;
if (textToFormatIsLink) {