Skip to content
This repository was archived by the owner on Sep 8, 2025. It is now read-only.

Commit 494dd38

Browse files
committed
Add JSDoc
1 parent 0b58501 commit 494dd38

File tree

2 files changed

+58
-18
lines changed

2 files changed

+58
-18
lines changed

README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Simple undo manager to provide undo and redo actions in JavaScript applications.
1515
- [hasRedo](#hasredo)
1616
- [setCallback](#setcallback)
1717
- [getIndex](#getindex)
18+
- [getCommands](#getcommands)
1819
- [Use with CommonJS](#use-with-commonjs)
1920
- [Use with RequireJS](#use-with-requirejs)
2021

@@ -142,7 +143,7 @@ const hasRedo = undoManager.hasRedo();
142143

143144
### setCallback
144145

145-
Get notified on changes.
146+
Get notified on changes. Pass a function to be called on undo and redo actions.
146147

147148
```js
148149
undoManager.setCallback(myCallback);
@@ -156,6 +157,14 @@ Returns the index of the actions list.
156157
const index = undoManager.getIndex();
157158
```
158159

160+
### getCommands
161+
162+
Returns the list of queued commands.
163+
164+
```js
165+
const commands = undoManager.getCommands();
166+
```
167+
159168
## Use with CommonJS
160169

161170
```bash

lib/undomanager.js

Lines changed: 48 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ https://github.com/ArthurClemens/JavaScript-Undo-Manager
2727
// functions
2828
execute;
2929

30+
/**
31+
* Executes a single command.
32+
* @property {object} command - Command
33+
* @property {function} command.undo - Undo function
34+
* @property {function} command.redo - Redo function
35+
* @property {string} action - "undo" or "redo"
36+
*/
3037
execute = function (command, action) {
3138
if (!command || typeof command[action] !== 'function') {
3239
return this;
@@ -40,9 +47,12 @@ https://github.com/ArthurClemens/JavaScript-Undo-Manager
4047
};
4148

4249
return {
43-
/*
44-
Add a command to the queue.
45-
*/
50+
/**
51+
* Adds a command to the queue.
52+
* @property {object} command - Command
53+
* @property {function} command.undo - Undo function
54+
* @property {function} command.redo - Redo function
55+
*/
4656
add: function (command) {
4757
if (isExecuting) {
4858
return this;
@@ -66,16 +76,17 @@ https://github.com/ArthurClemens/JavaScript-Undo-Manager
6676
return this;
6777
},
6878

69-
/*
70-
Pass a function to be called on undo and redo actions.
71-
*/
79+
/**
80+
* Pass a function to be called on undo and redo actions.
81+
* @property {function} callbackFunc - Callback function
82+
*/
7283
setCallback: function (callbackFunc) {
7384
callback = callbackFunc;
7485
},
7586

76-
/*
77-
Perform undo: call the undo function at the current index and decrease the index by 1.
78-
*/
87+
/**
88+
* Performs undo: call the undo function at the current index and decrease the index by 1.
89+
*/
7990
undo: function () {
8091
let command = commands[index];
8192
if (!command) {
@@ -89,9 +100,9 @@ https://github.com/ArthurClemens/JavaScript-Undo-Manager
89100
return this;
90101
},
91102

92-
/*
93-
Perform redo: call the redo function at the next index and increase the index by 1.
94-
*/
103+
/**
104+
* Performs redo: call the redo function at the next index and increase the index by 1.
105+
*/
95106
redo: function () {
96107
let command = commands[index + 1];
97108
if (!command) {
@@ -105,9 +116,9 @@ https://github.com/ArthurClemens/JavaScript-Undo-Manager
105116
return this;
106117
},
107118

108-
/*
109-
Clear the memory, losing all stored states. Reset the index.
110-
*/
119+
/**
120+
* Clears the memory, losing all stored states. Resets the index.
121+
*/
111122
clear: function () {
112123
let prev_size = commands.length;
113124

@@ -119,24 +130,44 @@ https://github.com/ArthurClemens/JavaScript-Undo-Manager
119130
}
120131
},
121132

133+
/**
134+
* Tests if any undo actions exist.
135+
* @returns {boolean}
136+
*/
122137
hasUndo: function () {
123138
return index !== -1;
124139
},
125140

141+
/**
142+
* Tests if any redo actions exist.
143+
* @returns {boolean}
144+
*/
126145
hasRedo: function () {
127146
return index < commands.length - 1;
128147
},
129148

149+
/**
150+
* Returns the list of queued commands.
151+
* @returns {array}
152+
*/
130153
getCommands: function () {
131154
return commands;
132155
},
133156

157+
/**
158+
* Returns the index of the actions list.
159+
* @returns {number}
160+
*/
134161
getIndex: function () {
135162
return index;
136163
},
137164

138-
setLimit: function (l) {
139-
limit = l;
165+
/**
166+
* Sets the maximum number of undo steps. Default: 0 (unlimited).
167+
* @property {number} max - Maximum number of undo steps
168+
*/
169+
setLimit: function (max) {
170+
limit = max;
140171
},
141172
};
142173
};

0 commit comments

Comments
 (0)