Skip to content

Commit

Permalink
Many improvements to linking files
Browse files Browse the repository at this point in the history
  • Loading branch information
harshad1 committed Jan 5, 2024
1 parent 96965ae commit 479c479
Show file tree
Hide file tree
Showing 15 changed files with 376 additions and 181 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -605,8 +605,11 @@ public void onFsViewerConfig(GsFileBrowserOptions.Options dopt) {
return true;
}
case R.id.action_show_file_browser: {
final Intent intent = new Intent(activity, MainActivity.class).putExtra(Document.EXTRA_FILE, _document.getFile());
GsContextUtils.instance.animateToActivity(activity, intent, false, null);
// Delay because I want menu to close before we open the file browser
_hlEditor.postDelayed(() -> {
final Intent intent = new Intent(activity, MainActivity.class).putExtra(Document.EXTRA_FILE, _document.getFile());
GsContextUtils.instance.animateToActivity(activity, intent, false, null);
}, 250);
return true;
}
default: {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package net.gsantner.markor.activity;

import android.content.Context;
import android.view.Menu;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import net.gsantner.markor.ApplicationObject;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import net.gsantner.opoc.util.GsFileUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
Expand Down Expand Up @@ -227,18 +228,27 @@ private boolean followLinkUnderCursor() {
final int cursor = sel - TextViewUtils.getLineStart(_hlEditor.getText(), sel);

final Matcher m = MARKDOWN_LINK.matcher(line);

final ArrayList<String> linksUnderCursor = new ArrayList<>();
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);
if (m.start() <= cursor && m.end() >= cursor && group != null) {
linksUnderCursor.add(group);
}
}

// We want to search the line backwards in order to find the link closest to the cursor
// This helps us to match a link right after the cursor when there is one right before
for (int i = linksUnderCursor.size() - 1; i >= 0; i--) {
final String group = linksUnderCursor.get(i);
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;
} else {
final File f = GsFileUtils.makeAbsolute(group, _document.getFile().getParentFile());
if (GsFileUtils.canCreate(f)) {
DocumentActivity.handleFileClick(getActivity(), f, null);
return true;
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Build;
import android.text.Editable;
import android.view.View;
import android.widget.Button;
Expand All @@ -27,11 +28,15 @@
import net.gsantner.markor.format.FormatRegistry;
import net.gsantner.markor.format.markdown.MarkdownSyntaxHighlighter;
import net.gsantner.markor.frontend.filebrowser.MarkorFileBrowserFactory;
import net.gsantner.markor.frontend.filesearch.FileSearchDialog;
import net.gsantner.markor.frontend.filesearch.FileSearchEngine;
import net.gsantner.markor.frontend.filesearch.FileSearchResultSelectorDialog;
import net.gsantner.markor.frontend.textview.TextViewUtils;
import net.gsantner.markor.model.AppSettings;
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 @@ -84,6 +89,8 @@ public static void showInsertImageOrLinkDialog(
final EditText inputPathName = view.findViewById(R.id.ui__select_path_dialog__name);
final EditText inputPathUrl = view.findViewById(R.id.ui__select_path_dialog__url);
final Button buttonBrowseFilesystem = view.findViewById(R.id.ui__select_path_dialog__browse_filesystem);
final Button buttonSelectSpecial = view.findViewById(R.id.ui__select_path_dialog__select_special);
final Button buttonSearch = view.findViewById(R.id.ui__select_path_dialog__search);
final Button buttonPictureGallery = view.findViewById(R.id.ui__select_path_dialog__gallery_picture);
final Button buttonPictureCamera = view.findViewById(R.id.ui__select_path_dialog__camera_picture);
final Button buttonPictureEdit = view.findViewById(R.id.ui__select_path_dialog__edit_picture);
Expand Down Expand Up @@ -137,13 +144,17 @@ public static void showInsertImageOrLinkDialog(
okType = InsertType.AUDIO_DIALOG;
} else {
dialog.setTitle(R.string.insert_link);
buttonSelectSpecial.setVisibility(View.VISIBLE);
buttonSearch.setVisibility(View.VISIBLE);
browseType = InsertType.LINK_BROWSE;
okType = InsertType.LINK_DIALOG;
}

final String ok = activity.getString(android.R.string.ok);
dialog.setButton(DialogInterface.BUTTON_POSITIVE, ok, (di, b) -> _insertItem.callback(okType));
buttonBrowseFilesystem.setOnClickListener(v -> _insertItem.callback(browseType));
buttonSelectSpecial.setOnClickListener(v -> _insertItem.callback(InsertType.LINK_SPECIAL));
buttonSearch.setOnClickListener(v -> _insertItem.callback(InsertType.LINK_SEARCH));
buttonPictureCamera.setOnClickListener(b -> _insertItem.callback(InsertType.IMAGE_CAMERA));
buttonPictureGallery.setOnClickListener(v -> _insertItem.callback(InsertType.IMAGE_GALLERY));
buttonAudioRecord.setOnClickListener(v -> _insertItem.callback(InsertType.AUDIO_RECORDING));
Expand All @@ -163,6 +174,8 @@ private enum InsertType {
AUDIO_DIALOG,
LINK_BROWSE,
LINK_DIALOG,
LINK_SPECIAL,
LINK_SEARCH
}

private static String getTemplateForAction(final InsertType action, final int textFormatId) {
Expand All @@ -181,6 +194,8 @@ private static String getTemplateForAction(final InsertType action, final int te
}
case LINK_DIALOG:
case LINK_BROWSE:
case LINK_SPECIAL:
case LINK_SEARCH:
default: {
return getLinkFormat(textFormatId);
}
Expand All @@ -203,6 +218,8 @@ private static GsCallback.b2<Context, File> getFilterForAction(final InsertType
}
case LINK_DIALOG:
case LINK_BROWSE:
case LINK_SPECIAL:
case LINK_SEARCH:
default: {
return null;
}
Expand Down Expand Up @@ -300,6 +317,16 @@ private static void insertItem(

final MarkorContextUtils cu = new MarkorContextUtils(activity);

final GsCallback.a1<File> setFields = file -> {
if (pathEdit != null) {
pathEdit.setText(GsFileUtils.relativePath(currentFile, file));
}

if (nameEdit != null && GsTextUtils.isNullOrEmpty(nameEdit.getText())) {
nameEdit.setText(GsFileUtils.getNameWithoutExtension(file.getName()));
}
};

// Do each thing as necessary
switch (action) {
case IMAGE_CAMERA: {
Expand Down Expand Up @@ -340,11 +367,7 @@ private static void insertItem(
final GsFileBrowserOptions.SelectionListener fsListener = new GsFileBrowserOptions.SelectionListenerAdapter() {
@Override
public void onFsViewerSelected(final String request, final File file, final Integer lineNumber) {
pathEdit.setText(GsFileUtils.relativePath(currentFile, file));

if (GsTextUtils.isNullOrEmpty(nameEdit.getText())) {
nameEdit.setText(GsFileUtils.getFilenameWithoutExtension(file));
}
setFields.callback(file);
}

@Override
Expand All @@ -358,6 +381,24 @@ public void onFsViewerConfig(GsFileBrowserOptions.Options dopt) {
}
break;
}
case LINK_SPECIAL: {
MarkorDialogFactory.showSelectSpecialFileDialog(activity, setFields);
break;
}
case LINK_SEARCH: {
final File nb = _appSettings.getNotebookDirectory();
final FileSearchDialog.Options options = new FileSearchDialog.Options();
options.enableSearchInContent = false;
options.searchLocation = R.string.notebook;
if (!FileSearchEngine.isSearchExecuting.get()) {
FileSearchDialog.showDialog(activity, options, searchOptions -> {
searchOptions.rootSearchDir = nb;
FileSearchEngine.queueFileSearch(activity, searchOptions, searchResults ->
FileSearchResultSelectorDialog.showDialog(activity, searchResults, (file, line, isLong) ->
setFields.callback(new File(nb, file))));
});
}
}
case LINK_DIALOG:
case AUDIO_DIALOG:
case IMAGE_DIALOG: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import android.widget.Toast;

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

Expand Down Expand Up @@ -141,12 +142,11 @@ public static void showSearchFilesDialog(
}

if (!FileSearchEngine.isSearchExecuting.get()) {
GsCallback.a1<FileSearchEngine.SearchOptions> fileSearchDialogCallback = (searchOptions) -> {
FileSearchDialog.showDialog(activity, searchOptions -> {
searchOptions.rootSearchDir = searchDir;
FileSearchEngine.queueFileSearch(activity, searchOptions, (searchResults) ->
FileSearchResultSelectorDialog.showDialog(activity, searchResults, callback));
};
FileSearchDialog.showDialog(activity, fileSearchDialogCallback);
FileSearchEngine.queueFileSearch(activity, searchOptions, searchResults ->
FileSearchResultSelectorDialog.showDialog(activity, searchResults, () -> callback));
});
}
}

Expand Down Expand Up @@ -585,24 +585,79 @@ public static void showDocumentChecklistDialog(
GsSearchOrCustomTextDialog.showMultiChoiceDialogWithSearchFilterUI(activity, dopt);
}

// Insert items
public static void showInsertItemsDialog(
public static void showSelectSpecialFileDialog(final Activity activity, final GsCallback.a1<File> callback) {
GsSearchOrCustomTextDialog.DialogOptions dopt = new GsSearchOrCustomTextDialog.DialogOptions();
baseConf(activity, dopt);
dopt.titleText = R.string.special_documents;
final ArrayList<String> data = new ArrayList<>();
data.add(activity.getString(R.string.recently_viewed_documents));
data.add(activity.getString(R.string.popular_documents));
data.add(activity.getString(R.string.favourites));
dopt.data = data;
dopt.isSearchEnabled = false;
final AppSettings as = ApplicationObject.settings();

dopt.positionCallback = i -> {
switch (i.get(0)) {
default:
case 0:
selectItemDialog(activity, R.string.recently_viewed_documents, as.getRecentFiles(), File::getName, callback);
break;
case 1:
selectItemDialog(activity, R.string.popular_documents, as.getPopularFiles(), File::getName, callback);
break;
case 2:
selectItemDialog(activity, R.string.favourites, as.getFavouriteFiles(), File::getName, callback);
break;
}
};

GsSearchOrCustomTextDialog.showMultiChoiceDialogWithSearchFilterUI(activity, dopt);
}

/* Dialog to select an item from a list of items */
public static <T> void selectItemDialog(
final Activity activity,
final @StringRes int title,
final List<String> data,
final GsCallback.a1<String> insertCallback
final int title,
final Collection<T> items,
final GsCallback.s1<T> toString,
final GsCallback.a1<T> callback
) {
GsSearchOrCustomTextDialog.DialogOptions dopt = new GsSearchOrCustomTextDialog.DialogOptions();
baseConf(activity, dopt);
dopt.data = new ArrayList<>(new TreeSet<>(data));
dopt.callback = insertCallback;
dopt.titleText = title;
dopt.searchHintText = R.string.search_or_custom;
dopt.isMultiSelectEnabled = true;
dopt.positionCallback = (result) -> {
for (final Integer pi : result) {
insertCallback.callback(dopt.data.get(pi).toString());
}
final List<T> data = items instanceof List ? (List<T>) items : new ArrayList<>(items);
dopt.data = GsCollectionUtils.map(data, toString::callback);
dopt.positionCallback = i -> callback.callback(data.get(i.get(0)));
dopt.isSearchEnabled = true;
GsSearchOrCustomTextDialog.showMultiChoiceDialogWithSearchFilterUI(activity, dopt);
}

@RequiresApi(api = Build.VERSION_CODES.O)
public static void showGlobFilesDialog(
final Activity activity,
final File searchDir,
final GsCallback.a1<File> callback
) {
GsSearchOrCustomTextDialog.DialogOptions dopt = new GsSearchOrCustomTextDialog.DialogOptions();
baseConf(activity, dopt);
dopt.titleText = R.string.search_documents;
dopt.isSearchEnabled = true;
dopt.defaultText = "**/[!.]*.*";
dopt.callback = (query) -> {
final List<File> found = GsFileUtils.searchFiles(searchDir, query);
GsSearchOrCustomTextDialog.DialogOptions dopt2 = new GsSearchOrCustomTextDialog.DialogOptions();
baseConf(activity, dopt2);
dopt2.titleText = R.string.select;
dopt2.isSearchEnabled = true;
dopt2.data = GsCollectionUtils.map(found, File::getPath);
dopt2.positionCallback = (result) -> callback.callback(found.get(result.get(0)));
dopt2.neutralButtonText = R.string.search;
dopt2.neutralButtonCallback = dialog2 -> {
dialog2.dismiss();
showGlobFilesDialog(activity, searchDir, callback);
};
GsSearchOrCustomTextDialog.showMultiChoiceDialogWithSearchFilterUI(activity, dopt2);
};
GsSearchOrCustomTextDialog.showMultiChoiceDialogWithSearchFilterUI(activity, dopt);
}
Expand Down
Loading

0 comments on commit 479c479

Please sign in to comment.