Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1261,9 +1261,13 @@ public void setViewModeVisibility(boolean show, final boolean animate) {
@JavascriptInterface
public void webViewJavascriptCallback(final String[] jsArgs) {
final String[] args = (jsArgs == null || jsArgs.length == 0 || jsArgs[0] == null) ? new String[0] : jsArgs;
final String type = args.length == 0 || TextUtils.isEmpty(args[0]) ? "" : args[0];
if (type.equalsIgnoreCase("toast") && args.length == 2) {
Toast.makeText(getActivity(), args[1], Toast.LENGTH_SHORT).show();
final Activity activity = getActivity();
if (activity != null) {
activity.runOnUiThread(() -> {
if (_format != null) {
_format.getActions().onWebViewJavascriptCallback(args);
}
});
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import android.webkit.WebView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.annotation.DrawableRes;
import androidx.annotation.NonNull;
Expand Down Expand Up @@ -105,6 +106,13 @@ public boolean onActionLongClick(final @StringRes int action) {
return runCommonLongPressAction(action);
}

// Override to implement custom callbacks from view-mode JavaScript
public void onWebViewJavascriptCallback(final String[] args) {
if (args.length == 2 && "toast".equalsIgnoreCase(args[0]) && _activity != null) {
Toast.makeText(_activity, args[1], Toast.LENGTH_SHORT).show();
}
}

private TextSearchViewHolder _textSearchViewHolder;

public ActionButtonBase initTextSearch(DocumentEditAndViewFragment fragment) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,43 @@ public MarkdownActionButtons(@NonNull Context context, Document document) {
super(context, document);
}

@Override
public void onWebViewJavascriptCallback(final String[] args) {
if (isMarkdownCheckboxAction(args)) {
runViewModeCheckboxAction(args);
return;
}
super.onWebViewJavascriptCallback(args);
}

private boolean isMarkdownCheckboxAction(final String[] args) {
return args.length == 3 &&
"markdown-checkbox".equals(args[0]) &&
("true".equals(args[2]) || "false".equals(args[2])) &&
_hlEditor != null;
}

private void runViewModeCheckboxAction(final String[] args) {
final int sourceIndex = GsTextUtils.tryParseInt(args[1], -1);
final Editable text = _hlEditor.getText();
if (sourceIndex <= 0 ||
sourceIndex >= text.length() - 1 ||
text.charAt(sourceIndex - 1) != '[' ||
text.charAt(sourceIndex + 1) != ']') {
return;
}

final char current = text.charAt(sourceIndex);
if (current != ' ' && current != 'x' && current != 'X') {
return;
}

final char replacement = "true".equals(args[2]) ? 'x' : ' ';
if (current != replacement) {
_hlEditor.withAutoFormatDisabled(() -> text.replace(sourceIndex, sourceIndex + 1, String.valueOf(replacement)));
}
}

@Override
protected @StringRes
int getFormatActionsKey() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.vladsch.flexmark.ext.footnotes.FootnoteExtension;
import com.vladsch.flexmark.ext.gfm.strikethrough.StrikethroughSubscriptExtension;
import com.vladsch.flexmark.ext.gfm.tasklist.TaskListExtension;
import com.vladsch.flexmark.ext.gfm.tasklist.TaskListItem;
import com.vladsch.flexmark.ext.gitlab.GitLabExtension;
import com.vladsch.flexmark.ext.ins.InsExtension;
import com.vladsch.flexmark.ext.jekyll.front.matter.JekyllFrontMatterExtension;
Expand Down Expand Up @@ -153,8 +154,6 @@ public class MarkdownTextConverter extends TextConverterBase {
//########################
// private static final String toDashChars = " -_"; // See HtmlRenderer.HEADER_ID_GENERATOR_TO_DASH_CHARS.getFrom(document)
private static final Pattern linkPattern = Pattern.compile("\\[(.*?)\\]\\((.*?)(\\s+\".*\")?\\)");


//########################
//## Methods
//########################
Expand All @@ -165,6 +164,9 @@ public String convertMarkup(String markup, Context context, boolean lightMode, b
final MutableDataSet options = new MutableDataSet();

options.set(Parser.EXTENSIONS, flexmarkExtensions);
final String sourceMarkup = markup;
final Document sourceDocument = flexmarkParser.parse(sourceMarkup);
final List<Integer> checkboxSourceIndices = getCheckboxSourceIndices(sourceDocument);

options.set(Parser.SPACE_IN_LINK_URLS, true); // Allow links like [this](some filename with spaces.md)

Expand All @@ -179,6 +181,13 @@ public String convertMarkup(String markup, Context context, boolean lightMode, b
final String checkedCheckbox = "<input type=checkbox class=task-list-item-checkbox checked=checked style='accent-color: #F04B4B;' />";
options.set(TaskListExtension.ITEM_NOT_DONE_MARKER, checkedCheckbox.replace("checked=checked", ""))
.set(TaskListExtension.ITEM_DONE_MARKER, checkedCheckbox);
if (!checkboxSourceIndices.isEmpty()) {
onLoadJs += "var checkboxSourceIndices=" + checkboxSourceIndices + ";"
+ "var boxes=document.querySelectorAll('input.task-list-item-checkbox');"
+ "for(var i=0;i<boxes.length&&i<checkboxSourceIndices.length;i++){"
+ "boxes[i].markorSourceIndex=checkboxSourceIndices[i];"
+ "boxes[i].addEventListener('change',function(){Android.webViewJavascriptCallback(['markdown-checkbox',String(this.markorSourceIndex),this.checked?'true':'false']);});}";
}

// GFM table parsing
options.set(TablesExtension.WITH_CAPTION, false)
Expand Down Expand Up @@ -318,7 +327,7 @@ public String convertMarkup(String markup, Context context, boolean lightMode, b

////////////
// Markup parsing - afterwards = HTML
Document document = flexmarkParser.parse(markup);
Document document = sourceMarkup.equals(markup) ? sourceDocument : flexmarkParser.parse(markup);
converted = fmaText + flexmarkRenderer.withOptions(options).render(document);

// After render changes: Fixes for Footnotes (converter creates footnote + <br> + ref#(click) --> remove line break)
Expand Down Expand Up @@ -375,6 +384,21 @@ private String escapeSpacesInLink(final String markup) {
return sb.toString();
}

private static List<Integer> getCheckboxSourceIndices(final Document document) {
final List<Integer> indices = new ArrayList<>();
collectCheckboxSourceIndices(document, indices);
return indices;
}

private static void collectCheckboxSourceIndices(final Node parent, final List<Integer> indices) {
for (Node node = parent.getFirstChild(); node != null; node = node.getNext()) {
if (node instanceof TaskListItem) {
indices.add(((TaskListItem) node).getMarkerSuffix().getStartOffset() + 1);
}
collectCheckboxSourceIndices(node, indices);
}
}

private String getViewHlPrismIncludes(final String theme) {

return CSS_PREFIX + "prism/themes/prism" + theme + ".min.css" + CSS_POSTFIX +
Expand Down Expand Up @@ -490,4 +514,4 @@ public static HtmlRenderer.HtmlRendererExtension create() {
return new LineNumberIdExtension();
}
}
}
}
Loading