Merge pull request #2024 from Zakkaus/unsaved-guard

This commit is contained in:
Markson Hon authored and GitHub committed 2026-09-20 23:18:56 +08:00
commit dd9c3248d1
6 files changed
+49 -3

No files matched your search

+26
View File
@@ -1,5 +1,8 @@
// @vitest-environment happy-dom
import { describe, expect, test, vi } from "vitest";
import { defineComponent, h } from "vue";
import { mount } from "@vue/test-utils";
import { useUnsavedGuard } from "./useUnsavedGuard";
import {
closeAllNotices,
dismissNotice,
@@ -72,3 +75,26 @@ describe("useLoading keeps the overlay until the last handle closes", () => {
vi.restoreAllMocks();
});
});
describe("useUnsavedGuard asks before a reload drops edits", () => {
test("cancels beforeunload only while dirty, and not after unmount", () => {
let dirty = false;
const Guarded = defineComponent({
setup() {
useUnsavedGuard(() => dirty);
return () => h("div");
},
});
const wrapper = mount(Guarded);
const fire = () => {
const event = new Event("beforeunload", { cancelable: true });
window.dispatchEvent(event);
return event.defaultPrevented;
};
expect(fire()).toBe(false);
dirty = true;
expect(fire()).toBe(true);
wrapper.unmount();
expect(fire()).toBe(false);
});
});
+1
View File
@@ -16,3 +16,4 @@ export { useConfirm, usePrompt } from "./useConfirm";
export { useMessageSocket, createMessageSocket } from "./useWebSocket";
export { useTraffic } from "./useTraffic";
export { useOutboundGroups } from "./useOutboundGroups";
export { useUnsavedGuard } from "./useUnsavedGuard";
+10
View File
@@ -0,0 +1,10 @@
import { onBeforeUnmount, onMounted } from "vue";
// The browser asks before a reload or a closed tab would drop unsaved edits.
export function useUnsavedGuard(dirty: () => boolean) {
const guard = (event: BeforeUnloadEvent) => {
if (dirty()) event.preventDefault();
};
onMounted(() => window.addEventListener("beforeunload", guard));
onBeforeUnmount(() => window.removeEventListener("beforeunload", guard));
}
+2 -1
View File
@@ -11,7 +11,7 @@ import {
} from "@mdi/js";
import { getRoutingA, putRoutingA } from "@/api";
import { errorText } from "@/api/errors";
import { useConfirm, useNotify } from "@/composables";
import { useConfirm, useNotify, useUnsavedGuard } from "@/composables";
import RoutingEditor from "./routingA/RoutingEditor.vue";
import RoutingReference from "./routingA/RoutingReference.vue";
import RoutingForm from "./routingA/RoutingForm.vue";
@@ -36,6 +36,7 @@ const confirm = useConfirm();
const notify = useNotify();
const routingA = ref("");
const original = ref("");
useUnsavedGuard(() => routingA.value !== original.value);
const loading = ref(true);
const saving = ref(false);
const confirming = ref(false);
+2 -1
View File
@@ -4,7 +4,7 @@ import { useI18n } from "vue-i18n";
import { useDisplay } from "vuetify";
import dayjs from "dayjs";
import { errorText } from "@/api/errors";
import { useDialog, useNotify } from "@/composables";
import { useDialog, useNotify, useUnsavedGuard } from "@/composables";
import DocsLink from "@/components/DocsLink.vue";
import CustomInboundDialog from "@/dialogs/settings/CustomInbound.vue";
import DnsDialog from "@/dialogs/settings/Dns.vue";
@@ -36,6 +36,7 @@ const notify = useNotify();
const { open } = useDialog();
const store = useAppStore();
const settings = useSettings();
useUnsavedGuard(() => settings.dirty.value);
const { form, ready, localGFWListVersion, remoteGFWListVersion } = settings;
const saving = ref(false);
const formRef = ref<{ validate(): Promise<{ valid: boolean }> } | null>(null);
+8 -1
View File
@@ -1,7 +1,7 @@
// The settings page's state and its two requests, without the view: the
// form as GET /setting returns it, saved with PUT /setting in the shape
// the old dialog sent (integers where it parsed them).
import { reactive, ref } from "vue";
import { computed, reactive, ref } from "vue";
import {
getRemoteGFWListVersion,
getSetting,
@@ -57,6 +57,10 @@ export function useSettings() {
const store = useAppStore();
const form = reactive(defaultForm());
const ready = ref(false);
const saved = ref("");
const dirty = computed(
() => ready.value && JSON.stringify(form) !== saved.value,
);
const localGFWListVersion = ref("");
const remoteGFWListVersion = ref("");
@@ -68,6 +72,7 @@ export function useSettings() {
}
localGFWListVersion.value = res.localGFWListVersion ?? "";
if (store.lite) form.transparentType = "system_proxy";
saved.value = JSON.stringify(form);
ready.value = true;
}
@@ -86,6 +91,7 @@ export function useSettings() {
putSetting(toRequest(form), { signal: control.signal }),
() => control.abort(),
);
saved.value = JSON.stringify(form);
} catch (err) {
// the backend restores the previous setting and keeps the core as it
// was; the touch says which state that is, and the form goes back to
@@ -108,6 +114,7 @@ export function useSettings() {
return {
form,
ready,
dirty,
localGFWListVersion,
remoteGFWListVersion,
load,