file loadig and file tree working
This commit is contained in:
parent
c2cc84f9dc
commit
869157c08c
15 changed files with 608 additions and 128 deletions
7
src-tauri/Cargo.lock
generated
7
src-tauri/Cargo.lock
generated
|
|
@ -144,6 +144,7 @@ name = "apographe"
|
|||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"comrak",
|
||||
"html_tag",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"shellexpand",
|
||||
|
|
@ -1582,6 +1583,12 @@ dependencies = [
|
|||
"syn 1.0.109",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "html_tag"
|
||||
version = "0.1.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "258c462501ea394856d3c6cf7ca16f384dfb928766617bf018af0068c6935dee"
|
||||
|
||||
[[package]]
|
||||
name = "http"
|
||||
version = "1.1.0"
|
||||
|
|
|
|||
|
|
@ -26,4 +26,5 @@ comrak = "0.29.0"
|
|||
tauri-plugin-fs = "2"
|
||||
tauri-plugin-log = "2"
|
||||
shellexpand = "3.1.0"
|
||||
html_tag = "0.1.3"
|
||||
|
||||
|
|
|
|||
|
|
@ -1,9 +1,16 @@
|
|||
use std::path::Path;
|
||||
|
||||
use comrak::{format_html, nodes::NodeValue, parse_document, Arena, Options};
|
||||
use html_tag::HtmlTag;
|
||||
use shellexpand;
|
||||
use tauri::Manager;
|
||||
use std::{
|
||||
collections::BTreeMap,
|
||||
env,
|
||||
ffi::{OsStr, OsString},
|
||||
fs,
|
||||
path::{absolute, Path},
|
||||
};
|
||||
use tauri::{ipc::IpcResponse, Manager};
|
||||
use tauri_plugin_fs::FsExt;
|
||||
|
||||
// Learn more about Tauri commands at https://tauri.app/develop/calling-rust/
|
||||
#[tauri::command]
|
||||
fn greet(name: &str) -> String {
|
||||
|
|
@ -11,7 +18,7 @@ fn greet(name: &str) -> String {
|
|||
}
|
||||
|
||||
#[tauri::command]
|
||||
fn parse_markdown(document: &str, pathtemplate: &str) -> String {
|
||||
fn parse_markdown(document: &str, pathtemplate: &str, basepath: &str) -> String {
|
||||
let mut rendered_markdown: String = String::new();
|
||||
let path = "/foo/bar.txt";
|
||||
println!("{:?}", shellexpand::full(path));
|
||||
|
|
@ -25,9 +32,16 @@ fn parse_markdown(document: &str, pathtemplate: &str) -> String {
|
|||
let root = parse_document(&arena, document, &options);
|
||||
|
||||
// Iterate over all the descendants of root.
|
||||
env::set_current_dir(basepath);
|
||||
for node in root.descendants() {
|
||||
if let NodeValue::Image(ref mut image_node) = node.data.borrow_mut().value {
|
||||
if let Ok(resolved_path) = shellexpand::full(&image_node.url) {
|
||||
let image_path = Path::new(&image_node.url).to_path_buf();
|
||||
let absolute_image_path = absolute(image_node.url.clone());
|
||||
let absolute_image_path =
|
||||
.unwrap_or(image_path)
|
||||
.as_path()
|
||||
.to_string_lossy()
|
||||
if let Ok(resolved_path) = shellexpand::full(&absolute_image_path) {
|
||||
image_node.url = pathtemplate.replace("FILEPATH", &resolved_path);
|
||||
}
|
||||
}
|
||||
|
|
@ -39,7 +53,100 @@ fn parse_markdown(document: &str, pathtemplate: &str) -> String {
|
|||
return rendered_markdown.to_owned();
|
||||
// String::from_str("lololo").unwrap()
|
||||
}
|
||||
// <div class="filetree-node" id="folder3">
|
||||
// <button class="filetree-element">
|
||||
// <img class="topbar_icon" src="images/dropdown.svg" />folder 3
|
||||
// </button>
|
||||
// </div>
|
||||
|
||||
fn add_dir_tree_node(path: &Path, filter: &Vec<String>) -> 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>) -> 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()
|
||||
|
|
@ -54,8 +161,7 @@ pub fn run() {
|
|||
|
||||
Ok(())
|
||||
})
|
||||
.invoke_handler(tauri::generate_handler![greet])
|
||||
.invoke_handler(tauri::generate_handler![parse_markdown])
|
||||
.invoke_handler(tauri::generate_handler![dir_tree_html, parse_markdown])
|
||||
.run(tauri::generate_context!())
|
||||
.expect("error while running tauri application");
|
||||
}
|
||||
|
|
|
|||
|
|
@ -11,6 +11,7 @@
|
|||
"windows": [
|
||||
{
|
||||
"title": "apographe",
|
||||
"decorations": false,
|
||||
"width": 800,
|
||||
"height": 600
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue