From 31016ee6842e4e722f47d94b8bb5d9601a28fcf9 Mon Sep 17 00:00:00 2001 From: Mr0Bread <55754540+Mr0Bread@users.noreply.github.com> Date: Sat, 14 Nov 2026 16:05:29 +0200 Subject: [PATCH] fix(agents): add Copy Link Address to the context menu of links in readable text (#3371) Right-clicking a link in Agent Task chat (and other readable panes using the shared Copy menu) only offered Copy, which copied the selected label. Links now also get Copy Link Address, as in browsers; Copy keeps copying the selection, and in-page anchors get no link item. Co-authored-by: Mr0Bread <55754540+Mr0Bread@users.noreply.github.com> --- docs/en/guide/agents.mdx | 2 ++ docs/zh/guide/agents.mdx | 2 ++ e2e/agentMarkdown.spec.ts | 31 ++++++++++++++++++++++++++ src/platform/locales/text/en.json | 1 + src/platform/locales/text/zh.json | 1 + src/ui/selectableText.test.ts | 37 +++++++++++++++++++++++++++++++ src/ui/selectableText.ts | 13 ++++++++--- 7 files changed, 84 insertions(+), 3 deletions(-) diff --git a/docs/en/guide/agents.mdx b/docs/en/guide/agents.mdx index 62e262d04..c29b94945 100644 --- a/docs/en/guide/agents.mdx +++ b/docs/en/guide/agents.mdx @@ -67,6 +67,8 @@ The task tab shows the conversation: your messages, the agent's replies, its rea On your own messages you can **Edit** (the messages after it are replaced; files already changed stay as they are) or **Retract Message**. On replies you can **Copy Markdown**, **Quote Reply**, **Regenerate Response** and **Branch Conversation from Here**, which starts a new task from that point. +Right-click a link and choose **Copy Link Address** to copy its URL; **Copy** copies the selected text. + ### Permissions When an agent wants to run a command or call a tool that needs approval, the task shows the request and is listed as **Needs Input**. Choose one of the options the agent offers, such as **Allow Once**, **Allow for This Session**, **Always Allow**, **Decline**, **Always Decline** or **Decline and Stop**. Many agents also have a mode picker that approves edits or everything automatically; use it with care. diff --git a/docs/zh/guide/agents.mdx b/docs/zh/guide/agents.mdx index 1e0a6f36d..57d135308 100644 --- a/docs/zh/guide/agents.mdx +++ b/docs/zh/guide/agents.mdx @@ -67,6 +67,8 @@ OpenIDEA 本身不带模型或账号:它启动你已安装并登录的 agent 在你自己的消息上可以 **编辑**(其后的消息会被替换,已经改动的文件保持不变)或 **撤回消息**。在回复上可以 **复制 Markdown**、**引用回复**、**重新生成回复**,以及 **从此处创建分支对话**,从这一点开始一个新任务。 +右键点击链接并选择 **复制链接地址** 可复制它的 URL;**复制** 复制选中的文字。 + ### 权限 agent 要运行命令或调用需要授权的工具时,任务中会显示请求,并归入 **等待输入**。从 agent 提供的选项中选择,例如 **允许一次**、**在此会话中允许**、**始终允许**、**拒绝**、**始终拒绝** 或 **拒绝并停止**。很多 agent 还有模式选择器,可以自动批准编辑甚至所有操作,请谨慎使用。 diff --git a/e2e/agentMarkdown.spec.ts b/e2e/agentMarkdown.spec.ts index 50cc7efdf..5a4505096 100644 --- a/e2e/agentMarkdown.spec.ts +++ b/e2e/agentMarkdown.spec.ts @@ -198,6 +198,37 @@ test("copies reply Markdown with the keyboard and keeps code copying separate", expect(await page.evaluate(() => navigator.clipboard.readText())).toBe("const a = 1;"); }); +test("copies a reply link's address from its context menu and keeps Copy for the selected text", async ({ + page, + context, +}) => { + await context.grantPermissions(["clipboard-read", "clipboard-write"]); + const url = "https://example.com/pull/42?tab=files#diff"; + const reply = await openReply(page, `[**Review changes**](${url})\n\nOrdinary reply text`); + const link = reply.getByRole("link", { name: "Review changes" }); + await link.locator("strong").click({ button: "right" }); + await page.getByRole("menuitem", { name: "Copy Link Address" }).click(); + await expect.poll(() => page.evaluate(() => navigator.clipboard.readText())).toBe(url); + + await link.evaluate((el) => { + const range = document.createRange(); + range.selectNodeContents(el); + window.getSelection()!.removeAllRanges(); + window.getSelection()!.addRange(range); + }); + await link.click({ button: "right" }); + await page + .getByRole("menuitem", { name: /^Copy\b/ }) + .first() + .click(); + await expect.poll(() => page.evaluate(() => navigator.clipboard.readText())).toBe("Review changes"); + + const paragraph = reply.getByText("Ordinary reply text", { exact: true }); + await paragraph.click({ button: "right" }); + await expect(page.getByRole("menuitem", { name: "Copy Link Address" })).toHaveCount(0); + await page.keyboard.press("Escape"); +}); + test("keeps latest reply actions visible and quotes into the draft without shifting content", async ({ page }) => { const reply = await openReply(page, REPLY); const copy = page.getByRole("button", { name: "Copy Markdown", exact: true }); diff --git a/src/platform/locales/text/en.json b/src/platform/locales/text/en.json index 225b0a87b..4dab059a6 100644 --- a/src/platform/locales/text/en.json +++ b/src/platform/locales/text/en.json @@ -13,6 +13,7 @@ "Rename Group": "Rename Group", "Remove from Group": "Remove from Group", "Remove “{name}”? Its tasks move back to their status groups.": "Remove “{name}”? Its tasks move back to their status groups.", + "Copy Link Address": "Copy Link Address", "Pin Agent Tasks": "Pin Agent Tasks", "Unpin Agent Tasks": "Unpin Agent Tasks", "New Agent Task Group": "New Agent Task Group", diff --git a/src/platform/locales/text/zh.json b/src/platform/locales/text/zh.json index 506ebc964..a96c05802 100644 --- a/src/platform/locales/text/zh.json +++ b/src/platform/locales/text/zh.json @@ -8,6 +8,7 @@ "Rename Group": "重命名组", "Remove from Group": "从组中移除", "Remove “{name}”? Its tasks move back to their status groups.": "移除“{name}”?其中的任务将回到各自的状态分组。", + "Copy Link Address": "复制链接地址", "Pin Agent Tasks": "固定 Agent 任务", "Unpin Agent Tasks": "取消固定 Agent 任务", "New Agent Task Group": "新建 Agent 任务组", diff --git a/src/ui/selectableText.test.ts b/src/ui/selectableText.test.ts index 8e50f5667..bf0c2dce8 100644 --- a/src/ui/selectableText.test.ts +++ b/src/ui/selectableText.test.ts @@ -35,6 +35,43 @@ describe("copyMenuFor", () => { expect(items![0]).toMatchObject({ label: "Copy", disabled: false }); }); + it.each([ + ["link", "none"], + ["nested", "link"], + ["text", "elsewhere"], + ])("offers Copy Link Address on a %s target with %s selected", async (target, selection) => { + const writeText = vi.fn().mockResolvedValue(undefined); + Object.defineProperty(navigator, "clipboard", { value: { writeText }, configurable: true }); + const url = "https://example.com/pull/42?tab=files#diff"; + mount( + `
Review changes

Other text

`, + ); + if (selection !== "none") select(document.getElementById(selection)!); + const node = target === "text" ? document.getElementById("nested")!.firstChild : document.getElementById(target); + const items = copyMenuFor(node)!; + expect(items.map((i) => ("label" in i ? i.label : null))).toEqual(["Copy", "Copy Link Address"]); + const link = items[1]!; + if ("run" in link) link.run?.(); + await Promise.resolve(); + expect(writeText).toHaveBeenCalledWith(url); + }); + + it("keeps Copy on a link copying the selected text", async () => { + const writeText = vi.fn().mockResolvedValue(undefined); + Object.defineProperty(navigator, "clipboard", { value: { writeText }, configurable: true }); + mount(`
Review changes
`); + select(document.getElementById("link")!); + const copy = copyMenuFor(document.getElementById("link"))![0]!; + if ("run" in copy) copy.run?.(); + await Promise.resolve(); + expect(writeText).toHaveBeenCalledWith("Review changes"); + }); + + it("offers no Copy Link Address on an anchor without a destination or with an in-page one", () => { + mount(`
SectionTop
`); + for (const id of ["anchor", "local"]) expect(copyMenuFor(document.getElementById(id))).toHaveLength(1); + }); + it("offers a disabled Copy in a selectable pane with nothing selected", () => { mount(`

Hello

`); expect(copyMenuFor(document.getElementById("p"))![0]).toMatchObject({ label: "Copy", disabled: true }); diff --git a/src/ui/selectableText.ts b/src/ui/selectableText.ts index b4055bc16..3e6857c6a 100644 --- a/src/ui/selectableText.ts +++ b/src/ui/selectableText.ts @@ -24,14 +24,19 @@ function selectionCovers(sel: Selection | null, target: Node): boolean { return false; } -/** The Copy menu for a right-click on `target`, or null when it is not on readable text. */ +/** + * The Copy menu for a right-click on `target`, or null when it is not on readable text. On a link + * it also offers Copy Link Address, as browsers do; Copy keeps copying the selected text. + */ export function copyMenuFor(target: EventTarget | null): MenuItem[] | null { const el = target instanceof Element ? target : target instanceof Node ? target.parentElement : null; if (!el || el.closest(EDITABLE)) return null; const sel = window.getSelection(); const text = selectionCovers(sel, el) ? sel!.toString() : ""; - if (!text && !el.closest(`.${selectableText}`)) return null; - return [ + const href = el.closest("a[href]")?.getAttribute("href") ?? ""; + const link = href.startsWith("#") ? "" : href; + if (!text && !link && !el.closest(`.${selectableText}`)) return null; + const items: MenuItem[] = [ { id: "copy", label: "Copy", @@ -41,6 +46,8 @@ export function copyMenuFor(target: EventTarget | null): MenuItem[] | null { run: () => void copyText(text), }, ]; + if (link) items.push({ id: "copy-link", label: "Copy Link Address", run: () => void copyText(link) }); + return items; } /** Writes `text` to the clipboard; falls back to copying the live selection. */