Skip to content

Commit 38978f9

Browse files
committed
feat(json-crdt-peritext-ui): 🎸 implement in-memory undo manager
1 parent a02f931 commit 38978f9

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
}

0 commit comments

Comments
 (0)