From 8c507fc471cfeae6805ad7021f124ff40c51b799 Mon Sep 17 00:00:00 2001 From: Ryan Phillips Date: Fri, 6 Jul 2018 20:27:22 -0400 Subject: [PATCH] Fix undo/redo exception when there are no stored elements - undoaction and redoaction would push undefined onto storedUndo and storedElement respectively if the other was empty when the action was triggered. This would cause an exception on redo during the redraw. - Fixes undo and redo buttons not being disabled by using find instead of children because they are not direct children of $tool. - Adds annotate-undo and annotate-redo to the respective buttons when the tool does not use bootstrap. --- djaodjin-annotate.js | 33 ++++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/djaodjin-annotate.js b/djaodjin-annotate.js index 716ca6a..bedd223 100644 --- a/djaodjin-annotate.js +++ b/djaodjin-annotate.js @@ -115,7 +115,7 @@ MIT License ''; } else { self.$tool = '
' + - ''; + ''; if (self.options.unselectTool) { self.$tool += 'NO TOOL SELECTED'; @@ -128,7 +128,7 @@ MIT License 'ARROWPEN' + - '' + '
'; } @@ -339,17 +339,24 @@ MIT License }, checkUndoRedo: function() { var self = this; - self.$tool.children('.annotate-redo').attr('disabled', self.storedUndo - .length === 0); - self.$tool.children('.annotate-undo').attr('disabled', self.storedElement - .length === 0); + self.$tool.find('.annotate-redo').attr( + 'disabled', + self.storedUndo.length === 0 + ); + self.$tool.find('.annotate-undo').attr( + 'disabled', + self.storedElement.length === 0 + ); }, undoaction: function(event) { event.preventDefault(); var self = this; - self.storedUndo.push(self.storedElement[self.storedElement.length - - 1]); - self.storedElement.pop(); + var action = self.storedElement.pop(); + + if (action) { + self.storedUndo.push(action); + } + self.checkUndoRedo(); self.clear(); self.redraw(); @@ -357,8 +364,12 @@ MIT License redoaction: function(event) { event.preventDefault(); var self = this; - self.storedElement.push(self.storedUndo[self.storedUndo.length - 1]); - self.storedUndo.pop(); + var action = self.storedUndo.pop(); + + if (action) { + self.storedElement.push(action); + } + self.checkUndoRedo(); self.clear(); self.redraw();