Files
apes-sidebar/src/client/file-tree-sort.ts
T
Menghuan1918andfinley08 0655b88629 feat(explorer): 资源管理器补「新建文件」+ 排序三档(#132)
第一部分:新建文件(移植贡献者 PR #832 的交互语义,写路径改落服务端)
- 新增路由 fs.createFile → fs-operations.ts 的 createWorkspaceFile:单段名、
  已存在即拒(fs-error 409,同一句 "x" already exists,绝不截断同名文件,
  落盘用 wx)、拒绝 Windows 非法字符(< > : " | ? * 与控制字符)与保留设备名
  (CON / PRN / AUX / NUL / COM1-9 / LPT1-9,带不带扩展名都拒)、路径穿越
  (..、a/b、a\b)与空名一律 bad-request 400
- src/client/api.ts 镜像 fsCreateFile;FileTree 目录行右键「新建文件」与
  「新建文件夹」并列,复用同一套行内输入(Enter 提交 / Escape 取消 / 失焦提交
  / IME 保护,图标跟随输入名),两者互斥;客户端先在已加载层做大小写不敏感的
  占用检查(newFileExists),服务端仍是最终裁决
- 词条 newFile / newFilePlaceholder / newFileInvalid / newFileExists 进入
  locales.ts 的 zh/en 与 19 份第三语言词典(ja 为真日文)

第二部分:排序三档(名称 / 类型 / 文件夹优先开关)
- 新增 src/client/file-tree-sort.ts:对 level.entries 的副本排序,不改服务端、
  不原地重排共享目录缓存;默认(名称 + 文件夹优先)与宿主 fs-tree.ts 的
  compareEntries 逐元素一致
- 控件落在文件树头部既有工具区(TreePanel 搜索行的刷新按钮旁):IconButton +
  Menu(名称 / 类型打勾 + 文件夹优先独立开关),沿用既有控件样式与可访问名
- 「按修改时间」不在本批范围

测试:tests/fs-create-file.spec.ts(11)、tests/file-tree-new-file.spec.tsx(11)、
tests/file-tree-sort.spec.tsx(12),并修 tests/file-tree-hover-title.spec.tsx 的
夹具顺序(原夹具不是任何宿主会返回的目录序)

Co-authored-by: finley08 <fazhang08@163.com>
2026-10-05 22:57:58 +08:00

78 lines
3.0 KiB
TypeScript

/**
* Explorer row ordering: the client-side sort behind the tree header's sort
* control.
*
* The host already returns every level directory-first, case-insensitively by
* name (`fs-tree.ts`'s `compareEntries`), and {@link DEFAULT_FILE_TREE_SORT}
* reproduces exactly that order — so the control's out-of-the-box state is
* indistinguishable from having no control at all. "Type" groups by extension
* (the part after the last dot, lowercased), the way VS Code's "Sort by Type"
* does; a directory has no extension, so with "folders first" off it lands
* with the extension-less files and both fall back to the name order.
*
* Sorting runs on a COPY: the level cache is shared per absolute path (two
* sessions reading one directory share an entry), so a cached listing must
* never be reordered in place.
*/
/** The two orderings the control offers ("by modified time" is a separate issue). */
export type FileTreeSortKey = 'name' | 'type'
/** One explorer sort choice: the key plus the folders-first switch. */
export interface FileTreeSort {
key: FileTreeSortKey
/** Directories before files, whatever the key says. */
dirsFirst: boolean
}
/** The default choice — byte-for-byte the order the host listing arrives in. */
export const DEFAULT_FILE_TREE_SORT: FileTreeSort = { key: 'name', dirsFirst: true }
/**
* The extension shown as a row's "type": the text after the LAST dot,
* lowercased, `''` for a directory or a dot-less name (a leading dot is a
* hidden file's marker, not an extension, so `.gitignore` has none).
*/
export function typeOf(name: string, isDir: boolean): string {
if (isDir) return ''
const at = name.lastIndexOf('.')
return at <= 0 ? '' : name.slice(at + 1).toLowerCase()
}
/**
* Case-insensitive name order with the host's deterministic tie-break: names
* that only differ in case keep their original code-point order (`A` before
* `a`), so the order is total and stable.
*/
function compareName(a: { name: string }, b: { name: string }): number {
const la = a.name.toLowerCase()
const lb = b.name.toLowerCase()
if (la !== lb) return la < lb ? -1 : 1
if (a.name === b.name) return 0
return a.name < b.name ? -1 : 1
}
/**
* Order one level's rows for display. The input is never mutated (the level
* cache is shared), and with {@link DEFAULT_FILE_TREE_SORT} the result equals
* `fs-tree.ts`'s server order for every input.
*
* @param entries - the level's rows, in host order.
* @param sort - the caller's choice (key + folders-first switch).
* @returns a new array; the rows themselves are the same objects.
*/
export function sortEntries<T extends { name: string; isDir: boolean }>(
entries: readonly T[],
sort: FileTreeSort,
): T[] {
return [...entries].sort((a, b) => {
if (sort.dirsFirst && a.isDir !== b.isDir) return a.isDir ? -1 : 1
if (sort.key === 'type') {
const ta = typeOf(a.name, a.isDir)
const tb = typeOf(b.name, b.isDir)
if (ta !== tb) return ta < tb ? -1 : 1
}
return compareName(a, b)
})
}