25 MB lightweight cross-platform database client for 100+ databases, including MySQL, PostgreSQL, SQLite, Redis, MongoDB, DuckDB, SQL Server, and Dameng. Built-in AI, MCP Server, CLI, desktop and Docker. | 轻量级跨平台数据库管理工具,支持 MySQL、PostgreSQL、SQLite、Redis、MongoDB、达梦等 100+ 数据库,提供桌面端、Docker、CLI、内置 AI 助手和 MCP。
TypeScript aicliclickhousedatabasedatabase-clientdatabase-managementdockerguimcpmongodbmysqlpostgresqlredisrustsql-serversqlitetaurivue
feat(editor): persist sql panel search modes and locate file in library
heeeex1001ac1af5134f1
diff --git a/apps/desktop/src/components/layout/SqlFilePanel.vue b/apps/desktop/src/components/layout/SqlFilePanel.vueindex 994d71c295..349fe70aa2 100644--- a/apps/desktop/src/components/layout/SqlFilePanel.vue+++ b/apps/desktop/src/components/layout/SqlFilePanel.vue@@ -6,6 +6,7 @@ import { Button } from "@/components/ui/button";import { DropdownMenu, DropdownMenuTrigger, DropdownMenuContent, DropdownMenuItem } from "@/components/ui/dropdown-menu";import { Input } from "@/components/ui/input";import { Label } from "@/components/ui/label";+import { useSqlPanelSearchMode } from "@/composables/useSqlPanelSearchMode";import SqlPanelSearch from "@/components/layout/SqlPanelSearch.vue";import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle } from "@/components/ui/dialog";import LightTooltip from "@/components/ui/LightTooltip.vue";@@ -27,7 +28,7 @@ import { externalSqlFileOpenErrorMessage, externalSqlEditorMaxBytes, formatSqlFiimport * as api from "@/lib/backend/api";import type { SqlFileEntry } from "@/lib/backend/api";import { getSqlFileFilter, getSqlFileFolderPaths, saveSqlFileFilter, saveSqlFileFolderPaths, notifySqlFileFoldersChanged } from "@/lib/sqlFile/sqlFileFolders";-import { filterAndSortSqlFileEntries, getSqlFileSortOrder, saveSqlFileSortOrder, type SqlFileSearchMode, type SqlFileSortOrder } from "@/lib/sql/sqlFileTreeFilter";+import { filterAndSortSqlFileEntries, getSqlFileSortOrder, saveSqlFileSortOrder, type SqlFileSortOrder } from "@/lib/sql/sqlFileTreeFilter";import { orderedListSelectionIntent, type OrderedListSelectionItem } from "@/lib/selection/orderedListSelection";import { getSqlFileOpenMode, saveSqlFileOpenMode, type SqlFileOpenMode } from "@/lib/sqlFile/sqlFileInteraction";import { getSqlFileOrder, saveSqlFileOrder, reorderSqlFiles, remapSqlFileOrder, sqlFileParentPath, type SqlFileOrder } from "@/lib/sqlFile/sqlFileOrder";@@ -73,7 +74,7 @@ const fileFilterRegexExample = "(?i)[.](sql|ddl|sh|py)$";const searchInput = ref<InstanceType<typeof SqlPanelSearch> | null>(null);const searchInputPending = ref(false);-const searchMode = ref<SqlFileSearchMode>("name");+const searchMode = useSqlPanelSearchMode("file");const searchQuery = ref("");const {searching: scanning,diff --git a/apps/desktop/src/components/layout/SqlLibraryPanel.vue b/apps/desktop/src/components/layout/SqlLibraryPanel.vueindex 7a963dda5b..ca2008d087 100644--- a/apps/desktop/src/components/layout/SqlLibraryPanel.vue+++ b/apps/desktop/src/components/layout/SqlLibraryPanel.vue@@ -33,6 +33,8 @@ import {} from "@lucide/vue";import { Button } from "@/components/ui/button";import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle } from "@/components/ui/dialog";+import { useSqlPanelSearchMode } from "@/composables/useSqlPanelSearchMode";+import SidebarLocateButton from "@/components/sidebar/SidebarLocateButton.vue";import SqlPanelSearch from "@/components/layout/SqlPanelSearch.vue";import { SearchableSelect } from "@/components/ui/searchable-select";import CustomContextMenu, { type ContextMenuItem as CtxMenuItem } from "@/components/ui/CustomContextMenu.vue";@@ -91,7 +93,7 @@ const searchInput = ref<InstanceType<typeof SqlPanelSearch> | null>(null);const searchInputPending = ref(false);const searchQuery = ref("");-const searchMode = ref<"name" | "content">("name");+const searchMode = useSqlPanelSearchMode("library");const loadingSearchContent = ref(false);const unloadedSearchFiles = computed(() => savedSqlStore.allFiles.filter((file) => file.sqlLoaded === false));const contentSearchActive = computed(() => searchMode.value === "content" && !!searchQuery.value);@@ -536,6 +538,33 @@ const lastClickedItemIndex = ref<number | null>(null); // Unified index for bothconst activeItemId = ref<string | null>(null);const activeItemType = ref<"file" | "folder" | null>(null);const activeSavedSqlId = computed(() => queryStore.tabs.find((tab) => tab.id === queryStore.activeTabId)?.savedSqlId ?? null);+const libraryTree = ref<HTMLElement | null>(null);+const currentSavedSqlFile = computed(() => (activeSavedSqlId.value ? savedSqlStore.getFile(activeSavedSqlId.value) : undefined));++async function locateCurrentFile() {+ const file = currentSavedSqlFile.value;+ if (!file) return;+ if (searchInputPending.value || !treeRows.value.some((row) => row.type === "file" && row.file.id === file.id)) {+ searchInput.value?.clear();+ }+ const expanded = new Set(collapsedFolders.value);+ const visited = new Set<string>();+ let folderId = file.folderId;+ while (folderId && !visited.has(folderId)) {+ visited.add(folderId);+ expanded.delete(folderId);+ folderId = savedSqlStore.allFolders.find((folder) => folder.id === folderId)?.parentFolderId;+ }+ collapsedFolders.value = expanded;+ clearSelection();+ activeItemId.value = file.id;+ activeItemType.value = "file";+ lastClickedItemIndex.value = treeRows.value.findIndex((row) => row.type === "file" && row.file.id === file.id);+ await nextTick();+ const row = Array.from(libraryTree.value?.querySelectorAll<HTMLElement>("[data-sql-library-file]") ?? []).find((element) => element.dataset.sqlLibraryFile === file.id);+ row?.scrollIntoView({ block: "center", inline: "nearest" });+}+const hasCurrentSavedSqlExecutionTarget = computed(() => {const activeTab = queryStore.tabs.find((tab) => tab.id === queryStore.activeTabId);const target = savedSqlExecutionTargetFromTab(activeTab);@@ -1603,6 +1632,9 @@ function showDropInside(targetId: string) {<ArrowDownWideNarrow :class="['h-3 w-3', sortMode === 'date' ? 'text-primary' : '']" /></Button></LightTooltip>+ <LightTooltip :text="t('sqlFileTree.locateActiveFile')" side="bottom" :delay="0" :close-delay="0" nowrap>+ <SidebarLocateButton class="disabled:pointer-events-none disabled:opacity-50" :label="t('sqlFileTree.locateActiveFile')" :disabled="!currentSavedSqlFile" @locate="locateCurrentFile" />+ </LightTooltip><LightTooltip :text="t('savedSql.newFolder')" side="bottom" :delay="0" :close-delay="0" nowrap><Button variant="ghost" size="icon" class="h-5 w-5" @click="openNewFolderInput()"><FolderPlus class="h-3 w-3" />@@ -1634,7 +1666,7 @@ function showDropInside(targetId: string) {</p></div>- <div class="min-h-0 flex-1 overflow-y-auto py-1">+ <div ref="libraryTree" class="min-h-0 flex-1 overflow-y-auto py-1"><CustomContextMenu :items="contextMenuItems" @close="clearContextTarget"><template #default="{ onContextMenu }"><divdiff --git a/apps/desktop/src/components/layout/__tests__/SqlFilePanel.contentSearch.spec.ts b/apps/desktop/src/components/layout/__tests__/SqlFilePanel.contentSearch.spec.tsindex f2454b6a5e..3bf3bc874d 100644--- a/apps/desktop/src/components/layout/__tests__/SqlFilePanel.contentSearch.spec.ts+++ b/apps/desktop/src/components/layout/__tests__/SqlFilePanel.contentSearch.spec.ts@@ -101,6 +101,7 @@ function result(path: string, lines = [3], column = 8): SqlFileContentSearchResu}beforeEach(() => {+ localStorage.clear();mocks.isTauri = true;mocks.search.mockReset();mocks.search.mockResolvedValue({ files: [], skippedFiles: [], truncated: false });@@ -280,3 +281,16 @@ describe("SqlFilePanel content search", () => {expect(mocks.search).not.toHaveBeenCalled();});});++it("remembers the file search mode independently of the SQL library", async () => {+ localStorage.setItem("dbx-sql-library-search-mode", "name");+ const first = await mountPanel();+ await changeSearchMode(first.host, "content");+ const mountedFirst = mounted.pop()!;+ mountedFirst.app.unmount();+ mountedFirst.host.remove();+ const second = await mountPanel();+ expect(second.host.querySelector<HTMLSelectElement>("[data-search-mode]")!.value).toBe("content");+ expect(localStorage.getItem("dbx-sql-library-search-mode")).toBe("name");+ expect(mocks.search).not.toHaveBeenCalled();+});diff --git a/apps/desktop/src/components/layout/__tests__/SqlLibraryPanel.search.spec.ts b/apps/desktop/src/components/layout/__tests__/SqlLibraryPanel.search.spec.tsindex 8c28141432..21e318a454 100644--- a/apps/desktop/src/components/layout/__tests__/SqlLibraryPanel.search.spec.ts+++ b/apps/desktop/src/components/layout/__tests__/SqlLibraryPanel.search.spec.ts@@ -1,8 +1,9 @@// @vitest-environment happy-domimport { createApp, nextTick, type App } from "vue";import { createPinia, setActivePinia } from "pinia";-import { afterEach, describe, expect, it, vi } from "vitest";+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";import i18n from "@/i18n";+import { useQueryStore } from "@/stores/queryStore";import { useSavedSqlStore } from "@/stores/savedSqlStore";import type { SavedSqlFile } from "@/types/database";import { changeSearchMode } from "./sqlSearchTestUtils";@@ -43,6 +44,7 @@ async function typeQuery(host: HTMLElement, query: string) {function visibleFiles(host: HTMLElement) {return [...host.querySelectorAll("[data-sql-library-file]")].map((row) => row.getAttribute("data-sql-library-file"));}+beforeEach(() => localStorage.clear());afterEach(() => {for (const { app, host } of mounted.splice(0)) {app.unmount();@@ -111,3 +113,65 @@ describe("SQL library search scopes", () => {expect(load).toHaveBeenCalledTimes(2);});});++describe("SQL library locate and search preferences", () => {+ it("remembers content mode across remounts without restoring a query or loading SQL", async () => {+ const first = await mountPanel();+ await changeSearchMode(first.host, "content");+ await typeQuery(first.host, "needle");+ const mountedFirst = mounted.pop()!;+ mountedFirst.app.unmount();+ mountedFirst.host.remove();+ const second = await mountPanel();+ expect(second.host.querySelector<HTMLSelectElement>("[data-search-mode]")!.value).toBe("content");+ expect(second.host.querySelector<HTMLInputElement>("[data-sql-library-search]")!.value).toBe("");+ expect(second.load).not.toHaveBeenCalled();+ await changeSearchMode(second.host, "name");+ expect(localStorage.getItem("dbx-sql-library-search-mode")).toBe("name");+ });++ it("locates the current nested file, clears a blocking search and preserves its mode", async () => {+ const { host, store } = await mountPanel();+ store.folders = [+ { id: "root", name: "Root", connectionId: "", createdAt: timestamp, updatedAt: timestamp },+ { id: "child", parentFolderId: "root", name: "Child", connectionId: "", createdAt: timestamp, updatedAt: timestamp },+ ];+ store.files[0]!.folderId = "child";+ useQueryStore().openSavedSql(store.files[0]!);+ await nextTick();+ expect(visibleFiles(host)).not.toContain("name");+ await changeSearchMode(host, "content");+ await typeQuery(host, "unmatched");+ const scroll = vi.spyOn(HTMLElement.prototype, "scrollIntoView").mockImplementation(() => {});+ host.querySelector<HTMLButtonElement>("[data-sidebar-locate-button]")!.click();+ await nextTick();+ expect(visibleFiles(host)).toContain("name");+ expect(host.querySelector<HTMLInputElement>("[data-sql-library-search]")!.value).toBe("");+ expect(host.querySelector<HTMLSelectElement>("[data-search-mode]")!.value).toBe("content");+ expect(scroll.mock.instances.at(-1)).toBe(host.querySelector('[data-sql-library-file="name"]'));+ expect(useQueryStore().tabs).toHaveLength(1);+ });++ it("keeps a search that already includes the active unfiled SQL", async () => {+ const { host, store } = await mountPanel();+ useQueryStore().openSavedSql(store.files[0]!);+ await typeQuery(host, "needle");+ const scroll = vi.spyOn(HTMLElement.prototype, "scrollIntoView").mockImplementation(() => {});+ host.querySelector<HTMLButtonElement>("[data-sidebar-locate-button]")!.click();+ await nextTick();+ expect(host.querySelector<HTMLInputElement>("[data-sql-library-search]")!.value).toBe("needle");+ expect(scroll).toHaveBeenCalledOnce();+ });++ it("disables locate without an existing saved SQL file", async () => {+ const { host, store } = await mountPanel();+ const button = host.querySelector<HTMLButtonElement>("[data-sidebar-locate-button]")!;+ expect(button.disabled).toBe(true);+ useQueryStore().openSavedSql(store.files[0]!);+ await nextTick();+ expect(button.disabled).toBe(false);+ store.files = [];+ await nextTick();+ expect(button.disabled).toBe(true);+ });+});diff --git a/apps/desktop/src/composables/__tests__/useSqlPanelSearchMode.spec.ts b/apps/desktop/src/composables/__tests__/useSqlPanelSearchMode.spec.tsnew file mode 100644index 0000000000..07bf7eb201--- /dev/null+++ b/apps/desktop/src/composables/__tests__/useSqlPanelSearchMode.spec.ts@@ -0,0 +1,41 @@+// @vitest-environment happy-dom+import { effectScope } from "vue";+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";+import { useSqlPanelSearchMode } from "../useSqlPanelSearchMode";++beforeEach(() => localStorage.clear());+afterEach(() => vi.restoreAllMocks());++describe("SQL panel search preferences", () => {+ it.each([null, "invalid", "name", "content"])("restores and validates %s", (stored) => {+ if (stored !== null) localStorage.setItem("dbx-sql-library-search-mode", stored);+ const scope = effectScope();+ try {+ const mode = scope.run(() => useSqlPanelSearchMode("library"))!;+ expect(mode.value).toBe(stored === "content" ? "content" : "name");+ mode.value = "content";+ expect(localStorage.getItem("dbx-sql-library-search-mode")).toBe("content");+ } finally {+ scope.stop();+ }+ });++ it("remains usable if browser storage cannot be read or written", () => {+ vi.spyOn(Storage.prototype, "getItem").mockImplementation(() => {+ throw new Error("storage blocked");+ });+ vi.spyOn(Storage.prototype, "setItem").mockImplementation(() => {+ throw new Error("storage full");+ });+ vi.spyOn(console, "warn").mockImplementation(() => {});+ const scope = effectScope();+ try {+ const mode = scope.run(() => useSqlPanelSearchMode("file"))!;+ expect(mode.value).toBe("name");+ mode.value = "content";+ expect(mode.value).toBe("content");+ } finally {+ scope.stop();+ }+ });+});diff --git a/apps/desktop/src/composables/useSqlPanelSearchMode.ts b/apps/desktop/src/composables/useSqlPanelSearchMode.tsnew file mode 100644index 0000000000..71536167ea--- /dev/null+++ b/apps/desktop/src/composables/useSqlPanelSearchMode.ts@@ -0,0 +1,10 @@+import { ref, watch } from "vue";+import { safeLocalStorageGet, safeLocalStorageSet } from "@/lib/backend/safeStorage";+import type { SqlFileSearchMode } from "@/lib/sql/sqlFileTreeFilter";++export function useSqlPanelSearchMode(panel: "file" | "library") {+ const key = `dbx-sql-${panel}-search-mode`;+ const mode = ref<SqlFileSearchMode>(safeLocalStorageGet(key) === "content" ? "content" : "name");+ watch(mode, (value) => safeLocalStorageSet(key, value), { flush: "sync" });+ return mode;+}