0

我正在尝试在 ace 编辑器中制作一些自动热键。例如,我想映射\R到 unicode 符号ℝ。这是我的 jsfiddle:http: //jsfiddle.net/U5JtP/2/

这是我的代码:

editor.getSession().setMode("ace/mode/latex");
 editor.commands.addCommand({

        bindKey: {win: '\R'},
        exec: function(editor) {
            editor.insert("ℝ");
            var selection = editor.getSelection();
            selection.moveCursorBy(0,0);
        },
        readOnly: false // false if this command should not apply in readOnly mode
    });     

如果我使用:它确实有效bindKey: {win: 'Ctrl-R'},但我不明白为什么它不适用于此代码。

4

1 回答 1

1

基本上你需要做

var Range = ace.require("ace/range").Range

var autohotkeys = [["\\R", "ℝ"]];
editor.commands.on("afterExec", function(e) {
    if (e.command.name == "insertstring") {
        var pos = editor.getCursorPosition()
        var line = editor.session.getLine(pos.row).substring(0, pos.column);
        for (var i = 0; i < autohotkeys.length; i++) {
            var key = autohotkeys[i]
            if (line.slice(-key[0].length) == key[0]) {
                editor.session.replace(new Range(
                   pos.row, pos.column - key[0].length,
                   pos.row, pos.column
                   ), key[1])
                return
            }
        }
    }
})
于 2013-10-31T22:10:56.477 回答