1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
|
import { observable, action, runInAction } from "mobx";
import { Without } from "../../Utils";
function getBatchName(target: any, key: string | symbol): string {
const keyName = key.toString();
if (target && target.constructor && target.constructor.name) {
return `${target.constructor.name}.${keyName}`;
}
return keyName;
}
function propertyDecorator(target: any, key: string | symbol) {
Object.defineProperty(target, key, {
configurable: true,
enumerable: false,
get: function () {
return 5;
},
set: function (value: any) {
Object.defineProperty(this, key, {
enumerable: false,
writable: true,
configurable: true,
value: function (...args: any[]) {
const batch = UndoManager.StartBatch(getBatchName(target, key));
try {
return value.apply(this, args);
} finally {
batch.end();
}
}
});
}
});
}
export function undoBatch(target: any, key: string | symbol, descriptor?: TypedPropertyDescriptor<any>): any;
export function undoBatch(fn: (...args: any[]) => any): (...args: any[]) => any;
export function undoBatch(target: any, key?: string | symbol, descriptor?: TypedPropertyDescriptor<any>): any {
if (!key) {
return function () {
const batch = UndoManager.StartBatch("");
try {
return target.apply(undefined, arguments);
} finally {
batch.end();
}
};
}
if (!descriptor) {
propertyDecorator(target, key);
return;
}
const oldFunction = descriptor.value;
descriptor.value = function (...args: any[]) {
const batch = UndoManager.StartBatch(getBatchName(target, key));
try {
return oldFunction.apply(this, args);
} finally {
batch.end();
}
};
return descriptor;
}
export namespace UndoManager {
export interface UndoEvent {
undo: () => void;
redo: () => void;
prop: string;
}
type UndoBatch = UndoEvent[];
export let undoStack: UndoBatch[] = observable([]);
export let redoStack: UndoBatch[] = observable([]);
let currentBatch: UndoBatch | undefined;
export let batchCounter = 0;
let undoing = false;
let tempEvents: UndoEvent[] | undefined = undefined;
export function AddEvent(event: UndoEvent): void {
if (currentBatch && batchCounter && !undoing) {
currentBatch.push(event);
tempEvents?.push(event);
}
}
export function CanUndo(): boolean {
return undoStack.length > 0;
}
export function CanRedo(): boolean {
return redoStack.length > 0;
}
export function PrintBatches(): void {
console.log("Open Undo Batches:");
GetOpenBatches().forEach(batch => console.log(batch.batchName));
}
const openBatches: Batch[] = [];
export function GetOpenBatches(): Without<Batch, 'end'>[] {
return openBatches;
}
export function FilterBatches(fieldTypes: string[]) {
const fieldCounts: { [key: string]: number } = {};
const lastStack = UndoManager.undoStack.slice(-1)[0];//.lastElement();
if (lastStack) {
lastStack.forEach(ev => fieldTypes.includes(ev.prop) && (fieldCounts[ev.prop] = (fieldCounts[ev.prop] || 0) + 1));
const fieldCount2: { [key: string]: number } = {};
runInAction(() =>
UndoManager.undoStack[UndoManager.undoStack.length - 1] = lastStack.filter(ev => {
if (fieldTypes.includes(ev.prop)) {
fieldCount2[ev.prop] = (fieldCount2[ev.prop] || 0) + 1;
if (fieldCount2[ev.prop] === 1 || fieldCount2[ev.prop] === fieldCounts[ev.prop]) return true;
return false;
}
return true;
}));
}
}
export function TraceOpenBatches() {
console.log(`Open batches:\n\t${openBatches.map(batch => batch.batchName).join("\n\t")}\n`);
}
export class Batch {
private disposed: boolean = false;
constructor(readonly batchName: string) {
openBatches.push(this);
}
private dispose = (cancel: boolean) => {
if (this.disposed) {
throw new Error("Cannot dispose an already disposed batch");
}
this.disposed = true;
openBatches.splice(openBatches.indexOf(this));
EndBatch(cancel);
}
end = () => { this.dispose(false); };
cancel = () => { this.dispose(true); };
}
export function StartBatch(batchName: string): Batch {
// console.log("Start " + batchCounter + " " + batchName);
batchCounter++;
if (batchCounter > 0 && currentBatch === undefined) {
currentBatch = [];
}
return new Batch(batchName);
}
const EndBatch = action((cancel: boolean = false) => {
batchCounter--;
// console.log("End " + batchCounter);
if (batchCounter === 0 && currentBatch?.length) {
// console.log("------ended----")
if (!cancel) {
undoStack.push(currentBatch);
}
redoStack.length = 0;
currentBatch = undefined;
}
});
export function RunInTempBatch<T>(fn: () => T) {
tempEvents = [];
try {
const success = runInAction(fn);
if (!success) UndoManager.UndoTempBatch();
return success;
} finally {
tempEvents = undefined;
}
}
//TODO Make this return the return value
export function RunInBatch<T>(fn: () => T, batchName: string) {
const batch = StartBatch(batchName);
try {
return runInAction(fn);
} finally {
batch.end();
}
}
export const UndoTempBatch = action(() => {
if (tempEvents) {
undoing = true;
for (let i = tempEvents.length - 1; i >= 0; i--) {
tempEvents[i].undo();
}
undoing = false;
}
tempEvents = undefined;
});
export const Undo = action(() => {
if (undoStack.length === 0) {
return;
}
const commands = undoStack.pop();
if (!commands) {
return;
}
undoing = true;
for (let i = commands.length - 1; i >= 0; i--) {
commands[i].undo();
}
undoing = false;
redoStack.push(commands);
});
export const Redo = action(() => {
if (redoStack.length === 0) {
return;
}
const commands = redoStack.pop();
if (!commands) {
return;
}
undoing = true;
for (const command of commands) {
command.redo();
}
undoing = false;
undoStack.push(commands);
});
}
|