Ace编辑器,如何删除除一个以外的所有keyBindings? [英] Ace editor, how to remove all keyBindings but one?
问题描述
我最近在我的jsp页面上有一个文本区域
I recently had a textarea on my jsp page
为此,我在键盘上有自己的快捷键
For it, I had my own shortcuts on the keyboard
然后,我实现了ace编辑器,然后我所有的旧键绑定都不起作用
Then, I implemented the ace editor, and all my old keybindings didn't work then
然后我搜索了一段时间,发现了以下代码:
Then i searched for a while, and found this code:
//I did this before, to add the ace editor
var editor = ace.edit("fileInfo");
var JavaScriptMode = ace.require("ace/mode/javascript").Mode;
editor.session.setMode(new JavaScriptMode());
//Then I found this for the keyBindings
delete editor.keyBinding;
最后一行,禁用了ace编辑器中的所有keyBindings,我自己的keyBindings开始起作用... 一切正常,但是随后,我搜索了很多有关ace编辑器的信息,找到了一个有趣的选项,然后是我喜欢使用的一个keyBinding,那就是DELETE ROW键绑定Ctrl + D
The last line, disables all the keyBindings from the ace editor, and my own keyBindings came to work... All worked fine, but then, I searched a lot about the ace editor, and found one interesting option, and thet is the one keyBinding, that I love to use, and that is the DELETE ROW keybinding Ctrl + D
我现在想找回它,但只有它,其他人不能,而且我的旧keyBindings也应该可以工作...
and I want now to get it back, but only it, not the others, and ofc my old keyBindings should also work...
我的旧keyBindings的代码如下:
The code for my old keyBindings looks like this:
document.addEventListener("keydown", function(e) {
// Ctrl + S = SAVE
if (e.keyCode == 83 && (navigator.platform.match("Mac") ? e.metaKey : e.ctrlKey)) {
e.preventDefault();
e.stopPropagation();
if($scope.mode!='BROWSE')
{
$('#saveBtn').trigger("click");
}
}
}, false);
你能帮我吗?
推荐答案
delete editor.keyBinding;
不是一个好的解决方案,它将在控制台中产生错误消息
delete editor.keyBinding;
is not a good solution, it will produce error messages in console
未捕获的TypeError:无法读取未定义的属性'ctrl-d' 在CommandManager.handleKeyboard(ace.js:10830) 在KeyBinding.$ callKeyboardHandlers(ace.js:4081) 在KeyBinding.onCommandKey(ace.js:4113) 在Editor.onCommandKey(ace.js:12424) 在normalizeCommandKeys(ace.js:1648) 在HTMLTextAreaElement. (ace.js:1667)
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)
选项1:删除所有绑定并重新定义我们需要的绑定.
//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")
选择2:实际上是遍历绑定.
使用for (key in obj)
遍历键绑定并删除不等于ctrl-d
和command-d
的键:
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]
}
这篇关于Ace编辑器,如何删除除一个以外的所有keyBindings?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!