diff --git a/libsnotes/Cargo.lock b/libsnotes/Cargo.lock index ed55c3c..214b998 100644 --- a/libsnotes/Cargo.lock +++ b/libsnotes/Cargo.lock @@ -176,6 +176,8 @@ dependencies = [ "chrono", "home", "rusqlite", + "serde", + "serde_derive", "serde_json", ] @@ -196,6 +198,12 @@ version = "0.4.21" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "90ed8c1e510134f979dbc4f070f87d4313098b704861a105fe34231c70a3901c" +[[package]] +name = "memchr" +version = "2.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" + [[package]] name = "num-traits" version = "0.2.18" @@ -219,9 +227,9 @@ checksum = "d231b230927b5e4ad203db57bbcbee2802f6bce620b1e4a9024a07d94e2907ec" [[package]] name = "proc-macro2" -version = "1.0.79" +version = "1.0.91" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e835ff2298f5721608eb1a980ecaee1aef2c132bf95ecc026a11b7bf3c01c02e" +checksum = "307e3004becf10f5a6e0d59d20f3cd28231b0e0827a96cd3e0ce6d14bc1e4bb3" dependencies = [ "unicode-ident", ] @@ -257,18 +265,18 @@ checksum = "e86697c916019a8588c99b5fac3cead74ec0b4b819707a682fd4d23fa0ce1ba1" [[package]] name = "serde" -version = "1.0.197" +version = "1.0.215" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3fb1c873e1b9b056a4dc4c0c198b24c3ffa059243875552b2bd0933b1aee4ce2" +checksum = "6513c1ad0b11a9376da888e3e0baa0077f1aed55c17f50e7b2397136129fb88f" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" -version = "1.0.197" +version = "1.0.215" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7eb0b34b42edc17f6b7cac84a52a1c5f0e1bb2227e997ca9011ea3dd34e8610b" +checksum = "ad1e866f866923f252f05c889987993144fb74e722403468a4ebd70c3cd756c0" dependencies = [ "proc-macro2", "quote", @@ -277,11 +285,12 @@ dependencies = [ [[package]] name = "serde_json" -version = "1.0.115" +version = "1.0.133" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "12dc5c46daa8e9fdf4f5e71b6cf9a53f2487da0e86e55808e2d35539666497dd" +checksum = "c7fceb2473b9166b2294ef05efcb65a3db80803f0b03ef86a5fc88a2b85ee377" dependencies = [ "itoa", + "memchr", "ryu", "serde", ] @@ -294,9 +303,9 @@ checksum = "3c5e1a9a646d36c3599cd173a41282daf47c44583ad367b8e6837255952e5c67" [[package]] name = "syn" -version = "2.0.58" +version = "2.0.89" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "44cfb93f38070beee36b3fef7d4f5a16f27751d94b187b666a5cc5e9b0d30687" +checksum = "44d46482f1c1c87acd84dea20c1bf5ebff4c757009ed6bf19cfd36fb10e92c4e" dependencies = [ "proc-macro2", "quote", diff --git a/libsnotes/Cargo.toml b/libsnotes/Cargo.toml index a690533..4a4a80e 100644 --- a/libsnotes/Cargo.toml +++ b/libsnotes/Cargo.toml @@ -8,6 +8,8 @@ edition = "2021" [dependencies] chrono = "0.4.37" home = "0.5.9" +serde = "1.0.215" +serde_derive = "1.0.215" serde_json = "1.0.115" [dependencies.rusqlite] version = "0.31.0" diff --git a/libsnotes/src/lib.rs b/libsnotes/src/lib.rs index 893fdb9..df1e7fd 100644 --- a/libsnotes/src/lib.rs +++ b/libsnotes/src/lib.rs @@ -274,6 +274,42 @@ pub fn get_note_by_id(id: u32) -> Result { Ok(json_string) } +/// Returns all notes as a json +pub fn export_all() -> Result { + let db = get_db_dir(); + let connection = Connection::open(db).map_err(|e| format!("Database Error: {}", e))?; + + let query = "SELECT * FROM notes"; + let mut prepare = connection.prepare(query).unwrap(); + + let notes = prepare + .query_map([], |row| { + Ok(Note { + id: row.get(0)?, + content: row.get(1)?, + date: row.get(2)?, + tag: row.get(3)?, + }) + }) + .unwrap(); + + let mut json_array = Vec::new(); + + for note in notes { + let unwrapped = note.unwrap(); + let note_json = json!({ + "id": unwrapped.id, + "date": unwrapped.date, + "content": unwrapped.content, + "tag": unwrapped.tag + }); + json_array.push(note_json); + } + + let json_string = serde_json::to_string_pretty(&json_array).unwrap(); + Ok(json_string) +} + #[cfg(test)] mod tests { use super::*; diff --git a/src-tauri/Cargo.lock b/src-tauri/Cargo.lock index c06ce6a..4ba7b42 100644 --- a/src-tauri/Cargo.lock +++ b/src-tauri/Cargo.lock @@ -2052,6 +2052,8 @@ dependencies = [ "chrono", "home", "rusqlite", + "serde", + "serde_derive", "serde_json", ] @@ -3304,9 +3306,9 @@ dependencies = [ [[package]] name = "serde_json" -version = "1.0.132" +version = "1.0.133" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d726bfaff4b320266d395898905d0eba0345aae23b54aee3a737e260fd46db03" +checksum = "c7fceb2473b9166b2294ef05efcb65a3db80803f0b03ef86a5fc88a2b85ee377" dependencies = [ "itoa 1.0.11", "memchr", diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index a4e3a29..1444ad1 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -5,6 +5,17 @@ use std::fs; use home::home_dir; use libsnotes::show_notes; +use tauri::{ + menu::{AboutMetadata, MenuBuilder, MenuItemBuilder, SubmenuBuilder}, + Emitter, +}; + +#[tauri::command] +fn export_all_notes() -> String { + let res = libsnotes::export_all().unwrap(); + println!("{}", &res); + res +} #[tauri::command] fn get_app_version() -> String { @@ -108,10 +119,40 @@ fn save_settings(settings: String) { fn main() { tauri::Builder::default() + .setup(|app| { + app.handle(); + + let settings = MenuItemBuilder::new("Export All") + .id("exportall") + //.accelerator("CmdOrCtrl+,") + .build(app)?; + + let app_submenu = SubmenuBuilder::new(app, "File") + .about(Some(AboutMetadata { + ..Default::default() + })) + .separator() + .item(&settings) + .build()?; + + let menu = MenuBuilder::new(app).items(&[&app_submenu]).build()?; + + app.set_menu(menu)?; + + app.on_menu_event(move |app, event| { + if event.id() == settings.id() { + // emit a window event to the frontend + let _event = app.emit("custom-event", "/exportall"); + } + }); + + Ok(()) + }) .plugin(tauri_plugin_fs::init()) .plugin(tauri_plugin_dialog::init()) .plugin(tauri_plugin_shell::init()) .invoke_handler(tauri::generate_handler![ + export_all_notes, get_app_version, init_db, get_notes_list, diff --git a/src/main.ts b/src/main.ts index 2dc950a..23c679f 100644 --- a/src/main.ts +++ b/src/main.ts @@ -909,7 +909,10 @@ async function openNoteById(value: string): Promise { async function exportNote(contents: string | null) { if (contents) { - const title = contents.slice(0, 10).trim(); + const title = + contents.replace(/\n/g, " ").length < 30 + ? contents.replace(/\n/g, " ").trim() + : contents.replace(/\n/g, " ").slice(0, 30).trim(); const filePath = await save({ defaultPath: (await homeDir()) + "/" + title + ".md", filters: [ @@ -931,6 +934,11 @@ async function exportNote(contents: string | null) { } } +async function exportAllNotes() { + let res = await invoke("export_all_notes"); + console.log(res); +} + async function handleOpenSettingsModal() { // insert app version const appVersionEl = document.getElementById("app-version");