This is an automated email from the ASF dual-hosted git repository.

wangzx pushed a commit to branch gh-pages
in repository https://gitbox.apache.org/repos/asf/echarts-examples.git


The following commit(s) were added to refs/heads/gh-pages by this push:
     new 1a3dafec feat(editor): add a prompt before leaving to prevent the code 
from being lost accidentally due to refreshing or closing the page.
1a3dafec is described below

commit 1a3dafec4120f06f315f0398d466f3e3faebfa12
Author: plainheart <y...@all-my-life.cn>
AuthorDate: Sun Nov 6 16:09:58 2022 +0800

    feat(editor): add a prompt before leaving to prevent the code from being 
lost accidentally due to refreshing or closing the page.
---
 src/editor/Editor.vue  | 16 +++++++++++++++-
 src/editor/Preview.vue |  8 +++++---
 2 files changed, 20 insertions(+), 4 deletions(-)

diff --git a/src/editor/Editor.vue b/src/editor/Editor.vue
index 3e2135b5..14d622fb 100644
--- a/src/editor/Editor.vue
+++ b/src/editor/Editor.vue
@@ -281,6 +281,16 @@ export default {
         }
       });
 
+      window.addEventListener('beforeunload', (e) => {
+        // no repeated prompt if already confirmed
+        if (window.__EDITOR_NO_LEAVE_CONFIRMATION__) {
+          return;
+        }
+        // prevent the code from being lost accidentally due to refreshing or 
closing the page
+        e.preventDefault();
+        e.returnValue = '';
+      });
+
       // ensure prettier
       store.typeCheck || this.prepareFormatter();
     }
@@ -411,7 +421,11 @@ export default {
             cancelButtonText: this.$t('editor.cancelButtonText'),
             type: 'warning'
           })
-            .then(() => gotoURL({ lang }))
+            .then(() => {
+              // already confirmed
+              window.__EDITOR_NO_LEAVE_CONFIRMATION__ = true;
+              gotoURL({ lang });
+            })
             .catch(() => {});
         }
       }
diff --git a/src/editor/Preview.vue b/src/editor/Preview.vue
index 98398c9b..5fe02318 100644
--- a/src/editor/Preview.vue
+++ b/src/editor/Preview.vue
@@ -477,12 +477,14 @@ export default {
     },
     changeVersion() {
       saveExampleCodeToLocal();
-      setTimeout(() =>
+      setTimeout(() => {
+        // no confirmation as we have saved the code to local storage
+        window.__EDITOR_NO_LEAVE_CONFIRMATION__ = true;
         gotoURL({
           version: store.echartsVersion,
           ...this.toolOptions
-        })
-      );
+        });
+      });
     },
     changeRandomSeed() {
       updateRandomSeed();


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@echarts.apache.org
For additional commands, e-mail: commits-h...@echarts.apache.org

Reply via email to