Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Android] Fix: split editor and message mode in html parsing #27

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
10 changes: 8 additions & 2 deletions .github/workflows/android.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ jobs:
matrix:
api-level: [29]
steps:
- name: Set env vars
run: |
echo "ANDROID_NDK_TOOLCHAIN_DIR=$ANDROID_NDK_HOME/toolchains" >> $GITHUB_ENV
export ANDROID_NDK_TOOLCHAIN_DIR=$ANDROID_NDK_HOME/toolchains
echo "Toolchain dir: $ANDROID_NDK_TOOLCHAIN_DIR."
echo "Contents:"
find $ANDROID_NDK_TOOLCHAIN_DIR -maxdepth 1

- uses: actions/checkout@v4
with:
fetch-depth: 0
Expand All @@ -50,8 +58,6 @@ jobs:

- name: Setup Gradle & Build test cases
uses: gradle/gradle-build-action@v3
env:
ANDROID_NDK_TOOLCHAIN_DIR: ${{ env.ANDROID_NDK_HOME }}/toolchains
with:
build-root-directory: platforms/android
cache-read-only: false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ class MainActivity : ComponentActivity() {
StyledHtmlConverter(
context = context,
mentionDisplayHandler = mentionDisplayHandler,
isEditor = false,
isMention = mentionDetector?.let { detector ->
{ _, url ->
detector.isMention(url)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -31,6 +32,7 @@ class StyledHtmlConverter(
context = context,
styleConfig = style.toStyleConfig(context),
mentionDisplayHandler = mentionDisplayHandler,
isEditor = isEditor,
isMention = isMention,
)
}
Expand Down
12 changes: 10 additions & 2 deletions platforms/android/library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ cargo {
targets = ["arm", "x86", "x86_64", "arm64"]
targetIncludes = ["libuniffi_wysiwyg_composer.so"]
targetDirectory = '../../../target'
prebuiltToolchains = true
}

android {
Expand Down Expand Up @@ -69,12 +70,12 @@ android {
jacocoVersion = "0.8.8"
}

ndkVersion = "27.1.12297006"

packagingOptions {
resources.excludes += 'META-INF/LICENSE.md'
resources.excludes += 'META-INF/LICENSE-notice.md'
}

ndkVersion getNdkVersionAsWorkaround()
}

kotlin {
Expand Down Expand Up @@ -143,3 +144,10 @@ tasks.withType(Test) {
jacoco.excludes = ['jdk.internal.*']
}

// Workaround for https://github.com/mozilla/rust-android-gradle/issues/46
// This looks for the NDK path like: '/some/path/to/sdk/ndk/23.0.7599858'
// and takes the last path component, which should be the version number
def getNdkVersionAsWorkaround() {
def ndkDirectory = new File(android.sdkDirectory, "ndk")
return ndkDirectory.list().sort().last().split(PATH_SEPARATOR).last()
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class InterceptInputConnectionIntegrationTest {
it.htmlConverter = HtmlConverter.Factory.create(
context = app,
styleConfig = styleConfig,
isEditor = true,
mentionDisplayHandler = null,
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ class EditorEditText : AppCompatEditText {
context = context.applicationContext,
styleConfig = styleConfig,
mentionDisplayHandler = mentionDisplayHandler,
isEditor = true,
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -32,6 +33,7 @@ interface HtmlConverter {
html = html,
styleConfig = styleConfig,
mentionDisplayHandler = mentionDisplayHandler,
isEditor = isEditor,
isMention = isMention,
)
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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')
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,43 @@ class HtmlToSpansParserTest {
)
}

@Test
fun testParagraphsAreTranslatedToSingleLineBreakWhenEditorModeIsEnabled() {
val html = """
<p>Hello</p><p>World!</p>
""".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 = """
<p>Hello</p><p>World!</p>
""".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()
Expand All @@ -191,6 +226,7 @@ class HtmlToSpansParserTest {
html = html,
styleConfig = styleConfig,
mentionDisplayHandler = mentionDisplayHandler,
isEditor = isEditor,
isMention = { _, url ->
url.startsWith("https://matrix.to/#/@")
}
Expand Down
Loading