Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(chat): 区分仅菜单引用与可点击按钮

Dash35416bb431d9
diff --git a/apps/desktop/src/renderer/__tests__/inlineReferenceChip.test.tsx b/apps/desktop/src/renderer/__tests__/inlineReferenceChip.test.tsx
index 58fa4015ee..b2f47ee056 100644
--- a/apps/desktop/src/renderer/__tests__/inlineReferenceChip.test.tsx
+++ b/apps/desktop/src/renderer/__tests__/inlineReferenceChip.test.tsx
@@ -1,8 +1,8 @@
// @vitest-environment jsdom
-import { cleanup, render, screen } from '@testing-library/react';
+import { cleanup, fireEvent, 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';
+import { afterEach, describe, expect, it, vi } from 'vitest';
import { InlineReferenceChip } from '@/components/chat/InlineReferenceChip';
@@ -11,10 +11,7 @@ afterEach(cleanup);
describe('InlineReferenceChip', () => {
it('keeps one compact pill geometry with primary text and no close action', () => {
const { container } = render(
- <InlineReferenceChip
- label="src/very-long-file-name.ts"
- icon={<File />}
- />,
+ <InlineReferenceChip label="src/very-long-file-name.ts" icon={<File />} />,
);
const chip = container.querySelector('[data-inline-reference-chip]');
expect(chip?.className).toContain('rounded-full');
@@ -32,6 +29,7 @@ describe('InlineReferenceChip', () => {
expect(screen.queryByRole('button')).toBeNull();
expect(chip?.getAttribute('draggable')).toBeNull();
expect(chip?.getAttribute('title')).toBeNull();
+ expect(chip?.getAttribute('tabindex')).toBeNull();
});
// 剪贴板契约。复制一段聊天消息时 Chromium 会把选区原样序列化进 text/html:
@@ -40,9 +38,7 @@ describe('InlineReferenceChip', () => {
// - `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 { 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();
@@ -69,7 +65,12 @@ describe('InlineReferenceChip', () => {
const user = userEvent.setup();
let clicks = 0;
const { container } = render(
- <InlineReferenceChip label="endpoint.json" onClick={() => { clicks += 1; }} />,
+ <InlineReferenceChip
+ label="endpoint.json"
+ onClick={() => {
+ clicks += 1;
+ }}
+ />,
);
const chip = container.querySelector('[data-inline-reference-chip]') as HTMLElement;
chip.focus();
@@ -77,4 +78,22 @@ describe('InlineReferenceChip', () => {
await user.keyboard(' ');
expect(clicks).toBe(2);
});
+
+ it('keeps primary activation and the keyboard menu independent', () => {
+ const onClick = vi.fn();
+ const onContextMenu = vi.fn();
+ render(
+ <InlineReferenceChip label="Reference" onClick={onClick} onContextMenu={onContextMenu} />,
+ );
+ const chip = screen.getByRole('button', { name: 'Reference' });
+ expect(chip.classList.contains('underline')).toBe(true);
+ expect(chip.classList.contains('cursor-pointer')).toBe(true);
+ fireEvent.keyDown(chip, { key: 'Enter', shiftKey: true });
+ expect(onClick).toHaveBeenCalledOnce();
+ expect(onClick.mock.calls[0][0].shiftKey).toBe(true);
+ expect(onContextMenu).not.toHaveBeenCalled();
+ fireEvent.keyDown(chip, { key: 'F10', shiftKey: true });
+ expect(onContextMenu).toHaveBeenCalledOnce();
+ expect(onClick).toHaveBeenCalledOnce();
+ });
});
diff --git a/apps/desktop/src/renderer/components/chat/InlineReferenceChip.tsx b/apps/desktop/src/renderer/components/chat/InlineReferenceChip.tsx
index fe93518ca6..3f1518655e 100644
--- a/apps/desktop/src/renderer/components/chat/InlineReferenceChip.tsx
+++ b/apps/desktop/src/renderer/components/chat/InlineReferenceChip.tsx
@@ -63,17 +63,15 @@ export function InlineReferenceChip({
splitPaneRouteAction = false,
textSelectable = true,
}: InlineReferenceChipProps) {
- const interactive = Boolean(onClick || onContextMenu);
+ const interactive = Boolean(onClick);
+ const focusable = interactive || Boolean(onContextMenu);
const sharedClassName = cn(
'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-12 font-normal leading-5',
'bg-[var(--surface-chip)] text-[var(--text-primary)]',
- // 下划线跟着 `interactive` 一起给:DESIGN.md §14.5 要求「下划线 ⇔ 可点」双向成立,
- // 而本组件同时服务可点的会话 / 项目深链 chip 与不可点的静态 chip(sidebar-embedded
- // 等不注入 onClick / onContextMenu 的调用点)。判据只有 `interactive` 这一处,
- // cursor 与下划线同源,不可能一边有一边没有(PR #1144 review 实捉:改前这些 chip
- // 有 onClick 却无下划线,是「可点但无下划线」的反例)。
+ // DESIGN.md §14.5:按钮语义、指针与下划线只表示实际点击动作。
+ // 仅有上下文菜单的引用仍可聚焦以使用菜单键,但不宣称可点击。
interactive && 'cursor-pointer underline underline-offset-2 transition-colors hover:bg-[var(--surface-hover)]',
className,
);
@@ -103,12 +101,12 @@ export function InlineReferenceChip({
<span
ref={chipRef}
role={interactive ? 'button' : undefined}
- tabIndex={interactive ? 0 : undefined}
+ tabIndex={focusable ? 0 : undefined}
aria-label={ariaLabel}
onClick={onClick}
onAuxClick={onAuxClick}
onContextMenu={onContextMenu}
- onKeyDown={interactive ? handleKeyDown : undefined}
+ onKeyDown={focusable ? handleKeyDown : undefined}
className={sharedClassName}
style={style}
data-inline-reference-chip=""
diff --git a/apps/desktop/src/renderer/components/chat/__tests__/ProjectLinkChip.test.tsx b/apps/desktop/src/renderer/components/chat/__tests__/ProjectLinkChip.test.tsx
index 21c156a2ae..08604a38eb 100644
--- a/apps/desktop/src/renderer/components/chat/__tests__/ProjectLinkChip.test.tsx
+++ b/apps/desktop/src/renderer/components/chat/__tests__/ProjectLinkChip.test.tsx
@@ -1,5 +1,5 @@
// @vitest-environment jsdom
-import { cleanup, fireEvent, render, screen, waitFor } from '@testing-library/react';
+import { act, cleanup, fireEvent, render, screen, waitFor } from '@testing-library/react';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
const mocks = vi.hoisted(() => ({ navigate: vi.fn(), focus: vi.fn(), writeText: vi.fn() }));
@@ -44,17 +44,41 @@ describe('ProjectLinkChip actions', () => {
expect(mocks.navigate).not.toHaveBeenCalled();
});
- it('offers copy in embedded content without exposing host-route actions', async () => {
- render(
- <SessionNavigationModeProvider mode="sidebar-embedded">
- <ProjectLinkChip href={href} />
- </SessionNavigationModeProvider>,
- );
- fireEvent.contextMenu(screen.getByRole('button', { name: 'My Project' }));
- expect(
- await screen.findByRole('menuitem', { name: 'chat.markdownRenderer.copyLink' }),
- ).toBeTruthy();
- expect(screen.queryByRole('menuitem', { name: 'chat.markdownRenderer.locateProject' })).toBeNull();
- expect(mocks.navigate).not.toHaveBeenCalled();
- });
+ it.each(['pointer', 'ContextMenu', 'Shift+F10'])(
+ 'offers embedded copy via %s without advertising a primary action',
+ async (gesture) => {
+ render(
+ <SessionNavigationModeProvider mode="sidebar-embedded">
+ <ProjectLinkChip href={href} />
+ </SessionNavigationModeProvider>,
+ );
+ const chip = screen
+ .getByText('My Project')
+ .closest('[data-inline-reference-chip]') as HTMLElement;
+ expect(screen.queryByRole('button', { name: 'My Project' })).toBeNull();
+ expect(chip.classList.contains('underline')).toBe(false);
+ expect(chip.classList.contains('cursor-pointer')).toBe(false);
+ expect(chip.tabIndex).toBe(0);
+ act(() => chip.focus());
+ fireEvent.click(chip);
+ fireEvent.keyDown(chip, { key: 'Enter' });
+ fireEvent.keyDown(chip, { key: ' ' });
+ expect(screen.queryByRole('menu')).toBeNull();
+ if (gesture === 'pointer') fireEvent.contextMenu(chip);
+ else
+ fireEvent.keyDown(chip, {
+ key: gesture === 'ContextMenu' ? 'ContextMenu' : 'F10',
+ shiftKey: gesture === 'Shift+F10',
+ });
+ const copy = await screen.findByRole('menuitem', { name: 'chat.markdownRenderer.copyLink' });
+ expect(
+ screen.queryByRole('menuitem', { name: 'chat.markdownRenderer.locateProject' }),
+ ).toBeNull();
+ fireEvent.click(copy);
+ await waitFor(() => expect(mocks.writeText).toHaveBeenCalledWith(href));
+ await waitFor(() => expect(document.activeElement).toBe(chip));
+ expect(mocks.focus).not.toHaveBeenCalled();
+ expect(mocks.navigate).not.toHaveBeenCalled();
+ },
+ );
});
diff --git a/docs/design-rules/DESIGN.md b/docs/design-rules/DESIGN.md
index 0a90ef0ef6..5cb3b13717 100644
--- a/docs/design-rules/DESIGN.md
+++ b/docs/design-rules/DESIGN.md
@@ -1178,7 +1178,8 @@ Only the two registered chart members inside Usage History may enlarge their non
不修改偏好。文件链接普通点击沿用预览/审查等原行为,修饰键和中键交给默认应用,
HTML 则交给系统浏览器;远程文件复用现有下载缓存/HTML 预览通道,不读取本机同名路径。
安全降级附件仍只允许另存,不因快捷键扩大打开能力。项目链接提供「定位到项目」与
-复制链接菜单;文内标题和媒体链接保留各自定位/预览动作,并可复制链接。
+复制链接菜单;嵌入式项目引用只保留复制菜单,不带按钮语义、手形指针或下划线,
+仍可聚焦并用 Shift+F10/菜单键打开菜单。文内标题和媒体链接保留各自定位/预览动作,并可复制链接。
菜单按对象精简并平铺,不使用「更多」或「打开方式」子菜单。HTML 的标准菜单为
「在侧边栏打开」「在浏览器打开」「查看源代码」/「在文件夹中显示」「复制路径」;
@@ -1376,7 +1377,7 @@ PR #1144 的两轮 review 各捉到一个**同族**缺陷:「有下划线却点
| 桌面聊天正文 | 已解析本地文件(chip / 散文 label) | ✅ | ✅ |
| 桌面聊天正文 | 未解析路径、行内 code | ❌ | ❌ |
| 桌面聊天正文 | **远程**会话里 stat 回 `unknown` 的引用 | 仅非歧义形状 | 由 `isAmbiguousPathShape` 门槛裁决 |
-| 桌面聊天正文 / 用户气泡 | 会话 / 项目深链 chip(`InlineReferenceChip`) | 取决于是否注入 onClick / onContextMenu | **与 `interactive` 同源** |
+| 桌面聊天正文 / 用户气泡 | 会话 / 项目深链 chip(`InlineReferenceChip`) | 取决于是否注入 onClick | **与 `interactive` 同源** |
| 桌面用户气泡 | URL / 图片路径 | ✅ | ✅(改前只有 hover 下划线) |
| 手机聊天正文 | 外链 / 图片 chip / 会话 chip | 取决于 handler 是否注入 | **与 handler 同源**(`clickableInlineStyle`) |
| 手机聊天正文 | 路径 chip | 取决于远端 verdict | 由 `ChatPathChipSpan.lit` 单点裁决 |