239 Star 762 Fork 397

开源中国 / RunJS

Merged
!1 fix LineHighlight plugin addEvent many times.

lxbzmy:master 开源中国:master

lxbzmy Created on: 2014-07-17 15:41

问题出在LineHighlight 向 instance添加了重复事件处理函数。每次切换项目都会调用onEditorViewInit一次,然后就向instance添加一遍事件,导致越点击越慢。

  instance.addEvent("onJsCursorActivity",
        function(cm) {
            cm.setLineClass(hlLine, null, null);
            hlLine = cm.setLineClass(cm.getCursor().line, null, "activeline");
        });
  • 简单的方法可以通过添加一个bool判断出否初始化过来解决。
  • 复杂的话就在插件接口中设计添加额外的destroy方法来反注册事件处理函数。
LineHighlight = (function() {

    var PT = LineHighlight.prototype;
    var instance;
    var hlLine;
    var added = false;

    PT.init = function(runjs) {
        instance = this;
    };

    PT.onEditorViewInit = function() {
        if(added)return;
        hlLine = runjs.editor.editorHtml.setLineClass( - 1, "activeline");

        instance.addEvent("onJsCursorActivity",
        function(cm) {
            cm.setLineClass(hlLine, null, null);
            hlLine = cm.setLineClass(cm.getCursor().line, null, "activeline");
        });

        instance.addEvent("onCssCursorActivity",
        function(cm) {
            cm.setLineClass(hlLine, null, null);
            hlLine = cm.setLineClass(cm.getCursor().line, null, "activeline");
        });
        instance.addEvent("onHtmlCursorActivity",
        function(cm) {
            cm.setLineClass(hlLine, null, null);
            hlLine = cm.setLineClass(cm.getCursor().line, null, "activeline");
        });
        added=true;
    }

});

4 comments, 3 participants 15347 lxbzmy 1578915028 3 bluishoul 1605772844 36 ld 1578913711

Show action logs Hide action logs
红薯 merged Pull Request 2014-07-21 08:09
Java
1
https://git.oschina.net/oschina/RunJS.git
git@git.oschina.net:oschina/RunJS.git
oschina
RunJS
RunJS

Search