From a9aaceb96777924278192ee4ab7e81647dc24ed6 Mon Sep 17 00:00:00 2001 From: catto Date: Tue, 16 Apr 2024 22:44:53 +0200 Subject: [PATCH] auto-save! --- src-tauri/tauri.conf.json | 2 +- src/main.ts | 16 +++++++++++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src-tauri/tauri.conf.json b/src-tauri/tauri.conf.json index 7f1e24d..17d54d9 100644 --- a/src-tauri/tauri.conf.json +++ b/src-tauri/tauri.conf.json @@ -8,7 +8,7 @@ }, "package": { "productName": "snotes-deck", - "version": "0.0.5" + "version": "0.0.6" }, "tauri": { "allowlist": { diff --git a/src/main.ts b/src/main.ts index 60c6abf..eccd255 100644 --- a/src/main.ts +++ b/src/main.ts @@ -19,6 +19,9 @@ let currentNoteId: number | null = null; /** reverse the order of note by id in the sidebar */ let reversed = true; +let typingTimer: number | null = null; +const AUTOSAVE_DELAY = 5000; + enum EditorState { NEW, @@ -104,6 +107,7 @@ async function retrieveNotes(): Promise> { * Handle even listeners on load. * This does not handle listeners for generated fields like * the Notes in the sidebar. + * TODO: consistency */ window.addEventListener("DOMContentLoaded", () => { createNoteContentEl = document.querySelector("#create-input"); @@ -172,6 +176,16 @@ window.addEventListener("DOMContentLoaded", () => { }) } + // auto-save timer + createNoteContentEl?.addEventListener("keyup", () => { + if (editorState === EditorState.EDITING) { + if (typingTimer) { + clearTimeout(typingTimer); + } + typingTimer = window.setTimeout(() => saveNote(), AUTOSAVE_DELAY); + } + }); + refreshContextMenuElements(); }); @@ -431,4 +445,4 @@ async function refreshSidebarAndOpenLatestNote() { function toggleReverse(val: boolean) { reversed = val; showNotes(); -} \ No newline at end of file +}