Documentation

Undo and redo

Connect Undo and Redo controls to the editor history.

Try it

Type in the editor and click Undo, then Redo. Undo also restores the saved selection. Use Mod+Z to undo, and Mod+Shift+Z or Mod+Y to redo.

Live example
Example result

Add it to your editor

History is available on the editor instance. Use useCanUndo and useCanRedo to make your buttons update after edits.

import { EditorContent, EditorProvider, useCanRedo, useCanUndo, useEditor } from 'yurich';

export function HistoryEditor() {
  const editor = useEditor({
    extensions: [],
    initialValue: "<p>Delete this line. Undo has your back.</p>",
    historyLimit: 50,
  });
  const canUndo = useCanUndo(editor);
  const canRedo = useCanRedo(editor);

  return (
    <EditorProvider editor={editor}>
      <div role="group" aria-label="Edit history">
        <button type="button" disabled={!canUndo} onClick={() => editor.history.undo()}>
          Undo
        </button>
        <button type="button" disabled={!canRedo} onClick={() => editor.history.redo()}>
          Redo
        </button>
      </div>
      <EditorContent ariaLabel="Editor" />
    </EditorProvider>
  );
}

Behavior to account for

historyLimit controls the undo depth and defaults to 100. Load an unrelated document with setHTML(html, { history: false }) when the previous document must not be restored through Undo.

Features