Skip to content
Snippets Groups Projects
Commit 46d181da authored by Maximilian Giller's avatar Maximilian Giller
Browse files

Implemented savepoint concept and interface

parent d4231240
No related branches found
No related tags found
1 merge request!2Implemented editor in the react framework
Pipeline #56581 passed
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;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment