Files
admin-web/src/modules/overseas-master-data/useFleetDraftGuard.ts
T

24 lines
1.7 KiB
TypeScript
Raw Normal View History

import { computed, getCurrentInstance, reactive, ref, type Ref } from 'vue'
import { ElMessageBox } from 'element-plus'
import { useUnsavedWorkRegistration } from '@/core/unsaved-work/useUnsavedWorkRegistration'
/** Veto native closing before Element Plus changes its internal visibility; route/tab exits use the shared registry. */
export function useFleetDraftGuard(key: string, open: Ref<boolean>, model: object, busy: Ref<boolean>, uncertain: Ref<boolean>) {
const baseline = ref(''), checking = ref(false)
const snapshot = computed(() => JSON.stringify(model))
const registration = useUnsavedWorkRegistration({ key: `${key}:${getCurrentInstance()?.uid}`, label: computed(() => '拼车表单'), isOpen: computed(() => open.value || uncertain.value), model: reactive({ form: model, uncertain }), requiredPermission: computed(() => null) })
function capture() { baseline.value = snapshot.value; if (!uncertain.value) registration.captureBaseline() }
async function mayClose() {
if (busy.value || checking.value) return false
if (snapshot.value !== baseline.value || uncertain.value) {
checking.value = true
try { await ElMessageBox.confirm(uncertain.value ? '上次提交结果尚未确定,原请求将保留。关闭后可重新打开并核对或重试,是否关闭?' : '表单有未保存内容,确认放弃本次修改?', '关闭表单', { customClass: 'admin-density-confirm', confirmButtonText: '确认关闭', cancelButtonText: '继续填写' }) }
catch { return false }
finally { checking.value = false }
}
return true
}
async function beforeClose(done: () => void) { if (await mayClose()) done() }
return { capture, beforeClose, dirty: computed(() => snapshot.value !== baseline.value) }
}