tableediting.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  6. import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
  7. import { getData as getModelData, setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  8. import { getCode } from '@ckeditor/ckeditor5-utils/src/keyboard';
  9. import TableEditing from '../src/tableediting';
  10. import { formatTable, formattedModelTable, modelTable } from './_utils/utils';
  11. import InsertRowCommand from '../src/commands/insertrowcommand';
  12. import InsertTableCommand from '../src/commands/inserttablecommand';
  13. import InsertColumnCommand from '../src/commands/insertcolumncommand';
  14. import RemoveRowCommand from '../src/commands/removerowcommand';
  15. import RemoveColumnCommand from '../src/commands/removecolumncommand';
  16. import SplitCellCommand from '../src/commands/splitcellcommand';
  17. import MergeCellCommand from '../src/commands/mergecellcommand';
  18. import SetHeaderRowCommand from '../src/commands/setheaderrowcommand';
  19. import SetHeaderColumnCommand from '../src/commands/setheadercolumncommand';
  20. describe( 'TableEditing', () => {
  21. let editor, model;
  22. beforeEach( () => {
  23. return VirtualTestEditor
  24. .create( {
  25. plugins: [ TableEditing, Paragraph ]
  26. } )
  27. .then( newEditor => {
  28. editor = newEditor;
  29. model = editor.model;
  30. } );
  31. } );
  32. afterEach( () => {
  33. editor.destroy();
  34. } );
  35. it( 'should set proper schema rules', () => {
  36. } );
  37. it( 'adds insertTable command', () => {
  38. expect( editor.commands.get( 'insertTable' ) ).to.be.instanceOf( InsertTableCommand );
  39. } );
  40. it( 'adds insertRowAbove command', () => {
  41. expect( editor.commands.get( 'insertRowAbove' ) ).to.be.instanceOf( InsertRowCommand );
  42. } );
  43. it( 'adds insertRowBelow command', () => {
  44. expect( editor.commands.get( 'insertRowBelow' ) ).to.be.instanceOf( InsertRowCommand );
  45. } );
  46. it( 'adds insertColumnBefore command', () => {
  47. expect( editor.commands.get( 'insertColumnBefore' ) ).to.be.instanceOf( InsertColumnCommand );
  48. } );
  49. it( 'adds insertColumnAfter command', () => {
  50. expect( editor.commands.get( 'insertColumnAfter' ) ).to.be.instanceOf( InsertColumnCommand );
  51. } );
  52. it( 'adds removeRow command', () => {
  53. expect( editor.commands.get( 'removeRow' ) ).to.be.instanceOf( RemoveRowCommand );
  54. } );
  55. it( 'adds removeColumn command', () => {
  56. expect( editor.commands.get( 'removeColumn' ) ).to.be.instanceOf( RemoveColumnCommand );
  57. } );
  58. it( 'adds splitCellVertically command', () => {
  59. expect( editor.commands.get( 'splitCellVertically' ) ).to.be.instanceOf( SplitCellCommand );
  60. } );
  61. it( 'adds splitCellHorizontally command', () => {
  62. expect( editor.commands.get( 'splitCellHorizontally' ) ).to.be.instanceOf( SplitCellCommand );
  63. } );
  64. it( 'adds mergeCellRight command', () => {
  65. expect( editor.commands.get( 'mergeCellRight' ) ).to.be.instanceOf( MergeCellCommand );
  66. } );
  67. it( 'adds mergeCellLeft command', () => {
  68. expect( editor.commands.get( 'mergeCellLeft' ) ).to.be.instanceOf( MergeCellCommand );
  69. } );
  70. it( 'adds mergeCellDown command', () => {
  71. expect( editor.commands.get( 'mergeCellDown' ) ).to.be.instanceOf( MergeCellCommand );
  72. } );
  73. it( 'adds mergeCellUp command', () => {
  74. expect( editor.commands.get( 'mergeCellUp' ) ).to.be.instanceOf( MergeCellCommand );
  75. } );
  76. it( 'adds setColumnHeader command', () => {
  77. expect( editor.commands.get( 'setColumnHeader' ) ).to.be.instanceOf( SetHeaderColumnCommand );
  78. } );
  79. it( 'adds setRowHeader command', () => {
  80. expect( editor.commands.get( 'setRowHeader' ) ).to.be.instanceOf( SetHeaderRowCommand );
  81. } );
  82. describe( 'conversion in data pipeline', () => {
  83. describe( 'model to view', () => {
  84. it( 'should create tbody section', () => {
  85. setModelData( model, '<table><tableRow><tableCell>foo[]</tableCell></tableRow></table>' );
  86. expect( editor.getData() ).to.equal(
  87. '<table>' +
  88. '<tbody>' +
  89. '<tr><td>foo</td></tr>' +
  90. '</tbody>' +
  91. '</table>'
  92. );
  93. } );
  94. it( 'should create thead section', () => {
  95. setModelData( model, '<table headingRows="1"><tableRow><tableCell>foo[]</tableCell></tableRow></table>' );
  96. expect( editor.getData() ).to.equal(
  97. '<table>' +
  98. '<thead>' +
  99. '<tr><th>foo</th></tr>' +
  100. '</thead>' +
  101. '</table>'
  102. );
  103. } );
  104. } );
  105. describe( 'view to model', () => {
  106. it( 'should convert table', () => {
  107. editor.setData( '<table><tbody><tr><td>foo</td></tr></tbody></table>' );
  108. expect( getModelData( model, { withoutSelection: true } ) )
  109. .to.equal( '<table><tableRow><tableCell>foo</tableCell></tableRow></table>' );
  110. } );
  111. } );
  112. } );
  113. describe( 'caret movement', () => {
  114. let domEvtDataStub;
  115. beforeEach( () => {
  116. domEvtDataStub = {
  117. keyCode: getCode( 'Tab' ),
  118. preventDefault: sinon.spy(),
  119. stopPropagation: sinon.spy()
  120. };
  121. } );
  122. it( 'should do nothing if not tab pressed', () => {
  123. setModelData( model, modelTable( [
  124. [ '11', '12[]' ]
  125. ] ) );
  126. domEvtDataStub.keyCode = getCode( 'a' );
  127. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  128. sinon.assert.notCalled( domEvtDataStub.preventDefault );
  129. sinon.assert.notCalled( domEvtDataStub.stopPropagation );
  130. expect( formatTable( getModelData( model ) ) ).to.equal( formattedModelTable( [
  131. [ '11', '12[]' ]
  132. ] ) );
  133. } );
  134. it( 'should do nothing if Ctrl+Tab is pressed', () => {
  135. setModelData( model, modelTable( [
  136. [ '11', '12[]' ]
  137. ] ) );
  138. domEvtDataStub.ctrlKey = true;
  139. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  140. sinon.assert.notCalled( domEvtDataStub.preventDefault );
  141. sinon.assert.notCalled( domEvtDataStub.stopPropagation );
  142. expect( formatTable( getModelData( model ) ) ).to.equal( formattedModelTable( [
  143. [ '11', '12[]' ]
  144. ] ) );
  145. } );
  146. describe( 'on TAB', () => {
  147. it( 'should do nothing if selection is not in a table', () => {
  148. setModelData( model, '[]' + modelTable( [
  149. [ '11', '12' ]
  150. ] ) );
  151. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  152. sinon.assert.notCalled( domEvtDataStub.preventDefault );
  153. sinon.assert.notCalled( domEvtDataStub.stopPropagation );
  154. expect( formatTable( getModelData( model ) ) ).to.equal( '[]' + formattedModelTable( [
  155. [ '11', '12' ]
  156. ] ) );
  157. } );
  158. it( 'should move to next cell', () => {
  159. setModelData( model, modelTable( [
  160. [ '11[]', '12' ]
  161. ] ) );
  162. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  163. sinon.assert.calledOnce( domEvtDataStub.preventDefault );
  164. sinon.assert.calledOnce( domEvtDataStub.stopPropagation );
  165. expect( formatTable( getModelData( model ) ) ).to.equal( formattedModelTable( [
  166. [ '11', '[12]' ]
  167. ] ) );
  168. } );
  169. it( 'should create another row and move to first cell in new row', () => {
  170. setModelData( model, modelTable( [
  171. [ '11', '[12]' ]
  172. ] ) );
  173. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  174. expect( formatTable( getModelData( model ) ) ).to.equal( formattedModelTable( [
  175. [ '11', '12' ],
  176. [ '[]', '' ]
  177. ] ) );
  178. } );
  179. it( 'should move to the first cell of next row if on end of a row', () => {
  180. setModelData( model, modelTable( [
  181. [ '11', '12[]' ],
  182. [ '21', '22' ]
  183. ] ) );
  184. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  185. expect( formatTable( getModelData( model ) ) ).to.equal( formattedModelTable( [
  186. [ '11', '12' ],
  187. [ '[21]', '22' ]
  188. ] ) );
  189. } );
  190. describe( 'on table widget selected', () => {
  191. it( 'should move caret to the first table cell on TAB', () => {
  192. const spy = sinon.spy();
  193. editor.editing.view.document.on( 'keydown', spy );
  194. setModelData( model, '[' + modelTable( [
  195. [ '11', '12' ]
  196. ] ) + ']' );
  197. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  198. sinon.assert.calledOnce( domEvtDataStub.preventDefault );
  199. sinon.assert.calledOnce( domEvtDataStub.stopPropagation );
  200. expect( formatTable( getModelData( model ) ) ).to.equal( formattedModelTable( [
  201. [ '[11]', '12' ]
  202. ] ) );
  203. // Should cancel event - so no other tab handler is called.
  204. sinon.assert.notCalled( spy );
  205. } );
  206. it( 'shouldn\' do anything on other blocks', () => {
  207. const spy = sinon.spy();
  208. editor.editing.view.document.on( 'keydown', spy );
  209. setModelData( model, '[<paragraph>foo</paragraph>]' );
  210. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  211. sinon.assert.notCalled( domEvtDataStub.preventDefault );
  212. sinon.assert.notCalled( domEvtDataStub.stopPropagation );
  213. expect( formatTable( getModelData( model ) ) ).to.equal( '[<paragraph>foo</paragraph>]' );
  214. // Should not cancel event.
  215. sinon.assert.calledOnce( spy );
  216. } );
  217. } );
  218. } );
  219. describe( 'on SHIFT+TAB', () => {
  220. beforeEach( () => {
  221. domEvtDataStub.shiftKey = true;
  222. } );
  223. it( 'should do nothing if selection is not in a table', () => {
  224. setModelData( model, '[]' + modelTable( [
  225. [ '11', '12' ]
  226. ] ) );
  227. domEvtDataStub.keyCode = getCode( 'Tab' );
  228. domEvtDataStub.shiftKey = true;
  229. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  230. sinon.assert.notCalled( domEvtDataStub.preventDefault );
  231. sinon.assert.notCalled( domEvtDataStub.stopPropagation );
  232. expect( formatTable( getModelData( model ) ) ).to.equal( '[]' + formattedModelTable( [
  233. [ '11', '12' ]
  234. ] ) );
  235. } );
  236. it( 'should move to previous cell', () => {
  237. setModelData( model, modelTable( [
  238. [ '11', '12[]' ]
  239. ] ) );
  240. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  241. sinon.assert.calledOnce( domEvtDataStub.preventDefault );
  242. sinon.assert.calledOnce( domEvtDataStub.stopPropagation );
  243. expect( formatTable( getModelData( model ) ) ).to.equal( formattedModelTable( [
  244. [ '[11]', '12' ]
  245. ] ) );
  246. } );
  247. it( 'should not move if caret is in first table cell', () => {
  248. setModelData( model, '<paragraph>foo</paragraph>' + modelTable( [
  249. [ '[]11', '12' ]
  250. ] ) );
  251. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  252. expect( formatTable( getModelData( model ) ) ).to.equal(
  253. '<paragraph>foo</paragraph>' + formattedModelTable( [ [ '[]11', '12' ] ] )
  254. );
  255. } );
  256. it( 'should move to the last cell of previous row if on beginning of a row', () => {
  257. setModelData( model, modelTable( [
  258. [ '11', '12' ],
  259. [ '[]21', '22' ]
  260. ] ) );
  261. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  262. expect( formatTable( getModelData( model ) ) ).to.equal( formattedModelTable( [
  263. [ '11', '[12]' ],
  264. [ '21', '22' ]
  265. ] ) );
  266. } );
  267. } );
  268. } );
  269. } );