Sample, add task list toggle

This commit is contained in:
Dimitry Ivanov 2020-02-26 09:51:33 +03:00
parent 7af0ead3a3
commit 8d483fe49d
8 changed files with 245 additions and 29 deletions

View File

@ -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;

View File

@ -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 <strong>inline LaTeX</strong>
* @see #blockTexxtSize()
*/
@Px
public abstract float inlineTextSize();
/**
* @return text size in pixels for <strong>block LaTeX</strong>
* @see #inlineTextSize()
*/
@Px
public abstract float blockTexxtSize();
@Nullable
public abstract BackgroundProvider inlineBackgroundProvider();
@Nullable
public abstract BackgroundProvider blockBackgroundProvider();
/**
* @return boolean if <strong>block LaTeX</strong> must fit the width of canvas
*/
public abstract boolean blockFitCanvas();
/**
* @return horizontal alignment of <strong>block LaTeX</strong> 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;
}
}

View File

@ -35,6 +35,7 @@
<activity android:name=".inlineparser.InlineParserActivity" />
<activity android:name=".htmldetails.HtmlDetailsActivity" />
<activity android:name=".tasklist.TaskListActivity" />
</application>

View File

@ -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);
}

View File

@ -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;

View File

@ -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() {

View File

@ -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
}
}
}

View File

@ -31,4 +31,6 @@
<string name="sample_html_details"># \# HTML &lt;details> tag\n\n&lt;details> tag parsed and rendered</string>
<string name="sample_task_list"># \# TaskList\n\nUsage of TaskListPlugin</string>
</resources>