Skip to content

armgit / dbx

Public Imported from GitHub
0

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。

fix(grid): preserve match position when rows change with filter

lxk9558b60ff3fc37c
diff --git a/apps/desktop/src/composables/__tests__/useDataGridSearch.spec.ts b/apps/desktop/src/composables/__tests__/useDataGridSearch.spec.ts
index 6340c63dc1..30795f5a26 100644
--- a/apps/desktop/src/composables/__tests__/useDataGridSearch.spec.ts
+++ b/apps/desktop/src/composables/__tests__/useDataGridSearch.spec.ts
@@ -166,6 +166,91 @@ describe("useDataGridSearch", () => {
expect(search.matches.value).toEqual([]);
expect(search.currentMatchIndex.value).toBe(-1);
});
+
+ it("does not auto-navigate or reset match position when rows change without a query change (#13241)", async () => {
+ vi.useFakeTimers();
+ const rows = ref([
+ ["hit", "other1"],
+ ["hit", "other2"],
+ ]);
+ const onNavigate = vi.fn();
+ const search = useDataGridSearch({
+ columns: ["id", "extra"],
+ rows,
+ getCellSearchText: (row, column) => row[column],
+ onNavigate,
+ });
+ search.searchText.value = "hit";
+ await flushSearchDebounce();
+ expect(search.currentMatchIndex.value).toBe(0);
+ expect(onNavigate).toHaveBeenCalledTimes(1);
+ expect(onNavigate).toHaveBeenLastCalledWith({ kind: "cell", displayRow: 0, col: 0 });
+
+ // User navigates to match 1
+ search.navigateMatch(1);
+ expect(search.currentMatchIndex.value).toBe(1);
+ expect(onNavigate).toHaveBeenCalledTimes(2);
+ expect(onNavigate).toHaveBeenLastCalledWith({ kind: "cell", displayRow: 1, col: 0 });
+
+ // User edits an unrelated column on row 1 (or sets it to NULL), updating rows
+ onNavigate.mockClear();
+ rows.value = [
+ ["hit", "other1"],
+ ["hit", "null"],
+ ];
+ await nextTick();
+ await nextTick();
+
+ // Match position must remain at index 1 and onNavigate must NOT be called
+ expect(search.currentMatchIndex.value).toBe(1);
+ expect(search.currentMatch.value).toEqual({ kind: "cell", displayRow: 1, col: 0 });
+ expect(onNavigate).not.toHaveBeenCalled();
+ });
+
+ it("preserves match index without onNavigate when cell is set to null or rows reload after commit (#13241)", async () => {
+ vi.useFakeTimers();
+ const rows = ref<Array<[string, string | null]>>([
+ ["user-1", "val1"],
+ ["user-2", "val2"],
+ ]);
+ const onNavigate = vi.fn();
+ const search = useDataGridSearch({
+ columns: ["_id", "status"],
+ rows,
+ getCellSearchText: (row, column) => (row[column] ?? "").toLowerCase(),
+ onNavigate,
+ });
+ search.searchText.value = "user";
+ await flushSearchDebounce();
+ expect(search.currentMatchIndex.value).toBe(0);
+ expect(onNavigate).toHaveBeenCalledTimes(1);
+
+ // Navigate to second document
+ search.navigateMatch(1);
+ expect(search.currentMatchIndex.value).toBe(1);
+ expect(onNavigate).toHaveBeenCalledTimes(2);
+
+ // Set field to null
+ onNavigate.mockClear();
+ rows.value = [
+ ["user-1", "val1"],
+ ["user-2", null],
+ ];
+ await nextTick();
+ await nextTick();
+ expect(search.currentMatchIndex.value).toBe(1);
+ expect(onNavigate).not.toHaveBeenCalled();
+
+ // Reload after commit with fresh objects but same data
+ rows.value = [
+ ["user-1", "val1"],
+ ["user-2", null],
+ ];
+ await nextTick();
+ await nextTick();
+ expect(search.currentMatchIndex.value).toBe(1);
+ expect(onNavigate).not.toHaveBeenCalled();
+ });
});
describe("useDataGridSearch persistence (#8524)", () => {
diff --git a/apps/desktop/src/composables/useDataGridSearch.ts b/apps/desktop/src/composables/useDataGridSearch.ts
index e4d1b2f507..1fa0b229d5 100644
--- a/apps/desktop/src/composables/useDataGridSearch.ts
+++ b/apps/desktop/src/composables/useDataGridSearch.ts
@@ -115,12 +115,18 @@ export function useDataGridSearch<Row>(options: UseDataGridSearchOptions<Row>) {
// A single-use token rather than a boolean flag: it is immune to Vue's async
// watcher flush and self-invalidates if the user types during the restore window.
let restoreToken: { searchText: string; matchIndex: number } | null = null;
+ let lastMatchKeys: number[] = [];
+ let userQueryRevision = 0;
+ let lastHandledUserQueryRevision = 0;
watch([searchText, () => toValue(options.caseSensitive), () => toValue(options.literalQuery)], ([value]) => {
clearTimer();
const text = toValue(options.literalQuery) ? value : value.trim();
const query = toValue(options.literalQuery) || effectiveCaseSensitive.value ? text : text.toLowerCase();
const restoring = restoreToken !== null && restoreToken.searchText === value;
+ if (!restoring) {
+ userQueryRevision++;
+ }
if (!query) deferredSearchText.value = "";
// Restoring is not typing: resolve the query now so the row set (and, in
// "filter" search mode, the content height) is settled before the grid's
@@ -156,8 +162,12 @@ export function useDataGridSearch<Row>(options: UseDataGridSearchOptions<Row>) {
watch(matchKeys, (value) => {
const restored = restoreToken;
restoreToken = null;
+ const isUserQuery = userQueryRevision !== lastHandledUserQueryRevision;
+ lastHandledUserQueryRevision = userQueryRevision;
+
if (!value.length) {
currentMatchIndex.value = -1;
+ lastMatchKeys = [];
return;
}
if (restored) {
@@ -165,11 +175,30 @@ export function useDataGridSearch<Row>(options: UseDataGridSearchOptions<Row>) {
// scroll restore owns the viewport (#8524). Clamp because the match list may
// have shifted while the tab was away.
currentMatchIndex.value = Math.min(Math.max(restored.matchIndex, 0), value.length - 1);
+ lastMatchKeys = value;
+ return;
+ }
+ if (isUserQuery) {
+ currentMatchIndex.value = 0;
+ lastMatchKeys = value;
+ const firstMatch = matchAt(0);
+ if (firstMatch) nextTick(() => options.onNavigate?.(firstMatch));
return;
}
- currentMatchIndex.value = 0;
- const firstMatch = matchAt(0);
- if (firstMatch) nextTick(() => options.onNavigate?.(firstMatch));
+
+ // Rows or cell edits changed the underlying data without a search query change:
+ // retain the current match position and do not navigate/scroll the viewport (#13241).
+ if (lastMatchKeys.length > 0 && currentMatchIndex.value >= 0 && currentMatchIndex.value < lastMatchKeys.length) {
+ const prevKey = lastMatchKeys[currentMatchIndex.value];
+ const nextIndex = value.indexOf(prevKey);
+ if (nextIndex >= 0) {
+ currentMatchIndex.value = nextIndex;
+ lastMatchKeys = value;
+ return;
+ }
+ }
+ currentMatchIndex.value = Math.min(Math.max(currentMatchIndex.value, 0), value.length - 1);
+ lastMatchKeys = value;
});
function acceptSuggestion(index = suggestionIndex.value) {