From f4b75a7c921181faeeee04fbd57cd24fbd57523e Mon Sep 17 00:00:00 2001
From: Mohammad Amoush <mohammad_amoush@brown.edu>
Date: Thu, 20 Jun 2019 19:16:42 -0400
Subject: Undo/Redo First Version

---
 .../views/presentationview/PresentationView.tsx    | 36 ++++++++++++++++++++--
 1 file changed, 33 insertions(+), 3 deletions(-)

(limited to 'src/client/views/presentationview/PresentationView.tsx')

diff --git a/src/client/views/presentationview/PresentationView.tsx b/src/client/views/presentationview/PresentationView.tsx
index 905117cd1..03e33dad7 100644
--- a/src/client/views/presentationview/PresentationView.tsx
+++ b/src/client/views/presentationview/PresentationView.tsx
@@ -15,6 +15,7 @@ import { library } from '@fortawesome/fontawesome-svg-core';
 import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
 import { faArrowRight, faArrowLeft, faPlay, faStop, faPlus, faTimes, faMinus, faEdit } from '@fortawesome/free-solid-svg-icons';
 import { Docs } from "../../documents/Documents";
+import { undoBatch, UndoManager } from "../../util/UndoManager";
 
 library.add(faArrowLeft);
 library.add(faArrowRight);
@@ -560,11 +561,13 @@ export class PresentationView extends React.Component<PresViewProps>  {
         if (castedGroupDocs !== undefined) {
             castedGroupDocs.forEach(async (groupDoc: Doc, index: number) => {
                 let castedGrouping = await DocListCastAsync(groupDoc.grouping);
+                // UndoManager.RunInBatch(() => {
                 if (castedGrouping) {
                     castedGrouping.forEach((doc: Doc) => {
                         doc.presentId = Utils.GenerateGuid();
                     });
                 }
+                // }, "guid assignment");
             });
         }
     }
@@ -742,27 +745,54 @@ export class PresentationView extends React.Component<PresViewProps>  {
      * list. Sets up the next presentation as current.
      */
     @action
-    removePresentation = () => {
+    removePresentation = async () => {
         if (this.presentationsMapping.size !== 1) {
             let presentationList = Cast(this.props.Documents, listSpec(Doc));
+            let batch = UndoManager.StartBatch("presRemoval");
             let removedDoc = this.presentationsMapping.get(this.currentSelectedPresValue!);
+            presentationList!.splice(presentationList!.indexOf(removedDoc!), 1);
             this.presentationsKeyMapping.delete(removedDoc!);
             this.presentationsMapping.delete(this.currentSelectedPresValue!);
             let remainingPresentations = this.presentationsMapping.values();
             let nextDoc = remainingPresentations.next().value;
+
+
+            let curGuid = this.currentSelectedPresValue!;
+            let curPresStatus = this.presStatus;
+            UndoManager.AddEvent({
+                undo: action(() => {
+                    this.curPresentation = removedDoc!;
+                    this.presStatus = curPresStatus;
+                    this.presentationsMapping.set(curGuid, removedDoc!);
+                    this.presentationsKeyMapping.set(removedDoc!, curGuid);
+                    this.currentSelectedPresValue = curGuid;
+                    this.setPresentationBackUps();
+
+                }),
+                redo: action(() => {
+                    this.curPresentation = nextDoc;
+                    this.presStatus = false;
+                    this.presentationsKeyMapping.delete(removedDoc!);
+                    this.presentationsMapping.delete(curGuid);
+                    this.currentSelectedPresValue = this.presentationsKeyMapping.get(nextDoc)!.toString();
+                    this.setPresentationBackUps();
+
+                }),
+            });
+
             this.curPresentation = nextDoc;
             this.resetGroupIds();
             this.resetPresentation();
             this.currentSelectedPresValue = this.presentationsKeyMapping.get(nextDoc)!.toString();
             this.setPresentationBackUps();
-            presentationList!.splice(presentationList!.indexOf(removedDoc!), 1);
+            batch.end();
         }
     }
 
     /**
      * The function that is called to change title of presentation to what user entered.
      */
-    @action
+    @undoBatch
     changePresentationTitle = (newTitle: string) => {
         if (newTitle === "") {
             return;
-- 
cgit v1.2.3-70-g09d2

