Update NoteViewerEditor.vue
authorMed <redacted>
Fri, 8 Sep 2023 21:30:41 +0000 (17:30 -0400)
committerGitHub <redacted>
Fri, 8 Sep 2023 21:30:41 +0000 (17:30 -0400)
Add beforeunload confirmation to prevent losing unsaved written text.

flatnotes/src/components/NoteViewerEditor.vue

index 441971ca55c0c6dfc66c83282385e95b118ca6ed..b6354b9ecb184a7bdac84291703bf1cd5683ddcb 100644 (file)
@@ -300,11 +300,22 @@ export default {
       return this.currentNote.content;
     },
 
+    setBeforeUnloadConfirmation: function (enable = true) {
+      if (enable) {
+        window.onbeforeunload = function() {
+          return true;
+        };
+      } else {
+        window.onbeforeunload = null;
+      }
+    },
+
     setEditMode: function (editMode = true) {
       let parent = this;
 
       // To Edit Mode
       if (editMode === true) {
+        this.setBeforeUnloadConfirmation(true);
         this.titleInput = this.currentNote.title;
         let draftContent = localStorage.getItem(this.currentNote.title);
 
@@ -338,6 +349,7 @@ export default {
       else {
         this.titleInput = null;
         this.initialContent = null;
+        this.setBeforeUnloadConfirmation(false);
         this.editMode = false;
       }
     },
git clone https://git.99rst.org/PROJECT