Undo/redo manager
npm install undoo --save
<script src="node_modules/undoo/dist/undoo.min.js"></script>
<script src="https://unpkg.com/undoo/dist/undoo.min.js"></script>
const Undoo = require('undoo');
const myHistory = new Undoo();
myHistory
.save('one')
.save('two')
.save('three')
.save('four');
myHistory.undo((item)=>{
console.log(item); //=> three
});
myHistory.current(); //=> three
myHistory.redo((item)=>{
console.log(item); //=> four
});
const Undoo = require('undoo');
const myHistory = new Undoo({
provider: () => document.getElementById('myTextArea').value
});
myHistory.save();
Kind: global class
- Undoo
- new Undoo([opts])
- instance
- .canUndo() ⇒
boolean
- .canRedo() ⇒
boolean
- .import(history) ⇒
Undoo
- .history() ⇒
Array
- .save([item]) ⇒
Undoo
- .suspendSave([state]) ⇒
Undoo
- .allowedSave() ⇒
boolean
- .clear() ⇒
Undoo
- .undo([callback]) ⇒
Undoo
- .redo([callback]) ⇒
Undoo
- .current() ⇒
*
- .count() ⇒
number
- .initialState() ⇒
*
- .onUpdate(callback) ⇒
Undoo
- .onMaxLength(callback) ⇒
Undoo
- .onBeforeSave(callback) ⇒
Undoo
- .canUndo() ⇒
- inner
- ~undoCallback :
function
- ~redoCallback :
function
- ~updateCallback :
function
- ~maxLengthCallback :
function
- ~beforeSaveCallback :
function
- ~undoCallback :
Create instance
Param | Type | Default | Description |
---|---|---|---|
[opts] | Object | configuration object |
|
[opts.provider] | function |
| optional function called on save that returns new state for history |
[opts.maxLength] | number | 20 | max length history |
Check if undo is available
Kind: instance method of Undoo
Kind: instance method of Undoo
Check: if redo is available
undoo.import(history) ⇒ Undoo
Import external history
Kind: instance method of Undoo
Param | Type |
---|---|
history | Array |
Get history
Kind: instance method of Undoo
undoo.save([item]) ⇒ Undoo
Save history
Kind: instance method of Undoo
Param | Type |
---|---|
[item] | * |
undoo.suspendSave([state]) ⇒ Undoo
Suspend save method
Kind: instance method of Undoo
Param | Type | Default |
---|---|---|
[state] | boolean | true |
Check if save is allowed
Kind: instance method of Undoo
undoo.clear() ⇒ Undoo
Clear history
Kind: instance method of Undoo
undoo.undo([callback]) ⇒ Undoo
Undo
Kind: instance method of Undoo
Param | Type | Description |
---|---|---|
[callback] | undoCallback | callback function |
undoo.redo([callback]) ⇒ Undoo
Redo
Kind: instance method of Undoo
Param | Type | Description |
---|---|---|
[callback] | redoCallback | callback function |
Get current item in history
Kind: instance method of Undoo
Count history items, the first element is not considered
Kind: instance method of Undoo
Get initial state history
Kind: instance method of Undoo
undoo.onUpdate(callback) ⇒ Undoo
Triggered when history is updated
Kind: instance method of Undoo
Param | Type | Description |
---|---|---|
callback | updateCallback | callback function |
undoo.onMaxLength(callback) ⇒ Undoo
Triggered when maxLength is exceeded
Kind: instance method of Undoo
Param | Type | Description |
---|---|---|
callback | maxLengthCallback | callback function |
undoo.onBeforeSave(callback) ⇒ Undoo
Triggered before save
Kind: instance method of Undoo
Param | Type | Description |
---|---|---|
callback | beforeSaveCallback | callback function |
Example
// If callback returns `false` the save command will not be executed
myHistory.onBeforeSave(()=>false)
// You can overwrite item before save
myHistory.onBeforeSave((item)=>{
return item.toUpperCase();
})
undo callback
Kind: inner typedef of Undoo
Param | Type | Description |
---|---|---|
item | * | current history item |
redo callback
Kind: inner typedef of Undoo
Param | Type | Description |
---|---|---|
item | * | current history item |
onUpdate callback
Kind: inner typedef of Undoo
Param | Type | Description |
---|---|---|
item | * | current history item |
action | string | action that has called update event. Can be: redo, undo, save, clear |
history | Array | history array |
istance | Undoo |
onMaxLength callback
Kind: inner typedef of Undoo
Param | Type | Description |
---|---|---|
item | * | current history item |
history | Array | history array |
istance | Undoo |
onBeforeSave callback
Kind: inner typedef of Undoo
Param | Type | Description |
---|---|---|
item | * | current history item |
istance | Undoo |
You can view the changelog here
Undoo is open-sourced software licensed under the MIT license