Faced with this issue, I published undo.js on GitHub to solve the problem. My thoughts on this are the following:
beforeinput
or input
event, and check the inputType
property of the event. Undo actions will have a value of historyUndo
and redo actions will have a value of historyRedo
. More details here and here.input
events will be triggered. The only way to do so is to force-enable undo/redo by using document.execCommand
. It is the only command to my knowledge that make changes to the undo stack.My library works with contenteditable elements, textarea and inputs, and uses custom events. An example would be the following:
var observer = undo.observe(document.body, {
allowUntrusted: false,
captureAll: true,
preventDefault:false
});
myelement.addEventListener("beforeundo", function(e){
})
myelement.addEventListener("undo", function(e){
console.log(e.detail.shortcut) // whether the undo event was caused by a shortcut (CTRL+Z)
})
myelement.addEventListener("beforeredo", function(e){
})
myelement.addEventListener("redo", function(e){
})