Files
apes-sidebar/src/client/open-in-app.ts
T
Menghuan1918 36fbc5ac7e feat(sidebar): 文件页与文件变动页 UIUX 重构 + 全局组件抽取
文件页(FileTree/TreePanel)
- Ctrl/Cmd 多选 + Shift 按可见行序连选(Esc/点空白/右键收敛),批量条支持复制路径、批量删除(共享 ConfirmDialog + 顺序 fsRemove)
- Git 变更着色:行名按状态上色 + M/A/D/U 徽标,目录按 dirHasChanges 着色,非仓库静默降级(数据来自共享 git 状态存储)
- 新建文件夹:fs.mkdir 路由 + mkdirWorkspaceEntry + 内联输入行(Enter/Esc/blur/IME 守卫)
- 性能:FileRow/DirRow memo(含反向验证)、搜索 hidden 停车不再卸载树、level 拉取世代守卫 + AbortController、fs.tree truncated 提示、use-dir-watch 入参 memo
- 拖拽上传视觉 token 化(硬编码色清零);可见性下传后隐藏页签停止轮询
- 右键「打开方式」改为复用 DSH 自带 open-in-app(宿主探测的关联应用 + 文件管理器显示)

文件变动页(changes/diff)
- 36px 头(宿主 SegmentedControl)+ 单一 ChangeRow + SectionHeader 分组 + 吸底提交条单状态行 + 历史独立空态
- 修掉:双份空态、暂存失败 unhandled rejection、手动刷新被丢、仓库切换刷新两次、worktree 清单缺 repoRoot、工作树被自动切走、预览持旧快照、恢复高度未 clamp、session ops 无谓轮询
- DiffPane/DiffTab 共用 use-git-diff;修掉 DiffPane 的 untracked 预览从不渲染

全局组件与宿主接入
- 新增 src/client/ui(IconButton/Chip/SectionHeader/Notice/ConfirmDialog/StatusBadge + 共享 git 状态存储)
- 新增 src/client/open-in-app.ts(宿主 open-in-app 客户端适配),删除自研 open-with 全链路与 open.external 路由
- 客户端 inject 补 remote / remote.session:缺声明时 ctx.get('remote').session 会抛出并把整个面板打成错误条(真实挂载 lane 抓到)
- 词典:+25 键 / -20 openWith* / -noChanges,20 份同步

验证:typecheck / lint / test(126 文件 1322 通过)/ build 全绿;
DSH_CMD='npx -y --package @deepseek-ai/dsh@0.1.7-rc.1 dsh' pnpm test:mount 7/7 通过(含 tab sweep 与 perf lane)
2026-09-28 00:27:31 +08:00

215 lines
7.6 KiB
TypeScript

/**
* The client half of "open with an application" — a thin adapter over DSH's
* OWN open-in-app capability, replacing the plugin's former home-grown
* vocabulary (URL templates, SSH hosts, custom editors, a spawn route).
*
* Two host facilities back this, and the adapter simply picks the right one:
*
* - The Host Remote (`ctx.remote.session`): `canOpenWorkspacePath()` reports
* whether this deployment can hand a path to a native desktop,
* `workspacePathApplications({path})` lists the OS handlers REGISTERED for
* one file, and `openWorkspacePath({path, action?, application?})` opens a
* path or reveals it in the file manager. The host validates the path and
* the application id before spawning anything.
* - The host's webServer routes: `GET /open-in-app/apps` is the probed
* application CATALOG (menu order) and `POST /open-in-app/open` launches
* one catalog app on a DIRECTORY — the "open this workspace in VS Code"
* gesture. Catalog ids are not file-handler ids, so `open()` tries the
* Remote first and falls back to the catalog route.
*
* Everything degrades to "no control" when the host says the desktop is
* unavailable: the caller hides the section instead of offering dead rows.
*/
import { hostT } from './locales.ts'
/** The host route paths (see `@deepseek-ai/dsh-host-open-in-app/shared`). */
const APPS_PATH = '/open-in-app/apps'
const ICON_PATH = '/open-in-app/icon'
const OPEN_PATH = '/open-in-app/open'
/** One application that can open the path. */
export interface OpenInAppEntry {
/** Host-side id: an OS handler id for files, a catalog id for directories. */
id: string
/** Display name (the OS reports handlers; the catalog id is localized). */
name: string
/** PNG/SVG data URL for a handler, an icon route for a catalog app, else null. */
icon: string | null
/** Whether the OS reports it as the default handler (files only). */
isDefault: boolean
}
/** The adapter the pages consume. */
export interface OpenInApp {
/** Whether the host can open paths on a desktop; null until probed. */
available(): boolean | null
/** Probe availability once (shared across callers). */
probe(): Promise<boolean>
/** The installed-application catalog, in host menu order (directories). */
directoryApps(): Promise<readonly OpenInAppEntry[]>
/** The OS handlers registered for one file, or null when the query failed. */
fileApps(path: string): Promise<readonly OpenInAppEntry[] | null>
/** Open a path with its default handler, or with one explicit application. */
open(path: string, application?: string): Promise<boolean>
/** Reveal a path in the OS file manager. */
reveal(path: string): Promise<boolean>
}
/** The slice of `ctx.remote.session` this module calls (structurally typed). */
interface SessionRemote {
canOpenWorkspacePath(): Promise<{ ok: boolean; value?: boolean }>
workspacePathApplications(request: { path: string }): Promise<{ ok: boolean; value?: readonly RawFileApplication[] }>
openWorkspacePath(request: { path: string; action?: 'reveal'; application?: string }): Promise<{ ok: boolean }>
}
/** The host's `NativeFileApplication` shape. */
interface RawFileApplication {
id: string
name: string
default: boolean
icon: string | null
}
/** A ctx that can resolve client services (the cordis client root). */
interface CtxLike {
get(name: string): unknown
}
/** The host's `open-in-app` locale namespace, read through the plugin's bridge. */
function appLabel(id: string): string {
const text = hostT('open-in-app', `app.${id}`)
if (text !== undefined) return text
// The namespace may be absent (host older than the capability): fall back to
// a readable form of the id itself.
return id.charAt(0).toUpperCase() + id.slice(1)
}
/**
* The remote namespace, or undefined when the client assembly lacks it.
*
* Cordis throws when a service (or a nested property of one) is read without
* a matching `inject` entry — the client bundle declares `remote` and
* `remote.session` for exactly this call, but a composition that mounts the
* sidebar without the Host Remote assembly must degrade to "no control"
* instead of taking a render down with it, so the read is guarded.
*/
function remoteOf(ctx: CtxLike): SessionRemote | undefined {
try {
const remote = ctx.get('remote') as { session?: SessionRemote } | undefined | null
const session = remote?.session
return session !== undefined && typeof session.openWorkspacePath === 'function' ? session : undefined
} catch {
return undefined
}
}
/** Build the adapter for one client ctx. */
export function createOpenInApp(ctx: CtxLike): OpenInApp {
const remote = remoteOf(ctx)
/** Tri-state availability; one shared probe per page. */
let availability: boolean | null = null
let probing: Promise<boolean> | null = null
const probe = async (): Promise<boolean> => {
if (availability !== null) return availability
if (remote === undefined) {
availability = false
return false
}
probing ??= remote.canOpenWorkspacePath()
.then(result => {
availability = result.ok && result.value === true
return availability
})
.catch(() => {
availability = false
return false
})
.finally(() => { probing = null })
return probing
}
const directoryApps = async (): Promise<readonly OpenInAppEntry[]> => {
if (!await probe()) return []
try {
const response = await fetch(APPS_PATH)
if (!response.ok) return []
const payload = await response.json() as { apps?: unknown }
const ids = Array.isArray(payload.apps) ? payload.apps.filter((id): id is string => typeof id === 'string') : []
return ids.map(id => ({
id,
name: appLabel(id),
icon: `${ICON_PATH}/${encodeURIComponent(id)}`,
isDefault: false,
}))
} catch {
return []
}
}
const fileApps = async (path: string): Promise<readonly OpenInAppEntry[] | null> => {
if (remote === undefined || !await probe()) return null
try {
const result = await remote.workspacePathApplications({ path })
if (!result.ok || result.value === undefined) return null
return result.value.map(app => ({
id: app.id,
name: app.name,
icon: app.icon,
isDefault: app.default,
}))
} catch {
return null
}
}
const openViaRemote = async (path: string, application: string | undefined): Promise<boolean> => {
if (remote === undefined) return false
try {
const result = await remote.openWorkspacePath({
path,
...(application !== undefined ? { application } : {}),
})
return result.ok
} catch {
return false
}
}
const open = async (path: string, application?: string): Promise<boolean> => {
if (await openViaRemote(path, application)) return true
// A catalog id is not a file-handler id: the host's directory route is the
// one that launches "VS Code on this folder".
if (application === undefined) return false
try {
const response = await fetch(OPEN_PATH, {
method: 'POST',
headers: { 'content-type': 'application/json' },
body: JSON.stringify({ app: application, path }),
})
return response.ok
} catch {
return false
}
}
const reveal = async (path: string): Promise<boolean> => {
if (remote === undefined) return false
try {
const result = await remote.openWorkspacePath({ path, action: 'reveal' })
return result.ok
} catch {
return false
}
}
return {
available: () => availability,
probe,
directoryApps,
fileApps,
open,
reveal,
}
}