refactor(ui): 合入 B3 —— diff、终端与预览宿主接入统一组件层

This commit is contained in:
Menghuan1918 committed 2026-11-25 01:00:09 +08:00
commit 5a8ab2f805
7 files changed
+79 -55

No files matched your search

+23 -6
View File
@@ -58,9 +58,9 @@ comm -23 \
| `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` |
| `diff/DiffFiles.tsx` | B3 | **已完成**:`StatusBadge`(tone 直接用它自带的 `added`/`deleted`/`renamed`/`neutral`);表头仍是原生 disclosure button |
| `diff/DiffRows.tsx` | B3 | **已完成**:展开全部改 `ActionButton`;折叠行保持原生(测试按 `[data-expandable]` 定位) |
| `TerminalView.tsx` | B3 | **已完成**:`StatusLine`(待连/已退出)+ `ActionButton`、`ErrorBlock`(不可用/错误)、`FallbackPane`(终端已失联) |
| `OrphanedTab.tsx` | B3 | **本批已完成**:`FallbackPane`(`role="status"`)+ 既有 `EmptyState` |
| `Switch.tsx` | B4 | `controls.ToggleSwitch`(唯一消费者 `native/guide-order.tsx`) |
| `sidebar/dock-fallback.tsx` | B4 | `IconButton` / `ActionButton` |
@@ -71,9 +71,9 @@ comm -23 \
| 文件 | 新组件 | 消费者 |
| --- | --- | --- |
| `binary-download.tsx` | `FallbackPane` + `DownloadLink` | **本批已完成**(示范替换) |
| `builtins/media-preview.tsx` | 同上 | 待 B3 |
| `PdfView.tsx`(部分接入) | `DownloadLink` | 待 B3 |
| `LargeFileView.tsx`(部分接入) | `DownloadLink`(`inline` 变体) | 待 B3 |
| `builtins/media-preview.tsx` | 同上 | **已完成**(B3):`FallbackPane role="status"` + `DownloadLink` |
| `PdfView.tsx`(部分接入) | `DownloadLink` | **已完成**(B3):工具栏 `DownloadLink` + 错误态 `ErrorBlock` |
| `LargeFileView.tsx`(部分接入) | `DownloadLink`(`inline` 变体) | **已完成**(B3):`DownloadLink` + 空文件 `EmptyState` |
## 本轮新增的组件
@@ -94,6 +94,23 @@ comm -23 \
| `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 引用 |
## B3 实施记录(标签页与预览宿主)
| 文件 | 实际改动 |
| --- | --- |
| `diff/DiffFiles.tsx` | 文件头徽标 `<span className={css.fileTag}>` → `StatusBadge`,`fileTag()` 从返回字符串改为 `{ tone, label }`;表头仍是原生 `button`,因为 `ListRow` 没有 `disabled` / `aria-expanded`,而这两条是折叠语义与 `tests/diff-*-*.spec.tsx` 的定位依据 |
| `diff/DiffRows.tsx` | 「展开全部」按钮 → `ActionButton size="sm"`;折叠行保留原生 div + `[data-expandable]`,因为测试按该属性点击并断言加载/失败标记 |
| `TerminalView.tsx` | 不可用告警 → `ErrorBlock`;待连/已退出状态行 → `StatusLine` + `ActionButton`;终端已失联(`role="alert"` + 新建终端)→ `FallbackPane`;连接错误 → `ErrorBlock` |
| `builtins/media-preview.tsx` | 不可播放提示 → `FallbackPane role="status"`;下载出口 → `DownloadLink` |
| `PdfView.tsx` | 工具栏下载 `<a className={css.editorDownloadLink}>` → `DownloadLink`;加载失败 → `ErrorBlock` |
| `LargeFileView.tsx` | 超大文件提示条里的下载 → `DownloadLink`;空文件占位 → `EmptyState tone="well"` |
缺口(留给后续批次,不动 `src/client/ui/**`):
- `StatusLine.role` 只接受 `'status'`,没有 `'alert'` 变体;告警态因此由 `FallbackPane` 承担。若 B4 也要用,再统一给 `StatusLine` 加 `role` 联合类型。
- `ListRow` 缺 `disabled` 与任意 `aria-*` 直通,带折叠语义的行只能继续用原生 button。
- 未改任何测试断言:`diff-binary-row` / `diff-files-collapse` / `diff-fold-expand` / `editor-large-file` / `terminal-tab` / `lazy-chunk` 六个 spec(49 例)直接通过。
## 风险与阻塞
- `windows/HostedWindow.tsx` 没有自有视觉(纯 portal),不在替换范围;建议里把它当示范是误判。
+3 -5
View File
@@ -33,7 +33,7 @@ import { formatByteSize, MAX_EDITABLE_BYTES, MAX_EDITABLE_LABEL, type EditLimitR
import { isJsonlPath, isTreeRecord, parseRecord, summarizeRecord } from './jsonl.ts'
import { splitLines, useTextPager } from './text-pages.ts'
import { t } from './locales.ts'
import { ActionButton, IconButton, LoadingIndicator, Notice } from './ui/index.ts'
import { ActionButton, DownloadLink, EmptyState, IconButton, LoadingIndicator, Notice } from './ui/index.ts'
import css from './sidebar.module.css'
/**
@@ -176,9 +176,7 @@ export function LargeFileView(props: {
})
: t('loadAll')}
</ActionButton>
<a className={css.largeFileDownload} href={downloadUrl(scope, path)} download>
{t('downloadToView')}
</a>
<DownloadLink href={downloadUrl(scope, path)}>{t('downloadToView')}</DownloadLink>
</div>
)}
{pager.atCap && (
@@ -227,7 +225,7 @@ export function LargeFileView(props: {
</div>
)}
{pager.error === null && pager.atEnd && totalRows === 0 && !pager.loading && (
<div className={css.editorPlaceholder}>{t('largeFileEmpty')}</div>
<EmptyState tone="well" title={t('largeFileEmpty')} />
)}
</div>
{showFooter && (
+3 -4
View File
@@ -4,6 +4,7 @@ import clsx from 'clsx'
import { downloadUrl, mediaUrl, type SessionScope } from './api.ts'
import { t } from './locales.ts'
import { LoadingIndicator } from './ui/LoadingIndicator.tsx'
import { DownloadLink, ErrorBlock } from './ui/index.ts'
import css from './sidebar.module.css'
export function PdfView(props: { scope: SessionScope; path: string; title: string }) {
@@ -87,13 +88,11 @@ export function PdfView(props: { scope: SessionScope; path: string; title: strin
return (
<div className={css.editorPdf}>
<div className={css.editorPdfToolbar}>
<a className={css.editorDownloadLink} href={downloadUrl(scope, path)} download>
{t('downloadToView')}
</a>
<DownloadLink href={downloadUrl(scope, path)}>{t('downloadToView')}</DownloadLink>
</div>
<div className={css.editorPdfStage}>
{load.status === 'loading' && <LoadingIndicator centered />}
{load.status === 'error' && <div className={css.editorError}>{load.message}</div>}
{load.status === 'error' && <ErrorBlock message={load.message} />}
{load.status === 'ready' && (
<iframe
ref={frameRef}
+36 -27
View File
@@ -54,6 +54,7 @@ import {
type HostTerminalEnvironment, type HostTerminalView, type HostTerminalViewState,
} from './terminal-client.ts'
import { terminalClipboardKey } from './terminal-clipboard.ts'
import { ActionButton, ErrorBlock, FallbackPane, StatusLine } from './ui/index.ts'
import css from './terminal.module.css'
/** The props the bottom terminal view consumes from its tab descriptor. */
@@ -110,7 +111,7 @@ export function TerminalBottomView({ ctx, scope, tab, visible }: TerminalBottomP
if (service === undefined) {
return (
<section className={css.root} data-sidebar-surface="terminal" data-dsh-bottom-terminal data-terminal-state="unavailable">
<p className={css.notice} role="alert">{t('terminalUnavailable')}</p>
<ErrorBlock message={t('terminalUnavailable')} />
</section>
)
}
@@ -126,38 +127,46 @@ export function TerminalBottomView({ ctx, scope, tab, visible }: TerminalBottomP
return (
<section className={css.root} data-sidebar-surface="terminal" data-dsh-bottom-terminal data-terminal-state={state.phase}>
{status !== undefined && (
<div className={css.status} role="status">
<span>{status}</span>
{reconnectable(state) && (
<button
type="button"
className={css.action}
onClick={() => {
// The host's own rule: a view with a terminal but no live
// attachment reconnects (a fresh snapshot follows); a view
// that never got one re-runs environment/terminal lookup.
if (state.info === undefined) void model.refresh()
else model.connect()
}}
>
{t('terminalRetry')}
</button>
)}
</div>
<StatusLine
role="status"
text={status}
action={reconnectable(state)
? (
<ActionButton
size="sm"
onClick={() => {
// The host's own rule: a view with a terminal but no live
// attachment reconnects (a fresh snapshot follows); a view
// that never got one re-runs environment/terminal lookup.
if (state.info === undefined) void model.refresh()
else model.connect()
}}
>
{t('terminalRetry')}
</ActionButton>
)
: undefined}
/>
)}
{(missing || ended) && (
<div className={css.noticeBlock}>
<p className={missing ? css.notice : css.status} role={missing ? 'alert' : 'status'}>
{missing ? t('terminalGone') : t('exited')}
</p>
<button type="button" className={css.action} onClick={newTerminal}>{t('terminalNew')}</button>
</div>
{missing && (
<FallbackPane
role="alert"
title={t('terminalGone')}
actions={<ActionButton size="sm" onClick={newTerminal}>{t('terminalNew')}</ActionButton>}
/>
)}
{!missing && ended && (
<StatusLine
role="status"
text={t('exited')}
action={<ActionButton size="sm" onClick={newTerminal}>{t('terminalNew')}</ActionButton>}
/>
)}
{state.info !== undefined && !missing && (
<TerminalScreen model={model} state={state} visible={visible} scheme={scheme} label={t('terminal')} />
)}
{state.error !== undefined && state.issue === undefined && (
<p className={css.error} role="alert">{t('terminalError')}: {state.error}</p>
<ErrorBlock message={`${t('terminalError')}: ${state.error}`} />
)}
</section>
)
+3 -4
View File
@@ -21,6 +21,7 @@ import type { ReactNode } from 'react'
import { downloadUrl } from '../api.ts'
import type { FileViewerProps } from '../service.ts'
import { t } from '../locales.ts'
import { DownloadLink, FallbackPane } from '../ui/index.ts'
import css from '../sidebar.module.css'
/** Extensions rendered with an `<audio>` transport instead of a video stage. */
@@ -44,7 +45,7 @@ export function MediaPreview(props: FileViewerProps): ReactNode {
return (
<div className={css.mediaPreview}>
{mediaUrl === undefined || failed
? <div className={css.mediaPreviewNotice}>{t('mediaUnplayable')}</div>
? <FallbackPane role="status" title={t('mediaUnplayable')} />
: mediaIsAudio(path)
? (
<audio
@@ -68,9 +69,7 @@ export function MediaPreview(props: FileViewerProps): ReactNode {
{/* The download escape hatch stays for every state: a container the
bundled ffmpeg cannot decode (an exotic codec, a damaged file) must
still end in a file the user can take elsewhere. */}
<a className={css.editorDownloadLink} href={downloadUrl(scope, path)} download>
{t('download')}
</a>
<DownloadLink href={downloadUrl(scope, path)}>{t('download')}</DownloadLink>
</div>
)
}
+8 -7
View File
@@ -12,6 +12,7 @@ import clsx from 'clsx'
import { t } from '../locales.ts'
import { diffStats, displayPath, parseUnifiedDiff, unifiedSegments, untrackedFile, type DiffFile, type DiffRow, type FoldSegment, type ParsedDiff } from './rows.ts'
import { langOfPath } from './highlight.ts'
import { StatusBadge } from '../ui/index.ts'
import { DiffRows } from './DiffRows.tsx'
import css from './diff.module.css'
@@ -50,14 +51,14 @@ function defaultExpandedFiles(files: readonly DiffFile[]): Set<number> {
return expanded
}
/** The file header badge: added / deleted / renamed / binary ('' for a plain edit). */
function fileTag(file: DiffFile): string | null {
if (file.binary) return t('diffBinary')
if (file.oldPath === '/dev/null') return t('diffAdded')
if (file.newPath === '/dev/null') return t('diffDeleted')
/** The file header badge: added / deleted / renamed / binary (absent for a plain edit). */
function fileTag(file: DiffFile): { tone: 'neutral' | 'added' | 'deleted' | 'renamed'; label: string } | null {
if (file.binary) return { tone: 'neutral', label: t('diffBinary') }
if (file.oldPath === '/dev/null') return { tone: 'added', label: t('diffAdded') }
if (file.newPath === '/dev/null') return { tone: 'deleted', label: t('diffDeleted') }
const oldPath = displayPath(file.oldPath)
const newPath = displayPath(file.newPath)
if (oldPath !== newPath) return t('diffRenamed')
if (oldPath !== newPath) return { tone: 'renamed', label: t('diffRenamed') }
return null
}
@@ -145,7 +146,7 @@ export function DiffFiles({ diff, parsed: provided, untrackedPath, untrackedCont
{expandable && <span aria-hidden="true" className={clsx(css.fileChevron, fileExpanded && css.fileChevronExpanded)}>›</span>}
<span className={css.filePath}>{to}</span>
{from !== to && <span className={css.fileOld}>← {from}</span>}
{tag !== null && <span className={css.fileTag}>{tag}</span>}
{tag !== null && <StatusBadge tone={tag.tone} className={css.fileTag}>{tag.label}</StatusBadge>}
{expandable && (stats.added > 0 || stats.deleted > 0) && (
<span className={css.fileStats}>
{stats.added > 0 && <span className={css.statAdd}>+{String(stats.added)}</span>}
+3 -2
View File
@@ -12,6 +12,7 @@
import { useEffect, useMemo, useRef, useState, type ReactElement, type ReactNode } from 'react'
import { t } from '../locales.ts'
import { coalesceInline, diffInline, MIN_FOLD, type DiffRow, type DiffSegment, type FoldSegment, type InlineDiff } from './rows.ts'
import { ActionButton } from '../ui/index.ts'
import { hasBlockComment, isColored, scanLine, type CodeToken, type TokenType } from './highlight.ts'
import css from './diff.module.css'
@@ -306,9 +307,9 @@ export function DiffRows({ segments, lang, resolveFold }: DiffRowsProps) {
<div className={css.rows}>
{parts}
{renderedRows >= MAX_ROWS && !expandedAll && (
<button type="button" className={css.expand} onClick={() => { setExpandedAll(true) }}>
<ActionButton size="sm" className={css.expand} onClick={() => { setExpandedAll(true) }}>
{t('diffExpand', { count: segments.reduce((sum, s) => sum + (s.kind === 'hunk' ? s.rows.length : 0), 0) - renderedRows })}
</button>
</ActionButton>
)}
</div>
)