From 0f705d03c2f259f996a72504a16371808a464797 Mon Sep 17 00:00:00 2001 From: Walter Aquino Date: Tue, 1 Dec 2026 15:03:10 -0300 Subject: [PATCH 1/2] fix(app): WAL-131 foco en el editor SQL y Ctrl+A sin seleccionar la interfaz MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit La ventana SQL enfoca su editor al abrirse o al volver a su pestaña, igual que la de programa. Ctrl+A fuera de un campo de texto o de la grilla selecciona el texto del editor activo en vez de toda la interfaz. Co-Authored-By: Claude Opus 5.5 --- app/src/App.test.tsx | 46 ++++++++++++++++++++++++++++++++ app/src/App.tsx | 14 ++++++++++ app/src/components/SqlWindow.tsx | 15 ++++++++++- docs/ESTADO.md | 2 ++ docs/behavior/sql.md | 2 +- 5 files changed, 77 insertions(+), 2 deletions(-) diff --git a/app/src/App.test.tsx b/app/src/App.test.tsx index 3e46e4f..d65ce4f 100644 --- a/app/src/App.test.tsx +++ b/app/src/App.test.tsx @@ -1567,3 +1567,49 @@ test("clic derecho en una pestaña cierra varias ventanas y deja las de transacc await waitFor(() => expect(tabs.queryByRole("button", { name: /^Programa/ })).not.toBeInTheDocument()); expect(tabs.getByRole("button", { name: /^SQL 1/ })).toBeInTheDocument(); }); + +test("una ventana SQL nueva o reactivada deja el foco en el editor", async () => { + windowEvents.close.mockResolvedValue(() => {}); + vi.mocked(api.listObjects).mockResolvedValue([]); + vi.mocked(api.sqlOpen).mockResolvedValueOnce("w1").mockResolvedValueOnce("w2"); + render(); + await connectAs(); + fireEvent.keyDown(window, { key: "n", ctrlKey: true }); + const first = await screen.findByRole("textbox", { name: "SQL" }); + await waitFor(() => expect(first).toHaveFocus()); + fireEvent.keyDown(window, { key: "n", ctrlKey: true }); + const tabs = within(screen.getByLabelText("Programas abiertos")); + await tabs.findByRole("button", { name: /^SQL 2/ }); + await waitFor(() => expect(first).not.toHaveFocus()); + const tab = tabs.getByRole("button", { name: /^SQL 1/ }); + tab.focus(); + fireEvent.click(tab); + await waitFor(() => expect(first).toHaveFocus()); +}); + +test("Ctrl A fuera del editor selecciona el texto de la ventana activa y no la interfaz", async () => { + windowEvents.close.mockResolvedValue(() => {}); + vi.mocked(api.listObjects).mockResolvedValue([]); + vi.mocked(api.sqlOpen).mockResolvedValueOnce("w1"); + render(); + await connectAs(); + fireEvent.keyDown(window, { key: "n", ctrlKey: true }); + const editor = await screen.findByRole("textbox", { name: "SQL" }); + const view = EditorView.findFromDOM(editor.closest(".cm-editor")!)!; + act(() => view.dispatch({ changes: { from: 0, insert: "select 1 from dual" } })); + const tab = within(screen.getByLabelText("Programas abiertos")).getByRole("button", { name: /^SQL 1/ }); + tab.focus(); + let handled = false; + act(() => { handled = press(tab, { key: "a", ctrlKey: true }); }); + expect(handled).toBe(true); + expect(editor).toHaveFocus(); + expect(view.state.selection.main).toMatchObject({ from: 0, to: 18 }); +}); + +test("Ctrl A en un campo de texto conserva la selección nativa", async () => { + windowEvents.close.mockResolvedValue(() => {}); + render(); + const input = (await screen.findAllByRole("textbox"))[0]; + input.focus(); + expect(press(input, { key: "a", ctrlKey: true })).toBe(false); +}); diff --git a/app/src/App.tsx b/app/src/App.tsx index 7d94169..420969b 100644 --- a/app/src/App.tsx +++ b/app/src/App.tsx @@ -39,6 +39,8 @@ import { neighbor, reorder } from "./tabs"; import { captureWorkspace, validateWorkspace } from "./workspace"; import { Logo } from "./components/Logo"; import { ContextMenu, type ContextItem } from "./components/ContextMenu"; +import { EditorView } from "@codemirror/view"; +import { selectAll } from "@codemirror/commands"; import { closeNotice, closeScopes, discardMessage, planClose } from "./closeTabs"; import { CommandPalette } from "./components/CommandPalette"; import { routineDraft, type RoutineOverload } from "./routineTest"; @@ -397,6 +399,18 @@ export default function App() { isFieldKey(e) ) return; + // Ctrl+A fuera de un campo seleccionaría toda la interfaz: va al editor de la pestaña activa. + if (matches(e, "Ctrl+A") && !(target instanceof HTMLElement && target.closest("input, textarea, [contenteditable=\"true\"], .cm-editor"))) { + e.preventDefault(); + const editor = [...document.querySelectorAll(".program-document .cm-editor")] + .find((element) => !element.closest("[hidden]")); + const view = editor && EditorView.findFromDOM(editor); + if (view) { + view.focus(); + selectAll(view); + } + return; + } handleShortcut(e, actions); }; window.addEventListener("keydown", onKey); diff --git a/app/src/components/SqlWindow.tsx b/app/src/components/SqlWindow.tsx index 503dd32..65f8132 100644 --- a/app/src/components/SqlWindow.tsx +++ b/app/src/components/SqlWindow.tsx @@ -1004,6 +1004,19 @@ export function SqlWindow({ // eslint-disable-next-line react-hooks/exhaustive-deps }, []); + // Al abrir o volver a la pestaña el foco va al editor, salvo que ya esté dentro de la ventana o haya un diálogo. + const root = useRef(null); + const wasActive = useRef(false); + useEffect(() => { + const activated = active && !wasActive.current; + wasActive.current = active; + if (!activated || command) return; + const focused = document.activeElement; + if (focused && root.current?.contains(focused)) return; + if (document.querySelector('[aria-modal="true"], dialog[open]')) return; + view.current?.focus(); + }, [active, command]); + const offset = grid?.hiddenRowid ? 1 : 0; const insertSource = activeQuery?.sql; const insertTarget = useMemo(() => insertSource ? editableQuery(insertSource)?.table : undefined, [insertSource]); @@ -1067,7 +1080,7 @@ export function SqlWindow({ {confirmationDialog}; return ( -
+