From 57aaf0f6213b825dbae4c28250420e6e41fe68ce Mon Sep 17 00:00:00 2001 From: Harshad Vedartham Date: Sat, 27 Jul 2024 04:29:05 -0700 Subject: [PATCH] Various bugfixes (anchor/toc jumping #2364, snippets folder #2369, wikitext newfile #2362, virtual directory browser #2350) (PR #2370 by @hardhard1) * Jump by a line identifier, not by id * For all nodes, use eval * Fixed bad key in snippet directory setting * Fix virtual folders and wikitext underscores --- .../markdown/MarkdownTextConverter.java | 12 +++-------- .../markor/frontend/MarkorDialogFactory.java | 9 ++++++--- .../markor/frontend/NewFileDialog.java | 8 ++++++-- .../gsantner/markor/model/AppSettings.java | 2 +- .../filebrowser/GsFileBrowserListAdapter.java | 20 +++++++++++-------- 5 files changed, 28 insertions(+), 23 deletions(-) diff --git a/app/src/main/java/net/gsantner/markor/format/markdown/MarkdownTextConverter.java b/app/src/main/java/net/gsantner/markor/format/markdown/MarkdownTextConverter.java index 69f0be58ff..afa051b512 100644 --- a/app/src/main/java/net/gsantner/markor/format/markdown/MarkdownTextConverter.java +++ b/app/src/main/java/net/gsantner/markor/format/markdown/MarkdownTextConverter.java @@ -454,18 +454,12 @@ private String replaceTokens(final String markup, final Map // Extension to add line numbers to headings // --------------------------------------------------------------------------------------------- - public static String getIdForLineNumber(final int num) { - return "line-" + num; - } - private static class LineNumberIdProvider implements AttributeProvider { @Override public void setAttributes(Node node, AttributablePart part, Attributes attributes) { - if (node instanceof com.vladsch.flexmark.ast.Heading) { - final Document document = node.getDocument(); - final int lineNumber = document.getLineNumber(node.getStartOffset()); - attributes.addValue("id", getIdForLineNumber(lineNumber)); - } + final Document document = node.getDocument(); + final int lineNumber = document.getLineNumber(node.getStartOffset()); + attributes.addValue("line", "" + lineNumber); } } diff --git a/app/src/main/java/net/gsantner/markor/frontend/MarkorDialogFactory.java b/app/src/main/java/net/gsantner/markor/frontend/MarkorDialogFactory.java index 0b6ca4e6f6..62e44bc3e7 100644 --- a/app/src/main/java/net/gsantner/markor/frontend/MarkorDialogFactory.java +++ b/app/src/main/java/net/gsantner/markor/frontend/MarkorDialogFactory.java @@ -840,9 +840,12 @@ public static void showHeadlineDialog( final int line = headings.get(index).line; TextViewUtils.selectLines(edit, line); - - final String id = MarkdownTextConverter.getIdForLineNumber(line); - webView.loadUrl(String.format("javascript:document.getElementById('%s').scrollIntoView();", id)); + final String jumpJs = "document.querySelector('[line=\"" + line + "\"]').scrollIntoView();"; + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { + webView.evaluateJavascript(jumpJs, null); + } else { + webView.loadUrl("javascript:" + jumpJs); + } }; dopt.neutralButtonText = R.string.filter; diff --git a/app/src/main/java/net/gsantner/markor/frontend/NewFileDialog.java b/app/src/main/java/net/gsantner/markor/frontend/NewFileDialog.java index a3fd00df3a..3e857d5407 100644 --- a/app/src/main/java/net/gsantner/markor/frontend/NewFileDialog.java +++ b/app/src/main/java/net/gsantner/markor/frontend/NewFileDialog.java @@ -258,10 +258,15 @@ public void onTextChanged(CharSequence s, int start, int before, int count) { final MarkorContextUtils cu = new MarkorContextUtils(getContext()); dialogBuilder.setNegativeButton(R.string.cancel, (dialogInterface, i) -> dialogInterface.dismiss()); dialogBuilder.setPositiveButton(getString(android.R.string.ok), (dialogInterface, i) -> { + final FormatRegistry.Format fmt = formats.get(typeSpinner.getSelectedItemPosition()); final String title = getTitle.callback(); final String ext = extEdit.getText().toString().trim(); - final String fileName = GsFileUtils.getFilteredFilenameWithoutDisallowedChars(title + ext); + String fileName = GsFileUtils.getFilteredFilenameWithoutDisallowedChars(title + ext); + + if (fmt.format == FormatRegistry.FORMAT_WIKITEXT) { + fileName = fileName.replace(" ", "_"); + } // Get template string // ------------------------------------------------------------------------------------- @@ -286,7 +291,6 @@ public void onTextChanged(CharSequence s, int start, int before, int count) { // These are done even if the file isn't created final String titleFormat = formatEdit.getText().toString().trim(); appSettings.setTemplateTitleFormat(templateAdapter.getItem(ti), titleFormat); - final FormatRegistry.Format fmt = formats.get(typeSpinner.getSelectedItemPosition()); appSettings.setTypeTemplate(fmt.format, (String) templateSpinner.getSelectedItem()); appSettings.setNewFileDialogLastUsedType(fmt.format); appSettings.setNewFileDialogLastUsedExtension(extEdit.getText().toString().trim()); diff --git a/app/src/main/java/net/gsantner/markor/model/AppSettings.java b/app/src/main/java/net/gsantner/markor/model/AppSettings.java index 780fee20f2..8f66ffedf6 100644 --- a/app/src/main/java/net/gsantner/markor/model/AppSettings.java +++ b/app/src/main/java/net/gsantner/markor/model/AppSettings.java @@ -127,7 +127,7 @@ public File getDefaultTodoFile() { public File getSnippetsDirectory() { final File _default = new File(getNotebookDirectory(), ".app/snippets"); - final File snf = new File(getString(R.string.pref_key__quicknote_filepath, _default.getAbsolutePath())); + final File snf = new File(getString(R.string.pref_key__snippet_directory_path, _default.getAbsolutePath())); return snf.isDirectory() && snf.canRead() ? snf : _default; } diff --git a/app/src/main/java/net/gsantner/opoc/frontend/filebrowser/GsFileBrowserListAdapter.java b/app/src/main/java/net/gsantner/opoc/frontend/filebrowser/GsFileBrowserListAdapter.java index 74d648ea70..923d229d0d 100644 --- a/app/src/main/java/net/gsantner/opoc/frontend/filebrowser/GsFileBrowserListAdapter.java +++ b/app/src/main/java/net/gsantner/opoc/frontend/filebrowser/GsFileBrowserListAdapter.java @@ -644,15 +644,18 @@ private void loadFolder(final File folder, final @Nullable File toShow) { virtualMapping.put(VIRTUAL_STORAGE_APP_DATA_PRIVATE, appDataFolder); newData.add(VIRTUAL_STORAGE_APP_DATA_PRIVATE); } - } else if (folder.isDirectory()) { - GsCollectionUtils.addAll(newData, folder.listFiles(GsFileBrowserListAdapter.this)); } else if (folder.equals(VIRTUAL_STORAGE_RECENTS)) { newData.addAll(_dopt.recentFiles); } else if (folder.equals(VIRTUAL_STORAGE_POPULAR)) { newData.addAll(_dopt.popularFiles); } else if (folder.equals(VIRTUAL_STORAGE_FAVOURITE)) { newData.addAll(_dopt.favouriteFiles); - } else if (folder.getAbsolutePath().equals("/storage/emulated")) { + } else if (folder.isDirectory()) { + GsCollectionUtils.addAll(newData, folder.listFiles(GsFileBrowserListAdapter.this)); + } + + // Some special folders get special children + if (folder.getAbsolutePath().equals("/storage/emulated")) { newData.add(new File(folder, "0")); } else if (folder.getAbsolutePath().equals("/")) { newData.add(new File(folder, "storage")); @@ -663,15 +666,16 @@ private void loadFolder(final File folder, final @Nullable File toShow) { } for (final File externalFileDir : ContextCompat.getExternalFilesDirs(_context, null)) { - for (int i = 0; i < newData.size(); i++) { - final File file = newData.get(i); - if (!canWrite(file) && !file.getAbsolutePath().equals("/") && externalFileDir != null && externalFileDir.getAbsolutePath().startsWith(file.getAbsolutePath())) { - final int depth = GsTextUtils.countChars(file.getAbsolutePath(), '/')[0]; + for (final File file : newData) { + final String absPath = file.getAbsolutePath(); + final String absExt = externalFileDir.getAbsolutePath() ; + if (!canWrite(file) && !absPath.equals("/") && absExt.startsWith(absPath)) { + final int depth = GsTextUtils.countChars(absPath, '/')[0]; if (depth < 3) { final File parent = file.getParentFile(); if (parent != null) { final File remap = new File(parent.getAbsolutePath(), "appdata-public (" + file.getName() + ")"); - virtualMapping.put(remap, new File(externalFileDir.getAbsolutePath())); + virtualMapping.put(remap, new File(absExt)); newData.add(remap); } }