@jucie.io/state 1.0.24 → 1.0.25
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/plugins/history/README.md +30 -22
package/package.json
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
# @jucio.io/state/history
|
|
2
2
|
|
|
3
|
-
History management plugin for @jucio.io/state that provides undo/redo functionality with markers,
|
|
3
|
+
History management plugin for @jucio.io/state that provides undo/redo functionality with markers, grouping, and commit listeners.
|
|
4
4
|
|
|
5
5
|
## Features
|
|
6
6
|
|
|
7
7
|
- ⏪ **Undo/Redo**: Full undo and redo support with automatic change tracking
|
|
8
8
|
- 🏷️ **Markers**: Add descriptive markers to create logical undo/redo boundaries
|
|
9
|
-
- 📦 **
|
|
9
|
+
- 📦 **Grouping**: Group multiple changes into a single undo/redo step (can span multiple calls; commit when done)
|
|
10
10
|
- 🔔 **Commit Listeners**: React to history commits
|
|
11
11
|
- 🎯 **Smart Change Consolidation**: Automatically merges changes to the same path
|
|
12
12
|
- 📏 **Configurable History Size**: Limit the number of stored changes
|
|
@@ -106,34 +106,42 @@ if (state.history.canRedo()) {
|
|
|
106
106
|
|
|
107
107
|
**Returns:** `boolean`
|
|
108
108
|
|
|
109
|
-
###
|
|
109
|
+
### Grouping
|
|
110
110
|
|
|
111
|
-
#### `
|
|
111
|
+
#### `group(callback?)`
|
|
112
112
|
|
|
113
|
-
Start
|
|
113
|
+
Start grouping changes into a single undo/redo step. Grouping can span multiple calls; call `commit()` (or the returned function) when done. Pass a function to run in grouping mode and commit when it returns (quick single-block use).
|
|
114
114
|
|
|
115
115
|
```javascript
|
|
116
|
-
|
|
117
|
-
|
|
116
|
+
// Span multiple calls
|
|
117
|
+
state.history.group();
|
|
118
118
|
state.set(['user', 'name'], 'Alice');
|
|
119
119
|
state.set(['user', 'age'], 30);
|
|
120
|
-
state.
|
|
120
|
+
state.history.commit(); // All changes are now a single undo/redo step
|
|
121
121
|
|
|
122
|
-
|
|
122
|
+
// Or use the returned function
|
|
123
|
+
const endGroup = state.history.group();
|
|
124
|
+
state.set(['user', 'email'], 'alice@example.com');
|
|
125
|
+
endGroup();
|
|
123
126
|
|
|
124
|
-
|
|
127
|
+
// Quick single-block: pass a callback
|
|
128
|
+
state.history.group(() => {
|
|
129
|
+
state.set(['user', 'name'], 'Alice');
|
|
130
|
+
state.set(['user', 'age'], 30);
|
|
131
|
+
});
|
|
132
|
+
state.history.undo(); // Undoes both changes at once
|
|
125
133
|
```
|
|
126
134
|
|
|
127
|
-
**Returns:** `
|
|
135
|
+
**Returns:** `HistoryManager` (for chaining) if a callback was provided; otherwise `Function` to call when done
|
|
128
136
|
|
|
129
|
-
#### `commit()`
|
|
137
|
+
#### `commit(description?)`
|
|
130
138
|
|
|
131
|
-
Manually commit pending changes and end the current
|
|
139
|
+
Manually commit pending changes and end the current group.
|
|
132
140
|
|
|
133
141
|
```javascript
|
|
134
|
-
state.history.
|
|
142
|
+
state.history.group();
|
|
135
143
|
state.set(['data', 'value'], 1);
|
|
136
|
-
state.history.commit(); //
|
|
144
|
+
state.history.commit('Initial value'); // Commits and ends grouping
|
|
137
145
|
```
|
|
138
146
|
|
|
139
147
|
**Returns:** `HistoryManager` instance (for chaining)
|
|
@@ -219,11 +227,11 @@ state.install(HistoryManager.configure({
|
|
|
219
227
|
|
|
220
228
|
## Advanced Usage
|
|
221
229
|
|
|
222
|
-
###
|
|
230
|
+
### Grouping with Markers
|
|
223
231
|
|
|
224
232
|
```javascript
|
|
225
|
-
// Start a complex operation
|
|
226
|
-
state.history.
|
|
233
|
+
// Start a complex operation (grouping can span multiple calls)
|
|
234
|
+
state.history.group();
|
|
227
235
|
state.history.addMarker('Start user registration');
|
|
228
236
|
|
|
229
237
|
state.set(['user', 'name'], 'Alice');
|
|
@@ -270,10 +278,10 @@ state.plugins.history.reset();
|
|
|
270
278
|
```javascript
|
|
271
279
|
// Track form edits
|
|
272
280
|
function handleFieldChange(field, value) {
|
|
273
|
-
|
|
281
|
+
state.history.group();
|
|
274
282
|
state.set(['form', field], value);
|
|
275
283
|
state.history.addMarker(`Update ${field}`);
|
|
276
|
-
|
|
284
|
+
state.history.commit();
|
|
277
285
|
}
|
|
278
286
|
|
|
279
287
|
// Implement undo/redo buttons
|
|
@@ -290,7 +298,7 @@ function handleUndo() {
|
|
|
290
298
|
|
|
291
299
|
```javascript
|
|
292
300
|
function performComplexOperation() {
|
|
293
|
-
|
|
301
|
+
state.history.group();
|
|
294
302
|
|
|
295
303
|
// Step 1: Update user
|
|
296
304
|
state.set(['user', 'status'], 'processing');
|
|
@@ -301,7 +309,7 @@ function performComplexOperation() {
|
|
|
301
309
|
// Step 3: Update timestamp
|
|
302
310
|
state.set(['lastUpdate'], Date.now());
|
|
303
311
|
|
|
304
|
-
|
|
312
|
+
state.history.commit();
|
|
305
313
|
state.history.addMarker('Complex operation completed');
|
|
306
314
|
}
|
|
307
315
|
|