File tree Expand file tree Collapse file tree 1 file changed +40
-0
lines changed
src/json-crdt-peritext-ui/dom/annals Expand file tree Collapse file tree 1 file changed +40
-0
lines changed Original file line number Diff line number Diff line change
1
+ import type { UndoManager , UndoItem } from '../../types' ;
2
+ import type { UiLifeCycles } from '../types' ;
3
+
4
+ /**
5
+ * A Memory-based undo manager.
6
+ */
7
+ export class MemoryUndo implements UndoManager , UiLifeCycles {
8
+ /** Undo stack. */
9
+ public uStack : UndoItem [ ] = [ ] ;
10
+ /** Redo stack. */
11
+ public rStack : UndoItem [ ] = [ ] ;
12
+
13
+ // /** ------------------------------------------------------ {@link UndoRedo} */
14
+
15
+ public push < U , R > ( undo : UndoItem < U , R > ) : void {
16
+ this . rStack = [ ] ;
17
+ this . uStack . push ( undo as UndoItem ) ;
18
+ }
19
+
20
+ undo ( ) : void {
21
+ const undo = this . uStack . pop ( ) ;
22
+ if ( undo ) {
23
+ const redo = undo [ 1 ] ( undo [ 0 ] ) ;
24
+ this . rStack . push ( redo ) ;
25
+ }
26
+ }
27
+
28
+ redo ( ) : void {
29
+ const redo = this . rStack . pop ( ) ;
30
+ if ( redo ) {
31
+ const undo = redo [ 1 ] ( redo [ 0 ] ) ;
32
+ this . uStack . push ( undo ) ;
33
+ }
34
+ }
35
+
36
+ /** -------------------------------------------------- {@link UiLifeCycles} */
37
+
38
+ public start ( ) : void { }
39
+ public stop ( ) : void { }
40
+ }
You can’t perform that action at this time.
0 commit comments