feat: display leave confirmation when playing (prevent accidental ctrl+w), but not when paused so can easily close if desired

This commit is contained in:
Vitaly Turovsky 2023-08-22 21:13:26 +03:00
commit fd84697db0

View file

@ -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