Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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 @@ -163,9 +163,7 @@ class ReactNativeRichTextEditorView : AppCompatEditText {
if (value == null) return
isSettingValue = true

val isHtmlTagRecognized = value.startsWith("<html>") && value.endsWith("</html>")
val isPTagRecognized = value.startsWith("<p>") && value.endsWith("</p>")
val isHtml = isHtmlTagRecognized || isPTagRecognized
val isHtml = value.startsWith("<html>") && value.endsWith("</html>")
if (isHtml) {
val parsed = EditorParser.fromHtml(value, EditorParser.FROM_HTML_MODE_COMPACT, richTextStyle, null, null)
val withoutLastNewLine = parsed.trimEnd('\n')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import android.text.style.BackgroundColorSpan;
import android.text.style.ForegroundColorSpan;
import android.text.style.ParagraphStyle;
import android.text.style.RelativeSizeSpan;
import android.text.style.TypefaceSpan;

import com.swmansion.reactnativerichtexteditor.spans.EditorBlockQuoteSpan;
Expand Down Expand Up @@ -700,11 +699,11 @@ private void handleStartTag(String tag, Attributes attributes) {
} else if (tag.equalsIgnoreCase("strike")) {
start(mSpannableStringBuilder, new Strikethrough());
} else if (tag.equalsIgnoreCase("h1")) {
start(mSpannableStringBuilder, new H1());
startHeading(mSpannableStringBuilder, attributes, 1);
} else if (tag.equalsIgnoreCase("h2")) {
start(mSpannableStringBuilder, new H2());
startHeading(mSpannableStringBuilder, attributes, 2);
} else if (tag.equalsIgnoreCase("h3")) {
start(mSpannableStringBuilder, new H3());
startHeading(mSpannableStringBuilder, attributes, 3);
} else if (tag.equalsIgnoreCase("img")) {
startImg(mSpannableStringBuilder, attributes, mImageGetter, mStyle);
} else if (tag.equalsIgnoreCase("code")) {
Expand Down Expand Up @@ -747,11 +746,11 @@ private void handleEndTag(String tag) {
} else if (tag.equalsIgnoreCase("s")) {
end(mSpannableStringBuilder, Strikethrough.class, new EditorStrikeThroughSpan(mStyle));
} else if (tag.equalsIgnoreCase("h1")) {
end(mSpannableStringBuilder, H1.class, new EditorH1Span(mStyle));
endHeading(mSpannableStringBuilder, mStyle, 1);
} else if (tag.equalsIgnoreCase("h2")) {
end(mSpannableStringBuilder, H2.class, new EditorH2Span(mStyle));
endHeading(mSpannableStringBuilder, mStyle, 2);
} else if (tag.equalsIgnoreCase("h3")) {
end(mSpannableStringBuilder, H3.class, new EditorH3Span(mStyle));
endHeading(mSpannableStringBuilder, mStyle, 3);
} else if (tag.equalsIgnoreCase("code")) {
end(mSpannableStringBuilder, Code.class, new EditorInlineCodeSpan(mStyle));
} else if (tag.equalsIgnoreCase("mention")) {
Expand Down Expand Up @@ -869,9 +868,9 @@ private static void endLi(Editable text, RichTextStyle style) {
List l = getLast(text, List.class);
if (l != null) {
if (l.mType.equals("ol")) {
setListSpanFromMark(text, l, new EditorOrderedListSpan(l.mIndex, style));
setParagraphSpanFromMark(text, l, new EditorOrderedListSpan(l.mIndex, style));
} else {
setListSpanFromMark(text, l, new EditorUnorderedListSpan(style));
setParagraphSpanFromMark(text, l, new EditorUnorderedListSpan(style));
}
}

Expand All @@ -885,34 +884,58 @@ private void startBlockquote(Editable text, Attributes attributes) {

private static void endBlockquote(Editable text, RichTextStyle style) {
endBlockElement(text);
end(text, Blockquote.class, new EditorBlockQuoteSpan(style));
Blockquote last = getLast(text, Blockquote.class);
setParagraphSpanFromMark(text, last, new EditorBlockQuoteSpan(style));
}

private void startCodeBlock(Editable text, Attributes attributes) {
startBlockElement(text, attributes, getMarginBlockquote());
start(text, new CodeBlock());
;
}

private static void endCodeBlock(Editable text, RichTextStyle style) {
endBlockElement(text);
end(text, CodeBlock.class, new EditorCodeBlockSpan(style));
CodeBlock last = getLast(text, CodeBlock.class);
setParagraphSpanFromMark(text, last, new EditorCodeBlockSpan(style));
}

private void startHeading(Editable text, Attributes attributes, int level) {
startBlockElement(text, attributes, getMarginHeading());
start(text, new Heading(level));
}

private static void endHeading(Editable text, RichTextStyle style) {
// RelativeSizeSpan and StyleSpan are CharacterStyles
// Their ranges should not include the newlines at the end
Heading h = getLast(text, Heading.class);
if (h != null) {
setSpanFromMark(text, h, new RelativeSizeSpan(HEADING_SIZES[h.mLevel]),
new EditorBoldSpan(style));
switch (level) {
case 1:
start(text, new H1());
break;
case 2:
start(text, new H2());
break;
case 3:
start(text, new H3());
break;
default:
throw new IllegalArgumentException("Unsupported heading level: " + level);
}
}

private static void endHeading(Editable text, RichTextStyle style, int level) {
endBlockElement(text);

switch (level) {
case 1:
H1 lastH1 = getLast(text, H1.class);
setParagraphSpanFromMark(text, lastH1, new EditorH1Span(style));
break;
case 2:
H2 lastH2 = getLast(text, H2.class);
setParagraphSpanFromMark(text, lastH2, new EditorH2Span(style));
break;
case 3:
H3 lastH3 = getLast(text, H3.class);
setParagraphSpanFromMark(text, lastH3, new EditorH3Span(style));
break;
default:
throw new IllegalArgumentException("Unsupported heading level: " + level);
}
}

private static <T> T getLast(Spanned text, Class<T> kind) {
Expand All @@ -939,7 +962,7 @@ private static void setSpanFromMark(Spannable text, Object mark, Object... spans
}
}

private static void setListSpanFromMark(Spannable text, Object mark, Object... spans) {
private static void setParagraphSpanFromMark(Spannable text, Object mark, Object... spans) {
int where = text.getSpanStart(mark);
text.removeSpan(mark);
int len = text.length();
Expand Down Expand Up @@ -1253,14 +1276,6 @@ public Background(int backgroundColor) {
}
}

private static class Heading {
private final int mLevel;

public Heading(int level) {
mLevel = level;
}
}

private static class Newline {
private final int mNumNewlines;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,11 @@ class EditorSelection(private val editorView: ReactNativeRichTextEditorView) {
endPosition++
}

if (startPosition >= endPosition) {
// If the start position is equal or greater than the end position, return the same position
startPosition = endPosition
}

return Pair(startPosition, endPosition)
}

Expand Down