e-virt-table
Version:
A powerful data table based on canvas. You can use it as data grid、Microsoft Excel or Google sheets. It supports virtual scroll、cell edit etc.
106 lines • 3.29 kB
JavaScript
/**
* 数据历史堆栈
*/
export default class History {
constructor(ctx) {
Object.defineProperty(this, "ctx", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "history", {
enumerable: true,
configurable: true,
writable: true,
value: []
});
Object.defineProperty(this, "historyIndex", {
enumerable: true,
configurable: true,
writable: true,
value: -1
});
this.ctx = ctx;
this.init();
}
init() {
this.ctx.on('keydown', (e) => {
if (this.ctx.editing) {
return;
}
// 撤销
if ((e.ctrlKey && !e.shiftKey && e.code === 'KeyZ') || (e.metaKey && !e.shiftKey && e.code === 'KeyZ')) {
e.preventDefault();
this.ctx.clearSelector();
this.ctx.clearAutofill();
this.backState();
return;
}
// 恢复
if ((e.ctrlKey && e.code === 'KeyY') ||
(e.ctrlKey && e.shiftKey && e.code === 'KeyZ') ||
(e.metaKey && e.shiftKey && e.code === 'KeyZ')) {
e.preventDefault();
this.ctx.clearSelector();
this.ctx.clearAutofill();
this.forwardState();
return;
}
});
}
pushState(changeList) {
const { HISTORY_NUM = 0, ENABLE_HISTORY } = this.ctx.config;
// 是否启动历史
if (!ENABLE_HISTORY) {
return;
}
this.history.push(changeList);
if (this.history.length > HISTORY_NUM) {
this.history.splice(0, 1);
}
this.historyIndex = this.history.length - 1;
}
// 回退
backState() {
if (this.historyIndex >= 0) {
const { changeList, scrollX, scrollY } = this.history[this.historyIndex];
const data = changeList.map((item) => {
return {
rowKey: item.rowKey,
key: item.key,
value: item.oldValue,
row: {},
};
});
// 设置滚动条位置
this.ctx.setScroll(scrollX, scrollY);
// 不需要添加历史记录
this.ctx.database.batchSetItemValue(data, false);
this.historyIndex -= 1;
}
}
// 前进
forwardState() {
if (this.historyIndex < this.history.length - 1) {
this.historyIndex += 1;
const { changeList } = this.history[this.historyIndex];
const data = changeList.map((item) => {
return {
rowKey: item.rowKey,
key: item.key,
value: item.newValue,
row: {},
};
});
// 不需要添加历史记录
this.ctx.database.batchSetItemValue(data, false);
}
}
// 清空历史
clear() {
this.history = [];
this.historyIndex = -1;
}
}
//# sourceMappingURL=History.js.map