2013-11-22 37 views
0

我找到了此圖表:http://jsperf.com/undo-redo並看到了結果。 但他的歷史腳本是什麼意思?看起來這是最快的,可能是最好的?我在哪裏可以找到它?尋找這個撤銷腳本的存儲庫和文檔

看來這是唯一的腳本?: https://gist.github.com/NoxArt/2692147/raw/3351cd3749bcacf684795580873c3a542e68854b/gistfile1.coffee

有沒有什麼地方對這段歷史腳本或完整的文檔存儲庫

+0

根據標記菜單:_「有問題要求我們推薦或找到工具,圖書館或最喜歡的非現場資源**,因爲它們傾向於吸引自以爲是的答案和垃圾郵件, [描述問題](http://meta.stackexchange.com/questions/139399/what-exactly-is-a-recommendation-question)以及到目前爲止解決它的過程。「_ – War10ck

+0

我知道。但我只是發現這個:https://gist.github.com/NoxArt/2692147/raw/3351cd3749bcacf684795580873c3a542e68854b/gistfile1.coffee但不是一個完整的庫或GitHub上的腳本與文檔,這是唯一的腳本,沒有文檔或庫? –

+0

我改變了我的問題,因爲我目前只是在尋找歷史腳本和一些文檔。或者這只是爲jsperf測試寫的? –

回答

1

更新

您發佈的鏈接是圖書館你正在找。但它寫在Coffeescript。如果你將它transpile轉換成Javascript,你會得到完全在測試中使用的類(以及我在下面發佈的)。


我不知道如果我只是缺少了點,但是所有使用的資料庫的整個代碼存儲在「準備代碼」一節中(怎麼回事就jsperf知道要執行什麼)。該History圖書館尤其是

/** 
    Class managing undo/redo 
    @constructor 
    */ 
var History = (function() { 

    /** 
    Properties 
    @actions [{name(string), identifier(string), undo(callback), redo(callback)}] 
    @current pointer to a current point in history list, all the lower is history and all the higher ones are future 

    @param config {Object} 
    */ 

    function History(config) { 
     this.config = { 
      limit: Infinity, 
      onUndo: function() {}, 
      onRedo: function() {}, 
      onChange: function() {} 
     }; 
     for (var i in config) this.config[i] = config[i]; 

     this.actions = []; 
     this.current = -1; 
    } 


    /** 
    Stores the action that happened and it's undo callback 
    Caller must ensure undo and redo callbacks turn the model into a proper state 

    @param {String} name 
    @param {Function} actionDo - callback describing what's happened, serves as a 'redo' callback 
    @param {Function} actionUndo 
    @optionalParam {String} identifier - identifier, for example name of the mark that has been just added 
    */ 
    History.prototype.happened = function (name, actionDo, actionUndo, id) { 
     this.clearFuture(); 
     this.actions.push({ 
      name: name, 
      identifier: id || "", 
      undo: actionUndo, 
      redo: actionDo 
     }); 
     this.current++; 
     this.config.onChange.call(this, "happened", this.actions.length); 
     if (this.config.limit !== Infinity) { 
      while (this.actions.length > this.config.limit) { 
       this.actions.shift(); 
       this.current--; 
      } 
     } 
    }; 


    /** 
    Triggers a given action and stores it as redo 

    Caller must ensure undo and redo callbacks turn the model into a proper state 

    @param {String} name 
    @param {Function} actionDo - callback describing should now happen, serves also as a 'redo' callback 
    @param {Function} actionUndo 
    @optionalParam {String} id - identifier, for example name of the mark that has been just added 
    */ 
    History.prototype.happen = function (name, actionDo, actionUndo, id) { 
     this.happened(name, actionDo, actionUndo, id || ""); 
     return actionDo(); 
    }; 

    /** 
    Undos X steps 

    @optionalParam {Number} steps 
    */ 
    History.prototype.undo = function (steps) { 
     var self = this, 
      step = 0; 

     steps = steps || 0; 
     if (steps === 0) return this.undoOne(); 

     while (step++ < steps) { 
      self.undoOne(); 
      self.config.onUndo(); 
      self.config.onChange.call(self, "undo", self.current + 1); 
     } 
    }; 

    History.prototype.undoOne = function() { 
     if (this.actions[this.current]) { 
      this.actions[this.current].undo(); 
      this.config.onChange.call(this, "undo", this.current); 
      return this.current--; 
     } 
    }; 

    /** 
    Redos X steps 

    @optionalParam {Number} steps 
    */ 
    History.prototype.redo = function (steps) { 
     var self = this, 
      step = 0; 

     steps = steps || 0; 

     if (steps === 0) return this.redoOne(); 

     while (step++ < steps) { 
      self.redoOne(); 
      self.config.onRedo(); 
      self.config.onChange.call(this, "redo", self.current + 1); 
     } 
    }; 

    History.prototype.redoOne = function() { 
     if (this.actions[this.current + 1]) { 
      this.current++; 
      this.config.onChange.call(this, "redo", this.current + 1); 
      return this.actions[this.current].redo(); 
     } 
    }; 

    History.prototype.goTo = function (val) { 
     return this.actions[val]; 
    }; 


    /** 
    Returns all entries that can be undo-ed 

    @return [historyStack] 
    */ 
    History.prototype.getHistory = function() { 
     if (this.current === -1) { 
      return []; 
     } else { 
      return this.actions.slice(0, this.current + 1); 
     } 
    }; 


    /** 
    Returns all entries that can be redo-ed 

    @return [historyStack] 
    */ 
    History.prototype.getFuture = function() { 
     if (this.current + 1 >= this.actions.length) { 
      return []; 
     } else { 
      return this.actions.slice(this.current + 1, this.actions.length); 
     } 
    }; 



    /** 
    Has history entries = can undo? 

    @returns bool 
    */ 
    History.prototype.hasHistory = function() { 
     return this.current > -1; 
    }; 


    /** 
    Has future entries = can redo? 

    @returns bool 
    */ 
    History.prototype.hasFuture = function() { 
     return this.current + 1 < this.actions.length; 
    }; 

    /** 
    Clear the complete history stack 

    @returns [historyStack] 
    */ 
    History.prototype.clear = function() { 
     this.current = -1; 
     this.actions = []; 
    }; 

    History.prototype.clearFuture = function() { 
     if (this.current + 1 < this.actions.length) { 
      this.actions.splice(this.current + 1, this.actions.length - this.current - 1); 
      return History; 
     } 
    }; 

    return History; 

})(); 

測試設置(=使用)也有

var history = new History(); 
var historyTotal = 0; 

function historyTest() { 
    history.happen("toggle", function() { 
     historyTotal++; 
    }, function() { 
     historyTotal--; 
    }); 
} 

historyTest(); 
historyTest(); 
historyTest(); 
history.undo(); 
history.undo(); 
history.redo(); 
history.undo(); 
history.redo(); 

但是,它並沒有說在哪裏,這是來自或誰寫的,所以許可證也不清楚。

+0

hm,這是不好的,因爲我需要文檔和許可證 –

+0

您真的需要能夠每秒執行300,000個undos嗎?這聽起來很瘋狂。只要去另一個圖書館。 –

+0

雖然你可以嘗試聯繫jsperf測試的作者,因爲他應該知道他從哪裏得到該庫。這裏是他的github配置文件:https://github.com/yckart –