mirror of
https://github.com/LHY0125/PathEditor.git
synced 2026-06-29 18:15:55 +08:00
refactor: 全面代码质量提升 — StringList→string[], strict 模式, 死代码清理
架构重构: - StringList 类替换为不可变 string[](消除 dataVersion hack,Zustand 自然检测变化) - UndoRedoManager.undo/redo 返回新数组而非原地修改 - 删除 dataVersion 字段和 _bumpVersion() - 启用 TypeScript strict 模式 死代码清理: - 删除 string-list.ts, string-list.test.ts, use-path-validation.ts - Rust AppError 保留供未来使用 功能修复: - importFromJson 添加 try/catch - handleClean 使用真实格式验证替代 () => true - savePaths 保存前调用 backup_registry,处理部分保存失败 - importFromJson 校验非 object 类型输入 i18n 完善: - MergePreview/StatusBar 硬编码中文 → t() 调用 - 新增 merge.* 和 status.* 翻译键 Rust 改进: - registry.rs 抽取 load_paths/save_paths 通用函数,消除重复 - registry 新增 6 个单元测试(split/join/roundtrip) - backup.rs 时间戳加毫秒防覆盖,回退路径改为 home_dir 元数据: - package.json 名称→patheditor, 版本→4.0.0 - 新增 CHANGELOG.md - 移除 UndoRedoButtons 废弃注释 - tsconfig 添加 strict:true Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -145,7 +145,14 @@ function parseCsvLine(line: string): string[] {
|
||||
export function importFromJson(content: string): ImportResult {
|
||||
const result: ImportResult = { system: [], user: [] };
|
||||
|
||||
const obj = JSON.parse(content);
|
||||
let obj: Record<string, unknown>;
|
||||
try {
|
||||
obj = JSON.parse(content);
|
||||
} catch {
|
||||
return result; // 无效 JSON 返回空结果,由调用方显示错误
|
||||
}
|
||||
|
||||
if (typeof obj !== 'object' || obj === null) return result;
|
||||
|
||||
if (Array.isArray(obj.system)) {
|
||||
result.system = obj.system.filter(
|
||||
|
||||
+14
-59
@@ -1,92 +1,47 @@
|
||||
/**
|
||||
* 路径管理器 — 对应 C 版 path_manager.c
|
||||
* 提供路径增删移清理等 CRUD 操作的纯逻辑
|
||||
* 路径管理器 — 不可变的 string[] 操作
|
||||
*/
|
||||
import { StringList } from './string-list';
|
||||
|
||||
/** 删除指定索引的路径 */
|
||||
export function pathRemoveAt(list: StringList, index: number): void {
|
||||
list.removeAt(index);
|
||||
}
|
||||
|
||||
/** 上移路径(调整优先级) */
|
||||
export function pathMoveUp(list: StringList, index: number): boolean {
|
||||
if (index <= 0 || index >= list.length) return false;
|
||||
list.swap(index, index - 1);
|
||||
return true;
|
||||
}
|
||||
|
||||
/** 下移路径(调整优先级) */
|
||||
export function pathMoveDown(list: StringList, index: number): boolean {
|
||||
if (index < 0 || index >= list.length - 1) return false;
|
||||
list.swap(index, index + 1);
|
||||
return true;
|
||||
}
|
||||
|
||||
/** 标记路径的有效性(调用方负责提供验证函数和展开 env vars) */
|
||||
export interface PathValidation {
|
||||
isValid: boolean;
|
||||
isDuplicate: boolean;
|
||||
isEnvVar: boolean;
|
||||
}
|
||||
|
||||
/**
|
||||
* 分析路径列表中各条目的状态
|
||||
* validateFn: 验证路径是否有效(需调用 Rust validate_path)
|
||||
*/
|
||||
export function analyzePaths(
|
||||
list: StringList,
|
||||
paths: readonly string[],
|
||||
validateFn: (path: string) => boolean,
|
||||
): PathValidation[] {
|
||||
const result: PathValidation[] = [];
|
||||
const seen = new Set<string>();
|
||||
|
||||
for (let i = 0; i < list.length; i++) {
|
||||
const path = list.get(i)!;
|
||||
for (const path of paths) {
|
||||
const lower = path.toLowerCase();
|
||||
const isDuplicate = seen.has(lower);
|
||||
seen.add(lower);
|
||||
|
||||
result.push({
|
||||
isValid: validateFn(path),
|
||||
isDuplicate,
|
||||
isEnvVar: path.includes('%'),
|
||||
});
|
||||
result.push({ isValid: validateFn(path), isDuplicate, isEnvVar: path.includes('%') });
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量删除选中的索引(从大到小排序以避免偏移)
|
||||
*/
|
||||
export function batchRemoveAt(list: StringList, indices: number[]): void {
|
||||
const sorted = [...indices].sort((a, b) => b - a);
|
||||
for (const idx of sorted) {
|
||||
list.removeAt(idx);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 一键清理 — 移除无效路径和重复路径
|
||||
* 从后往前操作以避免索引偏移
|
||||
* 返回被移除的路径数量
|
||||
*/
|
||||
/** 从数组中移除无效和重复路径,返回 [新数组, 被移除的路径] */
|
||||
export function pathClean(
|
||||
list: StringList,
|
||||
paths: readonly string[],
|
||||
validateFn: (path: string) => boolean,
|
||||
): string[] {
|
||||
const analysis = analyzePaths(list, validateFn);
|
||||
): [string[], string[]] {
|
||||
const analysis = analyzePaths(paths, validateFn);
|
||||
const kept: string[] = [];
|
||||
const removed: string[] = [];
|
||||
|
||||
for (let i = analysis.length - 1; i >= 0; i--) {
|
||||
for (let i = 0; i < paths.length; i++) {
|
||||
const a = analysis[i];
|
||||
// 移除无效或重复的路径
|
||||
if (!a.isValid || a.isDuplicate) {
|
||||
removed.unshift(list.get(i)!);
|
||||
list.removeAt(i);
|
||||
removed.push(paths[i]);
|
||||
} else {
|
||||
kept.push(paths[i]);
|
||||
}
|
||||
}
|
||||
|
||||
return removed;
|
||||
return [kept, removed];
|
||||
}
|
||||
|
||||
@@ -1,84 +0,0 @@
|
||||
/**
|
||||
* StringList — 纯 TypeScript 的字符串列表数据结构
|
||||
* 对应 C 版 include/utils/string_ext.h 的 StringList
|
||||
*/
|
||||
export class StringList {
|
||||
private items: string[] = [];
|
||||
|
||||
/** 追加字符串 */
|
||||
add(str: string): void {
|
||||
this.items.push(str);
|
||||
}
|
||||
|
||||
/** 在指定索引处插入 */
|
||||
insertAt(index: number, str: string): void {
|
||||
this.items.splice(index, 0, str);
|
||||
}
|
||||
|
||||
/** 删除指定索引处的元素 */
|
||||
removeAt(index: number): void {
|
||||
this.items.splice(index, 1);
|
||||
}
|
||||
|
||||
/** 读取索引处元素 */
|
||||
get(index: number): string | undefined {
|
||||
return this.items[index];
|
||||
}
|
||||
|
||||
/** 设置索引处元素 */
|
||||
set(index: number, str: string): void {
|
||||
this.items[index] = str;
|
||||
}
|
||||
|
||||
/** 不区分大小写查找是否包含 */
|
||||
contains(str: string): boolean {
|
||||
return this.items.some((item) => item.toLowerCase() === str.toLowerCase());
|
||||
}
|
||||
|
||||
/** 查找不区分大小写的索引,未找到返回 -1 */
|
||||
indexOfIgnoreCase(str: string): number {
|
||||
const lower = str.toLowerCase();
|
||||
return this.items.findIndex((item) => item.toLowerCase() === lower);
|
||||
}
|
||||
|
||||
/** 交换两个索引的元素 */
|
||||
swap(i: number, j: number): void {
|
||||
const tmp = this.items[i];
|
||||
this.items[i] = this.items[j];
|
||||
this.items[j] = tmp;
|
||||
}
|
||||
|
||||
/** 清空所有元素 */
|
||||
clear(): void {
|
||||
this.items = [];
|
||||
}
|
||||
|
||||
/** 深拷贝 */
|
||||
clone(): StringList {
|
||||
const list = new StringList();
|
||||
list.items = [...this.items];
|
||||
return list;
|
||||
}
|
||||
|
||||
/** 转换为普通数组(传给 Rust 后端) */
|
||||
toArray(): string[] {
|
||||
return [...this.items];
|
||||
}
|
||||
|
||||
/** 从数组初始化 */
|
||||
static fromArray(arr: string[]): StringList {
|
||||
const list = new StringList();
|
||||
list.items = [...arr];
|
||||
return list;
|
||||
}
|
||||
|
||||
/** 元素数量 */
|
||||
get length(): number {
|
||||
return this.items.length;
|
||||
}
|
||||
|
||||
/** 只读数组 */
|
||||
get all(): readonly string[] {
|
||||
return [...this.items];
|
||||
}
|
||||
}
|
||||
+36
-88
@@ -1,25 +1,13 @@
|
||||
/**
|
||||
* 撤销/重做管理器 — 对应 C 版 undo_redo.c
|
||||
* 支持 8 种操作类型的完整撤销/重做
|
||||
* 撤销/重做管理器 — 纯逻辑,操作不可变 string[]
|
||||
*/
|
||||
import { StringList } from './string-list';
|
||||
|
||||
export const OperationType = {
|
||||
ADD: 0, // 新增路径
|
||||
DELETE: 1, // 删除路径
|
||||
EDIT: 2, // 编辑路径
|
||||
MOVE_UP: 3, // 上移
|
||||
MOVE_DOWN: 4, // 下移
|
||||
CLEAN: 5, // 一键清理
|
||||
CLEAR: 6, // 清空
|
||||
IMPORT: 7, // 导入
|
||||
ADD: 0, DELETE: 1, EDIT: 2, MOVE_UP: 3, MOVE_DOWN: 4, CLEAN: 5, CLEAR: 6, IMPORT: 7,
|
||||
} as const;
|
||||
export type OperationType = (typeof OperationType)[keyof typeof OperationType];
|
||||
|
||||
export const TargetType = {
|
||||
SYSTEM: 0,
|
||||
USER: 1,
|
||||
} as const;
|
||||
export const TargetType = { SYSTEM: 0, USER: 1 } as const;
|
||||
export type TargetType = (typeof TargetType)[keyof typeof TargetType];
|
||||
|
||||
export interface OpRecord {
|
||||
@@ -42,139 +30,99 @@ export class UndoRedoManager {
|
||||
this.maxSize = maxSize;
|
||||
}
|
||||
|
||||
/** 推送新操作记录,推送后截断重做分支 */
|
||||
push(record: OpRecord): void {
|
||||
// 截断重做分支
|
||||
this.records = this.records.slice(0, this.current + 1);
|
||||
|
||||
// 如果已满,移除最旧的记录
|
||||
if (this.records.length >= this.maxSize) {
|
||||
this.records.shift();
|
||||
}
|
||||
|
||||
this.records.push(record);
|
||||
this.current = this.records.length - 1;
|
||||
}
|
||||
|
||||
/** 撤销当前操作 */
|
||||
undo(sysPaths: StringList, userPaths: StringList): boolean {
|
||||
if (this.current < 0) return false;
|
||||
undo(sysPaths: readonly string[], userPaths: readonly string[]): [string[], string[]] | null {
|
||||
if (this.current < 0) return null;
|
||||
|
||||
const rec = this.records[this.current];
|
||||
this.current--;
|
||||
|
||||
const target = rec.target === TargetType.SYSTEM ? sysPaths : userPaths;
|
||||
const sys = [...sysPaths];
|
||||
const user = [...userPaths];
|
||||
const target = rec.target === TargetType.SYSTEM ? sys : user;
|
||||
|
||||
switch (rec.type) {
|
||||
case OperationType.ADD:
|
||||
// 撤销添加 — 删除最后 count 个元素
|
||||
for (let i = 0; i < rec.count; i++) {
|
||||
target.removeAt(target.length - 1);
|
||||
}
|
||||
target.splice(target.length - rec.count, rec.count);
|
||||
break;
|
||||
|
||||
case OperationType.DELETE:
|
||||
// 撤销删除 — 逐个恢复
|
||||
for (let i = 0; i < rec.count; i++) {
|
||||
target.insertAt(rec.index + i, rec.oldPaths[i]);
|
||||
target.splice(rec.index + i, 0, rec.oldPaths[i]);
|
||||
}
|
||||
break;
|
||||
|
||||
case OperationType.EDIT:
|
||||
target.set(rec.index, rec.oldPaths[0]);
|
||||
target[rec.index] = rec.oldPaths[0];
|
||||
break;
|
||||
|
||||
case OperationType.MOVE_UP:
|
||||
target.swap(rec.index - 1, rec.index);
|
||||
[target[rec.index - 1], target[rec.index]] = [target[rec.index], target[rec.index - 1]];
|
||||
break;
|
||||
|
||||
case OperationType.MOVE_DOWN:
|
||||
target.swap(rec.index, rec.index + 1);
|
||||
[target[rec.index], target[rec.index + 1]] = [target[rec.index + 1], target[rec.index]];
|
||||
break;
|
||||
|
||||
case OperationType.CLEAN:
|
||||
case OperationType.IMPORT:
|
||||
// 恢复到操作前的完整列表
|
||||
target.clear();
|
||||
for (const path of rec.oldPaths) {
|
||||
target.add(path);
|
||||
}
|
||||
target.length = 0;
|
||||
target.push(...rec.oldPaths);
|
||||
break;
|
||||
|
||||
case OperationType.CLEAR:
|
||||
for (const path of rec.oldPaths) {
|
||||
target.add(path);
|
||||
}
|
||||
target.push(...rec.oldPaths);
|
||||
break;
|
||||
}
|
||||
|
||||
return true;
|
||||
return [sys, user];
|
||||
}
|
||||
|
||||
/** 重做下一个操作 */
|
||||
redo(sysPaths: StringList, userPaths: StringList): boolean {
|
||||
if (this.current >= this.records.length - 1) return false;
|
||||
redo(sysPaths: readonly string[], userPaths: readonly string[]): [string[], string[]] | null {
|
||||
if (this.current >= this.records.length - 1) return null;
|
||||
|
||||
this.current++;
|
||||
const rec = this.records[this.current];
|
||||
const target = rec.target === TargetType.SYSTEM ? sysPaths : userPaths;
|
||||
|
||||
const sys = [...sysPaths];
|
||||
const user = [...userPaths];
|
||||
const target = rec.target === TargetType.SYSTEM ? sys : user;
|
||||
|
||||
switch (rec.type) {
|
||||
case OperationType.ADD:
|
||||
for (let i = 0; i < rec.count; i++) {
|
||||
target.add(rec.newPaths[i]);
|
||||
}
|
||||
target.push(...rec.newPaths);
|
||||
break;
|
||||
|
||||
case OperationType.DELETE:
|
||||
// 从后往前删,避免索引偏移
|
||||
for (let i = rec.count - 1; i >= 0; i--) {
|
||||
target.removeAt(rec.index + i);
|
||||
target.splice(rec.index + i, 1);
|
||||
}
|
||||
break;
|
||||
|
||||
case OperationType.EDIT:
|
||||
target.set(rec.index, rec.newPaths[0]);
|
||||
target[rec.index] = rec.newPaths[0];
|
||||
break;
|
||||
|
||||
case OperationType.MOVE_UP:
|
||||
target.swap(rec.index - 1, rec.index);
|
||||
[target[rec.index - 1], target[rec.index]] = [target[rec.index], target[rec.index - 1]];
|
||||
break;
|
||||
|
||||
case OperationType.MOVE_DOWN:
|
||||
target.swap(rec.index, rec.index + 1);
|
||||
[target[rec.index], target[rec.index + 1]] = [target[rec.index + 1], target[rec.index]];
|
||||
break;
|
||||
|
||||
case OperationType.CLEAN:
|
||||
case OperationType.IMPORT:
|
||||
target.clear();
|
||||
for (const path of rec.newPaths) {
|
||||
target.add(path);
|
||||
}
|
||||
target.length = 0;
|
||||
target.push(...rec.newPaths);
|
||||
break;
|
||||
|
||||
case OperationType.CLEAR:
|
||||
target.clear();
|
||||
target.length = 0;
|
||||
break;
|
||||
}
|
||||
|
||||
return true;
|
||||
return [sys, user];
|
||||
}
|
||||
|
||||
canUndo(): boolean {
|
||||
return this.current >= 0;
|
||||
}
|
||||
|
||||
canRedo(): boolean {
|
||||
return this.current < this.records.length - 1;
|
||||
}
|
||||
|
||||
clear(): void {
|
||||
this.records = [];
|
||||
this.current = -1;
|
||||
}
|
||||
|
||||
get historyLength(): number {
|
||||
return this.records.length;
|
||||
}
|
||||
canUndo(): boolean { return this.current >= 0; }
|
||||
canRedo(): boolean { return this.current < this.records.length - 1; }
|
||||
clear(): void { this.records = []; this.current = -1; }
|
||||
get historyLength(): number { return this.records.length; }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user