From aa794231356d4429a9306f142eac9c25260589dc Mon Sep 17 00:00:00 2001 From: km4 Date: Mon, 12 Oct 2020 20:14:25 +0200 Subject: [PATCH 1/4] feat(#379): exit alert on playground --- src/playground/public/assets/js/app.ts | 9 +++++++++ src/playground/views/main.handlebars | 1 + 2 files changed, 10 insertions(+) diff --git a/src/playground/public/assets/js/app.ts b/src/playground/public/assets/js/app.ts index 035dbd6b..c2afc8f7 100644 --- a/src/playground/public/assets/js/app.ts +++ b/src/playground/public/assets/js/app.ts @@ -107,3 +107,12 @@ function removeFadeInClass() { const resultElem = document.getElementById('result')!; resultElem.classList.remove('fadeIn'); } +function exitEditor(editor: AceAjax.Editor) { + console.log(editor); + window.addEventListener('beforeunload', function (e) { + // Cancel the event + e.preventDefault(); // If you prevent default behavior in Mozilla Firefox prompt will always be shown + // Chrome requires returnValue to be set + e.returnValue = ''; + }); +} diff --git a/src/playground/views/main.handlebars b/src/playground/views/main.handlebars index 9f8aafb3..a4121383 100755 --- a/src/playground/views/main.handlebars +++ b/src/playground/views/main.handlebars @@ -149,6 +149,7 @@
From fb362bee963d13c8658739e09c3fb373a55760ee Mon Sep 17 00:00:00 2001 From: km4 Date: Tue, 13 Oct 2020 18:12:14 +0200 Subject: [PATCH 2/4] feat: confirmExitFromPlayground --- src/playground/public/assets/js/app.ts | 9 ++++----- src/playground/views/main.handlebars | 2 +- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/src/playground/public/assets/js/app.ts b/src/playground/public/assets/js/app.ts index c2afc8f7..d736d2a7 100644 --- a/src/playground/public/assets/js/app.ts +++ b/src/playground/public/assets/js/app.ts @@ -107,12 +107,11 @@ function removeFadeInClass() { const resultElem = document.getElementById('result')!; resultElem.classList.remove('fadeIn'); } -function exitEditor(editor: AceAjax.Editor) { - console.log(editor); + +// eslint-disable-next-line @typescript-eslint/no-unused-vars +function confirmExitFromPlayground(editor: AceAjax.Editor): void { window.addEventListener('beforeunload', function (e) { - // Cancel the event - e.preventDefault(); // If you prevent default behavior in Mozilla Firefox prompt will always be shown - // Chrome requires returnValue to be set + e.preventDefault(); e.returnValue = ''; }); } diff --git a/src/playground/views/main.handlebars b/src/playground/views/main.handlebars index a4121383..3ceeae5e 100755 --- a/src/playground/views/main.handlebars +++ b/src/playground/views/main.handlebars @@ -149,7 +149,7 @@
From 2ea878fa64e00695ae2d16ae896813f81a482cbe Mon Sep 17 00:00:00 2001 From: km4 Date: Fri, 16 Oct 2020 20:14:13 +0200 Subject: [PATCH 3/4] refactor: add a dirty flag to manage confirmExitFromPlayground fn --- src/playground/public/assets/js/app.ts | 10 ++++++++-- src/playground/views/main.handlebars | 3 +-- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/playground/public/assets/js/app.ts b/src/playground/public/assets/js/app.ts index d736d2a7..91b3fbfe 100644 --- a/src/playground/public/assets/js/app.ts +++ b/src/playground/public/assets/js/app.ts @@ -29,12 +29,18 @@ async function printAskVersion(elemId: string, askScriptServerUrl: string) { function registerAskScriptEditor( editorElementId: string, runElementId: string, - askScriptServerUrl: string + askScriptServerUrl: string, + dirty: boolean ) { const editor = ace.edit(editorElementId); editor.setTheme('ace/theme/twilight'); editor.session.setMode('ace/mode/javascript'); editor.session.setOption('useWorker', false); + editor.session.on('change', function () { + if (dirty) return; + confirmExitFromPlayground(); + dirty = true; + }); editor.commands.addCommand({ name: 'alertalert', @@ -109,7 +115,7 @@ function removeFadeInClass() { } // eslint-disable-next-line @typescript-eslint/no-unused-vars -function confirmExitFromPlayground(editor: AceAjax.Editor): void { +function confirmExitFromPlayground(): void { window.addEventListener('beforeunload', function (e) { e.preventDefault(); e.returnValue = ''; diff --git a/src/playground/views/main.handlebars b/src/playground/views/main.handlebars index 3ceeae5e..bf18cde2 100755 --- a/src/playground/views/main.handlebars +++ b/src/playground/views/main.handlebars @@ -147,9 +147,8 @@
From 42e898824a78278b1d9759b838682bd45c0d82ec Mon Sep 17 00:00:00 2001 From: km4 Date: Fri, 30 Oct 2020 20:15:01 +0100 Subject: [PATCH 4/4] fix: show exit alert only if playground is dirty --- src/playground/public/assets/js/app.ts | 22 ++++++++++++---------- src/playground/views/main.handlebars | 2 +- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/src/playground/public/assets/js/app.ts b/src/playground/public/assets/js/app.ts index 91b3fbfe..13e1fc54 100644 --- a/src/playground/public/assets/js/app.ts +++ b/src/playground/public/assets/js/app.ts @@ -29,16 +29,16 @@ async function printAskVersion(elemId: string, askScriptServerUrl: string) { function registerAskScriptEditor( editorElementId: string, runElementId: string, - askScriptServerUrl: string, - dirty: boolean + askScriptServerUrl: string ) { const editor = ace.edit(editorElementId); + let dirty = false; editor.setTheme('ace/theme/twilight'); editor.session.setMode('ace/mode/javascript'); editor.session.setOption('useWorker', false); - editor.session.on('change', function () { + editor.session.on('change', () => { if (dirty) return; - confirmExitFromPlayground(); + playgroundIsDirty(!dirty); dirty = true; }); @@ -87,6 +87,7 @@ async function executeAskScript( const json = await response.json(); if (response.status == 200) { showSuccessfulResponse(json.result); + playgroundIsDirty(false); } else { showErrorResponse(json.error); } @@ -114,10 +115,11 @@ function removeFadeInClass() { resultElem.classList.remove('fadeIn'); } -// eslint-disable-next-line @typescript-eslint/no-unused-vars -function confirmExitFromPlayground(): void { - window.addEventListener('beforeunload', function (e) { - e.preventDefault(); - e.returnValue = ''; - }); +function playgroundIsDirty(isDirty: boolean): void { + window.onbeforeunload = function (e: any) { + if (isDirty) { + e.preventDefault(); + e.returnValue = ''; + } + }; } diff --git a/src/playground/views/main.handlebars b/src/playground/views/main.handlebars index bf18cde2..9f8aafb3 100755 --- a/src/playground/views/main.handlebars +++ b/src/playground/views/main.handlebars @@ -147,7 +147,7 @@