CodeMirror批量更改

时间:2015-03-11 07:00:33

标签: javascript codemirror

有没有办法批量处理codemirror API中的更改? 例如,我使用changes API来捕获更改事件,但如果用户要按下键盘上的其中一个字母,他/她将在更改数组中获得多个条目。是否存在可以缓冲这些更改的事件,并且仅在数组中使用一个对象进行调用。

Object { from: Object, to: Object, text: Array[1], removed: Array[1], origin: "+delete" } editor.jsp:71:3
Object { from: Object, to: Object, text: Array[1], removed: Array[1], origin: "+input" } editor.jsp:71:3
Object { from: Object, to: Object, text: Array[1], removed: Array[1], origin: "+input" } editor.jsp:71:3
Object { from: Object, to: Object, text: Array[1], removed: Array[1], origin: "+input" } editor.jsp:71:3
Object { from: Object, to: Object, text: Array[1], removed: Array[1], origin: "+input" } editor.jsp:71:3
Object { from: Object, to: Object, text: Array[1], removed: Array[1], origin: "+input" } editor.jsp:71:3
Object { from: Object, to: Object, text: Array[1], removed: Array[1], origin: "+input" } editor.jsp:71:3
Object { from: Object, to: Object, text: Array[1], removed: Array[1], origin: "+input" } editor.jsp:71:3
Object { from: Object, to: Object, text: Array[1], removed: Array[1], origin: "+input" } editor.jsp:71:3
Object { from: Object, to: Object, text: Array[1], removed: Array[1], origin: "+input" } editor.jsp:71:3
Object { from: Object, to: Object, text: Array[1], removed: Array[1], origin: "+input" } editor.jsp:71:3
Object { from: Object, to: Object, text: Array[1], removed: Array[1], origin: "+input" } editor.jsp:71:3
Object { from: Object, to: Object, text: Array[1], removed: Array[1], origin: "+input" } editor.jsp:71:3
Object { from: Object, to: Object, text: Array[1], removed: Array[1], origin: "+input" } editor.jsp:71:3
Object { from: Object, to: Object, text: Array[1], removed: Array[1], origin: "+input" } editor.jsp:71:3
Object { from: Object, to: Object, text: Array[1], removed: Array[1], origin: "+input" } editor.jsp:71:3
Object { from: Object, to: Object, text: Array[1], removed: Array[1], origin: "+input" } editor.jsp:71:3
Object { from: Object, to: Object, text: Array[1], removed: Array[1], origin: "+input" } editor.jsp:71:3
Object { from: Object, to: Object, text: Array[1], removed: Array[1], origin: "+input" 

1 个答案:

答案 0 :(得分:1)

change事件始终在用户更改CodeMirror <textarea>后发生。但是,这些事件会缓存为history - 您可以使用doc.getHistory()doc.setHistory()方法根据自己的喜好进行修改和重复使用的对象:

var cm = CodeMirror.fromTextArea(document.getElementById('codesnippet_editable'), {
        mode: "javascript",
        theme: "default",
        lineNumbers: true,
        pollInterval: 100
});

document.getElementById("foo").addEventListener("click", function () {
    console.log(cm.getDoc().getHistory());  
    //check console for done and undone objects
});

HTML:

<button id="foo">See console after clicking me</button>

<textarea rows="4" cols="50" name="codesnippet_editable" id="codesnippet_editable">
// Write some code here
</textarea>

小提琴:http://jsfiddle.net/1kg2y0w5/

您可以使用pollInterval更改生成历史记录对象的频率

  

pollInterval :数字

     

指示CodeMirror应该如何快速轮询其输入textarea以进行更改(聚焦时)。大多数输入都是由事件捕获的,但某些内容(如某些浏览器上的IME输入)不会生成允许CodeMirror正确检测它的事件。因此,它民意调查。默认值为100毫秒。

这适用于用户也按住键的情况:尽管按住键的时间长于轮询间隔,但只创建了一个对象。