diff --git a/src/features/app/appActions.ts b/src/features/app/appActions.ts index 5dc69b50b..cd1a4e499 100644 --- a/src/features/app/appActions.ts +++ b/src/features/app/appActions.ts @@ -4,6 +4,7 @@ import type { Action } from "@/platform/actions"; import { revealInFileManager } from "@/platform/fileManager"; import { t } from "@/platform/i18n"; import { call, commands, isTauri } from "@/platform/ipc"; +import { selectReadingPane } from "@/ui"; import { maybeEditorService, notifications, runUndoHandlers } from "@/workbench/contributions"; import { showAbout } from "./AboutDialog"; import { quitApp } from "./closeGuard"; @@ -48,6 +49,7 @@ export function editCommand(command: "undo" | "redo" | "cut" | "copy" | "paste" } if (focused instanceof Element && runUndoHandlers(command, focused)) return; } + if (command === "selectAll" && selectReadingPane(document.activeElement)) return; if (command === "paste" && navigator.clipboard?.readText) { void navigator.clipboard.readText().then((text) => document.execCommand("insertText", false, text)); return; diff --git a/src/features/http-client/HttpClient.module.css b/src/features/http-client/HttpClient.module.css index 095debd3b..b0400947f 100644 --- a/src/features/http-client/HttpClient.module.css +++ b/src/features/http-client/HttpClient.module.css @@ -138,6 +138,11 @@ flex: 1; min-height: 0; overflow: auto; + outline: none; +} +.tabBody:focus-visible { + outline: 2px solid var(--focus-ring); + outline-offset: -2px; } .pre { margin: 0; diff --git a/src/features/http-client/HttpClientView.tsx b/src/features/http-client/HttpClientView.tsx index cc14e7741..0acbd69b3 100644 --- a/src/features/http-client/HttpClientView.tsx +++ b/src/features/http-client/HttpClientView.tsx @@ -3,10 +3,20 @@ * exchange (right): status line, timing, body (pretty-printed JSON / XML / * HTML or raw), response headers and the request as sent. */ -import { useEffect, useMemo, useState } from "react"; +import { useEffect, useMemo, useRef, useState } from "react"; import clsx from "clsx"; import type { HttpExchange, HttpHistoryEntry } from "@/platform/ipc"; -import { EmptyState, IconButton, SplitGroup, SplitHandle, SplitPanel, Spinner, Tabs, T } from "@/ui"; +import { + EmptyState, + IconButton, + SplitGroup, + SplitHandle, + SplitPanel, + Spinner, + Tabs, + T, + useSelectionBoundary, +} from "@/ui"; import { bodyKind, formatMs, formatSize, prettyBody } from "./format"; import { cancelRunning, clearHistory, refreshHistory, selectEntry, useHttpClient } from "./store"; import styles from "./HttpClient.module.css"; @@ -43,6 +53,8 @@ type Tab = "body" | "headers" | "request"; function ExchangeView({ ex }: { ex: HttpExchange }) { const [tab, setTab] = useState("body"); + const pane = useRef(null); + useSelectionBoundary(pane); const [raw, setRaw] = useState(false); const r = ex.response; const kind = r?.body != null ? bodyKind(r.contentType, r.body) : "text"; @@ -88,7 +100,8 @@ function ExchangeView({ ex }: { ex: HttpExchange }) { ) : undefined } /> -
+ {/* Focusable, so Select All and Copy work on the response like in a read-only editor. */} +
{tab === "body" && (r == null ? (
{ex.entry.error}
diff --git a/src/ui/index.ts b/src/ui/index.ts index cd35b8cba..3471299c1 100644 --- a/src/ui/index.ts +++ b/src/ui/index.ts @@ -37,7 +37,7 @@ export * from "./BarButton"; export { lazyComponent, lazyWhen, prefetchLazyComponents } from "./lazy"; export { Given, T } from "./T"; export { selectableText, copyMenuFor, copyText } from "./selectableText"; -export { useSelectionBoundary } from "./useSelectionBoundary"; +export { selectReadingPane, useSelectionBoundary } from "./useSelectionBoundary"; export { lockDragCursor, type DragCursor } from "./dragCursor"; export { useSanitizedField } from "./useSanitizedField"; export * from "./rowSelection"; diff --git a/src/ui/useSelectionBoundary.test.tsx b/src/ui/useSelectionBoundary.test.tsx index bd739b220..442d16038 100644 --- a/src/ui/useSelectionBoundary.test.tsx +++ b/src/ui/useSelectionBoundary.test.tsx @@ -1,7 +1,7 @@ import { useRef } from "react"; -import { cleanup, render } from "@testing-library/react"; +import { cleanup, fireEvent, render } from "@testing-library/react"; import { afterEach, expect, it } from "vitest"; -import { useSelectionBoundary } from "./useSelectionBoundary"; +import { selectReadingPane, useSelectionBoundary } from "./useSelectionBoundary"; afterEach(() => { window.getSelection()?.removeAllRanges(); @@ -98,3 +98,38 @@ it("removes the boundary listener when the pane unmounts", () => { expect(select(first, 0, last, 13).toString()).toContain("Branch or tag"); container.remove(); }); + +function FocusablePane() { + const ref = useRef(null); + useSelectionBoundary(ref); + return ( +
+

GET /data.json

+ +
+ ); +} + +it("selects only the pane's text on primary+A", () => { + const view = render( + <> +
Request history
+ + , + ); + const pane = view.getByTestId("pane"); + const event = fireEvent.keyDown(pane, { key: "a", ctrlKey: true, metaKey: true }); + expect(event).toBe(false); + const range = window.getSelection()!.getRangeAt(0); + expect(range.startContainer).toBe(pane); + expect(range.endContainer).toBe(pane); + expect(window.getSelection()!.toString()).not.toContain("Request history"); +}); + +it("leaves Select All in an editable field to the field", () => { + const view = render(); + const input = view.getByLabelText("Filter"); + expect(fireEvent.keyDown(input, { key: "a", ctrlKey: true, metaKey: true })).toBe(true); + expect(selectReadingPane(input)).toBe(false); + expect(selectReadingPane(document.body)).toBe(false); +}); diff --git a/src/ui/useSelectionBoundary.ts b/src/ui/useSelectionBoundary.ts index 4823ce2e3..156aee89f 100644 --- a/src/ui/useSelectionBoundary.ts +++ b/src/ui/useSelectionBoundary.ts @@ -1,4 +1,5 @@ import { useEffect, type RefObject } from "react"; +import { hasPrimaryModifier } from "@/platform/keymap"; /** Keep selections started in a reading pane from extending into neighboring panes. */ export function useSelectionBoundary(ref: RefObject): void { @@ -29,13 +30,23 @@ export function useSelectionBoundary(ref: RefObject): void { if (e.button === 0) html.setAttribute("data-selection-lock", ""); }; const unlock = () => html.removeAttribute("data-selection-lock"); + // Select All in the pane selects its text; the browser's would select across the whole + // window (Ctrl+A reaches the webview directly; ⌘A goes to the native Select All). + const selectAll = (e: KeyboardEvent) => { + if (e.key.toLowerCase() !== "a" || !hasPrimaryModifier(e) || e.shiftKey || e.altKey) return; + if (!selectReadingPane(e.target as Element)) return; + e.preventDefault(); + e.stopPropagation(); + }; root.setAttribute("data-selection-scope", ""); + root.addEventListener("keydown", selectAll); root.addEventListener("mousedown", lock); doc.addEventListener("mouseup", unlock, true); doc.addEventListener("dragend", unlock, true); doc.addEventListener("selectionchange", clamp); return () => { root.removeAttribute("data-selection-scope"); + root.removeEventListener("keydown", selectAll); root.removeEventListener("mousedown", lock); doc.removeEventListener("mouseup", unlock, true); doc.removeEventListener("dragend", unlock, true); @@ -67,3 +78,20 @@ function textEdge(root: HTMLElement, side: "start" | "end"): { node: Node; offse if (!text) return { node: root, offset: side === "start" ? 0 : root.childNodes.length }; return { node: text, offset: side === "start" ? 0 : text.textContent!.length }; } + +/** + * Selects the text of the reading pane (`useSelectionBoundary`) that `focused` is in, unless it + * is an editable field, which selects its own text. False when it isn't in one. + */ +export function selectReadingPane(focused: Element | null): boolean { + if (!(focused instanceof HTMLElement) || focused.isContentEditable) return false; + if (focused instanceof HTMLInputElement || focused instanceof HTMLTextAreaElement) return false; + const pane = focused.closest("[data-selection-scope]"); + const selection = pane?.ownerDocument.getSelection(); + if (!pane || !selection) return false; + const range = pane.ownerDocument.createRange(); + range.selectNodeContents(pane); + selection.removeAllRanges(); + selection.addRange(range); + return true; +}