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

View File

@@ -157,7 +157,7 @@
<textarea type="text" class="form-control code" onchange="updateField(this)" onfocus="this.select()" style="height: 0px"></textarea>
</div>
<div class="input-group mt-3" data-type="json-list">
<textarea class="form-control code" onchange="updateJsonListField(this)" rows=3></textarea>
<textarea class="form-control code" onchange="updateField(this)" rows=3></textarea>
</div>
<div class="input-group mt-3" data-type="nbt">
<div class="input-group-prepend">

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();

View File

@@ -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) {