From: Adam Dullage Date: Fri, 18 Feb 2022 13:37:58 +0000 (+0000) Subject: Added Local Drafts X-Git-Url: http://git.99rst.org/?a=commitdiff_plain;h=0abcf5da9c3ea3c977c0066ee5f449b753d950b2;p=flatnotes.git Added Local Drafts --- diff --git a/README.md b/README.md index eb896fe..fdd0025 100644 --- a/README.md +++ b/README.md @@ -38,10 +38,10 @@ This is what flatnotes aims to achieve. * [x] e to edit * [x] Ability to Delete a Note * [x] Exclude extension when renaming +* [x] Drafts * [ ] Loading & Not Found Indicators * [ ] / to search * [ ] CTRL-S to save -* [ ] Drafts * [ ] Image Embedding * [ ] Index Page (alphabetically sorted note list) * [ ] Clean & Responsive UI diff --git a/flatnotes/src/components/App.js b/flatnotes/src/components/App.js index cd2d450..48f8a0f 100644 --- a/flatnotes/src/components/App.js +++ b/flatnotes/src/components/App.js @@ -29,7 +29,8 @@ export default { rememberMeInput: false, notes: [], searchTerm: null, - searchTimeout: null, + draftSaveTimeout: null, + draftContent: null, searchResults: null, currentNote: null, titleInput: null, @@ -160,6 +161,18 @@ export default { }); }, + getContentForEditor: function() { + let draftContent = localStorage.getItem(this.currentNote.filename); + if (draftContent) { + if (confirm("Do you want to resume the saved draft?")) { + return draftContent; + } else { + localStorage.removeItem(this.currentNote.filename); + } + } + return this.currentNote.content; + }, + loadNote: function(filename) { let parent = this; api @@ -185,8 +198,27 @@ export default { this.currentView = this.views.note; }, + getEditorContent: function() { + return this.$refs.toastUiEditor.invoke("getMarkdown"); + }, + + clearDraftSaveTimeout: function() { + if (this.draftSaveTimeout != null) { + clearTimeout(this.draftSaveTimeout); + } + }, + + startDraftSaveTimeout: function() { + this.clearDraftSaveTimeout(); + this.draftSaveTimeout = setTimeout(this.saveDraft, 1000); + }, + + saveDraft: function() { + localStorage.setItem(this.currentNote.filename, this.getEditorContent()); + }, + saveNote: function() { - let newContent = this.$refs.toastUiEditor.invoke("getMarkdown"); + let newContent = this.getEditorContent(); // New Note if (this.currentNote.lastModified == null) { @@ -218,6 +250,7 @@ export default { }, saveNoteResponseHandler: function(response) { + localStorage.removeItem(this.currentNote.filename); this.currentNote = new Note( response.data.filename, response.data.lastModified, @@ -229,6 +262,16 @@ export default { this.toggleEditMode(); }, + cancelNote: function() { + localStorage.removeItem(this.currentNote.filename); + if (this.currentNote.lastModified == null) { + // Cancelling a new note + this.currentNote = null; + this.currentView = this.views.home; + } + this.toggleEditMode(); + }, + deleteNote: function() { if ( confirm( diff --git a/flatnotes/src/components/App.vue b/flatnotes/src/components/App.vue index d0bdf5a..7f502f4 100644 --- a/flatnotes/src/components/App.vue +++ b/flatnotes/src/components/App.vue @@ -103,7 +103,7 @@ v-if="currentView == views.note && editMode == true" type="button" class="btn btn-secondary mx-1" - @click="toggleEditMode" + @click="cancelNote" > Cancel @@ -146,10 +146,11 @@