|
12 | 12 | import android.text.style.BackgroundColorSpan; |
13 | 13 | import android.text.style.ForegroundColorSpan; |
14 | 14 | import android.text.style.ParagraphStyle; |
15 | | -import android.text.style.RelativeSizeSpan; |
16 | 15 | import android.text.style.TypefaceSpan; |
17 | 16 |
|
18 | 17 | import com.swmansion.reactnativerichtexteditor.spans.EditorBlockQuoteSpan; |
@@ -700,11 +699,11 @@ private void handleStartTag(String tag, Attributes attributes) { |
700 | 699 | } else if (tag.equalsIgnoreCase("strike")) { |
701 | 700 | start(mSpannableStringBuilder, new Strikethrough()); |
702 | 701 | } else if (tag.equalsIgnoreCase("h1")) { |
703 | | - start(mSpannableStringBuilder, new H1()); |
| 702 | + startHeading(mSpannableStringBuilder, attributes, 1); |
704 | 703 | } else if (tag.equalsIgnoreCase("h2")) { |
705 | | - start(mSpannableStringBuilder, new H2()); |
| 704 | + startHeading(mSpannableStringBuilder, attributes, 2); |
706 | 705 | } else if (tag.equalsIgnoreCase("h3")) { |
707 | | - start(mSpannableStringBuilder, new H3()); |
| 706 | + startHeading(mSpannableStringBuilder, attributes, 3); |
708 | 707 | } else if (tag.equalsIgnoreCase("img")) { |
709 | 708 | startImg(mSpannableStringBuilder, attributes, mImageGetter, mStyle); |
710 | 709 | } else if (tag.equalsIgnoreCase("code")) { |
@@ -747,11 +746,11 @@ private void handleEndTag(String tag) { |
747 | 746 | } else if (tag.equalsIgnoreCase("s")) { |
748 | 747 | end(mSpannableStringBuilder, Strikethrough.class, new EditorStrikeThroughSpan(mStyle)); |
749 | 748 | } else if (tag.equalsIgnoreCase("h1")) { |
750 | | - end(mSpannableStringBuilder, H1.class, new EditorH1Span(mStyle)); |
| 749 | + endHeading(mSpannableStringBuilder, mStyle, 1); |
751 | 750 | } else if (tag.equalsIgnoreCase("h2")) { |
752 | | - end(mSpannableStringBuilder, H2.class, new EditorH2Span(mStyle)); |
| 751 | + endHeading(mSpannableStringBuilder, mStyle, 2); |
753 | 752 | } else if (tag.equalsIgnoreCase("h3")) { |
754 | | - end(mSpannableStringBuilder, H3.class, new EditorH3Span(mStyle)); |
| 753 | + endHeading(mSpannableStringBuilder, mStyle, 3); |
755 | 754 | } else if (tag.equalsIgnoreCase("code")) { |
756 | 755 | end(mSpannableStringBuilder, Code.class, new EditorInlineCodeSpan(mStyle)); |
757 | 756 | } else if (tag.equalsIgnoreCase("mention")) { |
@@ -902,18 +901,41 @@ private static void endCodeBlock(Editable text, RichTextStyle style) { |
902 | 901 |
|
903 | 902 | private void startHeading(Editable text, Attributes attributes, int level) { |
904 | 903 | startBlockElement(text, attributes, getMarginHeading()); |
905 | | - start(text, new Heading(level)); |
906 | | - } |
907 | 904 |
|
908 | | - private static void endHeading(Editable text, RichTextStyle style) { |
909 | | - // RelativeSizeSpan and StyleSpan are CharacterStyles |
910 | | - // Their ranges should not include the newlines at the end |
911 | | - Heading h = getLast(text, Heading.class); |
912 | | - if (h != null) { |
913 | | - setSpanFromMark(text, h, new RelativeSizeSpan(HEADING_SIZES[h.mLevel]), |
914 | | - new EditorBoldSpan(style)); |
| 905 | + switch (level) { |
| 906 | + case 1: |
| 907 | + start(text, new H1()); |
| 908 | + break; |
| 909 | + case 2: |
| 910 | + start(text, new H2()); |
| 911 | + break; |
| 912 | + case 3: |
| 913 | + start(text, new H3()); |
| 914 | + break; |
| 915 | + default: |
| 916 | + throw new IllegalArgumentException("Unsupported heading level: " + level); |
915 | 917 | } |
| 918 | + } |
| 919 | + |
| 920 | + private static void endHeading(Editable text, RichTextStyle style, int level) { |
916 | 921 | endBlockElement(text); |
| 922 | + |
| 923 | + switch (level) { |
| 924 | + case 1: |
| 925 | + H1 lastH1 = getLast(text, H1.class); |
| 926 | + setParagraphSpanFromMark(text, lastH1, new EditorH1Span(style)); |
| 927 | + break; |
| 928 | + case 2: |
| 929 | + H2 lastH2 = getLast(text, H2.class); |
| 930 | + setParagraphSpanFromMark(text, lastH2, new EditorH2Span(style)); |
| 931 | + break; |
| 932 | + case 3: |
| 933 | + H3 lastH3 = getLast(text, H3.class); |
| 934 | + setParagraphSpanFromMark(text, lastH3, new EditorH3Span(style)); |
| 935 | + break; |
| 936 | + default: |
| 937 | + throw new IllegalArgumentException("Unsupported heading level: " + level); |
| 938 | + } |
917 | 939 | } |
918 | 940 |
|
919 | 941 | private static <T> T getLast(Spanned text, Class<T> kind) { |
@@ -1254,14 +1276,6 @@ public Background(int backgroundColor) { |
1254 | 1276 | } |
1255 | 1277 | } |
1256 | 1278 |
|
1257 | | - private static class Heading { |
1258 | | - private final int mLevel; |
1259 | | - |
1260 | | - public Heading(int level) { |
1261 | | - mLevel = level; |
1262 | | - } |
1263 | | - } |
1264 | | - |
1265 | 1279 | private static class Newline { |
1266 | 1280 | private final int mNumNewlines; |
1267 | 1281 |
|
|
0 commit comments