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 serde::{Deserialize, Serialize};
use std::{ use std::{
path::{Path, PathBuf}, path::{Path, PathBuf},
str::FromStr,
sync::Mutex, sync::Mutex,
}; };
use tauri::Manager; use tauri::Manager;
@ -46,21 +47,35 @@ pub fn get_basepath(app_handle: tauri::AppHandle) -> Option<String> {
#[tauri::command] #[tauri::command]
pub fn set_basepath(app_handle: tauri::AppHandle, path: String) -> bool { pub fn set_basepath(app_handle: tauri::AppHandle, path: String) -> bool {
if let Ok(resolved_path) = shellexpand::full(&path) {
if let Ok(resolved_path) = shellexpand::full(&path)
{
let basepath = Path::new(&resolved_path.to_string()).to_path_buf(); let basepath = Path::new(&resolved_path.to_string()).to_path_buf();
if !basepath.is_dir() { if !basepath.is_dir() {
println!("{:?}", basepath); println!("{:?}", basepath);
return false; return false;
} }
let config = app_handle.state::<Mutex<Config>>(); let config = app_handle.state::<Mutex<Config>>();
// Lock the mutex to get mutable access: // Lock the mutex to get mutable access:
let mut config = config.lock().unwrap(); let mut config = config.lock().unwrap();
config.core_cfg.home_path = Some(basepath); config.core_cfg.home_path = Some(basepath);
return true; return true;
} else {
return false;
};
} }
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.current_file = Some(PathBuf::from_str(path).unwrap_or_default());
} }

View file

@ -1,19 +1,17 @@
use crate::{
use tauri::{ipc::RuntimeCapability, App, AssetResolver, Manager, Url}; config::{get_open_file_path, set_open_file_path},
use std::fs::{read_to_string}; get_basepath,
};
use std::fs::read_to_string;
use std::path::Path; use std::path::Path;
use crate::get_basepath; use tauri::{ipc::RuntimeCapability, App, AssetResolver, Manager, Url};
#[tauri::command] #[tauri::command]
pub async fn save( pub async fn save_file(app_handle: tauri::AppHandle, content: String) {
app_handle: tauri::AppHandle, println!("{:?}", get_open_file_path(app_handle));
}
path: String, content: String) {}
#[tauri::command] #[tauri::command]
pub async fn load( pub async fn load_file(app_handle: tauri::AppHandle, path: String) -> Result<String, String> {
path: String) -> Result<String, String> { set_open_file_path(app_handle, &path);
println!("loading file");
read_to_string(path).map_err(|e| e.to_string()) 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 config::{get_basepath, load_config, set_basepath, Config};
use database::initialize_database; use database::initialize_database;
use file_handler::{load_file, save_file};
use file_tree::dir_tree_html; use file_tree::dir_tree_html;
use markdown_parser::parse_markdown; use markdown_parser::parse_markdown;
use search::search_files; use search::search_files;
@ -15,7 +16,6 @@ use std::env;
use std::sync::Mutex; use std::sync::Mutex;
use tauri::Manager; use tauri::Manager;
use tauri_plugin_fs::FsExt; use tauri_plugin_fs::FsExt;
use file_handler::load;
#[cfg_attr(mobile, tauri::mobile_entry_point)] #[cfg_attr(mobile, tauri::mobile_entry_point)]
pub fn run() { pub fn run() {
tauri::Builder::default() tauri::Builder::default()
@ -41,7 +41,8 @@ pub fn run() {
search_files, search_files,
get_basepath, get_basepath,
set_basepath, set_basepath,
load load_file,
save_file
]) ])
.run(tauri::generate_context!()) .run(tauri::generate_context!())
.expect("error while running tauri application"); .expect("error while running tauri application");

View file

@ -5,7 +5,7 @@ import { render_markdown } from "./main.js";
var selected_file = ""; var selected_file = "";
export function handle_file_select(filename) { export function handle_file_select(filename) {
if (filename.endsWith("md")) { if (filename.endsWith("md")) {
invoke("load", { path: filename }).then((ret) => { invoke("load_file", { path: filename }).then((ret) => {
var tag_id = document.getElementById("markdown_input"); var tag_id = document.getElementById("markdown_input");
tag_id.innerHTML = "<pre>".concat("", ret).concat("", "</pre>"); tag_id.innerHTML = "<pre>".concat("", ret).concat("", "</pre>");
render_markdown(); render_markdown();
@ -16,6 +16,10 @@ export function handle_file_select(filename) {
export function save_file() { export function save_file() {
console.log(selected_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 () { document.getElementById("save-file").onclick = function () {
save_file(); save_file();