Add undo/redo history

This commit is contained in:
Misode
2019-09-13 14:50:20 +02:00
parent 6a4e9a5c38
commit 7f8d21cee8
3 changed files with 47 additions and 3 deletions
+44
View File
@@ -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();