mirror of https://github.com/tasks/tasks
parent
40245eb4b9
commit
430dc04747
@ -0,0 +1,15 @@
|
|||||||
|
package org.tasks.markdown
|
||||||
|
|
||||||
|
import android.text.Editable
|
||||||
|
import android.widget.EditText
|
||||||
|
import android.widget.TextView
|
||||||
|
|
||||||
|
interface Markdown {
|
||||||
|
fun textWatcher(editText: EditText): ((Editable?) -> Unit)?
|
||||||
|
|
||||||
|
val enabled: Boolean
|
||||||
|
|
||||||
|
fun setMarkdown(tv: TextView, markdown: String?)
|
||||||
|
|
||||||
|
fun toMarkdown(markdown: String?): CharSequence?
|
||||||
|
}
|
||||||
@ -0,0 +1,17 @@
|
|||||||
|
package org.tasks.markdown
|
||||||
|
|
||||||
|
import android.text.Editable
|
||||||
|
import android.widget.EditText
|
||||||
|
import android.widget.TextView
|
||||||
|
|
||||||
|
class MarkdownDisabled : Markdown {
|
||||||
|
override fun textWatcher(editText: EditText): ((Editable?) -> Unit)? = null
|
||||||
|
|
||||||
|
override val enabled = false
|
||||||
|
|
||||||
|
override fun setMarkdown(tv: TextView, markdown: String?) {
|
||||||
|
tv.text = markdown
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun toMarkdown(markdown: String?) = markdown
|
||||||
|
}
|
||||||
@ -0,0 +1,21 @@
|
|||||||
|
package org.tasks.markdown
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
|
import dagger.hilt.android.qualifiers.ApplicationContext
|
||||||
|
import org.tasks.R
|
||||||
|
import org.tasks.preferences.Preferences
|
||||||
|
import javax.inject.Inject
|
||||||
|
|
||||||
|
class MarkdownProvider @Inject constructor(
|
||||||
|
@ApplicationContext private val context: Context,
|
||||||
|
private val preferences: Preferences
|
||||||
|
){
|
||||||
|
fun markdown(linkify: Int) = markdown(preferences.getBoolean(linkify, false))
|
||||||
|
|
||||||
|
fun markdown(linkify: Boolean = false) =
|
||||||
|
if (preferences.getBoolean(R.string.p_markdown, false)) {
|
||||||
|
Markwon(context, linkify)
|
||||||
|
} else {
|
||||||
|
MarkdownDisabled()
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,52 @@
|
|||||||
|
package org.tasks.markdown
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
|
import android.text.util.Linkify.*
|
||||||
|
import android.widget.EditText
|
||||||
|
import android.widget.TextView
|
||||||
|
import io.noties.markwon.editor.MarkwonEditor
|
||||||
|
import io.noties.markwon.editor.MarkwonEditorTextWatcher
|
||||||
|
import io.noties.markwon.ext.strikethrough.StrikethroughPlugin
|
||||||
|
import io.noties.markwon.ext.tables.TablePlugin
|
||||||
|
import io.noties.markwon.ext.tasklist.TaskListPlugin
|
||||||
|
import io.noties.markwon.linkify.LinkifyPlugin
|
||||||
|
import java.util.concurrent.Executors
|
||||||
|
|
||||||
|
class Markwon(context: Context, linkify: Boolean) : Markdown {
|
||||||
|
private val markwon: io.noties.markwon.Markwon
|
||||||
|
|
||||||
|
override fun textWatcher(editText: EditText) =
|
||||||
|
MarkwonEditorTextWatcher.withPreRender(
|
||||||
|
MarkwonEditor.create(markwon), Executors.newCachedThreadPool(), editText
|
||||||
|
)::afterTextChanged
|
||||||
|
|
||||||
|
override val enabled = true
|
||||||
|
|
||||||
|
override fun setMarkdown(tv: TextView, markdown: String?) {
|
||||||
|
if (markdown?.isNotBlank() == true) {
|
||||||
|
markwon.setMarkdown(tv, markdown)
|
||||||
|
} else {
|
||||||
|
tv.text = markdown
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun toMarkdown(markdown: String?) = markdown?.let { markwon.toMarkdown(it) }
|
||||||
|
|
||||||
|
init {
|
||||||
|
val builder = io.noties.markwon.Markwon
|
||||||
|
.builder(context)
|
||||||
|
.usePlugins(
|
||||||
|
listOf(
|
||||||
|
TaskListPlugin.create(context),
|
||||||
|
TablePlugin.create(context),
|
||||||
|
StrikethroughPlugin.create()
|
||||||
|
)
|
||||||
|
)
|
||||||
|
if (linkify) {
|
||||||
|
builder.usePlugin(
|
||||||
|
LinkifyPlugin.create(WEB_URLS or EMAIL_ADDRESSES or PHONE_NUMBERS, true)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
markwon = builder.build()
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue