From 46d181da6d2540acfcf51a11b8715a6c999c6705 Mon Sep 17 00:00:00 2001
From: Max <m.giller@tu-braunschweig.de>
Date: Wed, 1 Jun 2022 11:46:42 +0200
Subject: [PATCH] Implemented savepoint concept and interface

---
 src/editor/js/structures/manageddata.ts | 44 ++++++++++++++++++++++---
 1 file changed, 39 insertions(+), 5 deletions(-)

diff --git a/src/editor/js/structures/manageddata.ts b/src/editor/js/structures/manageddata.ts
index 61d4c18..c40ada5 100644
--- a/src/editor/js/structures/manageddata.ts
+++ b/src/editor/js/structures/manageddata.ts
@@ -1,16 +1,22 @@
-import {SerializableItem} from "./helper/serializableitem";
+import { SerializableItem } from "./helper/serializableitem";
 import jQuery from "jquery";
 
 const SAVE_BUTTON_ID = "div#ks-editor #toolbar-save";
 
+type SavePoint = {
+    description: string;
+    data: string;
+    id: number;
+};
+
 /**
  * Allows objects to have undo/redo functionality in their data and custom save points.
  */
 export default class ManagedData extends SerializableItem {
-    public data: any;   // The data to be stored in a history.
-    public history: any[];  // All save points of the data.
+    public data: any; // The data to be stored in a history.
+    public history: SavePoint[]; // All save points of the data.
     public historyPosition: number; // Currently selected save point in history. Latest always at index 0.
-    private savedHistoryId: number;  // Id of save point that is considered saved.
+    private savedHistoryId: number; // Id of save point that is considered saved.
     private storingEnabled: boolean; // To internally disable saving of objects on save call.
 
     /**
@@ -126,6 +132,21 @@ export default class ManagedData extends SerializableItem {
         }
     }
 
+    /**
+     * Moves current state of data to a given savepoint that is stored in the history.
+     * @param savePointId Id of desired savepoint.
+     * @returns True, if successful.
+     */
+    public goToSavePoint(savePointId: number): boolean {
+        // Iterate overhistory and find position with same savepoint id
+        for (let i = 0; i < this.history.length; i++) {
+            if (this.history[i].id === savePointId) {
+                return this.setHistoryPosition(i);
+            }
+        }
+        return false;   // Not found
+    }
+
     /**
      * Moves the history pointer to the desired position and adjusts the data object.
      * @param direction How many steps to take in the history. Positive for going back in time, negative for going forward.
@@ -142,7 +163,20 @@ export default class ManagedData extends SerializableItem {
             return false;
         }
 
-        this.historyPosition = newHistoryPosition;
+        return this.setHistoryPosition(newHistoryPosition);
+    }
+
+    /**
+     * Loads a given history index into the current data object and sets historyPosition accordingly.
+     * @param position Position (Index) of history point to load.
+     * @returns True, if successful.
+     */
+    private setHistoryPosition(position: number): boolean {
+        if (position < 0 || position >= this.history.length) {
+            return false;
+        }
+
+        this.historyPosition = position;
         this.data = JSON.parse(this.history[this.historyPosition].data);
 
         return true;
-- 
GitLab