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(editor): eliminate ui lag in hover column search

lxk95587ff3f4256be
diff --git a/apps/desktop/src/components/editor/queryEditorHoverContent.ts b/apps/desktop/src/components/editor/queryEditorHoverContent.ts
index 598054bdb3..8341ca45b2 100644
--- a/apps/desktop/src/components/editor/queryEditorHoverContent.ts
+++ b/apps/desktop/src/components/editor/queryEditorHoverContent.ts
@@ -85,6 +85,7 @@ export function createQueryEditorHoverContent(options: QueryEditorHoverContentOp
originalHtml: sqlContainer.innerHTML,
placeholder: t("grid.hoverSearchPlaceholder"),
noResultLabel: t("grid.hoverSearchNoResult"),
+ debounceMs: 120,
});
dom.appendChild(searchController.element);
diff --git a/apps/desktop/src/lib/editor/__tests__/sqlHoverSearch.spec.ts b/apps/desktop/src/lib/editor/__tests__/sqlHoverSearch.spec.ts
index ef61f43985..ccb82497b2 100644
--- a/apps/desktop/src/lib/editor/__tests__/sqlHoverSearch.spec.ts
+++ b/apps/desktop/src/lib/editor/__tests__/sqlHoverSearch.spec.ts
@@ -416,4 +416,105 @@ describe("createHoverSearch", () => {
expect(count.textContent).toBe("1 / 2");
expect(activeMarks(target)).toEqual([firstMatch]);
});
+
+ it("does not run search while IME composition is active, but runs after compositionend", () => {
+ const { target, input } = setup("id + amount");
+ input.dispatchEvent(new CompositionEvent("compositionstart", { bubbles: true }));
+
+ input.value = "amount";
+ input.dispatchEvent(new Event("input"));
+ // While composing, no marks should be injected into target
+ expect(target.querySelector("mark")).toBeNull();
+
+ input.dispatchEvent(new CompositionEvent("compositionend", { bubbles: true }));
+ // After composition ends, search is executed
+ expect(target.querySelector("mark")).not.toBeNull();
+ expect(target.querySelector("mark")?.textContent).toBe("amount");
+ });
+
+ it("debounces rapid input events when debounceMs is specified and flushes on Enter", () => {
+ vi.useFakeTimers();
+ try {
+ const target = document.createElement("div");
+ target.textContent = DDL;
+ const controller = createHoverSearch({
+ target,
+ originalHtml: target.innerHTML,
+ placeholder: "Search columns…",
+ noResultLabel: "No matching columns",
+ debounceMs: 120,
+ });
+ const input = controller.element.querySelector<HTMLInputElement>('[data-sql-hover-search-input="true"]')!;
+ const count = controller.element.querySelector<HTMLElement>('[data-sql-hover-search-count="true"]')!;
+
+ input.value = "customer";
+ input.dispatchEvent(new Event("input"));
+ // Immediately after input, before timer fires, no marks yet
+ expect(target.querySelector("mark")).toBeNull();
+
+ // Pressing Enter immediately flushes the pending search and activates match
+ pressKey(input, "Enter");
+ expect(target.querySelector("mark")).not.toBeNull();
+ expect(count.textContent).toBe("1 / 1");
+
+ // Now type another query
+ input.value = "amount";
+ input.dispatchEvent(new Event("input"));
+ expect(target.querySelector("mark")?.textContent).toBe("customer");
+
+ // Advance debounce timer
+ vi.advanceTimersByTime(120);
+ expect(target.querySelector("mark")?.textContent).toBe("amount");
+ } finally {
+ vi.useRealTimers();
+ }
+ });
+
+ it("cancels pending debounce timer on destroy", () => {
+ vi.useFakeTimers();
+ try {
+ const target = document.createElement("div");
+ target.textContent = DDL;
+ const controller = createHoverSearch({
+ target,
+ originalHtml: target.innerHTML,
+ placeholder: "Search columns…",
+ noResultLabel: "No matching columns",
+ debounceMs: 120,
+ });
+ const input = controller.element.querySelector<HTMLInputElement>('[data-sql-hover-search-input="true"]')!;
+
+ input.value = "amount";
+ input.dispatchEvent(new Event("input"));
+ controller.destroy();
+
+ vi.advanceTimersByTime(200);
+ expect(target.querySelector("mark")).toBeNull();
+ } finally {
+ vi.useRealTimers();
+ }
+ });
+
+ it("avoids mutating target innerHTML when non-matching queries are entered on pristine content", () => {
+ const target = document.createElement("div");
+ target.textContent = DDL;
+ const initialFirstChild = target.firstChild;
+ const controller = createHoverSearch({
+ target,
+ originalHtml: target.innerHTML,
+ placeholder: "Search columns…",
+ noResultLabel: "No matching columns",
+ });
+ const input = controller.element.querySelector<HTMLInputElement>('[data-sql-hover-search-input="true"]')!;
+
+ input.value = "zzz_missing";
+ input.dispatchEvent(new Event("input"));
+ // Since target was pristine and no matches were found, target children were untouched
+ expect(target.firstChild).toBe(initialFirstChild);
+ expect(controller.status.hidden).toBe(false);
+
+ input.value = "zzz_missing_2";
+ input.dispatchEvent(new Event("input"));
+ expect(target.firstChild).toBe(initialFirstChild);
+ });
});
diff --git a/apps/desktop/src/lib/editor/sqlHoverSearch.ts b/apps/desktop/src/lib/editor/sqlHoverSearch.ts
index 96bb26974e..54627888d5 100644
--- a/apps/desktop/src/lib/editor/sqlHoverSearch.ts
+++ b/apps/desktop/src/lib/editor/sqlHoverSearch.ts
@@ -131,6 +131,8 @@ export interface HoverSearchOptions {
placeholder: string;
/** Message shown when a non-empty query has no matches. */
noResultLabel: string;
+ /** Optional debounce delay in milliseconds for typing. Defaults to 0. */
+ debounceMs?: number;
}
/**
@@ -143,7 +145,7 @@ export interface HoverSearchOptions {
* Enter/composition never fire or dismiss the tooltip while typing.
*/
export function createHoverSearch(options: HoverSearchOptions): HoverSearchController {
- const { target, originalHtml, placeholder, noResultLabel } = options;
+ const { target, originalHtml, placeholder, noResultLabel, debounceMs = 0 } = options;
const element = document.createElement("div");
element.dataset.sqlHoverSearch = "true";
@@ -177,6 +179,25 @@ export function createHoverSearch(options: HoverSearchOptions): HoverSearchContr
let matchGroups: HTMLElement[][] = [];
let activeMatchIndex = -1;
let isComposing = false;
+ let isTargetPristine = true;
+ let lastSearchQuery: string | null = null;
+ let debounceTimer: ReturnType<typeof setTimeout> | null = null;
+
+ // Cache pristine text content once from target/originalHtml to avoid
+ // querying target.textContent on every keystroke.
+ let pristineText = target.textContent ?? "";
+ if (!pristineText && originalHtml) {
+ const temp = document.createElement("div");
+ temp.innerHTML = originalHtml;
+ pristineText = temp.textContent ?? "";
+ }
+
+ const cancelPending = () => {
+ if (debounceTimer !== null) {
+ clearTimeout(debounceTimer);
+ debounceTimer = null;
+ }
+ };
const activateMatch = (index: number) => {
for (const mark of matchGroups[activeMatchIndex] ?? []) mark.removeAttribute(ACTIVE_ATTRIBUTE);
@@ -186,30 +207,67 @@ export function createHoverSearch(options: HoverSearchOptions): HoverSearchContr
matchGroups[index][0]?.scrollIntoView({ block: "nearest" });
};
- const runSearch = () => {
- // Restore the pristine highlighted DDL, then re-apply match highlights.
- target.innerHTML = originalHtml;
- matchGroups = [];
- activeMatchIndex = -1;
+ const executeSearch = () => {
+ if (isComposing) return;
const query = input.value;
- // Search the rendered text, not the raw DDL string: the highlighter emits
- // `<br>` for line breaks, so `textContent` differs from the raw DDL by the
- // dropped newline characters — and the walker's offsets live in the
- // `textContent` domain. Using any other corpus shifts every match after
- // the first line.
- const matches = findHoverSearchMatches(target.textContent ?? "", query);
+ if (query === lastSearchQuery) return;
+ lastSearchQuery = query;
+
const hasQuery = query.trim().length > 0;
- matchCount.hidden = !hasQuery;
- matchCount.textContent = hasQuery ? "0 / 0" : "";
- status.hidden = !hasQuery || matches.length > 0;
- if (matches.length === 0) return;
+ if (!hasQuery) {
+ if (!isTargetPristine) {
+ target.innerHTML = originalHtml;
+ isTargetPristine = true;
+ }
+ matchGroups = [];
+ activeMatchIndex = -1;
+ matchCount.hidden = true;
+ matchCount.textContent = "";
+ status.hidden = true;
+ return;
+ }
+
+ const matches = findHoverSearchMatches(pristineText, query);
+ matchCount.hidden = false;
+ matchCount.textContent = matches.length > 0 ? "" : "0 / 0";
+ status.hidden = matches.length > 0;
+ if (matches.length === 0) {
+ if (!isTargetPristine) {
+ target.innerHTML = originalHtml;
+ isTargetPristine = true;
+ }
+ matchGroups = [];
+ activeMatchIndex = -1;
+ return;
+ }
+
+ target.innerHTML = originalHtml;
const marks = applyHoverSearchHighlights(target, matches);
+ isTargetPristine = false;
matchGroups = Array.from({ length: matches.length }, () => []);
for (const mark of marks) matchGroups[Number(mark.getAttribute(MATCH_INDEX_ATTRIBUTE))].push(mark);
activateMatch(0);
};
+ const scheduleSearch = () => {
+ if (isComposing) return;
+ cancelPending();
+ if (debounceMs <= 0) {
+ executeSearch();
+ } else {
+ debounceTimer = setTimeout(() => {
+ debounceTimer = null;
+ executeSearch();
+ }, debounceMs);
+ }
+ };
+
+ const flushSearch = () => {
+ cancelPending();
+ executeSearch();
+ };
+
const stopKeyboard = (event: KeyboardEvent) => {
// Keep editor keymap shortcuts and IME Enter/Escape from leaking out of the
// input; Escape clears the search but keeps the tooltip open.
@@ -217,12 +275,19 @@ export function createHoverSearch(options: HoverSearchOptions): HoverSearchContr
if (event.type !== "keydown" || isComposing || event.isComposing || event.keyCode === 229) return;
if (event.key === "Enter" && !event.ctrlKey && !event.metaKey && !event.altKey) {
event.preventDefault();
- if (matchGroups.length > 0) activateMatch((activeMatchIndex + (event.shiftKey ? -1 : 1) + matchGroups.length) % matchGroups.length);
+ const hadPending = debounceTimer !== null || input.value !== lastSearchQuery;
+ flushSearch();
+ if (matchGroups.length > 0) {
+ if (!hadPending) {
+ activateMatch((activeMatchIndex + (event.shiftKey ? -1 : 1) + matchGroups.length) % matchGroups.length);
+ }
+ }
}
if (event.key === "Escape" && input.value) {
event.preventDefault();
+ cancelPending();
input.value = "";
- runSearch();
+ executeSearch();
}
};
const stopPointer = (event: Event) => {
@@ -230,12 +295,14 @@ export function createHoverSearch(options: HoverSearchOptions): HoverSearchContr
};
const startComposition = () => {
isComposing = true;
+ cancelPending();
};
const endComposition = () => {
isComposing = false;
+ scheduleSearch();
};
- input.addEventListener("input", runSearch);
+ input.addEventListener("input", scheduleSearch);
input.addEventListener("keydown", stopKeyboard);
input.addEventListener("keyup", stopKeyboard);
input.addEventListener("compositionstart", startComposition);
@@ -250,7 +317,8 @@ export function createHoverSearch(options: HoverSearchOptions): HoverSearchContr
input.focus();
},
destroy() {
- input.removeEventListener("input", runSearch);
+ cancelPending();
+ input.removeEventListener("input", scheduleSearch);
input.removeEventListener("keydown", stopKeyboard);
input.removeEventListener("keyup", stopKeyboard);
input.removeEventListener("compositionstart", startComposition);