Search code examples
javascriptace-editor

Ace JS Editor - Enable/Disable Undo and Redo buttons in editor


I'm having some issues trying to implement a way to have my undo and redo buttons automatically become enabled or disabled based on the UndoManager.

var editor = ace.edit(editorElement);

editor.on('change', function (e) {
  var um = editor.getSession().getUndoManager();
  $('button.undo').attr('disabled', um.hasUndo() ? false : true );
  $('button.redo').attr('disabled', um.hasRedo() ? false : true );
});

When you make the first change to the document it doesn't change the disabled state of the button. At this point the UndoManager hasn't been informed that there has been a change.

I didn't see any events that would be appropriate to test the state of the UndoManager in.


Solution

  • use input event instead of change event. undomanager is updated async after change event fires, and input event is emitted after that.