Phr*_*ogz 23 javascript ace-editor
我正在使用ACE编辑器进行交互式JavaScript编辑.当我将编辑器设置为JavaScript模式时,ACE会自动确定代码是否有效,并且错误消息和行号不会突出显示.
在change事件处理程序期间,我想在我尝试之前检测ACE是否认为代码有效eval().我认为我可以这样做的唯一方法是:
var jsMode = require("ace/mode/javascript").Mode;
var editor = ace.edit('mycode'), edEl = document.querySelector('#mycode');
editor.getSession().setMode(new jsMode);
editor.getSession().on('change',function(){
// bail out if ACE thinks there's an error
if (edEl.querySelector('div.ace_gutter-cell.ace_error')) return;
try{
eval(editor.getSession().getValue());
}catch(e){}
});
Run Code Online (Sandbox Code Playgroud)
然而:
change回调发生.因此,我实际上必须等待超过500毫秒(JavaScript工作人员开始之前的延迟):
editor.getSession().on('change',function(){
setTimeout(function(){
// bail out if ACE thinks there's an error
if (edEl.querySelector('div.ace_gutter-cell.ace_error')) return;
try{
eval(editor.getSession().getValue());
}catch(e){}
},550); // Must be longer than timeout delay in javascript_worker.js
});
Run Code Online (Sandbox Code Playgroud)
有没有更好的方法,在JS模式的未记录的API中,询问是否有任何错误?
hkr*_*ish 25
当注释更改时,当前会话将触发onChangeAnnotation事件.
之后,可以按如下方式检索新的注释集
var annotations = editor.getSession().getAnnotations();
Run Code Online (Sandbox Code Playgroud)
似乎可以做到这一点.它返回其具有JSON对象行作为关键和阵列作为值.该值阵列可以具有多于一个的对象,这取决于是否存在用于每行多于一个注释.
结构如下(从firebug复制 - 我写的测试脚本)
// annotations would look like
({
82:[
{/*annotation*/
row:82,
column:22,
text:"Use the array literal notation [].",
type:"warning",
lint:{/*raw output from jslint*/}
}
],
rownumber : [ {anotation1}, {annotation2} ],
...
});
Run Code Online (Sandbox Code Playgroud)
所以..
editor.getSession().on("changeAnnotation", function(){
var annot = editor.getSession().getAnnotations();
for (var key in annot){
if (annot.hasOwnProperty(key))
console.log("[" + annot[key][0].row + " , " + annot[key][0].column + "] - \t" + annot[key][0].text);
}
});
// thanks http://stackoverflow.com/a/684692/1405348 for annot.hasOwnProperty(key) :)
Run Code Online (Sandbox Code Playgroud)
当注释发生变化时,应该给出当前Ace编辑会话中所有注释的列表!
希望这可以帮助!
AceJsHint在内部使用(在worker中),正如您在文件中看到的,发出了一个事件:
this.sender.emit("jslint", lint.errors);
Run Code Online (Sandbox Code Playgroud)
您可以订阅此事件,或者在需要时自己调用 JSHint 代码(非常短)。
| 归档时间: |
|
| 查看次数: |
11933 次 |
| 最近记录: |