MDL-60574 atto_undo: fixed redo erasing content

When you'd hit ctrl-y with nothing on the redo stack the entire
content of Atto would get deleted with no way of getting it back.
This commit is contained in:
Tim Schroeder 2020-07-16 17:22:30 +02:00
parent 07238ca511
commit 95fc9d9399
No known key found for this signature in database
GPG Key ID: 44FD4F625FBA67CD
4 changed files with 19 additions and 1 deletions

View File

@ -257,6 +257,12 @@ Y.namespace('M.atto_undo').Button = Y.Base.create('button', Y.M.editor_atto.Edit
*/
_redoHandler: function(e) {
e.preventDefault();
// Don't do anything if redo stack is empty.
if (this._redoStack.length === 0) {
return;
}
var html = this._getHTML(),
redo = this._getRedo();

View File

@ -1 +1 @@
YUI.add("moodle-atto_undo-button",function(t,e){t.namespace("M.atto_undo").Button=t.Base.create("button",t.M.editor_atto.EditorPlugin,[],{_maxUndos:40,_undoStack:null,_redoStack:null,initializer:function(){this._undoStack=[],this._redoStack=[],this.addButton({title:"undo",icon:"e/undo",callback:this._undoHandler,buttonName:"undo",keys:90}),this.addButton({title:"redo",icon:"e/redo",callback:this._redoHandler,buttonName:"redo",keys:89}),this.get("host").on("pluginsloaded",function(){this._addToUndo(this._getHTML()),this.get("host").on("atto:selectionchanged",this._changeListener,this)},this),this._updateButtonsStates()},_addToRedo:function(t){this._redoStack.push(t)},_addToUndo:function(t,e){for(void 0===e&&(e=!1),this._undoStack[this._undoStack.length-1]!==t&&(this._undoStack.push(t),e&&(this._redoStack=[]));this._undoStack.length>this._maxUndos;)this._undoStack.shift()},_getHTML:function(){return this.get("host").getCleanHTML()},_getRedo:function(){return this._redoStack.pop()},_getUndo:function(t){if(1===this._undoStack.length)return this._undoStack[0];var e=this._undoStack.pop();return e===t&&(e=this._undoStack.pop()),0===this._undoStack.length&&this._addToUndo(e),e},_restoreValue:function(t){this.editor.setHTML(t),this._addToUndo(t)},_updateButtonsStates:function(){1<this._undoStack.length?this.enableButtons("undo"):this.disableButtons("undo"),0<this._redoStack.length?this.enableButtons("redo"):this.disableButtons("redo")},_undoHandler:function(t){t.preventDefault();var e=this._getHTML(),o=this._getUndo(e);e!==o&&(this._restoreValue(o),this._addToRedo(e)),this._updateButtonsStates()},_redoHandler:function(t){t.preventDefault();var e=this._getHTML(),o=this._getRedo();e!==o&&this._restoreValue(o),this._updateButtonsStates()},_changeListener:function(t){t.event&&-1!==t.event.type.indexOf("key")&&39!==t.event.keyCode&&37!==t.event.keyCode&&40!==t.event.keyCode&&38!==t.event.keyCode||(this._addToUndo(this._getHTML(),!0),this._updateButtonsStates())}})},"@VERSION@",{requires:["moodle-editor_atto-plugin"]});
YUI.add("moodle-atto_undo-button",function(t,e){t.namespace("M.atto_undo").Button=t.Base.create("button",t.M.editor_atto.EditorPlugin,[],{_maxUndos:40,_undoStack:null,_redoStack:null,initializer:function(){this._undoStack=[],this._redoStack=[],this.addButton({title:"undo",icon:"e/undo",callback:this._undoHandler,buttonName:"undo",keys:90}),this.addButton({title:"redo",icon:"e/redo",callback:this._redoHandler,buttonName:"redo",keys:89}),this.get("host").on("pluginsloaded",function(){this._addToUndo(this._getHTML()),this.get("host").on("atto:selectionchanged",this._changeListener,this)},this),this._updateButtonsStates()},_addToRedo:function(t){this._redoStack.push(t)},_addToUndo:function(t,e){for(void 0===e&&(e=!1),this._undoStack[this._undoStack.length-1]!==t&&(this._undoStack.push(t),e&&(this._redoStack=[]));this._undoStack.length>this._maxUndos;)this._undoStack.shift()},_getHTML:function(){return this.get("host").getCleanHTML()},_getRedo:function(){return this._redoStack.pop()},_getUndo:function(t){if(1===this._undoStack.length)return this._undoStack[0];var e=this._undoStack.pop();return e===t&&(e=this._undoStack.pop()),0===this._undoStack.length&&this._addToUndo(e),e},_restoreValue:function(t){this.editor.setHTML(t),this._addToUndo(t)},_updateButtonsStates:function(){1<this._undoStack.length?this.enableButtons("undo"):this.disableButtons("undo"),0<this._redoStack.length?this.enableButtons("redo"):this.disableButtons("redo")},_undoHandler:function(t){t.preventDefault();var e=this._getHTML(),o=this._getUndo(e);e!==o&&(this._restoreValue(o),this._addToRedo(e)),this._updateButtonsStates()},_redoHandler:function(t){if(t.preventDefault(),0!==this._redoStack.length){var e=this._getHTML(),o=this._getRedo();e!==o&&this._restoreValue(o),this._updateButtonsStates()}},_changeListener:function(t){t.event&&-1!==t.event.type.indexOf("key")&&39!==t.event.keyCode&&37!==t.event.keyCode&&40!==t.event.keyCode&&38!==t.event.keyCode||(this._addToUndo(this._getHTML(),!0),this._updateButtonsStates())}})},"@VERSION@",{requires:["moodle-editor_atto-plugin"]});

View File

@ -257,6 +257,12 @@ Y.namespace('M.atto_undo').Button = Y.Base.create('button', Y.M.editor_atto.Edit
*/
_redoHandler: function(e) {
e.preventDefault();
// Don't do anything if redo stack is empty.
if (this._redoStack.length === 0) {
return;
}
var html = this._getHTML(),
redo = this._getRedo();

View File

@ -255,6 +255,12 @@ Y.namespace('M.atto_undo').Button = Y.Base.create('button', Y.M.editor_atto.Edit
*/
_redoHandler: function(e) {
e.preventDefault();
// Don't do anything if redo stack is empty.
if (this._redoStack.length === 0) {
return;
}
var html = this._getHTML(),
redo = this._getRedo();