Ace Editor, How To Remove All Keybindings But One?
I recently had a textarea on my jsp page For it, I had my own shortcuts on the keyboard Then, I implemented the ace editor, and all my old keybindings didn't work then Then i searc
Solution 1:
delete editor.keyBinding;
is not a good solution, it will produce error messages in console
Uncaught TypeError: Cannot read property 'ctrl-d' of undefined at CommandManager.handleKeyboard (ace.js:10830) at KeyBinding.$callKeyboardHandlers (ace.js:4081) at KeyBinding.onCommandKey (ace.js:4113) at Editor.onCommandKey (ace.js:12424) at normalizeCommandKeys (ace.js:1648) at HTMLTextAreaElement. (ace.js:1667)
Option 1: delete all bindings and redefine the one we need.
//safely delete all bindings
editor.keyBinding.$defaultHandler.commandKeyBinding = {}
//bind the wanted command
editor.commands.addCommand({
name: "removeline",
bindKey: { win: "Ctrl-D", mac: "Command-D" },
exec: function(editor) { editor.removeLines(); },
scrollIntoView: "cursor",
multiSelectAction: "forEachLine"
});
now that you have the command defined, you can call it from your own code if needed:
editor.execCommand("removeline")
Option 2: actually looping through the bindings.
using for (key in obj)
to loop through the key bindings and deleting keys that are not equal to ctrl-d
and command-d
:
for (key in editor.keyBinding.$defaultHandler.commandKeyBinding) {
if (key !== "ctrl-d" && key !== "command-d")
delete editor.keyBinding.$defaultHandler.commandKeyBinding[key]
}
Post a Comment for "Ace Editor, How To Remove All Keybindings But One?"