diff --git a/gui/src/composables/composables.spec.ts b/gui/src/composables/composables.spec.ts index e3089256..f8c9ddfa 100644 --- a/gui/src/composables/composables.spec.ts +++ b/gui/src/composables/composables.spec.ts @@ -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); + }); +}); diff --git a/gui/src/composables/index.ts b/gui/src/composables/index.ts index beb0476f..0bb5674c 100644 --- a/gui/src/composables/index.ts +++ b/gui/src/composables/index.ts @@ -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"; diff --git a/gui/src/composables/useUnsavedGuard.ts b/gui/src/composables/useUnsavedGuard.ts new file mode 100644 index 00000000..6ca73709 --- /dev/null +++ b/gui/src/composables/useUnsavedGuard.ts @@ -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)); +} diff --git a/gui/src/dialogs/settings/RoutingA.vue b/gui/src/dialogs/settings/RoutingA.vue index fe29b2ad..7451b28e 100644 --- a/gui/src/dialogs/settings/RoutingA.vue +++ b/gui/src/dialogs/settings/RoutingA.vue @@ -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); diff --git a/gui/src/views/SettingsView.vue b/gui/src/views/SettingsView.vue index b2c8480a..b4c510b2 100644 --- a/gui/src/views/SettingsView.vue +++ b/gui/src/views/SettingsView.vue @@ -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); diff --git a/gui/src/views/settings/model.ts b/gui/src/views/settings/model.ts index b2271381..f28a9be6 100644 --- a/gui/src/views/settings/model.ts +++ b/gui/src/views/settings/model.ts @@ -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,