diff options
author | Stefan Niedermann <info@niedermann.it> | 2020-11-29 13:09:19 +0300 |
---|---|---|
committer | Stefan Niedermann <info@niedermann.it> | 2020-11-29 13:09:19 +0300 |
commit | 9cc0a064ac497d3f48150fb85befd5842cbd462b (patch) | |
tree | f48bda5128b8c6db5c2ddd17be46fcdc701d8d41 | |
parent | 2882125ce8b7a1e51b0389805b74f1954d968e0c (diff) |
Improve Markdown implementation
Signed-off-by: Stefan Niedermann <info@niedermann.it>
5 files changed, 38 insertions, 66 deletions
diff --git a/app/src/main/res/layout/fragment_card_edit_tab_details.xml b/app/src/main/res/layout/fragment_card_edit_tab_details.xml index f4f8489e0..58df34fd3 100644 --- a/app/src/main/res/layout/fragment_card_edit_tab_details.xml +++ b/app/src/main/res/layout/fragment_card_edit_tab_details.xml @@ -172,6 +172,7 @@ android:textSize="@dimen/font_size_description" /> <it.niedermann.android.markdown.MarkdownViewerImpl + android:visibility="gone" android:id="@+id/descriptionViewer" android:layout_width="match_parent" android:layout_height="wrap_content" diff --git a/markdown/src/main/java/it/niedermann/android/markdown/AbstractMarkdownEditor.java b/markdown/src/main/java/it/niedermann/android/markdown/AbstractMarkdownEditor.java deleted file mode 100644 index 53520154f..000000000 --- a/markdown/src/main/java/it/niedermann/android/markdown/AbstractMarkdownEditor.java +++ /dev/null @@ -1,53 +0,0 @@ -package it.niedermann.android.markdown; - -import android.content.Context; -import android.util.AttributeSet; -import android.view.View; -import android.widget.FrameLayout; - -import androidx.annotation.NonNull; -import androidx.annotation.Nullable; -import androidx.annotation.RestrictTo; -import androidx.lifecycle.LiveData; - -import java.util.Map; - -@RestrictTo(value = RestrictTo.Scope.LIBRARY) -public abstract class AbstractMarkdownEditor<T extends View & MarkdownEditor> extends FrameLayout implements MarkdownEditor { - private final MarkdownEditor editor; - - - public AbstractMarkdownEditor(@NonNull Context context, T impl) { - this(context, null, impl); - } - - public AbstractMarkdownEditor(@NonNull Context context, @Nullable AttributeSet attrs, T impl) { - this(context, attrs, 0, impl); - } - - public AbstractMarkdownEditor(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr, T impl) { - super(context, attrs, defStyleAttr); - this.editor = impl; - addView(impl); - } - - @Override - public void setMarkdownString(CharSequence text) { - editor.setMarkdownString(text); - } - - @Override - public LiveData<CharSequence> getMarkdownString() { - return editor.getMarkdownString(); - } - - @Override - public void setEnabled(boolean enabled) { - editor.setEnabled(enabled); - } - - @Override - public void setMarkdownString(CharSequence text, @NonNull Map<String, String> mentions) { - editor.setMarkdownString(text, mentions); - } -} diff --git a/markdown/src/main/java/it/niedermann/android/markdown/MarkdownEditorImpl.java b/markdown/src/main/java/it/niedermann/android/markdown/MarkdownEditorImpl.java index 10c7eee53..6e4e10484 100644 --- a/markdown/src/main/java/it/niedermann/android/markdown/MarkdownEditorImpl.java +++ b/markdown/src/main/java/it/niedermann/android/markdown/MarkdownEditorImpl.java @@ -8,17 +8,16 @@ import androidx.annotation.Nullable; import it.niedermann.android.markdown.markwon.MarkwonMarkdownEditor; -public class MarkdownEditorImpl extends AbstractMarkdownEditor<MarkwonMarkdownEditor> { - +public class MarkdownEditorImpl extends MarkwonMarkdownEditor { public MarkdownEditorImpl(@NonNull Context context) { - super(context, new MarkwonMarkdownEditor(context)); + super(context); } public MarkdownEditorImpl(@NonNull Context context, @Nullable AttributeSet attrs) { - super(context, attrs, new MarkwonMarkdownEditor(context, attrs)); + super(context, attrs); } public MarkdownEditorImpl(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) { - super(context, attrs, defStyleAttr, new MarkwonMarkdownEditor(context, attrs, defStyleAttr)); + super(context, attrs, defStyleAttr); } } diff --git a/markdown/src/main/java/it/niedermann/android/markdown/MarkdownViewerImpl.java b/markdown/src/main/java/it/niedermann/android/markdown/MarkdownViewerImpl.java index 534161384..ae67d93d1 100644 --- a/markdown/src/main/java/it/niedermann/android/markdown/MarkdownViewerImpl.java +++ b/markdown/src/main/java/it/niedermann/android/markdown/MarkdownViewerImpl.java @@ -8,17 +8,17 @@ import androidx.annotation.Nullable; import it.niedermann.android.markdown.markwon.MarkwonMarkdownViewer; -public class MarkdownViewerImpl extends AbstractMarkdownEditor<MarkwonMarkdownViewer> { +public class MarkdownViewerImpl extends MarkwonMarkdownViewer { public MarkdownViewerImpl(@NonNull Context context) { - super(context, new MarkwonMarkdownViewer(context)); + super(context); } public MarkdownViewerImpl(@NonNull Context context, @Nullable AttributeSet attrs) { - super(context, attrs, new MarkwonMarkdownViewer(context, attrs)); + super(context, attrs); } public MarkdownViewerImpl(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) { - super(context, attrs, defStyleAttr, new MarkwonMarkdownViewer(context, attrs, defStyleAttr)); + super(context, attrs, defStyleAttr); } } diff --git a/markdown/src/main/java/it/niedermann/android/markdown/markwon/handler/HeadingEditHandler.java b/markdown/src/main/java/it/niedermann/android/markdown/markwon/handler/HeadingEditHandler.java index 33cdd5b23..491ce505b 100644 --- a/markdown/src/main/java/it/niedermann/android/markdown/markwon/handler/HeadingEditHandler.java +++ b/markdown/src/main/java/it/niedermann/android/markdown/markwon/handler/HeadingEditHandler.java @@ -38,7 +38,7 @@ public class HeadingEditHandler implements EditHandler<HeadingSpan> { @NonNull HeadingSpan span, int spanStart, int spanTextLength) { - HeadingSpan newSpan; + final HeadingSpan newSpan; switch (span.getLevel()) { case 1: @@ -63,15 +63,40 @@ public class HeadingEditHandler implements EditHandler<HeadingSpan> { return; } + final int newStart = getNewSpanStart(input, spanStart); + final int newEnd = findEnd(input, newStart, newSpan.getLevel()); editable.setSpan( newSpan, - spanStart, - spanStart + spanTextLength + newSpan.getLevel() + 1, + newStart, + newEnd, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE ); } + private int findEnd(String input, int searchFrom, int spanLevel) { + int end = searchFrom + spanLevel; + final int strLength = input.length(); + while (end < strLength - 1) { + end++; + if (input.charAt(end) == '\n') { + break; + } + } + return end + 1; + } + + private int getNewSpanStart(String input, int spanStart) { + int start = spanStart; + + while (start >= 0 && input.charAt(start) != '\n') { + start--; + } + start += 1; + + return start; + } + @NonNull @Override @@ -114,4 +139,4 @@ public class HeadingEditHandler implements EditHandler<HeadingSpan> { super(theme, 6); } } -} +}
\ No newline at end of file |