正在查找此撤消脚本的存储库和文档
Looking for the repository and documentation for this undo script
我找到了这个图表:http://jsperf.com/undo-redo并看到了结果。但他指的是哪本历史书呢?看起来这是最快的,可能也是最好的?我在哪里能找到它?
这似乎是唯一的剧本?:https://gist.github.com/NoxArt/2692147/raw/3351cd3749bcacf684795580873c3a542e68854b/gistfile1.coffee
是否有此历史脚本或完整文档的存储库
更新
你发布的链接就是你要找的图书馆。但它是用Coffeescapet写的。如果你把它转换成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();
然而,它没有说明这是从哪里来的,也没有说明是谁写的,所以许可证也不清楚。
相关文章:
- 使用promise和mongoose对文档进行排序
- 我该如何编写一个CouchDB视图,列出每个存储文档的大小
- 可编辑文档中多个字段的本地存储
- 在新窗口中打开存储在 SharePoint 中的文档
- 将文档存储在本地存储器中
- 如何在mongodb中查询id存储在$in的“var-ids”数组中的文档
- 如何存储HTML片段并稍后将其插入文档中
- Lawnchair:检查存储及其文档是否存在
- 在HTML文档中存储对象ID: ID vs data-id vs
- 从父文档和存储中访问iframe中的文本字段以供使用
- 如何以交互方式将文本作为列表项附加到HTML文档中的列表中,而无需将列表项存储为javascript变量
- IBM worklight JSON存储删除文档数组
- 存储机制的谷歌文档,如实时编辑器
- 如何存储文档,如谷歌文档
- 使用存储在文档中的数据在xpages中绘制图形
- 防止客户端存储XMLHttpRequest文档
- Mongodb/Mongoose:将变量的子文档存储到变量
- 文档存储库(PHP)
- 如何将事件处理程序存储在单独的文档中
- 如何使用正则表达式存储文档中出现的所有特定单词?