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。

feat(editor): focus newly created query editors

gggaiitxea22dc36523f
diff --git a/apps/desktop/src/components/editor/QueryEditor.vue b/apps/desktop/src/components/editor/QueryEditor.vue
index a3da6b68e2..56e80016d3 100644
--- a/apps/desktop/src/components/editor/QueryEditor.vue
+++ b/apps/desktop/src/components/editor/QueryEditor.vue
@@ -58,6 +58,7 @@ import { normalizeShortcutSettings, shortcutToCodeMirrorKey } from "@/lib/editor
import { trimmedSelectionLayer } from "@/lib/editor/codemirrorTrimmedSelectionLayer";
import { selectionMatchOccurrences } from "@/lib/editor/codemirrorSelectionMatches";
import { createInsertValueHintsExtension, requestInsertValueHintsRefresh } from "@/lib/editor/codemirrorInsertValueHints";
+import { focusEditorView } from "@/lib/editor/queryEditorFocus";
import { createDbxCodeMirrorSqlDialect } from "@/lib/editor/codemirrorSqlDialect";
import { startsQueryEditorRectangularSelection } from "@/lib/editor/queryEditorPointerSelection";
import { isSchemaAware, isSingleDatabase, supportsSqlInListPaste } from "@/lib/database/databaseFeatureSupport";
@@ -82,6 +83,7 @@ const props = defineProps<{
executionError?: string;
executionErrorSql?: string;
readOnly?: boolean;
+ autoFocus?: boolean;
forceWordWrap?: boolean;
hideExecutionControls?: boolean;
initialViewport?: { scrollTop: number; scrollLeft: number };
@@ -3120,6 +3122,15 @@ onMounted(async () => {
cachedCompletionObjects = [];
scheduleSemanticDiagnostics();
+ if (props.autoFocus) {
+ // Query tabs opt in; shared editor instances must preserve the surrounding UI focus.
+ nextTick(() => {
+ requestAnimationFrame(() => {
+ focusEditorView(view.value);
+ });
+ });
+ }
+
// Ensure theme is applied with the latest settings after mount
void nextTick(async () => {
if (!view.value || !codeMirrorTheme) return;
@@ -3369,8 +3380,7 @@ function restoreEditorSelection() {
function restoreEditorFocus() {
const focusEditorAcrossFrames = () => {
- if (!view.value || view.value.hasFocus) return;
- view.value.focus();
+ focusEditorView(view.value);
};
focusEditorAcrossFrames();
nextTick(() => {
diff --git a/apps/desktop/src/components/layout/ContentArea.vue b/apps/desktop/src/components/layout/ContentArea.vue
index 972ddc2ec5..a7e13449ea 100644
--- a/apps/desktop/src/components/layout/ContentArea.vue
+++ b/apps/desktop/src/components/layout/ContentArea.vue
@@ -766,6 +766,7 @@ defineExpose({ focusSearch, refreshData, handleModRTarget, requestQueryEditorExe
<QueryEditor
ref="queryEditorRef"
class="relative z-0 flex-1"
+ auto-focus
:model-value="activeTab.sql"
:connection-id="activeTab.connectionId"
:database="activeTab.database"
diff --git a/apps/desktop/src/lib/__tests__/editor/queryEditorFocus.spec.ts b/apps/desktop/src/lib/__tests__/editor/queryEditorFocus.spec.ts
new file mode 100644
index 0000000000..59ff8fe965
--- /dev/null
+++ b/apps/desktop/src/lib/__tests__/editor/queryEditorFocus.spec.ts
@@ -0,0 +1,49 @@
+import { readFileSync } from "node:fs";
+import { describe, expect, it, vi } from "vitest";
+import { focusEditorView, type EditorViewLike } from "@/lib/editor/queryEditorFocus";
+
+const queryEditorSource = readFileSync(new URL("../../../components/editor/QueryEditor.vue", import.meta.url), "utf8");
+const contentAreaSource = readFileSync(new URL("../../../components/layout/ContentArea.vue", import.meta.url), "utf8");
+
+function createMockView(overrides: Partial<EditorViewLike> = {}): EditorViewLike {
+ return {
+ hasFocus: false,
+ focus: vi.fn(),
+ ...overrides,
+ };
+}
+
+describe("focusEditorView", () => {
+ it("focuses the editor when view exists and does not have focus", () => {
+ const view = createMockView({ hasFocus: false });
+ const result = focusEditorView(view);
+ expect(result).toBe(true);
+ expect(view.focus).toHaveBeenCalledOnce();
+ });
+
+ it("skips focus when the editor already has focus", () => {
+ const view = createMockView({ hasFocus: true });
+ const result = focusEditorView(view);
+ expect(result).toBe(false);
+ expect(view.focus).not.toHaveBeenCalled();
+ });
+
+ it("returns false when view is null", () => {
+ expect(focusEditorView(null)).toBe(false);
+ });
+
+ it("returns false when view is undefined", () => {
+ expect(focusEditorView(undefined)).toBe(false);
+ });
+});
+
+describe("QueryEditor auto focus wiring", () => {
+ it("keeps auto focus opt-in for shared editor instances", () => {
+ expect(queryEditorSource).toContain("autoFocus?: boolean;");
+ expect(queryEditorSource).toMatch(/if \(props\.autoFocus\) \{[\s\S]*focusEditorView\(view\.value\);/);
+ });
+
+ it("enables auto focus for query tabs", () => {
+ expect(contentAreaSource).toMatch(/<QueryEditor[\s\S]*?\sauto-focus\s[\s\S]*?:model-value="activeTab\.sql"/);
+ });
+});
diff --git a/apps/desktop/src/lib/editor/queryEditorFocus.ts b/apps/desktop/src/lib/editor/queryEditorFocus.ts
new file mode 100644
index 0000000000..f54ac3f131
--- /dev/null
+++ b/apps/desktop/src/lib/editor/queryEditorFocus.ts
@@ -0,0 +1,21 @@
+/**
+ * Focus logic for the query editor CodeMirror view.
+ *
+ * Extracted from QueryEditor.vue so the guard condition can be unit-tested
+ * without mounting the full Vue component.
+ */
+
+export interface EditorViewLike {
+ hasFocus: boolean;
+ focus(): void;
+}
+
+/**
+ * Focus the editor if it exists and does not already have focus.
+ * Returns `true` when `view.focus()` was actually called, `false` otherwise.
+ */
+export function focusEditorView(view: EditorViewLike | null | undefined): boolean {
+ if (!view || view.hasFocus) return false;
+ view.focus();
+ return true;
+}