refactor(client): 收敛 /sidebar envelope 解析与取扩展名双实现
- api.ts:call 与 fetchUpload 里逐字相同的响应 envelope 解析 (json().catch(null) + ok/value 判断 + SidebarApiError 构造)收敛为 私有 readEnvelope<T>(),两处调用;判断顺序与错误码回退逻辑不变。 - 取扩展名双实现收敛:extOf 的实现迁至零依赖的 paths.ts(lang.ts 与 service.ts 均可达的基础模块),lang.ts re-export 保持既有导入面 (tests/file-types.spec.ts 从 lang.ts 导入 extOf);service.ts 删除 模块私有的同体 extOfPath,matchFileViewer 改用 extOf。方向说明: service.ts 不能反向 import lang.ts——lang.ts 携带全部 @codemirror/* 依赖且只经 editor 懒加载 chunk 进入浏览器,core bundle 静态引用它 会把 CodeMirror 拉进核心包(构建产物已验证:client.js / client-registry.js 中 0 处 codemirror,client-editor.js 不变)。
This commit is contained in:
@@ -27,6 +27,7 @@ import {
|
||||
type SidebarSnapshot, type SidebarState, type SidebarStore, type SidebarTab,
|
||||
} from './state.ts'
|
||||
import { isNarrowWidth } from './breakpoints.ts'
|
||||
import { extOf } from './paths.ts'
|
||||
import type { SessionScope } from './api.ts'
|
||||
import type { SidebarPrefs } from '../prefs-shared.ts'
|
||||
|
||||
@@ -427,14 +428,6 @@ export interface BetterSidebarService {
|
||||
openFile(scope: SessionScope, path: string, title?: string): void
|
||||
}
|
||||
|
||||
/** Extract the lowercase extension without leading dot from a path. */
|
||||
function extOfPath(path: string): string {
|
||||
const at = path.lastIndexOf('.')
|
||||
if (at === -1) return ''
|
||||
const base = path.slice(at + 1).toLowerCase()
|
||||
return base.includes('/') || base.includes('\\') ? '' : base
|
||||
}
|
||||
|
||||
/** The file name of a path (both separators). */
|
||||
function baseNameOf(path: string): string {
|
||||
const at = Math.max(path.lastIndexOf('/'), path.lastIndexOf('\\'))
|
||||
@@ -571,7 +564,7 @@ export function createBetterSidebarService(store: SidebarStore): BetterSidebarSe
|
||||
const isViewerEnabled = (id: string): boolean => store.getPrefs().viewersEnabled[id] !== false
|
||||
|
||||
const matchFileViewer = (path: string, head?: Uint8Array): FileViewerDescriptor | undefined => {
|
||||
const ext = extOfPath(path)
|
||||
const ext = extOf(path)
|
||||
// Single pass in priority order (descending; stable for equal
|
||||
// priorities — insertion order). Each descriptor gets first refusal in
|
||||
// its own turn: `detect` (when head bytes are available) beats its own
|
||||
|
||||
Reference in New Issue
Block a user