mirror of
https://github.com/misode/misode.github.io.git
synced 2026-05-02 07:55:29 +00:00
Add undo/redo history
This commit is contained in:
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user