From ea22dc36523f7926f931395c5e611d4bbcabd056 Mon Sep 17 00:00:00 2001 From: gggaiitx <62124152+gggaiitx@users.noreply.github.com> Date: Mon, 13 Jul 2026 16:19:12 +0800 Subject: [PATCH] feat(editor): focus newly created query editors --- .../src/components/editor/QueryEditor.vue | 14 +++++- .../src/components/layout/ContentArea.vue | 1 + .../__tests__/editor/queryEditorFocus.spec.ts | 49 +++++++++++++++++++ .../src/lib/editor/queryEditorFocus.ts | 21 ++++++++ 4 files changed, 83 insertions(+), 2 deletions(-) create mode 100644 apps/desktop/src/lib/__tests__/editor/queryEditorFocus.spec.ts create mode 100644 apps/desktop/src/lib/editor/queryEditorFocus.ts 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 = {}): 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(/