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