Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[pull] master from gsantner:master #209

Merged
merged 5 commits into from
Nov 16, 2023
Merged
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 @@ -81,14 +81,20 @@ public static void launch(Activity activity, File path, Boolean doPreview, Inten
}

public static void handleFileClick(Activity activity, File file, Integer lineNumber) {
if (activity != null && file != null) {
if (FormatRegistry.isFileSupported(file)) {
if (activity == null || file == null) {
return;
}

if (file.isDirectory()) {
if (file.canRead()) {
launch(activity, file, null, null, lineNumber);
} else if (GsFileUtils.getFilenameExtension(file).equals(".apk")) {
GsContextUtils.instance.requestApkInstallation(activity, file);
} else {
askUserIfWantsToOpenFileInThisApp(activity, file);
}
} else if (FormatRegistry.isFileSupported(file) && GsFileUtils.canCreate(file)) {
launch(activity, file, null, null, lineNumber);
} else if (GsFileUtils.getFilenameExtension(file).equals(".apk")) {
GsContextUtils.instance.requestApkInstallation(activity, file);
} else {
askUserIfWantsToOpenFileInThisApp(activity, file);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,12 @@ public class DocumentShareIntoFragment extends MarkorBaseFragment {
public static final String TEXT_TOKEN = "{{text}}";

public static DocumentShareIntoFragment newInstance(Intent intent) {
DocumentShareIntoFragment f = new DocumentShareIntoFragment();
Bundle args = new Bundle();
final DocumentShareIntoFragment f = new DocumentShareIntoFragment();
final Bundle args = new Bundle();

final String sharedText = formatLink(intent.getStringExtra(Intent.EXTRA_SUBJECT), intent.getStringExtra(Intent.EXTRA_TEXT));

Object intentFile = intent.getSerializableExtra(Document.EXTRA_PATH);
final Object intentFile = intent.getSerializableExtra(Document.EXTRA_PATH);
if (intentFile instanceof File && ((File) intentFile).isDirectory()) {
f.workingDir = (File) intentFile;
}
Expand Down Expand Up @@ -219,12 +219,12 @@ private void appendToExistingDocumentAndClose(final File file, final boolean sho
Toast.makeText(context, R.string.error_could_not_open_file, Toast.LENGTH_LONG).show();
}

_appSettings.addRecentDocument(file);
if (showEditor) {
showInDocumentActivity(document);
} else {
context.finish();
}
_appSettings.addRecentDocument(file);

context.finish();
}

private String formatShare(final String shared) {
Expand Down Expand Up @@ -264,6 +264,7 @@ private void showAppendDialog(int keyId) {
break;
}
}

MarkorFileBrowserFactory.showFileDialog(new GsFileBrowserOptions.SelectionListenerAdapter() {
@Override
public void onFsViewerConfig(GsFileBrowserOptions.Options dopt) {
Expand All @@ -280,23 +281,39 @@ public void onFsViewerSelected(String request, File file, final Integer lineNumb
}


private void createNewDocument() {
MarkorFileBrowserFactory.showFolderDialog(new GsFileBrowserOptions.SelectionListenerAdapter() {
private void createSelectNewDocument() {
MarkorFileBrowserFactory.showFileDialog(new GsFileBrowserOptions.SelectionListenerAdapter() {
GsFileBrowserOptions.Options _dopt = null;

@Override
public void onFsViewerConfig(GsFileBrowserOptions.Options dopt) {
dopt.rootFolder = (workingDir == null) ? _appSettings.getNotebookDirectory() : workingDir;
dopt.rootFolder = _appSettings.getNotebookDirectory();
dopt.startFolder = workingDir;
dopt.okButtonText = R.string.create_new_document;
dopt.okButtonEnable = true;
dopt.dismissAfterCallback = false;
_dopt = dopt;
}

@Override
public void onFsViewerSelected(String request, File dir, final Integer lineNumber) {
NewFileDialog dialog = NewFileDialog.newInstance(dir, false, (ok, f) -> {
if (ok && f.isFile()) {
appendToExistingDocumentAndClose(f, true);
}
});
dialog.show(getActivity().getSupportFragmentManager(), NewFileDialog.FRAGMENT_TAG);
public void onFsViewerSelected(final String request, final File sel, final Integer lineNumber) {
if (sel.isDirectory()) {
NewFileDialog.newInstance(sel, false, (ok, f) -> {
if (ok && f.isFile()) {
appendToExistingDocumentAndClose(f, true);
}
}).show(getChildFragmentManager(), NewFileDialog.FRAGMENT_TAG);
} else {
appendToExistingDocumentAndClose(sel, true);
}
}
}, getFragmentManager(), getActivity());

@Override
public void onFsViewerCancel(final String request) {
// Will cause the dialog to dismiss after this callback
_dopt.dismissAfterCallback = true;
}
}, getParentFragmentManager(), getActivity(), MarkorFileBrowserFactory.IsMimeText);
}

private void showInDocumentActivity(final Document document) {
Expand All @@ -321,14 +338,13 @@ public Boolean onPreferenceClicked(Preference preference, String key, int keyId)
close = true;
break;
}
case R.string.pref_key__share_into__create_document: {
createNewDocument();
case R.string.pref_key__select_create_document: {
createSelectNewDocument();
return true;
}
case R.string.pref_key__favourite_files:
case R.string.pref_key__popular_documents:
case R.string.pref_key__recent_documents:
case R.string.pref_key__share_into__existing_document: {
case R.string.pref_key__recent_documents: {
showAppendDialog(keyId);
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ public void onClickFab(final View view) {
if (f.isFile()) {
DocumentActivity.launch(MainActivity.this, f, false, null, null);
} else if (f.isDirectory()) {
_notebook.reloadCurrentFolder();
_notebook.getAdapter().showFile(f);
}
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,12 @@
import android.view.WindowManager;

import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.Fragment;

import net.gsantner.markor.ApplicationObject;
import net.gsantner.markor.R;
import net.gsantner.markor.model.AppSettings;
import net.gsantner.markor.util.MarkorContextUtils;
import net.gsantner.opoc.frontend.base.GsActivityBase;
import net.gsantner.opoc.util.GsCollectionUtils;

import java.util.List;

public abstract class MarkorBaseActivity extends GsActivityBase<AppSettings, MarkorContextUtils> {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,10 +235,26 @@ protected void onPreferenceChanged(final SharedPreferences prefs, final String k
@Override
@SuppressWarnings({"ConstantConditions", "ConstantIfStatement", "StatementWithEmptyBody"})
public Boolean onPreferenceClicked(Preference preference, String key, int keyResId) {
final FragmentManager fragManager = getActivity().getSupportFragmentManager();
switch (keyResId) {
case R.string.pref_key__snippet_directory_path: {
MarkorFileBrowserFactory.showFolderDialog(new GsFileBrowserOptions.SelectionListenerAdapter() {
@Override
public void onFsViewerSelected(String request, File file, final Integer lineNumber) {
_appSettings.setSnippetDirectory(file);
doUpdatePreferences();
}

@Override
public void onFsViewerConfig(GsFileBrowserOptions.Options dopt) {
dopt.titleText = R.string.snippet_directory;
dopt.rootFolder = _appSettings.getNotebookDirectory();
}
}, fragManager, getActivity());
return true;
}

case R.string.pref_key__notebook_directory: {
FragmentManager fragManager = getActivity().getSupportFragmentManager();
MarkorFileBrowserFactory.showFolderDialog(new GsFileBrowserOptions.SelectionListenerAdapter() {
@Override
public void onFsViewerSelected(String request, File file, final Integer lineNumber) {
Expand All @@ -256,7 +272,6 @@ public void onFsViewerConfig(GsFileBrowserOptions.Options dopt) {
return true;
}
case R.string.pref_key__quicknote_filepath: {
FragmentManager fragManager = getActivity().getSupportFragmentManager();
MarkorFileBrowserFactory.showFileDialog(new GsFileBrowserOptions.SelectionListenerAdapter() {
@Override
public void onFsViewerSelected(String request, File file, final Integer lineNumber) {
Expand All @@ -274,7 +289,6 @@ public void onFsViewerConfig(GsFileBrowserOptions.Options dopt) {
return true;
}
case R.string.pref_key__todo_filepath: {
FragmentManager fragManager = getActivity().getSupportFragmentManager();
MarkorFileBrowserFactory.showFileDialog(new GsFileBrowserOptions.SelectionListenerAdapter() {
@Override
public void onFsViewerSelected(String request, File file, final Integer lineNumber) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@
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 @@ -599,14 +598,17 @@ protected final boolean runCommonAction(final @StringRes int action) {
}
case R.string.abid_common_insert_snippet: {
MarkorDialogFactory.showInsertSnippetDialog(_activity, (snip) -> {
_hlEditor.insertOrReplaceTextOnCursor(TextViewUtils.interpolateEscapedDateTime(snip));
_hlEditor.insertOrReplaceTextOnCursor(TextViewUtils.interpolateSnippet(snip, _document.getTitle(), TextViewUtils.getSelectedText(_hlEditor)));
_lastSnip = snip;
});
return true;
}
case R.string.abid_common_open_link_browser: {
String url;
if ((url = GsTextUtils.tryExtractUrlAroundPos(text.toString(), _hlEditor.getSelectionStart())) != null) {
final int sel = TextViewUtils.getSelection(_hlEditor)[0];
final String line = TextViewUtils.getSelectedLines(_hlEditor, sel);
final int cursor = sel - TextViewUtils.getLineStart(_hlEditor.getText(), sel);
String url = GsTextUtils.tryExtractUrlAroundPos(line, cursor);
if (url != null) {
if (url.endsWith(")")) {
url = url.substring(0, url.length() - 1);
}
Expand Down Expand Up @@ -689,7 +691,7 @@ protected final boolean runCommonLongPressAction(@StringRes int action) {
}
case R.string.abid_common_insert_snippet: {
if (!TextUtils.isEmpty(_lastSnip)) {
_hlEditor.insertOrReplaceTextOnCursor(TextViewUtils.interpolateEscapedDateTime(_lastSnip));
_hlEditor.insertOrReplaceTextOnCursor(TextViewUtils.interpolateSnippet(_lastSnip, _document.getTitle(), TextViewUtils.getSelectedText(_hlEditor)));
}
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public class FormatRegistry {
};

public static boolean isFileSupported(final File file, final boolean... textOnly) {
boolean textonly = textOnly != null && textOnly.length > 0 && textOnly[0];
final boolean textonly = textOnly != null && textOnly.length > 0 && textOnly[0];
if (file != null) {
final String filepath = file.getAbsolutePath().toLowerCase(Locale.ROOT);
for (TextConverterBase converter : CONVERTERS) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,31 @@
import androidx.annotation.StringRes;

import net.gsantner.markor.R;
import net.gsantner.markor.activity.DocumentActivity;
import net.gsantner.markor.format.ActionButtonBase;
import net.gsantner.markor.frontend.MarkorDialogFactory;
import net.gsantner.markor.frontend.textview.AutoTextFormatter;
import net.gsantner.markor.frontend.textview.TextViewUtils;
import net.gsantner.markor.model.Document;
import net.gsantner.opoc.util.GsContextUtils;
import net.gsantner.opoc.util.GsFileUtils;

import java.io.File;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class MarkdownActionButtons extends ActionButtonBase {

private Set<Integer> _disabledHeadings = new HashSet<>();
// Group 1 matches text, group 2 matches path
private static final Pattern MARKDOWN_LINK = Pattern.compile("\\[([^\\]]*)\\]\\(([^)]+)\\)");

private static final Pattern WEB_URL = Pattern.compile("https?://[^\\s/$.?#].[^\\s]*");

private final Set<Integer> _disabledHeadings = new HashSet<>();

public MarkdownActionButtons(@NonNull Context context, Document document) {
super(context, document);
Expand Down Expand Up @@ -139,6 +149,11 @@ public boolean onActionClick(final @StringRes int action) {
MarkorDialogFactory.showInsertTableRowDialog(getActivity(), false, this::insertTableRow);
return true;
}
case R.string.abid_common_open_link_browser: {
if (followLinkUnderCursor()) {
return true;
}
}
default: {
return runCommonAction(action);
}
Expand Down Expand Up @@ -168,6 +183,34 @@ public boolean onActionLongClick(final @StringRes int action) {
}
}

private boolean followLinkUnderCursor() {
final int sel = TextViewUtils.getSelection(_hlEditor)[0];
if (sel < 0) {
return false;
}

final String line = TextViewUtils.getSelectedLines(_hlEditor, sel);
final int cursor = sel - TextViewUtils.getLineStart(_hlEditor.getText(), sel);

final Matcher m = MARKDOWN_LINK.matcher(line);
while (m.find()) {
final String group = m.group(2);
if (m.start() <= cursor && m.end() > cursor && group != null) {
if (WEB_URL.matcher(group).matches()) {
GsContextUtils.instance.openWebpageInExternalBrowser(getActivity(), group);
return true;
} else {
final File f = GsFileUtils.makeAbsolute(group, _document.getFile().getParentFile());
if (GsFileUtils.canCreate(f)) {
DocumentActivity.handleFileClick(getActivity(), f, null);
return true;
}
}
}
}
return false;
}

private void insertTableRow(int cols, boolean isHeaderEnabled) {
StringBuilder sb = new StringBuilder();
_hlEditor.requestFocus();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import net.gsantner.markor.frontend.textview.TextViewUtils;
import net.gsantner.markor.model.Document;
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
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ private static String getLinkFormat(final int textFormatId) {
return "{{LINK|TITLE}}";
} else if (textFormatId == FormatRegistry.FORMAT_ASCIIDOC) {
return "link:LINK[TITLE]";
} else{
} else {
return "<a href=\"LINK\">TITLE</a>";
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -793,7 +793,7 @@ public static void showInsertSnippetDialog(final Activity activity, final GsCall
dopt.data = data;
dopt.isSearchEnabled = true;
dopt.titleText = R.string.insert_snippet;
dopt.messageText = Html.fromHtml("<small><small>" + as().getSnippetsFolder().getAbsolutePath() + "</small></small>");
dopt.messageText = Html.fromHtml("<small><small>" + as().getSnippetsDirectory().getAbsolutePath() + "</small></small>");
dopt.positionCallback = (ind) -> callback.callback(GsFileUtils.readTextFileFast(texts.get(data.get(ind.get(0)))).first);
GsSearchOrCustomTextDialog.showMultiChoiceDialogWithSearchFilterUI(activity, dopt);
}
Expand Down
Loading
Loading