diff --git a/README.md b/README.md index f367472..c729916 100644 --- a/README.md +++ b/README.md @@ -15,6 +15,7 @@ Simple undo manager to provide undo and redo actions in JavaScript applications. - [hasRedo](#hasredo) - [setCallback](#setcallback) - [getIndex](#getindex) + - [getCommands](#getcommands) - [Use with CommonJS](#use-with-commonjs) - [Use with RequireJS](#use-with-requirejs) @@ -142,7 +143,7 @@ const hasRedo = undoManager.hasRedo(); ### setCallback -Get notified on changes. +Get notified on changes. Pass a function to be called on undo and redo actions. ```js undoManager.setCallback(myCallback); @@ -156,6 +157,14 @@ Returns the index of the actions list. const index = undoManager.getIndex(); ``` +### getCommands + +Returns the list of queued commands. + +```js +const commands = undoManager.getCommands(); +``` + ## Use with CommonJS ```bash diff --git a/lib/undomanager.js b/lib/undomanager.js index 79237d5..77f0c7d 100644 --- a/lib/undomanager.js +++ b/lib/undomanager.js @@ -27,6 +27,13 @@ https://github.com/ArthurClemens/JavaScript-Undo-Manager // functions execute; + /** + * Executes a single command. + * @property {object} command - Command + * @property {function} command.undo - Undo function + * @property {function} command.redo - Redo function + * @property {string} action - "undo" or "redo" + */ execute = function (command, action) { if (!command || typeof command[action] !== 'function') { return this; @@ -40,9 +47,12 @@ https://github.com/ArthurClemens/JavaScript-Undo-Manager }; return { - /* - Add a command to the queue. - */ + /** + * Adds a command to the queue. + * @property {object} command - Command + * @property {function} command.undo - Undo function + * @property {function} command.redo - Redo function + */ add: function (command) { if (isExecuting) { return this; @@ -66,16 +76,17 @@ https://github.com/ArthurClemens/JavaScript-Undo-Manager return this; }, - /* - Pass a function to be called on undo and redo actions. - */ + /** + * Pass a function to be called on undo and redo actions. + * @property {function} callbackFunc - Callback function + */ setCallback: function (callbackFunc) { callback = callbackFunc; }, - /* - Perform undo: call the undo function at the current index and decrease the index by 1. - */ + /** + * Performs undo: call the undo function at the current index and decrease the index by 1. + */ undo: function () { let command = commands[index]; if (!command) { @@ -89,9 +100,9 @@ https://github.com/ArthurClemens/JavaScript-Undo-Manager return this; }, - /* - Perform redo: call the redo function at the next index and increase the index by 1. - */ + /** + * Performs redo: call the redo function at the next index and increase the index by 1. + */ redo: function () { let command = commands[index + 1]; if (!command) { @@ -105,9 +116,9 @@ https://github.com/ArthurClemens/JavaScript-Undo-Manager return this; }, - /* - Clear the memory, losing all stored states. Reset the index. - */ + /** + * Clears the memory, losing all stored states. Resets the index. + */ clear: function () { let prev_size = commands.length; @@ -119,24 +130,44 @@ https://github.com/ArthurClemens/JavaScript-Undo-Manager } }, + /** + * Tests if any undo actions exist. + * @returns {boolean} + */ hasUndo: function () { return index !== -1; }, + /** + * Tests if any redo actions exist. + * @returns {boolean} + */ hasRedo: function () { return index < commands.length - 1; }, + /** + * Returns the list of queued commands. + * @returns {array} + */ getCommands: function () { return commands; }, + /** + * Returns the index of the actions list. + * @returns {number} + */ getIndex: function () { return index; }, - setLimit: function (l) { - limit = l; + /** + * Sets the maximum number of undo steps. Default: 0 (unlimited). + * @property {number} max - Maximum number of undo steps + */ + setLimit: function (max) { + limit = max; }, }; };