From 8d483fe49dc2211dfcb4e4c27860ce7c2714cdd6 Mon Sep 17 00:00:00 2001 From: Dimitry Ivanov Date: Wed, 26 Feb 2020 09:51:33 +0300 Subject: [PATCH] Sample, add task list toggle --- .../markwon/ext/latex/JLatexMathPlugin.java | 18 +-- .../markwon/ext/latex/JLatexMathTheme.java | 130 ++++++++++++++++-- sample/src/main/AndroidManifest.xml | 1 + .../noties/markwon/sample/MainActivity.java | 5 + .../java/io/noties/markwon/sample/Sample.java | 4 +- .../markwon/sample/latex/LatexActivity.java | 3 +- .../sample/tasklist/TaskListActivity.java | 111 +++++++++++++++ .../src/main/res/values/strings-samples.xml | 2 + 8 files changed, 245 insertions(+), 29 deletions(-) create mode 100644 sample/src/main/java/io/noties/markwon/sample/tasklist/TaskListActivity.java diff --git a/markwon-ext-latex/src/main/java/io/noties/markwon/ext/latex/JLatexMathPlugin.java b/markwon-ext-latex/src/main/java/io/noties/markwon/ext/latex/JLatexMathPlugin.java index 68308f6c..3f85b9f7 100644 --- a/markwon-ext-latex/src/main/java/io/noties/markwon/ext/latex/JLatexMathPlugin.java +++ b/markwon-ext-latex/src/main/java/io/noties/markwon/ext/latex/JLatexMathPlugin.java @@ -42,15 +42,7 @@ import ru.noties.jlatexmath.JLatexMathDrawable; /** * @since 3.0.0 */ -public class JLatexMathPlugin extends AbstractMarkwonPlugin { - - /** - * @since 4.0.0 - */ - public interface BackgroundProvider { - @NonNull - Drawable provide(); - } +public class JLatexMathPlugin extends AbstractMarkwonPlugin { public interface BuilderConfigure { void configureBuilder(@NonNull Builder builder); @@ -83,7 +75,7 @@ public class JLatexMathPlugin extends AbstractMarkwonPlugin { private final float textSize; // @since 4.0.0 - private final BackgroundProvider backgroundProvider; + private final JLatexMathTheme.BackgroundProvider backgroundProvider; @JLatexMathDrawable.Align private final int align; @@ -231,7 +223,7 @@ public class JLatexMathPlugin extends AbstractMarkwonPlugin { private final float textSize; // @since 4.0.0 - private BackgroundProvider backgroundProvider; + private JLatexMathTheme.BackgroundProvider backgroundProvider; @JLatexMathDrawable.Align private int align = JLatexMathDrawable.ALIGN_CENTER; @@ -252,7 +244,7 @@ public class JLatexMathPlugin extends AbstractMarkwonPlugin { } @NonNull - public Builder backgroundProvider(@NonNull BackgroundProvider backgroundProvider) { + public Builder backgroundProvider(@NonNull JLatexMathTheme.BackgroundProvider backgroundProvider) { this.backgroundProvider = backgroundProvider; return this; } @@ -342,7 +334,7 @@ public class JLatexMathPlugin extends AbstractMarkwonPlugin { private void execute() { // @since 4.0.1 (background provider can be null) - final BackgroundProvider backgroundProvider = config.backgroundProvider; + final JLatexMathTheme.BackgroundProvider backgroundProvider = config.backgroundProvider; final JLatexMathDrawable jLatexMathDrawable; diff --git a/markwon-ext-latex/src/main/java/io/noties/markwon/ext/latex/JLatexMathTheme.java b/markwon-ext-latex/src/main/java/io/noties/markwon/ext/latex/JLatexMathTheme.java index 956eb20f..04ea346c 100644 --- a/markwon-ext-latex/src/main/java/io/noties/markwon/ext/latex/JLatexMathTheme.java +++ b/markwon-ext-latex/src/main/java/io/noties/markwon/ext/latex/JLatexMathTheme.java @@ -1,28 +1,130 @@ package io.noties.markwon.ext.latex; import android.graphics.Rect; +import android.graphics.drawable.Drawable; + +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.annotation.Px; + +import ru.noties.jlatexmath.JLatexMathDrawable; /** * @since 4.3.0-SNAPSHOT */ -public class JLatexMathTheme { +public abstract class JLatexMathTheme { - private float textSize; - private float inlineTextSize; - private float blockTextSize; + @NonNull + public static JLatexMathTheme create(@Px float textSize) { + return null; + } - // TODO: move to a class - private JLatexMathPlugin.BackgroundProvider backgroundProvider; - private JLatexMathPlugin.BackgroundProvider inlineBackgroundProvider; - private JLatexMathPlugin.BackgroundProvider blockBackgroundProvider; + @NonNull + public static JLatexMathTheme builer() { + return null; + } - private boolean blockFitCanvas; - // horizontal alignment (when there is additional horizontal space) - private int blockAlign; + /** + * Moved from {@link JLatexMathPlugin} in {@code 4.3.0-SNAPSHOT} version + * + * @since 4.0.0 + */ + public interface BackgroundProvider { + @NonNull + Drawable provide(); + } - private Rect padding; - private Rect inlinePadding; - private Rect blockPadding; + /** + * Special immutable class to hold padding information + */ + public static class Padding { + public final int left; + public final int top; + public final int right; + public final int bottom; + + public Padding(int left, int top, int right, int bottom) { + this.left = left; + this.top = top; + this.right = right; + this.bottom = bottom; + } + + @Override + public String toString() { + return "Padding{" + + "left=" + left + + ", top=" + top + + ", right=" + right + + ", bottom=" + bottom + + '}'; + } + + @NonNull + public static Padding all(int value) { + return new Padding(value, value, value, value); + } + + @NonNull + public static Padding symmetric(int vertical, int horizontal) { + return new Padding(horizontal, vertical, horizontal, vertical); + } + } + + /** + * @return text size in pixels for inline LaTeX + * @see #blockTexxtSize() + */ + @Px + public abstract float inlineTextSize(); + + /** + * @return text size in pixels for block LaTeX + * @see #inlineTextSize() + */ + @Px + public abstract float blockTexxtSize(); + + @Nullable + public abstract BackgroundProvider inlineBackgroundProvider(); + + @Nullable + public abstract BackgroundProvider blockBackgroundProvider(); + + /** + * @return boolean if block LaTeX must fit the width of canvas + */ + public abstract boolean blockFitCanvas(); + + /** + * @return horizontal alignment of block LaTeX if {@link #blockFitCanvas()} + * is enabled (thus space for alignment is available) + */ + @JLatexMathDrawable.Align + public abstract int blockHorizontalAlignment(); + + @Nullable + public abstract Padding inlinePadding(); + + @Nullable + public abstract Padding blockPadding(); + public static class Builder { + private float textSize; + private float inlineTextSize; + private float blockTextSize; + + private BackgroundProvider backgroundProvider; + private BackgroundProvider inlineBackgroundProvider; + private BackgroundProvider blockBackgroundProvider; + + private boolean blockFitCanvas; + // horizontal alignment (when there is additional horizontal space) + private int blockAlign; + + private Padding padding; + private Padding inlinePadding; + private Padding blockPadding; + } } diff --git a/sample/src/main/AndroidManifest.xml b/sample/src/main/AndroidManifest.xml index 5e0ae714..0c02f47f 100644 --- a/sample/src/main/AndroidManifest.xml +++ b/sample/src/main/AndroidManifest.xml @@ -35,6 +35,7 @@ + diff --git a/sample/src/main/java/io/noties/markwon/sample/MainActivity.java b/sample/src/main/java/io/noties/markwon/sample/MainActivity.java index 4cdd6d73..a14a8183 100644 --- a/sample/src/main/java/io/noties/markwon/sample/MainActivity.java +++ b/sample/src/main/java/io/noties/markwon/sample/MainActivity.java @@ -30,6 +30,7 @@ import io.noties.markwon.sample.latex.LatexActivity; import io.noties.markwon.sample.precomputed.PrecomputedActivity; import io.noties.markwon.sample.recycler.RecyclerActivity; import io.noties.markwon.sample.simpleext.SimpleExtActivity; +import io.noties.markwon.sample.tasklist.TaskListActivity; public class MainActivity extends Activity { @@ -132,6 +133,10 @@ public class MainActivity extends Activity { activity = HtmlDetailsActivity.class; break; + case TASK_LIST: + activity = TaskListActivity.class; + break; + default: throw new IllegalStateException("No Activity is associated with sample-item: " + item); } diff --git a/sample/src/main/java/io/noties/markwon/sample/Sample.java b/sample/src/main/java/io/noties/markwon/sample/Sample.java index 36b13cd2..f243c0ec 100644 --- a/sample/src/main/java/io/noties/markwon/sample/Sample.java +++ b/sample/src/main/java/io/noties/markwon/sample/Sample.java @@ -27,7 +27,9 @@ public enum Sample { INLINE_PARSER(R.string.sample_inline_parser), - HTML_DETAILS(R.string.sample_html_details); + HTML_DETAILS(R.string.sample_html_details), + + TASK_LIST(R.string.sample_task_list); private final int textResId; diff --git a/sample/src/main/java/io/noties/markwon/sample/latex/LatexActivity.java b/sample/src/main/java/io/noties/markwon/sample/latex/LatexActivity.java index f7c382b3..3186c1cc 100644 --- a/sample/src/main/java/io/noties/markwon/sample/latex/LatexActivity.java +++ b/sample/src/main/java/io/noties/markwon/sample/latex/LatexActivity.java @@ -15,6 +15,7 @@ import org.commonmark.node.Node; import io.noties.markwon.AbstractMarkwonPlugin; import io.noties.markwon.Markwon; import io.noties.markwon.ext.latex.JLatexMathPlugin; +import io.noties.markwon.ext.latex.JLatexMathTheme; import io.noties.markwon.sample.R; import io.noties.markwon.utils.DumpNodes; import ru.noties.jlatexmath.JLatexMathDrawable; @@ -56,7 +57,7 @@ public class LatexActivity extends Activity { @Override public void configureBuilder(@NonNull JLatexMathPlugin.Builder builder) { builder - .backgroundProvider(new JLatexMathPlugin.BackgroundProvider() { + .backgroundProvider(new JLatexMathTheme.BackgroundProvider() { @NonNull @Override public Drawable provide() { diff --git a/sample/src/main/java/io/noties/markwon/sample/tasklist/TaskListActivity.java b/sample/src/main/java/io/noties/markwon/sample/tasklist/TaskListActivity.java new file mode 100644 index 00000000..dfbf59af --- /dev/null +++ b/sample/src/main/java/io/noties/markwon/sample/tasklist/TaskListActivity.java @@ -0,0 +1,111 @@ +package io.noties.markwon.sample.tasklist; + +import android.app.Activity; +import android.os.Bundle; +import android.text.Spanned; +import android.text.TextPaint; +import android.text.style.ClickableSpan; +import android.view.View; +import android.widget.TextView; + +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; + +import io.noties.debug.Debug; +import io.noties.markwon.AbstractMarkwonPlugin; +import io.noties.markwon.Markwon; +import io.noties.markwon.MarkwonSpansFactory; +import io.noties.markwon.SpanFactory; +import io.noties.markwon.ext.tasklist.TaskListItem; +import io.noties.markwon.ext.tasklist.TaskListPlugin; +import io.noties.markwon.ext.tasklist.TaskListSpan; +import io.noties.markwon.sample.R; + +public class TaskListActivity extends Activity { + + private TextView textView; + + @Override + protected void onCreate(@Nullable Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_text_view); + + textView = findViewById(R.id.text_view); + + mutate(); + } + + private void mutate() { + + final Markwon markwon = Markwon.builder(this) + .usePlugin(TaskListPlugin.create(this)) + .usePlugin(new AbstractMarkwonPlugin() { + @Override + public void configureSpansFactory(@NonNull MarkwonSpansFactory.Builder builder) { + // obtain origin task-list-factory + final SpanFactory origin = builder.getFactory(TaskListItem.class); + if (origin == null) { + return; + } + + builder.setFactory(TaskListItem.class, (configuration, props) -> { + // maybe it's better to validate the actual type here also + // and not force cast to task-list-span + final TaskListSpan span = (TaskListSpan) origin.getSpans(configuration, props); + if (span == null) { + return null; + } + + return new Object[]{ + span, + new TaskListToggleSpan(span) + }; + }); + } + }) + .build(); + + final String md = "" + + "- [ ] Not done here!\n" + + "- [x] and done\n" + + "- [X] and again!\n" + + "* [ ] **and** syntax _included_ `code`"; + + markwon.setMarkdown(textView, md); + } + + private static class TaskListToggleSpan extends ClickableSpan { + + private final TaskListSpan span; + + TaskListToggleSpan(@NonNull TaskListSpan span) { + this.span = span; + } + + @Override + public void onClick(@NonNull View widget) { + // toggle span (this is a mere visual change) + span.setDone(!span.isDone()); + // request visual update + widget.invalidate(); + + // it must be a TextView + final TextView textView = (TextView) widget; + // it must be spanned + final Spanned spanned = (Spanned) textView.getText(); + + // actual text of the span (this can be used along with the `span`) + final CharSequence task = spanned.subSequence( + spanned.getSpanStart(this), + spanned.getSpanEnd(this) + ); + + Debug.i("task done: %s, '%s'", span.isDone(), task); + } + + @Override + public void updateDrawState(@NonNull TextPaint ds) { + // no op, so text is not rendered as a link + } + } +} diff --git a/sample/src/main/res/values/strings-samples.xml b/sample/src/main/res/values/strings-samples.xml index d87585fd..f5a97644 100644 --- a/sample/src/main/res/values/strings-samples.xml +++ b/sample/src/main/res/values/strings-samples.xml @@ -31,4 +31,6 @@ # \# HTML <details> tag\n\n<details> tag parsed and rendered + # \# TaskList\n\nUsage of TaskListPlugin + \ No newline at end of file