Compare commits

...

3 Commits

Author SHA1 Message Date
Serendipity 49ef9c0cff chore: 添加 Issue 模板、CI 徽章、修复仓库描述
CI / 前端检查 (TypeScript + Lint + Test) (push) Has been cancelled
CI / Rust 检查 (Check + Clippy + Test) (push) Has been cancelled
- 新增 bug_report 和 feature_request Issue 模板
- README 添加 GitHub Actions CI 状态徽章
- 修复仓库描述(去重,更新至 v4.2)
- 默认分支改为 main

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-28 00:46:56 +08:00
Serendipity 344011a02c chore: 整理仓库 — 版本号统一、README 徽章更新、新增 CHANGELOG 和 CONTRIBUTING
- 版本号 4.0.0 → 4.2.0(tauri.conf.json, Cargo.toml, 窗口标题)
- README 徽章更新(tests 55→72, version 4.0.0→4.2.0)
- CHANGELOG.md 补充 v4.1 和 v4.2 变更记录
- 新增 CONTRIBUTING.md 贡献指南
- GitHub Release: v4.2.0 补充说明,旧 C 版本标记为 pre-release

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-28 00:43:36 +08:00
Serendipity 3aed03f599 fix: 修复 5 个 bug + 备份警告丢失
- BUG 1: undo/redo 后持久化 disabled 状态到 disabled.json
- BUG 2: expand_env_vars 增加缓冲区不足检测(result > required)
- BUG 3: E2E mock load_disabled_state 返回格式从对象改为数组
- BUG 4: 双 hive 保存失败时同时显示两个错误原因
- BUG 5: 导入 both 合并为单条 undo 记录(新增 IMPORT_BOTH 操作类型)
- 备份失败后保存成功时显示"保存成功(备份失败)"而非覆盖警告

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-28 00:38:32 +08:00
16 changed files with 428 additions and 33 deletions
+27
View File
@@ -0,0 +1,27 @@
---
name: Bug 报告
about: 提交问题报告帮助改进 PathEditor
title: "[Bug] "
labels: bug
assignees: ''
---
## 问题描述
<!-- 清晰描述 bug 是什么 -->
## 复现步骤
1.
2.
3.
## 期望行为
## 截图(如有)
## 系统信息
- Windows 版本:
- 是否管理员:
- PathEditor 版本:
+15
View File
@@ -0,0 +1,15 @@
---
name: 功能建议
about: 建议新功能或改进
title: "[Feature] "
labels: enhancement
assignees: ''
---
## 使用场景
<!-- 你会在什么场景下需要这个功能? -->
## 建议方案
<!-- 你期望的功能是什么样的? -->
+51 -12
View File
@@ -1,38 +1,77 @@
# Changelog
## v4.0.0 (2026-05-26)
## [4.2.0] — 2026-05-28
### 新增
- 路径启用/禁用功能:复选框控制 PATH 中每条路径是否生效
- PathEntry 数据类型:替代原有 `string[]`,支持 `enabled` 状态
- `disabled.json` 持久化禁用状态的独立存储
- E2E 测试框架:Playwright + 4 条核心流程测试
- CI/CD 流水线:TypeScript + Rust 自动检查,Release 自动构建
### 修复
- undo/redo after toggle 未持久化 disabled 状态
- expand_env_vars 两次 API 调用间缓冲区截断风险
- E2E mock load_disabled_state 返回格式与 Rust 后端不匹配
- 双 hive 保存失败时错误信息只显示一个
- 导入 both 产生两条 undo 记录,需两次 Ctrl+Z
- 备份失败警告被"保存成功"覆盖
- 非连续多行删除后 undo 恢复到错误位置
- backup_registry 未 await 导致竞态保存新值
### 变更
- 导入改用原生文件对话框(`@tauri-apps/plugin-dialog`
- PathTable 环境变量展开限流 20 并发
- CI 切换到 MSVC 工具链
- 版本号统一为 4.2.0
---
## [4.1.0] — 2026-05-26
### 新增
- app-store 单元测试:25 个测试覆盖 CRUD/undo-redo/loadPaths/savePaths
- 72 个前端单元测试 + 10 个 Rust 单元测试
### 修复
- NSIS 安装包缺少 WebView2Loader.dll
- AppShell overflow-hidden 导致窗口无法上下滚动
### 变更
- 清理 LOW 问题:样式去重、死代码删除、命名修正
- 抽取 Modal 共享组件、统一按钮样式
- 支持 JSON/CSV/TXT 三种导入导出格式
---
## [4.0.0] — 2026-05-25
### 重大变更
完全重写为 Tauri 2.x + React 19 + TypeScript + Rust 技术栈,替代原有的 C + IUP GUI。
### 新增
- 现代 Web UIReact + Tailwind CSS 4 + Zustand
- 现代 Web UIReact 19 + Tailwind CSS 4 + Zustand
- 深色/浅色模式切换
- 中英文界面即时切换
- 路径有效性颜色编码(红色无效、橙色重复)
- 环境变量展开悬停提示
- 文件夹拖拽添加路径
- 保存前 PATH 长度检查
- 66 个前端单元测试 + 10 个 Rust 单元测试
### 改进
- 安装包体积从 ~3MB 降至 ~8MB(含 WebView2 运行时)
- 完整撤销/重做支持(8 种操作类型,50 步历史)
- JSON/CSV/TXT 三种格式导入导出
- 合并预览查看系统+用户路径
- 类型安全:TypeScript strict 模式 + Rust 编译期检查
- NSIS 安装包,约 8MB
### 移除
- 旧 C + IUP + Lua + gettext 代码库
- Lua 配置引擎 → JSON 配置文件
- gettext 国际化 → i18next
### 已知限制
---
- 需要 Windows 10+ 系统预装的 WebView2 运行时
- 内存占用约 50MB(旧版约 15MB)
- 文件系统路径验证在清理功能中为同步检查(不含实际目录存在性验证)
## [3.x] 及更早
C + IUP GUI 版本,已停止维护。历史发布记录见 [GitHub Releases](https://github.com/LHY0125/PathEditor/releases)。
+29
View File
@@ -0,0 +1,29 @@
# 贡献指南
感谢你对 PathEditor 的关注!
## 提交 Issue
- 使用清晰的标题描述问题
- 提供复现步骤
- 附上系统信息(Windows 版本、是否管理员)
- 如果是功能建议,说明使用场景
## 提交 Pull Request
1. Fork 仓库并从 `main` 创建功能分支
2. 运行 `npm test``cargo check` 确保通过
3. 遵循项目代码规范:
- TypeScript `strict: true`,零编译错误
- 前端核心逻辑在 `src/core/`,纯函数,零依赖
- Rust `unsafe` 块必须有 `// SAFETY:` 注释
4. 新功能应包含测试
## 本地开发
```bash
npm install
npx tauri dev
```
详见 [README.md](./README.md#开发)。
+7 -6
View File
@@ -4,13 +4,14 @@
</p>
<p align="center">
<img src="https://img.shields.io/badge/version-4.0.0-blue" alt="version">
<img src="https://img.shields.io/badge/version-4.2.0-blue" alt="version">
<img src="https://img.shields.io/badge/tauri-2.x-ffa03a" alt="tauri">
<img src="https://img.shields.io/badge/react-19-61dafb" alt="react">
<img src="https://img.shields.io/badge/rust-1.95-000000" alt="rust">
<img src="https://img.shields.io/badge/typescript-strict-blue" alt="typescript">
<img src="https://img.shields.io/badge/license-MIT-green" alt="license">
<img src="https://img.shields.io/badge/tests-55%20passed-brightgreen" alt="tests">
<img src="https://img.shields.io/badge/tests-72%20passed-brightgreen" alt="tests">
<img src="https://github.com/LHY0125/PathEditor/actions/workflows/ci.yml/badge.svg" alt="CI">
</p>
---
@@ -19,7 +20,7 @@
PathEditor 是 Windows PATH 环境变量的可视化管理工具。支持系统变量和用户变量的增删改查、拖拽排序、一键清理无效路径、导入导出以及完整的撤销/重做。
v4.0 使用 **Tauri 2.x + React 19 + TypeScript + Rust** 完全重写,替代了原有的 C + IUP GUI。
v4.2 使用 **Tauri 2.x + React 19 + TypeScript + Rust** 完全重写,替代了原有的 C + IUP GUI。
## 截图
@@ -41,7 +42,7 @@ _[待补充]_
- 环境变量路径(含 `%VAR%`)悬浮展开预览
### 撤销/重做
- 支持 8 种操作类型,最多 50 步历史
- 支持 9 种操作类型,最多 50 步历史
- 新增、删除、编辑、移动、清理、清空、导入均可撤销
### 导入/导出
@@ -63,7 +64,7 @@ _[待补充]_
## 安装
从 [Releases](https://github.com/LHY0125/PathEditor/releases) 下载最新版 `PathEditor_4.0.0_x64-setup.exe` 安装。
从 [Releases](https://github.com/LHY0125/PathEditor/releases) 下载最新版 `PathEditor_4.2.0_x64-setup.exe` 安装。
或从源码构建:
@@ -106,7 +107,7 @@ cd src-tauri && cargo test
| 国际化 | i18next |
| 桌面框架 | Tauri 2.x |
| 后端 | Rust (winreg + windows-rs FFI) |
| 前端测试 | Vitest (45 个测试) |
| 前端测试 | Vitest (72 个测试) |
| Rust 测试 | cargo test (10 个测试) |
| 构建 | Vite |
| 打包 | NSIS |
+233
View File
@@ -0,0 +1,233 @@
# PathEditor v4.2 代码审查 — 隐含 Bug 分析报告
> 审查日期:2026-05-28 | 审查范围:全部前端核心模块 + Rust 后端 + E2E 测试
---
## BUG 1 [HIGH] — undo/redo TOGGLE 后不持久化 disabled 状态
### 位置
`src/store/app-store.ts``togglePath()` (L213-237) vs `undo()` (L239-248) / `redo()` (L251-260)
### 现象
1. 用户勾选复选框禁用一个路径 → `togglePath` 立即调用 `invoke('save_disabled_state', ...)` 写入 `disabled.json`
2. 用户按 Ctrl+Z 撤销 → `undo()` 恢复内存中的 `enabled` 状态,但**不调用** `save_disabled_state`
3. `disabled.json` 中该路径仍标记为 disabled
4. 下次启动 `loadPaths` 时,从 `disabled.json` 读到的是旧数据,路径又被标记为 disabled
### 根因
`undo()``redo()` 中处理 TOGGLE 时,只更新了 Zustand store 中的 `sysPaths`/`userPaths`,没有像 `togglePath` 那样同步持久化 disabled 状态。
```typescript
// togglePath — 有持久化
invoke('save_disabled_state', { system: sysDisabled, user: usrDisabled }).catch(() => {});
// undo/redo — 没有持久化!
set({
sysPaths: result[0], userPaths: result[1], selectedIndices: [],
isModified: !(arraysEqual(result[0], _savedSys) && arraysEqual(result[1], _savedUser)),
});
```
### 影响
- 撤销 toggle 后刷新/重启应用,disabled 状态恢复为撤销前的值
- 数据和 UI 展示不一致
### 修复方向
`undo()``redo()` 中,检测当前/上一条记录是否为 TOGGLE 类型,如果是则同步调用 `save_disabled_state`。或者更通用的方案:在 `set()` 之后统一检查 disabled 状态是否有变化并持久化。
---
## BUG 2 [MEDIUM] — `expand_env_vars` 未检测缓冲区截断
### 位置
`src-tauri/src/commands/system.rs:40-58``expand_env_vars()`
### 现象
Windows API `ExpandEnvironmentStringsW` 的行为:
- 第 1 次调用(`lpDst = NULL, nSize = 0`):返回所需缓冲区大小(TCHAR 数)
- 第 2 次调用(提供缓冲区):若缓冲区足够,返回写入的 TCHAR 数(≤ nSize);**若缓冲区不足,返回所需大小(> nSize),而非 0**
当前代码:
```rust
let required = unsafe {
ExpandEnvironmentStringsW(wide_path.as_ptr(), std::ptr::null_mut(), 0)
};
// ...
let mut buffer: Vec<u16> = vec![0; required as usize];
let result = unsafe {
ExpandEnvironmentStringsW(wide_path.as_ptr(), buffer.as_mut_ptr(), required)
};
if result == 0 {
// 仅处理了 API 失败
log::warn!("expand_env_vars: 展开失败, 返回原始路径: {path}");
return path.to_string();
}
// result > 0 但可能 > required(截断!)— 未检测
```
### 触发条件(低概率但真实存在)
环境变量在两次 `ExpandEnvironmentStringsW` 调用之间被修改:
1. 第 1 次调用查询到 `%VAR%` 展开后为 50 字符,`required = 51`(含 null
2. 在第 1 次和第 2 次调用之间,另一个进程修改了 `%VAR%` 使其变为 200 字符
3. 第 2 次调用时 51 大小的缓冲区不够,API 返回 201
4. `result = 201 > 51``result != 0`,代码未进入错误分支
5. 函数返回截断的不完整路径
### 修复方向
```rust
if result == 0 || result > required {
log::warn!("expand_env_vars: 展开失败或缓冲区不足, 返回原始路径: {path}");
return path.to_string();
}
```
---
## BUG 3 [MEDIUM] — E2E mock `load_disabled_state` 返回格式与 Rust 端不匹配
### 位置
- `e2e/mocks/ipc.ts:9`
- `src-tauri/src/commands/disabled.rs:44``load_disabled_state()`
- `src/store/app-store.ts:275``loadPaths()` 消费端
### 现象
**Rust 端返回类型**`Result<(Vec<String>, Vec<String>), String>`
Tauri 将元组序列化为 JSON 数组:`[["disabled_sys_1"], ["disabled_usr_1"]]`
**Mock 返回**
```javascript
case 'load_disabled_state': return { system: [], user: [] };
// ^^^^^^^^^^^^^^^^^^^^^^^^ 这是一个对象!
```
**前端消费**
```typescript
const result = await invoke<[string[], string[]]>('load_disabled_state');
sysDisabled = result[0]; // 从对象取 → undefined
usrDisabled = result[1]; // 从对象取 → undefined
new Set(sysDisabled); // → TypeError: undefined is not iterable
```
try/catch 捕获了 TypeError`sysDisabled`/`usrDisabled` 保持为初始空数组 `[]`
### 影响
- 生产环境无影响(Rust 端正确返回数组)
- E2E 测试中**disabled state 加载路径完全未被测试**(被 try/catch 静默跳过)
- 如果将来 E2E 测试要覆盖 disabled state 的加载-合并逻辑,会得到错误结果
### 修复方向
```javascript
case 'load_disabled_state': return []; // 返回空元组 [[], []]
// 或者如果要 mock 有禁用路径的场景:
case 'load_disabled_state': return [['C:\\disabled_path'], []];
```
---
## BUG 4 [LOW] — `savePaths` 双 hive 失败时丢失错误原因
### 位置
`src/store/app-store.ts:332-336`
### 现象
```typescript
const reason = (!sysOk && sysResult.status === 'rejected') ? String(sysResult.reason) :
(!userOk && userResult.status === 'rejected') ? String(userResult.reason) : '';
const msg = sysOk ? '用户 PATH 保存失败' :
userOk ? '系统 PATH 保存失败' :
`保存失败: ${reason}`;
```
当**两个 hive 都保存失败**时:
- `sysOk` = false,跳过第 1 个三元分支
- `userOk` = false,跳过第 2 个三元分支
- `reason` 取的是 `sysResult.reason`(第 1 行),`userResult.reason` 被丢弃
- 最终消息:`保存失败: <仅系统 PATH 的错误原因>`
### 修复方向
```typescript
const sysErr = (!sysOk && sysResult.status === 'rejected') ? String(sysResult.reason) : '';
const usrErr = (!userOk && userResult.status === 'rejected') ? String(userResult.reason) : '';
const parts = [sysErr, usrErr].filter(Boolean);
const msg = parts.length > 0 ? `保存失败: ${parts.join('; ')}` : '保存失败';
```
或者更清晰地分别显示两个 hive 的状态。
---
## BUG 5 [LOW] — `handleImportSelect` 导入 both 产生两条 undo 记录
### 位置
`src/hooks/use-app-actions.ts:160-165`
### 现象
```typescript
const handleImportSelect = useCallback((target: 'system' | 'user' | 'both') => {
const { system, user } = dialogs.importDialog;
const flat = flattenImportResult({ system, user }, target);
if (flat.system.length > 0)
useAppStore.getState().replacePaths(TargetType.SYSTEM, flat.system.map(e => e.path));
if (flat.user.length > 0)
useAppStore.getState().replacePaths(TargetType.USER, flat.user.map(e => e.path));
...
}, ...);
```
当用户选择「导入到两者」时:
1. 调用 `replacePaths(TargetType.SYSTEM, ...)` → 创建一条 IMPORT undo 记录
2. 调用 `replacePaths(TargetType.USER, ...)` → 创建另一条 IMPORT undo 记录
用户按一次 Ctrl+Z 只能撤销一个 hive 的导入,需要按两次才能完全撤销。
### 修复方向
`app-store.ts` 中新增一个 `replaceBothPaths` 方法,将 system + user 的替换合并为一条 undo 记录,或者把 `replacePaths` 扩展为支持同时替换两个 hive。
---
## 非 Bug 问题(值得关注但暂不紧急)
| # | 位置 | 描述 |
|---|------|------|
| 1 | `PathTable.tsx:35-36` | `validatedRef` / `expandedRef` 随会话持续增长,不清理。Set 中的 key 是路径字符串,正常使用下数量有限(几十到几百),无实际性能影响 |
| 2 | `use-keyboard.ts:39-54` | 非管理员模式下 Ctrl+Z/Y/N/S/Delete 静默忽略,用户无任何反馈 |
| 3 | `app-store.ts:317` | `backup_registry` 失败时 set statusMessage 为警告,但若后续保存成功,`statusMessage` 被覆盖为「保存成功」,用户看不到备份失败的警告 |
| 4 | `MergePreview.tsx:57` | `key={${source}-${displayIndex}}` — key 由翻译后的 source 字符串和 displayIndex 拼接,理论上在极端场景(同一秒内两次渲染翻译变化)可能重复,实际概率极低 |
| 5 | `system.rs:44-45` | `ExpandEnvironmentStringsW` 返回 0 时只 `log::warn`,可通过 `GetLastError` 获取具体错误码来改进日志 |
| 6 | `disabled.rs:54-56` | `load_disabled_state``content.trim().is_empty()` 检查在 `serde_json::from_str` 之前,空文件返回空数组。但如果文件包含有效 JSON 空对象 `{}`,反序列化为 `DisabledState::default()` 也正确。逻辑完整 |
---
## 修复优先级建议
| 优先级 | Bug | 理由 |
|--------|-----|------|
| **P0** | BUG 1 — undo/redo 不持久化 disabled | 用户可感知的数据不一致 |
| **P1** | BUG 2 — expand_env_vars 截断 | 概率低但后果是静默数据损坏 |
| **P2** | BUG 3 — E2E mock 格式 | 不影响生产,但阻碍 E2E 测试扩展 |
| **P3** | BUG 4 — 双失败错误消息 | 边缘场景的 UX 问题 |
| **P3** | BUG 5 — 导入双 undo | 用户体验小瑕疵 |
+1 -1
View File
@@ -6,7 +6,7 @@ export function createIpcMock() {
case 'check_admin': return true;
case 'load_system_paths': return ['C:\\\\Windows', 'C:\\\\Program Files'];
case 'load_user_paths': return ['C:\\\\Users\\\\me\\\\AppData'];
case 'load_disabled_state': return { system: [], user: [] };
case 'load_disabled_state': return [[], []];
case 'save_system_paths': return undefined;
case 'save_user_paths': return undefined;
case 'save_disabled_state': return undefined;
+1 -1
View File
@@ -1,6 +1,6 @@
[package]
name = "patheditor"
version = "4.0.0"
version = "4.2.0"
description = "Windows PATH Environment Variable Editor"
authors = ["刘航宇"]
license = "MIT"
+2 -2
View File
@@ -53,8 +53,8 @@ pub fn expand_env_vars(path: &str) -> String {
ExpandEnvironmentStringsW(wide_path.as_ptr(), buffer.as_mut_ptr(), required)
};
if result == 0 {
log::warn!("expand_env_vars: 展开失败, 返回原始路径: {path}");
if result == 0 || result > required {
log::warn!("expand_env_vars: 展开失败或缓冲区不足, 返回原始路径: {path}");
return path.to_string();
}
+2 -2
View File
@@ -1,7 +1,7 @@
{
"$schema": "../node_modules/@tauri-apps/cli/config.schema.json",
"productName": "PathEditor",
"version": "4.0.0",
"version": "4.2.0",
"identifier": "com.liuhangyu.patheditor",
"build": {
"frontendDist": "../dist",
@@ -12,7 +12,7 @@
"app": {
"windows": [
{
"title": "PathEditor v4.0",
"title": "PathEditor v4.2",
"width": 900,
"height": 700,
"minWidth": 800,
+17 -1
View File
@@ -5,7 +5,7 @@
import type { PathEntry } from './path-entry';
export const OperationType = {
ADD: 0, DELETE: 1, EDIT: 2, MOVE_UP: 3, MOVE_DOWN: 4, CLEAN: 5, CLEAR: 6, IMPORT: 7, TOGGLE: 8,
ADD: 0, DELETE: 1, EDIT: 2, MOVE_UP: 3, MOVE_DOWN: 4, CLEAN: 5, CLEAR: 6, IMPORT: 7, TOGGLE: 8, IMPORT_BOTH: 9,
} as const;
export type OperationType = (typeof OperationType)[keyof typeof OperationType];
@@ -21,6 +21,10 @@ export interface OpRecord {
newPaths: PathEntry[];
/** DELETE 操作专用:被删除的各路径的原始 index(升序) */
indices?: number[];
/** IMPORT_BOTH 专用:用户 hive 的旧路径 */
oldPathsOther?: PathEntry[];
/** IMPORT_BOTH 专用:用户 hive 的新路径 */
newPathsOther?: PathEntry[];
}
const DEFAULT_MAX_SIZE = 50;
@@ -88,6 +92,12 @@ export class UndoRedoManager {
case OperationType.TOGGLE:
target[rec.index] = rec.oldPaths[0];
break;
case OperationType.IMPORT_BOTH:
sys.length = 0;
sys.push(...rec.oldPaths);
user.length = 0;
user.push(...(rec.oldPathsOther || []));
return [sys, user];
}
return [sys, user];
@@ -138,6 +148,12 @@ export class UndoRedoManager {
case OperationType.TOGGLE:
target[rec.index] = rec.newPaths[0];
break;
case OperationType.IMPORT_BOTH:
sys.length = 0;
sys.push(...rec.newPaths);
user.length = 0;
user.push(...(rec.newPathsOther || []));
return [sys, user];
}
return [sys, user];
+6 -2
View File
@@ -160,8 +160,12 @@ export function useAppActions(activeTab: TabId, dialogs: DialogState) {
const handleImportSelect = useCallback((target: 'system' | 'user' | 'both') => {
const { system, user } = dialogs.importDialog;
const flat = flattenImportResult({ system, user }, target);
if (flat.system.length > 0) useAppStore.getState().replacePaths(TargetType.SYSTEM, flat.system.map(e => e.path));
if (flat.user.length > 0) useAppStore.getState().replacePaths(TargetType.USER, flat.user.map(e => e.path));
if (target === 'both' && flat.system.length > 0 && flat.user.length > 0) {
useAppStore.getState().replaceBothPaths(flat.system.map(e => e.path), flat.user.map(e => e.path));
} else {
if (flat.system.length > 0) useAppStore.getState().replacePaths(TargetType.SYSTEM, flat.system.map(e => e.path));
if (flat.user.length > 0) useAppStore.getState().replacePaths(TargetType.USER, flat.user.map(e => e.path));
}
setImportDialog({ open: false, system: [], user: [] });
}, [dialogs.importDialog, setImportDialog]);
+1
View File
@@ -38,6 +38,7 @@
"readonly": "Read-only mode — Administrator privileges required for editing",
"saving": "Saving...",
"saved": "Saved successfully",
"saved_without_backup": "Saved (backup failed)",
"error": "Operation failed",
"warning_backup": "Backup creation failed, save will proceed without backup",
"deleted": "Deleted {{count}} path(s)",
+1
View File
@@ -38,6 +38,7 @@
"readonly": "只读模式 — 需要管理员权限才能编辑",
"saving": "正在保存...",
"saved": "保存成功",
"saved_without_backup": "保存成功(备份失败)",
"error": "操作失败",
"warning_backup": "备份创建失败,保存将继续但不生成备份",
"deleted": "已删除 {{count}} 个路径",
+34 -5
View File
@@ -36,6 +36,7 @@ interface AppState {
moveDown: (index: number, target: TargetType) => void;
cleanPaths: (target: TargetType, validateFn: (p: string) => boolean) => string[];
replacePaths: (target: TargetType, newPaths: string[]) => void;
replaceBothPaths: (sysPaths: string[], userPaths: string[]) => void;
clearPaths: (target: TargetType) => void;
togglePath: (index: number, target: TargetType) => void;
@@ -195,6 +196,20 @@ export const useAppStore = create<AppState>((set, get) => {
markDirty();
},
replaceBothPaths: (sysPaths, userPaths) => {
const state = get();
const sysEntries: PathEntry[] = sysPaths.map(p => ({ path: p, enabled: true }));
const usrEntries: PathEntry[] = userPaths.map(p => ({ path: p, enabled: true }));
state.undoRedo.push({
type: OperationType.IMPORT_BOTH, target: TargetType.SYSTEM, index: 0,
count: sysEntries.length + usrEntries.length,
oldPaths: [...state.sysPaths], newPaths: [...sysEntries],
oldPathsOther: [...state.userPaths], newPathsOther: [...usrEntries],
});
set({ sysPaths: [...sysEntries], userPaths: [...usrEntries], selectedIndices: [] });
markDirty();
},
clearPaths: (target) => {
const state = get();
const list = target === TargetType.SYSTEM ? state.sysPaths : state.userPaths;
@@ -245,6 +260,11 @@ export const useAppStore = create<AppState>((set, get) => {
// 内联计算 isModified 而非调用 markDirty(),避免两次 set() 导致额外渲染
isModified: !(arraysEqual(result[0], _savedSys) && arraysEqual(result[1], _savedUser)),
});
// 同步持久化 disabled 状态,与 togglePath 保持一致
invoke('save_disabled_state', {
system: result[0].filter(e => !e.enabled).map(e => e.path),
user: result[1].filter(e => !e.enabled).map(e => e.path),
}).catch(() => {});
}
},
@@ -257,6 +277,11 @@ export const useAppStore = create<AppState>((set, get) => {
// 内联计算 isModified 而非调用 markDirty(),避免两次 set() 导致额外渲染
isModified: !(arraysEqual(result[0], _savedSys) && arraysEqual(result[1], _savedUser)),
});
// 同步持久化 disabled 状态,与 togglePath 保持一致
invoke('save_disabled_state', {
system: result[0].filter(e => !e.enabled).map(e => e.path),
user: result[1].filter(e => !e.enabled).map(e => e.path),
}).catch(() => {});
}
},
@@ -314,8 +339,9 @@ export const useAppStore = create<AppState>((set, get) => {
}
// 备份当前注册表(保存前备份旧值,失败仅警告不中断)
let backupFailed = false;
await invoke('backup_registry', { customDir: null })
.catch(() => set({ statusMessage: i18n.t('status.warning_backup') }));
.catch(() => { backupFailed = true; });
const [sysResult, userResult] = await Promise.allSettled([
invoke('save_system_paths', { paths: sysPaths }),
@@ -328,11 +354,14 @@ export const useAppStore = create<AppState>((set, get) => {
if (sysOk && userOk) {
invoke('broadcast_env_change').catch(() => {});
const savedSys = [...state.sysPaths], savedUser = [...state.userPaths];
set({ isModified: false, isSaving: false, statusMessage: i18n.t('status.saved'), _savedSys: savedSys, _savedUser: savedUser });
set({ isModified: false, isSaving: false,
statusMessage: backupFailed ? i18n.t('status.saved_without_backup') : i18n.t('status.saved'),
_savedSys: savedSys, _savedUser: savedUser });
} else {
const reason = (!sysOk && sysResult.status === 'rejected') ? String(sysResult.reason) :
(!userOk && userResult.status === 'rejected') ? String(userResult.reason) : '';
const msg = sysOk ? '用户 PATH 保存失败' : userOk ? '系统 PATH 保存失败' : `保存失败: ${reason}`;
const sysErr = (!sysOk && sysResult.status === 'rejected') ? String(sysResult.reason) : '';
const usrErr = (!userOk && userResult.status === 'rejected') ? String(userResult.reason) : '';
const parts = [sysErr, usrErr].filter(Boolean);
const msg = sysOk ? '用户 PATH 保存失败' : userOk ? '系统 PATH 保存失败' : `保存失败: ${parts.join('; ')}`;
set({ isSaving: false, statusMessage: msg });
}
},
+1 -1
View File
@@ -2,7 +2,7 @@ import { describe, it, expect, vi, beforeEach } from 'vitest';
// Mock @tauri-apps/api/core
vi.mock('@tauri-apps/api/core', () => ({
invoke: vi.fn(),
invoke: vi.fn().mockResolvedValue(undefined),
}));
// Mock i18n