fix(editor): confirm unsaved structure changes

This commit is contained in:
t8y2 committed 2026-07-13 21:27:02 +08:00
1 parent 032a8d0f80
commit 8f619fb791
6 files changed
+161 -12

No files matched your search

@@ -0,0 +1,107 @@
import { createPinia, setActivePinia } from "pinia";
import { beforeEach, describe, expect, it, vi } from "vitest";
function installLocalStorage() {
const data = new Map<string, string>();
vi.stubGlobal("localStorage", {
getItem: vi.fn((key: string) => data.get(key) ?? null),
setItem: vi.fn((key: string, value: string) => data.set(key, value)),
removeItem: vi.fn((key: string) => data.delete(key)),
});
}
describe("queryStore structure editor close protection", () => {
beforeEach(() => {
vi.resetModules();
vi.unstubAllGlobals();
installLocalStorage();
setActivePinia(createPinia());
});
it("prompts before closing a dirty structure tab", async () => {
const { useQueryStore } = await import("@/stores/queryStore");
const store = useQueryStore();
const tabId = store.openTableStructure("mysql-1", "app", undefined, "users");
const tab = store.tabs.find((item) => item.id === tabId)!;
tab.structureDraft = { dirty: true } as typeof tab.structureDraft;
store.closeTab(tabId);
expect(store.showCloseConfirm).toBe(true);
expect(store.pendingCloseTabId).toBe(tabId);
expect(store.tabs.some((item) => item.id === tabId)).toBe(true);
});
it("closes a clean structure tab without prompting", async () => {
const { useQueryStore } = await import("@/stores/queryStore");
const store = useQueryStore();
const tabId = store.openTableStructure("mysql-1", "app", undefined, "users");
const tab = store.tabs.find((item) => item.id === tabId)!;
tab.structureDraft = { dirty: false } as typeof tab.structureDraft;
store.closeTab(tabId);
expect(store.showCloseConfirm).toBe(false);
expect(store.tabs.some((item) => item.id === tabId)).toBe(false);
});
it("protects legacy persisted structure drafts without a dirty flag", async () => {
const { useQueryStore } = await import("@/stores/queryStore");
const store = useQueryStore();
const tabId = store.openTableStructure("mysql-1", "app", undefined, "users");
const tab = store.tabs.find((item) => item.id === tabId)!;
tab.structureDraft = { initialized: true } as typeof tab.structureDraft;
store.closeTab(tabId);
expect(store.showCloseConfirm).toBe(true);
expect(store.pendingCloseTabId).toBe(tabId);
});
it("protects dirty structure tabs during batch and app close", async () => {
const { useSettingsStore } = await import("@/stores/settingsStore");
const { useQueryStore } = await import("@/stores/queryStore");
const settingsStore = useSettingsStore();
settingsStore.editorSettings.confirmUnsavedSqlClose = false;
const store = useQueryStore();
const tabId = store.openTableStructure("mysql-1", "app", undefined, "users");
const tab = store.tabs.find((item) => item.id === tabId)!;
tab.structureDraft = { dirty: true } as typeof tab.structureDraft;
store.closeAllTabs();
expect(store.showCloseConfirm).toBe(true);
expect(store.closeConfirmContext).toBe("batch");
store.cancelClosePendingTab();
expect(store.requestAppCloseConfirmation()).toBe(true);
expect(store.closeConfirmContext).toBe("app");
});
it("discards a structure draft before force closing", async () => {
const { useQueryStore } = await import("@/stores/queryStore");
const store = useQueryStore();
const tabId = store.openTableStructure("mysql-1", "app", undefined, "users");
const tab = store.tabs.find((item) => item.id === tabId)!;
tab.structureDraft = { dirty: true } as typeof tab.structureDraft;
store.closeTab(tabId);
store.forceClosePendingTab();
expect(store.tabs.some((item) => item.id === tabId)).toBe(false);
});
it("preserves the disabled SQL close-confirm behavior", async () => {
const { useSettingsStore } = await import("@/stores/settingsStore");
const { useQueryStore } = await import("@/stores/queryStore");
const settingsStore = useSettingsStore();
settingsStore.editorSettings.confirmUnsavedSqlClose = false;
const store = useQueryStore();
const tabId = store.createTab("mysql-1", "app", "Query", "query");
store.updateSql(tabId, "select 1");
store.closeTab(tabId);
expect(store.showCloseConfirm).toBe(false);
expect(store.tabs.some((item) => item.id === tabId)).toBe(false);
});
});
+19 -6
View File
@@ -1204,6 +1204,10 @@ export const useQueryStore = defineStore("query", () => {
}
function isTabDirty(tab: QueryTab): boolean {
if (tab.mode === "structure") {
// Legacy persisted structure drafts predate the dirty flag; treat them as dirty until the editor rehydrates them.
return !!tab.structureDraft && tab.structureDraft.dirty !== false;
}
if (tab.mode !== "query") return false;
if (!tab.externalSqlPath && !tab.sql.trim()) return false;
const original = tab.originalSql;
@@ -1269,7 +1273,12 @@ export const useQueryStore = defineStore("query", () => {
function discardTabChanges(id: string) {
const tab = tabs.value.find((item) => item.id === id);
if (!tab || tab.mode !== "query") return false;
if (!tab) return false;
if (tab.mode === "structure") {
tab.structureDraft = undefined;
return true;
}
if (tab.mode !== "query") return false;
if (tab.originalSql !== undefined) {
tab.sql = tab.originalSql;
return true;
@@ -1303,9 +1312,9 @@ export const useQueryStore = defineStore("query", () => {
return;
}
const dirtyTab = shouldConfirmUnsavedSqlClose.value ? remainingIds.map((id) => tabs.value.find((tab) => tab.id === id)).find((tab): tab is QueryTab => !!tab && isTabDirty(tab)) : undefined;
const dirtyTab = remainingIds.map((id) => tabs.value.find((tab) => tab.id === id)).find((tab): tab is QueryTab => !!tab && shouldConfirmTabClose(tab));
if (dirtyTab) {
// Batch close must pause before dropping dirty query tabs so the existing save/discard dialog can protect unsaved SQL.
// Batch close must pause before dropping dirty tabs so the shared save/discard dialog protects every editable surface.
showDirtyTabCloseConfirm(dirtyTab, "batch");
return;
}
@@ -1332,7 +1341,7 @@ export const useQueryStore = defineStore("query", () => {
function closeTab(id: string, { force = false }: { force?: boolean } = {}) {
const tab = tabs.value.find((t) => t.id === id);
if (!tab) return;
if (!force && shouldConfirmUnsavedSqlClose.value && isTabDirty(tab)) {
if (!force && shouldConfirmTabClose(tab)) {
showDirtyTabCloseConfirm(tab, "tab");
return;
}
@@ -1355,6 +1364,11 @@ export const useQueryStore = defineStore("query", () => {
if (force) resumePendingBatchCloseAfter(id);
}
function shouldConfirmTabClose(tab: QueryTab): boolean {
if (tab.mode === "structure") return isTabDirty(tab);
return shouldConfirmUnsavedSqlClose.value && isTabDirty(tab);
}
function forceClosePendingTab() {
const id = pendingCloseTabId.value;
const confirmingAppClose = isConfirmingAppClose.value;
@@ -1500,8 +1514,7 @@ export const useQueryStore = defineStore("query", () => {
}
function requestAppCloseConfirmation() {
if (!shouldConfirmUnsavedSqlClose.value) return false;
const dirtyTab = tabs.value.find((tab) => isTabDirty(tab));
const dirtyTab = tabs.value.find((tab) => shouldConfirmTabClose(tab));
if (!dirtyTab) return false;
isConfirmingAppClose.value = true;
showDirtyTabCloseConfirm(dirtyTab, "app");