UNPKG

hyperformula-dc

Version:

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

184 lines (147 loc) 6.02 kB
"use strict"; exports.__esModule = true; exports.ClipboardOperations = exports.ClipboardCellType = void 0; require("regenerator-runtime/runtime.js"); var _AbsoluteCellRange = require("./AbsoluteCellRange"); var _Cell = require("./Cell"); var _errors = require("./errors"); function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } 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; } var ClipboardOperationType; (function (ClipboardOperationType) { ClipboardOperationType[ClipboardOperationType["COPY"] = 0] = "COPY"; ClipboardOperationType[ClipboardOperationType["CUT"] = 1] = "CUT"; })(ClipboardOperationType || (ClipboardOperationType = {})); var ClipboardCellType; exports.ClipboardCellType = ClipboardCellType; (function (ClipboardCellType) { ClipboardCellType[ClipboardCellType["VALUE"] = 0] = "VALUE"; ClipboardCellType[ClipboardCellType["EMPTY"] = 1] = "EMPTY"; ClipboardCellType[ClipboardCellType["FORMULA"] = 2] = "FORMULA"; ClipboardCellType[ClipboardCellType["PARSING_ERROR"] = 3] = "PARSING_ERROR"; })(ClipboardCellType || (exports.ClipboardCellType = ClipboardCellType = {})); var Clipboard = /*#__PURE__*/function () { function Clipboard(sourceLeftCorner, width, height, type, content) { _classCallCheck(this, Clipboard); this.sourceLeftCorner = sourceLeftCorner; this.width = width; this.height = height; this.type = type; this.content = content; } _createClass(Clipboard, [{ key: "getContent", value: /*#__PURE__*/regeneratorRuntime.mark(function getContent(leftCorner) { var y, x; return regeneratorRuntime.wrap(function getContent$(_context) { while (1) { switch (_context.prev = _context.next) { case 0: if (!(this.content === undefined)) { _context.next = 4; break; } return _context.abrupt("return"); case 4: y = 0; case 5: if (!(y < this.height)) { _context.next = 16; break; } x = 0; case 7: if (!(x < this.width)) { _context.next = 13; break; } _context.next = 10; return [(0, _Cell.simpleCellAddress)(leftCorner.sheet, leftCorner.col + x, leftCorner.row + y), this.content[y][x]]; case 10: ++x; _context.next = 7; break; case 13: ++y; _context.next = 5; break; case 16: case "end": return _context.stop(); } } }, getContent, this); }) }]); return Clipboard; }(); var ClipboardOperations = /*#__PURE__*/function () { function ClipboardOperations(config, dependencyGraph, operations) { _classCallCheck(this, ClipboardOperations); this.dependencyGraph = dependencyGraph; this.operations = operations; this.maxRows = config.maxRows; this.maxColumns = config.maxColumns; } _createClass(ClipboardOperations, [{ key: "cut", value: function cut(leftCorner, width, height) { this.clipboard = new Clipboard(leftCorner, width, height, ClipboardOperationType.CUT); } }, { key: "copy", value: function copy(leftCorner, width, height) { var content = []; for (var y = 0; y < height; ++y) { content[y] = []; for (var x = 0; x < width; ++x) { var clipboardCell = this.operations.getClipboardCell((0, _Cell.simpleCellAddress)(leftCorner.sheet, leftCorner.col + x, leftCorner.row + y)); content[y].push(clipboardCell); } } this.clipboard = new Clipboard(leftCorner, width, height, ClipboardOperationType.COPY, content); } }, { key: "abortCut", value: function abortCut() { if (this.clipboard && this.clipboard.type === ClipboardOperationType.CUT) { this.clear(); } } }, { key: "clear", value: function clear() { this.clipboard = undefined; } }, { key: "ensureItIsPossibleToCopyPaste", value: function ensureItIsPossibleToCopyPaste(destinationLeftCorner) { if (this.clipboard === undefined) { return; } if ((0, _Cell.invalidSimpleCellAddress)(destinationLeftCorner) || !this.dependencyGraph.sheetMapping.hasSheetWithId(destinationLeftCorner.sheet)) { throw new _errors.InvalidArgumentsError('a valid target address.'); } var targetRange = _AbsoluteCellRange.AbsoluteCellRange.spanFrom(destinationLeftCorner, this.clipboard.width, this.clipboard.height); if (targetRange.exceedsSheetSizeLimits(this.maxColumns, this.maxRows)) { throw new _errors.SheetSizeLimitExceededError(); } if (this.dependencyGraph.arrayMapping.isFormulaArrayInRange(targetRange)) { throw new Error('It is not possible to paste onto an array'); } } }, { key: "isCutClipboard", value: function isCutClipboard() { return this.clipboard !== undefined && this.clipboard.type === ClipboardOperationType.CUT; } }, { key: "isCopyClipboard", value: function isCopyClipboard() { return this.clipboard !== undefined && this.clipboard.type === ClipboardOperationType.COPY; } }]); return ClipboardOperations; }(); exports.ClipboardOperations = ClipboardOperations;