From 273ecdd7cda306ac2aafc2a1af2ffea515d9a800 Mon Sep 17 00:00:00 2001 From: Dimitry Ivanov Date: Mon, 21 Dec 2020 16:11:43 +0300 Subject: [PATCH] Sample, image click --- app-sample/samples.json | 13 ++++ .../app/samples/image/ClickImageSample.kt | 73 +++++++++++++++++++ 2 files changed, 86 insertions(+) create mode 100644 app-sample/src/main/java/io/noties/markwon/app/samples/image/ClickImageSample.kt diff --git a/app-sample/samples.json b/app-sample/samples.json index eb206642..36eb05a1 100644 --- a/app-sample/samples.json +++ b/app-sample/samples.json @@ -1,4 +1,17 @@ [ + { + "javaClassName": "io.noties.markwon.app.samples.image.ClickImageSample", + "id": "20201221130230", + "title": "Click images", + "description": "Make _all_ images clickable (to open in a gallery, etc)", + "artifacts": [ + "IMAGE" + ], + "tags": [ + "image", + "rendering" + ] + }, { "javaClassName": "io.noties.markwon.app.samples.ChangeBulletSpanSample", "id": "20201208150530", diff --git a/app-sample/src/main/java/io/noties/markwon/app/samples/image/ClickImageSample.kt b/app-sample/src/main/java/io/noties/markwon/app/samples/image/ClickImageSample.kt new file mode 100644 index 00000000..e103dc5d --- /dev/null +++ b/app-sample/src/main/java/io/noties/markwon/app/samples/image/ClickImageSample.kt @@ -0,0 +1,73 @@ +package io.noties.markwon.app.samples.image + +import android.view.View +import io.noties.markwon.AbstractMarkwonPlugin +import io.noties.markwon.LinkResolver +import io.noties.markwon.Markwon +import io.noties.markwon.MarkwonConfiguration +import io.noties.markwon.MarkwonSpansFactory +import io.noties.markwon.app.readme.GithubImageDestinationProcessor +import io.noties.markwon.app.sample.Tags +import io.noties.markwon.app.sample.ui.MarkwonTextViewSample +import io.noties.markwon.app.utils.loadReadMe +import io.noties.markwon.core.spans.LinkSpan +import io.noties.markwon.image.ImageProps +import io.noties.markwon.image.ImagesPlugin +import io.noties.markwon.sample.annotations.MarkwonArtifact +import io.noties.markwon.sample.annotations.MarkwonSampleInfo +import org.commonmark.node.Image + +@MarkwonSampleInfo( + id = "20201221130230", + title = "Click images", + description = "Make _all_ images clickable (to open in a gallery, etc)", + artifacts = [MarkwonArtifact.IMAGE], + tags = [Tags.rendering, Tags.image] +) +class ClickImageSample : MarkwonTextViewSample() { + override fun render() { + + val md = loadReadMe(context) + + // please note that if an image is already inside a link, original link would be overriden + + val markwon = Markwon.builder(context) + .usePlugin(ImagesPlugin.create()) + .usePlugin(object : AbstractMarkwonPlugin() { + override fun configureConfiguration(builder: MarkwonConfiguration.Builder) { + builder.imageDestinationProcessor(GithubImageDestinationProcessor()) + } + }) + .usePlugin(object : AbstractMarkwonPlugin() { + override fun configureSpansFactory(builder: MarkwonSpansFactory.Builder) { + builder.appendFactory(Image::class.java) { configuration, props -> + + // this is the destination of image, you can additionally process it + val url = ImageProps.DESTINATION.require(props) + + LinkSpan( + configuration.theme(), + url, + ImageLinkResolver(configuration.linkResolver()) + ) + } + } + }) + .build() + + markwon.setMarkdown(textView, md) + } + + class ImageLinkResolver(val original: LinkResolver) : LinkResolver { + override fun resolve(view: View, link: String) { + // decide if you want to open gallery or anything else, + // here we just pass to original + if (false) { + // do your thing + } else { + // just use original + original.resolve(view, link) + } + } + } +} \ No newline at end of file