Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(desktop): 聊天消息 chip 复制到外部富文本时不再丢内容 (#552)
Dash42b61e4cba9b
diff --git a/apps/desktop/src/renderer/__tests__/inlineReferenceChip.test.tsx b/apps/desktop/src/renderer/__tests__/inlineReferenceChip.test.tsxindex 20f3ad8d5b..3a641b22dc 100644--- a/apps/desktop/src/renderer/__tests__/inlineReferenceChip.test.tsx+++ b/apps/desktop/src/renderer/__tests__/inlineReferenceChip.test.tsx@@ -1,5 +1,6 @@// @vitest-environment jsdomimport { cleanup, render, screen } from '@testing-library/react';+import userEvent from '@testing-library/user-event';import { File } from 'lucide-react';import { afterEach, describe, expect, it } from 'vitest';@@ -32,4 +33,48 @@ describe('InlineReferenceChip', () => {expect(chip?.getAttribute('draggable')).toBeNull();expect(chip?.getAttribute('title')).toBeNull();});++ // 剪贴板契约。复制一段聊天消息时 Chromium 会把选区原样序列化进 text/html:+ // - `<button>` 不在外部富文本编辑器(Slack / 飞书 / Notion)的粘贴白名单里,+ // 整个节点连同文字会被丢弃,只留下一个断行;+ // - `user-select: none` 让文字连 text/plain 都进不了剪贴板。+ // 两条都会让用户复制出来的消息**缺内容**,所以固化成回归测试。+ it('stays a <span> even when interactive, so pasted HTML keeps the label', () => {+ const { container } = render(+ <InlineReferenceChip label="endpoint.json" onClick={() => {}} />,+ );+ const chip = container.querySelector('[data-inline-reference-chip]');+ expect(chip?.tagName).toBe('SPAN');+ expect(container.querySelector('button')).toBeNull();+ // 交互语义靠 role/tabIndex 补齐,不靠原生按钮元素。+ expect(chip?.getAttribute('role')).toBe('button');+ expect(chip?.getAttribute('tabindex')).toBe('0');+ });++ it('keeps chip text selectable by default, and only opts out for composer atoms', () => {+ const { container: sent } = render(<InlineReferenceChip label="sent.ts" />);+ expect(sent.querySelector('[data-inline-reference-chip]')?.className).not.toContain(+ 'select-none',+ );++ const { container: composer } = render(+ <InlineReferenceChip label="composer.ts" textSelectable={false} />,+ );+ expect(composer.querySelector('[data-inline-reference-chip]')?.className).toContain(+ 'select-none',+ );+ });++ it('activates on Enter and Space like the native button it replaced', async () => {+ const user = userEvent.setup();+ let clicks = 0;+ const { container } = render(+ <InlineReferenceChip label="endpoint.json" onClick={() => { clicks += 1; }} />,+ );+ const chip = container.querySelector('[data-inline-reference-chip]') as HTMLElement;+ chip.focus();+ await user.keyboard('{Enter}');+ await user.keyboard(' ');+ expect(clicks).toBe(2);+ });});diff --git a/apps/desktop/src/renderer/__tests__/markdownTargetRendererContract.test.ts b/apps/desktop/src/renderer/__tests__/markdownTargetRendererContract.test.tsindex 12561f4db3..fb90dfd03b 100644--- a/apps/desktop/src/renderer/__tests__/markdownTargetRendererContract.test.ts+++ b/apps/desktop/src/renderer/__tests__/markdownTargetRendererContract.test.ts@@ -56,6 +56,24 @@ describe('Markdown target rendering contract', () => {expect(markdownRenderer).not.toContain('function FilePathChip');});+ it('renders the path chip as <code>, so copied messages survive an external paste', () => {+ // 复制一段聊天消息时 Chromium 把选区原样序列化进 text/html。`<button>` 是交互+ // 控件,不属于富文本内容模型:Slack / 飞书 / Notion / Word 按标签白名单解析+ // 粘贴内容,会把整个节点连同路径文字丢掉,只在原位留下一个断行——用户看到的+ // 就是"文件名凭空消失"。`<code>` 既是语义正解(这个 chip 本来就是行内代码+ // 升级来的),粘出去还会落成对方的行内代码。+ const chipBody = markdownRenderer.match(/function FileTargetChip\([\s\S]*?\n}\n/)?.[0];+ expect(chipBody).toBeDefined();+ expect(chipBody).toContain('<code');+ expect(chipBody).not.toContain('<button');+ // 原生按钮语义靠 role/tabIndex/onKeyDown 补齐,不要退回 <button>。+ expect(chipBody).toContain('role="button"');+ expect(chipBody).toContain('tabIndex={0}');+ expect(chipBody).toContain('onKeyDown');+ // 同理:chip 文字必须能进剪贴板,不得 select-none。+ expect(chipBody).not.toContain('select-none');+ });+it('resolves targets through the renderer cache so session switches do not re-flash', () => {// Eager render-time resolution (chip only when unique) re-fires the IPC and// re-flashes plain-text → chip on every session switch unless results arediff --git a/apps/desktop/src/renderer/__tests__/quoteChip.test.tsx b/apps/desktop/src/renderer/__tests__/quoteChip.test.tsxindex 19506083c6..fcca0966f3 100644--- a/apps/desktop/src/renderer/__tests__/quoteChip.test.tsx+++ b/apps/desktop/src/renderer/__tests__/quoteChip.test.tsx@@ -14,6 +14,9 @@ describe('QuoteChip', () => {const chip = container.querySelector<HTMLElement>('[aria-label]');expect(chip?.getAttribute('aria-label')).toBe('first line\n\nsecond line');expect(chip?.textContent).toBe('first line second line');+ // 引用 chip 是消息内 chip 里唯一保留 select-none 的:它展示的是折叠成单行的+ // 摘要而非原文,不该跟着复制出去(其余 chip 默认可选中,见 inlineReferenceChip+ // 的剪贴板契约测试)。expect(chip?.className).toContain('select-none');});diff --git a/apps/desktop/src/renderer/components/chat/InlineReferenceChip.tsx b/apps/desktop/src/renderer/components/chat/InlineReferenceChip.tsxindex 29a5c9120b..c17a9df953 100644--- a/apps/desktop/src/renderer/components/chat/InlineReferenceChip.tsx+++ b/apps/desktop/src/renderer/components/chat/InlineReferenceChip.tsx@@ -5,8 +5,17 @@* Callers keep semantic behaviour such as navigation, file preview and* ProseMirror drag handling. Atomic chips intentionally have no close button;* composer nodes remain removable through normal node selection + Delete.+ *+ * 剪贴板契约(为什么这里是 `<span role="button">` 而不是 `<button>`):+ * 聊天消息被选中复制时,Chromium 会把选区**原样**序列化进 `text/html`。+ * `<button>` 是交互控件,不属于任何富文本内容模型,外部编辑器(Slack /+ * 飞书 / Notion / Word)解析粘贴的 HTML 时按标签白名单走,会把整个节点+ * 连同内部文字一起丢弃,并在原位留下一个断行——用户看到的就是"文件名+ * 凭空消失"。`<span>` 在所有这些白名单里,文字能完整落地。+ * 同理 `user-select: none` 会让文字连 `text/plain` 都进不了剪贴板,所以+ * 它只对 composer 的 ProseMirror atomic node 开启,见 `textSelectable`。*/-import type { MouseEventHandler, ReactNode, Ref } from 'react';+import type { KeyboardEventHandler, MouseEventHandler, ReactNode, Ref } from 'react';import { Tip } from '@/components/ui/tooltip';import { cn } from '@/lib/utils';@@ -18,12 +27,18 @@ export interface InlineReferenceChipProps {tooltipMono?: boolean;tooltipContentClassName?: string;selected?: boolean;- onClick?: MouseEventHandler<HTMLButtonElement>;- onContextMenu?: MouseEventHandler<HTMLButtonElement>;- buttonRef?: Ref<HTMLButtonElement>;+ onClick?: MouseEventHandler<HTMLSpanElement>;+ onContextMenu?: MouseEventHandler<HTMLSpanElement>;+ chipRef?: Ref<HTMLSpanElement>;ariaLabel?: string;className?: string;labelClassName?: string;+ /**+ * chip 文字能否被 selection 选中并进入剪贴板。默认 `true`——已发送消息里的+ * chip 必须能跟着正文一起复制出去。composer 里的 ProseMirror atomic node 传+ * `false`:那里 chip 是一个整体被选中 / 删除的原子节点,内部文字不参与 selection。+ */+ textSelectable?: boolean;}/** Theme-aware 12px reference pill with a formal full-content tooltip. */@@ -36,14 +51,16 @@ export function InlineReferenceChip({selected = false,onClick,onContextMenu,- buttonRef,+ chipRef,ariaLabel,className,labelClassName,+ textSelectable = true,}: InlineReferenceChipProps) {const interactive = Boolean(onClick || onContextMenu);const sharedClassName = cn(- 'inline-flex min-w-0 max-w-full select-none items-center',+ 'inline-flex min-w-0 max-w-full items-center',+ !textSelectable && 'select-none','gap-1.5 rounded-full border px-2 py-0.5 text-[12px] font-normal leading-5','bg-[var(--surface-chip)] text-[var(--text-primary)]',interactive && 'cursor-pointer transition-colors hover:bg-[var(--surface-hover)]',@@ -65,22 +82,23 @@ export function InlineReferenceChip({<span className={cn('min-w-0 truncate', labelClassName)}>{label}</span></>);- const trigger = interactive ? (- <button- ref={buttonRef}- type="button"+ // `<span role="button">` 换掉原生 `<button>` 后要自己补键盘激活:走 native+ // click(),让 onClick 收到一个正常的合成鼠标事件,调用方无需区分入口。+ const handleKeyDown: KeyboardEventHandler<HTMLSpanElement> = (e) => {+ if (!onClick) return;+ if (e.key !== 'Enter' && e.key !== ' ') return;+ e.preventDefault();+ e.currentTarget.click();+ };+ const trigger = (+ <span+ ref={chipRef}+ role={interactive ? 'button' : undefined}+ tabIndex={interactive ? 0 : undefined}aria-label={ariaLabel}onClick={onClick}onContextMenu={onContextMenu}- className={sharedClassName}- style={style}- data-inline-reference-chip=""- >- {contents}- </button>- ) : (- <span- aria-label={ariaLabel}+ onKeyDown={interactive ? handleKeyDown : undefined}className={sharedClassName}style={style}data-inline-reference-chip=""diff --git a/apps/desktop/src/renderer/components/chat/MarkdownRenderer.tsx b/apps/desktop/src/renderer/components/chat/MarkdownRenderer.tsxindex 43e3023263..f2b14fe003 100644--- a/apps/desktop/src/renderer/components/chat/MarkdownRenderer.tsx+++ b/apps/desktop/src/renderer/components/chat/MarkdownRenderer.tsx@@ -825,6 +825,12 @@ function localKindFromAbsPath(absPath: string, fallback: MarkdownLocalKind): Mar* unresolved / ambiguous / unsupported scheme — renders as plain text by the* call sites, not as a chip. So this component is always interactive: click* to open, hover to highlight, right-click for the file menu.+ *+ * 标签是 `<code role="button">` 而不是 `<button>`,这条不能回退:这个 chip 是+ * 行内代码升级来的,复制消息时 Chromium 会把选区原样序列化进 `text/html`,而+ * `<button>` 不在外部富文本编辑器(Slack / 飞书 / Notion / Word)的粘贴白名单+ * 里——整个节点连同路径文字会被丢弃,只在原位留下一个断行。`<code>` 既是语义+ * 正解,粘出去还会落成对方的行内代码。详见 InlineReferenceChip 的同款说明。*/function FileTargetChip({resolvedAbsPath,@@ -882,11 +888,18 @@ function FileTargetChip({return (<>- <button- type="button"+ <code+ role="button"+ tabIndex={0}title={title}onClick={activate}onContextMenu={ctxMenu.onContextMenu}+ onKeyDown={(e) => {+ // 换掉原生按钮元素后,键盘激活要自己补回来。+ if (e.key !== 'Enter' && e.key !== ' ') return;+ e.preventDefault();+ activate();+ }}className={cn('inline rounded-[6px] border-0 px-1 py-0.5','font-mono text-14 align-baseline',@@ -898,7 +911,7 @@ function FileTargetChip({)}>{children}- </button>+ </code>{ctxMenu.menu}</>);diff --git a/apps/desktop/src/renderer/components/chat/QuoteChip.tsx b/apps/desktop/src/renderer/components/chat/QuoteChip.tsxindex 85750f459e..46196b631a 100644--- a/apps/desktop/src/renderer/components/chat/QuoteChip.tsx+++ b/apps/desktop/src/renderer/components/chat/QuoteChip.tsx@@ -47,6 +47,10 @@ export function QuoteChip({tooltipContentClassName="max-h-64 w-80 max-w-[70vw] overflow-y-auto whitespace-normal"ariaLabel={quote.text}selected={selected}+ // 刻意的例外:chip 上是把换行折叠成单行的**摘要**,不是引用原文。让它进+ // 剪贴板等于把压扁过的文本混进复制结果,原文本身就在被引用的那条消息里。+ // 其余消息内 chip(文件名、会话、项目)展示的是完整实体名,默认可复制。+ textSelectable={false}/>);}diff --git a/apps/desktop/src/renderer/components/new-chat/MentionChipNode.tsx b/apps/desktop/src/renderer/components/new-chat/MentionChipNode.tsxindex 58917d7b10..a37b3cbca0 100644--- a/apps/desktop/src/renderer/components/new-chat/MentionChipNode.tsx+++ b/apps/desktop/src/renderer/components/new-chat/MentionChipNode.tsx@@ -101,6 +101,9 @@ function MentionChipNodeView({ node, selected }: NodeViewProps) {tooltip={tooltip}ariaLabel={displayLabel(attrs)}selected={selected}+ // ProseMirror atomic node:chip 整体被选中 / 删除,内部文字不参与+ // selection(外层 wrapper 也已 userSelect: none,这里显式声明意图)。+ textSelectable={false}className={attrs.kind === 'slash' ? 'text-[var(--chat-input-text)]' : undefined}/></NodeViewWrapper>diff --git a/apps/desktop/src/renderer/components/new-chat/PastedTextChipNode.tsx b/apps/desktop/src/renderer/components/new-chat/PastedTextChipNode.tsxindex 2ec4499167..4b25ac4516 100644--- a/apps/desktop/src/renderer/components/new-chat/PastedTextChipNode.tsx+++ b/apps/desktop/src/renderer/components/new-chat/PastedTextChipNode.tsx@@ -99,6 +99,8 @@ function PastedTextChipNodeView({ node, selected }: NodeViewProps) {tooltipContentClassName="max-h-64 w-80 max-w-[70vw] overflow-y-auto whitespace-pre-wrap [overflow-wrap:anywhere]"ariaLabel={attrs.display}selected={selected}+ // 同 MentionChipNode:composer 的原子节点不参与文字级 selection。+ textSelectable={false}className="cursor-pointer"/></NodeViewWrapper>