mt-ui-components-vue3
Version:
玛果添实UI组件库(Vue3)
67 lines (66 loc) • 1.85 kB
JavaScript
import { onMounted, watch, ref, onBeforeMount } from 'vue';
import { cloneDeep, isEqual } from 'lodash-es';
var useRevoke = function useRevoke(initData, options) {
var history = ref([]);
var currentIndex = ref(-1);
var currrent = ref();
var historyMax = 20; // 最多记录多少条数
/**
* 撤销
*/
var undo = function undo() {
console.log('undo0', history.value);
if (history.value.length > 1) {
history.value.pop();
currentIndex.value--;
currrent.value = history.value[currentIndex.value];
options === null || options === void 0 ? void 0 : options.undo(cloneDeep(currrent.value));
}
};
/**
* 新增
* @param data
*/
var updateState = function updateState(data) {
var cloneData = cloneDeep(data);
// 判断是否完全相等
var length = history.value.length;
if (length) {
console.log(isEqual([[1, 2], [2, 1]], [[2, 1], [1, 2]]));
console.log(JSON.stringify(cloneData), JSON.stringify(history.value[length - 1]));
if (isEqual(cloneData, history.value[length - 1])) {
return;
}
}
if (history.value.length === historyMax) {
history.value.shift(); // 删除第一个记录
}
console.log('updateState', history.value);
history.value.push(cloneDeep(cloneData));
currentIndex.value++;
};
var keyDown = function keyDown(e) {
if (e.ctrlKey && e.keyCode === 90) {
undo();
}
};
watch(function () {
return initData;
}, function () {
updateState(initData);
}, {
deep: true
});
onMounted(function () {
document.addEventListener('keydown', keyDown);
});
onBeforeMount(function () {
console.log('销毁');
document.removeEventListener('keydown', keyDown);
});
return {
updateState: updateState,
data: currrent
};
};
export default useRevoke;