diff --git a/src/editor/js/structures/manageddata.ts b/src/editor/js/structures/manageddata.ts index 61d4c18e0d9cbc0b55afb727902a2708ad8676b7..c40ada5ad1a18719e4a0365901b88fcdf04cd458 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;