Undo, Redo 기능 추가
This commit is contained in:
@@ -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;
|
||||
}
|
||||
Reference in New Issue
Block a user