work on file saving

This commit is contained in:
Iaphetes 2025-03-12 07:30:08 +01:00
parent 11bd003298
commit be1ab58bd5
4 changed files with 46 additions and 28 deletions

View file

@ -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<String> {
#[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::<Mutex<Config>>();
// 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<String> {
let config = app_handle.state::<Mutex<Config>>();
// 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::<Mutex<Config>>();
// 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());
}

View file

@ -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<String, String> {
println!("loading file");
pub async fn load_file(app_handle: tauri::AppHandle, path: String) -> Result<String, String> {
set_open_file_path(app_handle, &path);
read_to_string(path).map_err(|e| e.to_string())
}

View file

@ -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");