From 353d62ab03aa9ce430da047afa4ff6797b065c44 Mon Sep 17 00:00:00 2001
From: Jan-Hendrik Willms <tleilax+studip@gmail.com>
Date: Wed, 28 Aug 2024 09:25:44 +0000
Subject: [PATCH] add before unload handler to avoid data loss in wiki, fixes
 #4522

Closes #4522

Merge request studip/studip!3332
---
 resources/vue/components/WikiEditor.vue | 20 ++++++++++++++++----
 1 file changed, 16 insertions(+), 4 deletions(-)

diff --git a/resources/vue/components/WikiEditor.vue b/resources/vue/components/WikiEditor.vue
index 6aae50c275d..52290cde2d9 100644
--- a/resources/vue/components/WikiEditor.vue
+++ b/resources/vue/components/WikiEditor.vue
@@ -182,12 +182,17 @@ export default {
                 online: this.isOnlineAndEditing
             };
 
-            if (this.isChanged) {
+            if (this.autosave && this.isChanged) {
                 data.content = this.editor.getData();
                 this.isChanged = false;
             }
 
             return data;
+        },
+        securityHandler(event) {
+            event.preventDefault();
+
+            event.returnValue = true;
         }
     },
     mounted() {
@@ -195,9 +200,7 @@ export default {
 
         STUDIP.wysiwyg.replace(textarea).then((editor) => {
             editor.model.document.on('change:data', () => {
-                if (this.autosave) {
-                    this.isChanged = true;
-                }
+                this.isChanged = editor.getData() !== this.content;
             });
 
             if (this.isEditing) {
@@ -227,6 +230,15 @@ export default {
             },
             () => this.getUpdaterData()
         )
+    },
+    watch: {
+        isChanged(current) {
+            if (current) {
+                window.addEventListener('beforeunload', this.securityHandler);
+            } else {
+                window.removeEventListener('beforeunload', this.securityHandler);
+            }
+        }
     }
 }
 </script>
-- 
GitLab