undone 0.0.4
undone: ^0.0.4 copied to clipboard
A little undo-redo library. Not a song about sweaters.
Undone #
A little undo-redo library.
Usage #
Build an Action from Functions #
// Actions bind a 'Do' functon and an 'Undo' function together with arguments.
Do _increment = (a) => ++a['value'];
Undo _decrement = (a, _) => --a['value'];
var map = { 'value' : 42 };
var increment = new Action(map, _increment, _decrement);
Define a Custom Action Type #
// Use custom actions when you want your own type.
class Power2 extends Action {
static _square(a) => a['value'] = a['value'] * a['value'];
static _squareRoot(a, r) => a['value'] = math.sqrt(a['value']);
Power2(map): super(map, _square, _squareRoot);
}
Schedule an Action #
// Call your action, and listen for the result (if you want) - its easy!
increment().then((result) => print('$result')); // prints '43'
Schedule a Transaction #
var square = new Power2(map);
transact(() {
increment();
square();
}).then((_) => print('${map["value"]}')); // prints '1936'
Undo and Redo #
// Bind undo / redo to keyboard events.
document.onKeyUp.listen((e) {
if (e.ctrlKey) {
if (e.keyCode == KeyCode.Z) undo();
else if (e.keyCode == KeyCode.Y) redo();
}
});
Run the Examples #
The code above can be found here. For more fun, try to nudge a box around a canvas - its undoable!
Undone uses the MIT license as described in the LICENSE file, and follows semantic versioning.
