Skip to content

Commit 725573f

Browse files
t3dotggclaude
andauthored
fix(web): double-click selects the whole inline code span (#26639)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 26caea2 commit 725573f

2 files changed

Lines changed: 27 additions & 0 deletions

File tree

‎apps/web/src/components/ChatMarkdown.browser.test.tsx‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,22 @@ describe("ChatMarkdown headings", () => {
328328
});
329329
});
330330

331+
describe("ChatMarkdown inline code selection", () => {
332+
it("selects the whole inline code span on double-click, but only a word in fenced code", async () => {
333+
setup();
334+
await using screen = await renderText(
335+
"Run `migration-gate --dry-run` first.\n\n```txt\nfenced-block word\n```",
336+
);
337+
await userEvent.dblClick(screen.getByText("migration-gate --dry-run"));
338+
expect(window.getSelection()?.toString()).toBe("migration-gate --dry-run");
339+
340+
window.getSelection()?.removeAllRanges();
341+
const fenced = screen.container.querySelector("pre code")!;
342+
await userEvent.dblClick(fenced, { position: { x: 4, y: 4 } });
343+
expect(window.getSelection()?.toString()).toBe("fenced");
344+
});
345+
});
346+
331347
describe("ChatMarkdown Windows file links", () => {
332348
const environmentId = EnvironmentId.make("env-windows");
333349
const renderWindows = (text: string, parseRawHtml: boolean) =>

‎apps/web/src/components/ChatMarkdown.tsx‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ import React, {
7474
Suspense,
7575
type ComponentProps,
7676
type ClipboardEvent as ReactClipboardEvent,
77+
type MouseEvent as ReactMouseEvent,
7778
use,
7879
memo,
7980
useLayoutEffect,
@@ -942,6 +943,11 @@ function markdownHeadingRenderer(level: 1 | 2 | 3 | 4 | 5 | 6) {
942943
};
943944
}
944945

946+
// Native word selection stops at hyphens and dots, so a double-click takes the whole span.
947+
function selectInlineCode(event: ReactMouseEvent<HTMLElement>) {
948+
window.getSelection()?.selectAllChildren(event.currentTarget);
949+
}
950+
945951
const CHAT_MARKDOWN_COMPONENTS = {
946952
h1: markdownHeadingRenderer(1),
947953
h2: markdownHeadingRenderer(2),
@@ -1334,6 +1340,11 @@ const CHAT_MARKDOWN_COMPONENTS = {
13341340
inlineCodeFilePathCandidate(codeText) ?? codeText.trim(),
13351341
);
13361342
}
1343+
return (
1344+
<code {...props} className={className} onDoubleClick={selectInlineCode}>
1345+
{children}
1346+
</code>
1347+
);
13371348
}
13381349
return (
13391350
<code {...props} className={className}>

0 commit comments

Comments
 (0)