Files
SonicForgeStudio/app/static/js/services/undoRedoEngine.js
T

75 lines
1.6 KiB
JavaScript

// SonicForge Studio - Unified Undo/Redo Engine
// Handles all undoable actions across MAIN SESSION and SECTION-TAB
const UndoRedoEngine = (function() {
const MAX_HISTORY = 50;
const history = [];
let historyIndex = -1;
function push(entry) {
history.push(entry);
if (history.length > MAX_HISTORY) {
history.shift();
}
historyIndex = history.length - 1;
}
function undo() {
if (historyIndex < 0) return null;
const entry = history[historyIndex];
historyIndex--;
return entry;
}
function redo() {
if (historyIndex >= history.length - 1) return null;
historyIndex++;
const entry = history[historyIndex];
return entry;
}
function canUndo() { return historyIndex >= 0; }
function canRedo() { return historyIndex < history.length - 1; }
function clear() {
history.length = 0;
historyIndex = -1;
}
function execute(entry) {
// entry: { type, scope, label, before, after, undo, redo }
// Trim future history if we're not at the end
if (historyIndex < history.length - 1) {
history.splice(historyIndex + 1);
}
push(entry);
return entry;
}
function getStatus() {
return {
canUndo: canUndo(),
canRedo: canRedo(),
undoCount: historyIndex + 1,
redoCount: history.length - historyIndex - 1,
lastAction: history[historyIndex]?.type || null,
lastLabel: history[historyIndex]?.label || null
};
}
return {
push,
undo,
redo,
canUndo,
canRedo,
clear,
execute,
getStatus,
history,
historyIndex
};
})();
window.UndoRedoEngine = UndoRedoEngine;