59 lines
1.3 KiB
JavaScript
59 lines
1.3 KiB
JavaScript
// SonicForge Studio - DAW Command Dispatcher
|
|
// Command Pattern & Undo/Redo Engine (28_AI_PANEL.md §1 & §2)
|
|
|
|
const DAWCommandDispatcher = (function() {
|
|
const MAX_HISTORY = 50;
|
|
const history = [];
|
|
let historyIndex = -1;
|
|
|
|
function pushHistory(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; }
|
|
|
|
const registry = {};
|
|
|
|
function register(name, handler) {
|
|
registry[name] = handler;
|
|
}
|
|
|
|
function execute(name, args) {
|
|
if (!registry[name]) {
|
|
return { success: false, error: `Unknown command: ${name}` };
|
|
}
|
|
return registry[name](args);
|
|
}
|
|
|
|
return {
|
|
register,
|
|
execute,
|
|
undo,
|
|
redo,
|
|
canUndo,
|
|
canRedo,
|
|
pushHistory,
|
|
get history() { return history; },
|
|
get historyIndex() { return historyIndex; }
|
|
};
|
|
})();
|
|
|
|
window.DAWCommandDispatcher = DAWCommandDispatcher;
|