From efe31ae6b601ac2f4b59b46eeeb4af3398fc81e5 Mon Sep 17 00:00:00 2001 From: mgfcf <m.giller@tu-braunschweig.de> Date: Mon, 6 Sep 2021 22:36:23 +0200 Subject: [PATCH] Removed duplicate code --- editor/js/manageddata.js | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/editor/js/manageddata.js b/editor/js/manageddata.js index 170ccc4..05e1869 100644 --- a/editor/js/manageddata.js +++ b/editor/js/manageddata.js @@ -1,7 +1,7 @@ export default class ManagedData { constructor(data) { this.data = data; - this.history = []; + this.history = []; // Newest state is always at 0 this.historyPosition = 0; this.storeCurrentData("Initial state"); @@ -11,27 +11,32 @@ export default class ManagedData { onRedo() {} undo() { - if (this.historyPosition + 1 >= this.history.length) { + if (this.step(1)) { + this.onUndo(); + return true; + } else { return false; } - - this.historyPosition += 1; - this.data = JSON.parse(this.history[this.historyPosition].data); - - this.onUndo(); - - return true; } redo() { - if (this.historyPosition <= 0) { + if (this.step(-1)) { + this.onRedo(); + return true; + } else { return false; } + } - this.historyPosition -= 1; - this.data = JSON.parse(this.history[this.historyPosition].data); + step(direction = 1) { + var newHistoryPosition = this.historyPosition + Math.sign(direction); - this.onRedo(); + if (newHistoryPosition >= this.history.length || newHistoryPosition < 0) { + return false; + } + + this.historyPosition = newHistoryPosition; + this.data = JSON.parse(this.history[this.historyPosition].data); return true; } -- GitLab