UNPKG

hyperformula-dc

Version:

HyperFormula is a JavaScript engine for efficient processing of spreadsheet-like data and formulas

1,296 lines (1,106 loc) 42.7 kB
function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); } function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; } function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e2) { throw _e2; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e3) { didErr = true; err = _e3; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; } function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; } import "regenerator-runtime/runtime.js"; import "core-js/modules/es.function.name.js"; import "core-js/modules/es.array.iterator.js"; import "core-js/modules/es.map.js"; import "core-js/modules/es.object.to-string.js"; import "core-js/modules/es.string.iterator.js"; import "core-js/modules/web.dom-collections.iterator.js"; import "core-js/modules/es.array.splice.js"; import "core-js/modules/es.object.get-prototype-of.js"; import "core-js/modules/es.reflect.construct.js"; import "core-js/modules/es.array.slice.js"; import "core-js/modules/es.array.from.js"; import "core-js/modules/es.symbol.js"; import "core-js/modules/es.symbol.description.js"; import "core-js/modules/es.symbol.iterator.js"; function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); } function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); } function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; } function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); } function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } } function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } /** * @license * Copyright (c) 2021 Handsoncode. All rights reserved. */ import { equalSimpleCellAddress, simpleCellAddress } from './Cell'; import { AddColumnsCommand, AddRowsCommand, RemoveColumnsCommand, RemoveRowsCommand } from './Operations'; export var BaseUndoEntry = function BaseUndoEntry() { _classCallCheck(this, BaseUndoEntry); }; export var RemoveRowsUndoEntry = /*#__PURE__*/function (_BaseUndoEntry) { _inherits(RemoveRowsUndoEntry, _BaseUndoEntry); var _super = _createSuper(RemoveRowsUndoEntry); function RemoveRowsUndoEntry(command, rowsRemovals) { var _this; _classCallCheck(this, RemoveRowsUndoEntry); _this = _super.call(this); _this.command = command; _this.rowsRemovals = rowsRemovals; return _this; } _createClass(RemoveRowsUndoEntry, [{ key: "doUndo", value: function doUndo(undoRedo) { undoRedo.undoRemoveRows(this); } }, { key: "doRedo", value: function doRedo(undoRedo) { undoRedo.redoRemoveRows(this); } }]); return RemoveRowsUndoEntry; }(BaseUndoEntry); export var MoveCellsUndoEntry = /*#__PURE__*/function (_BaseUndoEntry2) { _inherits(MoveCellsUndoEntry, _BaseUndoEntry2); var _super2 = _createSuper(MoveCellsUndoEntry); function MoveCellsUndoEntry(sourceLeftCorner, width, height, destinationLeftCorner, overwrittenCellsData, addedGlobalNamedExpressions, version) { var _this2; _classCallCheck(this, MoveCellsUndoEntry); _this2 = _super2.call(this); _this2.sourceLeftCorner = sourceLeftCorner; _this2.width = width; _this2.height = height; _this2.destinationLeftCorner = destinationLeftCorner; _this2.overwrittenCellsData = overwrittenCellsData; _this2.addedGlobalNamedExpressions = addedGlobalNamedExpressions; _this2.version = version; return _this2; } _createClass(MoveCellsUndoEntry, [{ key: "doUndo", value: function doUndo(undoRedo) { undoRedo.undoMoveCells(this); } }, { key: "doRedo", value: function doRedo(undoRedo) { undoRedo.redoMoveCells(this); } }]); return MoveCellsUndoEntry; }(BaseUndoEntry); export var AddRowsUndoEntry = /*#__PURE__*/function (_BaseUndoEntry3) { _inherits(AddRowsUndoEntry, _BaseUndoEntry3); var _super3 = _createSuper(AddRowsUndoEntry); function AddRowsUndoEntry(command) { var _this3; _classCallCheck(this, AddRowsUndoEntry); _this3 = _super3.call(this); _this3.command = command; return _this3; } _createClass(AddRowsUndoEntry, [{ key: "doUndo", value: function doUndo(undoRedo) { undoRedo.undoAddRows(this); } }, { key: "doRedo", value: function doRedo(undoRedo) { undoRedo.redoAddRows(this); } }]); return AddRowsUndoEntry; }(BaseUndoEntry); export var SetRowOrderUndoEntry = /*#__PURE__*/function (_BaseUndoEntry4) { _inherits(SetRowOrderUndoEntry, _BaseUndoEntry4); var _super4 = _createSuper(SetRowOrderUndoEntry); function SetRowOrderUndoEntry(sheetId, rowMapping, oldContent) { var _this4; _classCallCheck(this, SetRowOrderUndoEntry); _this4 = _super4.call(this); _this4.sheetId = sheetId; _this4.rowMapping = rowMapping; _this4.oldContent = oldContent; return _this4; } _createClass(SetRowOrderUndoEntry, [{ key: "doUndo", value: function doUndo(undoRedo) { undoRedo.undoSetRowOrder(this); } }, { key: "doRedo", value: function doRedo(undoRedo) { undoRedo.redoSetRowOrder(this); } }]); return SetRowOrderUndoEntry; }(BaseUndoEntry); export var SetColumnOrderUndoEntry = /*#__PURE__*/function (_BaseUndoEntry5) { _inherits(SetColumnOrderUndoEntry, _BaseUndoEntry5); var _super5 = _createSuper(SetColumnOrderUndoEntry); function SetColumnOrderUndoEntry(sheetId, columnMapping, oldContent) { var _this5; _classCallCheck(this, SetColumnOrderUndoEntry); _this5 = _super5.call(this); _this5.sheetId = sheetId; _this5.columnMapping = columnMapping; _this5.oldContent = oldContent; return _this5; } _createClass(SetColumnOrderUndoEntry, [{ key: "doUndo", value: function doUndo(undoRedo) { undoRedo.undoSetColumnOrder(this); } }, { key: "doRedo", value: function doRedo(undoRedo) { undoRedo.redoSetColumnOrder(this); } }]); return SetColumnOrderUndoEntry; }(BaseUndoEntry); export var SetSheetContentUndoEntry = /*#__PURE__*/function (_BaseUndoEntry6) { _inherits(SetSheetContentUndoEntry, _BaseUndoEntry6); var _super6 = _createSuper(SetSheetContentUndoEntry); function SetSheetContentUndoEntry(sheetId, oldSheetContent, newSheetContent) { var _this6; _classCallCheck(this, SetSheetContentUndoEntry); _this6 = _super6.call(this); _this6.sheetId = sheetId; _this6.oldSheetContent = oldSheetContent; _this6.newSheetContent = newSheetContent; return _this6; } _createClass(SetSheetContentUndoEntry, [{ key: "doUndo", value: function doUndo(undoRedo) { undoRedo.undoSetSheetContent(this); } }, { key: "doRedo", value: function doRedo(undoRedo) { undoRedo.redoSetSheetContent(this); } }]); return SetSheetContentUndoEntry; }(BaseUndoEntry); export var MoveRowsUndoEntry = /*#__PURE__*/function (_BaseUndoEntry7) { _inherits(MoveRowsUndoEntry, _BaseUndoEntry7); var _super7 = _createSuper(MoveRowsUndoEntry); function MoveRowsUndoEntry(sheet, startRow, numberOfRows, targetRow, version) { var _this7; _classCallCheck(this, MoveRowsUndoEntry); _this7 = _super7.call(this); _this7.sheet = sheet; _this7.startRow = startRow; _this7.numberOfRows = numberOfRows; _this7.targetRow = targetRow; _this7.version = version; _this7.undoStart = _this7.startRow < _this7.targetRow ? _this7.targetRow - _this7.numberOfRows : _this7.targetRow; _this7.undoEnd = _this7.startRow > _this7.targetRow ? _this7.startRow + _this7.numberOfRows : _this7.startRow; return _this7; } _createClass(MoveRowsUndoEntry, [{ key: "doUndo", value: function doUndo(undoRedo) { undoRedo.undoMoveRows(this); } }, { key: "doRedo", value: function doRedo(undoRedo) { undoRedo.redoMoveRows(this); } }]); return MoveRowsUndoEntry; }(BaseUndoEntry); export var MoveColumnsUndoEntry = /*#__PURE__*/function (_BaseUndoEntry8) { _inherits(MoveColumnsUndoEntry, _BaseUndoEntry8); var _super8 = _createSuper(MoveColumnsUndoEntry); function MoveColumnsUndoEntry(sheet, startColumn, numberOfColumns, targetColumn, version) { var _this8; _classCallCheck(this, MoveColumnsUndoEntry); _this8 = _super8.call(this); _this8.sheet = sheet; _this8.startColumn = startColumn; _this8.numberOfColumns = numberOfColumns; _this8.targetColumn = targetColumn; _this8.version = version; _this8.undoStart = _this8.startColumn < _this8.targetColumn ? _this8.targetColumn - _this8.numberOfColumns : _this8.targetColumn; _this8.undoEnd = _this8.startColumn > _this8.targetColumn ? _this8.startColumn + _this8.numberOfColumns : _this8.startColumn; return _this8; } _createClass(MoveColumnsUndoEntry, [{ key: "doUndo", value: function doUndo(undoRedo) { undoRedo.undoMoveColumns(this); } }, { key: "doRedo", value: function doRedo(undoRedo) { undoRedo.redoMoveColumns(this); } }]); return MoveColumnsUndoEntry; }(BaseUndoEntry); export var AddColumnsUndoEntry = /*#__PURE__*/function (_BaseUndoEntry9) { _inherits(AddColumnsUndoEntry, _BaseUndoEntry9); var _super9 = _createSuper(AddColumnsUndoEntry); function AddColumnsUndoEntry(command) { var _this9; _classCallCheck(this, AddColumnsUndoEntry); _this9 = _super9.call(this); _this9.command = command; return _this9; } _createClass(AddColumnsUndoEntry, [{ key: "doUndo", value: function doUndo(undoRedo) { undoRedo.undoAddColumns(this); } }, { key: "doRedo", value: function doRedo(undoRedo) { undoRedo.redoAddColumns(this); } }]); return AddColumnsUndoEntry; }(BaseUndoEntry); export var RemoveColumnsUndoEntry = /*#__PURE__*/function (_BaseUndoEntry10) { _inherits(RemoveColumnsUndoEntry, _BaseUndoEntry10); var _super10 = _createSuper(RemoveColumnsUndoEntry); function RemoveColumnsUndoEntry(command, columnsRemovals) { var _this10; _classCallCheck(this, RemoveColumnsUndoEntry); _this10 = _super10.call(this); _this10.command = command; _this10.columnsRemovals = columnsRemovals; return _this10; } _createClass(RemoveColumnsUndoEntry, [{ key: "doUndo", value: function doUndo(undoRedo) { undoRedo.undoRemoveColumns(this); } }, { key: "doRedo", value: function doRedo(undoRedo) { undoRedo.redoRemoveColumns(this); } }]); return RemoveColumnsUndoEntry; }(BaseUndoEntry); export var AddSheetUndoEntry = /*#__PURE__*/function (_BaseUndoEntry11) { _inherits(AddSheetUndoEntry, _BaseUndoEntry11); var _super11 = _createSuper(AddSheetUndoEntry); function AddSheetUndoEntry(sheetName) { var _this11; _classCallCheck(this, AddSheetUndoEntry); _this11 = _super11.call(this); _this11.sheetName = sheetName; return _this11; } _createClass(AddSheetUndoEntry, [{ key: "doUndo", value: function doUndo(undoRedo) { undoRedo.undoAddSheet(this); } }, { key: "doRedo", value: function doRedo(undoRedo) { undoRedo.redoAddSheet(this); } }]); return AddSheetUndoEntry; }(BaseUndoEntry); export var RemoveSheetUndoEntry = /*#__PURE__*/function (_BaseUndoEntry12) { _inherits(RemoveSheetUndoEntry, _BaseUndoEntry12); var _super12 = _createSuper(RemoveSheetUndoEntry); function RemoveSheetUndoEntry(sheetName, sheetId, oldSheetContent, scopedNamedExpressions, version) { var _this12; _classCallCheck(this, RemoveSheetUndoEntry); _this12 = _super12.call(this); _this12.sheetName = sheetName; _this12.sheetId = sheetId; _this12.oldSheetContent = oldSheetContent; _this12.scopedNamedExpressions = scopedNamedExpressions; _this12.version = version; return _this12; } _createClass(RemoveSheetUndoEntry, [{ key: "doUndo", value: function doUndo(undoRedo) { undoRedo.undoRemoveSheet(this); } }, { key: "doRedo", value: function doRedo(undoRedo) { undoRedo.redoRemoveSheet(this); } }]); return RemoveSheetUndoEntry; }(BaseUndoEntry); export var RenameSheetUndoEntry = /*#__PURE__*/function (_BaseUndoEntry13) { _inherits(RenameSheetUndoEntry, _BaseUndoEntry13); var _super13 = _createSuper(RenameSheetUndoEntry); function RenameSheetUndoEntry(sheetId, oldName, newName) { var _this13; _classCallCheck(this, RenameSheetUndoEntry); _this13 = _super13.call(this); _this13.sheetId = sheetId; _this13.oldName = oldName; _this13.newName = newName; return _this13; } _createClass(RenameSheetUndoEntry, [{ key: "doUndo", value: function doUndo(undoRedo) { undoRedo.undoRenameSheet(this); } }, { key: "doRedo", value: function doRedo(undoRedo) { undoRedo.redoRenameSheet(this); } }]); return RenameSheetUndoEntry; }(BaseUndoEntry); export var ClearSheetUndoEntry = /*#__PURE__*/function (_BaseUndoEntry14) { _inherits(ClearSheetUndoEntry, _BaseUndoEntry14); var _super14 = _createSuper(ClearSheetUndoEntry); function ClearSheetUndoEntry(sheetId, oldSheetContent) { var _this14; _classCallCheck(this, ClearSheetUndoEntry); _this14 = _super14.call(this); _this14.sheetId = sheetId; _this14.oldSheetContent = oldSheetContent; return _this14; } _createClass(ClearSheetUndoEntry, [{ key: "doUndo", value: function doUndo(undoRedo) { undoRedo.undoClearSheet(this); } }, { key: "doRedo", value: function doRedo(undoRedo) { undoRedo.redoClearSheet(this); } }]); return ClearSheetUndoEntry; }(BaseUndoEntry); export var SetCellContentsUndoEntry = /*#__PURE__*/function (_BaseUndoEntry15) { _inherits(SetCellContentsUndoEntry, _BaseUndoEntry15); var _super15 = _createSuper(SetCellContentsUndoEntry); function SetCellContentsUndoEntry(cellContents) { var _this15; _classCallCheck(this, SetCellContentsUndoEntry); _this15 = _super15.call(this); _this15.cellContents = cellContents; return _this15; } _createClass(SetCellContentsUndoEntry, [{ key: "doUndo", value: function doUndo(undoRedo) { undoRedo.undoSetCellContents(this); } }, { key: "doRedo", value: function doRedo(undoRedo) { undoRedo.redoSetCellContents(this); } }]); return SetCellContentsUndoEntry; }(BaseUndoEntry); export var PasteUndoEntry = /*#__PURE__*/function (_BaseUndoEntry16) { _inherits(PasteUndoEntry, _BaseUndoEntry16); var _super16 = _createSuper(PasteUndoEntry); function PasteUndoEntry(targetLeftCorner, oldContent, newContent, addedGlobalNamedExpressions) { var _this16; _classCallCheck(this, PasteUndoEntry); _this16 = _super16.call(this); _this16.targetLeftCorner = targetLeftCorner; _this16.oldContent = oldContent; _this16.newContent = newContent; _this16.addedGlobalNamedExpressions = addedGlobalNamedExpressions; return _this16; } _createClass(PasteUndoEntry, [{ key: "doUndo", value: function doUndo(undoRedo) { undoRedo.undoPaste(this); } }, { key: "doRedo", value: function doRedo(undoRedo) { undoRedo.redoPaste(this); } }]); return PasteUndoEntry; }(BaseUndoEntry); export var AddNamedExpressionUndoEntry = /*#__PURE__*/function (_BaseUndoEntry17) { _inherits(AddNamedExpressionUndoEntry, _BaseUndoEntry17); var _super17 = _createSuper(AddNamedExpressionUndoEntry); function AddNamedExpressionUndoEntry(name, newContent, scope, options) { var _this17; _classCallCheck(this, AddNamedExpressionUndoEntry); _this17 = _super17.call(this); _this17.name = name; _this17.newContent = newContent; _this17.scope = scope; _this17.options = options; return _this17; } _createClass(AddNamedExpressionUndoEntry, [{ key: "doUndo", value: function doUndo(undoRedo) { undoRedo.undoAddNamedExpression(this); } }, { key: "doRedo", value: function doRedo(undoRedo) { undoRedo.redoAddNamedExpression(this); } }]); return AddNamedExpressionUndoEntry; }(BaseUndoEntry); export var RemoveNamedExpressionUndoEntry = /*#__PURE__*/function (_BaseUndoEntry18) { _inherits(RemoveNamedExpressionUndoEntry, _BaseUndoEntry18); var _super18 = _createSuper(RemoveNamedExpressionUndoEntry); function RemoveNamedExpressionUndoEntry(namedExpression, content, scope) { var _this18; _classCallCheck(this, RemoveNamedExpressionUndoEntry); _this18 = _super18.call(this); _this18.namedExpression = namedExpression; _this18.content = content; _this18.scope = scope; return _this18; } _createClass(RemoveNamedExpressionUndoEntry, [{ key: "doUndo", value: function doUndo(undoRedo) { undoRedo.undoRemoveNamedExpression(this); } }, { key: "doRedo", value: function doRedo(undoRedo) { undoRedo.redoRemoveNamedExpression(this); } }]); return RemoveNamedExpressionUndoEntry; }(BaseUndoEntry); export var ChangeNamedExpressionUndoEntry = /*#__PURE__*/function (_BaseUndoEntry19) { _inherits(ChangeNamedExpressionUndoEntry, _BaseUndoEntry19); var _super19 = _createSuper(ChangeNamedExpressionUndoEntry); function ChangeNamedExpressionUndoEntry(namedExpression, newContent, oldContent, scope, options) { var _this19; _classCallCheck(this, ChangeNamedExpressionUndoEntry); _this19 = _super19.call(this); _this19.namedExpression = namedExpression; _this19.newContent = newContent; _this19.oldContent = oldContent; _this19.scope = scope; _this19.options = options; return _this19; } _createClass(ChangeNamedExpressionUndoEntry, [{ key: "doUndo", value: function doUndo(undoRedo) { undoRedo.undoChangeNamedExpression(this); } }, { key: "doRedo", value: function doRedo(undoRedo) { undoRedo.redoChangeNamedExpression(this); } }]); return ChangeNamedExpressionUndoEntry; }(BaseUndoEntry); export var BatchUndoEntry = /*#__PURE__*/function (_BaseUndoEntry20) { _inherits(BatchUndoEntry, _BaseUndoEntry20); var _super20 = _createSuper(BatchUndoEntry); function BatchUndoEntry() { var _this20; _classCallCheck(this, BatchUndoEntry); _this20 = _super20.apply(this, arguments); _this20.operations = []; return _this20; } _createClass(BatchUndoEntry, [{ key: "add", value: function add(operation) { this.operations.push(operation); } }, { key: "reversedOperations", value: /*#__PURE__*/regeneratorRuntime.mark(function reversedOperations() { var i; return regeneratorRuntime.wrap(function reversedOperations$(_context) { while (1) { switch (_context.prev = _context.next) { case 0: i = this.operations.length - 1; case 1: if (!(i >= 0)) { _context.next = 7; break; } _context.next = 4; return this.operations[i]; case 4: i--; _context.next = 1; break; case 7: case "end": return _context.stop(); } } }, reversedOperations, this); }) }, { key: "doUndo", value: function doUndo(undoRedo) { undoRedo.undoBatch(this); } }, { key: "doRedo", value: function doRedo(undoRedo) { undoRedo.redoBatch(this); } }]); return BatchUndoEntry; }(BaseUndoEntry); export var UndoRedo = /*#__PURE__*/function () { function UndoRedo(config, operations) { _classCallCheck(this, UndoRedo); this.operations = operations; this.undoStack = []; this.redoStack = []; this.oldData = new Map(); this.undoLimit = config.undoLimit; } _createClass(UndoRedo, [{ key: "saveOperation", value: function saveOperation(operation) { if (this.batchUndoEntry !== undefined) { this.batchUndoEntry.add(operation); } else { this.addUndoEntry(operation); } } }, { key: "beginBatchMode", value: function beginBatchMode() { this.batchUndoEntry = new BatchUndoEntry(); } }, { key: "addUndoEntry", value: function addUndoEntry(operation) { this.undoStack.push(operation); this.undoStack.splice(0, Math.max(0, this.undoStack.length - this.undoLimit)); } }, { key: "commitBatchMode", value: function commitBatchMode() { if (this.batchUndoEntry === undefined) { throw 'Batch mode wasn\'t started'; } this.addUndoEntry(this.batchUndoEntry); this.batchUndoEntry = undefined; } }, { key: "storeDataForVersion", value: function storeDataForVersion(version, address, astHash) { if (!this.oldData.has(version)) { this.oldData.set(version, []); } var currentOldData = this.oldData.get(version); currentOldData.push([address, astHash]); } }, { key: "clearRedoStack", value: function clearRedoStack() { this.redoStack = []; } }, { key: "clearUndoStack", value: function clearUndoStack() { this.undoStack = []; } }, { key: "isUndoStackEmpty", value: function isUndoStackEmpty() { return this.undoStack.length === 0; } }, { key: "isRedoStackEmpty", value: function isRedoStackEmpty() { return this.redoStack.length === 0; } }, { key: "undo", value: function undo() { var operation = this.undoStack.pop(); if (!operation) { throw 'Attempted to undo without operation on stack'; } this.undoEntry(operation); this.redoStack.push(operation); } }, { key: "undoEntry", value: function undoEntry(operation) { operation.doUndo(this); } }, { key: "undoBatch", value: function undoBatch(batchOperation) { var _iterator = _createForOfIteratorHelper(batchOperation.reversedOperations()), _step; try { for (_iterator.s(); !(_step = _iterator.n()).done;) { var operation = _step.value; this.undoEntry(operation); } } catch (err) { _iterator.e(err); } finally { _iterator.f(); } } }, { key: "undoRemoveRows", value: function undoRemoveRows(operation) { this.operations.forceApplyPostponedTransformations(); var sheet = operation.command.sheet, rowsRemovals = operation.rowsRemovals; for (var i = rowsRemovals.length - 1; i >= 0; --i) { var rowsRemoval = rowsRemovals[i]; this.operations.addRows(new AddRowsCommand(sheet, [[rowsRemoval.rowFrom, rowsRemoval.rowCount]])); var _iterator2 = _createForOfIteratorHelper(rowsRemoval.removedCells), _step2; try { for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) { var _step2$value = _step2.value, address = _step2$value.address, cellType = _step2$value.cellType; this.operations.restoreCell(address, cellType); } } catch (err) { _iterator2.e(err); } finally { _iterator2.f(); } this.restoreOldDataFromVersion(rowsRemoval.version - 1); } } }, { key: "undoRemoveColumns", value: function undoRemoveColumns(operation) { this.operations.forceApplyPostponedTransformations(); var sheet = operation.command.sheet, columnsRemovals = operation.columnsRemovals; for (var i = columnsRemovals.length - 1; i >= 0; --i) { var columnsRemoval = columnsRemovals[i]; this.operations.addColumns(new AddColumnsCommand(sheet, [[columnsRemoval.columnFrom, columnsRemoval.columnCount]])); var _iterator3 = _createForOfIteratorHelper(columnsRemoval.removedCells), _step3; try { for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) { var _step3$value = _step3.value, address = _step3$value.address, cellType = _step3$value.cellType; this.operations.restoreCell(address, cellType); } } catch (err) { _iterator3.e(err); } finally { _iterator3.f(); } this.restoreOldDataFromVersion(columnsRemoval.version - 1); } } }, { key: "undoAddRows", value: function undoAddRows(operation) { var addedRowsSpans = operation.command.rowsSpans(); for (var i = addedRowsSpans.length - 1; i >= 0; --i) { var addedRows = addedRowsSpans[i]; this.operations.removeRows(new RemoveRowsCommand(operation.command.sheet, [[addedRows.rowStart, addedRows.numberOfRows]])); } } }, { key: "undoAddColumns", value: function undoAddColumns(operation) { var addedColumnsSpans = operation.command.columnsSpans(); for (var i = addedColumnsSpans.length - 1; i >= 0; --i) { var addedColumns = addedColumnsSpans[i]; this.operations.removeColumns(new RemoveColumnsCommand(operation.command.sheet, [[addedColumns.columnStart, addedColumns.numberOfColumns]])); } } }, { key: "undoSetCellContents", value: function undoSetCellContents(operation) { var _iterator4 = _createForOfIteratorHelper(operation.cellContents), _step4; try { for (_iterator4.s(); !(_step4 = _iterator4.n()).done;) { var cellContentData = _step4.value; var address = cellContentData.address; var _cellContentData$oldC = _slicedToArray(cellContentData.oldContent, 2), oldContentAddress = _cellContentData$oldC[0], oldContent = _cellContentData$oldC[1]; if (!equalSimpleCellAddress(address, oldContentAddress)) { this.operations.setCellEmpty(address); } this.operations.restoreCell(oldContentAddress, oldContent); } } catch (err) { _iterator4.e(err); } finally { _iterator4.f(); } } }, { key: "undoPaste", value: function undoPaste(operation) { this.restoreOperationOldContent(operation.oldContent); var _iterator5 = _createForOfIteratorHelper(operation.addedGlobalNamedExpressions), _step5; try { for (_iterator5.s(); !(_step5 = _iterator5.n()).done;) { var namedExpression = _step5.value; this.operations.removeNamedExpression(namedExpression); } } catch (err) { _iterator5.e(err); } finally { _iterator5.f(); } } }, { key: "undoMoveRows", value: function undoMoveRows(operation) { var sheet = operation.sheet; this.operations.moveRows(sheet, operation.undoStart, operation.numberOfRows, operation.undoEnd); this.restoreOldDataFromVersion(operation.version - 1); } }, { key: "undoMoveColumns", value: function undoMoveColumns(operation) { var sheet = operation.sheet; this.operations.moveColumns(sheet, operation.undoStart, operation.numberOfColumns, operation.undoEnd); this.restoreOldDataFromVersion(operation.version - 1); } }, { key: "undoMoveCells", value: function undoMoveCells(operation) { this.operations.forceApplyPostponedTransformations(); this.operations.moveCells(operation.destinationLeftCorner, operation.width, operation.height, operation.sourceLeftCorner); this.restoreOperationOldContent(operation.overwrittenCellsData); this.restoreOldDataFromVersion(operation.version - 1); var _iterator6 = _createForOfIteratorHelper(operation.addedGlobalNamedExpressions), _step6; try { for (_iterator6.s(); !(_step6 = _iterator6.n()).done;) { var namedExpression = _step6.value; this.operations.removeNamedExpression(namedExpression); } } catch (err) { _iterator6.e(err); } finally { _iterator6.f(); } } }, { key: "undoAddSheet", value: function undoAddSheet(operation) { var sheetName = operation.sheetName; this.operations.removeSheetByName(sheetName); } }, { key: "undoRemoveSheet", value: function undoRemoveSheet(operation) { this.operations.forceApplyPostponedTransformations(); var oldSheetContent = operation.oldSheetContent, sheetId = operation.sheetId; this.operations.addSheet(operation.sheetName); for (var rowIndex = 0; rowIndex < oldSheetContent.length; rowIndex++) { var row = oldSheetContent[rowIndex]; for (var col = 0; col < row.length; col++) { var cellType = row[col]; var address = simpleCellAddress(sheetId, col, rowIndex); this.operations.restoreCell(address, cellType); } } var _iterator7 = _createForOfIteratorHelper(operation.scopedNamedExpressions), _step7; try { for (_iterator7.s(); !(_step7 = _iterator7.n()).done;) { var _step7$value = _slicedToArray(_step7.value, 2), namedexpression = _step7$value[0], content = _step7$value[1]; this.operations.restoreNamedExpression(namedexpression, content, sheetId); } } catch (err) { _iterator7.e(err); } finally { _iterator7.f(); } this.restoreOldDataFromVersion(operation.version - 1); } }, { key: "undoRenameSheet", value: function undoRenameSheet(operation) { this.operations.renameSheet(operation.sheetId, operation.oldName); } }, { key: "undoClearSheet", value: function undoClearSheet(operation) { var oldSheetContent = operation.oldSheetContent, sheetId = operation.sheetId; for (var rowIndex = 0; rowIndex < oldSheetContent.length; rowIndex++) { var row = oldSheetContent[rowIndex]; for (var col = 0; col < row.length; col++) { var cellType = row[col]; var address = simpleCellAddress(sheetId, col, rowIndex); this.operations.restoreCell(address, cellType); } } } }, { key: "undoSetSheetContent", value: function undoSetSheetContent(operation) { var oldSheetContent = operation.oldSheetContent, sheetId = operation.sheetId; this.operations.clearSheet(sheetId); for (var rowIndex = 0; rowIndex < oldSheetContent.length; rowIndex++) { var row = oldSheetContent[rowIndex]; for (var col = 0; col < row.length; col++) { var cellType = row[col]; var address = simpleCellAddress(sheetId, col, rowIndex); this.operations.restoreCell(address, cellType); } } } }, { key: "undoAddNamedExpression", value: function undoAddNamedExpression(operation) { this.operations.removeNamedExpression(operation.name, operation.scope); } }, { key: "undoRemoveNamedExpression", value: function undoRemoveNamedExpression(operation) { this.operations.restoreNamedExpression(operation.namedExpression, operation.content, operation.scope); } }, { key: "undoChangeNamedExpression", value: function undoChangeNamedExpression(operation) { this.operations.restoreNamedExpression(operation.namedExpression, operation.oldContent, operation.scope); } }, { key: "undoSetRowOrder", value: function undoSetRowOrder(operation) { this.restoreOperationOldContent(operation.oldContent); } }, { key: "undoSetColumnOrder", value: function undoSetColumnOrder(operation) { this.restoreOperationOldContent(operation.oldContent); } }, { key: "restoreOperationOldContent", value: function restoreOperationOldContent(oldContent) { var _iterator8 = _createForOfIteratorHelper(oldContent), _step8; try { for (_iterator8.s(); !(_step8 = _iterator8.n()).done;) { var _step8$value = _slicedToArray(_step8.value, 2), address = _step8$value[0], clipboardCell = _step8$value[1]; this.operations.restoreCell(address, clipboardCell); } } catch (err) { _iterator8.e(err); } finally { _iterator8.f(); } } }, { key: "redo", value: function redo() { var operation = this.redoStack.pop(); if (!operation) { throw 'Attempted to redo without operation on stack'; } this.redoEntry(operation); this.undoStack.push(operation); } }, { key: "redoEntry", value: function redoEntry(operation) { operation.doRedo(this); } }, { key: "redoBatch", value: function redoBatch(batchOperation) { var _iterator9 = _createForOfIteratorHelper(batchOperation.operations), _step9; try { for (_iterator9.s(); !(_step9 = _iterator9.n()).done;) { var operation = _step9.value; this.redoEntry(operation); } } catch (err) { _iterator9.e(err); } finally { _iterator9.f(); } } }, { key: "redoRemoveRows", value: function redoRemoveRows(operation) { this.operations.removeRows(operation.command); } }, { key: "redoMoveCells", value: function redoMoveCells(operation) { this.operations.moveCells(operation.sourceLeftCorner, operation.width, operation.height, operation.destinationLeftCorner); } }, { key: "redoRemoveColumns", value: function redoRemoveColumns(operation) { this.operations.removeColumns(operation.command); } }, { key: "redoPaste", value: function redoPaste(operation) { var targetLeftCorner = operation.targetLeftCorner, newContent = operation.newContent; var height = newContent.length; var width = newContent[0].length; for (var y = 0; y < height; ++y) { for (var x = 0; x < width; ++x) { var address = simpleCellAddress(targetLeftCorner.sheet, targetLeftCorner.col + x, targetLeftCorner.row + y); this.operations.restoreCell(address, newContent[y][x]); } } } }, { key: "redoSetCellContents", value: function redoSetCellContents(operation) { var _iterator10 = _createForOfIteratorHelper(operation.cellContents), _step10; try { for (_iterator10.s(); !(_step10 = _iterator10.n()).done;) { var cellContentData = _step10.value; this.operations.setCellContent(cellContentData.address, cellContentData.newContent); } } catch (err) { _iterator10.e(err); } finally { _iterator10.f(); } } }, { key: "redoAddRows", value: function redoAddRows(operation) { this.operations.addRows(operation.command); } }, { key: "redoAddColumns", value: function redoAddColumns(operation) { this.operations.addColumns(operation.command); } }, { key: "redoRemoveSheet", value: function redoRemoveSheet(operation) { this.operations.removeSheetByName(operation.sheetName); } }, { key: "redoAddSheet", value: function redoAddSheet(operation) { this.operations.addSheet(operation.sheetName); } }, { key: "redoRenameSheet", value: function redoRenameSheet(operation) { this.operations.renameSheet(operation.sheetId, operation.newName); } }, { key: "redoMoveRows", value: function redoMoveRows(operation) { this.operations.moveRows(operation.sheet, operation.startRow, operation.numberOfRows, operation.targetRow); } }, { key: "redoMoveColumns", value: function redoMoveColumns(operation) { this.operations.moveColumns(operation.sheet, operation.startColumn, operation.numberOfColumns, operation.targetColumn); } }, { key: "redoClearSheet", value: function redoClearSheet(operation) { this.operations.clearSheet(operation.sheetId); } }, { key: "redoSetSheetContent", value: function redoSetSheetContent(operation) { var sheetId = operation.sheetId, newSheetContent = operation.newSheetContent; this.operations.setSheetContent(sheetId, newSheetContent); } }, { key: "redoAddNamedExpression", value: function redoAddNamedExpression(operation) { this.operations.addNamedExpression(operation.name, operation.newContent, operation.scope, operation.options); } }, { key: "redoRemoveNamedExpression", value: function redoRemoveNamedExpression(operation) { this.operations.removeNamedExpression(operation.namedExpression.displayName, operation.scope); } }, { key: "redoChangeNamedExpression", value: function redoChangeNamedExpression(operation) { this.operations.changeNamedExpressionExpression(operation.namedExpression.displayName, operation.newContent, operation.scope, operation.options); } }, { key: "redoSetRowOrder", value: function redoSetRowOrder(operation) { this.operations.setRowOrder(operation.sheetId, operation.rowMapping); } }, { key: "redoSetColumnOrder", value: function redoSetColumnOrder(operation) { this.operations.setColumnOrder(operation.sheetId, operation.columnMapping); } }, { key: "restoreOldDataFromVersion", value: function restoreOldDataFromVersion(version) { var oldDataToRestore = this.oldData.get(version) || []; var _iterator11 = _createForOfIteratorHelper(oldDataToRestore), _step11; try { for (_iterator11.s(); !(_step11 = _iterator11.n()).done;) { var entryToRestore = _step11.value; var _entryToRestore = _slicedToArray(entryToRestore, 2), address = _entryToRestore[0], hash = _entryToRestore[1]; this.operations.setFormulaToCellFromCache(hash, address); } } catch (err) { _iterator11.e(err); } finally { _iterator11.f(); } } }]); return UndoRedo; }();