copyPaste.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. 'use strict';
  2. exports.__esModule = true;
  3. var _copyPaste = require('./../../../lib/copyPaste/copyPaste');
  4. var _copyPaste2 = _interopRequireDefault(_copyPaste);
  5. var _SheetClip = require('./../../../lib/SheetClip/SheetClip');
  6. var _SheetClip2 = _interopRequireDefault(_SheetClip);
  7. var _pluginHooks = require('./../../pluginHooks');
  8. var _pluginHooks2 = _interopRequireDefault(_pluginHooks);
  9. var _unicode = require('./../../helpers/unicode');
  10. var _array = require('./../../helpers/array');
  11. var _number = require('./../../helpers/number');
  12. var _event = require('./../../helpers/dom/event');
  13. var _element = require('./../../helpers/dom/element');
  14. var _src = require('./../../3rdparty/walkontable/src');
  15. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  16. _pluginHooks2.default.getSingleton().register('afterCopyLimit');
  17. _pluginHooks2.default.getSingleton().register('modifyCopyableRange');
  18. _pluginHooks2.default.getSingleton().register('beforeCut');
  19. _pluginHooks2.default.getSingleton().register('afterCut');
  20. _pluginHooks2.default.getSingleton().register('beforePaste');
  21. _pluginHooks2.default.getSingleton().register('afterPaste');
  22. _pluginHooks2.default.getSingleton().register('beforeCopy');
  23. _pluginHooks2.default.getSingleton().register('afterCopy');
  24. /**
  25. * @description
  26. * This plugin enables the copy/paste functionality in Handsontable.
  27. *
  28. * @example
  29. * ```js
  30. * ...
  31. * copyPaste: true,
  32. * ...
  33. * ```
  34. * @class CopyPaste
  35. * @plugin CopyPaste
  36. */
  37. function CopyPastePlugin(instance) {
  38. var _this = this;
  39. this.copyPasteInstance = (0, _copyPaste2.default)();
  40. this.copyPasteInstance.onCut(onCut);
  41. this.copyPasteInstance.triggerCopy = callCopyAction;
  42. this.copyPasteInstance.onPaste(onPaste);
  43. this.onPaste = onPaste; // for paste testing purposes
  44. this.copyableRanges = [];
  45. instance.addHook('beforeKeyDown', onBeforeKeyDown);
  46. function onCut() {
  47. instance.isListening();
  48. }
  49. function callCutAction() {
  50. var rangedData = _this.getRangedData(_this.copyableRanges);
  51. if (instance.getSettings().fragmentSelection && _SheetClip2.default.stringify(rangedData) != (0, _element.getSelectionText)()) {
  52. return;
  53. }
  54. var allowCuttingOut = !!instance.runHooks('beforeCut', rangedData, _this.copyableRanges);
  55. if (allowCuttingOut) {
  56. instance.copyPaste.copyPasteInstance.copyable(_SheetClip2.default.stringify(rangedData));
  57. instance.selection.empty();
  58. instance.runHooks('afterCut', rangedData, _this.copyableRanges);
  59. } else {
  60. instance.copyPaste.copyPasteInstance.copyable('');
  61. }
  62. }
  63. function callCopyAction() {
  64. if (!instance.isListening()) {
  65. return;
  66. }
  67. var rangedData = _this.getRangedData(_this.copyableRanges);
  68. if (instance.getSettings().fragmentSelection && _SheetClip2.default.stringify(rangedData) != (0, _element.getSelectionText)()) {
  69. return;
  70. }
  71. var allowCopying = !!instance.runHooks('beforeCopy', rangedData, _this.copyableRanges);
  72. if (allowCopying) {
  73. instance.copyPaste.copyPasteInstance.copyable(_SheetClip2.default.stringify(rangedData));
  74. instance.runHooks('afterCopy', rangedData, _this.copyableRanges);
  75. } else {
  76. instance.copyPaste.copyPasteInstance.copyable('');
  77. }
  78. }
  79. function onPaste(str) {
  80. var input, inputArray, selected, coordsFrom, coordsTo, cellRange, topLeftCorner, bottomRightCorner, areaStart, areaEnd;
  81. if (!instance.isListening() || !instance.selection.isSelected()) {
  82. return;
  83. }
  84. input = str;
  85. inputArray = _SheetClip2.default.parse(input);
  86. selected = instance.getSelected();
  87. coordsFrom = new _src.CellCoords(selected[0], selected[1]);
  88. coordsTo = new _src.CellCoords(selected[2], selected[3]);
  89. cellRange = new _src.CellRange(coordsFrom, coordsFrom, coordsTo);
  90. topLeftCorner = cellRange.getTopLeftCorner();
  91. bottomRightCorner = cellRange.getBottomRightCorner();
  92. areaStart = topLeftCorner;
  93. areaEnd = new _src.CellCoords(Math.max(bottomRightCorner.row, inputArray.length - 1 + topLeftCorner.row), Math.max(bottomRightCorner.col, inputArray[0].length - 1 + topLeftCorner.col));
  94. var isSelRowAreaCoverInputValue = coordsTo.row - coordsFrom.row >= inputArray.length - 1;
  95. var isSelColAreaCoverInputValue = coordsTo.col - coordsFrom.col >= inputArray[0].length - 1;
  96. instance.addHookOnce('afterChange', function (changes, source) {
  97. var changesLength = changes ? changes.length : 0;
  98. if (changesLength) {
  99. var offset = { row: 0, col: 0 };
  100. var highestColumnIndex = -1;
  101. (0, _array.arrayEach)(changes, function (change, index) {
  102. var nextChange = changesLength > index + 1 ? changes[index + 1] : null;
  103. if (nextChange) {
  104. if (!isSelRowAreaCoverInputValue) {
  105. offset.row += Math.max(nextChange[0] - change[0] - 1, 0);
  106. }
  107. if (!isSelColAreaCoverInputValue && change[1] > highestColumnIndex) {
  108. highestColumnIndex = change[1];
  109. offset.col += Math.max(nextChange[1] - change[1] - 1, 0);
  110. }
  111. }
  112. });
  113. instance.selectCell(areaStart.row, areaStart.col, areaEnd.row + offset.row, areaEnd.col + offset.col);
  114. }
  115. });
  116. var allowPasting = !!instance.runHooks('beforePaste', inputArray, _this.copyableRanges);
  117. if (allowPasting) {
  118. instance.populateFromArray(areaStart.row, areaStart.col, inputArray, areaEnd.row, areaEnd.col, 'CopyPaste.paste', instance.getSettings().pasteMode);
  119. instance.runHooks('afterPaste', inputArray, _this.copyableRanges);
  120. }
  121. }
  122. function onBeforeKeyDown(event) {
  123. if (!instance.getSelected()) {
  124. return;
  125. }
  126. if (instance.getActiveEditor() && instance.getActiveEditor().isOpened()) {
  127. return;
  128. }
  129. if ((0, _event.isImmediatePropagationStopped)(event)) {
  130. return;
  131. }
  132. if ((0, _unicode.isCtrlKey)(event.keyCode)) {
  133. // When fragmentSelection is enabled and some text is selected then don't blur selection calling 'setCopyableText'
  134. if (instance.getSettings().fragmentSelection && (0, _element.getSelectionText)()) {
  135. return;
  136. }
  137. // when CTRL is pressed, prepare selectable text in textarea
  138. _this.setCopyableText();
  139. (0, _event.stopImmediatePropagation)(event);
  140. return;
  141. }
  142. // catch CTRL but not right ALT (which in some systems triggers ALT+CTRL)
  143. var ctrlDown = (event.ctrlKey || event.metaKey) && !event.altKey;
  144. if (ctrlDown) {
  145. if (event.keyCode == _unicode.KEY_CODES.A) {
  146. instance._registerTimeout(setTimeout(_this.setCopyableText.bind(_this), 0));
  147. }
  148. if (event.keyCode == _unicode.KEY_CODES.X) {
  149. callCutAction();
  150. }
  151. if (event.keyCode == _unicode.KEY_CODES.C) {
  152. callCopyAction();
  153. }
  154. }
  155. }
  156. /**
  157. * Destroy plugin instance.
  158. *
  159. * @function destroy
  160. * @memberof CopyPaste#
  161. */
  162. this.destroy = function () {
  163. if (this.copyPasteInstance) {
  164. this.copyPasteInstance.removeCallback(onCut);
  165. this.copyPasteInstance.removeCallback(onPaste);
  166. this.copyPasteInstance.destroy();
  167. this.copyPasteInstance = null;
  168. }
  169. instance.removeHook('beforeKeyDown', onBeforeKeyDown);
  170. };
  171. instance.addHook('afterDestroy', this.destroy.bind(this));
  172. /**
  173. * @function triggerPaste
  174. * @memberof CopyPaste#
  175. */
  176. this.triggerPaste = this.copyPasteInstance.triggerPaste.bind(this.copyPasteInstance);
  177. /**
  178. * @function triggerCut
  179. * @memberof CopyPaste#
  180. */
  181. this.triggerCut = this.copyPasteInstance.triggerCut.bind(this.copyPasteInstance);
  182. /**
  183. * Prepares copyable text in the invisible textarea.
  184. *
  185. * @function setCopyable
  186. * @memberof CopyPaste#
  187. */
  188. this.setCopyableText = function () {
  189. var settings = instance.getSettings();
  190. var copyRowsLimit = settings.copyRowsLimit;
  191. var copyColsLimit = settings.copyColsLimit;
  192. var selRange = instance.getSelectedRange();
  193. var topLeft = selRange.getTopLeftCorner();
  194. var bottomRight = selRange.getBottomRightCorner();
  195. var startRow = topLeft.row;
  196. var startCol = topLeft.col;
  197. var endRow = bottomRight.row;
  198. var endCol = bottomRight.col;
  199. var finalEndRow = Math.min(endRow, startRow + copyRowsLimit - 1);
  200. var finalEndCol = Math.min(endCol, startCol + copyColsLimit - 1);
  201. this.copyableRanges.length = 0;
  202. this.copyableRanges.push({
  203. startRow: startRow,
  204. startCol: startCol,
  205. endRow: finalEndRow,
  206. endCol: finalEndCol
  207. });
  208. this.copyableRanges = instance.runHooks('modifyCopyableRange', this.copyableRanges);
  209. var copyableData = this.getRangedCopyableData(this.copyableRanges);
  210. instance.copyPaste.copyPasteInstance.copyable(copyableData);
  211. if (endRow !== finalEndRow || endCol !== finalEndCol) {
  212. instance.runHooks('afterCopyLimit', endRow - startRow + 1, endCol - startCol + 1, copyRowsLimit, copyColsLimit);
  213. }
  214. };
  215. /**
  216. * Create copyable text releated to range objects.
  217. *
  218. * @since 0.19.0
  219. * @param {Array} ranges Array of Objects with properties `startRow`, `endRow`, `startCol` and `endCol`.
  220. * @returns {String} Returns string which will be copied into clipboard.
  221. */
  222. this.getRangedCopyableData = function (ranges) {
  223. var dataSet = [];
  224. var copyableRows = [];
  225. var copyableColumns = [];
  226. // Count all copyable rows and columns
  227. (0, _array.arrayEach)(ranges, function (range) {
  228. (0, _number.rangeEach)(range.startRow, range.endRow, function (row) {
  229. if (copyableRows.indexOf(row) === -1) {
  230. copyableRows.push(row);
  231. }
  232. });
  233. (0, _number.rangeEach)(range.startCol, range.endCol, function (column) {
  234. if (copyableColumns.indexOf(column) === -1) {
  235. copyableColumns.push(column);
  236. }
  237. });
  238. });
  239. // Concat all rows and columns data defined in ranges into one copyable string
  240. (0, _array.arrayEach)(copyableRows, function (row) {
  241. var rowSet = [];
  242. (0, _array.arrayEach)(copyableColumns, function (column) {
  243. rowSet.push(instance.getCopyableData(row, column));
  244. });
  245. dataSet.push(rowSet);
  246. });
  247. return _SheetClip2.default.stringify(dataSet);
  248. };
  249. /**
  250. * Create copyable text releated to range objects.
  251. *
  252. * @since 0.31.1
  253. * @param {Array} ranges Array of Objects with properties `startRow`, `startCol`, `endRow` and `endCol`.
  254. * @returns {Array} Returns array of arrays which will be copied into clipboard.
  255. */
  256. this.getRangedData = function (ranges) {
  257. var dataSet = [];
  258. var copyableRows = [];
  259. var copyableColumns = [];
  260. // Count all copyable rows and columns
  261. (0, _array.arrayEach)(ranges, function (range) {
  262. (0, _number.rangeEach)(range.startRow, range.endRow, function (row) {
  263. if (copyableRows.indexOf(row) === -1) {
  264. copyableRows.push(row);
  265. }
  266. });
  267. (0, _number.rangeEach)(range.startCol, range.endCol, function (column) {
  268. if (copyableColumns.indexOf(column) === -1) {
  269. copyableColumns.push(column);
  270. }
  271. });
  272. });
  273. // Concat all rows and columns data defined in ranges into one copyable string
  274. (0, _array.arrayEach)(copyableRows, function (row) {
  275. var rowSet = [];
  276. (0, _array.arrayEach)(copyableColumns, function (column) {
  277. rowSet.push(instance.getCopyableData(row, column));
  278. });
  279. dataSet.push(rowSet);
  280. });
  281. return dataSet;
  282. };
  283. }
  284. /**
  285. * Init plugin.
  286. *
  287. * @function init
  288. * @memberof CopyPaste#
  289. */
  290. function init() {
  291. var instance = this,
  292. pluginEnabled = instance.getSettings().copyPaste !== false;
  293. if (pluginEnabled && !instance.copyPaste) {
  294. /**
  295. * Instance of CopyPaste Plugin {@link Handsontable.CopyPaste}
  296. *
  297. * @alias copyPaste
  298. * @memberof! Handsontable.Core#
  299. * @type {CopyPaste}
  300. */
  301. instance.copyPaste = new CopyPastePlugin(instance);
  302. } else if (!pluginEnabled && instance.copyPaste) {
  303. instance.copyPaste.destroy();
  304. instance.copyPaste = null;
  305. }
  306. }
  307. _pluginHooks2.default.getSingleton().add('afterInit', init);
  308. _pluginHooks2.default.getSingleton().add('afterUpdateSettings', init);
  309. exports.default = CopyPastePlugin;