Skip to content
TypeScript

fix(web): double-click selects the whole inline code span (#26639)

Theo Browne725573f35850
diff --git a/apps/web/src/components/ChatMarkdown.browser.test.tsx b/apps/web/src/components/ChatMarkdown.browser.test.tsx
index 345bde84976..745e69087cb 100644
--- a/apps/web/src/components/ChatMarkdown.browser.test.tsx
+++ b/apps/web/src/components/ChatMarkdown.browser.test.tsx
@@ -328,6 +328,22 @@ describe("ChatMarkdown headings", () => {
});
});
+describe("ChatMarkdown inline code selection", () => {
+ it("selects the whole inline code span on double-click, but only a word in fenced code", async () => {
+ setup();
+ await using screen = await renderText(
+ "Run `migration-gate --dry-run` first.\n\n```txt\nfenced-block word\n```",
+ );
+ await userEvent.dblClick(screen.getByText("migration-gate --dry-run"));
+ expect(window.getSelection()?.toString()).toBe("migration-gate --dry-run");
+
+ window.getSelection()?.removeAllRanges();
+ const fenced = screen.container.querySelector("pre code")!;
+ await userEvent.dblClick(fenced, { position: { x: 4, y: 4 } });
+ expect(window.getSelection()?.toString()).toBe("fenced");
+ });
+});
+
describe("ChatMarkdown Windows file links", () => {
const environmentId = EnvironmentId.make("env-windows");
const renderWindows = (text: string, parseRawHtml: boolean) =>
diff --git a/apps/web/src/components/ChatMarkdown.tsx b/apps/web/src/components/ChatMarkdown.tsx
index 32f15bcf542..6a8f80d6d10 100644
--- a/apps/web/src/components/ChatMarkdown.tsx
+++ b/apps/web/src/components/ChatMarkdown.tsx
@@ -74,6 +74,7 @@ import React, {
Suspense,
type ComponentProps,
type ClipboardEvent as ReactClipboardEvent,
+ type MouseEvent as ReactMouseEvent,
use,
memo,
useLayoutEffect,
@@ -942,6 +943,11 @@ function markdownHeadingRenderer(level: 1 | 2 | 3 | 4 | 5 | 6) {
};
}
+// Native word selection stops at hyphens and dots, so a double-click takes the whole span.
+function selectInlineCode(event: ReactMouseEvent<HTMLElement>) {
+ window.getSelection()?.selectAllChildren(event.currentTarget);
+}
+
const CHAT_MARKDOWN_COMPONENTS = {
h1: markdownHeadingRenderer(1),
h2: markdownHeadingRenderer(2),
@@ -1334,6 +1340,11 @@ const CHAT_MARKDOWN_COMPONENTS = {
inlineCodeFilePathCandidate(codeText) ?? codeText.trim(),
);
}
+ return (
+ <code {...props} className={className} onDoubleClick={selectInlineCode}>
+ {children}
+ </code>
+ );
}
return (
<code {...props} className={className}>