Support inline images in the timeline (#5877)

* Support inline images in the timeline

Co-authored-by: Benoit Marty <benoitm@matrix.org>
This commit is contained in:
SpiritCroc 2022-10-04 17:59:52 +02:00 committed by GitHub
parent 2cb16d9f11
commit af9548dfdd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 30 additions and 1 deletions

1
changelog.d/351.feature Normal file
View File

@ -0,0 +1 @@
Render inline images in the timeline

View File

@ -119,6 +119,7 @@ ext.libs = [
markwon : [ markwon : [
'core' : "io.noties.markwon:core:$markwon", 'core' : "io.noties.markwon:core:$markwon",
'extLatex' : "io.noties.markwon:ext-latex:$markwon", 'extLatex' : "io.noties.markwon:ext-latex:$markwon",
'imageGlide' : "io.noties.markwon:image-glide:$markwon",
'inlineParser' : "io.noties.markwon:inline-parser:$markwon", 'inlineParser' : "io.noties.markwon:inline-parser:$markwon",
'html' : "io.noties.markwon:html:$markwon" 'html' : "io.noties.markwon:html:$markwon"
], ],

View File

@ -183,6 +183,7 @@ dependencies {
} }
implementation libs.markwon.core implementation libs.markwon.core
implementation libs.markwon.extLatex implementation libs.markwon.extLatex
implementation libs.markwon.imageGlide
implementation libs.markwon.inlineParser implementation libs.markwon.inlineParser
implementation libs.markwon.html implementation libs.markwon.html
implementation 'com.googlecode.htmlcompressor:htmlcompressor:1.5.2' implementation 'com.googlecode.htmlcompressor:htmlcompressor:1.5.2'

View File

@ -27,8 +27,13 @@ package im.vector.app.features.html
import android.content.Context import android.content.Context
import android.content.res.Resources import android.content.res.Resources
import android.graphics.drawable.Drawable
import android.text.Spannable import android.text.Spannable
import androidx.core.text.toSpannable 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.resources.ColorProvider
import im.vector.app.core.utils.DimensionConverter import im.vector.app.core.utils.DimensionConverter
import im.vector.app.features.settings.VectorPreferences 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.JLatexMathPlugin
import io.noties.markwon.ext.latex.JLatexMathTheme import io.noties.markwon.ext.latex.JLatexMathTheme
import io.noties.markwon.html.HtmlPlugin 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.EntityInlineProcessor
import io.noties.markwon.inlineparser.HtmlInlineProcessor import io.noties.markwon.inlineparser.HtmlInlineProcessor
import io.noties.markwon.inlineparser.MarkwonInlineParser import io.noties.markwon.inlineparser.MarkwonInlineParser
import io.noties.markwon.inlineparser.MarkwonInlineParserPlugin import io.noties.markwon.inlineparser.MarkwonInlineParserPlugin
import org.commonmark.node.Node import org.commonmark.node.Node
import org.commonmark.parser.Parser import org.commonmark.parser.Parser
import org.matrix.android.sdk.api.MatrixUrls.isMxcUrl
import timber.log.Timber import timber.log.Timber
import javax.inject.Inject import javax.inject.Inject
import javax.inject.Singleton import javax.inject.Singleton
@ -53,7 +61,8 @@ import javax.inject.Singleton
class EventHtmlRenderer @Inject constructor( class EventHtmlRenderer @Inject constructor(
htmlConfigure: MatrixHtmlPluginConfigure, htmlConfigure: MatrixHtmlPluginConfigure,
context: Context, context: Context,
vectorPreferences: VectorPreferences vectorPreferences: VectorPreferences,
private val activeSessionHolder: ActiveSessionHolder
) { ) {
interface PostProcessor { interface PostProcessor {
@ -62,6 +71,23 @@ class EventHtmlRenderer @Inject constructor(
private val builder = Markwon.builder(context) private val builder = Markwon.builder(context)
.usePlugin(HtmlPlugin.create(htmlConfigure)) .usePlugin(HtmlPlugin.create(htmlConfigure))
.usePlugin(GlideImagesPlugin.create(object : GlideImagesPlugin.GlideStore {
override fun load(drawable: AsyncDrawable): RequestBuilder<Drawable> {
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()) { private val markwon = if (vectorPreferences.latexMathsIsEnabled()) {
// If latex maths is enabled in app preferences, refomat it so Markwon recognises it // If latex maths is enabled in app preferences, refomat it so Markwon recognises it