Skip to content

Commit

Permalink
Small fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
harshad1 committed Dec 11, 2023
1 parent ee5ccbd commit c2cb1db
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import net.gsantner.markor.ApplicationObject;
import net.gsantner.markor.R;
import net.gsantner.markor.format.FormatRegistry;
import net.gsantner.markor.frontend.textview.TextViewUtils;
import net.gsantner.markor.model.AppSettings;
import net.gsantner.markor.model.Document;
import net.gsantner.markor.util.MarkorContextUtils;
Expand Down Expand Up @@ -289,7 +288,7 @@ public void setDocumentTitle(final String title) {
}

public void showTextEditor(final Document document, final Integer lineNumber, final Boolean startPreview) {
GsFragmentBase currentFragment = getCurrentVisibleFragment();
final GsFragmentBase currentFragment = getCurrentVisibleFragment();

final boolean sameDocumentRequested = (
currentFragment instanceof DocumentEditAndViewFragment &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
import net.gsantner.markor.util.MarkorContextUtils;
import net.gsantner.opoc.format.GsTextUtils;
import net.gsantner.opoc.util.GsCollectionUtils;
import net.gsantner.opoc.util.GsContextUtils;
import net.gsantner.opoc.util.GsFileUtils;
import net.gsantner.opoc.wrapper.GsCallback;

Expand Down Expand Up @@ -89,7 +90,7 @@ public abstract class ActionButtonBase {
public ActionButtonBase(@NonNull final Context context, final Document document) {
_document = document;
_appSettings = ApplicationObject.settings();
_buttonHorizontalMargin = (int) (_appSettings.getEditorActionButtonItemPadding() * context.getResources().getDisplayMetrics().density);
_buttonHorizontalMargin = GsContextUtils.instance.convertDpToPx(context, _appSettings.getEditorActionButtonItemPadding());
_indent = _appSettings.getDocumentIndentSize(_document != null ? _document.getPath() : null);
}

Expand Down Expand Up @@ -172,7 +173,7 @@ private List<ActionItem> getActionList() {
new ActionItem(R.string.abid_common_time, R.drawable.ic_access_time_black_24dp, R.string.date_and_time),

new ActionItem(R.string.abid_common_web_jump_to_very_top_or_bottom, R.drawable.ic_vertical_align_center_black_24dp, R.string.jump_to_bottom).setDisplayMode(ActionItem.DisplayMode.VIEW),
new ActionItem(R.string.abid_common_view_file_in_other_app, R.drawable.ic_baseline_open_in_new_24, R.string.open_with).setDisplayMode(ActionItem.DisplayMode.ANY),
new ActionItem(R.string.abid_common_view_file_in_other_app, R.drawable.ic_baseline_open_in_new_24, R.string.open_with).setDisplayMode(ActionItem.DisplayMode.VIEW),
new ActionItem(R.string.abid_common_rotate_screen, R.drawable.ic_rotate_left_black_24dp, R.string.rotate).setDisplayMode(ActionItem.DisplayMode.ANY)
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,18 @@ public class MarkdownActionButtons extends ActionButtonBase {

public static final String LINE_PREFIX = "^(>\\s|#{1,6}\\s|\\s*[-*+](?:\\s\\[[ xX]\\])?\\s|\\s*\\d+[.)]\\s)?";

// Patterns used for surrounding entire lines
// ----------------------------------------------------------------------------
// TODO - make these more intelligent. Should work with combined delimiters.
// Goup 1: Prefix, Group 2: Pre-space, Group 3: Open delim, Group 4: Text, Group 5: Close delim, Group 6: Post-space
public static final Pattern LINE_BOLD = Pattern.compile(LINE_PREFIX + "(\\s*)(\\*\\*)(\\S.*\\S)(\\3)(\\s*)$");
public static final Pattern LINE_ITALIC = Pattern.compile(LINE_PREFIX + "(\\s*)(_)(\\S.*\\S)(\\3)(\\s*)$");
public static final Pattern LINE_STRIKEOUT = Pattern.compile(LINE_PREFIX + "(\\s*)(~~)(\\S.*\\S)(\\3)(\\s*)$");

// Group 1: Prefix, Group 2: Pre-space, Group 3: Text, Group 4: Post-space
public static final Pattern LINE_NONE = Pattern.compile(LINE_PREFIX + "(\\s*)(.*)(\\s*)$");
// ----------------------------------------------------------------------------

public static final Pattern CHECKED_LIST_LINE = Pattern.compile("^(\\s*)(([-*+])\\s\\[([xX ])\\]\\s)");

public MarkdownActionButtons(@NonNull Context context, Document document) {
super(context, document);
Expand All @@ -67,23 +71,23 @@ public List<ActionItem> getFormatActionList() {
return Arrays.asList(
new ActionItem(R.string.abid_common_checkbox_list, R.drawable.ic_check_box_black_24dp, R.string.check_list),
new ActionItem(R.string.abid_common_unordered_list_char, R.drawable.ic_list_black_24dp, R.string.unordered_list),
new ActionItem(R.string.abid_common_ordered_list_number, R.drawable.ic_format_list_numbered_black_24dp, R.string.ordered_list),
new ActionItem(R.string.abid_markdown_bold, R.drawable.ic_format_bold_black_24dp, R.string.bold),
new ActionItem(R.string.abid_markdown_italic, R.drawable.ic_format_italic_black_24dp, R.string.italic),
new ActionItem(R.string.abid_markdown_strikeout, R.drawable.ic_format_strikethrough_black_24dp, R.string.strikeout),
new ActionItem(R.string.abid_common_insert_link, R.drawable.ic_link_black_24dp, R.string.insert_link),
new ActionItem(R.string.abid_common_insert_image, R.drawable.ic_image_black_24dp, R.string.insert_image),
new ActionItem(R.string.abid_common_insert_audio, R.drawable.ic_keyboard_voice_black_24dp, R.string.audio),
new ActionItem(R.string.abid_markdown_code_inline, R.drawable.ic_code_black_24dp, R.string.inline_code),
new ActionItem(R.string.abid_common_ordered_list_number, R.drawable.ic_format_list_numbered_black_24dp, R.string.ordered_list),
new ActionItem(R.string.abid_markdown_table_insert_columns, R.drawable.ic_view_module_black_24dp, R.string.table),
new ActionItem(R.string.abid_markdown_quote, R.drawable.ic_format_quote_black_24dp, R.string.quote),
new ActionItem(R.string.abid_markdown_h1, R.drawable.format_header_1, R.string.heading_1),
new ActionItem(R.string.abid_markdown_h2, R.drawable.format_header_2, R.string.heading_2),
new ActionItem(R.string.abid_markdown_h3, R.drawable.format_header_3, R.string.heading_3),
new ActionItem(R.string.abid_markdown_horizontal_line, R.drawable.ic_more_horiz_black_24dp, R.string.horizontal_line),
new ActionItem(R.string.abid_markdown_strikeout, R.drawable.ic_format_strikethrough_black_24dp, R.string.strikeout),
new ActionItem(R.string.abid_common_accordion, R.drawable.ic_arrow_drop_down_black_24dp, R.string.accordion),
new ActionItem(R.string.abid_common_indent, R.drawable.ic_format_indent_increase_black_24dp, R.string.indent),
new ActionItem(R.string.abid_common_deindent, R.drawable.ic_format_indent_decrease_black_24dp, R.string.deindent)
new ActionItem(R.string.abid_common_deindent, R.drawable.ic_format_indent_decrease_black_24dp, R.string.deindent),
new ActionItem(R.string.abid_common_accordion, R.drawable.ic_arrow_drop_down_black_24dp, R.string.accordion)
);
}

Expand Down Expand Up @@ -201,6 +205,12 @@ public boolean onActionLongClick(final @StringRes int action) {
runLineSurroundAction(LINE_STRIKEOUT, "~~");
return true;
}
case R.string.abid_common_checkbox_list: {
MarkorDialogFactory.showDocumentChecklistDialog(
getActivity(), _hlEditor.getText(), CHECKED_LIST_LINE, 4, "xX", " ",
pos -> TextViewUtils.setSelectionAndShow(_hlEditor, pos));
return true;
}
default: {
return runCommonLongPressAction(action);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@ public List<ActionItem> getFormatActionList() {
new ActionItem(R.string.abid_todotxt_add_context, R.drawable.gs_email_sign_black_24dp, R.string.add_context),
new ActionItem(R.string.abid_todotxt_add_project, R.drawable.ic_new_label_black_24dp, R.string.add_project),
new ActionItem(R.string.abid_todotxt_priority, R.drawable.ic_star_border_black_24dp, R.string.priority),
new ActionItem(R.string.abid_common_attach_something, R.drawable.ic_attach_file_black_24dp, R.string.attach),
new ActionItem(R.string.abid_todotxt_archive_done_tasks, R.drawable.ic_archive_black_24dp, R.string.archive_completed_tasks),
new ActionItem(R.string.abid_todotxt_current_date, R.drawable.ic_date_range_black_24dp, R.string.current_date),
new ActionItem(R.string.abid_todotxt_sort_todo, R.drawable.ic_sort_by_alpha_black_24dp, R.string.sort_by),
new ActionItem(R.string.abid_todotxt_current_date, R.drawable.ic_date_range_black_24dp, R.string.current_date)
new ActionItem(R.string.abid_common_attach_something, R.drawable.ic_attach_file_black_24dp, R.string.attach)
);
}

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

import androidx.annotation.Nullable;
import androidx.annotation.StringRes;
import androidx.core.content.ContextCompat;

Expand Down Expand Up @@ -59,11 +60,15 @@
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class MarkorDialogFactory {
public static AppSettings as() {
Expand Down Expand Up @@ -506,6 +511,80 @@ public static void showColorSelectionModeDialog(Activity activity, GsCallback.a1
GsSearchOrCustomTextDialog.showMultiChoiceDialogWithSearchFilterUI(activity, dopt);
}

/**
* Shows all checkboxes in the file in a muti select dialog.
* The multi select can be used to check or uncheck them.
*
* Long pressing a line will jump to the line in the file.
*/
public static void showDocumentChecklistDialog(
final Activity activity,
final Editable text,
final Pattern checkPattern,
final int checkGroup,
final String checkedChars,
final String uncheckedChars,
final @Nullable GsCallback.a1<Integer> showCallback
) {
final List<String> lines = new ArrayList<>(); // String of each line
final Set<Integer> checked = new HashSet<>(); // Whether the line is checked
final List<Integer> indices = new ArrayList<>(); // Indices of the check char in the line
final Matcher matcher = checkPattern.matcher("");
GsTextUtils.forEachline(text, (index, start, end) -> {
final String line = text.subSequence(start, end).toString();
matcher.reset(line);
if (matcher.find()) {
final int cs = matcher.start(checkGroup);
final char c = line.charAt(cs);
if (checkedChars.indexOf(c) >= 0) {
checked.add(lines.size());
}
lines.add(line);
indices.add(cs + start);
}
});

final DialogOptions dopt = new DialogOptions();
baseConf(activity, dopt);
dopt.isMultiSelectEnabled = true;
dopt.data = lines;
dopt.preSelected = checked;
dopt.titleText = R.string.check_list;
dopt.isLongPressSelectEnabled = true;

final String check = Character.toString(checkedChars.charAt(0));
final String uncheck = Character.toString(uncheckedChars.charAt(0));
final TextViewUtils.ChunkedEditable chunked = TextViewUtils.ChunkedEditable.wrap(text);

dopt.positionCallback = (result) -> {
// Result has the indices of the checker lines which are selected
for (final int i: GsCollectionUtils.setDiff(checked, result)) {
final int cs = indices.get(i);
chunked.replace(cs, cs + 1, uncheck);
}

for (final int i: GsCollectionUtils.setDiff(result, checked)) {
final int cs = indices.get(i);
chunked.replace(cs, cs + 1, check);
}

chunked.applyChanges();
};

if (showCallback != null) {
dopt.callback = (line) -> {
final int index = lines.indexOf(line);
if (index >= 0) {
final int cs = indices.get(index);
final int end = TextViewUtils.getLineEnd(text, cs);
showCallback.callback(end);
}
};
}

GsSearchOrCustomTextDialog.showMultiChoiceDialogWithSearchFilterUI(activity, dopt);
}

// Insert items
public static void showInsertItemsDialog(
final Activity activity,
Expand Down Expand Up @@ -609,7 +688,6 @@ public static void showHeadlineDialog(
final List<Integer> filtered = GsCollectionUtils.indices(headings, h -> !disabled.contains(h.level));
final List<String> data = GsCollectionUtils.map(filtered, i -> headings.get(i).str);


final DialogOptions dopt = new DialogOptions();
baseConf(activity, dopt);
dopt.data = data;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ public UnOrderedOrCheckListLine(CharSequence text, int position, FormatPatterns
if (isUnorderedOrCheckList) {
groupStart = lineStart + matcher.start(FULL_ITEM_PREFIX_GROUP);
groupEnd = lineStart + matcher.end(FULL_ITEM_PREFIX_GROUP);
String emptyCheckboxContent = " ";
final String emptyCheckboxContent = " ";
newItemPrefix = isChecklist ? matcher.group(CHECKBOX_PREFIX_LEFT_GROUP) + emptyCheckboxContent + matcher.group(CHECKBOX_PREFIX_RIGHT_GROUP)
: matcher.group(FULL_ITEM_PREFIX_GROUP);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ public boolean isEditorStartEditingInCenter() {
}

public int getEditorActionButtonItemPadding() {
return getInt(R.string.pref_key__editor_textaction_bar_item_padding, 8);
return getInt(R.string.pref_key__editor_textaction_bar_item_padding, 6);
}

public boolean isDisableSpellingRedUnderline() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.widget.AppCompatEditText;
import androidx.appcompat.widget.TooltipCompat;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.core.widget.TextViewCompat;

import net.gsantner.markor.R;
Expand All @@ -58,6 +57,7 @@
import net.gsantner.opoc.wrapper.GsTextWatcherAdapter;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
Expand Down Expand Up @@ -97,7 +97,7 @@ public static class DialogOptions {
public int searchInputType = InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS;
public GsCallback.a1<Spannable> highlighter = null;
public String extraFilter = null;
public List<Integer> preSelected = null;
public Collection<Integer> preSelected = null;
public GsCallback.a1<AlertDialog> neutralButtonCallback = null;
public GsCallback.b2<CharSequence, CharSequence> searchFunction = GsSearchOrCustomTextDialog::standardSearch;
public GsCallback.a1<DialogInterface> dismissCallback = null;
Expand Down

0 comments on commit c2cb1db

Please sign in to comment.