Ace编辑器,如何删除除一个以外的所有keyBindings?

编程入门 行业动态 更新时间:2024-10-27 17:24:05
本文介绍了Ace编辑器,如何删除除一个以外的所有keyBindings?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我最近在我的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.$defaultHandlermandKeyBinding = {} //bind the wanted command editormands.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.$defaultHandlermandKeyBinding) { if (key !== "ctrl-d" && key !== "command-d") delete editor.keyBinding.$defaultHandlermandKeyBinding[key] }

更多推荐

Ace编辑器,如何删除除一个以外的所有keyBindings?

本文发布于:2023-06-06 05:52:32,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/534714.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:编辑器   Ace   keyBindings

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!