1c7b1ea75f23bc3df7c7655290a1f322e6cc440d26ee8fbca03531209f13933401d372c37197011e969c33d35cb104326ab15bafbc9cb2a6cd5a6fde65d558 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. describe('CopyPaste', () => {
  2. var id = 'testContainer';
  3. beforeEach(function() {
  4. this.$container = $(`<div id="${id}"></div>`).appendTo('body');
  5. });
  6. afterEach(function() {
  7. if (this.$container) {
  8. destroy();
  9. this.$container.remove();
  10. }
  11. });
  12. it('should remove additional new line from copied text (only safari)', () => {
  13. var getData = jasmine.createSpy().and.returnValue('a\nb\n\n');
  14. var preventDefault = jasmine.createSpy();
  15. var hot = handsontable();
  16. $('.copyPaste')[0].onpaste(
  17. {clipboardData: {getData},
  18. preventDefault
  19. });
  20. if (Handsontable.helper.isSafari()) {
  21. expect($('.copyPaste')[0].value).toEqual('a\nb\n');
  22. expect(getData).toHaveBeenCalledWith('Text');
  23. expect(preventDefault).toHaveBeenCalled();
  24. } else if (Handsontable.helper.isChrome()) {
  25. expect($('.copyPaste')[0].value).toBe('a\nb\n\n');
  26. expect(getData).toHaveBeenCalledWith('Text');
  27. expect(preventDefault).toHaveBeenCalled();
  28. }
  29. });
  30. it('should allow blocking cutting cells by stopping the immediate propagation', (done) => {
  31. var onCut = jasmine.createSpy();
  32. var hot = handsontable({
  33. data: [
  34. ['2012', 10, 11, 12, 13, 15, 16],
  35. ['2013', 10, 11, 12, 13, 15, 16]
  36. ],
  37. beforeKeyDown(event) {
  38. if (event.ctrlKey && event.keyCode === Handsontable.helper.KEY_CODES.X) {
  39. event.isImmediatePropagationEnabled = false;
  40. }
  41. }
  42. });
  43. hot.copyPaste.copyPasteInstance.cutCallbacks.push(onCut);
  44. selectCell(0, 0);
  45. keyDown('ctrl+x');
  46. setTimeout(() => {
  47. expect(onCut).not.toHaveBeenCalled();
  48. done();
  49. }, 100);
  50. });
  51. describe('enabling/disabing plugin', () => {
  52. it('should enable copyPaste by default', () => {
  53. var hot = handsontable();
  54. expect(hot.copyPaste).toBeDefined();
  55. });
  56. it('should create copyPaste div if enabled', () => {
  57. expect($('#CopyPasteDiv').length).toEqual(0);
  58. var hot = handsontable();
  59. selectCell(0, 0);
  60. keyDownUp(Handsontable.helper.KEY_CODES.CONTROL_LEFT); // copyPaste div isn't created until you click CTRL
  61. expect($('#CopyPasteDiv').length).toEqual(1);
  62. });
  63. it('should not create copyPaste div if disabled', () => {
  64. expect($('#CopyPasteDiv').length).toEqual(0);
  65. var hot = handsontable({
  66. copyPaste: false
  67. });
  68. selectCell(0, 0);
  69. keyDownUp(Handsontable.helper.KEY_CODES.CONTROL_LEFT);
  70. expect($('#CopyPasteDiv').length).toEqual(0);
  71. });
  72. it('should not create copyPaste property if plugin is disabled', () => {
  73. var hot = handsontable({
  74. copyPaste: false
  75. });
  76. expect(hot.copyPaste).toBeUndefined();
  77. });
  78. it('should enable/disable plugin using updateSettings', () => {
  79. var hot = handsontable();
  80. expect(hot.copyPaste).toBeDefined();
  81. updateSettings({
  82. copyPaste: false
  83. });
  84. expect(hot.copyPaste).toBe(null);
  85. });
  86. it('should remove copyPaste div if plugin has been disabled using updateSetting', () => {
  87. expect($('#CopyPasteDiv').length).toEqual(0);
  88. var hot = handsontable();
  89. selectCell(0, 0);
  90. keyDownUp(Handsontable.helper.KEY_CODES.CONTROL_LEFT);
  91. expect($('#CopyPasteDiv').length).toEqual(1);
  92. updateSettings({
  93. copyPaste: false
  94. });
  95. expect($('#CopyPasteDiv').length).toEqual(0);
  96. selectCell(0, 0);
  97. keyDownUp(Handsontable.helper.KEY_CODES.CONTROL_LEFT);
  98. expect($('#CopyPasteDiv').length).toEqual(0);
  99. });
  100. });
  101. describe('setting values copyable', () => {
  102. it('should set copyable text when selecting a single cell and hitting ctrl', () => {
  103. handsontable({
  104. data: Handsontable.helper.createSpreadsheetData(2, 2)
  105. });
  106. var copyPasteTextarea = $('textarea.copyPaste');
  107. expect(copyPasteTextarea.val().length).toEqual(0);
  108. selectCell(0, 0);
  109. keyDownUp(Handsontable.helper.KEY_CODES.CONTROL_LEFT);
  110. expect(copyPasteTextarea.val()).toEqual('A1');
  111. });
  112. it('should set copyable text when selecting a single cell and hitting left command', () => {
  113. handsontable({
  114. data: Handsontable.helper.createSpreadsheetData(2, 2)
  115. });
  116. var copyPasteTextarea = $('textarea.copyPaste');
  117. expect(copyPasteTextarea.val().length).toEqual(0);
  118. selectCell(0, 0);
  119. keyDownUp(Handsontable.helper.KEY_CODES.COMMAND_LEFT);
  120. expect(copyPasteTextarea.val()).toEqual('A1');
  121. });
  122. it('should set copyable text when selecting a single cell and hitting right command', () => {
  123. handsontable({
  124. data: Handsontable.helper.createSpreadsheetData(2, 2)
  125. });
  126. var copyPasteTextarea = $('textarea.copyPaste');
  127. expect(copyPasteTextarea.val().length).toEqual(0);
  128. selectCell(0, 0);
  129. keyDownUp(Handsontable.helper.KEY_CODES.COMMAND_RIGHT);
  130. expect(copyPasteTextarea.val()).toEqual('A1');
  131. });
  132. it('should set copyable text when selecting multiple cells and hitting ctrl', () => {
  133. handsontable({
  134. data: Handsontable.helper.createSpreadsheetData(2, 2)
  135. });
  136. var copyPasteTextarea = $('textarea.copyPaste');
  137. expect(copyPasteTextarea.val().length).toEqual(0);
  138. selectCell(0, 0, 1, 0);
  139. keyDownUp(Handsontable.helper.KEY_CODES.CONTROL_LEFT);
  140. expect(copyPasteTextarea.val()).toEqual('A1\nA2');
  141. });
  142. it('should set copyable text when selecting all cells with CTRL+A', (done) => {
  143. handsontable({
  144. data: Handsontable.helper.createSpreadsheetData(2, 2)
  145. });
  146. var copyPasteTextarea = $('textarea.copyPaste');
  147. expect(copyPasteTextarea.val().length).toEqual(0);
  148. selectCell(0, 0);
  149. $(document.activeElement).simulate('keydown', {keyCode: Handsontable.helper.KEY_CODES.A, ctrlKey: true});
  150. setTimeout(() => {
  151. expect(getSelected()).toEqual([0, 0, 1, 1]);
  152. expect(copyPasteTextarea.val()).toEqual('A1\tB1\nA2\tB2');
  153. done();
  154. }, 10);
  155. });
  156. it('should not throw error when no cell is selected (#1221)', () => {
  157. handsontable({
  158. data: Handsontable.helper.createSpreadsheetData(2, 2)
  159. });
  160. selectCell(0, 0);
  161. deselectCell();
  162. function keydownCtrl() {
  163. $(document).simulate('keydown', {
  164. keyCode: Handsontable.helper.KEY_CODES.COMMAND_LEFT
  165. });
  166. }
  167. // expect no to throw any exception
  168. expect(keydownCtrl).not.toThrow();
  169. });
  170. it('should set copyable text when selecting a single cell with specified type and hitting ctrl (#1300)', () => {
  171. handsontable({
  172. data: [['A', 1], ['B', 2]],
  173. columns: [
  174. {
  175. type: 'text'
  176. },
  177. {
  178. type: 'numeric'
  179. }
  180. ]
  181. });
  182. var copyPasteTextarea = $('textarea.copyPaste');
  183. expect(copyPasteTextarea.val().length).toEqual(0);
  184. selectCell(0, 0, 1, 1);
  185. keyDownUp(Handsontable.helper.KEY_CODES.CONTROL_LEFT);
  186. expect(copyPasteTextarea.val()).toEqual('A\t1\nB\t2');
  187. });
  188. it('should set copyable text when selecting a single cell with editor type as false (#2574)', () => {
  189. handsontable({
  190. data: [['A', 1], ['B', 2]],
  191. columns: [
  192. {
  193. type: 'text'
  194. },
  195. {
  196. editor: false
  197. }
  198. ]
  199. });
  200. var copyPasteTextarea = $('textarea.copyPaste');
  201. expect(copyPasteTextarea.val().length).toEqual(0);
  202. selectCell(1, 1, 1, 1);
  203. keyDownUp(Handsontable.helper.KEY_CODES.CONTROL_LEFT);
  204. expect(copyPasteTextarea.val()).toEqual('2');
  205. });
  206. describe('working with multiple tables', () => {
  207. beforeEach(function() {
  208. this.$container2 = $(`<div id="${id}2"></div>`).appendTo('body');
  209. });
  210. afterEach(function() {
  211. if (this.$container2) {
  212. this.$container2.handsontable('destroy');
  213. this.$container2.remove();
  214. }
  215. });
  216. it('should disable copyPaste only in particular table', function() {
  217. var hot1 = handsontable();
  218. var hot2 = this.$container2.handsontable({
  219. copyPaste: false
  220. });
  221. expect(hot1.copyPaste).toBeDefined();
  222. expect(hot2.copyPaste).toBeUndefined();
  223. });
  224. it('should create only one CopyPasteDiv regardless of the number of tables', function() {
  225. var hot1 = handsontable();
  226. var hot2 = this.$container2.handsontable();
  227. expect($('#CopyPasteDiv').length).toEqual(1);
  228. });
  229. it('should leave CopyPasteDiv as long as at least one table has copyPaste enabled', function() {
  230. var hot1 = handsontable();
  231. var hot2 = this.$container2.handsontable().handsontable('getInstance');
  232. expect($('#CopyPasteDiv').length).toEqual(1);
  233. hot1.updateSettings({
  234. copyPaste: false
  235. });
  236. expect($('#CopyPasteDiv').length).toEqual(1);
  237. hot2.updateSettings({
  238. copyPaste: false
  239. });
  240. expect($('#CopyPasteDiv').length).toEqual(0);
  241. });
  242. });
  243. });
  244. describe('hooks', function() {
  245. it('should call beforeCut and afterCut during cutting out operation', function () {
  246. var beforeCutSpy = jasmine.createSpy('beforeCut');
  247. var afterCutSpy = jasmine.createSpy('afterCut');
  248. var hot = handsontable({
  249. data: Handsontable.helper.createSpreadsheetData(2, 2),
  250. beforeCut: beforeCutSpy,
  251. afterCut: afterCutSpy
  252. });
  253. selectCell(0, 0);
  254. keyDown('ctrl');
  255. keyDown('ctrl+x');
  256. expect(beforeCutSpy.calls.count()).toEqual(1);
  257. expect(beforeCutSpy).toHaveBeenCalledWith([['A1']], [{startRow: 0, startCol: 0, endRow: 0, endCol: 0}], void 0, void 0, void 0, void 0);
  258. expect(afterCutSpy.calls.count()).toEqual(1);
  259. expect(afterCutSpy).toHaveBeenCalledWith([['A1']], [{startRow: 0, startCol: 0, endRow: 0, endCol: 0}], void 0, void 0, void 0, void 0);
  260. });
  261. it('should call beforeCopy and afterCopy during copying operation', function () {
  262. var beforeCopySpy = jasmine.createSpy('beforeCopy');
  263. var afterCopySpy = jasmine.createSpy('afterCopy');
  264. var hot = handsontable({
  265. data: Handsontable.helper.createSpreadsheetData(2, 2),
  266. beforeCopy: beforeCopySpy,
  267. afterCopy: afterCopySpy
  268. });
  269. selectCell(0, 0);
  270. keyDown('ctrl');
  271. keyDown('ctrl+c');
  272. expect(beforeCopySpy.calls.count()).toEqual(1);
  273. expect(beforeCopySpy).toHaveBeenCalledWith([['A1']], [{startRow: 0, startCol: 0, endRow: 0, endCol: 0}], void 0, void 0, void 0, void 0);
  274. expect(afterCopySpy.calls.count()).toEqual(1);
  275. expect(afterCopySpy).toHaveBeenCalledWith([['A1']], [{startRow: 0, startCol: 0, endRow: 0, endCol: 0}], void 0, void 0, void 0, void 0);
  276. });
  277. it('should call beforePaste and afterPaste during copying operation', function (done) {
  278. var beforePasteSpy = jasmine.createSpy('beforePaste');
  279. var afterPasteSpy = jasmine.createSpy('afterPaste');
  280. var hot = handsontable({
  281. data: Handsontable.helper.createSpreadsheetData(2, 2),
  282. beforePaste: beforePasteSpy,
  283. afterPaste: afterPasteSpy
  284. });
  285. selectCell(0, 0);
  286. keyDown('ctrl');
  287. hot.copyPaste.triggerPaste(null, 'Kia');
  288. setTimeout(function() {
  289. expect(beforePasteSpy.calls.count()).toEqual(1);
  290. expect(beforePasteSpy).toHaveBeenCalledWith([['Kia']], [{startRow: 0, startCol: 0, endRow: 0, endCol: 0}], void 0, void 0, void 0, void 0);
  291. expect(afterPasteSpy.calls.count()).toEqual(1);
  292. expect(afterPasteSpy).toHaveBeenCalledWith([['Kia']], [{startRow: 0, startCol: 0, endRow: 0, endCol: 0}], void 0, void 0, void 0, void 0);
  293. done();
  294. }, 60);
  295. });
  296. it('should be possible to block cutting out', function () {
  297. var afterCutSpy = jasmine.createSpy('afterCut');
  298. var hot = handsontable({
  299. data: Handsontable.helper.createSpreadsheetData(2, 2),
  300. beforeCut() {
  301. return false;
  302. },
  303. afterCut: afterCutSpy
  304. });
  305. selectCell(0, 0);
  306. keyDown('ctrl');
  307. keyDown('ctrl+x');
  308. expect(afterCutSpy.calls.count()).toEqual(0);
  309. });
  310. it('should be possible to block copying', function () {
  311. var afterCopySpy = jasmine.createSpy('afterCopy');
  312. var hot = handsontable({
  313. data: Handsontable.helper.createSpreadsheetData(2, 2),
  314. beforeCopy() {
  315. return false;
  316. },
  317. afterCopy: afterCopySpy
  318. });
  319. selectCell(0, 0);
  320. keyDown('ctrl');
  321. keyDown('ctrl+c');
  322. expect(afterCopySpy.calls.count()).toEqual(0);
  323. });
  324. it('should be possible to block pasting', function (done) {
  325. var afterPasteSpy = jasmine.createSpy('afterPaste');
  326. var hot = handsontable({
  327. data: Handsontable.helper.createSpreadsheetData(2, 2),
  328. beforePaste() {
  329. return false;
  330. },
  331. afterPaste: afterPasteSpy
  332. });
  333. selectCell(0, 0);
  334. keyDown('ctrl');
  335. hot.copyPaste.triggerPaste(null, 'Kia');
  336. setTimeout(function() {
  337. expect(afterPasteSpy.calls.count()).toEqual(0);
  338. done();
  339. }, 60);
  340. });
  341. });
  342. });