aboutsummaryrefslogtreecommitdiff
path: root/src/client/util/UndoManager.ts
diff options
context:
space:
mode:
authorlaurawilsonri <laura_wilson@brown.edu>2019-04-11 14:12:49 -0400
committerlaurawilsonri <laura_wilson@brown.edu>2019-04-11 14:12:49 -0400
commitc392a9322c1df269cfd823dd82d07d991fe065c0 (patch)
treefdd44c511bd179984dc3dc18b92745751c86bfc5 /src/client/util/UndoManager.ts
parent15514b0f3d685764d1bd7ebeac9cdee1f778e184 (diff)
parent50be8cb7a93110821c972c679567ddb6aae8bc6f (diff)
Merge branch 'master' of https://github.com/browngraphicslab/Dash-Web into richTextEditor
Diffstat (limited to 'src/client/util/UndoManager.ts')
-rw-r--r--src/client/util/UndoManager.ts46
1 files changed, 31 insertions, 15 deletions
diff --git a/src/client/util/UndoManager.ts b/src/client/util/UndoManager.ts
index eb13ff1ee..27aed4bac 100644
--- a/src/client/util/UndoManager.ts
+++ b/src/client/util/UndoManager.ts
@@ -1,5 +1,5 @@
import { observable, action } from "mobx";
-import 'source-map-support/register'
+import 'source-map-support/register';
import { Without } from "../../Utils";
import { string } from "prop-types";
@@ -31,11 +31,24 @@ function propertyDecorator(target: any, key: string | symbol) {
batch.end();
}
}
- })
+ });
}
- })
+ });
}
-export function undoBatch(target: any, key: string | symbol, descriptor?: TypedPropertyDescriptor<any>): any {
+
+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 () {
+ let batch = UndoManager.StartBatch("");
+ try {
+ return target.apply(undefined, arguments);
+ } finally {
+ batch.end();
+ }
+ };
+ }
if (!descriptor) {
propertyDecorator(target, key);
return;
@@ -45,11 +58,11 @@ export function undoBatch(target: any, key: string | symbol, descriptor?: TypedP
descriptor.value = function (...args: any[]) {
let batch = UndoManager.StartBatch(getBatchName(target, key));
try {
- return oldFunction.apply(this, args)
+ return oldFunction.apply(this, args);
} finally {
batch.end();
}
- }
+ };
return descriptor;
}
@@ -104,8 +117,8 @@ export namespace UndoManager {
EndBatch(cancel);
}
- end = () => { this.dispose(false); }
- cancel = () => { this.dispose(true); }
+ end = () => { this.dispose(false); };
+ cancel = () => { this.dispose(true); };
}
export function StartBatch(batchName: string): Batch {
@@ -125,12 +138,15 @@ export namespace UndoManager {
redoStack.length = 0;
currentBatch = undefined;
}
- })
+ });
export function RunInBatch(fn: () => void, batchName: string) {
let batch = StartBatch(batchName);
- fn();
- batch.end();
+ try {
+ fn();
+ } finally {
+ batch.end();
+ }
}
export const Undo = action(() => {
@@ -150,7 +166,7 @@ export namespace UndoManager {
undoing = false;
redoStack.push(commands);
- })
+ });
export const Redo = action(() => {
if (redoStack.length === 0) {
@@ -163,12 +179,12 @@ export namespace UndoManager {
}
undoing = true;
- for (let i = 0; i < commands.length; i++) {
- commands[i].redo();
+ for (const command of commands) {
+ command.redo();
}
undoing = false;
undoStack.push(commands);
- })
+ });
} \ No newline at end of file