Skip to curriculum
Jump to the editor

19.8. Undo and redo

Medium
Topics
20–35 min

A drawing app, a form builder or a text editor all need undo and redo. The simplest version keeps whole states: every change pushes a new state, undo steps back to the one before it, and redo steps forward again until the user makes a new change, which throws the redo states away.

Write the class History in history.js. It holds one present state, the states before it (the past) and the states that undo stepped away from (the future).

Signature

JAVASCRIPT
class History {
  constructor(initial, { limit } = {})
  get present()   // the present state
  push(state)     // returns nothing
  undo()          // returns the present after the step
  redo()          // returns the present after the step
  canUndo()       // true or false
  canRedo()       // true or false
  reset(state)    // returns nothing
}
  • initial: the first present state. States can be any value; they are stored as they are, never copied.
  • limit: how many past states to keep at most. Missing means no limit (Infinity).

Requirements

  1. present starts as initial, and push(state) makes state the present.
  2. undo() makes the state before the present the new present and returns it; redo() steps forward again after an undo.
  3. undo() with no past state and redo() with no future state change nothing and return the present.
  4. A push clears the future: after undo and a new push, there is nothing to redo.
  5. Pushing a state that is the present (compared with Object.is, so NaN equals NaN and 0 differs from -0) records nothing and keeps the future.
  6. canUndo() and canRedo() say whether undo() and redo() would change the present.
  7. With limit, at most limit past states are kept; the oldest are dropped first. limit 0 keeps no past. Undo and redo never lose a kept state.
  8. A negative or NaN limit throws a RangeError.
  9. reset(state) makes state the present and forgets every past and future state.
  10. Each history has its own states.

Examples

JAVASCRIPT
const history = new History("");
history.push("H");
history.push("Hi");
history.undo();      // "H"
history.undo();      // ""
history.undo();      // "" (nothing left to undo)
history.redo();      // "H"
history.push("Ho");  // a new branch: "Hi" is gone
history.canRedo();   // false
JAVASCRIPT
const history = new History(0, { limit: 2 });
history.push(1);
history.push(2);
history.push(3);   // 0 is dropped: only 1 and 2 are kept in the past
history.undo();    // 2
history.undo();    // 1
history.canUndo(); // false
JAVASCRIPT
const history = new History(NaN);
history.push(NaN);  // the same value: nothing is recorded
history.canUndo();  // false

Environment

  • Plain JavaScript in a browser page; the tests create instances of the global class History from history.js.
  • Grouping several changes into one step and storing only the differences are not part of this problem: problem 19.9 does that for a key-value store.

Hints

  1. Which data structure gives you "the last thing in is the first thing out", for both the past and the future?
  2. What has to happen to the future when a new change arrives in the middle of the history?

The explanation comes with the course

A walkthrough of every line of a solution, the mistakes the tests catch, other ways to write it, and links to the lessons that teach each idea.

  • Every line explained
  • The mistakes each test catches
  • Links to the lessons behind it
Back to the question
Loading the editor…