add undo option

This commit is contained in:
2025-10-13 10:58:03 +02:00
parent cb730237f5
commit 4df063f9b3
3 changed files with 127 additions and 0 deletions

View File

@ -0,0 +1,64 @@
export interface AudioState {
leftChannel: Float32Array;
rightChannel: Float32Array;
params: any;
isProcessed: boolean;
waveformColor: string;
engineIndex: number;
}
export class UndoManager {
private undoStack: AudioState[] = [];
private readonly maxHistorySize: number;
constructor(maxHistorySize: number = 20) {
this.maxHistorySize = maxHistorySize;
}
pushState(state: AudioState): void {
this.undoStack.push({
leftChannel: state.leftChannel.slice(),
rightChannel: state.rightChannel.slice(),
params: state.params,
isProcessed: state.isProcessed,
waveformColor: state.waveformColor,
engineIndex: state.engineIndex,
});
if (this.undoStack.length > this.maxHistorySize) {
this.undoStack.shift();
}
}
undo(): AudioState | null {
const previousState = this.undoStack.pop();
return previousState || null;
}
canUndo(): boolean {
return this.undoStack.length > 0;
}
clear(): void {
this.undoStack = [];
}
static captureState(
buffer: AudioBuffer | null,
params: any,
isProcessed: boolean,
waveformColor: string,
engineIndex: number
): AudioState | null {
if (!buffer) return null;
return {
leftChannel: buffer.getChannelData(0).slice(),
rightChannel: buffer.getChannelData(1).slice(),
params,
isProcessed,
waveformColor,
engineIndex,
};
}
}

View File

@ -8,6 +8,7 @@ export interface KeyboardActions {
onVolumeDecrease?: (large: boolean) => void;
onVolumeIncrease?: (large: boolean) => void;
onEscape?: () => void;
onUndo?: () => void;
}
export function createKeyboardHandler(actions: KeyboardActions) {
@ -18,6 +19,9 @@ export function createKeyboardHandler(actions: KeyboardActions) {
const isLargeAdjustment = event.shiftKey;
switch (key) {
case 'z':
actions.onUndo?.();
break;
case 'm':
actions.onMutate?.();
break;