Add redo method.

Closes #14
This commit is contained in:
Jean-Benoist Leger 2020-04-17 17:37:19 +00:00
parent 5098263d34
commit 9936faf469
3 changed files with 39 additions and 1 deletions

View File

@ -9,6 +9,7 @@ var keybinds = {
// 'key(s)' : 'function', // 'key(s)' : 'function',
'defmod-shift-z' : 'clearWhiteboard', 'defmod-shift-z' : 'clearWhiteboard',
'defmod-z' : 'undoStep', 'defmod-z' : 'undoStep',
'defmod-shift-z' : 'redoStep',
'defmod-x' : 'setTool_recSelect', 'defmod-x' : 'setTool_recSelect',
'defmod-m' : 'setTool_mouse', 'defmod-m' : 'setTool_mouse',
'defmod-p' : 'setTool_pen', 'defmod-p' : 'setTool_pen',

View File

@ -101,6 +101,7 @@ $(document).ready(function () {
var shortcutFunctions = { var shortcutFunctions = {
clearWhiteboard: function () { whiteboard.clearWhiteboard(); }, clearWhiteboard: function () { whiteboard.clearWhiteboard(); },
undoStep: function () { whiteboard.undoWhiteboardClick(); }, undoStep: function () { whiteboard.undoWhiteboardClick(); },
redoStep: function () { whiteboard.redoWhiteboardClick(); },
setTool_mouse: function () { $(".whiteboardTool[tool=mouse]").click(); }, setTool_mouse: function () { $(".whiteboardTool[tool=mouse]").click(); },
setTool_recSelect: function () { $(".whiteboardTool[tool=recSelect]").click(); }, setTool_recSelect: function () { $(".whiteboardTool[tool=recSelect]").click(); },
setTool_pen: function () { setTool_pen: function () {

View File

@ -24,7 +24,8 @@ var whiteboard = {
svgRect: null, svgRect: null,
svgCirle: null, svgCirle: null,
drawBuffer: [], drawBuffer: [],
drawId: 0, //Used for undo function undoBuffer: [],
drawId: 0, //Used for undo/redo functions
imgDragActive: false, imgDragActive: false,
latestActiveTextBoxId: false, //The id of the latest clicked Textbox (for font and color change) latestActiveTextBoxId: false, //The id of the latest clicked Textbox (for font and color change)
pressedKeys: {}, pressedKeys: {},
@ -531,6 +532,7 @@ var whiteboard = {
_this.textContainer.empty(); _this.textContainer.empty();
_this.sendFunction({ "t": "clear" }); _this.sendFunction({ "t": "clear" });
_this.drawBuffer = []; _this.drawBuffer = [];
_this.undoBuffer = [];
_this.drawId = 0; _this.drawId = 0;
}, },
setStrokeThickness(thickness) { setStrokeThickness(thickness) {
@ -687,12 +689,39 @@ var whiteboard = {
var drawId = _this.drawBuffer[i]["drawId"]; var drawId = _this.drawBuffer[i]["drawId"];
for (var i = _this.drawBuffer.length - 1; i >= 0; i--) { for (var i = _this.drawBuffer.length - 1; i >= 0; i--) {
if (_this.drawBuffer[i]["drawId"] == drawId && _this.drawBuffer[i]["username"] == username) { if (_this.drawBuffer[i]["drawId"] == drawId && _this.drawBuffer[i]["username"] == username) {
_this.undoBuffer.push(_this.drawBuffer[i])
_this.drawBuffer.splice(i, 1); _this.drawBuffer.splice(i, 1);
} }
} }
break; break;
} }
} }
if (_this.undoBuffer.length > 1000) {
_this.undoBuffer.splice(0, _this.undoBuffer.length-1000);
}
_this.canvas.height = _this.canvas.height;
_this.imgContainer.empty();
_this.loadDataInSteps(_this.drawBuffer, false, function (stepData) {
//Nothing to do
});
},
redoWhiteboard: function (username) { //Not call this directly because you will get out of sync whit others...
var _this = this;
if (!username) {
username = _this.settings.username;
}
for (var i = _this.undoBuffer.length - 1; i >= 0; i--) {
if (_this.undoBuffer[i]["username"] == username) {
var drawId = _this.undoBuffer[i]["drawId"];
for (var i = _this.undoBuffer.length - 1; i >= 0; i--) {
if (_this.undoBuffer[i]["drawId"] == drawId && _this.undoBuffer[i]["username"] == username) {
_this.drawBuffer.push(_this.undoBuffer[i])
_this.undoBuffer.splice(i, 1);
}
}
break;
}
}
_this.canvas.height = _this.canvas.height; _this.canvas.height = _this.canvas.height;
_this.imgContainer.empty(); _this.imgContainer.empty();
_this.loadDataInSteps(_this.drawBuffer, false, function (stepData) { _this.loadDataInSteps(_this.drawBuffer, false, function (stepData) {
@ -703,6 +732,10 @@ var whiteboard = {
this.sendFunction({ "t": "undo" }); this.sendFunction({ "t": "undo" });
this.undoWhiteboard(); this.undoWhiteboard();
}, },
redoWhiteboardClick: function () {
this.sendFunction({ "t": "redo" });
this.redoWhiteboard();
},
setTool: function (tool) { setTool: function (tool) {
this.tool = tool; this.tool = tool;
if (this.tool === "text") { if (this.tool === "text") {
@ -778,6 +811,7 @@ var whiteboard = {
_this.imgContainer.empty(); _this.imgContainer.empty();
_this.textContainer.empty(); _this.textContainer.empty();
_this.drawBuffer = []; _this.drawBuffer = [];
_this.undoBuffer = [];
_this.drawId = 0; _this.drawId = 0;
} else if (tool === "cursor" && _this.settings) { } else if (tool === "cursor" && _this.settings) {
if (content["event"] === "move") { if (content["event"] === "move") {
@ -793,6 +827,8 @@ var whiteboard = {
} }
} else if (tool === "undo") { } else if (tool === "undo") {
_this.undoWhiteboard(username); _this.undoWhiteboard(username);
} else if (tool === "redo") {
_this.redoWhiteboard(username);
} }
}); });