Skip to content

Repository files navigation

Undo

A simple undo / redo manager. This was designed to work well with Replicache, but can be used entirely independently. From this library's perspective, undo/redo actions are just functions and this library doesn't care what those functions do. Please use Replidraw as a reference on how to use the library.

Installation

npm install @rocicorp/undo

Replicache Usage Example

import{Replicache}from"replicache";import{useSubscribe}from"replicache-react";
...
import{UndoManager}from'@rocicorp/undo';// Replicache and UndoManager are initialized outside of the initial component render.// undoManager = new UndoManager()constApp=({ rep }: {rep: Replicache<M>,undoManager: UndoManager})=>{consttodos=useSubscribe(rep,listTodos,[],[rep]);// new item with undoconsthandleNewItem=(text: string)=>{constid=nanoid();//function that will redo and executeconstputTodo=()=>{rep.mutate.putTodo({
id,text: text,sort: todos.length>0 ? todos[todos.length-1].sort+1 : 0,completed: false,});};//undo functionconstremoveTodo=()=>rep.mutate.deleteTodos([id]);undoManager.add({execute: putTodo,undo: removeTodo,});};return(<div><HeaderonNewItem={handleNewItem}/><MainSectiontodos={todos}/></div>);}

Basic Usage Example

import{UndoManager}from'@rocicorp/undo';constundoManager=newUndoManager();constmodifiedValue=0;undoManager.add({undo: ()=>{modifiedValue--;},execute: ()=>{modifiedValue++;},});

API

constructor

Constructor for UndoManager

ParamTypeDescription
optionsUnderManagerOptionsOptions passed for undo manager constructor

UnderManagerOptions

ParamTypeDescription
maxSizeObjectThe maximum number of entries in the stack. Default is 10000.
onChange(undoManager: UndoRedoStackState) => voidA callback function to be called when the stack canUndo or canRedo values change.

Example

constundoManager=newUndoManager({10,(e: UndoRedoStackState)=>{console.log('undo manager canUndo or canRedo values changed -- ',e.canUndo,e.canRedo);}});

canUndo

Determines if a user can perform the undo operation on the undoRedo stack.


canRedo

Determines if a user can perform the redo operation on the undoRedo stack.


add

Adds an entry to the undoRedo stack.

ParamTypeDescription
optionsAddOptionsA UndoRedo or ExecuteUndo object that can is added to the stack. If it is an ExecuteUndo it will run the execute function immediately after adding to the stack.

AddOptions

typeAddOptions=UndoRedo|ExecuteUndo;typeUndoRedo={redo: ()=>MaybePromise<void>;undo: ()=>MaybePromise<void>;};typeExecuteUndo={execute: ()=>MaybePromise<void>;undo: ()=>MaybePromise<void>;};

undo

Executes the undo function of the current entry in the undoRedo stack. If the current entry has groupID it will check the upcoming undo entry. If the upcoming undo entry also has the same groupID the function will recursively call undo until it runs into a entry that has has a different groupID or is undefined.


redo

Executes the redo function of the current entry in the undoRedo stack. If the current entry has groupID it will check the upcoming redo entry. If the upcoming redo entry also has the same groupID the function will recursively call redo until it runs into a entry that has has a different groupID or is undefined.


startGroup

Sets the undo manager to add groupID to all subsequent entries. Sets the isGrouping internal state of the stack to true


endGroup

Sets the undo manager to mark all subsequent added entries groupID to undefined. Sets the isGrouping internal state of the stack to false


About

Undo Redo Manager

Resources

Code of conduct

Stars

68 stars

Watchers

6 watching

Forks

Releases

Packages

Used by

Contributors

Languages