tableediting.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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( 'insertTableRowAbove' ) ).to.be.instanceOf( InsertRowCommand );
  42. } );
  43. it( 'adds insertRowBelow command', () => {
  44. expect( editor.commands.get( 'insertTableRowBelow' ) ).to.be.instanceOf( InsertRowCommand );
  45. } );
  46. it( 'adds insertColumnBefore command', () => {
  47. expect( editor.commands.get( 'insertTableColumnBefore' ) ).to.be.instanceOf( InsertColumnCommand );
  48. } );
  49. it( 'adds insertColumnAfter command', () => {
  50. expect( editor.commands.get( 'insertTableColumnAfter' ) ).to.be.instanceOf( InsertColumnCommand );
  51. } );
  52. it( 'adds removeRow command', () => {
  53. expect( editor.commands.get( 'removeTableRow' ) ).to.be.instanceOf( RemoveRowCommand );
  54. } );
  55. it( 'adds removeColumn command', () => {
  56. expect( editor.commands.get( 'removeTableColumn' ) ).to.be.instanceOf( RemoveColumnCommand );
  57. } );
  58. it( 'adds splitCellVertically command', () => {
  59. expect( editor.commands.get( 'splitTableCellVertically' ) ).to.be.instanceOf( SplitCellCommand );
  60. } );
  61. it( 'adds splitCellHorizontally command', () => {
  62. expect( editor.commands.get( 'splitTableCellHorizontally' ) ).to.be.instanceOf( SplitCellCommand );
  63. } );
  64. it( 'adds mergeCellRight command', () => {
  65. expect( editor.commands.get( 'mergeTableCellRight' ) ).to.be.instanceOf( MergeCellCommand );
  66. } );
  67. it( 'adds mergeCellLeft command', () => {
  68. expect( editor.commands.get( 'mergeTableCellLeft' ) ).to.be.instanceOf( MergeCellCommand );
  69. } );
  70. it( 'adds mergeCellDown command', () => {
  71. expect( editor.commands.get( 'mergeTableCellDown' ) ).to.be.instanceOf( MergeCellCommand );
  72. } );
  73. it( 'adds mergeCellUp command', () => {
  74. expect( editor.commands.get( 'mergeTableCellUp' ) ).to.be.instanceOf( MergeCellCommand );
  75. } );
  76. it( 'adds setColumnHeader command', () => {
  77. expect( editor.commands.get( 'setTableColumnHeader' ) ).to.be.instanceOf( SetHeaderColumnCommand );
  78. } );
  79. it( 'adds setRowHeader command', () => {
  80. expect( editor.commands.get( 'setTableRowHeader' ) ).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. '<figure class="table">' +
  88. '<table>' +
  89. '<tbody>' +
  90. '<tr><td>foo</td></tr>' +
  91. '</tbody>' +
  92. '</table>' +
  93. '</figure>'
  94. );
  95. } );
  96. it( 'should create thead section', () => {
  97. setModelData( model, '<table headingRows="1"><tableRow><tableCell>foo[]</tableCell></tableRow></table>' );
  98. expect( editor.getData() ).to.equal(
  99. '<figure class="table">' +
  100. '<table>' +
  101. '<thead>' +
  102. '<tr><th>foo</th></tr>' +
  103. '</thead>' +
  104. '</table>' +
  105. '</figure>'
  106. );
  107. } );
  108. } );
  109. describe( 'view to model', () => {
  110. it( 'should convert table', () => {
  111. editor.setData( '<table><tbody><tr><td>foo</td></tr></tbody></table>' );
  112. expect( getModelData( model, { withoutSelection: true } ) )
  113. .to.equal( '<table><tableRow><tableCell>foo</tableCell></tableRow></table>' );
  114. } );
  115. } );
  116. } );
  117. describe( 'caret movement', () => {
  118. let domEvtDataStub;
  119. beforeEach( () => {
  120. domEvtDataStub = {
  121. keyCode: getCode( 'Tab' ),
  122. preventDefault: sinon.spy(),
  123. stopPropagation: sinon.spy()
  124. };
  125. } );
  126. it( 'should do nothing if not tab pressed', () => {
  127. setModelData( model, modelTable( [
  128. [ '11', '12[]' ]
  129. ] ) );
  130. domEvtDataStub.keyCode = getCode( 'a' );
  131. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  132. sinon.assert.notCalled( domEvtDataStub.preventDefault );
  133. sinon.assert.notCalled( domEvtDataStub.stopPropagation );
  134. expect( formatTable( getModelData( model ) ) ).to.equal( formattedModelTable( [
  135. [ '11', '12[]' ]
  136. ] ) );
  137. } );
  138. it( 'should do nothing if Ctrl+Tab is pressed', () => {
  139. setModelData( model, modelTable( [
  140. [ '11', '12[]' ]
  141. ] ) );
  142. domEvtDataStub.ctrlKey = true;
  143. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  144. sinon.assert.notCalled( domEvtDataStub.preventDefault );
  145. sinon.assert.notCalled( domEvtDataStub.stopPropagation );
  146. expect( formatTable( getModelData( model ) ) ).to.equal( formattedModelTable( [
  147. [ '11', '12[]' ]
  148. ] ) );
  149. } );
  150. describe( 'on TAB', () => {
  151. it( 'should do nothing if selection is not in a table', () => {
  152. setModelData( model, '[]' + modelTable( [
  153. [ '11', '12' ]
  154. ] ) );
  155. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  156. sinon.assert.notCalled( domEvtDataStub.preventDefault );
  157. sinon.assert.notCalled( domEvtDataStub.stopPropagation );
  158. expect( formatTable( getModelData( model ) ) ).to.equal( '[]' + formattedModelTable( [
  159. [ '11', '12' ]
  160. ] ) );
  161. } );
  162. it( 'should move to next cell', () => {
  163. setModelData( model, modelTable( [
  164. [ '11[]', '12' ]
  165. ] ) );
  166. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  167. sinon.assert.calledOnce( domEvtDataStub.preventDefault );
  168. sinon.assert.calledOnce( domEvtDataStub.stopPropagation );
  169. expect( formatTable( getModelData( model ) ) ).to.equal( formattedModelTable( [
  170. [ '11', '[12]' ]
  171. ] ) );
  172. } );
  173. it( 'should create another row and move to first cell in new row', () => {
  174. setModelData( model, modelTable( [
  175. [ '11', '[12]' ]
  176. ] ) );
  177. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  178. expect( formatTable( getModelData( model ) ) ).to.equal( formattedModelTable( [
  179. [ '11', '12' ],
  180. [ '[]', '' ]
  181. ] ) );
  182. } );
  183. it( 'should move to the first cell of next row if on end of a row', () => {
  184. setModelData( model, modelTable( [
  185. [ '11', '12[]' ],
  186. [ '21', '22' ]
  187. ] ) );
  188. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  189. expect( formatTable( getModelData( model ) ) ).to.equal( formattedModelTable( [
  190. [ '11', '12' ],
  191. [ '[21]', '22' ]
  192. ] ) );
  193. } );
  194. describe( 'on table widget selected', () => {
  195. it( 'should move caret to the first table cell on TAB', () => {
  196. const spy = sinon.spy();
  197. editor.editing.view.document.on( 'keydown', spy );
  198. setModelData( model, '[' + modelTable( [
  199. [ '11', '12' ]
  200. ] ) + ']' );
  201. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  202. sinon.assert.calledOnce( domEvtDataStub.preventDefault );
  203. sinon.assert.calledOnce( domEvtDataStub.stopPropagation );
  204. expect( formatTable( getModelData( model ) ) ).to.equal( formattedModelTable( [
  205. [ '[11]', '12' ]
  206. ] ) );
  207. // Should cancel event - so no other tab handler is called.
  208. sinon.assert.notCalled( spy );
  209. } );
  210. it( 'shouldn\' do anything on other blocks', () => {
  211. const spy = sinon.spy();
  212. editor.editing.view.document.on( 'keydown', spy );
  213. setModelData( model, '[<paragraph>foo</paragraph>]' );
  214. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  215. sinon.assert.notCalled( domEvtDataStub.preventDefault );
  216. sinon.assert.notCalled( domEvtDataStub.stopPropagation );
  217. expect( formatTable( getModelData( model ) ) ).to.equal( '[<paragraph>foo</paragraph>]' );
  218. // Should not cancel event.
  219. sinon.assert.calledOnce( spy );
  220. } );
  221. } );
  222. } );
  223. describe( 'on SHIFT+TAB', () => {
  224. beforeEach( () => {
  225. domEvtDataStub.shiftKey = true;
  226. } );
  227. it( 'should do nothing if selection is not in a table', () => {
  228. setModelData( model, '[]' + modelTable( [
  229. [ '11', '12' ]
  230. ] ) );
  231. domEvtDataStub.keyCode = getCode( 'Tab' );
  232. domEvtDataStub.shiftKey = true;
  233. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  234. sinon.assert.notCalled( domEvtDataStub.preventDefault );
  235. sinon.assert.notCalled( domEvtDataStub.stopPropagation );
  236. expect( formatTable( getModelData( model ) ) ).to.equal( '[]' + formattedModelTable( [
  237. [ '11', '12' ]
  238. ] ) );
  239. } );
  240. it( 'should move to previous cell', () => {
  241. setModelData( model, modelTable( [
  242. [ '11', '12[]' ]
  243. ] ) );
  244. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  245. sinon.assert.calledOnce( domEvtDataStub.preventDefault );
  246. sinon.assert.calledOnce( domEvtDataStub.stopPropagation );
  247. expect( formatTable( getModelData( model ) ) ).to.equal( formattedModelTable( [
  248. [ '[11]', '12' ]
  249. ] ) );
  250. } );
  251. it( 'should not move if caret is in first table cell', () => {
  252. setModelData( model, '<paragraph>foo</paragraph>' + modelTable( [
  253. [ '[]11', '12' ]
  254. ] ) );
  255. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  256. expect( formatTable( getModelData( model ) ) ).to.equal(
  257. '<paragraph>foo</paragraph>' + formattedModelTable( [ [ '[]11', '12' ] ] )
  258. );
  259. } );
  260. it( 'should move to the last cell of previous row if on beginning of a row', () => {
  261. setModelData( model, modelTable( [
  262. [ '11', '12' ],
  263. [ '[]21', '22' ]
  264. ] ) );
  265. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  266. expect( formatTable( getModelData( model ) ) ).to.equal( formattedModelTable( [
  267. [ '11', '[12]' ],
  268. [ '21', '22' ]
  269. ] ) );
  270. } );
  271. } );
  272. } );
  273. } );