UNPKG

mt-ui-components-vue3

Version:

玛果添实UI组件库(Vue3)

74 lines (72 loc) 2.07 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _vue = require("vue"); var _lodashEs = require("lodash"); var useRevoke = function useRevoke(initData, options) { var history = (0, _vue.ref)([]); var currentIndex = (0, _vue.ref)(-1); var currrent = (0, _vue.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((0, _lodashEs.cloneDeep)(currrent.value)); } }; /** * 新增 * @param data */ var updateState = function updateState(data) { var cloneData = (0, _lodashEs.cloneDeep)(data); // 判断是否完全相等 var length = history.value.length; if (length) { console.log((0, _lodashEs.isEqual)([[1, 2], [2, 1]], [[2, 1], [1, 2]])); console.log(JSON.stringify(cloneData), JSON.stringify(history.value[length - 1])); if ((0, _lodashEs.isEqual)(cloneData, history.value[length - 1])) { return; } } if (history.value.length === historyMax) { history.value.shift(); // 删除第一个记录 } console.log('updateState', history.value); history.value.push((0, _lodashEs.cloneDeep)(cloneData)); currentIndex.value++; }; var keyDown = function keyDown(e) { if (e.ctrlKey && e.keyCode === 90) { undo(); } }; (0, _vue.watch)(function () { return initData; }, function () { updateState(initData); }, { deep: true }); (0, _vue.onMounted)(function () { document.addEventListener('keydown', keyDown); }); (0, _vue.onBeforeMount)(function () { console.log('销毁'); document.removeEventListener('keydown', keyDown); }); return { updateState: updateState, data: currrent }; }; var _default = useRevoke; exports.default = _default;