From af9548dfdd4edb46642677035a3ad0c1f9e468ed Mon Sep 17 00:00:00 2001 From: SpiritCroc Date: Tue, 4 Oct 2022 17:59:52 +0200 Subject: [PATCH] Support inline images in the timeline (#5877) * Support inline images in the timeline Co-authored-by: Benoit Marty --- changelog.d/351.feature | 1 + dependencies.gradle | 1 + vector/build.gradle | 1 + .../app/features/html/EventHtmlRenderer.kt | 28 ++++++++++++++++++- 4 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 changelog.d/351.feature diff --git a/changelog.d/351.feature b/changelog.d/351.feature new file mode 100644 index 0000000000..af86d2fb39 --- /dev/null +++ b/changelog.d/351.feature @@ -0,0 +1 @@ +Render inline images in the timeline diff --git a/dependencies.gradle b/dependencies.gradle index 7792c4ac48..610f8b97aa 100644 --- a/dependencies.gradle +++ b/dependencies.gradle @@ -119,6 +119,7 @@ ext.libs = [ markwon : [ 'core' : "io.noties.markwon:core:$markwon", 'extLatex' : "io.noties.markwon:ext-latex:$markwon", + 'imageGlide' : "io.noties.markwon:image-glide:$markwon", 'inlineParser' : "io.noties.markwon:inline-parser:$markwon", 'html' : "io.noties.markwon:html:$markwon" ], diff --git a/vector/build.gradle b/vector/build.gradle index 13d021e37e..e10d2a3436 100644 --- a/vector/build.gradle +++ b/vector/build.gradle @@ -183,6 +183,7 @@ dependencies { } implementation libs.markwon.core implementation libs.markwon.extLatex + implementation libs.markwon.imageGlide implementation libs.markwon.inlineParser implementation libs.markwon.html implementation 'com.googlecode.htmlcompressor:htmlcompressor:1.5.2' diff --git a/vector/src/main/java/im/vector/app/features/html/EventHtmlRenderer.kt b/vector/src/main/java/im/vector/app/features/html/EventHtmlRenderer.kt index 725f23cddd..9e869ecde1 100644 --- a/vector/src/main/java/im/vector/app/features/html/EventHtmlRenderer.kt +++ b/vector/src/main/java/im/vector/app/features/html/EventHtmlRenderer.kt @@ -27,8 +27,13 @@ package im.vector.app.features.html import android.content.Context import android.content.res.Resources +import android.graphics.drawable.Drawable import android.text.Spannable import androidx.core.text.toSpannable +import com.bumptech.glide.Glide +import com.bumptech.glide.RequestBuilder +import com.bumptech.glide.request.target.Target +import im.vector.app.core.di.ActiveSessionHolder import im.vector.app.core.resources.ColorProvider import im.vector.app.core.utils.DimensionConverter import im.vector.app.features.settings.VectorPreferences @@ -39,12 +44,15 @@ import io.noties.markwon.PrecomputedFutureTextSetterCompat import io.noties.markwon.ext.latex.JLatexMathPlugin import io.noties.markwon.ext.latex.JLatexMathTheme import io.noties.markwon.html.HtmlPlugin +import io.noties.markwon.image.AsyncDrawable +import io.noties.markwon.image.glide.GlideImagesPlugin import io.noties.markwon.inlineparser.EntityInlineProcessor import io.noties.markwon.inlineparser.HtmlInlineProcessor import io.noties.markwon.inlineparser.MarkwonInlineParser import io.noties.markwon.inlineparser.MarkwonInlineParserPlugin import org.commonmark.node.Node import org.commonmark.parser.Parser +import org.matrix.android.sdk.api.MatrixUrls.isMxcUrl import timber.log.Timber import javax.inject.Inject import javax.inject.Singleton @@ -53,7 +61,8 @@ import javax.inject.Singleton class EventHtmlRenderer @Inject constructor( htmlConfigure: MatrixHtmlPluginConfigure, context: Context, - vectorPreferences: VectorPreferences + vectorPreferences: VectorPreferences, + private val activeSessionHolder: ActiveSessionHolder ) { interface PostProcessor { @@ -62,6 +71,23 @@ class EventHtmlRenderer @Inject constructor( private val builder = Markwon.builder(context) .usePlugin(HtmlPlugin.create(htmlConfigure)) + .usePlugin(GlideImagesPlugin.create(object : GlideImagesPlugin.GlideStore { + override fun load(drawable: AsyncDrawable): RequestBuilder { + val url = drawable.destination + if (url.isMxcUrl()) { + val contentUrlResolver = activeSessionHolder.getActiveSession().contentUrlResolver() + val imageUrl = contentUrlResolver.resolveFullSize(url) + // Override size to avoid crashes for huge pictures + return Glide.with(context).load(imageUrl).override(500) + } + // We don't want to support other url schemes here, so just return a request for null + return Glide.with(context).load(null as String?) + } + + override fun cancel(target: Target<*>) { + Glide.with(context).clear(target) + } + })) private val markwon = if (vectorPreferences.latexMathsIsEnabled()) { // If latex maths is enabled in app preferences, refomat it so Markwon recognises it