From 7f2685d328bf9582d8e97216a217130fd4cc6dd5 Mon Sep 17 00:00:00 2001 From: Iaphetes Date: Wed, 8 Jan 2025 07:17:39 +0100 Subject: [PATCH] refactoring and work on file search --- src-tauri/src/file_tree.rs | 94 +++++++++++++++++++ src-tauri/src/lib.rs | 152 ++----------------------------- src-tauri/src/markdown_parser.rs | 49 ++++++++++ src-tauri/src/search.rs | 21 +++++ src/filesearch.js | 0 src/index.html | 4 +- src/shortcuts.js | 4 +- src/styles.css | 2 +- src/ui.js | 10 +- 9 files changed, 181 insertions(+), 155 deletions(-) create mode 100644 src-tauri/src/file_tree.rs create mode 100644 src-tauri/src/markdown_parser.rs create mode 100644 src-tauri/src/search.rs create mode 100644 src/filesearch.js diff --git a/src-tauri/src/file_tree.rs b/src-tauri/src/file_tree.rs new file mode 100644 index 0000000..c43561c --- /dev/null +++ b/src-tauri/src/file_tree.rs @@ -0,0 +1,94 @@ +use html_tag::HtmlTag; +use shellexpand; +use std::{ + fs::{self, DirEntry}, + path::Path, +}; +#[tauri::command] +pub fn dir_tree_html(basepath: &str, filter: Vec) -> String { + match shellexpand::full(basepath) { + Ok(path) => add_dir_tree_node(&Path::new(&path.into_owned()), &filter).to_html(), + + Err(_) => String::new(), + } +} +fn add_dir_tree_node(path: &Path, filter: &Vec) -> HtmlTag { + let mut html = HtmlTag::new("div") + .with_class("filetree-node") + .with_id(&format!( + "{}", + Path::new(path) + .file_name() + .unwrap_or_default() + .to_string_lossy() + )) + .with_attribute("expanded", "false") + .with_child( + HtmlTag::new("button") + .with_class("filetree-directory-button") + .with_child( + HtmlTag::new("img") + .with_class("filetree-icon") + .with_attribute("src", "images/directory.svg"), + ) + .with_child( + HtmlTag::new("a").with_body( + &Path::new(path) + .file_name() + .unwrap_or_default() + .to_string_lossy(), + ), + ), + ); + if let Ok(entries) = fs::read_dir(path) { + for dir_entry_res in entries { + println!("{:?}", dir_entry_res); + if let Ok(dir_entry) = dir_entry_res { + if let Ok(metadata) = fs::metadata(dir_entry.path()) { + if metadata.is_file() { + html.add_child(div_from_dir_entry(&dir_entry)) + } else if metadata.is_dir() { + html.add_child( + add_dir_tree_node(&dir_entry.path(), &filter) + .with_attribute("style", "visibility: hidden; height: 0px;"), // .with_style("visibility", "hidden") + // .with_style("height", "0px"), + ); + } + } + } + } + } + return html; +} + +fn div_from_dir_entry(dir_entry: &DirEntry) -> HtmlTag { + let mut file_div = HtmlTag::new("div") + .with_class("filetree-node") + .with_id(&format!("{}", dir_entry.path().to_string_lossy())) + .with_attribute("style", "visibility: hidden; height: 0px;"); + let mut file_button = HtmlTag::new("button").with_class("filetree-file-button"); + match dir_entry + .path() + .extension() + .unwrap_or_default() + .to_string_lossy() + .to_string() + .as_str() + { + "md" => file_button.add_child( + HtmlTag::new("img") + .with_class("filetree-icon") + .with_attribute("src", "images/markdown.svg"), + ), + + _ => file_button.add_child( + HtmlTag::new("img") + .with_class("filetree-icon") + .with_attribute("src", "images/file.svg"), + ), + }; + + file_button.add_child(HtmlTag::new("a").with_body(&dir_entry.file_name().to_string_lossy())); + file_div.add_child(file_button); + return file_div; +} diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index 07faac0..4bf24f8 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -1,152 +1,12 @@ -use comrak::{format_html, nodes::NodeValue, parse_document, Arena, Options}; -use html_tag::HtmlTag; -use shellexpand; -use std::{ - env, - fs, - path::{absolute, Path}, -}; +mod file_tree; +mod markdown_parser; +mod search; +use std::env; use tauri_plugin_fs::FsExt; +use file_tree::dir_tree_html; +use markdown_parser::parse_markdown; - -#[tauri::command] -fn parse_markdown(document: &str, pathtemplate: &str, basepath: &str) -> String { - let rendered_markdown: String; - let path = "/foo/bar.txt"; - println!("{:?}", shellexpand::full(path)); - let arena = Arena::new(); - - // Parse the document into a root `AstNode` - let mut options = Options::default(); - options.render.unsafe_ = true; - // options.render.hardbreaks = true; - let root = parse_document(&arena, document, &options); - - // Iterate over all the descendants of root. - - if let Ok(resolved_basepath) = shellexpand::full(&basepath) { - println!( - "{:?}", - env::set_current_dir(&resolved_basepath.into_owned()) - ); - } - - for node in root.descendants() { - if let NodeValue::Image(ref mut image_node) = node.data.borrow_mut().value { - let image_path = Path::new(&image_node.url).to_path_buf(); - - let absolute_image_path_res = absolute(image_node.url.clone()); - - let absolute_image_path = absolute_image_path_res.unwrap_or(image_path.clone()); - let absolute_image_path_str = absolute_image_path.as_path().to_string_lossy(); - if let Ok(resolved_path) = shellexpand::full(&absolute_image_path_str) { - image_node.url = pathtemplate.replace("FILEPATH", &resolved_path); - } - println!("{}", image_node.url); - } - } - let mut html = vec![]; - format_html(root, &options, &mut html).unwrap(); - // println!("{}", String::from_utf8(html.clone()).unwrap()); - rendered_markdown = String::from_utf8(html).unwrap(); - return rendered_markdown.to_owned(); - // String::from_str("lololo").unwrap() -} -//
-// -//
- -fn add_dir_tree_node(path: &Path, filter: &Vec) -> HtmlTag { - let mut html = HtmlTag::new("div") - .with_class("filetree-node") - .with_id(&format!( - "{}", - Path::new(path) - .file_name() - .unwrap_or_default() - .to_string_lossy() - )) - .with_attribute("expanded", "false") - .with_child( - HtmlTag::new("button") - .with_class("filetree-directory-button") - .with_child( - HtmlTag::new("img") - .with_class("filetree-icon") - .with_attribute("src", "images/directory.svg"), - ) - .with_child( - HtmlTag::new("a").with_body( - &Path::new(path) - .file_name() - .unwrap_or_default() - .to_string_lossy(), - ), - ), - ); - if let Ok(entries) = fs::read_dir(path) { - for dir_entry_res in entries { - println!("{:?}", dir_entry_res); - if let Ok(dir_entry) = dir_entry_res { - if let Ok(metadata) = fs::metadata(dir_entry.path()) { - if metadata.is_file() { - let mut file_div = HtmlTag::new("div") - .with_class("filetree-node") - .with_id(&format!("{}", dir_entry.path().to_string_lossy())) - .with_attribute("style", "visibility: hidden; height: 0px;"); - let mut file_button = - HtmlTag::new("button").with_class("filetree-file-button"); - match dir_entry - .path() - .extension() - .unwrap_or_default() - .to_string_lossy() - .to_string() - .as_str() - { - "md" => file_button.add_child( - HtmlTag::new("img") - .with_class("filetree-icon") - .with_attribute("src", "images/markdown.svg"), - ), - - _ => file_button.add_child( - HtmlTag::new("img") - .with_class("filetree-icon") - .with_attribute("src", "images/file.svg"), - ), - }; - - file_button.add_child( - HtmlTag::new("a").with_body(&dir_entry.file_name().to_string_lossy()), - ); - file_div.add_child(file_button); - - html.add_child(file_div) - } else if metadata.is_dir() { - html.add_child( - add_dir_tree_node(&dir_entry.path(), &filter) - .with_attribute("style", "visibility: hidden; height: 0px;"), // .with_style("visibility", "hidden") - // .with_style("height", "0px"), - ); - } - } - } - } - } - return html; -} -#[tauri::command] -fn dir_tree_html(basepath: &str, filter: Vec) -> String { - match shellexpand::full(basepath) { - Ok(path) => add_dir_tree_node(&Path::new(&path.into_owned()), &filter).to_html(), - - Err(_) => String::new(), - } -} #[cfg_attr(mobile, tauri::mobile_entry_point)] pub fn run() { tauri::Builder::default() diff --git a/src-tauri/src/markdown_parser.rs b/src-tauri/src/markdown_parser.rs new file mode 100644 index 0000000..29797ea --- /dev/null +++ b/src-tauri/src/markdown_parser.rs @@ -0,0 +1,49 @@ +use comrak::{format_html, nodes::NodeValue, parse_document, Arena, Options}; +use std::{ + env, + + path::{absolute, Path}, +}; +#[tauri::command] +pub fn parse_markdown(document: &str, pathtemplate: &str, basepath: &str) -> String { + let rendered_markdown: String; + let path = "/foo/bar.txt"; + println!("{:?}", shellexpand::full(path)); + let arena = Arena::new(); + + // Parse the document into a root `AstNode` + let mut options = Options::default(); + options.render.unsafe_ = true; + // options.render.hardbreaks = true; + let root = parse_document(&arena, document, &options); + + // Iterate over all the descendants of root. + + if let Ok(resolved_basepath) = shellexpand::full(&basepath) { + println!( + "{:?}", + env::set_current_dir(&resolved_basepath.into_owned()) + ); + } + + for node in root.descendants() { + if let NodeValue::Image(ref mut image_node) = node.data.borrow_mut().value { + let image_path = Path::new(&image_node.url).to_path_buf(); + + let absolute_image_path_res = absolute(image_node.url.clone()); + + let absolute_image_path = absolute_image_path_res.unwrap_or(image_path.clone()); + let absolute_image_path_str = absolute_image_path.as_path().to_string_lossy(); + if let Ok(resolved_path) = shellexpand::full(&absolute_image_path_str) { + image_node.url = pathtemplate.replace("FILEPATH", &resolved_path); + } + println!("{}", image_node.url); + } + } + let mut html = vec![]; + format_html(root, &options, &mut html).unwrap(); + // println!("{}", String::from_utf8(html.clone()).unwrap()); + rendered_markdown = String::from_utf8(html).unwrap(); + return rendered_markdown.to_owned(); + // String::from_str("lololo").unwrap() +} diff --git a/src-tauri/src/search.rs b/src-tauri/src/search.rs new file mode 100644 index 0000000..2947c33 --- /dev/null +++ b/src-tauri/src/search.rs @@ -0,0 +1,21 @@ + +pub fn search_files(searchstring: &str, filter: Vec<&str>) -> HtmlTag{ + + if let Ok(entries) = fs::read_dir(path) { + for dir_entry_res in entries { + if let Ok(dir_entry) = dir_entry_res { + if let Ok(metadata) = fs::metadata(dir_entry.path()) { + if metadata.is_file() { + html.add_child(div_from_dir_entry(&dir_entry)) + } else if metadata.is_dir() { + html.add_child( + add_dir_tree_node(&dir_entry.path(), &filter) + .with_attribute("style", "visibility: hidden; height: 0px;"), // .with_style("visibility", "hidden") + // .with_style("height", "0px"), + ); + } + } + } + } + } +} diff --git a/src/filesearch.js b/src/filesearch.js new file mode 100644 index 0000000..e69de29 diff --git a/src/index.html b/src/index.html index b4cb1ff..83af55e 100644 --- a/src/index.html +++ b/src/index.html @@ -18,9 +18,9 @@ - + -
+
diff --git a/src/shortcuts.js b/src/shortcuts.js index b528a85..6ee10fd 100644 --- a/src/shortcuts.js +++ b/src/shortcuts.js @@ -1,11 +1,11 @@ -import { showGlobalSearch, escape } from "./ui.js"; +import { showFileSearch, escape } from "./ui.js"; document .addEventListener("keydown", function(event) { if (event.ctrlKey && event.key === "k") { event.preventDefault(); - showGlobalSearch(); + showFileSearch(); } if (event.key == "Escape"){ diff --git a/src/styles.css b/src/styles.css index cf629cd..f03cbc0 100644 --- a/src/styles.css +++ b/src/styles.css @@ -214,7 +214,7 @@ dialog { height: 20%; margin: auto; } -#search_dialog_input{ +#file-search-dialog-input{ border: solid; border-color: var(--accent-color); border-top-right-radius:10px; diff --git a/src/ui.js b/src/ui.js index c078c88..b33a4d6 100644 --- a/src/ui.js +++ b/src/ui.js @@ -23,16 +23,18 @@ document.addEventListener("keydown", handleShortcut); document.getElementById("hide-sidebar").onclick = function () { toggle_visibility("sidebar"); }; -const global_search_dialog = document.querySelector("dialog"); -export function showGlobalSearch() { - topmost_element = "global-search"; +const global_search_dialog = document.getElementById("file-search-dialog"); +export function showFileSearch() { + topmost_element = "file-search"; global_search_dialog.show(); } let topmost_element = ""; export function escape(){ - if (topmost_element === "global-search"){ + if (topmost_element === "file-search"){ global_search_dialog.close(); } } + +