Undo/Redo support for easy peasy.
patched-undo-peasy
depends on an easy peasy fork. (but modifying undo-peasy to depend on stock easy-peasy should be straightforward.)
Usage
- Attach
undoRedoMiddleWare
in createStore
.
const store = createStore(appModel, {
middleware: [undoRedo()],
});
- If using typescript, the root application model should extend
WithUndo
.
WithUndo
will add types for undo actions and undo history to your root model.
interface Model extends WithUndo {
count: number;
increment: Action<Model>;
}
- Wrap the root application instance in
undoable
.
undoable
will add types for undo actions and undo history to your root model.
const appModel: Model = undoable({
count: 0,
increment: action((state) => {
state.count++;
}),
});
- Profit
const undoAction = useStoreActions((actions) => actions.undoUndo);
Supported Actions
undoUndo
- restore state to the most recently saved version.undoRedo
- restore state to the most recently undone version.undoSave
- save current application state to undo history.
undoSave is generated automatically by the middleware, but in rare cases it's useful to save manually.undoReset
- erases saved undo/redo history and saves the current state.
Configuration
The undoRedo()
middleware function accepts an optional configuration object.
noSaveActions
- a function that tells undoRedo to not save certain actions to undo/redo history.noSaveKeys
- a function tthat tells undoRedo not to save certain keys inside the state model
to undo/redo history. e.g. view state in the model.