Minor MarkwonImagePlugin updates
This commit is contained in:
parent
72e7605fce
commit
7f225b3973
@ -17,13 +17,12 @@ import com.caverock.androidsvg.SVG
|
|||||||
import com.caverock.androidsvg.SVGParseException
|
import com.caverock.androidsvg.SVGParseException
|
||||||
import com.topjohnwu.magisk.core.ResMgr
|
import com.topjohnwu.magisk.core.ResMgr
|
||||||
import com.topjohnwu.superuser.internal.WaitRunnable
|
import com.topjohnwu.superuser.internal.WaitRunnable
|
||||||
import io.noties.markwon.*
|
import io.noties.markwon.AbstractMarkwonPlugin
|
||||||
|
import io.noties.markwon.MarkwonSpansFactory
|
||||||
import io.noties.markwon.image.*
|
import io.noties.markwon.image.*
|
||||||
import io.noties.markwon.image.data.DataUriSchemeHandler
|
import io.noties.markwon.image.data.DataUriSchemeHandler
|
||||||
import io.noties.markwon.image.network.OkHttpNetworkSchemeHandler
|
import io.noties.markwon.image.network.OkHttpNetworkSchemeHandler
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.*
|
||||||
import kotlinx.coroutines.launch
|
|
||||||
import kotlinx.coroutines.runBlocking
|
|
||||||
import okhttp3.OkHttpClient
|
import okhttp3.OkHttpClient
|
||||||
import org.commonmark.node.Image
|
import org.commonmark.node.Image
|
||||||
import timber.log.Timber
|
import timber.log.Timber
|
||||||
@ -43,7 +42,11 @@ import java.io.InputStream
|
|||||||
class MarkwonImagePlugin(okHttp: OkHttpClient) : AbstractMarkwonPlugin() {
|
class MarkwonImagePlugin(okHttp: OkHttpClient) : AbstractMarkwonPlugin() {
|
||||||
|
|
||||||
override fun configureSpansFactory(builder: MarkwonSpansFactory.Builder) {
|
override fun configureSpansFactory(builder: MarkwonSpansFactory.Builder) {
|
||||||
builder.setFactory(Image::class.java, ImageSpanFactory())
|
builder.setFactory(Image::class.java) { _, props ->
|
||||||
|
val dest = ImageProps.DESTINATION.require(props)
|
||||||
|
val size = ImageProps.IMAGE_SIZE.get(props)
|
||||||
|
ImageSpan(dest, size)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@WorkerThread
|
@WorkerThread
|
||||||
@ -62,9 +65,10 @@ class MarkwonImagePlugin(okHttp: OkHttpClient) : AbstractMarkwonPlugin() {
|
|||||||
}
|
}
|
||||||
tv.post(wr)
|
tv.post(wr)
|
||||||
|
|
||||||
runBlocking(Dispatchers.IO) {
|
runBlocking {
|
||||||
// Download and decode all drawables in parallel
|
// Wait for drawable to be set
|
||||||
spans.forEach { launch { it.load() } }
|
spans.forEach { it.await() }
|
||||||
|
// Wait for canvasWidth to be set
|
||||||
wr.waitUntilDone()
|
wr.waitUntilDone()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -132,27 +136,25 @@ class MarkwonImagePlugin(okHttp: OkHttpClient) : AbstractMarkwonPlugin() {
|
|||||||
return drawable
|
return drawable
|
||||||
}
|
}
|
||||||
|
|
||||||
inner class ImageSpanFactory : SpanFactory {
|
|
||||||
override fun getSpans(configuration: MarkwonConfiguration, props: RenderProps): Any? {
|
|
||||||
val dest = ImageProps.DESTINATION.require(props)
|
|
||||||
val size = ImageProps.IMAGE_SIZE.get(props)
|
|
||||||
return ImageSpan(dest, size)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
inner class ImageSpan(
|
inner class ImageSpan(
|
||||||
private val dest: String,
|
dest: String,
|
||||||
private val size: ImageSize?
|
private val size: ImageSize?
|
||||||
) : DynamicDrawableSpan(ALIGN_BOTTOM) {
|
) : DynamicDrawableSpan(ALIGN_BOTTOM) {
|
||||||
|
|
||||||
var canvasWidth = 0
|
var canvasWidth = 0
|
||||||
private var measured = false
|
private var measured = false
|
||||||
private lateinit var draw: Drawable
|
private lateinit var draw: Drawable
|
||||||
|
private val job: Job
|
||||||
|
|
||||||
fun load() {
|
init {
|
||||||
draw = loadDrawable(dest) ?: ShapeDrawable()
|
// Asynchronously download/decode images in the background
|
||||||
|
job = GlobalScope.launch(Dispatchers.IO) {
|
||||||
|
draw = loadDrawable(dest) ?: ShapeDrawable()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
suspend fun await() = job.join()
|
||||||
|
|
||||||
override fun getDrawable() = draw
|
override fun getDrawable() = draw
|
||||||
|
|
||||||
private fun defaultBounds(): Rect {
|
private fun defaultBounds(): Rect {
|
||||||
|
Loading…
Reference in New Issue
Block a user