Javascript 正在查找此撤消脚本的存储库和文档
我找到了这个图表,并看到了结果。 但他指的是哪个历史剧本?看来这是最快的,也可能是最好的?我在哪里能找到它 似乎这是唯一的剧本Javascript 正在查找此撤消脚本的存储库和文档,javascript,jquery,undo,Javascript,Jquery,Undo,我找到了这个图表,并看到了结果。 但他指的是哪个历史剧本?看来这是最快的,也可能是最好的?我在哪里能找到它 似乎这是唯一的剧本 是否有此历史记录脚本或完整文档的存储库更新 你发布的链接就是你正在寻找的库。但它是用英文写的。如果将其转换为Javascript,您将获得测试中使用的类(以及我在下面发布的类) 我不确定我是否遗漏了要点,但所有使用的库的全部代码都存储在“准备代码”部分(否则jsperf将如何知道执行什么)。历史记录库尤其重要 /** Class managing undo/r
是否有此历史记录脚本或完整文档的存储库更新 你发布的链接就是你正在寻找的库。但它是用英文写的。如果将其转换为Javascript,您将获得测试中使用的类(以及我在下面发布的类)
我不确定我是否遗漏了要点,但所有使用的库的全部代码都存储在“准备代码”部分(否则jsperf将如何知道执行什么)。
历史记录
库尤其重要
/**
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;
})();
但是,它没有说这是从哪里来的,也没有说是谁写的,所以授权也不清楚。根据标记菜单:“要求我们推荐或查找工具、库或喜爱的非现场资源的问题对于堆栈溢出来说是离题的,因为它们往往会吸引固执己见的答案和垃圾邮件。相反,以及迄今为止为解决这一问题所做的工作。“我知道。但我刚刚发现了这个问题:但不是一个完整的库或GitHub上的脚本以及文档,这是唯一的脚本而没有文档或存储库吗?我已经改变了我的问题,因为我目前正在寻找历史脚本和它的一些文档。或者这只是为jsperf测试编写的?嗯,这不好,因为我需要文档和许可证,你真的需要每秒执行300000次撤消吗?听起来很疯狂。尽管您可以尝试联系jsperf测试的作者,因为他应该知道从哪里获得该库。这是他的github档案:我知道这是不允许的,但你会建议使用哪个库?你知道的。我刚才注意到了你发布的咖啡脚本的链接。那是
历史
脚本。但它是coffeescript,所以需要转换成Javascript。这意味着您已经找到了脚本的来源,包括作者。
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();