diff --git a/platforms/android/example-compose/src/main/java/io/element/wysiwyg/compose/MainActivity.kt b/platforms/android/example-compose/src/main/java/io/element/wysiwyg/compose/MainActivity.kt index 81801186b..191fdc02e 100644 --- a/platforms/android/example-compose/src/main/java/io/element/wysiwyg/compose/MainActivity.kt +++ b/platforms/android/example-compose/src/main/java/io/element/wysiwyg/compose/MainActivity.kt @@ -70,6 +70,7 @@ class MainActivity : ComponentActivity() { StyledHtmlConverter( context = context, mentionDisplayHandler = mentionDisplayHandler, + isEditor = false, isMention = mentionDetector?.let { detector -> { _, url -> detector.isMention(url) diff --git a/platforms/android/example-view/src/main/java/io/element/android/wysiwyg/poc/RichTextEditor.kt b/platforms/android/example-view/src/main/java/io/element/android/wysiwyg/poc/RichTextEditor.kt index 79f0278f7..a42a6c2ee 100644 --- a/platforms/android/example-view/src/main/java/io/element/android/wysiwyg/poc/RichTextEditor.kt +++ b/platforms/android/example-view/src/main/java/io/element/android/wysiwyg/poc/RichTextEditor.kt @@ -177,9 +177,9 @@ class RichTextEditor : LinearLayout { val rooms = listOf("matrix", "element").map(Mention::Room) val everyone = Mention.NotifyEveryone val names = when (menuAction.suggestionPattern.key) { - PatternKey.AT -> people + everyone - PatternKey.HASH -> rooms - PatternKey.SLASH -> + PatternKey.At -> people + everyone + PatternKey.Hash -> rooms + PatternKey.Slash, is PatternKey.Custom -> emptyList() // TODO } val suggestions = names diff --git a/platforms/android/library-compose/src/main/java/io/element/android/wysiwyg/compose/StyledHtmlConverter.kt b/platforms/android/library-compose/src/main/java/io/element/android/wysiwyg/compose/StyledHtmlConverter.kt index 8e3770f71..edbe09cde 100644 --- a/platforms/android/library-compose/src/main/java/io/element/android/wysiwyg/compose/StyledHtmlConverter.kt +++ b/platforms/android/library-compose/src/main/java/io/element/android/wysiwyg/compose/StyledHtmlConverter.kt @@ -20,6 +20,7 @@ import timber.log.Timber class StyledHtmlConverter( private val context: Context, private val mentionDisplayHandler: MentionDisplayHandler?, + private val isEditor: Boolean, private val isMention: ((text: String, url: String) -> Boolean)?, ) : HtmlConverter { @@ -31,6 +32,7 @@ class StyledHtmlConverter( context = context, styleConfig = style.toStyleConfig(context), mentionDisplayHandler = mentionDisplayHandler, + isEditor = isEditor, isMention = isMention, ) } diff --git a/platforms/android/library/src/androidTest/java/io/element/android/wysiwyg/inputhandlers/InterceptInputConnectionIntegrationTest.kt b/platforms/android/library/src/androidTest/java/io/element/android/wysiwyg/inputhandlers/InterceptInputConnectionIntegrationTest.kt index 9d0f17f5b..e12acd6d5 100644 --- a/platforms/android/library/src/androidTest/java/io/element/android/wysiwyg/inputhandlers/InterceptInputConnectionIntegrationTest.kt +++ b/platforms/android/library/src/androidTest/java/io/element/android/wysiwyg/inputhandlers/InterceptInputConnectionIntegrationTest.kt @@ -37,6 +37,7 @@ class InterceptInputConnectionIntegrationTest { it.htmlConverter = HtmlConverter.Factory.create( context = app, styleConfig = styleConfig, + isEditor = true, mentionDisplayHandler = null, ) } diff --git a/platforms/android/library/src/main/java/io/element/android/wysiwyg/EditorEditText.kt b/platforms/android/library/src/main/java/io/element/android/wysiwyg/EditorEditText.kt index 705ad2287..7496c9921 100644 --- a/platforms/android/library/src/main/java/io/element/android/wysiwyg/EditorEditText.kt +++ b/platforms/android/library/src/main/java/io/element/android/wysiwyg/EditorEditText.kt @@ -94,6 +94,7 @@ class EditorEditText : AppCompatEditText { context = context.applicationContext, styleConfig = styleConfig, mentionDisplayHandler = mentionDisplayHandler, + isEditor = true, ) } diff --git a/platforms/android/library/src/main/java/io/element/android/wysiwyg/EditorStyledTextView.kt b/platforms/android/library/src/main/java/io/element/android/wysiwyg/EditorStyledTextView.kt index f01c6e77d..38fd34ad3 100644 --- a/platforms/android/library/src/main/java/io/element/android/wysiwyg/EditorStyledTextView.kt +++ b/platforms/android/library/src/main/java/io/element/android/wysiwyg/EditorStyledTextView.kt @@ -216,6 +216,7 @@ open class EditorStyledTextView : AppCompatTextView { return HtmlConverter.Factory.create(context = context, styleConfig = styleConfig, mentionDisplayHandler = mentionDisplayHandler, + isEditor = false, isMention = mentionDetector?.let { detector -> { _, url -> detector.isMention(url) diff --git a/platforms/android/library/src/main/java/io/element/android/wysiwyg/utils/HtmlConverter.kt b/platforms/android/library/src/main/java/io/element/android/wysiwyg/utils/HtmlConverter.kt index 5b5007577..c2f4101f6 100644 --- a/platforms/android/library/src/main/java/io/element/android/wysiwyg/utils/HtmlConverter.kt +++ b/platforms/android/library/src/main/java/io/element/android/wysiwyg/utils/HtmlConverter.kt @@ -23,6 +23,7 @@ interface HtmlConverter { context: Context, styleConfig: StyleConfig, mentionDisplayHandler: MentionDisplayHandler?, + isEditor: Boolean, isMention: ((text: String, url: String) -> Boolean)? = null, ): HtmlConverter { val resourcesProvider = AndroidResourcesHelper(context) @@ -32,6 +33,7 @@ interface HtmlConverter { html = html, styleConfig = styleConfig, mentionDisplayHandler = mentionDisplayHandler, + isEditor = isEditor, isMention = isMention, ) }) diff --git a/platforms/android/library/src/main/java/io/element/android/wysiwyg/utils/HtmlToSpansParser.kt b/platforms/android/library/src/main/java/io/element/android/wysiwyg/utils/HtmlToSpansParser.kt index 441e9e426..c9171f3a0 100644 --- a/platforms/android/library/src/main/java/io/element/android/wysiwyg/utils/HtmlToSpansParser.kt +++ b/platforms/android/library/src/main/java/io/element/android/wysiwyg/utils/HtmlToSpansParser.kt @@ -54,6 +54,7 @@ internal class HtmlToSpansParser( private val html: String, private val styleConfig: StyleConfig, private val mentionDisplayHandler: MentionDisplayHandler?, + private val isEditor: Boolean, private val isMention: ((text: String, url: String) -> Boolean)? = null, ) { private val safeList = Safelist() @@ -301,6 +302,10 @@ internal class HtmlToSpansParser( private fun SpannableStringBuilder.addLeadingLineBreakForBlockNode(element: Element) { if (element.isBlock && element.previousElementSibling()?.takeIf { it.tagName() != "br" } != null) { append('\n') + // If we're not in editor mode, add another line break to separate blocks + if (!isEditor) { + append('\n') + } } } diff --git a/platforms/android/library/src/test/kotlin/io/element/android/wysiwyg/utils/HtmlToSpansParserTest.kt b/platforms/android/library/src/test/kotlin/io/element/android/wysiwyg/utils/HtmlToSpansParserTest.kt index 95644c35d..a881c0bdc 100644 --- a/platforms/android/library/src/test/kotlin/io/element/android/wysiwyg/utils/HtmlToSpansParserTest.kt +++ b/platforms/android/library/src/test/kotlin/io/element/android/wysiwyg/utils/HtmlToSpansParserTest.kt @@ -180,8 +180,43 @@ class HtmlToSpansParserTest { ) } + @Test + fun testParagraphsAreTranslatedToSingleLineBreakWhenEditorModeIsEnabled() { + val html = """ +

Hello

World!

+ """.trimIndent() + val spanned = convertHtml(html, isEditor = true, mentionDisplayHandler = object : MentionDisplayHandler { + override fun resolveAtRoomMentionDisplay(): TextDisplay = + TextDisplay.Pill + + override fun resolveMentionDisplay(text: String, url: String): TextDisplay = + TextDisplay.Pill + }) + assertThat( + spanned.toString(), equalTo("Hello\nWorld!") + ) + } + + @Test + fun testParagraphsAreTranslatedToDoubleLineBreakWhenEditorModeIsDisabled() { + val html = """ +

Hello

World!

+ """.trimIndent() + val spanned = convertHtml(html, isEditor = false, mentionDisplayHandler = object : MentionDisplayHandler { + override fun resolveAtRoomMentionDisplay(): TextDisplay = + TextDisplay.Pill + + override fun resolveMentionDisplay(text: String, url: String): TextDisplay = + TextDisplay.Pill + }) + assertThat( + spanned.toString(), equalTo("Hello\n\nWorld!") + ) + } + private fun convertHtml( html: String, + isEditor: Boolean = true, mentionDisplayHandler: MentionDisplayHandler? = null, ): Spanned { val app = RuntimeEnvironment.getApplication() @@ -191,6 +226,7 @@ class HtmlToSpansParserTest { html = html, styleConfig = styleConfig, mentionDisplayHandler = mentionDisplayHandler, + isEditor = isEditor, isMention = { _, url -> url.startsWith("https://matrix.to/#/@") }