Skip to content

Commit

Permalink
Add Spanned to HTML converter
Browse files Browse the repository at this point in the history
This drops using AndroidView in HtmlBlock.
  • Loading branch information
halilozercan committed Feb 29, 2024
1 parent 1deef5e commit f07681d
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 16 deletions.
3 changes: 2 additions & 1 deletion buildSrc/src/main/kotlin/Dependencies.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ object Compose {
val version = "1.5.4"
val compilerVersion = "1.5.8"
val desktopVersion = "1.6.0"
val activity = "androidx.activity:activity-compose:1.7.2"
val activity = "androidx.activity:activity-compose:1.8.2"
val annotatedText = "io.github.aghajari:AnnotatedText:1.0.3"
val toolingData = "androidx.compose.ui:ui-tooling-data:$version"
val multiplatformUiUtil = "org.jetbrains.compose.ui:ui-util:$desktopVersion"
val coil = "io.coil-kt:coil-compose:2.5.0"
Expand Down
1 change: 1 addition & 0 deletions richtext-markdown/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ kotlin {
val androidMain by getting {
dependencies {
implementation(Compose.coil)
implementation(Compose.annotatedText)

implementation(Commonmark.core)
implementation(Commonmark.tables)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,23 @@ import android.os.Build.VERSION_CODES
import android.text.Html
import android.widget.TextView
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.util.fastForEach
import androidx.compose.ui.viewinterop.AndroidView
import com.aghajari.compose.text.BasicAnnotatedText
import com.aghajari.compose.text.fromHtml
import com.halilibo.richtext.ui.RichTextScope
import com.halilibo.richtext.ui.string.Text
import com.halilibo.richtext.ui.string.richTextString

@Composable
internal actual fun HtmlBlock(content: String) {
AndroidView(
factory = { context ->
// TODO: pass current styling to legacy TextView
TextView(context)
},
update = {
it.text = if (VERSION.SDK_INT >= VERSION_CODES.N) {
Html.fromHtml(content, 0)
} else {
@Suppress("DEPRECATION")
Html.fromHtml(content)
internal actual fun RichTextScope.HtmlBlock(content: String) {
val richTextString = remember(content) {
richTextString {
withAnnotatedString {
append(content.fromHtml().annotatedString)
}
}
)
}
Text(richTextString)
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package com.halilibo.richtext.markdown

import androidx.compose.runtime.Composable
import com.halilibo.richtext.ui.RichTextScope

/**
* Android and JVM can have different WebView or HTML rendering implementations.
* We are leaving HTML rendering to platform side.
*/
@Composable
internal expect fun HtmlBlock(content: String)
internal expect fun RichTextScope.HtmlBlock(content: String)
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ package com.halilibo.richtext.markdown
import androidx.compose.foundation.text.BasicText
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import com.halilibo.richtext.ui.RichTextScope

@Composable
internal actual fun HtmlBlock(content: String) {
internal actual fun RichTextScope.HtmlBlock(content: String) {
DisposableEffect(Unit) {
println("Html blocks are rendered literally in Compose Desktop!")
onDispose { }
Expand Down

0 comments on commit f07681d

Please sign in to comment.