Search code examples
javascriptace-editor

ace editor - simply re-enable command after disabled it


I want to simply re-enable a command after I disabled it... This is how I simply disabled it:

editor.commands.removeCommand("backspace");


But now I need to enable it again but I don't know how to do it...

I found something like this but it is very difficult..

Is there a way to simple re-enable it?

Please help..


Solution

  • You can keep a reference to the command to add it back later

    var command = editor.commands.byName.backspace
    editor.commands.removeCommand(command)
    editor.commands.addCommand(command)
    

    or to remove only the key

    function setCommandEnabled(editor, name, enabled) {
        var command = editor.commands.byName[name]
        if (!command.bindKeyOriginal) 
            command.bindKeyOriginal = command.bindKey
        command.bindKey = enabled ? command.bindKeyOriginal : null;
        editor.commands.addCommand(command);
        // special case for backspace and delete which will be called from
        // textarea if not handled by main commandb binding
        if (!enabled) {
            var key = command.bindKeyOriginal;
            if (key && typeof key == "object")
                key = key[editor.commands.platform];
            if (/backspace|delete/i.test(key))
                editor.commands.bindKey(key, "null")
        }
    }
    

    and then call

    setCommandEnabled(editor, "backspace", false)
    setCommandEnabled(editor, "backspace", true)