From fd84697db0d1993acbe1cb890c72155e853039fe Mon Sep 17 00:00:00 2001 From: Vitaly Turovsky Date: Tue, 22 Aug 2023 21:13:26 +0300 Subject: [PATCH] feat: display leave confirmation when playing (prevent accidental ctrl+w), but not when paused so can easily close if desired --- src/globalState.js | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/globalState.js b/src/globalState.js index e6b0be15..ce881f51 100644 --- a/src/globalState.js +++ b/src/globalState.js @@ -1,6 +1,6 @@ //@ts-check -import { proxy, ref } from 'valtio' +import { proxy, ref, subscribe } from 'valtio' import { pointerLock } from './utils' // todo: refactor structure with support of hideNext=false @@ -110,4 +110,15 @@ export const miscUiState = proxy({ currentTouch: null }) +window.addEventListener('beforeunload', (event) => { + // todo-low maybe exclude chat? + if (!isGameActive(true) && activeModalStack.at(-1)?.elem.id !== 'chat') return + // For major browsers doning only this is enough + event.preventDefault() + + // Display a confirmation prompt + event.returnValue = '' // Required for some browsers + return 'The game is running. Are you sure you want to close this page?' +}); + window.miscUiState = miscUiState