settings json

This commit is contained in:
catto 2024-04-18 10:16:06 +02:00
parent 86a7fee5e9
commit 4f86f26972
5 changed files with 89 additions and 4 deletions

1
src-tauri/Cargo.lock generated
View File

@ -2494,6 +2494,7 @@ checksum = "3c5e1a9a646d36c3599cd173a41282daf47c44583ad367b8e6837255952e5c67"
name = "snotes-deck"
version = "0.0.0"
dependencies = [
"home",
"libsnotes",
"serde",
"serde_json",

View File

@ -15,6 +15,7 @@ tauri = { version = "1", features = ["shell-open"] }
serde = { version = "1", features = ["derive"] }
serde_json = "1"
libsnotes = { path = "../libsnotes" }
home = "0.5.9"
[features]
# This feature is used for production builds or when a dev server is not specified, DO NOT REMOVE!!

View File

@ -1,6 +1,9 @@
// Prevents additional console window on Windows in release, DO NOT REMOVE!!
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
use std::fs;
use home::home_dir;
use libsnotes::show_notes;
// Learn more about Tauri commands at https://tauri.app/v1/guides/features/command
@ -50,6 +53,43 @@ fn update_specific_note(id: u32, content: &str, tag: &str) -> bool {
libsnotes::edit_specific_note(id.try_into().unwrap(), tag, content).is_ok()
}
#[tauri::command]
fn load_settings() -> String {
let settings_string = fs::read_to_string(
home_dir()
.unwrap()
.join(".snotes-data/snotes-settings.json"),
)
.unwrap_or(String::from(""))
.parse()
.unwrap_or(String::from(""));
dbg!(&settings_string);
settings_string
}
#[tauri::command]
fn init_settings() {
let dir = home_dir().unwrap().join(".snotes-data");
dbg!(&dir);
if !dir.exists() {
fs::create_dir(dir).unwrap();
}
let settings = r#"
{
"fontSize": "16px"
}
"#;
fs::write(
home_dir()
.unwrap()
.join(".snotes-data/snotes-settings.json"),
settings,
)
.unwrap();
}
fn main() {
tauri::Builder::default()
.invoke_handler(tauri::generate_handler![
@ -60,7 +100,9 @@ fn main() {
search_notes,
create_note,
delete_specific_note,
update_specific_note
update_specific_note,
init_settings,
load_settings
])
.run(tauri::generate_context!())
.expect("error while running tauri application");

View File

@ -1,5 +1,5 @@
import { invoke } from "@tauri-apps/api/tauri";
import { Note } from "./model";
import { Note, Settings } from "./model";
let notesMsgEl: HTMLElement | null;
@ -38,6 +38,8 @@ enum SearchState {
let editorState = EditorState.NEW;
let searchState = SearchState.EMPTY;
let settings: Settings | null = null;
/**
* Saves the note.
* Or updates an existing note depending on editor state
@ -111,7 +113,11 @@ async function retrieveNotes(): Promise<Array<JSON>> {
* the Notes in the sidebar.
* TODO: consistency
*/
window.addEventListener("DOMContentLoaded", () => {
window.addEventListener("DOMContentLoaded", async () => {
// settings
settings = await loadSettings();
console.log("ACTUAL SETTINGS IN FRONTEND: ", settings.fontSize)
createNoteContentEl = document.querySelector("#create-input");
createNoteTagEl = document.querySelector("#create-tag");
searchbarEl = document.querySelector("#note-searchbar");
@ -162,7 +168,6 @@ window.addEventListener("DOMContentLoaded", () => {
} else {
searchState = SearchState.RESULTS;
}
searchbarContents = input;
searchNote(input);
@ -188,9 +193,35 @@ window.addEventListener("DOMContentLoaded", () => {
}
});
if (createNoteContentEl) {
createNoteContentEl.style.fontSize = settings.fontSize
}
refreshContextMenuElements();
});
async function loadSettings(): Promise<Settings> {
const defaultSettings: Settings = {
fontSize: "16px"
};
try {
let loadedSettingsString: string = await invoke("load_settings");
console.log(loadedSettingsString);
if (loadedSettingsString === "") {
await invoke("init_settings");
return defaultSettings;
}
const loadedSettings = JSON.parse(loadedSettingsString);
return loadedSettings as Settings;
} catch (error) {
console.error("An error occurred while loading settings:", error);
return defaultSettings;
}
}
/**
* We need to add new event listeners every time we refresh the note list
*/
@ -302,6 +333,12 @@ function handleSidebarNoteClick(id: Number): any {
});
if (n) {
// save if there's something in the editor currently
// before we open the new note to prevent data loss
if (createNoteContentEl.value != "") {
saveNote();
}
openNote(n);
} else {
// don't destory currently editing note if this fails

View File

@ -4,3 +4,7 @@ export type Note = {
date: String,
tag: String;
};
export type Settings = {
fontSize: string
};