diff --git a/index.html b/index.html index 2ec63925..68aaa770 100644 --- a/index.html +++ b/index.html @@ -157,7 +157,7 @@
- +
diff --git a/model.js b/model.js index bd43c1e6..ed83f47b 100644 --- a/model.js +++ b/model.js @@ -20,6 +20,9 @@ let table = { } ] }; +let historyBuffer = 100; +let history = []; +let historyIndex = -1; invalidated(); const params = new URLSearchParams(window.location.search); @@ -34,6 +37,47 @@ if (params.has('q')) { $('.container').removeClass('d-none'); } +$(document).keydown(function(e){ + if (e.which === 89 && e.ctrlKey ){ + redo(); + } else if (e.which === 90 && e.ctrlKey ){ + undo(); + } +}); + +function undo() { + if (historyIndex > 0) { + historyIndex -= 1; + table = JSON.parse(history[historyIndex]); + updateView(); + } +} + +function redo() { + if (historyIndex < history.length - 1) { + historyIndex += 1; + table = JSON.parse(history[historyIndex]); + updateView(); + } +} + +function invalidated() { + if (historyIndex === history.length - 1) { + historyIndex += 1; + history.push(JSON.stringify(table)); + } else { + historyIndex += 1; + history = history.slice(0, historyIndex); + history.push(JSON.stringify(table)); + } + if (history.length > historyBuffer) { + console.log('remove history'); + historyIndex -= 1; + history.splice(0, 1); + } + updateView(); +} + function updateTableType() { table.type = $('#tableType').val(); invalidated(); diff --git a/view.js b/view.js index 3756471a..da1907eb 100644 --- a/view.js +++ b/view.js @@ -7,11 +7,11 @@ function changeVersion(version) { $('#versionLabel').text(version); structure = json.root; components = json.components; - invalidated(); + updateView(); }); } -function invalidated() { +function updateView() { if (structure) { generateTable(); if (i18next.isInitialized) {