Skip to content

Commit

Permalink
Add JSDoc
Browse files Browse the repository at this point in the history
  • Loading branch information
ArthurClemens committed Mar 5, 2023
1 parent 0b58501 commit 494dd38
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 18 deletions.
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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);
Expand All @@ -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
Expand Down
65 changes: 48 additions & 17 deletions lib/undomanager.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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;

Expand All @@ -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;
},
};
};
Expand Down

0 comments on commit 494dd38

Please sign in to comment.