Sample, add heading handler
This commit is contained in:
parent
3ef41b1b81
commit
f47124a2ac
@ -64,7 +64,8 @@ public class EditorActivity extends ActivityWithMenuOptions {
|
|||||||
.add("multipleEditSpans", this::multiple_edit_spans)
|
.add("multipleEditSpans", this::multiple_edit_spans)
|
||||||
.add("multipleEditSpansPlugin", this::multiple_edit_spans_plugin)
|
.add("multipleEditSpansPlugin", this::multiple_edit_spans_plugin)
|
||||||
.add("pluginRequire", this::plugin_require)
|
.add("pluginRequire", this::plugin_require)
|
||||||
.add("pluginNoDefaults", this::plugin_no_defaults);
|
.add("pluginNoDefaults", this::plugin_no_defaults)
|
||||||
|
.add("heading", this::heading);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -317,6 +318,16 @@ public class EditorActivity extends ActivityWithMenuOptions {
|
|||||||
editor, Executors.newSingleThreadExecutor(), editText));
|
editor, Executors.newSingleThreadExecutor(), editText));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void heading() {
|
||||||
|
final Markwon markwon = Markwon.create(this);
|
||||||
|
final MarkwonEditor editor = MarkwonEditor.builder(markwon)
|
||||||
|
.useEditHandler(new HeadingEditHandler())
|
||||||
|
.build();
|
||||||
|
|
||||||
|
editText.addTextChangedListener(MarkwonEditorTextWatcher.withPreRender(
|
||||||
|
editor, Executors.newSingleThreadExecutor(), editText));
|
||||||
|
}
|
||||||
|
|
||||||
private void initBottomBar() {
|
private void initBottomBar() {
|
||||||
// all except block-quote wraps if have selection, or inserts at current cursor position
|
// all except block-quote wraps if have selection, or inserts at current cursor position
|
||||||
|
|
||||||
|
@ -0,0 +1,78 @@
|
|||||||
|
package io.noties.markwon.sample.editor;
|
||||||
|
|
||||||
|
import android.text.Editable;
|
||||||
|
import android.text.Spanned;
|
||||||
|
|
||||||
|
import androidx.annotation.NonNull;
|
||||||
|
|
||||||
|
import io.noties.markwon.Markwon;
|
||||||
|
import io.noties.markwon.core.MarkwonTheme;
|
||||||
|
import io.noties.markwon.core.spans.HeadingSpan;
|
||||||
|
import io.noties.markwon.editor.EditHandler;
|
||||||
|
import io.noties.markwon.editor.PersistedSpans;
|
||||||
|
|
||||||
|
public class HeadingEditHandler implements EditHandler<HeadingSpan> {
|
||||||
|
|
||||||
|
private MarkwonTheme theme;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void init(@NonNull Markwon markwon) {
|
||||||
|
this.theme = markwon.configuration().theme();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void configurePersistedSpans(@NonNull PersistedSpans.Builder builder) {
|
||||||
|
builder
|
||||||
|
.persistSpan(Head1.class, () -> new Head1(theme))
|
||||||
|
.persistSpan(Head2.class, () -> new Head2(theme));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void handleMarkdownSpan(
|
||||||
|
@NonNull PersistedSpans persistedSpans,
|
||||||
|
@NonNull Editable editable,
|
||||||
|
@NonNull String input,
|
||||||
|
@NonNull HeadingSpan span,
|
||||||
|
int spanStart,
|
||||||
|
int spanTextLength
|
||||||
|
) {
|
||||||
|
final Class<?> type;
|
||||||
|
switch (span.getLevel()) {
|
||||||
|
case 1: type = Head1.class; break;
|
||||||
|
case 2: type = Head2.class; break;
|
||||||
|
default:
|
||||||
|
type = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (type != null) {
|
||||||
|
final int index = input.indexOf('\n', spanStart + spanTextLength);
|
||||||
|
final int end = index < 0
|
||||||
|
? input.length()
|
||||||
|
: index;
|
||||||
|
editable.setSpan(
|
||||||
|
persistedSpans.get(type),
|
||||||
|
spanStart,
|
||||||
|
end,
|
||||||
|
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@NonNull
|
||||||
|
@Override
|
||||||
|
public Class<HeadingSpan> markdownSpanType() {
|
||||||
|
return HeadingSpan.class;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static class Head1 extends HeadingSpan {
|
||||||
|
Head1(@NonNull MarkwonTheme theme) {
|
||||||
|
super(theme, 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static class Head2 extends HeadingSpan {
|
||||||
|
Head2(@NonNull MarkwonTheme theme) {
|
||||||
|
super(theme, 2);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user