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

Support inline images in the timeline #5877

Merged
merged 7 commits into from
Oct 4, 2022
Merged
Show file tree
Hide file tree
Changes from 4 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
1 change: 1 addition & 0 deletions changelog.d/351.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Render inline images in the timeline
1 change: 1 addition & 0 deletions dependencies.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,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"
],
Expand Down
1 change: 1 addition & 0 deletions vector/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,7 @@ dependencies {
implementation 'me.gujun.android:span:1.7'
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'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,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
Expand All @@ -30,8 +35,11 @@ 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.MarkwonInlineParserPlugin
import org.commonmark.node.Node
import org.matrix.android.sdk.api.MatrixUrls.isMxcUrl
import timber.log.Timber
import javax.inject.Inject
import javax.inject.Singleton
Expand All @@ -40,15 +48,35 @@ import javax.inject.Singleton
class EventHtmlRenderer @Inject constructor(
htmlConfigure: MatrixHtmlPluginConfigure,
context: Context,
vectorPreferences: VectorPreferences
vectorPreferences: VectorPreferences,
private val activeSessionHolder: ActiveSessionHolder
) {

interface PostProcessor {
fun afterRender(renderedText: Spannable)
}

private val builder = Markwon.builder(context)
.usePlugin(HtmlPlugin.create(htmlConfigure))
.usePlugins(listOf(
HtmlPlugin.create(htmlConfigure),
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()) {
builder
Expand Down