Ace编辑器和vim键绑定:using:w命令

时间:2015-01-21 23:01:06

标签: javascript ace-editor

我正在将Ace编辑器集成到Web应用程序中并使用像这样的vim键绑定:

 var editor = ace.edit('editor');
 editor.setDisplayIndentGuides(false);
 editor.setHighlightActiveLine(false);
 editor.setShowFoldWidgets(false);
 editor.setShowInvisibles(false);
 editor.setShowPrintMargin(false);
 editor.setKeyboardHandler('ace/keyboard/vim');

我也将此命令映射到Ctrl-S / Command-S只是因为我想测试行为

editor.commands.addCommand({
  name: 'saveFile',
  bindKey: {
    win: 'Ctrl-S', mac: 'Command-S',
    sender: 'editor|cli'
  },
  exec: function (env, args, request) {
    console.log('saving...', env, args, request);
  }
});

虽然这样可行,但问题是当使用ESCape键在Vim中进入“正常”模式,并使用:w来保存文件时,上面定义的命令的exec函数不会像使用Ctrl-那样被调用S / Command-S ......

keybinding-vim.js文件抛出一个错误,关于没有定义CodeMirror.commands.save ...

我查看了API文档和演示,但一直无法找到解决此问题的“正确”方法。

帮助表示感谢。 感谢

2 个答案:

答案 0 :(得分:10)

这样做还没有公开的api。但你可以做到

ace.config.loadModule("ace/keyboard/vim", function(m) {
    var VimApi = require("ace/keyboard/vim").CodeMirror.Vim
    VimApi.defineEx("write", "w", function(cm, input) {
        cm.ace.execCommand("save")
    })
})

答案 1 :(得分:0)

支架

import ace from 'brace';
require('brace/keybinding/vim');

editor.setKeyboardHandler('ace/keyboard/vim');

ace.config.loadModule('ace/keyboard/vim', function(module) {
   var VimApi = module.CodeMirror.Vim;
   VimApi.defineEx('write', 'w', function(cm, input) {
      cm.ace.execCommand('save');
   });
});
相关问题