UNPKG

mathsteps-experimental-fork

Version:

Step by step math solutions. Experimental Fork

52 lines (50 loc) 1.09 kB
class UndoableString { initialValue; value; history; future; constructor(initialValue = "") { this.value = initialValue; this.initialValue = initialValue; this.history = []; this.future = []; } getInitialValue() { return this.initialValue; } // Get the current value of the string getValue() { return this.value; } getHistory() { return this.history; } getFuture() { return this.future; } // Update the value and save the current state to the history setValue(newValue) { this.history.push(this.value); this.value = newValue; this.future = []; } // Undo the last operation undo() { if (this.history.length > 0) { this.future.push(this.value); this.value = this.history.pop(); } else { console.warn("Undo not available"); } } // Redo the last undone operation redo() { if (this.future.length > 0) { this.history.push(this.value); this.value = this.future.pop(); } else { console.warn("Redo not available"); } } } export { UndoableString };