Documentation 1.11.6

Undo and Redo

This page explains how editorHistory records editing state and how undo() / redo() restore the content and the caret.

What Is Recorded

Each entry is a snapshot, not a diff:

Field Content
index Row of the caret (0-based)
offset Caret position within that row
content innerText of every <div> row joined with \n

Undo and redo are plain arrays with no size limit; continuous editing of a long document grows memory linearly with the number of steps.

When Entries Are Written

Action Timing
init() (on construction and clear()) Initial state written immediately
Normal typing (input event) Delayed 300 ms; typing again cancels the previous timer, so a burst of typing becomes one entry
Cut, paste, Enter, Backspace, Tab, Cmd/Ctrl+] Written via a 0 ms timer
Formatting methods (bold(), heading(), ...) Written via a 0 ms timer

Every write clears the redo stack, so any edit after an undo makes redo unavailable.

undo()

  1. Takes the second-to-last undo entry as the target; does nothing when fewer than two entries exist
  2. Moves the last entry onto the redo stack
  3. Calls init(content, false, false) with the target's content to rebuild the rows without recording
  4. Puts the caret back at the target's index / offset
  5. Updates the preview when autosave is truthy

redo()

Pops the last redo entry, pushes it back onto the undo stack, then rebuilds, restores the caret and updates the preview just like undo().

Shortcuts

Combo Method
Cmd/Ctrl+Z undo()
Cmd/Ctrl+Shift+Z redo()

Both work even with hotKey: false. goBack() and goForward() are the old names; see Deprecations.

中文