Skip to content

Commit 1c65ad0

Browse files
fix(web): Cmd+A in the rendered Markdown preview selects only the file (#30738)
1 parent 3e90486 commit 1c65ad0

2 files changed

Lines changed: 58 additions & 6 deletions

File tree

‎apps/web/src/components/files/FilePreviewPanel.browser.test.tsx‎

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,11 +121,11 @@ function setup() {
121121
};
122122
}
123123

124-
function markdownPreview(truncated: boolean) {
124+
function markdownPreview(truncated: boolean, contents = "- [ ] Keep the rest of this file\n") {
125125
state.file = {
126126
relativePath: "tasks.md",
127-
contents: "- [ ] Keep the rest of this file\n",
128-
byteLength: truncated ? 1_200_000 : 32,
127+
contents,
128+
byteLength: truncated ? 1_200_000 : contents.length,
129129
truncated,
130130
};
131131
return filePreview("tasks.md");
@@ -278,3 +278,29 @@ describe("rendered Markdown task saves", () => {
278278
});
279279
});
280280
});
281+
282+
describe("rendered Markdown select all", () => {
283+
it("selects only the rendered file, including after a task toggle", async () => {
284+
using _settings = setup();
285+
await using screen = await render(
286+
<>
287+
<p>Transcript text from the chat thread.</p>
288+
{markdownPreview(false, "- [ ] Task item\n\nRendered file body.\n")}
289+
</>,
290+
);
291+
const expectFileOnlySelection = async () => {
292+
await userEvent.keyboard(
293+
isMacPlatform(navigator.platform) ? "{Meta>}a{/Meta}" : "{Control>}a{/Control}",
294+
);
295+
const selection = window.getSelection()?.toString() ?? "";
296+
expect(selection).toContain("Rendered file body.");
297+
expect(selection).not.toContain("Transcript text from the chat thread.");
298+
};
299+
300+
await userEvent.click(screen.getByText("Rendered file body."));
301+
await expectFileOnlySelection();
302+
// A toggled checkbox keeps focus on an input inside the preview.
303+
await userEvent.click(screen.getByRole("checkbox"));
304+
await expectFileOnlySelection();
305+
});
306+
});

‎apps/web/src/components/files/FilePreviewPanel.tsx‎

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,14 @@ import {
4747
import { mediaFileReference } from "@t3tools/client-runtime/media-reference";
4848
import { FolderTree, Globe2, WrapTextIcon } from "lucide-react";
4949
import * as Schema from "effect/Schema";
50-
import { lazy, Suspense, useEffect, useRef, useState } from "react";
50+
import {
51+
lazy,
52+
Suspense,
53+
useEffect,
54+
useRef,
55+
useState,
56+
type KeyboardEvent as ReactKeyboardEvent,
57+
} from "react";
5158

5259
import { isBrowserPreviewFile, openFileInPreview } from "~/browser/openFileInPreview";
5360
import { useAssetUrlRefresh, useAssetUrlState } from "~/assets/assetUrls";
@@ -61,7 +68,7 @@ import { useStableCallback } from "~/hooks/useStableCallback";
6168
import { getLocalStorageItem, setLocalStorageItem, useLocalStorage } from "~/hooks/useLocalStorage";
6269
import { resolveDiffThemeName } from "~/lib/diffRendering";
6370
import { PREFERRED_HIGHLIGHTER } from "~/lib/syntaxHighlighting";
64-
import { cn } from "~/lib/utils";
71+
import { cn, isMacPlatform } from "~/lib/utils";
6572
import { requestWorkspaceFocus } from "~/lib/workspaceFocus";
6673
import type { ChatFileAttachment } from "~/types";
6774
import { isHostPath } from "@t3tools/shared/path";
@@ -933,6 +940,25 @@ function EditableFileSurface({
933940
);
934941
}
935942

943+
function selectRenderedContentOnSelectAll(event: ReactKeyboardEvent<HTMLElement>) {
944+
const mac = isMacPlatform(navigator.platform);
945+
const selectAll =
946+
event.key.toLowerCase() === "a" &&
947+
(mac ? event.metaKey && !event.ctrlKey : event.ctrlKey && !event.metaKey) &&
948+
!event.shiftKey &&
949+
!event.altKey;
950+
// React bubbles keys from portaled children (the image dialog) through this surface.
951+
if (
952+
!selectAll ||
953+
!(event.target instanceof Node) ||
954+
!event.currentTarget.contains(event.target)
955+
) {
956+
return;
957+
}
958+
event.preventDefault();
959+
window.getSelection()?.selectAllChildren(event.currentTarget);
960+
}
961+
936962
function RenderedMarkdownSurface({
937963
environmentId,
938964
cwd,
@@ -960,7 +986,7 @@ function RenderedMarkdownSurface({
960986
useFileSaveSession(saveTarget, onPendingChange);
961987

962988
return (
963-
<ScrollArea className="min-h-0 flex-1">
989+
<ScrollArea className="min-h-0 flex-1" onKeyDown={selectRenderedContentOnSelectAll}>
964990
<FileMarkdownPreview
965991
text={contents}
966992
cwd={cwd}

0 commit comments

Comments
 (0)