refactor(ui): 统一组件层地基与批次规划

- 新增 DownloadLink(kit.tsx)与 FallbackPane(patterns.tsx),从 ui/index.ts 导出;
  样式只用 --dsw-* / --ds-* 令牌,覆盖下载出口与 pane 级兜底消息两个跨页面模式
- 示范替换孤儿标签页占位组件(改用 FallbackPane role="status",文案与类型 chip 不变)
  与 binary-download(整页 FallbackPane + DownloadLink),删除 binary 独占的
  .editorBinary / .editorBinaryNotice 规则
- tests/ui-kit.spec.tsx、tests/ui-patterns.spec.tsx 补两个新组件的契约:
  download 属性、inline 变体、role 只在调用方要求时透传
- docs/plans/2026-11-25-ui-kit-convergence.md:36 个未接入 tsx 的 A/B/C 分类与
  B1–B4 批次(任务/作业 → 侧边对话 → 标签页与预览宿主 → 文件树与侧栏外壳)
This commit is contained in:
Menghuan1918 committed 2026-11-25 00:55:07 +08:00
1 parent d7e525471e
commit fd13838b09
11 files changed
+283 -32

No files matched your search

+112
View File
@@ -0,0 +1,112 @@
# 统一 UI 组件层收敛:地基与批次规划(2026-11-25)
> 目标:把 `src/client` 里还没接统一组件层(`src/client/ui/index.ts`)的页面盘点清楚,按风险分批,
> 并为跨页面的重复模式补齐共享件。本文是分批施工的地图,不是逐文件的实施记录。
## 结论
- 存量 `src/client/**/*.tsx` 共 119 个;已 import 统一层(`ui/` 任意模块)的 60 个,`ui/` 自身 10 个;
扣除 `clues/`、`changes/`、`native/` 三个已接入目录后,**剩余 36 个未接入**。
- 36 个里 **22 个是纯技术件(A)**,不该接组件层(chunk 入口、图标、描述符、iframe、portal、分发器);
**12 个有现成等价组件(B)**,**2 个缺共享件(C)**。
- C 类只需造 2 个件:`FallbackPane`(pane 级兜底消息)与 `DownloadLink`(下载出口),
本轮已落进 `src/client/ui` 并从 `index.ts` 导出;另有 2 个**部分接入**文件(`PdfView`、`LargeFileView`)
只差 `DownloadLink`,已列为同批收编对象。
- 批次按风险从低到高:**B1 任务/作业 → B2 侧边对话 → B3 标签页与预览宿主 → B4 文件树与侧栏外壳**。
## 盘点口径
```sh
# 未 import 统一层的 tsx(排除已接入目录与 ui/ 自身)
comm -23 \
<(find src/client -name '*.tsx' | grep -vE '/(clues|changes|native|ui)/' | sort) \
<(grep -rl "from '[^']*/ui[/']" src/client --include='*.tsx' | grep -vE '/ui/' | sort)
```
口径只认「是否 import 了 `ui/` 下的任意模块」,因此**部分接入**的文件不会出现在名单里:
`PdfView.tsx` 只引了 `ui/LoadingIndicator.tsx`,`LargeFileView.tsx` 引了 `ActionButton` 等,
两处仍手搓下载链接,单独补充在 B3 里。
## 映射表
### A 类:纯技术件,不接组件层(22)
| 文件 | 依据 |
| --- | --- |
| `chunks/editor.tsx`、`chunks/terminal.tsx`、`chunks/mermaid.tsx`、`chunks/locale.tsx` | 懒加载 chunk 入口,只 re-export |
| `mermaid-lazy.tsx`、`terminal-lazy.tsx` | chunk 包装器,无自有 UI |
| `icons.tsx`、`activity-icons.tsx`、`file-icons.tsx`、`builtins/tab-icons.tsx` | 图标集(宿主 `Icon*` 之外的品牌图标) |
| `markdown-labels.tsx` | 宿主 `MarkdownText` 的 props 工厂 |
| `SidechatAssistantRow.tsx` | 包一层宿主 `MarkdownText`,无自有 chrome |
| `HtmlPreviewFrame.tsx` | iframe 传输层(沙箱 / 内联 / blob) |
| `MarkdownHtml.tsx` | 纯 Markdown-HTML 段渲染器 |
| `selection-sidechat.tsx` | 会话与窗口接线,UI 全在 `SideChatView` |
| `windows/HostedWindow.tsx` | 纯 `createPortal` 到宿主窗口,无自有视觉(不是组件替换对象) |
| `sidebar/TabContent.tsx` | tab 分发器 + `+` 菜单构建 |
| `sidebar/bottom-toggle.tsx` | 槽注册(`conversation.session.header.utilities`) |
| `builtins/clues.tsx`、`builtins/viewers.tsx`、`builtins/tabs.tsx` | 描述符注册与设置项声明 |
| `index.tsx` | 插件入口 / 服务装配 |
### B 类:有现成等价组件(12)
> `builtins/media-preview.tsx` 归 C 类(需要新建件),见下一表。
| 文件 | 批次 | 目标组件 |
| --- | --- | --- |
| `tasks-shared.tsx` | B1 | `StatusLine` / `FactList`(`TaskLine`、`LiveLine` 的文本行) |
| `TaskWindow.tsx` | B1 | `Field` + `TextArea`/`TextInput`(`taskField/taskLabel/taskInput`)、`FactList`(`popKey/popValue`)、`ActionFooter`(`jobPopActions`) |
| `TasksGraph.tsx` | B1 | `ListRow`、`StatusBadge`、`Disclosure` |
| `SidechatImages.tsx` | B2 | `Notice`(inline)/ `StatusLine` |
| `SidechatAttachments.tsx` | B2 | `ListRow`、`Chip` |
| `diff/DiffFiles.tsx` | B3 | `ListRow`、`StatusBadge`、`ActionButton` |
| `diff/DiffRows.tsx` | B3 | 行壳 + `ActionFooter`(折叠) |
| `TerminalView.tsx` | B3 | `IconButton` / `ActionButton`、`Notice`、`StatusLine` |
| `OrphanedTab.tsx` | B3 | **本批已完成**:`FallbackPane`(`role="status"`)+ 既有 `EmptyState` |
| `Switch.tsx` | B4 | `controls.ToggleSwitch`(唯一消费者 `native/guide-order.tsx`) |
| `sidebar/dock-fallback.tsx` | B4 | `IconButton` / `ActionButton` |
| `split-pane.tsx` | B4 | `IconButton`(拖拽手柄保留原生 pointer 事件) |
### C 类:缺共享件(2,本轮已造件)
| 文件 | 新组件 | 消费者 |
| --- | --- | --- |
| `binary-download.tsx` | `FallbackPane` + `DownloadLink` | **本批已完成**(示范替换) |
| `builtins/media-preview.tsx` | 同上 | 待 B3 |
| `PdfView.tsx`(部分接入) | `DownloadLink` | 待 B3 |
| `LargeFileView.tsx`(部分接入) | `DownloadLink`(`inline` 变体) | 待 B3 |
## 本轮新增的组件
- **`FallbackPane`**(`patterns.tsx`):pane 级「这里没有东西可渲染,出口在下面」的填充块——
内层复用 `EmptyState`(`tone="page"`),外层负责 `flex:1` 与居中。签名:
`{ icon?, title, description?, actions?, role?, className? }`。
之前 `binary-download` 与 `media-preview` 各手搓一遍同款布局(一个 24/16 padding、一个 16 padding)。
- **`DownloadLink`**(`kit.tsx`):真实 `<a download>`;`variant: 'button' | 'inline'`。
`button` 是兜底面板里的描边胶囊,`inline` 是提示条里的下划线出口。
此前四处各写一份(binary / media / PDF 工具栏 / 大文件提示条),其中三处共用同一个 `editorDownloadLink` 类。
- 两者都从 `src/client/ui/index.ts` 导出;样式只用 `--dsw-*` / `--ds-*` 令牌;
测试补在 `tests/ui-patterns.spec.tsx`(`FallbackPane`)与 `tests/ui-kit.spec.tsx`(`DownloadLink`)。
## 示范替换(行为与文案不变)
| 文件 | 改动 |
| --- | --- |
| `src/client/OrphanedTab.tsx` | 正文从手写 `.editorPlaceholder` 换成 `FallbackPane role="status"`;标题栏保留;`my-plugin:db` / 标题文本不变(`tests/orphaned-tab.spec.tsx` 继续断言) |
| `src/client/binary-download.tsx` | 整页换成 `<FallbackPane title={t('binaryNoPreview')} actions={<DownloadLink …>} />`;删除自身 CSS 引用 |
## 风险与阻塞
- `windows/HostedWindow.tsx` 没有自有视觉(纯 portal),不在替换范围;建议里把它当示范是误判。
- `.editorPlaceholder` 仍被 `lazy-chunk.tsx`、`EditorHost.tsx`、`LargeFileView.tsx` 共用,
本轮只换 `OrphanedTab` 的用法、不动共享类,避免同批改三处宿主。
- `Switch.tsx` 与 `controls.ToggleSwitch` 语义不同(原生 checkbox vs `button[role=switch]`),
替换要同步改 `native/guide-order.tsx` 与相关测试,因此放 B4。
- 更名 / 删除 CSS 类属于行为面之外的视觉调整,一律放在「文件已确认无其他引用」之后做,
本轮只删了 `binary-download` 独占的 `.editorBinary` / `.editorBinaryNotice`。
## 批次顺序建议
1. **B1 任务/作业**(`tasks-shared` → `TaskWindow` → `TasksGraph`):模式最像 clues,收益最大。
2. **B2 侧边对话**(`SidechatImages` → `SidechatAttachments`)。
3. **B3 标签页与预览宿主**(`diff/*` → `TerminalView` → `media-preview` → `PdfView`/`LargeFileView` → `OrphanedTab` 收尾)。
4. **B4 文件树与侧栏外壳**(`Switch` → `dock-fallback` → `split-pane`):直接动交互与布局,放最后。
+10 -4
View File
@@ -5,11 +5,16 @@
* it through the tab bar's X button as usual. This is the graceful-
* degradation path for the open tab-type set: {@link Sidebar} renders this
* instead of crashing on an unknown type.
*
* The body is the shared {@link FallbackPane} (a pane-filling centred message
* with `role="status"`), so the sentence and the type chip are the only parts
* this file owns.
*/
import type { ReactNode } from 'react'
import { t } from './locales.ts'
import css from './sidebar.module.css'
import type { TabComponentProps } from './service.ts'
import { FallbackPane } from './ui/index.ts'
export function OrphanedTab(props: TabComponentProps): ReactNode {
const { tab } = props
@@ -18,10 +23,11 @@ export function OrphanedTab(props: TabComponentProps): ReactNode {
<div className={css.editorHeader}>
<span className={css.editorTitle} title={tab.type}>{tab.title}</span>
</div>
<div className={css.editorPlaceholder} role="status">
<span>{t('pluginNotLoaded')}</span>
<code className={css.orphanedType}>{tab.type}</code>
</div>
<FallbackPane
role="status"
title={t('pluginNotLoaded')}
description={<code className={css.orphanedType}>{tab.type}</code>}
/>
</div>
)
}
+8 -7
View File
@@ -3,21 +3,22 @@
* viewer can render a file (binary without a registered renderer, or the
* `binary-download` strategy) and registered as the `binary-download` viewer
* component so the declarative route and the host fallback share one UI.
*
* The pane is the shared {@link FallbackPane}; only its sentence and its way
* out belong to this file.
*/
import type { ReactNode } from 'react'
import type { SessionScope } from './api.ts'
import { downloadUrl } from './api.ts'
import { t } from './locales.ts'
import css from './sidebar.module.css'
import { DownloadLink, FallbackPane } from './ui/index.ts'
export function BinaryDownload(props: { scope: SessionScope; path: string }): ReactNode {
const { scope, path } = props
return (
<div className={css.editorBinary}>
<span className={css.editorBinaryNotice}>{t('binaryNoPreview')}</span>
<a className={css.editorDownloadLink} href={downloadUrl(scope, path)} download>
{t('downloadToView')}
</a>
</div>
<FallbackPane
title={t('binaryNoPreview')}
actions={<DownloadLink href={downloadUrl(scope, path)}>{t('downloadToView')}</DownloadLink>}
/>
)
}
-16
View File
@@ -1604,22 +1604,6 @@ body[data-dsh-sidebar-dragging] .bottomPanel {
/* Binary / download-only files (legacy OLE .doc/.xls/.ppt, unknown binaries):
a centered notice plus a download link — the user always gets the file. */
.editorBinary {
flex: 1;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
gap: 12px;
padding: 24px 16px;
text-align: center;
}
.editorBinaryNotice {
font: var(--dsw-font-xxs-12);
color: var(--dsw-alias-label-tertiary);
}
.editorDownloadLink {
display: inline-flex;
align-items: center;
+3 -3
View File
@@ -4,7 +4,7 @@
* free to split further without touching call sites.
*/
export {
ActionButton, Chip, ConfirmDialog, IconButton, Notice, SectionHeader, StatusBadge,
ActionButton, Chip, ConfirmDialog, DownloadLink, IconButton, Notice, SectionHeader, StatusBadge,
type StatusTone,
} from './kit.tsx'
export { LoadingIndicator, type LoadingIndicatorProps } from './LoadingIndicator.tsx'
@@ -12,9 +12,9 @@ export { WorkbenchSection, type WorkbenchSectionProps } from './WorkbenchSection
export { SelectMenu, type SelectMenuProps, type SelectOption } from './SelectMenu.tsx'
export { TextInput, TextArea, NumberInput, Checkbox, ToggleSwitch, Tabs, SettingRow, type TabOption } from './controls.tsx'
export {
ActionFooter, ChoiceCard, Disclosure, EmptyState, ErrorBlock, FactList, FactRow, Field, ListRow, StatusLine,
ActionFooter, ChoiceCard, Disclosure, EmptyState, ErrorBlock, FactList, FactRow, FallbackPane, Field, ListRow, StatusLine,
type ActionFooterProps, type ChoiceCardProps, type ChoiceOption, type DisclosureProps, type EmptyStateProps,
type ErrorBlockProps, type Fact, type FactListProps, type FactRowProps, type FieldProps,
type ErrorBlockProps, type Fact, type FactListProps, type FactRowProps, type FallbackPaneProps, type FieldProps,
type ListRowProps, type PatternTone, type StatusLineProps,
} from './patterns.tsx'
export { SourceSwitcher, type SourceSwitcherItem, type SourceSwitcherProps } from './SourceSwitcher.tsx'
+42
View File
@@ -321,3 +321,45 @@
.statusBadge[data-tone='write'] { color: var(--dsw-alias-state-business-primary); }
.statusBadge[data-tone='edit'] { color: var(--dsw-alias-state-warn-primary); }
.statusBadge[data-tone='neutral'] { color: var(--dsw-alias-label-tertiary); }
/* The one download affordance (kit.tsx’s DownloadLink): a bordered pill in a
fallback pane, or the underlined escape hatch inside a notice bar. */
.downloadLink {
display: inline-flex;
align-items: center;
gap: var(--ds-sidebar-space-2);
padding: 6px 14px;
border: 1px solid var(--dsw-alias-border-l2);
border-radius: 6px;
background: var(--dsw-alias-bg-layer-2);
color: var(--dsw-alias-label-primary);
font: var(--dsw-font-xxs-strong-12);
text-decoration: none;
cursor: pointer;
transition:
background var(--ds-transition-duration-slow) var(--ds-ease-in-out),
border-color var(--ds-transition-duration-slow) var(--ds-ease-in-out);
}
.downloadLink:hover {
background: var(--dsw-alias-interactive-bg-hover);
}
.downloadLink:focus-visible {
outline: 2px solid var(--dsw-alias-interactive-bg-hover-accent);
outline-offset: -1px;
}
.downloadLinkInline {
padding: 0;
border: 0;
border-radius: 0;
background: none;
color: inherit;
font: inherit;
text-decoration: underline;
}
.downloadLinkInline:hover {
background: none;
}
+25
View File
@@ -105,6 +105,31 @@ export function Chip(props: {
)
}
/**
* The ONE download affix: a real anchor carrying `download`, so the file goes
* to disk instead of opening a tab. `button` is the bordered pill a fallback
* pane offers as its way out; `inline` is the underlined escape hatch a notice
* bar puts beside its sentence. Four surfaces cut this recipe by hand before
* (binary download, media fallback, PDF toolbar, large-file notice).
*/
export function DownloadLink(props: {
href: string
children: ReactNode
variant?: 'button' | 'inline'
className?: string | undefined
}): ReactNode {
const { href, children, variant = 'button', className } = props
return (
<a
className={clsx(css.downloadLink, variant === 'inline' && css.downloadLinkInline, className)}
href={href}
download
>
{children}
</a>
)
}
/**
* A 28px section band: a label (with an optional count) that may carry ONE
* trailing action. The hairline under it is the page's only section divider.
+13
View File
@@ -558,3 +558,16 @@
@media (pointer: coarse) {
.disclosureSummary { min-height: 44px; }
}
/* A pane-filling centred message (patterns.tsx’s FallbackPane): the body a
viewer or window shows when there is nothing to render, with its way out
under the sentence. It owns the fill; the sentence belongs to the caller. */
.fallbackPane {
flex: 1;
min-height: 0;
display: flex;
align-items: center;
justify-content: center;
padding: var(--ds-sidebar-space-4) var(--ds-sidebar-space-3);
text-align: center;
}
+34
View File
@@ -228,6 +228,40 @@ export function EmptyState({ icon, title, description, actions, tone = 'well', c
)
}
/** Props of {@link FallbackPane}. */
export interface FallbackPaneProps extends DataAttributes {
readonly icon?: ReactNode
readonly title: ReactNode
readonly description?: ReactNode
/** The way out of the pane: a download pill, a retry, a settings link. */
readonly actions?: ReactNode
/** The pane announces itself only when the caller asks (the orphaned tab asks). */
readonly role?: string
readonly className?: string | undefined
}
/**
* The pane-filling "nothing to render here, here is the way out" shape: a
* centred {@link EmptyState} that fills the tab or window body.
*
* The binary and media fallbacks cut this same recipe by hand before (a flex
* column filling the pane, centred tertiary text, a download pill under it,
* 24/16 padding), so the layout lives here and the sentence stays the caller's.
*/
export function FallbackPane({ icon, title, description, actions, role, className, ...dataProps }: FallbackPaneProps): ReactNode {
return (
<div {...dataProps} {...(role === undefined ? {} : { role })} className={clsx(css.fallbackPane, className)}>
<EmptyState
tone="page"
{...(icon === undefined ? {} : { icon })}
title={title}
{...(description === undefined ? {} : { description })}
{...(actions === undefined ? {} : { actions })}
/>
</div>
)
}
/** Props of {@link ErrorBlock}. */
export interface ErrorBlockProps extends DataAttributes {
readonly message: ReactNode
+19 -1
View File
@@ -12,7 +12,7 @@ import { createElement, type ReactNode } from 'react'
import { createRoot, type Root } from 'react-dom/client'
import { act } from 'react-dom/test-utils'
import { setupReactAct } from './test-utils.ts'
import { Chip, ConfirmDialog, IconButton, Notice, SectionHeader, StatusBadge } from '../src/client/ui/kit.tsx'
import { Chip, ConfirmDialog, DownloadLink, IconButton, Notice, SectionHeader, StatusBadge } from '../src/client/ui/kit.tsx'
import { invalidateGitStatus, statusOfXY, useGitStatus, type GitStatusView } from '../src/client/ui/git-status.ts'
import type { GitStatusResult } from '../src/client/api.ts'
@@ -339,3 +339,21 @@ describe('ui kit atoms', () => {
await unmount()
})
})
describe('DownloadLink', () => {
it('is a real anchor that downloads instead of navigating in place', async () => {
const { container, unmount } = await mount(createElement(DownloadLink, { href: '/sidebar/file?path=a.bin', children: 'Download' }))
const link = container.querySelector('a')
expect(link?.getAttribute('href')).toBe('/sidebar/file?path=a.bin')
expect(link?.hasAttribute('download')).toBe(true)
expect(link?.className).toContain('downloadLink')
expect(link?.className).not.toContain('downloadLinkInline')
await unmount()
})
it('drops the pill for the inline variant a notice bar uses', async () => {
const { container, unmount } = await mount(createElement(DownloadLink, { href: '/x', variant: 'inline', children: 'Download' }))
expect(container.querySelector('a')?.className).toContain('downloadLinkInline')
await unmount()
})
})
+17 -1
View File
@@ -18,7 +18,7 @@
import { afterEach, describe, expect, it, vi } from 'vitest'
import { createElement } from 'react'
import {
ActionFooter, ChoiceCard, EmptyState, ErrorBlock, FactList, FactRow, Field, ListRow, StatusLine,
ActionFooter, ChoiceCard, EmptyState, ErrorBlock, FactList, FactRow, FallbackPane, Field, ListRow, StatusLine,
} from '../src/client/ui/patterns.tsx'
import { renderRoot, setupReactAct, type RenderedRoot } from './test-utils.ts'
@@ -335,3 +335,19 @@ describe('ActionFooter', () => {
expect(byClass(view.container, 'actionFooterBleed')).not.toBeNull()
})
})
describe('FallbackPane', () => {
it('centres the caller sentence and its way out in a pane-filling block', () => {
view = renderRoot(createElement(FallbackPane, { title: 'No preview', actions: 'Download' }))
const pane = byClass(view.container, 'fallbackPane')!
expect(byClass(pane, 'emptyTitle')!.textContent).toBe('No preview')
expect(byClass(pane, 'emptyActions')!.textContent).toBe('Download')
})
it('announces itself only when the caller asks', () => {
view = renderRoot(createElement(FallbackPane, { title: 'Unregistered tab' }))
expect(view.container.querySelector('[role="status"]')).toBeNull()
view.rerender(createElement(FallbackPane, { title: 'Unregistered tab', role: 'status' }))
expect(view.container.querySelector('[role="status"]')).not.toBeNull()
})
})