From be1ab58bd584ffc19d62c2f1b2650fe886da42af Mon Sep 17 00:00:00 2001 From: Iaphetes Date: Wed, 12 Mar 2025 07:30:08 +0100 Subject: [PATCH] work on file saving --- src-tauri/src/config.rs | 39 ++++++++++++++++++++++++----------- src-tauri/src/file_handler.rs | 24 ++++++++++----------- src-tauri/src/lib.rs | 5 +++-- src/filesystem.js | 6 +++++- 4 files changed, 46 insertions(+), 28 deletions(-) diff --git a/src-tauri/src/config.rs b/src-tauri/src/config.rs index d707b25..8750bba 100644 --- a/src-tauri/src/config.rs +++ b/src-tauri/src/config.rs @@ -1,6 +1,7 @@ use serde::{Deserialize, Serialize}; use std::{ path::{Path, PathBuf}, + str::FromStr, sync::Mutex, }; use tauri::Manager; @@ -46,21 +47,35 @@ pub fn get_basepath(app_handle: tauri::AppHandle) -> Option { #[tauri::command] pub fn set_basepath(app_handle: tauri::AppHandle, path: String) -> bool { + if let Ok(resolved_path) = shellexpand::full(&path) { + let basepath = Path::new(&resolved_path.to_string()).to_path_buf(); + if !basepath.is_dir() { + println!("{:?}", basepath); + return false; + } - if let Ok(resolved_path) = shellexpand::full(&path) -{ - let basepath = Path::new(&resolved_path.to_string()).to_path_buf(); - if !basepath.is_dir(){ - println!("{:?}", basepath); + let config = app_handle.state::>(); + // Lock the mutex to get mutable access: + let mut config = config.lock().unwrap(); + config.core_cfg.home_path = Some(basepath); + return true; + } else { return false; - } - - + }; +} +// TODO error handling +pub fn get_open_file_path(app_handle: tauri::AppHandle) -> Option { let config = app_handle.state::>(); + + // Lock the mutex to get mutable access: + let config = config.lock().unwrap(); + let open_file_path = config.core_cfg.current_file.clone(); + return open_file_path.map(|p| p.to_string_lossy().into_owned()); +} +pub fn set_open_file_path(app_handle: tauri::AppHandle, path: &String) { + let mut config = app_handle.state::>(); + // Lock the mutex to get mutable access: let mut config = config.lock().unwrap(); - config.core_cfg.home_path = Some(basepath); - return true; -} -else {return false}; + config.core_cfg.current_file = Some(PathBuf::from_str(path).unwrap_or_default()); } diff --git a/src-tauri/src/file_handler.rs b/src-tauri/src/file_handler.rs index ffea18e..50892a4 100644 --- a/src-tauri/src/file_handler.rs +++ b/src-tauri/src/file_handler.rs @@ -1,19 +1,17 @@ - -use tauri::{ipc::RuntimeCapability, App, AssetResolver, Manager, Url}; -use std::fs::{read_to_string}; +use crate::{ + config::{get_open_file_path, set_open_file_path}, + get_basepath, +}; +use std::fs::read_to_string; use std::path::Path; -use crate::get_basepath; +use tauri::{ipc::RuntimeCapability, App, AssetResolver, Manager, Url}; #[tauri::command] -pub async fn save( - app_handle: tauri::AppHandle, - - path: String, content: String) {} - +pub async fn save_file(app_handle: tauri::AppHandle, content: String) { + println!("{:?}", get_open_file_path(app_handle)); +} #[tauri::command] -pub async fn load( - path: String) -> Result { - println!("loading file"); - +pub async fn load_file(app_handle: tauri::AppHandle, path: String) -> Result { + set_open_file_path(app_handle, &path); read_to_string(path).map_err(|e| e.to_string()) } diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index 8d1f4a1..044a575 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -8,6 +8,7 @@ mod search; use config::{get_basepath, load_config, set_basepath, Config}; use database::initialize_database; +use file_handler::{load_file, save_file}; use file_tree::dir_tree_html; use markdown_parser::parse_markdown; use search::search_files; @@ -15,7 +16,6 @@ use std::env; use std::sync::Mutex; use tauri::Manager; use tauri_plugin_fs::FsExt; -use file_handler::load; #[cfg_attr(mobile, tauri::mobile_entry_point)] pub fn run() { tauri::Builder::default() @@ -41,7 +41,8 @@ pub fn run() { search_files, get_basepath, set_basepath, - load + load_file, + save_file ]) .run(tauri::generate_context!()) .expect("error while running tauri application"); diff --git a/src/filesystem.js b/src/filesystem.js index b78bb97..526a6f7 100644 --- a/src/filesystem.js +++ b/src/filesystem.js @@ -5,7 +5,7 @@ import { render_markdown } from "./main.js"; var selected_file = ""; export function handle_file_select(filename) { if (filename.endsWith("md")) { - invoke("load", { path: filename }).then((ret) => { + invoke("load_file", { path: filename }).then((ret) => { var tag_id = document.getElementById("markdown_input"); tag_id.innerHTML = "
".concat("", ret).concat("", "
"); render_markdown(); @@ -16,6 +16,10 @@ export function handle_file_select(filename) { export function save_file() { console.log(selected_file); + var tag_id = document.getElementById("markdown_input"); + + invoke("save_file", {content: tag_id.innerHTML}).then((ret) => { + }); } document.getElementById("save-file").onclick = function () { save_file();