我正在尝试在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'}
,它确实有效,但我不明白为什么它不适用于此代码。
答案 0 :(得分: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
}
}
}
})