hippofish/packages/client/src/scripts/use-leave-guard.ts

47 lines
951 B
TypeScript
Raw Normal View History

2023-01-13 05:40:33 +01:00
import { inject, onUnmounted, Ref } from "vue";
import { i18n } from "@/i18n";
import * as os from "@/os";
export function useLeaveGuard(enabled: Ref<boolean>) {
2022-06-23 17:47:55 +02:00
/* TODO
const setLeaveGuard = inject('setLeaveGuard');
if (setLeaveGuard) {
setLeaveGuard(async () => {
if (!enabled.value) return false;
const { canceled } = await os.confirm({
type: 'warning',
text: i18n.ts.leaveConfirm,
});
return canceled;
});
} else {
onBeforeRouteLeave(async (to, from) => {
if (!enabled.value) return true;
const { canceled } = await os.confirm({
type: 'warning',
text: i18n.ts.leaveConfirm,
});
return !canceled;
});
}
2022-06-23 17:47:55 +02:00
*/
/*
function onBeforeLeave(ev: BeforeUnloadEvent) {
if (enabled.value) {
ev.preventDefault();
ev.returnValue = '';
}
}
window.addEventListener('beforeunload', onBeforeLeave);
onUnmounted(() => {
window.removeEventListener('beforeunload', onBeforeLeave);
});
*/
}