Commit 70a3bf3
feat: group api for
Adds a new api to `UndoManager` to support grouping together changes into *ideally* one undo item.
`UndoManager::group_start()`: will start a new group of changes, all subsequent changes will be merged into a new item on the undo stack. If we receive remote changes, we determine wether or not they are conflicting. If the remote changes are conflicting we split the undo item and close the group. If there are no conflict in changed container ids we continue the group merge.
`UndoManager::group_end()`: ends the current group, calling `UndoManager::undo()` after this will undo all changes that occurred during the group.
- Introduces new semantics for handling remote imports. If a remote import happens we compare `ContainerID`'s from the remote import and the last item on the undo stack, if there is no overlap we deem it safe to continue merging into the last undo item regardless of the remote import. If there are conflicts it behaves as it did previously.
```
┌────────────────┐
│ group_start() │
└────────────────┘
│
UndoStack │
┌─────────────────┐ make local change
│ ┌─────────────┐ │ push new item ┌────────────────┐
│ │ undo_1 │◀│─ ─ ─ to undo stack ─ ─ ─ ─│ commit() │
│ └─────────────┘ │ └────────────────┘ ┌───────────┐
│ ▲ │ │ │group_end()│◀ ─ ─ ─ ─ ┐
│ │ │ make local change └───────────┘
│ │ │ merge with ┌────────────────┐ │
│ └ ─ ─ ─ ─│─ ─ ─ previous item ─ ─ ─ ─│ commit() │
│ │ │ in same group └────────────────┘ Λ │
│ │ │ ╱ ╲
│ │ │ │ ─▶▕ Y ▏─ ┘
│ │ import remote change ┌ ─ ─ ─ ─ ─ ─ ─ ┐ │ ╲ ╱
│ │ │ ┌────────────────┐ does import V
│ │ │ import() │─ ─ ─▶ │ affect last │─ ┤
│ │ │ └────────────────┘ undo item? Λ
│ │ Λ │ └ ─ ─ ─ ─ ─ ─ ─ ┘ │ ╱ ╲
│ │ │ ╱ ╲ │ ─▶▕ N ▏
│ ─ ─ ─ ─ ┼ ─▕ Y ▏◀─ ─ ─ ─ ─ ▼ ╲ ╱
│ │ ╲ ╱ │ make local change V
│ │ V ┌ ─ ─ ─ ─ ─ ─ ─ ┐ ┌────────────────┐
│ │ is group_active ─│ commit() │
│ │ └ ─ ─ ─ ─ ─ ─ ─ ┘ └────────────────┘
│ ┌─────────────┐ │ │
│ │ undo_2 │◀┼───┐ Λ
│ └─────────────┘ │ │ ╱ ╲ │
│ │ └─────▕ N ▏◀ ─
│ push new item ╲ ╱
│ │ V
└─────────────────┘
```
Example usage from tests:
```typescript
const doc = new LoroDoc()
const undoManager = new UndoManager(doc, {});
const text = doc.getText("text");
undoManager.groupStart();
text.update("hello", undefined);
doc.commit();
text.update("world", undefined);
doc.commit();
undoManager.groupEnd();
undoManager.undo();
expect(text.toString()).toBe("");
```
---------
Co-authored-by: Zixuan Chen <remch183@outlook.com>UndoManager (#720)1 parent c2263d2 commit 70a3bf3
File tree
7 files changed
+430
-24
lines changed- .changeset
- crates
- loro-common/src
- loro-internal
- src
- tests
- loro-wasm
- src
- tests
7 files changed
+430
-24
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
72 | 72 | | |
73 | 73 | | |
74 | 74 | | |
75 | | - | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
76 | 78 | | |
77 | 79 | | |
78 | 80 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
| 1 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
5 | | - | |
| 5 | + | |
6 | 6 | | |
7 | | - | |
| 7 | + | |
8 | 8 | | |
9 | 9 | | |
10 | 10 | | |
| |||
199 | 199 | | |
200 | 200 | | |
201 | 201 | | |
| 202 | + | |
202 | 203 | | |
203 | 204 | | |
204 | 205 | | |
| |||
212 | 213 | | |
213 | 214 | | |
214 | 215 | | |
| 216 | + | |
215 | 217 | | |
216 | 218 | | |
217 | 219 | | |
218 | 220 | | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
219 | 236 | | |
220 | 237 | | |
221 | 238 | | |
| |||
308 | 325 | | |
309 | 326 | | |
310 | 327 | | |
311 | | - | |
| 328 | + | |
312 | 329 | | |
313 | 330 | | |
314 | | - | |
| 331 | + | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
315 | 338 | | |
316 | 339 | | |
317 | | - | |
318 | | - | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
319 | 357 | | |
320 | 358 | | |
321 | 359 | | |
322 | 360 | | |
323 | 361 | | |
324 | | - | |
325 | 362 | | |
326 | | - | |
327 | | - | |
328 | | - | |
329 | | - | |
330 | | - | |
331 | | - | |
332 | | - | |
333 | | - | |
| 363 | + | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
| 369 | + | |
| 370 | + | |
| 371 | + | |
| 372 | + | |
334 | 373 | | |
335 | 374 | | |
336 | | - | |
337 | | - | |
338 | | - | |
339 | 375 | | |
| 376 | + | |
| 377 | + | |
| 378 | + | |
| 379 | + | |
340 | 380 | | |
341 | 381 | | |
342 | 382 | | |
| |||
415 | 455 | | |
416 | 456 | | |
417 | 457 | | |
| 458 | + | |
418 | 459 | | |
419 | 460 | | |
420 | 461 | | |
| 462 | + | |
| 463 | + | |
| 464 | + | |
| 465 | + | |
| 466 | + | |
| 467 | + | |
| 468 | + | |
| 469 | + | |
| 470 | + | |
| 471 | + | |
421 | 472 | | |
| 473 | + | |
| 474 | + | |
422 | 475 | | |
423 | 476 | | |
424 | 477 | | |
| |||
428 | 481 | | |
429 | 482 | | |
430 | 483 | | |
431 | | - | |
| 484 | + | |
| 485 | + | |
| 486 | + | |
| 487 | + | |
| 488 | + | |
| 489 | + | |
| 490 | + | |
| 491 | + | |
432 | 492 | | |
433 | 493 | | |
434 | 494 | | |
| |||
437 | 497 | | |
438 | 498 | | |
439 | 499 | | |
440 | | - | |
441 | | - | |
| 500 | + | |
| 501 | + | |
| 502 | + | |
| 503 | + | |
| 504 | + | |
| 505 | + | |
| 506 | + | |
| 507 | + | |
| 508 | + | |
| 509 | + | |
| 510 | + | |
| 511 | + | |
| 512 | + | |
| 513 | + | |
| 514 | + | |
| 515 | + | |
| 516 | + | |
| 517 | + | |
| 518 | + | |
442 | 519 | | |
443 | 520 | | |
444 | 521 | | |
| |||
526 | 603 | | |
527 | 604 | | |
528 | 605 | | |
| 606 | + | |
| 607 | + | |
529 | 608 | | |
530 | 609 | | |
| 610 | + | |
| 611 | + | |
| 612 | + | |
| 613 | + | |
| 614 | + | |
| 615 | + | |
531 | 616 | | |
532 | 617 | | |
533 | 618 | | |
| |||
556 | 641 | | |
557 | 642 | | |
558 | 643 | | |
| 644 | + | |
| 645 | + | |
| 646 | + | |
| 647 | + | |
| 648 | + | |
| 649 | + | |
| 650 | + | |
| 651 | + | |
| 652 | + | |
| 653 | + | |
| 654 | + | |
| 655 | + | |
| 656 | + | |
| 657 | + | |
| 658 | + | |
| 659 | + | |
559 | 660 | | |
560 | 661 | | |
561 | 662 | | |
| |||
726 | 827 | | |
727 | 828 | | |
728 | 829 | | |
| 830 | + | |
729 | 831 | | |
730 | 832 | | |
731 | 833 | | |
| |||
0 commit comments