diff --git a/index.html b/index.html index f1865d2..2d62893 100644 --- a/index.html +++ b/index.html @@ -24,8 +24,7 @@ - - + + + + +


diff --git a/script.js b/script.js index fea8bf7..9818c4b 100644 --- a/script.js +++ b/script.js @@ -217,3 +217,106 @@ document.getElementById('remove').addEventListener('click', function() { padding: 5 }); + + var current; +var list = []; +var state = []; +var index = 0; +var index2 = 0; +var action = false; +var refresh = true; + +canvas.on("object:added", function (e) { + var object = e.target; + console.log('object:modified'); + + if (action === true) { + state = [state[index2]]; + list = [list[index2]]; + + action = false; + console.log(state); + index = 1; + } + object.saveState(); + + console.log(object.originalState); + state[index] = JSON.stringify(object.originalState); + list[index] = object; + index++; + index2 = index - 1; + + refresh = true; +}); + +canvas.on("object:modified", function (e) { + var object = e.target; + console.log('object:modified'); + + if (action === true) { + state = [state[index2]]; + list = [list[index2]]; + + action = false; + console.log(state); + index = 1; + } + + object.saveState(); + + state[index] = JSON.stringify(object.originalState); + list[index] = object; + index++; + index2 = index - 1; + + console.log(state); + refresh = true; +}); + +function undo() { + + if (index <= 0) { + index = 0; + return; + } + + if (refresh === true) { + index--; + refresh = false; + } + + console.log('undo'); + + index2 = index - 1; + current = list[index2]; + current.setOptions(JSON.parse(state[index2])); + + index--; + current.setCoords(); + canvas.renderAll(); + action = true; +} + +function redo() { + + action = true; + if (index >= state.length - 1) { + return; + } + + console.log('redo'); + + index2 = index + 1; + current = list[index2]; + current.setOptions(JSON.parse(state[index2])); + + index++; + current.setCoords(); + canvas.renderAll(); +} +document.getElementById('undo').addEventListener('click', function() { + undo(); +}); +document.getElementById('redo').addEventListener('click', function() { + redo(); +}); \ No newline at end of file