All files / webview/csv history.ts

100% Statements 21/21
100% Branches 6/6
100% Functions 6/6
100% Lines 19/19

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52                    3x     185x 185x     153x 153x 5x   153x       122x 119x 119x 119x       8x 5x 5x 5x       276x       176x       52x 52x      
/**
 * 通用撤销/重做栈
 * 存储状态快照(headers + rows),限制最大栈深度避免内存溢出
 */
 
export interface Snapshot {
  headers: string[];
  rows: string[][];
}
 
const MAX_HISTORY = 100;
 
export class UndoRedoStack {
  private undoStack: Snapshot[] = [];
  private redoStack: Snapshot[] = [];
 
  push(snapshot: Snapshot) {
    this.undoStack.push(snapshot);
    if (this.undoStack.length > MAX_HISTORY) {
      this.undoStack.shift();
    }
    this.redoStack = [];
  }
 
  undo(current: Snapshot): Snapshot | null {
    if (this.undoStack.length === 0) return null;
    const prev = this.undoStack.pop()!;
    this.redoStack.push(current);
    return prev;
  }
 
  redo(current: Snapshot): Snapshot | null {
    if (this.redoStack.length === 0) return null;
    const next = this.redoStack.pop()!;
    this.undoStack.push(current);
    return next;
  }
 
  canUndo(): boolean {
    return this.undoStack.length > 0;
  }
 
  canRedo(): boolean {
    return this.redoStack.length > 0;
  }
 
  clear() {
    this.undoStack = [];
    this.redoStack = [];
  }
}