Skip to content

Commit

Permalink
Additional change to how we do action buttons
Browse files Browse the repository at this point in the history
  • Loading branch information
harshad1 committed Jan 21, 2024
1 parent a233904 commit 5d54567
Show file tree
Hide file tree
Showing 8 changed files with 199 additions and 114 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.Editable;
import android.view.KeyEvent;

import androidx.annotation.NonNull;
Expand Down Expand Up @@ -171,10 +172,25 @@ public boolean onActionClick(final @StringRes int action) {
* @param delim - Delimiter to surround text with
*/
private void runLineSurroundAction(final Pattern pattern, final String delim) {
final int[] sel = TextViewUtils.getSelection(_hlEditor);
final String lineBefore = sel[0] == sel[1] ? TextViewUtils.getSelectedLines(_hlEditor, sel[0]) : null;

runRegexReplaceAction(
new ReplacePattern(pattern, "$1$2$4$6"),
new ReplacePattern(LINE_NONE, "$1$2" + delim + "$3" + delim + "$4")
);

// This logic sets the cursor to the inside of the delimiters if the delimiters were empty
if (lineBefore != null) {
final String lineAfter = TextViewUtils.getSelectedLines(_hlEditor, sel[0]);
final String pair = delim + delim;
if (lineAfter.length() - lineBefore.length() == pair.length() && lineAfter.trim().endsWith(pair)) {
final Editable text = _hlEditor.getText();
final int end = TextViewUtils.getLineEnd(text, sel[0]);
final int ns = TextViewUtils.getLastNonWhitespace(text, end) - delim.length();
_hlEditor.setSelection(ns);
}
}
}

@SuppressLint("NonConstantResourceId")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import net.gsantner.markor.R;
import net.gsantner.markor.format.FormatRegistry;
import net.gsantner.markor.format.markdown.MarkdownSyntaxHighlighter;
import net.gsantner.markor.format.todotxt.TodoTxtTask;
import net.gsantner.markor.frontend.filebrowser.MarkorFileBrowserFactory;
import net.gsantner.markor.frontend.filesearch.FileSearchDialog;
import net.gsantner.markor.frontend.filesearch.FileSearchEngine;
Expand All @@ -36,7 +37,6 @@
import net.gsantner.markor.util.MarkorContextUtils;
import net.gsantner.opoc.format.GsTextUtils;
import net.gsantner.opoc.frontend.GsAudioRecordOmDialog;
import net.gsantner.opoc.frontend.filebrowser.GsFileBrowserListAdapter;
import net.gsantner.opoc.frontend.filebrowser.GsFileBrowserOptions;
import net.gsantner.opoc.util.GsFileUtils;
import net.gsantner.opoc.wrapper.GsCallback;
Expand Down Expand Up @@ -66,6 +66,8 @@ private static String getLinkFormat(final int textFormatId) {
return "{{LINK|TITLE}}";
} else if (textFormatId == FormatRegistry.FORMAT_ASCIIDOC) {
return "link:LINK[TITLE]";
} else if (textFormatId == FormatRegistry.FORMAT_TODOTXT) {
return "TITLE:LINK";
} else {
return "<a href=\"LINK\">TITLE</a>";
}
Expand Down Expand Up @@ -113,7 +115,7 @@ public static void showInsertImageOrLinkDialog(
} else {
m = null;
}
if (m != null && m.find()) {
if (m != null && m.find() && sel[0] >= m.start() && sel[1] <= m.end()) {
inputPathName.setText(m.group(1));
inputPathUrl.setText((m.group(2)));
sel[0] = m.start() + lineSel[0];
Expand Down Expand Up @@ -226,6 +228,33 @@ private static GsCallback.b2<Context, File> getFilterForAction(final InsertType
}
}

public static String formatLink(String title, String path, final int textFormatId) {
return formatLink(title, path, textFormatId, InsertType.LINK_DIALOG);
}

private static String formatLink(String title, String path, final int textFormatId, final InsertType action) {
title = title.trim().replace(")", "\\)");
path = path.trim().replace(")", "\\)")
// Workaround for parser - cannot deal with spaces and have other entities problems
.replace(" ", "%20")
// Disable space encoding for Jekyll
.replace("{{%20site.baseurl%20}}", "{{ site.baseurl }}");

String newText = getTemplateForAction(action, textFormatId)
.replace("TITLE", title)
.replace("LINK", path);

if (textFormatId == FormatRegistry.FORMAT_WIKITEXT && newText.endsWith("|]]")) {
newText = newText.replaceFirst("\\|]]$", "]]");
}

if (textFormatId == FormatRegistry.FORMAT_TODOTXT) {
newText = newText.replaceAll("\\n", " ");
}

return newText;
}

private static void insertItem(
final InsertType action,
final int textFormatId,
Expand All @@ -251,20 +280,7 @@ private static void insertItem(
return;
}

title = title.trim().replace(")", "\\)");
path = path.trim().replace(")", "\\)")
// Workaround for parser - cannot deal with spaces and have other entities problems
.replace(" ", "%20")
// Disable space encoding for Jekyll
.replace("{{%20site.baseurl%20}}", "{{ site.baseurl }}");

String newText = getTemplateForAction(action, textFormatId)
.replace("TITLE", title)
.replace("LINK", path);

if (textFormatId == FormatRegistry.FORMAT_WIKITEXT && newText.endsWith("|]]")) {
newText = newText.replaceFirst("\\|]]$", "]]");
}
final String newText = formatLink(title, path, textFormatId, action);

if (!newText.equals(text.subSequence(sel[0], sel[1]).toString())) {
text.replace(sel[0], sel[1], newText);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public static void showSearchFilesDialog(
FileSearchDialog.showDialog(activity, searchOptions -> {
searchOptions.rootSearchDir = searchDir;
FileSearchEngine.queueFileSearch(activity, searchOptions, searchResults ->
FileSearchResultSelectorDialog.showDialog(activity, searchResults, () -> callback));
FileSearchResultSelectorDialog.showDialog(activity, searchResults, callback));
});
}
}
Expand Down Expand Up @@ -542,6 +542,7 @@ public static void showDocumentChecklistDialog(
lines.add(line);
indices.add(cs + start);
}
return true;
});

final DialogOptions dopt = new DialogOptions();
Expand Down Expand Up @@ -732,6 +733,7 @@ public static void showHeadlineDialog(
if (level > 0) {
headings.add(new Heading(level, text.subSequence(start, end), line));
}
return true;
});

// List of levels present in text
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,12 @@ public static int getLastNonWhitespace(final CharSequence s) {
}

public static int getLastNonWhitespace(final CharSequence s, final int end) {
for (int i = Math.min(s.length() - 1, end); i >= 0; i--) {
char c = s.charAt(i);
if (c != ' ' && c != '\t') {
return i;
if (s != null) {
for (int i = Math.min(s.length() - 1, end); i >= 0; i--) {
char c = s.charAt(i);
if (c != ' ' && c != '\t') {
return i;
}
}
}
return -1;
Expand Down
6 changes: 4 additions & 2 deletions app/src/main/java/net/gsantner/opoc/format/GsTextUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -320,12 +320,14 @@ public static List<Integer> findChar(final CharSequence text, final char c, fina
return posns;
}

public static void forEachline(final CharSequence text, GsCallback.a3<Integer, Integer, Integer> callback) {
public static void forEachline(final CharSequence text, GsCallback.b3<Integer, Integer, Integer> callback) {
final List<Integer> ends = findChar(text, '\n');
int start = 0, i = 0;
for (; i < ends.size(); i++) {
final int end = ends.get(i);
callback.callback(i, start, end);
if (!callback.callback(i, start, end)) {
break;
};
start = end + 1;
}
callback.callback(i, start, text.length());
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/string-not_translatable.xml
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ work. If not, see <https://creativecommons.org/publicdomain/zero/1.0/>.
<string name="pref_key__share_into__clipboard" translatable="false">pref_key__share_into__clipboard</string>
<string name="pref_key__share_into__reshare" translatable="false">pref_key__share_into__reshare</string>
<string name="pref_key__share_into__calendar_event" translatable="false">pref_key__share_into__calendar_event</string>
<string name="pref_key__attach_as_link" translatable="false">pref_key__attach_as_link</string>
<string name="pref_key__more_info__settings" translatable="false">pref_key__more_info__settings</string>
<string name="pref_key__more_info__source_code" translatable="false">pref_key__more_info__source_code</string>
<string name="pref_key__more_info__project_license" translatable="false">pref_key__more_info__project_license</string>
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/res/xml/prefactions_share_into.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
android:layout_height="match_parent"
android:title="@string/share_to_arrow">

<CheckBoxPreference
android:icon="@drawable/ic_link_black_24dp"
android:key="@string/pref_key__attach_as_link"
android:checked="true"
android:title="@string/insert_link" />
<Preference
android:icon="@drawable/ic_lightning_black_24dp"
android:key="@string/pref_key__share_into__quicknote"
Expand Down

0 comments on commit 5d54567

Please sign in to comment.