Sample, add task list toggle
This commit is contained in:
parent
7af0ead3a3
commit
8d483fe49d
@ -44,14 +44,6 @@ import ru.noties.jlatexmath.JLatexMathDrawable;
|
|||||||
*/
|
*/
|
||||||
public class JLatexMathPlugin extends AbstractMarkwonPlugin {
|
public class JLatexMathPlugin extends AbstractMarkwonPlugin {
|
||||||
|
|
||||||
/**
|
|
||||||
* @since 4.0.0
|
|
||||||
*/
|
|
||||||
public interface BackgroundProvider {
|
|
||||||
@NonNull
|
|
||||||
Drawable provide();
|
|
||||||
}
|
|
||||||
|
|
||||||
public interface BuilderConfigure {
|
public interface BuilderConfigure {
|
||||||
void configureBuilder(@NonNull Builder builder);
|
void configureBuilder(@NonNull Builder builder);
|
||||||
}
|
}
|
||||||
@ -83,7 +75,7 @@ public class JLatexMathPlugin extends AbstractMarkwonPlugin {
|
|||||||
private final float textSize;
|
private final float textSize;
|
||||||
|
|
||||||
// @since 4.0.0
|
// @since 4.0.0
|
||||||
private final BackgroundProvider backgroundProvider;
|
private final JLatexMathTheme.BackgroundProvider backgroundProvider;
|
||||||
|
|
||||||
@JLatexMathDrawable.Align
|
@JLatexMathDrawable.Align
|
||||||
private final int align;
|
private final int align;
|
||||||
@ -231,7 +223,7 @@ public class JLatexMathPlugin extends AbstractMarkwonPlugin {
|
|||||||
private final float textSize;
|
private final float textSize;
|
||||||
|
|
||||||
// @since 4.0.0
|
// @since 4.0.0
|
||||||
private BackgroundProvider backgroundProvider;
|
private JLatexMathTheme.BackgroundProvider backgroundProvider;
|
||||||
|
|
||||||
@JLatexMathDrawable.Align
|
@JLatexMathDrawable.Align
|
||||||
private int align = JLatexMathDrawable.ALIGN_CENTER;
|
private int align = JLatexMathDrawable.ALIGN_CENTER;
|
||||||
@ -252,7 +244,7 @@ public class JLatexMathPlugin extends AbstractMarkwonPlugin {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@NonNull
|
@NonNull
|
||||||
public Builder backgroundProvider(@NonNull BackgroundProvider backgroundProvider) {
|
public Builder backgroundProvider(@NonNull JLatexMathTheme.BackgroundProvider backgroundProvider) {
|
||||||
this.backgroundProvider = backgroundProvider;
|
this.backgroundProvider = backgroundProvider;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
@ -342,7 +334,7 @@ public class JLatexMathPlugin extends AbstractMarkwonPlugin {
|
|||||||
private void execute() {
|
private void execute() {
|
||||||
|
|
||||||
// @since 4.0.1 (background provider can be null)
|
// @since 4.0.1 (background provider can be null)
|
||||||
final BackgroundProvider backgroundProvider = config.backgroundProvider;
|
final JLatexMathTheme.BackgroundProvider backgroundProvider = config.backgroundProvider;
|
||||||
|
|
||||||
final JLatexMathDrawable jLatexMathDrawable;
|
final JLatexMathDrawable jLatexMathDrawable;
|
||||||
|
|
||||||
|
@ -1,28 +1,130 @@
|
|||||||
package io.noties.markwon.ext.latex;
|
package io.noties.markwon.ext.latex;
|
||||||
|
|
||||||
import android.graphics.Rect;
|
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
|
* @since 4.3.0-SNAPSHOT
|
||||||
*/
|
*/
|
||||||
public class JLatexMathTheme {
|
public abstract class JLatexMathTheme {
|
||||||
|
|
||||||
|
@NonNull
|
||||||
|
public static JLatexMathTheme create(@Px float textSize) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@NonNull
|
||||||
|
public static JLatexMathTheme builer() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Moved from {@link JLatexMathPlugin} in {@code 4.3.0-SNAPSHOT} version
|
||||||
|
*
|
||||||
|
* @since 4.0.0
|
||||||
|
*/
|
||||||
|
public interface BackgroundProvider {
|
||||||
|
@NonNull
|
||||||
|
Drawable provide();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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 textSize;
|
||||||
private float inlineTextSize;
|
private float inlineTextSize;
|
||||||
private float blockTextSize;
|
private float blockTextSize;
|
||||||
|
|
||||||
// TODO: move to a class
|
private BackgroundProvider backgroundProvider;
|
||||||
private JLatexMathPlugin.BackgroundProvider backgroundProvider;
|
private BackgroundProvider inlineBackgroundProvider;
|
||||||
private JLatexMathPlugin.BackgroundProvider inlineBackgroundProvider;
|
private BackgroundProvider blockBackgroundProvider;
|
||||||
private JLatexMathPlugin.BackgroundProvider blockBackgroundProvider;
|
|
||||||
|
|
||||||
private boolean blockFitCanvas;
|
private boolean blockFitCanvas;
|
||||||
// horizontal alignment (when there is additional horizontal space)
|
// horizontal alignment (when there is additional horizontal space)
|
||||||
private int blockAlign;
|
private int blockAlign;
|
||||||
|
|
||||||
private Rect padding;
|
private Padding padding;
|
||||||
private Rect inlinePadding;
|
private Padding inlinePadding;
|
||||||
private Rect blockPadding;
|
private Padding blockPadding;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -35,6 +35,7 @@
|
|||||||
|
|
||||||
<activity android:name=".inlineparser.InlineParserActivity" />
|
<activity android:name=".inlineparser.InlineParserActivity" />
|
||||||
<activity android:name=".htmldetails.HtmlDetailsActivity" />
|
<activity android:name=".htmldetails.HtmlDetailsActivity" />
|
||||||
|
<activity android:name=".tasklist.TaskListActivity" />
|
||||||
|
|
||||||
</application>
|
</application>
|
||||||
|
|
||||||
|
@ -30,6 +30,7 @@ import io.noties.markwon.sample.latex.LatexActivity;
|
|||||||
import io.noties.markwon.sample.precomputed.PrecomputedActivity;
|
import io.noties.markwon.sample.precomputed.PrecomputedActivity;
|
||||||
import io.noties.markwon.sample.recycler.RecyclerActivity;
|
import io.noties.markwon.sample.recycler.RecyclerActivity;
|
||||||
import io.noties.markwon.sample.simpleext.SimpleExtActivity;
|
import io.noties.markwon.sample.simpleext.SimpleExtActivity;
|
||||||
|
import io.noties.markwon.sample.tasklist.TaskListActivity;
|
||||||
|
|
||||||
public class MainActivity extends Activity {
|
public class MainActivity extends Activity {
|
||||||
|
|
||||||
@ -132,6 +133,10 @@ public class MainActivity extends Activity {
|
|||||||
activity = HtmlDetailsActivity.class;
|
activity = HtmlDetailsActivity.class;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
case TASK_LIST:
|
||||||
|
activity = TaskListActivity.class;
|
||||||
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
throw new IllegalStateException("No Activity is associated with sample-item: " + item);
|
throw new IllegalStateException("No Activity is associated with sample-item: " + item);
|
||||||
}
|
}
|
||||||
|
@ -27,7 +27,9 @@ public enum Sample {
|
|||||||
|
|
||||||
INLINE_PARSER(R.string.sample_inline_parser),
|
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;
|
private final int textResId;
|
||||||
|
|
||||||
|
@ -15,6 +15,7 @@ import org.commonmark.node.Node;
|
|||||||
import io.noties.markwon.AbstractMarkwonPlugin;
|
import io.noties.markwon.AbstractMarkwonPlugin;
|
||||||
import io.noties.markwon.Markwon;
|
import io.noties.markwon.Markwon;
|
||||||
import io.noties.markwon.ext.latex.JLatexMathPlugin;
|
import io.noties.markwon.ext.latex.JLatexMathPlugin;
|
||||||
|
import io.noties.markwon.ext.latex.JLatexMathTheme;
|
||||||
import io.noties.markwon.sample.R;
|
import io.noties.markwon.sample.R;
|
||||||
import io.noties.markwon.utils.DumpNodes;
|
import io.noties.markwon.utils.DumpNodes;
|
||||||
import ru.noties.jlatexmath.JLatexMathDrawable;
|
import ru.noties.jlatexmath.JLatexMathDrawable;
|
||||||
@ -56,7 +57,7 @@ public class LatexActivity extends Activity {
|
|||||||
@Override
|
@Override
|
||||||
public void configureBuilder(@NonNull JLatexMathPlugin.Builder builder) {
|
public void configureBuilder(@NonNull JLatexMathPlugin.Builder builder) {
|
||||||
builder
|
builder
|
||||||
.backgroundProvider(new JLatexMathPlugin.BackgroundProvider() {
|
.backgroundProvider(new JLatexMathTheme.BackgroundProvider() {
|
||||||
@NonNull
|
@NonNull
|
||||||
@Override
|
@Override
|
||||||
public Drawable provide() {
|
public Drawable provide() {
|
||||||
|
@ -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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -31,4 +31,6 @@
|
|||||||
|
|
||||||
<string name="sample_html_details"># \# HTML <details> tag\n\n<details> tag parsed and rendered</string>
|
<string name="sample_html_details"># \# HTML <details> tag\n\n<details> tag parsed and rendered</string>
|
||||||
|
|
||||||
|
<string name="sample_task_list"># \# TaskList\n\nUsage of TaskListPlugin</string>
|
||||||
|
|
||||||
</resources>
|
</resources>
|
Loading…
x
Reference in New Issue
Block a user