Worked on search
This commit is contained in:
parent
7f2685d328
commit
aea2a9a7ba
5 changed files with 92 additions and 9 deletions
|
|
@ -4,6 +4,7 @@ mod search;
|
|||
use std::env;
|
||||
use tauri_plugin_fs::FsExt;
|
||||
|
||||
use search::search_files;
|
||||
use file_tree::dir_tree_html;
|
||||
use markdown_parser::parse_markdown;
|
||||
|
||||
|
|
@ -21,7 +22,7 @@ pub fn run() {
|
|||
|
||||
Ok(())
|
||||
})
|
||||
.invoke_handler(tauri::generate_handler![dir_tree_html, parse_markdown])
|
||||
.invoke_handler(tauri::generate_handler![dir_tree_html, parse_markdown, search_files])
|
||||
.run(tauri::generate_context!())
|
||||
.expect("error while running tauri application");
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,21 +1,80 @@
|
|||
use html_tag::HtmlTag;
|
||||
use std::{
|
||||
fs::{self, DirEntry},
|
||||
path::Path,
|
||||
};
|
||||
|
||||
pub fn search_files(searchstring: &str, filter: Vec<&str>) -> HtmlTag{
|
||||
#[tauri::command]
|
||||
pub fn search_files(basepath: &str, searchstring: &str, filter: Vec<String>) -> Vec<String> {
|
||||
match shellexpand::full(basepath) {
|
||||
Ok(path) => _search_files(&Path::new(&path.into_owned()), searchstring, &filter),
|
||||
|
||||
Err(_) => Vec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
fn search_result_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;
|
||||
}
|
||||
pub fn _search_files(path: &Path, searchstring: &str, filter: &Vec<String>) -> Vec<String> {
|
||||
let mut search_results: Vec<String> = Vec::new();
|
||||
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))
|
||||
if filter.contains(
|
||||
&dir_entry
|
||||
.path()
|
||||
.extension()
|
||||
.unwrap_or_default()
|
||||
.to_string_lossy()
|
||||
.to_string()
|
||||
) {
|
||||
let div =search_result_div_from_dir_entry(&dir_entry).to_html();
|
||||
println!("{:?}", div);
|
||||
search_results
|
||||
.push(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"),
|
||||
);
|
||||
search_results.append(&mut _search_files(
|
||||
&dir_entry.path(),
|
||||
searchstring,
|
||||
&filter,
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
println!("{:?}", search_results);
|
||||
return search_results;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,22 @@
|
|||
const { convertFileSrc, invoke } = window.__TAURI__.core;
|
||||
|
||||
var search_input = document.getElementById("file-search-dialog-input");
|
||||
search_input.addEventListener('input', () => {
|
||||
search_files();
|
||||
});
|
||||
|
||||
|
||||
function search_files(){
|
||||
var text = search_input.innerText;
|
||||
invoke("search_files", { searchstring: text, basepath: "$HOME/Documents/Knowledgebase", filter: ["md"]}).then(
|
||||
(ret) => {
|
||||
var tag_id = document.getElementById('rendered_markdown');
|
||||
var result_div = "";
|
||||
ret.array.forEach(element => {
|
||||
result_div += element;
|
||||
});
|
||||
tag_id.innerHTML = "<pre>".concat("", result_div).concat("", "</pre>");
|
||||
// tag_id.innerHTML = assetUrl.concat(" ", ' \n <img src="'.concat("", assetUrl).concat("", '" alt="Girl in a jacket" width="500" height="600">'))
|
||||
}
|
||||
);
|
||||
}
|
||||
|
|
@ -10,6 +10,7 @@
|
|||
<script type="module" src="./ui.js" defer></script>
|
||||
<script type="module" src="./filesystem.js" defer></script>
|
||||
<script type="module" src="./shortcuts.js" defer></script>
|
||||
<script type="module" src="./filesearch.js" defer></script>
|
||||
<script type="text/javascript" src="./main.js"></script>
|
||||
<script type="text/javascript" src="./filesystem.js"></script>
|
||||
<script type="text/javascript" src="./shortcuts.js"></script>
|
||||
|
|
|
|||
|
|
@ -10,7 +10,7 @@
|
|||
--sidebar-width: 3em;
|
||||
--filetree-width: 10em;
|
||||
font-family: Avenir, Helvetica, Arial, sans-serif;
|
||||
font-size: 16px;
|
||||
font-size: 30px;
|
||||
line-height: 24px;
|
||||
|
||||
color: #f6f6f6;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue