jianghh-canvas
Version:
canvas画板,支持笔写、手写、鼠标流畅绘图,自动保存,撤销重做
46 lines (45 loc) • 1.45 kB
JavaScript
var r = Object.defineProperty;
var h = (t, e, i) => e in t ? r(t, e, { enumerable: !0, configurable: !0, writable: !0, value: i }) : t[e] = i;
var s = (t, e, i) => (h(t, typeof e != "symbol" ? e + "" : e, i), i);
class o {
constructor(e) {
s(this, "revokeList", []);
s(this, "redoList", []);
s(this, "preItem", "");
// 存储上一步内容,由于撤销操作是需要滞后一步的,所以有个预备入栈的项
s(this, "maxSize", 5);
this.revokeList = [], e && (this.maxSize = e);
}
get revokeSize() {
return this.revokeList.length;
}
get redoSize() {
return this.redoList.length;
}
push(e) {
this.revokeSize === this.maxSize ? this.revokeList.shift() : this.revokeSize > this.maxSize && (this.revokeList = this.revokeList.reverse().splice(0, this.maxSize - 1).reverse()), this.preItem !== "" && this.revokeList.push(this.preItem), this.preItem = e, this.redoList = [];
}
pop() {
this.redoList.push(this.preItem);
const e = this.revokeSize ? this.revokeList.pop() : "";
return this.preItem = e, e;
}
popRedo() {
if (this.redoSize) {
const e = this.redoList.pop();
return this.revokeList.push(this.preItem), this.preItem = e, e;
} else
return "";
}
clear() {
this.revokeList = [], this.redoList = [], this.preItem = "";
}
}
function k(t, e = 5) {
const i = new o(e);
t.cacheStack = i;
}
export {
o as CacheStack,
k as useStack
};