Undo, Redo 기능 추가

This commit is contained in:
2026-05-15 22:24:25 +09:00
parent 31ecf94142
commit a1df424485
4 changed files with 96 additions and 2 deletions
+33
View File
@@ -0,0 +1,33 @@
const MAX = 50;
let _stack = []; // each entry: { files: [], affectedPaths: Set | null }
let _cursor = -1;
export function push(files, affectedPaths = null) {
_stack = _stack.slice(0, _cursor + 1);
_stack.push({ files: [...files], affectedPaths: affectedPaths ? new Set(affectedPaths) : null });
if (_stack.length > MAX) _stack.shift();
_cursor = _stack.length - 1;
}
// Returns { files, affectedPaths } where affectedPaths are from the entry being undone
export function undo() {
if (!canUndo()) return null;
const affectedPaths = _stack[_cursor].affectedPaths;
--_cursor;
return { files: [..._stack[_cursor].files], affectedPaths };
}
// Returns { files, affectedPaths } from the entry being redone
export function redo() {
if (!canRedo()) return null;
++_cursor;
return { files: [..._stack[_cursor].files], affectedPaths: _stack[_cursor].affectedPaths };
}
export function canUndo() { return _cursor > 0; }
export function canRedo() { return _cursor < _stack.length - 1; }
export function reset() {
_stack = [];
_cursor = -1;
}