From 2775a3a5881ac96e0b404057e7ddabebca57bac7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E8=88=AA=E5=AE=87?= <3364451258@qq.com> Date: Tue, 26 May 2026 21:57:21 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E9=9D=9E=E8=BF=9E=E7=BB=AD=E5=88=A0?= =?UTF-8?q?=E9=99=A4=20undo=20=E6=81=A2=E5=A4=8D=E5=88=B0=E9=94=99?= =?UTF-8?q?=E8=AF=AF=E4=BD=8D=E7=BD=AE=20=E2=80=94=20OpRecord=20=E6=96=B0?= =?UTF-8?q?=E5=A2=9E=20indices=20=E7=B2=BE=E7=A1=AE=E8=AE=B0=E5=BD=95?= =?UTF-8?q?=E5=8E=9F=E5=A7=8B=E4=BD=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Opus 4.7 --- src/core/undo-redo.ts | 22 ++++++++++++++++++---- src/store/app-store.ts | 11 ++++++----- tests/unit/app-store.test.ts | 12 ++++++++++++ tests/unit/undo-redo.test.ts | 22 ++++++++++++++++++++++ 4 files changed, 58 insertions(+), 9 deletions(-) diff --git a/src/core/undo-redo.ts b/src/core/undo-redo.ts index dd4894f..d38722b 100644 --- a/src/core/undo-redo.ts +++ b/src/core/undo-redo.ts @@ -17,6 +17,8 @@ export interface OpRecord { count: number; oldPaths: string[]; newPaths: string[]; + /** DELETE 操作专用:被删除的各路径的原始 index(升序) */ + indices?: number[]; } const DEFAULT_MAX_SIZE = 50; @@ -54,8 +56,14 @@ export class UndoRedoManager { target.splice(target.length - rec.count, rec.count); break; case OperationType.DELETE: - for (let i = 0; i < rec.count; i++) { - target.splice(rec.index + i, 0, rec.oldPaths[i]); + if (rec.indices) { + for (let i = 0; i < rec.indices.length; i++) { + target.splice(rec.indices[i], 0, rec.oldPaths[i]); + } + } else { + for (let i = 0; i < rec.count; i++) { + target.splice(rec.index + i, 0, rec.oldPaths[i]); + } } break; case OperationType.EDIT: @@ -95,8 +103,14 @@ export class UndoRedoManager { target.push(...rec.newPaths); break; case OperationType.DELETE: - for (let i = rec.count - 1; i >= 0; i--) { - target.splice(rec.index + i, 1); + if (rec.indices) { + for (let i = rec.indices.length - 1; i >= 0; i--) { + target.splice(rec.indices[i], 1); + } + } else { + for (let i = rec.count - 1; i >= 0; i--) { + target.splice(rec.index + i, 1); + } } break; case OperationType.EDIT: diff --git a/src/store/app-store.ts b/src/store/app-store.ts index 32c7e38..7fbd053 100644 --- a/src/store/app-store.ts +++ b/src/store/app-store.ts @@ -105,17 +105,18 @@ export const useAppStore = create((set, get) => ({ if (indices.length === 0) return; const state = get(); const list = target === TargetType.SYSTEM ? state.sysPaths : state.userPaths; - const sorted = [...indices].sort((a, b) => b - a); - const oldPaths = sorted.map((i) => list[i]); + const sortedDesc = [...indices].sort((a, b) => b - a); + const sortedAsc = [...indices].sort((a, b) => a - b); + const oldPaths = sortedAsc.map((i) => list[i]); - // 单条撤销记录覆盖全部删除 state.undoRedo.push({ type: OperationType.DELETE, target, - index: sorted[sorted.length - 1], count: sorted.length, + index: sortedAsc[0], count: sortedAsc.length, oldPaths, newPaths: [], + indices: sortedAsc, }); - const toRemove = new Set(sorted); + const toRemove = new Set(sortedDesc); const newList = list.filter((_, i) => !toRemove.has(i)); if (target === TargetType.SYSTEM) set({ sysPaths: newList, selectedIndices: [] }); else set({ userPaths: newList, selectedIndices: [] }); diff --git a/tests/unit/app-store.test.ts b/tests/unit/app-store.test.ts index f9fcf5d..de73fea 100644 --- a/tests/unit/app-store.test.ts +++ b/tests/unit/app-store.test.ts @@ -95,6 +95,18 @@ describe('app-store CRUD', () => { expect(useAppStore.getState().userPaths).toEqual(['A', 'C']); }); + it('deletePaths 非连续多选删除后可 undo 恢复到正确位置', () => { + const store = useAppStore.getState(); + store.addPath('A', TargetType.SYSTEM); + store.addPath('B', TargetType.SYSTEM); + store.addPath('C', TargetType.SYSTEM); + store.addPath('D', TargetType.SYSTEM); + store.deletePaths([1, 3], TargetType.SYSTEM); + expect(useAppStore.getState().sysPaths).toEqual(['A', 'C']); + useAppStore.getState().undo(); + expect(useAppStore.getState().sysPaths).toEqual(['A', 'B', 'C', 'D']); + }); + it('moveUp index=0 无操作', () => { const store = useAppStore.getState(); store.addPath('A', TargetType.SYSTEM); diff --git a/tests/unit/undo-redo.test.ts b/tests/unit/undo-redo.test.ts index e0a56fd..b3b90ba 100644 --- a/tests/unit/undo-redo.test.ts +++ b/tests/unit/undo-redo.test.ts @@ -128,6 +128,28 @@ describe('UndoRedoManager', () => { expect(small.historyLength).toBe(3); }); + it('非连续多选 DELETE 撤销恢复到原始位置', () => { + // 扩展初始数组 + sys.push('C:\\Extra1', 'C:\\Extra2'); + const old = [...sys]; + // 删除 indices [1, 3](C:\Program Files 和 C:\Extra2) + const removed = [sys[1], sys[3]]; + mgr.push({ + type: OperationType.DELETE, target: TargetType.SYSTEM, + index: 1, count: 2, + oldPaths: removed, newPaths: [], + indices: [1, 3], + }); + sys.splice(3, 1); + sys.splice(1, 1); + + const u = mgr.undo(sys, user)!; + expect(u[0]).toEqual(old); + + const r = mgr.redo(...u)!; + expect(r[0]).toEqual(['C:\\Windows', 'C:\\Extra1']); + }); + it('操作 USER 路径', () => { user.push('C:\\NewUserPath'); mgr.push(makeRecord(OperationType.ADD, TargetType.USER, 1, 1, [], ['C:\\NewUserPath']));