From 9dd7c016ed6ed732b802b91915c5f94869e762ac 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/assets/javascripts/lib/wiki.js | 15 ++++++++++++++-
 1 file changed, 14 insertions(+), 1 deletion(-)

diff --git a/resources/assets/javascripts/lib/wiki.js b/resources/assets/javascripts/lib/wiki.js
index 5e273c366a0..4f68fc945c2 100644
--- a/resources/assets/javascripts/lib/wiki.js
+++ b/resources/assets/javascripts/lib/wiki.js
@@ -76,6 +76,10 @@ const Wiki = {
                             this.$nextTick(() => {
                                 this.editor.editing.view.focus();
                             });
+                        },
+                        securityHandler(event) {
+                            event.preventDefault();
+                            event.returnValue = true;
                         }
                     },
                     mounted() {
@@ -86,7 +90,7 @@ const Wiki = {
                                 editor.editing.view.focus();
                             }
                             editor.model.document.on('change:data',() => {
-                                this.isChanged = true;
+                                this.isChanged = editor.getData() !== this.content;
                                 this.lastChangeDate = new Date();
                             });
                             this.editor = editor;
@@ -99,6 +103,15 @@ const Wiki = {
                                 .sort((a, b) => a.fullname.localeCompare(b.fullname));
                         }
                     },
+                    watch: {
+                        isChanged(current) {
+                            if (current) {
+                                window.addEventListener('beforeunload', this.securityHandler);
+                            } else {
+                                window.removeEventListener('beforeunload', this.securityHandler);
+                            }
+                        }
+                    },
                     components: { WikiEditorOnlineUsers }
                 });
             }).then((app) => {
-- 
GitLab